diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bc8ce7f..7d73687 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -54,6 +54,27 @@ jobs: - name: Test run: cargo test --locked ${{ matrix.features.crate }} ${{ matrix.features.flags }} + # The node also runs on macOS and Windows, which the job above never + # builds on. The whole suite runs on both, default features: hashing, + # decoding and the consensus rules must give the same answers on every + # platform, and file handling, paths and line endings are where they can + # part. Clippy and the smtp build stay on Linux; neither differs by OS. + test-platforms: + name: Test (${{ matrix.os }}) + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [macos-latest, windows-latest] + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@1.93.1 + - uses: Swatinem/rust-cache@v2 + - name: Build + run: cargo build --locked --workspace + - name: Test + run: cargo test --locked --workspace + clippy: name: Clippy runs-on: ubuntu-latest diff --git a/Cargo.lock b/Cargo.lock index 9f14cfc..12360c4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3310,6 +3310,7 @@ dependencies = [ "rustock-trie", "serde", "serde_json", + "sha2", "sha3", "tempfile", "time", @@ -3333,6 +3334,7 @@ dependencies = [ "hex", "hex-literal", "k256", + "num-bigint", "ripemd", "rustock-trie", "secp256k1 0.31.1", @@ -3355,6 +3357,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 d2fc8e7..17e219e 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,25 @@ cargo test --workspace 786 tests covering consensus validation, RLP encoding, P2P handshakes, sync state machines, storage, the Unitrie, EVM execution, every RSK precompile (including the full Bridge surface and REMASC), the transaction pool, RPC methods, transaction relay, and chain reorganizations. +### Starting from Rootstock's published state + +Rootstock publishes signed copies of the mainnet and testnet state for rskj's +`--import`, and `--import` here does what rskj's does: it reads each trusted +signer's index, takes the newest copy that more than half of them sign, checks +those signatures and the copy's SHA-256, loads its blocks and state, and starts +the node from there. + +```bash +rustock --import --data-dir ./data +``` + +The data directory must not hold a chain: where rskj deletes one, rustock +refuses. Mainnet's copy at #9,405,000 downloaded in about two minutes and +loaded in 18 seconds, and the node then caught up with the network in about one. +`--import-url` and `--import-trusted-key` point it at another network's copies; +[`crates/cli/src/bootstrap_import.rs`](crates/cli/src/bootstrap_import.rs) says +what it checks. + ### Importing from rskj A synced rskj database can be imported instead of syncing from peers — about an @@ -150,15 +169,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 ``` @@ -184,11 +202,10 @@ rustock --network-id 30 --allow-chain-overrides \ The overrides reach everything that reads the schedule: header decoding and hashing, difficulty, execution, REMASC and the Bridge. Names the node reads at -one fixed height per chain (`bahamas`, `afterBridgeSync`, `hop401`) are -accepted at that height only; `orchid060` and `tbd1000` are accepted and -change nothing, and the node says so (`tbd1000` only turns on RSKIP-559, -pegout sorting in the Bridge, which it does not implement). A node started -this way must not follow the real network. +one fixed height per chain (`bahamas`, `orchid060`, `hop401`) are accepted at +that height only; `tbd1000` is accepted and changes nothing, and the node says +so (it only turns on RSKIP-559, pegout sorting in the Bridge, which the node +does not implement). A node started this way must not follow the real network. Blocks rskj exported with its `ExportBlocks` tool (`number,hash,td,rlp` per line, plain or gzipped) can then be imported from an empty data directory: @@ -276,6 +293,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/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); 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/Cargo.toml b/crates/cli/Cargo.toml index a1e8e14..36d785f 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -35,10 +35,14 @@ rocksdb = { workspace = true } libc = { workspace = true } alloy-rlp = { workspace = true } flate2 = "1" -# HTTPS client for the external-IP lookup (src/public_ip.rs): rustls with the -# ring provider and Mozilla's root store, not the lettre/smtp stack. +# HTTPS client for the external-IP lookup (src/public_ip.rs) and for --import +# (src/https.rs): rustls with the ring provider and Mozilla's root store, not +# the lettre/smtp stack. --import fetches Rootstock's signed state copies as +# rskj's does, with the signers' index.json and the copy's SHA-256. tokio-rustls = { version = "0.26", default-features = false, features = ["ring", "tls12"] } webpki-roots = "1" +serde_json = "1" +sha2 = { workspace = true } [features] # Enable outgoing email for peg-out alerts (adds the SMTP transport to the @@ -47,7 +51,6 @@ smtp = ["rustock-alerts/smtp"] [dev-dependencies] tempfile = { workspace = true } -serde_json = "1" # For the examples that decode a peg-in's bitcoin transaction straight out of # the call data, without executing the block. `validation_cost` also reaches # through it for `secp256k1`, the rust-bitcoin bindings to the C diff --git a/crates/cli/examples/block_writes.rs b/crates/cli/examples/block_writes.rs index 5dacb48..81ba193 100644 --- a/crates/cli/examples/block_writes.rs +++ b/crates/cli/examples/block_writes.rs @@ -39,11 +39,12 @@ fn main() -> Result<()> { let number: u64 = args.next().ok_or_else(|| anyhow::anyhow!(USAGE))?.parse()?; let network_id: u64 = args.next().map(|a| a.parse()).transpose()?.unwrap_or(support::MAINNET); let trie_dir = args.next(); + // Before the stores open: headers are read by the network's heights. + let hardfork_cfg = support::hardfork_for(network_id); let replay = Replay::open(&data_dir, trie_dir.as_deref())?; replay.ensure_replayable(number)?; - let hardfork_cfg = support::hardfork_for(network_id); eprintln!( "replaying #{}..=#{number} on network-id {network_id} (chain_id {})", replay.exec_number + 1, diff --git a/crates/cli/examples/diff_state.rs b/crates/cli/examples/diff_state.rs index d01de7e..a2fc2c5 100644 --- a/crates/cli/examples/diff_state.rs +++ b/crates/cli/examples/diff_state.rs @@ -47,12 +47,13 @@ fn main() -> Result<()> { let rskj_dir = args.next().ok_or_else(|| anyhow::anyhow!(USAGE))?; let network_id: u64 = args.next().map(|a| a.parse()).transpose()?.unwrap_or(support::MAINNET); let trie_dir = args.next(); + // Before the stores open: headers are read by the network's heights. + let hardfork_cfg = support::hardfork_for(network_id); // --- rustock side: replay to , get the computed post-state trie --- let replay = Replay::open(&data_dir, trie_dir.as_deref())?; replay.ensure_replayable(number)?; - let hardfork_cfg = support::hardfork_for(network_id); eprintln!("replaying on network-id {network_id} (chain_id {})", hardfork_cfg.chain_id); let processor = BlockProcessor::new(hardfork_cfg, replay.store.clone()); diff --git a/crates/cli/examples/loadtest.rs b/crates/cli/examples/loadtest.rs index 05d6827..020ccea 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. @@ -221,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); @@ -774,12 +767,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 rustock_core::validation::merged_mining::compress_coinbase; + use bitcoin::hashes::Hash as _; + use rustock_core::validation::fork_detection::REQUIRED_BLOCKS; + 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 @@ -788,10 +783,23 @@ 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); + // 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)) @@ -877,6 +885,7 @@ fn bench_header(number: u64, parent_hash: B256, state_root: B256, edges: Vec Result<()> { // Default mainnet so existing invocations are unchanged. let network_id: u64 = args.next().map(|a| a.parse()).transpose()?.unwrap_or(support::MAINNET); let trie_dir = args.next(); + // Before the stores open: headers are read by the network's heights. + let hardfork_cfg = support::hardfork_for(network_id); let replay = Replay::open(&data_dir, trie_dir.as_deref())?; replay.ensure_replayable(number)?; - let hardfork_cfg = support::hardfork_for(network_id); eprintln!("replaying on network-id {network_id} (chain_id {})", hardfork_cfg.chain_id); let processor = BlockProcessor::new(hardfork_cfg, replay.store.clone()); diff --git a/crates/cli/examples/snap_holes.rs b/crates/cli/examples/snap_holes.rs new file mode 100644 index 0000000..1cf1c53 --- /dev/null +++ b/crates/cli/examples/snap_holes.rs @@ -0,0 +1,233 @@ +//! 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/cli/examples/support/mod.rs b/crates/cli/examples/support/mod.rs index 539293b..84272db 100644 --- a/crates/cli/examples/support/mod.rs +++ b/crates/cli/examples/support/mod.rs @@ -14,6 +14,7 @@ #![allow(dead_code)] // Each example uses a different subset. use anyhow::{Context, Result}; +use rustock_core::config::ActivationHeights; use rustock_core::Block; use rustock_execution::RskHardforkConfig; use rustock_storage::epoch_store::{EpochConfig, EpochTrieStore}; @@ -26,18 +27,28 @@ use std::sync::Arc; pub const MAINNET: u64 = 30; pub const TESTNET: u64 = 31; -/// The consensus rules to replay under. +/// The consensus rules to replay under, with the upgrade heights headers are +/// read by. /// /// Replaying a testnet block under mainnet rules compares against the wrong /// activation heights, REMASC config and bridge constants. That has already /// cost one investigation, so the selection is explicit rather than defaulted /// inside each tool. +/// +/// The node installs its network's heights before it reads a header, and so +/// does this: a header is read, and hashed, by its own network's heights, and +/// under mainnet's a testnet header from between the two chains' papyrus200, +/// or from testnet's reed810, does not decode at all. The heights can be +/// installed once per process, and the first header read installs mainnet's, +/// so call this before opening the stores. pub fn hardfork_for(network_id: u64) -> RskHardforkConfig { - match network_id { - MAINNET => RskHardforkConfig::mainnet(), - TESTNET => RskHardforkConfig::testnet(), - _ => RskHardforkConfig::regtest(), - } + let (heights, rules) = match network_id { + MAINNET => (ActivationHeights::mainnet(), RskHardforkConfig::mainnet()), + TESTNET => (ActivationHeights::testnet(), RskHardforkConfig::testnet()), + _ => (ActivationHeights::regtest(), RskHardforkConfig::regtest()), + }; + heights.install(); + rules } /// An opened, read-only view of a node's databases, positioned at the 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/cli/examples/verify_range.rs b/crates/cli/examples/verify_range.rs index 57bae24..a361460 100644 --- a/crates/cli/examples/verify_range.rs +++ b/crates/cli/examples/verify_range.rs @@ -353,6 +353,9 @@ fn main() -> Result<()> { let from = *targets.first().expect("non-empty"); let to = *targets.last().expect("non-empty"); let network_id: u64 = option(&options, "--network-id", support::TESTNET)?; + // Before any header is read or built: headers are read by the network's + // heights. + let hardfork_cfg = support::hardfork_for(network_id); let scratch: Option = options.iter().rev().find(|(k, _)| k == "--scratch").map(|(_, v)| v.clone()); // 0 disables the stop, for a run that is deliberately exhaustive. @@ -581,7 +584,6 @@ fn main() -> Result<()> { carried = Some(genesis); } - let hardfork_cfg = support::hardfork_for(network_id); eprintln!( "verifying {} block(s) in #{from}..=#{to} on network-id {network_id} (chain_id {})", targets.len(), diff --git a/crates/cli/examples/verify_stored_headers.rs b/crates/cli/examples/verify_stored_headers.rs new file mode 100644 index 0000000..05b1d8a --- /dev/null +++ b/crates/cli/examples/verify_stored_headers.rs @@ -0,0 +1,357 @@ +//! 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 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. +//! +//! 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; +use rustock_core::config::ChainConfig; +use rustock_core::validation::{fork_detection, merged_mining, HeaderVerifier}; +use rustock_core::Header; +use rustock_storage::BlockStore; +use std::collections::{BTreeMap, VecDeque}; +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; + +/// 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, + blocks: Vec, + 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).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. + 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 = 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 wasabi100 = config.activation_heights.wasabi100; + let verifier = HeaderVerifier::default_rsk(config.clone()); + 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}, chain {}\n head #{}\n verifying #{highest} down to #{lowest} on {threads} threads", + config.chain_id, + 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 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); + // 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<()> { + 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, config, failures, checked, not_later, orphaned, missing) = ( + &store, &verifier, &config, &failures, &checked, ¬_later, &orphaned, &missing, + ); + 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)?.inspect(|h| rehash(h, 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(()); + }; + // 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)? + .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 { + trees.fetch_add(1, Ordering::Relaxed); + } + if let Err(e) = verifier.verify(&header, parent.as_ref()) { + record(failures, header.number, &e); + } + 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()); + } + carry( + &mut window, + header.clone(), + wasabi100, + failures, + fork_checked, + ); + if header.number <= bottom { + 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, 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(()) + })); + } + 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 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}"); + println!( + " fallback-mined, their signatures checked: {}", + fallback.into_inner() + ); + println!( + " 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, + "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/cli/src/bootstrap_import.rs b/crates/cli/src/bootstrap_import.rs new file mode 100644 index 0000000..073f665 --- /dev/null +++ b/crates/cli/src/bootstrap_import.rs @@ -0,0 +1,1484 @@ +//! `--import`: start from the newest state copy Rootstock publishes, the way +//! rskj's `--import` does (`co.rsk.db.importer`, VETIVER-9.0.4). +//! +//! The steps are rskj's, in rskj's order: +//! +//! 1. **The signers' indexes.** Each trusted key has `/index.json`, +//! listing the copies it vouches for: a height, a file, the file's SHA-256 +//! and a signature over that hash (`BootstrapIndexRetriever`). Every index +//! has to be read. +//! 2. **The height.** The highest height at which more than half the keys, and +//! never fewer than two, name the same hash (`BootstrapIndexCandidateSelector`, +//! with the minimum `RskContext.getBootstrapImporter` sets). A key that lists +//! one height twice is an error. +//! 3. **The signatures.** Each of those entries is checked against its key, and +//! as many signatures as that minimum have to hold (`BootstrapDataVerifier`). +//! 4. **The copy.** Downloaded from one of those entries chosen at random, and +//! its SHA-256 checked against the signed hash (`BootstrapFileHandler`). +//! 5. **The load.** `bootstrap-data.bin`, out of the zip, in either of its +//! formats and with every check rskj's reader makes (`BootstrapImporter`): +//! the blocks with their total difficulties, the long values, and the state +//! nodes, each stored under the Keccak-256 that rskj's `TrieStoreImpl` stores +//! it under. +//! +//! Where this differs, it is in what it refuses or how it recovers, never in +//! what it accepts as signed: +//! +//! * rskj deletes the database before importing. This refuses a data directory +//! that already holds a chain instead, so a flag left on a command line +//! cannot wipe a synced node. +//! * A copy that does not download, or does not hash to the signed hash, is +//! fetched from the next signer that lists it, where rskj stops. Every +//! signer's file is the same file: the signatures cover its hash. +//! * A state node whose message is not the one rskj would write for it is +//! refused. rskj would store such a node re-encoded, under the hash of the +//! re-encoding; the copies are written by rskj's own encoder, so none has one. +//! * Beyond rskj's checks, the blocks have to link, each body has to be the +//! one its header names, the highest block has to be at the height the +//! signers named, and its state root has to be among the nodes. Nothing +//! executes those blocks, so these are the only checks they get; everything +//! after them the node executes and checks against the network as usual. +//! +//! The node then starts from the highest block as from any other. The blocks +//! below the copy's lowest are recorded as absent, with the same floor a +//! pruned database has. + +use crate::https::{self, Sink}; +use alloy_primitives::{keccak256, B256, U256}; +use alloy_rlp::Decodable; +use anyhow::{anyhow, bail, ensure, Context, Result}; +use k256::ecdsa::signature::hazmat::PrehashVerifier; +use k256::ecdsa::{Signature, VerifyingKey}; +use rand::seq::SliceRandom; +use rustock_core::config::ChainConfig; +use rustock_core::{Block, Header}; +use rustock_storage::epoch_backend::CF_TRIE; +use rustock_storage::epoch_store::{EpochTrieStore, TrieBackend}; +use rustock_storage::{ + BlockStore, FlatTrieStore, RocksDbTrieStore, Transition, TrieEngine, Validated, +}; +use rustock_trie::{empty_trie_hash, MemoryTrieStore, NodeRef, TrieNode, TrieStore}; +use serde::Deserialize; +use sha2::{Digest, Sha256}; +use std::collections::{BTreeMap, HashMap}; +use std::fs::File; +use std::io::{BufReader, BufWriter, Read, Seek, SeekFrom, Write}; +use std::path::Path; +use std::sync::Arc; +use std::time::{Duration, Instant}; +use tracing::{info, warn}; + +/// rskj's `config/main.conf`, `database.import`. +const MAINNET_URL: &str = "https://import.mainnet.rskcomputing.net/dbs/mainnet/"; +const MAINNET_KEYS: [&str; 3] = [ + "045499f5553ac687c8d196819f69536c0ae585b73e55caa4892fe86067cc1d43bae7edb52ad855a16a030e32cac4fc60b0fd1b0ee5c0230e584b8cde790daaff1a", + "04639c3f22ffafff9fef6e2ae8a0e6b7ecd9751d765d986f05b68fff20f0d041ab2aa093e07ed613c25589f93e00005b1155a2303e8bfe42930e74e2398106f707", + "0479df483b141170c247d4af4e5b92f752bd240ddb57abf021a06c57c7fbeb8a20ed9f35b0f914531a7601f863abd01f0dac722cf9d8fc1137992e18a778b1d8c4", +]; +/// rskj's `config/testnet.conf`, `database.import`. +const TESTNET_URL: &str = "https://import.testnet.rskcomputing.net/dbs/testnet/"; +const TESTNET_KEYS: [&str; 3] = [ + "0437e3ab7b714831d84c75fa05752ef013947a00adfd6e088b75a7d9ff8e55430b10962e8b35a3bf74c9323c502022d64e7960526f68b903a50eb6f6096ebeb7ff", + "04b39e3cf64a3889c41dac45fd42d280787a101786ffd574faa212ab66e0197c9bfd5cf4a36d0c0756cbcfd845ed470059391209e0bea3103a1e65fcea92fa4a7b", + "04c64c11b9d9e68a60bd39f186bc1daa736d1330415fc285dccb0cfcff45dd054bf7701758051141b7718c67b20b4065d8a60a411db70221ff463b5b1cc27e6365", +]; + +const INDEX_NAME: &str = "index.json"; +/// The most an index may hold. Rootstock's run to about 72 KB. +const MAX_INDEX_BYTES: usize = 16 << 20; +/// The file in the zip that holds the copy. +const BOOTSTRAP_ENTRY: &str = "bootstrap-data.bin"; +/// Where the copy is downloaded to, inside the data directory, and removed after. +const DOWNLOAD_DIR: &str = "import"; +/// Present in the data directory while an import has written part of a chain. +const UNFINISHED: &str = "import-unfinished"; +const PROGRESS_EVERY: Duration = Duration::from_secs(15); + +/// rskj's `BootstrapV2Format`. +const V2_MAGIC: &[u8; 8] = b"RSKBOOT\n"; +const V2_VERSION: u8 = 0x02; +const TAG_END: u8 = 0x00; +const TAG_BLOCKS: u8 = 0x01; +const TAG_NODES: u8 = 0x02; +const TAG_VALUES: u8 = 0x03; +/// `MAX_CHUNK_LEN`: the most a v2 chunk may declare. +const MAX_CHUNK_LEN: u64 = 512 << 20; + +/// Trie entries per write. +const BATCH: usize = 50_000; + +/// Where a network's copies are published, and whose signatures they need. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct Signers { + /// Ends in `/`: an index is at `/index.json`, and a copy at + /// ``, as rskj's `BootstrapURLProvider` builds them. + pub url: String, + /// Uncompressed SEC1 public keys, in lowercase hex. + pub keys: Vec, +} + +impl Signers { + /// rskj's `database.import` for the network, with `--import-url` and + /// `--import-trusted-key` in place of its `url` and `trusted-keys`. + pub(crate) fn for_network(network_id: u64, url: Option<&str>, keys: &[String]) -> Result { + let (published_url, published_keys): (Option<&str>, &[&str]) = match network_id { + 30 => (Some(MAINNET_URL), &MAINNET_KEYS), + 31 => (Some(TESTNET_URL), &TESTNET_KEYS), + _ => (None, &[]), + }; + let url = url.or(published_url).context( + "Rootstock publishes copies of mainnet and testnet only; for another network \ + give --import-url and --import-trusted-key", + )?; + let keys: Vec = if keys.is_empty() { + published_keys.iter().map(|k| k.to_string()).collect() + } else { + keys.iter().map(|k| k.trim().to_ascii_lowercase()).collect() + }; + for (i, key) in keys.iter().enumerate() { + let bytes = + hex::decode(key).with_context(|| format!("trusted key {key:?} is not hex"))?; + VerifyingKey::from_sec1_bytes(&bytes) + .map_err(|_| anyhow!("trusted key {key} is not a secp256k1 public key"))?; + ensure!(!keys[..i].contains(key), "trusted key {key} is given twice"); + } + ensure!( + keys.len() >= 2, + "{} trusted key: a copy has to be signed by at least two, as rskj requires", + keys.len() + ); + let mut url = url.to_string(); + if !url.ends_with('/') { + url.push('/'); + } + https::parse_url(&url)?; + Ok(Self { url, keys }) + } + + /// How many have to agree, and sign: rskj's `keys / 2 + 1`, raised to two. + pub(crate) fn threshold(&self) -> usize { + (self.keys.len() / 2 + 1).max(2) + } +} + +/// A signer's `index.json` (rskj's `BootstrapDataIndex`). +#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +pub(crate) struct Index { + pub dbs: Vec, +} + +/// One copy a signer lists (rskj's `BootstrapDataEntry`). Its `date` is +/// not read, by rskj or here. +#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +pub(crate) struct Entry { + pub height: u64, + /// The file, relative to the publishing URL. + pub db: String, + /// The file's SHA-256, in hex: what the signature covers. + pub hash: String, + pub sig: EntrySignature, +} + +/// rskj's `BootstrapDataSignature`: secp256k1's `r` and `s`, in hex. +#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +pub(crate) struct EntrySignature { + pub r: String, + pub s: String, +} + +/// The height chosen, and the entries at it that name the agreed hash, each +/// with its signer's position among the trusted keys. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct Candidate { + pub height: u64, + pub entries: Vec<(usize, Entry)>, +} + +/// rskj's `BootstrapIndexCandidateSelector.getHeightData`: the highest height +/// at which `threshold` signers name the same hash. `indexes[i]` is the index +/// `keys[i]` published. +pub(crate) fn select(indexes: &[Index], keys: &[String], threshold: usize) -> Result { + let mut per_height: BTreeMap> = BTreeMap::new(); + for (signer, (index, key)) in indexes.iter().zip(keys).enumerate() { + for entry in &index.dbs { + if per_height + .entry(entry.height) + .or_default() + .insert(signer, entry) + .is_some() + { + bail!( + "There is an invalid file from {key}: it has 2 entries from same height {}", + entry.height + ); + } + } + } + ensure!( + !per_height.is_empty(), + "Downloaded files contain no height entries" + ); + for (&height, entries) in per_height.iter().rev() { + let mut counts: BTreeMap<&str, usize> = BTreeMap::new(); + for entry in entries.values() { + *counts.entry(entry.hash.as_str()).or_default() += 1; + } + // More than half the keys have to agree, so no two hashes can both reach it. + let Some((&hash, &count)) = counts.iter().max_by_key(|(_, n)| **n) else { + continue; + }; + if count >= threshold { + let entries = entries + .iter() + .filter(|(_, e)| e.hash == hash) + .map(|(&signer, &e)| (signer, e.clone())) + .collect(); + return Ok(Candidate { height, entries }); + } + } + bail!("Downloaded files doesn't contain enough entries for a common height") +} + +/// rskj's `BootstrapDataVerifier.verifyEntries`: how many of the candidate's +/// entries are signed by their signer. +pub(crate) fn verify(candidate: &Candidate, keys: &[String]) -> Result { + let Some((_, first)) = candidate.entries.first() else { + return Ok(0); + }; + let hash = hex_bytes(&first.hash).context("the copy's hash")?; + let mut valid = 0; + for (signer, entry) in &candidate.entries { + ensure!( + entry.hash == first.hash, + "Error trying to verify different hashes: {} vs {}", + first.hash, + entry.hash + ); + let key = &keys[*signer]; + let key = VerifyingKey::from_sec1_bytes(&hex_bytes(key)?) + .map_err(|_| anyhow!("trusted key {key} is not a secp256k1 public key"))?; + let r = hex_bytes(&entry.sig.r).context("a signature's r")?; + let s = hex_bytes(&entry.sig.s).context("a signature's s")?; + if signs(&key, &hash, &r, &s) { + valid += 1; + } + } + Ok(valid) +} + +/// rskj reads every hex string with BouncyCastle's `Hex.decode`, which fails +/// on anything that is not hex. +fn hex_bytes(s: &str) -> Result> { + hex::decode(s).with_context(|| format!("{s:?} is not hex")) +} + +/// rskj's `Secp256k1.verify`, on BouncyCastle's `ECDSASigner`: `r` and `s` +/// are unsigned integers of any length, valid from 1 to the group order. +fn signs(key: &VerifyingKey, hash: &[u8], r: &[u8], s: &[u8]) -> bool { + let (Some(r), Some(s)) = (scalar(r), scalar(s)) else { + return false; + }; + let Ok(signature) = Signature::from_scalars(r, s) else { + return false; + }; + // BouncyCastle takes an `s` from either half of the range, and two of + // the three testnet signers use the upper half. k256 takes only the + // lower, and `n - s` verifies exactly when `s` does. + let signature = signature.normalize_s().unwrap_or(signature); + key.verify_prehash(hash, &signature).is_ok() +} + +/// `new BigInteger(1, bytes)` as 32 bytes, or `None` past 2^256. +fn scalar(bytes: &[u8]) -> Option { + let start = bytes.iter().position(|b| *b != 0).unwrap_or(bytes.len()); + let digits = &bytes[start..]; + if digits.len() > 32 { + return None; + } + let mut out = k256::FieldBytes::default(); + out[32 - digits.len()..].copy_from_slice(digits); + Some(out) +} + +/// Fetches over HTTPS; the tests stand in for it. +#[async_trait::async_trait] +pub(crate) trait Source: Send + Sync { + async fn get(&self, url: &str, sink: &mut dyn Sink) -> Result; +} + +pub(crate) struct Web; + +#[async_trait::async_trait] +impl Source for Web { + async fn get(&self, url: &str, sink: &mut dyn Sink) -> Result { + https::get(url, sink).await + } +} + +/// A body kept in memory, up to a limit. +struct Capped { + body: Vec, + limit: usize, +} + +impl Sink for Capped { + fn write(&mut self, data: &[u8]) -> Result<()> { + ensure!( + self.body.len() + data.len() <= self.limit, + "more than {} bytes", + self.limit + ); + self.body.extend_from_slice(data); + Ok(()) + } +} + +async fn fetch_index(source: &dyn Source, url: &str) -> Result { + let mut index = Capped { + body: Vec::new(), + limit: MAX_INDEX_BYTES, + }; + source.get(url, &mut index).await?; + serde_json::from_slice(&index.body).context("reading the index") +} + +/// A download on its way to disk, hashed as it goes. +struct Download { + file: BufWriter, + sha: Sha256, + bytes: u64, + started: Instant, + said: Instant, +} + +impl Sink for Download { + fn write(&mut self, data: &[u8]) -> Result<()> { + self.file + .write_all(data) + .context("writing the copy to disk")?; + self.sha.update(data); + self.bytes += data.len() as u64; + if self.said.elapsed() >= PROGRESS_EVERY { + self.said = Instant::now(); + let secs = self.started.elapsed().as_secs_f64(); + info!( + "Import: {} MB downloaded ({:.1} MB/s)", + self.bytes >> 20, + self.bytes as f64 / (1 << 20) as f64 / secs + ); + } + Ok(()) + } +} + +/// rskj's `BootstrapFileHandler.retrieveAndUnpack`, up to the unpacking: the +/// copy from one of the candidate's entries chosen at random, kept only if +/// it hashes to the signed hash. Where rskj stops at the first failure, the +/// next entry is tried. +async fn download( + source: &dyn Source, + url: &str, + candidate: &Candidate, + path: &Path, +) -> Result<()> { + let mut order: Vec<&Entry> = candidate.entries.iter().map(|(_, e)| e).collect(); + order.shuffle(&mut rand::thread_rng()); + let mut failed = Vec::new(); + for entry in order { + let from = format!("{url}{}", entry.db); + match fetch_copy(source, &from, &entry.hash, path).await { + Ok(()) => return Ok(()), + Err(e) => { + warn!("Import: the copy at {from} cannot be used: {e:#}"); + failed.push(format!("{from}: {e:#}")); + } + } + } + bail!("no signer's copy could be used:\n {}", failed.join("\n ")) +} + +async fn fetch_copy(source: &dyn Source, url: &str, hash: &str, path: &Path) -> Result<()> { + let want = hex_bytes(hash)?; + let file = File::create(path).with_context(|| format!("creating {}", path.display()))?; + let now = Instant::now(); + let mut copy = Download { + file: BufWriter::with_capacity(1 << 20, file), + sha: Sha256::new(), + bytes: 0, + started: now, + said: now, + }; + info!("Import: downloading {url}"); + source.get(url, &mut copy).await?; + copy.file.flush().context("writing the copy to disk")?; + ensure!( + copy.sha.finalize()[..] == want[..], + "File: {} does not match with expected hash: {hash}", + path.display() + ); + info!( + "Import: {} MB downloaded in {:.0} s, and its SHA-256 is the signed one", + copy.bytes >> 20, + copy.started.elapsed().as_secs_f64() + ); + Ok(()) +} + +/// Refuses `--import` into a data directory that holds a chain: rskj deletes +/// the database first, which a flag left on a command line would turn into +/// the loss of a synced node. A store that never got past genesis holds +/// nothing to lose, and one an import left unfinished is redone. +pub(crate) fn refuse_held_chain(store: &BlockStore, data_dir: &Path) -> Result<()> { + if data_dir.join(UNFINISHED).exists() { + return Ok(()); + } + let head = match store.head()? { + Some(hash) => store.header(hash)?.map_or(0, |h| h.number), + None => 0, + }; + ensure!( + head == 0 && store.exec_head()?.is_none(), + "{} already holds a chain, up to #{head}. --import loads a copy into a data \ + directory that holds none; start without it to run on this chain, or give an \ + empty --data-dir", + data_dir.display() + ); + Ok(()) +} + +/// Refuses to start on a chain an import left half-written. +pub(crate) fn refuse_unfinished(data_dir: &Path) -> Result<()> { + ensure!( + !data_dir.join(UNFINISHED).exists(), + "an --import into {} did not finish, so it holds part of a chain. Run it again \ + with --import, or start from an empty --data-dir", + data_dir.display() + ); + Ok(()) +} + +/// What an import put in the store. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct Summary { + pub lowest: u64, + pub highest: u64, + pub hash: B256, + pub state_root: B256, + pub nodes: u64, + pub values: u64, +} + +/// Everything `--import` does, from the signers' indexes to the node's new +/// position (rskj's `BootstrapImporter.importData`). +pub(crate) async fn import( + source: &dyn Source, + signers: &Signers, + data_dir: &Path, + store: &BlockStore, + trie: &TrieTarget, + chain: &ChainConfig, +) -> Result { + let started = Instant::now(); + let mut indexes = Vec::with_capacity(signers.keys.len()); + for key in &signers.keys { + let url = format!("{}{key}/{INDEX_NAME}", signers.url); + let index = fetch_index(source, &url) + .await + .with_context(|| format!("Failed to download and parse index from {url}"))?; + indexes.push(index); + } + let threshold = signers.threshold(); + let candidate = select(&indexes, &signers.keys, threshold)?; + let valid = verify(&candidate, &signers.keys)?; + ensure!( + valid >= threshold, + "Not enough valid signatures: selected height {} doesn't have enough trustworthy \ + sources: {valid} of {threshold}", + candidate.height + ); + info!( + "Import: Rootstock's copy at #{} ({}), signed by {valid} of the {} trusted keys", + candidate.height, + candidate.entries[0].1.hash, + signers.keys.len() + ); + + let dir = data_dir.join(DOWNLOAD_DIR); + std::fs::create_dir_all(&dir).with_context(|| format!("creating {}", dir.display()))?; + let zip = dir.join(format!("{}.zip", candidate.height)); + let loaded = async { + download(source, &signers.url, &candidate, &zip).await?; + tokio::task::block_in_place(|| load(&zip, candidate.height, data_dir, store, trie, chain)) + } + .await; + // Spent either way: a failed load starts again from the indexes. + let _ = std::fs::remove_file(&zip); + let _ = std::fs::remove_dir(&dir); + let summary = loaded?; + info!( + "Import: the node starts at #{} {:?}, {:.0} s after the first index was asked for", + summary.highest, + summary.hash, + started.elapsed().as_secs_f64() + ); + Ok(summary) +} + +/// rskj's `BootstrapImporter.updateDatabase`, from the downloaded zip: +/// `bootstrap-data.bin` into the store, then the node's position at its +/// highest block. +pub(crate) fn load( + zip: &Path, + height: u64, + data_dir: &Path, + store: &BlockStore, + trie: &TrieTarget, + chain: &ChainConfig, +) -> Result { + let started = Instant::now(); + let mut file = File::open(zip).with_context(|| format!("opening {}", zip.display()))?; + let entries = zip_entries(&mut file).context("The file is corrupted or incomplete")?; + let entry = bootstrap_entry(&entries)?.clone(); + let reader = open_entry(file, &entry)?; + + // From the first write until the position is recorded, the store holds + // part of a chain; this says so to the next start. + let unfinished = data_dir.join(UNFINISHED); + File::create(&unfinished).with_context(|| format!("creating {}", unfinished.display()))?; + + let mut loader = Loader::new(store, trie); + let format = read_bootstrap(reader, entry.size, &mut loader)?; + info!( + "Import: bootstrap-data {format} read: {} blocks, {} long values and {} state nodes \ + in {:.1} s", + loader.blocks.len(), + loader.values_read, + loader.nodes, + started.elapsed().as_secs_f64() + ); + let summary = loader.finish(height, chain)?; + std::fs::remove_file(&unfinished) + .with_context(|| format!("removing {}", unfinished.display()))?; + info!( + "Import: loaded in {:.1} s: blocks #{}..#{}, state root {:?}", + started.elapsed().as_secs_f64(), + summary.lowest, + summary.highest, + summary.state_root + ); + Ok(summary) +} + +/// Where the state goes: the store the node will run on, as `--trie-backend` +/// and `--trie-engine` choose it. +pub(crate) enum TrieTarget { + /// `trie_nodes` in the node's own database: `--trie-backend single`. + Column(Arc), + /// `--trie-backend external` on rocksdb. + Rocks(RocksDbTrieStore), + /// `--trie-backend external` on flatdb. + Flat(FlatTrieStore), + /// `--trie-backend epoch`: one seed epoch, which the node closes at the + /// height it resumes from when it opens the store. + Epochs(Box), +} + +impl TrieTarget { + pub(crate) fn open( + backend: &TrieBackend, + engine: TrieEngine, + expected_keys: u64, + trie_dir: &str, + store: &BlockStore, + ) -> Result { + let dir = Path::new(trie_dir); + Ok(match backend { + TrieBackend::Single => Self::Column(store.db().clone()), + TrieBackend::External => { + engine.check_matches(dir)?; + match engine { + TrieEngine::RocksDb => Self::Rocks(RocksDbTrieStore::open(dir)?), + TrieEngine::FlatDb => Self::Flat(FlatTrieStore::open( + dir, + rustock_flatdb::Options { + expected_keys, + ..rustock_flatdb::Options::default() + }, + )?), + } + } + TrieBackend::Epochs(config) => { + engine.check_matches(dir)?; + // No resume point: the node gives it when it opens the store. + Self::Epochs(Box::new(EpochTrieStore::open_with( + dir, + config.clone(), + None, + engine.factory(expected_keys), + )?)) + } + }) + } + + fn put(&self, entries: &[(B256, &[u8])]) -> Result<()> { + if entries.is_empty() { + return Ok(()); + } + if let Self::Column(db) = self { + let cf = db + .cf_handle(CF_TRIE) + .context("the database has no trie_nodes column family")?; + let mut batch = rocksdb::WriteBatch::default(); + for (key, value) in entries { + batch.put_cf(cf, key.as_slice(), value); + } + return db.write(batch).context("writing state to the database"); + } + let owned: Vec<(Vec, Vec)> = entries + .iter() + .map(|(k, v)| (k.to_vec(), v.to_vec())) + .collect(); + match self { + Self::Rocks(s) => s.put_many(&owned), + Self::Flat(s) => s.put_many(&owned), + Self::Epochs(s) => s.put_many(&owned), + Self::Column(_) => {} + } + self.healthy() + } + + /// The stores that cannot return an error from a write say afterwards. + fn healthy(&self) -> Result<()> { + let failed = match self { + Self::Rocks(s) => s.write_failed(), + Self::Flat(s) => s.write_failed(), + Self::Column(_) | Self::Epochs(_) => false, + }; + ensure!( + !failed, + "a write to the trie store failed; the log says why" + ); + Ok(()) + } + + /// Durable before the node's position names the state. + fn finish(&self) -> Result<()> { + match self { + // The database's own log; synced with the position. + Self::Column(_) => Ok(()), + Self::Rocks(s) => { + s.flush(); + self.healthy() + } + Self::Flat(s) => { + s.flush(); + self.healthy() + } + Self::Epochs(s) => EpochTrieStore::flush(s), + } + } + + fn holds(&self, key: &B256) -> Result { + Ok(match self { + Self::Column(db) => { + let cf = db + .cf_handle(CF_TRIE) + .context("no trie_nodes column family")?; + db.get_cf(cf, key.as_slice())?.is_some() + } + Self::Rocks(s) => s.get(key.as_slice()).is_some(), + Self::Flat(s) => s.get(key.as_slice()).is_some(), + Self::Epochs(s) => s.get(key.as_slice()).is_some(), + }) + } +} + +/// What has been read from the copy so far, and where it went. +struct Loader<'a> { + store: &'a BlockStore, + trie: &'a TrieTarget, + threads: usize, + blocks: BTreeMap, + /// Each long value's hash and length, which the nodes are checked against. + values: HashMap, + /// The long values read, as rskj counts them: one copy can hold a value + /// more than once. + values_read: u64, + nodes: u64, +} + +impl<'a> Loader<'a> { + fn new(store: &'a BlockStore, trie: &'a TrieTarget) -> Self { + let threads = std::thread::available_parallelism() + .map_or(4, |n| n.get()) + .min(16); + Self { + store, + trie, + threads, + blocks: BTreeMap::new(), + values: HashMap::new(), + values_read: 0, + nodes: 0, + } + } + + /// rskj's `saveBlockFromTuple`: `[block, totalDifficulty]`. + fn block(&mut self, tuple: &[u8]) -> Result<()> { + let parts = decode_list(tuple)?; + ensure!(parts.len() >= 2, "a block entry of {} item(s)", parts.len()); + let encoded = non_empty(data(parts[0])?, "block data is missing")?; + let td = total_difficulty(non_empty( + data(parts[1])?, + "block difficulty data is missing", + )?)?; + let mut rest = encoded; + let block = Block::decode(&mut rest).context("decoding a block")?; + let number = block.header.number; + ensure!( + rest.is_empty(), + "{} bytes after block #{number}", + rest.len() + ); + check_body(&block)?; + ensure!( + !self.blocks.contains_key(&number), + "the copy holds two blocks at #{number}" + ); + let hash = block.header.hash(); + self.store.put_header_with_hash(hash, &block.header)?; + self.store + .put_body(hash, &block.transactions, &block.ommers)?; + self.store.put_total_difficulty(hash, td)?; + self.blocks.insert(number, (hash, block.header, td)); + Ok(()) + } + + /// rskj's `TrieStore.saveValue`: each long value under its Keccak-256. + fn values(&mut self, elements: &[&[u8]]) -> Result<()> { + let mut batch = Vec::with_capacity(elements.len().min(BATCH)); + for element in elements { + let value = non_empty(data(element)?, "the values section holds an empty element")?; + let key = keccak256(value); + self.values.insert(key, value.len()); + self.values_read += 1; + batch.push((key, value)); + if batch.len() == BATCH { + self.trie.put(&batch)?; + batch.clear(); + } + } + self.trie.put(&batch) + } + + /// rskj's `Trie.fromMessage` and `TrieStore.save` for each node. + fn nodes(&mut self, elements: &[&[u8]]) -> Result<()> { + for part in elements.chunks(BATCH) { + let checked = check_nodes(part, &self.values, self.threads)?; + self.trie.put(&checked)?; + self.nodes += checked.len() as u64; + } + Ok(()) + } + + /// One v2 chunk: whole RLP elements, all of one section. Whatever fails in + /// it is reported as rskj's `importElements` reports it, with the section. + fn chunk(&mut self, tag: u8, chunk: &[u8]) -> Result<()> { + let section = match tag { + TAG_BLOCKS => "blocks", + TAG_VALUES => "values", + _ => "nodes", + }; + let malformed = || format!("Bootstrap-data v2 {section} section is malformed or corrupt"); + let elements = items(chunk).with_context(malformed)?; + match tag { + TAG_BLOCKS => elements.iter().try_for_each(|element| { + self.block(non_empty( + data(element)?, + "the blocks section holds an empty element", + )?) + }), + TAG_VALUES => self.values(&elements), + _ => self.nodes(&elements), + } + .with_context(malformed) + } + + /// The node's position at the copy's highest block, once everything in + /// the copy is stored. + fn finish(self, height: u64, chain: &ChainConfig) -> Result { + let (&lowest, _) = self + .blocks + .first_key_value() + .context("Bootstrap-data has no blocks; refusing to import incomplete data")?; + let (&highest, (tip_hash, tip, _)) = self.blocks.last_key_value().context("no blocks")?; + ensure!( + highest == height, + "the copy's highest block is #{highest}, where its signers list it as the copy \ + at #{height}" + ); + self.trie.finish()?; + ensure!( + self.trie.holds(&tip.state_root)?, + "the state root of #{highest}, {:?}, is not among the copy's state nodes", + tip.state_root + ); + + // Genesis, as the node's own start writes it, without moving the head + // to it: that start finds it and leaves the position alone. + let genesis = chain.genesis_header(); + let genesis_hash = chain.known_genesis_hash().unwrap_or_else(|| genesis.hash()); + match self.blocks.get(&0) { + Some((hash, _, _)) => ensure!( + *hash == genesis_hash, + "the copy's block #0 is {hash:?}, not this network's genesis {genesis_hash:?}" + ), + None => { + self.store.put_header_with_hash(genesis_hash, &genesis)?; + self.store + .put_total_difficulty(genesis_hash, genesis.difficulty)?; + } + } + self.store.put_canonical_hash(0, genesis_hash)?; + // Nothing below the copy's lowest block is held: the floor a pruned + // database records, which everything that walks the chain stops at. + if lowest > 0 { + let (hash, _, td) = &self.blocks[&lowest]; + self.store.set_history_floor(lowest, *hash, *td)?; + } + + // Adopting the tip proves that every block down to the floor is held + // and names the one below as its parent. + let head = Validated::prove(self.store, *tip_hash) + .map_err(|e| anyhow!("the copy's blocks do not form one chain: {e}"))?; + self.store.apply(&Transition::Adopt { head })?; + let at = Validated::prove(self.store, *tip_hash) + .map_err(|e| anyhow!("the copy's blocks do not form one chain: {e}"))?; + self.store.apply(&Transition::Executed { + at, + state_root: tip.state_root, + })?; + // Into the tables, so the log the load wrote can go: the node's next + // start then has none of it to replay. + self.store.flush_all()?; + + Ok(Summary { + lowest, + highest, + hash: *tip_hash, + state_root: tip.state_root, + nodes: self.nodes, + values: self.values_read, + }) + } +} + +/// Every node checked on as many threads as there are cores, then keyed for +/// the store in the copy's order. +fn check_nodes<'a>( + elements: &[&'a [u8]], + values: &HashMap, + threads: usize, +) -> Result> { + let per_thread = elements.len().div_ceil(threads.max(1)).max(1); + std::thread::scope(|scope| { + let workers: Vec<_> = elements + .chunks(per_thread) + .map(|part| { + scope.spawn(move || { + let none = MemoryTrieStore::new(); + part.iter() + .map(|e| check_node(e, values, &none)) + .collect::>>() + }) + }) + .collect(); + let mut out = Vec::with_capacity(elements.len()); + for worker in workers { + out.extend( + worker + .join() + .map_err(|_| anyhow!("checking state nodes panicked"))??, + ); + } + Ok(out) + }) +} + +/// rskj's `Trie.fromMessage`, then the key `TrieStoreImpl.save` gives it. +fn check_node<'a>( + element: &'a [u8], + values: &HashMap, + none: &MemoryTrieStore, +) -> Result<(B256, &'a [u8])> { + let message = non_empty(data(element)?, "the nodes section holds an empty element")?; + let node = TrieNode::try_from_message(message, none) + .context("Bootstrap-data nodes section holds a node that does not parse")?; + ensure!( + node.to_message(none) == message, + "Bootstrap-data nodes section holds a node not in the form rskj writes it" + ); + long_values_held(&node, values)?; + let key = if node.is_empty_trie() { + empty_trie_hash() + } else { + keccak256(message) + }; + Ok((key, message)) +} + +/// rskj saves a node's long value with it, and those of the nodes embedded +/// in it, reading each from the values already stored: one that is missing, +/// or of another length, fails the import (`BootstrapImporter.saveNode`). +fn long_values_held(node: &TrieNode, values: &HashMap) -> Result<()> { + if node.has_long_value() { + let hash = node.value_hash.context("a long value without its hash")?; + ensure!( + values.get(&hash) == Some(&node.value_length()), + "Failed to save a state node during bootstrap import: a referenced long value is \ + missing or inconsistent in the values section (incomplete or corrupt bootstrap data)" + ); + } + for child in [&node.left, &node.right] { + if let NodeRef::Node(child) = child { + long_values_held(child, values)?; + } + } + Ok(()) +} + +/// A body has to be the one its header names, as snapshot sync checks the +/// blocks it fetches below its state: nothing executes these blocks, so +/// nothing else would. +fn check_body(block: &Block) -> Result<()> { + let number = block.header.number; + let root = block.header.transactions_root; + ensure!( + rustock_core::ordered_tx_trie_root(&block.transactions, true) == root + || rustock_core::ordered_tx_trie_root(&block.transactions, false) == root, + "block #{number}: its transactions are not the ones its header names" + ); + ensure!( + rustock_execution::processor::compute_ommers_hash(&block.ommers) + == block.header.ommers_hash, + "block #{number}: its uncles are not the ones its header names" + ); + Ok(()) +} + +/// rskj's `new BlockDifficulty(new BigInteger(bytes))`: two's complement, so +/// a set top bit is a negative difficulty, which it refuses. +fn total_difficulty(bytes: &[u8]) -> Result { + ensure!( + bytes.first().is_none_or(|b| b & 0x80 == 0), + "A block difficulty must be positive or zero" + ); + let start = bytes.iter().position(|b| *b != 0).unwrap_or(bytes.len()); + let digits = &bytes[start..]; + ensure!( + digits.len() <= 32, + "a total difficulty of {} bytes", + digits.len() + ); + Ok(U256::from_be_slice(digits)) +} + +/// rskj's `RLPElement.getRLPData()` is `null` for an empty item, which every +/// reader of the copy refuses. +fn non_empty<'b>(bytes: &'b [u8], what: &str) -> Result<&'b [u8]> { + ensure!(!bytes.is_empty(), "{what}; the file is corrupt"); + Ok(bytes) +} + +/// One RLP item at the front of `buf`: whether it is a list, its payload, and +/// what follows it. Lenient about the form of a length, as rskj's reader is. +fn item(buf: &[u8]) -> Result<(bool, &[u8], &[u8])> { + let first = *buf + .first() + .context("RLP: expected an item, found the end")?; + let (list, start, len) = match first { + 0x00..=0x7f => (false, 0, 1), + 0x80..=0xb7 => (false, 1, usize::from(first - 0x80)), + 0xb8..=0xbf => { + let n = usize::from(first - 0xb7); + ( + false, + 1 + n, + be_len(buf.get(1..1 + n).context("RLP: truncated length")?)?, + ) + } + 0xc0..=0xf7 => (true, 1, usize::from(first - 0xc0)), + _ => { + let n = usize::from(first - 0xf7); + ( + true, + 1 + n, + be_len(buf.get(1..1 + n).context("RLP: truncated length")?)?, + ) + } + }; + let end = start + .checked_add(len) + .context("RLP: a length that overflows")?; + ensure!(end <= buf.len(), "RLP: an item runs past its container"); + Ok((list, &buf[start..end], &buf[end..])) +} + +fn be_len(bytes: &[u8]) -> Result { + ensure!(bytes.len() <= 4, "RLP: a length of {} bytes", bytes.len()); + Ok(bytes + .iter() + .fold(0usize, |acc, b| (acc << 8) | usize::from(*b))) +} + +/// The items `payload` holds, each as its whole encoding: rskj's `RLP.decode2`. +fn items(mut payload: &[u8]) -> Result> { + let mut out = Vec::new(); + while !payload.is_empty() { + let (_, _, rest) = item(payload)?; + out.push(&payload[..payload.len() - rest.len()]); + payload = rest; + } + Ok(out) +} + +/// rskj's `RLPElement.getRLPData()`: a string's payload, or a list's whole +/// encoding. +fn data(encoded: &[u8]) -> Result<&[u8]> { + let (is_list, payload, _) = item(encoded)?; + Ok(if is_list { encoded } else { payload }) +} + +/// rskj's `RLP.decodeList(bytes)`: `bytes` is exactly one list, whose items +/// these are. +fn decode_list(bytes: &[u8]) -> Result> { + let (is_list, payload, rest) = item(bytes)?; + ensure!(is_list, "RLP: expected a list"); + ensure!(rest.is_empty(), "RLP: {} bytes after a list", rest.len()); + items(payload) +} + +/// A reader that counts what it has given out. +struct Counted { + inner: R, + read: u64, +} + +impl Read for Counted { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + let n = self.inner.read(buf)?; + self.read += n as u64; + Ok(n) + } +} + +fn read_byte(input: &mut impl Read) -> Result> { + let mut byte = [0u8; 1]; + loop { + match input.read(&mut byte) { + Ok(0) => return Ok(None), + Ok(_) => return Ok(Some(byte[0])), + Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue, + Err(e) => return Err(e.into()), + } + } +} + +/// rskj's `BootstrapImporter.isV2`, then the reader for the format found. +/// `size` is what the zip says the file holds. +fn read_bootstrap(entry: impl Read, size: u64, loader: &mut Loader) -> Result<&'static str> { + let mut input = Counted { + inner: BufReader::with_capacity(1 << 20, entry), + read: 0, + }; + let first = read_byte(&mut input)?.context("Empty bootstrap data file")?; + if first == V2_MAGIC[0] { + let mut magic = [0u8; 7]; + input + .read_exact(&mut magic) + .context("Invalid bootstrap-data v2 magic")?; + ensure!(magic == V2_MAGIC[1..], "Invalid bootstrap-data v2 magic"); + info!("Import: bootstrap-data v2 (chunked)"); + read_v2(&mut input, size, loader)?; + Ok("v2") + } else if first >= 0xc0 { + info!("Import: bootstrap-data v1 (legacy)"); + let mut all = Vec::with_capacity(usize::try_from(size).unwrap_or(0)); + all.push(first); + input.read_to_end(&mut all)?; + read_v1(&all, loader)?; + Ok("v1") + } else { + bail!( + "Unrecognized bootstrap-data format: first byte 0x{first:02X} is neither the v2 \ + magic ('R') nor a v1 RLP list prefix (0xc0+)" + ) + } +} + +/// rskj's `updateDatabaseV1`: one RLP list, +/// `[blocks, [nodes, values]]`, read whole. +fn read_v1(all: &[u8], loader: &mut Loader) -> Result<()> { + let top = decode_list(all)?; + ensure!( + top.len() >= 2, + "a v1 copy of {} part(s), not blocks and state", + top.len() + ); + for entry in decode_list(data(top[0])?)? { + loader.block(non_empty(data(entry)?, "block data is missing")?)?; + } + let state = decode_list(data(top[1])?)?; + ensure!( + state.len() >= 2, + "a v1 state of {} list(s), not nodes and values", + state.len() + ); + // The values first: the nodes are checked against them. + loader.values(&decode_list(data(state[1])?)?)?; + loader.nodes(&decode_list(data(state[0])?)?) +} + +/// rskj's `updateDatabaseV2`: sections of length-prefixed chunks, read a +/// chunk at a time, with every structural check its `scanSections` makes. +fn read_v2(input: &mut Counted, size: u64, loader: &mut Loader) -> Result<()> { + let version = read_byte(input)?.context("the copy ends inside its header")?; + ensure!( + version == V2_VERSION, + "Unsupported bootstrap-data v2 version: {version}" + ); + let mut values_seen = false; + loop { + let tag = read_byte(input)? + .context("Truncated bootstrap-data v2: missing end-of-sections marker")?; + if tag == TAG_END { + break; + } + if tag == TAG_VALUES { + values_seen = true; + } + ensure!( + tag != TAG_NODES || values_seen, + "Bootstrap-data v2 sections are out of order: the nodes section appears before the \ + values section, but long values must be co-located before the nodes that reference them" + ); + loop { + let mut len = [0u8; 8]; + input + .read_exact(&mut len) + .context("the copy ends inside a section")?; + let len = u64::from_be_bytes(len); + if len == 0 { + break; + } + ensure!( + len <= MAX_CHUNK_LEN, + "Bootstrap-data v2 chunk length out of range: {len}" + ); + let remaining = size.saturating_sub(input.read); + ensure!( + len <= remaining, + "Bootstrap-data v2 chunk length {len} exceeds the {remaining} bytes remaining in the \ + file; the file is truncated or its length field is corrupt" + ); + if matches!(tag, TAG_BLOCKS | TAG_VALUES | TAG_NODES) { + let mut chunk = vec![0u8; usize::try_from(len)?]; + input + .read_exact(&mut chunk) + .context("the copy ends inside a chunk")?; + loader.chunk(tag, &chunk)?; + } else { + // A section this reader does not know, as rskj skips one. + let skipped = std::io::copy(&mut (&mut *input).take(len), &mut std::io::sink())?; + ensure!(skipped == len, "the copy ends inside a chunk"); + } + } + } + ensure!( + read_byte(input)?.is_none(), + "Bootstrap-data v2 has trailing bytes after the end-of-sections marker; the file is \ + corrupt or not canonical" + ); + ensure!( + !loader.blocks.is_empty(), + "Bootstrap-data v2 has no blocks section (or it is empty); refusing to import incomplete data" + ); + ensure!( + loader.nodes > 0, + "Bootstrap-data v2 has no state-nodes section (or it is empty); refusing to import a \ + stateless snapshot" + ); + Ok(()) +} + +/// One entry of a zip's central directory. +#[derive(Debug, Clone, PartialEq, Eq)] +struct ZipEntry { + name: String, + flags: u16, + method: u16, + crc: u32, + compressed: u64, + size: u64, + offset: u64, +} + +const EOCD_SIG: u32 = 0x0605_4b50; +const ZIP64_LOCATOR_SIG: u32 = 0x0706_4b50; +const ZIP64_EOCD_SIG: u32 = 0x0606_4b50; +const CENTRAL_SIG: u32 = 0x0201_4b50; +const LOCAL_SIG: u32 = 0x0403_4b50; + +/// Little-endian fields, bounds-checked. +struct Fields<'b> { + bytes: &'b [u8], + at: usize, +} + +impl<'b> Fields<'b> { + fn new(bytes: &'b [u8]) -> Self { + Self { bytes, at: 0 } + } + + fn take(&mut self, n: usize) -> Result<&'b [u8]> { + let end = self + .at + .checked_add(n) + .context("zip: a length that overflows")?; + let field = self + .bytes + .get(self.at..end) + .context("zip: a record runs past its end")?; + self.at = end; + Ok(field) + } + + fn u16(&mut self) -> Result { + Ok(u16::from_le_bytes(self.take(2)?.try_into()?)) + } + + fn u32(&mut self) -> Result { + Ok(u32::from_le_bytes(self.take(4)?.try_into()?)) + } + + fn u64(&mut self) -> Result { + Ok(u64::from_le_bytes(self.take(8)?.try_into()?)) + } +} + +fn read_at(file: &mut File, at: u64, len: usize) -> Result> { + file.seek(SeekFrom::Start(at))?; + let mut buf = vec![0; len]; + file.read_exact(&mut buf) + .context("zip: the file ends early")?; + Ok(buf) +} + +/// The zip's central directory, zip64 included. +fn zip_entries(file: &mut File) -> Result> { + let len = file.seek(SeekFrom::End(0))?; + let tail_len = len.min(22 + 0xFFFF); + let tail_at = len - tail_len; + let tail = read_at(file, tail_at, usize::try_from(tail_len)?)?; + let eocd = (0..=tail.len().checked_sub(22).context("not a zip file")?) + .rev() + .find(|&i| tail[i..i + 4] == EOCD_SIG.to_le_bytes()) + .context("not a zip file: no end of central directory")?; + let mut end = Fields::new(&tail[eocd..]); + end.take(10)?; // signature, this disk, the directory's disk, entries on this disk + let mut count = u64::from(end.u16()?); + let mut cd_size = u64::from(end.u32()?); + let mut cd_offset = u64::from(end.u32()?); + if count == 0xFFFF || cd_size == 0xFFFF_FFFF || cd_offset == 0xFFFF_FFFF { + let eocd_at = tail_at + eocd as u64; + let at = eocd_at.checked_sub(20).context("zip64: no locator")?; + let locator = read_at(file, at, 20)?; + let mut locator = Fields::new(&locator); + ensure!(locator.u32()? == ZIP64_LOCATOR_SIG, "zip64: no locator"); + locator.u32()?; + let record_at = locator.u64()?; + let record = read_at(file, record_at, 56)?; + let mut record = Fields::new(&record); + ensure!( + record.u32()? == ZIP64_EOCD_SIG, + "zip64: no end of central directory" + ); + record.take(28)?; // its size, versions, disks, entries on this disk + count = record.u64()?; + cd_size = record.u64()?; + cd_offset = record.u64()?; + } + ensure!( + cd_offset.checked_add(cd_size).is_some_and(|end| end <= len), + "zip: the central directory runs past the file" + ); + ensure!( + cd_size <= 64 << 20, + "zip: a central directory of {cd_size} bytes" + ); + let directory = read_at(file, cd_offset, usize::try_from(cd_size)?)?; + let mut directory = Fields::new(&directory); + let mut entries = Vec::new(); + for _ in 0..count { + ensure!( + directory.u32()? == CENTRAL_SIG, + "zip: a damaged central directory" + ); + directory.take(4)?; // versions + let flags = directory.u16()?; + let method = directory.u16()?; + directory.take(4)?; // time and date + let crc = directory.u32()?; + let mut compressed = u64::from(directory.u32()?); + let mut size = u64::from(directory.u32()?); + let name_len = usize::from(directory.u16()?); + let extra_len = usize::from(directory.u16()?); + let comment_len = usize::from(directory.u16()?); + directory.take(8)?; // disk and attributes + let mut offset = u64::from(directory.u32()?); + let name = String::from_utf8_lossy(directory.take(name_len)?).into_owned(); + let mut extra = Fields::new(directory.take(extra_len)?); + directory.take(comment_len)?; + while extra.at + 4 <= extra.bytes.len() { + let id = extra.u16()?; + let field_len = usize::from(extra.u16()?); + let mut field = Fields::new(extra.take(field_len)?); + if id == 0x0001 { + if size == 0xFFFF_FFFF { + size = field.u64()?; + } + if compressed == 0xFFFF_FFFF { + compressed = field.u64()?; + } + if offset == 0xFFFF_FFFF { + offset = field.u64()?; + } + } + } + entries.push(ZipEntry { + name, + flags, + method, + crc, + compressed, + size, + offset, + }); + } + Ok(entries) +} + +/// `bootstrap-data.bin`, once. rskj's `Unzipper` extracts every entry and +/// refuses one whose name would place it outside its directory. +fn bootstrap_entry(entries: &[ZipEntry]) -> Result<&ZipEntry> { + for entry in entries { + ensure!( + !entry.name.contains("..") + && !entry.name.starts_with(['/', '\\']) + && !entry.name.contains(':'), + "The source contains an illegal entry: {}", + entry.name + ); + } + let mut found = entries.iter().filter(|e| e.name == BOOTSTRAP_ENTRY); + let entry = found + .next() + .context("the copy holds no bootstrap-data.bin")?; + ensure!( + found.next().is_none(), + "the copy holds bootstrap-data.bin twice" + ); + Ok(entry) +} + +type Stored = std::io::Take>; + +enum Body { + Stored(Stored), + Deflated(flate2::bufread::DeflateDecoder), +} + +/// An entry's contents, held to its CRC-32 and sizes once read to the end, +/// as Java's `ZipInputStream` holds them. +struct EntryReader { + body: Body, + crc: flate2::Crc, + read: u64, + entry: ZipEntry, + ended: bool, +} + +fn open_entry(mut file: File, entry: &ZipEntry) -> Result { + ensure!(entry.flags & 1 == 0, "encrypted ZIP entry not supported"); + let local = read_at(&mut file, entry.offset, 30)?; + let mut local = Fields::new(&local); + ensure!( + local.u32()? == LOCAL_SIG, + "zip: no local header at {}", + entry.offset + ); + local.take(22)?; + let name_len = u64::from(local.u16()?); + let extra_len = u64::from(local.u16()?); + file.seek(SeekFrom::Start(entry.offset + 30 + name_len + extra_len))?; + let data = BufReader::with_capacity(1 << 20, file).take(entry.compressed); + let body = match entry.method { + 0 => Body::Stored(data), + 8 => Body::Deflated(flate2::bufread::DeflateDecoder::new(data)), + m => bail!("invalid compression method {m}"), + }; + Ok(EntryReader { + body, + crc: flate2::Crc::new(), + read: 0, + entry: entry.clone(), + ended: false, + }) +} + +impl EntryReader { + fn check(&self) -> Result<()> { + if let Body::Deflated(d) = &self.body { + ensure!( + d.total_in() == self.entry.compressed, + "invalid entry compressed size (expected {} but got {} bytes)", + self.entry.compressed, + d.total_in() + ); + } + ensure!( + self.read == self.entry.size, + "invalid entry size (expected {} but got {} bytes)", + self.entry.size, + self.read + ); + ensure!( + self.crc.sum() == self.entry.crc, + "invalid entry CRC (expected 0x{:x} but got 0x{:x})", + self.entry.crc, + self.crc.sum() + ); + Ok(()) + } +} + +impl Read for EntryReader { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + let n = match &mut self.body { + Body::Stored(r) => r.read(buf)?, + Body::Deflated(r) => r.read(buf)?, + }; + if n == 0 && !buf.is_empty() && !self.ended { + self.ended = true; + self.check() + .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?; + } + self.crc.update(&buf[..n]); + self.read += n as u64; + Ok(n) + } +} + +#[cfg(test)] +#[path = "bootstrap_import_tests.rs"] +mod tests; diff --git a/crates/cli/src/bootstrap_import_tests.rs b/crates/cli/src/bootstrap_import_tests.rs new file mode 100644 index 0000000..4ec3f10 --- /dev/null +++ b/crates/cli/src/bootstrap_import_tests.rs @@ -0,0 +1,1200 @@ +//! `--import` against rskj's own tests (`co.rsk.db.importer`), Rootstock's real +//! signatures, and copies built here in both of rskj's formats. + +use super::*; +use alloy_primitives::{Address, Bytes}; +use k256::ecdsa::signature::hazmat::PrehashSigner; +use k256::ecdsa::SigningKey; +use rustock_storage::epoch_store::EpochConfig; +use rustock_trie::TrieKeySlice; +use std::path::PathBuf; + +// ------------------------------------------------------------------ signers + +#[test] +fn mainnet_and_testnet_use_rskjs_url_and_keys() { + let main = Signers::for_network(30, None, &[]).unwrap(); + assert_eq!(main.url, MAINNET_URL); + assert_eq!(main.keys, MAINNET_KEYS.map(String::from).to_vec()); + assert_eq!(main.threshold(), 2); + let test = Signers::for_network(31, None, &[]).unwrap(); + assert_eq!(test.url, TESTNET_URL); + assert_eq!(test.keys, TESTNET_KEYS.map(String::from).to_vec()); +} + +#[test] +fn another_network_needs_a_url_and_keys_of_its_own() { + let keys: Vec = MAINNET_KEYS[..2].iter().map(|k| k.to_uppercase()).collect(); + assert!(Signers::for_network(33, None, &keys).is_err()); + let signers = Signers::for_network(33, Some("https://copies.example/regtest"), &keys).unwrap(); + assert_eq!(signers.url, "https://copies.example/regtest/"); + assert_eq!( + signers.keys, + MAINNET_KEYS[..2] + .iter() + .map(|k| k.to_string()) + .collect::>() + ); +} + +#[test] +fn keys_that_cannot_sign_or_cannot_make_two_are_refused() { + let one = vec![MAINNET_KEYS[0].to_string()]; + assert!(Signers::for_network(30, None, &one).is_err()); + let twice = vec![MAINNET_KEYS[0].to_string(), MAINNET_KEYS[0].to_uppercase()]; + assert!(Signers::for_network(30, None, &twice).is_err()); + let off_the_curve = vec![ + MAINNET_KEYS[0].to_string(), + format!("04{}", "11".repeat(64)), + ]; + assert!(Signers::for_network(30, None, &off_the_curve).is_err()); + let not_hex = vec![MAINNET_KEYS[0].to_string(), "zz".to_string()]; + assert!(Signers::for_network(30, None, ¬_hex).is_err()); + assert!(Signers::for_network(30, Some("http://import.example/"), &[]).is_err()); +} + +#[test] +fn more_than_half_have_to_agree_and_never_fewer_than_two() { + for (n, want) in [(2, 2), (3, 2), (4, 3), (5, 3), (6, 4), (7, 4)] { + let keys = (0..n).map(|i| format!("k{i}")).collect(); + assert_eq!( + Signers { + url: String::new(), + keys + } + .threshold(), + want, + "{n} keys" + ); + } +} + +// ---------------------------------------------- BootstrapIndexCandidateSelectorTest + +fn entry(height: u64, hash: &str) -> Entry { + Entry { + height, + db: "dbPath".into(), + hash: hash.into(), + sig: EntrySignature { + r: "r".into(), + s: "s".into(), + }, + } +} + +fn keys(n: usize) -> Vec { + (1..=n).map(|i| format!("key{i}")).collect() +} + +#[test] +fn no_index_is_an_error() { + // getMaximumCommonHeightDataEmpty + assert!(select(&[], &keys(2), 2).is_err()); +} + +#[test] +fn one_signer_alone_is_not_enough() { + // getMaximumCommonHeightDataOneEntry + let indexes = [Index { + dbs: vec![entry(1, "hash")], + }]; + assert!(select(&indexes, &keys(2), 2).is_err()); +} + +#[test] +fn a_height_one_signer_lists_twice_is_an_error() { + // getMaximumCommonHeightDataDuplicatedEntries + let indexes = [Index { + dbs: vec![entry(1, "hash"), entry(1, "hash")], + }]; + let error = select(&indexes, &keys(2), 2).unwrap_err().to_string(); + assert!(error.contains("2 entries from same height 1"), "{error}"); +} + +#[test] +fn two_signers_agreeing_choose_their_height() { + // getMaximumCommonHeightDataTwoEntries + let indexes = [ + Index { + dbs: vec![entry(1, "hash")], + }, + Index { + dbs: vec![entry(1, "hash")], + }, + ]; + assert_eq!(select(&indexes, &keys(2), 2).unwrap().height, 1); +} + +#[test] +fn the_highest_height_enough_signers_agree_on_is_chosen() { + // getMaximumCommonHeightDataThreeEntries + let indexes = [ + Index { + dbs: vec![entry(1, "hash"), entry(2, "hash")], + }, + Index { + dbs: vec![entry(1, "hash")], + }, + ]; + assert_eq!(select(&indexes, &keys(2), 2).unwrap().height, 1); + // getMaximumCommonHeightDataManyEntries + let both = Index { + dbs: vec![entry(1, "hash"), entry(2, "hash")], + }; + let indexes = [both.clone(), both.clone(), both]; + assert_eq!(select(&indexes, &keys(3), 2).unwrap().height, 2); +} + +#[test] +fn signers_naming_different_copies_do_not_count_together() { + let indexes = [ + Index { + dbs: vec![entry(1, "x"), entry(2, "a")], + }, + Index { + dbs: vec![entry(1, "x"), entry(2, "b")], + }, + Index { + dbs: vec![entry(1, "y"), entry(2, "a")], + }, + ]; + let candidate = select(&indexes, &keys(3), 2).unwrap(); + assert_eq!(candidate.height, 2); + assert_eq!( + candidate + .entries + .iter() + .map(|(i, _)| *i) + .collect::>(), + [0, 2] + ); + let indexes = [ + Index { + dbs: vec![entry(1, "x"), entry(2, "a")], + }, + Index { + dbs: vec![entry(1, "x"), entry(2, "b")], + }, + Index { + dbs: vec![entry(1, "y"), entry(2, "c")], + }, + ]; + let candidate = select(&indexes, &keys(3), 2).unwrap(); + assert_eq!(candidate.height, 1, "no two agree at 2"); + assert_eq!( + candidate + .entries + .iter() + .map(|(i, _)| *i) + .collect::>(), + [0, 1] + ); +} + +// ------------------------------------------------------ BootstrapDataVerifierTest + +fn signed_entry(hash: &str, r: &str, s: &str) -> Entry { + Entry { + height: 1, + db: "dbPath".into(), + hash: hash.into(), + sig: EntrySignature { + r: r.into(), + s: s.into(), + }, + } +} + +fn candidate(hash: &str, signatures: &[(&str, &str)]) -> Candidate { + Candidate { + height: 1, + entries: signatures + .iter() + .enumerate() + .map(|(i, (r, s))| (i, signed_entry(hash, r, s))) + .collect(), + } +} + +#[test] +fn nothing_to_verify_verifies_nothing() { + // verifyFileEmpty + let none = Candidate { + height: 1, + entries: Vec::new(), + }; + assert_eq!(verify(&none, &[]).unwrap(), 0); +} + +#[test] +fn rskjs_vectors_verify_two_of_three_both_with_a_high_s() { + // verifyFileMany + let keys = [ + "04330037e82c177d7108077c80440821e13c1c62105f85e030214b48d7b5dff0b8e7c158b171546a71139e4de56c8535c964514033b89a669a8e87a5e8770c147c", + "0473602083afe175e7cae12dbc27da54ec5ac77f99920787f3e891e7af303aaed480770c0de4c991aea1712729260175e158fa73f63c60f0f1de057139c52714de", + "04bf74915a14e96df0b520a659acc28ae21aada3b0415e35f82f0c7b546338fc48bbfdce858ce3e4690feeb443d7f4955881ba0b793999e4fef7e46732e7fedf02", + ] + .map(String::from); + let entries = candidate( + "53cb8e93030183c5ba198433e8cd1f013f3d113e0f4d1756de0d1f124ead155a", + &[ + ( + "8dba957877d5bdcb26d551dfa2fa509dfe3fe327caf0166130b9f467a0a0c249", + "dab3fdf2031515d2de1d420310c69153fcc356f22b50dfd53c6e13e74e346eee", + ), + ( + "f0e8aab4fdd83382292a1bbc5480e2ae8084dc245f000f4bc4534d383a3a7919", + "a30891f2176bd87b4a3ac5c75167f2442453c17c6e2fbfb36c3b972ee67a4c2d", + ), + ("00", "00"), + ], + ); + assert_eq!(verify(&entries, &keys).unwrap(), 2); +} + +#[test] +fn entries_naming_different_hashes_are_an_error() { + // doNotVerifyForDifferentHashes + let keys = [ + "04330037e82c177d7108077c80440821e13c1c62105f85e030214b48d7b5dff0b8e7c158b171546a71139e4de56c8535c964514033b89a669a8e87a5e8770c147c", + "04dffaa346b18c26230d6050c6ab67f0761ab12136b0dc3a1f1c6ed31890ffac38baa3d8da1df4aa2acc72e45dc3599797ba668666c5395280217f2fd215262b8a", + "04bf74915a14e96df0b520a659acc28ae21aada3b0415e35f82f0c7b546338fc48bbfdce858ce3e4690feeb443d7f4955881ba0b793999e4fef7e46732e7fedf02", + ] + .map(String::from); + let mixed = Candidate { + height: 1, + entries: vec![ + ( + 0, + signed_entry( + "53cb8e93030183c5ba198433e8cd1f013f3d113e0f4d1756de0d1f124ead155a", + "8dba957877d5bdcb26d551dfa2fa509dfe3fe327caf0166130b9f467a0a0c249", + "dab3fdf2031515d2de1d420310c69153fcc356f22b50dfd53c6e13e74e346eee", + ), + ), + ( + 1, + signed_entry( + "87d149cb424c0387656f211d2589fb5b1e16229921309e98588419ccca8a7362", + "0efa8e0738468e434e443676b5a91fe15b0c416ff7190d8d912dff65161ad33c", + "aae27edef80e137dc1a0e92b6ae7fed0d1dc1b4a0ea8930bad6abec5ca1297c1", + ), + ), + ( + 2, + signed_entry( + "53cb8e93030183c5ba198433e8cd1f013f3d113e0f4d1756de0d1f124ead155c", + "00", + "00", + ), + ), + ], + }; + assert!(verify(&mixed, &keys).is_err()); +} + +/// The three signatures of Rootstock's mainnet copy at #9,405,000, from the +/// signers' indexes on 14 November 2026. +const MAINNET_9405000: (&str, [(&str, &str); 3]) = ( + "b5a4e411ea42f30346b63813592e7a21fa47fd8d84c706c3e03f22c7ccfe74a9", + [ + ( + "bd6f96ba76b4a5f581dd93465fe7a4853be5525ab18ab02f16c476b2e2f88147", + "0bf83fe46aadd800d7a542bde14fc9287554c854ec05beeedf2e73b94d6900ba", + ), + ( + "abf0a61cdf98ba617850fd6abcb7db3cb2a77d38b77cbd11f725a50c42466c56", + "78fa0eae6bcb99a70b32f3188d323754fd18297dd526a70be0ca89c0da2364e2", + ), + ( + "7d81c5826834a8aa1fdd96d698b294c922390e23b5d477ec6d5a9ff2d43e6a39", + "16f6507e11df9a2457c177d3d571fa1c96207c73e627de1bfb434dd7042509ff", + ), + ], +); + +/// The three of testnet's at #8,280,000. The second and third carry an `s` +/// in the upper half of the range, which k256 alone would refuse. +const TESTNET_8280000: (&str, [(&str, &str); 3]) = ( + "c2c93308f8b15af0d03ac82c06c122c02b82ecd8403635143bb3ebc671f380d1", + [ + ( + "18ee6a41542b8ff9114dbe072891fc1f5cad0f57d9643406fb521d1a64a17edd", + "30575649f926e4d8779a584d59006bce53529d08e91d9535b894fe420e8ffbd1", + ), + ( + "685ee59686690ceaec185f6a3f5d4a5847bba7434ab61197463bd22990651783", + "ad1858dcaa1f1a25476ac8d24cdd8089d8fa145c3fe60e312d4790ed8c2012f9", + ), + ( + "256b93d27095a10f9c703522de321c6c209128760e0fae4ed43985911d2124bc", + "90c180d08d39168509682edfa4edec93d03a5b275181a9440d53f1715c893091", + ), + ], +); + +#[test] +fn rootstocks_signers_signed_their_real_copies() { + let mainnet = MAINNET_KEYS.map(String::from); + let (hash, signatures) = MAINNET_9405000; + assert_eq!(verify(&candidate(hash, &signatures), &mainnet).unwrap(), 3); + let testnet = TESTNET_KEYS.map(String::from); + let (hash, signatures) = TESTNET_8280000; + assert_eq!(verify(&candidate(hash, &signatures), &testnet).unwrap(), 3); +} + +#[test] +fn a_signature_counts_only_by_its_own_key_over_its_own_hash() { + let (hash, signatures) = MAINNET_9405000; + let mut rotated = MAINNET_KEYS.map(String::from); + rotated.rotate_left(1); + assert_eq!(verify(&candidate(hash, &signatures), &rotated).unwrap(), 0); + let other = "a5a4e411ea42f30346b63813592e7a21fa47fd8d84c706c3e03f22c7ccfe74a9"; + let mainnet = MAINNET_KEYS.map(String::from); + assert_eq!(verify(&candidate(other, &signatures), &mainnet).unwrap(), 0); + // `new BigInteger(1, bytes)` reads leading zeros as nothing. + let padded: Vec<(String, String)> = signatures + .iter() + .map(|(r, s)| (format!("0000{r}"), format!("00{s}"))) + .collect(); + let padded: Vec<(&str, &str)> = padded + .iter() + .map(|(r, s)| (r.as_str(), s.as_str())) + .collect(); + assert_eq!(verify(&candidate(hash, &padded), &mainnet).unwrap(), 3); + // Hex that is not hex fails the import, as BouncyCastle's `Hex.decode` does. + assert!(verify(&candidate(hash, &[("0g", "00"), ("00", "00")]), &mainnet).is_err()); +} + +// ------------------------------------------------------------------- copies + +/// A chain of empty blocks from `from`, each naming the one before. +fn chain(from: u64, count: u64, state_root: B256) -> Vec { + let mut blocks = Vec::new(); + let mut parent = B256::repeat_byte(0xEE); + for number in from..from + count { + let header = Header { + parent_hash: parent, + ommers_hash: rustock_execution::processor::compute_ommers_hash(&[]), + beneficiary: Address::repeat_byte(3), + state_root, + transactions_root: rustock_core::ordered_tx_trie_root(&[], true), + receipts_root: B256::repeat_byte(6), + logs_bloom: Default::default(), + extension_data: None, + difficulty: U256::from(100), + number, + gas_limit: U256::from(6_800_000), + gas_used: 0, + timestamp: 1_600_000_000 + number, + extra_data: Bytes::new(), + paid_fees: U256::ZERO, + minimum_gas_price: U256::from(1), + 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, + kept: None, + }; + parent = header.hash(); + blocks.push(Block { + header, + transactions: vec![], + ommers: vec![], + }); + } + blocks +} + +fn td_of(number: u64) -> U256 { + U256::from(1_000_000 + 100 * number) +} + +const CODE_KEY: &[u8] = b"a contract's code, which is long"; + +fn long_value() -> Vec { + (0..=199u8).collect() +} + +/// A state trie as rskj exports one: its nodes, and its long values apart. +struct State { + root: B256, + nodes: Vec>, + values: Vec>, +} + +fn state() -> State { + let mem = MemoryTrieStore::new(); + let mut root = TrieNode::empty(); + for i in 0u8..40 { + root = root.put(&TrieKeySlice::from_key(&[i, i ^ 0x5a, 7]), &[i; 12], &mem); + } + root = root.put(&TrieKeySlice::from_key(CODE_KEY), &long_value(), &mem); + root.save(&mem, true); + let hash = root.compute_hash(&mem); + let value_key = keccak256(long_value()); + let mut nodes: Vec> = mem + .keys() + .into_iter() + .filter(|k| k[..] != value_key[..]) + .map(|k| mem.get(&k).unwrap()) + .collect(); + nodes.sort(); + State { + root: hash, + nodes, + values: vec![long_value()], + } +} + +fn element(bytes: &[u8]) -> Vec { + alloy_rlp::encode(bytes) +} + +fn 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 +} + +/// `BigInteger.toByteArray()` of a number that is not negative. +fn java_bytes(n: U256) -> Vec { + let mut bytes = n.to_be_bytes_trimmed_vec(); + if bytes.first().is_none_or(|b| b & 0x80 != 0) { + bytes.insert(0, 0); + } + bytes +} + +/// What rskj's exporter writes for a block: `LIST[ELEMENT(block), ELEMENT(td)]`. +fn block_element(block: &Block, td: U256) -> Vec { + list(&[element(&alloy_rlp::encode(block)), element(&java_bytes(td))]) +} + +/// What a copy holds, element by element, before it is framed. +struct Copy { + blocks: Vec>, + values: Vec>, + nodes: Vec>, +} + +impl Copy { + /// `count` blocks up to `top`, and the state above at the top. + fn new(top: u64, count: u64) -> (Self, State, Vec) { + let state = state(); + let blocks = chain(top + 1 - count, count, state.root); + let copy = Self { + blocks: blocks + .iter() + .map(|b| block_element(b, td_of(b.header.number))) + .collect(), + values: state.values.iter().map(|v| element(v)).collect(), + nodes: state.nodes.iter().map(|n| element(n)).collect(), + }; + (copy, state, blocks) + } + + /// rskj's v2, in its exporter's order, a few elements to a chunk. + fn v2(&self) -> Vec { + v2(&[ + (TAG_BLOCKS, chunks(&self.blocks, 3)), + (TAG_VALUES, chunks(&self.values, 3)), + (TAG_NODES, chunks(&self.nodes, 7)), + ]) + } + + /// rskj's v1: `LIST[ELEMENT(blocks), LIST[nodes, values]]`. + fn v1(&self) -> Vec { + list(&[ + element(&list(&self.blocks)), + list(&[list(&self.nodes), list(&self.values)]), + ]) + } +} + +fn chunks(elements: &[Vec], per_chunk: usize) -> Vec> { + elements.chunks(per_chunk).map(|c| c.concat()).collect() +} + +fn v2(sections: &[(u8, Vec>)]) -> Vec { + let mut out = V2_MAGIC.to_vec(); + out.push(V2_VERSION); + for (tag, chunks) in sections { + out.push(*tag); + for chunk in chunks { + out.extend_from_slice(&(chunk.len() as u64).to_be_bytes()); + out.extend_from_slice(chunk); + } + out.extend_from_slice(&0u64.to_be_bytes()); + } + out.push(TAG_END); + out +} + +/// A zip of `(name, deflated, contents)` entries, with zip64's records when +/// `zip64`. +fn zip(entries: &[(&str, bool, &[u8])], zip64: bool) -> Vec { + let mut out = Vec::new(); + let mut central = Vec::new(); + for (name, deflated, contents) in entries { + let mut crc = flate2::Crc::new(); + crc.update(contents); + let data = if *deflated { + let mut encoder = + flate2::write::DeflateEncoder::new(Vec::new(), flate2::Compression::default()); + encoder.write_all(contents).unwrap(); + encoder.finish().unwrap() + } else { + contents.to_vec() + }; + let method: u16 = if *deflated { 8 } else { 0 }; + let offset = out.len() as u32; + out.extend_from_slice(&LOCAL_SIG.to_le_bytes()); + out.extend_from_slice(&20u16.to_le_bytes()); + out.extend_from_slice(&0u16.to_le_bytes()); + out.extend_from_slice(&method.to_le_bytes()); + out.extend_from_slice(&[0; 4]); + out.extend_from_slice(&crc.sum().to_le_bytes()); + out.extend_from_slice(&(data.len() as u32).to_le_bytes()); + out.extend_from_slice(&(contents.len() as u32).to_le_bytes()); + out.extend_from_slice(&(name.len() as u16).to_le_bytes()); + out.extend_from_slice(&0u16.to_le_bytes()); + out.extend_from_slice(name.as_bytes()); + out.extend_from_slice(&data); + + let wide = |v: u32| if zip64 { 0xFFFF_FFFF } else { v }; + central.extend_from_slice(&CENTRAL_SIG.to_le_bytes()); + central.extend_from_slice(&[20, 0, 20, 0]); + central.extend_from_slice(&0u16.to_le_bytes()); + central.extend_from_slice(&method.to_le_bytes()); + central.extend_from_slice(&[0; 4]); + central.extend_from_slice(&crc.sum().to_le_bytes()); + central.extend_from_slice(&wide(data.len() as u32).to_le_bytes()); + central.extend_from_slice(&wide(contents.len() as u32).to_le_bytes()); + central.extend_from_slice(&(name.len() as u16).to_le_bytes()); + central.extend_from_slice(&(if zip64 { 28u16 } else { 0 }).to_le_bytes()); + central.extend_from_slice(&0u16.to_le_bytes()); + central.extend_from_slice(&[0; 8]); + central.extend_from_slice(&wide(offset).to_le_bytes()); + central.extend_from_slice(name.as_bytes()); + if zip64 { + central.extend_from_slice(&1u16.to_le_bytes()); + central.extend_from_slice(&24u16.to_le_bytes()); + central.extend_from_slice(&(contents.len() as u64).to_le_bytes()); + central.extend_from_slice(&(data.len() as u64).to_le_bytes()); + central.extend_from_slice(&u64::from(offset).to_le_bytes()); + } + } + let cd_offset = out.len() as u64; + let cd_size = central.len() as u64; + let count = entries.len() as u64; + out.extend_from_slice(¢ral); + if zip64 { + let record_at = out.len() as u64; + out.extend_from_slice(&ZIP64_EOCD_SIG.to_le_bytes()); + out.extend_from_slice(&44u64.to_le_bytes()); + out.extend_from_slice(&[45, 0, 45, 0]); + out.extend_from_slice(&[0; 8]); + out.extend_from_slice(&count.to_le_bytes()); + out.extend_from_slice(&count.to_le_bytes()); + out.extend_from_slice(&cd_size.to_le_bytes()); + out.extend_from_slice(&cd_offset.to_le_bytes()); + out.extend_from_slice(&ZIP64_LOCATOR_SIG.to_le_bytes()); + out.extend_from_slice(&0u32.to_le_bytes()); + out.extend_from_slice(&record_at.to_le_bytes()); + out.extend_from_slice(&1u32.to_le_bytes()); + } + let (count, cd_size, cd_offset) = if zip64 { + (0xFFFF, 0xFFFF_FFFF, 0xFFFF_FFFF) + } else { + (count as u16, cd_size as u32, cd_offset as u32) + }; + out.extend_from_slice(&EOCD_SIG.to_le_bytes()); + out.extend_from_slice(&[0; 4]); + out.extend_from_slice(&count.to_le_bytes()); + out.extend_from_slice(&count.to_le_bytes()); + out.extend_from_slice(&cd_size.to_le_bytes()); + out.extend_from_slice(&cd_offset.to_le_bytes()); + out.extend_from_slice(&0u16.to_le_bytes()); + out +} + +/// A store loaded from a zip, with the directory that holds them. +struct Loaded { + dir: tempfile::TempDir, + store: BlockStore, + result: Result, +} + +fn write_zip(dir: &Path, zip_bytes: &[u8]) -> PathBuf { + let path = dir.join("copy.zip"); + std::fs::write(&path, zip_bytes).unwrap(); + path +} + +fn load_zip(zip_bytes: &[u8], height: u64) -> Loaded { + let dir = tempfile::tempdir().unwrap(); + let store = BlockStore::open(dir.path()).unwrap(); + let path = write_zip(dir.path(), zip_bytes); + let trie = TrieTarget::Column(store.db().clone()); + let result = load( + &path, + height, + dir.path(), + &store, + &trie, + &ChainConfig::regtest(), + ); + Loaded { dir, store, result } +} + +fn load_bin(bin: &[u8], height: u64) -> Loaded { + load_zip(&zip(&[(BOOTSTRAP_ENTRY, true, bin)], false), height) +} + +fn refused(bin: &[u8], height: u64) -> String { + match load_bin(bin, height).result { + Ok(summary) => panic!("loaded {summary:?}"), + Err(e) => format!("{e:#}"), + } +} + +/// What a node finds after a load: its position at the copy's top, the +/// blocks indexed, the floor at the lowest, the state readable through the +/// trie code the node executes with, and its own coherence checks content. +fn assert_loaded(store: &BlockStore, blocks: &[Block], state: &State, summary: &Summary) { + let tip = &blocks.last().unwrap().header; + assert_eq!(summary.highest, tip.number); + assert_eq!(summary.lowest, blocks[0].header.number); + assert_eq!(summary.state_root, state.root); + assert_eq!(summary.nodes, state.nodes.len() as u64); + assert_eq!(summary.values, 1); + assert_eq!(store.head().unwrap(), Some(tip.hash())); + assert_eq!(store.exec_head().unwrap(), Some((tip.hash(), state.root))); + for block in blocks { + let hash = block.header.hash(); + assert_eq!( + store.canonical_hash(block.header.number).unwrap(), + Some(hash) + ); + assert_eq!( + store.total_difficulty(hash).unwrap(), + Some(td_of(block.header.number)) + ); + assert!(store.body(hash).unwrap().is_some()); + } + let chain = ChainConfig::regtest(); + let genesis = chain + .known_genesis_hash() + .unwrap_or_else(|| chain.genesis_header().hash()); + assert_eq!(store.canonical_hash(0).unwrap(), Some(genesis)); + assert_eq!( + store.prune_floor().unwrap().map(|f| f.number), + Some(blocks[0].header.number) + ); + + let trie = RocksDbTrieStore::from_db(store.db().clone()); + let root = TrieNode::from_message(&trie.get(state.root.as_slice()).unwrap(), &trie); + assert_eq!(root.compute_hash(&trie), state.root); + assert_eq!( + root.get(&TrieKeySlice::from_key(CODE_KEY), &trie), + Some(long_value()) + ); + assert_eq!( + root.get(&TrieKeySlice::from_key(&[5, 5 ^ 0x5a, 7]), &trie), + Some(vec![5; 12]) + ); + rustock_sync::invariant::check(store, Some(&trie), rustock_sync::Scope::Full).unwrap(); +} + +#[test] +fn a_v2_copy_loads_as_rskj_loads_it() { + let (copy, state, blocks) = Copy::new(1_009, 6); + let loaded = load_bin(©.v2(), 1_009); + let summary = loaded.result.unwrap(); + assert_loaded(&loaded.store, &blocks, &state, &summary); + assert!(!loaded.dir.path().join(UNFINISHED).exists()); +} + +#[test] +fn a_v1_copy_loads_the_same() { + let (copy, state, blocks) = Copy::new(1_009, 6); + let loaded = load_bin(©.v1(), 1_009); + assert_loaded(&loaded.store, &blocks, &state, &loaded.result.unwrap()); +} + +#[test] +fn a_stored_entry_and_zip64_records_read_the_same() { + let (copy, state, blocks) = Copy::new(1_009, 6); + let bin = copy.v2(); + for (deflated, zip64) in [(false, false), (true, true), (false, true)] { + let with_others = zip( + &[ + ("README", true, b"hello"), + (BOOTSTRAP_ENTRY, deflated, &bin), + ], + zip64, + ); + let loaded = load_zip(&with_others, 1_009); + assert_loaded(&loaded.store, &blocks, &state, &loaded.result.unwrap()); + } +} + +#[test] +fn a_section_this_reader_does_not_know_is_skipped() { + let (copy, state, blocks) = Copy::new(1_009, 6); + let bin = v2(&[ + (0x04, vec![b"a manifest".to_vec()]), + (TAG_BLOCKS, chunks(©.blocks, 2)), + (TAG_VALUES, chunks(©.values, 1)), + (TAG_NODES, chunks(©.nodes, 100)), + ]); + let loaded = load_bin(&bin, 1_009); + assert_loaded(&loaded.store, &blocks, &state, &loaded.result.unwrap()); +} + +#[test] +fn the_epoch_backend_takes_the_state_as_one_seed_epoch() { + let (copy, state, _) = Copy::new(1_009, 6); + let dir = tempfile::tempdir().unwrap(); + let store = BlockStore::open(dir.path()).unwrap(); + let epochs = dir.path().join("trie-epochs"); + let factory = || TrieEngine::RocksDb.factory(10_000); + let trie = TrieTarget::Epochs(Box::new( + EpochTrieStore::open_with(&epochs, EpochConfig::default(), None, factory()).unwrap(), + )); + let path = write_zip( + dir.path(), + &zip(&[(BOOTSTRAP_ENTRY, true, ©.v2())], false), + ); + load( + &path, + 1_009, + dir.path(), + &store, + &trie, + &ChainConfig::regtest(), + ) + .unwrap(); + drop(trie); + // The node opens it with the height it resumes from, which closes the + // seed epoch there and writes on into a new one. + let reopened = + EpochTrieStore::open_with(&epochs, EpochConfig::default(), Some(1_009), factory()).unwrap(); + assert_eq!(reopened.epoch_count(), 2); + let root = TrieNode::from_message(&reopened.get(state.root.as_slice()).unwrap(), &reopened); + assert_eq!( + root.get(&TrieKeySlice::from_key(CODE_KEY), &reopened), + Some(long_value()) + ); +} + +#[test] +fn rskjs_structural_refusals_hold() { + let (copy, _, _) = Copy::new(1_009, 6); + let good = copy.v2(); + let refusals: Vec<(Vec, &str)> = vec![ + (Vec::new(), "Empty bootstrap data file"), + (vec![0x10, 0x00], "Unrecognized bootstrap-data format"), + ( + b"RSKBOOX\n\x02\x00".to_vec(), + "Invalid bootstrap-data v2 magic", + ), + ( + b"RSKBOOT\n\x03\x00".to_vec(), + "Unsupported bootstrap-data v2 version: 3", + ), + ( + good[..good.len() - 1].to_vec(), + "missing end-of-sections marker", + ), + ([&good[..], &[0]].concat(), "trailing bytes"), + (good[..good.len() - 20].to_vec(), "exceeds the"), + ( + [ + &b"RSKBOOT\n\x02\x01"[..], + &(MAX_CHUNK_LEN + 1).to_be_bytes(), + ] + .concat(), + "chunk length out of range", + ), + ( + v2(&[ + (TAG_BLOCKS, chunks(©.blocks, 3)), + (TAG_NODES, chunks(©.nodes, 7)), + (TAG_VALUES, chunks(©.values, 3)), + ]), + "out of order", + ), + ( + v2(&[ + (TAG_VALUES, chunks(©.values, 3)), + (TAG_NODES, chunks(©.nodes, 7)), + ]), + "no blocks section", + ), + ( + v2(&[ + (TAG_BLOCKS, chunks(©.blocks, 3)), + (TAG_VALUES, chunks(©.values, 3)), + ]), + "no state-nodes section", + ), + ( + v2(&[ + (TAG_BLOCKS, chunks(©.blocks, 3)), + (TAG_VALUES, vec![[©.values[0][..], &[0x80]].concat()]), + (TAG_NODES, chunks(©.nodes, 7)), + ]), + "empty element", + ), + ( + v2(&[ + (TAG_BLOCKS, chunks(©.blocks, 3)), + (TAG_VALUES, Vec::new()), + (TAG_NODES, chunks(©.nodes, 7)), + ]), + "a referenced long value is missing", + ), + ( + v2(&[ + ( + TAG_BLOCKS, + vec![[©.blocks[0][..], &[0x05, 0x06]].concat()], + ), + (TAG_VALUES, chunks(©.values, 3)), + (TAG_NODES, chunks(©.nodes, 7)), + ]), + "Bootstrap-data v2 blocks section is malformed", + ), + ]; + for (bin, why) in refusals { + let error = refused(&bin, 1_009); + assert!(error.contains(why), "wanted {why:?}, got {error}"); + } +} + +#[test] +fn a_node_not_in_the_form_rskj_writes_is_refused() { + let (mut copy, _, _) = Copy::new(1_009, 6); + let mut node = State::node_bytes(©.nodes[0]); + node[0] &= !0b0100_0000; // the version bits rskj writes as 01 + copy.nodes[0] = element(&node); + assert!(refused(©.v2(), 1_009).contains("not in the form rskj writes")); +} + +impl State { + fn node_bytes(element: &[u8]) -> Vec { + data(element).unwrap().to_vec() + } +} + +#[test] +fn a_negative_total_difficulty_is_refused_as_rskj_refuses_it() { + let (mut copy, _, blocks) = Copy::new(1_009, 6); + copy.blocks[2] = list(&[element(&alloy_rlp::encode(&blocks[2])), element(&[0x80])]); + assert!(refused(©.v2(), 1_009).contains("must be positive or zero")); + assert_eq!(total_difficulty(&[0x00, 0x80]).unwrap(), U256::from(0x80)); + assert_eq!(total_difficulty(&[0x00]).unwrap(), U256::ZERO); +} + +#[test] +fn blocks_that_do_not_make_one_chain_are_refused() { + let (mut copy, _, _) = Copy::new(1_009, 6); + copy.blocks.remove(3); + assert!(refused(©.v2(), 1_009).contains("do not form one chain")); + let (mut copy, _, _) = Copy::new(1_009, 6); + let again = copy.blocks[1].clone(); + copy.blocks.push(again); + assert!(refused(©.v2(), 1_009).contains("two blocks at #1005")); +} + +#[test] +fn a_body_that_is_not_its_headers_is_refused() { + let (mut copy, _, blocks) = Copy::new(1_009, 6); + let mut odd = blocks[2].clone(); + odd.ommers = vec![blocks[0].header.clone()]; + copy.blocks[2] = block_element(&odd, td_of(odd.header.number)); + assert!(refused(©.v2(), 1_009).contains("uncles are not the ones its header names")); +} + +#[test] +fn the_top_has_to_be_where_the_signers_say_and_hold_its_state() { + let (copy, _, _) = Copy::new(1_009, 6); + assert!(refused(©.v2(), 1_010).contains("where its signers list it")); + let (mut copy, _, _) = Copy::new(1_009, 6); + copy.blocks = chain(1_004, 6, B256::repeat_byte(9)) + .iter() + .map(|b| block_element(b, td_of(b.header.number))) + .collect(); + assert!(refused(©.v2(), 1_009).contains("is not among the copy's state nodes")); +} + +#[test] +fn a_copy_from_genesis_has_to_start_at_this_networks() { + let (copy, _, _) = Copy::new(5, 6); + assert!(refused(©.v2(), 5).contains("not this network's genesis")); +} + +#[test] +fn the_zip_is_held_to_rskjs_unzipper_and_its_checksums() { + let (copy, _, _) = Copy::new(1_009, 6); + let bin = copy.v2(); + let cases: Vec<(Vec, &str)> = vec![ + ( + zip(&[("../bootstrap-data.bin", true, &bin)], false), + "illegal entry", + ), + ( + zip(&[("other.bin", true, &bin)], false), + "holds no bootstrap-data.bin", + ), + ( + zip( + &[(BOOTSTRAP_ENTRY, true, &bin), (BOOTSTRAP_ENTRY, true, &bin)], + false, + ), + "bootstrap-data.bin twice", + ), + ( + b"not a zip at all, nowhere near twenty-two bytes of one".to_vec(), + "not a zip", + ), + ]; + for (zip_bytes, why) in cases { + let error = format!("{:#}", load_zip(&zip_bytes, 1_009).result.unwrap_err()); + assert!(error.contains(why), "wanted {why:?}, got {error}"); + } + // The CRC-32 the directory gives no longer the one the entry has. + let mut damaged = zip(&[(BOOTSTRAP_ENTRY, false, &bin)], false); + let directory = 30 + BOOTSTRAP_ENTRY.len() + bin.len(); + damaged[directory + 16] ^= 1; + let error = format!("{:#}", load_zip(&damaged, 1_009).result.unwrap_err()); + assert!(error.contains("invalid entry CRC"), "{error}"); + // A byte changed where nothing reads it, in a section this reader skips: + // only the CRC-32 can tell. + let skipped = b"a section nobody reads"; + let with_skipped = v2(&[ + (0x04, vec![skipped.to_vec()]), + (TAG_BLOCKS, chunks(©.blocks, 3)), + (TAG_VALUES, chunks(©.values, 3)), + (TAG_NODES, chunks(©.nodes, 7)), + ]); + let mut damaged = zip(&[(BOOTSTRAP_ENTRY, false, &with_skipped)], false); + let at = 30 + BOOTSTRAP_ENTRY.len() + V2_MAGIC.len() + 1 + 1 + 8 + 2; + assert_eq!(damaged[at], skipped[2]); + damaged[at] ^= 1; + let error = format!("{:#}", load_zip(&damaged, 1_009).result.unwrap_err()); + assert!(error.contains("invalid entry CRC"), "{error}"); +} + +#[test] +fn a_chain_already_held_is_refused_and_an_unfinished_import_is_redone() { + let (copy, _, _) = Copy::new(1_009, 6); + let loaded = load_bin(©.v2(), 1_009); + loaded.result.unwrap(); + let dir = loaded.dir.path(); + let error = refuse_held_chain(&loaded.store, dir) + .unwrap_err() + .to_string(); + assert!( + error.contains("already holds a chain, up to #1009"), + "{error}" + ); + refuse_unfinished(dir).unwrap(); + + File::create(dir.join(UNFINISHED)).unwrap(); + refuse_held_chain(&loaded.store, dir).unwrap(); + assert!(refuse_unfinished(dir).is_err()); + + let empty = tempfile::tempdir().unwrap(); + let store = BlockStore::open(empty.path()).unwrap(); + refuse_held_chain(&store, empty.path()).unwrap(); +} + +#[test] +fn rlp_reads_as_rskjs_does() { + assert_eq!(data(&[0x05]).unwrap(), [0x05]); + assert_eq!( + data(&[0x81, 0x05]).unwrap(), + [0x05], + "a single byte written long" + ); + assert_eq!( + data(&[0xc1, 0x05]).unwrap(), + [0xc1, 0x05], + "a list is its whole encoding" + ); + assert!(data(&[0x80]).unwrap().is_empty()); + assert!( + decode_list(&[0xc1, 0x05, 0x00]).is_err(), + "bytes after the list" + ); + assert!(decode_list(&[0x81, 0x05]).is_err(), "not a list"); + assert!( + items(&[0x82, 0x05]).is_err(), + "an item running past its container" + ); +} + +// ----------------------------------------------------------- the whole import + +/// Serves what it was given, as Rootstock's server would. +struct Served(HashMap>); + +#[async_trait::async_trait] +impl Source for Served { + async fn get(&self, url: &str, sink: &mut dyn Sink) -> Result { + let body = self + .0 + .get(url) + .with_context(|| format!("HTTP 404 for {url}"))?; + for piece in body.chunks(4096) { + sink.write(piece)?; + } + Ok(body.len() as u64) + } +} + +fn signer(n: u8) -> (SigningKey, String) { + let key = SigningKey::from_slice(&[n; 32]).unwrap(); + let public = hex::encode(key.verifying_key().to_encoded_point(false).as_bytes()); + (key, public) +} + +fn sign(key: &SigningKey, hash: &[u8]) -> (String, String) { + let signature: Signature = key.sign_prehash(hash).unwrap(); + let (r, s) = signature.split_bytes(); + (hex::encode(r), hex::encode(s)) +} + +const URL: &str = "https://copies.example/regtest/"; + +/// Three signers' indexes for a zip at #2,003, each signed by `signs`, and +/// an older copy at #1,000 all three agree on. The zip itself is served for +/// the signers in `serving` only. +fn published( + zip_bytes: &[u8], + signs: impl Fn(usize) -> bool, + serving: impl Fn(usize) -> bool, +) -> (Served, Vec) { + let hash = Sha256::digest(zip_bytes); + let mut served = HashMap::new(); + let mut keys = Vec::new(); + for i in 0..3 { + let (key, public) = signer(i as u8 + 1); + let (r, s) = if signs(i) { + sign(&key, &hash) + } else { + ("01".into(), "01".into()) + }; + let db = format!("{public}/2003/output.zip"); + let index = serde_json::json!({ "dbs": [ + { "height": 1000, "date": "", "db": format!("{public}/1000/output.zip"), + "hash": "00".repeat(32), "sig": { "r": "01", "s": "01" } }, + { "height": 2003, "date": "14/11/2026 12:00:00", "db": db, + "hash": hex::encode(hash), "sig": { "r": r, "s": s } }, + ]}); + served.insert( + format!("{URL}{public}/index.json"), + serde_json::to_vec(&index).unwrap(), + ); + if serving(i) { + served.insert(format!("{URL}{db}"), zip_bytes.to_vec()); + } + keys.push(public); + } + (Served(served), keys) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn import_reads_the_indexes_checks_the_signatures_and_loads_the_newest_copy() { + let (copy, state, blocks) = Copy::new(2_003, 4); + let zip_bytes = zip(&[(BOOTSTRAP_ENTRY, true, ©.v2())], false); + // Only the third signer serves the copy: whichever signer is tried + // first, the import has to reach it. + let (served, keys) = published(&zip_bytes, |_| true, |i| i == 2); + let signers = Signers::for_network(33, Some(URL), &keys).unwrap(); + let dir = tempfile::tempdir().unwrap(); + let store = BlockStore::open(dir.path()).unwrap(); + let trie = TrieTarget::Column(store.db().clone()); + let summary = import( + &served, + &signers, + dir.path(), + &store, + &trie, + &ChainConfig::regtest(), + ) + .await + .unwrap(); + assert_loaded(&store, &blocks, &state, &summary); + assert!( + !dir.path().join(DOWNLOAD_DIR).exists(), + "the download is removed" + ); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn import_stops_without_enough_signatures_or_a_copy_that_hashes_right() { + let (copy, _, _) = Copy::new(2_003, 4); + let zip_bytes = zip(&[(BOOTSTRAP_ENTRY, true, ©.v2())], false); + let attempt = |served: Served, keys: Vec| async move { + let signers = Signers::for_network(33, Some(URL), &keys).unwrap(); + let dir = tempfile::tempdir().unwrap(); + let store = BlockStore::open(dir.path()).unwrap(); + let trie = TrieTarget::Column(store.db().clone()); + let result = import( + &served, + &signers, + dir.path(), + &store, + &trie, + &ChainConfig::regtest(), + ) + .await; + let held = store.head().unwrap(); + (format!("{:#}", result.unwrap_err()), held) + }; + + let (served, keys) = published(&zip_bytes, |i| i == 0, |_| true); + let (error, held) = attempt(served, keys).await; + assert!(error.contains("Not enough valid signatures"), "{error}"); + assert_eq!(held, None); + + // Signed, but every server has a different file. + let (mut served, keys) = published(&zip_bytes, |_| true, |_| true); + for (url, body) in served.0.iter_mut() { + if url.ends_with(".zip") { + body.push(0); + } + } + let (error, held) = attempt(served, keys).await; + assert!( + error.contains("does not match with expected hash"), + "{error}" + ); + assert_eq!(held, None); + + // A signer's index that cannot be read stops it, as in rskj. + let (mut served, keys) = published(&zip_bytes, |_| true, |_| true); + served + .0 + .retain(|url, _| !url.ends_with(&format!("{}/index.json", keys[1]))); + let (error, _) = attempt(served, keys).await; + assert!( + error.contains("Failed to download and parse index"), + "{error}" + ); +} diff --git a/crates/cli/src/config_file.rs b/crates/cli/src/config_file.rs index ddb463e..5c05cd0 100644 --- a/crates/cli/src/config_file.rs +++ b/crates/cli/src/config_file.rs @@ -69,8 +69,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 { @@ -82,7 +82,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>, @@ -207,6 +206,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. @@ -334,11 +336,89 @@ pub struct MiningSection { pub coinbase: Option, pub extra_data: Option, pub refresh_secs: Option, - pub min_gas_price: Option, + pub min_gas_price: Option, + pub gas_limit_target: Option, + pub force_gas_limit: Option, pub submission_rate_limit_ms: Option, pub update_work_on_new_transaction: Option, pub timed: Option, pub flush_every: Option, + /// rskj `miner.stableGasPrice`, which has no flag: rskj sets it in its + /// configuration only. + pub stable_gas_price: Option, +} + +/// rskj `miner.stableGasPrice`: a minimum gas price held stable in a +/// currency, its keys as rskj names them, in this file's snake case. +#[derive(Debug, Clone, Default, PartialEq, Deserialize, Serialize)] +#[serde(deny_unknown_fields)] +pub struct StableGasPriceSection { + pub enabled: Option, + pub refresh_rate: Option, + pub min_stable_gas_price: Option, + pub min_valid_price: Option, + pub max_valid_price: Option, + pub source: Option, +} + +impl StableGasPriceSection { + /// Whether the section says only what rskj's defaults say: off, every + /// price a valid one, and no source, its tables empty if they are there. + /// A node with it runs as one without it. + pub fn is_default(&self) -> bool { + let Self { + enabled, + refresh_rate, + min_stable_gas_price, + min_valid_price, + max_valid_price, + source, + } = self; + *enabled != Some(true) + && refresh_rate.is_none() + && min_stable_gas_price.is_none() + && min_valid_price.is_none_or(|price| price == 0) + && max_valid_price.is_none_or(|price| price == i64::MAX) + && source.as_ref().is_none_or(|source| { + source.method.is_none() + && source + .params + .as_ref() + .is_none_or(|params| *params == StableGasPriceParams::default()) + }) + } +} + +/// rskj `miner.stableGasPrice.source`. +#[derive(Debug, Clone, Default, PartialEq, Deserialize, Serialize)] +#[serde(deny_unknown_fields)] +pub struct StableGasPriceSource { + /// `HTTP_GET`, `ETH_CALL` or `FIXED`, as rskj names them. + pub method: Option, + pub params: Option, +} + +/// rskj `miner.stableGasPrice.source.params`: `url`, `json_path` and +/// `timeout` for `HTTP_GET`; `from`, `to` and `data` for `ETH_CALL`. +#[derive(Debug, Clone, Default, PartialEq, Deserialize, Serialize)] +#[serde(deny_unknown_fields)] +pub struct StableGasPriceParams { + pub url: Option, + pub json_path: Option, + pub timeout: Option, + pub from: Option, + pub to: Option, + pub data: Option, +} + +/// A duration as rskj's HOCON writes one: a number of milliseconds, or a +/// number and a unit (`"6 hours"`). +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] +#[serde(untagged)] +pub enum DurationValue { + Millis(i64), + FractionalMillis(f64), + Text(String), } #[derive(Debug, Default, Deserialize, Serialize)] @@ -453,6 +533,12 @@ pub const ONE_SHOT: &[&str] = &[ "verify_state", "trie_shape", "measure_block_reads", + // --import loads a copy into an empty data directory and refuses one + // that holds a chain, so a file that turned it on would stop every start + // after the first. + "import", + "import_url", + "import_trusted_key", ]; /// Every runtime setting the file can supply. Kept beside the merge so the two @@ -492,6 +578,7 @@ pub const CONFIGURABLE: &[&str] = &[ "max_inbound_per_cidr", "inbound_cidr_prefix", "banned_peers", + "banned_miners", "bootnodes", "no_peer_punishment", "btc_block_cache_entries", @@ -525,6 +612,8 @@ pub const CONFIGURABLE: &[&str] = &[ "mining_extra_data", "mining_refresh_secs", "mining_min_gas_price", + "mining_gas_limit_target", + "mining_force_gas_limit", "mining_submission_rate_limit_ms", "mining_update_work_on_new_transaction", "mine_timed", @@ -560,7 +649,6 @@ pub const CONFIGURABLE: &[&str] = &[ "skip_pow_validation", "consensus_rule", "min_gas_limit", - "max_gas_limit", ]; #[cfg(test)] diff --git a/crates/cli/src/https.rs b/crates/cli/src/https.rs new file mode 100644 index 0000000..7d764d6 --- /dev/null +++ b/crates/cli/src/https.rs @@ -0,0 +1,907 @@ +//! The node's one HTTP client: a `GET` at a time over HTTP/1.1, for `--import` +//! to fetch Rootstock's signed state copies, and for the miner's stable +//! minimum gas price to fetch the price of a bitcoin. +//! +//! The server's certificate is checked against Mozilla's roots (`webpki-roots`) +//! through rustls on `ring`. Rootstock serves its copies from S3 through +//! CloudFront, as plain `200` answers carrying a `Content-Length`; this also +//! reads chunked bodies and bodies that run to the close, and follows +//! redirects. How it fetches is a [`Fetch`]: `--import`'s, `https://` only, +//! and the price's, as Java's `HttpURLConnection` fetches for rskj. Nothing +//! `--import` fetches is trusted for arriving this way: it holds every byte to +//! what the signers signed. + +use anyhow::{anyhow, bail, ensure, Context, Result}; +use std::future::Future; +use std::sync::Arc; +use std::time::Duration; +use tokio::io::{ + AsyncBufRead, AsyncBufReadExt, AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, BufReader, +}; +use tokio::net::TcpStream; +use tokio_rustls::rustls::pki_types::ServerName; +use tokio_rustls::rustls::{self, ClientConfig, RootCertStore}; +use tokio_rustls::TlsConnector; + +/// How one kind of fetch is made. +#[derive(Debug, Clone, Copy)] +pub(crate) struct Fetch { + /// How long connecting, and then the TLS handshake, may each take; `None` + /// for as long as they take. + pub connect_timeout: Option, + /// How long the server may go quiet before the transfer is given up; + /// `None` for as long as it likes. + pub read_timeout: Option, + pub max_redirects: usize, + /// Whether `http://` is fetched as well as `https://`. + pub plain_http: bool, + /// Whether every `2xx` answer is a body, or only `200`. + pub any_success: bool, + /// The redirect codes followed; any other `3xx` is an error. + pub redirects: &'static [u16], + /// Where a redirect's `Location` points, from the URL that answered. + pub resolve: fn(&Url, &str) -> Result, +} + +/// `--import`'s: Rootstock's copies, over `https://` only. +pub(crate) const IMPORT: Fetch = Fetch { + connect_timeout: Some(Duration::from_secs(30)), + read_timeout: Some(Duration::from_secs(60)), + max_redirects: 5, + plain_http: false, + any_success: false, + redirects: &[301, 302, 303, 307, 308], + resolve, +}; + +impl Fetch { + /// As Java's `HttpURLConnection` fetches for rskj's `SimpleHttpClient`: + /// `timeout` to connect and to each read, with no limit where it is zero; + /// `http://` as well as `https://`; every `2xx` an answer; and the + /// redirects Java follows, as it resolves them (`resolve_java`), for 300 + /// to 303 and 307. Java gives up at the 20th request (`http.maxRedirects`), + /// so after 19 redirects. A 305, which Java takes as a proxy to go + /// through, isn't followed: a server would choose where the node's + /// request goes. + pub(crate) fn java(timeout: Duration) -> Self { + let limit = (!timeout.is_zero()).then_some(timeout); + Self { + connect_timeout: limit, + read_timeout: limit, + max_redirects: 19, + plain_http: true, + any_success: true, + redirects: &[300, 301, 302, 303, 307], + resolve: resolve_java, + } + } +} + +/// The most the status line and the headers may take, together. +const MAX_HEAD: u64 = 64 * 1024; +/// The most a chunk-size line, or the trailers after the last chunk, may take. +const MAX_CHUNK_LINE: u64 = 4 * 1024; + +/// Where a body goes as it arrives. +pub(crate) trait Sink: Send { + fn write(&mut self, data: &[u8]) -> Result<()>; +} + +/// Whether a URL is fetched in the clear or over TLS. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum Scheme { + Http, + Https, +} + +impl Scheme { + fn prefix(self) -> &'static str { + match self { + Scheme::Http => "http://", + Scheme::Https => "https://", + } + } + + fn default_port(self) -> u16 { + match self { + Scheme::Http => 80, + Scheme::Https => 443, + } + } +} + +/// A URL, split as a request needs it. +#[derive(Debug, PartialEq, Eq)] +pub(crate) struct Url { + pub scheme: Scheme, + pub host: String, + pub port: u16, + /// The path and query, from the first `/`; `/` for a URL without one. + pub path: String, +} + +/// An `https://` URL, as `--import` fetches. +pub(crate) fn parse_url(url: &str) -> Result { + ensure!( + url.starts_with("https://"), + "{url}: only https:// URLs are fetched" + ); + parse_any_url(url) +} + +/// An `http://` or `https://` URL. +pub(crate) fn parse_any_url(url: &str) -> Result { + let (scheme, rest) = if let Some(rest) = url.strip_prefix("https://") { + (Scheme::Https, rest) + } else if let Some(rest) = url.strip_prefix("http://") { + (Scheme::Http, rest) + } else { + bail!("{url}: only http:// and https:// URLs are fetched") + }; + let rest = rest.split('#').next().unwrap_or_default(); + let (authority, path) = match rest.find(['/', '?']) { + Some(i) if rest.as_bytes()[i] == b'/' => (&rest[..i], rest[i..].to_string()), + Some(i) => (&rest[..i], format!("/{}", &rest[i..])), + None => (rest, "/".to_string()), + }; + // Anything else in a request line would let the URL write headers of its own. + ensure!( + path.bytes().all(|b| b.is_ascii_graphic()), + "{url}: the path has to be printable ASCII, without spaces" + ); + ensure!( + !authority.contains('@'), + "{url}: a URL carrying credentials is not fetched" + ); + ensure!( + !authority.starts_with('['), + "{url}: an IPv6 literal is not supported" + ); + let (host, port) = match authority.split_once(':') { + Some((host, port)) => ( + host, + port.parse::() + .with_context(|| format!("{url}: the port {port:?}"))?, + ), + None => (authority, scheme.default_port()), + }; + ensure!( + !host.is_empty() + && host + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'.'), + "{url}: the host has to be a name or an IPv4 address" + ); + Ok(Url { + scheme, + host: host.to_ascii_lowercase(), + port, + path, + }) +} + +/// What one request came to. +#[derive(Debug, PartialEq, Eq)] +enum Outcome { + /// The body went to the sink: this many bytes of it. + Body(u64), + /// The server pointed elsewhere. + Redirect(String), +} + +/// Fetch `url` into `sink` as `--import` does, following redirects. Returns +/// the body's length. +pub(crate) async fn get(url: &str, sink: &mut dyn Sink) -> Result { + get_with(url, &IMPORT, sink).await +} + +/// Fetch `url` into `sink` as `fetch` says, following redirects. Returns the +/// body's length. +pub(crate) async fn get_with(url: &str, fetch: &Fetch, sink: &mut dyn Sink) -> Result { + let config = tls_config()?; + let mut url = url.to_string(); + for _ in 0..=fetch.max_redirects { + let target = if fetch.plain_http { + parse_any_url(&url)? + } else { + parse_url(&url)? + }; + let outcome = match target.scheme { + Scheme::Https => { + let stream = connect_tls(&target, &config, fetch).await?; + exchange(stream, &target, fetch, sink).await + } + Scheme::Http => { + let stream = connect_tcp(&target, fetch).await?; + exchange(stream, &target, fetch, sink).await + } + }; + match outcome.with_context(|| format!("fetching {url}"))? { + Outcome::Body(n) => return Ok(n), + Outcome::Redirect(location) => url = (fetch.resolve)(&target, &location)?, + } + } + bail!("{url}: more than {} redirects", fetch.max_redirects) +} + +fn tls_config() -> Result> { + let mut roots = RootCertStore::empty(); + roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned()); + let provider = Arc::new(rustls::crypto::ring::default_provider()); + let config = ClientConfig::builder_with_provider(provider) + .with_safe_default_protocol_versions() + .context("TLS: the default protocol versions")? + .with_root_certificates(roots) + .with_no_client_auth(); + Ok(Arc::new(config)) +} + +async fn connect_tcp(url: &Url, fetch: &Fetch) -> Result { + let (host, port) = (url.host.as_str(), url.port); + within(fetch.connect_timeout, TcpStream::connect((host, port))) + .await + .map_err(|_| anyhow!("connecting to {host}:{port} timed out"))? + .with_context(|| format!("connecting to {host}:{port}")) +} + +async fn connect_tls( + url: &Url, + config: &Arc, + fetch: &Fetch, +) -> Result> { + let tcp = connect_tcp(url, fetch).await?; + let host = url.host.as_str(); + let name = ServerName::try_from(url.host.clone()) + .map_err(|_| anyhow!("{host} is not a name a certificate can be checked against"))?; + within( + fetch.connect_timeout, + TlsConnector::from(config.clone()).connect(name, tcp), + ) + .await + .map_err(|_| anyhow!("the TLS handshake with {host} timed out"))? + .with_context(|| format!("TLS with {host}")) +} + +/// `f`, given up after `limit`, or never where there is none. +async fn within( + limit: Option, + f: impl Future, +) -> std::result::Result { + match limit { + Some(limit) => tokio::time::timeout(limit, f).await, + None => Ok(f.await), + } +} + +async fn exchange( + mut stream: S, + url: &Url, + fetch: &Fetch, + sink: &mut dyn Sink, +) -> Result { + let request = format!( + "GET {} HTTP/1.1\r\nHost: {}\r\nUser-Agent: rustock/{}\r\nAccept: */*\r\n\ + Accept-Encoding: identity\r\nConnection: close\r\n\r\n", + url.path, + authority(url), + env!("CARGO_PKG_VERSION") + ); + timed(fetch.read_timeout, stream.write_all(request.as_bytes())).await?; + timed(fetch.read_timeout, stream.flush()).await?; + let mut reader = BufReader::with_capacity(1 << 18, stream); + read_response(&mut reader, fetch, sink).await +} + +/// The host, and the port where it isn't the scheme's, as a `Host` header +/// names them. +fn authority(url: &Url) -> String { + if url.port == url.scheme.default_port() { + url.host.clone() + } else { + format!("{}:{}", url.host, url.port) + } +} + +async fn timed( + limit: Option, + f: impl Future>, +) -> Result { + match within(limit, f).await { + Ok(done) => Ok(done?), + Err(_) => bail!( + "the server sent nothing for {} s", + limit.unwrap_or_default().as_secs() + ), + } +} + +async fn read_response( + r: &mut R, + fetch: &Fetch, + sink: &mut dyn Sink, +) -> Result { + let limit = fetch.read_timeout; + let mut budget = MAX_HEAD; + let status_line = read_line(r, &mut budget, limit).await?; + let mut parts = status_line.splitn(3, ' '); + let version = parts.next().unwrap_or_default(); + ensure!( + version.starts_with("HTTP/1."), + "not an HTTP/1 answer: {status_line:?}" + ); + let status: u16 = parts + .next() + .and_then(|s| s.parse().ok()) + .ok_or_else(|| anyhow!("no status code in {status_line:?}"))?; + + let mut length: Option = None; + let mut chunked = false; + let mut location: Option = None; + loop { + let line = read_line(r, &mut budget, limit).await?; + if line.is_empty() { + break; + } + let (name, value) = line + .split_once(':') + .ok_or_else(|| anyhow!("a header line without a colon: {line:?}"))?; + let value = value.trim(); + match name.trim().to_ascii_lowercase().as_str() { + "content-length" => { + let n: u64 = value + .parse() + .with_context(|| format!("Content-Length {value:?}"))?; + ensure!( + length.is_none_or(|l| l == n), + "two different Content-Length headers" + ); + length = Some(n); + } + "transfer-encoding" => { + let codings: Vec = value + .split(',') + .map(|c| c.trim().to_ascii_lowercase()) + .collect(); + ensure!( + codings == ["chunked"], + "Transfer-Encoding {value:?}: only chunked is read" + ); + chunked = true; + } + "location" => location = Some(value.to_string()), + _ => {} + } + } + + if fetch.redirects.contains(&status) { + return location + .map(Outcome::Redirect) + .ok_or_else(|| anyhow!("HTTP {status} without a Location")); + } + if fetch.any_success { + ensure!((200..300).contains(&status), "HTTP {status}"); + } else { + ensure!(status == 200, "HTTP {status}"); + } + // A chunked body says where it ends whatever the length says (RFC 9112). + let n = if chunked { + read_chunked(r, limit, sink).await? + } else if let Some(len) = length { + read_exact_body(r, len, limit, sink).await? + } else { + read_to_close(r, limit, sink).await? + }; + Ok(Outcome::Body(n)) +} + +/// One line, without its line ending, its bytes taken from `budget`. +async fn read_line( + r: &mut R, + budget: &mut u64, + limit: Option, +) -> Result { + let mut buf = Vec::new(); + let n = timed(limit, (&mut *r).take(*budget).read_until(b'\n', &mut buf)).await?; + ensure!(n > 0, "the connection closed in the middle of an answer"); + ensure!( + buf.ends_with(b"\n"), + "the answer ends, or runs past its limit, inside a line" + ); + *budget -= n as u64; + while buf.last().is_some_and(|b| *b == b'\n' || *b == b'\r') { + buf.pop(); + } + String::from_utf8(buf).context("a line of the answer that isn't UTF-8") +} + +async fn read_exact_body( + r: &mut R, + len: u64, + limit: Option, + sink: &mut dyn Sink, +) -> Result { + let mut left = len; + while left > 0 { + let buf = timed(limit, r.fill_buf()).await?; + ensure!( + !buf.is_empty(), + "the body ends after {} of {len} bytes", + len - left + ); + let n = buf.len().min(usize::try_from(left).unwrap_or(usize::MAX)); + sink.write(&buf[..n])?; + r.consume(n); + left -= n as u64; + } + Ok(len) +} + +async fn read_chunked( + r: &mut R, + limit: Option, + sink: &mut dyn Sink, +) -> Result { + let mut total = 0u64; + loop { + let mut budget = MAX_CHUNK_LINE; + let line = read_line(r, &mut budget, limit).await?; + let size = line.split(';').next().unwrap_or_default().trim(); + ensure!( + !size.is_empty() && size.bytes().all(|b| b.is_ascii_hexdigit()), + "a chunk size {line:?}" + ); + let size = + u64::from_str_radix(size, 16).with_context(|| format!("a chunk size {line:?}"))?; + if size == 0 { + // The trailers, if any, then the blank line that ends the body. + let mut budget = MAX_CHUNK_LINE; + while !read_line(r, &mut budget, limit).await?.is_empty() {} + return Ok(total); + } + read_exact_body(r, size, limit, sink).await?; + total = total + .checked_add(size) + .context("a body of more than 2^64 bytes")?; + let mut budget = 2; + ensure!( + read_line(r, &mut budget, limit).await?.is_empty(), + "a chunk runs past its size" + ); + } +} + +async fn read_to_close( + r: &mut R, + limit: Option, + sink: &mut dyn Sink, +) -> Result { + let mut total = 0u64; + loop { + let buf = match within(limit, r.fill_buf()).await { + Err(_) => bail!( + "the server sent nothing for {} s", + limit.unwrap_or_default().as_secs() + ), + Ok(Ok(buf)) => buf, + // rustls reports a close without TLS's close_notify as an + // unexpected end; for a body that runs to the close, it is the end. + Ok(Err(e)) if e.kind() == std::io::ErrorKind::UnexpectedEof => return Ok(total), + Ok(Err(e)) => return Err(e.into()), + }; + if buf.is_empty() { + return Ok(total); + } + let n = buf.len(); + sink.write(buf)?; + r.consume(n); + total += n as u64; + } +} + +/// Where a redirect's `Location` points, from the URL that answered with it, +/// for `--import`: a URL of the scheme that answered, or an absolute path. +fn resolve(from: &Url, location: &str) -> Result { + let prefix = from.scheme.prefix(); + if location.starts_with(prefix) { + Ok(location.to_string()) + } else if let Some(rest) = location.strip_prefix("//") { + Ok(format!("{prefix}{rest}")) + } else if location.starts_with('/') { + Ok(format!("{prefix}{}{location}", authority(from))) + } else { + bail!("a redirect to {location:?}: only {prefix} URLs and absolute paths are followed") + } +} + +/// Where a redirect's `Location` points as Java's `HttpURLConnection` takes +/// it (`followRedirect`): a URL of the scheme that answered, written in any +/// case; or one without a scheme, resolved against the URL that answered as +/// Java's `URL(URL, String)` resolves it. Java follows none to another +/// scheme. One that names no host, which Java would take to this machine, +/// isn't followed (`parse_any_url`). +fn resolve_java(from: &Url, location: &str) -> Result { + // `URL`'s constructor drops what surrounds the reference up to a space, + // and a `url:` before it. + let location = location.trim_matches(|c: char| c <= ' '); + let location = match location.get(..4) { + Some(head) if head.eq_ignore_ascii_case("url:") => &location[4..], + _ => location, + }; + let prefix = from.scheme.prefix(); + // `isValidProtocol`: a letter, then letters, digits, `+`, `-` and `.`. + let scheme = location + .split_once(':') + .map(|(scheme, _)| scheme) + .filter(|scheme| { + scheme.starts_with(char::is_alphabetic) + && scheme + .chars() + .all(|c| c.is_alphanumeric() || matches!(c, '+' | '-' | '.')) + }); + if let Some(scheme) = scheme { + ensure!( + prefix + .strip_suffix("://") + .is_some_and(|ours| ours.eq_ignore_ascii_case(scheme)), + "a redirect to {location:?}: Java follows none to another scheme" + ); + let Some(rest) = location[scheme.len() + 1..].strip_prefix("//") else { + bail!("a redirect to {location:?} names no host") + }; + return Ok(format!("{prefix}{rest}")); + } + // A host after `//`; after `////`, Java reads a path. + if let Some(rest) = location + .strip_prefix("//") + .filter(|rest| !rest.starts_with("//")) + { + return Ok(format!("{prefix}{rest}")); + } + Ok(format!( + "{prefix}{}{}", + authority(from), + java_relative(&from.path, location) + )) +} + +/// Java's `URL(URL, String)` for a reference without a scheme or a host +/// (`URLStreamHandler.parseURL`): the path and query it names, from the path +/// and query that answered. Empty, or a fragment alone, it names those again; +/// a query alone keeps the directory and not the file, as Java has it. +fn java_relative(from: &str, reference: &str) -> String { + let reference = reference.split('#').next().unwrap_or_default(); + if reference.is_empty() { + return from.to_string(); + } + let base = from.split_once('?').map_or(from, |(path, _)| path); + let (path, query) = match reference.split_once('?') { + Some((path, query)) => (path, Some(query)), + None => (reference, None), + }; + let path = if path.starts_with('/') { + path.to_string() + } else if !path.is_empty() { + let directory = base.rfind('/').map_or("", |at| &base[..=at]); + java_dot_segments(format!("{directory}{path}")) + } else { + let directory = base.rfind('/').map_or("", |at| &base[..at]); + format!("{directory}/") + }; + match query { + Some(query) => format!("{path}?{query}"), + None => path, + } +} + +/// Java's `parseURL` on a path it joined: each `/./` gone, each `/../` with +/// the segment before it where there is one, a `/..` at the end likewise, a +/// `./` at the start and a `.` at the end. +fn java_dot_segments(mut path: String) -> String { + while let Some(at) = path.find("/./") { + path.replace_range(at..at + 2, ""); + } + let mut from = 0; + while let Some(at) = path[from..].find("/../").map(|found| found + from) { + // `lastIndexOf('/', i - 1)`, and Java's check that what goes isn't + // a `/../` the path starts with. + match path[..at].rfind('/') { + Some(before) if !(before == 0 && path.starts_with("/../")) => { + path.replace_range(before..at + 3, ""); + from = 0; + } + _ => from = at + 3, + } + } + while path.ends_with("/..") { + let at = path.find("/..").unwrap_or_default(); + match path[..at].rfind('/') { + Some(before) => path.truncate(before + 1), + None => break, + } + } + if path.starts_with("./") && path.len() > 2 { + path.replace_range(..2, ""); + } + if path.ends_with("/.") { + path.pop(); + } + path +} + +#[cfg(test)] +mod tests { + use super::*; + + impl Sink for Vec { + fn write(&mut self, data: &[u8]) -> Result<()> { + self.extend_from_slice(data); + Ok(()) + } + } + + async fn answer(raw: &[u8]) -> (Result, Vec) { + answer_to(&IMPORT, raw).await + } + + async fn answer_to(fetch: &Fetch, raw: &[u8]) -> (Result, Vec) { + let mut body = Vec::new(); + let mut reader = BufReader::new(raw); + let outcome = read_response(&mut reader, fetch, &mut body).await; + (outcome, body) + } + + #[test] + fn urls_are_split_as_a_request_needs_them() { + let url = + parse_url("https://Import.Mainnet.rskcomputing.net/dbs/mainnet/k/index.json").unwrap(); + assert_eq!( + url, + Url { + scheme: Scheme::Https, + host: "import.mainnet.rskcomputing.net".into(), + port: 443, + path: "/dbs/mainnet/k/index.json".into(), + } + ); + let url = parse_url("https://example.org:8443?x=1#top").unwrap(); + assert_eq!((url.port, url.path.as_str()), (8443, "/?x=1")); + assert_eq!(parse_url("https://example.org").unwrap().path, "/"); + for bad in [ + "http://example.org/", + "https:///path", + "https://user@example.org/", + "https://[::1]/", + "https://example.org:x/", + "https://example.org/a b", + "https://example.org/a\r\nX-Injected: 1", + "https://exa mple.org/", + "https://example.org\r\nX: 1/", + ] { + assert!(parse_url(bad).is_err(), "{bad} was taken"); + } + } + + #[tokio::test] + async fn a_body_of_a_given_length_is_read_whole_and_no_further() { + let (outcome, body) = + answer(b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\nServer: x\r\n\r\nhelloEXTRA").await; + assert_eq!(outcome.unwrap(), Outcome::Body(5)); + assert_eq!(body, b"hello"); + } + + #[tokio::test] + async fn a_body_shorter_than_its_length_is_refused() { + let (outcome, _) = answer(b"HTTP/1.1 200 OK\r\nContent-Length: 9\r\n\r\nhello").await; + assert!(outcome.unwrap_err().to_string().contains("5 of 9")); + } + + #[tokio::test] + async fn a_chunked_body_is_put_back_together() { + let raw = b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nContent-Length: 1\r\n\r\n\ + 5;ext=1\r\nhello\r\n6\r\n world\r\n0\r\nTrailer: t\r\n\r\n"; + let (outcome, body) = answer(raw).await; + assert_eq!(outcome.unwrap(), Outcome::Body(11)); + assert_eq!(body, b"hello world"); + } + + #[tokio::test] + async fn a_chunk_that_overruns_its_size_or_a_bad_size_is_refused() { + let overrun = + b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n3\r\nhello\r\n0\r\n\r\n"; + assert!(answer(overrun).await.0.is_err()); + let signed = b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n+3\r\nhey\r\n0\r\n\r\n"; + assert!(answer(signed).await.0.is_err()); + } + + #[tokio::test] + async fn without_a_length_the_body_runs_to_the_close() { + let (outcome, body) = answer(b"HTTP/1.0 200 OK\r\n\r\nall of it").await; + assert_eq!(outcome.unwrap(), Outcome::Body(9)); + assert_eq!(body, b"all of it"); + } + + #[tokio::test] + async fn a_redirect_is_handed_back_and_its_body_left_unread() { + let raw = b"HTTP/1.1 302 Found\r\nLocation: /elsewhere\r\nContent-Length: 4\r\n\r\nbody"; + let (outcome, body) = answer(raw).await; + assert_eq!(outcome.unwrap(), Outcome::Redirect("/elsewhere".into())); + assert!(body.is_empty()); + } + + #[tokio::test] + async fn anything_but_200_or_a_redirect_is_an_error() { + for raw in [ + &b"HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n"[..], + b"HTTP/1.1 206 Partial Content\r\nContent-Length: 1\r\n\r\nx", + b"HTTP/1.1 301 Moved\r\n\r\n", + b"SSH-2.0-OpenSSH\r\n\r\n", + ] { + assert!(answer(raw).await.0.is_err()); + } + } + + #[tokio::test] + async fn framing_that_can_be_read_two_ways_is_refused() { + let two_lengths = b"HTTP/1.1 200 OK\r\nContent-Length: 1\r\nContent-Length: 2\r\n\r\nab"; + assert!(answer(two_lengths).await.0.is_err()); + let gzip = b"HTTP/1.1 200 OK\r\nTransfer-Encoding: gzip, chunked\r\n\r\n0\r\n\r\n"; + assert!(answer(gzip).await.0.is_err()); + } + + #[tokio::test] + async fn headers_past_their_limit_are_refused() { + let mut raw = b"HTTP/1.1 200 OK\r\nX-Long: ".to_vec(); + raw.extend(std::iter::repeat_n(b'a', MAX_HEAD as usize)); + raw.extend_from_slice(b"\r\n\r\n"); + assert!(answer(&raw).await.0.is_err()); + } + + #[test] + fn redirects_resolve_to_https_only() { + let from = parse_url("https://a.example:8443/x").unwrap(); + assert_eq!( + resolve(&from, "/y?z=1").unwrap(), + "https://a.example:8443/y?z=1" + ); + assert_eq!( + resolve(&from, "//b.example/y").unwrap(), + "https://b.example/y" + ); + assert_eq!( + resolve(&from, "https://c.example/y").unwrap(), + "https://c.example/y" + ); + assert!(resolve(&from, "http://c.example/y").is_err()); + assert!(resolve(&from, "y").is_err()); + } + + /// The price's fetch takes `http://` too, each scheme on its own port, and + /// a redirect stays on the scheme it came from, as Java's does. + #[test] + fn a_plain_http_url_is_split_and_redirected_on_its_own_scheme() { + let url = parse_any_url("http://Price.Example/ticker?ids=bitcoin").unwrap(); + assert_eq!( + url, + Url { + scheme: Scheme::Http, + host: "price.example".into(), + port: 80, + path: "/ticker?ids=bitcoin".into(), + } + ); + assert_eq!(parse_any_url("https://price.example").unwrap().port, 443); + assert!(parse_any_url("ftp://price.example/").is_err()); + assert!(parse_any_url("url").is_err()); + + let from = parse_any_url("http://a.example:8080/x").unwrap(); + assert_eq!(resolve(&from, "/y").unwrap(), "http://a.example:8080/y"); + assert_eq!( + resolve(&from, "//b.example/y").unwrap(), + "http://b.example/y" + ); + assert_eq!( + resolve(&from, "http://c.example/y").unwrap(), + "http://c.example/y" + ); + assert!(resolve(&from, "https://c.example/y").is_err()); + let plain = parse_any_url("http://a.example/x").unwrap(); + assert_eq!(authority(&plain), "a.example"); + } + + /// As Java fetches for rskj: every `2xx` is an answer, 300 to 303 and 307 + /// are followed and 308 isn't, and a timeout of zero is no limit. + /// `--import` keeps to `200` and follows 308. + #[tokio::test] + async fn a_java_fetch_takes_every_2xx_and_follows_javas_redirects() { + let java = Fetch::java(Duration::from_secs(3)); + assert_eq!(java.read_timeout, Some(Duration::from_secs(3))); + assert_eq!(Fetch::java(Duration::ZERO).connect_timeout, None); + assert_eq!(java.max_redirects, 19, "Java's 20th request is its last"); + + let no_content = b"HTTP/1.1 204 No Content\r\nContent-Length: 0\r\n\r\n"; + assert_eq!( + answer_to(&java, no_content).await.0.unwrap(), + Outcome::Body(0) + ); + assert!(answer(no_content).await.0.is_err()); + let (outcome, body) = answer_to( + &java, + b"HTTP/1.1 206 Partial\r\nContent-Length: 2\r\n\r\n42", + ) + .await; + assert_eq!((outcome.unwrap(), body), (Outcome::Body(2), b"42".to_vec())); + + let moved = + |code: u16| format!("HTTP/1.1 {code} Moved\r\nLocation: /p\r\n\r\n").into_bytes(); + for code in [300, 301, 302, 303, 307] { + assert_eq!( + answer_to(&java, &moved(code)).await.0.unwrap(), + Outcome::Redirect("/p".into()), + "{code}" + ); + } + for code in [305, 308] { + assert!(answer_to(&java, &moved(code)).await.0.is_err(), "{code}"); + } + assert_eq!( + answer(&moved(308)).await.0.unwrap(), + Outcome::Redirect("/p".into()) + ); + } + + /// Java's `HttpURLConnection` follows a redirect within its scheme, the + /// scheme written in any case, and resolves one without a scheme as + /// `URL(URL, String)` does. + #[test] + fn a_java_redirect_resolves_as_javas_url_does() { + let from = parse_any_url("https://a.example:8443/x/y?q=1").unwrap(); + let to = |location: &str| resolve_java(&from, location); + assert_eq!(to("https://b.example/z").unwrap(), "https://b.example/z"); + assert_eq!(to("HTTPS://b.example/z").unwrap(), "https://b.example/z"); + assert!(to("http://b.example/z").is_err(), "another scheme"); + assert!(to("ftp://b.example/z").is_err()); + assert!(to("mailto:someone").is_err()); + assert!(to("https:z").is_err(), "no host"); + assert_eq!(to("//c.example/z").unwrap(), "https://c.example/z"); + assert_eq!(to("/z?w=2").unwrap(), "https://a.example:8443/z?w=2"); + assert_eq!(to("z").unwrap(), "https://a.example:8443/x/z"); + assert_eq!(to("./z").unwrap(), "https://a.example:8443/x/z"); + assert_eq!(to("../z").unwrap(), "https://a.example:8443/z"); + assert_eq!(to("z/..").unwrap(), "https://a.example:8443/x/"); + assert_eq!(to("z/.").unwrap(), "https://a.example:8443/x/z/"); + assert_eq!( + to("../../z").unwrap(), + "https://a.example:8443/../z", + "Java keeps a /../ it can't take back" + ); + assert_eq!( + to("?w=2").unwrap(), + "https://a.example:8443/x/?w=2", + "Java keeps the directory, not the file" + ); + assert_eq!(to("").unwrap(), "https://a.example:8443/x/y?q=1"); + assert_eq!(to("#top").unwrap(), "https://a.example:8443/x/y?q=1"); + assert_eq!( + to("a/b:c").unwrap(), + "https://a.example:8443/x/a/b:c", + "a colon after a slash is no scheme" + ); + assert_eq!( + to("////c.example/z").unwrap(), + "https://a.example:8443////c.example/z", + "four slashes are a path" + ); + assert_eq!(to(" \t/z\u{1} ").unwrap(), "https://a.example:8443/z"); + assert_eq!(to("URL:/z").unwrap(), "https://a.example:8443/z"); + assert_eq!( + to("url:https://b.example/z").unwrap(), + "https://b.example/z" + ); + assert!(to("é:z").is_err(), "a protocol Java has no handler for"); + let plain = parse_any_url("http://a.example/x").unwrap(); + assert_eq!( + resolve_java(&plain, "Http://b.example/").unwrap(), + "http://b.example/" + ); + } +} diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 008a7b0..7650ae0 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -1,7 +1,10 @@ use clap::{CommandFactory, FromArgMatches, Parser}; +mod bootstrap_import; mod config_file; +mod https; mod public_ip; +mod stable_gas_price; use rustock_core::config::ChainConfig; use rustock_core::validation::HeaderVerifier; use rustock_storage::BlockStore; @@ -246,6 +249,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 @@ -1050,9 +1063,37 @@ struct Args { /// Minimum gas price to steer mined blocks toward, in wei, as rskj's /// `miner.minGasPrice`: one RSKIP-09 step (1% of the parent's) per - /// block. Absent, each block inherits its parent's. - #[arg(long, value_name = "WEI")] - mining_min_gas_price: Option, + /// block, toward 0 unless set, rskj's default. With the config file's + /// `[mining.stable_gas_price]` on, the price that stands in until a + /// stable one is fetched. A price below zero steers as zero does, as in + /// rskj. + #[arg( + long, + value_name = "WEI", + default_value_t = 0, + allow_negative_numbers = true + )] + mining_min_gas_price: i64, + + /// rskj's `miner.stableGasPrice`: the config file's + /// `[mining.stable_gas_price]`, which has no flag, as rskj has none. + #[arg(skip)] + mining_stable_gas_price: Option, + + /// Gas limit to steer mined blocks toward, as rskj's `targetgaslimit`: + /// one step, a 1024th of the parent's, per block. Absent, rskj's: + /// 7,500,000 on a regtest, as its `regtest.conf` sets it, and 6,800,000 + /// on mainnet and testnet. A target under the chain's minimum gas limit + /// is the minimum. + #[arg(long, value_name = "GAS")] + mining_gas_limit_target: Option, + + /// Steer the gas limit toward the target whatever the parent block used, + /// as rskj's `forcegaslimit`, on by default. `false` follows the gas the + /// parent used instead: up when it used more than two thirds of its gas + /// limit, down otherwise, never past the target. + #[arg(long, default_value_t = true, action = clap::ArgAction::Set)] + mining_force_gas_limit: bool, /// Refuse a merged-mining submission within this many milliseconds of /// the previous one, as rskj's `miner.server.workSubmissionRateLimitInMills`. @@ -1370,10 +1411,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 @@ -1402,6 +1443,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, @@ -1413,7 +1456,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, rskip136, rskip144, rskip351, + /// rskip535, rskip536 and rskipUMM. #[arg(long, value_name = "NAME=HEIGHT")] consensus_rule: Vec, @@ -1421,23 +1465,23 @@ 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 - /// 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, orchid060 and hop401 are accepted at the height this node + /// already reads them at; tbd1000 is accepted and changes nothing (it is + /// rskj's upgrade for RSKIP559, which no network schedules and this node + /// doesn't implement). An upgrade left out below a scheduled one, or + /// scheduled out of its order, is refused, but for reed810 and + /// arrowhead631: this node reads a rule from the upgrade in force, and + /// would read such a schedule otherwise than rskj. #[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. @@ -1479,6 +1523,31 @@ struct Args { /// connects. Off by default, and then no clock is read. #[arg(long, value_name = "FILE")] breakdown_log: Option, + + /// Start from the newest state copy Rootstock publishes, as rskj's + /// `--import` does: read each trusted signer's index, take the highest + /// height at which more than half of them (and at least two) sign the + /// same copy, check those signatures, download the copy, check its + /// SHA-256 against the signed hash, and load its blocks and state. The + /// node then starts from there and syncs on as usual. + /// + /// Mainnet and testnet use rskj's publishing URL and keys. Unlike rskj's, + /// this does not delete a database that holds a chain: it refuses to + /// start instead. + #[arg(long, default_value_t = false)] + import: bool, + + /// With --import: where the copies are published, in place of the + /// network's (rskj's `database.import.url`). Each signer's index is read + /// from `/index.json`. + #[arg(long, value_name = "URL")] + import_url: Option, + + /// With --import: a public key whose signatures count, in uncompressed + /// hex. Repeat it for each key; together they replace the network's + /// (rskj's `database.import.trusted-keys`). + #[arg(long, value_name = "HEX")] + import_trusted_key: Vec, } /// Apply the file underneath the command line, one setting per line so the @@ -1502,7 +1571,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::>() }); @@ -1578,6 +1646,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. @@ -1646,13 +1722,22 @@ fn apply_file_config( apply(matches, "mining_extra_data", f.mining.extra_data.as_ref(), &mut a.mining_extra_data); apply(matches, "mining_refresh_secs", f.mining.refresh_secs.as_ref(), &mut a.mining_refresh_secs); - apply_opt(matches, "mining_min_gas_price", f.mining.min_gas_price.as_ref(), &mut a.mining_min_gas_price); + apply(matches, "mining_min_gas_price", f.mining.min_gas_price.as_ref(), &mut a.mining_min_gas_price); + apply_opt(matches, "mining_gas_limit_target", f.mining.gas_limit_target.as_ref(), + &mut a.mining_gas_limit_target); + apply(matches, "mining_force_gas_limit", f.mining.force_gas_limit.as_ref(), + &mut a.mining_force_gas_limit); apply(matches, "mining_submission_rate_limit_ms", f.mining.submission_rate_limit_ms.as_ref(), &mut a.mining_submission_rate_limit_ms); apply(matches, "mining_update_work_on_new_transaction", f.mining.update_work_on_new_transaction.as_ref(), &mut a.mining_update_work_on_new_transaction); apply_opt(matches, "mine_timed", f.mining.timed.as_ref(), &mut a.mine_timed); apply(matches, "flush_every", f.mining.flush_every.as_ref(), &mut a.flush_every); + // rskj `miner.stableGasPrice`, which has no flag. A section that says + // only what rskj's defaults say is left out: the node runs the same. + if let Some(section) = f.mining.stable_gas_price.as_ref().filter(|s| !s.is_default()) { + a.mining_stable_gas_price = Some(section.clone()); + } apply(matches, "account_tx_rate_limit", f.account_tx_rate_limit.enabled.as_ref(), &mut a.account_tx_rate_limit); @@ -1818,10 +1903,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<()> { @@ -2141,8 +2237,49 @@ async fn run(local_offset: Option) -> Result<()> { BlockStore::open(&args.data_dir)? }); + // Where the trie lives, as the flags choose it: `--import` writes into it, + // and the node opens it further down. + let layout = trie_layout(&args)?; + + // `--import`: Rootstock's newest signed copy goes in before anything reads + // the chain, as rskj's `--import` runs before its node starts. + if args.import { + anyhow::ensure!( + !read_only, + "--import loads a chain into the store, and this store is opened read-only" + ); + let signers = bootstrap_import::Signers::for_network( + args.network_id, + args.import_url.as_deref(), + &args.import_trusted_key, + )?; + let data_dir = std::path::Path::new(&args.data_dir); + bootstrap_import::refuse_held_chain(&store, data_dir)?; + let trie = bootstrap_import::TrieTarget::open( + &layout.backend, + layout.engine, + layout.expected_keys, + &layout.trie_dir, + &store, + )?; + bootstrap_import::import( + &bootstrap_import::Web, + &signers, + data_dir, + &store, + &trie, + &config, + ) + .await?; + } else { + bootstrap_import::refuse_unfinished(std::path::Path::new(&args.data_dir))?; + } + let genesis_hash = setup_genesis(&store, &config)?; info!("Genesis Hash: {:?}", genesis_hash); + if !read_only && rewrite_old_genesis(&store, &config, genesis_hash)? { + info!("Block 0 written again, with its difficulty as its genesis file writes it"); + } let verifier = Arc::new(HeaderVerifier::default_rsk(config.clone())); // The manager takes ownership below; snapshot sync needs the same rules. @@ -2283,43 +2420,22 @@ async fn run(local_offset: Option) -> Result<()> { }; let (event_tx, event_rx) = tokio::sync::mpsc::unbounded_channel(); - 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 gc_config = rustock_storage::epoch_store::EpochConfig { - epochs: args.gc_epochs, - burial_depth: args.gc_burial, - rotate_bytes: args.gc_rotate_mb * (1 << 20), - }; - let rotate_bytes = gc_config.rotate_bytes; - let backend = rustock_storage::epoch_store::TrieBackend::parse(&args.trie_backend, gc_config)?; - let engine = rustock_storage::TrieEngine::parse(&args.trie_engine)?; - // An epoch's key count is bounded by when it rotates, so the default - // is computed rather than configured. - let expected_keys = if args.trie_expected_keys > 0 { - args.trie_expected_keys - } else { - rustock_storage::epoch_backend::keys_for_rotation(rotate_bytes) - }; - if matches!(backend, rustock_storage::epoch_store::TrieBackend::Single) - && engine == rustock_storage::TrieEngine::FlatDb - { - anyhow::bail!( - "--trie-backend single keeps nodes in a column family of the node's main \ - database, which only rocksdb can provide; use --trie-backend external or \ - epoch to run flatdb" - ); - } - - // The node only ever holds an `Arc`, so this is the entire - // extent of the choice: nothing downstream knows which store it got. - let trie_dir = args.trie_dir.clone().unwrap_or_else(|| { - let suffix = if backend.is_collecting() { "trie-epochs" } else { "trie" }; - std::path::Path::new(&args.data_dir).join(suffix).to_string_lossy().into_owned() - }); + // 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(sync_handler_for( + sync_manager.clone(), + event_tx, + args.simulate_height, + checkpoint_defence, + args.network_id, + &args.banned_miners, + )?); + let TrieLayout { + backend, + engine, + expected_keys, + trie_dir, + } = layout; // Always say where the trie is. With three backends and a migration in the // field, "which trie is this node actually reading?" is the first question // worth answering in any log, and the answer should not depend on which @@ -2347,7 +2463,8 @@ async fn run(local_offset: Option) -> Result<()> { // against a blank trie -- no error, no missing file, just a node that cannot // execute anything and looks like it needs a resync. That is worth stopping // for; the operator can always choose a backend, but they cannot recover - // state that was quietly declared absent. + // state that was quietly declared absent. A new database, whose trie is + // empty until block 0's state is built below, starts (`trie_is_missing`). { let has_chain = store.exec_head()?.is_some() || store.head()?.is_some(); let detached_dir_populated = std::path::Path::new(&trie_dir) @@ -2355,7 +2472,7 @@ async fn run(local_offset: Option) -> Result<()> { .map(|mut d| d.next().is_some()) .unwrap_or(false); - if !backend.is_detached() && has_chain && store.trie_cf_is_empty() { + if trie_is_missing(&store, &backend)? { anyhow::bail!( "This database has no trie in it, but --trie-backend is \"{}\".\n\ \n\ @@ -2572,13 +2689,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(), @@ -2586,10 +2696,7 @@ 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(pool_chain_rules(&config)?); if let Some(cache) = &sender_cache { pool_builder = pool_builder.with_sender_cache(cache.clone()); } @@ -2688,10 +2795,9 @@ async fn run(local_offset: Option) -> Result<()> { None }; - let mut block_processor = rustock_execution::BlockProcessor::new( - hardfork_cfg.clone(), - store.clone(), - ); + // The same chain configuration the header verifier uses, and fork + // detection on: see `rule_keeping_processor`. + let mut block_processor = rule_keeping_processor(&hardfork_cfg, &store, &config); if let Some(cache) = &btc_block_cache { block_processor = block_processor.with_btc_block_cache(cache.clone()); } @@ -2699,7 +2805,12 @@ async fn run(local_offset: Option) -> Result<()> { block_processor = block_processor.with_sender_cache(cache.clone()); } block_processor = block_processor.with_execution_threads(args.execution_threads); - if !args.no_peg_event_recording { + // The peg events the Bridge logs nothing for, recorded as blocks are + // committed. One store, which the miner's processor shares, so a block + // this node mines is recorded as a peer's block is. + let legacy_events = if args.no_peg_event_recording { + None + } else { let dir = args .peg_events_dir .clone() @@ -2707,14 +2818,18 @@ async fn run(local_offset: Option) -> Result<()> { match rustock_storage::legacy_event_store::LegacyEventStore::open(&dir) { Ok(st) => { info!("Recording unlogged peg events to {dir}"); - block_processor = block_processor.with_legacy_event_store(std::sync::Arc::new(st)); + Some(std::sync::Arc::new(st)) } // A diagnostic store failing to open must not stop the node from // following the chain. Say so loudly and carry on without it. - Err(e) => tracing::warn!( - "peg event recording disabled: cannot open {dir}: {e}" - ), + Err(e) => { + tracing::warn!("peg event recording disabled: cannot open {dir}: {e}"); + None + } } + }; + if let Some(st) = &legacy_events { + block_processor = block_processor.with_legacy_event_store(st.clone()); } if config.genesis_spec.is_some() { // A chain from an rskj genesis file is rskj's regtest, Bridge @@ -2724,9 +2839,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 { @@ -3477,6 +3593,10 @@ async fn run(local_offset: Option) -> Result<()> { }); } + // Where a block the miner keeps beside the chain leaves the peg decisions + // the Bridge logs nothing for, for the sync service to store once the + // chain takes the block up. + let unlogged_holder = sync_service.unlogged_holder(); // Supervise the sync task: a panic inside it would otherwise be swallowed // by tokio and leave the node running but silently stalled. let sync_handle = tokio::spawn(sync_service.start()); @@ -3487,6 +3607,12 @@ async fn run(local_offset: Option) -> Result<()> { } }); + // rskj's `ETH_CALL` stable gas price runs a call on the node's own + // `EthModule`, which rskj has whether it serves RPC or not. The state a + // call runs on is built further down, once the miner is: the price's + // fetches, on a thread of their own, wait for it, as rskj's wait on its + // `getEthModule()`. + let node_calls: Arc> = Arc::default(); // The miner. It shares the store and trie store with sync but holds its // own block processor: `BlockProcessor` is a handle over those two, and // the sync service has taken ownership of the one built above. @@ -3515,17 +3641,34 @@ async fn run(local_offset: Option) -> Result<()> { let mining_config = rustock_execution::MiningConfig { coinbase_address, extra_data, - // rskj `miner.minGasPrice`, through `MinimumGasPriceCalculator`. - min_gas_price_target: args.mining_min_gas_price.map(alloy_primitives::U256::from), + // rskj `targetgaslimit` and `forcegaslimit`, through + // `GasLimitCalculator`. + gas_limit_target: mining_gas_limit_target( + config.chain_id, + args.mining_gas_limit_target, + ), + force_gas_limit: args.mining_force_gas_limit, + // rskj `miner.minGasPrice`, through `MinimumGasPriceCalculator`; + // or with `miner.stableGasPrice` on, a price held stable in a + // currency, that one standing in until it is fetched. + min_gas_price: stable_gas_price::create( + args.mining_min_gas_price, + args.mining_stable_gas_price.as_ref(), + config.chain_id, + node_eth_call(node_calls.clone()), + )?, ..Default::default() }; - // The miner's processor is wired like the node's: the same caches, - // threads and, on a chain from an rskj genesis file, rskj's regtest - // Bridge constants. A bare processor here ran REMASC against the - // 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()); + // The miner's processor is wired like the node's: the same rules + // (`rule_keeping_processor`), caches, threads, store of the peg + // events the Bridge logs nothing for and, on a chain from an + // rskj genesis file, rskj's regtest Bridge constants. A bare + // processor here ran REMASC against the built-in federation, so the + // first payout (block 15 on regtest) reached a state root no node + // with the right constants reproduces; and one without fork + // detection let the miner name an uncle, or store a block of its + // own, that the node's processor refuses. + let mut miner_processor = rule_keeping_processor(&hardfork_cfg, &store, &config); if let Some(cache) = &btc_block_cache { miner_processor = miner_processor.with_btc_block_cache(cache.clone()); } @@ -3533,6 +3676,9 @@ async fn run(local_offset: Option) -> Result<()> { miner_processor = miner_processor.with_sender_cache(cache.clone()); } miner_processor = miner_processor.with_execution_threads(args.execution_threads); + if let Some(st) = &legacy_events { + miner_processor = miner_processor.with_legacy_event_store(st.clone()); + } if config.genesis_spec.is_some() { miner_processor = miner_processor.with_bridge_constants( rustock_execution::bridge::constants::BridgeConstants::rskj_regtest(), @@ -3545,20 +3691,43 @@ 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 + // connected: the sync service takes its state as the node's, or weighs + // it where it was kept beside the chain, and the peers are sent it, + // best or not. The pool drops what a best block mined, as rskj's + // `TransactionPoolImpl.processBest` does; a block beside the chain + // leaves it, and a switch to it later takes them out then. + let on_committed: Arc< + dyn Fn(&rustock_core::Block, rustock_execution::mining::ImportResult) + Send + Sync, + > = { + let pool = pool.clone(); + let broadcast = rustock_sync::on_mined( + peer_store.clone(), + mined_events.clone(), + tokio::runtime::Handle::current(), + ); + Arc::new( + move |block: &rustock_core::Block, + result: rustock_execution::mining::ImportResult| { + if result == rustock_execution::mining::ImportResult::ImportedBest { + 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, + ) + .with_leave_unlogged(unlogged_holder), + ); let mut server = rustock_execution::MinerServer::new( builder, chain, @@ -3682,7 +3851,11 @@ async fn run(local_offset: Option) -> Result<()> { None => None, }; - if !args.no_rpc { + // The node's state for calls: for its RPC, and for a stable gas price + // from a contract, which rskj's node calls whether it serves RPC or not. + let calls_for_price = + args.mine && stable_gas_price::calls_this_node(args.mining_stable_gas_price.as_ref()); + if !args.no_rpc || calls_for_price { let trie_store: Arc = match &detached_for_readers { Some(t) => t.clone(), None => Arc::new(rustock_storage::RocksDbTrieStore::from_db(store.db().clone())), @@ -3703,6 +3876,12 @@ async fn run(local_offset: Option) -> Result<()> { trie_store: if args.spv { None } else { Some(trie_store) }, proof_fetcher: proof_fetcher.clone(), 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()))), @@ -3751,29 +3930,43 @@ async fn run(local_offset: Option) -> Result<()> { scoring: Some(scoring.clone()), events: events.clone(), }; - // The WebSocket server shares the same `RpcState`, so every method - // available over HTTP works over the socket too -- plus - // `eth_subscribe`, which only means anything on a connection that - // stays open. - if args.ws { - let ws_host = args.rpc_host.clone(); - let ws_port = args.ws_port; - let ws_state = rpc_state.clone(); + // The price's calls run on the same state, without the miner the RPC + // reaches through it: the miner holds the price. + if calls_for_price { + let _ = node_calls.set(rustock_rpc::server::RpcState { + miner: None, + auto_miner: None, + ..rpc_state.clone() + }); + } + if !args.no_rpc { + // The WebSocket server shares the same `RpcState`, so every method + // available over HTTP works over the socket too -- plus + // `eth_subscribe`, which only means anything on a connection that + // stays open. + if args.ws { + let ws_host = args.rpc_host.clone(); + let ws_port = args.ws_port; + let ws_state = rpc_state.clone(); + tokio::spawn(async move { + if let Err(e) = + rustock_rpc::ws::start_ws_server(&ws_host, ws_port, ws_state).await + { + tracing::error!("RPC WebSocket server error: {}", e); + } + }); + } + + let rpc_host = args.rpc_host.clone(); + let rpc_port = args.rpc_port; tokio::spawn(async move { - if let Err(e) = rustock_rpc::ws::start_ws_server(&ws_host, ws_port, ws_state).await + if let Err(e) = + rustock_rpc::server::start_rpc_server(&rpc_host, rpc_port, rpc_state).await { - tracing::error!("RPC WebSocket server error: {}", e); + tracing::error!("RPC server error: {}", e); } }); } - - let rpc_host = args.rpc_host.clone(); - let rpc_port = args.rpc_port; - tokio::spawn(async move { - if let Err(e) = rustock_rpc::server::start_rpc_server(&rpc_host, rpc_port, rpc_state).await { - tracing::error!("RPC server error: {}", e); - } - }); } // Independent of the RPC server: metrics are useful on a node that @@ -3909,15 +4102,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 @@ -3995,8 +4214,35 @@ fn run_export_blocks( Ok(()) } +/// The gas limit a miner steers toward: `--mining-gas-limit-target`, or +/// rskj's `targetgaslimit` where none is set, 6,800,000, and on a regtest +/// 7,500,000, which rskj's `regtest.conf` sets. +fn mining_gas_limit_target(chain_id: u8, set: Option) -> u64 { + set.unwrap_or(if matches!(chain_id, 30 | 31) { + rustock_execution::mining::GAS_LIMIT_TARGET + } else { + rustock_execution::mining::REGTEST_GAS_LIMIT_TARGET + }) +} + +/// rskj `EthModule.call`, as its `EthCallMinGasPriceProvider` makes one: the +/// node's own `eth_call`, run once the node's state is open to calls, and +/// its answer, or its error. +fn node_eth_call( + node: Arc>, +) -> stable_gas_price::EthCaller { + Arc::new(move |params: &serde_json::Value| { + let response = rustock_rpc::call::eth_call(serde_json::Value::from(1), params, node.wait()); + match (response.result, response.error) { + (_, Some(error)) => anyhow::bail!("{} (code {})", error.message, error.code), + (Some(serde_json::Value::String(output)), None) => Ok(output), + (other, None) => anyhow::bail!("an answer that is no hex string: {other:?}"), + } + }) +} + /// 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. @@ -4005,17 +4251,16 @@ fn apply_chain_options(mut config: ChainConfig, args: &Args) -> Result Result config.activation_heights.rskip97 = 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)), } } @@ -4054,9 +4313,6 @@ fn apply_chain_options(mut config: ChainConfig, args: &Args) -> Result Resu Ok(root) } +/// Where the node keeps its trie, as the flags choose it: what `--import` +/// writes into, and what the node then opens. +struct TrieLayout { + backend: rustock_storage::epoch_store::TrieBackend, + engine: rustock_storage::TrieEngine, + expected_keys: u64, + trie_dir: String, +} + +fn trie_layout(args: &Args) -> Result { + let gc_config = rustock_storage::epoch_store::EpochConfig { + epochs: args.gc_epochs, + burial_depth: args.gc_burial, + rotate_bytes: args.gc_rotate_mb * (1 << 20), + }; + let rotate_bytes = gc_config.rotate_bytes; + let backend = rustock_storage::epoch_store::TrieBackend::parse(&args.trie_backend, gc_config)?; + let engine = rustock_storage::TrieEngine::parse(&args.trie_engine)?; + // An epoch's key count is bounded by when it rotates, so the default + // is computed rather than configured. + let expected_keys = if args.trie_expected_keys > 0 { + args.trie_expected_keys + } else { + rustock_storage::epoch_backend::keys_for_rotation(rotate_bytes) + }; + if matches!(backend, rustock_storage::epoch_store::TrieBackend::Single) + && engine == rustock_storage::TrieEngine::FlatDb + { + anyhow::bail!( + "--trie-backend single keeps nodes in a column family of the node's main \ + database, which only rocksdb can provide; use --trie-backend external or \ + epoch to run flatdb" + ); + } + + // The node only ever holds an `Arc`, so this is the entire + // extent of the choice: nothing downstream knows which store it got. + let trie_dir = args.trie_dir.clone().unwrap_or_else(|| { + let suffix = if backend.is_collecting() { + "trie-epochs" + } else { + "trie" + }; + std::path::Path::new(&args.data_dir) + .join(suffix) + .to_string_lossy() + .into_owned() + }); + Ok(TrieLayout { + backend, + engine, + expected_keys, + trie_dir, + }) +} + fn setup_genesis(store: &BlockStore, config: &ChainConfig) -> Result { if let Some(genesis) = store.canonical_hash(0)? { return Ok(genesis); @@ -4217,6 +4529,71 @@ fn setup_genesis(store: &BlockStore, config: &ChainConfig) -> Result Result { + // Under the chain's own heights, which a header's hash depends on. + let genesis = config + .genesis_header() + .with_activations(&config.activation_heights); + if genesis.hash() != hash { + return Ok(false); + } + match store.header(hash)? { + Some(stored) if stored.hash() != hash => { + store.put_header_with_hash(hash, &genesis)?; + Ok(true) + } + _ => Ok(false), + } +} + +/// Whether the node would start against a trie that isn't there: blocks ran +/// here, so the trie holds the state they left, and the trie it would read, +/// the main database's own, is empty (see `examples/detach_trie.rs`). A new +/// database holds block 0's header (`setup_genesis`) and no trie until block +/// 0's state is built (`load_or_build_state`), and a node that has run no +/// block builds that state again whatever its trie holds, so neither is +/// refused. +fn trie_is_missing( + store: &BlockStore, + backend: &rustock_storage::epoch_store::TrieBackend, +) -> Result { + Ok(!backend.is_detached() && store.exec_head()?.is_some() && store.trie_cf_is_empty()) +} + +/// A block processor that holds a block to what every node holds it to: the +/// chain's own constants, for the header rules an uncle meets, and fork +/// detection on, as rskj runs `ForkDetectionDataRule` on every block it +/// imports and on every uncle. The node's processor and the miner's are both +/// built from it, so a block this node mines meets the rules a peer's does +/// (rskj connects both through `blockchain.tryToConnect`). +/// +/// Fork detection reads the 449 headers below each block'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. +fn rule_keeping_processor( + hardfork_cfg: &rustock_execution::RskHardforkConfig, + store: &Arc, + config: &Arc, +) -> rustock_execution::BlockProcessor { + rustock_execution::BlockProcessor::new(hardfork_cfg.clone(), store.clone()) + .with_chain_config(config.clone()) + .with_fork_detection_validation(true) +} + /// Runs an rskj database import and returns; the node is not started. /// /// Order matters: state first, then blocks, then the derived chain metadata. @@ -4954,6 +5331,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; @@ -5459,6 +5858,23 @@ mod genesis_tests { Args::parse_from(argv) } + /// rskj's miner aims for its `targetgaslimit`: 6,800,000 where its + /// configuration sets none, as on mainnet and testnet, and 7,500,000 on + /// its regtest, whose `regtest.conf` sets it. A target set is the target, + /// and the force switch is on unless set off, as rskj's `forcegaslimit`. + #[test] + fn a_miner_aims_for_rskjs_gas_limit_target() { + assert_eq!(mining_gas_limit_target(30, None), 6_800_000); + assert_eq!(mining_gas_limit_target(31, None), 6_800_000); + assert_eq!(mining_gas_limit_target(33, None), 7_500_000); + assert_eq!(mining_gas_limit_target(33, Some(9_000_000)), 9_000_000); + + assert!(regtest_args(&[]).mining_force_gas_limit); + let off = regtest_args(&["--mining-force-gas-limit", "false", "--mining-gas-limit-target", "8000000"]); + assert!(!off.mining_force_gas_limit); + assert_eq!(off.mining_gas_limit_target, Some(8_000_000)); + } + /// The lab's rskj regtest genesis (`tests/fixtures/rskj-regtest-genesis.json`, /// sixteen funded accounts, 25M gas limit). rskj gives it this hash: /// block 1 of every chain mined from it names it as parent. Getting there @@ -5479,6 +5895,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(&[ @@ -5486,20 +5963,58 @@ mod genesis_tests { "--consensus-rule", "rskip97=0", "--min-gas-limit", - "3000000", - "--max-gas-limit", - "18446744073709551615", + "5000000", ]); let config = apply_chain_options(ChainConfig::regtest(), &args).unwrap(); assert!(config.skip_pow_validation); assert_eq!(config.activation_heights.rskip97, Some(0)); - assert_eq!((config.min_gas_limit, config.max_gas_limit), (3_000_000, u64::MAX)); + assert_eq!(config.min_gas_limit, 5_000_000); let disabled = regtest_args(&["--consensus-rule", "rskip97=-1"]); 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=9000000"]); + let config = apply_chain_options(ChainConfig::mainnet(), &reed810).unwrap(); + let heights = &config.activation_heights; + assert_eq!( + (heights.rskip144, heights.rskip351), + (Some(9_000_000), Some(9_000_000)) + ); + let (ladder, _) = hardfork_config(&config).unwrap(); + assert!(ladder.runs_sublists(9_000_000) && !ladder.runs_sublists(8_999_999)); + // Below mainnet's earlier upgrades, reed810 would bring their rules + // down with it on this ladder: refused. + let low = mainnet_args(&["--allow-chain-overrides", "--hardfork", "reed810=100"]); + assert!(apply_chain_options(ChainConfig::mainnet(), &low).is_err()); + // An upgrade left out below a scheduled one is refused: rskj would + // have its rules off, and this ladder on from the later upgrade. + let hole = regtest_args(&["--hardfork", "lovell700=-1"]); + assert!(apply_chain_options(ChainConfig::regtest(), &hole).is_err()); + + // 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"]); @@ -5529,7 +6044,7 @@ mod genesis_tests { "--consensus-rule", "rskipUMM=2392700", "--consensus-rule", "rskip351=9002504", "--consensus-rule", "rskip535=9002504", "--consensus-rule", "rskip536=8804200", "--skip-pow-validation", - "--min-gas-limit", "3000000", "--max-gas-limit", "18446744073709551615", + "--min-gas-limit", "3000000", ]; /// Without the opt-in, mainnet is mainnet: every override refused, and a @@ -5548,7 +6063,7 @@ mod genesis_tests { assert_eq!(plain.activation_heights.rskip144, None); // 3,000,000 since the rskj parity fix (rskj `Constants.getMinGasLimit`); // this asserts mainnet's own defaults, so it tracks them. - assert_eq!((plain.min_gas_limit, plain.max_gas_limit), (3_000_000, 1_000_000_000)); + assert_eq!(plain.min_gas_limit, 3_000_000); assert!(!plain.skip_pow_validation); assert!(plain.hardfork_overrides.is_empty()); let (ladder, unmodelled) = hardfork_config(&plain).unwrap(); @@ -5566,14 +6081,19 @@ 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'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); assert!(config.skip_pow_validation); - assert_eq!((config.min_gas_limit, config.max_gas_limit), (3_000_000, u64::MAX)); + assert_eq!(config.min_gas_limit, 3_000_000); assert!(config.genesis_spec.is_none(), "mainnet's genesis stays"); let (ladder, unmodelled) = hardfork_config(&config).unwrap(); - assert_eq!(unmodelled, vec!["orchid060".to_string(), "tbd1000".to_string()]); + assert_eq!(unmodelled, vec!["tbd1000".to_string()]); assert!(!ladder.has_rskip144(9_002_503)); assert!(ladder.has_rskip144(9_003_580)); assert!(ladder.has_rskip351(9_003_580)); @@ -5682,4 +6202,156 @@ mod genesis_tests { let t = build_genesis_state(&ChainConfig::testnet(), &store_t).unwrap(); assert_ne!(m.compute_hash(&store_m), t.compute_hash(&store_t)); } + + /// A store from before block 0 kept its difficulty as its genesis file + /// writes it: its block 0 reads back to another hash than its key, the + /// network's genesis hash. `rewrite_old_genesis` writes the chain's header + /// in its place, once; a store whose block 0 reads back to its key is left + /// as it is, and so is one that holds another chain's. + #[test] + fn an_old_stores_block_0_is_written_again_to_read_back_to_its_hash() { + for config in [ChainConfig::mainnet(), ChainConfig::testnet()] { + let dir = tempfile::tempdir().unwrap(); + let store = BlockStore::open(dir.path()) + .unwrap() + .with_activations(config.activation_heights.clone()); + let hash = config.known_genesis_hash().unwrap(); + let old = rustock_core::Header { + kept: None, + ..config.genesis_header() + } + .with_activations(&config.activation_heights); + store.put_header_with_hash(hash, &old).unwrap(); + store.put_canonical_hash(0, hash).unwrap(); + assert_ne!(store.header(hash).unwrap().unwrap().hash(), hash); + + assert!(rewrite_old_genesis(&store, &config, hash).unwrap()); + assert_eq!(store.header(hash).unwrap().unwrap().hash(), hash); + assert!(!rewrite_old_genesis(&store, &config, hash).unwrap(), "once"); + } + + let dir = tempfile::tempdir().unwrap(); + let config = ChainConfig::mainnet(); + let store = BlockStore::open(dir.path()) + .unwrap() + .with_activations(config.activation_heights.clone()); + let hash = setup_genesis(&store, &config).unwrap(); + assert!(!rewrite_old_genesis(&store, &config, hash).unwrap()); + assert!(!rewrite_old_genesis(&store, &ChainConfig::testnet(), hash).unwrap()); + } + + /// The node's block processor and its miner's are built by the same + /// function, and it turns fork detection on: a block this node mines, and + /// the uncles it names, meet the rule a peer's block meets. + #[test] + fn the_processor_the_node_and_its_miner_run_checks_fork_detection() { + let dir = tempfile::tempdir().unwrap(); + let store = Arc::new(BlockStore::open(dir.path()).unwrap()); + let config = Arc::new(ChainConfig::regtest()); + let (ladder, _) = hardfork_config(&config).unwrap(); + assert!(rule_keeping_processor(&ladder, &store, &config).validates_fork_detection()); + } + + /// A new database holds block 0's header and no trie until block 0's + /// state is built, after the check: it starts on the default backend, on + /// every network. One whose blocks ran and whose own trie is empty is + /// refused, as upstream's check refuses it, and one whose trie is there + /// isn't. + #[test] + fn a_new_database_starts_and_one_whose_trie_is_gone_does_not() { + use rustock_storage::epoch_store::TrieBackend; + for config in [ + ChainConfig::mainnet(), + ChainConfig::testnet(), + ChainConfig::regtest(), + ] { + let dir = tempfile::tempdir().unwrap(); + let store = BlockStore::open(dir.path()) + .unwrap() + .with_activations(config.activation_heights.clone()); + let hash = setup_genesis(&store, &config).unwrap(); + assert!(store.trie_cf_is_empty()); + assert!(!trie_is_missing(&store, &TrieBackend::Single).unwrap()); + + store + .set_exec_head(hash, alloy_primitives::B256::repeat_byte(1)) + .unwrap(); + assert!(trie_is_missing(&store, &TrieBackend::Single).unwrap()); + assert!(!trie_is_missing(&store, &TrieBackend::External).unwrap()); + + rustock_storage::RocksDbTrieStore::from_db(store.db().clone()).put(b"node", b"bytes"); + assert!(!trie_is_missing(&store, &TrieBackend::Single).unwrap()); + } + } +} + +#[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/cli/src/public_ip.rs b/crates/cli/src/public_ip.rs index 1196524..07466db 100644 --- a/crates/cli/src/public_ip.rs +++ b/crates/cli/src/public_ip.rs @@ -291,7 +291,10 @@ mod tests { .with_single_cert(vec![CertificateDer::from(der(cert))], key) .unwrap(); let acceptor = TlsAcceptor::from(Arc::new(config)); - let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + // Bound to the address `localhost` resolves to first, which the lookup + // tries first. On Windows that is ::1, and Windows takes two seconds to + // refuse a connection, longer than SHORT, before trying 127.0.0.1. + let listener = TcpListener::bind("localhost:0").await.unwrap(); let port = listener.local_addr().unwrap().port(); let hits = Arc::new(AtomicUsize::new(0)); let counter = hits.clone(); @@ -330,7 +333,8 @@ mod tests { } async fn raw_listener(reply: &'static [u8]) -> Server { - let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + // Where `localhost` resolves first, as in `serve_tls`. + let listener = TcpListener::bind("localhost:0").await.unwrap(); let port = listener.local_addr().unwrap().port(); let hits = Arc::new(AtomicUsize::new(0)); let counter = hits.clone(); @@ -508,7 +512,15 @@ mod tests { let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); let port = listener.local_addr().unwrap().port(); drop(listener); - let err = lookup_err(&trusting_test_ca(), &format!("https://localhost:{port}/")).await; + // Windows takes two seconds to refuse each of `localhost`'s two + // addresses, so this lookup is given longer than SHORT to fail. + let url = format!("https://localhost:{port}/"); + let err = format!( + "{:#}", + lookup_with(&trusting_test_ca(), &url, Duration::from_secs(10)) + .await + .unwrap_err() + ); assert!(err.contains("connect failed"), "{err}"); } diff --git a/crates/cli/src/stable_gas_price.rs b/crates/cli/src/stable_gas_price.rs new file mode 100644 index 0000000..54d98d8 --- /dev/null +++ b/crates/cli/src/stable_gas_price.rs @@ -0,0 +1,1138 @@ +//! The miner's stable minimum gas price, rskj's `miner.stableGasPrice`: its +//! settings, read as rskj reads them (`StableMinGasPriceSystemConfig`), its +//! two sources, and the provider rskj's `MinGasPriceProviderFactory` picks. +//! +//! A web API answers a bitcoin's price as JSON, read at a JSON pointer +//! (`HttpGetMinGasPriceProvider`); or a contract on this chain answers it to +//! an `eth_call` on this node (`EthCallMinGasPriceProvider`). Each answer is +//! read as rskj reads it, the web's as Jackson reads JSON (`java`). The +//! provider that keeps the price, and falls back on the fixed one, is +//! `rustock_execution::mining::StableMinGasPriceProvider`. + +mod java; + +use crate::config_file::{DurationValue, StableGasPriceParams, StableGasPriceSection}; +use crate::https; +use anyhow::{anyhow, bail, ensure, Context, Result}; +use java::JsonPointer; +use rustock_execution::mining::{ + BtcExchangeRateSource, FixedMinGasPriceProvider, MinGasPriceProvider, MinGasPriceProviderType, + StableMinGasPriceProvider, +}; +use serde_json::{json, Value}; +use std::fmt; +use std::sync::Arc; +use std::time::Duration; +use tracing::{error, info}; + +/// rskj `StableMinGasPriceSystemConfig`. +#[derive(Debug, Clone, PartialEq)] +pub struct StableGasPriceConfig { + pub enabled: bool, + /// `refreshRate`, in milliseconds. + pub refresh_rate_millis: i64, + pub min_stable_gas_price: i64, + pub method: MinGasPriceProviderType, + pub min_valid_price: i64, + pub max_valid_price: i64, + params: StableGasPriceParams, +} + +/// rskj `HttpGetStableMinGasSystemConfig`. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct HttpGetConfig { + pub url: String, + pub json_path: String, + pub timeout_millis: i64, +} + +/// rskj `EthCallMinGasPriceSystemConfig`. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct EthCallConfig { + /// The contract called, rskj's `getAddress`. + pub to: String, + pub from: String, + pub data: String, +} + +impl StableGasPriceConfig { + /// As rskj's constructor reads `miner.stableGasPrice`: off unless + /// `enabled`, and off, no refresh rate, no stable price and the method + /// `FIXED`. On, `source.method` must name one of rskj's three, or rskj + /// doesn't start. The valid range is read either way: 0 to a `long`'s + /// largest unless set. + pub fn read(section: &StableGasPriceSection) -> Result { + let enabled = section.enabled.unwrap_or(false); + let refresh_rate_millis = match §ion.refresh_rate { + Some(rate) if enabled => { + duration_millis(rate).context("[mining.stable_gas_price] refresh_rate")? + } + _ => 0, + }; + let min_stable_gas_price = if enabled { + section.min_stable_gas_price.unwrap_or(0) + } else { + 0 + }; + let source = section.source.clone().unwrap_or_default(); + let method = if enabled { + let method = source.method.as_deref().ok_or_else(|| { + anyhow!("[mining.stable_gas_price] is enabled and names no source.method") + })?; + method_named(method)? + } else { + MinGasPriceProviderType::Fixed + }; + Ok(Self { + enabled, + refresh_rate_millis, + min_stable_gas_price, + method, + min_valid_price: section.min_valid_price.unwrap_or(0), + max_valid_price: section.max_valid_price.unwrap_or(i64::MAX), + params: source.params.unwrap_or_default(), + }) + } + + /// rskj `getHttpGetConfig`: `url`, `json_path` and `timeout`, each + /// required. + pub fn http_get(&self) -> Result { + let missing = |key: &str| { + anyhow!("[mining.stable_gas_price.source.params] names no {key}, which HTTP_GET needs") + }; + let params = &self.params; + let url = params.url.clone().ok_or_else(|| missing("url"))?; + let json_path = params + .json_path + .clone() + .ok_or_else(|| missing("json_path"))?; + let timeout = params.timeout.as_ref().ok_or_else(|| missing("timeout"))?; + Ok(HttpGetConfig { + url, + json_path, + timeout_millis: duration_millis(timeout) + .context("[mining.stable_gas_price.source.params] timeout")?, + }) + } + + /// rskj `getEthCallConfig`: `to`, `from` and `data`, each required. + pub fn eth_call(&self) -> Result { + let missing = |key: &str| { + anyhow!("[mining.stable_gas_price.source.params] names no {key}, which ETH_CALL needs") + }; + let params = &self.params; + Ok(EthCallConfig { + to: params.to.clone().ok_or_else(|| missing("to"))?, + from: params.from.clone().ok_or_else(|| missing("from"))?, + data: params.data.clone().ok_or_else(|| missing("data"))?, + }) + } +} + +/// rskj's `getEnum(MinGasPriceProviderType.class, "source.method")`: a +/// constant's name, exactly. +fn method_named(name: &str) -> Result { + match name { + "FIXED" => Ok(MinGasPriceProviderType::Fixed), + "HTTP_GET" => Ok(MinGasPriceProviderType::HttpGet), + "ETH_CALL" => Ok(MinGasPriceProviderType::EthCall), + other => bail!( + "[mining.stable_gas_price] source.method {other:?} is none of FIXED, HTTP_GET and ETH_CALL" + ), + } +} + +/// Whether the settings have the miner's price from an `eth_call` on this +/// node, which needs the node's state open to calls. +pub fn calls_this_node(section: Option<&StableGasPriceSection>) -> bool { + section.is_some_and(|section| { + section.enabled == Some(true) + && section.source.as_ref().and_then(|s| s.method.as_deref()) == Some("ETH_CALL") + }) +} + +/// A duration as rskj's HOCON reads it (`SimpleConfig.parseDuration`), in the +/// milliseconds `Duration.toMillis` gives: a number alone is milliseconds; +/// otherwise a number and one of HOCON's units. +pub fn duration_millis(value: &DurationValue) -> Result { + let nanos = match value { + DurationValue::Millis(millis) => millis.saturating_mul(1_000_000), + DurationValue::FractionalMillis(millis) => (millis * 1_000_000.0) as i64, + DurationValue::Text(text) => hocon_duration_nanos(text)?, + }; + Ok(nanos.div_euclid(1_000_000)) +} + +/// `SimpleConfig.parseDuration`: the letters at the end are the unit, a +/// singular one longer than two letters read as its plural, and the rest the +/// number, a `long` where it is all digits and a Java double otherwise. +fn hocon_duration_nanos(input: &str) -> Result { + let s = input.trim(); + let unit_at = s + .char_indices() + .rev() + .take_while(|(_, c)| c.is_alphabetic()) + .last() + .map_or(s.len(), |(at, _)| at); + let (number, unit) = (s[..unit_at].trim(), &s[unit_at..]); + ensure!(!number.is_empty(), "no number in the duration {input:?}"); + let unit = if unit.len() > 2 && !unit.ends_with('s') { + format!("{unit}s") + } else { + unit.to_string() + }; + let nanos_per_unit: i64 = match unit.as_str() { + "" | "ms" | "millis" | "milliseconds" => 1_000_000, + "us" | "micros" | "microseconds" => 1_000, + "ns" | "nanos" | "nanoseconds" => 1, + "d" | "days" => 86_400_000_000_000, + "h" | "hours" => 3_600_000_000_000, + "s" | "seconds" => 1_000_000_000, + "m" | "minutes" => 60_000_000_000, + other => bail!("the duration {input:?} has a unit HOCON doesn't read: {other:?}"), + }; + let unsigned = number.strip_prefix(['+', '-']).unwrap_or(number); + if !unsigned.is_empty() && unsigned.bytes().all(|b| b.is_ascii_digit()) { + let count: i64 = number + .parse() + .with_context(|| format!("the duration {input:?} is past a long"))?; + return Ok(count.saturating_mul(nanos_per_unit)); + } + let count = java::parse_double(number) + .ok_or_else(|| anyhow!("the duration {input:?} has no number HOCON reads"))?; + Ok((count * nanos_per_unit as f64) as i64) +} + +/// rskj `MinGasPriceProviderFactory.create`: the fixed price, or a stable one +/// over it where `miner.stableGasPrice` is on. `eth_call` runs a call on +/// this node for the `ETH_CALL` source. +pub fn create( + fixed_min_gas_price: i64, + section: Option<&StableGasPriceSection>, + chain_id: u8, + eth_call: EthCaller, +) -> Result> { + create_with( + fixed_min_gas_price, + section, + chain_id, + eth_call, + Arc::new(simple_http_get), + ) +} + +/// [`create`], with what the web source fetches through. +fn create_with( + fixed_min_gas_price: i64, + section: Option<&StableGasPriceSection>, + chain_id: u8, + eth_call: EthCaller, + http_get: HttpGet, +) -> Result> { + let fixed: Arc = + Arc::new(FixedMinGasPriceProvider::new(fixed_min_gas_price)); + // rskj's `reference.conf` has the section, off, where a node sets none. + let config = StableGasPriceConfig::read(section.unwrap_or(&StableGasPriceSection::default()))?; + if !config.enabled { + info!( + target: "rustock::mining", + "Stable min gas price is disabled. Falling back to FIXED with fixedMinGasPrice: {fixed_min_gas_price}" + ); + return Ok(fixed); + } + let source: Arc = match config.method { + MinGasPriceProviderType::HttpGet => { + info!(target: "rustock::mining", "Creating 'Http-Get' stable min gas price provider"); + Arc::new(HttpGetSource::with_get(config.http_get()?, http_get)?) + } + MinGasPriceProviderType::EthCall => { + info!(target: "rustock::mining", "Creating 'Eth-Call' stable min gas price provider"); + Arc::new(EthCallSource::new(config.eth_call()?, chain_id, eth_call)) + } + MinGasPriceProviderType::Fixed => { + info!( + target: "rustock::mining", + "Creating 'Fixed' min gas price provider with fixedMinGasPrice: {fixed_min_gas_price}" + ); + return Ok(fixed); + } + }; + Ok(Arc::new(StableMinGasPriceProvider::new( + source, + fixed, + config.min_stable_gas_price, + config.refresh_rate_millis, + config.min_valid_price, + config.max_valid_price, + ))) +} + +/// What fetches a URL's body, with a timeout in milliseconds, as rskj's +/// `SimpleHttpClient.doGet` does. +pub type HttpGet = Arc Result + Send + Sync>; + +/// rskj `HttpGetMinGasPriceProvider`'s source: a bitcoin's price from a web +/// API, at a JSON pointer. +pub struct HttpGetSource { + config: HttpGetConfig, + pointer: JsonPointer, + get: HttpGet, +} + +impl fmt::Debug for HttpGetSource { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("HttpGetSource") + .field("config", &self.config) + .finish_non_exhaustive() + } +} + +impl HttpGetSource { + /// rskj's constructor reads `jsonPath` as a JSON pointer + /// (`JsonPointer.valueOf`), which refuses one that isn't empty and doesn't + /// start with `/`, and its client refuses a timeout past a Java `int` of + /// milliseconds: rskj doesn't start on either. + pub fn with_get(config: HttpGetConfig, get: HttpGet) -> Result { + let pointer = JsonPointer::compile(&config.json_path) + .map_err(|e| anyhow!("[mining.stable_gas_price.source.params] json_path: {e}"))?; + ensure!( + config.timeout_millis <= i64::from(i32::MAX), + "[mining.stable_gas_price.source.params] timeout of {} ms is more than rskj's client takes", + config.timeout_millis + ); + Ok(Self { + config, + pointer, + get, + }) + } +} + +impl BtcExchangeRateSource for HttpGetSource { + /// rskj `getBtcExchangeRate`: the answer, unless blank, read at the + /// pointer. + fn btc_exchange_rate(&self) -> Option { + let response = match (self.get)(&self.config.url, self.config.timeout_millis) { + Ok(response) => response, + Err(e) => { + error!(target: "rustock::mining", "Error getting min gas price from web provider: {e:#}"); + return None; + } + }; + if is_blank(&response) { + return None; + } + parse_price(&response, &self.pointer) + } + + fn provider_type(&self) -> MinGasPriceProviderType { + MinGasPriceProviderType::HttpGet + } +} + +/// The most of an answer read. rskj reads all of one; no price API answers +/// with more than a few hundred bytes, and a server that sent without end +/// would otherwise have the node hold all it sent. +const MAX_ANSWER: usize = 1 << 20; + +/// rskj `SimpleHttpClient.doGet`: the body of a `2xx` answer, as UTF-8, with +/// the lines Java's `readLine` loop reads run together. +fn simple_http_get(url: &str, timeout_millis: i64) -> Result { + // `HttpURLConnection.setConnectTimeout` refuses one below zero. + let timeout = + u64::try_from(timeout_millis).map_err(|_| anyhow!("timeouts can't be negative"))?; + let fetch = https::Fetch::java(Duration::from_millis(timeout)); + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .context("a runtime for the price's fetch")?; + let mut answer = Capped::default(); + runtime.block_on(https::get_with(url, &fetch, &mut answer))?; + Ok(joined_lines(&answer.0)) +} + +/// A body as Java's `readLine` loop gives it: UTF-8, every line ending gone. +fn joined_lines(body: &[u8]) -> String { + String::from_utf8_lossy(body) + .chars() + .filter(|c| !matches!(c, '\n' | '\r')) + .collect() +} + +/// An answer kept whole, up to [`MAX_ANSWER`]. +#[derive(Default)] +struct Capped(Vec); + +impl https::Sink for Capped { + fn write(&mut self, data: &[u8]) -> Result<()> { + ensure!( + self.0.len() + data.len() <= MAX_ANSWER, + "the answer runs past {MAX_ANSWER} bytes" + ); + self.0.extend_from_slice(data); + Ok(()) + } +} + +/// Apache's `StringUtils.isBlank`, which rskj asks of an answer: nothing in +/// it but what Java's `Character.isWhitespace` takes for whitespace. +fn is_blank(text: &str) -> bool { + text.chars().all(|c| { + matches!( + c, + '\t'..='\r' + | '\u{1c}'..=' ' + | '\u{1680}' + | '\u{2000}'..='\u{2006}' + | '\u{2008}'..='\u{200a}' + | '\u{2028}' + | '\u{2029}' + | '\u{205f}' + | '\u{3000}' + ) + }) +} + +/// rskj `HttpGetMinGasPriceProvider.parsePrice`: the answer as Jackson's +/// tree, the node at the pointer, and Jackson's `asLong` of it; nothing where +/// the answer isn't JSON or the pointer finds no node. +fn parse_price(response: &str, pointer: &JsonPointer) -> Option { + match java::read_tree(response) { + Ok(tree) => tree + .as_ref() + .and_then(|root| root.at(pointer)) + .map(java::Node::as_long), + Err(e) => { + error!(target: "rustock::mining", "Error parsing min gas price from web provider: {e}"); + None + } + } +} + +/// What runs an `eth_call` on this node at its best block, as rskj's +/// `EthModule.call` does for the provider: the call's answer, a hex string. +pub type EthCaller = Arc Result + Send + Sync>; + +/// rskj `EthCallMinGasPriceProvider`'s source: a bitcoin's price from a +/// contract on this chain, as this node runs the call. +pub struct EthCallSource { + config: EthCallConfig, + chain_id: String, + call: EthCaller, +} + +impl fmt::Debug for EthCallSource { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("EthCallSource") + .field("config", &self.config) + .field("chain_id", &self.chain_id) + .finish_non_exhaustive() + } +} + +impl EthCallSource { + /// `chain_id` is the node's, which rskj's call names as + /// `ethModule.chainId()` gives it. + pub fn new(config: EthCallConfig, chain_id: u8, call: EthCaller) -> Self { + Self { + config, + chain_id: format!("{chain_id:#x}"), + call, + } + } +} + +impl BtcExchangeRateSource for EthCallSource { + /// rskj `getBtcExchangeRate`: the call from `from` to `to` with `data`, at + /// the best block, its answer read as `HexUtils.jsonHexToLong` reads it; + /// nothing where the call fails or answers no such number. + fn btc_exchange_rate(&self) -> Option { + let arguments = json!({ + "from": self.config.from, + "to": self.config.to, + "chainId": self.chain_id, + "data": self.config.data, + }); + match (self.call)(&json!([arguments, "latest"])) + .and_then(|output| json_hex_to_long(&output)) + { + Ok(price) => Some(price), + Err(e) => { + error!(target: "rustock::mining", "Error calling eth module: {e:#}"); + None + } + } + } + + fn provider_type(&self) -> MinGasPriceProviderType { + MinGasPriceProviderType::EthCall + } +} + +/// rskj `HexUtils.jsonHexToLong`: `0x`, then hex digits read as a Java +/// `long`. +fn json_hex_to_long(x: &str) -> Result { + let digits = x + .strip_prefix("0x") + .ok_or_else(|| anyhow!("{x:?} is not hex: it has no 0x"))?; + i64::from_str_radix(digits, 16).with_context(|| format!("{x:?} is not a hex long")) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::sync::atomic::{AtomicUsize, Ordering}; + use std::sync::Mutex; + + const FALLBACK_PRICE: i64 = 10; + + /// A fallback of 10 that counts how often it is asked, as the tests' + /// mocks do. + #[derive(Debug, Default)] + struct CountedFallback { + asked: AtomicUsize, + } + + impl MinGasPriceProvider for CountedFallback { + fn min_gas_price(&self) -> i64 { + self.asked.fetch_add(1, Ordering::SeqCst); + FALLBACK_PRICE + } + + fn provider_type(&self) -> MinGasPriceProviderType { + MinGasPriceProviderType::Fixed + } + } + + fn section(toml_text: &str) -> StableGasPriceSection { + toml::from_str(toml_text).expect("the section parses") + } + + // ── rskj HttpGetMinGasPriceProviderTest ───────────────────────────── + + /// The test's `createWebStableSystemConfig`. + fn web_config() -> HttpGetConfig { + HttpGetConfig { + url: "https://rsk.co/price?ids=bitcoin&vs_currencies=usd".into(), + json_path: "/bitcoin/usd".into(), + timeout_millis: 3000, + } + } + + /// A web that answers `answer` to every request, counting them. + fn web(answer: std::result::Result<&'static str, &'static str>) -> (HttpGet, Arc) { + let asked = Arc::new(AtomicUsize::new(0)); + let counted = asked.clone(); + let get: HttpGet = Arc::new(move |_url: &str, _timeout: i64| { + counted.fetch_add(1, Ordering::SeqCst); + answer.map(str::to_string).map_err(|e| anyhow!(e)) + }); + (get, asked) + } + + /// The test's provider over `get`: rskj's `createStableMinGasPriceSystemConfig`, + /// a stable price of 4,265,280,000,000, kept 30 s, for a bitcoin priced + /// 1,000 to 100,000. + fn web_provider(get: HttpGet, fallback: Arc) -> StableMinGasPriceProvider { + let source = HttpGetSource::with_get(web_config(), get).unwrap(); + StableMinGasPriceProvider::new( + Arc::new(source), + fallback, + 4_265_280_000_000, + 30_000, + 1_000, + 100_000, + ) + } + + /// rskj `returnsMappedPriceFromWebClient`. + #[test] + fn returns_mapped_price_from_web_client() { + let (get, _) = web(Ok(r#"{"bitcoin":{"usd":10000}}"#)); + let source = HttpGetSource::with_get(web_config(), get).unwrap(); + assert_eq!(source.btc_exchange_rate(), Some(10_000)); + } + + /// rskj `whenRequestingTheValueTwiceCachedValueIsUsed`. + #[test] + fn when_requesting_the_value_twice_cached_value_is_used() { + let (get, asked) = web(Ok(r#"{"bitcoin":{"usd":10000}}"#)); + let provider = web_provider(get, Arc::default()); + assert_eq!(provider.get_min_gas_price(true), 426_528_000); + assert_eq!(provider.min_gas_price(), 426_528_000); + assert_eq!(asked.load(Ordering::SeqCst), 1); + } + + /// rskj `whenEmptyResponseReturnsFallbackProvider`. + #[test] + fn when_empty_response_returns_fallback_provider() { + let fallback = Arc::new(CountedFallback::default()); + let (get, _) = web(Ok("")); + assert_eq!( + web_provider(get, fallback.clone()).get_min_gas_price(true), + FALLBACK_PRICE + ); + assert_eq!(fallback.asked.load(Ordering::SeqCst), 1); + } + + /// rskj `whenErrorReturnsFallbackProvider`. + #[test] + fn when_error_returns_fallback_provider() { + let fallback = Arc::new(CountedFallback::default()); + let (get, _) = web(Err("Error")); + assert_eq!( + web_provider(get, fallback.clone()).get_min_gas_price(true), + FALLBACK_PRICE + ); + assert_eq!(fallback.asked.load(Ordering::SeqCst), 1); + } + + /// rskj `whenPathIsWrongReturnsFallBack`. + #[test] + fn when_path_is_wrong_returns_fall_back() { + let fallback = Arc::new(CountedFallback::default()); + let (get, _) = web(Ok(r#"{"btc":{"usd":10000}}"#)); + assert_eq!( + web_provider(get, fallback.clone()).get_min_gas_price(true), + FALLBACK_PRICE + ); + assert_eq!(fallback.asked.load(Ordering::SeqCst), 1); + } + + // ── rskj EthCallMinGasPriceProviderTest ───────────────────────────── + + const ORACLE: &str = "0xbffBD993FF1d229B0FfE55668F2009d20d4F7C5f"; + + /// A node whose `eth_call` answers `answer`, keeping what it was asked. + fn node( + answer: std::result::Result<&'static str, &'static str>, + ) -> (EthCaller, Arc>>) { + let calls = Arc::new(Mutex::new(Vec::new())); + let kept = calls.clone(); + let call: EthCaller = Arc::new(move |params: &Value| { + kept.lock().unwrap().push(params.clone()); + answer.map(str::to_string).map_err(|e| anyhow!(e)) + }); + (call, calls) + } + + fn oracle_source(data: &str, call: EthCaller) -> EthCallSource { + let config = EthCallConfig { + to: ORACLE.into(), + from: ORACLE.into(), + data: data.into(), + }; + EthCallSource::new(config, 33, call) + } + + /// rskj `constructorSetsFieldsCorrectly`, for the two values the settings + /// can give; they can't give none (rskj's `getString` refuses that too). + #[test] + fn constructor_sets_fields_correctly() { + for data in ["0x123", "0xabc"] { + let config = EthCallConfig { + to: "0xaddress".into(), + from: "0xfrom".into(), + data: data.into(), + }; + let source = EthCallSource::new(config, 33, node(Ok("0x0")).0); + // rskj's getters `getToAddress`, `getFromAddress` and `getData`. + assert_eq!(source.config.to, "0xaddress"); + assert_eq!(source.config.from, "0xfrom"); + assert_eq!(source.config.data, data); + } + } + + /// rskj `constructorSetsFieldsToNullWhenConfigReturnsNull` has a mocked + /// configuration give none. rustock's settings can't: an `ETH_CALL` + /// source without `to`, `from` or `data` is refused, as rskj's + /// `EthCallMinGasPriceSystemConfig` refuses it. + #[test] + fn the_settings_refuse_an_eth_call_without_to_from_or_data() { + let full = r#" + enabled = true + [source] + method = "ETH_CALL" + [source.params] + to = "0x77045E71a7A2c50903d88e564cD72fab11e82051" + from = "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826" + data = "0x98d5fdca" + "#; + assert!(StableGasPriceConfig::read(§ion(full)) + .unwrap() + .eth_call() + .is_ok()); + for key in ["to", "from", "data"] { + let without: String = full + .lines() + .filter(|line| !line.trim_start().starts_with(&format!("{key} ="))) + .collect::>() + .join("\n"); + let config = StableGasPriceConfig::read(§ion(&without)).unwrap(); + assert!(config.eth_call().is_err(), "without {key}"); + } + } + + /// rskj `getStableMinGasPrice_callsEthModulesCallMethod`: the call is made + /// from and to the configured addresses with the data and the chain's id, + /// at the best block, and `0x21` is 33. + #[test] + fn get_stable_min_gas_price_calls_eth_modules_call_method() { + let (call, calls) = node(Ok("0x21")); + let source = oracle_source("0x", call); + assert_eq!(source.btc_exchange_rate(), Some(33)); + let calls = calls.lock().unwrap(); + assert_eq!( + calls[0], + json!([ + {"from": ORACLE, "to": ORACLE, "chainId": "0x21", "data": "0x"}, + "latest" + ]) + ); + } + + /// rskj `getStableMinGasPrice_callsFallback_whenNoData`: a call that + /// answers nothing gives no price, whatever data it was given. + #[test] + fn get_stable_min_gas_price_calls_fallback_when_no_data() { + for data in ["", "0x"] { + let source = oracle_source(data, node(Err("no answer")).0); + assert_eq!(source.btc_exchange_rate(), None, "data {data:?}"); + } + } + + /// rskj `getStableMinGasPrice_callsFallback_whenEthModuleIsNull`: a node + /// that can't run the call yet gives no price. + #[test] + fn get_stable_min_gas_price_calls_fallback_when_eth_module_is_null() { + let source = oracle_source( + "0x", + node(Err("the node's state is not open to calls yet")).0, + ); + assert_eq!(source.btc_exchange_rate(), None); + } + + /// rskj `getType_returnsOnChain`. + #[test] + fn get_type_returns_on_chain() { + let source = oracle_source("0x", node(Ok("0x0")).0); + assert_eq!(source.provider_type(), MinGasPriceProviderType::EthCall); + } + + /// `HexUtils.jsonHexToLong`: `0x` first, a Java long's hex digits after. + #[test] + fn a_call_answer_is_read_as_a_hex_long() { + assert_eq!(json_hex_to_long("0x21").unwrap(), 33); + assert_eq!( + json_hex_to_long(&format!("0x{:064x}", 60_000)).unwrap(), + 60_000, + "a word with its leading zeros" + ); + assert!(json_hex_to_long("0x").is_err(), "no digits"); + assert!(json_hex_to_long("21").is_err(), "no 0x"); + assert!( + json_hex_to_long(&format!("0x{}", "f".repeat(64))).is_err(), + "past a long" + ); + } + + // ── rskj MinGasPriceProviderFactoryTest ────────────────────────────── + + fn no_node() -> EthCaller { + node(Err("no node")).0 + } + + /// rskj `createFixedMinGasPriceProvider`. + #[test] + fn create_fixed_min_gas_price_provider() { + let config = section( + r#" + enabled = true + refresh_rate = 10 + min_stable_gas_price = 100 + source = { method = "FIXED" } + "#, + ); + let provider = create(100, Some(&config), 33, no_node()).unwrap(); + assert_eq!(provider.min_gas_price(), 100); + assert_eq!(provider.provider_type(), MinGasPriceProviderType::Fixed); + } + + /// rskj `createWithNullConfig`. + #[test] + fn create_with_null_config() { + let provider = create(100, None, 33, no_node()).unwrap(); + assert_eq!(provider.min_gas_price(), 100); + assert_eq!(provider.provider_type(), MinGasPriceProviderType::Fixed); + } + + /// rskj `createWebProviderMethod`: a web source on a URL that can't be + /// fetched, whose price is the fixed one, through rustock's own client. + #[test] + fn create_web_provider_method() { + let config = section( + r#" + enabled = true + refresh_rate = 10 + min_stable_gas_price = 100 + [source] + method = "HTTP_GET" + [source.params] + url = "url" + timeout = 1000 + json_path = "/price" + "#, + ); + let provider = create(100, Some(&config), 33, no_node()).unwrap(); + assert_eq!(provider.min_gas_price(), 100); + assert_eq!(provider.provider_type(), MinGasPriceProviderType::HttpGet); + } + + /// rskj `createWithDisabledConfigReturnFixed`: off, the source isn't + /// read at all, its pointer that isn't one included. + #[test] + fn create_with_disabled_config_return_fixed() { + let config = section( + r#" + enabled = false + refresh_rate = 10 + min_stable_gas_price = 100 + [source] + method = "HTTP_GET" + [source.params] + url = "url" + timeout = 1000 + json_path = "price" + "#, + ); + let provider = create(100, Some(&config), 33, no_node()).unwrap(); + assert_eq!(provider.min_gas_price(), 100); + assert_eq!(provider.provider_type(), MinGasPriceProviderType::Fixed); + } + + /// On, a web source whose pointer isn't one, or whose timeout is past a + /// Java `int` of milliseconds, stops the node, as it stops rskj. + #[test] + fn a_web_source_rskj_would_not_start_on_is_refused() { + let web_section = |json_path: &str, timeout: &str| { + section(&format!( + "enabled = true\n[source]\nmethod = \"HTTP_GET\"\n[source.params]\n\ + url = \"https://price.example/\"\njson_path = \"{json_path}\"\ntimeout = {timeout}\n" + )) + }; + let (get, _) = web(Ok("1")); + let made = |s: StableGasPriceSection| create_with(0, Some(&s), 33, no_node(), get.clone()); + assert!(made(web_section("/price", "1000")).is_ok()); + assert!( + made(web_section("", "1000")).is_ok(), + "the empty pointer is the whole answer" + ); + assert!(made(web_section("price", "1000")).is_err()); + assert!(made(web_section("/price", "2147483648")).is_err()); + } + + // ── rskj EthCallMinGasPriceSystemConfigTest ───────────────────────── + + fn eth_call_config() -> EthCallConfig { + let config = section( + r#" + enabled = true + [source] + method = "ETH_CALL" + [source.params] + to = "0x77045E71a7A2c50903d88e564cD72fab11e82051" + from = "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826" + data = "0x98d5fdca" + "#, + ); + StableGasPriceConfig::read(&config) + .unwrap() + .eth_call() + .unwrap() + } + + /// rskj `testAddress`. + #[test] + fn test_address() { + assert_eq!( + eth_call_config().to, + "0x77045E71a7A2c50903d88e564cD72fab11e82051" + ); + } + + /// rskj `testFrom`. + #[test] + fn test_from() { + assert_eq!( + eth_call_config().from, + "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826" + ); + } + + /// rskj `testData`. + #[test] + fn test_data() { + assert_eq!(eth_call_config().data, "0x98d5fdca"); + } + + // ── rskj HttpGetStableMinGasSystemConfigTest ───────────────────────── + + fn http_get_config() -> HttpGetConfig { + let config = section( + r#" + enabled = true + [source] + method = "HTTP_GET" + [source.params] + url = "http://test.url" + json_path = "testPath" + timeout = "1000 milliseconds" + "#, + ); + StableGasPriceConfig::read(&config) + .unwrap() + .http_get() + .unwrap() + } + + /// rskj `testGetUrl`. + #[test] + fn test_get_url() { + assert_eq!(http_get_config().url, "http://test.url"); + } + + /// rskj `testRequestPath`: the settings keep the path as written; only + /// the source reads it as a pointer. + #[test] + fn test_request_path() { + assert_eq!(http_get_config().json_path, "testPath"); + } + + /// rskj `testGetTimeout`. + #[test] + fn test_get_timeout() { + assert_eq!(http_get_config().timeout_millis, 1000); + } + + // ── rskj StableMinGasPriceSystemConfigTest ────────────────────────── + + fn stable_config() -> StableGasPriceConfig { + StableGasPriceConfig::read(§ion( + r#" + enabled = true + refresh_rate = "10 hours" + min_stable_gas_price = 100 + source = { method = "FIXED" } + "#, + )) + .unwrap() + } + + /// rskj `testGetRefreshRate`. + #[test] + fn test_get_refresh_rate() { + assert_eq!(stable_config().refresh_rate_millis, 10 * 3_600_000); + } + + /// rskj `testGetMinStableGasPrice`. + #[test] + fn test_get_min_stable_gas_price() { + assert_eq!(stable_config().min_stable_gas_price, 100); + } + + /// rskj `testIsEnabled`. + #[test] + fn test_is_enabled() { + assert!(stable_config().enabled); + } + + /// rskj `testGetMethod`. + #[test] + fn test_get_method() { + assert_eq!(stable_config().method, MinGasPriceProviderType::Fixed); + } + + /// rskj `wrongMethodShouldThrowException`. + #[test] + fn wrong_method_should_throw_exception() { + let config = section( + r#" + enabled = true + refresh_rate = 10 + min_stable_gas_price = 100 + source = { method = "INVALID" } + "#, + ); + assert!(StableGasPriceConfig::read(&config).is_err()); + } + + /// rskj `missingPropertyShouldThrowException`: a `method` beside + /// `enabled` is no key of the section, and the file is refused; without + /// it, the section names no `source.method`, which an enabled one must. + #[test] + fn missing_property_should_throw_exception() { + let stray: std::result::Result = + toml::from_str("enabled = true\nmin_stable_gas_price = 100\nmethod = \"FIXED\"\n"); + assert!(stray.is_err()); + let config = section("enabled = true\nmin_stable_gas_price = 100\n"); + assert!(StableGasPriceConfig::read(&config).is_err()); + } + + /// rskj `testRanges`. + #[test] + fn test_ranges() { + let config = section( + r#" + enabled = true + min_valid_price = 10 + max_valid_price = 1000 + source = { method = "FIXED" } + "#, + ); + let config = StableGasPriceConfig::read(&config).unwrap(); + assert_eq!(config.min_valid_price, 10); + assert_eq!(config.max_valid_price, 1000); + } + + /// Off, nothing is read but the valid range: no refresh rate, no stable + /// price, `FIXED`, whatever the section says. + #[test] + fn off_the_section_is_fixed_whatever_it_says() { + let config = StableGasPriceConfig::read(§ion( + r#" + refresh_rate = "not a duration" + min_stable_gas_price = 100 + source = { method = "INVALID" } + "#, + )) + .unwrap(); + assert!(!config.enabled); + assert_eq!(config.refresh_rate_millis, 0); + assert_eq!(config.min_stable_gas_price, 0); + assert_eq!(config.method, MinGasPriceProviderType::Fixed); + assert_eq!( + (config.min_valid_price, config.max_valid_price), + (0, i64::MAX) + ); + } + + // ── reading as rskj reads ───────────────────────────────────────────── + + /// HOCON's durations: a number alone is milliseconds, units long or + /// short, singular or plural, a fraction of one, and a space or none. + #[test] + fn durations_are_read_as_hocon_reads_them() { + let ms = |text: &str| duration_millis(&DurationValue::Text(text.into())); + assert_eq!(duration_millis(&DurationValue::Millis(10)).unwrap(), 10); + assert_eq!( + duration_millis(&DurationValue::FractionalMillis(1.5)).unwrap(), + 1 + ); + assert_eq!(ms("10").unwrap(), 10); + assert_eq!(ms("6 hours").unwrap(), 6 * 3_600_000); + assert_eq!(ms("1 hour").unwrap(), 3_600_000); + assert_eq!(ms("2 seconds").unwrap(), 2_000); + assert_eq!(ms("2s").unwrap(), 2_000); + assert_eq!(ms("1.5 seconds").unwrap(), 1_500); + assert_eq!(ms("2.5 minutes").unwrap(), 150_000); + assert_eq!(ms("4 days").unwrap(), 4 * 86_400_000); + assert_eq!(ms("1000 milliseconds").unwrap(), 1_000); + assert_eq!(ms("1 millisecond").unwrap(), 1); + assert_eq!(ms("1500 us").unwrap(), 1); + assert_eq!(ms("-1 ns").unwrap(), -1, "Duration.toMillis rounds down"); + assert_eq!(ms("0x1p3").unwrap(), 8, "a Java hexadecimal double"); + assert_eq!(ms("0x1p3 s").unwrap(), 8_000); + assert_eq!(ms("0x1p3d").unwrap(), 8 * 86_400_000, "whose d is the unit"); + assert_eq!(ms("NaN ms").unwrap(), 0); + assert_eq!(ms("Infinity ms").unwrap(), i64::MAX / 1_000_000); + assert!(ms("hours").is_err(), "no number"); + assert!(ms("3 fortnights").is_err(), "no such unit"); + assert!(ms("1.2.3 s").is_err(), "no number HOCON reads"); + } + + /// rskj's `parsePrice`, through Jackson's tree and `asLong`: a whole + /// number's low 64 bits, a double cast as Java casts it, a string read + /// as `parseAsLong` reads it, `true` as 1, anything else 0; nothing + /// where the answer is no JSON or the pointer names nothing. + #[test] + fn a_price_is_read_as_jackson_reads_it() { + let price = + |answer: &str, at: &str| parse_price(answer, &JsonPointer::compile(at).unwrap()); + assert_eq!( + price("60000", ""), + Some(60_000), + "the empty pointer is the answer" + ); + assert_eq!(price(r#"{"p":60000.9}"#, "/p"), Some(60_000)); + assert_eq!(price(r#"{"p":-60000.9}"#, "/p"), Some(-60_000)); + assert_eq!(price(r#"{"p":1e4}"#, "/p"), Some(10_000)); + assert_eq!( + price(r#"{"p":1e400}"#, "/p"), + Some(i64::MAX), + "past a double, infinite" + ); + assert_eq!(price(r#"{"p":" 60000 "}"#, "/p"), Some(60_000)); + assert_eq!(price(r#"{"p":"60000.5"}"#, "/p"), Some(60_000)); + assert_eq!(price(r#"{"p":"+60000"}"#, "/p"), Some(60_000)); + assert_eq!(price(r#"{"p":"1.5e3d"}"#, "/p"), Some(1_500)); + assert_eq!( + price(r#"{"p":"0x1p4"}"#, "/p"), + Some(16), + "a hexadecimal double" + ); + assert_eq!( + price(r#"{"p":"99999999999999999999"}"#, "/p"), + Some(0), + "past a long" + ); + assert_eq!(price(r#"{"p":"abc"}"#, "/p"), Some(0)); + assert_eq!(price(r#"{"p":"NaN"}"#, "/p"), Some(0)); + assert_eq!(price(r#"{"p":"Infinity"}"#, "/p"), Some(i64::MAX)); + assert_eq!(price(r#"{"p":"inf"}"#, "/p"), Some(0), "Java reads no inf"); + assert_eq!(price(r#"{"p":true}"#, "/p"), Some(1)); + assert_eq!(price(r#"{"p":null}"#, "/p"), Some(0)); + assert_eq!(price(r#"{"p":{"q":1}}"#, "/p"), Some(0)); + assert_eq!(price(r#"{"p":[7,8]}"#, "/p/1"), Some(8)); + assert_eq!( + price(r#"{"p":9223372036854775808}"#, "/p"), + Some(i64::MIN), + "a long's low 64 bits" + ); + assert_eq!( + price(r#"{"p":18446744073709551621}"#, "/p"), + Some(5), + "a BigInteger's too" + ); + assert_eq!( + price(r#"{"p":1} trailing"#, "/p"), + Some(1), + "what follows the value is left" + ); + assert_eq!( + price("7x", ""), + None, + "a number at the top runs on into no space" + ); + assert_eq!(price(r#"{"p":1"#, "/p"), None, "no JSON"); + assert_eq!(price(r#"{"q":1}"#, "/p"), None, "no node"); + assert!(is_blank(" \t\u{1c}\u{3000}"), "Java's whitespace"); + assert!(!is_blank("\u{a0}"), "which a no-break space isn't"); + } + + /// Java's `readLine` loop drops every line ending; the cap stops an + /// answer past a mebibyte. + #[test] + fn an_answer_is_read_as_rskjs_client_reads_it() { + assert_eq!( + joined_lines(b"{\"a\":\r\n1,\n\"b\":\r2}"), + "{\"a\":1,\"b\":2}" + ); + let mut answer = Capped::default(); + assert!(https::Sink::write(&mut answer, &vec![b' '; MAX_ANSWER]).is_ok()); + assert!(https::Sink::write(&mut answer, b" ").is_err()); + assert!( + simple_http_get("https://price.example/", -1).is_err(), + "a negative timeout" + ); + } +} diff --git a/crates/cli/src/stable_gas_price/java.rs b/crates/cli/src/stable_gas_price/java.rs new file mode 100644 index 0000000..344ac6b --- /dev/null +++ b/crates/cli/src/stable_gas_price/java.rs @@ -0,0 +1,980 @@ +//! What rskj's price sources read an answer with, as rskj's Java reads it: +//! Jackson 2.15's `ObjectMapper.readTree`, `JsonPointer` and `JsonNode.asLong`, +//! with Jackson's defaults as rskj leaves them; and Java's +//! `Double.parseDouble`, by which `asLong` reads a string and HOCON reads a +//! duration's number. + +/// How deep Jackson 2.15 reads a document (`StreamReadConstraints`). +const MAX_NESTING_DEPTH: usize = 1000; + +/// How many digits Jackson 2.15 reads in a number (`StreamReadConstraints`). +const MAX_NUMBER_LENGTH: usize = 1000; + +const END: &str = "Unexpected end-of-input"; + +/// Jackson `JsonPointer`: the names and indexes a node is found by. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(super) struct JsonPointer(Vec); + +impl JsonPointer { + /// Jackson `JsonPointer.compile`, which rskj's `valueOf` is: empty, the + /// whole document; otherwise `/` and a segment after each `/`, in which + /// `~0` is `~`, `~1` is `/`, and a `~` before anything else stays, with + /// the character after it. + pub(super) fn compile(expr: &str) -> Result { + if expr.is_empty() { + return Ok(Self(Vec::new())); + } + let Some(tail) = expr.strip_prefix('/') else { + return Err(format!( + "Invalid input: JSON Pointer expression must start with '/': {expr:?}" + )); + }; + let mut segments = Vec::new(); + let mut segment = String::new(); + let mut chars = tail.chars(); + while let Some(c) = chars.next() { + match c { + '/' => segments.push(std::mem::take(&mut segment)), + '~' => match chars.next() { + Some('0') => segment.push('~'), + Some('1') => segment.push('/'), + Some(other) => { + segment.push('~'); + segment.push(other); + } + None => segment.push('~'), + }, + c => segment.push(c), + } + } + segments.push(segment); + Ok(Self(segments)) + } +} + +/// Jackson's tree of a document, as far as `at` and `asLong` look into it. +#[derive(Debug, Clone, PartialEq)] +pub(super) enum Node { + /// The members in order. A name holding half a surrogate pair is `None`: + /// no pointer the configuration can give names it. + Object(Vec<(Option, Node)>), + Array(Vec), + /// A string. Half a surrogate pair reads as U+FFFD, which `parseAsLong` + /// reads as no number, as it does the half pair. + Text(String), + /// A number with neither a fraction nor an exponent: the low 64 bits + /// `asLong` takes of Jackson's `int`, `long` or `BigInteger`. + Integer(i64), + /// A number with either, read into a double. + Double(f64), + Bool(bool), + Null, +} + +impl Node { + /// Jackson `JsonNode.at`: the node the pointer names, or `None` for + /// Jackson's `MissingNode`. A name given twice names the last, as + /// Jackson's `ObjectNode` keeps it. + pub(super) fn at(&self, pointer: &JsonPointer) -> Option<&Node> { + pointer.0.iter().try_fold(self, |node, segment| match node { + Node::Object(members) => members + .iter() + .rev() + .find(|(name, _)| name.as_deref() == Some(segment.as_str())) + .map(|(_, value)| value), + Node::Array(elements) => array_index(segment).and_then(|i| elements.get(i)), + _ => None, + }) + } + + /// Jackson `JsonNode.asLong()`. + pub(super) fn as_long(&self) -> i64 { + match self { + Node::Integer(n) => *n, + // `DoubleNode.longValue`: Java's cast, which saturates and takes + // NaN to 0, as Rust's does. + Node::Double(d) => *d as i64, + Node::Text(text) => parse_as_long(text), + Node::Bool(b) => i64::from(*b), + Node::Object(_) | Node::Array(_) | Node::Null => 0, + } + } +} + +/// Jackson `JsonPointer._parseIndex`: an `int` written without a sign or a +/// leading zero names an array's element; any other segment names none. +fn array_index(segment: &str) -> Option { + let digits = segment.as_bytes(); + if digits.is_empty() || digits.len() > 10 || !digits.iter().all(u8::is_ascii_digit) { + return None; + } + if digits.len() > 1 && digits[0] == b'0' { + return None; + } + segment + .parse::() + .ok() + .and_then(|i| usize::try_from(i).ok()) +} + +/// An object or an array being read, with what it holds so far, and an +/// object with the name of the member being read. +enum Open { + Object(Vec<(Option, Node)>, Option), + Array(Vec), +} + +/// Jackson `ObjectMapper.readTree(String)`: the document's first value, with +/// anything after it left unread; `None` for whitespace alone, Jackson's +/// `MissingNode`. Read as Jackson's parser reads it by default, which +/// refuses comments, single quotes, names without quotes, trailing commas, +/// `NaN`, a leading zero or plus, a control character unescaped and an +/// escape it doesn't know, and a document past its depth or a number past +/// its digits. A number at the top must end the document or be followed by +/// whitespace, and a word anywhere must not run on into what a Java name +/// goes on with. +pub(super) fn read_tree(text: &str) -> Result, String> { + let mut reader = Reader { text, at: 0 }; + if !reader.skip_whitespace()? { + return Ok(None); + } + // Iterative, not recursive: a document a thousand deep is read on the + // price's own thread, whatever its stack. + let mut open: Vec = Vec::new(); + loop { + let mut node = match reader.peek() { + Some(b'{') => { + reader.enter(open.len())?; + if reader.closes(b'}')? { + Node::Object(Vec::new()) + } else { + open.push(Open::Object(Vec::new(), reader.member_name()?)); + continue; + } + } + Some(b'[') => { + reader.enter(open.len())?; + if reader.closes(b']')? { + Node::Array(Vec::new()) + } else { + open.push(Open::Array(Vec::new())); + continue; + } + } + _ => reader.scalar(open.is_empty())?, + }; + // A whole value: into what holds it, and out of each container it + // was the last of. + loop { + let close = match open.last_mut() { + None => return Ok(Some(node)), + Some(Open::Object(members, name)) => { + members.push((name.take(), node)); + b'}' + } + Some(Open::Array(elements)) => { + elements.push(node); + b']' + } + }; + if !reader.skip_whitespace()? { + return Err(END.into()); + } + match reader.peek() { + Some(b',') => { + reader.at += 1; + if !reader.skip_whitespace()? { + return Err(END.into()); + } + if let Some(Open::Object(_, name)) = open.last_mut() { + *name = reader.member_name()?; + } + break; + } + Some(b) if b == close => { + reader.at += 1; + node = match open.pop() { + Some(Open::Object(members, _)) => Node::Object(members), + Some(Open::Array(elements)) => Node::Array(elements), + None => return Err(END.into()), + }; + } + _ => { + return Err(format!( + "Unexpected character at {}: was expecting comma to separate entries", + reader.at + )) + } + } + } + } +} + +struct Reader<'a> { + text: &'a str, + at: usize, +} + +impl Reader<'_> { + fn peek(&self) -> Option { + self.text.as_bytes().get(self.at).copied() + } + + /// Past JSON's whitespace; whether anything is left. Any other control + /// character is refused, as Jackson's `_skipWSOrEnd` refuses it. + fn skip_whitespace(&mut self) -> Result { + while let Some(b) = self.peek() { + match b { + b' ' | b'\t' | b'\n' | b'\r' => self.at += 1, + 0..=0x1f => return Err(format!("Illegal character (CTRL-CHAR, code {b})")), + _ => return Ok(true), + } + } + Ok(false) + } + + /// Into an object or an array inside `outside` others, as deep as + /// Jackson goes. + fn enter(&mut self, outside: usize) -> Result<(), String> { + let depth = outside + 1; + if depth > MAX_NESTING_DEPTH { + return Err(format!( + "Document nesting depth ({depth}) exceeds the maximum allowed ({MAX_NESTING_DEPTH})" + )); + } + self.at += 1; + Ok(()) + } + + /// Whether the container just entered closes at once, with `close`. + fn closes(&mut self, close: u8) -> Result { + if !self.skip_whitespace()? { + return Err(END.into()); + } + let closes = self.peek() == Some(close); + if closes { + self.at += 1; + } + Ok(closes) + } + + /// A member's name and its colon, up to its value. + fn member_name(&mut self) -> Result, String> { + if self.peek() != Some(b'"') { + return Err(format!( + "Unexpected character at {}: was expecting double-quote to start field name", + self.at + )); + } + self.at += 1; + let name = String::from_utf16(&self.string()?).ok(); + if !self.skip_whitespace()? { + return Err(END.into()); + } + if self.peek() != Some(b':') { + return Err(format!( + "Unexpected character at {}: was expecting a colon to separate field name and value", + self.at + )); + } + self.at += 1; + if !self.skip_whitespace()? { + return Err(END.into()); + } + Ok(name) + } + + /// A string, a number or a word; `top` for the document's own value. + fn scalar(&mut self, top: bool) -> Result { + match self.peek() { + Some(b'"') => { + self.at += 1; + Ok(Node::Text(String::from_utf16_lossy(&self.string()?))) + } + Some(b'-' | b'0'..=b'9') => self.number(top), + Some(b't') => self.word("true", Node::Bool(true)), + Some(b'f') => self.word("false", Node::Bool(false)), + Some(b'n') => self.word("null", Node::Null), + Some(_) => Err(format!( + "Unexpected character at {}: expected a valid value", + self.at + )), + None => Err(END.into()), + } + } + + /// A string's UTF-16 units, from past its opening quote to past its + /// closing one. + fn string(&mut self) -> Result, String> { + let mut units = Vec::new(); + loop { + let Some(c) = self + .text + .get(self.at..) + .and_then(|rest| rest.chars().next()) + else { + return Err(format!( + "{END}: was expecting closing quote for a string value" + )); + }; + self.at += c.len_utf8(); + match c { + '"' => return Ok(units), + '\\' => units.push(self.escape()?), + '\0'..='\u{1f}' => { + return Err(format!( + "Illegal unquoted character (CTRL-CHAR, code {}): has to be escaped", + u32::from(c) + )) + } + c => units.extend_from_slice(c.encode_utf16(&mut [0; 2])), + } + } + } + + /// What a backslash in a string stands for: one of JSON's escapes, or + /// `u` and four hex digits, half a surrogate pair included. + fn escape(&mut self) -> Result { + let Some(b) = self.peek() else { + return Err(END.into()); + }; + let unit = match b { + b'b' => 0x08, + b't' => 0x09, + b'n' => 0x0a, + b'f' => 0x0c, + b'r' => 0x0d, + b'"' | b'/' | b'\\' => u16::from(b), + b'u' => { + let hex = self + .text + .as_bytes() + .get(self.at + 1..self.at + 5) + .ok_or(END)?; + let mut unit = 0u16; + for &digit in hex { + let value = char::from(digit).to_digit(16).ok_or( + "Unexpected character: expected a hex-digit for character escape sequence", + )?; + unit = unit << 4 | value as u16; + } + self.at += 4; + unit + } + _ => return Err(format!("Unrecognized character escape (code {b})")), + }; + self.at += 1; + Ok(unit) + } + + /// A number as Jackson's parser reads one: `-`, then `0` or digits + /// without a leading zero, then a fraction and an exponent, each with a + /// digit at least. At the top it ends the document or whitespace follows + /// it (`_verifyRootSpace`). + fn number(&mut self, top: bool) -> Result { + let text = self.text; + let bytes = text.as_bytes(); + let digits = |from: usize| { + bytes.get(from..).map_or(0, |rest| { + rest.iter().take_while(|b| b.is_ascii_digit()).count() + }) + }; + let start = self.at; + if bytes.get(self.at) == Some(&b'-') { + self.at += 1; + } + let whole = digits(self.at); + if whole == 0 { + return Err("Unexpected character: expected digit (0-9) to follow minus sign".into()); + } + if whole > 1 && bytes.get(self.at) == Some(&b'0') { + return Err("Invalid numeric value: Leading zeroes not allowed".into()); + } + self.at += whole; + let (mut fraction, mut exponent, mut integer) = (0, 0, true); + if self.peek() == Some(b'.') { + integer = false; + fraction = digits(self.at + 1); + if fraction == 0 { + return Err("Unexpected character: Decimal point not followed by a digit".into()); + } + self.at += 1 + fraction; + } + if matches!(self.peek(), Some(b'e' | b'E')) { + integer = false; + self.at += 1; + if matches!(self.peek(), Some(b'+' | b'-')) { + self.at += 1; + } + exponent = digits(self.at); + if exponent == 0 { + return Err( + "Unexpected character: Exponent indicator not followed by a digit".into(), + ); + } + self.at += exponent; + } + if top && !matches!(self.peek(), None | Some(b' ' | b'\t' | b'\n' | b'\r')) { + return Err("Unexpected character: Expected space separating root-level values".into()); + } + let length = whole + fraction + exponent; + if length > MAX_NUMBER_LENGTH { + return Err(format!( + "Number value length ({length}) exceeds the maximum allowed ({MAX_NUMBER_LENGTH})" + )); + } + let token = &text[start..self.at]; + if integer { + Ok(Node::Integer(low_64_bits(token))) + } else { + // Rust reads a double as Java's `Double.parseDouble` does: to the + // nearest, and past the largest as infinite. + token + .parse::() + .map(Node::Double) + .map_err(|e| e.to_string()) + } + } + + /// `true`, `false` or `null`, as Jackson's `_matchToken` takes one: a + /// word run on into what a Java name goes on with is no word. + fn word(&mut self, word: &str, node: Node) -> Result { + let rest = self.text.get(self.at..).unwrap_or_default(); + let Some(after) = rest.strip_prefix(word) else { + return Err(format!("Unrecognized token: was expecting '{word}'")); + }; + if let Some(next) = after.chars().next() { + if next >= '0' && next != ']' && next != '}' && is_java_identifier_part(next) { + return Err(format!("Unrecognized token '{word}{next}'")); + } + } + self.at += word.len(); + Ok(node) + } +} + +/// Java `Character.isJavaIdentifierPart(char)`, for the character after a +/// word: a letter or a digit, `$` or `_`, or a control Java takes as +/// ignorable, exactly through Latin-1; past it, a letter or a digit, where +/// Java's answer is that of the Unicode its version carries. A character +/// past 16 bits is half a surrogate pair to Java's `char`, which is none. +fn is_java_identifier_part(c: char) -> bool { + match c { + '\u{0}'..='\u{8}' | '\u{e}'..='\u{1b}' | '\u{7f}'..='\u{9f}' => true, + '$' | '_' | '0'..='9' | 'A'..='Z' | 'a'..='z' => true, + // The cent, pound, currency and yen signs, the soft hyphen, and + // Latin-1's letters. + '\u{a2}'..='\u{a5}' | '\u{aa}' | '\u{ad}' | '\u{b5}' | '\u{ba}' => true, + '\u{c0}'..='\u{d6}' | '\u{d8}'..='\u{f6}' | '\u{f8}'..='\u{ff}' => true, + '\u{0}'..='\u{ff}' | '\u{10000}'.. => false, + _ => c.is_alphanumeric(), + } +} + +/// A whole number's low 64 bits, two's complement: `asLong` of Jackson's +/// `IntNode`, `LongNode` and `BigIntegerNode` alike. +fn low_64_bits(token: &str) -> i64 { + let (negative, digits) = match token.strip_prefix('-') { + Some(digits) => (true, digits), + None => (false, token), + }; + let magnitude = digits.bytes().fold(0u64, |value, digit| { + value + .wrapping_mul(10) + .wrapping_add(u64::from(digit.wrapping_sub(b'0'))) + }); + (if negative { + magnitude.wrapping_neg() + } else { + magnitude + }) as i64 +} + +/// Jackson `NumberInput.parseAsLong(text, 0)`, which `TextNode.asLong` reads +/// by: trimmed of characters up to the space, a `+` dropped, then digits +/// read as a `long`, or 0 past one; anything else read as a Java double and +/// cast, or 0 where it is none. +pub(super) fn parse_as_long(text: &str) -> i64 { + let s = text.trim_matches(|c: char| c <= ' '); + if s.is_empty() { + return 0; + } + let s = s.strip_prefix('+').unwrap_or(s); + let digits = s.strip_prefix('-').unwrap_or(s); + if digits.bytes().all(|b| b.is_ascii_digit()) { + return s.parse::().unwrap_or(0); + } + parse_double(s).map_or(0, |d| d as i64) +} + +/// Java `Double.parseDouble`: trimmed of characters up to the space, a sign, +/// then `NaN`, `Infinity`, a decimal number, or a hexadecimal one with a +/// binary exponent, either of the last two with a `d` or `f` after it if it +/// likes; `None` where Java throws `NumberFormatException`. +pub(super) fn parse_double(text: &str) -> Option { + let s = text.trim_matches(|c: char| c <= ' '); + let (negative, body) = match s.as_bytes().first() { + Some(b'-') => (true, &s[1..]), + Some(b'+') => (false, &s[1..]), + _ => (false, s), + }; + let magnitude = match body { + "NaN" => f64::NAN, + "Infinity" => f64::INFINITY, + _ => { + let body = body.strip_suffix(['d', 'D', 'f', 'F']).unwrap_or(body); + match body.strip_prefix("0x").or_else(|| body.strip_prefix("0X")) { + Some(hex) => hex_float(hex)?, + None if decimal_literal(body) => body.parse::().ok()?, + None => return None, + } + } + }; + Some(if negative { -magnitude } else { magnitude }) +} + +/// Digits with at most one point and at least one digit, then an optional +/// exponent: Java's decimal floating-point literal, unsigned. +fn decimal_literal(s: &str) -> bool { + let digits = |t: &str| t.bytes().all(|b| b.is_ascii_digit()); + let (mantissa, exponent) = match s.find(['e', 'E']) { + Some(at) => (&s[..at], Some(&s[at + 1..])), + None => (s, None), + }; + let (whole, fraction) = mantissa.split_once('.').unwrap_or((mantissa, "")); + if !(digits(whole) && digits(fraction)) || (whole.is_empty() && fraction.is_empty()) { + return false; + } + exponent.is_none_or(|e| { + let e = e.strip_prefix(['+', '-']).unwrap_or(e); + !e.is_empty() && digits(e) + }) +} + +/// What follows `0x` in a Java hexadecimal float, its `d` or `f` gone: hex +/// digits with a point or without, then `p` and a signed decimal exponent, +/// rounded to the nearest double, ties to even, as Java's +/// `FloatingDecimal.parseHexString` rounds it. +fn hex_float(s: &str) -> Option { + let (significand, exponent) = s.split_once(['p', 'P'])?; + let (whole, fraction) = significand.split_once('.').unwrap_or((significand, "")); + let hex = |t: &str| t.bytes().all(|b| b.is_ascii_hexdigit()); + if (whole.is_empty() && fraction.is_empty()) || !hex(whole) || !hex(fraction) { + return None; + } + let (up, exponent) = match exponent.as_bytes().first() { + Some(b'-') => (false, &exponent[1..]), + Some(b'+') => (true, &exponent[1..]), + _ => (true, exponent), + }; + if exponent.is_empty() || !exponent.bytes().all(|b| b.is_ascii_digit()) { + return None; + } + // The digits as one integer of up to 64 bits, and whether any dropped + // past those was other than zero. + let (mut mantissa, mut dropped, mut sticky) = (0u64, 0i64, false); + for digit in whole.bytes().chain(fraction.bytes()) { + let value = u64::from(char::from(digit).to_digit(16).unwrap_or(0)); + if mantissa >> 60 == 0 { + mantissa = mantissa << 4 | value; + } else { + dropped += 1; + sticky |= value != 0; + } + } + if mantissa == 0 { + return Some(0.0); + } + // Java reads the exponent as an `int`; past one, the number is infinite + // or zero. + let Ok(exponent) = exponent.parse::() else { + return Some(if up { f64::INFINITY } else { 0.0 }); + }; + let exponent = if up { + i64::from(exponent) + } else { + -i64::from(exponent) + }; + let scale = exponent - 4 * fraction.len() as i64 + 4 * dropped; + Some(round_to_double(mantissa, sticky, scale)) +} + +/// `mantissa` times two to the `scale`, with `sticky` where more was below +/// its last bit, to the nearest double, ties to even. +fn round_to_double(mantissa: u64, sticky: bool, scale: i64) -> f64 { + let top = scale + i64::from(63 - mantissa.leading_zeros()); + if top > 1023 { + return f64::INFINITY; + } + // The last bit a double keeps: 52 below its top one, or the smallest + // subnormal's. + let last = (top - 52).max(-1074); + let shift = last - scale; + if shift <= 0 { + // Every bit is kept. + return mantissa as f64 * power_of_two(scale); + } + if shift > 65 { + // Under a quarter of the smallest subnormal. + return 0.0; + } + let wide = u128::from(mantissa); + let kept = wide >> shift; + let rest = wide & ((1u128 << shift) - 1); + let half = 1u128 << (shift - 1); + let up = rest > half || (rest == half && (sticky || kept & 1 == 1)); + (kept + u128::from(up)) as f64 * power_of_two(last) +} + +/// Two to the `exponent`, for one from the smallest subnormal's to the +/// largest double's. +fn power_of_two(exponent: i64) -> f64 { + if exponent >= -1022 { + f64::from_bits(((exponent + 1023) as u64) << 52) + } else { + f64::from_bits(1u64 << (exponent + 1074)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn pointer(expr: &str) -> JsonPointer { + JsonPointer::compile(expr).unwrap() + } + + /// `asLong` of what `at` names in `document`, as rskj's provider reads a + /// price. + fn long_at(document: &str, at: &str) -> Option { + let tree = read_tree(document).unwrap(); + tree.as_ref() + .and_then(|root| root.at(&pointer(at))) + .map(Node::as_long) + } + + #[test] + fn a_pointer_is_compiled_as_jacksons() { + assert_eq!(pointer("").0, Vec::::new()); + assert!(JsonPointer::compile("price").is_err()); + assert_eq!(pointer("/").0, [""]); + assert_eq!(pointer("//").0, ["", ""]); + assert_eq!(pointer("/bitcoin/usd").0, ["bitcoin", "usd"]); + assert_eq!(pointer("/a~1b/c~0d").0, ["a/b", "c~d"]); + assert_eq!(pointer("/~01").0, ["~1"]); + assert_eq!(pointer("/a~2").0, ["a~2"], "a ~ before anything else stays"); + assert_eq!(pointer("/a~").0, ["a~"]); + assert_eq!( + pointer("/a~/b").0, + ["a~/b"], + "and keeps the / after it in the segment" + ); + assert_eq!(pointer("/~~1").0, ["~~1"]); + } + + #[test] + fn an_array_is_indexed_as_jackson_indexes_one() { + let document = "[10, 11, 12]"; + assert_eq!(long_at(document, "/0"), Some(10)); + assert_eq!(long_at(document, "/2"), Some(12)); + assert_eq!(long_at(document, "/3"), None); + assert_eq!(long_at(document, "/01"), None, "a leading zero"); + assert_eq!(long_at(document, "/-1"), None); + assert_eq!(long_at(document, "/+1"), None); + assert_eq!(array_index("2147483647"), Some(2_147_483_647)); + assert_eq!(array_index("2147483648"), None, "past an int"); + assert_eq!( + long_at(r#"{"0":5}"#, "/0"), + Some(5), + "an object's member by its name" + ); + } + + #[test] + fn a_tree_is_read_as_jackson_reads_one() { + let document = r#" { "bitcoin" : { "usd" : 60000 , "eur" : [ 1 , 2.5 , "3" ] } , "ok" : true , "none" : null } "#; + assert_eq!(long_at(document, "/bitcoin/usd"), Some(60_000)); + assert_eq!(long_at(document, "/bitcoin/eur/1"), Some(2)); + assert_eq!(long_at(document, "/bitcoin/eur/2"), Some(3)); + assert_eq!(long_at(document, "/ok"), Some(1)); + assert_eq!(long_at(document, "/none"), Some(0)); + assert_eq!( + long_at(document, "/bitcoin"), + Some(0), + "an object reads as 0" + ); + assert_eq!(long_at(document, "/btc"), None); + assert_eq!(long_at(document, "/ok/x"), None, "nothing is under a value"); + assert_eq!( + long_at(r#"{"p":1,"p":2}"#, "/p"), + Some(2), + "a name given twice is the last" + ); + assert_eq!( + long_at(r#"{"a\/b":7}"#, "/a~1b"), + Some(7), + "an escape in a name" + ); + assert_eq!( + long_at(r#"{"\ud800":7,"p":1}"#, "/p"), + Some(1), + "half a surrogate pair is read" + ); + assert_eq!(long_at(r#"{"p":"\ud800"}"#, "/p"), Some(0)); + assert_eq!( + long_at(r#"{"😀":7}"#, "/\u{1f600}"), + Some(7), + "a pair escaped is its character" + ); + assert_eq!(long_at("{\"\u{e9}\":7}", "/\u{e9}"), Some(7)); + assert_eq!( + read_tree("").unwrap(), + None, + "nothing is Jackson's MissingNode" + ); + assert_eq!(read_tree(" \t\r\n").unwrap(), None); + assert_eq!( + long_at(r#"{"p":1} trailing [garbage"#, "/p"), + Some(1), + "what follows the value is left unread" + ); + assert_eq!(long_at(r#"[[[{"p":[{}]}]]]"#, "/0/0/0/p/0"), Some(0)); + } + + #[test] + fn what_jackson_refuses_by_default_is_refused() { + for refused in [ + "{", + "[", + r#"{"p":1"#, + r#"{"p":1,}"#, + "[1,]", + "[,1]", + "[1 2]", + r#"{"p" 1}"#, + "{p:1}", + "{'p':1}", + r#"{"p":01}"#, + r#"{"p":-}"#, + r#"{"p":1.}"#, + r#"{"p":.5}"#, + r#"{"p":+1}"#, + r#"{"p":1e}"#, + r#"{"p":1e+}"#, + r#"{"p":NaN}"#, + r#"{"p":Infinity}"#, + r#"{"p":"a\qb"}"#, + "[\"a\tb\"]", + r#"{"p":"\u12"}"#, + r#"{"p":"unclosed}"#, + "/* c */ 1", + "# c\n1", + "\u{1}1", + r#"{"p":tru}"#, + r#"{"p":truex}"#, + r#"{"p":nul}"#, + "\u{feff}{}", + ] { + assert!(read_tree(refused).is_err(), "{refused:?} was read"); + } + } + + #[test] + fn a_number_or_a_word_at_the_top_is_read_as_jackson_reads_it() { + let top = |document: &str| read_tree(document).map(|tree| tree.map(|node| node.as_long())); + assert_eq!(top("42"), Ok(Some(42))); + assert_eq!(top("42 x"), Ok(Some(42)), "whitespace, then anything"); + assert!(top("42x").is_err(), "Jackson's _verifyRootSpace"); + assert!(top("42,").is_err()); + assert!(top("42]").is_err()); + assert!(top("4.2e1x").is_err()); + assert_eq!(top("true"), Ok(Some(1))); + assert_eq!(top("false x"), Ok(Some(0))); + assert_eq!( + top("true,"), + Ok(Some(1)), + "below '0', nothing a name goes on with" + ); + assert_eq!(top("true:"), Ok(Some(1))); + assert_eq!(top("true]"), Ok(Some(1))); + assert_eq!( + top("true\u{b2}"), + Ok(Some(1)), + "a superscript two is no part of a Java name" + ); + assert_eq!( + top("true\u{1f600}"), + Ok(Some(1)), + "nor is half a surrogate pair" + ); + assert!(top("true_").is_err()); + assert!(top("true1").is_err()); + assert!(top("true\u{e9}").is_err()); + assert!(top("true\u{a5}").is_err(), "a currency sign is part of one"); + assert!( + top("null\u{ad}").is_err(), + "and a soft hyphen, which Java ignores in one" + ); + assert_eq!(top("\"7\" x"), Ok(Some(7))); + } + + #[test] + fn jacksons_limits_are_kept() { + let nested = |depth: usize| format!("{}{}", "[".repeat(depth), "]".repeat(depth)); + assert!(read_tree(&nested(1000)).is_ok()); + assert!(read_tree(&nested(1001)).is_err()); + assert!(read_tree(&"1".repeat(1000)).is_ok()); + assert!(read_tree(&"1".repeat(1001)).is_err()); + assert!(read_tree(&format!("1.{}", "1".repeat(999))).is_ok()); + assert!( + read_tree(&format!("1.{}", "1".repeat(1000))).is_err(), + "a fraction's digits count" + ); + assert!( + read_tree(&format!("1e{}", "1".repeat(1000))).is_err(), + "and an exponent's" + ); + } + + #[test] + fn a_whole_number_reads_as_its_low_64_bits() { + assert_eq!(long_at("9223372036854775807", ""), Some(i64::MAX)); + assert_eq!(long_at("-9223372036854775808", ""), Some(i64::MIN)); + assert_eq!( + long_at("9223372036854775808", ""), + Some(i64::MIN), + "BigInteger.longValue" + ); + assert_eq!(long_at("18446744073709551616", ""), Some(0)); + assert_eq!(long_at("18446744073709551621", ""), Some(5)); + assert_eq!(long_at("-18446744073709551617", ""), Some(-1)); + assert_eq!( + long_at("100000000000000000000000", ""), + Some(100_000_000_000_000_000_000_000u128 as u64 as i64) + ); + assert_eq!(long_at("-0", ""), Some(0)); + } + + #[test] + fn a_double_reads_as_javas_cast() { + assert_eq!(long_at(r#"{"p":60000.9}"#, "/p"), Some(60_000)); + assert_eq!(long_at(r#"{"p":-60000.9}"#, "/p"), Some(-60_000)); + assert_eq!(long_at(r#"{"p":1e4}"#, "/p"), Some(10_000)); + assert_eq!(long_at(r#"{"p":1E+4}"#, "/p"), Some(10_000)); + assert_eq!( + long_at(r#"{"p":1e400}"#, "/p"), + Some(i64::MAX), + "past a double: infinite" + ); + assert_eq!(long_at(r#"{"p":-1e400}"#, "/p"), Some(i64::MIN)); + assert_eq!(long_at(r#"{"p":1e-400}"#, "/p"), Some(0)); + assert_eq!( + long_at(r#"{"p":9.3e18}"#, "/p"), + Some(i64::MAX), + "past a long: the cast saturates" + ); + } + + #[test] + fn a_string_reads_as_jacksons_parse_as_long() { + for (text, long) in [ + ("60000", 60_000), + (" 60000 ", 60_000), + ("\t60000\u{1}", 60_000), + ("+60000", 60_000), + ("-60000", -60_000), + ("60000.9", 60_000), + ("6e4", 60_000), + ("1.5e3d", 1_500), + ("1.5f", 1), + ("0x1p4", 16), + ("-0x1.8p1", -3), + ("99999999999999999999", 0), + ("9223372036854775807", i64::MAX), + ("abc", 0), + ("", 0), + ("+", 0), + ("-", 0), + ("+-5", -5), + ("-+5", 0), + ("++5", 5), + ("NaN", 0), + ("Infinity", i64::MAX), + ("-Infinity", i64::MIN), + ("inf", 0), + ("1e", 0), + ("0x10", 0), + ("1_000", 0), + ("\u{663}", 0), + ] { + assert_eq!(parse_as_long(text), long, "{text:?}"); + } + } + + #[test] + fn a_double_is_read_as_java_reads_one() { + for (text, value) in [ + ("1", 1.0), + ("1.", 1.0), + (".5", 0.5), + ("1e3", 1000.0), + ("1E-3", 0.001), + ("+2.5", 2.5), + ("-2.5", -2.5), + ("2.5d", 2.5), + ("2.5F", 2.5), + (" 7 ", 7.0), + ("Infinity", f64::INFINITY), + ("-Infinity", f64::NEG_INFINITY), + ("0x1p3", 8.0), + ("0X1.8P1", 3.0), + ("0x.8p1", 1.0), + ("0x1.p1", 2.0), + ("0x1p3d", 8.0), + ("0x1P-1", 0.5), + ("0x0p0", 0.0), + ("0x0.0p99999999999", 0.0), + ("0x1p99999999999", f64::INFINITY), + ("0x1p-99999999999", 0.0), + ("0x1.fffffffffffffp1023", f64::MAX), + ("0x1p1024", f64::INFINITY), + ("0x1.fffffffffffff8p1023", f64::INFINITY), + ("0x1p-1022", f64::MIN_POSITIVE), + ("0x1p-1074", f64::from_bits(1)), + ("0x1p-1075", 0.0), + ("0x1.8p-1075", f64::from_bits(1)), + ("0x1.00000000000008p0", 1.0), + ("0x1.00000000000018p0", 1.0 + 2.0 * f64::EPSILON), + ("0x1.0000000000000801p0", 1.0 + f64::EPSILON), + ("0x0000000000000000000001p0", 1.0), + ("0x10000000000000000000p-76", 1.0), + ] { + assert_eq!(parse_double(text), Some(value), "{text:?}"); + } + assert!(parse_double("NaN").is_some_and(f64::is_nan)); + for refused in [ + "", + ".", + "e5", + "1e", + "1e+", + "1.2.3", + "0x1", + "0x1p", + "0xp1", + "0x.p1", + "0xgp1", + "1d5", + "inf", + "nan", + "NaNd", + "Infinityf", + "1_0", + "0x1p1.5", + "--1", + ] { + assert_eq!(parse_double(refused), None, "{refused:?}"); + } + } +} diff --git a/crates/cli/tests/import_blocks.rs b/crates/cli/tests/import_blocks.rs index 9268b5d..87524a8 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,12 +32,8 @@ 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([ - "--min-gas-limit", - "3000000", - "--max-gas-limit", - "18446744073709551615", - ]) + .args(["--consensus-rule", "rskip144=-1"]) + .args(["--min-gas-limit", "3000000"]) .arg("--import-blocks") .arg(blocks); cmd @@ -153,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/Cargo.toml b/crates/core/Cargo.toml index ebe5a81..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 } @@ -32,3 +33,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/btc_block.rs b/crates/core/src/btc_block.rs new file mode 100644 index 0000000..322b25a --- /dev/null +++ b/crates/core/src/btc_block.rs @@ -0,0 +1,338 @@ +//! 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 +//! 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)]))); + } + + /// 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/config.rs b/crates/core/src/config.rs index aabb0af..afa1a57 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, @@ -37,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. /// @@ -53,12 +59,39 @@ 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 + /// `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, + /// 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 @@ -80,22 +113,86 @@ 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. + /// `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, 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); 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), + "hop400" => { + if self.rskip290 == at(self.hop400) { + self.rskip290 = height; + } + self.hop400 = 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, @@ -104,10 +201,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, + 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, } } @@ -121,10 +228,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), + 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), } } @@ -132,15 +250,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`, 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 schedule reed810 either. - rskip144: None, + // 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), + // 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, } } } @@ -151,17 +287,24 @@ 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 + /// `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 - /// 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 /// (`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)`; @@ -197,10 +340,10 @@ 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, - max_gas_limit: 1_000_000_000, min_sequential_set_gas_limit: 10_000_000, activation_heights: ActivationHeights::mainnet(), genesis_spec: None, @@ -217,10 +360,10 @@ 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, - max_gas_limit: 1_000_000_000, min_sequential_set_gas_limit: 10_000_000, activation_heights: ActivationHeights::testnet(), genesis_spec: None, @@ -237,10 +380,10 @@ 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, - max_gas_limit: 10_000_000, min_sequential_set_gas_limit: 6_800_000, activation_heights: ActivationHeights::regtest(), genesis_spec: None, @@ -250,13 +393,36 @@ 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, + } + } + + /// rskj `RskContext` loads a genesis file with `useRskip92Encoding` as + /// RSKIP92, an orchid rule, is active at block 0. + fn rskip92_at_genesis(&self) -> bool { + self.activation_heights.orchid == 0 + } + /// 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 /// from standard encoding, producing a different hash. pub fn known_genesis_hash(&self) -> Option { if let Some(spec) = &self.genesis_spec { - return Some(spec.hash()); + return Some(spec.hash_encoded(self.rskip92_at_genesis())); } match self.chain_id { 30 => Some("0xf88529d4ab262c0f4d042e9d8d3f2472848eaafe1a9b7213f57617eb40a9f9e0".parse().unwrap()), @@ -271,7 +437,7 @@ impl ChainConfig { let empty_trie_hash: B256 = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421".parse().unwrap(); if let Some(spec) = &self.genesis_spec { - return spec.header(); + return spec.header_encoded(self.rskip92_at_genesis()); } match self.chain_id { @@ -300,14 +466,17 @@ impl ChainConfig { bitcoin_merged_mining_coinbase_transaction: Some(Bytes::from_static(&[0x00])), cached_hash: None, cached_hash_for_merged_mining: None, + kept: Self::genesis_difficulty_as_written(), }, + // 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), @@ -321,11 +490,12 @@ 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: Self::genesis_difficulty_as_written(), }, _ => Header { parent_hash: B256::ZERO, @@ -364,10 +534,22 @@ impl ChainConfig { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, }, } } + /// Mainnet's and testnet's genesis files write the difficulty + /// `0x0000100000`, and rskj's `GenesisHeader` writes it back, and hashes + /// it, as written: kept, so that the genesis written to the store reads + /// back with the network's genesis hash. + fn genesis_difficulty_as_written() -> Option> { + Some(Box::new(crate::types::header::KeptFields { + difficulty: Some(Bytes::from_static(&hex_literal::hex!("0000100000"))), + ..Default::default() + })) + } + /// The ten accounts rskj's regtest genesis funds, in its own order. /// /// `keccak256("cow")`, then `keccak256("cow1")` through @@ -586,6 +768,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 +785,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); } @@ -613,6 +797,152 @@ 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))); + } + + /// `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(); @@ -664,13 +994,71 @@ 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); + } + + /// Mainnet's and testnet's genesis headers hash to the networks' genesis + /// hashes, and so do they as this node writes them to its store and reads + /// them back: rskj's `GenesisHeader` hashes the difficulty as the genesis + /// file writes it, `0x0000100000`. + #[test] + fn the_stored_genesis_reads_back_with_the_networks_genesis_hash() { + use crate::types::header::HeaderForm; + for (config, heights) in [ + (ChainConfig::mainnet(), ActivationHeights::mainnet()), + (ChainConfig::testnet(), ActivationHeights::testnet()), + ] { + let genesis = config.genesis_header(); + let known = config.known_genesis_hash().unwrap(); + assert_eq!(genesis.clone().with_activations(&heights).hash(), known); + let read = Header::decode_in( + &mut genesis.encode_stored().as_slice(), + &heights, + HeaderForm::Stored, + ) + .unwrap(); + assert_eq!(read.hash(), known, "chain {}", config.chain_id); + } } #[test] diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index bda3ff7..5136553 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -7,6 +7,16 @@ pub mod validation; pub mod config; pub mod rlp_compat; pub mod rskj_genesis; +pub mod rskj_rlp; +pub mod partial_merkle_tree; +pub mod btc_block; +pub mod selection_rule; + +#[cfg(test)] +mod mutate; + +#[cfg(test)] +mod rskj_tests; pub use types::block::Block; pub use types::header::Header; diff --git a/crates/core/src/mutate.rs b/crates/core/src/mutate.rs new file mode 100644 index 0000000..37848f6 --- /dev/null +++ b/crates/core/src/mutate.rs @@ -0,0 +1,137 @@ +//! 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 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()), + (ActivationHeights::regtest(), ChainConfig::regtest()), + ]; + for (activations, config) in networks { + let verifier = HeaderVerifier::default_rsk(Arc::new(config)); + 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); + 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 new file mode 100644 index 0000000..cd6cb20 --- /dev/null +++ b/crates/core/src/partial_merkle_tree.rs @@ -0,0 +1,682 @@ +//! A Bitcoin partial Merkle tree, as bitcoinj-thin's `PartialMerkleTree` +//! (`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 +//! 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. +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 { + 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)); + } + + /// 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_genesis.rs b/crates/core/src/rskj_genesis.rs index 5bfff2a..6150097 100644 --- a/crates/core/src/rskj_genesis.rs +++ b/crates/core/src/rskj_genesis.rs @@ -120,20 +120,46 @@ impl GenesisSpec { } let coinbase = field("coinbase")?; - let timestamp = u256_from_be(&field("timestamp")?)?; + // rskj reads the timestamp and the gas limit with + // `ByteUtil.byteArrayToLong`, which refuses more than 8 bytes, leading + // zeros or not. + let long = |key: &str| -> Result> { + let bytes = field(key)?; + if bytes.len() > 8 { + bail!("'{key}' is longer than 8 bytes, which rskj refuses"); + } + Ok(bytes) + }; + let timestamp = long("timestamp")?; + // Eight bytes whose top bit is set are a negative `long` to rskj: a + // timestamp no header here holds. + if timestamp.len() == 8 && timestamp[0] & 0x80 != 0 { + bail!("'timestamp' is below zero as rskj reads it"); + } + let timestamp = u256_from_be(×tamp)?; + // `RLP.parseSignedCoinNonNullZero`: a signed number of at least one + // byte, for `new BigInteger` throws on none, so a file without one + // is refused. One whose top bit is set is below zero, which no + // header here holds. + let minimum_gas_price = field("minimumGasPrice")?; + match minimum_gas_price.first() { + None => bail!("'minimumGasPrice' is empty, which rskj refuses"), + Some(first) if first & 0x80 != 0 => { + bail!("'minimumGasPrice' is below zero as rskj reads it") + } + Some(_) => {} + } Ok(Self { parent_hash: B256::try_from(field("parentHash")?.as_slice()) .context("parentHash is not 32 bytes")?, - coinbase: if coinbase.is_empty() { - Address::ZERO - } else { - Address::try_from(coinbase.as_slice()).context("coinbase is not 20 bytes")? - }, + // rskj's `new RskAddress(bytes)` throws on any other length, none + // included. + coinbase: Address::try_from(coinbase.as_slice()).context("coinbase is not 20 bytes")?, timestamp: u64::try_from(timestamp).context("timestamp out of range")?, extra_data: Bytes::from(field("extraData")?), - gas_limit: u256_from_be(&field("gasLimit")?)?, + gas_limit: u256_from_be(&long("gasLimit")?)?, difficulty_bytes: Bytes::from(field("difficulty")?), - minimum_gas_price: u256_from_be(&optional("minimumGasPrice")?)?, + minimum_gas_price: u256_from_be(&minimum_gas_price)?, bitcoin_merged_mining_header: Bytes::from(optional("bitcoinMergedMiningHeader")?), bitcoin_merged_mining_merkle_proof: Bytes::from(optional( "bitcoinMergedMiningMerkleProof", @@ -165,29 +191,41 @@ impl GenesisSpec { || !self.bitcoin_merged_mining_coinbase_transaction.is_empty() } + /// The gas limit as rskj's `GenesisHeader` holds it: the file's value + /// (`ByteUtil.byteArrayToLong`, then `longToBytes`) without leading + /// zeros, and one zero byte for zero (`ByteUtil.stripLeadingZeroes`). + fn gas_limit_bytes(&self) -> Vec { + crate::types::header::strip_leading_zeroes(&self.gas_limit.to_be_bytes::<32>()).to_vec() + } + /// The genesis hash, as rskj's `GenesisHeader.getEncoded` produces it: a /// version-0 header with the difficulty bytes as given and RSKIP92's /// encoding (only the merged-mining header, no proof or coinbase), which /// is what every regtest chain since Orchid uses. pub fn hash(&self) -> B256 { + self.hash_encoded(true) + } + + /// The genesis hash with RSKIP92's encoding or without it, as rskj's + /// `GenesisLoaderImpl` is given `useRskip92Encoding`: whether RSKIP92 is + /// active at block 0 (`RskContext`). Without it a header with mining + /// fields hashes the merkle proof and the coinbase after the Bitcoin + /// header (`BlockHeader.getEncodedForHash`). + pub fn hash_encoded(&self, rskip92: bool) -> B256 { let mut list = Vec::new(); self.parent_hash.encode(&mut list); EMPTY_LIST_HASH.encode(&mut list); - if self.coinbase == Address::ZERO { - // RLP.encodeRskAddress: the zero address is an empty element. - list.push(0x80); - } else { - self.coinbase.encode(&mut list); - } + // `new RskAddress(bytes)` holds the file's 20 bytes, and + // `RLP.encodeRskAddress` writes them: only the null address, of no + // bytes, is written empty, and the zero address is not it. + self.coinbase.encode(&mut list); self.state_root.unwrap_or_default().encode(&mut list); EMPTY_TRIE_HASH.encode(&mut list); EMPTY_TRIE_HASH.encode(&mut list); alloy_primitives::Bloom::ZERO.encode(&mut list); self.difficulty_bytes.encode(&mut list); 0u64.encode(&mut list); // number - let gas_limit = self.gas_limit.to_be_bytes::<32>(); - let start = gas_limit.iter().position(|b| *b != 0).unwrap_or(32); - gas_limit[start..].encode(&mut list); + self.gas_limit_bytes().as_slice().encode(&mut list); 0u64.encode(&mut list); // gasUsed self.timestamp.encode(&mut list); self.extra_data.encode(&mut list); @@ -202,6 +240,11 @@ impl GenesisSpec { 0u64.encode(&mut list); // uncleCount if self.has_mining_fields() { self.bitcoin_merged_mining_header.encode(&mut list); + if !rskip92 { + self.bitcoin_merged_mining_merkle_proof.encode(&mut list); + self.bitcoin_merged_mining_coinbase_transaction + .encode(&mut list); + } } let mut out = Vec::with_capacity(list.len() + 4); alloy_rlp::Header { @@ -213,11 +256,26 @@ impl GenesisSpec { alloy_primitives::keccak256(&out) } - /// The genesis header, carrying [`GenesisSpec::hash`]: it cannot be - /// recomputed from the fields, because the difficulty bytes are not - /// canonical. + /// The genesis header, carrying [`GenesisSpec::hash`], and keeping the + /// difficulty's bytes as the file writes them and the gas limit's as + /// rskj holds them where the typed fields would write them otherwise, so + /// that the header written to the store reads back with that hash. pub fn header(&self) -> Header { + self.header_encoded(true) + } + + /// The genesis header carrying [`GenesisSpec::hash_encoded`]'s hash. + pub fn header_encoded(&self, rskip92: bool) -> Header { let some = |b: &Bytes| (!b.is_empty()).then(|| b.clone()); + let java = crate::types::header::java_signed_bytes; + let difficulty = U256::from_be_slice(&self.difficulty_bytes); + let gas_limit = self.gas_limit_bytes(); + let kept = crate::types::header::KeptFields { + difficulty: (self.difficulty_bytes[..] != java(&difficulty)[..]) + .then(|| self.difficulty_bytes.clone()), + gas_limit: (gas_limit != java(&self.gas_limit)).then(|| Bytes::from(gas_limit)), + ..Default::default() + }; Header { parent_hash: self.parent_hash, ommers_hash: EMPTY_LIST_HASH, @@ -227,7 +285,7 @@ impl GenesisSpec { receipts_root: EMPTY_TRIE_HASH, logs_bloom: Default::default(), extension_data: None, - difficulty: U256::from_be_slice(&self.difficulty_bytes), + difficulty, number: 0, gas_limit: self.gas_limit, gas_used: 0, @@ -243,8 +301,9 @@ impl GenesisSpec { bitcoin_merged_mining_coinbase_transaction: some( &self.bitcoin_merged_mining_coinbase_transaction, ), - cached_hash: Some(self.hash()), + cached_hash: Some(self.hash_encoded(rskip92)), cached_hash_for_merged_mining: None, + kept: (kept != Default::default()).then(|| Box::new(kept)), } } } @@ -309,4 +368,157 @@ mod tests { ); assert!(GenesisSpec::from_json(&json).is_err()); } + + /// rskj's `rsk-dev.json` has a gas limit whose top bit is set and a + /// difficulty written with leading zeros; here with the zero address as + /// coinbase too, which rskj writes as its 20 bytes, and, in a second file, + /// a gas limit of zero, which a `GenesisHeader` holds as one zero byte. + /// The genesis header's fields hash as the file does, and the header the + /// store writes reads back with that hash. + #[test] + fn the_genesis_header_hashes_as_the_file_and_reads_back_so() { + use crate::types::header::HeaderForm; + let heights = crate::config::ActivationHeights::regtest(); + let dev = GENESIS + .replace("0x017D7840", "0x989680") + .replace("0x0010000000", "0x0000000001") + .replace( + "0x3333333333333333333333333333333333333333", + "0x0000000000000000000000000000000000000000", + ); + let no_gas = dev.replace("0x989680", "0x00"); + for json in [dev, no_gas] { + let g = GenesisSpec::from_json(&json) + .unwrap() + .with_state_root(B256::repeat_byte(1)); + let header = g.header(); + let built = Header { + cached_hash: None, + ..header.clone() + } + .with_activations(&heights); + assert_eq!( + built.hash(), + g.hash(), + "the header's fields hash as the file's" + ); + let read = Header::decode_in( + &mut header.encode_stored().as_slice(), + &heights, + HeaderForm::Stored, + ) + .unwrap(); + assert_eq!( + read.hash(), + g.hash(), + "and so does what the store reads back" + ); + } + } + + /// rskj's `new RskAddress(bytes)` throws on a coinbase of any length but + /// 20, none included. + #[test] + fn a_coinbase_that_is_not_20_bytes_is_refused() { + for coinbase in ["0x", "0x33"] { + let json = GENESIS.replace("0x3333333333333333333333333333333333333333", coinbase); + assert!(GenesisSpec::from_json(&json).is_err(), "{coinbase}"); + } + } + + /// rskj reads the timestamp and the gas limit with + /// `ByteUtil.byteArrayToLong`, which refuses more than 8 bytes even where + /// the first are zeros, and reads a timestamp of 8 bytes whose top bit is + /// set as below zero. A gas limit of 8 bytes is the block the same value + /// written short is. + #[test] + fn a_timestamp_or_gas_limit_over_8_bytes_is_refused() { + let timestamp = r#""timestamp": "0x00""#; + let gas_limit = r#""gasLimit": "0x017D7840""#; + for (from, to) in [ + (timestamp, r#""timestamp": "0x000000000000000000""#), + (timestamp, r#""timestamp": "0x8000000000000000""#), + (gas_limit, r#""gasLimit": "0x0000000000017D7840""#), + ] { + let json = GENESIS.replace(from, to); + assert!(GenesisSpec::from_json(&json).is_err(), "{to}"); + } + let short = GenesisSpec::from_json(GENESIS).unwrap(); + let eight = GENESIS.replace(gas_limit, r#""gasLimit": "0x00000000017D7840""#); + let eight = GenesisSpec::from_json(&eight).unwrap(); + assert_eq!(eight.gas_limit, U256::from(25_000_000)); + assert_eq!(eight.hash(), short.hash()); + let latest = GENESIS.replace(timestamp, r#""timestamp": "0x7fffffffffffffff""#); + let latest = GenesisSpec::from_json(&latest).unwrap(); + assert_eq!(latest.timestamp, i64::MAX as u64); + } + + /// rskj's `RLP.parseSignedCoinNonNullZero` reads the minimum gas price + /// as a signed number with `new BigInteger`, which throws on no bytes: a + /// file without one, or with an empty one, is refused, and so is one + /// below zero, which a leading zero byte keeps a price from being. + #[test] + fn a_minimum_gas_price_is_required_and_not_below_zero() { + let missing = GENESIS.replace("\"minimumGasPrice\"", "\"unread\""); + assert!(GenesisSpec::from_json(&missing).is_err(), "missing"); + let price = r#""minimumGasPrice": "0x00""#; + for written in ["0x", "0x80", "0xff00"] { + let json = GENESIS.replace(price, &format!(r#""minimumGasPrice": "{written}""#)); + assert!(GenesisSpec::from_json(&json).is_err(), "{written}"); + } + let json = GENESIS.replace(price, r#""minimumGasPrice": "0x0080""#); + let g = GenesisSpec::from_json(&json).unwrap(); + assert_eq!(g.minimum_gas_price, U256::from(128)); + } + + /// Where orchid isn't at block 0, rskj loads the file without RSKIP92's + /// encoding (`useRskip92Encoding` is RSKIP92 at block 0), so block 0's + /// hash covers the merkle proof and the coinbase after the Bitcoin + /// header. The chain's configuration hashes it so, the header's fields + /// hash so under those heights, and the store reads it back so. A file + /// without mining fields hashes alike either way. + #[test] + fn where_orchid_is_not_at_genesis_its_hash_covers_the_proof_and_coinbase() { + use crate::types::header::HeaderForm; + let g = GenesisSpec::from_json(GENESIS) + .unwrap() + .with_state_root(B256::repeat_byte(1)); + let without = g.hash_encoded(false); + assert_ne!(without, g.hash(), "the file has mining fields"); + + let mut config = crate::config::ChainConfig::regtest(); + config.genesis_spec = Some(g.clone()); + assert_eq!(config.known_genesis_hash(), Some(g.hash())); + config.activation_heights.orchid = 5; + assert_eq!(config.known_genesis_hash(), Some(without)); + + let heights = config.activation_heights.clone(); + let header = config.genesis_header(); + assert_eq!(header.hash(), without); + let built = Header { + cached_hash: None, + ..header.clone() + } + .with_activations(&heights); + assert_eq!( + built.hash(), + without, + "the header's fields hash as the file's" + ); + let read = Header::decode_in( + &mut header.encode_stored().as_slice(), + &heights, + HeaderForm::Stored, + ) + .unwrap(); + assert_eq!( + read.hash(), + without, + "and so does what the store reads back" + ); + + let bare = GENESIS.replace("\"bitcoinMergedMining", "\"unread"); + let bare = GenesisSpec::from_json(&bare).unwrap(); + assert_eq!(bare.hash_encoded(false), bare.hash()); + } } diff --git a/crates/core/src/rskj_rlp.rs b/crates/core/src/rskj_rlp.rs new file mode 100644 index 0000000..621072c --- /dev/null +++ b/crates/core/src/rskj_rlp.rs @@ -0,0 +1,303 @@ +//! 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, and for which reason. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +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)] +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 = 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, + } + } + + /// 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::NotAList), + } + } +} + +/// `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) +} + +/// 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::Empty); + } + 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]; + 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::TooLong)?, + offset, + ) + }; + let ending = (position as i32) + .checked_add(length) + .ok_or(RlpError::TooLong)? as usize; + if ending > data.len() { + return Err(RlpError::PastEnd); + } + 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::PastEnd); + } + 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::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::NegativeLength); + } + 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::NotAList), + "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])])); + 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), 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::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: + /// 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/rskj_tests/blocks/block_difficulty_rule_test.rs b/crates/core/src/rskj_tests/blocks/block_difficulty_rule_test.rs new file mode 100644 index 0000000..ac056b7 --- /dev/null +++ b/crates/core/src/rskj_tests/blocks/block_difficulty_rule_test.rs @@ -0,0 +1,37 @@ +//! rskj `co.rsk.validators.BlockDifficultyRuleTest`: a block's difficulty +//! is the one `DifficultyCalculator` gives for it and its parent, and no +//! other (`BlockDifficultyRule`). + +use super::support::header; +use crate::config::ChainConfig; +use crate::validation::{DifficultyRule, ParentHeaderValidator, ValidationError}; +use alloy_primitives::U256; +use std::sync::Arc; + +/// rskj `invalidWhenCalculatedDifficultyDoesntMatch`: on regtest a block +/// five seconds after a parent of difficulty 20,480 must carry 20,490 +/// (+1/2048, inside the ten-second duration); one more or one less is +/// refused. +#[test] +fn a_difficulty_other_than_the_calculated_one_is_refused() { + let rule = DifficultyRule { + config: Arc::new(ChainConfig::regtest()), + }; + let mut parent = header(10, 1_000); + parent.difficulty = U256::from(20_480); + let child = |difficulty: u64| { + let mut child = header(11, 1_005); + child.difficulty = U256::from(difficulty); + rule.validate_with_parent(&child, &parent) + }; + assert_eq!(child(20_490), Ok(())); + for got in [20_491, 20_489] { + assert_eq!( + child(got), + Err(ValidationError::DifficultyMismatch { + expected: U256::from(20_490), + got: U256::from(got), + }) + ); + } +} diff --git a/crates/core/src/rskj_tests/blocks/block_difficulty_validation_rule_test.rs b/crates/core/src/rskj_tests/blocks/block_difficulty_validation_rule_test.rs new file mode 100644 index 0000000..94be04a --- /dev/null +++ b/crates/core/src/rskj_tests/blocks/block_difficulty_validation_rule_test.rs @@ -0,0 +1,37 @@ +//! rskj `co.rsk.validators.BlockDifficultyValidationRuleTest`: the +//! difficulty a block must carry, from regtest's `DifficultyCalculator`. + +use super::support::header; +use crate::config::ChainConfig; +use crate::validation::{DifficultyRule, ParentHeaderValidator, ValidationError}; +use alloy_primitives::U256; +use std::sync::Arc; + +/// rskj `testDifficulty`: a block ten seconds after a parent of difficulty +/// 2,048, naming one uncle, carries 2,049. rskj gives a block +/// `(1 + uncleCount) * duration` seconds before it counts as late, so the +/// uncle makes ten seconds early on regtest's ten-second duration: the +/// difficulty rises by 2,048 / 2,048. With no uncle the same ten seconds are +/// on time, and the difficulty stays at 2,048. +#[test] +fn an_uncle_doubles_the_time_a_block_has_before_it_is_late() { + let rule = DifficultyRule { + config: Arc::new(ChainConfig::regtest()), + }; + let mut parent = header(0, 0); + parent.difficulty = U256::from(2_048); + let child = |uncle_count: u64| { + let mut child = header(1, 10); + child.uncle_count = uncle_count; + child.difficulty = U256::from(2_049); + rule.validate_with_parent(&child, &parent) + }; + assert_eq!(child(1), Ok(())); + assert_eq!( + child(0), + Err(ValidationError::DifficultyMismatch { + expected: U256::from(2_048), + got: U256::from(2_049), + }) + ); +} diff --git a/crates/core/src/rskj_tests/blocks/block_executor_test.rs b/crates/core/src/rskj_tests/blocks/block_executor_test.rs new file mode 100644 index 0000000..a413428 --- /dev/null +++ b/crates/core/src/rskj_tests/blocks/block_executor_test.rs @@ -0,0 +1,42 @@ +//! rskj `co.rsk.core.bc.BlockExecutorTest`, its strange transactions +//! (`createStrangeTransaction`): which a block can carry, read from the +//! bytes a block holds them in. + +use crate::Transaction; +use alloy_primitives::U256; +use alloy_rlp::Encodable; + +/// An unsigned transfer, from its nine fields: nonce 0, gas price 1, gas +/// limit 21,000, to twenty bytes, a value of the bytes given, no data and +/// no signature. +fn unsigned_transfer(value: &[u8]) -> Vec { + let to = [0x11u8; 20]; + let fields: [&[u8]; 9] = [&[], &[1], &[0x52, 0x08], &to, value, &[], &[], &[], &[]]; + let mut payload = Vec::new(); + for field in fields { + field.encode(&mut payload); + } + let mut raw = Vec::new(); + alloy_rlp::Header { + list: true, + payload_length: payload.len(), + } + .encode(&mut raw); + raw.extend_from_slice(&payload); + raw +} + +/// rskj `executeBlocksWithOneStrangeTransactions3`: a value written in +/// 1,024 bytes, all but its last zero, reads as its number, and the +/// transaction is kept, and hashed, as the one writing it in one byte. +/// Its first two, a `to` of one byte and of 1,024, can't be read +/// (`what_rskj_cannot_take_is_refused_when_read`, transaction.rs). +#[test] +fn a_value_padded_with_zeros_reads_as_its_number() { + let padded = [vec![0u8; 1023], vec![10]].concat(); + let read = Transaction::from_rskj(&unsigned_transfer(&padded)).unwrap(); + let one_byte = Transaction::from_rskj(&unsigned_transfer(&[10])).unwrap(); + assert_eq!(read.value, U256::from(10)); + assert_eq!(read.cached_rlp, one_byte.cached_rlp); + assert_eq!(read.tx_hash(), one_byte.tx_hash()); +} diff --git a/crates/core/src/rskj_tests/blocks/block_factory_test.rs b/crates/core/src/rskj_tests/blocks/block_factory_test.rs new file mode 100644 index 0000000..8c73b32 --- /dev/null +++ b/crates/core/src/rskj_tests/blocks/block_factory_test.rs @@ -0,0 +1,790 @@ +//! rskj `co.rsk.core.BlockFactoryTest`: how `BlockFactory.decodeHeader` reads +//! a header at its height. +//! +//! rskj counts a header's fields exactly (`canBeDecoded`): the sixteen every +//! header has; the UMM root from RSKIPUMM; from RSKIP351, and in the full +//! encoding only, the version, the edges where RSKIP144 is active too and the +//! base event where RSKIP535 is; then the three merged-mining fields or none. +//! On testnet below #7,800,000 it also reads a full header one field short +//! of that, with an empty base event (`isInBaseEventRecoveryWindow`). A +//! header of any other count isn't read; neither is one whose Bitcoin header +//! isn't 80 bytes from RSKIP98. +//! +//! rskj's test mocks its activations rule by rule. Here a case's heights put +//! each rule it has on at genesis and every other off; rustock's `orchid` +//! is RSKIP92 and RSKIP98 together, as `reference.conf` has them. A header +//! is built with the fields rskj's builder gives it, encoded by rustock, +//! its fields counted against the count rskj's test asserts, and read back +//! under those heights. + +use super::support::{compressed_coinbase, header}; +use crate::config::{ActivationHeights, ChainConfig}; +use crate::rskj_rlp; +use crate::types::header::{Header, HeaderForm, HeaderVersionFields}; +use crate::types::header_extension::{BlockHeaderExtension, ExtensionCommitment}; +use crate::validation::merged_mining::{extract_fork_detection_data, RSK_TAG}; +use alloy_primitives::{hex, keccak256, Address, Bloom, Bytes, B256, U256}; +use alloy_rlp::Encodable; + +/// Heights with every rule off, for a case to turn on the ones its rskj test +/// enables. +pub(super) fn heights(turn_on: impl FnOnce(&mut ActivationHeights)) -> ActivationHeights { + let mut heights = ActivationHeights { + orchid: u64::MAX, + wasabi100: u64::MAX, + papyrus200: u64::MAX, + iris300: u64::MAX, + fingerroot500: u64::MAX, + hop400: u64::MAX, + reed810: u64::MAX, + rskip97: None, + rskip144: None, + rskip351: None, + rskip290: None, + rskip_umm: None, + rskip535: None, + base_event_recovery_below: None, + }; + turn_on(&mut heights); + heights +} + +/// The header rskj's `createBlockHeader` builds at `number`: no merged-mining +/// fields, no UMM root, no version fields. +pub(super) fn built(number: u64) -> Header { + let mut header = header(number, 7_731_067); + header.parent_hash = keccak256(format!("hash{number}")); + header.ommers_hash = keccak256([alloy_rlp::EMPTY_LIST_CODE]); + header.beneficiary = Address::from_slice(&keccak256(format!("coinbase{number}"))[12..]); + header.state_root = keccak256([alloy_rlp::EMPTY_STRING_CODE]); + header.transactions_root = keccak256("tx_trie_root"); + header.receipts_root = keccak256([alloy_rlp::EMPTY_STRING_CODE]); + header.difficulty = U256::from(1); + header.gas_limit = U256::from(6_800_000); + header.gas_used = 3_000_000; + header.minimum_gas_price = U256::from(10); + header +} + +/// With rskj's merged-mining fields: a Bitcoin header of `btc_header` +/// bytes, a 32-byte proof and a 128-byte coinbase. +pub(super) fn mined(mut header: Header, btc_header: usize) -> Header { + header.bitcoin_merged_mining_header = Some(Bytes::from(vec![0; btc_header])); + header.bitcoin_merged_mining_merkle_proof = Some(Bytes::from(vec![0; 32])); + header.bitcoin_merged_mining_coinbase_transaction = Some(Bytes::from(vec![0; 128])); + header +} + +/// From RSKIP351: `version`, the `edges` and the `base_event`. +fn versioned( + mut header: Header, + version: u8, + edges: Option<&[i16]>, + base_event: Option<&[u8]>, +) -> Header { + header.version_fields = Some(HeaderVersionFields { + version, + edges: edges.map(<[i16]>::to_vec), + base_event: base_event.map(Bytes::copy_from_slice), + }); + header +} + +/// rskj's test bloom: its first four bytes set. +fn bloom() -> Bloom { + let mut bytes = [0u8; 256]; + bytes[..4].fill(1); + Bloom::from(bytes) +} + +const EDGES: [i16; 4] = [-5, 300, 7, 32_767]; + +/// Twenty bytes named by `label`, as rskj's `TestUtils.generateBytes`. +pub(super) fn twenty_bytes(label: &str) -> Bytes { + Bytes::copy_from_slice(&keccak256(label)[..20]) +} + +fn base_event() -> Vec { + keccak256("baseEvent").to_vec() +} + +/// The full encoding, as blocks carry headers (`getFullEncoded`). +pub(super) fn full(header: &Header) -> Vec { + let mut out = Vec::new(); + header.encode(&mut out); + out +} + +/// How many fields an encoded header has. +pub(super) fn count(encoded: &[u8]) -> usize { + let (list, _) = rskj_rlp::first_element(encoded).unwrap(); + list.elements().unwrap().len() +} + +fn read( + encoded: &[u8], + heights: &ActivationHeights, + form: HeaderForm, +) -> alloy_rlp::Result
{ + Header::decode_in(&mut &encoded[..], heights, form) +} + +/// The hash rskj gives `header` built under `heights`. +fn hash_under(header: &Header, heights: &ActivationHeights) -> B256 { + header.clone().with_activations(heights).hash() +} + +/// Reads `header` back from `encoded` under `heights`, and checks it keeps +/// its hash. +fn round_trip( + header: &Header, + encoded: &[u8], + heights: &ActivationHeights, + form: HeaderForm, +) -> Header { + let read = + read(encoded, heights, form).unwrap_or_else(|e| panic!("block {}: {e}", header.number)); + assert_eq!( + read.hash(), + hash_under(header, heights), + "block {}", + header.number + ); + read +} + +/// The commitment a compressed header carries in its bloom's slot. +fn commitment(header: &Header) -> ExtensionCommitment { + header.extension_commitment().unwrap().unwrap() +} + +// ── the genesis block, and the hash a header has without mining fields + +/// rskj's test genesis, which is testnet's. +const GENESIS: &str = concat!( + "f901dbf901d6a00000000000000000000000000000000000000000000000000000000000000000a01dcc", + "4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d493479433333333333333333333333333333333333", + "33333a045bce5168430c42b3d568331753f900a32457b4f3748697cbd8375ff4da72641a056e81f171bcc55a6ff8345e6", + "92c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb", + "5e363b421b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "00000000000000000000000000000000000000000085000010000080834c4b40808085434d272841800080000000c0c0", +); + +/// rskj's test genesis, read under `heights` as the header of a block. +fn decoded_genesis(heights: &ActivationHeights) -> Header { + let raw = hex::decode(GENESIS).unwrap(); + let (block, _) = rskj_rlp::first_element(&raw).unwrap(); + let parts = block.elements().unwrap(); + assert_eq!(parts.len(), 3, "header, transactions, uncles"); + let rskj_rlp::Element::List { encoding, .. } = parts[0] else { + panic!("a header is a list"); + }; + read(encoding, heights, HeaderForm::Full).unwrap() +} + +/// rskj `decodeGenesisBlock`: the genesis block, read with RSKIP92 on from +/// genesis, has testnet's genesis hash. rskj reads block 0 as a +/// `GenesisHeader`, which writes its difficulty back as the bytes it was +/// read from, `0x0000100000` here, and hashes those. +#[test] +fn the_genesis_block_reads_to_its_hash() { + let genesis = decoded_genesis(&heights(|h| h.orchid = 0)); + let expected: B256 = "0xcabb7fbe88cd6d922042a32ffc08ce8b1fbb37d650b9d4e7dbfe2a7469adfa42" + .parse() + .unwrap(); + assert_eq!(genesis.hash(), expected); +} + +/// rskj `genesisHasVersion0`: with no rule on, the genesis block reads as a +/// version-0 header, with the fields of testnet's genesis, its difficulty +/// held as the bytes it came as. +#[test] +fn the_genesis_block_reads_as_testnets_version_0_genesis() { + let genesis = decoded_genesis(&heights(|_| {})); + let testnet = ChainConfig::testnet().genesis_header(); + assert_eq!(genesis.version_fields, None); + let difficulty = |header: &Header| header.kept().and_then(|kept| kept.difficulty.clone()); + assert_eq!(difficulty(&genesis), Some(hex!("0000100000").into())); + assert_eq!(difficulty(&genesis), difficulty(&testnet)); + assert_eq!( + Header { + kept: None, + ..genesis + }, + Header { + kept: None, + ..testnet + } + ); +} + +/// rskj `newHeaderWithNoForkDetectionDataAndRskip110On`: a header without +/// merged-mining fields hashes as its merged-mining hash, which leaves +/// those fields out. +#[test] +fn a_header_without_mining_fields_hashes_as_its_merged_mining_hash() { + let heights = heights(|h| { + h.orchid = 0; + h.wasabi100 = 0; + }); + let header = built(20).with_activations(&heights); + assert_eq!(header.hash(), header.hash_for_merged_mining()); +} + +// ── before RSKIPUMM and RSKIP351: sixteen fields, and three for mining + +/// rskj `decodeBlockPriorToHeight449AndRskip110On`, +/// `decodeBlockPriorToHeight449AndRskip110Off`, +/// `decodeBlockAfterHeight449AndRskip110OFF` and +/// `decodeBlockAfterHeight449AndRskip110On`: a merge-mined header has 19 +/// fields and reads back with its hash and the fork-detection data its +/// coinbase carries, below block 449 and above it, with RSKIP110 or without. +/// rskj's test writes the tag at the coinbase's start; here it follows the +/// coinbase's 40-byte SHA-256 midstate, where the proof-of-work rule looks +/// for it, and where rustock reads fork-detection data from. +#[test] +fn a_merge_mined_header_has_nineteen_fields_and_reads_back() { + for (number, rskip110) in [(20, true), (20, false), (457, false), (457, true)] { + let heights = heights(|h| { + h.orchid = 0; + if rskip110 { + h.wasabi100 = 0; + } + }); + let mut header = mined(built(number), 80); + if number == 457 && rskip110 { + // The coinbase commits to the merged-mining hash's first twenty + // bytes, then twelve of fork-detection data. + let hash = header.hash_for_merged_mining(); + let fork_detection = keccak256("forkDetectionData"); + let tail = [RSK_TAG, &hash[..20], &fork_detection[..12]].concat(); + header.bitcoin_merged_mining_coinbase_transaction = + Some(Bytes::from(compressed_coinbase(0, &tail))); + assert_eq!( + extract_fork_detection_data(&header).map(|data| data.to_vec()), + Some(fork_detection[..12].to_vec()) + ); + } + let encoded = full(&header); + assert_eq!(count(&encoded), 19, "block {number}"); + let read = round_trip(&header, &encoded, &heights, HeaderForm::Full); + assert_eq!( + extract_fork_detection_data(&read), + extract_fork_detection_data(&header) + ); + } +} + +/// rskj `decodeWithNoMergedMiningDataAndRskip110OffAndNoForkDetectionData` +/// and `decodeWithNoMergedMiningDataAndRskip110OffAndForkDetectionData`: a +/// header without mining fields has 16, and reads back with its hash. +/// Fork-detection data lives in the coinbase, so there is none to give it. +#[test] +fn a_header_without_mining_fields_has_sixteen_fields_and_reads_back() { + let heights = heights(|h| h.orchid = 0); + let header = built(20); + let encoded = full(&header); + assert_eq!(count(&encoded), 16); + round_trip(&header, &encoded, &heights, HeaderForm::Full); +} + +// ── RSKIPUMM: the UMM root + +/// rskj `decodeBlockRskip110OffRskipUMMOnAndNoMergedMiningFieldsValidUMMRoot`, +/// `...AndNoMergedMiningFieldsEmptyUMMRoot`, `...AndNoMergedMiningFieldsNullUMMRoot`, +/// `...AndMergedMiningFieldsValidUMMRoot`, `...AndMergedMiningFieldsEmptyUmmRoot` +/// and `...AndMergedMiningFieldsNullUmmRoot`: from RSKIPUMM a header carries a +/// UMM root, twenty bytes or none, after its uncle count, and reads back with +/// it; one that carries no field for it isn't read. +#[test] +fn from_rskipumm_a_header_has_a_umm_root_or_isnt_read() { + let heights = heights(|h| { + h.orchid = 0; + h.rskip_umm = Some(0); + }); + for mining in [false, true] { + for (umm_root, fields) in [ + (Some(twenty_bytes("ummRoot")), 17), + (Some(Bytes::new()), 17), + (None, 16), + ] { + let mut header = built(500); + header.umm_root = umm_root.clone(); + if mining { + header = mined(header, 80); + } + let encoded = full(&header); + let fields = fields + if mining { 3 } else { 0 }; + assert_eq!(count(&encoded), fields); + match umm_root { + Some(root) => { + let read = round_trip(&header, &encoded, &heights, HeaderForm::Full); + assert_eq!(read.umm_root, Some(root)); + } + None => assert!( + read(&encoded, &heights, HeaderForm::Full).is_err(), + "{fields} fields, no UMM root" + ), + } + } + } +} + +// ── RSKIP351: the version + +/// rskj `headerIsVersion0Before351Activation` and +/// `headerIsVersion1After351AndBefore535Activation`: a header built below +/// RSKIP351 is version 0; from it, without RSKIP535, version 1. +#[test] +fn a_header_is_version_1_from_rskip351_and_before_rskip535() { + let heights = heights(|h| h.rskip351 = Some(20)); + assert_eq!(heights.header_version(19), 0); + assert_eq!(heights.header_version(20), 1); +} + +/// RSKIP144 and RSKIP351 from block 20, as rskj's tests below have them. +fn rskip351_at_20(also: impl FnOnce(&mut ActivationHeights)) -> ActivationHeights { + heights(|h| { + h.rskip144 = Some(20); + h.rskip351 = Some(20); + also(h); + }) +} + +/// rskj `decodeCompressedBefore351`, `decodeCompressedOfExtendedBefore351` +/// and `decodeFullBefore351And144`: below RSKIP351 a header is version 0 and +/// has one encoding, its bloom in slot 6, whichever form it is read as. +#[test] +fn below_rskip351_a_header_has_one_encoding_with_its_bloom() { + let heights = rskip351_at_20(|_| {}); + let mut header = built(19); + header.logs_bloom = bloom(); + assert_eq!(header.encode_compressed(), full(&header)); + for form in [HeaderForm::Compressed, HeaderForm::Full] { + let read = round_trip(&header, &full(&header), &heights, form); + assert_eq!(read.version_fields, None); + assert_eq!(read.extension_data, None); + assert_eq!(read.logs_bloom, bloom()); + } +} + +/// rskj `decodeCompressedWithNoEdgesAndMergedMiningFields`: a version-1 +/// header with a UMM root and mining fields, sent compressed, reads back with +/// its hash, its UMM root, and slot 6's commitment to its extension, which +/// names version 1; its bloom and edges aren't in that form. +#[test] +fn a_compressed_version_1_header_with_mining_fields_reads_back() { + let heights = rskip351_at_20(|h| { + h.orchid = 0; + h.rskip_umm = Some(0); + }); + let mut header = mined(built(20), 80); + header.umm_root = Some(Bytes::new()); + header.logs_bloom = bloom(); + let header = versioned(header, 1, None, None); + let encoded = header.encode_compressed(); + assert_eq!(count(&encoded), 20); + let read = round_trip(&header, &encoded, &heights, HeaderForm::Compressed); + assert_eq!(commitment(&read), commitment(&header)); + assert_eq!(commitment(&read).version, 1); + assert_eq!(read.version_fields, None); + assert_eq!(read.logs_bloom, Bloom::ZERO); + assert_eq!(read.umm_root, Some(Bytes::new())); +} + +/// rskj `decodeCompressedAfter351WithEdges`: a version-1 header with edges, +/// sent compressed, reads back with slot 6's commitment to its extension, +/// version 1, and neither bloom nor edges. +#[test] +fn a_compressed_version_1_header_commits_to_its_edges() { + let heights = rskip351_at_20(|_| {}); + let mut header = built(20); + header.logs_bloom = bloom(); + let header = versioned(header, 1, Some(&[1, 2, 3, 4]), None); + let encoded = header.encode_compressed(); + assert_eq!(count(&encoded), 16); + let read = round_trip(&header, &encoded, &heights, HeaderForm::Compressed); + let extension = header.extension().unwrap(); + assert_eq!( + read.extension_commitment().unwrap().unwrap(), + ExtensionCommitment::from_extension_data(&extension.create_extension_data()).unwrap() + ); + assert_eq!(read.version_fields, None); + assert_eq!(read.logs_bloom, Bloom::ZERO); +} + +/// rskj `decodeFullAfter351WithEdges`: in full, a version-1 header carries +/// its bloom, its version and its edges, and reads back with them. +#[test] +fn a_full_version_1_header_carries_its_bloom_and_edges() { + let heights = rskip351_at_20(|_| {}); + let mut header = built(20); + header.logs_bloom = bloom(); + let header = versioned(header, 1, Some(&[1, 2, 3, 4]), None); + let encoded = full(&header); + assert_eq!(count(&encoded), 18); + let read = round_trip(&header, &encoded, &heights, HeaderForm::Full); + assert_eq!(read.logs_bloom, bloom()); + assert_eq!(read.version_fields, header.version_fields); + assert_eq!(read.extension(), header.extension()); + let extension = header.extension().unwrap(); + assert_eq!( + commitment(&read), + ExtensionCommitment::from_extension_data(&extension.create_extension_data()).unwrap() + ); +} + +// ── RSKIP144: the edges + +/// rskj `decodeBlockRskip144OnRskipUMMOnAndMergedMiningFields` (22 fields), +/// `decodeBlockHeaderRSKIP1444ON_UMMOn_MergedMiningFieldsOFF_RSKIP5350FF` +/// (19), `decodeBlockRskip144OnRskipUMMOffAndMergedMiningFields` (21) and +/// `decodeBlockRskip144OnRskipUMMOffAndNoMergedMiningFields` (18): with +/// RSKIP144 and RSKIP351 a full header carries its version and its edges, +/// after the UMM root where there is one, and reads back with them. +#[test] +fn with_rskip144_a_full_header_carries_its_edges() { + for (umm, mining, fields) in [ + (true, true, 22), + (true, false, 19), + (false, true, 21), + (false, false, 18), + ] { + let heights = heights(|h| { + h.rskip144 = Some(0); + h.rskip351 = Some(0); + if umm { + h.rskip_umm = Some(0); + } + }); + let mut header = built(500); + header.logs_bloom = bloom(); + if umm { + header.umm_root = Some(Bytes::new()); + } + if mining { + header = mined(header, 80); + } + let header = versioned(header, 1, Some(&EDGES), None); + let encoded = full(&header); + assert_eq!(count(&encoded), fields, "UMM {umm}, mining {mining}"); + let read = round_trip(&header, &encoded, &heights, HeaderForm::Full); + assert_eq!(read.version_fields.unwrap().edges, Some(EDGES.to_vec())); + } +} + +// ── RSKIP535: the base event + +/// rskj `decodeBlockRskip535WithoutRskip351`: RSKIP535 needs RSKIP351, so +/// without it a header is version 0, with no base event, in 16 fields. +#[test] +fn without_rskip351_rskip535_adds_no_base_event() { + let heights = heights(|h| { + h.orchid = 0; + h.rskip535 = Some(0); + }); + assert_eq!(heights.header_version(500), 0); + let header = built(500); + let encoded = full(&header); + assert_eq!(count(&encoded), 16); + let read = round_trip(&header, &encoded, &heights, HeaderForm::Full); + assert_eq!(read.version_fields, None); +} + +/// rskj `decodeBlockRskip535WithoutRskip144` (18 fields), +/// `decodeBlockRskip535WithRskip144AndNoMergedMiningFields` (19), +/// `decodeBlockRskip535WithUMMAndNoMergedMiningFields` (19), +/// `decodeBlockRskip535WithRskip144AndUMMAndNoMergedMiningFields` (20) and +/// `decodeBlockRskip535WithEmptyBaseEvent` (18): with RSKIP351 and RSKIP535 a +/// full header is version 2 and carries its base event, empty or not, after +/// its edges where RSKIP144 gives it some, and reads back with them. +#[test] +fn with_rskip535_a_full_header_carries_its_base_event() { + for (umm, edges, event, fields) in [ + (false, false, base_event(), 18), + (false, true, base_event(), 19), + (true, false, base_event(), 19), + (true, true, base_event(), 20), + (false, false, Vec::new(), 18), + ] { + let heights = heights(|h| { + h.orchid = 0; + h.rskip351 = Some(0); + h.rskip535 = Some(0); + if edges { + h.rskip144 = Some(0); + } + if umm { + h.rskip_umm = Some(0); + } + }); + assert_eq!(heights.header_version(500), 2); + let mut header = built(500); + if umm { + header.umm_root = Some(twenty_bytes("ummRoot")); + } + let header = versioned(header, 2, edges.then_some(&EDGES[..]), Some(&event)); + let encoded = full(&header); + assert_eq!(count(&encoded), fields, "UMM {umm}, edges {edges}"); + let read = round_trip(&header, &encoded, &heights, HeaderForm::Full); + let version = read.version_fields.unwrap(); + assert_eq!(version.version, 2); + assert_eq!(version.base_event, Some(Bytes::from(event))); + assert_eq!(version.edges, edges.then(|| EDGES.to_vec())); + assert_eq!(read.umm_root, header.umm_root); + } +} + +/// rskj `decodeCompressedBlockRskip535WithRskip144`, +/// `decodeCompressedBlockRskip535WithoutRskip144` and +/// `decodeCompressedBlockRskip535WithUMMWithoutRskip144`: a version-2 header +/// sent compressed reads back with its hash and slot 6's commitment, which +/// names version 2, and neither edges nor base event, which that form +/// leaves to the extension. +#[test] +fn a_compressed_version_2_header_commits_to_its_base_event() { + for (umm, edges) in [(false, true), (false, false), (true, false)] { + let heights = heights(|h| { + h.orchid = 0; + h.rskip351 = Some(0); + h.rskip535 = Some(0); + if edges { + h.rskip144 = Some(0); + } + if umm { + h.rskip_umm = Some(0); + } + }); + let mut header = built(500); + header.logs_bloom = bloom(); + if umm { + header.umm_root = Some(twenty_bytes("ummRoot")); + } + let event = base_event(); + let header = versioned(header, 2, edges.then_some(&EDGES[..]), Some(&event)); + let encoded = header.encode_compressed(); + let read = round_trip(&header, &encoded, &heights, HeaderForm::Compressed); + assert_eq!(commitment(&read), commitment(&header)); + assert_eq!(commitment(&read).version, 2); + assert_eq!(read.version_fields, None, "UMM {umm}, edges {edges}"); + assert_eq!(read.umm_root, header.umm_root); + } +} + +/// rskj `decodeFullBlockRskip535WithRskip144`: in full, a version-2 header +/// with edges reads back with its bloom, edges and base event. +#[test] +fn a_full_version_2_header_carries_its_bloom_edges_and_base_event() { + let heights = heights(|h| { + h.orchid = 0; + h.rskip144 = Some(0); + h.rskip351 = Some(0); + h.rskip535 = Some(0); + }); + let mut header = built(500); + header.logs_bloom = bloom(); + let event = base_event(); + let header = versioned(header, 2, Some(&EDGES), Some(&event)); + let read = round_trip(&header, &full(&header), &heights, HeaderForm::Full); + assert_eq!(read.logs_bloom, bloom()); + assert_eq!(read.version_fields, header.version_fields); +} + +/// rskj `emptyBaseEventPreservedAcrossExtensionRoundTripWithMining` and +/// `emptyBaseEventPreservedAcrossExtensionRoundTripNoMining`: an empty base +/// event survives the extension's own encoding, keeps its field in the +/// header's (23 fields with mining), and reads back empty, the hash the same. +#[test] +fn an_empty_base_event_keeps_its_field_and_reads_back_empty() { + for mining in [true, false] { + let heights = heights(|h| { + h.orchid = 0; + h.rskip144 = Some(0); + h.rskip351 = Some(0); + h.rskip535 = Some(0); + if mining { + h.rskip_umm = Some(0); + } + }); + let mut header = built(500); + if mining { + header = mined(header, 80); + header.umm_root = Some(twenty_bytes("ummRoot")); + } + let header = versioned(header, 2, Some(&EDGES), Some(&[])); + let extension = header.extension().unwrap(); + let from_wire = BlockHeaderExtension::from_encoded(&extension.to_encoded()).unwrap(); + assert_eq!(from_wire.base_event(), Some(&[][..])); + assert_eq!(from_wire, extension); + let encoded = full(&header); + if mining { + assert_eq!(count(&encoded), 23); + } + let read = round_trip(&header, &encoded, &heights, HeaderForm::Full); + assert_eq!(read.version_fields.unwrap().base_event, Some(Bytes::new())); + } +} + +// ── testnet's one-field-short header + +/// RSKIP351, RSKIP535, RSKIP144 and RSKIPUMM from genesis, merged mining +/// with them, on the chain whose other heights `chain` gives. +fn base_event_rules(chain: ActivationHeights, umm: bool) -> ActivationHeights { + ActivationHeights { + orchid: 0, + rskip144: Some(0), + rskip351: Some(0), + rskip535: Some(0), + rskip_umm: umm.then_some(0), + ..chain + } +} + +/// A version-2 header at `number` written without its base event's field, as +/// a version-2 extension with none writes it: 22 fields with mining. +fn one_field_short(number: u64, mining: bool) -> Header { + let mut header = built(number); + if mining { + header = mined(header, 80); + header.umm_root = Some(twenty_bytes("ummRoot")); + } + versioned(header, 2, Some(&EDGES), None) +} + +/// rskj `headerOneElementShortDecodableOnTestnetWithinBoundWithMining` and +/// `headerOneElementShortDecodableOnTestnetWithinBoundNoMining`: on testnet, +/// below #7,800,000, a full header one field short of its layout is read, +/// with an empty base event, whether the three mining fields follow or not. +#[test] +fn testnet_reads_a_header_one_field_short_with_an_empty_base_event() { + for mining in [true, false] { + let heights = base_event_rules(ActivationHeights::testnet(), mining); + let header = one_field_short(500, mining); + let encoded = full(&header); + if mining { + assert_eq!(count(&encoded), 22); + } + let read = read(&encoded, &heights, HeaderForm::Full).unwrap(); + let version = read.version_fields.unwrap(); + assert_eq!(version.base_event, Some(Bytes::new())); + assert_eq!(version.edges, Some(EDGES.to_vec())); + } +} + +/// rskj `headerOneElementShortRejectedOnMainnetFactory`, +/// `headerOneElementShortRejectedAboveUpperBound` and +/// `headerOneElementShortRejectedOnNonTestnetChainId`: that tolerance is +/// testnet's alone, and ends at #7,800,000. +#[test] +fn no_other_chain_and_no_later_block_reads_a_header_one_field_short() { + for (name, chain, number, is_read) in [ + ("mainnet", ActivationHeights::mainnet(), 500, false), + ("regtest", ActivationHeights::regtest(), 500, false), + ("testnet", ActivationHeights::testnet(), 7_800_000, false), + ("testnet", ActivationHeights::testnet(), 7_799_999, true), + ] { + let heights = base_event_rules(chain, true); + let encoded = full(&one_field_short(number, true)); + assert_eq!(count(&encoded), 22); + assert_eq!( + read(&encoded, &heights, HeaderForm::Full).is_ok(), + is_read, + "{name}, block {number}" + ); + } +} + +/// rskj `headerOneElementShortWithBaseEventPresentRejectedOnTestnet`: a +/// header that keeps its base event and drops a mining field is the same +/// length, so testnet reads its base event in the Bitcoin header's place, +/// which RSKIP98 holds to 80 bytes, and refuses it. +#[test] +fn a_header_short_of_a_mining_field_is_not_one_short_of_its_base_event() { + let heights = base_event_rules(ActivationHeights::testnet(), true); + let mut header = mined(built(500), 80); + header.umm_root = Some(twenty_bytes("ummRoot")); + let event = base_event(); + let header = versioned(header, 2, Some(&EDGES), Some(&event)); + let encoded = full(&header); + assert_eq!(count(&encoded), 23); + round_trip(&header, &encoded, &heights, HeaderForm::Full); + + let (list, _) = rskj_rlp::first_element(&encoded).unwrap(); + let fields = list.elements().unwrap(); + let mut payload = Vec::new(); + for field in &fields[..fields.len() - 1] { + field.raw_data().encode(&mut payload); + } + let mut truncated = Vec::new(); + alloy_rlp::Header { + list: true, + payload_length: payload.len(), + } + .encode(&mut truncated); + truncated.extend_from_slice(&payload); + assert_eq!(count(&truncated), 22); + assert!(read(&truncated, &heights, HeaderForm::Full).is_err()); +} + +/// rskj `compressedHeaderRejectedWhenSizeDoesNotMatch`: the tolerance is the +/// full encoding's; read as compressed, the same 22 fields are none of the +/// counts that form has, and the header isn't read. +#[test] +fn a_compressed_header_is_not_read_one_field_short() { + let heights = base_event_rules(ActivationHeights::testnet(), true); + let encoded = full(&one_field_short(500, true)); + assert!(read(&encoded, &heights, HeaderForm::Full).is_ok()); + assert!(read(&encoded, &heights, HeaderForm::Compressed).is_err()); +} + +// ── RSKIP98: the Bitcoin header's size + +/// rskj `decodeHeaderValidatesBtcHeaderSize`: from RSKIP98 a header is read +/// with a Bitcoin header of 80 bytes, and not of 79, 81 or 160. +#[test] +fn from_rskip98_the_bitcoin_header_is_read_at_80_bytes_only() { + let heights = heights(|h| h.orchid = 0); + for size in [80, 79, 81, 160] { + let header = mined(built(20), size); + let encoded = full(&header); + if size == 80 { + let read = round_trip(&header, &encoded, &heights, HeaderForm::Full); + assert_eq!(read.bitcoin_merged_mining_header.unwrap().len(), 80); + } else { + assert!( + read(&encoded, &heights, HeaderForm::Full).is_err(), + "{size} bytes" + ); + } + } +} + +/// rskj `decodeHeaderRejectsOversizedBtcHeaderWithVariousActiveRules`: an +/// 81-byte Bitcoin header isn't read after a UMM root, nor after a version +/// and edges. +#[test] +fn an_oversized_bitcoin_header_is_refused_after_the_umm_root_or_the_edges() { + let umm = heights(|h| { + h.orchid = 0; + h.rskip_umm = Some(0); + }); + let mut header = mined(built(500), 81); + header.umm_root = Some(Bytes::new()); + assert!(read(&full(&header), &umm, HeaderForm::Full).is_err()); + header.bitcoin_merged_mining_header = Some(Bytes::from(vec![0; 80])); + assert!(read(&full(&header), &umm, HeaderForm::Full).is_ok()); + + let edges = heights(|h| { + h.orchid = 0; + h.rskip144 = Some(0); + h.rskip351 = Some(0); + }); + let header = versioned(mined(built(500), 81), 1, Some(&EDGES), None); + assert!(read(&full(&header), &edges, HeaderForm::Full).is_err()); + let header = versioned(mined(built(500), 80), 1, Some(&EDGES), None); + assert!(read(&full(&header), &edges, HeaderForm::Full).is_ok()); +} diff --git a/crates/core/src/rskj_tests/blocks/block_header_builder_test.rs b/crates/core/src/rskj_tests/blocks/block_header_builder_test.rs new file mode 100644 index 0000000..5e09219 --- /dev/null +++ b/crates/core/src/rskj_tests/blocks/block_header_builder_test.rs @@ -0,0 +1,106 @@ +//! rskj `org.ethereum.core.BlockHeaderBuilderTest`: the fields a header is +//! built with, by the rules active at its height. +//! +//! rustock's miner fills a header's fields as rskj's `BlockHeaderBuilder` +//! does, which the miner's own tests hold; this holds what those fields come +//! to in the encoding blocks carry and in the hash. + +use super::block_factory_test::{built, count, full, heights, mined}; +use crate::rskj_rlp; +use crate::types::header::HeaderVersionFields; +use alloy_primitives::{keccak256, Bytes}; + +/// `encoded`, a header, without its last `drop` fields. +fn without_last(encoded: &[u8], drop: usize) -> Vec { + let (list, _) = rskj_rlp::first_element(encoded).unwrap(); + let rskj_rlp::Element::List { encoding, offset } = list else { + panic!("a header is a list"); + }; + let payload = &encoding[offset..]; + let mut ends = Vec::new(); + let mut position = 0; + while position < payload.len() { + let (_, next) = rskj_rlp::first_element(&payload[position..]).unwrap(); + position += next; + ends.push(position); + } + let kept = &payload[..ends[ends.len() - 1 - drop]]; + let mut out = Vec::new(); + alloy_rlp::Header { + list: true, + payload_length: kept.len(), + } + .encode(&mut out); + out.extend_from_slice(kept); + out +} + +/// rskj `testHeaderCreationWithParameters`: a header built under RSKIPUMM +/// with the three merged-mining fields has 20 fields; RSKIP351 adds its +/// version and edges (22), and RSKIP535 its base event (23), which without +/// RSKIP351 adds none. Built with RSKIP92's encoding, rskj's test counts +/// those fields but the merkle proof and the coinbase (18, 20, 21): rskj +/// writes that form only to its RPC, and hashes the header compressed, a +/// versioned header's own fields in slot 6's commitment. From RSKIP92 the +/// hash leaves the proof and the coinbase out, whatever the version. +#[test] +fn a_built_header_carries_the_fields_its_rules_give_it() { + // rskj's cases: consensus-compliant, RSKIP92's encoding, RSKIP351, + // RSKIP535, and the fields it counts. A consensus-compliant header uses + // RSKIP92's encoding whatever it is asked, and RSKIP144 is on with + // RSKIP351 and off without it. + for (compliant, rskip92_encoding, rskip351, rskip535, rskj_count) in [ + (false, false, false, false, 20), + (false, false, false, true, 20), + (false, false, true, false, 22), + (false, false, true, true, 23), + (false, true, false, false, 18), + (false, true, false, true, 18), + (false, true, true, false, 20), + (false, true, true, true, 21), + (true, false, false, false, 18), + (true, false, false, true, 18), + (true, false, true, false, 20), + (true, false, true, true, 21), + ] { + let rskip92 = compliant || rskip92_encoding; + let heights = heights(|h| { + h.rskip_umm = Some(0); + if rskip92 { + h.orchid = 0; + } + if rskip351 { + h.rskip144 = Some(0); + h.rskip351 = Some(0); + } + if rskip535 { + h.rskip535 = Some(0); + } + }); + // As rskj's builder fills them: an empty UMM root, empty edges and + // an empty base event. + let mut header = mined(built(1), 80); + header.umm_root = Some(Bytes::new()); + let version = heights.header_version(1); + if version > 0 { + header.version_fields = Some(HeaderVersionFields { + version, + edges: Some(Vec::new()), + base_event: (version == 2).then(Bytes::new), + }); + } + let header = header.with_activations(&heights); + let case = format!("{compliant} {rskip92_encoding} {rskip351} {rskip535}"); + let fields = count(&full(&header)); + let compressed = header.encode_compressed(); + if rskip92 { + assert_eq!(fields - 2, rskj_count, "{case}"); + let hashed = without_last(&compressed, 2); + assert_eq!(count(&hashed), 18, "{case}"); + assert_eq!(header.hash(), keccak256(&hashed), "{case}"); + } else { + assert_eq!(fields, rskj_count, "{case}"); + assert_eq!(header.hash(), keccak256(&compressed), "{case}"); + } + } +} diff --git a/crates/core/src/rskj_tests/blocks/block_header_test.rs b/crates/core/src/rskj_tests/blocks/block_header_test.rs new file mode 100644 index 0000000..083ae6c --- /dev/null +++ b/crates/core/src/rskj_tests/blocks/block_header_test.rs @@ -0,0 +1,31 @@ +//! rskj `co.rsk.core.BlockHeaderTest`, which builds its headers as RSKIP92 +//! has them: what of it no block has reached. + +use super::block_factory_test::{built, mined, twenty_bytes}; +use alloy_primitives::{keccak256, Bytes}; + +/// rskj `getHashForMergedMiningWhenUmmRoot` and +/// `getHashForMergedMiningWhenEmptyUmmRoot`: a header with a UMM root of 20 +/// bytes merge-mines the hash of the first 20 bytes of its own merged-mining +/// hash and the root; one with an empty root, its own, the hash of the header +/// compressed and without its merged-mining fields. rskj's test then writes +/// fork-detection data over the last 12 bytes, which rustock's miner does +/// apart from the header, and its proof-of-work rule reads past. +#[test] +fn a_umm_root_joins_the_merged_mining_hash() { + for root in [twenty_bytes("ummRoot"), Bytes::new()] { + let mut bare = built(500); + bare.umm_root = Some(root.clone()); + let own = keccak256(bare.encode_compressed()); + let expected = match root.is_empty() { + true => own, + false => keccak256([&own[..20], &root[..]].concat()), + }; + assert_eq!( + mined(bare, 80).hash_for_merged_mining(), + expected, + "UMM root of {} bytes", + root.len() + ); + } +} diff --git a/crates/core/src/rskj_tests/blocks/block_parent_number_rule_test.rs b/crates/core/src/rskj_tests/blocks/block_parent_number_rule_test.rs new file mode 100644 index 0000000..4ce3c8d --- /dev/null +++ b/crates/core/src/rskj_tests/blocks/block_parent_number_rule_test.rs @@ -0,0 +1,20 @@ +//! rskj `co.rsk.validators.BlockParentNumberRuleTest`: a block's number is +//! its parent's plus one (`BlockParentNumberRule`). + +use super::support::header; +use crate::validation::{BlockNumberRule, ParentHeaderValidator, ValidationError}; + +/// rskj `isValid`: against a parent at 451, 452 is valid, and the parent's +/// own number, the one below it and one far above are not. +#[test] +fn a_block_is_numbered_one_past_its_parent() { + let parent = header(451, 1_000); + let child = |number: u64| BlockNumberRule.validate_with_parent(&header(number, 1_010), &parent); + assert_eq!(child(452), Ok(())); + for got in [451, 450, 999] { + assert_eq!( + child(got), + Err(ValidationError::InvalidBlockNumber { expected: 452, got }) + ); + } +} diff --git a/crates/core/src/rskj_tests/blocks/block_uncles_validation_rule_test.rs b/crates/core/src/rskj_tests/blocks/block_uncles_validation_rule_test.rs new file mode 100644 index 0000000..bd962bd --- /dev/null +++ b/crates/core/src/rskj_tests/blocks/block_uncles_validation_rule_test.rs @@ -0,0 +1,243 @@ +//! rskj `co.rsk.validators.BlockUnclesValidationRuleTest`: an uncle is a +//! block below the one that names it, never its sibling or descendant +//! (`BlockUnclesValidationRule.validateParentNumber`); and the rule's other +//! refusals, under rskj's limits of ten uncles and seven generations. + +use super::support::header; +use crate::types::block::Block; +use crate::types::header::Header; +use crate::validation::uncles::{ + AncestorSource, UnclesValidationRule, UNCLE_GENERATION_LIMIT, UNCLE_LIST_LIMIT, +}; +use crate::validation::{HeaderValidator, ParentHeaderValidator, ValidationError}; +use alloy_primitives::{Address, B256}; +use std::collections::HashMap; + +/// The chain as a block store holds it: headers by hash, and the uncles +/// each block named. +#[derive(Default)] +struct Store(HashMap, HashMap>); + +impl Store { + fn put(&mut self, header: &Header) { + self.0.insert(header.hash(), header.clone()); + } +} + +impl AncestorSource for Store { + fn header(&self, hash: B256) -> Option
{ + self.0.get(&hash).cloned() + } + + fn uncles_of(&self, hash: B256) -> Vec
{ + self.1.get(&hash).cloned().unwrap_or_default() + } +} + +/// A child of `parent`, told apart from its siblings by its `miner`. +fn child_of(parent: &Header, miner: u8) -> Header { + let mut child = header(parent.number + 1, parent.timestamp + 10); + child.parent_hash = parent.hash(); + child.beneficiary = Address::repeat_byte(miner); + child +} + +/// rskj's chain: genesis and block 1 stored, and block 2, on block 1, +/// naming `uncle`. Uncle and generation limits of ten and no rule on the +/// uncle's own header, as rskj's test builds its rule. +fn judge(store: &Store, block_1: &Header, uncle: Header) -> Result<(), ValidationError> { + let block = Block { + header: child_of(block_1, 0xb0), + transactions: Vec::new(), + ommers: vec![uncle], + }; + let header_rules: [Box; 0] = []; + let parent_rules: [Box; 0] = []; + UnclesValidationRule { + store, + uncle_list_limit: 10, + uncle_generation_limit: 10, + header_rules: &header_rules, + parent_rules: &parent_rules, + fork_detection: None, + } + .validate(&block) +} + +fn genesis_and_block_1() -> (Store, Header, Header) { + let genesis = header(0, 1_000); + let block_1 = child_of(&genesis, 0xa0); + let mut store = Store::default(); + store.put(&genesis); + store.put(&block_1); + (store, genesis, block_1) +} + +/// rskj `rejectBlockWithSiblingUncle`: block 2 can't name another child of +/// block 1, its sibling. A child of genesis, block 1's sibling, it can. +#[test] +fn a_sibling_of_the_block_is_no_uncle() { + let (store, genesis, block_1) = genesis_and_block_1(); + let sibling = child_of(&block_1, 0xc0); + assert_eq!( + judge(&store, &block_1, sibling.clone()), + Err(ValidationError::UncleIsSiblingOrDescendant { + hash: sibling.hash() + }) + ); + + let parents_sibling = child_of(&genesis, 0xc1); + assert_eq!(judge(&store, &block_1, parents_sibling), Ok(())); +} + +/// rskj `rejectBlockWithUncleHavingHigherNumber`: block 2 can't name block 3, +/// a grandchild of block 1. +#[test] +fn a_block_above_the_one_naming_it_is_no_uncle() { + let (store, _genesis, block_1) = genesis_and_block_1(); + let sibling = child_of(&block_1, 0xc0); + let higher = child_of(&sibling, 0xc2); + assert_eq!(higher.number, 3); + assert_eq!( + judge(&store, &block_1, higher.clone()), + Err(ValidationError::UncleIsSiblingOrDescendant { + hash: higher.hash() + }) + ); +} + +/// Genesis and blocks 1 to `top`, each the child of the one before. +fn trunk(top: u64) -> (Store, Vec
) { + let mut store = Store::default(); + let mut chain = vec![header(0, 1_000)]; + store.put(&chain[0]); + for _ in 1..=top { + let next = child_of(chain.last().unwrap(), 0xa0); + store.put(&next); + chain.push(next); + } + (store, chain) +} + +/// The block on `chain`'s last naming `uncles`, judged under rskj's limits. +fn names(store: &Store, chain: &[Header], uncles: Vec
) -> Result<(), ValidationError> { + let block = Block { + header: child_of(chain.last().unwrap(), 0xb0), + transactions: Vec::new(), + ommers: uncles, + }; + let header_rules: [Box; 0] = []; + let parent_rules: [Box; 0] = []; + UnclesValidationRule { + store, + uncle_list_limit: UNCLE_LIST_LIMIT, + uncle_generation_limit: UNCLE_GENERATION_LIMIT, + header_rules: &header_rules, + parent_rules: &parent_rules, + fork_detection: None, + } + .validate(&block) +} + +/// `validateParentNumber`: an uncle's parent must be no further than seven +/// blocks below the block naming it. Block 10 may name a child of block 3, +/// and not one of block 2. +#[test] +fn an_uncle_more_than_seven_generations_back_is_too_old() { + let (store, chain) = trunk(9); + assert_eq!( + names(&store, &chain, vec![child_of(&chain[3], 0xc0)]), + Ok(()) + ); + let old = child_of(&chain[2], 0xc0); + assert_eq!( + names(&store, &chain, vec![old.clone()]), + Err(ValidationError::UncleTooOld { + hash: old.hash(), + limit: 7 + }) + ); +} + +/// `validateUncleList`: ten uncles at most. +#[test] +fn more_than_ten_uncles_are_too_many() { + let (store, chain) = trunk(3); + let uncles = |count: u8| (0..count).map(|miner| child_of(&chain[2], miner)).collect(); + assert_eq!(names(&store, &chain, uncles(10)), Ok(())); + assert_eq!( + names(&store, &chain, uncles(11)), + Err(ValidationError::TooManyUncles { max: 10, got: 11 }) + ); +} + +/// `validateUncleList`: no uncle twice in one block. +#[test] +fn an_uncle_named_twice_is_refused() { + let (store, chain) = trunk(3); + let uncle = child_of(&chain[2], 0xc0); + assert_eq!( + names(&store, &chain, vec![uncle.clone(), uncle.clone()]), + Err(ValidationError::UncleRepeated { hash: uncle.hash() }) + ); +} + +/// `validateUnclesAncestors`: a block's own ancestor is no uncle of it. +#[test] +fn an_ancestor_is_no_uncle() { + let (store, chain) = trunk(3); + assert_eq!( + names(&store, &chain, vec![chain[2].clone()]), + Err(ValidationError::UncleIsAncestor { + hash: chain[2].hash() + }) + ); +} + +/// `validateIfUncleWasNeverUsed`: an uncle one of the last seven ancestors +/// named already is used, and naming it again is refused. +#[test] +fn an_uncle_an_ancestor_named_is_used() { + let (mut store, chain) = trunk(3); + let uncle = child_of(&chain[1], 0xc0); + store.1.insert(chain[3].hash(), vec![uncle.clone()]); + assert_eq!( + names(&store, &chain, vec![uncle.clone()]), + Err(ValidationError::UncleAlreadyUsed { hash: uncle.hash() }) + ); +} + +/// `validateUncleParent`: an uncle's parent is one of the block's ancestors. +/// A child of a block the store doesn't hold, or of one off the block's +/// chain, has none in common with it. +#[test] +fn an_uncle_whose_parent_is_no_ancestor_has_no_common_parent() { + let (mut store, chain) = trunk(3); + let stranger = child_of(&header(1, 2_000), 0xd0); + let off_chain = child_of(&chain[0], 0xe0); + store.put(&off_chain); + let nephew = child_of(&off_chain, 0xe1); + for uncle in [stranger, nephew] { + assert_eq!( + names(&store, &chain, vec![uncle.clone()]), + Err(ValidationError::UncleHasNoCommonParent { hash: uncle.hash() }) + ); + } +} + +/// rskj `FamilyUtils.getAncestors` and `getUsedUncles`: for block 10, the +/// ancestors from its parent down to block 3, ten less seven, and the +/// uncles those named; block 2 and its uncles are out of reach. +#[test] +fn the_family_reaches_seven_blocks_back() { + use crate::validation::uncles::{ancestors, used_uncles}; + let (mut store, chain) = trunk(9); + let named_by = |at: usize| child_of(&chain[at - 1], 0xc0 + at as u8); + store.1.insert(chain[3].hash(), vec![named_by(3)]); + store.1.insert(chain[2].hash(), vec![named_by(2)]); + let family = ancestors(&store, 10, chain[9].hash(), UNCLE_GENERATION_LIMIT); + assert!(family.contains(&chain[3].hash()) && !family.contains(&chain[2].hash())); + let used = used_uncles(&store, 10, chain[9].hash(), UNCLE_GENERATION_LIMIT); + assert!(used.contains(&named_by(3).hash())); + assert!(!used.contains(&named_by(2).hash())); +} diff --git a/crates/core/src/rskj_tests/blocks/fork_detection_data_rule_test.rs b/crates/core/src/rskj_tests/blocks/fork_detection_data_rule_test.rs new file mode 100644 index 0000000..71dbc86 --- /dev/null +++ b/crates/core/src/rskj_tests/blocks/fork_detection_data_rule_test.rs @@ -0,0 +1,126 @@ +//! rskj `co.rsk.validators.ForkDetectionDataRuleTest` (RSKIP110). +//! +//! From RSKIP110 (wasabi100) the twelve bytes after the tag and the first +//! twenty bytes of the merged-mining hash commit to the miner's view of the +//! chain: above block 449 a block is refused unless they are what the 449 +//! headers below it make (`ForkDetectionDataRule.isValid`). rskj reads no such +//! bytes below block 449 (`BlockFactory` builds those headers with +//! `includeForkDetectionData` false), and compares none at 449 itself. +//! The calculator, which rskj's test mocks, is rustock's own here. + +use super::support::header; +use crate::types::header::Header; +use crate::validation::fork_detection::{self, MainchainView, REQUIRED_BLOCKS}; +use crate::validation::ValidationError; +use alloy_primitives::{Bytes, B256}; + +/// A view of the chain that holds these headers, newest first. +struct Held(Vec
); + +impl MainchainView for Held { + fn headers_from(&self, _from: B256, count: u64) -> Vec
{ + self.0.iter().take(count as usize).cloned().collect() + } +} + +/// `count` headers below `number`, newest first, each merge-mined in a +/// Bitcoin header of its own, as the calculation reads them. +fn chain_below(number: u64, count: u64) -> Held { + Held( + (1..=count) + .map(|depth| { + let mut header = header(number - depth, 1_000); + let btc = bitcoin::block::Header { + version: bitcoin::block::Version::from_consensus(0x2000_0000), + prev_blockhash: bitcoin::hashes::Hash::all_zeros(), + merkle_root: bitcoin::hashes::Hash::all_zeros(), + time: depth as u32, + bits: bitcoin::CompactTarget::from_consensus(0x207f_ffff), + nonce: 0, + }; + header.bitcoin_merged_mining_header = + Some(Bytes::from(bitcoin::consensus::serialize(&btc))); + header + }) + .collect(), + ) +} + +/// What rskj's test hands its rule as fork-detection data. +const DATA: [u8; 12] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; + +/// rskj `validForBlocksBeforeRskip110` and +/// `validForBlocksBeforeRskip110UsingMethodThatReceivesBlockAsParameter`: +/// before RSKIP110 the rule asks nothing of block 4,242, whatever its +/// coinbase carries and whatever the chain below it. From the activation +/// on, it does. +#[test] +fn before_rskip110_no_fork_detection_data_is_asked_for() { + let block = header(4_242, 1_000); + let none_below = Held(Vec::new()); + for actual in [None, Some(DATA)] { + assert_eq!( + fork_detection::validate(&block, actual, &none_below, 4_243), + Ok(()) + ); + } + assert_eq!( + fork_detection::validate(&block, Some(DATA), &none_below, 4_242), + Err(ValidationError::ForkDetectionDataUnreadable) + ); +} + +/// rskj `validForRskip110ActiveButNoForkDetectionDataBecauseNoEnoughBlocksToCalculateIt`: +/// block 42, with RSKIP110 active and no chain below it to calculate from, +/// carries no fork-detection data and is valid. +#[test] +fn below_block_449_a_block_carries_no_fork_detection_data() { + let block = header(42, 1_000); + assert_eq!( + fork_detection::validate(&block, None, &Held(Vec::new()), 0), + Ok(()) + ); +} + +/// rskj `invalidForRskip110ActiveAndForkDetectionDataButMissingBlocksForCalculation`: +/// above block 449, a chain that can't give the 449 headers the data is +/// calculated from refuses the block; it isn't let through unchecked. +#[test] +fn a_chain_too_short_to_calculate_from_refuses_the_block() { + let block = header(4_242, 1_000); + for held in [0, REQUIRED_BLOCKS - 1] { + assert_eq!( + fork_detection::validate(&block, Some(DATA), &chain_below(4_242, held), 0), + Err(ValidationError::ForkDetectionDataUnreadable), + "{held} headers below it" + ); + } +} + +/// rskj `validForRskip110ActiveAndForkDetectionData`: data that matches +/// what the 449 headers below the block make is valid. +#[test] +fn fork_detection_data_matching_the_chain_is_valid() { + let block = header(4_242, 1_000); + let below = chain_below(4_242, REQUIRED_BLOCKS); + let expected = fork_detection::calculate(&below.0).unwrap(); + assert_eq!( + fork_detection::validate(&block, Some(expected), &below, 0), + Ok(()) + ); +} + +/// rskj `invalidForRskip110ActiveAndForkDetectionDataBecauseDataDoesNotMatch`: +/// one byte other than the chain makes, and the block is refused. +#[test] +fn fork_detection_data_not_matching_the_chain_is_refused() { + let block = header(4_242, 1_000); + let below = chain_below(4_242, REQUIRED_BLOCKS); + let expected = fork_detection::calculate(&below.0).unwrap(); + let mut got = expected; + got[6] = got[6].wrapping_add(35); + assert_eq!( + fork_detection::validate(&block, Some(got), &below, 0), + Err(ValidationError::ForkDetectionDataMismatch { expected, got }) + ); +} diff --git a/crates/core/src/rskj_tests/blocks/local_basic_test.rs b/crates/core/src/rskj_tests/blocks/local_basic_test.rs new file mode 100644 index 0000000..8c8afb4 --- /dev/null +++ b/crates/core/src/rskj_tests/blocks/local_basic_test.rs @@ -0,0 +1,148 @@ +//! rskj `co.rsk.jsontestsuite.LocalBasicTest`: the difficulty cases of +//! rskj's `json/BasicTests/difficulty*.json`, each a parent, a child's +//! timestamp and number, and the difficulty rskj gives the child +//! (`DifficultyCalculator.calcDifficulty`). + +use super::support::header; +use crate::config::{ActivationHeights, ChainConfig}; +use crate::validation::DifficultyRule; +use alloy_primitives::U256; +use std::sync::Arc; + +/// The cases of `difficulty.json` and `difficultyBeforeRSKIP156.json`, +/// which share them: the parent's timestamp and difficulty, and the child's +/// timestamp and number. +#[rustfmt::skip] +const CASES: [(&str, u64, u64, u64, u64); 10] = [ + ("preExpDiffIncrease", 42, 10_000_000_000_000_000, 43, 42), + ("preExpDiffDecrease", 42, 10_000_000_000_000_000, 60, 42), + ("DifficultyTest1", 0xff6f9889aec4a8ab, 0xe5f6f17a6cfb97a9, 0xff6f9889aec4a8ab, 0x01), + ("DifficultyTest2", 0x4807463f1b55475e, 0xa9c932bb020082a4, 0x4807463f1b55475e, 0x61a9), + ("DifficultyTest3", 0x39636370b8492dde, 0x24a37089ce303762, 0x39636370b8492dde, 0xc351), + ("DifficultyTest4", 0x894395be53be575d, 0xd21e04041ea258f6, 0x894395be53be575d, 0x0124f9), + ("DifficultyTest5", 0x85d238e2ee51c214, 0x734df3bd1e36dc09, 0x85d238e2ee51c214, 0x0186a1), + ("DifficultyTest6", 0x4c9b2b0a61e65adb, 0xb0e35f896c8e0280, 0x4c9b2b0a61e65adb, 0x01e849), + ("DifficultyTest7", 0x701109b82158d260, 0xf9e6aa1a79f7bc1d, 0x701109b82158d260, 0x0249f1), + ("DifficultyTest8", 0x711f963840eae450, 0x12b73b0b5b0cf756, 0x711f963840eae450, 0x02ab99), +]; + +/// What `difficultyBeforeRSKIP156.json` expects of them: the parent's +/// difficulty and a fiftieth of it. +const BEFORE_RSKIP156: [u64; 10] = [ + 10_200_000_000_000_000, + 9_800_000_000_000_000, + 0xEA905CB531B8F1BB, + 0xAD2E808B8C483355, + 0x255F0744E1AC0F87, + 0xD651D0E57B68278A, + 0x759C4FA74CE60965, + 0xB46D0A68552035C0, + 0xFEE62862AF9B68D5, + 0x13170E252ECAAA5C, +]; + +/// What `difficulty.json` expects of them: the parent's difficulty and a +/// four-hundredth of it, from RSKIP156. +const FROM_RSKIP156: [u64; 10] = [ + 10_025_000_000_000_000, + 9_975_000_000_000_000, + 0xe68a1ee1c59342eb, + 0xaa35dc75134978ba, + 0x24bae361309fb266, + 0xd2a47da04a3b12c8, + 0x7397bf3a640cc1b4, + 0xb15494e549a048e8, + 0xfa8699e380ac31b4, + 0x12c3356e9584adb6, +]; + +/// `chain` with RSKIP97 on from genesis, as every case has it, and RSKIP156 +/// and RSKIP290 on from genesis or never. +fn chain(chain: ChainConfig, rskip156: bool, rskip290: bool) -> ChainConfig { + ChainConfig { + activation_heights: ActivationHeights { + rskip97: Some(0), + papyrus200: if rskip156 { 0 } else { u64::MAX }, + rskip290: rskip290.then_some(0), + ..chain.activation_heights.clone() + }, + ..chain + } +} + +/// The difficulty `chain` gives a child at `number` and `timestamp` of a +/// parent at `parent_timestamp` of `parent_difficulty`, without uncles. +fn calculated( + chain: &Arc, + parent_timestamp: u64, + parent_difficulty: u64, + timestamp: u64, + number: u64, +) -> U256 { + let mut parent = header(number - 1, parent_timestamp); + parent.difficulty = U256::from(parent_difficulty); + let rule = DifficultyRule { + config: chain.clone(), + }; + rule.difficulty_for_child(&parent, number, timestamp, 0) +} + +fn run(chain: ChainConfig, expected: &[u64; 10]) { + let chain = Arc::new(chain); + for ((name, parent_timestamp, parent_difficulty, timestamp, number), expected) in + CASES.iter().zip(expected) + { + assert_eq!( + calculated( + &chain, + *parent_timestamp, + *parent_difficulty, + *timestamp, + *number + ), + U256::from(*expected), + "{name}" + ); + } +} + +/// rskj `runDifficultyTestBeforeRSKIP156`: under mainnet's constants and +/// without RSKIP156, a child a second after its parent rises by a fiftieth +/// of the parent's difficulty, and one 18 seconds after falls by as much; +/// children at their parent's timestamp, which the cases put past 2^63, +/// rise. +#[test] +fn before_rskip156_the_difficulty_moves_by_a_fiftieth() { + run(chain(ChainConfig::mainnet(), false, true), &BEFORE_RSKIP156); +} + +/// rskj `runDifficultyTestBeforeRSKIP290`: under testnet's constants, with +/// RSKIP156 and without RSKIP290, it moves by a four-hundredth. +#[test] +fn from_rskip156_the_difficulty_moves_by_a_four_hundredth() { + run(chain(ChainConfig::testnet(), true, false), &FROM_RSKIP156); +} + +/// rskj `runDifficultyTest`, from the rest of the class: so it does under +/// mainnet's constants with every rule on. +#[test] +fn on_mainnet_from_rskip156_the_difficulty_moves_by_a_four_hundredth() { + run(chain(ChainConfig::mainnet(), true, true), &FROM_RSKIP156); +} + +/// rskj `runDifficultyTestAfterRSKIP290`: on testnet from RSKIP290 a child +/// whose difficulty would rise to 218,359,490 is raised to the floor of +/// 550,000,000 (`difficulty1.json`). +#[test] +fn from_rskip290_testnets_difficulty_is_floored_at_550_million() { + let testnet = Arc::new(chain(ChainConfig::testnet(), true, true)); + let at_floor = calculated( + &testnet, + 0xff6f9889aec4a8ab, + 0xcfb97a9, + 0xff6f9889aec4a8ab, + 1, + ); + assert_eq!(at_floor, U256::from(0x20C85580u64)); + assert_eq!(at_floor, U256::from(550_000_000u64)); +} diff --git a/crates/core/src/rskj_tests/blocks/mod.rs b/crates/core/src/rskj_tests/blocks/mod.rs new file mode 100644 index 0000000..38fe2d2 --- /dev/null +++ b/crates/core/src/rskj_tests/blocks/mod.rs @@ -0,0 +1,17 @@ +//! Headers, the rules a block is held to, and transactions. + +mod support; + +mod block_difficulty_rule_test; +mod block_difficulty_validation_rule_test; +mod block_executor_test; +mod block_factory_test; +mod block_header_builder_test; +mod block_header_test; +mod block_parent_number_rule_test; +mod block_uncles_validation_rule_test; +mod fork_detection_data_rule_test; +mod local_basic_test; +mod prev_min_gas_price_validator_test; +mod proof_of_work_rule_test; +mod rskip92_merkle_proof_validator_tests; diff --git a/crates/core/src/rskj_tests/blocks/prev_min_gas_price_validator_test.rs b/crates/core/src/rskj_tests/blocks/prev_min_gas_price_validator_test.rs new file mode 100644 index 0000000..c098fa0 --- /dev/null +++ b/crates/core/src/rskj_tests/blocks/prev_min_gas_price_validator_test.rs @@ -0,0 +1,25 @@ +//! rskj `co.rsk.validators.PrevMinGasPriceValidatorTest`: a block's minimum +//! gas price moves at most 1% from its parent's (`PrevMinGasPriceRule`). + +use super::support::header; +use crate::validation::{ParentHeaderValidator, PrevMinGasPriceRule, ValidationError}; +use alloy_primitives::U256; + +/// rskj `genesisBlock`: genesis has no parent to follow, so its minimum gas +/// price is never held to the band; the same price on block 1, against a +/// parent at 10, is out of it. +#[test] +fn genesis_is_held_to_no_parents_minimum_gas_price() { + let mut parent = header(0, 0); + parent.minimum_gas_price = U256::from(10); + let at = |number: u64| { + let mut block = header(number, 1); + block.minimum_gas_price = U256::from(1_050); + PrevMinGasPriceRule.validate_with_parent(&block, &parent) + }; + assert_eq!(at(0), Ok(())); + assert!(matches!( + at(1), + Err(ValidationError::MinGasPriceOutOfRange { .. }) + )); +} diff --git a/crates/core/src/rskj_tests/blocks/proof_of_work_rule_test.rs b/crates/core/src/rskj_tests/blocks/proof_of_work_rule_test.rs new file mode 100644 index 0000000..4b78844 --- /dev/null +++ b/crates/core/src/rskj_tests/blocks/proof_of_work_rule_test.rs @@ -0,0 +1,169 @@ +//! rskj `org.ethereum.validator.ProofOfWorkRuleTest`: the merged-mining +//! proof `ProofOfWorkRule` checks. +//! +//! From RSKIP98 (orchid) the field that carries the Bitcoin header holds a +//! Bitcoin header of 80 bytes and nothing else (`BtcHeaderSizeRule`, and +//! `BlockFactory.validateBitcoinMergedMiningHeader` when a block is read). +//! The coinbase carries the RSK tag and the block's merged-mining hash, the +//! tag within its first 64 bytes after the midstate, no later tag after it, +//! and no more than 128 bytes after the hash. + +use super::support::{ + bitcoin_header_with_root, coinbase_committing_to, compressed_coinbase, header, + merged_mining_rule, minable_header, mined_with_siblings, regtest_with_iris300_at, + tail_committing_to, +}; +use crate::config::{ActivationHeights, ChainConfig}; +use crate::types::header::Header; +use crate::validation::merged_mining::RSK_TAG; +use crate::validation::{HeaderValidator, ValidationError}; +use alloy_primitives::{Bytes, U256}; +use alloy_rlp::Encodable; +use bitcoin::hashes::Hash; + +/// A Bitcoin coinbase transaction whose output carries `tag_and_hash`, as a +/// merge-mining pool's does. +fn bitcoin_coinbase(tag_and_hash: &[u8]) -> bitcoin::Transaction { + let push: &bitcoin::script::PushBytes = tag_and_hash.try_into().unwrap(); + bitcoin::Transaction { + version: bitcoin::transaction::Version::ONE, + lock_time: bitcoin::absolute::LockTime::ZERO, + input: vec![bitcoin::TxIn { + previous_output: bitcoin::OutPoint::null(), + script_sig: bitcoin::ScriptBuf::from_bytes(vec![0x51]), + sequence: bitcoin::Sequence::MAX, + witness: bitcoin::Witness::default(), + }], + output: vec![bitcoin::TxOut { + value: bitcoin::Amount::from_sat(50), + script_pubkey: bitcoin::ScriptBuf::new_op_return(push), + }], + } +} + +/// `header` merge-mined in a one-transaction Bitcoin block, and that block +/// whole: its header, the count of its transactions, and its coinbase. +fn mined_in_a_whole_block(mut header: Header) -> (Header, Vec) { + let committed = [RSK_TAG, header.hash_for_merged_mining().as_slice()].concat(); + let coinbase = bitcoin_coinbase(&committed); + let coinbase_bytes = bitcoin::consensus::serialize(&coinbase); + // A one-transaction block's merkle root is its coinbase's id. + let btc = bitcoin_header_with_root(coinbase.compute_txid().to_byte_array(), header.difficulty); + let header_bytes = bitcoin::consensus::serialize(&btc); + header.bitcoin_merged_mining_header = Some(Bytes::from(header_bytes.clone())); + header.bitcoin_merged_mining_coinbase_transaction = + Some(Bytes::from(compressed_coinbase(0, &coinbase_bytes))); + header.bitcoin_merged_mining_merkle_proof = Some(Bytes::new()); + let whole = [header_bytes, vec![1], coinbase_bytes].concat(); + (header, whole) +} + +/// rskj `test_fullBtcBlockAsHeader_rejectedByRskj` (RSKIP98): a whole Bitcoin +/// block in place of its header proves the same work and the same merkle +/// root, and leaves the merged-mining hash alone, which leaves out the +/// merged-mining fields; from RSKIP98 the rule refuses it, and so does +/// reading the block. Mainnet's orchid, where RSKIP98 arrives. +#[test] +fn from_rskip98_a_whole_bitcoin_block_is_no_merged_mining_header() { + let rule = merged_mining_rule(ChainConfig::mainnet()); + let heights = ActivationHeights::mainnet(); + let mut at_orchid = header(heights.orchid, 1_000); + at_orchid.difficulty = U256::from(1); + let (mined, whole) = mined_in_a_whole_block(at_orchid); + assert!(whole.len() > 80); + assert_eq!(rule.validate(&mined), Ok(())); + + let mut with_block = mined.clone(); + with_block.bitcoin_merged_mining_header = Some(Bytes::from(whole)); + assert_eq!( + with_block.hash_for_merged_mining(), + mined.hash_for_merged_mining() + ); + assert_eq!( + rule.validate(&with_block), + Err(ValidationError::BitcoinHeaderDecodeError) + ); + + let read = |header: &Header| { + let mut bytes = Vec::new(); + header.encode(&mut bytes); + Header::decode_with_activations(&mut bytes.as_slice(), &heights) + }; + assert!(read(&mined).is_ok()); + assert!(read(&with_block).is_err()); +} + +/// rskj `test_noRSKTagInCoinbaseTransaction`: a coinbase that carries the +/// merged-mining hash but no tag commits to nothing. +#[test] +fn a_coinbase_without_the_rsk_tag_is_refused() { + let rule = merged_mining_rule(regtest_with_iris300_at(0)); + let header = minable_header(10); + let hash = header.hash_for_merged_mining(); + let untagged = compressed_coinbase(0, &[&[0u8; 60][..], hash.as_slice()].concat()); + let mined = mined_with_siblings(header.clone(), untagged, &[]); + assert_eq!( + rule.validate(&mined), + Err(ValidationError::BitcoinCoinbaseTagInvalid) + ); + + let tagged = coinbase_committing_to(&header, 30); + assert_eq!( + rule.validate(&mined_with_siblings(header, tagged, &[])), + Ok(()) + ); +} + +/// rskj `test_RSKTagInCoinbaseTransactionTooFar`: the tag must start within +/// the coinbase's first 64 bytes after its midstate, so that two midstates +/// of the same coinbase can't prove two blocks. 63 bytes before it pass; +/// 64 don't. +#[test] +fn the_rsk_tag_must_start_within_the_first_64_bytes_of_the_tail() { + let rule = merged_mining_rule(regtest_with_iris300_at(0)); + let header = minable_header(10); + let hash = header.hash_for_merged_mining(); + let judge = |before: usize| { + let coinbase = compressed_coinbase(0, &tail_committing_to(hash.as_slice(), before, 0)); + rule.validate(&mined_with_siblings(header.clone(), coinbase, &[])) + }; + assert_eq!(judge(63), Ok(())); + assert_eq!(judge(64), Err(ValidationError::BitcoinCoinbaseTagInvalid)); + assert_eq!(judge(960), Err(ValidationError::BitcoinCoinbaseTagInvalid)); +} + +/// rskj `bytesAfterMergedMiningHashAreLessThan128` and +/// `bytesAfterMergedMiningHashAreMoreThan128`: a coinbase may carry up to +/// 128 bytes after the merged-mining hash, and no more (a pool can't add +/// three outputs of 36 bytes after it). +#[test] +fn at_most_128_bytes_may_follow_the_merged_mining_hash() { + let rule = merged_mining_rule(regtest_with_iris300_at(0)); + let header = minable_header(10); + let judge = |after: usize| { + let coinbase = coinbase_committing_to(&header, after); + rule.validate(&mined_with_siblings(header.clone(), coinbase, &[])) + }; + assert_eq!(judge(128), Ok(())); + assert_eq!(judge(129), Err(ValidationError::BitcoinCoinbaseTagInvalid)); +} + +/// Not in rskj's suite: `ProofOfWorkRule` also refuses a coinbase whose tag +/// committing to the block is not its last, so that no later tag can claim +/// the same work. +#[test] +fn the_tag_committing_to_the_block_must_be_the_last_tag() { + let rule = merged_mining_rule(regtest_with_iris300_at(0)); + let header = minable_header(10); + let hash = header.hash_for_merged_mining(); + let judge = |later_tag: bool| { + let mut tail = tail_committing_to(hash.as_slice(), 0, 30); + if later_tag { + tail.extend_from_slice(RSK_TAG); + } + let coinbase = compressed_coinbase(0, &tail); + rule.validate(&mined_with_siblings(header.clone(), coinbase, &[])) + }; + assert_eq!(judge(false), Ok(())); + assert_eq!(judge(true), Err(ValidationError::BitcoinCoinbaseTagInvalid)); +} diff --git a/crates/core/src/rskj_tests/blocks/rskip92_merkle_proof_validator_tests.rs b/crates/core/src/rskj_tests/blocks/rskip92_merkle_proof_validator_tests.rs new file mode 100644 index 0000000..3917561 --- /dev/null +++ b/crates/core/src/rskj_tests/blocks/rskip92_merkle_proof_validator_tests.rs @@ -0,0 +1,193 @@ +//! rskj `org.ethereum.validator.Rskip92MerkleProofValidatorTests`. +//! +//! From RSKIP92 (orchid) a merged-mining merkle proof is a run of 32-byte +//! hashes, each combined on the right of the node below it, starting from +//! the coinbase's hash (`Rskip92MerkleProofValidator`). A proof whose length +//! is no whole number of hashes is refused, and from RSKIP180 (iris300) so is +//! one over 960 bytes, checked first. rskj refuses the block when building +//! the validator throws (`ProofOfWorkRule.isValid` catches it and answers +//! false); here the same checks are [`MergedMiningRule`]'s. +//! +//! [`MergedMiningRule`]: crate::validation::MergedMiningRule + +use super::support::{ + coinbase_committing_to, folded_root, hashes, merged_mining_rule, minable_header, mined_through, + mined_with_siblings, regtest_with_iris300_at, wire, +}; +use crate::validation::merged_mining::{self, MAX_MERGED_MINING_MERKLE_PROOF_LENGTH}; +use crate::validation::{HeaderValidator, ValidationError}; +use alloy_primitives::Bytes; + +/// RSKIP180 arrives at this height in these tests; below it the proof's +/// length is held only to whole hashes. +const IRIS300: u64 = 100; + +/// bitcoinj's `MerkleTreeUtils.combineLeftRight`, worked by hand: each node +/// reversed from display order, the two double-SHA-256'd, and the result +/// reversed back. +fn combine(left: &[u8; 32], right: &[u8; 32]) -> [u8; 32] { + use sha2::{Digest, Sha256}; + let mut input = [0u8; 64]; + input[..32].copy_from_slice(left); + input[..32].reverse(); + input[32..].copy_from_slice(right); + input[32..].reverse(); + let mut out: [u8; 32] = Sha256::digest(Sha256::digest(input)).into(); + out.reverse(); + out +} + +/// rskj `isValid_PassValidHashes_ShouldReturnTrue`, +/// `isValid_PassInvalidCoinbaseHash_ShouldReturnFalse` and +/// `isValid_PassInvalidRootHash_ShouldReturnFalse`: five hashes, the first +/// of them the coinbase's too, fold from the coinbase to one root, and a +/// different coinbase or a different root does not match it. Then the same +/// at the rule, on a header merge-mined through five siblings. +#[test] +fn a_proof_folds_the_coinbase_hash_with_each_hash_in_turn() { + let list = hashes(5); + let proof = list.concat(); + let coinbase = list[0]; + let root = list + .iter() + .fold(coinbase, |node, hash| combine(&node, hash)); + + assert_eq!( + merged_mining::rskip92_merkle_root(&coinbase, &proof), + Some(root) + ); + assert_ne!( + merged_mining::rskip92_merkle_root(&[0; 32], &proof), + Some(root), + "another coinbase" + ); + assert_ne!( + merged_mining::rskip92_merkle_root(&coinbase, &proof), + Some([0; 32]), + "another root" + ); + + let rule = merged_mining_rule(regtest_with_iris300_at(0)); + let header = minable_header(10); + let coinbase = coinbase_committing_to(&header, 30); + let mined = mined_with_siblings(header.clone(), coinbase.clone(), &list); + assert_eq!(rule.validate(&mined), Ok(())); + + let mut wrong_root = mined.clone(); + wrong_root.bitcoin_merged_mining_header = + mined_through(header.clone(), coinbase.clone(), list.concat(), [0; 32]) + .bitcoin_merged_mining_header; + assert_eq!( + rule.validate(&wrong_root), + Err(ValidationError::BitcoinMerkleProofInvalid) + ); + + // A bit of the coinbase's midstate: the tag is still there, but the + // coinbase's hash, which the fold starts from, is another. + let mut other_coinbase = coinbase; + other_coinbase[20] ^= 1; + let mut wrong_coinbase = mined; + wrong_coinbase.bitcoin_merged_mining_coinbase_transaction = Some(Bytes::from(other_coinbase)); + assert_eq!( + rule.validate(&wrong_coinbase), + Err(ValidationError::BitcoinMerkleProofInvalid) + ); +} + +/// rskj `createInstance_PassNullProofAndRskip180Enabled_ShouldThrowIllegalArgumentError` +/// and `createInstance_PassNullProofAndRskip180Disabled_ShouldThrowNullPointerError`: +/// with no proof, the validator can't be built whether or not RSKIP180 is +/// active, and the block is refused. +#[test] +fn a_header_without_a_merkle_proof_is_refused_with_or_without_rskip180() { + let rule = merged_mining_rule(regtest_with_iris300_at(IRIS300)); + for number in [IRIS300 - 1, IRIS300] { + let header = minable_header(number); + let coinbase = coinbase_committing_to(&header, 30); + let mut mined = mined_with_siblings(header, coinbase, &[]); + assert_eq!( + rule.validate(&mined), + Ok(()), + "block {number}, with its proof" + ); + mined.bitcoin_merged_mining_merkle_proof = None; + assert_eq!( + rule.validate(&mined), + Err(ValidationError::BitcoinMerkleProofDecodeError), + "block {number}" + ); + } +} + +/// rskj `createInstance_PassLargeProofAndRskip180Enabled_ShouldThrowError`, +/// `createInstance_PassLargeMalformedProofAndRskip180Disabled_ShouldThrowError` +/// and `createInstance_PassLargeProofAndRskip180Disabled_ShouldNotThrowError`: +/// from RSKIP180 a proof over 960 bytes is refused before its form is +/// looked at; below it, 31 hashes (992 bytes) are a proof like any other, +/// and 961 bytes are no whole number of hashes. +#[test] +fn from_rskip180_a_proof_over_960_bytes_is_refused() { + let rule = merged_mining_rule(regtest_with_iris300_at(IRIS300)); + let max = MAX_MERGED_MINING_MERKLE_PROOF_LENGTH; + assert_eq!(max, 960); + let mined = |number: u64, siblings: usize| { + let header = minable_header(number); + let coinbase = coinbase_committing_to(&header, 30); + mined_with_siblings(header, coinbase, &hashes(siblings)) + }; + + // 30 hashes, 960 bytes: at the limit, on either side of RSKIP180. + assert_eq!(rule.validate(&mined(IRIS300, 30)), Ok(())); + assert_eq!(rule.validate(&mined(IRIS300 - 1, 30)), Ok(())); + + // 31 hashes, 992 bytes: a proof below RSKIP180, too long from it. + assert_eq!(rule.validate(&mined(IRIS300 - 1, 31)), Ok(())); + assert_eq!( + rule.validate(&mined(IRIS300, 31)), + Err(ValidationError::MerkleProofTooLarge { max, got: 992 }) + ); + + // 961 bytes: refused for its size from RSKIP180, which is checked + // before its form, and for its form below it. + let malformed = |number: u64| { + let mut header = mined(number, 30); + let mut proof = header + .bitcoin_merged_mining_merkle_proof + .take() + .unwrap() + .to_vec(); + proof.push(0); + header.bitcoin_merged_mining_merkle_proof = Some(Bytes::from(proof)); + rule.validate(&header) + }; + assert_eq!( + malformed(IRIS300), + Err(ValidationError::MerkleProofTooLarge { max, got: 961 }) + ); + assert_eq!( + malformed(IRIS300 - 1), + Err(ValidationError::BitcoinMerkleProofDecodeError) + ); +} + +/// rskj `createInstance_PassMalformedProofAndRskip180Enabled_ShouldThrowError` +/// and `createInstance_PassMalformedProofAndRskip180Disabled_ShouldThrowError`: +/// 33 bytes are no whole number of hashes, with or without RSKIP180, though +/// the first 32 are the hash that would make the proof right. +#[test] +fn a_proof_of_no_whole_number_of_hashes_is_refused_with_or_without_rskip180() { + let rule = merged_mining_rule(regtest_with_iris300_at(IRIS300)); + for number in [IRIS300 - 1, IRIS300] { + let header = minable_header(number); + let coinbase = coinbase_committing_to(&header, 30); + let sibling = hashes(1)[0]; + let root = folded_root(merged_mining::compute_coinbase_hash(&coinbase), &[sibling]); + let proof = [&sibling[..], &[0]].concat(); + let mined = mined_through(header, coinbase, proof, wire(root)); + assert_eq!( + rule.validate(&mined), + Err(ValidationError::BitcoinMerkleProofDecodeError), + "block {number}" + ); + } +} diff --git a/crates/core/src/rskj_tests/blocks/support.rs b/crates/core/src/rskj_tests/blocks/support.rs new file mode 100644 index 0000000..6082ebc --- /dev/null +++ b/crates/core/src/rskj_tests/blocks/support.rs @@ -0,0 +1,177 @@ +//! What the ported tests of this folder build on: a header holding only the +//! fields a test sets, and a header merge-mined through a coinbase and a +//! merkle proof of the test's choosing, as rskj's `BlockMiner` mines one. + +use crate::config::{ActivationHeights, ChainConfig}; +use crate::types::header::Header; +use crate::validation::merged_mining::{self, MergedMiningRule}; +use alloy_primitives::{Address, Bytes, B256, U256}; +use bitcoin::hashes::Hash; +use std::sync::Arc; + +/// A header at `number` and `timestamp`, every other field empty or zero. +pub(super) fn header(number: u64, timestamp: u64) -> 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: Default::default(), + extension_data: None, + difficulty: U256::ZERO, + number, + gas_limit: U256::from(10_000_000), + gas_used: 0, + timestamp, + 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: None, + bitcoin_merged_mining_merkle_proof: None, + bitcoin_merged_mining_coinbase_transaction: None, + cached_hash: None, + cached_hash_for_merged_mining: None, + kept: None, + } +} + +/// Regtest's rules with iris300, where RSKIP179 and RSKIP180 arrive, at +/// `iris300`: regtest has every upgrade from genesis, so a test of a height +/// window moves the one it is about. +pub(super) fn regtest_with_iris300_at(iris300: u64) -> ChainConfig { + ChainConfig { + activation_heights: ActivationHeights { + iris300, + ..ActivationHeights::regtest() + }, + ..ChainConfig::regtest() + } +} + +pub(super) fn merged_mining_rule(config: ChainConfig) -> MergedMiningRule { + MergedMiningRule { + config: Arc::new(config), + } +} + +/// A header at `number` and difficulty 1, where one Bitcoin hash in three +/// meets rskj's target, 2^256 / 3: [`mined_through`] rolls the nonce until +/// one does, so only the coinbase and the merkle proof are in question. +pub(super) fn minable_header(number: u64) -> Header { + let mut header = header(number, 1_000); + header.difficulty = U256::from(1); + header +} + +/// A coinbase in the compressed form a header carries: SHA-256's initial +/// state as its midstate, claiming `absorbed` bytes already hashed, then +/// `tail`. +pub(super) 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: `before` bytes, the RSK tag, `committed`, then `after` +/// bytes. +pub(super) 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 +} + +/// The compressed coinbase of a Bitcoin block that commits to `header`'s +/// whole merged-mining hash, right after its tag, with `after` bytes after it. +pub(super) fn coinbase_committing_to(header: &Header, after: usize) -> Vec { + let hash = header.hash_for_merged_mining(); + compressed_coinbase(0, &tail_committing_to(hash.as_slice(), 0, after)) +} + +/// A root folded in bitcoinj's display order, as a Bitcoin header holds it: +/// reversed. +pub(super) fn wire(display: [u8; 32]) -> [u8; 32] { + let mut out = display; + out.reverse(); + out +} + +/// Roll `btc`'s nonce until its hash meets rskj's target for `difficulty`, +/// as a miner does. +pub(super) fn mine(btc: &mut bitcoin::block::Header, 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}"); +} + +/// A mined Bitcoin header holding `wire_root` as its merkle root. +pub(super) fn bitcoin_header_with_root( + wire_root: [u8; 32], + difficulty: U256, +) -> bitcoin::block::Header { + let mut btc = bitcoin::block::Header { + version: bitcoin::block::Version::from_consensus(1), + prev_blockhash: bitcoin::BlockHash::all_zeros(), + merkle_root: bitcoin::TxMerkleNode::from_byte_array(wire_root), + time: 1_000, + bits: bitcoin::CompactTarget::from_consensus(0x207f_ffff), + nonce: 0, + }; + mine(&mut btc, difficulty); + btc +} + +/// `header` merge-mined through `coinbase` and `proof`, by a Bitcoin header +/// holding `wire_root` as its merkle root. +pub(super) fn mined_through( + mut header: Header, + coinbase: Vec, + proof: Vec, + wire_root: [u8; 32], +) -> Header { + let btc = bitcoin_header_with_root(wire_root, 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)); + header +} + +/// The root RSKIP92's proof folds to: the coinbase's hash, combined on the +/// left with each sibling in turn. +pub(super) fn folded_root(coinbase_hash: [u8; 32], siblings: &[[u8; 32]]) -> [u8; 32] { + siblings.iter().fold(coinbase_hash, |node, sibling| { + merged_mining::combine_left_right(&node, sibling) + }) +} + +/// `header` merge-mined through `coinbase` and an RSKIP92 proof of +/// `siblings`, by a Bitcoin header holding the root they fold to. +pub(super) fn mined_with_siblings( + header: Header, + coinbase: Vec, + siblings: &[[u8; 32]], +) -> Header { + let root = folded_root(merged_mining::compute_coinbase_hash(&coinbase), siblings); + mined_through(header, coinbase, siblings.concat(), wire(root)) +} + +/// `count` distinct hashes, none of which a test means anything by. +pub(super) fn hashes(count: usize) -> Vec<[u8; 32]> { + (0..count) + .map(|i| alloy_primitives::keccak256((i as u64).to_be_bytes()).0) + .collect() +} diff --git a/crates/core/src/rskj_tests/mod.rs b/crates/core/src/rskj_tests/mod.rs new file mode 100644 index 0000000..e6d5992 --- /dev/null +++ b/crates/core/src/rskj_tests/mod.rs @@ -0,0 +1,9 @@ +//! Tests ported from rskj's own test suite: headers, the rules a block is held +//! to, and transactions. +//! +//! Each module ports one rskj test class from the release VETIVER-9.0.4 +//! (`rskj-core/src/test/java/`) and is named after it; each test asserts what +//! the rskj test asserts, in rustock's own terms. What the tree is for is said +//! in `rustock-execution`'s `rskj_tests`. + +mod blocks; diff --git a/crates/core/src/selection_rule.rs b/crates/core/src/selection_rule.rs new file mode 100644 index 0000000..9778652 --- /dev/null +++ b/crates/core/src/selection_rule.rs @@ -0,0 +1,251 @@ +//! Which of two blocks is the best: rskj's `SelectionRule.shouldWeAddThisBlock`. +//! +//! rskj asks it of every block it connects, its own mined blocks included, +//! against the best block it holds (`BlockChainImpl.internalTryToConnect`, +//! VETIVER-9.0.4). The heavier chain wins. Two chains of the same total +//! difficulty are told apart by their blocks' fees and then their hashes, so +//! that nodes take the same block whichever of the two reached them first: the +//! newcomer wins if it paid more than twice what the best block paid, or else +//! if the best block paid less than twice what the newcomer paid and the +//! newcomer's hash is the lower. Two blocks that paid nothing keep the one held +//! first. +//! +//! REMASC holds miners to the same rule after the fact +//! (`SelectionRule.isBrokenSelectionRule`, in `rustock_execution::remasc`): a +//! block built on one that the rule wouldn't have chosen has its reward cut. + +use alloy_primitives::{B256, U256}; +use std::cmp::Ordering; + +/// What the rule weighs of a block. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Contender { + /// Its total difficulty: its parent's, its own difficulty and its uncles'. + pub total_difficulty: U256, + /// The fees its header says it paid. + pub paid_fees: U256, + /// Its hash. + pub hash: B256, +} + +/// Whether `block`, just connected, takes the best block's place from +/// `current`: rskj's `SelectionRule.shouldWeAddThisBlock`. +pub fn should_we_add_this_block(block: &Contender, current: &Contender) -> bool { + match block.total_difficulty.cmp(¤t.total_difficulty) { + Ordering::Greater => return true, + Ordering::Less => return false, + Ordering::Equal => {} + } + // rskj: "fees over PAID_FEES_MULTIPLIER_CRITERIA times higher". + if more_than_twice(block.paid_fees, current.paid_fees) { + return true; + } + // rskj: "As a last resort, choose the block with the lower hash. We ask + // that the fees are at least bigger than the half of current block." + less_than_twice(current.paid_fees, block.paid_fees) + && is_this_block_hash_smaller(&block.hash, ¤t.hash) +} + +/// rskj's `SelectionRule.isThisBlockHashSmaller`: the 32 bytes compared first +/// to last, each as an unsigned byte (`FastByteComparisons.compareTo`). +pub fn is_this_block_hash_smaller(this: &B256, other: &B256) -> bool { + this.as_slice() < other.as_slice() +} + +/// `fees > 2 * of`, as rskj's `Coin` computes it, which has no width: a +/// doubling past 256 bits is more than any fees are. +fn more_than_twice(fees: U256, of: U256) -> bool { + of.checked_mul(U256::from(2)) + .is_some_and(|twice| fees > twice) +} + +/// `fees < 2 * of`, likewise. +fn less_than_twice(fees: U256, of: U256) -> bool { + of.checked_mul(U256::from(2)) + .is_none_or(|twice| fees < twice) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn block(total_difficulty: u64, paid_fees: u64, hash: B256) -> Contender { + Contender { + total_difficulty: U256::from(total_difficulty), + paid_fees: U256::from(paid_fees), + hash, + } + } + + /// Two hashes, the first the lower. + fn low_and_high() -> (B256, B256) { + (B256::repeat_byte(0x11), B256::repeat_byte(0xEE)) + } + + /// rskj `SelectionRuleTest.smallerBlockHashTest`, and the bytes read + /// unsigned: a first byte of 0x80 is above 0x7F, not below it. + #[test] + fn a_hash_is_smaller_by_its_bytes_read_unsigned() { + let mut one = B256::ZERO; + one[0] = 1; + assert!(is_this_block_hash_smaller(&B256::ZERO, &one)); + assert!(!is_this_block_hash_smaller(&one, &B256::ZERO)); + + let mut high_bit = B256::ZERO; + high_bit[0] = 0x80; + let below = B256::repeat_byte(0x7F); + assert!(is_this_block_hash_smaller(&below, &high_bit)); + assert!(!is_this_block_hash_smaller(&high_bit, &below)); + assert!( + !is_this_block_hash_smaller(&one, &one), + "a hash isn't smaller than itself" + ); + } + + /// rskj `SelectionRuleTest.addBlockTest`: the heavier chain wins whatever + /// the fees and the hashes, and on a tie a block with fees takes the place + /// of one without. + #[test] + fn the_heavier_chain_wins_and_a_tie_goes_to_fees() { + let (low, high) = low_and_high(); + assert!(!should_we_add_this_block( + &block(1, 1_000, low), + &block(5, 0, high) + )); + assert!(should_we_add_this_block( + &block(5, 0, high), + &block(1, 1_000, low) + )); + assert!(should_we_add_this_block( + &block(5, 10, high), + &block(5, 0, low) + )); + } + + /// On a tie, with fees within twice each other's, the lower hash wins, + /// whichever block arrived first. + #[test] + fn a_tie_with_fees_alike_goes_to_the_lower_hash() { + let (low, high) = low_and_high(); + assert!(should_we_add_this_block( + &block(7, 100, low), + &block(7, 100, high) + )); + assert!(!should_we_add_this_block( + &block(7, 100, high), + &block(7, 100, low) + )); + assert!(should_we_add_this_block( + &block(7, 60, low), + &block(7, 100, high) + )); + assert!(!should_we_add_this_block( + &block(7, 60, high), + &block(7, 100, low) + )); + } + + /// The two bounds at twice the fees. More than twice the best block's + /// fees wins whatever the hash; exactly twice falls to the hashes. The + /// best block keeps its place against a lower hash once it paid twice the + /// newcomer's fees or more. + #[test] + fn twice_the_fees_is_the_bound_both_ways() { + let (low, high) = low_and_high(); + assert!(should_we_add_this_block( + &block(7, 201, high), + &block(7, 100, low) + )); + assert!(!should_we_add_this_block( + &block(7, 200, high), + &block(7, 100, low) + )); + assert!(should_we_add_this_block( + &block(7, 200, low), + &block(7, 100, high) + )); + + assert!(!should_we_add_this_block( + &block(7, 100, low), + &block(7, 200, high) + )); + assert!(!should_we_add_this_block( + &block(7, 100, low), + &block(7, 250, high) + )); + assert!(should_we_add_this_block( + &block(7, 100, low), + &block(7, 199, high) + )); + } + + /// Two blocks that paid nothing: the one held first keeps its place, + /// however the hashes fall. A newcomer that paid anything beats one that + /// paid nothing. + #[test] + fn without_fees_a_tie_keeps_the_block_held_first() { + let (low, high) = low_and_high(); + assert!(!should_we_add_this_block( + &block(7, 0, low), + &block(7, 0, high) + )); + assert!(!should_we_add_this_block( + &block(7, 0, low), + &block(7, 1, high) + )); + assert!(should_we_add_this_block( + &block(7, 1, high), + &block(7, 0, low) + )); + } + + /// rskj's `Coin` doubles without a width, so a doubling past 256 bits is + /// more than any fees: neither comparison may wrap. + #[test] + fn a_doubling_past_256_bits_does_not_wrap() { + let (low, high) = low_and_high(); + let half = U256::from(1) << 255; + let at = |paid_fees: U256, hash: B256| Contender { + total_difficulty: U256::from(7), + paid_fees, + hash, + }; + // The best block paid 2^255, and twice that is 2^256, which no fees + // are more than: the newcomer's `MAX` doesn't win by its fees, and + // the hashes decide. + assert!(!should_we_add_this_block( + &at(U256::MAX, high), + &at(half, low) + )); + assert!(should_we_add_this_block( + &at(U256::MAX, low), + &at(half, high) + )); + // The newcomer paid 2^255: the best block's `MAX` is less than twice + // that, so the hashes decide again. + assert!(should_we_add_this_block( + &at(half, low), + &at(U256::MAX, high) + )); + assert!(!should_we_add_this_block( + &at(half, high), + &at(U256::MAX, low) + )); + } + + /// The rule isn't transitive once fees differ by twice or more: with fees + /// of 25, 10 and 15, and the cheapest block's hash the lowest and the + /// first's the highest, the block that paid 10 loses to the one that paid + /// 25 and beats the one that paid 15, which beats the first. So a header + /// weighed again after the head moved can take a head that rskj, weighing + /// each block once, would keep (the catalogue's §99). + #[test] + fn the_rule_is_not_transitive() { + let first = block(7, 25, B256::repeat_byte(0x30)); + let cheapest = block(7, 10, B256::repeat_byte(0x10)); + let third = block(7, 15, B256::repeat_byte(0x20)); + assert!(!should_we_add_this_block(&cheapest, &first)); + assert!(should_we_add_this_block(&third, &first)); + assert!(should_we_add_this_block(&cheapest, &third)); + } +} 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 9e0ee1f..62c560e 100644 --- a/crates/core/src/types/header.rs +++ b/crates/core/src/types/header.rs @@ -1,7 +1,9 @@ use alloy_primitives::{Address, Bloom, B256, U256, Bytes}; use alloy_rlp::Encodable; +use num_bigint::BigUint; use serde::{Deserialize, Serialize}; -use crate::rlp_compat::{decode_u64_lenient, decode_u256_lenient}; +use std::borrow::Cow; + #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct Header { @@ -42,55 +44,240 @@ 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. + /// 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 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>, + + /// 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, } -/// 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, } -/// 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, +/// 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, and how it reads it. +/// +/// 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 + /// 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` + /// 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. + 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, + /// 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, + /// Block 0's difficulty as the bytes it was read from, where they aren't + /// how rskj writes the value (`toByteArray`). rskj reads block 0 as a + /// `GenesisHeader`, which writes its difficulty back as it read it, + /// leading zeros and all (`GenesisHeader.encodeBlockDifficulty`), and + /// rskj's genesis files write it with them: `0x0000100000` on mainnet + /// and testnet. `difficulty` holds the value. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub difficulty: 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 +/// 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")) +} + +/// `new BigInteger(bytes)`, which reads the difficulty and the minimum gas +/// 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; + } + 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. +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 } } - count +} + +/// 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), + } +} + +/// 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), + } } /// rskj `ByteUtil.rlpToShorts`: little-endian pairs. @@ -103,7 +290,7 @@ fn rlp_to_shorts(bytes: &[u8]) -> Vec { /// encodes difficulty (`RLP.encodeBlockDifficulty`), gasLimit (raw bytes it /// produced the same way) and minimumGasPrice /// (`RLP.encodeSignedCoinNonNullZero`) with these semantics. -fn java_signed_bytes(value: &U256) -> Vec { +pub(crate) fn java_signed_bytes(value: &U256) -> Vec { if value.is_zero() { return vec![0]; } @@ -117,6 +304,270 @@ fn java_signed_bytes(value: &U256) -> Vec { out } +/// `ByteUtil.stripLeadingZeroes`: the bytes from the first that isn't zero, +/// and one zero byte where all are. +pub(crate) fn strip_leading_zeroes(bytes: &[u8]) -> &[u8] { + match bytes.iter().position(|byte| *byte != 0) { + Some(first) => &bytes[first..], + None => &[0], + } +} + +/// `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) +} + +/// 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) +} + +/// 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 +} + +/// 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. @@ -156,12 +607,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, @@ -176,100 +625,225 @@ 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() }, + // 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, + 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) { + // 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); - // 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() { + 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, + ); + // 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), }, - _ => self.logs_bloom.encode(list), + None => encode_kept( + list, + kept.and_then(|k| k.logs_bloom.as_ref()), + &self.logs_bloom, + ), + } + // Block 0's difficulty as it was read, where rskj keeps it so. + match kept.and_then(|k| k.difficulty.as_ref()) { + Some(bytes) => bytes.encode(list), + None => java_signed_bytes(&self.difficulty).as_slice().encode(list), } - java_signed_bytes(&self.difficulty).as_slice().encode(list); self.number.encode(list); - 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); - self.paid_fees.encode(list); - java_signed_bytes(&self.minimum_gas_price).as_slice().encode(list); + encode_kept( + list, + kept.and_then(|k| k.paid_fees.as_ref()), + &self.paid_fees, + ); + self.minimum_gas_price_bytes()[..].encode(list); 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 { @@ -288,7 +862,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); } @@ -301,7 +875,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()) @@ -309,46 +884,240 @@ 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 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 { - 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) + 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, + /// 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. + /// + /// 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, form)?; + *buf = &buf[next..]; + Ok(header) + } + + /// 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. + 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 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 + // 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_bytes = field(7)?; + let difficulty = difficulty_bytes.and_then(java_signed).ok_or(refused( + "a difficulty that is none, negative or past 256 bits", + ))?; + if number == 0 { + // rskj reads block 0 as a `GenesisHeader` + // (`BlockFactory.createBlockHeader`), which writes its difficulty + // back as the bytes it read: see `KeptFields::difficulty`. + kept.difficulty = difficulty_bytes + .filter(|bytes| *bytes != java_signed_bytes(&difficulty).as_slice()) + .map(Bytes::copy_from_slice); + } + // 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)?.ok_or(refused("no gas limit"))?; + let (gas_limit, bytes) = read_gas_limit(bytes); + kept.gas_limit = bytes; + gas_limit + }; + 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 { - // 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) } }; + // `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 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)?, + }; - let mut header = Self { + // 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; + 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")); + } + 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, + }, + }); + } + } + + // 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 + // 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 mining { + [] => (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", + )) + } + }; + + // How rskj will hash it, by the heights it's read under. + kept.set_rskip92(number, activations); + + Ok(Self { parent_hash, ommers_hash, beneficiary, @@ -357,237 +1126,273 @@ impl Header { receipts_root, logs_bloom, extension_data, - difficulty: decode_u256_lenient(&mut body)?, - number: decode_u64_lenient(&mut body)?, - gas_limit: decode_u256_lenient(&mut body)?, - gas_used: decode_u64_lenient(&mut body)?, - timestamp: decode_u64_lenient(&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)?, - umm_root: None, - version_fields: None, - bitcoin_merged_mining_header: None, - bitcoin_merged_mining_merkle_proof: None, - bitcoin_merged_mining_coinbase_transaction: None, + 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, - }; + }) + } +} - // 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) - let remaining_items = count_rlp_items(body); +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), + /// as the heights the header was read or built under say + /// ([`KeptFields::rskip92`]), or else this process's. + fn rskip92_active(&self) -> bool { + self.kept() + .and_then(|kept| kept.rskip92) + .unwrap_or_else(|| self.number >= crate::config::ActivationHeights::current().orchid) + } - if remaining_items >= 4 { - header.umm_root = Some(Bytes::decode(&mut body)?); - } + /// What rskj kept of this header that the typed fields can't hold. + pub fn kept(&self) -> Option<&KeptFields> { + self.kept.as_deref() + } - // 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..]; - - // 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; - - // 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. - 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); - if version >= 2 && extras == 1 && !rskip144 { - base_event = Some(Bytes::decode(&mut body)?); - } else { - if extras >= 1 { - edges = Some(rlp_to_shorts(&Bytes::decode(&mut body)?)); - } - if version >= 2 && extras >= 2 { - base_event = Some(Bytes::decode(&mut body)?); - } - } - header.version_fields = Some(HeaderVersionFields { version, edges, base_event }); - } + /// 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) + } - if !body.is_empty() { - header.bitcoin_merged_mining_header = Some(Bytes::decode(&mut body)?); + /// 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 + /// 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), + None => Cow::Owned(java_signed_bytes(&self.gas_limit)), } - if !body.is_empty() { - header.bitcoin_merged_mining_merkle_proof = Some(Bytes::decode(&mut body)?); + } + + /// 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); } - if !body.is_empty() { - header.bitcoin_merged_mining_coinbase_transaction = Some(Bytes::decode(&mut body)?); + 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()) + } + + /// 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 (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)), } - - Ok(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). - fn rskip92_active(&self) -> bool { - self.number >= crate::config::ActivationHeights::current().orchid + /// 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); + } + + /// 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)) { + let mut kept = self.kept.take().map(|kept| *kept).unwrap_or_default(); + change(&mut kept); + 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 { + 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; } - 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) - } - - /// 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; + alloy_primitives::keccak256(self.encoded( + HeaderForm::Compressed, + true, + !self.rskip92_active(), + )) + } + + /// [`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, + payload_length: payload.len(), } - 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)) + .encode(&mut out); + out.extend_from_slice(&payload); + out + } + + /// 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 from RLP bytes and compute the hash from those original - /// bytes (before our re-encoding potentially changes them). + /// 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 + /// 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 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 { - use alloy_rlp::Decodable; - 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]) - } 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 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()); - 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) - }; + Self::decode_with_hash_and_activations(buf, crate::config::ActivationHeights::current()) + } - header.cached_hash_for_merged_mining = Some(mm_hash); - } + /// [`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 { + 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) } @@ -602,86 +1407,893 @@ 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 base_hash = + alloy_primitives::keccak256(self.encoded(HeaderForm::Compressed, false, false)); + + if is_umm_block { + let umm_bytes = self.umm_root.as_ref().unwrap(); + let mut input = Vec::with_capacity(20 + umm_bytes.len()); + input.extend_from_slice(&base_hash.as_slice()[..20]); + input.extend_from_slice(umm_bytes.as_ref()); + alloy_primitives::keccak256(&input) + } else { + base_hash + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + 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` + /// 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), + beneficiary: Address::repeat_byte(0x33), + state_root: B256::repeat_byte(0x44), + transactions_root: B256::repeat_byte(0x55), + receipts_root: B256::repeat_byte(0x66), + logs_bloom: Bloom::repeat_byte(0x77), + extension_data: None, + difficulty: U256::from(1234567), + number: 42, + gas_limit: U256::from(10_000_000), + gas_used: 123456, + timestamp: 1700000000, + extra_data: Bytes::from("extra"), + paid_fees: U256::from(100), + minimum_gas_price: U256::from(1), + uncle_count: 0, + umm_root: None, + version_fields: None, + bitcoin_merged_mining_header: Some(Bytes::from("btc_header")), + bitcoin_merged_mining_merkle_proof: Some(Bytes::from("proof")), + bitcoin_merged_mining_coinbase_transaction: Some(Bytes::from("coinbase")), + cached_hash: None, + cached_hash_for_merged_mining: None, + kept: None, + } + } + + #[test] + fn test_header_rlp_roundtrip() { + let header = standard_test_header(); + + // Encode + let mut buffer = Vec::new(); + header.encode(&mut buffer); + + // Decode + 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); + } + + /// 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; + 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. + pub(super) 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)| { + // 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() + }; + 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(); + 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 fields = |extra: usize| { + let mut fields = items[..16].to_vec(); + fields.extend(items[16..].iter().cycle().take(extra).cloned()); + 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 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. + pub(super) 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()); + } + } + + /// Block 0 is the exception for the difficulty: rskj reads it as a + /// `GenesisHeader`, which writes the difficulty back as it came, leading + /// zeros and all, and hashes it so. The store reads back what it wrote. + #[test] + fn block_0_keeps_its_difficulty_as_it_came() { + let mut items = standard_items(); + items[7] = element(&[0, 0, 0x12, 0xd6, 0x87]); + items[8] = element(&[]); + let header = Header::decode_with_hash(&mut list_of(&items).as_slice()).unwrap(); + assert_eq!( + (header.number, header.difficulty), + (0, U256::from(1_234_567)) + ); + writes_back_as(&header, &items); + let stored = Header::decode_in( + &mut header.encode_stored().as_slice(), + crate::config::ActivationHeights::current(), + HeaderForm::Stored, + ) + .unwrap(); + assert_eq!(stored.hash(), header.hash()); + + // Block 1 with the same bytes hashes its difficulty as rskj writes it. + items[8] = element(&[1]); + let header = Header::decode_with_hash(&mut list_of(&items).as_slice()).unwrap(); + assert_eq!( + header.hash(), + keccak256(list_of(&{ + let mut items = items.clone(); + items[7] = element(&[0x12, 0xd6, 0x87]); + 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), + ] { + 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); + } + + /// 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, `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, value) in [ + // As rskj's miner writes 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, 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, 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 + /// 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); + 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(), + "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); } + } - let mut out = Vec::new(); - alloy_rlp::Header { list: true, payload_length: payload.len() }.encode(&mut out); - out.extend_from_slice(&payload); + /// 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)); + } - let base_hash = alloy_primitives::keccak256(&out); + /// 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; + 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, 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. + let before = ActivationHeights::mainnet(); + let from = ActivationHeights { + rskip351: Some(1), + ..ActivationHeights::mainnet() + }; - if is_umm_block { - let umm_bytes = self.umm_root.as_ref().unwrap(); - let mut input = Vec::with_capacity(20 + umm_bytes.len()); - input.extend_from_slice(&base_hash.as_slice()[..20]); - input.extend_from_slice(umm_bytes.as_ref()); - alloy_primitives::keccak256(&input) - } else { - base_hash + 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())) + ); + // 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))) + ); + 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)); } } -} -#[cfg(test)] -mod tests { - use super::*; - use alloy_primitives::{Address, B256, U256, Bytes, keccak256, b256, address, hex}; - use alloy_rlp::{Decodable, Encodable}; + /// 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() + }; - fn standard_test_header() -> Header { - Header { - parent_hash: B256::repeat_byte(0x11), - ommers_hash: B256::repeat_byte(0x22), - beneficiary: Address::repeat_byte(0x33), - state_root: B256::repeat_byte(0x44), - transactions_root: B256::repeat_byte(0x55), - receipts_root: B256::repeat_byte(0x66), - logs_bloom: Bloom::repeat_byte(0x77), - extension_data: None, - difficulty: U256::from(1234567), - number: 42, - gas_limit: U256::from(10_000_000), - gas_used: 123456, - timestamp: 1700000000, - extra_data: Bytes::from("extra"), - paid_fees: U256::from(100), - minimum_gas_price: U256::from(1), - uncle_count: 0, - umm_root: Some(Bytes::from("umm")), - version_fields: None, - bitcoin_merged_mining_header: Some(Bytes::from("btc_header")), - bitcoin_merged_mining_merkle_proof: Some(Bytes::from("proof")), - bitcoin_merged_mining_coinbase_transaction: Some(Bytes::from("coinbase")), - cached_hash: None, - cached_hash_for_merged_mining: None, + 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. + let extension = BlockHeaderExtension::V1 { + logs_bloom: shaped, + edges: None, + }; + let mut hashed = items.clone(); + hashed[6] = element(&extension.create_extension_data()); + hashed.remove(16); + 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); + } + + /// 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 test_header_rlp_roundtrip() { - let header = standard_test_header(); + 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()); + } - // Encode - let mut buffer = Vec::new(); - header.encode(&mut buffer); + /// 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"), + (9, element(&[]), "no gas limit"), + (14, element(&[]), "no minimum gas price"), + (14, element(&[0xff]), "a negative minimum gas price"), + ( + 14, + element(&[0x80; 33]), + "a negative 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)); + } - // Decode - let decoded_header = Header::decode(&mut buffer.as_slice()).expect("Failed to decode header"); + /// 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(16); + 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)); + } - // Assert - assert_eq!(header, decoded_header); - - // Hash check (just to ensure it doesn't panic) - let hash = header.hash(); - assert_ne!(hash, B256::ZERO); + /// 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] @@ -729,6 +2341,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"); @@ -790,6 +2403,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!( @@ -887,6 +2501,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(); @@ -994,6 +2609,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")); } @@ -1060,6 +2676,25 @@ 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_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 + .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. @@ -1128,15 +2763,217 @@ 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_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_peers(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)); + } + + /// **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_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(); + 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, + kept: 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_peers(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. 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 } @@ -1179,14 +3016,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); @@ -1195,17 +3032,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:?}"); + } } } @@ -1244,10 +3077,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", @@ -1269,7 +3103,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); @@ -1291,7 +3128,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" @@ -1300,46 +3141,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 @@ -1356,4 +3213,562 @@ 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 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_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 { + 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 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); + 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, + }) + }; + let base_event = || Some(Bytes::from_static(b"base event")); + let versions = [ + 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 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] { + let mut header = Header { + number, + 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; + header.bitcoin_merged_mining_coinbase_transaction = None; + } + 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; + } + } + } + } + assert_eq!(layouts, 2 * 3 * 10 * 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_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); + 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); + } + } + } + } + + /// 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/types/receipt.rs b/crates/core/src/types/receipt.rs index 776c826..3f190c7 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,145 @@ 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()); + } + + /// A receipt that spent no gas writes both gas counts empty, as rskj's + /// do: `TransactionReceipt.setGasUsed` and `setCumulativeGas` take the + /// count through BouncyCastle 1.59's `BigIntegers.asUnsignedByteArray`, + /// which makes zero the empty array (rskj's own `ByteUtil.bigIntegerToBytes` + /// says so, being written to avoid it), and `RLP.encodeElement` writes that + /// as `0x80`. A REMASC transaction spends none, so every block whose only + /// transaction is REMASC's carries the root of this one receipt: mainnet's + /// #1 under the Orchid hash, and #1,591,001, past RSKIP-126 at #1,591,000, + /// under the Unitrie's (`BlockHashesHelper.calculateReceiptsTrieRoot`). + #[test] + fn a_receipt_that_spent_no_gas_writes_it_empty_and_hashes_to_mainnets_root() { + let receipt = Receipt::new(true, 0, 0, Bloom::ZERO, vec![]); + let encoded = receipt.rlp_encode(); + // [postTxState 01, cumulativeGas 80, bloom, logs c0, gasUsed 80, status 01] + assert_eq!(&encoded[..5], &[0xf9, 0x01, 0x08, 0x01, 0x80]); + assert_eq!(&encoded[encoded.len() - 3..], &[0xc0, 0x80, 0x01]); + let orchid: B256 = "0xa70fc9a7d692ac80a46b23dcf81d5928dbdebc8ac097cfdbce2c03301d9e9406" + .parse() + .unwrap(); + let unitrie: B256 = "0x66cfdb731f620cd96e2c2cb0f7d3c3a2879c29b40014aa27efbbf3cf9cd3b0f6" + .parse() + .unwrap(); + assert_eq!( + ordered_trie_root(std::slice::from_ref(&receipt), false), + orchid + ); + assert_eq!(ordered_trie_root(&[receipt], true), unitrie); + } + + /// A receipt's logs and bloom, held to a real block: mainnet #1,590,872's + /// only transaction is REMASC's, paying two miners and logging each + /// payment (`RemascFeesPayer.logPayment`). Each log is written + /// `[address, [topics], data]` (`LogInfo.getEncoded`), the receipt's bloom + /// is the union of its logs' (`TransactionReceipt.setLogInfoList`, + /// `LogInfo.getBloom`), and the block carries that bloom and the root. + #[test] + fn a_receipts_logs_and_bloom_hash_to_mainnets_root() { + let remasc: Address = "0x0000000000000000000000000000000001000008" + .parse() + .unwrap(); + let topic: B256 = "0x000000000000000000000000000000006d696e696e675f6665655f746f706963" + .parse() + .unwrap(); + let paid = |to: &str, data: &str| Log { + address: remasc, + topics: vec![topic, to.parse().unwrap()], + data: Bytes::from(hex_to_bytes(data)), + }; + let logs = vec![ + paid( + "0x00000000000000000000000014d3065c8eb89895f4df12450ec6b130049f8034", + "e8a029e5929235c79467fe3b7e2e1742607c8fa55450c89560557226ec022566418d8602384fb8b07a", + ), + paid( + "0x00000000000000000000000012d3178a62ef1f520944534ed04504609f7307a1", + "e8a029e5929235c79467fe3b7e2e1742607c8fa55450c89560557226ec022566418d8608ca83607d69", + ), + ]; + let mut bloom = Bloom::ZERO; + for log in &logs { + crate::bloom::accrue_log(&mut bloom, log); + } + let carried: Bloom = concat!( + "0x", + "0000000000000000000000000000000000000000000000000000000000000000", + "0000000000000000000000000000000000000000000000000000000000000000", + "0000000000000000000000000000000000000000000000000000020000000000", + "0000000000000000000000000000000000000000000000000000000000000000", + "0000000000000000000000000000000000000000000000000000000000000000", + "0000000000001000200000000000000000000000000000000010000000000000", + "0000000100000000000000000000000000002000010000040020000010000000", + "0000000000000000000004000000000000020000000000000000000000000000", + ) + .parse() + .unwrap(); + assert_eq!(bloom, carried); + let receipt = Receipt::new(true, 0, 0, bloom, logs); + let root: B256 = "0xac2a5ff23a9f7cc1b6dc9ddd310d9d9f86dcf56a3ffa28715600d63e419dd15a" + .parse() + .unwrap(); + assert_eq!(ordered_trie_root(&[receipt], false), root); + } } diff --git a/crates/core/src/types/transaction.rs b/crates/core/src/types/transaction.rs index 111cce2..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. /// - /// 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; + /// 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. + /// + /// 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,21 +345,223 @@ 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 /// 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) } +/// The sender rskj gives a transaction whose signature recovers the point at +/// infinity rather than a key: the last 20 bytes of keccak256 of nothing. +/// +/// libsecp256k1 refuses such a signature, and rskj takes it. Bouncy Castle +/// encodes the point at infinity as the one byte `00`, and rskj's native +/// service, its default, answers that same byte when libsecp256k1 refuses a +/// key that would have been the point at infinity +/// (`Secp256k1ServiceNative.recoverFromSignature`). `ECKey.getAddress` then +/// hashes the encoding past its first byte, which is nothing. Testnet holds +/// such transactions: the first in #8,269,754 is one, from an account that +/// had sent ten before it. +pub const INFINITY_SENDER: Address = + alloy_primitives::address!("dcc703c0e500b653ca82273b7bfad8045d85a470"); + +/// The key a transaction's signature recovers, as rskj recovers it. +enum Recovered { + /// A point on the curve. + Key(secp256k1::PublicKey), + /// The point at infinity, whose encoding to rskj, compressed or not, is + /// the one byte `00`: see [`INFINITY_SENDER`]. + Infinity, +} + +/// Whether a signature whose key libsecp256k1 refused to recover would have +/// recovered the point at infinity, which is what rskj's native service asks +/// when its recovery fails (`NativeSecp256k1.isInfinity`). +/// +/// The key is r⁻¹(sR − eG), where R is the point whose x is r and whose y has +/// the parity of the recovery ID, and e is the signing hash modulo the +/// curve's order: it's the point at infinity just when R exists and sR = eG. +/// `from_compact` has already held r and s below the order; rskj refuses +/// either at zero before it recovers anything +/// (`ECDSASignature.validateComponentsWithoutV`), and so does this. +fn recovers_infinity(signature: &[u8; 64], recovery_id: u8, hash: &[u8; 32]) -> 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, + } +} + +/// 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. + /// 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. /// @@ -218,81 +572,59 @@ 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
{ - let hash = self.signing_hash(chain_id); + Ok(match self.recover_key(chain_id)? { + Recovered::Key(key) => address_of(&key), + Recovered::Infinity => INFINITY_SENDER, + }) + } - 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) { - 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 + /// 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 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>()); - - // `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); - - 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..])) - } - - 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. /// - /// 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 +638,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); @@ -333,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, @@ -360,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] @@ -388,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), @@ -418,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" ); } @@ -475,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(); @@ -487,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; @@ -530,6 +887,165 @@ 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 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 { + 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); + 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 = 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()); + } + + /// 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()); + } + + /// `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 { @@ -669,3 +1185,489 @@ 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(); + } + } + // 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), + 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}" + ); + } + } + } +} + +#[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..22d01a8 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 @@ -19,33 +20,32 @@ 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 /// 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 { 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 { return Err(ValidationError::TxGasPriceBelowMinimum { tx_gas_price: tx.gas_price, - block_minimum: min, + block_minimum: block.header.minimum_gas_price_value(), }); } } @@ -80,8 +80,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 +101,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), } } @@ -143,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) } } @@ -166,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 }); } @@ -207,22 +213,12 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } 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 +254,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). @@ -278,15 +280,49 @@ mod tests { Err(ValidationError::TxGasPriceAboveCap { .. }) )); - // One block before fingerroot500 the rule does not apply. + // One block before fingerroot500 the rule does not apply, and from + // its first block it does. let pre = block_at(5_467_999, 100, vec![user_tx(10_001), remasc_tx()]); assert!(rule.validate_block(&pre).is_ok()); + let first = block_at(5_468_000, 100, vec![user_tx(10_001), remasc_tx()]); + assert!(matches!( + rule.validate_block(&first), + Err(ValidationError::TxGasPriceAboveCap { .. }) + )); // `isSurpassed` short-circuits when the block's minimum is zero. let zero_min = block_at(9_000_000, 0, vec![user_tx(u64::MAX), remasc_tx()]); assert!(rule.validate_block(&zero_min).is_ok()); } + /// The body rules a chain runs hold it to RSKIP252 from its own + /// fingerroot500 (`reference.conf`): mainnet's #5,468,000 and testnet's + /// #4,015,800, and not a block sooner. + #[test] + fn the_default_body_rules_cap_prices_from_each_chains_fingerroot500() { + use crate::config::ChainConfig; + use crate::validation::BlockVerifier; + let over = |number| block_at(number, 100, vec![user_tx(10_001), remasc_tx()]); + for (config, first) in [ + (ChainConfig::mainnet(), 5_468_000), + (ChainConfig::testnet(), 4_015_800), + ] { + let verifier = BlockVerifier::default_rsk(&config); + assert!( + verifier.verify(&over(first - 1)).is_ok(), + "no cap at #{}", + first - 1 + ); + assert!( + matches!( + verifier.verify(&over(first)), + Err(ValidationError::TxGasPriceAboveCap { .. }) + ), + "the cap from #{first}" + ); + } + } + /// rskj `RemascValidationRule`. #[test] fn remasc_rule_requires_remasc_as_the_last_transaction() { @@ -328,15 +364,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); @@ -359,6 +395,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). @@ -381,10 +479,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, @@ -393,7 +495,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(()); } @@ -472,12 +578,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/core/src/validation/difficulty.rs b/crates/core/src/validation/difficulty.rs index f592bdf..7532ccd 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); } } @@ -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 @@ -94,8 +104,10 @@ impl DifficultyRule { return parent.difficulty; } - // RSKIP156 (papyrus200): difficulty divisor changes from 50 to 400. - let divisor = if header.number >= activations.papyrus200 { + // RSKIP156 (papyrus200): the divisor goes from 50 to 400, except on + // regtest, which keeps its 2,048 (`Constants.getDifficultyBoundDivisor`, + // `getChainId() != REGTEST_CHAIN_ID`). + let divisor = if header.number >= activations.papyrus200 && self.config.chain_id != 33 { RSKIP156_DIFFICULTY_DIVISOR } else { self.config.difficulty_divisor @@ -111,9 +123,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 } @@ -127,7 +141,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, @@ -153,6 +167,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 @@ -210,4 +225,160 @@ 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); + } +} + +#[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); + } + } + + /// rskj's testnet2 (chain 34) has testnet's floor and testnet's + /// RSKIP290 one: `getMinimumDifficulty` asks the chain ID for either. + #[test] + fn testnet2_has_rskip290s_floor_as_testnet_does() { + let mut config = ChainConfig::testnet(); + config.chain_id = 34; + let rule = DifficultyRule { + config: Arc::new(config), + }; + let parent = header_at(2_581_799, 1_000_000, U256::from(550_000_000u64)); + let late = rule.difficulty_for_child(&parent, 2_581_800, 1_000_000 + 60, 0); + assert_eq!(late, U256::from(550_000_000u64)); + } +} + +#[cfg(test)] +mod rskip156_tests { + use super::rskip97_tests::header_at; + use super::*; + use crate::config::ChainConfig; + use std::sync::Arc; + + /// rskj moves the difficulty by a 400th from RSKIP156 on mainnet and + /// testnet, and by regtest's own 2,048th on regtest, RSKIP156 or not + /// (`Constants.getDifficultyBoundDivisor`). Here a block sooner than its + /// chain's duration, under each chain's constants with papyrus200 at + /// genesis. + #[test] + fn regtest_keeps_its_divisor_under_rskip156() { + for (mut config, divisor) in [ + (ChainConfig::mainnet(), 400u64), + (ChainConfig::testnet(), 400), + (ChainConfig::regtest(), 2_048), + ] { + config + .activation_heights + .set_rskj_hardfork("papyrus200", Some(0)); + let chain = config.chain_id; + let rule = DifficultyRule { + config: Arc::new(config), + }; + let parent = header_at(100, 1_000_000, U256::from(10u64.pow(18))); + let got = rule.difficulty_for_child(&parent, 101, 1_000_000 + 1, 0); + let step = U256::from(10u64.pow(18) / divisor); + assert_eq!(got, parent.difficulty + step, "chain {chain}"); + } + } + + /// Mainnet's divisor is 50 up to the block before papyrus200 and 400 from + /// its first block; its ten-minute drop goes with RSKIP97, at orchid: the + /// block before still drops to the floor, and orchid's first does not. + #[test] + fn mainnet_changes_its_divisor_and_its_drop_at_their_first_blocks() { + let rule = DifficultyRule { + config: Arc::new(ChainConfig::mainnet()), + }; + let d = U256::from(10u64.pow(18)); + let after = |number: u64, seconds: u64| { + let parent = header_at(number - 1, 1_000_000, d); + rule.difficulty_for_child(&parent, number, 1_000_000 + seconds, 0) + }; + assert_eq!(after(2_392_699, 1), d + d / U256::from(50)); + assert_eq!(after(2_392_700, 1), d + d / U256::from(400)); + assert_eq!(after(728_999, 600), rule.config.min_difficulty); + assert_eq!(after(729_000, 600), d - d / U256::from(50)); + } } diff --git a/crates/core/src/validation/fork_detection.rs b/crates/core/src/validation/fork_detection.rs index a8e6978..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. @@ -103,14 +124,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)?; @@ -118,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/header_rules.rs b/crates/core/src/validation/header_rules.rs index 6aa4652..1116472 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> { @@ -44,6 +39,15 @@ 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, as it +/// holds a block a peer sent whole before that block is taken and relayed +/// ([`HeaderVerifier::verify_for_relay`](super::HeaderVerifier::verify_for_relay)). pub struct TimestampRule { max_future_offset: u64, } @@ -107,56 +111,68 @@ 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(()) } } -/// 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(()) } } +/// 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)?; + // 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 delta = parent_limit / divisor; - - if limit < parent_limit - delta || limit > parent_limit + delta { + 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: limit, + got, }); } Ok(()) diff --git a/crates/core/src/validation/merged_mining.rs b/crates/core/src/validation/merged_mining.rs index 0cceeb4..0f3427a 100644 --- a/crates/core/src/validation/merged_mining.rs +++ b/crates/core/src/validation/merged_mining.rs @@ -1,4 +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; @@ -25,6 +28,17 @@ 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, block 0 excepted: rskj +/// `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, } @@ -33,12 +47,16 @@ 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 /// `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, } @@ -48,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 @@ -68,7 +83,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 @@ -89,6 +106,134 @@ 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 + } +} + +/// 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; @@ -96,31 +241,45 @@ impl HeaderValidator for MergedMiningRule { use bitcoin::hashes::Hash; use alloy_primitives::{B256, U256}; - if header.number < self.config.activation_heights.orchid { - return Ok(()); + // 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, + }); } // 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, 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[..]; let btc_header: BtcHeader = Decodable::consensus_decode(&mut reader) .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 { @@ -140,9 +299,25 @@ 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 { @@ -170,11 +345,24 @@ 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); 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 @@ -193,22 +381,45 @@ 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(()) } } +/// 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. /// @@ -392,15 +603,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()); @@ -421,15 +641,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 afa78bc..0360eef 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 { @@ -19,24 +20,20 @@ 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, - - #[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: u64, got: u64 }, + GasLimitInvalid { parent: BigUint, got: BigUint }, #[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, @@ -52,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, @@ -61,8 +61,23 @@ 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("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 }, + #[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 }, @@ -73,8 +88,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 }, @@ -103,6 +123,11 @@ 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 }, } @@ -168,6 +193,14 @@ 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>, + /// The rules a block a peer sent whole meets against its parent before + /// it is taken and relayed, past a trunk header's: see + /// [`Self::verify_for_relay`]. + relay_parent_rules: Vec>, } impl Default for HeaderVerifier { @@ -181,59 +214,123 @@ impl HeaderVerifier { Self { static_rules: Vec::new(), parent_rules: Vec::new(), + uncle_rules: Vec::new(), + uncle_parent_rules: Vec::new(), + relay_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 - /// finding it in the store already proves hash consistency. Including the - /// rule would break for the genesis block, whose canonical hash is derived - /// from Java's non-canonical RLP encoding (leading zeros in difficulty) - /// and cannot be reproduced by our standard `Header::hash()`. + /// finding it in the store already proves hash consistency. A store keys + /// a header by the hash it came with, and block 0 is the one header that + /// ever read back to another: in a store written before it kept its + /// difficulty's bytes as the genesis file writes them, which the node + /// writes again when it opens one. 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, - 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() }) + // 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 }) + .with_parent_rule(DifficultyRule { config: config.clone() }); + verifier.uncle_rules = uncles::rsk_header_rules(config.clone()); + verifier.relay_parent_rules = + vec![Box::new(TimestampRule::new(config.max_future_block_time))]; + 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 } + pub fn with_relay_parent_rule(mut self, rule: impl ParentHeaderValidator + 'static) -> Self { + self.relay_parent_rules.push(Box::new(rule)); + self + } + + /// What rskj's relay validator holds a block to against its parent, past + /// the rules every trunk header meets (`RskContext.getBlockRelayValidator`): + /// its time later than its parent's, the parent form of + /// `BlockTimeStampValidationRule`. + /// + /// rskj runs that validator on a block a peer sends whole + /// (`AsyncNodeBlockProcessor.processBlock`, its default): one that fails + /// is an invalid block to it, neither queued to be connected nor relayed. + /// `BlockValidatorImpl` doesn't run it, so the same block is still one + /// rskj connects, with a descendant that arrives or through its sync, and + /// [`Self::verify`] doesn't run it either. + pub fn verify_for_relay(&self, header: &Header, parent: &Header) -> Result<(), ValidationError> { + for rule in &self.relay_parent_rules { + rule.validate_with_parent(header, parent)?; + } + Ok(()) + } + /// Runs only the rules that relate a header to its parent. /// /// For walking a chain, where each header's own rules are checked once as @@ -273,7 +370,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, 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 9882592..d8247e2 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; @@ -36,6 +37,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, } } @@ -81,15 +83,16 @@ 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 { + let mut btc_header = BtcHeader { version: bitcoin::block::Version::from_consensus(1), prev_blockhash: bitcoin::BlockHash::all_zeros(), merkle_root, @@ -97,6 +100,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(); @@ -107,6 +111,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); @@ -153,8 +170,29 @@ 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 { .. }))); + + // 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] @@ -239,13 +277,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 { .. }))); } @@ -296,6 +338,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] @@ -382,17 +493,613 @@ 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 }) + ); +} + +/// 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, +/// 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] @@ -429,6 +1136,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 +1152,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 +1171,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,21 +1188,43 @@ 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 `Constants.getMaxTimestampsDiffInSecs`: testnet widens the window to -/// two hours from RSKIP297 (hop400); mainnet keeps five minutes. +/// 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 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; @@ -560,6 +1298,26 @@ fn a_zero_period_turns_the_timestamp_rule_off() { assert!(rule.validate_with_parent_at(NOW, &at(NOW), &at(NOW + 1_000)).is_ok()); } +/// rskj's relay validator runs the parent form, which its `BlockValidatorImpl` +/// doesn't (`RskContext.getBlockRelayValidator`, `getBlockValidationRule`): +/// the default verifier's relay rules refuse a header no later than its +/// parent on mainnet, and regtest's zero period turns them off. The headers +/// are long past, so the clock refuses none of them. +#[test] +fn the_relay_rules_hold_a_header_to_its_parents_time() { + let verifier = + |config: crate::config::ChainConfig| HeaderVerifier::default_rsk(Arc::new(config)); + let (parent, same, later) = (at(1_000), at(1_000), at(1_001)); + let mainnet = verifier(crate::config::ChainConfig::mainnet()); + assert!(matches!( + mainnet.verify_for_relay(&same, &parent), + Err(ValidationError::TimestampOlderThanParent { .. }) + )); + assert!(mainnet.verify_for_relay(&later, &parent).is_ok()); + let regtest = verifier(crate::config::ChainConfig::regtest()); + assert!(regtest.verify_for_relay(&same, &parent).is_ok()); +} + /// rskj `Constants`: `newBlockMaxSecondsInTheFuture` per network, and /// `getMinGasLimit()` on all of them. #[test] @@ -575,8 +1333,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 { @@ -600,8 +1360,972 @@ 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 + +/// 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()); +} + +/// 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. +#[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(())); +} + +/// 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; + +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) + ); +} + +/// 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) { + 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; + 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); +} + +/// 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, 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) +} + +/// 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(())); +} + +/// The verifier every sync path holds judges uncles by rskj's uncle rules, +/// 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() { + 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]); + seal(&mut chatty, 1010); + assert_eq!(verifier.verify_uncle(&chatty), Ok(())); + 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::GasLimitBelowMinimum { + min: 3_000_000, + got: BigUint::from(2_999_999u32) + }) + ); + + 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" + ); +} + +/// 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 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); + 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 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), + time: 1000, + 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)); + 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" + ); + } + } +} + +/// 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/core/src/validation/uncles.rs b/crates/core/src/validation/uncles.rs index 599ddc1..5da7f10 100644 --- a/crates/core/src/validation/uncles.rs +++ b/crates/core/src/validation/uncles.rs @@ -5,17 +5,68 @@ //! 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. +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 +130,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 +179,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/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/abi.rs b/crates/execution/src/abi.rs new file mode 100644 index 0000000..d8127ca --- /dev/null +++ b/crates/execution/src/abi.rs @@ -0,0 +1,522 @@ +//! A call's arguments, read as rskj reads them for its own contracts. +//! +//! 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; +//! - 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()) + } + + /// `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)) + } + + /// 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 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() { + 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 +} + +/// `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 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); + 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] + 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); + } + + /// 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_block_cache_readpath_tests.rs b/crates/execution/src/bridge/btc_block_cache_readpath_tests.rs index f050949..6f9962a 100644 --- a/crates/execution/src/bridge/btc_block_cache_readpath_tests.rs +++ b/crates/execution/src/bridge/btc_block_cache_readpath_tests.rs @@ -220,6 +220,40 @@ fn an_absent_block_is_never_remembered() { assert_eq!(cache.stats.snapshot().inserts, 0); } +/// The cache is the node's, not the chain's: after the node follows another +/// RSK chain it can hold a block this chain never stored. rskj looks a block +/// up by its hash in storage alone (`RepositoryBtcBlockStoreWithCache.get`), +/// so a header naming such a block as its parent has none and a getter +/// knows no such block; only its walk down the main chain asks the cache +/// (`getStoredBlockAtMainChainDepth`). A block only the cache holds is found +/// by no lookup of its hash, and the walk down this chain still answers +/// this chain's blocks. +#[test] +fn a_block_only_the_cache_holds_is_not_this_chains() { + let (store, root, blocks) = btc_chain_in_trie(10, 0); + let dyn_store: Arc = store.clone(); + let cache = Arc::new(BtcBlockCache::with_default_capacity()); + let head = blocks.last().unwrap(); + let elsewhere = StoredBlock::new( + header_at(head.header.block_hash(), 7_777), + head.height + 1, + head.chain_work + U256::from(100u64), + ); + let hash = elsewhere.header.block_hash(); + cache.insert(hash, elsewhere); + let mut ctx = ctx_over(dyn_store, root, Some(cache)); + + assert!(super::btc_store::get_stored_block(&mut ctx, &hash).is_none()); + for block in &blocks { + match main_chain_block_at_height(&mut ctx, block.height, false, None) { + MainChainLookup::Found(found) => { + assert_eq!(found.header.block_hash(), block.header.block_hash()) + } + other => panic!("height {}: {}", block.height, outcome(&other)), + } + } +} + // --------------------------------------------------- the read-set property -- /// **The property that keeps trie snapshots self-contained.** diff --git a/crates/execution/src/bridge/btc_chain.rs b/crates/execution/src/bridge/btc_chain.rs index b4f0364..9bded8d 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 @@ -73,12 +74,11 @@ pub fn receive_header( // call by `2 * input.len()` (mainnet #6,223,762 tx[2]: 2*164 = 328 gas). let block_number = revm::context_interface::Block::number(ctx.block()).to::(); let rskip199 = hardfork_cfg.has_rskip199(block_number); - ensure_btc_chain_seeded(ctx, config, use_v2, rskip199); + 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 = crate::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 @@ -136,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, @@ -163,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, @@ -244,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` + @@ -396,68 +407,126 @@ pub fn main_chain_block_at_height( } // Walk `depth` parents down from the head (getStoredBlockAtMainChainDepth, - // l.271-310). A missing block returns null; a height mismatch throws. + // l.271-310), each block from the cache before storage, as rskj takes it. + // A missing block returns null; a height mismatch throws. + use super::btc_store::get_stored_block_walking_the_main_chain as walking; let mut block_hash = head.header.block_hash(); for _ in 0..depth { - match get_stored_block(ctx, &block_hash) { + match walking(ctx, &block_hash) { Some(b) => block_hash = b.header.prev_blockhash, None => return MainChainLookup::Absent, } } - match get_stored_block(ctx, &block_hash) { + match walking(ctx, &block_hash) { None => MainChainLookup::Absent, Some(block) if block.height == height => MainChainLookup::Found(block), Some(_) => MainChainLookup::StoreError, } } -/// 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. +/// +/// Before RSKIP454 bitcoinj writes the checkpoint in the legacy format, which +/// throws for a chain work past 12 bytes (`StoredBlock.serializeCompactLegacy`): +/// the genesis block stays the head, and the error is the call's to answer as +/// its method answers a throw. pub fn ensure_btc_chain_seeded( ctx: &mut CTX, config: &BridgeConstants, use_v2: bool, rskip199: bool, -) { - if load_chain_head(ctx).is_some() { - return; +) -> Result<(), PrecompileError> { + 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 Ok(()); + } + 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 Ok(()); }; - 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 Ok(()); }; + let work = + U256::from_be_slice(&alloy_primitives::hex::decode(chain_work_hex).unwrap_or_default()); + let checkpoint = StoredBlock::new(header, height, work); + if !use_v2 && !checkpoint.fits_legacy() { + return Err(PrecompileError::other( + "The given number does not fit in 12", + )); + } + put_stored_block(ctx, &checkpoint, use_v2); + set_chain_head(ctx, &checkpoint, use_v2, rskip199); + Ok(()) +} - 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, +) -> Result<(), PrecompileError> { + 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)) } +/// 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, + } +} + +/// Whether bitcoinj reads `rest` as a transaction count above +/// `Integer.MAX_VALUE`. `BtcBlock.parseTransactions` casts the count to an +/// `int` and sizes its list of transactions by it, so `new ArrayList` throws +/// `IllegalArgumentException` for a negative one, where a count it can't +/// read the transactions of throws `ProtocolException`. Of the widths that +/// fit in five bytes only the four-byte one counts that high. +fn transaction_count_is_negative(rest: &[u8]) -> bool { + rest.first() == Some(&0xfe) && rest.get(4).is_some_and(|&high| high & 0x80 != 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], @@ -466,77 +535,104 @@ 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 = crate::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: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. + 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 refuse("receiveHeaders: a header is not a header's size"); + } + // `makeBlock` parses the headers in order, and the first it can't parse + // throws. Only its `ProtocolException` is caught and rethrown as a + // `BridgeIllegalArgumentException` (Bridge.java:546-555): the + // `IllegalArgumentException` of a negative transaction count reaches + // `Bridge.execute`'s last catch and fails the call, before RSKIP88 too. + if let Some(rest) = headers + .iter() + .map(|header| &header[80..]) + .find(|rest| !no_transactions_follow(rest)) + { + if transaction_count_is_negative(rest) { + return Err(PrecompileError::other( + "receiveHeaders: a header counts its transactions below zero", + )); + } + return refuse("receiveHeaders: a header could not be parsed"); + } + let rskip434 = hardfork_cfg.has_rskip434(block_number); let rskip199 = hardfork_cfg.has_rskip199(block_number); - ensure_btc_chain_seeded(ctx, config, use_v2, rskip199); + 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) { - if header_bytes.len() < 80 { + // However many there are. + for header_bytes in &headers { + let Ok(btc_header) = deserialize::(&header_bytes[..80]) else { continue; - } - - 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() { + // 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; - } - - // Check PoW - if !check_proof_of_work(&btc_header) { - 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 // 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())) } @@ -544,7 +640,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. /// @@ -569,6 +664,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")); }; @@ -656,17 +752,17 @@ 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( + crate::abi::Args::new(args) + .int(0) + .ok_or_else(|| PrecompileError::other("getBtcBlockchainBlockHashAtDepth: no 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") })?; @@ -702,10 +798,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, @@ -713,18 +813,30 @@ 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())), + // rskj's `Bridge` answers what the four header getters throw, a store + // that can't be seeded among it, with `EMPTY_BYTE_ARRAY`. + if ensure_btc_block_store(ctx, config, hardfork_cfg).is_err() { + return Ok(PrecompileOutput::new( + gas_cost, + encode_abi_bytes(&[]).into(), + )); } + // 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 @@ -732,50 +844,75 @@ pub fn get_block_header_by_hash( ctx: &mut CTX, args: &[u8], gas_cost: u64, + config: &BridgeConstants, + hardfork_cfg: &RskHardforkConfig, ) -> Result { - if args.len() < 32 { - return Err(PrecompileError::other("getBlockHeaderByHash: args too short")); + 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)); + if ensure_btc_block_store(ctx, config, hardfork_cfg).is_err() { + return Ok(PrecompileOutput::new( + gas_cost, + encode_abi_bytes(&[]).into(), + )); } - 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())), - } + // 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 = crate::abi::Args::new(args) + .int(0) + .ok_or_else(|| PrecompileError::other("getBlockHeaderByHeight: no height"))? + .int_value(); + if ensure_btc_block_store(ctx, config, hardfork_cfg).is_err() { + return Ok(PrecompileOutput::new( + gas_cost, + encode_abi_bytes(&[]).into(), + )); } - - 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 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 @@ -783,16 +920,23 @@ pub fn get_parent_block_header_by_hash( ctx: &mut CTX, args: &[u8], gas_cost: u64, + config: &BridgeConstants, + hardfork_cfg: &RskHardforkConfig, ) -> Result { - if args.len() < 32 { - return Err(PrecompileError::other("getParentBlockHeaderByHash: args too short")); + 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)); + if ensure_btc_block_store(ctx, config, hardfork_cfg).is_err() { + return Ok(PrecompileOutput::new( + gas_cost, + encode_abi_bytes(&[]).into(), + )); } - let hash = b256_to_bitcoin_hash(&alloy_primitives::B256::from_slice(&args[..32])); - // 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 +945,9 @@ pub fn get_parent_block_header_by_hash( } // --------------------------------------------------------------------------- -// ABI encoding/decoding helpers +// ABI encoding helpers (arguments are read through `crate::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 @@ -990,12 +1075,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, @@ -1019,14 +1104,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. @@ -1077,7 +1176,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::abi::Args::new(&encoded).bytes(0).unwrap(); assert_eq!(decoded, original); } @@ -1085,7 +1184,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::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 +1205,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!(decode_abi_bytes(&encoded).unwrap(), Vec::::new()); + assert_eq!(crate::abi::Args::new(&encoded).bytes(0), Some(&[][..])); } #[test] @@ -1155,4 +1254,600 @@ 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 (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(&[])); + } + + /// rskj's `receiveHeaders` checks every header's size before it takes any, + /// 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 (config, cfg) = (BridgeConstants::mainnet(), RskHardforkConfig::mainnet()); + 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 + /// 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).expect("seeded"); + 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).expect("seeded"); + 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).expect("seeded"); + 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).expect("seeded"); + 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).expect("seeded"); + ensure_btc_chain_seeded(&mut ctx, ®test, true, true).expect("seeded"); + 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).expect("seeded"); + 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)); + } + + /// 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])); + } + + /// Before RSKIP124 a header's trailing transaction count is parsed, and + /// rskj's `makeBlock` throws for one above `Integer.MAX_VALUE` an + /// `IllegalArgumentException` that `Bridge.receiveHeaders` doesn't catch: + /// the call fails, even before RSKIP88, where a header it can't read is + /// answered with a success that saves nothing. The first header it can't + /// parse decides which. + #[test] + fn before_rskip88_a_header_counting_transactions_below_zero_fails_the_call() { + use crate::abi::encode::{self, Arg}; + // Mainnet's first block: below RSKIP88 (orchid) and RSKIP124. + let (config, cfg) = (BridgeConstants::regtest(), RskHardforkConfig::mainnet()); + assert!(!cfg.has_rskip88(0) && !cfg.has_rskip124(0)); + let header = mine_regtest(regtest_genesis(), 0x11); + let call = |rests: &[&[u8]]| { + let encoded: Vec> = rests + .iter() + .map(|rest| [serialize(&header), rest.to_vec()].concat()) + .collect(); + let slices: Vec<&[u8]> = encoded.iter().map(Vec::as_slice).collect(); + let args = encode::args(&[Arg::BytesArray(&slices)]); + let mut ctx = ctx_at(0); + let answer = receive_headers(&mut ctx, &args, 0, &config, false, &cfg); + let stored = get_stored_block(&mut ctx, &header.block_hash()).is_some(); + (answer.map(|output| output.bytes.to_vec()), stored) + }; + let below_zero: &[u8] = &[0xfe, 0, 0, 0, 0x80]; + let past_its_bytes: &[u8] = &[0xfe, 0xff, 0xff, 0xff, 0x7f]; + assert!(transaction_count_is_negative(below_zero)); + assert!(!transaction_count_is_negative(past_its_bytes)); + assert!(!transaction_count_is_negative(&[0xfe, 0, 0, 0x80])); + assert!(matches!(call(&[below_zero]), (Err(_), false))); + assert!(matches!(call(&[past_its_bytes]), (Ok(ref out), false) if out.is_empty())); + // The first header bitcoinj can't parse decides. + assert!(matches!( + call(&[past_its_bytes, below_zero]), + (Ok(_), false) + )); + assert!(matches!( + call(&[below_zero, past_its_bytes]), + (Err(_), false) + )); + assert!(matches!(call(&[&[0], below_zero]), (Err(_), false))); + // From RSKIP88 both fail the call. + let mut late = ctx_at(729_000); + let encoded = [serialize(&header), past_its_bytes.to_vec()].concat(); + let args = encode::args(&[Arg::BytesArray(&[&encoded])]); + assert!(receive_headers(&mut late, &args, 0, &config, false, &cfg).is_err()); + } + + /// 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/btc_rules.rs b/crates/execution/src/bridge/btc_rules.rs new file mode 100644 index 0000000..11e1d13 --- /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-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). + +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 7a629c5..f217ada 100644 --- a/crates/execution/src/bridge/btc_store.rs +++ b/crates/execution/src/bridge/btc_store.rs @@ -54,6 +54,14 @@ impl StoredBlock { self.header.block_hash() } + /// Whether the chain work fits the legacy format's 12 bytes. bitcoinj's + /// `StoredBlock.serializeCompactLegacy` throws for one that does not + /// (`Utils.bigIntegerToBytes`), where [`Self::serialize_compact_legacy`] + /// keeps its low 12 bytes. + pub fn fits_legacy(&self) -> bool { + self.chain_work.bit_len() <= LEGACY_CHAIN_WORK_SIZE * 8 + } + /// Serialize in the legacy compact format (96 bytes). /// Layout: `[chain_work: 12B BE][height: 4B BE][header: 80B]` pub fn serialize_compact_legacy(&self) -> Vec { @@ -206,19 +214,39 @@ pub fn put_stored_block(ctx: &mut CTX, block: &StoredB store_raw_bytes(ctx, key, &data); } -/// Load a StoredBlock by its hash. +/// Load a StoredBlock by its hash: rskj `RepositoryBtcBlockStoreWithCache.get`, +/// which reads storage alone, never its cache. /// -/// Three layers, in this order, and the order is the correctness argument: -/// -/// 1. **This block's own writes.** Always authoritative, never cached -- they -/// are not committed yet and may still be discarded. -/// 2. **The cache**, when one is installed. Safe to consult here because the -/// key is the block's own hash and the value at that key never changes. -/// 3. **The parent trie.** A value found here is what the cache records. A -/// value *not* found here records nothing: see -/// [`crate::bridge::btc_block_cache`] for why negative caching would turn a -/// missing trie node into a wrong consensus answer. +/// This block's own writes come first: they are not committed yet and may +/// still be discarded. Then the parent trie. The cache is the node's, not the +/// chain's: it keeps the blocks the node read on whatever RSK chain it ran, +/// and after the node follows another chain it can hold a block this one never +/// stored. Answered from the cache, a header naming such a block as its parent +/// would be taken where rskj finds no parent, and a block known by its hash +/// where rskj knows none. A block read from the trie is recorded in the cache +/// for the main-chain walk ([`get_stored_block_walking_the_main_chain`]); a +/// block not found records nothing (see [`crate::bridge::btc_block_cache`]). pub fn get_stored_block(ctx: &mut CTX, hash: &BlockHash) -> Option { + stored_block(ctx, hash, false) +} + +/// A step of the walk down the main chain from its head: rskj +/// `getStoredBlockAtMainChainDepth`, which asks its cache before storage +/// (`getFromCache`, then `get`). Only that walk may: every block it reaches +/// from the head this chain stored, and a block's hash fixes its header, so +/// its height and chain work too, whichever chain stored it. +pub fn get_stored_block_walking_the_main_chain( + ctx: &mut CTX, + hash: &BlockHash, +) -> Option { + stored_block(ctx, hash, true) +} + +fn stored_block( + ctx: &mut CTX, + hash: &BlockHash, + cache_first: bool, +) -> Option { let key = btc_hash_to_storage_key(hash); // 1. The overlay wins over everything. @@ -230,15 +258,15 @@ pub fn get_stored_block(ctx: &mut CTX, hash: &BlockHas return StoredBlock::deserialize_compact(&data); } - // 2. The cache, if the node installed one. + // 2. The cache, if the node installed one, on the main-chain walk alone. let cache = ctx.chain_mut().btc_block_cache.clone(); - if let Some(cache) = &cache { + if let Some(cache) = cache.as_ref().filter(|_| cache_first) { if let Some(block) = cache.get(hash) { return Some(block); } } - // 3. The parent trie -- the only read a cache stands in front of. + // 3. The parent trie. let data = ctx.chain_mut().raw_storage.get_from_trie(BRIDGE_ADDR, key).unwrap_or_default(); if data.is_empty() { // Deliberately not recorded. An absence here may be a genuinely @@ -253,15 +281,64 @@ 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). // --------------------------------------------------------------------------- +/// 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 { @@ -309,20 +386,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 @@ -335,6 +399,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), @@ -522,7 +589,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. @@ -556,7 +626,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" + ); } } @@ -585,6 +658,38 @@ mod tests { ); } + /// Bitcoin block 849,138, the one rskj's mainnet constants name + /// (`blockWithTooMuchChainWorkHeight`): its chain work, 849,137's plus one + /// block of work at the same bits, is the first at or above 2^95, so the + /// top bit of a 12-byte field is set and the field read as signed is + /// negative, where 849,137's is not. Unsigned it still fits the legacy 12 + /// bytes, as it does the 32 of the second format. + #[test] + fn mainnet_849138_is_the_first_chain_work_to_set_the_top_bit_of_12_bytes() { + let cw_849137 = U256::from_str_radix("7fffdc6f043e4a69ea179a7a", 16).unwrap(); + let cw_849138 = cw_849137 + compute_work(CompactTarget::from_consensus(0x17035d25)); + assert_eq!( + cw_849138, + U256::from_str_radix("800028897aa1dce163ead2cf", 16).unwrap() + ); + let top_bit = U256::from(1u8) << 95; + assert!(cw_849137 < top_bit); + assert!(cw_849138 >= top_bit && cw_849138 < top_bit << 1); + assert_eq!( + crate::bridge::constants::BridgeConstants::mainnet() + .block_with_too_much_chain_work_height, + 849_138 + ); + + // 849,137's header, carrying 849,138's work through both formats. + let block = StoredBlock::new(parse_mainnet_header(), 849_138, cw_849138); + let legacy = + StoredBlock::deserialize_compact_legacy(&block.serialize_compact_legacy()).unwrap(); + assert_eq!(legacy.chain_work, cw_849138); + let v2 = StoredBlock::deserialize_compact_v2(&block.serialize_compact_v2()).unwrap(); + assert_eq!(v2.chain_work, cw_849138); + } + /// Verify auto-detect correctly identifies legacy vs V2 format for /// a real mainnet header. #[test] diff --git a/crates/execution/src/bridge/constants.rs b/crates/execution/src/bridge/constants.rs index af4c74f..6e1c851 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 -- @@ -49,16 +48,31 @@ 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 -- - /// 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, + // -- 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, @@ -71,13 +85,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], @@ -106,6 +118,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 @@ -124,6 +140,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 @@ -165,9 +186,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 @@ -175,9 +195,12 @@ 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, - btc_height_when_block_index_activates: 696_000, + 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, @@ -199,18 +222,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", @@ -238,11 +249,13 @@ 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, 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, @@ -271,9 +284,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, @@ -281,9 +293,11 @@ 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, - btc_height_when_block_index_activates: 2_000_000, + 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, @@ -294,18 +308,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", @@ -333,11 +335,13 @@ 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, 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, @@ -360,18 +364,43 @@ 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. + /// + /// Five authorizers vote its federation changes, 'auth-a' to 'auth-e', + /// and three decide (`FederationRegTestConstants`, MAJORITY). 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", ], + federation_change_authorizer_keys: &[ + "04dde17c5fab31ffc53c91c2390136c325bb8690dc135b0840075dd7b86910d8ab9e88baad0c32f3eea8833446a6bc5ff1cd2efa99ecb17801bcb65fc16fc7d991", + "04af886c67231476807e2a8eee9193878b9d94e30aa2ee469a9611d20e1e1c1b438e5044148f65e6e61bf03e9d72e597cb9cdea96d6fc044001b22099f9ec403e2", + "045d4dedf9c69ab3ea139d0f0da0ad00160b7663d01ce7a6155cd44a3567d360112b0480ab6f31cac7345b5f64862205ea7ccf555fcf218f87fa0d801008fecb61", + "04709f002ac4642b6a87ea0a9dc76eeaa93f71b3185985817ec1827eae34b46b5d869320efb5c5cbe2a5c13f96463fe0210710b53352a4314188daffe07bd54154", + "04aff62315e9c18004392a5d9e39496ff5794b2d9f43ab4e8ade64740d7fdfe896969be859b43f26ef5aa4b5a0d11808277b4abfa1a07cc39f2839b89cc2bc6b4c", + ], + genesis_federation_format: 4000, + block_with_too_much_chain_work_height: i32::MAX as u32, ..Self::regtest() } } @@ -381,9 +410,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, @@ -394,11 +422,13 @@ 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, - // 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", @@ -414,21 +444,23 @@ impl BridgeConstants { increase_locking_cap_authorizer_keys: &[ "04450bbaab83ec48b3cb8fbb077c950ee079733041c039a8c4f1539e5181ca1a27589eeaf0fbf430e49d2909f14c767bf6909ad6845831f683416ee12b832e36ed", ], - // FederationRegTestConstants authorizer keys (majority = 3 of 5) + // The first three of rskj's FederationRegTestConstants authorizer + // keys, GenNodeKey's 'auth-a' to 'auth-c' (majority = 2 of 3); rskj's + // regtest has all five (`rskj_regtest`). federation_change_authorizer_keys: &[ "04dde17c5fab31ffc53c91c2390136c325bb8690dc135b0840075dd7b86910d8ab9e88baad0c32f3eea8833446a6bc5ff1cd2efa99ecb17801bcb65fc16fc7d991", "04af886c67231476807e2a8eee9193878b9d94e30aa2ee469a9611d20e1e1c1b438e5044148f65e6e61bf03e9d72e597cb9cdea96d6fc044001b22099f9ec403e2", "045d4dedf9c69ab3ea139d0f0da0ad00160b7663d01ce7a6155cd44a3567d360112b0480ab6f31cac7345b5f64862205ea7ccf555fcf218f87fa0d801008fecb61", - "04709f002ac4642b6a87ea0a9dc76eeaa93f71b3185985817ec1827eae34b46b5d869320efb5c5cbe2a5c13f96463fe0210710b53352a4314188daffe07bd54154", - "04aff62315e9c18004392a5d9e39496ff5794b2d9f43ab4e8ade64740d7fdfe896969be859b43f26ef5aa4b5a0d11808277b4abfa1a07cc39f2839b89cc2bc6b4c", ], 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, 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, @@ -509,4 +541,73 @@ 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); + } + } + + /// 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 + ); + } + + /// 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] + 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); + 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(); + 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/der_signature.rs b/crates/execution/src/bridge/der_signature.rs new file mode 100644 index 0000000..5d190fe --- /dev/null +++ b/crates/execution/src/bridge/der_signature.rs @@ -0,0 +1,1257 @@ +//! A federator's signature as bitcoinj-thin's +//! `BtcECKey.ECDSASignature.decodeFromDER` reads it. +//! +//! rskj reads every signature `addSignature` is given this way before it does +//! anything else, and fails the call where it throws (`Bridge.addSignature`); +//! then it reads each again to sign with it +//! (`BridgeSupport.getDecodedSignatures`). +//! +//! bitcoinj reads with the ASN.1 classes of Bouncy Castle 1.58, which rskj +//! ships under SpongyCastle's package names. `ASN1InputStream` reads one +//! object, and bitcoinj casts it to a sequence of a definite length (a DL +//! sequence) and its first two elements to INTEGERs; nothing after that object +//! is looked at. But Bouncy Castle parses every element of the sequence as it +//! builds it, and every element inside those, and throws where it can't, so a +//! signature is refused for anything after its two integers that Bouncy Castle +//! can't read. This module reads as those classes do, with their streams: +//! `DefiniteLengthInputStream`, `IndefiniteLengthInputStream` with its two +//! bytes of look-ahead for the end-of-contents octets, `ASN1InputStream` for +//! what a definite length holds and `ASN1StreamParser` for what an indefinite +//! one holds. Where they leave bytes unread (a NULL's contents, the odd byte of +//! a BMPString), the next object starts there, as it does in Bouncy Castle. +//! +//! Bouncy Castle sets no limit on how deep objects may nest, and neither does +//! this. It keeps a stack of its own of the objects it is inside, and works +//! out where each stream stands from how far into the signature it has read, +//! where Bouncy Castle passes each byte through every stream in turn, so a +//! signature is read in time that grows with its length, however deep it goes. + +use std::collections::BTreeSet; + +/// Bouncy Castle threw. rskj treats every way it throws alike. +struct Refused; + +type Read = Result; + +// Bouncy Castle's `BERTags`: the bits of a tag byte, and the universal tag +// numbers its readers know. +const CONSTRUCTED: i32 = 0x20; +const APPLICATION: i32 = 0x40; +const TAGGED: i32 = 0x80; +const BOOLEAN: i32 = 0x01; +const INTEGER: i32 = 0x02; +const BIT_STRING: i32 = 0x03; +const OCTET_STRING: i32 = 0x04; +const NULL: i32 = 0x05; +const OBJECT_IDENTIFIER: i32 = 0x06; +const EXTERNAL: i32 = 0x08; +const ENUMERATED: i32 = 0x0a; +const UTF8_STRING: i32 = 0x0c; +const SEQUENCE: i32 = 0x10; +const SET: i32 = 0x11; +const NUMERIC_STRING: i32 = 0x12; +const PRINTABLE_STRING: i32 = 0x13; +const T61_STRING: i32 = 0x14; +const VIDEOTEX_STRING: i32 = 0x15; +const IA5_STRING: i32 = 0x16; +const UTC_TIME: i32 = 0x17; +const GENERALIZED_TIME: i32 = 0x18; +const GRAPHIC_STRING: i32 = 0x19; +const VISIBLE_STRING: i32 = 0x1a; +const GENERAL_STRING: i32 = 0x1b; +const UNIVERSAL_STRING: i32 = 0x1c; +const BMP_STRING: i32 = 0x1e; + +/// What a stream reads from: the signature's bytes, or another stream. +#[derive(Clone, Copy)] +enum Source { + Bytes, + Stream(usize), +} + +/// One of Bouncy Castle's limited input streams. Every byte a stream gives +/// comes from the signature through each stream under it, in order, so where a +/// stream stands follows from how many of the signature's bytes have been read, +/// and is not counted byte by byte. +#[derive(Clone, Copy)] +struct Stream { + /// The stream it reads from. + inner: Source, + /// Its place among the streams open. + slot: usize, + /// How far the next byte it gives lies behind the next byte of the + /// signature: two bytes for each indefinite stream it reads through, + /// itself among them, as each holds two bytes ahead of what it has given. + lag: usize, + /// Where the first to end of the definite streams under it ends. + first_end_below: usize, + kind: Kind, +} + +#[derive(Clone, Copy)] +enum Kind { + /// `DefiniteLengthInputStream`, which has nothing left once the signature + /// has been read up to `end`. + Definite { end: usize }, + /// `IndefiniteLengthInputStream`, which ends at the end-of-contents octets + /// `00 00` in its two bytes of look-ahead, and looks for them only while + /// `eof_on_00`. + Indefinite { eof_on_00: bool, eof_reached: bool }, +} + +/// An object, as far as what reads it asks: `decodeFromDER` (the outer +/// sequence, and its first two elements as integers), `DERExternal` (an object +/// identifier, an integer, a tagged object's number) and a constructed OCTET +/// STRING (its parts). +enum Object { + /// An INTEGER of `len` bytes, at `at` in the signature. + Integer { + at: usize, + len: usize, + }, + /// An OBJECT IDENTIFIER. + Oid, + /// A primitive OCTET STRING, a `DEROctetString`. + DerOctetString, + /// A constructed one, a `BEROctetString`. + BerOctetString, + Tagged(i32), + /// The outer object, a SEQUENCE of a definite length with elements, which + /// `DERFactory.createSequence` makes a `DLSequence`. + DlSequence(Vec), + Other, +} + +/// `ASN1StreamParser`: a stream to read objects from, and the limit their +/// lengths are held to. +#[derive(Clone, Copy)] +struct Parser { + source: Source, + limit: i32, +} + +/// What `ASN1StreamParser.readObject` gives: an object, or a parser of one, +/// which reads its contents when it is loaded. +enum Parsed { + Loaded(Object), + /// `DEROctetStringParser`, over a definite stream. + DerOctetString(usize), + /// `BEROctetStringParser`. + BerOctetString(Parser), + /// The parsers of the sequences and sets, and + /// `BERApplicationSpecificParser`: a vector of objects. + Vector(Parser), + /// `DERExternalParser`. + External(Parser), + /// `BERTaggedObjectParser`. + Tagged { + constructed: bool, + tag_no: i32, + parser: Parser, + }, +} + +/// A constructed object whose contents are being read, and what has been read +/// of them. +enum Frame { + /// `ASN1InputStream.buildObject` of a constructed OCTET STRING, SEQUENCE, + /// SET or EXTERNAL of a definite length, whose contents an + /// `ASN1InputStream` of their own reads, held to their length + /// (`buildDEREncodableVector`). `top` marks the outer object. + Built { + inside: Source, + limit: i32, + tag_no: i32, + top: bool, + items: Vec, + }, + /// `ASN1StreamParser.readVector`, each object loaded: the contents of a + /// sequence, a set, an application-specific object, an EXTERNAL or a + /// constructed tagged object that a parser reads. + Vector { + parser: Parser, + makes: Makes, + items: Vec, + }, + /// `ConstructedOctetStream`: the parts of a constructed OCTET STRING that a + /// parser reads, each read to its end (`BEROctetStringParser`). + Octets(Parser), +} + +/// What a `Frame::Vector` makes of the objects it read. +#[derive(Clone, Copy)] +enum Makes { + Other, + External, + Tagged(i32), +} + +/// What reading the next object in a frame's contents gives. +enum Step { + /// The object, read whole. + Done(Object), + /// A constructed object, whose contents are read next. + Open(Frame), + /// The end of the frame's contents. + End, +} + +impl Frame { + fn vector(parser: Parser, makes: Makes) -> Self { + Frame::Vector { + parser, + makes, + items: Vec::new(), + } + } + + fn add(&mut self, object: Object) { + match self { + Frame::Built { items, .. } | Frame::Vector { items, .. } => items.push(object), + Frame::Octets(_) => {} + } + } + + /// The object, its contents read. + fn finish(self) -> Read { + match self { + Frame::Built { + tag_no, top, items, .. + } => match tag_no { + // `new BEROctetString(ASN1OctetString[])`, which casts each part + // to a `DEROctetString`. + OCTET_STRING + if items + .iter() + .all(|item| matches!(item, Object::DerOctetString)) => + { + Ok(Object::BerOctetString) + } + SEQUENCE if top && !items.is_empty() => Ok(Object::DlSequence(items)), + SEQUENCE | SET => Ok(Object::Other), + EXTERNAL => external(&items).map(|()| Object::Other), + _ => Err(Refused), + }, + Frame::Vector { + makes: Makes::Other, + .. + } => Ok(Object::Other), + Frame::Vector { + makes: Makes::External, + items, + .. + } => external(&items).map(|()| Object::Other), + Frame::Vector { + makes: Makes::Tagged(tag_no), + .. + } => Ok(Object::Tagged(tag_no)), + Frame::Octets(_) => Ok(Object::BerOctetString), + } + } +} + +/// How many bytes a primitive holds, and the first two of them. +#[derive(Default)] +struct Content { + len: usize, + head: [u8; 2], +} + +impl Content { + /// `ASN1Integer.isMalformed`: a first byte that only repeats the sign of + /// the next. + fn malformed(&self) -> bool { + let [first, next] = self.head; + self.len > 1 && ((first == 0x00 && next < 0x80) || (first == 0xff && next >= 0x80)) + } +} + +struct Reader<'a> { + bytes: &'a [u8], + /// How many of the signature's bytes have been read. + pos: usize, + streams: Vec, + /// The streams open, each reading from the one before it, the first from + /// the signature's bytes. Bouncy Castle reads from a stream only once it is + /// done with those over it, so reading from one closes them. + open: Vec, + /// The slots of the indefinite streams open that look for `00 00`, or have + /// found them. + watching: BTreeSet, +} + +impl<'a> Reader<'a> { + fn new(bytes: &'a [u8]) -> Self { + Reader { + bytes, + pos: 0, + streams: Vec::new(), + open: Vec::new(), + watching: BTreeSet::new(), + } + } + + /// `new ASN1InputStream(bytes).readObject()`: the outer object, read whole. + /// Bouncy Castle reads an object inside another by calling itself; here + /// each constructed object being read is a frame on a stack, and its + /// contents are read in the same order. + fn signature(&mut self) -> Read { + let limit = i32::try_from(self.bytes.len()).unwrap_or(i32::MAX); + let mut frames: Vec = Vec::new(); + let mut step = self.object(Source::Bytes, limit, true)?; + loop { + let object = match step { + Step::Open(frame) => { + frames.push(frame); + None + } + Step::Done(object) => Some(object), + // With no frame open, the bytes held no object, and bitcoinj + // throws. + Step::End => Some(frames.pop().ok_or(Refused)?.finish()?), + }; + if let Some(object) = object { + let Some(frame) = frames.last_mut() else { + return Ok(object); + }; + frame.add(object); + } + let Some(frame) = frames.last() else { + return Err(Refused); + }; + step = self.next(frame)?; + } + } + + /// The next object in `frame`'s contents, or their end. + fn next(&mut self, frame: &Frame) -> Read { + match *frame { + Frame::Built { inside, limit, .. } => self.object(inside, limit, false), + Frame::Vector { parser, .. } => match self.parsed(parser)? { + Some(parsed) => self.load(parsed), + None => Ok(Step::End), + }, + // `ConstructedOctetStream`, which casts each part to an + // `ASN1OctetStringParser`. + Frame::Octets(parser) => match self.parsed(parser)? { + Some(Parsed::DerOctetString(def)) => { + self.drain(def).map(|_| Step::Done(Object::DerOctetString)) + } + Some(Parsed::BerOctetString(inner)) => Ok(Step::Open(Frame::Octets(inner))), + Some(_) => Err(Refused), + None => Ok(Step::End), + }, + } + } + + /// `read()` of `source`: its next byte, or -1 at its end. A stream throws + /// `EOFException` where one under it has ended, as where the signature has. + fn read(&mut self, source: Source) -> Read { + self.close_above(source); + let Source::Stream(i) = source else { + let byte = self.byte_at(self.pos); + self.pos += usize::from(byte >= 0); + return Ok(byte); + }; + if self.at_end(i) { + return Ok(-1); + } + if self.pos >= self.bytes.len() || self.ended_below(i) { + return Err(Refused); + } + let byte = self.byte_at(self.pos.checked_sub(self.streams[i].lag).ok_or(Refused)?); + self.pos += 1; + Ok(byte) + } + + /// The signature's byte at `at`, or -1 past its end. + fn byte_at(&self, at: usize) -> i32 { + self.bytes.get(at).map_or(-1, |&byte| i32::from(byte)) + } + + /// Whether stream `i` has come to its end: a definite stream with nothing + /// left, or an indefinite one at `00 00` while it looks for them. + fn at_end(&mut self, i: usize) -> bool { + match self.streams[i].kind { + Kind::Definite { end } => end <= self.pos, + Kind::Indefinite { .. } => self.check_for_eof(i), + } + } + + /// Whether a stream under stream `i` has ended, so that it gives -1, on + /// which the stream over it throws. (What `checkForEof` marks on the way + /// no longer matters once the read throws.) + fn ended_below(&self, i: usize) -> bool { + let Stream { + slot, + first_end_below, + .. + } = self.streams[i]; + first_end_below <= self.pos + || self.watching.range(..slot).any(|&below| { + self.open + .get(below) + .is_some_and(|&j| match self.streams[j].kind { + Kind::Indefinite { + eof_on_00, + eof_reached, + } => eof_reached || (eof_on_00 && self.look_ahead(j) == [0, 0]), + Kind::Definite { .. } => false, + }) + }) + } + + /// The two bytes indefinite stream `i` holds ahead of what it has given. + fn look_ahead(&self, i: usize) -> [i32; 2] { + let at = self.pos.wrapping_sub(self.streams[i].lag); + [self.byte_at(at), self.byte_at(at.wrapping_add(1))] + } + + /// `IndefiniteLengthInputStream.checkForEof`: whether stream `i` is at its + /// end, which, the first time it finds `00 00` while it looks for them, it + /// marks, and tells the stream it reads from (`setParentEofDetect`). + fn check_for_eof(&mut self, i: usize) -> bool { + let Kind::Indefinite { + eof_on_00, + eof_reached, + } = self.streams[i].kind + else { + return false; + }; + if eof_reached || !eof_on_00 || self.look_ahead(i) != [0, 0] { + return eof_reached; + } + self.mark_eof_reached(i); + self.eof_detect(self.streams[i].inner); + true + } + + /// `setParentEofDetect(true)` of a stream that reads from `source`: an + /// indefinite stream there looks for `00 00` again, and if it is at them, + /// it has ended, and tells the one it reads from in turn. + fn eof_detect(&mut self, mut source: Source) { + while let Source::Stream(j) = source { + let Kind::Indefinite { eof_reached, .. } = self.streams[j].kind else { + return; + }; + self.set_eof_on_00(j, true); + if eof_reached || self.look_ahead(j) != [0, 0] { + return; + } + self.mark_eof_reached(j); + source = self.streams[j].inner; + } + } + + /// `setEofOn00` of indefinite stream `i`. + fn set_eof_on_00(&mut self, i: usize, on: bool) { + let slot = self.streams[i].slot; + if let Kind::Indefinite { + eof_on_00, + eof_reached, + } = &mut self.streams[i].kind + { + *eof_on_00 = on; + if on || *eof_reached { + self.watching.insert(slot); + } else { + self.watching.remove(&slot); + } + } + } + + fn mark_eof_reached(&mut self, i: usize) { + if let Kind::Indefinite { eof_reached, .. } = &mut self.streams[i].kind { + *eof_reached = true; + } + } + + /// Closes the streams over `source`, which Bouncy Castle is done with once + /// it reads from `source`. A definite stream that came to its end told the + /// stream it reads from, when it did, that that one may end at `00 00` + /// (`setParentEofDetect`). No byte is read between its end and its + /// closing, so the stream it tells holds the same two bytes then as now, + /// and here it tells it as it is closed. + fn close_above(&mut self, source: Source) { + let keep = match source { + Source::Bytes => 0, + Source::Stream(i) => self.streams[i].slot + 1, + }; + while self.open.len() > keep { + let Some(j) = self.open.pop() else { break }; + self.watching.remove(&self.open.len()); + let Stream { inner, kind, .. } = self.streams[j]; + if matches!(kind, Kind::Definite { end } if end <= self.pos) { + self.eof_detect(inner); + } + } + } + + /// Opens a stream of `kind` over `inner`. + fn open_stream(&mut self, inner: Source, kind: Kind) -> usize { + self.close_above(inner); + let (lag, first_end_below) = match inner { + Source::Bytes => (0, usize::MAX), + Source::Stream(j) => { + let Stream { + lag, + first_end_below, + kind, + .. + } = self.streams[j]; + let end = match kind { + Kind::Definite { end } => end, + Kind::Indefinite { .. } => usize::MAX, + }; + (lag, first_end_below.min(end)) + } + }; + let look_ahead = if matches!(kind, Kind::Indefinite { .. }) { + 2 + } else { + 0 + }; + let i = self.streams.len(); + self.streams.push(Stream { + inner, + slot: self.open.len(), + lag: lag + look_ahead, + first_end_below, + kind, + }); + self.open.push(i); + i + } + + /// A `DefiniteLengthInputStream` of the next `length` bytes `inner` gives. + fn definite(&mut self, inner: Source, length: usize) -> usize { + let end = self.pos.saturating_add(length); + self.open_stream(inner, Kind::Definite { end }) + } + + /// An `IndefiniteLengthInputStream` over `inner`, which reads its two bytes + /// of look-ahead as it is made. + fn indefinite(&mut self, inner: Source) -> Read { + self.read(inner)?; + if self.read(inner)? < 0 { + return Err(Refused); + } + let i = self.open_stream( + inner, + Kind::Indefinite { + eof_on_00: true, + eof_reached: false, + }, + ); + self.watching.insert(self.streams[i].slot); + self.check_for_eof(i); + Ok(i) + } + + /// `getRemaining` of definite stream `def`. + fn remaining(&self, def: usize) -> usize { + match self.streams[def].kind { + Kind::Definite { end } => end.saturating_sub(self.pos), + Kind::Indefinite { .. } => 0, + } + } + + /// `DefiniteLengthInputStream.toByteArray`: every byte the definite stream + /// has left, or `EOFException` where what it wraps ends first. + fn drain(&mut self, def: usize) -> Read { + let mut content = Content::default(); + while self.remaining(def) > 0 { + let b = u8::try_from(self.read(Source::Stream(def))?).map_err(|_| Refused)?; + if let Some(slot) = content.head.get_mut(content.len) { + *slot = b; + } + content.len += 1; + } + Ok(content) + } + + /// `ASN1InputStream.readTagNumber`: the number in the tag byte's five bits, + /// or where they are all set, in the bytes after it, seven bits a byte, in + /// a Java `int` that drops what it shifts out. + fn tag_number(&mut self, source: Source, tag: i32) -> Read { + let mut tag_no = tag & 0x1f; + if tag_no == 0x1f { + tag_no = 0; + let mut b = self.read(source)?; + if b & 0x7f == 0 { + return Err(Refused); + } + while b >= 0 && b & 0x80 != 0 { + tag_no |= b & 0x7f; + tag_no = tag_no.wrapping_shl(7); + b = self.read(source)?; + } + if b < 0 { + return Err(Refused); + } + tag_no |= b & 0x7f; + } + Ok(tag_no) + } + + /// `ASN1InputStream.readLength`: -1 for the indefinite form, and a long + /// form of at most four bytes, read into a Java `int`, held below `limit`. + fn length(&mut self, source: Source, limit: i32) -> Read { + let first = self.read(source)?; + if first < 0 { + return Err(Refused); + } + if first == 0x80 { + return Ok(-1); + } + if first < 0x80 { + return Ok(first); + } + let size = first & 0x7f; + if size > 4 { + return Err(Refused); + } + let mut length: i32 = 0; + for _ in 0..size { + let next = self.read(source)?; + if next < 0 { + return Err(Refused); + } + length = length.wrapping_shl(8).wrapping_add(next); + } + if length < 0 || length >= limit { + return Err(Refused); + } + Ok(length) + } + + /// `ASN1InputStream.readObject` from `source`, whose lengths `limit` holds: + /// the next object, or the end. `top` marks the outer object. + fn object(&mut self, source: Source, limit: i32, top: bool) -> Read { + let tag = self.read(source)?; + if tag < 0 { + return Ok(Step::End); + } + // A tag byte of 0 is an end-of-contents marker where no indefinite + // length is open. + if tag == 0 { + return Err(Refused); + } + let tag_no = self.tag_number(source, tag)?; + let constructed = tag & CONSTRUCTED != 0; + let length = self.length(source, limit)?; + let Ok(len) = usize::try_from(length) else { + if !constructed { + return Err(Refused); + } + let parser = Parser { + source: Source::Stream(self.indefinite(source)?), + limit, + }; + let parsed = if tag & APPLICATION != 0 { + Parsed::Vector(parser) + } else if tag & TAGGED != 0 { + Parsed::Tagged { + constructed: true, + tag_no, + parser, + } + } else { + indefinite_universal(tag_no, parser)? + }; + return self.load(parsed); + }; + let def = self.definite(source, len); + let inside = Source::Stream(def); + if tag & APPLICATION != 0 { + self.drain(def)?; + return Ok(Step::Done(Object::Other)); + } + if tag & TAGGED != 0 { + return self.tagged( + Parser { + source: inside, + limit: length, + }, + constructed, + tag_no, + ); + } + if !constructed { + return self.primitive(def, tag_no).map(Step::Done); + } + if !matches!(tag_no, OCTET_STRING | SEQUENCE | SET | EXTERNAL) { + return Err(Refused); + } + Ok(Step::Open(Frame::Built { + inside, + limit: length, + tag_no, + top, + items: Vec::new(), + })) + } + + /// `ASN1InputStream.createPrimitiveDERObject`: the primitive the definite + /// stream `def` holds. + fn primitive(&mut self, def: usize, tag_no: i32) -> Read { + match tag_no { + BIT_STRING => { + // `ASN1BitString.fromInputStream`: a byte that counts the + // unused bits, at most 7, and none where no data follows. + let data = self.remaining(def).checked_sub(1).ok_or(Refused)?; + let pad = self.read(Source::Stream(def))?; + self.drain(def)?; + if pad > 7 || (data == 0 && pad != 0) { + return Err(Refused); + } + Ok(Object::Other) + } + BMP_STRING => { + // `getBMPCharBuffer`: two bytes a character, an odd one left + // unread. + for _ in 0..self.remaining(def) / 2 { + self.read(Source::Stream(def))?; + self.read(Source::Stream(def))?; + } + Ok(Object::Other) + } + BOOLEAN => match self.drain(def)?.len { + 1 => Ok(Object::Other), + _ => Err(Refused), + }, + ENUMERATED => { + let content = self.drain(def)?; + if content.len == 0 || content.malformed() { + return Err(Refused); + } + Ok(Object::Other) + } + INTEGER => { + let at = self.pos.saturating_sub(self.streams[def].lag); + let content = self.drain(def)?; + if content.malformed() { + return Err(Refused); + } + Ok(Object::Integer { + at, + len: content.len, + }) + } + // `DERNull.INSTANCE`, its contents not read. + NULL => Ok(Object::Other), + OBJECT_IDENTIFIER => self.drain(def).map(|_| Object::Oid), + OCTET_STRING => self.drain(def).map(|_| Object::DerOctetString), + GENERALIZED_TIME | GENERAL_STRING | IA5_STRING | NUMERIC_STRING | PRINTABLE_STRING + | T61_STRING | UNIVERSAL_STRING | UTC_TIME | UTF8_STRING | VISIBLE_STRING + | GRAPHIC_STRING | VIDEOTEX_STRING => self.drain(def).map(|_| Object::Other), + _ => Err(Refused), + } + } + + /// `ASN1StreamParser.readObject`: the next object `parser` holds, or none at + /// its end. + fn parsed(&mut self, parser: Parser) -> Read> { + let tag = self.read(parser.source)?; + if tag < 0 { + return Ok(None); + } + // `set00Check(false)`: an indefinite stream doesn't end at `00 00` + // inside an object it is reading. + if let Source::Stream(j) = parser.source { + self.set_eof_on_00(j, false); + } + let tag_no = self.tag_number(parser.source, tag)?; + let constructed = tag & CONSTRUCTED != 0; + let length = self.length(parser.source, parser.limit)?; + let Ok(len) = usize::try_from(length) else { + if !constructed { + return Err(Refused); + } + let inner = Parser { + source: Source::Stream(self.indefinite(parser.source)?), + limit: parser.limit, + }; + return Ok(Some(if tag & APPLICATION != 0 { + Parsed::Vector(inner) + } else if tag & TAGGED != 0 { + Parsed::Tagged { + constructed: true, + tag_no, + parser: inner, + } + } else { + indefinite_universal(tag_no, inner)? + })); + }; + let def = self.definite(parser.source, len); + let inside = Parser { + source: Source::Stream(def), + limit: length, + }; + if tag & APPLICATION != 0 { + self.drain(def)?; + return Ok(Some(Parsed::Loaded(Object::Other))); + } + if tag & TAGGED != 0 { + return Ok(Some(Parsed::Tagged { + constructed, + tag_no, + parser: inside, + })); + } + if constructed { + return match tag_no { + OCTET_STRING => Ok(Some(Parsed::BerOctetString(inside))), + SEQUENCE | SET => Ok(Some(Parsed::Vector(inside))), + EXTERNAL => Ok(Some(Parsed::External(inside))), + _ => Err(Refused), + }; + } + if tag_no == OCTET_STRING { + return Ok(Some(Parsed::DerOctetString(def))); + } + self.primitive(def, tag_no) + .map(|object| Some(Parsed::Loaded(object))) + } + + /// `getLoadedObject` of what `parsed` is: read whole, or opened to read. + fn load(&mut self, parsed: Parsed) -> Read { + match parsed { + Parsed::Loaded(object) => Ok(Step::Done(object)), + Parsed::DerOctetString(def) => { + self.drain(def).map(|_| Step::Done(Object::DerOctetString)) + } + Parsed::BerOctetString(parser) => Ok(Step::Open(Frame::Octets(parser))), + Parsed::Vector(parser) => Ok(Step::Open(Frame::vector(parser, Makes::Other))), + Parsed::External(parser) => Ok(Step::Open(Frame::vector(parser, Makes::External))), + Parsed::Tagged { + constructed, + tag_no, + parser, + } => self.tagged(parser, constructed, tag_no), + } + } + + /// `ASN1StreamParser.readTaggedObject`: a primitive's bytes, or the objects + /// a constructed one holds. + fn tagged(&mut self, parser: Parser, constructed: bool, tag_no: i32) -> Read { + if constructed { + return Ok(Step::Open(Frame::vector(parser, Makes::Tagged(tag_no)))); + } + if let Source::Stream(def) = parser.source { + self.drain(def)?; + } + Ok(Step::Done(Object::Tagged(tag_no))) + } +} + +/// What an indefinite length may hold of the universal tags (`readIndef`). +fn indefinite_universal(tag_no: i32, parser: Parser) -> Read { + match tag_no { + EXTERNAL => Ok(Parsed::External(parser)), + OCTET_STRING => Ok(Parsed::BerOctetString(parser)), + SEQUENCE | SET => Ok(Parsed::Vector(parser)), + _ => Err(Refused), + } +} + +/// `DERExternal(ASN1EncodableVector)`: an object identifier, an integer and an +/// object that isn't tagged, each if it is there, then a tagged object +/// numbered 0, 1 or 2, and nothing after it. +fn external(items: &[Object]) -> Read<()> { + let mut offset = 0; + if matches!(items.get(offset), Some(Object::Oid)) { + offset += 1; + } + if matches!(items.get(offset), Some(Object::Integer { .. })) { + offset += 1; + } + if matches!(items.get(offset), Some(item) if !matches!(item, Object::Tagged(_))) { + offset += 1; + } + match items.get(offset) { + Some(Object::Tagged(tag_no)) if items.len() == offset + 1 && (0..=2).contains(tag_no) => { + Ok(()) + } + _ => Err(Refused), + } +} + +/// The two integers of a signature as bitcoinj's +/// `BtcECKey.ECDSASignature.decodeFromDER` reads them, or `None` where it +/// throws: the outer object must be a SEQUENCE of a definite length whose +/// first two elements are INTEGERs, each read as the unsigned number its bytes +/// spell (`getPositiveValue`), so that a negative encoding is read as a +/// positive number, not refused, and an INTEGER of no bytes as 0. +pub(crate) fn der_signature_integers(sig: &[u8]) -> Option<(&[u8], &[u8])> { + let Ok(Object::DlSequence(items)) = Reader::new(sig).signature() else { + return None; + }; + let integer = |item: Option<&Object>| match item { + Some(&Object::Integer { at, len }) => sig.get(at..at.checked_add(len)?), + _ => None, + }; + Some((integer(items.first())?, integer(items.get(1))?)) +} + +#[cfg(test)] +mod tests { + use super::der_signature_integers; + use alloy_primitives::hex; + + /// Signatures, each with the two integers `decodeFromDER` reads from it, or + /// `None` where it throws, as the release jar of rskj 9.0.4 reads them. + const CASES: &[(&str, &str, Option<(&str, &str)>)] = &[ + ( + "two integers in a sequence, as federators sign", + "3006020105020107", + Some(("05", "07")), + ), + ( + "integers of 33 and 32 bytes", + "3045022100c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1\ + 02207f11111111111111111111111111111111111111111111111111111111111111", + Some(( + "00c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1", + "7f11111111111111111111111111111111111111111111111111111111111111", + )), + ), + ( + "negative encodings, read as unsigned", + "30060201800201ff", + Some(("80", "ff")), + ), + ( + "integers of no bytes, read as 0", + "300402000200", + Some(("", "")), + ), + ( + "a first byte 00 that only repeats the sign", + "300702020005020107", + None, + ), + ( + "a first byte ff that only repeats the sign", + "30070202ff80020107", + None, + ), + ( + "the outer length in the long form", + "308106020105020107", + Some(("05", "07")), + ), + ( + "an INTEGER tag in the high-tag-number form", + "30071f020105020107", + Some(("05", "07")), + ), + ( + "a tag number past a Java int, which keeps its low bits", + "300c1f8180808080020105020107", + Some(("05", "07")), + ), + ( + "bytes after the sequence", + "3006020105020107ffff", + Some(("05", "07")), + ), + ("r alone", "3003020105", None), + ("an empty sequence", "3000", None), + ( + "a sequence of an indefinite length", + "30800201050201070000", + None, + ), + ("a set", "3106020105020107", None), + ("no bytes", "", None), + ("a NULL after s", "30080201050201070500", Some(("05", "07"))), + ( + "a NULL that claims a byte, which is never read", + "30080201050201070501", + Some(("05", "07")), + ), + ( + "a NULL whose byte is read as the next tag", + "3009020105020107050100", + None, + ), + ( + "a BMPString whose odd byte begins the next object", + "300c0201050201071e0300410500", + Some(("05", "07")), + ), + ( + "end-of-contents octets where no indefinite length is open", + "30080201050201070000", + None, + ), + ("a BOOLEAN of two bytes", "300a0201050201070102ffff", None), + ( + "a BIT STRING with 8 unused bits", + "300a020105020107030208ff", + None, + ), + ( + "a BIT STRING of no data that has unused bits", + "3009020105020107030101", + None, + ), + ( + "a BIT STRING of no data", + "3009020105020107030100", + Some(("05", "07")), + ), + ("an ENUMERATED of no bytes", "30080201050201070a00", None), + ( + "an object identifier of any bytes", + "300b0201050201070603ffffff", + Some(("05", "07")), + ), + ( + "a universal tag no reader knows", + "30080201050201070700", + None, + ), + ("a constructed INTEGER", "30080201050201072200", None), + ( + "a length in five bytes", + "300e02010502010704850000000001aa", + None, + ), + ( + "a long-form length as long as the limit", + "3009020105020107058109", + None, + ), + ( + "a short-form length past the limit, which is not held to it", + "30080201050201070508", + Some(("05", "07")), + ), + ( + "a length past the sequence", + "300a0201050201070405aabb", + None, + ), + ( + "a sequence of an indefinite length inside", + "300a02010502010730800000", + Some(("05", "07")), + ), + ( + "an indefinite length that never ends", + "30080201050201073080", + None, + ), + ( + "00 00 inside an object inside an indefinite length", + "300e0201050201073080040200000000", + Some(("05", "07")), + ), + ( + "a NULL that claims a byte, inside an indefinite length", + "300c020105020107308005010000", + None, + ), + ( + "an empty definite object just before the end-of-contents", + "300c020105020107308030000000", + Some(("05", "07")), + ), + ( + "an indefinite length whose look-ahead runs past its definite one", + "300b0201050201073003308005", + None, + ), + ( + "an indefinite OCTET STRING of OCTET STRINGs", + "300f02010502010724800401aa04000000", + Some(("05", "07")), + ), + ( + "one with a part that is not an OCTET STRING", + "300c020105020107248005000000", + None, + ), + ( + "one with a part of parts", + "3011020105020107248024800401aa00000000", + Some(("05", "07")), + ), + ( + "a definite OCTET STRING of OCTET STRINGs", + "300d02010502010724050401aa0400", + Some(("05", "07")), + ), + ( + "one with a constructed part", + "300c020105020107240424020400", + None, + ), + ( + "an EXTERNAL", + "300c0201050201072804a0020500", + Some(("05", "07")), + ), + ( + "an EXTERNAL with an identifier and an integer", + "3012020105020107280a06012a020101a0020500", + Some(("05", "07")), + ), + ( + "an EXTERNAL whose tagged object is numbered 3", + "300c0201050201072804a3020500", + None, + ), + ( + "an EXTERNAL with an object after its tagged one", + "300e0201050201072806a00205000500", + None, + ), + ( + "an EXTERNAL of an indefinite length", + "300e0201050201072880a10205000000", + Some(("05", "07")), + ), + ( + "an application-specific object, its contents not read", + "30090201050201076101ff", + Some(("05", "07")), + ), + ( + "one of an indefinite length, its contents read", + "300c020105020107608005000000", + Some(("05", "07")), + ), + ( + "a private tag, read as application-specific", + "3009020105020107c101ff", + Some(("05", "07")), + ), + ( + "a primitive tagged object", + "30090201050201078001ff", + Some(("05", "07")), + ), + ( + "a tagged object whose contents are not an object", + "3009020105020107a001ff", + None, + ), + ( + "a tagged object of an indefinite length", + "300c020105020107a08005000000", + Some(("05", "07")), + ), + ( + "a private constructed tag, read as application-specific", + "3009020105020107e101ff", + Some(("05", "07")), + ), + ( + "a SET of an indefinite length inside", + "300a02010502010731800000", + Some(("05", "07")), + ), + ( + "a primitive of an indefinite length", + "300a02010502010704800000", + None, + ), + ( + "a high tag number whose first byte holds no bits", + "300a0201050201071f800500", + None, + ), + ( + "a length past the sequence, into bytes after it", + "300a0201050201070405aabbccddee", + None, + ), + ( + "an empty indefinite SEQUENCE inside another", + "300e0201050201073080308000000000", + Some(("05", "07")), + ), + ( + "a primitive of an indefinite length inside an indefinite length", + "300e0201050201073080048000000000", + None, + ), + ]; + + fn read(sig: &[u8]) -> Option<(String, String)> { + der_signature_integers(sig).map(|(r, s)| (hex::encode(r), hex::encode(s))) + } + + #[test] + fn reads_a_signature_as_bouncy_castle_1_58_does() { + for &(what, sig, integers) in CASES { + let expected = integers.map(|(r, s)| (r.to_owned(), s.to_owned())); + assert_eq!(read(&hex::decode(sig).unwrap()), expected, "{what}"); + } + } + + /// A tag byte and a definite length. + fn header(tag: u8, len: usize) -> Vec { + match u8::try_from(len) { + Ok(short) if short < 0x80 => vec![tag, short], + _ => { + let bytes = len.to_be_bytes(); + let skip = bytes.iter().take_while(|&&byte| byte == 0).count(); + let size = u8::try_from(bytes.len() - skip).unwrap(); + [&[tag, 0x80 | size][..], &bytes[skip..]].concat() + } + } + } + + /// A signature of `r` 5 and `s` 7, then `nest`. + fn signature(nest: &[u8]) -> Vec { + let content = [&[0x02, 0x01, 0x05, 0x02, 0x01, 0x07][..], nest].concat(); + [header(0x30, content.len()), content].concat() + } + + /// As deep as a signature goes, each checked against the release jar. + #[test] + fn reads_as_deep_as_a_signature_goes() { + let read_5_and_7 = Some(("05".to_owned(), "07".to_owned())); + // 45,000 SEQUENCEs of an indefinite length, one inside another, + // around a NULL: 180 kB. + let depth = 45_000; + let nest = [ + b"\x30\x80".repeat(depth), + b"\x05\x00".to_vec(), + b"\x00\x00".repeat(depth), + ] + .concat(); + assert_eq!(read(&signature(&nest)), read_5_and_7); + // The same, its last end-of-contents octets left off. + assert_eq!(read(&signature(&nest[..nest.len() - 2])), None); + // 20,000 SEQUENCEs of a definite length. + let mut headers = Vec::new(); + let mut inner = 2; + for _ in 0..20_000 { + let header = header(0x30, inner); + inner += header.len(); + headers.push(header); + } + let nest = [ + headers.into_iter().rev().flatten().collect(), + b"\x05\x00".to_vec(), + ] + .concat(); + assert_eq!(read(&signature(&nest)), read_5_and_7); + // 30,000 OCTET STRINGs of an indefinite length, each a part of the + // one around it. + let depth = 30_000; + let nest = [ + b"\x24\x80".repeat(depth), + b"\x04\x01\xaa".to_vec(), + b"\x00\x00".repeat(depth), + ] + .concat(); + assert_eq!(read(&signature(&nest)), read_5_and_7); + } +} diff --git a/crates/execution/src/bridge/events.rs b/crates/execution/src/bridge/events.rs index 5814f75..0651053 100644 --- a/crates/execution/src/bridge/events.rs +++ b/crates/execution/src/bridge/events.rs @@ -73,7 +73,8 @@ pub fn log_legacy_release_btc( /// (rskj BrigeEventLoggerLegacyImpl.logCommitFederation): /// data = RLP[ RLP[oldFedAddressHash160, RLP[oldKeys...]], /// RLP[newFedAddressHash160, RLP[newKeys...]], -/// activationBlockNumber as a DECIMAL ASCII string ]. +/// activationBlockNumber as a DECIMAL ASCII string ], +/// each key its encoding (`getPubKey()`), `00` for the point at infinity. pub fn log_legacy_commit_federation( ctx: &mut CTX, old_fed_hash160: &[u8; 20], @@ -82,9 +83,13 @@ pub fn log_legacy_commit_federation( new_keys: &[[u8; 33]], activation_block: u64, ) { + use super::federation::key_encoding; use super::serialization::{rlp_encode_element, rlp_encode_list}; let fed_data = |hash160: &[u8; 20], keys: &[[u8; 33]]| { - let key_items: Vec> = keys.iter().map(|k| rlp_encode_element(k)).collect(); + let key_items: Vec> = keys + .iter() + .map(|k| rlp_encode_element(key_encoding(k))) + .collect(); rlp_encode_list(&[rlp_encode_element(hash160), rlp_encode_list(&key_items)]) }; let data = rlp_encode_list(&[ @@ -182,8 +187,10 @@ pub fn log_solidity_release_btc( /// oldFederationBtcAddress, bytes newFederationBtcPublicKeys, string /// newFederationBtcAddress, int256 activationHeight)` — RSKIP146 federation /// commit event (rskj logCommitFederationInSolidityFormat). The key bytes -/// are the federation's sorted compressed BTC pubkeys flat-concatenated; -/// the addresses are Base58Check P2SH strings. +/// are the federation's sorted BTC pubkeys flat-concatenated, each its +/// encoding (`flatKeysAsByteArray` of `getPubKey()`): 33 compressed bytes, +/// or the one byte `00` for the point at infinity; the addresses are +/// Base58Check P2SH strings. pub fn log_solidity_commit_federation( ctx: &mut CTX, old_keys: &[[u8; 33]], @@ -212,8 +219,13 @@ fn commit_federation_event_data( new_fed_address: &str, activation_block: u64, ) -> Vec { - let old_flat = old_keys.concat(); - let new_flat = new_keys.concat(); + let flat = |keys: &[[u8; 33]]| -> Vec { + keys.iter() + .flat_map(|key| super::federation::key_encoding(key).iter().copied()) + .collect() + }; + let old_flat = flat(old_keys); + let new_flat = flat(new_keys); let dynamics: [&[u8]; 4] = [ &old_flat, old_fed_address.as_bytes(), diff --git a/crates/execution/src/bridge/federation.rs b/crates/execution/src/bridge/federation.rs index f573c9d..dd193df 100644 --- a/crates/execution/src/bridge/federation.rs +++ b/crates/execution/src/bridge/federation.rs @@ -209,7 +209,9 @@ fn address_cache() -> &'static std::sync::RwLock< /// RSK address of a federation member from its secp256k1 public key /// (compressed or uncompressed): keccak256 of the uncompressed point's -/// 64 coordinate bytes, last 20 bytes (rskj ECKey.getAddress). +/// 64 coordinate bytes, last 20 bytes (rskj ECKey.getAddress). The one byte +/// `00`, which Bouncy Castle decodes as the point at infinity, has an address +/// too: see [`derive_rsk_address_from_public_key`]. pub fn rsk_address_from_public_key(key: &[u8]) -> Option { // A poisoned lock means some other thread panicked mid-update. That is a // reason to stop trusting the cache, not a reason to fail: the answer is @@ -235,6 +237,13 @@ pub fn rsk_address_from_public_key(key: &[u8]) -> Option Option { use k256::elliptic_curve::sec1::ToEncodedPoint; use sha3::Digest; + // rskj `ECKey.getAddress` hashes the key's uncompressed encoding less its + // first byte. The point at infinity's encoding is the one byte 00 alone, + // whatever the compression, so nothing is left to hash. + if key == [0x00] { + let digest = sha3::Keccak256::digest(b""); + return Some(alloy_primitives::Address::from_slice(&digest[12..])); + } let parsed = k256::PublicKey::from_sec1_bytes(key).ok()?; let point = parsed.to_encoded_point(false); let digest = sha3::Keccak256::digest(&point.as_bytes()[1..]); @@ -511,10 +520,125 @@ mod tests { let b = rsk_address_from_public_key(uncompressed.as_bytes()).expect("uncompressed derives"); assert_eq!(a, b, "one key, one federator, one address"); } + + /// rskj reads a member key with Bouncy Castle's `decodePoint`, which + /// takes the one byte 00 as the point at infinity, and only that byte: a + /// 00 prefix on more bytes is refused ("Incorrect length for infinity + /// encoding"). The member keeps the point, whose encoding is 00 again + /// (`getPubKey()`), so a federation writes 00 for it in each of its + /// forms, reads it back, and sorts it before every other key. + #[test] + fn a_member_key_of_the_point_at_infinity_is_kept_as_its_one_byte() { + use super::super::serialization::{rlp_encode_element, rlp_encode_list, rlp_encode_u64}; + assert_eq!(member_key_from_encoding(&[0x00]), Some(INFINITY_KEY)); + assert_eq!(member_key_from_encoding(&[0u8; 33]), None, "00 on 33 bytes"); + assert_eq!(key_encoding(&INFINITY_KEY), [0x00]); + let real = [0x02; 33]; + assert_eq!(member_key_from_encoding(&real), Some(real)); + assert_eq!(key_encoding(&real), real); + + let infinity = StoredMember::from_btc(INFINITY_KEY); + let other = StoredMember::from_btc(real); + assert!(infinity < other, "00 sorts first"); + // serializeFederationMember: RLP[btc, rsk, mst], each 00. + assert_eq!(infinity.to_rlp(), [0xc3, 0x00, 0x00, 0x00]); + assert_eq!( + StoredMember::from_stored(&infinity.to_rlp()), + Some(infinity) + ); + // Before RSKIP123 a member is its BTC key alone: the one byte. + assert_eq!(StoredMember::from_stored(&[0x00]), Some(infinity)); + let mixed = StoredMember { + btc: real, + rsk: INFINITY_KEY, + mst: real, + }; + assert_eq!(StoredMember::from_stored(&mixed.to_rlp()), Some(mixed)); + + // A stored federation's two forms, its members sorted. + assert_eq!( + serialize_federation_only_btc_keys(&[real, INFINITY_KEY], 5, 7), + rlp_encode_list(&[ + rlp_encode_u64(5), + rlp_encode_u64(7), + rlp_encode_list(&[vec![0x00], rlp_encode_element(&real)]), + ]) + ); + assert_eq!( + serialize_federation_multikey(&[other, infinity], 5, 7), + rlp_encode_list(&[ + rlp_encode_u64(5), + rlp_encode_u64(7), + rlp_encode_list(&[ + rlp_encode_element(&[0xc3, 0x00, 0x00, 0x00]), + rlp_encode_element(&other.to_rlp()), + ]), + ]) + ); + } + + /// rskj `ECKey.getAddress` hashes the key's uncompressed encoding less + /// its first byte. The point at infinity's is the one byte 00, so its + /// address is the last 20 bytes of the Keccak-256 hash of nothing. + #[test] + fn the_point_at_infinitys_address_is_that_of_the_hash_of_nothing() { + let address: alloy_primitives::Address = "0xdcc703c0e500b653ca82273b7bfad8045d85a470" + .parse() + .unwrap(); + assert_eq!(rsk_address_from_public_key(&[0x00]), Some(address)); + assert_eq!(member_rsk_address(&INFINITY_KEY), Some(address)); + assert_eq!( + rsk_address_from_public_key(&[0u8; 33]), + None, + "00 on 33 bytes" + ); + } +} + +/// A member key that is the point at infinity. rskj reads a federator's keys +/// with Bouncy Castle's `decodePoint` (`BtcECKey.fromPublicOnly`, +/// `ECKey.fromPublicOnly`), which takes the one byte `00` as that point, and +/// keeps it: its encoding is `00` again, compressed or not, so `00` is what a +/// federation with such a member serializes, hashes, writes in its redeem +/// scripts, logs and answers with. A member's keys are 33 bytes each here, and +/// this value, which no compressed key can be (one starts with 02 or 03), +/// stands for that one byte: [`member_key_from_encoding`] reads it and +/// [`key_encoding`] writes it back. It sorts before every other key, as `00` +/// does in the unsigned lexicographic order rskj sorts keys and members by +/// (`BtcECKey.PUBKEY_COMPARATOR`, `BTC_RSK_MST_PUBKEYS_COMPARATOR`), and two of +/// them are equal, as two points at infinity are. +pub const INFINITY_KEY: [u8; 33] = [0; 33]; + +/// The bytes rskj has for a member key (`getPubKey()`): the one byte `00` for +/// the point at infinity, the 33 compressed bytes for any other. +pub fn key_encoding(key: &[u8; 33]) -> &[u8] { + if *key == INFINITY_KEY { + &[0x00] + } else { + key + } +} + +/// A member key from the bytes rskj writes for one: `00`, the point at +/// infinity, or a compressed key's 33 bytes. Anything else is `None`. +pub fn member_key_from_encoding(bytes: &[u8]) -> Option<[u8; 33]> { + if bytes == [0x00] { + return Some(INFINITY_KEY); + } + <[u8; 33]>::try_from(bytes) + .ok() + .filter(|key| *key != INFINITY_KEY) +} + +/// The RSK address of a member key, as rskj's `ECKey.getAddress` derives it: +/// [`rsk_address_from_public_key`] of the key's encoding. +pub fn member_rsk_address(key: &[u8; 33]) -> Option { + rsk_address_from_public_key(key_encoding(key)) } /// A federation member as stored in bridge state: compressed BTC, RSK and -/// MST public keys (rskj `FederationMember`). Legacy (pre-RSKIP123) members +/// MST public keys (rskj `FederationMember`), any of them possibly the point +/// at infinity ([`INFINITY_KEY`]). Legacy (pre-RSKIP123) members /// carry only a BTC key, with rsk = mst = btc /// (`FederationMember.getFederationMemberFromKey`). The derived ordering is /// rskj's `BTC_RSK_MST_PUBKEYS_COMPARATOR` (lexicographic by btc, rsk, mst). @@ -531,33 +655,36 @@ impl StoredMember { } /// rskj `BridgeSerializationUtils.serializeFederationMember`: - /// `RLP[btcKey, rskKey, mstKey]`, all compressed. + /// `RLP[btcKey, rskKey, mstKey]`, each key's encoding: compressed, or `00` + /// for the point at infinity. pub fn to_rlp(&self) -> Vec { use super::serialization::{rlp_encode_element, rlp_encode_list}; rlp_encode_list(&[ - rlp_encode_element(&self.btc), - rlp_encode_element(&self.rsk), - rlp_encode_element(&self.mst), + rlp_encode_element(key_encoding(&self.btc)), + rlp_encode_element(key_encoding(&self.rsk)), + rlp_encode_element(key_encoding(&self.mst)), ]) } - /// Decode a member from either stored shape: a bare 33-byte compressed - /// BTC key (pre-RSKIP123) or `RLP[btc, rsk, mst]` (post-RSKIP123). rskj + /// Decode a member from either stored shape: a bare BTC key, 33 compressed + /// bytes or the point at infinity's `00` (pre-RSKIP123), or + /// `RLP[btc, rsk, mst]` (post-RSKIP123). rskj /// picks the format by the presence of the format-version cell; the two - /// shapes are unambiguous (a compressed key starts with 0x02/0x03, an - /// RLP list with >= 0xc0), so shape detection is equivalent. + /// shapes are unambiguous (a compressed key starts with 0x02/0x03, the + /// point at infinity is one byte, an RLP list starts with >= 0xc0 and is + /// never 33 bytes long), so shape detection is equivalent. pub fn from_stored(data: &[u8]) -> Option { - if data.len() == 33 { - return Some(Self::from_btc(data.try_into().ok()?)); + if data.len() == 33 || data == [0x00] { + return Some(Self::from_btc(member_key_from_encoding(data)?)); } let keys = super::serialization::rlp_decode_list(data)?; if keys.len() != 3 { return None; } Some(Self { - btc: keys[0].as_slice().try_into().ok()?, - rsk: keys[1].as_slice().try_into().ok()?, - mst: keys[2].as_slice().try_into().ok()?, + btc: member_key_from_encoding(&keys[0])?, + rsk: member_key_from_encoding(&keys[1])?, + mst: member_key_from_encoding(&keys[2])?, }) } } @@ -595,7 +722,10 @@ pub fn serialize_federation_only_btc_keys( use super::serialization::{rlp_encode_element, rlp_encode_list, rlp_encode_u64}; let mut sorted = keys.to_vec(); sorted.sort(); - let key_items: Vec> = sorted.iter().map(|k| rlp_encode_element(k)).collect(); + let key_items: Vec> = sorted + .iter() + .map(|k| rlp_encode_element(key_encoding(k))) + .collect(); rlp_encode_list(&[ rlp_encode_u64(creation_time_millis), rlp_encode_u64(creation_block), @@ -630,7 +760,10 @@ pub fn load_stored_federation( storage_key: &str, ) -> Option { use super::serialization::{rlp_decode_list, rlp_decode_u64}; - use super::storage::bridge_load_bytes_named; + use super::storage::{bridge_load_bytes_named, NEW_FEDERATION_KEY}; + if storage_key == NEW_FEDERATION_KEY { + ctx.chain_mut().new_federation_read = true; + } let data = bridge_load_bytes_named(ctx, storage_key); if data.is_empty() { return None; diff --git a/crates/execution/src/bridge/getters.rs b/crates/execution/src/bridge/getters.rs index 0b82f08..67b600c 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); @@ -318,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))) } @@ -338,14 +317,13 @@ pub fn get_lock_whitelist_address( args: &[u8], gas_cost: u64, config: &BridgeConstants, + hardfork_cfg: &crate::hardfork::RskHardforkConfig, ) -> Result { - if args.len() < 32 { - return Err(PrecompileError::other("getLockWhitelistAddress: args too short")); - } - let index = U256::from_be_slice(&args[..32]); - let entries = merged_whitelist(ctx); + let index = index_arg(args, "getLockWhitelistAddress")?; + let entries = merged_whitelist(&loaded_and_saved_whitelist(ctx, hardfork_cfg)); - 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), @@ -373,18 +351,21 @@ 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) = abi_decode_string(args, 0) else { + let Some(addr_str) = crate::abi::Args::new(args).string(0) else { return Err(PrecompileError::other( "getLockWhitelistEntryByAddress: malformed string argument", )); }; + // 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 { @@ -396,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 @@ -421,14 +414,18 @@ fn p2pkh_base58_address(hash160: &[u8; 20], config: &BridgeConstants) -> String bitcoin::base58::encode_check(&payload) } -fn p2pkh_version(config: &BridgeConstants) -> u8 { +/// bitcoinj `NetworkParameters.getAddressHeader`: the version of a P2PKH +/// address on the Bridge's network. +pub(super) fn p2pkh_version(config: &BridgeConstants) -> u8 { match config.btc_network { super::constants::BtcNetwork::Mainnet => 0, _ => 111, } } -fn p2sh_version(config: &BridgeConstants) -> u8 { +/// bitcoinj `NetworkParameters.getP2SHHeader`: the version of a P2SH address +/// on the Bridge's network. +pub(super) fn p2sh_version(config: &BridgeConstants) -> u8 { match config.btc_network { super::constants::BtcNetwork::Mainnet => 5, _ => 196, @@ -441,7 +438,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; @@ -483,23 +483,25 @@ fn parse_key_type(value: &str) -> Option { } } -fn member_key(member: &super::federation::StoredMember, key_type: KeyType) -> [u8; 33] { - match key_type { - KeyType::Btc => member.btc, - KeyType::Rsk => member.rsk, - KeyType::Mst => member.mst, - } +/// The member's key of `key_type` as rskj answers it (`getPubKey`, or +/// `getPubKey(true)` for the RSK and MST keys): 33 compressed bytes, or the +/// one byte `00` for the point at infinity. +fn member_key(member: &super::federation::StoredMember, key_type: KeyType) -> Vec { + let key = match key_type { + KeyType::Btc => &member.btc, + KeyType::Rsk => &member.rsk, + KeyType::Mst => &member.mst, + }; + super::federation::key_encoding(key).to_vec() } -/// 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 = crate::abi::Args::new(args) + .string(1) .as_deref() .and_then(parse_key_type) .ok_or_else(|| PrecompileError::other("PublicKeyOfType: unknown key type"))?; @@ -507,17 +509,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) +) -> Result, PrecompileError> { + 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 +564,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 +646,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())); @@ -686,13 +681,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())) @@ -763,31 +761,50 @@ 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 { +/// A method's first argument as an index: `((BigInteger) args[0]).intValue()`. +fn index_arg(args: &[u8], method: &str) -> Result { + crate::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> { + crate::abi::Args::new(args) + .string(0) + .as_deref() + .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 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()?; + 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(out) + Some(hash) } /// The federation creation time as the era reports it: milliseconds before @@ -804,23 +821,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 // --------------------------------------------------------------------------- @@ -846,6 +846,10 @@ pub fn get_state_for_btc_release_client( let data = bridge_load_bytes_named(ctx, PEGOUTS_WAITING_FOR_SIGNATURES_KEY); let waiting = super::peg::deserialize_rsk_txs_waiting_for_signatures(&data); let serialized = super::peg::serialize_rsk_txs_waiting_for_signatures(&waiting); + // rskj's `save()` writes back the set the call read, the empty list where + // the cell held nothing. Only a transaction's call keeps that, so only one + // before RSKIP88, which made this a local call. + bridge_store_bytes_named(ctx, PEGOUTS_WAITING_FOR_SIGNATURES_KEY, &serialized); let encoded = serialization::rlp_encode_list(&[serialized]); Ok(PrecompileOutput::new(gas_cost, abi_encode_bytes(&encoded).into())) } @@ -903,8 +907,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); @@ -917,17 +925,43 @@ pub fn get_state_for_debugging( PEGOUTS_WAITING_FOR_SIGNATURES_KEY, )), ); - let queue = super::peg::load_release_request_queue(ctx, papyrus); + // rskj's serializers each take their half of a collection: with the + // hashes from RSKIP146 the entries that carry one (`getEntriesWithHash`, + // `getEntriesWithHashOrdered`), before it the entries that carry none. + let queue: Vec<_> = super::peg::load_release_request_queue(ctx, papyrus) + .into_iter() + .filter(|entry| entry.rsk_tx_hash.is_some() == papyrus) + .collect(); let queue_bytes = if papyrus { super::peg::serialize_release_queue_with_hash(&queue) } else { super::peg::serialize_release_queue_legacy(&queue) }; - let confirmations = super::peg::load_pegout_confirmation_set(ctx, papyrus); + let confirmations: Vec<_> = super::peg::load_pegout_confirmation_set(ctx, papyrus) + .into_iter() + .filter(|entry| entry.rsk_tx_hash.is_some() == papyrus) + .collect(); let confirmations_bytes = super::peg::serialize_pegouts_waiting_for_confirmations(&confirmations, papyrus); let next_pegout = bridge_load_u256(ctx, NEXT_PEGOUT_HEIGHT_KEY).to::(); + // rskj's `save()` writes back what the call read, each as rskj serializes + // it, the empty list where a cell held nothing; the next peg-out height + // only where RSKIP271 has it read. Only a transaction's call keeps that, + // so only one before RSKIP88, which made this a local call. + store_federation_utxos(ctx, hardfork_cfg, block_number, &utxos); + bridge_store_bytes_named( + ctx, + PEGOUTS_WAITING_FOR_SIGNATURES_KEY, + &waiting_for_signatures, + ); + super::peg::write_back_release_request_queue(ctx, papyrus); + let all_confirmations = super::peg::load_pegout_confirmation_set(ctx, papyrus); + super::peg::store_pegout_confirmation_set(ctx, &all_confirmations, papyrus); + if hardfork_cfg.has_rskip271(block_number) { + bridge_store_u256(ctx, NEXT_PEGOUT_HEIGHT_KEY, U256::from(next_pegout)); + } + let encoded = serialization::rlp_encode_list(&[ // encodeBigInteger, not encodeElement: a minimal-length integer. serialization::rlp_encode_u64(u64::from(height)), @@ -1012,7 +1046,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 @@ -1109,7 +1143,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); } @@ -1171,7 +1205,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. @@ -1213,7 +1293,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), @@ -1230,7 +1310,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] @@ -1394,6 +1474,65 @@ 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}"); + } + } + } + + /// 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; 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 @@ -1493,16 +1632,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!( @@ -1512,6 +1659,53 @@ mod tests { } } + /// From RSKIP146 `BridgeState.getEncoded` serializes the release-request + /// queue and the peg-outs waiting for confirmations with their hashes, + /// and rskj's serializers take only the entries that carry one: a request + /// or a peg-out from before RSKIP146 is left out, not written with a zero + /// hash. + #[test] + fn state_for_debugging_leaves_out_what_carries_no_hash() { + use crate::bridge::peg::{ + serialize_pegouts_waiting_for_confirmations, serialize_release_queue_legacy, + serialize_release_queue_with_hash, store_pegout_confirmation_set, + PegoutWaitingForConfirmations, ReleaseRequest, + }; + use crate::bridge::storage::{bridge_storage_key, bridge_store_bytes}; + let mut ctx = ctx_at(6_500_000); + let request = |tag: u8, hash: Option<[u8; 32]>| ReleaseRequest { + btc_dest_hash160: [tag; 20], + amount_satoshis: 1_000_000, + rsk_tx_hash: hash, + }; + bridge_store_bytes( + &mut ctx, + bridge_storage_key("releaseRequestQueue"), + &serialize_release_queue_legacy(&[request(1, None)]), + ); + let hashed = request(2, Some([0x22; 32])); + bridge_store_bytes( + &mut ctx, + bridge_storage_key("releaseRequestQueueWithTxHash"), + &serialize_release_queue_with_hash(std::slice::from_ref(&hashed)), + ); + let pegout = |tag: u8, hash: Option<[u8; 32]>| PegoutWaitingForConfirmations { + btc_tx_raw: vec![tag; 10], + rsk_block_height: 5, + rsk_tx_hash: hash, + }; + let confirming = pegout(4, Some([0x44; 32])); + store_pegout_confirmation_set(&mut ctx, &[pegout(3, None), confirming.clone()], true); + + let out = get_state_for_debugging(&mut ctx, 0, &mainnet(), &hardforks()).unwrap(); + let fields = crate::bridge::serialization::rlp_decode_list(&as_bytes(&out)).unwrap(); + assert_eq!(fields[3], serialize_release_queue_with_hash(&[hashed])); + assert_eq!( + fields[4], + serialize_pegouts_waiting_for_confirmations(&[confirming], true) + ); + } + // ---- BTC tx index ---- #[test] @@ -1607,4 +1801,41 @@ 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); + } + + #[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 8a78d1b..b983b74 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}; @@ -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")) + crate::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: &crate::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)), + ) } // --------------------------------------------------------------------------- @@ -130,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)) } @@ -146,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)) } @@ -177,7 +173,7 @@ pub fn add_federator_public_key_multikey( config, hardfork_cfg, tx_ctx, - ); + )?; Ok(encode_int_result(gas_cost, code)) } @@ -193,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)) } @@ -205,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, @@ -216,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 { @@ -247,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, @@ -258,20 +260,113 @@ 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"); + } + // 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, + args, + dry_run, + config, + hardfork_cfg, + )) +} + +/// 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, + 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, + // and a member keeps that key, whose encoding stays the one byte + // (`federation::INFINITY_KEY`). 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() + if bytes == [0x00] { + return Some(super::federation::INFINITY_KEY); + } + let parsed = bitcoin::secp256k1::PublicKey::from_slice(bytes).ok()?; + Some(parsed.serialize()) }; match function { "create" => { if !bridge_load_bytes_named(ctx, PENDING_FEDERATION_KEY).is_empty() { return -1; // PENDING_FEDERATION_ALREADY_EXISTS } + // From RSKIP419 a committed federation waits as the proposed one + // through its spendability validation, and none is created while + // it does (`getProposedFederation` answers none before RSKIP419). + if hardfork_cfg.has_rskip419(block_number) + && !bridge_load_bytes_named(ctx, PROPOSED_FEDERATION_KEY).is_empty() + { + return -4; // PROPOSED_FEDERATION_ALREADY_EXISTS + } let new_fed = super::federation::load_stored_federation(ctx, NEW_FEDERATION_KEY); let old_fed = super::federation::load_stored_federation(ctx, OLD_FEDERATION_KEY); let age = federation_activation_age(config, hardfork_cfg, block_number); @@ -291,11 +386,11 @@ fn execute_federation_change( if function == "add" && multikey { return -10; // "add" disabled post-RSKIP123 } - let pending_data = bridge_load_bytes_named(ctx, PENDING_FEDERATION_KEY); - if pending_data.is_empty() { - return -1; // FEDERATION_NON_EXISTENT - } - // BtcECKey/ECKey.fromPublicOnly failure -> BridgeIllegalArgumentException -> -10 + // executeVoteFederationChangeFunction reads the keys before + // addFederatorPublicKeyMultikey looks for the pending federation: + // a key BtcECKey or ECKey.fromPublicOnly can't decode throws a + // BridgeIllegalArgumentException, which the vote answers -10 for, + // pending federation or not. let member = if function == "add" { let Some(btc) = parse_key(&args[0]) else { return -10 }; StoredMember::from_btc(btc) @@ -309,6 +404,10 @@ fn execute_federation_change( }; StoredMember { btc, rsk, mst } }; + let pending_data = bridge_load_bytes_named(ctx, PENDING_FEDERATION_KEY); + if pending_data.is_empty() { + return -1; // FEDERATION_NON_EXISTENT + } let mut members = deserialize_pending_federation(&pending_data); if members .iter() @@ -380,15 +479,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, } } @@ -477,7 +582,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); @@ -509,8 +620,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, + // 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()) @@ -590,16 +708,27 @@ 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 // 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 = super::peg::build_committed_federation_redeem_script( - &old_keys, config, hardfork_cfg, old_block, + // 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, @@ -785,9 +914,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) = crate::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 +938,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 @@ -939,9 +1073,10 @@ fn load_fee_per_kb_election( /// a non-positive or over-long value throws BridgeIllegalArgumentException; /// the sender must match an increase-locking-cap authorizer key (minimum /// ONE); the new cap may not be below the current one (equal is allowed) -/// nor above current × lockingCapIncrementsMultiplier. Reading the current -/// cap lazily initializes it (getLockingCap), so a failed bound check still -/// persists the initial value — but a failed authorizer check does not. +/// nor above current × lockingCapIncrementsMultiplier, a product that fails +/// the call where it passes a Java long. Reading the current cap lazily +/// initializes it (getLockingCap), so a failed bound check still persists +/// the initial value — but a failed authorizer check does not. pub fn increase_locking_cap( ctx: &mut CTX, args: &[u8], @@ -949,17 +1084,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 = crate::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) @@ -973,9 +1106,21 @@ pub fn increase_locking_cap( } let current_cap = super::peg::get_or_init_locking_cap(ctx, config); - if new_cap < current_cap - || new_cap > current_cap.saturating_mul(config.locking_cap_increments_multiplier) - { + if new_cap < current_cap { + return Ok(PrecompileOutput::new(gas_cost, output.to_vec().into())); + } + // The most the cap may grow to, as bitcoinj's `Coin.multiply` takes it: + // Guava's `LongMath.checkedMultiply`, which throws past a Java long, and + // `Bridge.execute` makes the throw a VMException that fails the call. + let most = i64::try_from(current_cap) + .ok() + .zip(i64::try_from(config.locking_cap_increments_multiplier).ok()) + .and_then(|(cap, multiplier)| cap.checked_mul(multiplier)) + .and_then(|most| u64::try_from(most).ok()) + .ok_or_else(|| { + PrecompileError::other("increaseLockingCap: the cap times its multiplier passes a long") + })?; + if new_cap > most { return Ok(PrecompileOutput::new(gas_cost, output.to_vec().into())); } @@ -1004,9 +1149,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 @@ -1037,6 +1183,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) { @@ -1044,33 +1191,38 @@ 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 = crate::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)); } - 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 @@ -1087,6 +1239,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) { @@ -1094,7 +1247,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)), }; @@ -1103,19 +1256,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 @@ -1131,6 +1283,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) { @@ -1142,30 +1295,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) { - 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 @@ -1179,55 +1319,94 @@ pub fn set_lock_whitelist_disable_block_delay( args: &[u8], gas_cost: u64, config: &super::constants::BridgeConstants, + hardfork_cfg: &crate::hardfork::RskHardforkConfig, tx_ctx: &crate::precompiles::BridgeTxContext, ) -> Result { - if args.len() < 32 { - return Err(PrecompileError::other( - "setLockWhitelistDisableBlockDelay: args too short", - )); - } + // 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. + 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); 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 - } - - let delay = U256::from_be_slice(&args[..32]).to::() as i64; - let best_height = super::btc_store::load_chain_head(ctx) - .map(|h| h.height as i64) - .unwrap_or(0); - if delay + 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); - - 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 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 + } 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)) } // --------------------------------------------------------------------------- // 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 +/// `getActiveFederationCreationBlockHeight()` → uint256: rskj +/// `FederationSupportImpl.getActiveFederationCreationBlockHeight` (RSKIP186). +/// Once the block run is the activation age past the next federation's +/// creation block, that block, though no `updateCollections` has moved it to +/// the active federation's cell yet; until then the active federation's, or 0 +/// where neither is stored. pub fn get_active_federation_creation_block_height( ctx: &mut CTX, gas_cost: u64, + config: &super::constants::BridgeConstants, + hardfork_cfg: &crate::hardfork::RskHardforkConfig, ) -> Result { - let val = bridge_load_u256(ctx, ACTIVE_FEDERATION_CREATION_BLOCK_HEIGHT_KEY); + let block_number = revm::context_interface::Block::number(ctx.block()).to::(); + let val = if !hardfork_cfg.has_rskip186(block_number) { + U256::ZERO + } else { + let activation_age = federation_activation_age(config, hardfork_cfg, block_number); + match get_next_federation_creation_block_height(ctx) { + Some(next) if block_number >= next.saturating_add(activation_age) => U256::from(next), + _ => bridge_load_u256(ctx, ACTIVE_FEDERATION_CREATION_BLOCK_HEIGHT_KEY), + } + }; let output = val.to_be_bytes::<32>(); Ok(PrecompileOutput::new(gas_cost, output.to_vec().into())) } @@ -1254,7 +1433,7 @@ fn serialize_pending_federation_btc_keys(members: &[StoredMember]) -> Vec { keys.sort(); let encoded_keys: Vec> = keys .iter() - .map(|k| serialization::rlp_encode_element(k)) + .map(|k| serialization::rlp_encode_element(super::federation::key_encoding(k))) .collect(); serialization::rlp_encode_list(&encoded_keys) } @@ -1312,23 +1491,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()) + crate::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 +1655,983 @@ 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'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. + #[test] + fn a_one_off_value_beyond_a_long_throws_before_the_sender_check() { + 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, &hardforks, &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" + ); + } + + /// rskj `LockingCapSupportImpl.increaseLockingCap` takes the most the cap + /// may grow to with bitcoinj's `Coin.multiply`, which is Guava's + /// `LongMath.checkedMultiply`: where the current cap times the increments + /// multiplier passes a Java long, it throws, and the Bridge call fails. A + /// new cap below the current one is refused before that product is taken, + /// and answers false. + #[test] + fn a_cap_whose_limit_passes_a_long_fails_the_increase() { + use crate::abi::encode::{self, Arg}; + use revm::MainContext; + let config = super::super::constants::BridgeConstants::mainnet(); + let key = + alloy_primitives::hex::decode(config.increase_locking_cap_authorizer_keys[0]).unwrap(); + let tx_ctx = crate::precompiles::BridgeTxContext { + rsk_sender: super::super::federation::rsk_address_from_public_key(&key).unwrap(), + ..Default::default() + }; + let mut ctx = + revm::Context::mainnet().with_chain(crate::raw_storage::RskChainExt::default()); + // Mainnet's multiplier is 2, and twice this passes i64::MAX. + let cap: i64 = (1 << 62) + 1; + bridge_store_u256(&mut ctx, LOCKING_CAP_KEY, U256::from(cap)); + let mut increase = |new_cap: i64| { + let args = encode::args(&[Arg::Word(encode::int(new_cap))]); + increase_locking_cap(&mut ctx, &args, 0, &config, &tx_ctx).map(|out| out.bytes[31] == 1) + }; + assert!( + !increase(cap - 1).unwrap(), + "below the current cap: refused before the product is taken" + ); + assert!( + increase(cap).is_err(), + "the product passes a long: the call fails" + ); + assert_eq!( + bridge_load_u256(&mut ctx, LOCKING_CAP_KEY), + U256::from(cap), + "and the cap is as it was" + ); + } + + /// rustock's own regtest has three federation-change authorizers, the first + /// three of rskj's regtest ('auth-a' to 'auth-c'), so two decide. rskj's + /// regtest has five, and three decide there (`BridgeConstants::rskj_regtest`). + #[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"); + } + + /// rskj's regtest has five federation-change authorizers, 'auth-a' to + /// 'auth-e' (`FederationRegTestConstants`), and a change takes three of + /// them (`AddressBasedAuthorizer`, MAJORITY): the last two vote as the + /// first three do. + #[test] + fn three_of_rskj_regtests_five_authorizers_decide_a_federation_change() { + use revm::MainContext; + let config = super::super::constants::BridgeConstants::rskj_regtest(); + let hardforks = crate::hardfork::RskHardforkConfig::rskj_regtest(); + let authorizers = federation_change_authorizers(&config); + assert_eq!(authorizers.len(), 5); + let mut ctx = + revm::Context::mainnet().with_chain(crate::raw_storage::RskChainExt::default()); + let mut create = |sender: alloy_primitives::Address| { + let tx_ctx = crate::precompiles::BridgeTxContext { + rsk_sender: sender, + ..Default::default() + }; + let out = create_federation(&mut ctx, 0, &config, &hardforks, &tx_ctx).unwrap(); + let answer = i64::from_be_bytes(out.bytes[24..32].try_into().unwrap()); + let pending = !bridge_load_bytes_named(&mut ctx, PENDING_FEDERATION_KEY).is_empty(); + (answer, pending) + }; + assert_eq!(create(authorizers[3]), (1, false), "one vote waits"); + assert_eq!(create(authorizers[4]), (1, false), "two wait"); + assert_eq!(create(authorizers[0]), (1, true), "three decide"); + } + + /// rskj reads a new member's keys before it looks for the pending + /// federation (`FederationSupportImpl.executeVoteFederationChangeFunction`, + /// then `addFederatorPublicKeyMultikey`): with no pending federation, a key + /// Bouncy Castle can't decode answers -10 and a readable one -1, before + /// RSKIP123 and after it. + #[test] + fn an_unreadable_key_answers_minus_10_with_no_pending_federation() { + use crate::abi::encode::{self, Arg}; + use crate::precompiles::BridgeTxContext; + use revm::MainContext; + // The generator, compressed, and a prefix no encoding has. + let good = alloy_primitives::hex::decode( + "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798", + ) + .unwrap(); + let mut bad = good.clone(); + bad[0] = 0x05; + 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 add = |block: u64, keys: &[&[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()); + let args: Vec = keys.iter().map(|key| Arg::Bytes(key)).collect(); + let args = encode::args(&args); + let out = if keys.len() == 1 { + add_federator_public_key(&mut ctx, &args, 0, &config, &cfg, &authorized) + } else { + add_federator_public_key_multikey(&mut ctx, &args, 0, &config, &cfg, &authorized) + } + .unwrap(); + i64::from_be_bytes(out.bytes[24..32].try_into().unwrap()) + }; + let (before, after) = (1_000_000u64, 2_000_000u64); + assert!(!cfg.has_rskip123(before) && cfg.has_rskip123(after)); + assert_eq!(add(before, &[&bad]), -10); + assert_eq!(add(before, &[&good]), -1); + assert_eq!(add(after, &[&good, &good, &bad]), -10); + assert_eq!(add(after, &[&good, &good, &good]), -1); + } + + /// 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)); + } + + /// From RSKIP377 a handover keeps, as the last retired federation's script, + /// the output script of its members' multisig, the default branch of its + /// ERP script, P2SH-P2WSH for a P2SH-P2WSH federation + /// (`FederationSupportImpl.getFederationMembersP2SHScript`, + /// `ErpFederation.getDefaultP2SHScript`), where before RSKIP377 it kept the + /// federation's own. rskj's regtest genesis federation, a P2SH-P2WSH ERP + /// one, handed over both ways: the legacy commit, between fingerroot500 and + /// lovell700, and RSKIP419's of a proposed federation. + #[test] + fn from_rskip377_the_last_retired_script_is_its_members_multisigs() { + use bitcoin::hashes::Hash as _; + use revm::MainContext; + let config = super::super::constants::BridgeConstants::rskj_regtest(); + let hardforks = crate::hardfork::RskHardforkConfig::mainnet(); + 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 genesis = super::super::peg::genesis_federation_keys(&config); + let multisig = + super::super::peg::build_federation_redeem_script(&genesis, genesis.len() / 2 + 1); + let witness_program = [ + &[0x00, 0x20][..], + &bitcoin::hashes::sha256::Hash::hash(&multisig).to_byte_array(), + ] + .concat(); + let members_script = super::super::peg::p2sh_output_script_program( + &super::super::peg::redeem_script_hash160_pub(&witness_program), + ); + let expected = + serialization::rlp_encode_list(&[serialization::rlp_encode_element(&members_script)]); + let members: Vec = super::super::peg::genesis_federation_keys( + &super::super::constants::BridgeConstants::testnet(), + ) + .into_iter() + .map(StoredMember::from_btc) + .collect(); + let retired = + |ctx: &mut _| bridge_load_bytes_named(ctx, LAST_RETIRED_FEDERATION_P2SH_SCRIPT_KEY); + + let block = 6_000_000u64; + 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!(retired(&mut ctx), expected, "the legacy commit"); + + 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!(retired(&mut ctx), expected, "a proposed federation's"); + } + + /// 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)); + } + + /// 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" + ); + } + + /// From RSKIP419 a committed federation is proposed first, and + /// `createPendingFederation` answers -4, PROPOSED_FEDERATION_ALREADY_EXISTS, + /// while one is stored: after it looks for a pending federation, before it + /// looks for a federation awaiting activation or retiring. The dry run + /// answers the same, so the vote isn't counted. Before RSKIP419 a proposed + /// federation is none to rskj. + #[test] + fn from_rskip419_no_federation_is_created_while_one_is_proposed() { + use revm::MainContext; + let config = super::super::constants::BridgeConstants::mainnet(); + let cfg = crate::hardfork::RskHardforkConfig::mainnet(); + let authorizers = federation_change_authorizers(&config); + let deciding = authorizers.len() / 2 + 1; + // The deciding authorizers' answers to createFederation at `block`, with + // a proposed federation stored or not, and whether a pending federation + // was created. + let votes = |block: u64, proposed: bool| { + 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()); + if proposed { + let members = [ + StoredMember::from_btc([0x02; 33]), + StoredMember::from_btc([0x03; 33]), + ]; + bridge_store_bytes_named( + &mut ctx, + PROPOSED_FEDERATION_KEY, + &super::super::federation::serialize_federation_multikey( + &members, + 0, + block - 1, + ), + ); + } + let answers: Vec = authorizers[..deciding] + .iter() + .map(|authorizer| { + let tx_ctx = crate::precompiles::BridgeTxContext { + rsk_sender: *authorizer, + ..Default::default() + }; + let out = create_federation(&mut ctx, 0, &config, &cfg, &tx_ctx).unwrap(); + i64::from_be_bytes(out.bytes[24..32].try_into().unwrap()) + }) + .collect(); + let pending = !bridge_load_bytes_named(&mut ctx, PENDING_FEDERATION_KEY).is_empty(); + (answers, pending) + }; + let lovell = 7_338_024; + assert!(cfg.has_rskip419(lovell) && !cfg.has_rskip419(lovell - 1)); + let created = (vec![1; deciding], true); + assert_eq!(votes(lovell - 1, true), created, "before RSKIP419"); + assert_eq!(votes(lovell, true), (vec![-4; deciding], false)); + assert_eq!(votes(lovell, false), created); + } + + /// 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, or for a + /// P2SH-P2WSH federation than a witness script may be + /// (`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); + + // From Reed a federation is a P2SH-P2WSH one, its redeem script in a + // witness, where 20 members' fits: 20 keys and no more. + let p2sh_p2wsh = 8_100_000; + assert_eq!(federation_format_version(&cfg, p2sh_p2wsh), 4000); + let mut keys: Vec<[u8; 33]> = (0..20).map(|i| member(i).btc).collect(); + keys.sort(); + let redeem = super::super::peg::build_committed_federation_redeem_script( + &keys, &config, &cfg, p2sh_p2wsh, + ); + assert!(redeem.len() <= 3600 - 34); + assert_eq!(votes(p2sh_p2wsh, 20), committed); + assert_eq!(votes(p2sh_p2wsh, 21), thrown); + } + + /// A member whose key is the point at infinity takes two bytes of a redeem + /// script where another takes 34: bitcoinj's `ScriptBuilder.data` pushes + /// its encoding, the one byte 00. So a P2SH ERP federation of a key more + /// than a scriptSig holds is built, and committed, when one of its keys is + /// that point. + #[test] + fn the_point_at_infinity_takes_two_bytes_of_the_redeem_scripts_size() { + 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 block = 5_000_000u64; + assert_eq!(federation_format_version(&cfg, block), 3000); + 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()) + }; + // Nine members of 33-byte keys fit what a scriptSig holds, ten don't. + 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, block) + .len() + }; + assert!(size(9) <= MAX_P2SH_REDEEM_SCRIPT_SIZE && size(10) > MAX_P2SH_REDEEM_SCRIPT_SIZE); + let authorizers = federation_change_authorizers(&config); + let deciding = authorizers.len() / 2 + 1; + let votes = |members: &[StoredMember]| -> 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()); + 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; + let ten: Vec = (0..10).map(member).collect(); + assert_eq!(votes(&ten), thrown, "ten keys of 33 bytes"); + let mut nine_and_infinity: Vec = (0..9).map(member).collect(); + nine_and_infinity.push(StoredMember::from_btc( + super::super::federation::INFINITY_KEY, + )); + assert_eq!( + votes(&nine_and_infinity), + committed, + "nine, and the point at infinity" + ); + } + + /// 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, 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, + &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, &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, + &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 9e03c6b..5cef26a 100644 --- a/crates/execution/src/bridge/mod.rs +++ b/crates/execution/src/bridge/mod.rs @@ -14,19 +14,23 @@ 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; +mod der_signature; pub mod events; pub mod federation; pub mod getters; pub mod legacy_events; pub mod governance; pub mod peg; +pub mod peg_legacy; pub mod pegin_instructions; pub mod pmt; pub mod release_tx; /// rskj-compatible peg-in sender detection, isolated so it can be removed. pub mod rskj_sender_compat; +pub mod script_spend; pub mod serialization; pub mod storage; pub mod tx; @@ -68,154 +72,22 @@ 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 `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. + +/// 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 + crate::abi::Args::new(args) + .decode(signature) + .unwrap_or(false) } // --------------------------------------------------------------------------- -// 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. @@ -249,6 +121,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 { @@ -257,6 +133,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), } } } @@ -269,14 +146,19 @@ 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), + // 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 @@ -386,6 +268,12 @@ fn method_allows_static_call(name: &str) -> bool { | "getQueuedPegoutsCount" | "getEstimatedFeesForNextPegOutEvent" | "getEstimatedFeesForPegOutAmount" + // RSKIP502: the union bridge's two getters. + | "getUnionBridgeContractAddress" + | "getUnionBridgeLockingCap" + // RSKIP529: the two event getters. + | "getSuperEvent" + | "getBaseEvent" ) } @@ -475,6 +363,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) -- @@ -543,6 +438,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]) @@ -557,8 +458,9 @@ pub fn find_bridge_method(selector: &[u8; 4]) -> Option<&'static BridgeMethodInf /// `getGasForData` and, when `Bridge.execute` throws (parse failure OR a /// method `VMException`/`RuntimeException`), only spends `requiredGas + /// basicTxCost` while still flagging the summary as failed: the receipt is -/// SUCCESS with that spent gas, no logs, no endowment, but the sender is -/// charged the FULL gas limit. The marker carries `requiredGas` so the +/// SUCCESS with that spent gas, no endowment, and of the logs those the +/// method wrote before it threw; but the sender is charged the FULL gas +/// limit. The marker carries `requiredGas` so the /// precompile frame can record exactly that cost (the basic tx cost is /// charged separately by revm's intrinsic accounting). pub const INVISIBLE_EXCEPTION_MARKER: &str = "rskj invisible exception:"; @@ -602,6 +504,26 @@ 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 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 @@ -662,30 +584,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 @@ -732,7 +658,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 { @@ -742,15 +668,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; @@ -760,18 +688,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)); @@ -779,7 +709,17 @@ pub fn execute_bridge( return Err(marker_with_gas(INTERNAL_BRIDGE_THROW_MARKER, gas_cost)); } + // What this call reads of the federation stored as the new one is what + // rskj's `save()` writes back, not what was read before it. + ctx.chain_mut().new_federation_read = false; let result = execute_method(ctx, method.name, args, gas_cost, config, block_store, use_v2, hardfork_cfg, tx_ctx, caller, call_depth, call_value); + // rskj `Bridge.execute` ends a method that returns with `teardown`, whose + // `FederationStorageProviderImpl.saveNewFederation` writes back the new + // federation the method read, from RSKIP123 with all three keys of each + // member and its format version, however the cell held it. + if matches!(&result, Ok(output) if !output.reverted) && ctx.chain().new_federation_read { + federation::save_new_federation_multikey(ctx, hardfork_cfg, block_number); + } // rskj `Bridge.execute` wraps any method `RuntimeException` in a // `VMException`, which `TransactionExecutor.call` / `Program` then handle // by spending only `requiredGas` (the `getGasForData` cost) — NOT all the @@ -787,6 +727,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 { @@ -820,6 +782,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); }; @@ -829,7 +794,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)) @@ -845,7 +810,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 crate::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, } @@ -853,24 +821,37 @@ 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], config: &BridgeConstants, + hardfork_cfg: &RskHardforkConfig, ) -> u64 { const BASIC_COST: u64 = 27_000; const STEP_COST: u64 = 315; 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 = 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, }; + // 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. A store that can't be + // seeded is priced as it stands; the call then throws as it seeds. + let _ = 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 { @@ -885,14 +866,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, @@ -909,45 +882,82 @@ 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")); } } + // 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" => 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), - "getBtcBlockchainParentBlockHeaderByHash" => btc_chain::get_parent_block_header_by_hash(ctx, args, gas_cost), + "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, 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, 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, config, hardfork_cfg) + } // RSKIP502: the union bridge, authorized against the *immediate* // caller. @@ -977,9 +987,37 @@ 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), + "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) @@ -994,23 +1032,30 @@ 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, 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), + // 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, &as_called) + } // Transaction-callable read-only - "getActivePowpegRedeemScript" => governance::get_active_powpeg_redeem_script(ctx, gas_cost), - "getActiveFederationCreationBlockHeight" => governance::get_active_federation_creation_block_height(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, config, hardfork_cfg) + } "getNextPegoutCreationBlockNumber" => peg::get_next_pegout_creation_block_number(ctx, gas_cost), "getQueuedPegoutsCount" => peg::get_queued_pegouts_count(ctx, gas_cost), "getEstimatedFeesForNextPegOutEvent" => { @@ -1040,14 +1085,18 @@ 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), "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), @@ -1056,7 +1105,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 @@ -1093,6 +1142,21 @@ fn unimplemented_method(method: &str) -> Result 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" @@ -1146,15 +1213,17 @@ 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): BridgeMethods of rskj's release + // VETIVER-9.0.4, 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] @@ -1259,6 +1328,26 @@ 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")); + // 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, VETIVER-9.0.4), 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, 54); } /// rskj `validateLocalCall`: a local-only getter reached by an on-chain call @@ -1289,6 +1378,48 @@ 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)", + "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] { + 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] @@ -1311,9 +1442,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" ); } @@ -1343,22 +1475,327 @@ 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 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] fn receive_headers_cost_matches_rskj() { let cfg = crate::hardfork::RskHardforkConfig::mainnet(); - // ABI bytes[] with 3 entries: offset word + length word (entries not needed) - let mut args = vec![0u8; 64]; - args[31] = 0x20; // offset 32 - args[63] = 3; // length 3 + // Three headers. rskj counts the decoded array, so a count the + // entries do not back is not three headers but a parse failure. + use crate::abi::encode::{self, Arg}; + let header: &[u8] = &[0u8; 80]; + let args = encode::args(&[Arg::BytesArray(&[header, header, header])]); // Pre-wasabi100: flat 22_000 regardless of header count. assert_eq!(receive_headers_cost(&args, &cfg, 1_590_999), 22_000); // Post-wasabi100 (rskip124+132 both active on mainnet): 25_000 + 2*3_500. assert_eq!(receive_headers_cost(&args, &cfg, 1_591_000), 32_000); - // Unparseable args fall back to the base cost. + // No headers, which is what an empty payload decodes to, is the base + // cost, and so is a payload that does not decode. 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 + ); + } + + /// 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/peg.rs b/crates/execution/src/bridge/peg.rs index 7d10fd8..fc6cddc 100644 --- a/crates/execution/src/bridge/peg.rs +++ b/crates/execution/src/bridge/peg.rs @@ -65,14 +65,18 @@ pub struct ReleaseRequest { pub rsk_tx_hash: Option<[u8; 32]>, } +/// rskj `ReleaseRequestQueue.Entry.equals`: the same destination, amount and +/// creation hash, no hash being equal to no hash. +fn same_release_request(a: &ReleaseRequest, b: &ReleaseRequest) -> bool { + a.btc_dest_hash160 == b.btc_dest_hash160 + && a.amount_satoshis == b.amount_satoshis + && a.rsk_tx_hash == b.rsk_tx_hash +} + // --------------------------------------------------------------------------- // registerBtcTransaction // --------------------------------------------------------------------------- -/// `registerBtcTransaction(bytes btcTxSerialized, int256 height, bytes pmtSerialized)` -/// -/// The core peg-in method. Verifies a BTC transaction is included in the -/// BTC header chain, classifies it, and credits RBTC to the sender. /// rskj BridgeSupport.shouldMarkRejectedPeginAsProcessed: only between /// RSKIP459 (lovell700) and RSKIP551 (vetiver900) are rejected /// non-refundable peg-ins marked as processed. @@ -95,6 +99,153 @@ fn pegin_below_minimum(live_output_values: &[u64], min: u64, rskip293: bool) -> } } +/// 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 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`, 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), +} + +/// 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 `BridgeIllegalArgumentException`. + if !PartialMerkleTree::has_expected_size(pmt_data) { + return Err(Invalid::IllegalArgument( + "PartialMerkleTree doesn't have expected size", + )); + } + // `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::IllegalArgument("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, + } +} + +/// `registerBtcTransaction(bytes btcTxSerialized, int256 height, bytes pmtSerialized)` +/// +/// The core peg-in method. Verifies a BTC transaction is included in the +/// BTC header chain, classifies it, and credits RBTC to the sender. pub fn register_btc_transaction( ctx: &mut CTX, args: &[u8], @@ -103,26 +254,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 = 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 { 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); @@ -130,117 +273,37 @@ pub fn register_btc_transaction( // A fresh bridge BTC store must be seeded with the rskj checkpoint // before any chain lookups. let block_number = revm::context_interface::Block::number(ctx.block()).to::(); - let use_v2 = hardfork_cfg.has_stored_block_v2(block_number); - 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)?; // 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. + // 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. // - // `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). - // - // 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 with bitcoinj and runs its `verify()` + // on it (BridgeSupport l.400-401). A throw from either fails the + // transaction too. + // The processed transactions' map as the call found it. rskj's `save()` + // writes back the map the check below reads, unless the call saves nothing. + let processed_map = bridge_storage_key(BTC_TX_HASHES_ALREADY_PROCESSED_KEY); + let processed_map_found = bridge_load_raw(ctx, processed_map); + 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)) => { @@ -250,19 +313,30 @@ 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" + ); + // The map read above stays as it was found. + bridge_store_raw(ctx, processed_map, processed_map_found); + 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)); } } - // 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)`): @@ -285,18 +359,13 @@ pub fn register_btc_transaction( // version, not the current block — an ERP federation keeps its ERP script // even after a later template hardfork (rskj getActiveFederation / // getRetiringFederation → Federation.getRedeemScript()). - let (federation_keys, fed_redeem) = + let (_, fed_redeem) = active_federation_keys_and_redeem(ctx, config, hardfork_cfg, block_number_now); // getActiveFederationAddress is format-aware: a P2SH-P2WSH (format 4000, // RSKIP305) federation is recognized at its P2SH-P2WSH address, so peg-in / // UTXO-registration output matching must use that script, not a plain P2SH. let active_format = active_federation_format(ctx, config, hardfork_cfg, block_number_now); let fed_script = p2sh_output_script(&federation_output_hash160(&fed_redeem, active_format)); - // rskj isPegOutTx compares against each federation's STANDARD redeem script - // (getStandardRedeemScript) — for an ERP federation this is the default - // multisig branch only, not the full ERP/flyover-wrapped redeem. - let fed_standard_redeem = - build_federation_redeem_script(&federation_keys, federation_keys.len() / 2 + 1); let retiring_format = federation_format_version(ctx, super::storage::OLD_FEDERATION_FORMAT_VERSION_KEY); let retiring = retiring_federation_keys_and_redeem(ctx, config, hardfork_cfg, block_number_now) @@ -304,8 +373,6 @@ pub fn register_btc_transaction( let script = p2sh_output_script(&federation_output_hash160(&redeem, retiring_format)); (redeem, script) }); - let retiring_standard_redeem = retiring_federation_keys(ctx, config, hardfork_cfg, block_number_now) - .map(|keys| build_federation_redeem_script(&keys, keys.len() / 2 + 1)); // rskj marks processed txs with the RSK execution block number // (BridgeSupport.markTxAsProcessed), not the BTC block height. @@ -314,22 +381,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 { @@ -399,107 +483,127 @@ pub fn register_btc_transaction( config.legacy_minimum_pegin_tx_value }; - // rskj PegUtilsLegacy.getTransactionType — PEGOUT_OR_MIGRATION detection. - // An input "spends" a federation when its scriptSig redeem hashes to that - // federation's P2SH; isPegOutTx/migration compare the input's STANDARD - // (default-branch) redeem (extractStandardRedeemScript) to each fed's - // standard P2SH, while txIsFromOldFederation compares the FULL redeem. - let active_std_hash160 = redeem_script_hash160(&fed_standard_redeem); - let retiring_std_hash160 = retiring_standard_redeem.as_ref().map(|r| redeem_script_hash160(r)); - // lastRetiredFederationP2SHScript (RSKIP186): the P2SH of the last fully - // retired federation, stored as serializeScript = RLP([program]). - let retired_hash160: Option<[u8; 20]> = if hardfork_cfg.has_rskip186(rsk_height) { + // `getLastRetiredFederationP2SHScript` (RSKIP186): the P2SH script of the + // last federation to retire, as `setLastRetiredFederationScript` stored it + // (`serializeScript`, the RLP of [program]). + let retired_p2sh: Option> = if hardfork_cfg.has_rskip186(rsk_height) { let raw = super::storage::bridge_load_bytes_named( ctx, super::storage::LAST_RETIRED_FEDERATION_P2SH_SCRIPT_KEY, ); - rlp_decode_list(&raw) - .and_then(|items| items.into_iter().next()) - .filter(|p| p.len() == 23) - .map(|p| p[2..22].try_into().expect("23-byte P2SH program")) + rlp_decode_list(&raw).and_then(|items| items.into_iter().next()) } else { None }; - // rskj `PegUtilsLegacy.isPegOutTx` builds BOTH `createP2SHOutputScript` and - // (post-RSKIP305) `createP2SHP2WSHOutputScript` of the input's STANDARD - // redeem and matches either against each federation's - // `getFederationStandardP2SHScript` (`getDefaultP2SHScript`, which is the - // P2SH-P2WSH form for a segwit federation). The input's standard redeem is - // the same redeem the federation stored, so checking the input's legacy AND - // witness-program hashes against a federation's stored hash matches it - // whichever form that hash was stored in (a segwit retired/retiring/active - // fed stores the witness-program hash; #8,569,377). - let has_rskip305 = hardfork_cfg.has_rskip305(rsk_height); - let input_matches_fed = |full: &[u8], target: [u8; 20]| -> bool { - let std = build_federation_redeem_script( - &super::release_tx::spending_redeem_keys(full), - super::release_tx::redeem_script_threshold(full), - ); - redeem_script_hash160(&std) == target - || (has_rskip305 && federation_output_hash160(&std, 4000) == target) - }; - - // rskj `BitcoinUtils.extractRedeemScriptFromInput`: a segwit (P2SH-P2WSH) - // input carries its redeem in the witness (last item), not the scriptSig - // (which is just the witness-program push) — so classification must read it - // from there to recognize a segwit peg-out/migration. - let input_redeem = |i: &bitcoin::TxIn| -> Option> { - if !i.witness.is_empty() { - i.witness.last().map(|r| r.to_vec()) - } else { - super::release_tx::extract_redeem_script(i.script_sig.as_bytes()) - } - }; - // (1) txIsFromOldFederation (RSKIP199): any input spends the hardcoded old - // federation address — unconditionally a migration. - let tx_from_old_fed = hardfork_cfg.has_rskip199(rsk_height) - && btc_tx.input.iter().any(|i| { - input_redeem(i) - .is_some_and(|r| redeem_script_hash160(&r) == config.old_federation_address_hash160) - }); - // (4) isPegOutTx(liveFeds): an input's standard redeem matches the active - // or retiring federation. - let spends_live_fed = btc_tx.input.iter().any(|i| { - input_redeem(i).is_some_and(|r| { - input_matches_fed(&r, active_std_hash160) - || 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). - 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)) - }) - }); let active_output_values: Vec = btc_tx .output .iter() .filter(|o| o.script_pubkey == fed_script) .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) + + // 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 + // "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 `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` + // (`super::peg_legacy`), which leaves unregistered a transaction it refuses + // as a peg-in for an input and finds neither a migration nor a pegout. + 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 { - active_output_values.iter().sum::() >= min_pegin + use super::peg_legacy::{LegacyRules, LiveFederation, PegType}; + let rskip293 = hardfork_cfg.has_rskip293(rsk_height); + let old_federation_p2sh = + p2sh_output_script_program(&config.old_federation_address_hash160); + let rules = LegacyRules { + rskip199: hardfork_cfg.has_rskip199(rsk_height), + rskip201: hardfork_cfg.has_rskip201(rsk_height), + rskip305: hardfork_cfg.has_rskip305(rsk_height), + old_federation_p2sh: &old_federation_p2sh, + active: LiveFederation { + p2sh: fed_script.as_bytes(), + redeem: fed_redeem.as_slice(), + format: active_format, + }, + retiring: retiring.as_ref().map(|(redeem, script)| LiveFederation { + p2sh: script.as_bytes(), + redeem: redeem.as_slice(), + format: retiring_format, + }), + retired_p2sh: retired_p2sh.as_deref(), + live_below_minimum: pegin_below_minimum(&live_output_values, min_pegin, rskip293), + active_below_minimum: pegin_below_minimum(&active_output_values, min_pegin, rskip293), + }; + let peg_type = rules.transaction_type(&btc_tx).map_err(|why| { + PrecompileError::other(format!( + "registerBtcTransaction: rskj aborts classifying this transaction ({why})" + )) + })?; + match peg_type { + PegType::FromOldFederation => Some("FROM_OLD_FEDERATION"), + PegType::Migration => Some("MIGRATION"), + PegType::Pegout => Some("PEGOUT"), + PegType::Unknown { + refused_for_an_input: true, + } => { + tracing::debug!("registerBtcTransaction: refused as a peg-in for an input, and neither a migration nor a pegout; not registered"); + return Ok(PrecompileOutput::new(gas_cost, Bytes::new())); + } + PegType::PegIn + | PegType::Unknown { + refused_for_an_input: false, + } => None, + } }; - let is_migration = spends_retired_or_retiring && move_to_active; - if tx_from_old_fed || is_migration || spends_live_fed { + 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); @@ -516,27 +620,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 - // "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 @@ -544,15 +627,27 @@ 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; + // rskj `registerPegIn` and `legacyRegisterPegin` begin with + // `computeTotalAmountSent`, which reads the active federation's UTXOs and, + // while one retires, the retiring federation's: `save()` writes them back + // as the call returns, whatever becomes of the peg-in. + if !not_a_peg_in { + let active_utxo_key = active_federation_utxo_key(ctx, config, hardfork_cfg, rsk_height); + write_back_utxos(ctx, active_utxo_key); + if retiring.is_some() { + write_back_utxos(ctx, super::storage::OLD_FEDERATION_BTC_UTXOS_KEY); + } + } + 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 @@ -566,17 +661,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())); @@ -623,12 +718,21 @@ pub fn register_btc_transaction( match super::pegin_instructions::build_pegin_instructions(&btc_tx) { Ok(opt) => opt, Err(_) => { - // processPegIn: parse exception. RSKIP181 logs - // PEGIN_V1_INVALID_PAYLOAD(4); refund to the sender (the only - // refund address available — a malformed payload has no v1 - // refund), then mark processed. - if hardfork_cfg.has_rskip181(rsk_height) { - super::events::log_rejected_pegin(ctx, &btc_txid_event_bytes(&btc_tx), 4); + // rskj `PeginInformation.parse` threw: before RSKIP379 into + // `legacyRegisterPegin`'s catch, from it into `evaluatePegin`'s, + // which makes a (REFUND or NO_REFUND, PEGIN_V1_INVALID_PAYLOAD) of + // it. Both log rejected_pegin(PEGIN_V1_INVALID_PAYLOAD=4) (before + // RSKIP379 from RSKIP181), then refund to the sender, the only + // refund address a payload that did not parse leaves, or, with no + // sender, log unrefundable_pegin(LEGACY_PEGIN_UNDETERMINED_SENDER=1), + // the version being still 0 (`refundTxSender`, + // `handleNonRefundablePegin`). A refund marks the transaction + // processed; no refund does too before RSKIP379, and from it only + // between RSKIP459 and RSKIP551. + let hash = btc_txid_event_bytes(&btc_tx); + let logs = hardfork_cfg.has_rskip181(rsk_height); + if logs { + super::events::log_rejected_pegin(ctx, &hash, 4); } match &sender { Some(s) => { @@ -665,14 +769,25 @@ pub fn register_btc_transaction( ], ), } - if let Some(sender) = &sender { - let (refund_hash160, refund_is_p2sh) = sender_refund_target(sender); - emit_pegin_rejection_release( - ctx, &btc_tx, &refund_hash160, refund_is_p2sh, total_value, &fed_redeem, - &retiring, &pays_live_federation, rsk_height, config, hardfork_cfg, tx_ctx, - ); + let mark = match &sender { + Some(sender) => { + let (refund_hash160, refund_is_p2sh) = sender_refund_target(sender); + emit_pegin_rejection_release( + ctx, &btc_tx, &refund_hash160, refund_is_p2sh, total_value, &fed_redeem, + &retiring, &pays_live_federation, rsk_height, config, hardfork_cfg, tx_ctx, + )?; + true + } + None => { + if logs { + super::events::log_unrefundable_pegin(ctx, &hash, 1); + } + !rskip379 || should_mark_rejected_pegin_as_processed(hardfork_cfg, rsk_height) + } + }; + if mark { + set_btc_tx_processed(ctx, &legacy_txid, rsk_height, hardfork_cfg.has_rskip134(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())); } } @@ -688,14 +803,17 @@ pub fn register_btc_transaction( // ---- Version 1 (RSKIP170 OP_RETURN) peg-in ---- if let Some(instr) = instructions { - // processPegInVersion1: NO whitelist, only the locking cap. - if !cap_ok(ctx) { + // processPegInVersion1: NO whitelist, only the locking cap. From + // RSKIP379 `evaluatePegin` registers a v1 peg-in with `executePegIn`, + // and the cap isn't asked. + if !rskip379 && !cap_ok(ctx) { if hardfork_cfg.has_rskip181(rsk_height) { super::events::log_rejected_pegin(ctx, &btc_txid_event_bytes(&btc_tx), 1); // PEGIN_CAP_SURPASSED } // refundTxSender: refund to the v1 refund address if present, else - // the BtcLockSender address; if neither, no refund (a non-refundable - // peg-in — unrefundable_pegin event not modeled, see TODO). + // the BtcLockSender address; if neither, no refund, and from RSKIP181 + // unrefundable_pegin(PEGIN_V1_REFUND_ADDRESS_NOT_SET=2), the version + // being 1. The transaction is marked processed either way. match v1_refund_target(&instr, sender.as_ref()) { Some((h, p2sh)) => { let mut extra = refund_fields(&h, p2sh, config); @@ -737,7 +855,9 @@ pub fn register_btc_transaction( emit_pegin_rejection_release( ctx, &btc_tx, &refund_hash160, refund_is_p2sh, total_value, &fed_redeem, &retiring, &pays_live_federation, rsk_height, config, hardfork_cfg, tx_ctx, - ); + )?; + } else if hardfork_cfg.has_rskip181(rsk_height) { + super::events::log_unrefundable_pegin(ctx, &btc_txid_event_bytes(&btc_tx), 2); } set_btc_tx_processed(ctx, &legacy_txid, rsk_height, hardfork_cfg.has_rskip134(rsk_height)); return Ok(PrecompileOutput::new(gas_cost, Bytes::new())); @@ -746,13 +866,14 @@ pub fn register_btc_transaction( // executePegIn: credit the v1 destination from the OP_RETURN payload. let dest = RskAddress::from(instr.rsk_destination); if !rbtc_amount.is_zero() { - let _ = ctx.journal_mut().transfer(BRIDGE_ADDR, dest, rbtc_amount); + let _ = bridge_transfer(ctx, dest, rbtc_amount); } else { // transferTo(dest, 0) still creates+persists the account (RSK keeps // empty touched accounts — see the legacy path below). 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( @@ -787,8 +908,13 @@ pub fn register_btc_transaction( // (NO_REFUND, LEGACY_PEGIN_UNDETERMINED_SENDER). Post-RSKIP379 // `handleNonRefundablePegin` logs rejected_pegin(LEGACY_PEGIN_UNDETERMINED_SENDER=3) // then unrefundable_pegin(NonRefundablePeginReason.LEGACY_PEGIN_UNDETERMINED_SENDER=1), - // and marks the tx processed only when RSKIP459 && !RSKIP551. Pre-379 the - // legacy path aborts silently (sender could not be determined). + // and marks the tx processed only when RSKIP459 && !RSKIP551. Before + // RSKIP379 `PeginInformation.parse` throws, finding neither a sender nor + // instructions, and from RSKIP170 `legacyRegisterPegin`'s catch logs + // rejected_pegin(PEGIN_V1_INVALID_PAYLOAD=4) (from RSKIP181), then + // `refundTxSender`, with no refund address, unrefundable_pegin( + // LEGACY_PEGIN_UNDETERMINED_SENDER=1) (from RSKIP181), and marks the tx + // processed (testnet #4,513,687). Before RSKIP170 it does none of that. record_pegin( SilentKind::PeginRejectedNoRefund, &btc_tx, @@ -796,9 +922,9 @@ pub fn register_btc_transaction( total_value, "LEGACY_PEGIN_UNDETERMINED_SENDER", vec![ - // Post-RSKIP379 this path logs rejected_pegin then - // unrefundable_pegin; pre-379 it aborts silently. - ("logged", FieldValue::Flag(hardfork_cfg.has_rskip379(rsk_height))), + // From RSKIP181 this path logs rejected_pegin then + // unrefundable_pegin, before RSKIP379 and after it. + ("logged", FieldValue::Flag(hardfork_cfg.has_rskip181(rsk_height))), ("senderType", FieldValue::Text("UNDETERMINED".into())), ("inputShape", FieldValue::Text(undetermined_input_shape(&btc_tx).into())), ( @@ -818,7 +944,7 @@ pub fn register_btc_transaction( ), ), ("peginVersion", FieldValue::U64(0)), - ("logged", FieldValue::Flag(hardfork_cfg.has_rskip379(rsk_height))), + ("logged", FieldValue::Flag(hardfork_cfg.has_rskip181(rsk_height))), ], ); if hardfork_cfg.has_rskip379(rsk_height) { @@ -828,6 +954,13 @@ pub fn register_btc_transaction( if 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)); } + } else if hardfork_cfg.has_rskip170(rsk_height) { + if hardfork_cfg.has_rskip181(rsk_height) { + let hash = btc_txid_event_bytes(&btc_tx); + super::events::log_rejected_pegin(ctx, &hash, 4); + super::events::log_unrefundable_pegin(ctx, &hash, 1); + } + set_btc_tx_processed(ctx, &legacy_txid, rsk_height, hardfork_cfg.has_rskip134(rsk_height)); } return Ok(PrecompileOutput::new(gas_cost, Bytes::new())); }; @@ -854,33 +987,81 @@ 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) + } + }; + + // RSKIP379 (`registerPegIn`): `PegUtils.evaluateLegacyPegin` registers a + // P2PKH or P2SH-P2WPKH sender's peg-in with `executePegIn`, asking neither + // the lock whitelist nor the locking cap, and refunds a multisig or + // P2SH-P2WSH sender's, unless its outputs pay live federations of + // different types (`hasOutputsToDifferentTypesOfFeds`): that one is turned + // down with no refund, logging rejected_pegin(LEGACY_PEGIN_MULTISIG_SENDER=2) + // and unrefundable_pegin(OUTPUTS_SENT_TO_DIFFERENT_TYPES_OF_FEDS=4), and + // marked processed only between RSKIP459 and RSKIP551 + // (`handleNonRefundablePegin`). Reading the outputs throws for one that is + // neither P2PKH nor P2SH, which rskj doesn't catch: the call fails. + if rskip379 && !lockable { + let different = outputs_to_different_types_of_feds( + &btc_tx, + (&fed_script, active_format), + retiring.as_ref().map(|(_, script)| (script, retiring_format)), + ) + .map_err(|why| { + PrecompileError::other(format!( + "registerBtcTransaction: rskj aborts reading this transaction's outputs ({why})" + )) + })?; + if different { + record_pegin( + SilentKind::PeginRejectedNoRefund, + &btc_tx, + tx_ctx, + total_value, + "LEGACY_PEGIN_MULTISIG_SENDER", + vec![ + ("senderType", FieldValue::Text(sender_type_name(Some(&sender)).into())), + ("peginVersion", FieldValue::U64(0)), + ("logged", FieldValue::Flag(true)), + ], + ); + let hash = btc_txid_event_bytes(&btc_tx); + super::events::log_rejected_pegin(ctx, &hash, 2); + super::events::log_unrefundable_pegin(ctx, &hash, 4); + if 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())); + } + } - // Lock whitelist gate (rskj verifyLockSenderIsWhitelisted): the matching - // one-off entry is consumed on success; rejection generates a refund - // peg-out back to the sender (generateRejectionRelease). + // Before RSKIP379, the lock whitelist gate (rskj + // verifyLockSenderIsWhitelisted): the matching one-off entry is consumed on + // success; rejection generates a refund peg-out back to the sender + // (generateRejectionRelease). // Locking cap gate (rskj verifyLockDoesNotSurpassLockingCap, RSKIP134): // evaluated only when the whitelist passed (rskj short-circuits the &&, // so a whitelist rejection never lazily initializes the cap). A cap // rejection produces the same refund release as a whitelist one. let allowed = lockable - && whitelist_allows_and_consume( - ctx, - &sender_hash160, - total_value, - btc_block_height, - hardfork_cfg.has_rskip87(rsk_height), - ) - && cap_ok(ctx); + && (rskip379 + || (whitelist_allows_and_consume( + ctx, + &sender_hash160, + total_value, + btc_block_height, + hardfork_cfg.has_rskip87(rsk_height), + ) && cap_ok(ctx))); tracing::debug!( "pegin lock check: sender {} amount {} btc_height {} -> {}", to_hex(&sender_hash160), @@ -952,20 +1133,20 @@ pub fn register_btc_transaction( emit_pegin_rejection_release( ctx, &btc_tx, &sender_hash160, sender_is_p2sh, total_value, &fed_redeem, &retiring, &pays_live_federation, rsk_height, config, hardfork_cfg, tx_ctx, - ); + )?; set_btc_tx_processed(ctx, &legacy_txid, rsk_height, hardfork_cfg.has_rskip134(rsk_height)); 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 { if !rbtc_amount.is_zero() { - let _ = ctx.journal_mut().transfer(BRIDGE_ADDR, dest, rbtc_amount); + let _ = bridge_transfer(ctx, dest, rbtc_amount); } else { // rskj executePegIn always calls transferTo(dest, amount); a 0-value // transfer still creates and persists the destination account (RSK @@ -974,6 +1155,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). @@ -1085,7 +1267,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())), ]; @@ -1158,7 +1341,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), @@ -1178,6 +1361,42 @@ fn v1_refund_target( sender.map(sender_refund_target) } +/// rskj `PegUtils.hasOutputsToDifferentTypesOfFeds`, which RSKIP379's +/// `evaluateLegacyPegin` asks of a multisig or P2SH-P2WSH sender's peg-in: +/// whether the live federations its outputs pay, each a federation's P2SH +/// script and format version, are two of different format versions. Every +/// output is read by bitcoinj's `Script.getPubKeyHash` and matched against +/// each live federation's P2SH hash, the active one first +/// (`BridgeBtcWallet.getDestinationFederation`), so a P2PKH output to a +/// federation's hash counts as paying it. An output that is neither P2PKH nor +/// P2SH throws, which rskj doesn't catch: `Err`, and the call fails. +fn outputs_to_different_types_of_feds( + tx: &BtcTransaction, + active: (&bitcoin::ScriptBuf, u64), + retiring: Option<(&bitcoin::ScriptBuf, u64)>, +) -> Result { + let hash_of = |script: &bitcoin::ScriptBuf| -> [u8; 20] { + script.as_bytes()[2..22].try_into().expect("a federation's 23-byte P2SH script") + }; + let (mut pays_active, mut pays_retiring) = (false, false); + for output in &tx.output { + let hash = super::rskj_sender_compat::pub_key_hash(output.script_pubkey.as_bytes())?; + if hash == hash_of(active.0) { + pays_active = true; + } else if retiring.is_some_and(|(script, _)| hash == hash_of(script)) { + pays_retiring = true; + } + } + match (pays_active, retiring) { + (true, Some((_, retiring_format))) if pays_retiring => Ok(active.1 != retiring_format), + _ if pays_active || pays_retiring => Ok(false), + // An empty set's `iterator.next()`. RSKIP379's minimum check, which + // comes first, wants an output to a live federation, so it isn't + // reached. + _ => Err("NoSuchElementException: the outputs pay no live federation"), + } +} + /// rskj `generateRejectionRelease`: build an empty-wallet refund of all the /// outputs paying the live federation(s) back to `refund_hash160`, enqueue it /// in the pegouts-waiting-for-confirmations set, and (post-RSKIP146) log @@ -1196,7 +1415,7 @@ fn emit_pegin_rejection_release( config: &BridgeConstants, hardfork_cfg: &RskHardforkConfig, tx_ctx: &BridgeTxContext, -) { +) -> Result<(), PrecompileError> { // TODO(rustock): a rejected peg-in paying BOTH live federations would // need per-input redeem scripts in the refund; none exists on // mainnet pre-wasabi. Inputs paying the retiring federation use its @@ -1215,13 +1434,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 }; @@ -1230,13 +1464,17 @@ fn emit_pegin_rejection_release( } else { p2pkh_output_script(refund_hash160) }; + // bitcoinj throws where it cannot size the inputs + // (`release_tx::signatures_required`), and so does the peg-in's + // registration: nothing of it is kept, and it is not marked processed. let built = super::release_tx::build_empty_wallet_to( &pegin_utxos, &refund_script, refund_redeem, fee_per_kb, tx_version, - ); + format >= 4000, + )?; tracing::debug!( "pegin rejection release: utxos {} fee_per_kb {} built {:?}", pegin_utxos.len(), @@ -1260,6 +1498,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 @@ -1281,86 +1520,174 @@ fn emit_pegin_rejection_release( ], ); } + Ok(()) } -/// 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), + })); } } @@ -1461,17 +1788,19 @@ 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 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,30 +1809,32 @@ 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 } /// rskj `BridgeSupport.registerNewUtxos`: register every output paying the -/// given federation script into the given UTXO storage set. +/// given federation script into the given UTXO storage set. rskj reads the +/// set whether or not the transaction pays the federation, and `save()` +/// writes it back, the empty list where the cell held nothing. fn register_federation_outputs( ctx: &mut CTX, btc_tx: &BtcTransaction, @@ -1524,9 +1855,6 @@ fn register_federation_outputs( coinbase: btc_tx.is_coinbase(), }) .collect(); - if new_utxos.is_empty() { - return; - } let mut utxos = load_utxos_at(ctx, utxo_key); utxos.extend(new_utxos); store_utxos_at(ctx, utxo_key, &utxos); @@ -1568,14 +1896,41 @@ fn flyover_code_output(code: i64) -> Bytes { Bytes::copy_from_slice(&value.to_be_bytes::<32>()) } +/// rskj `BridgeUtils.deserializeBtcAddressWithVersion`, for a Flyover BTC +/// address argument, and `serializeBtcAddressWithVersion` of the address it +/// builds: the 21 bytes, `[version || hash160]`, that the derivation hash and +/// the refund read. `None` where rskj throws, which +/// `Bridge.registerFlyoverBtcTransaction` answers with GENERIC_ERROR. +/// +/// Before RSKIP284 (`BridgeUtilsLegacy.deserializeBtcAddressWithVersionLegacy`) +/// the version is `addressBytes[0]`, a signed Java byte, and the bytes after +/// the 21st are ignored; an array shorter than 21 throws in its `arraycopy`. +/// From RSKIP284 the array must be exactly 21 bytes, and the version is read +/// unsigned. Either way bitcoinj's `Address(params, version, hash160)` then +/// refuses a negative version (`VersionedChecksummedBytes`) and one its +/// network doesn't accept (`WrongNetworkException`): mainnet 0 and 5, testnet +/// and regtest 111 and 196. So before RSKIP284 a testnet P2SH address, version +/// 196 or `0xc4`, which is -60 as a signed byte, is refused (testnet +/// #2,122,593). That also makes the written version one byte in both eras: +/// `BigInteger.valueOf(version).toByteArray()` is one byte for the versions +/// that get through before RSKIP284 (0 to 127), and `intToBytesNoLeadZeroes` +/// is from it. +fn flyover_btc_address(bytes: &[u8], config: &BridgeConstants, rskip284: bool) -> Option<[u8; 21]> { + if rskip284 && bytes.len() != 21 { + return None; + } + let address: [u8; 21] = bytes.get(..21)?.try_into().ok()?; + let version = if rskip284 { i32::from(address[0]) } else { i32::from(address[0] as i8) }; + let accepted = [super::getters::p2pkh_version(config), super::getters::p2sh_version(config)]; + accepted.iter().any(|&v| i32::from(v) == version).then_some(address) +} + /// rskj `PegUtils.getFlyoverDerivationHash`: /// `keccak256(derivationArgumentsHash(32) || userRefundAddrBytes || /// lbcAddress(20) || lpBtcAddrBytes)`. Note the array-copy order puts the LBC /// address BEFORE the LP address (NOT the parameter order). The BTC-address -/// bytes are `serializeBtcAddressWithVersion`; pre-RSKIP284 (iris) that -/// round-trips the input `[version || hash160]` arg unchanged (a single version -/// byte for every mainnet address version), so the raw 21-byte ABI args are -/// used verbatim. +/// bytes are `serializeBtcAddressWithVersion`'s, as [`flyover_btc_address`] +/// returns them. fn flyover_derivation_hash( derivation_arguments_hash: &[u8; 32], user_refund_addr_bytes: &[u8], @@ -1607,9 +1962,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, @@ -1631,52 +1986,61 @@ 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 = crate::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)); }; // rskj parses BOTH BTC addresses in the ABI layer (`Bridge.registerFlyover- - // BtcTransaction`) via `BridgeUtils.deserializeBtcAddressWithVersion`, wrapped - // in a try/catch that returns GENERIC_ERROR on ANY exception — and this runs - // BEFORE `bridgeSupport.registerFlyoverBtcTransaction`'s isContractTx/sender - // checks. Post-RSKIP284 (hop400) the deserializer REQUIRES exactly 21 bytes - // (`[version || 20-byte hash160]`), throwing `BridgeIllegalArgumentException` - // otherwise; pre-RSKIP284 the legacy path only needs ≥21 bytes (a shorter - // array throws `ArrayIndexOutOfBoundsException` in its `arraycopy`). Either - // way a bad-length address yields GENERIC_ERROR. The userRefund address is - // parsed first, the LP address second (mainnet #5,967,453: a 33-byte - // userRefund address makes rskj return GENERIC_ERROR, not a validations code). + // BtcTransaction`) via `BridgeUtils.deserializeBtcAddressWithVersion`, the + // userRefund address first, wrapped in a try/catch that returns + // GENERIC_ERROR on ANY exception -- and this runs BEFORE + // `bridgeSupport.registerFlyoverBtcTransaction`'s isContractTx/sender + // checks. [`flyover_btc_address`] says what it refuses: mainnet #5,967,453 + // has a 33-byte userRefund address, refused from RSKIP284, and testnet + // #2,122,593 a testnet P2SH address, refused before it. let rskip284 = hardfork_cfg.has_rskip284(rsk_height_of(ctx)); - let address_len_ok = |bytes: &[u8]| -> bool { - if rskip284 { bytes.len() == 21 } else { bytes.len() >= 21 } - }; - if !address_len_ok(&user_refund_addr) || !address_len_ok(&lp_btc_addr) { + let (Some(user_refund_addr), Some(lp_btc_addr)) = ( + flyover_btc_address(&user_refund_addr, config, rskip284), + flyover_btc_address(&lp_btc_addr, config, rskip284), + ) else { return ok(flyover_code_output(flyover_codes::GENERIC_ERROR)); - } + }; // rskj `isContractTx`: the rskTx must be an InternalTransaction (the Bridge // was reached via a contract CALL), i.e. call depth > 1. @@ -1688,16 +2052,14 @@ 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); let block_number = revm::context_interface::Block::number(ctx.block()).to::(); - let use_v2 = hardfork_cfg.has_stored_block_v2(block_number); - let rskip199 = hardfork_cfg.has_rskip199(block_number); - super::btc_chain::ensure_btc_chain_seeded(ctx, config, use_v2, rskip199); + // A store that can't be seeded throws, which rskj answers as any throw. + if super::btc_chain::ensure_btc_block_store(ctx, config, hardfork_cfg).is_err() { + return ok(flyover_code_output(flyover_codes::GENERIC_ERROR)); + } // getFlyoverDerivationHash (over the raw 21-byte address args; see helper). let flyover_hash = flyover_derivation_hash( @@ -1712,51 +2074,35 @@ 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; + // 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 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; + Err(Invalid::Thrown(_) | Invalid::IllegalArgument(_)) => { + return ok(flyover_code_output(flyover_codes::GENERIC_ERROR)) } - 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)); } - // 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"))?; + // 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); // Second already-used check, keyed by the legacy txid (only when it differs @@ -1790,8 +2136,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) { @@ -1814,6 +2160,21 @@ pub fn register_fast_bridge_btc_transaction( None }; + // Before RSKIP293 `getAmountSentToAddresses` is + // `BridgeUtilsLegacy.getAmountSentToAddress`, which reads every output as an + // address with bitcoinj's `Script.getToAddress`. That throws for one that is + // neither P2PKH nor P2SH -- a native segwit output, an OP_RETURN, a script it + // can't parse -- and `Bridge.registerFlyoverBtcTransaction` answers the throw + // with GENERIC_ERROR, nothing transferred or registered. + if !hardfork_cfg.has_rskip293(block_number) + && btc_tx + .output + .iter() + .any(|o| !bitcoinj_reads_as_address(o.script_pubkey.as_bytes())) + { + return ok(flyover_code_output(flyover_codes::GENERIC_ERROR)); + } + // validateFlyoverPeginValue: the amount sent to the flyover address(es) must // be non-zero (getAmountSentToAddresses sums outputs paying any flyover // address — under RSKIP293 both active and retiring). Under RSKIP293 it then @@ -1890,9 +2251,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()))); @@ -1910,8 +2279,8 @@ 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 } else { @@ -1947,8 +2316,9 @@ pub fn register_fast_bridge_btc_transaction( // transferTo(lbcAddress, amount): credit the LBC contract. let rbtc_amount = btc_satoshi_to_rbtc_wei(total_satoshis); if !rbtc_amount.is_zero() { - let _ = ctx.journal_mut().transfer(BRIDGE_ADDR, lbc_address, rbtc_amount); + let _ = bridge_transfer(ctx, 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) @@ -1983,6 +2353,25 @@ pub fn register_fast_bridge_btc_transaction( ok(Bytes::copy_from_slice(&rbtc_amount.to_be_bytes::<32>())) } +/// Whether bitcoinj's `Script.getToAddress` reads `script` as an address rather +/// than throwing: the script parses, and is P2PKH by its chunks +/// (`isSentToAddress`, a push of 20 bytes between `OP_DUP OP_HASH160` and +/// `OP_EQUALVERIFY OP_CHECKSIG`, however the push is written) or P2SH by its +/// bytes (`isPayToScriptHash`, the 23-byte template). +fn bitcoinj_reads_as_address(script: &[u8]) -> bool { + use super::release_tx::{parse_chunks, Chunk}; + let Some(chunks) = parse_chunks(script) else { + return false; + }; + let p2pkh = matches!( + chunks.as_slice(), + [Chunk::Op(0x76), Chunk::Op(0xa9), Chunk::Data(hash), Chunk::Op(0x88), Chunk::Op(0xac)] + if hash.len() == 20 + ); + let p2sh = script.len() == 23 && script[0] == 0xa9 && script[1] == 0x14 && script[22] == 0x87; + p2pkh || p2sh +} + /// Flyover federation information for one federation (active or retiring): the /// destination fed's redeem-script P2SH hash, plus the derived flyover redeem, /// its P2SH hash, and its P2SH output script. Mirrors rskj @@ -2024,17 +2413,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 @@ -2093,21 +2514,45 @@ 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`). +/// hash, or none where nothing is kept under it. Decodes the bitcoinj `VarInt` +/// concatenation (`UtxoUtils`). fn load_release_outpoints_values( ctx: &mut CTX, btc_tx_hash_display: &[u8; 32], -) -> Vec { +) -> Option> { let key = compound_key( super::storage::RELEASES_OUTPOINTS_VALUES_KEY, "-", &to_hex(btc_tx_hash_display), ); - let Some(bytes) = super::storage::bridge_load_raw(ctx, key) else { - return Vec::new(); - }; + let bytes = super::storage::bridge_load_raw(ctx, key)?; let mut values = Vec::new(); let mut i = 0; while i < bytes.len() { @@ -2129,7 +2574,7 @@ fn load_release_outpoints_values( values.push(v); i += consumed; } - values + Some(values) } /// rskj `getStorageKeyForFlyoverHash`: @@ -2237,10 +2682,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, @@ -2248,22 +2694,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(); +) -> Result<(), PrecompileError> { + // 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, @@ -2275,6 +2720,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 { @@ -2289,30 +2743,40 @@ 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 { + // 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, ); + process_release_transaction_info(ctx, &built, hardfork_cfg, rsk_height); store_pegout_confirmation_set(ctx, &waiting, use_tx_hash); } + Ok(()) } /// rskj `ReleaseTransactionBuilder.buildEmptyWalletTo` over a multi-script @@ -2322,34 +2786,35 @@ 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 size limit is +/// `release_tx::fill_inputs`'s. It refuses, and throws, where +/// `release_tx::build_empty_wallet_to` does. fn build_flyover_empty_wallet_multi( utxos: &[BridgeUtxo], input_redeems: &[&[u8]], refund_script: &bitcoin::ScriptBuf, fee_per_kb: u64, version: i32, -) -> Option { + is_segwit: bool, +) -> Result, super::release_tx::UncountableSignatures> { 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. - const SIG_SIZE: usize = 75; + // bitcoinj's minimum fee (see release_tx.rs): 5000 sat/kB. const REFERENCE_DEFAULT_MIN_TX_FEE: u64 = 5000; - const MAX_STANDARD_TX_SIZE: usize = 100_000; if utxos.is_empty() { - return None; + return Ok(None); } let thresholds: Vec = input_redeems .iter() .map(|r| super::release_tx::redeem_script_threshold(r)) .collect(); if thresholds.contains(&0) { - return None; + return Ok(None); } let gathered: u64 = utxos.iter().map(|u| u.value_satoshis).sum(); @@ -2383,32 +2848,30 @@ fn build_flyover_empty_wallet_multi( }], }; - let base_size = bitcoin::consensus::serialize(&tx).len(); - 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; + // 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 = super::release_tx::signing_bytes(input_redeems.iter().copied())?; + 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; + return Ok(None); } let value = gathered - fee; if value < super::release_tx::min_non_dust_value(refund_script) { - return None; + return Ok(None); } 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 bitcoin::consensus::serialize(&tx).len() > MAX_STANDARD_TX_SIZE { - return None; + if !super::release_tx::fill_inputs(&mut tx, input_redeems.iter().copied(), is_segwit) { + return Ok(None); } - Some(super::release_tx::BuiltPegout { tx, used_utxos: utxos.to_vec() }) + Ok(Some(super::release_tx::BuiltPegout { tx, used_utxos: utxos.to_vec() })) } // --------------------------------------------------------------------------- @@ -2454,7 +2917,15 @@ 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 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 @@ -2472,7 +2943,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, @@ -2482,8 +2953,15 @@ pub fn release_btc( ); return Ok(PrecompileOutput::new(gas_cost, Bytes::new())); } - // Pre-RSKIP185: rskj throws Program.OutOfGasException (no event, no refund). - return Err(PrecompileError::OutOfGas); + // Before RSKIP185 rskj throws `Program.OutOfGasException`: no event, no + // refund. `Bridge.releaseBtc` lets it through and `Bridge.execute` + // wraps it in a `VMException`, as it wraps any other throw, so the + // call fails as a throw does: before RSKIP197, which comes with + // RSKIP185 on every network, the contract that made the call fails + // with all its gas. + return Err(PrecompileError::other( + "releaseBtc: a contract asked for a peg-out", + )); } // rskj BridgeSupport.requestRelease: post-RSKIP219 the minimum is INCLUSIVE @@ -2499,22 +2977,24 @@ pub fn release_btc( let (active_keys, active_redeem) = active_federation_keys_and_redeem(ctx, config, hardfork_cfg, block_number); let num_sigs_required = (active_keys.len() / 2 + 1) as u64; + let segwit = active_federation_format(ctx, config, hardfork_cfg, block_number) >= 4000; let require_funds_for_fee = require_funds_for_fee( &active_redeem, num_sigs_required, fee_per_kb, hardfork_cfg.has_rskip271(block_number), + segwit, 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_u256 <= U256::from(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 @@ -2530,21 +3010,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 { - amount_satoshis_u256 * U256::from(10_000_000_000u64) + 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, @@ -2556,6 +3043,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. @@ -2564,17 +3054,17 @@ pub fn release_btc( let use_tx_hash_format = hardfork_cfg.has_rskip146(block_number); if use_tx_hash_format { - // Post-RSKIP146: store in releaseRequestQueueWithTxHash (triple format) - let key = bridge_storage_key(RELEASE_REQUEST_QUEUE_WITH_TXHASH_KEY); - let existing = bridge_load_bytes(ctx, key); - let mut queue = deserialize_release_queue_with_hash(&existing); + // Post-RSKIP146: rskj's `getReleaseRequestQueue` reads both cells, + // `releaseRequestQueue` and `releaseRequestQueueWithTxHash`, the + // request goes in with its hash, and `saveReleaseRequestQueue` writes + // both back, the legacy one as the empty list where it held nothing. + let mut queue = load_release_request_queue(ctx, true); queue.push(ReleaseRequest { btc_dest_hash160: btc_dest, amount_satoshis, rsk_tx_hash: Some(tx_ctx.rsk_tx_hash), }); - let updated = serialize_release_queue_with_hash(&queue); - bridge_store_bytes(ctx, key, &updated); + save_release_request_queue(ctx, &queue, true); } else { // Pre-RSKIP146: store in releaseRequestQueue (pair format) let key = bridge_storage_key(RELEASE_REQUEST_QUEUE_KEY); @@ -2599,7 +3089,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, @@ -2614,6 +3104,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 // --------------------------------------------------------------------------- @@ -2687,34 +3189,39 @@ pub fn update_collections( // `newFederationFormatVersion` (mainnet #1,591,009). super::federation::save_new_federation_multikey(ctx, hardfork_cfg, block_number); + // rskj reads the peg-outs waiting for confirmations once in the call, + // before the call adds any; how many it read sizes the set + // `processConfirmedPegouts` walks (`next_pegout_with_enough_confirmations`). + let loaded_pegouts = load_pegout_confirmation_set(ctx, use_tx_hash).len(); + // ----------------------------------------------------------------------- // Step 0: Funds migration (rskj processFundsMigration) — while the new // federation is in its migration window, each updateCollections call // moves the retiring federation's balance into the active federation // with a migration transaction queued like a peg-out. // ----------------------------------------------------------------------- - process_funds_migration(ctx, config, hardfork_cfg, block_number, tx_ctx); + process_funds_migration(ctx, config, hardfork_cfg, block_number, tx_ctx)?; // ----------------------------------------------------------------------- // Step 1: Process peg-out requests (rskj processPegoutRequests) // ----------------------------------------------------------------------- - let should_process_requests = if use_rskip271 { - // RSKIP271: batching — only process when nextPegoutHeight ≤ current block - let next_height = - super::storage::bridge_load_u256(ctx, NEXT_PEGOUT_HEIGHT_KEY).to::(); - block_number >= next_height - } else { - true // Pre-RSKIP271: process every block - }; + // RSKIP271: rskj's `processPegoutsInBatch` first reads the next peg-out + // height (`BridgeStorageProvider.getNextPegoutHeight`, 0 where the cell + // holds nothing), and `saveNextPegoutHeight` writes back what it read + // unless a new height was set. + let read_next_height = use_rskip271 + .then(|| super::storage::bridge_load_u256(ctx, NEXT_PEGOUT_HEIGHT_KEY).to::()); + // RSKIP271: batching -- only process when nextPegoutHeight <= current + // block. Pre-RSKIP271: process every block. + let should_process_requests = + read_next_height.is_none_or(|next_height| block_number >= next_height); + let mut next_height_set = false; if should_process_requests { // rskj `getReleaseRequestQueue` concatenates the legacy queue with the - // with-txhash one; it does not pick whichever is populated. Reading - // only one was correct on mainnet by circumstance -- the legacy queue - // was drained before RSKIP146 activated at iris300 (#3,614,800) and - // every request since carries a tx hash -- but it is not correct by - // construction, and an entry that ever landed in the legacy queue - // afterwards would have been silently stranded there forever. + // with-txhash one, from RSKIP146 (papyrus200, mainnet #2,392,700); it + // does not pick whichever is populated, so a request queued before + // RSKIP146 and still waiting is read with those queued since. let pending = load_release_request_queue(ctx, use_tx_hash); // RSKIP271: rskj processPegoutsInBatch sets nextPegoutHeight whenever @@ -2801,23 +3308,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 @@ -2840,7 +3331,13 @@ pub fn update_collections( let spent_by_federation = sum_inputs.saturating_sub(change); if spent_by_federation < amount { let to_burn = amount - spent_by_federation; - let _ = ctx.journal_mut().transfer( + let _ = bridge_transfer( + ctx, + 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), @@ -2865,6 +3362,9 @@ pub fn update_collections( script: p2pkh_output_script(&request.btc_dest_hash160), amount_satoshis: request.amount_satoshis, }]; + // A build bitcoinj cannot size throws, and so does + // this call, with no request processed (testnet + // #2,237,507). match super::release_tx::complete_pegout_tx( &available, &outputs, @@ -2873,7 +3373,7 @@ pub fn update_collections( fee_per_kb, tx_version, is_segwit, - ) { + )? { Some(built) => { settle( ctx, @@ -2891,34 +3391,49 @@ pub fn update_collections( kept.extend(to_retry); save_release_request_queue(ctx, &kept, use_tx_hash); } else { - // RSKIP271+ (processPegoutsInBatch): one batched BTC tx - // for the whole queue, keyed by this updateCollections - // call's tx hash. - let outputs: Vec = pending - .iter() - .map(|r| super::release_tx::PegoutOutput { - script: p2pkh_output_script(&r.btc_dest_hash160), - amount_satoshis: r.amount_satoshis, - }) - .collect(); - if let Some(built) = super::release_tx::complete_pegout_tx( - &available, - &outputs, - &change_script, - redeem_for, - fee_per_kb, - tx_version, - is_segwit, - ) { - let total: u64 = pending.iter().map(|r| r.amount_satoshis).sum(); - // rskj logBatchPegoutCreated needs the batch BTC tx hash - // and the per-request creation RSK tx hashes; capture - // them before `settle` consumes `built`. - let batch_btc_tx_hash = btc_txid_event_bytes(&built.tx); - let batch_rsk_tx_hashes: Vec<[u8; 32]> = pending + // RSKIP271+ (processPegoutsInBatch): one batched BTC tx, + // keyed by this updateCollections call's tx hash. While + // bitcoinj finds the transaction over its size and there + // is more than one request, rskj halves the batch, keeping + // the first half of the queue (`subList(0, size / 2)`). + let build = |batch: &[ReleaseRequest]| { + let outputs: Vec = batch .iter() - .map(|r| r.rsk_tx_hash.unwrap_or(tx_ctx.rsk_tx_hash)) + .map(|r| super::release_tx::PegoutOutput { + script: p2pkh_output_script(&r.btc_dest_hash160), + amount_satoshis: r.amount_satoshis, + }) .collect(); + super::release_tx::build_pegout_tx( + &available, + &outputs, + &change_script, + redeem_for, + fee_per_kb, + tx_version, + is_segwit, + ) + }; + let mut batched: &[ReleaseRequest] = &pending; + let mut result = build(batched)?; + while batched.len() > 1 + && matches!( + result, + Err(super::release_tx::BuildRefusal::ExceededMaxTransactionSize) + ) + { + batched = &batched[..batched.len() / 2]; + result = build(batched)?; + } + if let Ok(built) = result { + // rskj settles the batch with what the whole queue asked + // for (`totalPegoutValue`), batched or not: the amount + // `release_requested` logs, and what + // `adjustBalancesIfChangeOutputWasDust` burns against. + let total: u64 = pending.iter().map(|r| r.amount_satoshis).sum(); + // rskj logBatchPegoutCreated needs the batch BTC tx hash; + // capture it before `settle` consumes `built`. + let batch_btc_tx_hash = btc_txid_event_bytes(&built.tx); // RSKIP379: index this pegout by the legacy sighash of // its first input (BridgeSupport.savePegoutTxSigHash, // inside settleReleaseRequest). Compute before `settle` @@ -2945,20 +3460,36 @@ pub fn update_collections( ); // rskj BridgeSupport.processPegoutsInBatch logs // batch_pegout_created right after settleReleaseRequest - // (which already logged release_requested), listing every - // batched request's RSK creation tx hash. + // (which already logged release_requested), listing each + // batched request's RSK creation tx hash. A request + // queued before RSKIP146 has none, and + // `serializeRskTxHashes` throws on it: so does the call. + let Some(batch_rsk_tx_hashes) = batched + .iter() + .map(|r| r.rsk_tx_hash) + .collect::>>() + else { + return Err(PrecompileError::other( + "updateCollections: a batched peg-out request has no creation hash", + )); + }; super::events::log_batch_pegout_created( ctx, &batch_btc_tx_hash, &batch_rsk_tx_hashes, ); created_any = true; - // The whole queue was batched into one BTC tx, so it - // is now empty (rskj removeEntries(pegoutEntries)). - // rskj re-serializes the emptied queue, so the cell - // holds RLP.encodeList([]) = [0xc0], NOT empty bytes. - save_release_request_queue(ctx, &[], use_tx_hash); - queue_emptied = true; + // rskj `removeEntries` is `List.removeAll`: every queued + // request equal to a batched one goes. The queue is + // re-serialized, so an emptied cell holds + // RLP.encodeList([]) = [0xc0], NOT empty bytes. + let kept: Vec = pending + .iter() + .filter(|r| !batched.iter().any(|b| same_release_request(b, r))) + .cloned() + .collect(); + save_release_request_queue(ctx, &kept, use_tx_hash); + queue_emptied = kept.is_empty(); } } @@ -2982,8 +3513,12 @@ pub fn update_collections( NEXT_PEGOUT_HEIGHT_KEY, U256::from(next_height), ); + next_height_set = true; } } + if let (Some(read), false) = (read_next_height, next_height_set) { + super::storage::bridge_store_u256(ctx, NEXT_PEGOUT_HEIGHT_KEY, U256::from(read)); + } // ----------------------------------------------------------------------- // Step 2: Promote ONE confirmed pegout → waiting for signatures @@ -2997,6 +3532,7 @@ pub fn update_collections( &waiting, block_number, min_confirmations, + loaded_pegouts, ); if let Some(pos) = confirmed_pos { @@ -3025,9 +3561,16 @@ pub fn update_collections( // - RSKIP375+: the pegout creation RSK tx hash // - RSKIP146..RSKIP176: creation hash, falling back to this tx // - otherwise (incl. pre-RSKIP146): this updateCollections tx hash - #[allow(clippy::if_same_then_else)] // distinct rskj rules that coincide today - let rsk_hash = if hardfork_cfg.has_rskip375(block_number) { - entry.rsk_tx_hash.unwrap_or(tx_ctx.rsk_tx_hash) + let rskip375 = hardfork_cfg.has_rskip375(block_number); + let rsk_hash = if rskip375 { + // A peg-out of no creation hash, made before RSKIP146, is + // looked up in rskj's `TreeMap` under null, which throws. + let Some(creation) = entry.rsk_tx_hash else { + return Err(PrecompileError::other( + "updateCollections: a confirmed peg-out has no creation hash", + )); + }; + creation } else if use_tx_hash && !hardfork_cfg.has_rskip176(block_number) { entry.rsk_tx_hash.unwrap_or(tx_ctx.rsk_tx_hash) } else { @@ -3037,6 +3580,14 @@ pub fn update_collections( let wfs_key = bridge_storage_key(PEGOUTS_WAITING_FOR_SIGNATURES_KEY); let wfs_data = bridge_load_bytes(ctx, wfs_key); let mut wfs = deserialize_rsk_txs_waiting_for_signatures(&wfs_data); + // rskj `checkIfEntryExistsInPegoutsWaitingForSignatures`: from + // RSKIP375 a key the set already holds is not taken over; the call + // throws, and both sets stay as they were. + if rskip375 && wfs.contains_key(&rsk_hash) { + return Err(PrecompileError::other( + "updateCollections: a peg-out already waits for signatures under this key", + )); + } wfs.insert(rsk_hash, entry.btc_tx_raw); let updated_wfs = serialize_rsk_txs_waiting_for_signatures(&wfs); bridge_store_bytes(ctx, wfs_key, &updated_wfs); @@ -3062,7 +3613,7 @@ pub fn update_collections( // Step 4: drive the RSKIP419 Sign Validation Protocol for a proposed // federation (rskj updateSvpState). // ----------------------------------------------------------------------- - update_svp_state(ctx, config, hardfork_cfg, block_number, tx_ctx); + update_svp_state(ctx, config, hardfork_cfg, block_number, tx_ctx)?; Ok(PrecompileOutput::new(gas_cost, Bytes::new())) } @@ -3078,19 +3629,19 @@ fn update_svp_state( hardfork_cfg: &RskHardforkConfig, block_number: u64, tx_ctx: &BridgeTxContext, -) { +) -> Result<(), PrecompileError> { if !hardfork_cfg.has_rskip419(block_number) { - return; + return Ok(()); } let Some(proposed) = super::federation::load_stored_federation(ctx, PROPOSED_FEDERATION_KEY) else { - return; + return Ok(()); }; // isSvpOngoing: the proposal is still within its validation window. if block_number >= proposed.creation_block + config.validation_period_duration_in_blocks { process_svp_failure(ctx, &proposed, config, hardfork_cfg, block_number); - return; + return Ok(()); } // shouldCreateAndProcessSvpFundTransaction: every SVP value must be clear. @@ -3106,7 +3657,7 @@ fn update_svp_state( hardfork_cfg, block_number, tx_ctx, - ); + )?; } // Once the fund tx change has been registered (svpFundTxSigned set), build @@ -3123,6 +3674,7 @@ fn update_svp_state( tx_ctx, ); } + Ok(()) } /// rskj `BridgeSupport.processSvpFailure`: the proposed federation did not pass @@ -3173,12 +3725,12 @@ fn process_svp_fund_transaction_unsigned( hardfork_cfg: &RskHardforkConfig, block_number: u64, tx_ctx: &BridgeTxContext, -) { +) -> Result<(), PrecompileError> { let svp_fund_tx_outputs_value = config.minimum_pegout_tx_value * 2; let Some(built) = - build_svp_fund_transaction(ctx, proposed, svp_fund_tx_outputs_value, config, hardfork_cfg, block_number) + build_svp_fund_transaction(ctx, proposed, svp_fund_tx_outputs_value, config, hardfork_cfg, block_number)? else { - return; + return Ok(()); }; // setSvpFundTxHashUnsigned(tx.getHash()) = RLP element of the display-order txid. @@ -3250,6 +3802,7 @@ fn process_svp_fund_transaction_unsigned( super::events::log_pegout_transaction_created(ctx, &fund_hash, &outpoint_values); save_release_outpoints_values(ctx, &fund_hash, &outpoint_values, hardfork_cfg, block_number); } + Ok(()) } /// rskj `BridgeSupport.processSvpSpendTransactionUnsigned`: once the SVP fund @@ -3516,32 +4069,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. +pub(crate) 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), + } + } + 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; } - input.script_sig = - bitcoin::ScriptBuf::from_bytes(super::release_tx::placeholder_scriptsig(&redeem, threshold)); } - Some(btc_txid_event_bytes(©)) + 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. +pub(crate) 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))); + } + 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 @@ -3555,7 +4183,7 @@ fn build_svp_fund_transaction( config: &BridgeConstants, hardfork_cfg: &RskHardforkConfig, block_number: u64, -) -> Option { +) -> Result, super::release_tx::UncountableSignatures> { // Proposed federation redeem + P2SH (its TYPE is keyed on its creation block). let proposed_keys = proposed.btc_keys(); let proposed_redeem = build_committed_federation_redeem_script( @@ -3648,23 +4276,28 @@ fn build_svp_fund_transaction( /// `new PegoutsWaitingForConfirmations(entries)` → /// `this.entries = new HashSet<>(entries)` (constructor, NOT incremental). The /// `HashSet(Collection)` constructor pre-sizes the table to -/// `tableSizeFor(max((int)(n / 0.75f) + 1, 16))`, so for `n` final entries the -/// capacity is the next power of two `>= max((int)(n / 0.75) + 1, 16)`. This -/// differs from an incremental default-16 fill exactly at the resize boundaries -/// (e.g. `n = 12` -> cap 32 here, but an incremental fill would stay at 16; -/// `n = 24` -> 64 vs 32), and getting it wrong reshuffles the bucket walk and -/// forks the pegout selection (mainnet #3,344,303). Returns the index into -/// `entries`. -fn next_pegout_with_enough_confirmations( +/// `tableSizeFor(max((int)(n / 0.75f) + 1, 16))`, so for the `loaded` entries +/// the call read the capacity is the next power of two +/// `>= max((int)(n / 0.75) + 1, 16)`. This differs from an incremental +/// default-16 fill exactly at the resize boundaries (e.g. `n = 12` -> cap 32 +/// here, but an incremental fill would stay at 16; `n = 24` -> 64 vs 32), and +/// getting it wrong reshuffles the bucket walk and forks the pegout selection +/// (mainnet #3,344,303). The migration and the peg-outs the same call built +/// went into that same set one at a time (`PegoutsWaitingForConfirmations.add`), +/// and `HashMap.putVal` doubled its table whenever one left it more than +/// three quarters full (`walked_hashset_capacity`); `entries` holds them too. +/// Returns the index into `entries`. +pub(crate) fn next_pegout_with_enough_confirmations( entries: &[PegoutWaitingForConfirmations], current_block: u64, min_confirmations: u64, + loaded: usize, ) -> Option { if entries.is_empty() { return None; } - let cap = java_hashset_capacity(entries.len()); + let cap = walked_hashset_capacity(loaded, entries.len()); // Bucket each entry; within a bucket preserve insertion (storage) order. let mut buckets: BTreeMap> = BTreeMap::new(); @@ -3696,6 +4329,20 @@ fn java_hashset_capacity(n: usize) -> u32 { init_cap.next_power_of_two() } +/// The capacity of a set built from `loaded` entries (`java_hashset_capacity`) +/// once it holds `now`, the rest added one at a time: `HashMap.putVal` doubles +/// the table whenever an insertion leaves more than three quarters of it +/// taken (`++size > threshold`, the threshold `0.75` of the capacity). +fn walked_hashset_capacity(loaded: usize, now: usize) -> u32 { + let mut capacity = java_hashset_capacity(loaded); + for size in loaded + 1..=now { + if size as u64 > u64::from(capacity) * 3 / 4 { + capacity *= 2; + } + } + capacity +} + /// rskj `PegoutsWaitingForConfirmations.Entry.hashCode()`: /// `Objects.hash(btcTransaction, pegoutCreationRskBlockNumber)` /// `= 31 * (31 * 1 + btcTransaction.hashCode()) + Long.hashCode(height)`. @@ -3718,22 +4365,22 @@ fn pegout_entry_hash(entry: &PegoutWaitingForConfirmations) -> u32 { 31u32.wrapping_mul(r).wrapping_add(long_hash_code) } -/// rskj `BridgeSupport.processFundsMigration` (pre-RSKIP294/376 era). +/// rskj `BridgeSupport.processFundsMigration`. fn process_funds_migration( ctx: &mut CTX, config: &BridgeConstants, hardfork_cfg: &RskHardforkConfig, block_number: u64, tx_ctx: &BridgeTxContext, -) { +) -> Result<(), PrecompileError> { let Some(retiring_keys) = retiring_federation_keys(ctx, config, hardfork_cfg, block_number) else { - return; + return Ok(()); }; let Some(new_fed) = super::federation::load_stored_federation(ctx, NEW_FEDERATION_KEY) else { - return; + return Ok(()); }; let age = block_number.saturating_sub(new_fed.creation_block); @@ -3755,7 +4402,7 @@ fn process_funds_migration( let in_migration_age = age > migration_start && age < migration_end; let past_migration_age = age >= migration_end; if !in_migration_age && !past_migration_age { - return; + return Ok(()); } let mut old_utxos = super::storage::load_old_federation_utxos(ctx); @@ -3793,25 +4440,25 @@ fn process_funds_migration( build_federation_redeem_script(&retiring_keys, retiring_keys.len() / 2 + 1) }); // rskj `migrateFunds` sends the migration to `getActiveFederationAddress()` - // = the NEW (active) federation's P2SH address (BridgeSupport.java:1308, - // FederationSupportImpl.getActiveFederationAddress -> getAddress()). The - // active federation here is the ERP federation committed at #4,652,781, so - // its address derives from the (RSKIP201-era non-standard) ERP redeem - // script, NOT a plain multisig. Use the committed-federation redeem builder - // so this matches whatever federation format was committed (standard pre- - // RSKIP201, non-standard ERP, P2SH-ERP, or P2SH-P2WSH-ERP). + // = the NEW (active) federation's address (BridgeSupport.java:1308, + // FederationSupportImpl.getActiveFederationAddress -> getAddress()), the + // federation as it is stored: rskj reads it back in its own format + // version (`deserializeFederationAccordingToVersion`), whatever format a + // federation committed in this block would take. The first ERP + // federation, committed at #4,652,781, is a non-standard ERP one. let new_btc_keys = new_fed.btc_keys(); - let new_redeem = build_committed_federation_redeem_script( - &new_btc_keys, - config, - hardfork_cfg, - block_number, - ); // getActiveFederationAddress is format-aware: a P2SH-P2WSH (format 4000, // RSKIP305) active federation receives the migration at its P2SH-P2WSH // address, not a plain P2SH of the redeem. First hit when the first // reed800 segwit federation activates (#8,147,324). let new_format = federation_format_version(ctx, super::storage::NEW_FEDERATION_FORMAT_VERSION_KEY); + let new_redeem = federation_redeem_for_format( + &new_btc_keys, + new_format, + config, + hardfork_cfg, + block_number, + ); let active_script = p2sh_output_script(&federation_output_hash160(&new_redeem, new_format)); // The migration spends the retiring (OLD) federation; a P2SH-P2WSH @@ -3837,14 +4484,26 @@ fn process_funds_migration( .unwrap_or(retiring_redeem.as_slice()) }; - // createMigrationTransaction: target the full balance, halving on - // size overflow. rskj ReleaseTransactionBuilder sets version 2 once - // RSKIP201 is active (setDefaultTxConfig), but buildMigrationTransaction - // forces it back to version 1 until RSKIP376 — from RSKIP376 (arrowhead600) - // on, migration txs are version 2 like every other pegout. + // createMigrationTransaction: rskj ReleaseTransactionBuilder sets version + // 2 once RSKIP201 is active (setDefaultTxConfig), but + // buildMigrationTransaction forces it back to version 1 until RSKIP376 — + // from RSKIP376 (arrowhead600) on, migration txs are version 2 like every + // other pegout. let migration_tx_version = if hardfork_cfg.has_rskip376(block_number) { 2 } else { 1 }; + // It asks for the whole balance and, each time bitcoinj can't build the + // transaction (INSUFFICIENT_MONEY, EXCEED_MAX_TRANSACTION_SIZE, + // COULD_NOT_ADJUST_DOWNWARDS), for half of it, with no floor. An amount + // under the destination's dust limit is DUSTY_SEND_REQUESTED, and + // createMigrationTransaction throws ("Retiring federation wallet cannot + // be emptied"), as it throws where bitcoinj can't size an input. + let dust = super::release_tx::min_non_dust_value(&active_script); let mut target = balance; - let built = loop { + let built: Result = loop { + if target < dust { + break Err(PrecompileError::other( + "createMigrationTransaction: the retiring federation's wallet cannot be emptied", + )); + } let outputs = [super::release_tx::PegoutOutput { script: active_script.clone(), amount_satoshis: target, @@ -3858,15 +4517,19 @@ fn process_funds_migration( migration_tx_version, retiring_is_segwit, ) { - Some(b) => break Some(b), - None => { - target /= 2; - if target < 10_000 { - break None; - } - } + Ok(Some(b)) => break Ok(b), + Ok(None) => target /= 2, + Err(e) => break Err(PrecompileError::from(e)), } }; + // processFundsMigration lets what migrateFunds throws fail the call while + // the active federation is in its migration age; past it, it catches the + // throw, migrates nothing, and retires the federation all the same. + let built = match built { + Ok(built) => Some(built), + Err(_) if past_migration_age => None, + Err(e) => return Err(e), + }; if let Some(built) = built { // RSKIP379: settleReleaseRequest indexes every pegout — migrations @@ -3902,19 +4565,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| { @@ -3946,6 +4597,7 @@ fn process_funds_migration( } bridge_store_bytes_named(ctx, OLD_FEDERATION_KEY, &[]); } + Ok(()) } /// Active federation member keys (rskj FederationSupport.getActiveFederation): @@ -4045,6 +4697,14 @@ fn store_utxos_at(ctx: &mut CTX, key: &str, utxos: &[B bridge_store_bytes_named(ctx, key, &super::storage::serialize_utxo_list(utxos)); } +/// rskj `FederationStorageProviderImpl.saveNewFederationBtcUTXOs` and +/// `saveOldFederationBtcUTXOs`: a call that read a federation's UTXOs writes +/// them back as it returns, the empty list where the cell held nothing. +fn write_back_utxos(ctx: &mut CTX, key: &str) { + let utxos = load_utxos_at(ctx, key); + store_utxos_at(ctx, key, &utxos); +} + /// Active federation keys together with the redeem script built from the /// federation's STORED format version (rskj `getActiveFederation().getRedeemScript()`). /// During the activation window the active federation is still the OLD one, so its @@ -4109,14 +4769,22 @@ 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, + 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) } @@ -4141,7 +4809,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) } @@ -4165,7 +4833,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)) } @@ -4209,17 +4877,20 @@ 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 /// 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, @@ -4227,7 +4898,7 @@ pub(crate) fn is_sender_active_or_retiring_fed_member( block_number: u64, sender: alloy_primitives::Address, ) -> bool { - use super::federation::{rsk_address_from_public_key, StoredFederation}; + use super::federation::{member_rsk_address, rsk_address_from_public_key, StoredFederation}; let new = super::federation::load_stored_federation(ctx, NEW_FEDERATION_KEY); let old = super::federation::load_stored_federation(ctx, OLD_FEDERATION_KEY); @@ -4250,9 +4921,9 @@ pub(crate) fn is_sender_active_or_retiring_fed_member( }; let member_match = |fed: &StoredFederation| { - fed.members.iter().any(|m| { - rsk_address_from_public_key(&m.rsk) == Some(sender) - }) + fed.members + .iter() + .any(|m| member_rsk_address(&m.rsk) == Some(sender)) }; if active.as_ref().map(member_match).unwrap_or(false) { @@ -4272,6 +4943,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::member_rsk_address; + hardfork_cfg.has_rskip419(block_number) + && super::federation::load_stored_federation(ctx, PROPOSED_FEDERATION_KEY).is_some_and( + |proposed| { + proposed + .members + .iter() + .any(|m| member_rsk_address(&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 @@ -4309,35 +5002,20 @@ pub(crate) fn genesis_federation_keys(config: &BridgeConstants) -> Vec<[u8; 33]> /// bitcoinj reads one ASN.1 object and casts it to a sequence, then casts the /// first two elements to integers; anything else throws. It does *not* check /// for trailing data, and it reads r and s with `getPositiveValue()`, so a -/// negative encoding is tolerated rather than rejected. This mirrors that: a -/// DER SEQUENCE whose first two elements are INTEGERs, with lengths that stay -/// inside the buffer. Stricter checks would reject signatures rskj accepts. +/// negative encoding is tolerated rather than rejected. Bouncy Castle reads +/// a length's long form as well as its short one, and refuses an integer +/// whose first byte only repeats the sign of the next +/// (`der_signature::der_signature_integers`). fn is_der_signature(sig: &[u8]) -> bool { - // SEQUENCE tag plus a definite-length byte. - if sig.len() < 2 || sig[0] != 0x30 { - return false; - } - let (seq_len, mut i) = match sig[1] { - n if n < 0x80 => (n as usize, 2usize), - 0x81 if sig.len() >= 3 => (sig[2] as usize, 3usize), - _ => return false, // longer forms never appear in a 32-byte-scalar signature - }; - if seq_len > sig.len() - i { - return false; - } - let end = i + seq_len; - // Two INTEGERs. - for _ in 0..2 { - if i + 2 > end || sig[i] != 0x02 { - return false; - } - let len = sig[i + 1] as usize; - if len == 0 || len >= 0x80 || i + 2 + len > end { - return false; - } - i += 2 + len; - } - true + super::der_signature::der_signature_integers(sig).is_some() +} + +/// 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( @@ -4347,51 +5025,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: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 + // 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 @@ -4400,16 +5041,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 = 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 { + 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 @@ -4426,7 +5089,7 @@ pub fn add_signature( &sigs, &rsk_tx_hash, &items[1], - block_number, + hardfork_cfg.has_rskip305(block_number), gas_cost, ); } @@ -4437,6 +5100,16 @@ pub fn add_signature( let wfs_key = bridge_storage_key(PEGOUTS_WAITING_FOR_SIGNATURES_KEY); let wfs_data = bridge_load_bytes(ctx, wfs_key); let mut wfs = deserialize_rsk_txs_waiting_for_signatures(&wfs_data); + // rskj `BridgeStorageProvider.savePegoutsWaitingForSignatures`: `save()` + // writes back the set the call read, whatever the call does next, so a + // cell never written takes the empty list. + if wfs_data.is_empty() { + bridge_store_bytes( + ctx, + wfs_key, + &serialize_rsk_txs_waiting_for_signatures(&wfs), + ); + } // Find the BTC tx by RSK tx hash let btc_tx_raw = match wfs.get(&rsk_tx_hash) { @@ -4497,17 +5170,18 @@ pub fn add_signature( return Ok(PrecompileOutput::new(gas_cost, Bytes::new())); } - // Membership check against the active federation (genesis fallback). - // TODO(rustock): rskj also accepts retiring-federation members. + // Membership check against the active federation (genesis fallback), then + // the retiring one. let block_number = revm::context_interface::Block::number(ctx.block()).to::(); let federation_keys = federation_keys_or_genesis(ctx, config, hardfork_cfg, block_number, false); if federation_keys.is_empty() { return Ok(PrecompileOutput::new(gas_cost, Bytes::new())); } - let Some(compressed_key) = compress_pubkey(&fed_key) else { + // rskj getFederationFromPublicKey: active first, then retiring, each asked + // whether it holds the key as the call gave it (`hasBtcPublicKey`). + let Some(compressed_key) = member_key_as_given(&fed_key) else { return Ok(PrecompileOutput::new(gas_cost, Bytes::new())); }; - // rskj getFederationFromPublicKey: active first, then retiring. let is_member = federation_keys.contains(&compressed_key) || retiring_federation_keys(ctx, config, hardfork_cfg, block_number) .is_some_and(|keys| keys.contains(&compressed_key)); @@ -4526,10 +5200,11 @@ pub fn add_signature( // rskj `BridgeEventLoggerImpl.getFederatorRskPublicKey`: the add_signature // event's federatorRskAddress topic derives from the member's BTC public // key before RSKIP415 (arrowhead600) and from its RSK public key after. - // For legacy single-key members the two keys are identical. + // For legacy single-key members the two keys are identical. A member's + // RSK key may be the point at infinity, whose address is that of `00`. let federator_addr_key: Vec = if hardfork_cfg.has_rskip415(block_number) { federator_rsk_key_for_btc(ctx, config, hardfork_cfg, block_number, &compressed_key) - .map(|k| k.to_vec()) + .map(|k| super::federation::key_encoding(&k).to_vec()) .unwrap_or_else(|| fed_key.clone()) } else { fed_key.clone() @@ -4544,8 +5219,10 @@ pub fn add_signature( &rsk_tx_hash, ); } else if let Some(addr) = super::federation::rsk_address_from_public_key(&federator_addr_key) { - // Topic = federatorRskAddress; data = the BTC public key (always). - super::events::log_solidity_add_signature(ctx, &rsk_tx_hash, addr, &fed_key); + // Topic = federatorRskAddress; data = the member's BTC key as the + // federation keeps it, compressed, however the call gave it + // (`BridgeEventLoggerImpl.logAddSignature`). + super::events::log_solidity_add_signature(ctx, &rsk_tx_hash, addr, &compressed_key); } }; if !log_only_when_applied { @@ -4556,12 +5233,15 @@ pub fn add_signature( // input's sighash before applying any, then insert each at the position // determined by the key order in the redeem script. A segwit pegout signs // each input with its BIP-143 sighash, whose prevValue comes from the - // releasesOutpointsValues entry keyed by the tx's witness-stripped hash. - let prev_values = { - let txid = btc_txid_event_bytes(&btc_tx); - load_release_outpoints_values(ctx, &txid) + // releasesOutpointsValues entry keyed by the tx's witness-stripped hash, + // which rskj's `getReleaseOutpointsValues` refuses to read before RSKIP305. + let prev_values = if hardfork_cfg.has_rskip305(block_number) { + load_release_outpoints_values(ctx, &btc_txid_event_bytes(&btc_tx)) + } else { + None }; - let applied = apply_signatures_to_tx(&mut btc_tx, &sigs, &compressed_key, &prev_values); + let applied = + apply_signatures_to_tx(&mut btc_tx, &sigs, &compressed_key, prev_values.as_deref())?; if log_only_when_applied && applied { emit_add_signature(ctx, &btc_tx); } @@ -4604,23 +5284,31 @@ fn add_svp_spend_tx_signatures( sigs: &[Vec], rsk_tx_hash: &[u8; 32], spend_tx_bytes: &[u8], - _block_number: u64, + rskip305: bool, gas_cost: u64, ) -> Result { let empty = || Ok(PrecompileOutput::new(gas_cost, Bytes::new())); + // rskj throws where there is no proposed federation, and where the key is + // none of its members' (`getMemberByBtcPublicKey(..).orElseThrow`): the + // call fails. A member whose key is the point at infinity is found by its + // key, 00, as any other is by its own. let Some(proposed) = super::federation::load_stored_federation(ctx, PROPOSED_FEDERATION_KEY) else { - return empty(); - }; - let Some(compressed_key) = compress_pubkey(fed_key) else { - return empty(); + return Err(PrecompileError::other( + "addSignature: no proposed federation signs the SVP spend transaction", + )); }; - // The federator must belong to the proposed federation. - let Some(member_rsk) = - proposed.members.iter().find(|m| m.btc == compressed_key).map(|m| m.rsk) - else { - return empty(); + let Some((compressed_key, member_rsk)) = member_key_of(fed_key).and_then(|key| { + proposed + .members + .iter() + .find(|m| m.btc == key) + .map(|m| (key, m.rsk)) + }) else { + return Err(PrecompileError::other( + "addSignature: the key is no member's of the proposed federation", + )); }; let mut spend_tx: BtcTransaction = match deserialize(spend_tx_bytes) { Ok(tx) => tx, @@ -4633,15 +5321,21 @@ fn add_svp_spend_tx_signatures( // generateInputSigHash (segwit): the per-input prevValue comes from the // releasesOutpointsValues entry keyed by the spend tx's witness-stripped - // hash, saved when the spend tx was created (processReleaseTransactionInfo). + // hash, saved when the spend tx was created (processReleaseTransactionInfo), + // and read only from RSKIP305. let spend_hash = btc_txid_event_bytes(&spend_tx); - let prev_values = load_release_outpoints_values(ctx, &spend_hash); + let prev_values = if rskip305 { + load_release_outpoints_values(ctx, &spend_hash) + } else { + None + }; // processSigning: apply the signatures, then (RSKIP326) log add_signature. - let applied = apply_signatures_to_tx(&mut spend_tx, sigs, &compressed_key, &prev_values); + let applied = + apply_signatures_to_tx(&mut spend_tx, sigs, &compressed_key, prev_values.as_deref())?; if applied { - if let Some(addr) = super::federation::rsk_address_from_public_key(&member_rsk) { - super::events::log_solidity_add_signature(ctx, rsk_tx_hash, addr, fed_key); + if let Some(addr) = super::federation::member_rsk_address(&member_rsk) { + super::events::log_solidity_add_signature(ctx, rsk_tx_hash, addr, &compressed_key); } } @@ -4669,12 +5363,29 @@ fn btc_txid_event_bytes(tx: &BtcTransaction) -> [u8; 32] { bytes } -/// Compress a SEC1 public key (33- or 65-byte) to its 33-byte form. -fn compress_pubkey(key: &[u8]) -> Option<[u8; 33]> { - use k256::elliptic_curve::sec1::ToEncodedPoint; - let parsed = k256::PublicKey::from_sec1_bytes(key).ok()?; - let point = parsed.to_encoded_point(true); - point.as_bytes().try_into().ok() +/// The member key a federator's key names where rskj compares points, as +/// `Federation.getMemberByBtcPublicKey` does, reading the key again and +/// comparing it with each member's by `BtcECKey.equals`: the point at +/// infinity ([`super::federation::INFINITY_KEY`]) for the one byte 00 Bouncy +/// Castle decodes as it, and the compressed form of a key in any other form +/// it reads, compressed, uncompressed or hybrid. +fn member_key_of(key: &[u8]) -> Option<[u8; 33]> { + if key == [0x00] { + return Some(super::federation::INFINITY_KEY); + } + bitcoin::secp256k1::PublicKey::from_slice(key) + .ok() + .map(|key| key.serialize()) +} + +/// The member key a federator's key names where rskj compares bytes, as +/// `Federation.hasBtcPublicKey` does, by which `getFederationFromPublicKey` +/// finds the federation a peg-out is signed for: the key's bytes as the call +/// gave them against each member's compressed key. So a key given compressed, +/// or the one byte 00, names its member, and one given uncompressed or hybrid +/// names none. +fn member_key_as_given(key: &[u8]) -> Option<[u8; 33]> { + member_key_of(key).filter(|member| super::federation::key_encoding(member) == key) } /// RIPEMD160(SHA256(pubkey)) — bitcoinj `ECKey.getPubKeyHash` over the key @@ -4989,23 +5700,50 @@ 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), and each is pushed as `ScriptBuilder.data` +/// pushes its encoding: 33 compressed bytes, or for the point at infinity +/// the one byte 00, which is a one-byte push (`01 00`), not OP_0. 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); + let encoded = super::federation::key_encoding(key); + script.push(encoded.len() as u8); // OP_PUSHBYTES_33, or _1 + script.extend_from_slice(encoded); } - 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 @@ -5032,29 +5770,109 @@ fn signed_long_to_byte_array_le(value: i64) -> Vec { be } -/// rskj `NonStandardErpRedeemScriptBuilder` (post-RSKIP284/293) 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`]. -fn build_erp_redeem_script( +/// 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", +); + +/// The hard-coded script's bytes. bitcoinj-thin's `RedeemScriptParserFactory` +/// holds the same script, and an input spent with it cannot be sized +/// (`release_tx::signatures_required`). +pub(super) fn legacy_erp_testnet_redeem_script() -> &'static [u8] { + static SCRIPT: std::sync::OnceLock> = std::sync::OnceLock::new(); + SCRIPT.get_or_init(|| { + alloy_primitives::hex::decode(LEGACY_ERP_TESTNET_REDEEM_SCRIPT) + .expect("the hard-coded script is hex") + }) +} + +/// 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. +/// +/// 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 legacy_erp_testnet_redeem_script().to_vec(); + } + 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] }; @@ -5076,6 +5894,25 @@ fn build_erp_redeem_script( script } +/// rskj `FederationFactory.buildP2shErpFederation`'s redeem script: a P2SH +/// ERP federation of `members`, with the emergency keys `emergency` after +/// `delay` blocks, each threshold a majority. +#[cfg(test)] +pub(crate) fn p2sh_erp_redeem_script( + members: &[[u8; 33]], + emergency: &[[u8; 33]], + delay: i64, +) -> Vec { + build_erp_redeem_script( + members, + members.len() / 2 + 1, + emergency, + emergency.len() / 2 + 1, + delay, + ErpScript::P2sh, + ) +} + /// rskj `PendingFederation.buildFederation` redeem-script selection: a /// committed federation is a standard multisig (pre-RSKIP201), a non-standard /// ERP federation (RSKIP201 active, RSKIP353 not), a P2SH-ERP federation @@ -5104,8 +5941,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**, @@ -5118,11 +5959,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 { @@ -5138,9 +5984,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 @@ -5235,12 +6085,18 @@ pub(crate) fn load_federation_member_keys(ctx: &mut CT /// /// - Pre-RSKIP271: `BridgeUtilsLegacy.calculatePegoutTxSize` — an analytic /// approximation parameterised by the script-sig chunk size. -/// - Post-RSKIP271: `BridgeUtils.calculateLegacyTxSize` — the exact +/// - Post-RSKIP271: `BridgeUtils.calculatePegoutTxSize`, by the federation's +/// format: `calculateSegwitTxSize` for a P2SH-P2WSH ERP federation, +/// `calculateLegacyTxSize` for any other -- the exact /// `BtcTransaction.bitcoinSerialize()` length of a 2-in/2-out legacy tx whose /// inputs carry the redeem script as their scriptSig, plus -/// `numSigsRequired * inputsCount * 72`. (Mainnet federations are never -/// segwit at the heights rustock handles, so only the legacy branch applies.) -fn regular_pegout_tx_size(redeem: &[u8], num_sigs_required: u64, rskip271: bool) -> u64 { +/// `numSigsRequired * inputsCount * 72`. +fn regular_pegout_tx_size( + redeem: &[u8], + num_sigs_required: u64, + rskip271: bool, + segwit: bool, +) -> u64 { const INPUTS: u64 = 2; const OUTPUTS: u64 = 2; @@ -5258,29 +6114,20 @@ fn regular_pegout_tx_size(redeem: &[u8], num_sigs_required: u64, rskip271: bool) + (OUTPUT_SIZE + 1 + OUTPUT_ADDITIONAL_DATA_SIZE) * OUTPUTS; } - // Post-RSKIP271: exact bitcoinSerialize() length of the legacy tx that - // BridgeUtils.calculateLegacyTxSize builds (input scriptSig = redeem, - // output scriptPubKey = the 23-byte P2SH-to-federation script). - const P2SH_SCRIPT_LEN: u64 = 23; - let r = redeem.len() as u64; - let base_size = 4 // version - + varint_size(INPUTS) - + INPUTS * (36 + varint_size(r) + r + 4) - + varint_size(OUTPUTS) - + OUTPUTS * (8 + varint_size(P2SH_SCRIPT_LEN) + P2SH_SCRIPT_LEN) - + 4; // locktime - let signing_size = num_sigs_required * INPUTS * 72; - base_size + signing_size -} - -/// bitcoinj `VarInt` serialized length. -fn varint_size(n: u64) -> u64 { - match n { - 0..=0xfc => 1, - 0xfd..=0xffff => 3, - 0x1_0000..=0xffff_ffff => 5, - _ => 9, - } + // The outputs pay the federation's address, a P2SH output script of 23 + // bytes whatever the format; only its length counts. + let fed_script = p2sh_output_script(&[0; 20]); + let (threshold, inputs, outputs) = ( + num_sigs_required as usize, + INPUTS as usize, + OUTPUTS as usize, + ); + let size = if segwit { + pegout_tx_size_segwit(redeem, &fed_script, threshold, inputs, outputs) + } else { + pegout_tx_size_legacy(redeem, &fed_script, threshold, inputs, outputs) + }; + size as u64 } /// rskj `BridgeSupport.requestRelease` minimum-fee estimate (post-RSKIP219): @@ -5290,9 +6137,10 @@ fn require_funds_for_fee( num_sigs_required: u64, fee_per_kb: u64, rskip271: bool, + segwit: bool, config: &BridgeConstants, ) -> u64 { - let pegout_size = regular_pegout_tx_size(redeem, num_sigs_required, rskip271); + let pegout_size = regular_pegout_tx_size(redeem, num_sigs_required, rskip271, segwit); let base = fee_per_kb * pegout_size / 1000; base + base * config.minimum_pegout_value_percentage_to_receive_after_fee / 100 } @@ -5315,16 +6163,30 @@ fn get_effective_fee_per_kb(ctx: &mut CTX, config: &Br /// scriptSig) before any is applied; a single invalid signature aborts the /// call. Each valid signature is then inserted at the position dictated by /// the key order in the redeem script, consuming one OP_0 placeholder. -/// Returns whether at least one signature was applied. +/// Returns whether at least one signature was applied, and `Err` where rskj +/// throws: at an input with a witness whose spent value `prev_values`, what +/// `getReleaseOutpointsValues` read, doesn't hold, at an input whose +/// redeem script it can't take, and where a signature is checked against the +/// point at infinity. +/// +/// The point at infinity verifies nothing in rskj: Bouncy Castle refuses it +/// as a verifying key (`ECPublicKeyParameters` throws "point at infinity"), +/// and `BtcECKey.verify` lets that `IllegalArgumentException` through. +/// `getTransactionSignatures` doesn't catch it, so a call with that key fails +/// at its first signature; nor does `BridgeSupport.sign`, which catches +/// `IllegalStateException` alone, so once an input holds a signature, any +/// member's call that signs it fails at `findSigInRedeem`, which tries the +/// redeem script's keys in order and meets that key, sorted first, before +/// any other. fn apply_signatures_to_tx( tx: &mut BtcTransaction, sigs: &[Vec], fed_key: &[u8; 33], - prev_values: &[u64], -) -> bool { + prev_values: Option<&[u64]>, +) -> Result { use super::release_tx::{ extract_redeem_script, input_signed_by, legacy_sighash_all, segwit_sighash_all, - sig_insertion_index, update_script_with_signature, verify_der_signature, + sig_insertion_index, signature_to_insert, update_script_with_signature, witness_input_signed_by, witness_sig_insertion_index, witness_update_with_signature, }; @@ -5337,16 +6199,33 @@ fn apply_signatures_to_tx( let mut sighashes = Vec::with_capacity(n); #[allow(clippy::needless_range_loop)] // indexes several parallel collections for i in 0..n { + // rskj reads a segwit input's value first, and throws where there is + // none: nothing kept under the transaction's hash (`orElseThrow`), or + // fewer values than inputs (`List.get`). + let prev_value = if is_witness[i] { + let Some(value) = prev_values.and_then(|values| values.get(i).copied()) else { + return Err(PrecompileError::other( + "addSignature: no value kept for the output a segwit input spends", + )); + }; + value + } else { + 0 + }; let redeem = if is_witness[i] { tx.input[i].witness.last().map(|r| r.to_vec()) } else { extract_redeem_script(tx.input[i].script_sig.as_bytes()) }; + // `BitcoinUtils.generateSigHashForLegacyTransactionInput` and its + // segwit twin throw where they can't take the input's redeem script. let Some(redeem) = redeem else { - return false; + return Err(PrecompileError::other( + "addSignature: an input has no redeem script to sign over", + )); }; let sighash = if is_witness[i] { - segwit_sighash_all(tx, i, &redeem, prev_values.get(i).copied().unwrap_or(0)) + segwit_sighash_all(tx, i, &redeem, prev_value) } else { legacy_sighash_all(tx, i, &redeem) }; @@ -5354,14 +6233,31 @@ fn apply_signatures_to_tx( redeems.push(redeem); } - // getTransactionSignatures: any malformed or non-verifying signature - // aborts without applying anything. + // getTransactionSignatures: the federator's key is the point at infinity, + // whose verification throws (see above): the call fails. + if *fed_key == super::federation::INFINITY_KEY { + return Err(PrecompileError::other( + "addSignature: a signature is verified with the point at infinity", + )); + } + // getTransactionSignatures: a signature bitcoinj cannot read, that does + // not verify, or whose s is in the upper half of the order aborts + // without applying anything; each is applied as bitcoinj writes it + // (`signature_to_insert`). + let mut encodings = Vec::with_capacity(n); for (sig, sighash) in sigs.iter().zip(&sighashes) { - if !verify_der_signature(sig, sighash, fed_key) { - return false; - } + let Some(encoded) = signature_to_insert(sig, sighash, fed_key) else { + return Ok(false); + }; + encodings.push(encoded); } + // `findSigInRedeem` met the point at infinity (see above): the call fails. + let meets_infinity = || { + PrecompileError::other( + "addSignature: a signature is looked up against the point at infinity", + ) + }; // sign(): stop at the first input already signed by this federator. let mut signed = false; for i in 0..n { @@ -5382,77 +6278,34 @@ fn apply_signatures_to_tx( // verified fine against the federator's own key, so without this // gate it would fill a placeholder it has no claim to (#2,448,984). if !super::release_tx::spending_redeem_keys(&redeems[i]).contains(fed_key) { - return signed; + return Ok(signed); } - let mut encoded = sigs[i].clone(); - encoded.push(0x01); // SIGHASH_ALL + let encoded = encodings[i].clone(); if is_witness[i] { let pushes: Vec> = tx.input[i].witness.iter().map(|p| p.to_vec()).collect(); - let index = - witness_sig_insertion_index(&pushes, &sighashes[i], fed_key, &redeems[i]); + let Some(index) = + witness_sig_insertion_index(&pushes, &sighashes[i], fed_key, &redeems[i]) + else { + return Err(meets_infinity()); + }; let Some(updated) = witness_update_with_signature(&pushes, &encoded, index) else { - return false; // no placeholder left (bitcoinj throws) + return Ok(false); // no placeholder left (bitcoinj throws) }; tx.input[i].witness = bitcoin::Witness::from_slice(&updated); } else { let script_sig = tx.input[i].script_sig.as_bytes().to_vec(); - let index = sig_insertion_index(&script_sig, &sighashes[i], fed_key, &redeems[i]); + let Some(index) = sig_insertion_index(&script_sig, &sighashes[i], fed_key, &redeems[i]) + else { + return Err(meets_infinity()); + }; let Some(updated) = update_script_with_signature(&script_sig, &encoded, index) else { - return false; // no placeholder left (bitcoinj throws) + return Ok(false); // no placeholder left (bitcoinj throws) }; tx.input[i].script_sig = bitcoin::ScriptBuf::from_bytes(updated); } signed = true; } - 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 + Ok(signed) } // --------------------------------------------------------------------------- @@ -5465,32 +6318,22 @@ 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 // --------------------------------------------------------------------------- /// `getNextPegoutCreationBlockNumber()` → int256 +/// +/// The method exists from RSKIP271, where rskj's +/// `BridgeStorageProvider.getNextPegoutHeight` reads the cell, 0 where it +/// holds nothing, and the Bridge's `teardown` writes back what was read +/// (`saveNextPegoutHeight`). pub fn get_next_pegout_creation_block_number( ctx: &mut CTX, gas_cost: u64, ) -> Result { let val = super::storage::bridge_load_u256(ctx, NEXT_PEGOUT_HEIGHT_KEY); + super::storage::bridge_store_u256(ctx, NEXT_PEGOUT_HEIGHT_KEY, val); let mut output = [0u8; 32]; let bytes = val.to_be_bytes::<32>(); output.copy_from_slice(&bytes); @@ -5511,6 +6354,7 @@ pub fn get_queued_pegouts_count( // only exists from RSKIP271 (hop400), which is well past RSKIP146, so the // with-txhash half is always in scope here. let entries = load_release_request_queue(ctx, true); + write_back_release_request_queue(ctx, true); let count = U256::from(entries.len()); Ok(PrecompileOutput::new(gas_cost, count.to_be_bytes::<32>().to_vec().into())) } @@ -5555,6 +6399,20 @@ fn save_release_request_queue( } } +/// rskj's `Bridge.execute` ends a method that returns with `teardown`, whose +/// `BridgeStorageProvider.save` writes the release-request queue back if the +/// method read it (`saveReleaseRequestQueue`): `releaseRequestQueue`, and +/// from RSKIP146 `releaseRequestQueueWithTxHash`, each as rskj serializes +/// what it read, the empty list where a cell held nothing. The getters that +/// read the queue write it back so too. +pub(crate) fn write_back_release_request_queue( + ctx: &mut CTX, + rskip146: bool, +) { + let queue = load_release_request_queue(ctx, rskip146); + save_release_request_queue(ctx, &queue, rskip146); +} + /// rskj `BridgeStorageProvider.getReleaseRequestQueue` (l.168-193): the legacy /// `releaseRequestQueue` entries FIRST, then — once RSKIP146 is active — the /// `releaseRequestQueueWithTxHash` entries appended. Both lists, concatenated, @@ -5597,20 +6455,20 @@ fn estimated_fees_for_next_pegout( config: &BridgeConstants, hardfork_cfg: &RskHardforkConfig, block_number: u64, -) -> u64 { +) -> Result { let queue = load_release_request_queue(ctx, hardfork_cfg.has_rskip146(block_number)); // shouldReturnZeroEstimatedFees (l.2646-2651). if !hardfork_cfg.has_rskip385(block_number) && (!hardfork_cfg.has_rskip271(block_number) || queue.is_empty()) { - return 0; + return Ok(0); } if !hardfork_cfg.has_rskip305(block_number) { - return estimated_fees_from_inputs_and_outputs_count( + return Ok(estimated_fees_from_inputs_and_outputs_count( ctx, config, hardfork_cfg, block_number, queue.len(), - ); + )); } // getEstimatedFeesFromPegoutTransactionSimulation (l.2677-2692): the @@ -5622,13 +6480,15 @@ fn estimated_fees_for_next_pegout( 100_000_000 }; - simulate_pegout_fee(ctx, config, hardfork_cfg, block_number, &queue, estimated_next_amount) + // A build bitcoinj cannot size throws past the fallback + // (`release_tx::signatures_required`). + Ok(simulate_pegout_fee(ctx, config, hardfork_cfg, block_number, &queue, estimated_next_amount)? .unwrap_or_else(|| { // BridgeIllegalArgumentException -> "fallback to old logic". estimated_fees_from_inputs_and_outputs_count( ctx, config, hardfork_cfg, block_number, queue.len(), ) - }) + })) } /// rskj `BridgeSupport.getEstimatedFeesFromInputsAndOutputsCount` @@ -5653,9 +6513,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 { @@ -5672,7 +6532,8 @@ fn estimated_fees_from_inputs_and_outputs_count( /// federation's real UTXO set, and report `inputSum - outputSum`. /// /// `None` mirrors the `BridgeIllegalArgumentException` rskj throws when the -/// builder does not return SUCCESS; the caller then falls back. +/// builder does not return SUCCESS; the caller then falls back. `Err` is a +/// build bitcoinj cannot size, which throws past that fallback. fn simulate_pegout_fee( ctx: &mut CTX, config: &BridgeConstants, @@ -5680,11 +6541,11 @@ fn simulate_pegout_fee( block_number: u64, queue: &[ReleaseRequest], estimated_next_amount: u64, -) -> Option { +) -> Result, super::release_tx::UncountableSignatures> { let (keys, redeem_script) = active_federation_keys_and_redeem(ctx, config, hardfork_cfg, block_number); if keys.is_empty() { - return None; + return Ok(None); } let format = active_federation_format(ctx, config, hardfork_cfg, block_number); let is_segwit = format >= 4000; @@ -5694,6 +6555,10 @@ fn simulate_pegout_fee( let utxo_key = active_federation_utxo_key(ctx, config, hardfork_cfg, block_number); let available = load_utxos_at(ctx, utxo_key); + // rskj reads them with `getActiveFederationBtcUTXOs`, and `save()` writes + // them back as the call returns, the empty list where the cell held + // nothing. + store_utxos_at(ctx, utxo_key, &available); // Same FlyoverCompatibleBtcWallet resolution updateCollections uses. let mut candidate_fed_redeems = vec![redeem_script.clone()]; @@ -5722,7 +6587,7 @@ fn simulate_pegout_fee( amount_satoshis: estimated_next_amount, }); - let built = super::release_tx::complete_pegout_tx( + let Some(built) = super::release_tx::complete_pegout_tx( &available, &outputs, &change_script, @@ -5730,11 +6595,14 @@ fn simulate_pegout_fee( fee_per_kb, tx_version, is_segwit, - )?; + )? + else { + return Ok(None); + }; let input_sum: u64 = built.used_utxos.iter().map(|u| u.value_satoshis).sum(); let output_sum: u64 = built.tx.output.iter().map(|o| o.value.to_sat()).sum(); - Some(input_sum.saturating_sub(output_sum)) + Ok(Some(input_sum.saturating_sub(output_sum))) } /// `getEstimatedFeesForNextPegOutEvent()` → uint256 (satoshis) @@ -5749,7 +6617,10 @@ pub fn get_estimated_fees_for_next_pegout( hardfork_cfg: &RskHardforkConfig, ) -> Result { let block_number = revm::context_interface::Block::number(ctx.block()).to::(); - let fee = estimated_fees_for_next_pegout(ctx, config, hardfork_cfg, block_number); + let fee = estimated_fees_for_next_pegout(ctx, config, hardfork_cfg, block_number)?; + // The method exists from RSKIP271, where `getQueuedPegoutsCount` reads + // the queue. + write_back_release_request_queue(ctx, hardfork_cfg.has_rskip146(block_number)); let out = U256::from(fee).to_be_bytes::<32>(); Ok(PrecompileOutput::new(gas_cost, out.to_vec().into())) } @@ -5757,9 +6628,9 @@ pub fn get_estimated_fees_for_next_pegout( /// `getEstimatedFeesForPegOutAmount(uint256 pegoutAmountInWeis)` → uint256 /// /// Enabled by RSKIP540 (Vetiver900). Validates the amount against the -/// minimum peg-out value like rskj BridgeSupport.getEstimatedFeesForPegOutAmount; -/// the fee simulation itself shares getEstimatedFeesForNextPegOutEvent's -/// (stub) fidelity. +/// minimum peg-out value like rskj BridgeSupport.getEstimatedFeesForPegOutAmount, +/// then runs getEstimatedFeesForNextPegOutEvent's peg-out simulation with the +/// caller's amount. pub fn get_estimated_fees_for_pegout_amount( ctx: &mut CTX, args: &[u8], @@ -5773,16 +6644,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 = crate::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", )); @@ -5798,10 +6675,11 @@ pub fn get_estimated_fees_for_pegout_amount( PrecompileError::other("getEstimatedFeesForPegOutAmount: cannot simulate peg-out") })?; let queue = load_release_request_queue(ctx, hardfork_cfg.has_rskip146(block_number)); - let fee = simulate_pegout_fee(ctx, config, hardfork_cfg, block_number, &queue, amount) + let fee = simulate_pegout_fee(ctx, config, hardfork_cfg, block_number, &queue, amount)? .ok_or_else(|| { PrecompileError::other("getEstimatedFeesForPegOutAmount: cannot simulate peg-out") })?; + write_back_release_request_queue(ctx, hardfork_cfg.has_rskip146(block_number)); let out = U256::from(fee).to_be_bytes::<32>(); Ok(PrecompileOutput::new(gas_cost, out.to_vec().into())) } @@ -5998,6 +6876,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)); @@ -6008,6 +6889,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] @@ -6276,8 +7222,64 @@ mod tests { sig.extend_from_slice(&[0x03, 0x20]); sig.extend_from_slice(&[0x22u8; 32]); assert!(!is_der_signature(&sig), "second element must be INTEGER"); - // zero-length integer - assert!(!is_der_signature(&[0x30, 0x04, 0x02, 0x00, 0x02, 0x00]), "empty integer"); + // an integer whose first byte only repeats the sign of the next + assert!(!is_der_signature(&[ + 0x30, 0x07, 0x02, 0x02, 0x00, 0x7f, 0x02, 0x01, 0x01 + ])); + assert!(!is_der_signature(&[ + 0x30, 0x07, 0x02, 0x01, 0x01, 0x02, 0x02, 0xff, 0x80 + ])); + // an indefinite length, and a length of five bytes + assert!(!is_der_signature(&[ + 0x30, 0x80, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00 + ])); + assert!(!is_der_signature(&[ + 0x30, 0x0b, 0x02, 0x85, 0, 0, 0, 0, 0x01, 0x01, 0x02, 0x01, 0x01 + ])); + // a length in four bytes, past what a Java int holds + assert!(!is_der_signature(&[ + 0x30, 0x84, 0x80, 0, 0, 0, 0x02, 0x01, 0x01 + ])); + // a high tag number that is not the sequence's, or the integer's + assert!(!is_der_signature(&[ + 0x3f, 0x11, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 + ])); + assert!(!is_der_signature(&[ + 0x30, 0x07, 0x1f, 0x03, 0x01, 0x01, 0x02, 0x01, 0x01 + ])); + // the integer's high tag cut off by the sequence's end + assert!(!is_der_signature(&[ + 0x30, 0x04, 0x02, 0x01, 0x01, 0x1f, 0x02, 0x01, 0x01 + ])); + } + + /// What Bouncy Castle 1.58's `ASN1InputStream` reads and bitcoinj's + /// `decodeFromDER` takes, though it is not DER. + #[test] + fn der_signature_accepts_what_bouncy_castle_reads() { + // integers with no bytes, read as 0 + assert!(is_der_signature(&[0x30, 0x04, 0x02, 0x00, 0x02, 0x00])); + // lengths in the long form + assert!(is_der_signature(&[ + 0x30, 0x81, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 + ])); + assert!(is_der_signature(&[ + 0x30, 0x0a, 0x02, 0x84, 0, 0, 0, 0x01, 0x01, 0x02, 0x01, 0x01 + ])); + // the high-tag-number forms of the sequence's and the integer's tags + assert!(is_der_signature(&[ + 0x3f, 0x10, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 + ])); + assert!(is_der_signature(&[ + 0x30, 0x07, 0x1f, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 + ])); + // a negative encoding, and bytes after the sequence + assert!(is_der_signature(&[ + 0x30, 0x06, 0x02, 0x01, 0x81, 0x02, 0x01, 0x01 + ])); + assert!(is_der_signature(&[ + 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01, 0xde, 0xad + ])); } /// A federation member's signature over the **wrong sighash** is not @@ -6333,17 +7335,57 @@ mod tests { // Well formed DER, right key, right federation -- wrong message. let before = tx.input[0].script_sig.clone(); assert!( - !apply_signatures_to_tx(&mut tx, &[sig_over(&wrong)], pk, &[]), + !apply_signatures_to_tx(&mut tx, &[sig_over(&wrong)], pk, None).expect("no throw"), "a signature over the wrong sighash must not be applied" ); assert_eq!(tx.input[0].script_sig, before, "nothing may be written"); // The control: the same key over the right message does apply, so the // rejection above is about the message and not the setup. - assert!(apply_signatures_to_tx(&mut tx, &[sig_over(&correct)], pk, &[])); + assert!( + apply_signatures_to_tx(&mut tx, &[sig_over(&correct)], pk, None).expect("no throw") + ); assert_ne!(tx.input[0].script_sig, before); } + /// rskj takes every input's sighash before it reads a signature + /// (`BridgeSupport.processSigning`), and + /// `BitcoinUtils.generateSigHashForLegacyTransactionInput` throws where it + /// can't take an input's redeem script from its scriptSig: the call fails, + /// whatever the signatures. + #[test] + fn an_input_with_no_redeem_script_fails_the_call() { + use k256::ecdsa::signature::hazmat::PrehashSigner; + use k256::ecdsa::{Signature, SigningKey}; + + let sk = SigningKey::from_slice(&[1; 32]).unwrap(); + let pk: [u8; 33] = sk + .verifying_key() + .to_encoded_point(true) + .as_bytes() + .try_into() + .unwrap(); + let mut tx = BtcTransaction { + version: bitcoin::transaction::Version::ONE, + lock_time: bitcoin::absolute::LockTime::ZERO, + input: vec![bitcoin::TxIn { + previous_output: bitcoin::OutPoint::null(), + script_sig: bitcoin::ScriptBuf::new(), + sequence: bitcoin::Sequence::MAX, + witness: bitcoin::Witness::new(), + }], + output: vec![], + }; + let sig: Signature = sk.sign_prehash(&[0x5a; 32]).unwrap(); + let signed = + apply_signatures_to_tx(&mut tx, &[sig.to_der().as_bytes().to_vec()], &pk, None); + assert!( + signed.is_err(), + "rskj throws where an input has no redeem script" + ); + assert!(tx.input[0].script_sig.is_empty(), "nothing may be written"); + } + /// Regression for mainnet #2,448,984: a federator whose key is NOT in an /// input's redeem script (a NEW-federation member signing the migration /// tx that spends the OLD federation's UTXOs) must not have its @@ -6391,11 +7433,13 @@ mod tests { // Outsider: verifies against its own key but is not in the redeem. let before = tx.input[0].script_sig.clone(); - assert!(!apply_signatures_to_tx(&mut tx, &[sign(&outsider_sk)], &outsider_pk, &[])); + let outsider = apply_signatures_to_tx(&mut tx, &[sign(&outsider_sk)], &outsider_pk, None); + assert!(!outsider.expect("no throw")); assert_eq!(tx.input[0].script_sig, before, "script must be untouched"); // Member: applied normally. - assert!(apply_signatures_to_tx(&mut tx, &[sign(&members[0].0)], &members[0].1, &[])); + let member = apply_signatures_to_tx(&mut tx, &[sign(&members[0].0)], &members[0].1, None); + assert!(member.expect("no throw")); assert_ne!(tx.input[0].script_sig, before); } @@ -6655,6 +7699,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() { @@ -6684,6 +7766,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 @@ -6710,7 +7889,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"); } @@ -6738,7 +7918,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" @@ -6770,9 +7951,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", @@ -6780,7 +7965,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), @@ -6794,7 +7979,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"); } @@ -6882,7 +8067,7 @@ mod tests { entries.push(mk(&[1, 0, 0, 0, i], 3_345_000)); } - let pos = next_pegout_with_enough_confirmations(&entries, 3_345_557, 4000) + let pos = next_pegout_with_enough_confirmations(&entries, 3_345_557, 4000, entries.len()) .expect("one confirmed entry"); assert_eq!( entries[pos].btc_tx_raw, selected_btc, @@ -6919,6 +8104,24 @@ mod tests { assert_eq!(java_hashset_capacity(48), 128); } + /// rskj adds the peg-outs a call builds to the set it read + /// (`PegoutsWaitingForConfirmations.add`), one at a time, and + /// `HashMap.putVal` doubles the table once the set holds more than three + /// quarters of it: eleven read and one added stay at 16, where a set read + /// with twelve is 32, and a second added takes it to 32. + #[test] + fn a_set_grows_by_the_entries_added_after_it_was_read() { + assert_eq!(walked_hashset_capacity(11, 11), 16); + assert_eq!(walked_hashset_capacity(11, 12), 16); + assert_eq!(walked_hashset_capacity(11, 13), 32); + assert_eq!(walked_hashset_capacity(12, 12), 32); + assert_eq!(walked_hashset_capacity(0, 12), 16); + assert_eq!(walked_hashset_capacity(0, 13), 32); + assert_eq!(walked_hashset_capacity(23, 24), 32); + assert_eq!(walked_hashset_capacity(23, 25), 64); + assert_eq!(walked_hashset_capacity(24, 25), 64); + } + /// Resize-boundary selection: with the two real h=3,341,556 entries plus /// fillers totalling exactly 13 entries the capacity is 32 (a7c69a46→bucket 9, /// 0260d432→bucket 20), so the a7c69a46 tx is selected — the #3,345,557 @@ -6945,7 +8148,7 @@ mod tests { } assert_eq!(entries.len(), 13); assert_eq!(java_hashset_capacity(entries.len()), 32); - let pos = next_pegout_with_enough_confirmations(&entries, 3_345_557, 4000) + let pos = next_pegout_with_enough_confirmations(&entries, 3_345_557, 4000, entries.len()) .expect("one confirmed entry"); assert_eq!(entries[pos].btc_tx_raw, selected_btc); } @@ -6987,7 +8190,7 @@ mod tests { assert_eq!(entries.len(), 12); // n=12 → the HashSet(Collection) constructor pre-sizes to 32. assert_eq!(java_hashset_capacity(12), 32); - let pos = next_pegout_with_enough_confirmations(&entries, 3_344_303, 4000) + let pos = next_pegout_with_enough_confirmations(&entries, 3_344_303, 4000, entries.len()) .expect("a confirmed entry"); assert_eq!( entries[pos].btc_tx_raw, rskj_btc, @@ -7081,7 +8284,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()), } } @@ -7211,13 +8420,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 }, ] { @@ -7281,9 +8497,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 { .. })), @@ -7302,7 +8518,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" @@ -7363,78 +8579,469 @@ 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" ); } - /// 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. - #[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" - ); - } - - /// sender address = P2SH(hash160(redeem)) — processable post-RSKIP143 - /// but NOT lockable (refunded). Groundtruth shape from mainnet - /// #2,851,909 tx 2. + /// **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 classify_p2sh_multisig_pegin_sender() { - // 2-of-3 multisig redeem with valid-shape compressed keys. - let mut redeem = vec![0x52]; - for i in 0..3u8 { - redeem.push(33); - let mut k = [0x02u8; 33]; - k[32] = i; - redeem.extend_from_slice(&k); - } - redeem.push(0x53); - redeem.push(0xae); - let mut script = vec![0x00, 71]; - script.extend_from_slice(&[0x30; 71]); - script.push(71); - script.extend_from_slice(&[0x30; 71]); - script.push(0x4c); // PUSHDATA1 - script.push(redeem.len() as u8); - script.extend_from_slice(&redeem); - let tx = BtcTransaction { - version: bitcoin::transaction::Version(1), - lock_time: bitcoin::absolute::LockTime::ZERO, - input: vec![bitcoin::TxIn { - previous_output: Default::default(), - script_sig: bitcoin::ScriptBuf::from_bytes(script), - sequence: bitcoin::Sequence::MAX, - witness: bitcoin::Witness::new(), - }], - output: vec![], - }; - match classify_pegin_sender(&tx).unwrap() { - Some(PeginSender::P2shMultisig { p2sh_hash }) => { - assert_eq!(p2sh_hash, pubkey_hash160(&redeem)); + 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 } - other => panic!("expected P2SH-multisig sender, got {}", other.is_some()), } - } + + // 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_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); + 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 _ = member_key_as_given(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); + } + } + } + + // 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 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 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 + /// but NOT lockable (refunded). Groundtruth shape from mainnet + /// #2,851,909 tx 2. + #[test] + fn classify_p2sh_multisig_pegin_sender() { + // 2-of-3 multisig redeem with valid-shape compressed keys. + let mut redeem = vec![0x52]; + for i in 0..3u8 { + redeem.push(33); + let mut k = [0x02u8; 33]; + k[32] = i; + redeem.extend_from_slice(&k); + } + redeem.push(0x53); + redeem.push(0xae); + let mut script = vec![0x00, 71]; + script.extend_from_slice(&[0x30; 71]); + script.push(71); + script.extend_from_slice(&[0x30; 71]); + script.push(0x4c); // PUSHDATA1 + script.push(redeem.len() as u8); + script.extend_from_slice(&redeem); + let tx = BtcTransaction { + version: bitcoin::transaction::Version(1), + lock_time: bitcoin::absolute::LockTime::ZERO, + input: vec![bitcoin::TxIn { + previous_output: Default::default(), + script_sig: bitcoin::ScriptBuf::from_bytes(script), + sequence: bitcoin::Sequence::MAX, + witness: bitcoin::Witness::new(), + }], + output: vec![], + }; + match classify_pegin_sender(&tx).unwrap() { + Some(PeginSender::P2shMultisig { p2sh_hash }) => { + assert_eq!(p2sh_hash, pubkey_hash160(&redeem)); + } + other => panic!("expected P2SH-multisig sender, got {}", other.is_some()), + } + } /// Conversion: 1 satoshi = 10^10 wei. #[test] @@ -7514,10 +9121,13 @@ mod tests { let redeem = build_federation_redeem_script(&keys, threshold); // Post-RSKIP271: exact match against rskj's 2058. - assert_eq!(regular_pegout_tx_size(&redeem, threshold as u64, true), 2058); + assert_eq!( + regular_pegout_tx_size(&redeem, threshold as u64, true, false), + 2058 + ); // Pre-RSKIP271: within 2% of the real-world 2076-byte tx. - let legacy = regular_pegout_tx_size(&redeem, threshold as u64, false) as i64; + let legacy = regular_pegout_tx_size(&redeem, threshold as u64, false, false) as i64; let orig = 2076i64; assert!( (orig - legacy).abs() as f64 <= orig as f64 * 0.02, @@ -7535,7 +9145,7 @@ mod tests { let keys = genesis_federation_keys(&config); let threshold = keys.len() / 2 + 1; let redeem = build_federation_redeem_script(&keys, threshold); - let fee = require_funds_for_fee(&redeem, threshold as u64, 30_000, true, &config); + let fee = require_funds_for_fee(&redeem, threshold as u64, 30_000, true, false, &config); assert!( fee < config.minimum_pegout_tx_value, "require_funds_for_fee {fee} should be below {} so reason is LOW_AMOUNT", @@ -7782,11 +9392,15 @@ 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) + .unwrap() + .expect("multi build"); + let single = crate::bridge::release_tx::build_empty_wallet_to( + &utxos, &refund, &fly_a, 1000, 2, false, + ) + .unwrap() + .expect("single build"); assert_eq!( bitcoin::consensus::serialize(&multi.tx), bitcoin::consensus::serialize(&single.tx), @@ -7798,8 +9412,10 @@ 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) + .unwrap() + .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. @@ -7886,7 +9502,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" @@ -7906,28 +9528,1653 @@ 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::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), + Err(Invalid::IllegalArgument(why)) => ("illegal argument", 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 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") + ); + // 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 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()); + } + + /// 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 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_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" + ); + } - // 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()); + /// A Flyover call with the given BTC addresses at `height`, as the LBC + /// 0x4242..42 makes it, for a transaction whose confirmations rskj's + /// validations refuse: what the method answers, by name. + fn flyover_answer( + config: &BridgeConstants, + cfg: &RskHardforkConfig, + height: u64, + user: &[u8], + lp: &[u8], + registered: Option<[u8; 32]>, + ) -> &'static str { + use revm::MainContext; + let lbc = RskAddress::repeat_byte(0x42); + let mut lbc_word = [0u8; 32]; + lbc_word[12..].copy_from_slice(lbc.as_slice()); + let args = encode::args(&[ + Arg::Bytes(&ONE_INPUT), + Arg::Word(encode::int(i64::from(i32::MAX))), + Arg::Bytes(&[0xFF; 3]), + Arg::Word([0x11; 32]), + Arg::Bytes(user), + Arg::Word(lbc_word), + Arg::Bytes(lp), + Arg::Word([0; 32]), + ]); + let mut ctx = revm::Context::mainnet().with_chain(crate::raw_storage::RskChainExt::default()); + ctx.block.number = U256::from(height); + if let Some(hash) = registered { + mark_flyover_derivation_hash_used(&mut ctx, &calculate_btc_tx_hash(&ONE_INPUT), &hash); + } + let out = register_fast_bridge_btc_transaction( + &mut ctx, + &args, + 0, + config, + cfg, + &BridgeTxContext::default(), + lbc, + 2, + ) + .unwrap() + .bytes; + [ + (flyover_codes::GENERIC_ERROR, "generic"), + (flyover_codes::UNPROCESSABLE_VALIDATIONS, "validations"), + (flyover_codes::UNPROCESSABLE_ALREADY_PROCESSED, "already processed"), + ] + .into_iter() + .find(|(code, _)| flyover_code_output(*code) == out) + .map_or("other", |(_, name)| name) + } + + /// rskj reads both Flyover BTC addresses with bitcoinj before anything + /// else (`BridgeUtils.deserializeBtcAddressWithVersion`) and answers + /// GENERIC_ERROR where that throws: in both eras for a version the network + /// doesn't accept, and before RSKIP284 for a version that is negative as a + /// signed byte, which testnet's P2SH version 196 (`0xc4`) is. Testnet + /// #2,122,593 makes such a call: rskj answers GENERIC_ERROR and the LBC + /// carries on, where rustock answered UNPROCESSABLE_VALUE_ZERO and the LBC + /// reverted. + #[test] + fn a_flyover_address_bitcoinj_refuses_is_a_generic_error() { + let with = |version: u8| [&[version][..], &[0x51; 20][..]].concat(); + let (p2pkh, p2sh) = (with(0x6F), with(0xC4)); + let longer = [&p2pkh[..], &[0xEE]].concat(); + + // Testnet past iris300, before its RSKIP284 (#2,581,800). + let (testnet, testnet_cfg) = (BridgeConstants::testnet(), RskHardforkConfig::testnet()); + let before = |user: &[u8], lp: &[u8]| flyover_answer(&testnet, &testnet_cfg, 2_122_593, user, lp, None); + assert_eq!(before(&p2pkh, &p2pkh), "validations"); + assert_eq!(before(&p2sh, &p2pkh), "generic", "a P2SH address, -60 as a signed byte"); + assert_eq!(before(&p2pkh, &p2sh), "generic", "the liquidity provider's"); + assert_eq!(before(&with(0x00), &p2pkh), "generic", "a mainnet version"); + assert_eq!(before(&longer, &p2pkh), "validations", "a 22nd byte, ignored"); + + // From testnet's RSKIP284 the version is read unsigned, and the + // address is 21 bytes exactly. + let from = |user: &[u8], lp: &[u8]| flyover_answer(&testnet, &testnet_cfg, 2_581_800, user, lp, None); + assert_eq!(from(&p2sh, &p2sh), "validations"); + assert_eq!(from(&with(0x00), &p2pkh), "generic", "a mainnet version"); + assert_eq!(from(&longer, &p2pkh), "generic", "a 22nd byte"); + + // Mainnet before its RSKIP284 (hop400, #4,598,500): both its versions, + // 0 and 5, are positive bytes. + let (mainnet, mainnet_cfg) = (BridgeConstants::mainnet(), RskHardforkConfig::mainnet()); + let on_mainnet = |user: &[u8], lp: &[u8]| flyover_answer(&mainnet, &mainnet_cfg, 4_000_000, user, lp, None); + assert_eq!(on_mainnet(&with(0x05), &with(0x00)), "validations"); + assert_eq!(on_mainnet(&p2pkh, &with(0x00)), "generic", "a testnet version"); + } + + /// The derivation hash is built from the address bitcoinj read and wrote + /// back (`serializeBtcAddressWithVersion`), not from the argument: before + /// RSKIP284 a 22nd byte is dropped, so a payment registered under the 21 + /// bytes is found registered when the argument has 22. + #[test] + fn a_flyover_derivation_hash_reads_the_address_bitcoinj_read() { + let (config, cfg) = (BridgeConstants::testnet(), RskHardforkConfig::testnet()); + let address = [0x6F; 21]; + let longer = [&address[..], &[0xEE]].concat(); + let lbc = RskAddress::repeat_byte(0x42); + let hash = flyover_derivation_hash(&[0x11; 32], &address, &lbc, &address); + assert_eq!( + flyover_answer(&config, &cfg, 2_122_593, &longer, &address, Some(hash)), + "already processed" + ); } - /// 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() + ); + } + + // --- 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, 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); + 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 { + 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 { + regtest_bridge_at(raw, 10, &RskHardforkConfig::rskj_regtest()) + } + + /// The same at RSK block `number`, with the headers taken under `cfg`. + fn regtest_bridge_at(raw: &[u8], number: u64, cfg: &RskHardforkConfig) -> impl crate::RskContextTr { + use revm::MainContext; + let block = revm::context::BlockEnv { + number: U256::from(number), + 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(raw); + 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, + cfg, + ) + .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 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 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), + ); + } + outcome(&mut ctx, tx, height, &RskHardforkConfig::rskj_regtest()) + } + + /// rskj's `validationsForRegisterBtcTransaction` throws + /// `BridgeIllegalArgumentException` for a partial merkle tree of the wrong + /// 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_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); + // 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 + /// 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) { + 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::Word(encode::int(height)), + Arg::Bytes(&tree), + ]); + let ok = register_btc_transaction( + ctx, + &args, + 0, + &BridgeConstants::rskj_regtest(), + cfg, + &BridgeTxContext::default(), + ) + .is_ok(); + let logs = ctx.journal_mut().take_logs().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) + } + + /// RSKj's regtest federation's output script. + fn regtest_federation_script() -> bitcoin::ScriptBuf { + regtest_federation_script_for(&RskHardforkConfig::rskj_regtest()) + } + + /// The same under `cfg`. + fn regtest_federation_script_for(cfg: &RskHardforkConfig) -> bitcoin::ScriptBuf { + let config = BridgeConstants::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 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, 3, 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)); + } + + /// 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)); + } + + /// RSKj's regtest with every network upgrade after `last` taken off its + /// ladder. + fn rskj_regtest_until(last: crate::hardfork::RskNetworkUpgrade) -> RskHardforkConfig { + use crate::hardfork::RskNetworkUpgrade::*; + [ + Orchid, Wasabi100, TwoToThree, Papyrus200, Iris300, Hop400, Fingerroot500, + Arrowhead600, Arrowhead631, Lovell700, Reed800, Reed810, Vetiver900, + ] + .into_iter() + .filter(|upgrade| *upgrade > last) + .fold(RskHardforkConfig::rskj_regtest(), |cfg, upgrade| cfg.with_hardfork(upgrade, None)) + } + + /// A native P2WPKH input: nothing in its scriptSig, from which rskj can + /// name no sender. + fn native_segwit() -> (bitcoin::ScriptBuf, bitcoin::Witness) { + let mut witness = bitcoin::Witness::new(); + witness.push([0x30u8; 71]); + witness.push(real_pubkey()); + (bitcoin::ScriptBuf::new(), witness) + } + + /// An OP_RETURN output's script pushing `data`. + fn op_return(data: &[u8]) -> bitcoin::ScriptBuf { + bitcoin::ScriptBuf::from_bytes([&[0x6a, data.len() as u8][..], data].concat()) + } + + /// An RSKT payload of `version` naming `destination`. + fn rskt(version: u8, destination: [u8; 20]) -> bitcoin::ScriptBuf { + op_return(&[&b"RSKT"[..], &[version], &destination].concat()) + } + + /// The events a registration logged, each as (topics, data). + type Logged = Vec<(Vec, Vec)>; + + /// `registerBtcTransaction` of `tx` at Bitcoin height 350 in `ctx`, RSKj's + /// regtest Bridge at RSK block `block`, under `cfg`: whether the call + /// succeeded, whether it marked the transaction processed, the events it + /// logged, and how many UTXOs the active federation then holds. + fn registration_in( + ctx: &mut CTX, + tx: &BtcTransaction, + block: u64, + cfg: &RskHardforkConfig, + ) -> (bool, bool, Logged, usize) { + let raw = btc_serialize(tx); + let tree = one_leaf_tree(&raw); + let args = encode::args(&[Arg::Bytes(&raw), Arg::Word(encode::int(350)), Arg::Bytes(&tree)]); + let ok = register_btc_transaction( + ctx, + &args, + 0, + &BridgeConstants::rskj_regtest(), + cfg, + &BridgeTxContext::default(), + ) + .is_ok(); + let logs = ctx + .journal_mut() + .take_logs() + .into_iter() + .map(|log| (log.data.topics().to_vec(), log.data.data.to_vec())) + .collect(); + let processed = is_btc_tx_processed(ctx, &legacy_btc_txid(tx)); + let utxos = super::super::storage::load_federation_utxos(ctx, cfg, block).len(); + (ok, processed, logs, utxos) + } + + /// The same at RSK block 10, the Bridge holding nothing but the chain. + fn registration(tx: &BtcTransaction, cfg: &RskHardforkConfig) -> (bool, bool, Logged, usize) { + let mut ctx = regtest_bridge_at(&btc_serialize(tx), 10, cfg); + registration_in(&mut ctx, tx, 10, cfg) + } + + fn rejected(tx: &BtcTransaction, reason: u64) -> (Vec, Vec) { + let event = super::super::legacy_events::rejected_pegin(&btc_txid_event_bytes(tx), reason); + (event.topics, event.data) + } + + fn unrefundable(tx: &BtcTransaction, reason: u64) -> (Vec, Vec) { + let event = super::super::legacy_events::unrefundable_pegin(&btc_txid_event_bytes(tx), reason); + (event.topics, event.data) + } + + fn pegin_btc( + to: RskAddress, + tx: &BtcTransaction, + amount: u64, + version: u64, + ) -> (Vec, Vec) { + let event = super::super::legacy_events::pegin_btc(to, &btc_txid_event_bytes(tx), amount, version); + (event.topics, event.data) + } + + /// The peg-in of testnet #4,513,687: one native-segwit input, so no sender + /// rskj can name, and no OP_RETURN. Before RSKIP379 + /// `PeginInformation.parse` throws for want of both, and from RSKIP170 + /// `legacyRegisterPegin`'s catch logs rejected_pegin(PEGIN_V1_INVALID_PAYLOAD=4), + /// then, with nowhere to refund to and the version still 0, + /// unrefundable_pegin(LEGACY_PEGIN_UNDETERMINED_SENDER=1), and marks the + /// transaction processed. Before RSKIP170 it does none of that. From + /// RSKIP379 `evaluateLegacyPegin` turns it down as an undetermined + /// sender's, marking it processed only between RSKIP459 and RSKIP551. + #[test] + fn a_peg_in_with_no_sender_and_no_instructions_testnet_4513687() { + use crate::hardfork::RskNetworkUpgrade::*; + let paying = |cfg: &RskHardforkConfig| { + spend(vec![native_segwit()], &[(1_000_000_000, regtest_federation_script_for(cfg))]) + }; + let cfg = rskj_regtest_until(Fingerroot500); + let tx = paying(&cfg); + assert_eq!( + registration(&tx, &cfg), + (true, true, vec![rejected(&tx, 4), unrefundable(&tx, 1)], 0) + ); + let cfg = rskj_regtest_until(Papyrus200); + let tx = paying(&cfg); + assert_eq!(registration(&tx, &cfg), (true, false, vec![], 0)); + for (cfg, marked) in [ + (rskj_regtest_until(Reed810), true), + (RskHardforkConfig::rskj_regtest(), false), + ] { + let tx = paying(&cfg); + assert_eq!( + registration(&tx, &cfg), + (true, marked, vec![rejected(&tx, 3), unrefundable(&tx, 1)], 0) + ); + } + } + + /// A peg-in whose RSKT payload doesn't parse, from no sender rskj can name: + /// `PeginInformation.parse` throws with the version still 0, so rskj logs + /// rejected_pegin(PEGIN_V1_INVALID_PAYLOAD=4) and, with nowhere to refund + /// to, unrefundable_pegin(LEGACY_PEGIN_UNDETERMINED_SENDER=1). Before + /// RSKIP379 (`legacyRegisterPegin`'s catch) it marks the transaction + /// processed; from it (`handleNonRefundablePegin`) only between RSKIP459 + /// and RSKIP551. + #[test] + fn a_peg_in_whose_payload_does_not_parse_from_no_sender_is_not_refundable() { + use crate::hardfork::RskNetworkUpgrade::*; + for (cfg, marked) in [ + (rskj_regtest_until(Fingerroot500), true), + (rskj_regtest_until(Reed810), true), + (RskHardforkConfig::rskj_regtest(), false), + ] { + // Version 2, which no rskj reads. + let tx = spend( + vec![native_segwit()], + &[(1_000_000_000, regtest_federation_script_for(&cfg)), (0, rskt(2, [0x42; 20]))], + ); + assert_eq!( + registration(&tx, &cfg), + (true, marked, vec![rejected(&tx, 4), unrefundable(&tx, 1)], 0) + ); + } + } + + /// From RSKIP170 rskj looks for the RSKT payload in every output's script, + /// and one bitcoinj can't parse throws the `PeginInstructionsException` a + /// payload that doesn't parse throws. A P2PKH sender's peg-in is refunded, + /// logging rejected_pegin(PEGIN_V1_INVALID_PAYLOAD=4), where it would be + /// credited: before RSKIP379 in `legacyRegisterPegin`'s catch, from it in + /// `evaluatePegin`'s. From no sender it is turned down for its payload, + /// not for its sender. Before RSKIP170 nothing reads the outputs so. + #[test] + fn from_rskip170_an_output_bitcoinj_cannot_parse_is_an_invalid_payload() { + use crate::hardfork::RskNetworkUpgrade::*; + let key = real_pubkey(); + let sender = (pushes(&[&[0x30; 71], &key]), bitcoin::Witness::new()); + let to = super::super::federation::rsk_address_from_public_key(&key).unwrap(); + let amount = 1_000_000_000; + // OP_PUSHDATA1 with no length after it. + let unparseable = bitcoin::ScriptBuf::from_bytes(vec![0x4c]); + let paying = |input: &(bitcoin::ScriptBuf, bitcoin::Witness), cfg: &RskHardforkConfig| { + spend( + vec![input.clone()], + &[ + (amount, regtest_federation_script_for(cfg)), + (0, unparseable.clone()), + ], + ) + }; + + for cfg in [ + rskj_regtest_until(Fingerroot500), + RskHardforkConfig::rskj_regtest(), + ] { + let tx = paying(&sender, &cfg); + let (ok, processed, logs, utxos) = registration(&tx, &cfg); + assert_eq!((ok, processed, utxos), (true, true, 0), "refunded"); + assert_eq!(logs.first(), Some(&rejected(&tx, 4))); + let credited = pegin_btc(to, &tx, amount, 0); + assert!(logs.iter().all(|(topics, _)| topics[0] != credited.0[0])); + } + + let cfg = RskHardforkConfig::rskj_regtest(); + let tx = paying(&native_segwit(), &cfg); + assert_eq!( + registration(&tx, &cfg), + (true, false, vec![rejected(&tx, 4), unrefundable(&tx, 1)], 0) + ); + + let cfg = rskj_regtest_until(Papyrus200); + let tx = paying(&native_segwit(), &cfg); + assert_eq!(registration(&tx, &cfg), (true, false, vec![], 0)); + } + + /// bitcoinj's `Script.getToAddress` reads P2PKH by its chunks, so a 20-byte + /// hash pushed with OP_PUSHDATA1 is still an address, and P2SH by its exact + /// bytes, so the same hash pushed that way is not. + #[test] + fn bitcoinj_reads_only_p2pkh_and_p2sh_as_addresses() { + let hash = [0x11u8; 20]; + let p2pkh = [&[0x76, 0xa9, 0x14][..], &hash, &[0x88, 0xac]].concat(); + let p2pkh_pushdata1 = [&[0x76, 0xa9, 0x4c, 0x14][..], &hash, &[0x88, 0xac]].concat(); + let p2sh = [&[0xa9, 0x14][..], &hash, &[0x87]].concat(); + let p2sh_pushdata1 = [&[0xa9, 0x4c, 0x14][..], &hash, &[0x87]].concat(); + let p2wpkh = [&[0x00, 0x14][..], &hash].concat(); + let short_hash = [&[0x76, 0xa9, 0x13][..], &hash[..19], &[0x88, 0xac]].concat(); + for (script, address) in [ + (p2pkh, true), + (p2pkh_pushdata1, true), + (p2sh, true), + (p2sh_pushdata1, false), + (p2wpkh, false), + (short_hash, false), + (vec![0x6a, 0x01, 0x01], false), + // An opcode where the hash goes. + (vec![0x76, 0xa9, 0x61, 0x88, 0xac], false), + (vec![0x4c], false), + (vec![], false), + ] { + assert_eq!(bitcoinj_reads_as_address(&script), address, "{script:02x?}"); + } + } + + /// A v1 peg-in over the locking cap, from no sender, with no refund + /// address in its payload: before RSKIP379 `processPegInVersion1` logs + /// rejected_pegin(PEGIN_CAP_SURPASSED=1), then `refundTxSender`, with + /// nowhere to refund to and the version 1, + /// unrefundable_pegin(PEGIN_V1_REFUND_ADDRESS_NOT_SET=2), and marks the + /// transaction processed. From RSKIP379 `evaluatePegin` registers a v1 + /// peg-in with `executePegIn`, which doesn't ask the cap. + #[test] + fn a_v1_peg_in_over_the_locking_cap() { + use crate::hardfork::RskNetworkUpgrade::*; + let destination = [0x42u8; 20]; + // Twice regtest's locking cap of 1,000 BTC. + let amount = 200_000_000_000; + let paying = |cfg: &RskHardforkConfig| { + spend( + vec![native_segwit()], + &[(amount, regtest_federation_script_for(cfg)), (0, rskt(1, destination))], + ) + }; + let cfg = rskj_regtest_until(Fingerroot500); + let tx = paying(&cfg); + assert_eq!( + registration(&tx, &cfg), + (true, true, vec![rejected(&tx, 1), unrefundable(&tx, 2)], 0) + ); + let cfg = RskHardforkConfig::rskj_regtest(); + let tx = paying(&cfg); + assert_eq!( + registration(&tx, &cfg), + (true, true, vec![pegin_btc(RskAddress::from(destination), &tx, amount, 1)], 1) + ); + } + + /// From RSKIP379 a P2PKH sender's peg-in is registered as + /// `evaluateLegacyPegin` decides, asking neither the lock whitelist nor + /// the locking cap: on regtest, whose whitelist is empty and never + /// disabled, rskj credits one over the cap that the rules before RSKIP379 + /// refund. + #[test] + fn from_rskip379_a_legacy_peg_in_asks_neither_the_whitelist_nor_the_cap() { + use crate::hardfork::RskNetworkUpgrade::*; + let key = real_pubkey(); + let sender = (pushes(&[&[0x30; 71], &key]), bitcoin::Witness::new()); + // Twice regtest's locking cap of 1,000 BTC. + let amount = 200_000_000_000; + let cfg = RskHardforkConfig::rskj_regtest(); + let tx = spend(vec![sender.clone()], &[(amount, regtest_federation_script_for(&cfg))]); + let to = super::super::federation::rsk_address_from_public_key(&key).unwrap(); + assert_eq!(registration(&tx, &cfg), (true, true, vec![pegin_btc(to, &tx, amount, 0)], 1)); + + let cfg = rskj_regtest_until(Fingerroot500); + let tx = spend(vec![sender], &[(amount, regtest_federation_script_for(&cfg))]); + let (ok, processed, logs, utxos) = registration(&tx, &cfg); + assert_eq!((ok, processed, utxos), (true, true, 0), "refunded"); + let credited = pegin_btc(to, &tx, amount, 0); + assert!(logs.iter().all(|(topics, _)| topics[0] != credited.0[0])); + } + + /// From RSKIP379 a multisig or P2SH-P2WSH sender's peg-in has its outputs + /// read by bitcoinj's `getPubKeyHash` (`hasOutputsToDifferentTypesOfFeds`), + /// which throws for one that is neither P2PKH nor P2SH; rskj doesn't catch + /// that, so the call fails. With outputs it can read, the peg-in is + /// refunded as before. + #[test] + fn from_rskip379_a_multisig_peg_in_fails_on_an_output_bitcoinj_cannot_hash() { + let redeem = build_federation_redeem_script(&test_keys(0x10), 2); + let cfg = RskHardforkConfig::rskj_regtest(); + let federation = (1_000_000_000, regtest_federation_script()); + let p2wpkh = bitcoin::ScriptBuf::from_bytes([&[0x00, 0x14][..], &[0x77; 20]].concat()); + for other in [p2wpkh, op_return(&[0xbe, 0xef])] { + let tx = spend(vec![signed_multisig(&redeem)], &[federation.clone(), (40_000, other)]); + assert!(!registration(&tx, &cfg).0, "the call fails"); + } + for other in [p2pkh_output_script(&[0x77; 20]), p2sh_output_script(&[0x77; 20])] { + let tx = spend(vec![signed_multisig(&redeem)], &[federation.clone(), (40_000, other)]); + let (ok, processed, logs, _) = registration(&tx, &cfg); + assert!(ok && processed); + assert_eq!((logs.len(), &logs[0]), (3, &rejected(&tx, 2)), "refunded"); + } + } + + /// `test_keys(0x30)` in `active_format` as the new federation and + /// `test_keys(0x20)` in `retiring_format` as the old one, the new one + /// created at block 0: from regtest's activation age of 150 blocks the new + /// one is active and the old one retiring. + fn with_retiring_federation( + ctx: &mut CTX, + active_format: u64, + retiring_format: u64, + ) { + use super::super::federation::{serialize_federation_multikey, StoredMember}; + use super::super::serialization::rlp_encode_u64; + use super::super::storage::{ + NEW_FEDERATION_FORMAT_VERSION_KEY, NEW_FEDERATION_KEY, OLD_FEDERATION_FORMAT_VERSION_KEY, + OLD_FEDERATION_KEY, + }; + let federation = |first: u8| { + let members: Vec = + test_keys(first).into_iter().map(StoredMember::from_btc).collect(); + serialize_federation_multikey(&members, 0, 0) + }; + bridge_store_bytes_named(ctx, OLD_FEDERATION_KEY, &federation(0x20)); + bridge_store_bytes_named(ctx, OLD_FEDERATION_FORMAT_VERSION_KEY, &rlp_encode_u64(retiring_format)); + bridge_store_bytes_named(ctx, NEW_FEDERATION_KEY, &federation(0x30)); + bridge_store_bytes_named(ctx, NEW_FEDERATION_FORMAT_VERSION_KEY, &rlp_encode_u64(active_format)); + } + + /// The output scripts of the active and the retiring federation in `ctx` + /// at block `block`. + fn live_federation_scripts( + ctx: &mut CTX, + cfg: &RskHardforkConfig, + block: u64, + ) -> (bitcoin::ScriptBuf, bitcoin::ScriptBuf) { + let config = BridgeConstants::rskj_regtest(); + let (_, active) = active_federation_keys_and_redeem(ctx, &config, cfg, block); + let active_format = active_federation_format(ctx, &config, cfg, block); + let (_, retiring) = retiring_federation_keys_and_redeem(ctx, &config, cfg, block) + .expect("a retiring federation"); + let retiring_format = + federation_format_version(ctx, super::super::storage::OLD_FEDERATION_FORMAT_VERSION_KEY); + ( + p2sh_output_script(&federation_output_hash160(&active, active_format)), + p2sh_output_script(&federation_output_hash160(&retiring, retiring_format)), + ) + } + + /// From RSKIP379 a multisig sender's peg-in that pays both live + /// federations, of different format versions, is turned down with no + /// refund (`evaluateLegacyPegin`, `hasOutputsToDifferentTypesOfFeds`): + /// rejected_pegin(LEGACY_PEGIN_MULTISIG_SENDER=2) and + /// unrefundable_pegin(OUTPUTS_SENT_TO_DIFFERENT_TYPES_OF_FEDS=4), and not + /// marked processed after RSKIP551. Paying two of one format, it's + /// refunded as before. + #[test] + fn from_rskip379_a_multisig_peg_in_to_two_kinds_of_federation_is_not_refunded() { + let redeem = build_federation_redeem_script(&test_keys(0x10), 2); + let cfg = RskHardforkConfig::rskj_regtest(); + for (active_format, retiring_format, refunded) in [(4000, 3000, false), (4000, 4000, true)] { + let (active, retiring) = { + let mut ctx = queue_ctx(); + with_retiring_federation(&mut ctx, active_format, retiring_format); + live_federation_scripts(&mut ctx, &cfg, 200) + }; + let tx = spend( + vec![signed_multisig(&redeem)], + &[(1_000_000_000, active), (1_000_000_000, retiring)], + ); + let mut ctx = regtest_bridge_at(&btc_serialize(&tx), 200, &cfg); + with_retiring_federation(&mut ctx, active_format, retiring_format); + let (ok, processed, logs, _) = registration_in(&mut ctx, &tx, 200, &cfg); + assert!(ok); + if refunded { + assert!(processed); + assert_eq!(logs[0], rejected(&tx, 2)); + assert!(!logs.contains(&unrefundable(&tx, 4))); + } else { + assert_eq!((processed, logs), (false, vec![rejected(&tx, 2), unrefundable(&tx, 4)])); + } + } + } + + /// bitcoinj reads a P2PKH output's hash where it reads a P2SH output's, so + /// a P2PKH output to a federation's script hash counts as paying that + /// federation; and a set of outputs paying no live federation is the + /// empty set's `NoSuchElementException`. + #[test] + fn outputs_to_different_types_of_feds_reads_outputs_as_bitcoinj_does() { + let (active, retiring) = (p2sh_output_script(&[0x11; 20]), p2sh_output_script(&[0x22; 20])); + let ask = |outputs: &[bitcoin::ScriptBuf], active_format: u64, retiring_format: u64| { + let paid: Vec<(u64, bitcoin::ScriptBuf)> = outputs.iter().map(|s| (1_000, s.clone())).collect(); + outputs_to_different_types_of_feds( + &spend(vec![native_segwit()], &paid), + (&active, active_format), + Some((&retiring, retiring_format)), + ) + }; + assert_eq!(ask(&[active.clone(), retiring.clone()], 4000, 3000), Ok(true)); + assert_eq!(ask(&[active.clone(), p2pkh_output_script(&[0x22; 20])], 4000, 3000), Ok(true)); + assert_eq!(ask(&[active.clone(), retiring.clone()], 3000, 3000), Ok(false)); + assert_eq!(ask(&[active.clone(), active.clone()], 4000, 3000), Ok(false)); + assert_eq!(ask(std::slice::from_ref(&retiring), 4000, 3000), Ok(false)); + assert!(ask(&[p2pkh_output_script(&[0x33; 20])], 4000, 3000).is_err()); + assert!(ask(&[active.clone(), op_return(&[0x01])], 4000, 4000).is_err()); + } + + /// 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 + /// 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)); + } + + /// Below the index's height, `isValidPegInTx` refuses its members' 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() { + use k256::ecdsa::signature::hazmat::PrehashSigner as _; + use k256::ecdsa::{Signature, SigningKey}; + let members: Vec = (0x10..0x13u8) + .map(|seed| SigningKey::from_slice(&[seed; 32]).unwrap()) + .collect(); + let key_of = |member: &SigningKey| -> [u8; 33] { + member + .verifying_key() + .to_encoded_point(true) + .as_bytes() + .try_into() + .unwrap() + }; + let mut keys: Vec<[u8; 33]> = members.iter().map(key_of).collect(); + keys.sort(); + let standard = build_federation_redeem_script(&keys, 2); + 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(); + 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()) + }; + // A spend two members sign: OP_0, their signatures in their keys' + // order, OP_0 for the ERP script's default branch, and the ERP script. + let signed_spend = |outputs: &[(u64, bitcoin::ScriptBuf)]| { + let mut tx = spend(vec![legacy(vec![])], outputs); + let hash = bitcoin::sighash::SighashCache::new(&tx) + .legacy_signature_hash(0, bitcoin::Script::from_bytes(&erp), 1) + .unwrap() + .to_byte_array(); + let mut signers: Vec<&SigningKey> = members.iter().collect(); + signers.sort_by_key(|member| key_of(member)); + let signatures: Vec> = signers[..2] + .iter() + .map(|member| { + let signature: Signature = member.sign_prehash(&hash).unwrap(); + [signature.to_der().as_bytes(), &[0x01]].concat() + }) + .collect(); + tx.input[0].script_sig = pushes(&[&[], &signatures[0], &signatures[1], &[], &erp]); + tx + }; + let to_federation = (1_000_000_000, regtest_federation_script()); + let paying_nothing = signed_spend(&[(40_000, p2sh_output_script(&[0x77; 20]))]); + let migrating = signed_spend(std::slice::from_ref(&to_federation)); + // 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, 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)); + 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 + ); + } + + /// rskj reads an ERP federation's standard redeem script out of its redeem + /// script with bitcoinj's parser, which has none for testnet's hard-coded + /// ERP script, a non-standard ERP federation's there before RSKIP284, and + /// throws a `ScriptException`. Below the index's height `isValidPegInTx` + /// catches it, and a transaction paying such a federation enough is a + /// peg-in; `isPegOutTx` of the live federations doesn't, and one it + /// refuses that is no migration fails the call. Another ERP federation's + /// is read, and the transaction is left alone. + #[test] + fn testnets_hard_coded_erp_script_has_no_standard_redeem_script() { + use super::super::peg_legacy::{LegacyRules, LiveFederation, PegType}; + /// The rules with `redeem` the active federation's, at its P2SH + /// script `p2sh`, and an old federation `old` no input spends. + fn rules<'a>( + old: &'a [u8], + (redeem, p2sh): (&'a [u8], &'a [u8]), + format: u64, + below_minimum: bool, + ) -> LegacyRules<'a> { + LegacyRules { + rskip199: true, + rskip201: true, + rskip305: false, + old_federation_p2sh: old, + active: LiveFederation { + p2sh, + redeem, + format, + }, + retiring: None, + retired_p2sh: None, + live_below_minimum: below_minimum, + active_below_minimum: below_minimum, + } + } + let old = p2sh_output_script(&[0x0d; 20]).into_bytes(); + let p2sh_of = + |redeem: &[u8]| p2sh_output_script(&redeem_script_hash160(redeem)).into_bytes(); + let hard_coded = legacy_erp_testnet_redeem_script(); + let hard_coded_p2sh = p2sh_of(hard_coded); + let p2sh_erp = build_erp_redeem_script( + &test_keys(0x10), + 2, + &test_keys(0x40), + 2, + 500, + ErpScript::P2sh, + ); + let p2sh_erp_p2sh = p2sh_of(&p2sh_erp); + // A multisig spend, whose standard redeem script is read and compared. + let tx = spend( + vec![signed_multisig(&build_federation_redeem_script( + &test_keys(0x20), + 2, + ))], + &[(40_000, p2sh_output_script(&[0x77; 20]))], + ); + let hard_coded = (hard_coded, hard_coded_p2sh.as_slice()); + assert_eq!( + rules(&old, hard_coded, 2000, false).transaction_type(&tx), + Ok(PegType::PegIn) + ); + assert!(rules(&old, hard_coded, 2000, true) + .transaction_type(&tx) + .is_err()); + assert_eq!( + rules(&old, (&p2sh_erp, &p2sh_erp_p2sh), 3000, true).transaction_type(&tx), + Ok(PegType::Unknown { + refused_for_an_input: false + }) + ); + } + + /// 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)); } } diff --git a/crates/execution/src/bridge/peg_legacy.rs b/crates/execution/src/bridge/peg_legacy.rs new file mode 100644 index 0000000..672dd42 --- /dev/null +++ b/crates/execution/src/bridge/peg_legacy.rs @@ -0,0 +1,243 @@ +//! rskj `PegUtilsLegacy.getTransactionType`: what a Bitcoin transaction +//! registered below the pegout index's height is to the Bridge, a peg-in, a +//! migration, a pegout, or nothing it registers. +//! +//! rskj tells an input that spends a federation in two ways. Its scriptSig +//! spends the federation's script, its signatures checked +//! (`scriptCorrectlySpendsTx`, run as bitcoinj runs it in +//! [`super::script_spend`]). Or the redeem script it carries is the +//! federation's: as it is before RSKIP201, and from RSKIP201 the standard +//! multisig it holds, which bitcoinj's parser reads out of an ERP or Flyover +//! redeem script ([`standard_redeem_script`]). Where bitcoinj throws something +//! the classification doesn't catch, the call fails. + +use bitcoin::Transaction; + +use super::peg::{ + federation_output_hash160, p2sh_output_script_program, redeem_script_hash160_pub, +}; +use super::rskj_sender_compat::{ + redeem_script_of_input, standard_redeem_script, written_back, Written, +}; +use super::script_spend::script_correctly_spends_tx; + +/// A standard multisig federation's format version. +const STANDARD_MULTISIG_FEDERATION: u64 = 1000; +/// A P2SH-P2WSH ERP federation's format version. +const P2SH_P2WSH_ERP_FEDERATION: u64 = 4000; + +/// The `ScriptException` a federation with no standard redeem script throws +/// where nothing catches it. +const NO_STANDARD_REDEEM: &str = "ScriptException reading a federation's standard redeem script"; + +/// What `getTransactionType` answers. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum PegType { + /// From RSKIP199, an input spends the old federation + /// (`txIsFromOldFederation`): a pegout or migration. + FromOldFederation, + PegIn, + Migration, + Pegout, + /// None of them: refused as a peg-in for one of its inputs, or for what it + /// pays the live federations. + Unknown { + refused_for_an_input: bool, + }, +} + +/// A live federation, as `getTransactionType` reads it. +pub(crate) struct LiveFederation<'a> { + /// `getP2SHScript`: the script its UTXOs are paid to. + pub p2sh: &'a [u8], + /// `getRedeemScript`. + pub redeem: &'a [u8], + /// Its stored format version. + pub format: u64, +} + +impl LiveFederation<'_> { + /// `PegUtilsLegacy.getFederationStandardRedeemScript`: a standard multisig + /// federation's redeem script, an ERP federation's default multisig + /// (`ErpFederation.getDefaultRedeemScript`), which bitcoinj's parser reads + /// out of its redeem script. `Ok(None)` is that parser's `ScriptException` + /// where it has none to give: for testnet's hard-coded ERP script, which a + /// non-standard ERP federation there has before RSKIP284. + fn standard_redeem(&self) -> Result, &'static str> { + if self.format == STANDARD_MULTISIG_FEDERATION { + return Ok(Some(Ok(self.redeem.to_vec()))); + } + standard_redeem_script(self.redeem) + } + + /// `PegUtilsLegacy.getFederationStandardP2SHScript`: the P2SH script of + /// that, a P2SH-P2WSH federation's P2SH-P2WSH one. What it can't read + /// throws here where nothing catches it. + fn standard_p2sh(&self) -> Result, &'static str> { + let standard = self.standard_redeem()?.ok_or(NO_STANDARD_REDEEM)??; + Ok(p2sh_output_script_program(&federation_output_hash160( + &standard, + self.format, + ))) + } +} + +/// What `getTransactionType` reads besides the transaction. +pub(crate) struct LegacyRules<'a> { + pub rskip199: bool, + pub rskip201: bool, + pub rskip305: bool, + /// The old federation's P2SH script (`getOldFederationAddress`). + pub old_federation_p2sh: &'a [u8], + pub active: LiveFederation<'a>, + pub retiring: Option>, + /// `getLastRetiredFederationP2SHScript`, from RSKIP186. + pub retired_p2sh: Option<&'a [u8]>, + /// `isValidPegInTx`'s last test, of what the transaction pays the live + /// federations: below the minimum peg-in value, from RSKIP293 in any one + /// output, before it in total. + pub live_below_minimum: bool, + /// The same of what it pays the active federation alone. + pub active_below_minimum: bool, +} + +impl LegacyRules<'_> { + /// `PegUtilsLegacy.getTransactionType`. `Err` is a throw nothing catches. + pub(crate) fn transaction_type(&self, tx: &Transaction) -> Result { + if self.rskip199 + && (0..tx.input.len()) + .any(|i| script_correctly_spends_tx(tx, i, self.old_federation_p2sh)) + { + return Ok(PegType::FromOldFederation); + } + let live: Vec<&LiveFederation> = std::iter::once(&self.active) + .chain(self.retiring.as_ref()) + .collect(); + let refused_for_an_input = self.refused_for_an_input(tx, &live, self.retired_p2sh)?; + if !refused_for_an_input && !self.live_below_minimum { + return Ok(PegType::PegIn); + } + if self.is_migration(tx)? { + return Ok(PegType::Migration); + } + let live_standard_p2sh = live + .iter() + .map(|federation| federation.standard_p2sh()) + .collect::, _>>()?; + if self.is_pegout(tx, &live_standard_p2sh)? { + return Ok(PegType::Pegout); + } + Ok(PegType::Unknown { + refused_for_an_input, + }) + } + + /// The inputs `isValidPegInTx` refuses a peg-in for: one that spends a + /// live federation's script or the last retired one's, its signatures + /// checked, or from RSKIP201 one whose redeem script's standard multisig + /// is a live federation's, or as P2SH is the last retired one's script. + /// It reads each input's redeem script whatever the upgrade, and a + /// scriptSig bitcoinj can't parse throws there. + fn refused_for_an_input( + &self, + tx: &Transaction, + live: &[&LiveFederation], + retired_p2sh: Option<&[u8]>, + ) -> Result { + for (index, input) in tx.input.iter().enumerate() { + if live + .iter() + .any(|federation| script_correctly_spends_tx(tx, index, federation.p2sh)) + || retired_p2sh.is_some_and(|script| script_correctly_spends_tx(tx, index, script)) + { + return Ok(true); + } + let redeem = redeem_script_of_input(input.script_sig.as_bytes(), input.witness.last())?; + let (true, Some(redeem)) = (self.rskip201, redeem) else { + continue; + }; + if standard_redeem_is_a_federations(&redeem, live, retired_p2sh)? == Some(true) { + return Ok(true); + } + } + Ok(false) + } + + /// `PegUtilsLegacy.isMigrationTx`: an input that is a pegout of the + /// retiring or the last retired federation, by their standard scripts, in + /// a transaction that is a peg-in to the active federation alone. + fn is_migration(&self, tx: &Transaction) -> Result { + if self.retired_p2sh.is_none() && self.retiring.is_none() { + return Ok(false); + } + let mut scripts = Vec::new(); + if let Some(retired) = self.retired_p2sh { + scripts.push(retired.to_vec()); + } + if let Some(retiring) = &self.retiring { + scripts.push(retiring.standard_p2sh()?); + } + let from_retiring_or_retired = self.is_pegout(tx, &scripts)?; + let to_active = + !self.refused_for_an_input(tx, &[&self.active], None)? && !self.active_below_minimum; + Ok(from_retiring_or_retired && to_active) + } + + /// `PegUtilsLegacy.isPegOutTx`: an input whose redeem script's P2SH + /// script, or from RSKIP305 its P2SH-P2WSH one, is among `scripts`. The + /// redeem script is taken as it is before RSKIP201 and from RSKIP201 as + /// its standard multisig; an input with neither is passed over. + fn is_pegout(&self, tx: &Transaction, scripts: &[Vec]) -> Result { + for input in &tx.input { + let Some(redeem) = + redeem_script_of_input(input.script_sig.as_bytes(), input.witness.last())? + else { + continue; + }; + let written = if self.rskip201 { + standard_redeem_script(&redeem)? + } else { + written_back(&redeem) + }; + let Some(written) = written else { + continue; + }; + let program = written?; + let p2sh = p2sh_output_script_program(&redeem_script_hash160_pub(&program)); + let p2sh_p2wsh = p2sh_output_script_program(&federation_output_hash160( + &program, + P2SH_P2WSH_ERP_FEDERATION, + )); + if scripts.contains(&p2sh) || (self.rskip305 && scripts.contains(&p2sh_p2wsh)) { + return Ok(true); + } + } + Ok(false) + } +} + +/// The try block of `isValidPegInTx` from RSKIP201: whether `redeem`'s +/// standard multisig is a live federation's, or its P2SH script the last +/// retired federation's. `None` is the `ScriptException` it catches, from the +/// input's redeem script or from a federation's. `Script.equals` writes the +/// federation's program and then the input's, and the input's P2SH script +/// writes it again where no federation's was compared. +fn standard_redeem_is_a_federations( + redeem: &[u8], + live: &[&LiveFederation], + retired_p2sh: Option<&[u8]>, +) -> Result, &'static str> { + let Some(written) = standard_redeem_script(redeem)? else { + return Ok(None); + }; + for federation in live { + let Some(standard) = federation.standard_redeem()? else { + return Ok(None); + }; + if standard? == written.clone()? { + return Ok(Some(true)); + } + } + let p2sh = p2sh_output_script_program(&redeem_script_hash160_pub(&written?)); + Ok(Some(retired_p2sh.is_some_and(|retired| p2sh == retired))) +} diff --git a/crates/execution/src/bridge/pegin_instructions.rs b/crates/execution/src/bridge/pegin_instructions.rs index 796a5e3..18419d4 100644 --- a/crates/execution/src/bridge/pegin_instructions.rs +++ b/crates/execution/src/bridge/pegin_instructions.rs @@ -26,7 +26,8 @@ //! back to **legacy** (version-0) processing. Modeled here as `Ok(None)`. //! * `PeginInstructionsException` / `PeginInstructionsParseException` (more //! than one RSKT OP_RETURN, an unsupported protocol version, a bad data -//! length, or a bad refund-address type) → the v1 peg-in is **rejected** +//! length, a bad refund-address type, or any output whose script bitcoinj +//! can't parse) → the v1 peg-in is **rejected** //! (refund + mark processed + `rejected_pegin(PEGIN_V1_INVALID_PAYLOAD)`). //! Modeled here as `Err(PeginInstructionsError)`. @@ -99,6 +100,11 @@ pub fn build_pegin_instructions( /// magic. Returns the payload of the single match (`Ok(Some)`), `Ok(None)` if /// there is none (`NoOpReturnException`), or `Err` if more than one carries the /// RSKT magic (`PeginInstructionsException`). +/// +/// `hasOpReturnForRsk` reads every output's script with `getScriptPubKey()`, +/// OP_RETURN or not, and the `ScriptException` of one bitcoinj can't parse +/// reaches `buildPeginInstructions`' catch, which throws a +/// `PeginInstructionsException` for it: `Err` as well, whichever output it is. fn extract_op_return_data( btc_tx: &BtcTransaction, ) -> Result>, PeginInstructionsError> { @@ -106,6 +112,9 @@ fn extract_op_return_data( let mut occurrences = 0u32; for output in &btc_tx.output { + if !super::rskj_sender_compat::parses_as_script(output.script_pubkey.as_bytes()) { + return Err(PeginInstructionsError); + } if let Some(data) = op_return_rsk_payload(output.script_pubkey.as_bytes()) { found = Some(data); occurrences += 1; @@ -337,4 +346,46 @@ mod tests { let instr = build_pegin_instructions(&tx).unwrap().unwrap(); assert_eq!(instr.rsk_destination, dest); } + + /// rskj reads every output's script looking for the RSKT payload, and a + /// script bitcoinj can't parse throws, OP_RETURN or not, before or after + /// the payload, or with no payload at all. One it can parse, however + /// unusual, is passed over. + #[test] + fn an_output_bitcoinj_cannot_parse_is_an_invalid_payload() { + let rsk = op_return_script(&rsk_op_return_payload(1, [0x33; 20], None)); + // OP_PUSHDATA1 with no length; a push of two bytes with one left. + for unparseable in [ + vec![0x4c], + vec![0x02, 0x01], + vec![0x6a, 0x4c], + vec![0x6a, 0x05, b'R'], + ] { + let unparseable = ScriptBuf::from_bytes(unparseable); + for outputs in [ + vec![unparseable.clone()], + vec![rsk.clone(), unparseable.clone()], + vec![unparseable.clone(), rsk.clone()], + ] { + assert_eq!( + build_pegin_instructions(&tx_with_outputs(outputs)), + Err(PeginInstructionsError) + ); + } + } + + // OP_PUSHDATA1 of nothing, and an opcode on its own. + let unusual = vec![ + ScriptBuf::from_bytes(vec![0x4c, 0x00]), + ScriptBuf::from_bytes(vec![0xff]), + ]; + assert_eq!( + build_pegin_instructions(&tx_with_outputs(unusual.clone())), + Ok(None) + ); + let instr = build_pegin_instructions(&tx_with_outputs([unusual, vec![rsk]].concat())) + .unwrap() + .unwrap(); + assert_eq!(instr.rsk_destination, [0x33; 20]); + } } diff --git a/crates/execution/src/bridge/pmt.rs b/crates/execution/src/bridge/pmt.rs index e9f6ec5..3b58c70 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,96 @@ 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 { - /// 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. - 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 + /// 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 }; - - let mut hashes = Vec::with_capacity(count); - let data_start = array_offset + 32; - 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); - } - - // 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)) + (hashes.len() <= 32 && significant <= hashes.len()).then_some(Self { hashes, path }) } - /// Reduce the branch from a given tx hash to compute the merkle root. - pub fn reduce_from(&self, tx_hash: &[u8; 32]) -> [u8; 32] { + /// 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, + /// 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 = crate::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)) + } + + /// `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 +130,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 +144,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 + 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.oneHashBranch - /// assertBranchCorrectlyProves(hashes, path, txHash, expectedMerkleRoot) + /// 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_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"); - } - - /// Ported from rskj MerkleBranchTest.twoHashesBranch - #[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 +363,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/release_tx.rs b/crates/execution/src/bridge/release_tx.rs index 15f0a9b..ec71d2f 100644 --- a/crates/execution/src/bridge/release_tx.rs +++ b/crates/execution/src/bridge/release_tx.rs @@ -20,8 +20,12 @@ //! - 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`). An input +//! spent with testnet's hard-coded ERP redeem script cannot be sized, and +//! the build throws (`signatures_required`). use bitcoin::absolute::LockTime; use bitcoin::transaction::Version; @@ -125,6 +129,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(); @@ -162,6 +197,49 @@ pub fn redeem_script_threshold(redeem_script: &[u8]) -> usize { .unwrap_or(0) } +/// bitcoinj-thin threw while it sized an input. `Wallet.completeTx` refuses a +/// build with failures of its own, which `ReleaseTransactionBuilder` catches; +/// this one nothing catches, and the Bridge method that asked for the build +/// throws. rskj writes the Bridge's storage only once a method returns, so +/// none of what the method changed is kept; what it logged stays logged. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct UncountableSignatures; + +impl From for revm::precompile::PrecompileError { + fn from(_: UncountableSignatures) -> Self { + Self::other("Unsupported script type") + } +} + +/// bitcoinj-thin `Script.getNumberOfSignaturesRequiredToSpend` on a P2SH +/// input's redeem script, as `Wallet.estimateBytesForSigning` asks it: the +/// threshold of the multisig the script's parser finds (`getM()`), which is +/// `redeem_script_threshold`. rskj gives every non-standard ERP federation on +/// testnet, before RSKIP284, a hard-coded redeem script. Its parser is +/// `NonStandardErpRedeemScriptParserHardcoded`, whose `getM()` is -1, so +/// bitcoinj takes the script for no multisig and throws "Unsupported script +/// type". `RedeemScriptParserFactory` knows that script by its chunks and +/// their offsets, so only as a whole redeem script: inside a Flyover redeem +/// script it is parsed by its structure, and counts 3. +pub fn signatures_required(redeem_script: &[u8]) -> Result { + if redeem_script == super::peg::legacy_erp_testnet_redeem_script() { + return Err(UncountableSignatures); + } + Ok(redeem_script_threshold(redeem_script)) +} + +/// bitcoinj `Wallet.estimateBytesForSigning` over P2SH inputs, each spent with +/// its own redeem script: `signatures * SIG_SIZE + redeemScript.len()` apiece. +/// It throws at the first input it cannot count. +pub fn signing_bytes<'r>( + redeem_scripts: impl IntoIterator, +) -> Result { + redeem_scripts + .into_iter() + .map(|redeem| Ok(signatures_required(redeem)? * SIG_SIZE + redeem.len())) + .sum() +} + /// Replica of `Wallet.completeTx` for the bridge peg-out path /// (`recipientsPayFees = true`, `shuffleOutputs = false`, legacy P2SH /// multisig federation, `ensureMinRequiredFee = true`). @@ -172,8 +250,10 @@ pub fn redeem_script_threshold(redeem_script: &[u8]) -> usize { /// flyover UTXO this is the `PUSH32 OP_DROP ` /// wrapper (34 bytes longer than the plain fed redeem), which changes both the /// USE_OP_ZERO placeholder scriptSig and the per-input fee-sizing estimate. -/// Returns `None` on any of bitcoinj's build failures (dusty send, insufficient -/// money, could not adjust downwards, exceeded max size). +/// Returns `Ok(None)` on any of bitcoinj's build failures (dusty send, +/// insufficient money, could not adjust downwards, exceeded max size), and +/// `Err` where bitcoinj throws instead, unable to size a selected input +/// (`signatures_required`). pub fn complete_pegout_tx<'r, F>( available_utxos: &[BridgeUtxo], outputs: &[PegoutOutput], @@ -182,18 +262,56 @@ pub fn complete_pegout_tx<'r, F>( fee_per_kb: u64, version: i32, is_segwit: bool, -) -> Option +) -> Result, UncountableSignatures> where F: Fn(&BridgeUtxo) -> &'r [u8], { + build_pegout_tx( + available_utxos, + outputs, + change_script, + redeem_for, + fee_per_kb, + version, + is_segwit, + ) + .map(Result::ok) +} + +/// Why bitcoinj's `Wallet.completeTx` refused a build, as far as rskj's +/// `ReleaseTransactionBuilder.Response` tells the Bridge. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum BuildRefusal { + /// `EXCEED_MAX_TRANSACTION_SIZE`: the transaction is over bitcoinj's + /// 100,000 bytes (`Wallet.ExceededMaxTransactionSize`). + ExceededMaxTransactionSize, + /// Any other: a dusty output, too little money, an output the fee leaves + /// below dust, an input bitcoinj cannot spend. + Other, +} + +/// [`complete_pegout_tx`], with why a build is refused. +pub fn build_pegout_tx<'r, F>( + available_utxos: &[BridgeUtxo], + outputs: &[PegoutOutput], + change_script: &ScriptBuf, + redeem_for: F, + fee_per_kb: u64, + version: i32, + is_segwit: bool, +) -> Result, UncountableSignatures> +where + F: Fn(&BridgeUtxo) -> &'r [u8], +{ + let refused = Ok(Err(BuildRefusal::Other)); if outputs.is_empty() { - return None; + return refused; } // completeTx: dusty sends are rejected up front (DustySendRequested). for output in outputs { if output.amount_satoshis < min_non_dust_value(&output.script) { - return None; + return refused; } } @@ -204,7 +322,7 @@ where available_utxos.iter().map(|u| (u, redeem_for(u))).collect(); candidates.sort_by(|a, b| a.0.tx_hash.cmp(&b.0.tx_hash).then(a.0.vout.cmp(&b.0.vout))); if candidates.iter().any(|(_, r)| redeem_script_threshold(r) == 0) { - return None; + return refused; } let value: u64 = outputs.iter().map(|o| o.amount_satoshis).sum(); @@ -225,7 +343,7 @@ where v -= (fee % n_outputs) as i128; } if v < min_non_dust_value(&output.script) as i128 { - return None; + return refused; } out_values.push(v as u64); } @@ -242,7 +360,7 @@ where gathered += utxo.value_satoshis; } if gathered < value { - return None; + return refused; } let mut tx_outputs: Vec = outputs @@ -263,7 +381,7 @@ where let missing = min_change - change; let first = tx_outputs[0].value.to_sat() as i128 - missing as i128; if first < 0 || (first as u64) < min_non_dust_value(&outputs[0].script) { - return None; + return refused; } tx_outputs[0].value = Amount::from_sat(first as u64); change = min_change; @@ -301,10 +419,7 @@ where // vsize = (baseSize + signing + 3*baseSize) / 4. let base_size = bitcoin::consensus::serialize(&tx).len() + if is_segwit { selected.len() * 36 } else { 0 }; - let signing: usize = selected - .iter() - .map(|(_, r)| redeem_script_threshold(r) * SIG_SIZE + r.len()) - .sum(); + let signing = signing_bytes(selected.iter().map(|(_, r)| *r))?; let size = if is_segwit { (base_size + signing + 3 * base_size) / 4 } else { @@ -319,21 +434,15 @@ 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 { - return None; + if !fill_inputs( + &mut tx, + selected.iter().map(|(_, redeem)| *redeem), + is_segwit, + ) { + return Ok(Err(BuildRefusal::ExceededMaxTransactionSize)); } let used_utxos = selected.into_iter().map(|(u, _)| u.clone()).collect(); - return Some(BuiltPegout { tx, used_utxos }); + return Ok(Ok(BuiltPegout { tx, used_utxos })); } fee = fee_needed; } @@ -344,7 +453,8 @@ where /// outputs are FIXED and the change output to `change_script` (the active /// federation) absorbs the size-based fee. Selection target is `value + fee`; /// the fee grows until it covers the tx size. A dusty change output is dropped -/// into the fee (no change output) — bitcoinj `Wallet.completeTx`. +/// into the fee (no change output) — bitcoinj `Wallet.completeTx`. It refuses +/// a build, and throws, where `complete_pegout_tx` does. pub fn complete_recipients_dont_pay_fees_tx<'r, F>( available_utxos: &[BridgeUtxo], outputs: &[PegoutOutput], @@ -353,17 +463,17 @@ pub fn complete_recipients_dont_pay_fees_tx<'r, F>( fee_per_kb: u64, version: i32, is_segwit: bool, -) -> Option +) -> Result, UncountableSignatures> where F: Fn(&BridgeUtxo) -> &'r [u8], { if outputs.is_empty() { - return None; + return Ok(None); } // completeTx: dusty sends are rejected up front (DustySendRequested). for output in outputs { if output.amount_satoshis < min_non_dust_value(&output.script) { - return None; + return Ok(None); } } @@ -372,7 +482,7 @@ where available_utxos.iter().map(|u| (u, redeem_for(u))).collect(); candidates.sort_by(|a, b| a.0.tx_hash.cmp(&b.0.tx_hash).then(a.0.vout.cmp(&b.0.vout))); if candidates.iter().any(|(_, r)| redeem_script_threshold(r) == 0) { - return None; + return Ok(None); } let value: u64 = outputs.iter().map(|o| o.amount_satoshis).sum(); @@ -390,7 +500,7 @@ where gathered += utxo.value_satoshis; } if gathered < target { - return None; + return Ok(None); } // Recipient outputs are unchanged; the change output absorbs the fee. @@ -436,10 +546,7 @@ where // `(baseSize + signing + 3*baseSize) / 4`. let base_size = bitcoin::consensus::serialize(&tx).len() + if is_segwit { selected.len() * 36 } else { 0 }; - let signing: usize = selected - .iter() - .map(|(_, r)| redeem_script_threshold(r) * SIG_SIZE + r.len()) - .sum(); + let signing = signing_bytes(selected.iter().map(|(_, r)| *r))?; let size = if is_segwit { (base_size + signing + 3 * base_size) / 4 } else { @@ -450,22 +557,15 @@ 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 { - return None; + if !fill_inputs( + &mut tx, + selected.iter().map(|(_, redeem)| *redeem), + is_segwit, + ) { + return Ok(None); } let used_utxos = selected.into_iter().map(|(u, _)| u.clone()).collect(); - return Some(BuiltPegout { tx, used_utxos }); + return Ok(Some(BuiltPegout { tx, used_utxos })); } fee = fee_needed; } @@ -476,19 +576,30 @@ 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. +/// +/// Returns `Ok(None)` where bitcoinj refuses the refund, and `Err` where it +/// throws, unable to size the inputs (`signatures_required`), which it asks +/// before whether the refund is dust. pub fn build_empty_wallet_to( utxos: &[BridgeUtxo], refund_script: &ScriptBuf, redeem_script: &[u8], fee_per_kb: u64, version: i32, -) -> Option { + is_segwit: bool, +) -> Result, UncountableSignatures> { if utxos.is_empty() { - return None; + return Ok(None); } let threshold = redeem_script_threshold(redeem_script); if threshold == 0 { - return None; + return Ok(None); } let gathered: u64 = utxos.iter().map(|u| u.value_satoshis).sum(); @@ -515,29 +626,34 @@ 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 = signing_bytes(utxos.iter().map(|_| redeem_script))?; + 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; + return Ok(None); } let value = gathered - fee; if value < min_non_dust_value(refund_script) { - return None; + return Ok(None); } 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 bitcoin::consensus::serialize(&tx).len() > MAX_STANDARD_TX_SIZE { - return None; + if !fill_inputs(&mut tx, std::iter::repeat(redeem_script), is_segwit) { + return Ok(None); } - Some(BuiltPegout { tx, used_utxos: utxos.to_vec() }) + Ok(Some(BuiltPegout { tx, used_utxos: utxos.to_vec() })) } /// Convert a stored UTXO hash (bitcoinj display/stored byte order) into a @@ -673,7 +789,9 @@ pub fn redeem_script_keys(redeem_script: &[u8]) -> Vec<[u8; 33]> { /// a P2SH input. For an ERP redeem a normal (`OP_NOTIF` default-branch) spend /// uses only the DEFAULT-federation multisig, so the emergency keys after /// `OP_ELSE` must be excluded; otherwise sig insertion indexes against the wrong -/// key set. For a plain (or flyover-plain) redeem this is every 33-byte push. +/// key set. For a plain (or flyover-plain) redeem this is every key push: +/// 33 bytes, or the one byte 00 of the point at infinity, kept as +/// `federation::INFINITY_KEY`. pub fn spending_redeem_keys(redeem_script: &[u8]) -> Vec<[u8; 33]> { let Some(chunks) = parse_chunks(redeem_script) else { return Vec::new(); @@ -682,8 +800,8 @@ pub fn spending_redeem_keys(redeem_script: &[u8]) -> Vec<[u8; 33]> { for c in &chunks { match c { Chunk::Op(0x67) => break, // OP_ELSE: emergency keys follow - Chunk::Data(d) if d.len() == 33 => { - if let Ok(k) = d.as_slice().try_into() { + Chunk::Data(d) if d.len() == 33 || d.as_slice() == [0x00] => { + if let Some(k) = super::federation::member_key_from_encoding(d) { out.push(k); } } @@ -762,53 +880,68 @@ pub fn update_script_with_signature( /// bitcoinj `Script.getSigInsertionIndex`: position for this federator's /// signature among the existing ones, ordered by key index in the redeem -/// script. +/// script. `None` where bitcoinj throws looking an existing signature up +/// ([`find_sig_key_index`]). pub fn sig_insertion_index( script_sig: &[u8], sighash: &[u8; 32], signing_key: &[u8; 33], redeem_script: &[u8], -) -> usize { +) -> Option { let keys = spending_redeem_keys(redeem_script); let my_index = keys .iter() .position(|k| k == signing_key) .unwrap_or(usize::MAX); let Some(chunks) = parse_chunks(script_sig) else { - return 0; + return Some(0); }; if chunks.len() < 2 { - return 0; + return Some(0); } // Skip the trailing redeem (and the ERP OP_NOTIF flag) when scanning sigs. let suffix = if input_redeem_is_erp(&chunks) { 2 } else { 1 }; if chunks.len() < 1 + suffix { - return 0; + return Some(0); } let mut sig_index = 0; for chunk in &chunks[1..chunks.len() - suffix] { if let Chunk::Data(sig) = chunk { - let sig_key_index = find_sig_key_index(sig, sighash, &keys); + let sig_key_index = find_sig_key_index(sig, sighash, &keys)?; if my_index < sig_key_index { - return sig_index; + return Some(sig_index); } sig_index += 1; } } - sig_index + Some(sig_index) } /// bitcoinj `Script.findSigInRedeem`: index of the redeem-script key that -/// verifies this encoded signature (DER + sighash byte) for `sighash`. -fn find_sig_key_index(sig_with_hashtype: &[u8], sighash: &[u8; 32], keys: &[[u8; 33]]) -> usize { +/// verifies this encoded signature (DER + sighash byte) for `sighash`, the +/// keys tried in order. `None` where it meets the point at infinity before a +/// key that verifies: `BtcECKey.verify` throws for it (Bouncy Castle's +/// `ECPublicKeyParameters` refuses it), an `IllegalArgumentException` that +/// nothing on the way to the Bridge catches, so the call fails. +fn find_sig_key_index( + sig_with_hashtype: &[u8], + sighash: &[u8; 32], + keys: &[[u8; 33]], +) -> Option { if sig_with_hashtype.len() < 2 { - return keys.len(); + return Some(keys.len()); } let der = &sig_with_hashtype[..sig_with_hashtype.len() - 1]; - keys.iter() - .position(|k| verify_der_signature(der, sighash, k)) - .unwrap_or(keys.len()) + for (index, key) in keys.iter().enumerate() { + if *key == super::federation::INFINITY_KEY { + return None; + } + if verify_der_signature(der, sighash, key) { + return Some(index); + } + } + Some(keys.len()) } /// Verify a DER-encoded ECDSA signature over a 32-byte message hash against a @@ -830,6 +963,41 @@ pub fn verify_der_signature(der: &[u8], msg32: &[u8; 32], pubkey: &[u8; 33]) -> key.verify_prehash(msg32, &sig).is_ok() } +/// What rskj makes of the signature a federator sends for an input, in +/// `BridgeSupport.processSigning`: `getTransactionSignatures` reads it as +/// `decodeFromDER` does, and throws its `SignatureException`, so that +/// nothing is signed, unless Bouncy Castle's `ECDSASigner` verifies it +/// against the input's sighash (r and s from 1 to n - 1, s in either half of +/// the order) and its s is in the lower half (`isCanonical`). What `sign` +/// then puts in the input is `TransactionSignature.encodeToBitcoin`: the DER +/// bitcoinj writes of r and s, which need not be the bytes the federator +/// sent, and the SIGHASH_ALL byte. `None` where rskj throws. +pub fn signature_to_insert(sig: &[u8], msg32: &[u8; 32], pubkey: &[u8; 33]) -> Option> { + use k256::ecdsa::signature::hazmat::PrehashVerifier; + use k256::ecdsa::{Signature, VerifyingKey}; + + let (r, s) = super::der_signature::der_signature_integers(sig)?; + let scalar = |bytes: &[u8]| -> Option<[u8; 32]> { + let start = bytes.iter().position(|&b| b != 0).unwrap_or(bytes.len()); + let digits = &bytes[start..]; + if digits.len() > 32 { + return None; + } + let mut field = [0u8; 32]; + field[32 - digits.len()..].copy_from_slice(digits); + Some(field) + }; + let signature = Signature::from_scalars(scalar(r)?, scalar(s)?).ok()?; + if signature.normalize_s().is_some() { + return None; + } + let key = VerifyingKey::from_sec1_bytes(pubkey).ok()?; + key.verify_prehash(msg32, &signature).ok()?; + let mut encoded = signature.to_der().as_bytes().to_vec(); + encoded.push(0x01); + Some(encoded) +} + /// Legacy (pre-segwit) BIP-143-less signature hash with the redeem script as /// the script code, SIGHASH_ALL. pub fn legacy_sighash_all(tx: &BtcTransaction, input_index: usize, redeem_script: &[u8]) -> [u8; 32] { @@ -962,31 +1130,32 @@ pub fn witness_update_with_signature( Some(result) } -/// Witness equivalent of `sig_insertion_index`. +/// Witness equivalent of `sig_insertion_index` (`TransactionWitness +/// .getSigInsertionIndex`), `None` where bitcoinj throws alike. pub fn witness_sig_insertion_index( pushes: &[Vec], sighash: &[u8; 32], signing_key: &[u8; 33], redeem_script: &[u8], -) -> usize { +) -> Option { let keys = spending_redeem_keys(redeem_script); let my_index = keys.iter().position(|k| k == signing_key).unwrap_or(usize::MAX); let total = pushes.len(); let suffix = if witness_redeem_is_erp(pushes) { 2 } else { 1 }; if total < 1 + suffix { - return 0; + return Some(0); } let mut sig_index = 0; for item in &pushes[1..total - suffix] { if !item.is_empty() { - let sig_key_index = find_sig_key_index(item, sighash, &keys); + let sig_key_index = find_sig_key_index(item, sighash, &keys)?; if my_index < sig_key_index { - return sig_index; + return Some(sig_index); } sig_index += 1; } } - sig_index + Some(sig_index) } /// Witness equivalent of `input_signed_by`. @@ -1222,6 +1391,7 @@ mod tests { 1, false, ) + .unwrap() .expect("build"); // Selection order: hash 01 vout 0, hash 01 vout 1 (covers 100k > 60k). assert_eq!(built.used_utxos.len(), 2); @@ -1254,6 +1424,7 @@ mod tests { 2, segwit, ) + .unwrap() .expect("build") }; let seg = build(true); @@ -1285,6 +1456,7 @@ mod tests { 1, false, ) + .unwrap() .expect("build"); let tx = &built.tx; assert_eq!(tx.output.len(), 2); @@ -1332,6 +1504,7 @@ mod tests { 2, false, ) + .unwrap() .expect("build"); let tx = &built.tx; assert_eq!(tx.version.0, 2); @@ -1372,6 +1545,7 @@ mod tests { complete_recipients_dont_pay_fees_tx( &utxos, &outputs, &p2sh(8), |_| redeem.as_slice(), 5000, 2, seg, ) + .unwrap() .expect("build") }; let legacy = build(false); @@ -1402,6 +1576,7 @@ mod tests { 1, false, ) + .unwrap() .is_none()); } @@ -1418,9 +1593,69 @@ mod tests { 1, false, ) + .unwrap() .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) + .unwrap() + }; + 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, + ) + .unwrap() + }; + 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).unwrap() + }; + 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 @@ -1437,6 +1672,7 @@ mod tests { 1, false, ) + .unwrap() .expect("build"); let tx = &built.tx; assert_eq!(tx.output.len(), 2); @@ -1478,6 +1714,7 @@ mod tests { 1, false, ) + .unwrap() .expect("build"); let tx = &built.tx; assert_eq!(tx.input.len(), 2); @@ -1507,6 +1744,50 @@ mod tests { assert_eq!(fee, 5000 * size as u64 / 1000); } + /// Testnet #2,237,507: the federation committed at #2,235,076 is a + /// non-standard ERP one, and before RSKIP284 rskj gives it testnet's + /// hard-coded redeem script. bitcoinj-thin counts no signatures for that + /// script and throws while it sizes the federation's input, so rskj's + /// updateCollections threw where rustock built the peg-out. Inside a + /// Flyover redeem script the same script counts its threshold, and a build + /// refused before the inputs are sized is still refused. + #[test] + fn an_input_spent_with_the_hardcoded_testnet_erp_script_cannot_be_sized() { + let hardcoded = crate::bridge::peg::legacy_erp_testnet_redeem_script(); + assert_eq!(signatures_required(hardcoded), Err(UncountableSignatures)); + let mut flyover = vec![32u8]; + flyover.extend_from_slice(&[7u8; 32]); + flyover.push(0x75); + flyover.extend_from_slice(hardcoded); + assert_eq!(signatures_required(&flyover), Ok(3)); + + // The block's request: 500,000 satoshis from the federation's one + // UTXO, at 100,000 satoshis per kB. + let utxos = vec![utxo(1, 0, 92_345_185_965)]; + let to = |amount_satoshis| [PegoutOutput { script: p2pkh(7), amount_satoshis }]; + let pegout = |redeem: &[u8], amount| { + complete_pegout_tx(&utxos, &to(amount), &p2sh(8), |_| redeem, 100_000, 2, false) + }; + assert_eq!(pegout(hardcoded, 500_000).err(), Some(UncountableSignatures)); + assert!(pegout(&flyover, 500_000).expect("sized").is_some()); + // Refused before it is sized: more than the federation holds, and dust. + assert!(pegout(hardcoded, 100_000_000_000).expect("refused").is_none()); + assert!(pegout(hardcoded, 2_729).expect("refused").is_none()); + + let fund = complete_recipients_dont_pay_fees_tx( + &utxos, + &to(500_000), + &p2sh(8), + |_| hardcoded, + 100_000, + 2, + false, + ); + assert_eq!(fund.err(), Some(UncountableSignatures)); + let refund = build_empty_wallet_to(&utxos, &p2pkh(7), hardcoded, 100_000, 2, false); + assert_eq!(refund.err(), Some(UncountableSignatures)); + } + #[test] fn update_script_inserts_and_consumes_placeholder() { let redeem = test_redeem(2, 3); @@ -1584,7 +1865,9 @@ mod tests { &redeem, 500_000, 1, + false, ) + .unwrap() .expect("rejection release builds"); let mut rid = *built.tx.compute_txid().to_raw_hash().as_byte_array(); rid.reverse(); @@ -1665,7 +1948,9 @@ mod tests { &redeem, 500_000, // mainnet genesis feePerKb 1, // pre-RSKIP201 + false, ) + .unwrap() .expect("rejection release builds"); // Fee: 500000 sat/kB over 1198 bytes (598 serialized + 8*75+598 @@ -1756,7 +2041,8 @@ mod tests { .as_bytes() .try_into() .unwrap(); - let idx = sig_insertion_index(tx.input[0].script_sig.as_bytes(), &sighash, &key1, &redeem); + let idx = sig_insertion_index(tx.input[0].script_sig.as_bytes(), &sighash, &key1, &redeem) + .expect("no point at infinity"); assert_eq!(idx, 0); let updated = update_script_with_signature(tx.input[0].script_sig.as_bytes(), &sign(first_sk), idx) @@ -1773,7 +2059,8 @@ mod tests { .try_into() .unwrap(); assert!(!input_signed_by(&tx, 0, &key0, &sighash)); - let idx0 = sig_insertion_index(tx.input[0].script_sig.as_bytes(), &sighash, &key0, &redeem); + let idx0 = sig_insertion_index(tx.input[0].script_sig.as_bytes(), &sighash, &key0, &redeem) + .expect("no point at infinity"); assert_eq!(idx0, 0); let updated0 = update_script_with_signature(tx.input[0].script_sig.as_bytes(), &sign(second_sk), idx0) @@ -1791,6 +2078,58 @@ mod tests { assert!(verify_der_signature(&s1[..s1.len() - 1], &sighash, &key1)); } + /// bitcoinj's `findSigInRedeem` tries a redeem script's keys in order, + /// verifying the signature with each, and `BtcECKey.verify` throws for + /// the point at infinity, the one byte 00, which sorts first: looking up + /// a signature an input holds fails, in its scriptSig and in its witness + /// alike. With none held nothing is looked up, and the signer's own index + /// is found among keys that count the point at infinity. + #[test] + fn looking_up_a_held_signature_meets_the_point_at_infinity_first() { + use k256::ecdsa::signature::hazmat::PrehashSigner; + use k256::ecdsa::{Signature, SigningKey}; + + let signer = SigningKey::from_bytes(&[1u8; 32].into()).unwrap(); + let key: [u8; 33] = signer + .verifying_key() + .to_encoded_point(true) + .as_bytes() + .try_into() + .unwrap(); + // One of the point at infinity and the key. + let redeem = [&[0x51, 0x01, 0x00, 0x21][..], &key, &[0x52, 0xae]].concat(); + assert_eq!( + spending_redeem_keys(&redeem), + vec![super::super::federation::INFINITY_KEY, key] + ); + let sighash = [0x11; 32]; + let signature: Signature = signer.sign_prehash(&sighash).unwrap(); + let held = [signature.to_der().as_bytes(), &[0x01]].concat(); + let script_sig = |slot: &[u8]| { + let mut script = vec![0x00]; + push_data(&mut script, slot); + push_data(&mut script, &redeem); + script + }; + assert_eq!( + sig_insertion_index(&script_sig(&[]), &sighash, &key, &redeem), + Some(0) + ); + assert_eq!( + sig_insertion_index(&script_sig(&held), &sighash, &key, &redeem), + None + ); + let witness = |slot: &[u8]| vec![Vec::new(), slot.to_vec(), redeem.clone()]; + assert_eq!( + witness_sig_insertion_index(&witness(&[]), &sighash, &key, &redeem), + Some(0) + ); + assert_eq!( + witness_sig_insertion_index(&witness(&held), &sighash, &key, &redeem), + None + ); + } + /// `parse_chunks` is the script parser the peg-in sender classifier runs on /// attacker-supplied scriptSigs, so its malformed-input behaviour is a /// consensus surface: a panic here halts block execution, and a chunk list diff --git a/crates/execution/src/bridge/rskj_sender_compat.rs b/crates/execution/src/bridge/rskj_sender_compat.rs index f5032ad..62af987 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,11 @@ 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_EQUAL: u8 = 0x87; +const OP_EQUALVERIFY: u8 = 0x88; +const OP_HASH160: u8 = 0xa9; +const OP_CHECKSIG: u8 = 0xac; /// `RedeemScriptParserFactory.NON_STANDARD_ERP_TESTNET_REDEEM_SCRIPT_SERIALIZED`. /// @@ -102,7 +115,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), } @@ -117,9 +132,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. @@ -300,6 +334,25 @@ 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() +} + +/// 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 { @@ -348,8 +401,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) } @@ -358,44 +414,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 @@ -409,7 +482,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 ---------------------------------------------------------- @@ -418,10 +496,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 @@ -434,9 +512,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; } } @@ -451,26 +533,250 @@ 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, + } +} + +// --- the standard part of a redeem script ----------------------------------- + +/// A script's program as bitcoinj writes it from its chunks, which it does +/// only when the program is asked for: `Err` is the `IllegalStateException` +/// `ScriptChunk.write` throws then for an `OP_PUSHDATA4` push of more than 520 +/// bytes, which nothing between it and `Bridge.execute` catches. +pub type Written = Result, &'static str>; + +/// `RedeemScriptParserFactory.get(chunks).extractStandardRedeemScriptChunks()`, +/// built back into a script (`new ScriptBuilder().addChunks(..).build()`), as +/// rskj's `PegUtilsLegacy` reads an input's redeem script from RSKIP201 and an +/// ERP federation's default one (`ErpFederation.getDefaultRedeemScript`): a +/// standard multisig as it is, the default multisig of a P2SH ERP script, that +/// of a non-standard ERP script with the `OP_CHECKMULTISIG` the two branches +/// share appended, and whatever a Flyover prefix wraps. Each chunk is written +/// back as it was read, so the script is the bytes of the chunks it keeps. +/// +/// `Ok(None)` is the `ScriptException` rskj catches: a script bitcoinj can't +/// parse, one of fewer than four chunks, one no parser knows, and the +/// hard-coded testnet script, whose parser has no standard part. `Err` is the +/// index error a validator raises, which nothing catches. +pub fn standard_redeem_script(redeem_script: &[u8]) -> Result, &'static str> { + let Some(chunks) = parse_script_program(redeem_script) else { + return Ok(None); + }; + Ok(standard_chunks(&chunks, 0)?.map(|chunks| program_of(&chunks))) +} + +/// The parser `dispatch` picks, asked for its standard chunks. +fn standard_chunks(chunks: &[JChunk], depth: usize) -> Result>, &'static str> { + if depth > MAX_FLYOVER_DEPTH { + return Ok(None); + } + 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 Ok(None); + } + } + if chunks.len() < 4 { + return Ok(None); + } + if has_flyover_structure(chunks) { + return standard_chunks(&chunks[2..], depth + 1); + } + match has_standard_structure(chunks) { + Err(()) => return Err("ArrayIndexOutOfBoundsException decoding M or N from an empty push"), + Ok(true) => return Ok(Some(chunks.to_vec())), + Ok(false) => {} + } + // An ERP script's default multisig runs from after its OP_NOTIF to its + // first OP_ELSE (`extractDefaultRedeemScriptChunks`), where the validators + // found it. + let default_multisig = || { + let else_index = (1..chunks.len()) + .find(|&i| chunks[i].equals_op_code(OP_ELSE)) + .unwrap_or(chunks.len()); + chunks[1..else_index].to_vec() + }; + if has_p2sh_erp_structure(chunks)? { + return Ok(Some(default_multisig())); + } + if has_non_standard_erp_structure(chunks)? { + let mut default = default_multisig(); + default.push(JChunk { + opcode: OP_CHECKMULTISIG, + data: None, + }); + return Ok(Some(default)); + } + Ok(None) +} + +/// `new ScriptBuilder().addChunks(script.getChunks()).build()`, the script +/// bitcoinj's `PegUtilsLegacy.isPegOutTx` builds of an input's redeem script +/// before RSKIP201: `None` for one bitcoinj can't parse. +pub fn written_back(script: &[u8]) -> Option { + parse_script_program(script).map(|chunks| program_of(&chunks)) +} + +/// `Script.getProgram()` of a script built from `chunks`. +fn program_of(chunks: &[JChunk]) -> Written { + let mut program = Vec::new(); + for chunk in chunks { + program.extend(write_chunk(chunk)?); + } + Ok(program) +} + +/// `ScriptChunk.write`: the opcode, then for a push its length as the opcode +/// writes it and its data. An `OP_PUSHDATA4` push of more than 520 bytes +/// fails its `checkState`. +fn write_chunk(chunk: &JChunk) -> Written { + let mut out = vec![chunk.opcode]; + if let (false, Some(data)) = (chunk.is_op_code(), &chunk.data) { + match chunk.opcode { + OP_PUSHDATA1 => out.push(data.len() as u8), + OP_PUSHDATA2 => out.extend_from_slice(&(data.len() as u16).to_le_bytes()), + OP_PUSHDATA4 if data.len() > 520 => { + return Err( + "IllegalStateException writing an OP_PUSHDATA4 push of more than 520 bytes", + ) + } + OP_PUSHDATA4 => out.extend_from_slice(&(data.len() as u32).to_le_bytes()), + _ => {} + } + out.extend_from_slice(data); + } + Ok(out) +} + +// --- 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) +} + +/// `Script.getPubKeyHash()` of an output's script, which +/// `TransactionOutput.getScriptPubKey()` parses first: the hash a P2PKH script +/// (`isSentToAddress`) pays, or the one a P2SH script (`isPayToScriptHash`, +/// which reads the bytes, not the chunks) does. `Err` is what bitcoinj throws +/// for every other script: the `ScriptException` for one it can't parse or +/// that is neither, and the `NullPointerException` for an opcode in a +/// P2PKH's hash's place, which [`is_sent_to_address`] answers as neither. +pub fn pub_key_hash(program: &[u8]) -> Result<[u8; 20], &'static str> { + let chunks = parse_script_program(program).ok_or("ScriptException parsing an output's script")?; + if is_sent_to_address(&chunks) { + if let Some(Ok(hash)) = chunks[2].data.as_deref().map(<[u8; 20]>::try_from) { + return Ok(hash); + } + } + if program.len() == 23 && program[0] == OP_HASH160 && program[1] == 0x14 && program[22] == OP_EQUAL { + return Ok(program[2..22].try_into().expect("twenty bytes")); + } + Err("ScriptException: an output's script is not in the standard scriptPubKey form") +} + fn hex_decode(s: &str) -> Vec { (0..s.len()) .step_by(2) @@ -499,6 +805,70 @@ 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 + } + + /// `Script.getPubKeyHash()`: a P2PKH script's hash, however its push is + /// written, and a P2SH script's, by its exact bytes; for anything else + /// bitcoinj throws. + #[test] + fn pub_key_hash_reads_only_p2pkh_and_p2sh() { + let hash = [0x77u8; 20]; + let p2pkh = [&[OP_DUP, OP_HASH160, 20][..], &hash, &[OP_EQUALVERIFY, OP_CHECKSIG]].concat(); + assert_eq!(pub_key_hash(&p2pkh), Ok(hash)); + // bitcoinj takes a hash pushed by OP_PUSHDATA1 as one pushed directly. + let pushdata = [&[OP_DUP, OP_HASH160, OP_PUSHDATA1, 20][..], &hash, &[OP_EQUALVERIFY, OP_CHECKSIG]].concat(); + assert_eq!(pub_key_hash(&pushdata), Ok(hash)); + let p2sh = [&[OP_HASH160, 20][..], &hash, &[OP_EQUAL]].concat(); + assert_eq!(pub_key_hash(&p2sh), Ok(hash)); + for other in [ + // P2WPKH and P2WSH. + [&[0x00, 20][..], &hash].concat(), + [&[0x00, 32][..], &[0x77; 32]].concat(), + // OP_RETURN. + vec![0x6a, 0x02, 0xbe, 0xef], + // P2SH's template with its hash pushed by OP_PUSHDATA1. + [&[OP_HASH160, OP_PUSHDATA1, 20][..], &hash, &[OP_EQUAL]].concat(), + // An opcode where a P2PKH's hash goes: Java's NullPointerException. + vec![OP_DUP, OP_HASH160, OP_DUP, OP_EQUALVERIFY, OP_CHECKSIG], + // A push past the end, which bitcoinj can't parse. + vec![OP_HASH160, 20, 0x77], + vec![], + ] { + assert!(pub_key_hash(&other).is_err(), "{other:02x?}"); + } + } + #[test] fn standard_multisig_is_multisig() { assert_eq!(is_sent_to_multisig(&standard_redeem(2, 3)), Verdict::Multisig); @@ -573,6 +943,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] @@ -652,6 +1129,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 { @@ -659,18 +1152,60 @@ 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] fn the_switch_defaults_to_on() { assert!(enabled(), "rskj has not changed, so emulation must be on by default"); } + + /// bitcoinj builds a script back from the chunks it read, and + /// `ScriptChunk.write` refuses an `OP_PUSHDATA4` push of more than 520 + /// bytes, which its parser reads. A push of 520 is written back as it was. + #[test] + fn a_pushdata4_of_more_than_520_bytes_is_not_written_back() { + let multisig_of = |len: usize| { + [ + &[0x51, OP_PUSHDATA4][..], + &(len as u32).to_le_bytes(), + &vec![0x02; len], + &[0x51, OP_CHECKMULTISIG], + ] + .concat() + }; + let fits = multisig_of(520); + assert_eq!(written_back(&fits), Some(Ok(fits.clone()))); + assert_eq!(standard_redeem_script(&fits), Ok(Some(Ok(fits.clone())))); + let too_long = multisig_of(521); + assert!(matches!(written_back(&too_long), Some(Err(_)))); + assert!(matches!( + standard_redeem_script(&too_long), + Ok(Some(Err(_))) + )); + } } diff --git a/crates/execution/src/bridge/script_spend.rs b/crates/execution/src/bridge/script_spend.rs new file mode 100644 index 0000000..53b1c1e --- /dev/null +++ b/crates/execution/src/bridge/script_spend.rs @@ -0,0 +1,867 @@ +//! bitcoinj-thin's `Script.correctlySpends`, as rskj's +//! `PegUtilsLegacy.scriptCorrectlySpendsTx` runs it: whether an input of a +//! Bitcoin transaction spends a script, its signatures checked. +//! +//! rskj asks it of each input of a transaction it reads by the legacy rules: +//! whether it spends the old federation (`txIsFromOldFederation`, from +//! RSKIP199), a live federation or the last retired one (`isValidPegInTx`). +//! Each script asked about is a P2SH script, and the redeem script an input +//! pushes must hash to it, so what runs is a scriptSig, which holds pushes +//! and small numbers only, the P2SH script, and a federation's redeem script: +//! a multisig, an ERP script, or a P2SH-P2WSH federation's witness program, +//! run without its witness. This runs the opcodes those hold as bitcoinj runs +//! them, with every flag bitcoinj has (`Script.ALL_VERIFY_FLAGS`). bitcoinj +//! runs the rest of Bitcoin's opcodes too; none is in a script asked about, +//! and here any of them fails the script. + +use std::collections::VecDeque; + +use bitcoin::{Amount, ScriptBuf, Transaction, TxOut}; +use sha2::Digest as _; + +const OP_0: u8 = 0x00; +const OP_PUSHDATA1: u8 = 0x4c; +const OP_PUSHDATA2: u8 = 0x4d; +const OP_PUSHDATA4: u8 = 0x4e; +const OP_1NEGATE: u8 = 0x4f; +const OP_1: u8 = 0x51; +const OP_16: u8 = 0x60; +const OP_NOP: u8 = 0x61; +const OP_IF: u8 = 0x63; +const OP_NOTIF: u8 = 0x64; +const OP_VERIF: u8 = 0x65; +const OP_VERNOTIF: u8 = 0x66; +const OP_ELSE: u8 = 0x67; +const OP_ENDIF: u8 = 0x68; +const OP_VERIFY: u8 = 0x69; +const OP_DROP: u8 = 0x75; +const OP_DUP: u8 = 0x76; +const OP_EQUAL: u8 = 0x87; +const OP_EQUALVERIFY: u8 = 0x88; +const OP_HASH160: u8 = 0xa9; +const OP_CODESEPARATOR: u8 = 0xab; +const OP_CHECKMULTISIG: u8 = 0xae; +const OP_CHECKMULTISIGVERIFY: u8 = 0xaf; +const OP_CHECKSEQUENCEVERIFY: u8 = 0xb2; + +/// The opcodes bitcoinj refuses wherever they stand, run or not: OP_CAT to +/// OP_RIGHT, OP_INVERT to OP_XOR, OP_2MUL, OP_2DIV and OP_MUL to OP_RSHIFT. +const DISABLED: [u8; 15] = [ + 0x7e, 0x7f, 0x80, 0x81, 0x83, 0x84, 0x85, 0x86, 0x8d, 0x8e, 0x95, 0x96, 0x97, 0x98, 0x99, +]; + +/// `BtcTransaction.SEQUENCE_LOCKTIME_DISABLE_FLAG`. +const SEQUENCE_LOCKTIME_DISABLE_FLAG: i64 = 1 << 31; +/// `BtcTransaction.SEQUENCE_LOCKTIME_TYPE_FLAG`. +const SEQUENCE_LOCKTIME_TYPE_FLAG: i64 = 1 << 22; +/// `BtcTransaction.SEQUENCE_LOCKTIME_MASK`. +const SEQUENCE_LOCKTIME_MASK: i64 = 0xffff; + +/// bitcoinj's `ScriptException`: the script fails. +struct Failed; + +/// A chunk of a script as bitcoinj's `ScriptParser` reads it. +struct Chunk { + opcode: u8, + /// What a push pushes; nothing for any other opcode. + data: Option>, + /// Where the chunk starts in its program. + start: usize, +} + +impl Chunk { + /// `ScriptChunk.isOpCode`: anything but a push, so `OP_1NEGATE` and the + /// small numbers too. + fn is_op_code(&self) -> bool { + self.opcode > OP_PUSHDATA4 + } +} + +/// `ScriptParser.parseScriptProgram`; `None` is its `ScriptException`, a push +/// that runs past the end. +fn parse(program: &[u8]) -> Option> { + let mut chunks = Vec::new(); + let mut pos = 0; + while pos < program.len() { + let start = pos; + let opcode = program[pos]; + pos += 1; + let len = match opcode { + o if o < OP_PUSHDATA1 => Some(o as usize), + OP_PUSHDATA1 => { + let n = *program.get(pos)?; + pos += 1; + Some(n as usize) + } + OP_PUSHDATA2 => { + let n = program.get(pos..pos + 2)?; + pos += 2; + Some(u16::from_le_bytes([n[0], n[1]]) as usize) + } + OP_PUSHDATA4 => { + let n = program.get(pos..pos + 4)?; + pos += 4; + Some(u32::from_le_bytes([n[0], n[1], n[2], n[3]]) as usize) + } + _ => None, + }; + let data = match len { + None => None, + Some(n) if n > program.len() - pos => return None, + Some(n) => { + pos += n; + Some(program[pos - n..pos].to_vec()) + } + }; + chunks.push(Chunk { + opcode, + data, + start, + }); + } + Some(chunks) +} + +/// rskj `PegUtilsLegacy.scriptCorrectlySpendsTx`: whether input `index` of +/// `tx` spends `script_pubkey`. A scriptSig bitcoinj can't parse, or that +/// holds an opcode beyond the small numbers, spends nothing, and so does one +/// that fails as bitcoinj runs it. +pub(crate) fn script_correctly_spends_tx( + tx: &Transaction, + index: usize, + script_pubkey: &[u8], +) -> bool { + let script_sig = tx.input[index].script_sig.as_bytes(); + let Some(sig_chunks) = parse(script_sig) else { + return false; + }; + if sig_chunks + .iter() + .any(|c| c.is_op_code() && c.opcode > OP_16) + { + return false; + } + correctly_spends(tx, index, script_sig, &sig_chunks, script_pubkey).is_ok() +} + +/// `Script.correctlySpends` with every flag: the scriptSig run, then the +/// script it spends on what the scriptSig left, and for a P2SH script the +/// redeem script the scriptSig pushed last, on what the scriptSig left beneath +/// it. Each must leave a true value on top. +fn correctly_spends( + tx: &Transaction, + index: usize, + script_sig: &[u8], + sig_chunks: &[Chunk], + script_pubkey: &[u8], +) -> Result<(), Failed> { + if script_sig.len() > 10_000 || script_pubkey.len() > 10_000 { + return Err(Failed); + } + let pubkey_chunks = parse(script_pubkey).ok_or(Failed)?; + let mut stack = Vec::new(); + execute(tx, index, script_sig, sig_chunks, &mut stack)?; + let mut p2sh_stack = stack.clone(); + execute(tx, index, script_pubkey, &pubkey_chunks, &mut stack)?; + if !cast_to_bool(&stack.pop().ok_or(Failed)?) { + return Err(Failed); + } + if is_pay_to_script_hash(script_pubkey) { + if sig_chunks + .iter() + .any(|c| c.is_op_code() && c.opcode > OP_16) + { + return Err(Failed); + } + let redeem = p2sh_stack.pop().ok_or(Failed)?; + let redeem_chunks = parse(&redeem).ok_or(Failed)?; + execute(tx, index, &redeem, &redeem_chunks, &mut p2sh_stack)?; + if !cast_to_bool(&p2sh_stack.pop().ok_or(Failed)?) { + return Err(Failed); + } + } + Ok(()) +} + +/// `Script.isPayToScriptHash`: the exact bytes of `OP_HASH160 <20> OP_EQUAL`. +fn is_pay_to_script_hash(script: &[u8]) -> bool { + script.len() == 23 && script[0] == OP_HASH160 && script[1] == 0x14 && script[22] == OP_EQUAL +} + +/// `Script.executeScript` with every flag. +fn execute( + tx: &Transaction, + index: usize, + program: &[u8], + chunks: &[Chunk], + stack: &mut Vec>, +) -> Result<(), Failed> { + let mut op_count = 0usize; + let mut last_code_separator = 0usize; + let mut if_stack: Vec = Vec::new(); + for chunk in chunks { + let should_execute = !if_stack.contains(&false); + if chunk.opcode == OP_0 { + if should_execute { + stack.push(Vec::new()); + } + } else if !chunk.is_op_code() { + let data = chunk.data.as_deref().unwrap_or_default(); + if data.len() > 520 { + return Err(Failed); + } + if should_execute { + stack.push(data.to_vec()); + } + } else { + let opcode = chunk.opcode; + if opcode > OP_16 { + op_count += 1; + if op_count > 201 { + return Err(Failed); + } + } + if opcode == OP_VERIF || opcode == OP_VERNOTIF || DISABLED.contains(&opcode) { + return Err(Failed); + } + match opcode { + OP_IF | OP_NOTIF => { + if should_execute { + let top = stack.pop().ok_or(Failed)?; + if_stack.push(cast_to_bool(&top) == (opcode == OP_IF)); + } else { + if_stack.push(false); + } + continue; + } + OP_ELSE => { + let branch = if_stack.pop().ok_or(Failed)?; + if_stack.push(!branch); + continue; + } + OP_ENDIF => { + if_stack.pop().ok_or(Failed)?; + continue; + } + _ => {} + } + if !should_execute { + continue; + } + match opcode { + OP_1NEGATE => stack.push(vec![0x81]), + OP_1..=OP_16 => stack.push(vec![opcode - OP_1 + 1]), + OP_NOP => {} + OP_VERIFY => { + if !cast_to_bool(&stack.pop().ok_or(Failed)?) { + return Err(Failed); + } + } + OP_DROP => { + stack.pop().ok_or(Failed)?; + } + OP_DUP => { + let top = stack.last().ok_or(Failed)?.clone(); + stack.push(top); + } + OP_EQUAL | OP_EQUALVERIFY => { + let (Some(a), Some(b)) = (stack.pop(), stack.pop()) else { + return Err(Failed); + }; + if opcode == OP_EQUAL { + stack.push(if a == b { vec![1] } else { Vec::new() }); + } else if a != b { + return Err(Failed); + } + } + OP_HASH160 => { + let top = stack.pop().ok_or(Failed)?; + stack.push(hash160(&top).to_vec()); + } + OP_CODESEPARATOR => last_code_separator = chunk.start + 1, + OP_CHECKMULTISIG | OP_CHECKMULTISIGVERIFY => { + let connected = &program[last_code_separator.min(program.len())..]; + op_count = check_multisig(tx, index, connected, opcode, stack, op_count)?; + } + OP_CHECKSEQUENCEVERIFY => check_sequence_verify(tx, index, stack)?, + _ => return Err(Failed), + } + } + if stack.len() > 1000 { + return Err(Failed); + } + } + if !if_stack.is_empty() { + return Err(Failed); + } + Ok(()) +} + +/// `Script.castToBool`: false for nothing but zeros, or zeros ending in the +/// negative zero 0x80. +fn cast_to_bool(data: &[u8]) -> bool { + match data.iter().position(|b| *b != 0) { + None => false, + Some(i) => !(i == data.len() - 1 && data[i] == 0x80), + } +} + +/// `Utils.decodeMPI(Utils.reverseBytes(data), false)`: a number written +/// little-endian, the top bit of its last byte its sign. +fn script_number(data: &[u8]) -> i64 { + let Some((last, rest)) = data.split_last() else { + return 0; + }; + let magnitude = rest + .iter() + .chain(std::iter::once(&(last & 0x7f))) + .enumerate() + .fold(0i64, |n, (i, b)| n | (i64::from(*b) << (8 * i))); + if last & 0x80 != 0 { + -magnitude + } else { + magnitude + } +} + +/// `castToBigInteger(data).intValue()`: a number of at most four bytes. +fn small_number(data: &[u8]) -> Result { + if data.len() > 4 { + return Err(Failed); + } + Ok(script_number(data)) +} + +/// `Script.executeMultiSig`: the key count, the keys, the signature count and +/// the signatures popped, each signature taken out of the script it is checked +/// against, and the signatures matched to the keys in order from the last of +/// each, a key passed over for good once it fails one. Then the extra item +/// `OP_CHECKMULTISIG` pops, which must be empty (NULLDUMMY). +fn check_multisig( + tx: &Transaction, + index: usize, + connected: &[u8], + opcode: u8, + stack: &mut Vec>, + mut op_count: usize, +) -> Result { + if stack.len() < 2 { + return Err(Failed); + } + let key_count = small_number(&stack.pop().ok_or(Failed)?)?; + if !(0..=20).contains(&key_count) { + return Err(Failed); + } + let key_count = key_count as usize; + op_count += key_count; + if op_count > 201 || stack.len() < key_count + 1 { + return Err(Failed); + } + let mut keys: VecDeque> = (0..key_count).filter_map(|_| stack.pop()).collect(); + let sig_count = small_number(&stack.pop().ok_or(Failed)?)?; + if sig_count < 0 || sig_count as usize > key_count { + return Err(Failed); + } + let sig_count = sig_count as usize; + if stack.len() < sig_count + 1 { + return Err(Failed); + } + let mut sigs: VecDeque> = (0..sig_count).filter_map(|_| stack.pop()).collect(); + let connected = sigs.iter().fold(connected.to_vec(), |script, sig| { + remove_all_instances_of(&script, &push_of(sig)) + }); + let mut valid = true; + while let Some(sig) = sigs.front() { + if let Some(key) = keys.pop_front() { + if signature_verifies(tx, index, &connected, sig, &key) { + sigs.pop_front(); + } + } + if sigs.len() > keys.len() { + valid = false; + break; + } + } + let dummy = stack.pop().ok_or(Failed)?; + if !dummy.is_empty() { + return Err(Failed); + } + if opcode == OP_CHECKMULTISIG { + stack.push(if valid { vec![1] } else { Vec::new() }); + } else if !valid { + return Err(Failed); + } + Ok(op_count) +} + +/// `Script.writeBytes`: `data` as a push. +fn push_of(data: &[u8]) -> Vec { + let mut out = Vec::with_capacity(data.len() + 3); + match data.len() { + n if n < OP_PUSHDATA1 as usize => out.push(n as u8), + n if n < 256 => out.extend_from_slice(&[OP_PUSHDATA1, n as u8]), + n => { + out.push(OP_PUSHDATA2); + out.extend_from_slice(&(n as u16).to_le_bytes()); + } + } + out.extend_from_slice(data); + out +} + +/// `Script.removeAllInstancesOf`: `script` without each chunk whose bytes start +/// with `remove`, the chunks walked as bitcoinj walks them. +fn remove_all_instances_of(script: &[u8], remove: &[u8]) -> Vec { + let byte = |i: usize| script.get(i).map_or(0, |b| *b as usize); + let mut out = Vec::with_capacity(script.len()); + let mut cursor = 0; + while cursor < script.len() { + let skip = script[cursor..].starts_with(remove); + let opcode = script[cursor]; + cursor += 1; + let additional = match opcode { + o if o < OP_PUSHDATA1 => o as usize, + OP_PUSHDATA1 => byte(cursor) + 1, + OP_PUSHDATA2 => (byte(cursor) | (byte(cursor + 1) << 8)) + 2, + // bitcoinj reads the length's second byte three times over. + OP_PUSHDATA4 => { + (byte(cursor) + | (byte(cursor + 1) << 8) + | (byte(cursor + 1) << 16) + | (byte(cursor + 1) << 24)) + + 4 + } + _ => 0, + }; + if !skip { + out.push(opcode); + out.extend_from_slice( + &script[cursor.min(script.len())..(cursor + additional).min(script.len())], + ); + } + cursor += additional; + } + out +} + +/// One turn of `executeMultiSig`'s loop: the signature read as +/// `TransactionSignature.decodeFromBitcoin` reads it, its encoding required +/// canonical and its S not required low, the transaction hashed for its +/// sighash byte, and the key checked as Bouncy Castle's `ECDSASigner` checks +/// one. What throws on the way is a signature that doesn't verify. +fn signature_verifies( + tx: &Transaction, + index: usize, + connected: &[u8], + sig: &[u8], + key: &[u8], +) -> bool { + if !is_encoding_canonical(sig) { + return false; + } + let len_r = sig[3] as usize; + let r = &sig[4..4 + len_r]; + let len_s = sig[5 + len_r] as usize; + let s = &sig[6 + len_r..6 + len_r + len_s]; + let hash = signature_hash(tx, index, connected, sig[sig.len() - 1]); + verify(&hash, r, s, key) +} + +/// `TransactionSignature.isEncodingCanonical`: DER's sequence of two +/// integers, neither negative nor padded, then a sighash byte that is ALL, +/// NONE or SINGLE with or without ANYONECANPAY. +fn is_encoding_canonical(sig: &[u8]) -> bool { + if sig.len() < 9 || sig.len() > 73 { + return false; + } + let hash_type = sig[sig.len() - 1] & !0x80; + if !(1..=3).contains(&hash_type) { + return false; + } + if sig[0] != 0x30 || sig[1] as usize != sig.len() - 3 { + return false; + } + let len_r = sig[3] as usize; + if 5 + len_r >= sig.len() || len_r == 0 { + return false; + } + let len_s = sig[5 + len_r] as usize; + if len_r + len_s + 7 != sig.len() || len_s == 0 { + return false; + } + if sig[2] != 0x02 || sig[4] & 0x80 != 0 { + return false; + } + if len_r > 1 && sig[4] == 0 && sig[5] & 0x80 == 0 { + return false; + } + if sig[4 + len_r] != 0x02 || sig[6 + len_r] & 0x80 != 0 { + return false; + } + if len_s > 1 && sig[6 + len_r] == 0 && sig[7 + len_r] & 0x80 == 0 { + return false; + } + true +} + +/// Bouncy Castle's `ECDSASigner.verifySignature` over a 32-byte hash: R and S +/// read unsigned, each from 1 to the order less one, and a high S taken as the +/// low one it mirrors. A key that doesn't decode verifies nothing. +fn verify(hash: &[u8; 32], r: &[u8], s: &[u8], key: &[u8]) -> bool { + use k256::ecdsa::signature::hazmat::PrehashVerifier; + let scalar = |bytes: &[u8]| -> Option<[u8; 32]> { + let start = bytes.iter().position(|b| *b != 0).unwrap_or(bytes.len()); + let digits = &bytes[start..]; + if digits.len() > 32 { + return None; + } + let mut out = [0u8; 32]; + out[32 - digits.len()..].copy_from_slice(digits); + Some(out) + }; + let (Some(r), Some(s)) = (scalar(r), scalar(s)) else { + return false; + }; + let Ok(signature) = k256::ecdsa::Signature::from_scalars(r, s) else { + return false; + }; + let signature = signature.normalize_s().unwrap_or(signature); + let Ok(key) = k256::ecdsa::VerifyingKey::from_sec1_bytes(key) else { + return false; + }; + key.verify_prehash(hash, &signature).is_ok() +} + +/// `BtcTransaction.hashForSignature(index, connectedScript, sigHashType)`: +/// Bitcoin's legacy signature hash, but for the copy's serialization, which +/// bitcoinj writes as it writes every transaction, with its witnesses when it +/// has any. Under ANYONECANPAY the copy keeps the signing input alone and, +/// since bitcoinj keeps a transaction's witnesses by position, the first +/// input's witness. +fn signature_hash(tx: &Transaction, index: usize, connected: &[u8], sighash_type: u8) -> [u8; 32] { + let mut copy = tx.clone(); + for input in &mut copy.input { + input.script_sig = ScriptBuf::new(); + } + copy.input[index].script_sig = + ScriptBuf::from_bytes(remove_all_instances_of(connected, &[OP_CODESEPARATOR])); + let others_sequence_zeroed = |copy: &mut Transaction| { + for (i, input) in copy.input.iter_mut().enumerate() { + if i != index { + input.sequence = bitcoin::Sequence(0); + } + } + }; + match sighash_type & 0x1f { + 2 => { + copy.output.clear(); + others_sequence_zeroed(&mut copy); + } + 3 => { + if index >= copy.output.len() { + let mut one = [0u8; 32]; + one[0] = 1; + return one; + } + copy.output.truncate(index + 1); + for output in &mut copy.output[..index] { + *output = TxOut { + value: Amount::from_sat(u64::MAX), + script_pubkey: ScriptBuf::new(), + }; + } + others_sequence_zeroed(&mut copy); + } + _ => {} + } + if sighash_type & 0x80 != 0 { + let mut input = copy.input[index].clone(); + input.witness = copy.input[0].witness.clone(); + copy.input = vec![input]; + } + let mut bytes = bitcoin::consensus::serialize(©); + bytes.extend_from_slice(&u32::from(sighash_type).to_le_bytes()); + sha2::Sha256::digest(sha2::Sha256::digest(&bytes)).into() +} + +/// `Script.executeCheckSequenceVerify`: the number on top of the stack, of at +/// most five bytes and not negative, against the input's sequence as BIP112 +/// compares them, unless it has the disable flag. +fn check_sequence_verify(tx: &Transaction, index: usize, stack: &[Vec]) -> Result<(), Failed> { + let top = stack.last().ok_or(Failed)?; + if top.len() > 5 { + return Err(Failed); + } + let sequence = script_number(top); + if sequence < 0 { + return Err(Failed); + } + if sequence & SEQUENCE_LOCKTIME_DISABLE_FLAG != 0 { + return Ok(()); + } + let tx_sequence = i64::from(tx.input[index].sequence.0); + if (tx.version.0 as u32) < 2 || tx_sequence & SEQUENCE_LOCKTIME_DISABLE_FLAG != 0 { + return Err(Failed); + } + let mask = SEQUENCE_LOCKTIME_TYPE_FLAG | SEQUENCE_LOCKTIME_MASK; + let (tx_masked, masked) = (tx_sequence & mask, sequence & mask); + if (tx_masked < SEQUENCE_LOCKTIME_TYPE_FLAG) != (masked < SEQUENCE_LOCKTIME_TYPE_FLAG) + || masked > tx_masked + { + return Err(Failed); + } + Ok(()) +} + +/// RIPEMD160(SHA256(data)). +fn hash160(data: &[u8]) -> [u8; 20] { + use ripemd::Digest as _; + ripemd::Ripemd160::digest(sha2::Sha256::digest(data)).into() +} + +#[cfg(test)] +mod tests { + use bitcoin::hashes::Hash as _; + use k256::ecdsa::signature::hazmat::PrehashSigner as _; + use k256::ecdsa::{Signature, SigningKey}; + + use super::*; + + fn key_of(member: &SigningKey) -> [u8; 33] { + member + .verifying_key() + .to_encoded_point(true) + .as_bytes() + .try_into() + .unwrap() + } + + /// The keys of the private keys `[seed; 32]`, in their public keys' order. + fn signers(seeds: std::ops::RangeInclusive) -> Vec { + let mut signers: Vec = seeds + .map(|seed| SigningKey::from_slice(&[seed; 32]).unwrap()) + .collect(); + signers.sort_by_key(key_of); + signers + } + + /// `data` pushed as bitcoinj pushes it. + fn push(data: &[u8]) -> Vec { + let mut out = if data.len() < usize::from(OP_PUSHDATA1) { + vec![data.len() as u8] + } else { + vec![OP_PUSHDATA1, data.len() as u8] + }; + out.extend_from_slice(data); + out + } + + /// `OP_2 OP_3 OP_CHECKMULTISIG`. + fn multisig(members: &[SigningKey]) -> Vec { + let mut script = vec![0x52]; + for member in members { + script.extend(push(&key_of(member))); + } + script.extend([0x53, OP_CHECKMULTISIG]); + script + } + + /// A P2SH ERP script: `members`' multisig, or once the input's sequence + /// counts `delay`, `emergency`'s. + fn p2sh_erp(members: &[SigningKey], emergency: &[SigningKey], delay: &[u8]) -> Vec { + [ + vec![OP_NOTIF], + multisig(members), + vec![OP_ELSE], + push(delay), + vec![OP_CHECKSEQUENCEVERIFY, OP_DROP], + multisig(emergency), + vec![OP_ENDIF], + ] + .concat() + } + + fn p2sh(redeem: &[u8]) -> Vec { + [&[OP_HASH160, 0x14][..], &hash160(redeem), &[OP_EQUAL]].concat() + } + + /// A version 2 transaction whose first input has the sequence `sequence`, + /// followed by `witnessed` inputs with a witness each. + fn transaction(sequence: u32, witnessed: u32) -> Transaction { + let input = |vout: u32, sequence: u32, witness: bitcoin::Witness| bitcoin::TxIn { + previous_output: bitcoin::OutPoint { + txid: bitcoin::Txid::from_byte_array([0x5a; 32]), + vout, + }, + script_sig: ScriptBuf::new(), + sequence: bitcoin::Sequence(sequence), + witness, + }; + let witness = || bitcoin::Witness::from_slice(&[vec![0x42; 72], vec![0x02; 33]]); + Transaction { + version: bitcoin::transaction::Version::TWO, + lock_time: bitcoin::absolute::LockTime::ZERO, + input: std::iter::once(input(0, sequence, bitcoin::Witness::new())) + .chain((1..=witnessed).map(|vout| input(vout, u32::MAX, witness()))) + .collect(), + output: vec![TxOut { + value: Amount::from_sat(90_000), + script_pubkey: ScriptBuf::from_bytes(vec![0x51]), + }], + } + } + + /// Bitcoin's legacy signature hash of the first input for SIGHASH_ALL. + fn bitcoin_sighash(tx: &Transaction, redeem: &[u8]) -> [u8; 32] { + bitcoin::sighash::SighashCache::new(tx) + .legacy_signature_hash(0, bitcoin::Script::from_bytes(redeem), 1) + .unwrap() + .to_byte_array() + } + + /// `hash` signed by `signer`, DER-encoded, with the sighash byte ALL. + fn signature(signer: &SigningKey, hash: &[u8; 32]) -> Vec { + let signature: Signature = signer.sign_prehash(hash).unwrap(); + [signature.to_der().as_bytes(), &[0x01]].concat() + } + + /// Whether the first input of `tx`, its scriptSig `items` and then + /// `redeem` pushed, spends `redeem`'s P2SH script. + fn spends(tx: &Transaction, items: &[&[u8]], redeem: &[u8]) -> bool { + let mut tx = tx.clone(); + let mut script_sig = Vec::new(); + for item in items { + script_sig.extend(if item.is_empty() { + vec![OP_0] + } else { + push(item) + }); + } + script_sig.extend(push(redeem)); + tx.input[0].script_sig = ScriptBuf::from_bytes(script_sig); + script_correctly_spends_tx(&tx, 0, &p2sh(redeem)) + } + + /// A multisig spend that two members sign in their keys' order spends + /// it; signed in the other order, or by a key not a member's, it doesn't. + #[test] + fn two_members_signing_in_their_keys_order_spend_a_multisig() { + let members = signers(1..=3); + let redeem = multisig(&members); + let tx = transaction(u32::MAX, 0); + let hash = bitcoin_sighash(&tx, &redeem); + let (first, third) = (signature(&members[0], &hash), signature(&members[2], &hash)); + assert!(spends(&tx, &[&[], &first, &third], &redeem)); + assert!(!spends(&tx, &[&[], &third, &first], &redeem)); + let outsider = signature(&SigningKey::from_slice(&[9; 32]).unwrap(), &hash); + assert!(!spends(&tx, &[&[], &first, &outsider], &redeem)); + } + + /// bitcoinj checks a multisig's signatures with S high as with S low, and + /// refuses one whose encoding isn't DER's shortest, or whose sighash byte + /// is none of ALL, NONE and SINGLE. + #[test] + fn a_high_s_verifies_and_an_encoding_not_canonical_does_not() { + let members = signers(1..=3); + let redeem = multisig(&members); + let tx = transaction(u32::MAX, 0); + let hash = bitcoin_sighash(&tx, &redeem); + let first = signature(&members[0], &hash); + let low: Signature = members[1].sign_prehash(&hash).unwrap(); + let low = low.normalize_s().unwrap_or(low); + let high = Signature::from_scalars((*low.r()).to_bytes(), (-*low.s()).to_bytes()).unwrap(); + let encoded = |signature: &Signature, sighash: u8| { + [signature.to_der().as_bytes(), &[sighash]].concat() + }; + assert!(spends(&tx, &[&[], &first, &encoded(&low, 0x01)], &redeem)); + assert!(spends(&tx, &[&[], &first, &encoded(&high, 0x01)], &redeem)); + assert!(!spends(&tx, &[&[], &first, &encoded(&low, 0x04)], &redeem)); + // R behind a zero it doesn't need. + let der = low.to_der(); + let der = der.as_bytes(); + let padded = [ + &[0x30, der[1] + 1, 0x02, der[3] + 1, 0x00][..], + &der[4..], + &[0x01], + ] + .concat(); + assert!(!spends(&tx, &[&[], &first, &padded], &redeem)); + } + + /// `OP_CHECKMULTISIG` pops an item more than its signatures, which must be + /// empty. + #[test] + fn a_multisig_needs_its_extra_item_empty() { + let members = signers(1..=3); + let redeem = multisig(&members); + let tx = transaction(u32::MAX, 0); + let hash = bitcoin_sighash(&tx, &redeem); + let (first, second) = (signature(&members[0], &hash), signature(&members[1], &hash)); + assert!(spends(&tx, &[&[], &first, &second], &redeem)); + assert!(!spends(&tx, &[&[0x01], &first, &second], &redeem)); + } + + /// bitcoinj hashes a transaction for a legacy signature as it serializes + /// it, with its witnesses where it has any: where another input has one, a + /// signature over that hash verifies and one over Bitcoin's doesn't. + #[test] + fn the_signature_hash_covers_the_witnesses_of_a_transaction_with_any() { + let members = signers(1..=3); + let redeem = multisig(&members); + let tx = transaction(u32::MAX, 1); + let mut copy = tx.clone(); + copy.input[0].script_sig = ScriptBuf::from_bytes(redeem.clone()); + let mut preimage = bitcoin::consensus::serialize(©); + preimage.extend_from_slice(&1u32.to_le_bytes()); + let with_witnesses: [u8; 32] = sha2::Sha256::digest(sha2::Sha256::digest(&preimage)).into(); + let without = bitcoin_sighash(&tx, &redeem); + assert_ne!(with_witnesses, without); + let signed_over = |hash: &[u8; 32]| { + let (first, second) = (signature(&members[0], hash), signature(&members[1], hash)); + spends(&tx, &[&[], &first, &second], &redeem) + }; + assert!(signed_over(&with_witnesses)); + assert!(!signed_over(&without)); + } + + /// An ERP script's members spend it at once. Its emergency keys spend it + /// once the input's sequence counts its delay, in a transaction of version + /// 2 or more, as BIP112 has it (bitcoinj's `checkSequence`). + #[test] + fn an_erp_scripts_emergency_keys_wait_for_its_delay() { + let (members, emergency) = (signers(1..=3), signers(4..=6)); + let redeem = p2sh_erp(&members, &emergency, &[0xf4, 0x01]); + let signed = |tx: &Transaction, signers: &[SigningKey], branch: &[u8]| { + let hash = bitcoin_sighash(tx, &redeem); + let (first, second) = (signature(&signers[0], &hash), signature(&signers[1], &hash)); + spends(tx, &[&[], &first, &second, branch], &redeem) + }; + assert!(signed(&transaction(0, 0), &members, &[])); + assert!(!signed(&transaction(0, 0), &emergency, &[])); + assert!(signed(&transaction(500, 0), &emergency, &[0x01])); + assert!(!signed(&transaction(499, 0), &emergency, &[0x01])); + let mut version_1 = transaction(500, 0); + version_1.version = bitcoin::transaction::Version::ONE; + assert!(!signed(&version_1, &emergency, &[0x01])); + } + + /// `scriptCorrectlySpendsTx` takes no scriptSig holding an opcode beyond + /// the small numbers, whatever it would do run, nor one bitcoinj can't + /// parse. + #[test] + fn a_script_sig_of_more_than_pushes_spends_nothing() { + let members = signers(1..=3); + let redeem = multisig(&members); + let mut tx = transaction(u32::MAX, 0); + let hash = bitcoin_sighash(&tx, &redeem); + let (first, second) = (signature(&members[0], &hash), signature(&members[1], &hash)); + let pushed = [vec![OP_0], push(&first), push(&second)].concat(); + for script_sig in [ + [&pushed[..], &[OP_NOP], &push(&redeem)].concat(), + [&pushed[..], &push(&redeem)[..redeem.len()]].concat(), + ] { + tx.input[0].script_sig = ScriptBuf::from_bytes(script_sig); + assert!(!script_correctly_spends_tx(&tx, 0, &p2sh(&redeem))); + } + } +} diff --git a/crates/execution/src/bridge/storage.rs b/crates/execution/src/bridge/storage.rs index eb6491e..89b2fc9 100644 --- a/crates/execution/src/bridge/storage.rs +++ b/crates/execution/src/bridge/storage.rs @@ -220,15 +220,36 @@ pub fn bridge_load_bytes_named(ctx: &mut CTX, key_name bridge_load_bytes(ctx, key) } -/// Transfer amount from Bridge contract to recipient. -pub fn bridge_transfer(ctx: &mut CTX, recipient: Address, amount: U256) -> bool { - if amount.is_zero() { - return true; +/// Moves `amount`, which is more than nothing, from the Bridge to +/// `recipient`, as rskj's `BridgeSupport.transferTo` moves it: with +/// `Repository.transfer`, which takes it off the Bridge whatever the Bridge +/// holds. On a chain the Bridge holds what it pays. A local call can name +/// the Bridge as its sender and have it send what it holds away first, or +/// run over a state in which it holds too little; rskj's Bridge pays all the +/// same, and is left below zero. No balance here can be, so the Bridge is +/// lifted to the amount and owes what it was lifted by +/// (`RskChainExt::debts`), as the sender of a local call is. +/// +/// Nothing a Bridge method does after it has paid can fail, so what the +/// Bridge owes is taken back with the frame its caller runs in and not +/// otherwise (`RskHandler`'s `frames`). +pub fn bridge_transfer( + ctx: &mut CTX, + recipient: Address, + amount: U256, +) -> Result< + Option, + ::Error, +> { + use revm::context_interface::journaled_state::account::JournaledAccountTr; + let held = *ctx.journal_mut().load_account_mut(BRIDGE_ADDR)?.balance(); + if held < amount { + ctx.journal_mut() + .load_account_mut(BRIDGE_ADDR)? + .set_balance(amount); + ctx.chain_mut().debts.add(BRIDGE_ADDR, amount - held); } - matches!( - ctx.journal_mut().transfer(BRIDGE_ADDR, recipient, amount), - Ok(None) - ) + ctx.journal_mut().transfer(BRIDGE_ADDR, recipient, amount) } // --------------------------------------------------------------------------- @@ -404,6 +425,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); @@ -549,9 +639,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 +711,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 @@ -1128,4 +1231,46 @@ mod tests { let decoded = deserialize_utxo_list(&data); assert!(decoded.is_empty()); } + + /// The Bridge pays whatever it holds, as rskj's `Repository.transfer` + /// moves a balance: one that doesn't cover the payment is left below + /// zero, which here is what the Bridge owes. rskj's release answered a + /// Bridge that holds too little through `eth_call` (the rpc crate's + /// `testdata/rskj_local_calls.txt`, the scenarios `bridgepays` and + /// `bridgepoor`). + #[test] + fn the_bridge_pays_whatever_it_holds() { + use revm::context_interface::journaled_state::account::JournaledAccountTr; + use revm::context_interface::ContextTr; + use revm::MainContext; + + let mut ctx = + revm::Context::mainnet().with_chain(crate::raw_storage::RskChainExt::default()); + let to = Address::repeat_byte(0x77); + ctx.journal_mut() + .load_account_mut(BRIDGE_ADDR) + .unwrap() + .set_balance(U256::from(7)); + // What the Bridge holds and owes after it has paid 5, and what the + // account it paid holds. + let mut pay = || { + assert!(matches!( + bridge_transfer(&mut ctx, to, U256::from(5)), + Ok(None) + )); + let mut held = |who| ctx.journal_mut().load_account(who).unwrap().info.balance; + let (bridge, paid) = (held(BRIDGE_ADDR), held(to)); + ( + bridge.to::(), + ctx.chain().debts.of(BRIDGE_ADDR).to::(), + paid.to::(), + ) + }; + // It holds 7: 2 are left, and it owes nothing. + assert_eq!(pay(), (2, 0, 5)); + // It holds 2: it is left 3 below zero. + assert_eq!(pay(), (0, 3, 10)); + // And nothing: 8 below. + assert_eq!(pay(), (0, 8, 15)); + } } 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 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/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 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..e223fbd --- /dev/null +++ b/crates/execution/src/bridge/testdata/rskj_refunded_pegins.txt @@ -0,0 +1,456 @@ +# 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 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. 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 +# 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 +# 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 +# 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 +# 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 +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 +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/bridge/tx.rs b/crates/execution/src/bridge/tx.rs index 19c8919..a1734b8 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::*; @@ -173,30 +173,36 @@ pub fn register_btc_coinbase_transaction( ctx: &mut CTX, args: &[u8], gas_cost: u64, + config: &super::constants::BridgeConstants, + hardfork_cfg: &crate::hardfork::RskHardforkConfig, ) -> 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 = crate::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(); + }; + let (btc_tx_data, pmt_data) = (btc_tx_data.to_vec(), pmt_data.to_vec()); - // 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)?; + // 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); @@ -218,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. @@ -237,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 @@ -246,16 +252,24 @@ 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. - let btc_tx: bitcoin::Transaction = bitcoin::consensus::deserialize(&btc_tx_data) - .map_err(|_| PrecompileError::other("registerBtcCoinbaseTransaction: invalid btc tx"))?; + // rskj parses the transaction and runs bitcoinj's `btcTx.verify()` on it + // (BridgeSupport l.2559-2560). Either throws. + 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. + 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 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(); @@ -263,11 +277,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( @@ -292,13 +309,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 = 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); let mut output = [0u8; 32]; @@ -347,13 +360,13 @@ pub fn get_btc_transaction_confirmations( revm::context_interface::Block::number(revm::context_interface::ContextTr::block(ctx)) .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_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)) @@ -401,21 +414,28 @@ 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 + || (hardfork_cfg.has_rskip143(block_number) + && 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, @@ -469,37 +489,240 @@ 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) } -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")); +/// 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()`: how many bytes it was parsed from, not counting any +/// left over after it. +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"); } - 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")); + if serialized_size > MAX_BLOCK_SIZE { + return Err("bitcoinj verify: larger than a block"); } - Ok(args[data_start..data_start + len].to_vec()) + 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(()) +} + +/// 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 { @@ -633,13 +856,15 @@ 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" ); - // 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") @@ -672,7 +897,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] = @@ -690,6 +915,345 @@ 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) + ); + } + + /// 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); + } + } + + /// 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 + /// 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. @@ -723,7 +1287,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/bridge/union.rs b/crates/execution/src/bridge/union.rs index 88e35d2..bacb204 100644 --- a/crates/execution/src/bridge/union.rs +++ b/crates/execution/src/bridge/union.rs @@ -11,15 +11,21 @@ //! 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}; -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. /// @@ -49,12 +55,19 @@ 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. /// /// 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, @@ -78,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(), @@ -219,7 +244,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], @@ -228,13 +253,19 @@ 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()); - } - let Some(new_address) = abi_address(args, 0) else { + 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 { return int_result(gas_cost, UnionResponse::GenericError.code()); }; store_long(ctx, UNION_CONTRACT_ADDRESS_KEY, &rlp_encode_bytes(new_address.as_slice())); @@ -275,14 +306,20 @@ 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()); - } - let Some(new_cap) = abi_uint(args, 0) else { + 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()); }; 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()); } @@ -310,12 +347,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) = crate::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) { @@ -397,10 +437,15 @@ 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()); - } - let (Some(request), Some(release)) = (abi_bool(args, 0), abi_bool(args, 1)) else { + 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); + let (Some(request), Some(release)) = (args.bool(0), args.bool(1)) else { return int_result(gas_cost, UnionResponse::GenericError.code()); }; @@ -415,36 +460,120 @@ pub fn set_transfer_permissions( int_result(gas_cost, UnionResponse::Success.code()) } -// --------------------------------------------------------------------------- -// Helpers -// --------------------------------------------------------------------------- +/// `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())) +} -fn abi_word(args: &[u8], index: usize) -> Option<&[u8]> { - args.get(index * 32..index * 32 + 32) +/// `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 = crate::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!( + "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())) } -fn abi_address(args: &[u8], index: usize) -> Option
{ - abi_word(args, index).map(|w| Address::from_slice(&w[12..])) +/// 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(()) } -fn abi_uint(args: &[u8], index: usize) -> Option { - abi_word(args, index).map(U256::from_be_slice) +/// 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(()) } -fn abi_bool(args: &[u8], index: usize) -> Option { - abi_word(args, index).map(|w| w.iter().any(|b| *b != 0)) +// --------------------------------------------------------------------------- +// Helpers +// --------------------------------------------------------------------------- + +/// rskj `new co.rsk.core.Coin((BigInteger) arg)`: the word as a signed +/// number, `None` where it is negative. +fn coin(value: &crate::abi::Int) -> Option { + let value = value.signed(); + (!value.is_negative()).then(|| value.into_raw()) } -/// Moves RBTC out of the Bridge's own balance. +/// Moves RBTC out of the Bridge's own balance, whatever it holds +/// (`bridge_transfer`). fn transfer_from_bridge( ctx: &mut CTX, to: Address, amount: U256, ) -> Result<(), PrecompileError> { - use revm::context_interface::JournalTr; - ctx.journal_mut() - .transfer(crate::precompiles::BRIDGE_ADDR, to, amount) + super::storage::bridge_transfer(ctx, 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(()) } @@ -553,15 +682,315 @@ 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()); + } + } + + /// 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. + 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); + 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/collision.rs b/crates/execution/src/collision.rs index 0f86ed8..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()) { @@ -712,7 +713,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/database.rs b/crates/execution/src/database.rs index 1d7a486..8038959 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; @@ -50,6 +50,9 @@ pub struct RskDatabase { /// Cache of code by hash, populated during `basic_ref` calls. /// Uses `RefCell` because `DatabaseRef` methods take `&self`. code_cache: RefCell>, + /// The number and hash of the parent of the block run on this view, whose + /// ancestors BLOCKHASH answers from ([`RskDatabase::for_block`]). + parent: Option<(u64, B256)>, } impl RskDatabase { @@ -58,7 +61,34 @@ impl RskDatabase { store: Arc, block_store: Arc, ) -> Self { - Self { root, store, block_store, code_cache: RefCell::new(HashMap::new()) } + Self { + root, + store, + block_store, + code_cache: RefCell::new(HashMap::new()), + parent: None, + } + } + + /// The block run on this view, or the block a call runs as part of. + /// BLOCKHASH answers from that block's own ancestors: rskj's + /// `Program.getBlockHash` asks `IndexedBlockStore.getBlockHashByNumber` + /// with the block's parent, which walks back from it. On the main chain + /// that is the main chain's block; beside it, the fork's, down to where + /// the fork leaves the main chain. Without it, the main chain's. + pub fn for_block(mut self, header: &rustock_core::Header) -> Self { + self.parent = header + .number + .checked_sub(1) + .map(|number| (number, header.parent_hash)); + self + } + + /// 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 @@ -120,8 +150,13 @@ impl DatabaseRef for RskDatabase { None => revm::primitives::KECCAK_EMPTY, }; + // Code is the bytes saved, and runs as they are. rskj has no EIP-7702, + // so code that starts 0xEF01 names no other account's code to run: its + // 0xEF is no opcode, and a call to it fails there. Such code could be + // deployed until RSKIP544 (vetiver900). `Bytecode::new_raw` would read + // it as a designation, and panic where it isn't one. let bytecode = match code { - Some(c) => Bytecode::new_raw(c.into()), + Some(c) => Bytecode::new_legacy(c.into()), None => Bytecode::default(), }; @@ -190,9 +225,25 @@ impl DatabaseRef for RskDatabase { } fn block_hash_ref(&self, number: u64) -> Result { - self.block_store - .canonical_hash(number) - .map_err(|e| RskDbError::BlockStore(e.to_string()))? + let store_error = |e: anyhow::Error| RskDbError::BlockStore(e.to_string()); + let canonical = |n: u64| self.block_store.canonical_hash(n).map_err(store_error); + // Up the run block's parents while they are beside the main chain; + // from the first that is on it, the main chain's index answers. + if let Some((mut n, mut hash)) = self.parent { + while n > number && canonical(n)? != Some(hash) { + hash = self + .block_store + .header(hash) + .map_err(store_error)? + .ok_or_else(|| RskDbError::BlockStore(format!("block {hash} not found")))? + .parent_hash; + n -= 1; + } + if n == number { + return Ok(hash); + } + } + canonical(number)? .ok_or_else(|| RskDbError::BlockStore(format!("block {} not found", number))) } } diff --git a/crates/execution/src/env.rs b/crates/execution/src/env.rs index bbe1d98..73cc618 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; @@ -30,25 +31,88 @@ 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). +/// +/// 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_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!( + "gas limit {} is not gas: eight bytes with the top bit set", + tx.gas_limit + )) + })?; let kind = if tx.to.is_empty() { TxKind::Create } else { @@ -58,11 +122,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 +137,7 @@ pub fn tx_env_from_rsk_tx( blob_hashes: Vec::new(), max_fee_per_blob_gas: 0, authorization_list: Vec::new(), - } + }) } #[cfg(test)] @@ -107,6 +171,7 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } @@ -136,6 +201,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(); @@ -155,7 +276,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 +302,84 @@ 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 + ); + } + + /// 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 548dee1..44ed575 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}; @@ -30,9 +30,56 @@ pub struct TxExecutionResult { pub created_address: Option
, /// The transaction reached REMASC or the Bridge, so under RSKIP-144 it /// may only sit in the sequential sublist - /// (`SEQUENTIAL_ONLY_PRECOMPILES`). True whatever the call returned -- - /// reaching the precompile is what counts. + /// (`SEQUENTIAL_ONLY_PRECOMPILES`). As rskj counts it: whatever the call + /// returned, and though it was refused for the depth or the caller's + /// balance, but not from under a constructor of a CREATE or CREATE2 + /// (`RskHandler`'s `note_call`). 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 @@ -149,6 +196,16 @@ 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), + }; } /// Fold one sublist's state into the block's reported state. @@ -211,6 +268,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 +308,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 +345,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 +356,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, } } @@ -317,7 +389,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 @@ -325,43 +397,77 @@ 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. + /// + /// `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 db = RskDatabase::new(state_root.clone(), trie_store.clone(), self.block_store.clone()); + 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(), + ) + .for_block(header); let cfg = make_cfg_env(spec_id, self.hardfork_cfg.chain_id, self.hardfork_cfg.has_rskip544(header.number)); 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) @@ -372,6 +478,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()); @@ -383,6 +490,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(); @@ -405,15 +513,19 @@ 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), self.hardfork_cfg.has_rskip91(header.number), + !self.hardfork_cfg.has_rskip103(header.number), + !self.hardfork_cfg.has_rskip150(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, + crate::rsk_instructions::BlockWords::of(header), ); let exec_out = { @@ -424,10 +536,17 @@ 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), - ); + ) + .failing_programs(!header_fits_program_invoke(header)) + .with_rskip90(self.hardfork_cfg.has_rskip90(header.number)) + .with_rskip174(self.hardfork_cfg.has_rskip174(header.number)) + .as_local_call(local_call, self.hardfork_cfg.has_rskip136(header.number)) + .from_nobody(no_sender) + .with_reached_sequential_only(reached_sequential_only.clone()); let out = handler .run(&mut evm) .map_err(|e: revm::context::result::EVMError<_>| { @@ -443,6 +562,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(); @@ -457,6 +577,7 @@ impl RskExecutor { output, logs, created_address, + reverted, }) } @@ -485,14 +606,20 @@ 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 db = RskDatabase::new(state_root.clone(), trie_store.clone(), self.block_store.clone()); + 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(), + ) + .for_block(header); let cfg = make_cfg_env(spec_id, self.hardfork_cfg.chain_id, self.hardfork_cfg.has_rskip544(header.number)); 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) @@ -503,6 +630,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()); @@ -514,6 +642,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(); @@ -538,15 +667,19 @@ 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), self.hardfork_cfg.has_rskip91(header.number), + !self.hardfork_cfg.has_rskip103(header.number), + !self.hardfork_cfg.has_rskip150(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, + crate::rsk_instructions::BlockWords::of(header), ); let exec_out = { @@ -556,10 +689,16 @@ 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), - ); + ) + .failing_programs(!header_fits_program_invoke(header)) + .with_rskip90(self.hardfork_cfg.has_rskip90(header.number)) + .with_rskip174(self.hardfork_cfg.has_rskip174(header.number)) + .as_local_call(local_call, self.hardfork_cfg.has_rskip136(header.number)) + .with_reached_sequential_only(reached_sequential_only.clone()); use revm::inspector::InspectorHandler; let out = handler .inspect_run(&mut evm) @@ -576,6 +715,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); @@ -588,6 +728,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 { @@ -596,6 +744,7 @@ impl RskExecutor { output, logs, created_address, + reverted, reached_sequential_only: reached_sequential_only .load(std::sync::atomic::Ordering::Relaxed), }, @@ -606,9 +755,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 @@ -617,18 +771,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 @@ -645,21 +804,31 @@ 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 db = RskDatabase::new(state_root.clone(), trie_store.clone(), self.block_store.clone()); + 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(), + ) + .for_block(header); let cfg = make_cfg_env(spec_id, self.hardfork_cfg.chain_id, self.hardfork_cfg.has_rskip544(header.number)); 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) @@ -670,6 +839,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()); @@ -681,6 +851,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(); @@ -705,15 +876,19 @@ 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), self.hardfork_cfg.has_rskip91(header.number), + !self.hardfork_cfg.has_rskip103(header.number), + !self.hardfork_cfg.has_rskip150(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, + crate::rsk_instructions::BlockWords::of(header), ); let exec_out = { @@ -723,10 +898,17 @@ 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), - ); + ) + .failing_programs(!header_fits_program_invoke(header)) + .with_rskip90(self.hardfork_cfg.has_rskip90(header.number)) + .with_rskip174(self.hardfork_cfg.has_rskip174(header.number)) + .as_local_call(local_call, self.hardfork_cfg.has_rskip136(header.number)) + .from_nobody(no_sender) + .with_reached_sequential_only(reached_sequential_only.clone()); use revm::inspector::InspectorHandler; let out = handler .inspect_run(&mut evm) @@ -743,20 +925,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 { @@ -765,6 +946,7 @@ impl RskExecutor { output, logs, created_address, + reverted, reached_sequential_only: reached_sequential_only .load(std::sync::atomic::Ordering::Relaxed), }, @@ -780,6 +962,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, @@ -787,9 +971,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(()); } @@ -864,6 +1050,7 @@ impl RskExecutor { crate::tracer::RskTracer::new(0), TracedTxs::None, U256::ZERO, + &[], true, Some(&mut dropped), )?; @@ -985,16 +1172,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, @@ -1005,6 +1194,7 @@ impl RskExecutor { inspector, traced, U256::ZERO, + &[], true, None, ); @@ -1064,6 +1254,7 @@ impl RskExecutor { inspector.fresh_like(), TracedTxs::None, U256::ZERO, + &[], true, None, )?; @@ -1132,6 +1323,7 @@ impl RskExecutor { template.fresh_like(), traced, U256::ZERO, + &[], false, None, ); @@ -1202,6 +1394,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, @@ -1212,6 +1410,7 @@ impl RskExecutor { inspector, traced, fees_carried, + &deleted_before, false, None, )?; @@ -1290,6 +1489,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` @@ -1307,12 +1510,19 @@ impl RskExecutor { > { let spec_id = self.hardfork_cfg.spec_id(header.number); let block_env = block_env_from_header(header, &self.hardfork_cfg); - let db = RskDatabase::new(state_root.clone(), trie_store.clone(), self.block_store.clone()); + // BLOCKHASH reads the block's own ancestors, beside the main chain too. + let db = RskDatabase::new( + state_root.clone(), + trie_store.clone(), + self.block_store.clone(), + ) + .for_block(header); let cfg = make_cfg_env(spec_id, self.hardfork_cfg.chain_id, self.hardfork_cfg.has_rskip544(header.number)); 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) @@ -1323,6 +1533,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()); @@ -1331,6 +1542,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
= @@ -1356,23 +1568,28 @@ 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), self.hardfork_cfg.has_rskip91(header.number), + !self.hardfork_cfg.has_rskip103(header.number), + !self.hardfork_cfg.has_rskip150(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, + crate::rsk_instructions::BlockWords::of(header), ); // 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 @@ -1382,7 +1599,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 @@ -1390,12 +1607,28 @@ 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 = 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, + 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 @@ -1466,34 +1699,90 @@ 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 - // **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. + // + // 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. // - // 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. - if !Self::is_remasc_tx(tx) && (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 { + // 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()); + 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 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(); + 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; @@ -1501,25 +1790,19 @@ 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 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 - // 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 @@ -1550,14 +1833,43 @@ impl RskExecutor { let _ = evm.ctx.journal_mut().load_account(REMASC_ADDR); evm.ctx.journal_mut().touch_account(REMASC_ADDR); } + // What REMASC reads that fails is the node's store, never the + // block: rskj holds every block it runs a block on. crate::remasc::process_miners_fees( &mut evm.ctx, &self.remasc_config, &self.bridge_constants, &self.hardfork_cfg, Some(&self.block_store), + header, ) - .map_err(|e| ExecutionError::Evm(format!("REMASC: {e:?}")))?; + .map_err(|e| { + ExecutionError::Database(crate::database::RskDbError::BlockStore(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 @@ -1572,6 +1884,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. @@ -1599,11 +1913,20 @@ impl RskExecutor { output: Vec::new(), logs, created_address: None, + reverted: false, }); 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 @@ -1621,7 +1944,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) { @@ -1635,6 +1961,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. @@ -1644,23 +1971,128 @@ impl RskExecutor { } } - // Direct Bridge calls that revm's gas validation cannot model: + // A transaction's own call to a precompile that rskj runs in a way + // revm's gas validation cannot model (`TransactionExecutor.call`): // - free bridge transactions (pre-areBridgeTxsPaid): gas_limit 0, // nothing charged (Transaction.transactionCost == 0, // Bridge.getGasForData == 0); // - paid calls before RSKIP136 (bahamas): rskj checked the limit - // against the precompile cost ALONE and recorded - // gasUsed = precompileCost + intrinsicCost, possibly above the - // tx gas limit (mainnet #3307: 31280 used with a 30000 limit). + // against the precompile's price ALONE and recorded + // gasUsed = price + intrinsicCost, possibly above the tx gas + // limit (mainnet #3307: 31280 used with a 30000 limit). REMASC + // asks for nothing (`RemascContract.getGasForData`), so the rule + // changes nothing for it, and its calls stay in the EVM. let is_free = self.is_free_bridge_tx(tx, *sender, header.number); - let is_pre136_bridge = !self.hardfork_cfg.has_rskip136(header.number) - && tx.to.len() == 20 - && tx.to.as_ref() == crate::precompiles::BRIDGE_ADDR.as_slice(); - if is_free || is_pre136_bridge { - debug!(tx_index = i, is_free, "executing direct bridge call"); + let before_rskip136 = + !self.hardfork_cfg.has_rskip136(header.number) && tx.to.len() == 20; + let is_pre136_bridge = + before_rskip136 && tx.to.as_ref() == crate::precompiles::BRIDGE_ADDR.as_slice(); + // Any other precompile is run now: it writes nothing, so what it + // does is known before the transaction is charged. + let pre136_outcome = if before_rskip136 && !is_pre136_bridge { + evm.precompiles.run_for_a_transaction( + &evm.ctx, + Address::from_slice(tx.to.as_ref()), + tx.input.as_ref(), + tx_gas, + ) + } else { + None + }; + if is_free || is_pre136_bridge || pre136_outcome.is_some() { + let callee = Address::from_slice(tx.to.as_ref()); + debug!(tx_index = i, is_free, %callee, "executing a direct precompile call"); + use crate::native::Outcome; use revm::context_interface::journaled_state::account::JournaledAccountTr; use revm::context_interface::{ContextTr, JournalTr}; - // rskj increments the sender nonce in init(), outside the + // rskj `Transaction.transactionCost`: nothing for a free Bridge + // transaction, and 68 for a byte of data that isn't zero, 16 + // from RSKIP400. + let basic: u64 = if is_free { + 0 + } else { + let nonzero = if self.hardfork_cfg.has_reduced_calldata_cost(header.number) { + 16 + } else { + 68 + }; + 21_000 + + tx.input + .iter() + .map(|b| if *b == 0 { 4u64 } else { nonzero }) + .sum::() + }; + // rskj `TransactionExecutor.init`, which revm's own checks are + // for every other transaction: a limit that pays the intrinsic + // cost, the sender's next nonce, and a balance that covers the + // value and, for a priced transaction, the whole limit. Mining + // takes a call only where the balance covers what it can come + // to past its limit as well: rskj would take that whatever the + // sender holds (see the fee below), which this node can't. A + // precompile's run says what that is; the Bridge's price is + // known only as it runs, so for it the most it can be. + let most = match &pre136_outcome { + Some(Outcome::Returned { gas, .. } | Outcome::Null { gas }) => { + gas.saturating_add(basic) + } + Some(_) => tx_gas, + None => tx_gas.saturating_add(basic), + }; + let refusal = { + let account = evm + .ctx + .journal_mut() + .load_account(*sender) + .map_err(|e| ExecutionError::Evm(format!("{e:?}")))?; + let (nonce, balance) = (account.data.info.nonce, account.data.info.balance); + let covers = |gas: u64| { + let cost = if basic > 0 { + tx.gas_price + .checked_mul(U256::from(gas)) + .and_then(|cost| cost.checked_add(tx.value)) + } else { + Some(tx.value) + }; + cost.is_some_and(|cost| balance >= cost) + }; + if tx_gas < basic { + Some(( + format!("Not enough gas for transaction execution: tx needs: {basic} tx sent: {tx_gas}"), + true, + )) + } else if nonce != tx.nonce { + Some(( + format!("Invalid nonce: required: {nonce} , tx.nonce: {}", tx.nonce), + true, + )) + } else if !covers(tx_gas) { + Some((format!("Not enough cash: Sender cash: {balance}"), true)) + } else if discard_unfitting.is_some() && !covers(most) { + Some(( + "its sender may not cover the gas past its limit".to_string(), + false, + )) + } else { + None + } + }; + if let Some((reason, refused)) = refusal { + if let Some(drops) = discard_unfitting.as_deref_mut() { + debug!(tx_index = i, %reason, "mining: dropping a direct precompile call"); + // What rskj refuses its miner reports as invalid, for + // the pool to forget. The other stays pending. + if refused { + drops.invalid.push(i); + } else { + drops.unfitting.push(i); + } + dropped_senders.insert(*sender); + evm.ctx.chain_mut().raw_storage.discard_tx(); + continue; + } + return Err(ExecutionError::Evm(format!("transaction {i}: {reason}"))); + } + // rskj increments the sender nonce in `execute()`, outside the // execution rollback scope. { let mut acc = evm.ctx.journal_mut().load_account_mut(*sender) @@ -1668,136 +2100,233 @@ impl RskExecutor { acc.data.bump_nonce(); } - // Warm the Bridge account: outside revm's transact flow the + // Warm the callee: outside revm's transact flow the // precompile warm-set is not populated and journal sloads on // a cold account fail. - let _ = evm.ctx.journal_mut() - .load_account(crate::precompiles::BRIDGE_ADDR); - - // Pre-RSKIP136 gas accounting (rskj TransactionExecutor.call). - let (gas_used, enough_gas) = if is_free { - (0u64, true) - } else { - let required = crate::bridge::bridge_call_gas_cost( - &mut evm.ctx, - tx.input.as_ref(), - &self.bridge_constants, - &self.hardfork_cfg, - ) - .unwrap_or(0); - let basic: u64 = 21_000 - + tx.input.iter() - .map(|b| if *b == 0 { 4u64 } else { 68 }) - .sum::(); - let limit = tx.gas_limit.to::(); - if limit >= required { - // gasUsed may exceed the limit; that is consensus here. - (required.saturating_add(basic), true) - } else { - // rskj execError: all gas consumed, nothing executed. - (limit, false) - } - }; + let _ = evm.ctx.journal_mut().load_account(callee); let mut invisible_exception = false; - let (success, output, logs) = if enough_gas { - let tx_ctx = bridge_ctx_slot.lock().map(|g| g.clone()).unwrap_or_default(); - let use_v2 = self.hardfork_cfg.has_stored_block_v2(header.number); - let checkpoint = evm.ctx.journal_mut().checkpoint(); - let outcome = crate::bridge::execute_bridge( - &mut evm.ctx, - tx.input.as_ref(), - u64::MAX, // cost already accounted above - &self.bridge_constants, - Some(&self.block_store), - use_v2, - &self.hardfork_cfg, - &tx_ctx, - *sender, - 1, // a direct top-level tx is depth 1 (not a contract call) - crate::bridge::BridgeCallKind::Call, // a tx is always a plain CALL - tx.value, // top-level call value = the tx value - ); - match outcome { - Ok(out) => { - evm.ctx.chain_mut().raw_storage.commit_call(); - evm.ctx.journal_mut().checkpoint_commit(); - // rskj transfers the endowment after execute() - // returns without throwing. - if !tx.value.is_zero() { - let _ = evm.ctx.journal_mut().transfer( - *sender, - crate::precompiles::BRIDGE_ADDR, - tx.value, - ); - } - // rskj setupContract after a successful direct - // precompile call (TransactionExecutor.call). - if !markers.precompiles.contains(&crate::precompiles::BRIDGE_ADDR) { - markers.precompiles.push(crate::precompiles::BRIDGE_ADDR); - } - // Drain bridge events into the receipt like revm - // does for normal transactions. - let logs = evm.ctx.journal_mut().take_logs(); - (true, out.bytes.to_vec(), logs) + let (gas_used, success, output, logs) = match pre136_outcome { + // What `getGasForData` asked for came to more than the whole + // limit: rskj's execError, all the gas used and nothing run. + Some(Outcome::OutOfGas) => (tx_gas, false, Vec::new(), Vec::new()), + // `execute` threw: rskj's invisible exception, as for the + // Bridge below. + Some(Outcome::Threw { gas, reason }) => { + debug!(tx_index = i, %reason, "direct precompile call threw"); + invisible_exception = true; + (gas.saturating_add(basic), true, Vec::new(), Vec::new()) + } + Some(Outcome::Fatal(reason)) => return Err(ExecutionError::Evm(reason)), + // It returned, or returned rskj's null, which is no output. + // rskj sets the callee up as a contract and moves the value + // to it, which creates its account even where there is none + // to move (`Repository.transfer`). + Some(Outcome::Returned { gas, output }) => { + let _ = evm.ctx.journal_mut().transfer(*sender, callee, tx.value); + if !markers.precompiles.contains(&callee) { + markers.precompiles.push(callee); } - Err(e) if crate::bridge::is_invisible_exception(&e) => { - // rskj invisible exception: receipt SUCCESS with - // the spent gas, no logs, no endowment — but the - // sender is charged the FULL gas limit. - evm.ctx.chain_mut().raw_storage.discard_call(); - // RSKIP-144: the writes this call attempted are - // about to be popped off the journal. The EVM's - // own frames are covered in `rsk_handler`; a - // direct bridge call never enters one. - if crate::collision::records_attempted_writes() { - let j = &evm.ctx.journal_mut().inner; - crate::collision::note_journal_entries(&j.journal, &j.state); - } - evm.ctx.journal_mut().checkpoint_revert(checkpoint); - crate::collision::forget_entries_above( - evm.ctx.journal_mut().inner.journal.len(), - ); - invisible_exception = true; - (true, Vec::new(), Vec::new()) + (gas.saturating_add(basic), true, output.to_vec(), Vec::new()) + } + Some(Outcome::Null { gas }) => { + let _ = evm.ctx.journal_mut().transfer(*sender, callee, tx.value); + if !markers.precompiles.contains(&callee) { + markers.precompiles.push(callee); } - Err(e) => { - evm.ctx.chain_mut().raw_storage.discard_call(); - // RSKIP-144: the writes this call attempted are - // about to be popped off the journal. The EVM's - // own frames are covered in `rsk_handler`; a - // direct bridge call never enters one. - if crate::collision::records_attempted_writes() { - let j = &evm.ctx.journal_mut().inner; - crate::collision::note_journal_entries(&j.journal, &j.state); + (gas.saturating_add(basic), true, Vec::new(), Vec::new()) + } + // The Bridge, whose price depends on what its storage holds. + None => { + // Pre-RSKIP136 gas accounting (rskj TransactionExecutor.call). + let (gas_used, enough_gas) = if is_free { + (0u64, true) + } else { + let required = crate::bridge::bridge_call_gas_cost( + &mut evm.ctx, + tx.input.as_ref(), + &self.bridge_constants, + &self.hardfork_cfg, + ) + .unwrap_or(0); + let limit = tx_gas; + if limit >= required { + // gasUsed may exceed the limit; that is consensus here. + (required.saturating_add(basic), true) + } else { + // rskj execError: all gas consumed, nothing executed. + (limit, false) } - evm.ctx.journal_mut().checkpoint_revert(checkpoint); - crate::collision::forget_entries_above( - evm.ctx.journal_mut().inner.journal.len(), + }; + let (success, output, logs) = if enough_gas { + let tx_ctx = bridge_ctx_slot + .lock() + .map(|g| g.clone()) + .unwrap_or_default(); + let use_v2 = self.hardfork_cfg.has_stored_block_v2(header.number); + let checkpoint = evm.ctx.journal_mut().checkpoint(); + // The logs the journal holds before the call, to tell + // the call's own apart if it throws (below). + let logs_before = evm.ctx.journal().logs().len(); + let outcome = crate::bridge::execute_bridge( + &mut evm.ctx, + tx.input.as_ref(), + u64::MAX, // cost already accounted above + &self.bridge_constants, + Some(&self.block_store), + use_v2, + &self.hardfork_cfg, + &tx_ctx, + *sender, + 1, // a direct top-level tx is depth 1 (not a contract call) + crate::bridge::BridgeCallKind::Call, // a tx is always a plain CALL + tx.value, // top-level call value = the tx value ); - debug!(tx_index = i, error = %e, "direct bridge call failed"); + match outcome { + Ok(out) => { + evm.ctx.chain_mut().raw_storage.commit_call(); + evm.ctx.journal_mut().checkpoint_commit(); + // rskj transfers the endowment after execute() + // returns without throwing. + if !tx.value.is_zero() { + let _ = evm.ctx.journal_mut().transfer( + *sender, + crate::precompiles::BRIDGE_ADDR, + tx.value, + ); + } + // rskj setupContract after a successful direct + // precompile call (TransactionExecutor.call). + let bridge = crate::precompiles::BRIDGE_ADDR; + if !markers.precompiles.contains(&bridge) { + markers.precompiles.push(bridge); + } + // Drain bridge events into the receipt like revm + // does for normal transactions. + let logs = evm.ctx.journal_mut().take_logs(); + (true, out.bytes.to_vec(), logs) + } + Err(e) if crate::bridge::is_invisible_exception(&e) => { + // rskj invisible exception: receipt SUCCESS with + // the spent gas and no endowment, but the sender is + // charged the FULL gas limit. What the method logged + // before it threw stays in the receipt: the Bridge + // writes its logs straight into the result's list, + // which only the VM's path clears on a throw + // (`TransactionExecutor.go` returns at `vm == null`), + // and the receipt takes that list. + let kept = evm.ctx.journal().logs()[logs_before..].to_vec(); + evm.ctx.chain_mut().raw_storage.discard_call(); + // RSKIP-144: the writes this call attempted are + // about to be popped off the journal. The EVM's + // own frames are covered in `rsk_handler`; a + // direct bridge call never enters one. + if crate::collision::records_attempted_writes() { + let j = &evm.ctx.journal_mut().inner; + crate::collision::note_journal_entries( + &j.journal, &j.state, + ); + } + evm.ctx.journal_mut().checkpoint_revert(checkpoint); + crate::collision::forget_entries_above( + evm.ctx.journal_mut().inner.journal.len(), + ); + invisible_exception = true; + (true, Vec::new(), kept) + } + Err(e) => { + evm.ctx.chain_mut().raw_storage.discard_call(); + // RSKIP-144: the writes this call attempted are + // about to be popped off the journal. The EVM's + // own frames are covered in `rsk_handler`; a + // direct bridge call never enters one. + if crate::collision::records_attempted_writes() { + let j = &evm.ctx.journal_mut().inner; + crate::collision::note_journal_entries( + &j.journal, &j.state, + ); + } + evm.ctx.journal_mut().checkpoint_revert(checkpoint); + crate::collision::forget_entries_above( + evm.ctx.journal_mut().inner.journal.len(), + ); + debug!(tx_index = i, error = %e, "direct bridge call failed"); + (false, Vec::new(), Vec::new()) + } + } + } 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()) - } + }; + (gas_used, success, output, logs) } - } else { - (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 precompile 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(callee), 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). let fee_gas = if invisible_exception { - tx.gas_limit.to::() + tx_gas } else { gas_used }; - let fee = U256::from(fee_gas) * tx.gas_price; + let fee = U256::from(fee_gas).saturating_mul(tx.gas_price); if !fee.is_zero() { - let _ = evm.ctx.journal_mut().transfer( - *sender, - crate::precompiles::REMASC_ADDR, - fee, - ); + // The sender pays it. Before RSKIP136 a call can come to + // more than its limit, and rskj takes that whatever the + // sender holds: `TransactionExecutor.finalization` adds a + // negative leftover, and the balance goes below zero, which + // rskj stores signed (`RLP.encodeSignedCoinNonNullZero`). + // A balance can't here, so such a block is refused. + let paid = evm + .ctx + .journal_mut() + .load_account_mut(*sender) + .map_err(|e| ExecutionError::Evm(format!("{e:?}")))? + .decr_balance(fee); + if !paid { + return Err(ExecutionError::Evm(format!( + "transaction {i} used {gas_used} gas on a limit of {tx_gas}, and its \ + sender can't pay for the rest: rskj leaves the sender's balance \ + below zero, which this node can't hold" + ))); + } + // REMASC has it now, or with the block's other fees where + // they wait (`postponeFeePayment`). + if !postpone_fees { + evm.ctx + .journal_mut() + .load_account_mut(crate::precompiles::REMASC_ADDR) + .map_err(|e| ExecutionError::Evm(format!("{e:?}")))? + .incr_balance(fee); + } } // What this transaction paid in that nobody was credited // with, for the per-transaction supply check. @@ -1806,18 +2335,29 @@ impl RskExecutor { total_gas += gas_used; tx_results.push(TxExecutionResult { - // This branch exists only to call the Bridge. - reached_sequential_only: true, + // REMASC and the Bridge bar a transaction from a parallel + // sublist. + reached_sequential_only: crate::precompiles::SEQUENTIAL_ONLY_PRECOMPILES + .contains(&callee), gas_used, success, output, logs, created_address: None, + reverted: false, }); 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. @@ -1832,7 +2372,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). @@ -1850,7 +2408,11 @@ impl RskExecutor { // whatever was there, so before RSKIP174 (iris300) deploying onto // an address that already holds a balance destroys it. Someone // funding a deploy address before the deployment lands loses the - // money, and the chain's supply drops with it. + // money, and the chain's supply drops with it -- unless the + // creation fails: the account is written in `cacheTrack`, which + // rskj's `go` rolls back then. So the handler takes the balance in + // the creation's frame, which a failure takes back + // (`RskHandler::with_rskip174`). // // revm does the sane thing and keeps the balance, which is the // post-iris300 rule, so only the earlier window needs this. The @@ -1860,6 +2422,11 @@ impl RskExecutor { // // Testnet #647,051 is a live instance -- 0.05 RBTC sitting at the // deploy address, gone in the reference node's post-state. + // + // The fresh account's nonce is zero too. Only a hash collision + // gives an address a creation derives a nonce, or code, which + // revm would refuse the creation for; the nonce is reset here, + // before the frame opens, and code is left alone. if tx.to.is_empty() && !self.hardfork_cfg.has_rskip174(header.number) { use revm::context_interface::journaled_state::account::JournaledAccountTr; use revm::context_interface::{ContextTr, JournalTr}; @@ -1867,24 +2434,18 @@ impl RskExecutor { // Read before taking a mutable handle, so an address with // nothing in it is not materialised into an empty account that // the state root would then have to carry. - let occupied = evm + let has_nonce = evm .ctx .journal_mut() .load_account(created) - .map(|a| !a.data.info.balance.is_zero() || a.data.info.nonce != 0) + .map(|a| a.data.info.nonce != 0) .unwrap_or(false); - if occupied { + if has_nonce { let mut acc = evm .ctx .journal_mut() .load_account_mut(created) .map_err(|e| ExecutionError::Evm(format!("{e:?}")))?; - // `new AccountState()` -- nonce and balance both zero. - // Code lives under its own trie key and createAccount does - // not touch it, so an address holding code still collides - // in revm where rskj would overwrite. Reaching that needs a - // hash collision, so it is left alone rather than modelled. - acc.data.set_balance(U256::ZERO); acc.data.set_nonce(0); } } @@ -1897,11 +2458,17 @@ 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), ) - .postponing_fees(postpone_fees); + .postponing_fees(postpone_fees) + .failing_programs(fails_programs) + .with_rskip90(self.hardfork_cfg.has_rskip90(header.number)) + .with_rskip174(self.hardfork_cfg.has_rskip174(header.number)) + .with_called_precompiles(called_precompiles.clone()) + .with_reached_sequential_only(reached_sequential_only.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 @@ -1941,14 +2508,26 @@ 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 + // 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(); @@ -1982,7 +2561,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 }; @@ -1999,12 +2578,21 @@ impl RskExecutor { output, logs, created_address, + reverted, }); 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. @@ -2030,14 +2618,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); } } } @@ -2070,6 +2671,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(); @@ -2100,19 +2706,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( @@ -2210,8 +2803,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; + } } } @@ -2238,18 +2837,45 @@ 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() } +/// 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 +} + +/// 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). @@ -2350,13 +2976,17 @@ 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, bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } @@ -2431,31 +3061,156 @@ 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); assert!(after < 150_000, "post-RSKIP197 the caller continues (got {after})"); } + /// A contract's `releaseBtc` before RSKIP185: rskj's + /// `BridgeSupport.releaseBtc` throws `Program.OutOfGasException` for a + /// contract's request, `Bridge.releaseBtc` lets it through, and + /// `Bridge.execute` wraps it in a `VMException` as it wraps any other + /// throw. Before RSKIP197, which testnet's iris300 (#2,060,500) brings with + /// RSKIP185, that fails the contract that made the call with all its gas, + /// though it forwarded only 50,000. From iris300 the request is turned down + /// (CALLER_CONTRACT) and the contract goes on to its end. + #[test] + fn a_contracts_release_before_rskip185_fails_the_contract_with_all_its_gas() { + let run = |height: u64| { + let store = Arc::new(MemoryTrieStore::new()); + let sender = Address::repeat_byte(0xAA); + let contract = Address::repeat_byte(0xBB); + // CALL the Bridge with no data and no value, `releaseBtc`, + // forwarding 50,000 gas, then STOP. + let mut code: Vec = vec![ + 0x60, 0x00, // retSize + 0x60, 0x00, // retOffset + 0x60, 0x00, // argsSize + 0x60, 0x00, // argsOffset + 0x60, 0x00, // value + 0x73, // PUSH20 + ]; + code.extend_from_slice(crate::precompiles::BRIDGE_ADDR.as_slice()); + code.extend_from_slice(&[0x62, 0x00, 0xc3, 0x50, 0xf1, 0x00]); + let root = put_account( + &TrieNode::empty(), + store.as_ref(), + &sender, + 0, + U256::from(10u64).pow(U256::from(18)), + ); + let root = + put_account_with_code(&root, store.as_ref(), &contract, 1, U256::ZERO, &code); + let block_store = + Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let executor = RskExecutor::new(RskHardforkConfig::testnet(), block_store); + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(0), + gas_limit: U256::from(1_000_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(height), &[(tx, sender)], &root, store) + .expect("block executes"); + (result.tx_results[0].success, result.tx_results[0].gas_used) + }; + assert_eq!( + run(2_060_499), + (false, 1_000_000), + "the contract fails with all its gas" + ); + let (succeeded, used) = run(2_060_500); + assert!(succeeded, "from iris300 the contract goes on"); + assert!( + used < 150_000, + "from iris300 it pays what it used (got {used})" + ); + } + + /// The Bridge's Bitcoin store is written in bitcoinj's legacy format before + /// RSKIP454 and in its 32-byte one from it + /// (`RepositoryBtcBlockStoreWithCache.serializeBlock`), on both of the + /// executor's paths to the Bridge: the EVM's, which a paid call takes from + /// RSKIP136, and the one a call takes before it (mainnet #3,397). `receiveHeaders`, + /// called by a member of mainnet's genesis federation, seeds an empty store + /// in the format its call is given; the head's cell is 96 bytes in the + /// legacy format and 116 in the other. + #[test] + fn the_btc_store_is_written_in_the_blocks_format_on_both_paths_to_the_bridge() { + use crate::abi::encode::{args, Arg}; + let config = crate::bridge::constants::BridgeConstants::mainnet(); + let federator = crate::bridge::federation::rsk_address_from_public_key( + &crate::bridge::peg::genesis_federation_keys(&config)[0], + ) + .expect("an address"); + let receive_headers = [ + crate::abi::selector("receiveHeaders(bytes[])").to_vec(), + args(&[Arg::BytesArray(&[&[0u8; 80]])]), + ] + .concat(); + let head_cell = |number: u64| -> usize { + let store = Arc::new(MemoryTrieStore::new()); + let root = put_account( + &TrieNode::empty(), + store.as_ref(), + &federator, + 0, + U256::from(10u64).pow(U256::from(18)), + ); + 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(1_000_000), + to: Bytes::copy_from_slice(crate::precompiles::BRIDGE_ADDR.as_slice()), + value: U256::ZERO, + input: Bytes::from(receive_headers.clone()), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + let result = executor + .execute_block(&dummy_header(number), &[(tx, federator)], &root, store) + .expect("block executes"); + let head = crate::bridge::storage::bridge_storage_key("blockStoreChainHead"); + result + .markers + .raw_storage + .iter() + .rev() + .find(|(address, key, _)| { + *address == crate::precompiles::BRIDGE_ADDR && *key == head + }) + .and_then(|(_, _, value)| value.as_ref()) + .map_or(0, Vec::len) + }; + // Mainnet's lovell700, which brings RSKIP454. + let lovell700 = 7_338_024; + assert_eq!(head_cell(lovell700 - 1), 96, "the EVM's path, legacy"); + assert_eq!(head_cell(lovell700), 116, "the EVM's path, 32 bytes"); + assert_eq!(head_cell(3_000), 96, "before RSKIP136, legacy"); + } + /// Runs the same contract on both sides of testnet iris300 (#2,060,500) /// and returns the gas each transaction used. fn bridge_throw_gas_either_side_of_iris300() -> (u64, u64) { @@ -2477,7 +3232,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 ]; @@ -2537,7 +3292,7 @@ mod tests { 0x60, 0x00, // retSize 0x60, 0x00, // retOffset 0x60, 0x04, // argsSize - 0x60, 0x00, // argsOffset + 0x60, 0x1c, // argsOffset 0x60, 0x00, // value 0x73, // PUSH20 ]; @@ -2773,994 +3528,1260 @@ mod tests { assert!(trace.truncated, "and the trace says it was cut short"); } - #[test] - fn test_rskj_call_stipend_is_charged_to_caller() { - let store = Arc::new(MemoryTrieStore::new()); - let root = TrieNode::empty(); - - let sender = Address::repeat_byte(0xAA); - let contract = Address::repeat_byte(0xCC); - let eoa = Address::repeat_byte(0xBB); - 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(), &eoa, 1, U256::from(5)); - let root = put_account(&root, store.as_ref(), &contract, 1, U256::from(1_000)); - // PUSH1 0 x4 (ret/args) PUSH1 1 (value) PUSH20 eoa PUSH1 0 (gas) CALL STOP - let mut code = vec![ - 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, // out/in offsets+sizes - 0x60, 0x01, // value 1 - 0x73, // PUSH20 - ]; - code.extend_from_slice(eoa.as_slice()); - code.extend_from_slice(&[0x60, 0x00, 0xF1, 0x00]); // gas 0, CALL, STOP - let code_key_bytes = rustock_trie::code_key(&contract); - let root = root.put( - &TrieKeySlice::from_key(&code_key_bytes), - &code, - store.as_ref(), - ); - - let block_store = - Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); - let header = dummy_header(356_285); - - let tx = rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(0), - gas_limit: U256::from(600_000), - to: Bytes::copy_from_slice(contract.as_slice()), - value: U256::ZERO, - input: Bytes::new(), + /// 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, - }; + } + } - let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); - let result = executor - .execute_tx(&header, &tx, sender, &root, store) - .expect("execution succeeds"); - assert!(result.success); + /// 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. 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!( - result.gas_used, 30_721, - "rskj charges the stipend through calleeGas; canonical EVM would yield 28,421" + 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 } - /// Regression for mainnet #7,515,160 (gas used mismatch header=1976590 vs - /// computed=1975990). rskj `GasCost.EXT_CODE_COPY` is a flat 700 at every - /// fork. lovell700 maps to revm's SHANGHAI (>= BERLIN), where revm's stock - /// instruction-table static for EXTCODECOPY is WARM_STORAGE_READ_COST (and - /// the cold-account surcharge is pinned to 0 in make_cfg_env), so without an - /// explicit override EXTCODECOPY would undercharge by 600. `install` pins it - /// back to 700. The contract runs `EXTCODECOPY(self, 0, 0, 0)` (length 0 → - /// no copy/memory-expansion cost), isolating the flat base: - /// 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); + /// 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: 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 root = TrieNode::empty(); - let sender = Address::repeat_byte(0xAA); - let contract = Address::repeat_byte(0xCC); + 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); - let root = put_account(&root, store.as_ref(), &sender, 0, one_rbtc); - let root = put_account(&root, store.as_ref(), &contract, 1, U256::from(1_000)); - // PUSH1 0 (length) PUSH1 0 (codeOffset) PUSH1 0 (destOffset) - // PUSH20 contract EXTCODECOPY STOP - let mut code = vec![0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x73]; - code.extend_from_slice(contract.as_slice()); - code.extend_from_slice(&[0x3c, 0x00]); // EXTCODECOPY, STOP - let code_key_bytes = rustock_trie::code_key(&contract); - let root = root.put( - &TrieKeySlice::from_key(&code_key_bytes), - &code, + // 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 block_store = - Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); - let header = dummy_header(7_338_024); - let tx = rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(0), - gas_limit: U256::from(600_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 executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); - let result = executor - .execute_tx(&header, &tx, sender, &root, store) - .expect("execution succeeds"); - assert!(result.success); + 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!( - result.gas_used, 21_712, - "EXTCODECOPY must charge the flat rskj base of 700 at lovell700/SHANGHAI" + 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" ); - } - - /// Regression for mainnet block #1713 (gas used mismatch 26656 vs 24556): - /// cfg.spec alone leaves the latest-fork gas table in place; the params - /// must be derived per spec. RSK uses Petersburg-style SSTORE metering at - /// EVERY fork (rskj's `GasCost` constants are fork-independent), so - /// make_cfg_env pins SSTORE to the rskj values regardless of SpecId — the - /// 5000 reset cost is fully carried by the static charge and the EIP-2200 - /// net-metering split (static 800) is overridden away. - #[test] - fn test_cfg_env_gas_params_follow_spec() { - let byz = make_cfg_env(SpecId::BYZANTIUM, 30, false); - assert_eq!(byz.gas_params.sstore_static_gas(), 5000); - assert_eq!(byz.gas_params.sstore_reset_without_cold_load_cost(), 0); - assert_eq!(byz.gas_params.sstore_set_without_load_cost(), 15_000); - - // Istanbul/Shanghai (arrowhead600+) keep the SAME rskj Petersburg SSTORE - // metering: EIP-2200's static=800 split is pinned back to 5000 / 0. - let ist = make_cfg_env(SpecId::ISTANBUL, 30, false); - assert_eq!(ist.gas_params.sstore_static_gas(), 5000); - assert_eq!(ist.gas_params.sstore_reset_without_cold_load_cost(), 0); - assert_eq!(ist.gas_params.sstore_set_without_load_cost(), 15_000); - } + let telemetry = + assert_estimate_run_agrees(&executor, &header, &call, sender, &root, &store); + assert!(telemetry.call_with_value, "the refused call carried value"); - /// Regression for lovell700 activation block #7,338,024 (gas used mismatch - /// 62749 vs 58749): RSK never adopted EIP-2929 warm/cold access lists, so - /// SLOAD/BALANCE/EXTCODE*/CALL have no cold-access surcharge at any fork. - /// lovell700 maps to SHANGHAI (>= BERLIN), where revm would add the cold - /// surcharge read from these gas params, so make_cfg_env zeros them. They - /// are already 0 for every spec < BERLIN, so this is a no-op pre-lovell. - #[test] - fn test_cfg_env_no_eip2929_cold_access_cost() { - for spec in [SpecId::ISTANBUL, SpecId::SHANGHAI] { - let cfg = make_cfg_env(spec, 30, false); - assert_eq!(cfg.gas_params.cold_account_additional_cost(), 0, "{spec:?}"); - assert_eq!(cfg.gas_params.cold_storage_additional_cost(), 0, "{spec:?}"); - assert_eq!(cfg.gas_params.cold_storage_cost(), 0, "{spec:?}"); - assert_eq!(cfg.gas_params.warm_storage_read_cost(), 0, "{spec:?}"); - // SELFDESTRUCT cold-beneficiary cost must be 0 (RSK charges no - // warm/cold) — #7,403,649 charged a cold SELFDESTRUCT 100 too much. - assert_eq!(cfg.gas_params.selfdestruct_cold_cost(), 0, "{spec:?}"); + // 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); } - /// Regression for #7,388,594 (gas used mismatch 2663446 vs 2639446 — a - /// contract-creation tx whose SELFDESTRUCT earned no refund). RSK keeps the - /// 24000 SELFDESTRUCT refund (GasCost.SUICIDE_REFUND) at every fork; EIP-3529 - /// (London) zeroed it and lovell700 maps to SHANGHAI (>= LONDON), so it must - /// be pinned back. Equals the pre-London default, so a no-op pre-lovell. + /// 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. #[test] - fn test_cfg_env_selfdestruct_refund_kept() { - for spec in [SpecId::ISTANBUL, SpecId::SHANGHAI] { - let cfg = make_cfg_env(spec, 30, false); - assert_eq!(cfg.gas_params.selfdestruct_refund(), 24_000, "{spec:?}"); - } - } + 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); - /// rskj `TransactionExecutor.create()`: a CREATE *transaction* carrying - /// empty data creates the account "without code nor storage. It doesn't - /// even call setupContract()". revm still flags it created-locally, so we - /// must record the created address in `skip_created` to suppress the - /// storage-prefix marker — which requires computing the address. The - /// stub used to return None, leaving 13 spurious markers at #1,591,000. - /// Ground truth: the canonical CREATE address vector (sender - /// 0x6ac7…dbf0, nonce 0 → 0xcd23…cd8d). - #[test] - fn extract_created_address_matches_create_scheme() { - let sender: Address = "0x6ac7ea33f8831ea9dcc53393aaa88b25a785dbf0" - .parse() - .unwrap(); - let create_tx = rustock_core::Transaction { - nonce: 0, - gas_price: U256::ZERO, - gas_limit: U256::from(21_000), - to: Bytes::new(), - value: U256::ZERO, - input: Bytes::new(), - v: 0, - r: U256::ZERO, - s: U256::ZERO, - cached_rlp: None, - }; - assert_eq!( - RskExecutor::extract_created_address(&create_tx, sender, &true), - Some("0xcd234a471b72ba2f1ccf0a70fcaba648a5eecd8d".parse().unwrap()), + // 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, ); - // A reverted CREATE tx leaves no account. - assert_eq!( - RskExecutor::extract_created_address(&create_tx, sender, &false), - None, + let root = crate::state::apply_state_changes( + &root, + store.as_ref(), + &deployed.state_changes, + &deployed.markers, ); - // A call (non-empty `to`) is never a creation. - let call_tx = rustock_core::Transaction { - to: Bytes::copy_from_slice(sender.as_slice()), - ..create_tx - }; + + 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!( - RskExecutor::extract_created_address(&call_tx, sender, &true), - None, + 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); } - /// Regression for mainnet #671,450: the feePerKb election must apply the - /// voted value once a MAJORITY of authorizers votes it (the migration tx - /// fee at #667,434 depends on the 30,000 sat/kB vote applied at - /// #632,051). The election storage round-trip previously decoded the - /// RLP-encoded coin as raw bytes, so votes never matched across blocks. + /// 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. #[test] - fn test_fee_per_kb_vote_applies_at_majority() { + fn a_trace_or_an_estimate_drops_a_nested_constructors_refund() { let store = Arc::new(MemoryTrieStore::new()); - let root = TrieNode::empty(); - - // Mainnet feePerKb authorizer RSK addresses. - let voter1 = Address::from_slice( - &alloy_primitives::hex::decode("a02db0ed94a5894bc6f9079bb9a2d93ada1917f3").unwrap(), - ); - let voter2 = Address::from_slice( - &alloy_primitives::hex::decode("180a7edda4e640ea5a3e495e17a1efad260c39e9").unwrap(), - ); + let sender = Address::repeat_byte(0xAB); let one_rbtc = U256::from(10u64).pow(U256::from(18)); - 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 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); - let vote_tx = |nonce: u64| { - let mut input = vec![0x04, 0x61, 0x31, 0x3e]; // voteFeePerKbChange(int256) - let mut value = [0u8; 32]; - value[30..].copy_from_slice(&30_000u16.to_be_bytes()); - input.extend_from_slice(&value); - rustock_core::Transaction { - nonce, - gas_price: U256::from(0), - gas_limit: U256::from(100_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, - } - }; - - // Vote 1 in one block, vote 2 in the NEXT block: the election must - // round-trip through the trie between them. - let header1 = dummy_header(632_000); - let r1 = executor - .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 header2 = dummy_header(632_051); - let r2 = executor - .execute_block(&header2, &[(vote_tx(0), voter2)], &root2, store.clone()) - .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, + // 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 fee_write = r2 - .markers - .raw_storage - .iter() - .rev() - .find(|(addr, key, _)| *addr == crate::precompiles::BRIDGE_ADDR && *key == fee_key) - .expect("feePerKb entry written"); - assert_eq!( - fee_write.2.as_deref(), - Some(crate::bridge::storage::rlp_encode_uint(U256::from(30_000)).as_slice()), - "majority vote stores the new feePerKb as rskj serializeCoin RLP" + 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); } - /// Regression for mainnet #892,228: rskj has no EIP-161 — contracts - /// created during execution start with nonce 0, and addresses they later - /// derive via CREATE shift by one if the nonce is seeded to 1 (revm's - /// SPURIOUS_DRAGON default from the per-era CfgEnv spec). The dividend - /// contract at 0xcf0e5881… created payout wallets at - /// keccak(rlp([contract, nonce])) and every recipient came out one nonce - /// ahead of rskj's (real first wallet d82ac9f5… = nonce 0). + /// 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. #[test] - fn test_created_contract_starts_with_nonce_zero() { + fn a_trace_or_an_estimate_stops_a_precompiles_caller_before_rskip197() { 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 executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); - - // Runtime code: CREATE(0,0,0); SSTORE(0, created); STOP. - let runtime: Vec = vec![ - 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0xf0, 0x60, 0x00, 0x55, 0x00, - ]; - // Init code: CODECOPY the runtime and RETURN it. - let mut init = vec![ - 0x60, 0x0b, 0x60, 0x0c, 0x60, 0x00, 0x39, 0x60, 0x0b, 0x60, 0x00, 0xf3, + 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, ]; - init.extend_from_slice(&runtime); + 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 deploy = rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(0), - gas_limit: U256::from(1_000_000), - to: Bytes::new(), - value: U256::ZERO, - input: Bytes::from(init), - v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, - }; - let header1 = dummy_header(892_000); - let r1 = executor - .execute_block(&header1, &[(deploy, sender)], &root, store.clone()) - .expect("deploy block"); - assert!(r1.tx_results[0].success); - let deployed = sender.create(0); // deploy tx from sender nonce 0 - let d_acct = r1.state_changes.get(&deployed).expect("deployed account"); + 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!( - d_acct.info.nonce, 0, - "created contract must start at nonce 0 (no EIP-161 in rskj)" + U256::from_be_slice(&block.tx_results[0].output), + U256::ZERO, + "the outer contract's CALL pushes 0" ); - let root2 = - crate::state::apply_state_changes(&root, store.as_ref(), &r1.state_changes, &r1.markers); - - let call = rustock_core::Transaction { - nonce: 1, - gas_price: U256::from(0), - gas_limit: U256::from(1_000_000), - 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, - }; - let header2 = dummy_header(892_001); - let r2 = executor - .execute_block(&header2, &[(call, sender)], &root2, store.clone()) - .expect("create block"); - assert!(r2.tx_results[0].success); + assert_estimate_run_agrees(&executor, &header, &to_outer, sender, &root, &store); - // The child must derive from nonce 0: keccak(rlp([deployed, 0])). - let expected_child = deployed.create(0); - let slot0 = r2 - .state_changes - .get(&deployed) - .expect("contract state") - .storage - .get(&U256::ZERO) - .expect("slot 0 written"); + // 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!( - Address::from_slice(&slot0.present_value.to_be_bytes::<32>()[12..]), - expected_child, - "CREATE inside a freshly deployed contract must use nonce 0" + (traced.gas_used, traced.success), + (plain.gas_used, plain.success) + ); + assert_eq!( + trace.struct_logs.last().map(|step| step.op.as_str()), + Some("CALL") ); } - /// Regression for mainnet #3,650,574: rustock pins `block.beneficiary` to - /// REMASC_ADDR so revm routes gas fees to the REMASC contract, but rskj's - /// `OpCode.COINBASE` returns the block's *real* miner (`header.getCoinbase`). - /// A token contract that minted to `block.coinbase` produced a Transfer to - /// REMASC (0x…01000008) instead of the miner — diverging both the state and - /// receipts roots. COINBASE must return `header.beneficiary`, not REMASC. + /// 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, where revm's loop keeps it. A contract + /// seeds the Bridge's BTC store through a getter and reverts. #[test] - fn test_coinbase_returns_real_miner_not_remasc() { + 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 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 executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); - - // Init code: COINBASE; SSTORE(0, coinbase); RETURN empty runtime. - let init: Vec = vec![ - 0x41, // COINBASE - 0x60, 0x00, // PUSH1 0 - 0x55, // SSTORE - 0x60, 0x00, 0x60, 0x00, 0xf3, // PUSH1 0 PUSH1 0 RETURN - ]; - - let deploy = rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(0), - gas_limit: U256::from(1_000_000), - to: Bytes::new(), - value: U256::ZERO, - input: Bytes::from(init), - 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 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, ); - header.beneficiary = miner; + 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 r1 = executor - .execute_block(&header, &[(deploy, sender)], &root, store.clone()) - .expect("deploy block"); - assert!(r1.tx_results[0].success); - let deployed = sender.create(0); - let slot0 = r1 - .state_changes - .get(&deployed) - .expect("contract state") - .storage - .get(&U256::ZERO) - .expect("slot 0 written"); - assert_eq!( - Address::from_slice(&slot0.present_value.to_be_bytes::<32>()[12..]), - miner, - "COINBASE must return the real miner, not REMASC_ADDR" + 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); } - /// Regression for mainnet #1,613,128: post-RSKIP125 (wasabi) an internal - /// CREATE/CREATE2 opcode bumps the created contract's nonce to 1 (rskj - /// Program.createContract). A top-level CREATE *transaction* still keeps - /// nonce 0 (TransactionExecutor.create never bumps). The transferAndCall - /// callback there created an empty contract rskj stored as nonce 1 - /// (`c20100`) while rustock, zeroing every created nonce, stored `c28000`. + /// 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 test_internal_create_nonce_one_post_rskip125() { + 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 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 executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + 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); - // Runtime: CREATE(0,0,0) (empty init → no-code child); SSTORE(0, child). - let runtime: Vec = vec![ - 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0xf0, 0x60, 0x00, 0x55, 0x00, - ]; - let mut init = vec![ - 0x60, 0x0b, 0x60, 0x0c, 0x60, 0x00, 0x39, 0x60, 0x0b, 0x60, 0x00, 0xf3, - ]; - init.extend_from_slice(&runtime); + // 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)); - // Deploy the creator via a top-level CREATE tx (post-wasabi). - let deploy = rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(0), - gas_limit: U256::from(1_000_000), - to: Bytes::new(), - value: U256::ZERO, - input: Bytes::from(init), - v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, + // 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); + } + + /// Before RSKIP174 (iris300) rskj's `create()` writes a creation's + /// account over one already at its address, for a local call and an + /// estimate as for a block's transaction (`createAccount`'s carry-over + /// is `RSKIP174 && isExist`, TransactionExecutor.java:403): a contract + /// that reads its own balance as it is created holds only the 7 it was + /// sent. From iris300 the 1,000 already there is carried over. + #[test] + fn before_rskip174_every_run_of_a_creation_holds_only_what_it_was_sent() { + let sender = Address::repeat_byte(0x5F); + let store = Arc::new(MemoryTrieStore::new()); + 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(), + &sender.create(0), + 0, + U256::from(1_000), + ); + // ADDRESS BALANCE, returned as the contract's code. + let init = vec![0x30, 0x31, 0x60, 0x00, 0x52, 0x60, 0x20, 0x60, 0x00, 0xf3]; + let tx = unsigned_tx(0, None, 7, 1_000_000, init); + let executor = mainnet_executor(); + for (number, holds) in [(3_614_799u64, 7u64), (3_614_800, 1_007)] { + let header = dummy_header(number); + let holds = U256::from(holds); + let local = executor + .execute_local_call(&header, &tx, sender, &root, store.clone()) + .unwrap(); + let read = U256::from_be_slice(&local.output); + assert_eq!(read, holds, "a local call at #{number}"); + let (traced, _) = executor + .execute_tx_traced(&header, &tx, sender, &root, store.clone(), true, 0) + .unwrap(); + let read = U256::from_be_slice(&traced.output); + assert_eq!(read, holds, "a trace at #{number}"); + let (estimated, _) = executor + .execute_tx_for_estimate(&header, &tx, sender, &root, store.clone(), true) + .unwrap(); + assert_eq!( + U256::from_be_slice(&estimated.output), + holds, + "an estimate at #{number}" + ); + } + } + + /// rskj's BLOCKHASH answers the block at that height among the run + /// block's own ancestors: `Program.getBlockHash` asks + /// `IndexedBlockStore.getBlockHashByNumber` with the block's parent, which + /// walks back from it (Program.java:1133-1136). A block run beside the + /// main chain reads its fork's blocks, and the main chain's below where + /// the fork leaves it, in a block's run, a local call, a trace and an + /// estimate alike. Here the fork is longer than the main chain, so the + /// main chain has no block at its last height. + #[test] + fn blockhash_answers_from_the_run_blocks_own_ancestors() { + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + // Blocks 0 to 3 of the main chain, and blocks 2 to 4 of a fork from + // block 1, each with a beneficiary of its own. + let child = |parent: &Header, beneficiary: u8| Header { + parent_hash: parent.hash(), + beneficiary: Address::repeat_byte(beneficiary), + ..dummy_header(parent.number + 1) }; - let header1 = dummy_header(1_591_500); - let r1 = executor - .execute_block(&header1, &[(deploy, sender)], &root, store.clone()) - .expect("deploy block"); - assert!(r1.tx_results[0].success); - let deployed = sender.create(0); + let mut main = vec![dummy_header(0)]; + for _ in 1..4 { + let next = child(main.last().unwrap(), 0x01); + main.push(next); + } + let mut fork = vec![child(&main[1], 0xF0)]; + for _ in 3..5 { + let next = child(fork.last().unwrap(), 0xF0); + fork.push(next); + } + for header in main.iter().chain(&fork) { + block_store.put_header(header).unwrap(); + } + for header in &main { + block_store + .put_canonical_hash(header.number, header.hash()) + .unwrap(); + } + // CALLDATALOAD(0) BLOCKHASH, returned. + let code = [ + 0x60, 0x00, 0x35, 0x40, 0x60, 0x00, 0x52, 0x60, 0x20, 0x60, 0x00, 0xf3, + ]; + let store = Arc::new(MemoryTrieStore::new()); + let (sender, contract) = (Address::repeat_byte(0xAA), Address::repeat_byte(0xBB)); + 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_with_code(&root, store.as_ref(), &contract, 1, U256::ZERO, &code); + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + // What BLOCKHASH(number) answers in the block after `parent`, each way. + let ask = |parent: &Header, number: u64| -> Vec { + let header = child(parent, 0x02); + let input = U256::from(number).to_be_bytes::<32>().to_vec(); + let tx = unsigned_tx(0, Some(contract), 0, 100_000, input); + let block = executor + .execute_block(&header, &[(tx.clone(), sender)], &root, store.clone()) + .unwrap(); + let local = executor + .execute_local_call(&header, &tx, sender, &root, store.clone()) + .unwrap(); + let (traced, _) = executor + .execute_tx_traced(&header, &tx, sender, &root, store.clone(), true, 0) + .unwrap(); + let (estimated, _) = executor + .execute_tx_for_estimate(&header, &tx, sender, &root, store.clone(), true) + .unwrap(); + [ + &block.tx_results[0].output, + &local.output, + &traced.output, + &estimated.output, + ] + .map(|output| B256::from_slice(output)) + .to_vec() + }; + for number in 0..4 { + let on_main = main[number as usize].hash(); + assert_eq!( + ask(&main[3], number), + vec![on_main; 4], + "block {number} on the main chain" + ); + } + let (one, two, three, four) = ( + main[1].hash(), + fork[0].hash(), + fork[1].hash(), + fork[2].hash(), + ); assert_eq!( - r1.state_changes.get(&deployed).expect("creator").info.nonce, 0, - "a top-level CREATE tx keeps nonce 0 even post-RSKIP125" + ask(&fork[2], 4), + vec![four; 4], + "the parent, where the main chain has none" ); - let root2 = - crate::state::apply_state_changes(&root, store.as_ref(), &r1.state_changes, &r1.markers); + assert_eq!( + ask(&fork[2], 3), + vec![three; 4], + "the fork's, past the main chain's" + ); + assert_eq!(ask(&fork[2], 2), vec![two; 4], "the fork's first"); + assert_eq!( + ask(&fork[2], 1), + vec![one; 4], + "where the fork leaves the main chain" + ); + assert_eq!(ask(&fork[2], 0), vec![main[0].hash(); 4], "below it"); + } - // Call the creator → it runs an internal CREATE. - let call = rustock_core::Transaction { - nonce: 1, - gas_price: U256::from(0), - gas_limit: U256::from(1_000_000), - 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, - }; - let header2 = dummy_header(1_591_501); - let r2 = executor - .execute_block(&header2, &[(call, sender)], &root2, store.clone()) - .expect("create block"); - assert!(r2.tx_results[0].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)); + } - let child = deployed.create(0); + /// 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!( - r2.state_changes.get(&child).expect("child contract").info.nonce, 1, - "internal CREATE post-RSKIP125 bumps the created contract nonce to 1" + B256::from_slice(&local.output), + revm::primitives::KECCAK_EMPTY ); } - /// Wasabi enables the Constantinople shift opcodes (RSKIP120) and CREATE2 - /// (RSKIP125) but not EXTCODEHASH (RSKIP140, papyrus). The spec must be - /// PETERSBURG from wasabi (not BYZANTIUM, which lacks shifts). + /// 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 test_wasabi_spec_enables_shift_opcodes() { - let cfg = RskHardforkConfig::mainnet(); - assert_eq!(cfg.spec_id(729_000), SpecId::BYZANTIUM, "orchid"); - assert_eq!(cfg.spec_id(1_590_999), SpecId::BYZANTIUM, "pre-wasabi"); - assert_eq!(cfg.spec_id(1_591_000), SpecId::PETERSBURG, "wasabi"); - assert!(!cfg.has_rskip140(1_591_000), "no EXTCODEHASH at wasabi"); - assert!(cfg.has_rskip140(2_392_700), "EXTCODEHASH from papyrus"); + 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]); } - /// Regression for mainnet #1,613,128..#1,661,216 era: a contract whose - /// runtime uses SHR (solc >=0.5 selector dispatch) must execute post-wasabi - /// instead of hitting an invalid opcode that consumes the whole gas limit. - /// #1,661,216's setCompleted(uint256) call cost 42,023 gas in rskj but - /// rustock charged the full 6,721,975 limit (SHR invalid under BYZANTIUM). #[test] - fn test_shr_opcode_executes_post_wasabi() { + fn test_rskj_call_stipend_is_charged_to_caller() { let store = Arc::new(MemoryTrieStore::new()); let root = TrieNode::empty(); + let sender = Address::repeat_byte(0xAA); + let contract = Address::repeat_byte(0xCC); + let eoa = Address::repeat_byte(0xBB); 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(), &eoa, 1, U256::from(5)); + let root = put_account(&root, store.as_ref(), &contract, 1, U256::from(1_000)); + // PUSH1 0 x4 (ret/args) PUSH1 1 (value) PUSH20 eoa PUSH1 0 (gas) CALL STOP + let mut code = vec![ + 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, // out/in offsets+sizes + 0x60, 0x01, // value 1 + 0x73, // PUSH20 + ]; + code.extend_from_slice(eoa.as_slice()); + code.extend_from_slice(&[0x60, 0x00, 0xF1, 0x00]); // gas 0, CALL, STOP + let code_key_bytes = rustock_trie::code_key(&contract); + let root = root.put( + &TrieKeySlice::from_key(&code_key_bytes), + &code, + store.as_ref(), + ); + 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 mut init = vec![ - 0x60, 0x09, 0x60, 0x0c, 0x60, 0x00, 0x39, 0x60, 0x09, 0x60, 0x00, 0xf3, - ]; - init.extend_from_slice(&runtime); + let header = dummy_header(356_285); - let deploy = rustock_core::Transaction { + let tx = rustock_core::Transaction { nonce: 0, gas_price: U256::from(0), - gas_limit: U256::from(1_000_000), - to: Bytes::new(), - value: U256::ZERO, - input: Bytes::from(init), - v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, - }; - let header1 = dummy_header(1_591_500); - let r1 = executor - .execute_block(&header1, &[(deploy, sender)], &root, store.clone()) - .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 call = rustock_core::Transaction { - nonce: 1, - gas_price: U256::from(0), - gas_limit: U256::from(1_000_000), - to: Bytes::copy_from_slice(deployed.as_slice()), + gas_limit: U256::from(600_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, + 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].gas_used < 1_000_000, - "SHR call must not consume the whole gas limit (invalid opcode)" + + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + let result = executor + .execute_tx(&header, &tx, sender, &root, store) + .expect("execution succeeds"); + assert!(result.success); + assert_eq!( + result.gas_used, 30_721, + "rskj charges the stipend through calleeGas; canonical EVM would yield 28,421" ); - let slot0 = r2 - .state_changes - .get(&deployed) - .expect("contract") - .storage - .get(&U256::ZERO) - .expect("slot 0"); - assert_eq!(slot0.present_value, U256::from(4), "4 >> 0 == 4"); } - /// Regression for mainnet #6,362,186 tx[3] (gas 1,197,343 vs 1,182,343, - /// +15,000): rskj `Program.finalizeContractCreation` propagates only logInfos - /// and deleteAccounts from a constructor to the parent result — NOT - /// futureRefund (unlike the CALL path's `ProgramResult.merge`). So a gas - /// refund accrued inside a nested CREATE/CREATE2 constructor is DISCARDED. - /// A constructor that SETs then CLEARs a storage slot must therefore NOT - /// earn the 15,000 SSTORE-clear refund when created via the CREATE opcode: - /// compared with a set-only constructor, clearing costs MORE (the extra - /// 5,000 CLEAR_SSTORE), not 10,000 less. + /// Regression for mainnet #7,515,160 (gas used mismatch header=1976590 vs + /// computed=1975990). rskj `GasCost.EXT_CODE_COPY` is a flat 700 at every + /// fork. lovell700 maps to revm's SHANGHAI (>= BERLIN), where revm's stock + /// instruction-table static for EXTCODECOPY is WARM_STORAGE_READ_COST (and + /// the cold-account surcharge is pinned to 0 in make_cfg_env), so without an + /// explicit override EXTCODECOPY would undercharge by 600. `install` pins it + /// back to 700. The contract runs `EXTCODECOPY(self, 0, 0, 0)` (length 0 → + /// no copy/memory-expansion cost), isolating the flat base: + /// 21,000 intrinsic + 9 (3 PUSH1) + 3 (PUSH20) + 700 EXTCODECOPY = 21,712. #[test] - fn nested_create_constructor_refund_is_dropped_rskj() { - // child init: SET slot0=1, optionally CLEAR it, then RETURN empty code. - let child_init = |clear: bool| -> Vec { - let mut c = vec![0x60, 0x01, 0x60, 0x00, 0x55]; // PUSH1 1 PUSH1 0 SSTORE - if clear { - c.extend_from_slice(&[0x60, 0x00, 0x60, 0x00, 0x55]); // clear slot0 - } - c.extend_from_slice(&[0x60, 0x00, 0x60, 0x00, 0xf3]); // RETURN empty - c - }; - // factory runtime: MSTORE the child init (right-aligned), then CREATE it. - let factory_runtime = |init: &[u8]| -> Vec { - let n = init.len(); - assert!((1..=32).contains(&n)); - let off = (32 - n) as u8; - let mut r = vec![0x5f + n as u8]; // PUSH{n} - r.extend_from_slice(init); - r.extend_from_slice(&[0x60, 0x00, 0x52]); // PUSH1 0 MSTORE - r.extend_from_slice(&[0x60, n as u8]); // PUSH1 size - r.extend_from_slice(&[0x60, off]); // PUSH1 offset - r.extend_from_slice(&[0x60, 0x00, 0xf0, 0x00]); // PUSH1 0 (value) CREATE STOP - r - }; - // deploy wrapper: CODECOPY the runtime (after the 12-byte prefix) + RETURN. - let deploy_init = |runtime: &[u8]| -> Vec { - let l = runtime.len() as u8; - let mut d = vec![ - 0x60, l, 0x60, 0x0c, 0x60, 0x00, 0x39, 0x60, l, 0x60, 0x00, 0xf3, - ]; - d.extend_from_slice(runtime); - d - }; - - // Run the whole deploy+call sequence and return the call's gas_used. - let run = |clear: bool| -> u64 { - let store = Arc::new(MemoryTrieStore::new()); - let root = TrieNode::empty(); - let sender = Address::repeat_byte(0xAB); - 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 executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); - - let factory = deploy_init(&factory_runtime(&child_init(clear))); - let deploy = rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(0), - gas_limit: U256::from(2_000_000), - to: Bytes::new(), - value: U256::ZERO, - input: Bytes::from(factory), - v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, - }; - let r1 = executor - .execute_block(&dummy_header(6_362_186), &[(deploy, sender)], &root, store.clone()) - .expect("deploy block"); - assert!(r1.tx_results[0].success, "factory deploy failed"); - let factory_addr = sender.create(0); - let root2 = crate::state::apply_state_changes( - &root, store.as_ref(), &r1.state_changes, &r1.markers, - ); - let call = rustock_core::Transaction { - nonce: 1, - gas_price: U256::from(0), - gas_limit: U256::from(2_000_000), - to: Bytes::copy_from_slice(factory_addr.as_slice()), - value: U256::ZERO, - input: Bytes::new(), - v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, - }; - let r2 = executor - .execute_block(&dummy_header(6_362_187), &[(call, sender)], &root2, store.clone()) - .expect("call block"); - assert!(r2.tx_results[0].success, "factory call (CREATE) failed"); - r2.tx_results[0].gas_used - }; - - let gas_clear = run(true); - let gas_noclear = run(false); - // With the nested-create refund dropped, clearing the slot adds the - // 5,000 CLEAR_SSTORE cost and earns NO 15,000 refund, so it costs MORE. - // (Were the refund wrongly propagated, gas_clear would be ~10,000 LESS.) - assert!( - gas_clear > gas_noclear, - "nested-create constructor clear must not earn a refund: clear={gas_clear} noclear={gas_noclear}" + fn test_extcodecopy_flat_700_at_lovell700() { + assert_eq!( + RskHardforkConfig::mainnet().spec_id(7_338_024), + SpecId::SHANGHAI ); - } - /// Regression for mainnet #3,631,998 tx[1] (gas used 27,540 vs 27,529, +11, - /// divergent revert branch): rskj `VM.doEXTCODEHASH` (`getCodeHashStandard`) - /// keys on TRIE EXISTENCE, not EIP-161 emptiness. An account that exists in - /// the trie but is "empty" (nonce 0, balance 0, no code) must hash to - /// `keccak256("")`, while an account absent from the trie hashes to 0. revm's - /// stock EXTCODEHASH returns 0 for both (it uses `AccountInfo::is_empty()`), - /// so the existing-empty case forked. Ground truth: a USDT-on-ETH address - /// (no code on RSK) that a prior 0-value call had created hashed to - /// `keccak256("")` in rskj, sending the proxy down a different branch. - #[test] - fn test_extcodehash_existing_empty_account_is_keccak_empty() { let store = Arc::new(MemoryTrieStore::new()); - let mut root = TrieNode::empty(); - let sender = Address::repeat_byte(0xAC); + let root = TrieNode::empty(); + let sender = Address::repeat_byte(0xAA); + let contract = Address::repeat_byte(0xCC); let one_rbtc = U256::from(10u64).pow(U256::from(18)); - root = put_account(&root, store.as_ref(), &sender, 0, one_rbtc); - - // An existing-but-empty account (nonce 0, balance 0, no code): present - // in the trie, yet EIP-161-empty. rskj -> keccak256(""); revm stock -> 0. - let existing_empty = Address::repeat_byte(0xE1); - root = put_account(&root, store.as_ref(), &existing_empty, 0, U256::ZERO); - // An address never placed in the trie: rskj & rustock both -> 0. - let absent = Address::repeat_byte(0xE2); + let root = put_account(&root, store.as_ref(), &sender, 0, one_rbtc); + let root = put_account(&root, store.as_ref(), &contract, 1, U256::from(1_000)); + // PUSH1 0 (length) PUSH1 0 (codeOffset) PUSH1 0 (destOffset) + // PUSH20 contract EXTCODECOPY STOP + let mut code = vec![0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x73]; + code.extend_from_slice(contract.as_slice()); + code.extend_from_slice(&[0x3c, 0x00]); // EXTCODECOPY, STOP + let code_key_bytes = rustock_trie::code_key(&contract); + let root = root.put( + &TrieKeySlice::from_key(&code_key_bytes), + &code, + store.as_ref(), + ); let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); - let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); - - // Runtime: EXTCODEHASH(existing_empty) -> slot 0; - // EXTCODEHASH(absent) -> slot 1; STOP. - let mut runtime: Vec = Vec::new(); - runtime.push(0x73); // PUSH20 existing_empty - runtime.extend_from_slice(existing_empty.as_slice()); - runtime.extend_from_slice(&[0x3f, 0x60, 0x00, 0x55]); // EXTCODEHASH; PUSH1 0; SSTORE - runtime.push(0x73); // PUSH20 absent - runtime.extend_from_slice(absent.as_slice()); - runtime.extend_from_slice(&[0x3f, 0x60, 0x01, 0x55]); // EXTCODEHASH; PUSH1 1; SSTORE - runtime.push(0x00); // STOP - - // Init: CODECOPY the runtime to memory and RETURN it. - let rt_len = runtime.len() as u8; - let mut init = vec![ - 0x60, rt_len, // PUSH1 - 0x60, 0x0c, // PUSH1 0x0c (runtime offset in this init) - 0x60, 0x00, // PUSH1 0 - 0x39, // CODECOPY - 0x60, rt_len, // PUSH1 - 0x60, 0x00, // PUSH1 0 - 0xf3, // RETURN - ]; - init.extend_from_slice(&runtime); - - let deploy = rustock_core::Transaction { + let header = dummy_header(7_338_024); + let tx = rustock_core::Transaction { nonce: 0, gas_price: U256::from(0), - gas_limit: U256::from(1_000_000), - to: Bytes::new(), - value: U256::ZERO, - input: Bytes::from(init), - v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, - }; - // papyrus200 (>= 2,392,700) so EXTCODEHASH (RSKIP140) is active. - let header1 = dummy_header(2_392_800); - let r1 = executor - .execute_block(&header1, &[(deploy, sender)], &root, store.clone()) - .expect("deploy block"); - assert!(r1.tx_results[0].success, "deploy must succeed"); - let deployed = sender.create(0); - let root2 = - crate::state::apply_state_changes(&root, store.as_ref(), &r1.state_changes, &r1.markers); - - let call = rustock_core::Transaction { - nonce: 1, - gas_price: U256::from(0), - gas_limit: U256::from(1_000_000), - to: Bytes::copy_from_slice(deployed.as_slice()), + gas_limit: U256::from(600_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, + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, }; - let header2 = dummy_header(2_392_801); - let r2 = executor - .execute_block(&header2, &[(call, sender)], &root2, store.clone()) - .expect("call block"); - assert!(r2.tx_results[0].success, "EXTCODEHASH call must succeed"); - - let storage = &r2.state_changes.get(&deployed).expect("contract").storage; - let slot0 = storage.get(&U256::ZERO).expect("slot 0").present_value; - let slot1 = storage.get(&U256::from(1)).map(|s| s.present_value).unwrap_or(U256::ZERO); - // Existing-but-empty account -> keccak256(""), NOT 0. + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + let result = executor + .execute_tx(&header, &tx, sender, &root, store) + .expect("execution succeeds"); + assert!(result.success); assert_eq!( - B256::from(slot0.to_be_bytes()), - crate::rsk_instructions::KECCAK_EMPTY, - "EXTCODEHASH of an existing-but-empty account must be keccak256(\"\")" + result.gas_used, 21_712, + "EXTCODECOPY must charge the flat rskj base of 700 at lovell700/SHANGHAI" ); - // Absent account -> 0. - assert_eq!(slot1, U256::ZERO, "EXTCODEHASH of an absent account must be 0"); } - /// Regression for mainnet #2,430,894: CHAINID (RSKIP152) and SELFBALANCE - /// (RSKIP151) activate at papyrus200 while the SpecId stays PETERSBURG — - /// revm's spec-checked impls halted NotActivated and the tx consumed its - /// whole gas limit (rskj: success, 235,956 gas). Pre-papyrus both must - /// still be invalid opcodes consuming all frame gas. + /// Regression for mainnet block #1713 (gas used mismatch 26656 vs 24556): + /// cfg.spec alone leaves the latest-fork gas table in place; the params + /// must be derived per spec. RSK uses Petersburg-style SSTORE metering at + /// EVERY fork (rskj's `GasCost` constants are fork-independent), so + /// make_cfg_env pins SSTORE to the rskj values regardless of SpecId — the + /// 5000 reset cost is fully carried by the static charge and the EIP-2200 + /// net-metering split (static 800) is overridden away. #[test] - fn test_chainid_selfbalance_activate_at_papyrus() { - let store = Arc::new(MemoryTrieStore::new()); - let root = TrieNode::empty(); - let sender = Address::repeat_byte(0xAB); - 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 executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + fn test_cfg_env_gas_params_follow_spec() { + let byz = make_cfg_env(SpecId::BYZANTIUM, 30, false); + assert_eq!(byz.gas_params.sstore_static_gas(), 5000); + assert_eq!(byz.gas_params.sstore_reset_without_cold_load_cost(), 0); + assert_eq!(byz.gas_params.sstore_set_without_load_cost(), 15_000); - // Runtime: CHAINID; PUSH1 0; SSTORE; SELFBALANCE; PUSH1 1; SSTORE; STOP - let runtime: Vec = vec![0x46, 0x60, 0x00, 0x55, 0x47, 0x60, 0x01, 0x55, 0x00]; - let mut init = vec![ - 0x60, 0x09, 0x60, 0x0c, 0x60, 0x00, 0x39, 0x60, 0x09, 0x60, 0x00, 0xf3, - ]; - init.extend_from_slice(&runtime); - let deploy = rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(0), - gas_limit: U256::from(1_000_000), - to: Bytes::new(), - value: U256::ZERO, - input: Bytes::from(init), - v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, - }; - let r1 = executor - .execute_block(&dummy_header(2_392_700), &[(deploy, sender)], &root, store.clone()) - .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); + // Istanbul/Shanghai (arrowhead600+) keep the SAME rskj Petersburg SSTORE + // metering: EIP-2200's static=800 split is pinned back to 5000 / 0. + let ist = make_cfg_env(SpecId::ISTANBUL, 30, false); + assert_eq!(ist.gas_params.sstore_static_gas(), 5000); + assert_eq!(ist.gas_params.sstore_reset_without_cold_load_cost(), 0); + assert_eq!(ist.gas_params.sstore_set_without_load_cost(), 15_000); + } - let call = |nonce: u64| rustock_core::Transaction { - nonce, - gas_price: U256::from(0), - gas_limit: U256::from(1_000_000), - to: Bytes::copy_from_slice(deployed.as_slice()), + /// Regression for lovell700 activation block #7,338,024 (gas used mismatch + /// 62749 vs 58749): RSK never adopted EIP-2929 warm/cold access lists, so + /// SLOAD/BALANCE/EXTCODE*/CALL have no cold-access surcharge at any fork. + /// lovell700 maps to SHANGHAI (>= BERLIN), where revm would add the cold + /// surcharge read from these gas params, so make_cfg_env zeros them. They + /// are already 0 for every spec < BERLIN, so this is a no-op pre-lovell. + #[test] + fn test_cfg_env_no_eip2929_cold_access_cost() { + for spec in [SpecId::ISTANBUL, SpecId::SHANGHAI] { + let cfg = make_cfg_env(spec, 30, false); + assert_eq!(cfg.gas_params.cold_account_additional_cost(), 0, "{spec:?}"); + assert_eq!(cfg.gas_params.cold_storage_additional_cost(), 0, "{spec:?}"); + assert_eq!(cfg.gas_params.cold_storage_cost(), 0, "{spec:?}"); + assert_eq!(cfg.gas_params.warm_storage_read_cost(), 0, "{spec:?}"); + // SELFDESTRUCT cold-beneficiary cost must be 0 (RSK charges no + // warm/cold) — #7,403,649 charged a cold SELFDESTRUCT 100 too much. + assert_eq!(cfg.gas_params.selfdestruct_cold_cost(), 0, "{spec:?}"); + } + } + + /// Regression for #7,388,594 (gas used mismatch 2663446 vs 2639446 — a + /// contract-creation tx whose SELFDESTRUCT earned no refund). RSK keeps the + /// 24000 SELFDESTRUCT refund (GasCost.SUICIDE_REFUND) at every fork; EIP-3529 + /// (London) zeroed it and lovell700 maps to SHANGHAI (>= LONDON), so it must + /// be pinned back. Equals the pre-London default, so a no-op pre-lovell. + #[test] + fn test_cfg_env_selfdestruct_refund_kept() { + for spec in [SpecId::ISTANBUL, SpecId::SHANGHAI] { + let cfg = make_cfg_env(spec, 30, false); + assert_eq!(cfg.gas_params.selfdestruct_refund(), 24_000, "{spec:?}"); + } + } + + /// rskj `TransactionExecutor.create()`: a CREATE *transaction* carrying + /// empty data creates the account "without code nor storage. It doesn't + /// even call setupContract()". revm still flags it created-locally, so we + /// must record the created address in `skip_created` to suppress the + /// storage-prefix marker — which requires computing the address. The + /// stub used to return None, leaving 13 spurious markers at #1,591,000. + /// Ground truth: the canonical CREATE address vector (sender + /// 0x6ac7…dbf0, nonce 0 → 0xcd23…cd8d). + #[test] + fn extract_created_address_matches_create_scheme() { + let sender: Address = "0x6ac7ea33f8831ea9dcc53393aaa88b25a785dbf0" + .parse() + .unwrap(); + let create_tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::ZERO, + gas_limit: U256::from(21_000), + to: Bytes::new(), 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, }; - // Post-papyrus: both opcodes execute; CHAINID pushes 30 (RSK mainnet). - let r2 = executor - .execute_block(&dummy_header(2_392_701), &[(call(1), sender)], &root2, store.clone()) - .expect("papyrus call block"); - assert!(r2.tx_results[0].success, "CHAINID/SELFBALANCE valid at papyrus200"); - let storage = &r2.state_changes.get(&deployed).expect("contract").storage; assert_eq!( - storage.get(&U256::ZERO).unwrap().present_value, - U256::from(30), - "CHAINID == 30" + RskExecutor::extract_created_address(&create_tx, sender, &true), + Some( + "0xcd234a471b72ba2f1ccf0a70fcaba648a5eecd8d" + .parse() + .unwrap() + ), ); + // A reverted CREATE tx leaves no account. assert_eq!( - storage.get(&U256::from(1)).unwrap().present_value, - U256::ZERO, - "SELFBALANCE of the fresh contract == 0" + RskExecutor::extract_created_address(&create_tx, sender, &false), + None, + ); + // A call (non-empty `to`) is never a creation. + let call_tx = rustock_core::Transaction { + to: Bytes::copy_from_slice(sender.as_slice()), + ..create_tx + }; + assert_eq!( + RskExecutor::extract_created_address(&call_tx, sender, &true), + None, ); - - // Pre-papyrus: invalid opcode, the whole gas limit is consumed. - let r3 = executor - .execute_block(&dummy_header(2_392_699), &[(call(1), sender)], &root2, store.clone()) - .expect("pre-papyrus call block"); - assert!(!r3.tx_results[0].success, "CHAINID invalid before papyrus200"); - assert_eq!(r3.tx_results[0].gas_used, 1_000_000, "invalid opcode consumes all gas"); } - /// RSKIP197 (iris300): a CALL to a precompile that fails with a non-OOG - /// error is *handled* — the call pushes 0, the surplus forwarded gas is - /// refunded, and execution continues; the failing precompile costs exactly - /// its `getGasForData` requiredGas. Ported from rskj `ProgramTest` - /// (`testCallToPrecompiledAddress_throwPrecompiledContractException`: - /// post-197 `gasUsed == gasCost`, stack pushes 0). - /// - /// Here a deployed contract CALLs BLAKE2F (0x09) with a 213-byte input - /// whose final-block byte is 0x02 (invalid: must be 0 or 1) and `rounds`=1, - /// forwarding 900,000 gas. rskj's `Blake2F.execute` throws - /// `BLAKE2F_ERROR_FINAL_BLOCK_BYTES`; post-197 the call costs only - /// `requiredGas == rounds == 1` (the surplus is refunded), so the whole tx - /// settles far below the forwarded gas. The CALL result (0) is stored at - /// slot 0 to prove the failure was observed without aborting the frame. - /// What a *failing* top-level Bridge call costs, and what its receipt says. - /// - /// Not what one would guess. rskj's precompile path in - /// `TransactionExecutor.call` sets `gasLeftover = txGasLimit - gasUsed` - /// before the try and its catch only calls `result.setException(e)` -- it - /// never zeroes the leftover the way the VM path does for a reverted - /// contract call. The exception is then recorded for FEE purposes but not - /// for RECEIPT purposes, so the transaction: - /// - /// * reports status SUCCESS, - /// * reports `gasUsed = requiredGas + basicTxCost`, well under the limit, - /// * yet charges the sender the ENTIRE gas limit, the surplus to REMASC. - /// - /// That is quirks-frozen-bugs.md §50, reported to the rskj security team in - /// docs/rskj-report-precompile-receipt-status.md, and consensus-frozen. - /// - /// This test exists where it does because the rskj-compat sender path now - /// returns `Err` for inputs rskj throws on, and that error flows through - /// exactly this mechanism. Pinning it here means a change to the quirk - /// cannot silently change what a rejected peg-in costs. + /// Regression for mainnet #671,450: the feePerKb election must apply the + /// voted value once a MAJORITY of authorizers votes it (the migration tx + /// fee at #667,434 depends on the 30,000 sat/kB vote applied at + /// #632,051). The election storage round-trip previously decoded the + /// RLP-encoded coin as raw bytes, so votes never matched across blocks. #[test] - fn a_failing_bridge_call_reports_success_and_spends_only_the_precompile_gas() { + fn test_fee_per_kb_vote_applies_at_majority() { let store = Arc::new(MemoryTrieStore::new()); let root = TrieNode::empty(); - let sender = Address::repeat_byte(0xCE); + + // Mainnet feePerKb authorizer RSK addresses. + let voter1 = Address::from_slice( + &alloy_primitives::hex::decode("a02db0ed94a5894bc6f9079bb9a2d93ada1917f3").unwrap(), + ); + let voter2 = Address::from_slice( + &alloy_primitives::hex::decode("180a7edda4e640ea5a3e495e17a1efad260c39e9").unwrap(), + ); 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()); - // Post-RSKIP136, so this takes revm's ordinary transact path rather than - // the direct-bridge branch that handles the free and pre-136 cases. - let mut header = dummy_header(7_000_000); - header.gas_limit = U256::from(6_800_000); + let root = put_account(&root, store.as_ref(), &voter1, 0, one_rbtc); + let root = put_account(&root, store.as_ref(), &voter2, 0, one_rbtc); - // registerBtcTransaction with a truncated argument list: rejected - // before any work, which is rskj's BridgeIllegalArgumentException. - let mut input = { - use sha3::{Digest, Keccak256}; - let h = Keccak256::digest(b"registerBtcTransaction(bytes,int256,bytes)"); - h[..4].to_vec() - }; - input.extend_from_slice(&[0u8; 32]); // far short of the 96 bytes required + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); - let limit = 500_000u64; - let tx = rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(1), - gas_limit: U256::from(limit), - 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 vote_tx = |nonce: u64| { + let mut input = vec![0x04, 0x61, 0x31, 0x3e]; // voteFeePerKbChange(int256) + let mut value = [0u8; 32]; + value[30..].copy_from_slice(&30_000u16.to_be_bytes()); + input.extend_from_slice(&value); + rustock_core::Transaction { + nonce, + gas_price: U256::from(0), + gas_limit: U256::from(100_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 executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); - let result = executor - .execute_tx(&header, &tx, sender, &root, store) - .expect("a rejected Bridge call fails the transaction, never the block"); - assert!( - result.success, - "the invisible-exception quirk reports SUCCESS even though the call threw" - ); - assert!( - result.gas_used < limit, - "the receipt must report requiredGas + basicTxCost, not the limit: \ - got {} of {}", - result.gas_used, limit + // Vote 1 in one block, vote 2 in the NEXT block: the election must + // round-trip through the trie between them. + let header1 = dummy_header(632_000); + let r1 = executor + .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, ); - assert!( - result.gas_used > 22_000, - "and it must include the Bridge's fixed 22,000: got {}", - result.gas_used + + let header2 = dummy_header(632_051); + let r2 = executor + .execute_block(&header2, &[(vote_tx(0), voter2)], &root2, store.clone()) + .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_write = r2 + .markers + .raw_storage + .iter() + .rev() + .find(|(addr, key, _)| *addr == crate::precompiles::BRIDGE_ADDR && *key == fee_key) + .expect("feePerKb entry written"); + assert_eq!( + fee_write.2.as_deref(), + Some(crate::bridge::storage::rlp_encode_uint(U256::from(30_000)).as_slice()), + "majority vote stores the new feePerKb as rskj serializeCoin RLP" ); } + /// Regression for mainnet #892,228: rskj has no EIP-161 — contracts + /// created during execution start with nonce 0, and addresses they later + /// derive via CREATE shift by one if the nonce is seeded to 1 (revm's + /// SPURIOUS_DRAGON default from the per-era CfgEnv spec). The dividend + /// contract at 0xcf0e5881… created payout wallets at + /// keccak(rlp([contract, nonce])) and every recipient came out one nonce + /// ahead of rskj's (real first wallet d82ac9f5… = nonce 0). #[test] - fn test_rskip197_failing_precompile_refunds_surplus_gas() { + fn test_created_contract_starts_with_nonce_zero() { let store = Arc::new(MemoryTrieStore::new()); let root = TrieNode::empty(); - let sender = Address::repeat_byte(0xCD); + 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 executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); - // Runtime: build a BLAKE2F input (rounds=1, bad final byte), CALL 0x09 - // forwarding 900,000 gas, then SSTORE the CALL result at slot 0. + // Runtime code: CREATE(0,0,0); SSTORE(0, created); STOP. let runtime: Vec = vec![ - 0x60, 0x01, 0x60, 0x03, 0x53, // PUSH1 1; PUSH1 3; MSTORE8 -> mem[3]=1 (rounds=1) - 0x60, 0x02, 0x60, 0xd4, 0x53, // PUSH1 2; PUSH1 212; MSTORE8 -> mem[212]=2 (bad flag) - 0x60, 0x00, // retLength 0 - 0x60, 0x00, // retOffset 0 - 0x60, 0xd5, // argsLength 213 - 0x60, 0x00, // argsOffset 0 - 0x60, 0x00, // value 0 - 0x60, 0x09, // addr 0x09 (blake2f) - 0x62, 0x0d, 0xbb, 0xa0, // PUSH3 900000 (gas) - 0xf1, // CALL - 0x60, 0x00, 0x55, // PUSH1 0; SSTORE -> slot 0 = CALL result - 0x00, // STOP + 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0xf0, 0x60, 0x00, 0x55, 0x00, ]; - // init: copy `runtime` to memory and RETURN it as the deployed code. - let rt_len = runtime.len() as u8; + // Init code: CODECOPY the runtime and RETURN it. let mut init = vec![ - 0x60, rt_len, 0x60, 0x0c, 0x60, 0x00, 0x39, // CODECOPY(0, 12, len) - 0x60, rt_len, 0x60, 0x00, 0xf3, // RETURN(0, len) + 0x60, 0x0b, 0x60, 0x0c, 0x60, 0x00, 0x39, 0x60, 0x0b, 0x60, 0x00, 0xf3, ]; init.extend_from_slice(&runtime); + let deploy = rustock_core::Transaction { nonce: 0, gas_price: U256::from(0), @@ -3770,11 +4791,17 @@ mod tests { input: Bytes::from(init), v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, }; + let header1 = dummy_header(892_000); let r1 = executor - .execute_block(&dummy_header(3_614_800), &[(deploy, sender)], &root, store.clone()) + .execute_block(&header1, &[(deploy, sender)], &root, store.clone()) .expect("deploy block"); - assert!(r1.tx_results[0].success, "deploy ok"); - let deployed = sender.create(0); + assert!(r1.tx_results[0].success); + let deployed = sender.create(0); // deploy tx from sender nonce 0 + let d_acct = r1.state_changes.get(&deployed).expect("deployed account"); + assert_eq!( + d_acct.info.nonce, 0, + "created contract must start at nonce 0 (no EIP-161 in rskj)" + ); let root2 = crate::state::apply_state_changes(&root, store.as_ref(), &r1.state_changes, &r1.markers); @@ -3787,57 +4814,52 @@ mod tests { input: Bytes::new(), v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, }; + let header2 = dummy_header(892_001); let r2 = executor - .execute_block(&dummy_header(3_614_801), &[(call, sender)], &root2, store.clone()) - .expect("call block"); - assert!( - r2.tx_results[0].success, - "post-197 the failing precompile is handled; the caller frame does not abort" - ); - // The CALL pushed 0 (failure) — slot 0 must be 0. - let storage = &r2.state_changes.get(&deployed).expect("contract").storage; + .execute_block(&header2, &[(call, sender)], &root2, store.clone()) + .expect("create block"); + assert!(r2.tx_results[0].success); + + // The child must derive from nonce 0: keccak(rlp([deployed, 0])). + let expected_child = deployed.create(0); + let slot0 = r2 + .state_changes + .get(&deployed) + .expect("contract state") + .storage + .get(&U256::ZERO) + .expect("slot 0 written"); assert_eq!( - storage.get(&U256::ZERO).map(|s| s.present_value).unwrap_or(U256::ZERO), - U256::ZERO, - "CALL to a failing precompile pushes 0" - ); - // The surplus of the 900,000 forwarded gas was refunded: the whole tx - // settles well below it (would be >= 900,000 if all child gas burned). - assert!( - r2.tx_results[0].gas_used < 100_000, - "post-197 refunds the surplus forwarded gas; gas_used = {}", - r2.tx_results[0].gas_used + Address::from_slice(&slot0.present_value.to_be_bytes::<32>()[12..]), + expected_child, + "CREATE inside a freshly deployed contract must use nonce 0" ); } - /// Regression for mainnet #2,814,761: RSKIP150 lowers the EVM call-stack - /// limit to 400 (rskj Program.getMaxDepth); revm hard-codes 1024. A - /// self-recursive contract must be cut off at depth 400 — frames at - /// depths 0..=400 run, the CALL at depth 400 is refused with the child - /// gas refunded. + /// Regression for mainnet #3,650,574: rustock pins `block.beneficiary` to + /// REMASC_ADDR so revm routes gas fees to the REMASC contract, but rskj's + /// `OpCode.COINBASE` returns the block's *real* miner (`header.getCoinbase`). + /// A token contract that minted to `block.coinbase` produced a Transfer to + /// REMASC (0x…01000008) instead of the miner — diverging both the state and + /// receipts roots. COINBASE must return `header.beneficiary`, not REMASC. #[test] - fn test_rskip150_call_depth_capped_at_400() { + fn test_coinbase_returns_real_miner_not_remasc() { let store = Arc::new(MemoryTrieStore::new()); let root = TrieNode::empty(); - let sender = Address::repeat_byte(0xAD); + 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: SSTORE(0, SLOAD(0)+1); CALL(self, all gas); STOP. - let runtime: Vec = vec![ - 0x60, 0x00, 0x54, 0x60, 0x01, 0x01, 0x60, 0x00, 0x55, // counter += 1 - 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, // ret/arg/value - 0x30, 0x5a, 0xf1, 0x50, // ADDRESS GAS CALL POP - 0x00, - ]; - let mut init = vec![ - 0x60, runtime.len() as u8, 0x60, 0x0c, 0x60, 0x00, 0x39, - 0x60, runtime.len() as u8, 0x60, 0x00, 0xf3, + // Init code: COINBASE; SSTORE(0, coinbase); RETURN empty runtime. + let init: Vec = vec![ + 0x41, // COINBASE + 0x60, 0x00, // PUSH1 0 + 0x55, // SSTORE + 0x60, 0x00, 0x60, 0x00, 0xf3, // PUSH1 0 PUSH1 0 RETURN ]; - init.extend_from_slice(&runtime); + let deploy = rustock_core::Transaction { nonce: 0, gas_price: U256::from(0), @@ -3845,2201 +4867,4926 @@ 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" + )); + header.beneficiary = miner; + let r1 = executor - .execute_block(&dummy_header(2_500_000), &[(deploy, sender)], &root, store.clone()) + .execute_block(&header, &[(deploy, sender)], &root, store.clone()) .expect("deploy block"); assert!(r1.tx_results[0].success); - let contract = sender.create(0); - let root2 = - crate::state::apply_state_changes(&root, store.as_ref(), &r1.state_changes, &r1.markers); - - let call = rustock_core::Transaction { - nonce: 1, - gas_price: U256::from(0), - gas_limit: U256::from(6_000_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 r2 = executor - .execute_block(&dummy_header(2_500_001), &[(call, sender)], &root2, store.clone()) - .expect("call block"); - assert!(r2.tx_results[0].success, "recursion must end cleanly at the cap"); - let slot0 = r2 + let deployed = sender.create(0); + let slot0 = r1 .state_changes - .get(&contract) - .expect("contract") + .get(&deployed) + .expect("contract state") .storage .get(&U256::ZERO) - .expect("slot 0"); + .expect("slot 0 written"); assert_eq!( - slot0.present_value, - U256::from(401), - "frames at depths 0..=400 run; the call at depth 400 is refused" + Address::from_slice(&slot0.present_value.to_be_bytes::<32>()[12..]), + miner, + "COINBASE must return the real miner, not REMASC_ADDR" ); } - /// Regression for mainnet #2,669,886: rskj resets the caller's - /// returnDataBuffer only when callee code actually runs (executeCode) or - /// on precompile calls; a CALL to an empty-code account (plain value - /// transfer) keeps the PREVIOUS call's return data, where canonical - /// EIP-211 clears it. RETURNDATASIZE after such a call must report the - /// prior call's output size. + /// Regression for mainnet #1,613,128: post-RSKIP125 (wasabi) an internal + /// CREATE/CREATE2 opcode bumps the created contract's nonce to 1 (rskj + /// Program.createContract). A top-level CREATE *transaction* still keeps + /// nonce 0 (TransactionExecutor.create never bumps). The transferAndCall + /// callback there created an empty contract rskj stored as nonce 1 + /// (`c20100`) while rustock, zeroing every created nonce, stored `c28000`. #[test] - fn test_empty_code_call_preserves_return_data_buffer() { + fn test_internal_create_nonce_one_post_rskip125() { let store = Arc::new(MemoryTrieStore::new()); let root = TrieNode::empty(); - let sender = Address::repeat_byte(0xAC); + 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); - let deploy_tx = |nonce: u64, runtime: &[u8]| { - // Init: copy `runtime` to memory and return it. - let mut init = vec![ - 0x60, runtime.len() as u8, 0x60, 0x0c, 0x60, 0x00, 0x39, - 0x60, runtime.len() as u8, 0x60, 0x00, 0xf3, - ]; - init.extend_from_slice(runtime); - rustock_core::Transaction { - nonce, - gas_price: U256::from(0), - gas_limit: U256::from(1_000_000), - to: Bytes::new(), - value: U256::ZERO, - input: Bytes::from(init), - v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, - } - }; - - // Returner: RETURN 32 zero bytes. - let returner_runtime = vec![0x60, 0x20, 0x60, 0x00, 0xf3]; - let returner = sender.create(0); - let eoa = Address::repeat_byte(0xEE); + // Runtime: CREATE(0,0,0) (empty init → no-code child); SSTORE(0, child). + let runtime: Vec = vec![ + 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0xf0, 0x60, 0x00, 0x55, 0x00, + ]; + let mut init = vec![ + 0x60, 0x0b, 0x60, 0x0c, 0x60, 0x00, 0x39, 0x60, 0x0b, 0x60, 0x00, 0xf3, + ]; + init.extend_from_slice(&runtime); - // Main: CALL returner; CALL the EOA; SSTORE(0, RETURNDATASIZE); STOP. - let mut main_runtime = Vec::new(); - let call_to = |code: &mut Vec, addr: Address| { - code.extend_from_slice(&[0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x73]); - code.extend_from_slice(addr.as_slice()); - code.extend_from_slice(&[0x62, 0x0f, 0x42, 0x40, 0xf1, 0x50]); // PUSH3 1M gas; CALL; POP + // Deploy the creator via a top-level CREATE tx (post-wasabi). + let deploy = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(0), + gas_limit: U256::from(1_000_000), + to: Bytes::new(), + value: U256::ZERO, + input: Bytes::from(init), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, }; - call_to(&mut main_runtime, returner); - call_to(&mut main_runtime, eoa); - main_runtime.extend_from_slice(&[0x3d, 0x60, 0x00, 0x55, 0x00]); - let main = sender.create(1); - - let header1 = dummy_header(2_500_000); + let header1 = dummy_header(1_591_500); let r1 = executor - .execute_block( - &header1, - &[(deploy_tx(0, &returner_runtime), sender), (deploy_tx(1, &main_runtime), sender)], - &root, - store.clone(), - ) + .execute_block(&header1, &[(deploy, sender)], &root, store.clone()) .expect("deploy block"); - assert!(r1.tx_results.iter().all(|r| r.success)); - let root2 = - crate::state::apply_state_changes(&root, store.as_ref(), &r1.state_changes, &r1.markers); + assert!(r1.tx_results[0].success); + let deployed = sender.create(0); + assert_eq!( + 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, + ); + // Call the creator → it runs an internal CREATE. let call = rustock_core::Transaction { - nonce: 2, + nonce: 1, gas_price: U256::from(0), gas_limit: U256::from(1_000_000), - to: Bytes::copy_from_slice(main.as_slice()), + 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(&dummy_header(2_500_001), &[(call, sender)], &root2, store.clone()) - .expect("call block"); + .execute_block(&header2, &[(call, sender)], &root2, store.clone()) + .expect("create block"); assert!(r2.tx_results[0].success); - let slot0 = r2 - .state_changes - .get(&main) - .expect("main contract") - .storage - .get(&U256::ZERO) - .expect("slot 0"); + + let child = deployed.create(0); assert_eq!( - slot0.present_value, - U256::from(32), - "RETURNDATASIZE after a call to an empty-code account must keep \ - the previous call's 32-byte size (rskj returnDataBuffer)" + r2.state_changes + .get(&child) + .expect("child contract") + .info + .nonce, + 1, + "internal CREATE post-RSKIP125 bumps the created contract nonce to 1" ); } - /// Regression for mainnet #3,616,094: RSKIP171 (iris300) makes - /// `Program.cleanReturnDataBuffer` reset the caller's returnDataBuffer on - /// every call that runs no new frame, so a CALL to an empty-code account - /// CLEARS the buffer (canonical EIP-211) instead of preserving the prior - /// call's output. RETURNDATASIZE after such a call must report 0. - /// Ground truth: rskj VMTest.returnDataSizeAfterCallToNonExistentContract - /// (active = true → 0); the pre-iris twin asserts 32 above. + /// Wasabi enables the Constantinople shift opcodes (RSKIP120) and CREATE2 + /// (RSKIP125) but not EXTCODEHASH (RSKIP140, papyrus). The spec must be + /// PETERSBURG from wasabi (not BYZANTIUM, which lacks shifts). #[test] - fn test_empty_code_call_clears_return_data_buffer_post_rskip171() { - let store = Arc::new(MemoryTrieStore::new()); - let root = TrieNode::empty(); - let sender = Address::repeat_byte(0xAC); - 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 executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); - - let deploy_tx = |nonce: u64, runtime: &[u8]| { - let mut init = vec![ - 0x60, runtime.len() as u8, 0x60, 0x0c, 0x60, 0x00, 0x39, - 0x60, runtime.len() as u8, 0x60, 0x00, 0xf3, - ]; - init.extend_from_slice(runtime); - rustock_core::Transaction { - nonce, - gas_price: U256::from(0), - gas_limit: U256::from(1_000_000), - to: Bytes::new(), - value: U256::ZERO, - input: Bytes::from(init), - v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, - } - }; - - // Returner: RETURN 32 zero bytes. - let returner_runtime = vec![0x60, 0x20, 0x60, 0x00, 0xf3]; - let returner = sender.create(0); - let eoa = Address::repeat_byte(0xEE); - - // Main: CALL returner; CALL the EOA; SSTORE(0, RETURNDATASIZE); STOP. - let mut main_runtime = Vec::new(); - let call_to = |code: &mut Vec, addr: Address| { - code.extend_from_slice(&[0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x73]); - code.extend_from_slice(addr.as_slice()); - code.extend_from_slice(&[0x62, 0x0f, 0x42, 0x40, 0xf1, 0x50]); // PUSH3 1M gas; CALL; POP - }; - call_to(&mut main_runtime, returner); - call_to(&mut main_runtime, eoa); - main_runtime.extend_from_slice(&[0x3d, 0x60, 0x00, 0x55, 0x00]); - let main = sender.create(1); - - // Iris300 activates at mainnet #3,614,800 — pick a block past it. - let header1 = dummy_header(3_616_094); - let r1 = executor - .execute_block( - &header1, - &[(deploy_tx(0, &returner_runtime), sender), (deploy_tx(1, &main_runtime), sender)], - &root, - store.clone(), - ) - .expect("deploy block"); - assert!(r1.tx_results.iter().all(|r| r.success)); - let root2 = - crate::state::apply_state_changes(&root, store.as_ref(), &r1.state_changes, &r1.markers); - - let call = rustock_core::Transaction { - nonce: 2, - gas_price: U256::from(0), - gas_limit: U256::from(1_000_000), - to: Bytes::copy_from_slice(main.as_slice()), - value: U256::ZERO, - input: Bytes::new(), - v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, - }; - let r2 = executor - .execute_block(&dummy_header(3_616_095), &[(call, sender)], &root2, store.clone()) - .expect("call block"); - assert!(r2.tx_results[0].success); - let slot0 = r2 - .state_changes - .get(&main) - .expect("main contract") - .storage - .get(&U256::ZERO) - .map(|s| s.present_value) - .unwrap_or(U256::ZERO); - assert_eq!( - slot0, - U256::ZERO, - "RSKIP171: RETURNDATASIZE after a call to an empty-code account \ - must be 0 (the buffer is cleared, not preserved)" - ); + fn test_wasabi_spec_enables_shift_opcodes() { + let cfg = RskHardforkConfig::mainnet(); + assert_eq!(cfg.spec_id(729_000), SpecId::BYZANTIUM, "orchid"); + assert_eq!(cfg.spec_id(1_590_999), SpecId::BYZANTIUM, "pre-wasabi"); + assert_eq!(cfg.spec_id(1_591_000), SpecId::PETERSBURG, "wasabi"); + assert!(!cfg.has_rskip140(1_591_000), "no EXTCODEHASH at wasabi"); + assert!(cfg.has_rskip140(2_392_700), "EXTCODEHASH from papyrus"); } - /// 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 - /// precompile (0x01000010) reverted in rustock (size 0) but not in rskj. + /// Regression for mainnet #1,613,128..#1,661,216 era: a contract whose + /// runtime uses SHR (solc >=0.5 selector dispatch) must execute post-wasabi + /// instead of hitting an invalid opcode that consumes the whole gas limit. + /// #1,661,216's setCompleted(uint256) call cost 42,023 gas in rskj but + /// rustock charged the full 6,721,975 limit (SHR invalid under BYZANTIUM). #[test] - fn test_extcodesize_of_precompile_is_max() { + fn test_shr_opcode_executes_post_wasabi() { 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 block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); - // Runtime: PUSH20 0x..01000010; EXTCODESIZE; PUSH1 0; SSTORE; STOP. - let mut runtime: Vec = vec![0x73]; - runtime.extend_from_slice(crate::precompiles::BLOCK_HEADER_ADDR.as_slice()); - runtime.extend_from_slice(&[0x3b, 0x60, 0x00, 0x55, 0x00]); + // 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 mut init = vec![ - 0x60, runtime.len() as u8, 0x60, 0x0c, 0x60, 0x00, 0x39, - 0x60, runtime.len() as u8, 0x60, 0x00, 0xf3, + 0x60, 0x09, 0x60, 0x0c, 0x60, 0x00, 0x39, 0x60, 0x09, 0x60, 0x00, 0xf3, ]; init.extend_from_slice(&runtime); let deploy = rustock_core::Transaction { - nonce: 0, gas_price: U256::ZERO, gas_limit: U256::from(1_000_000), - to: Bytes::new(), value: U256::ZERO, input: Bytes::from(init), - v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, + nonce: 0, + gas_price: U256::from(0), + gas_limit: U256::from(1_000_000), + to: Bytes::new(), + value: U256::ZERO, + input: Bytes::from(init), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, }; - let header1 = dummy_header(1_700_000); + let header1 = dummy_header(1_591_500); let r1 = executor .execute_block(&header1, &[(deploy, sender)], &root, store.clone()) .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, gas_price: U256::ZERO, gas_limit: U256::from(1_000_000), - 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, + nonce: 1, + gas_price: U256::from(0), + gas_limit: U256::from(1_000_000), + 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, }; - let header2 = dummy_header(1_700_001); + 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); - let slot0 = r2.state_changes.get(&deployed).expect("contract") - .storage.get(&U256::ZERO).expect("slot 0"); - assert_eq!( - slot0.present_value, U256::MAX, - "EXTCODESIZE of an active precompile must be 2^256-1 (RSKIP90)" + 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)" ); + let slot0 = r2 + .state_changes + .get(&deployed) + .expect("contract") + .storage + .get(&U256::ZERO) + .expect("slot 0"); + assert_eq!(slot0.present_value, U256::from(4), "4 >> 0 == 4"); } - /// Regression for mainnet #1,071,481: before RSKIP453 (lovell700) an - /// INTERNAL create whose constructor succeeds always deploys — an - /// unpayable code deposit leaves the contract with EMPTY code, the - /// constructor state committed and the address pushed (rskj - /// Program.finalizeContractCreation), instead of EIP-2's out-of-gas. - /// The RNS registrar deploys a Deed whose 944,800-gas deposit cannot - /// fit in the 600,000 tx limit; rskj deploys it codeless and the - /// auction proceeds (tx gas 259,460, status success). + /// Regression for mainnet #6,362,186 tx[3] (gas 1,197,343 vs 1,182,343, + /// +15,000): rskj `Program.finalizeContractCreation` propagates only logInfos + /// and deleteAccounts from a constructor to the parent result — NOT + /// futureRefund (unlike the CALL path's `ProgramResult.merge`). So a gas + /// refund accrued inside a nested CREATE/CREATE2 constructor is DISCARDED. + /// A constructor that SETs then CLEARs a storage slot must therefore NOT + /// earn the 15,000 SSTORE-clear refund when created via the CREATE opcode: + /// compared with a set-only constructor, clearing costs MORE (the extra + /// 5,000 CLEAR_SSTORE), not 10,000 less. #[test] - fn test_pre_rskip453_internal_create_with_unpayable_deposit_deploys_empty() { - // Child initcode: RETURN(0, 0x10000) — 65,536 zero bytes of "code", - // deposit 13.1M gas, unpayable within the 1M tx limit. - // Caller runtime: MSTORE initcode; CREATE; SSTORE(0, address). - let runtime: Vec = vec![ - 0x66, 0x62, 0x01, 0x00, 0x00, 0x60, 0x00, 0xf3, // PUSH7 initcode - 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 mut init = vec![ - 0x60, 0x15, 0x60, 0x0c, 0x60, 0x00, 0x39, 0x60, 0x15, 0x60, 0x00, 0xf3, - ]; - init.extend_from_slice(&runtime); - - let run_at = |block: u64, init: Vec| { - 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 executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); - - let deploy = rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(0), - gas_limit: U256::from(1_000_000), - to: Bytes::new(), - value: U256::ZERO, - input: Bytes::from(init), - v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, - }; - let header1 = dummy_header(block); - let r1 = executor - .execute_block(&header1, &[(deploy, sender)], &root, store.clone()) - .expect("deploy block"); - assert!(r1.tx_results[0].success); - let root2 = - crate::state::apply_state_changes(&root, store.as_ref(), &r1.state_changes, &r1.markers); + fn nested_create_constructor_refund_is_dropped_rskj() { + // child init: SET slot0=1, optionally CLEAR it, then RETURN empty code. + let child_init = |clear: bool| -> Vec { + let mut c = vec![0x60, 0x01, 0x60, 0x00, 0x55]; // PUSH1 1 PUSH1 0 SSTORE + if clear { + c.extend_from_slice(&[0x60, 0x00, 0x60, 0x00, 0x55]); // clear slot0 + } + c.extend_from_slice(&[0x60, 0x00, 0x60, 0x00, 0xf3]); // RETURN empty + c + }; + // factory runtime: MSTORE the child init (right-aligned), then CREATE it. + let factory_runtime = |init: &[u8]| -> Vec { + let n = init.len(); + assert!((1..=32).contains(&n)); + let off = (32 - n) as u8; + let mut r = vec![0x5f + n as u8]; // PUSH{n} + r.extend_from_slice(init); + r.extend_from_slice(&[0x60, 0x00, 0x52]); // PUSH1 0 MSTORE + r.extend_from_slice(&[0x60, n as u8]); // PUSH1 size + r.extend_from_slice(&[0x60, off]); // PUSH1 offset + r.extend_from_slice(&[0x60, 0x00, 0xf0, 0x00]); // PUSH1 0 (value) CREATE STOP + r + }; + // deploy wrapper: CODECOPY the runtime (after the 12-byte prefix) + RETURN. + let deploy_init = |runtime: &[u8]| -> Vec { + let l = runtime.len() as u8; + let mut d = vec![ + 0x60, l, 0x60, 0x0c, 0x60, 0x00, 0x39, 0x60, l, 0x60, 0x00, 0xf3, + ]; + d.extend_from_slice(runtime); + d + }; - let caller = sender.create(0); + // Run the whole deploy+call sequence and return the call's gas_used. + let run = |clear: bool| -> u64 { + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + let sender = Address::repeat_byte(0xAB); + 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 executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + + let factory = deploy_init(&factory_runtime(&child_init(clear))); + let deploy = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(0), + gas_limit: U256::from(2_000_000), + to: Bytes::new(), + value: U256::ZERO, + input: Bytes::from(factory), + v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, + }; + let r1 = executor + .execute_block(&dummy_header(6_362_186), &[(deploy, sender)], &root, store.clone()) + .expect("deploy block"); + assert!(r1.tx_results[0].success, "factory deploy failed"); + let factory_addr = sender.create(0); + let root2 = crate::state::apply_state_changes( + &root, store.as_ref(), &r1.state_changes, &r1.markers, + ); let call = rustock_core::Transaction { nonce: 1, gas_price: U256::from(0), - gas_limit: U256::from(1_000_000), - to: Bytes::copy_from_slice(caller.as_slice()), + gas_limit: U256::from(2_000_000), + to: Bytes::copy_from_slice(factory_addr.as_slice()), value: U256::ZERO, input: Bytes::new(), v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, }; - let header2 = dummy_header(block + 1); let r2 = executor - .execute_block(&header2, &[(call, sender)], &root2, store.clone()) - .expect("create block"); - let slot0 = r2.state_changes.get(&caller).and_then(|a| { - a.storage.get(&U256::ZERO).map(|s| s.present_value) - }); - (caller, slot0, r2) + .execute_block(&dummy_header(6_362_187), &[(call, sender)], &root2, store.clone()) + .expect("call block"); + assert!(r2.tx_results[0].success, "factory call (CREATE) failed"); + r2.tx_results[0].gas_used }; - // Pre-RSKIP453: the create "succeeds" — address pushed, empty code. - let (caller, slot0, r2) = run_at(1_071_481, init.clone()); - assert!(r2.tx_results[0].success); - let child = caller.create(0); - assert_eq!( - Address::from_slice(&slot0.expect("slot 0 written").to_be_bytes::<32>()[12..]), - child, - "unpayable deposit must still deploy (empty code) pre-RSKIP453" - ); - let child_acct = r2.state_changes.get(&child).expect("child account exists"); + let gas_clear = run(true); + let gas_noclear = run(false); + // With the nested-create refund dropped, clearing the slot adds the + // 5,000 CLEAR_SSTORE cost and earns NO 15,000 refund, so it costs MORE. + // (Were the refund wrongly propagated, gas_clear would be ~10,000 LESS.) assert!( - child_acct - .info - .code - .as_ref() - .map(|c| c.is_empty()) - .unwrap_or(true), - "child must have empty code" + gas_clear > gas_noclear, + "nested-create constructor clear must not earn a refund: clear={gas_clear} noclear={gas_noclear}" ); + } - // Post-RSKIP453 (lovell700): EIP-2/EIP-170 semantics — the failed - // create consumes the forwarded gas (no 63/64 in RSK) and the - // caller goes out of gas with it. - let (_, _, r2) = run_at(7_338_024, init); - assert!( - !r2.tx_results[0].success, - "unpayable/oversize deposit must fail post-RSKIP453" - ); + /// Testnet #3,088,983 tx[0] (gas 60,388 against the header's 36,388): a + /// contract CREATEs a child whose constructor self-destructs. rskj counts a + /// self-destruct's 24,000 refund when the transaction ends, from the set of + /// deleted accounts (`TransactionExecutor.refundGas`), and + /// `Program.finalizeContractCreation` hands that set up from a constructor; + /// what it drops is only the constructor's `futureRefund` (the test above). + /// So against a constructor that returns, one that self-destructs costs its + /// CALLER and SELFDESTRUCT (5,002) less the 24,000, also where both set and + /// clear a slot first, whose 15,000 stays dropped. + #[test] + fn nested_create_constructor_selfdestruct_refund_is_kept_rskj() { + // child init: optionally set then clear slot 0, then SELFDESTRUCT to the + // creator or RETURN empty code. + let child_init = |clear: bool, destruct: bool| -> Vec { + let mut c = Vec::new(); + if clear { + c.extend_from_slice(&[0x60, 0x01, 0x60, 0x00, 0x55]); // slot0 = 1 + c.extend_from_slice(&[0x60, 0x00, 0x60, 0x00, 0x55]); // slot0 = 0 + } + if destruct { + c.extend_from_slice(&[0x33, 0xff]); // CALLER SELFDESTRUCT + } else { + c.extend_from_slice(&[0x60, 0x00, 0x60, 0x00, 0xf3]); // RETURN empty + } + c + }; + // factory runtime: MSTORE the child init (right-aligned), then CREATE it. + let factory_runtime = |init: &[u8]| -> Vec { + let n = init.len(); + assert!((1..=32).contains(&n)); + let off = (32 - n) as u8; + let mut r = vec![0x5f + n as u8]; // PUSH{n} + r.extend_from_slice(init); + r.extend_from_slice(&[0x60, 0x00, 0x52]); // PUSH1 0 MSTORE + r.extend_from_slice(&[0x60, n as u8]); // PUSH1 size + r.extend_from_slice(&[0x60, off]); // PUSH1 offset + r.extend_from_slice(&[0x60, 0x00, 0xf0, 0x00]); // PUSH1 0 (value) CREATE STOP + r + }; + // deploy wrapper: CODECOPY the runtime (after the 12-byte prefix) + RETURN. + let deploy_init = |runtime: &[u8]| -> Vec { + let l = runtime.len() as u8; + let mut d = vec![ + 0x60, l, 0x60, 0x0c, 0x60, 0x00, 0x39, 0x60, l, 0x60, 0x00, 0xf3, + ]; + d.extend_from_slice(runtime); + d + }; + + // Deploy the factory, call it, and return the call's gas_used. + let run = |child: Vec| -> u64 { + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + let sender = Address::repeat_byte(0xAB); + 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 executor = RskExecutor::new(RskHardforkConfig::testnet(), block_store); + + let deploy = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(0), + gas_limit: U256::from(2_000_000), + to: Bytes::new(), + value: U256::ZERO, + input: Bytes::from(deploy_init(&factory_runtime(&child))), + v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, + }; + let r1 = executor + .execute_block(&dummy_header(3_088_982), &[(deploy, sender)], &root, store.clone()) + .expect("deploy block"); + assert!(r1.tx_results[0].success, "factory deploy failed"); + let factory_addr = sender.create(0); + let root2 = crate::state::apply_state_changes( + &root, store.as_ref(), &r1.state_changes, &r1.markers, + ); + let call = rustock_core::Transaction { + nonce: 1, + gas_price: U256::from(0), + gas_limit: U256::from(2_000_000), + to: Bytes::copy_from_slice(factory_addr.as_slice()), + value: U256::ZERO, + input: Bytes::new(), + v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, + }; + let r2 = executor + .execute_block(&dummy_header(3_088_983), &[(call, sender)], &root2, store.clone()) + .expect("call block"); + assert!(r2.tx_results[0].success, "factory call (CREATE) failed"); + r2.tx_results[0].gas_used + }; + + for clear in [false, true] { + let returns = run(child_init(clear, false)); + let destructs = run(child_init(clear, true)); + // CALLER + SELFDESTRUCT (2 + 5,000) against PUSH1 PUSH1 RETURN + // (3 + 3), less the self-destruct's refund. + assert_eq!( + destructs + 24_000, + returns + 5_002 - 6, + "a nested constructor's self-destruct keeps its refund (clear: {clear})" + ); + } } - /// Regression for mainnet #764,123: addLockWhitelistAddress is disabled - /// post-RSKIP87 (orchid), so rskj's parseData returns null. The gas is - /// the flat releaseBtc cost (23,000, no data cost) and — although - /// RSKIP88 makes execute() throw — a direct transaction records SUCCESS - /// with empty output because TransactionExecutor spends only - /// requiredGas + basicTxCost and go() (vm == null) just commits. - /// Real receipt: status 0x1, gasUsed 47,408 (21,000 + 3,408 + 23,000). + /// Regression for mainnet #3,631,998 tx[1] (gas used 27,540 vs 27,529, +11, + /// divergent revert branch): rskj `VM.doEXTCODEHASH` (`getCodeHashStandard`) + /// keys on TRIE EXISTENCE, not EIP-161 emptiness. An account that exists in + /// the trie but is "empty" (nonce 0, balance 0, no code) must hash to + /// `keccak256("")`, while an account absent from the trie hashes to 0. revm's + /// stock EXTCODEHASH returns 0 for both (it uses `AccountInfo::is_empty()`), + /// so the existing-empty case forked. Ground truth: a USDT-on-ETH address + /// (no code on RSK) that a prior 0-value call had created hashed to + /// `keccak256("")` in rskj, sending the proxy down a different branch. #[test] - fn test_disabled_bridge_method_post_rskip87_charges_flat_cost() { + fn test_extcodehash_existing_empty_account_is_keccak_empty() { let store = Arc::new(MemoryTrieStore::new()); - let root = TrieNode::empty(); - - let sender = Address::from_slice( - &alloy_primitives::hex::decode("6ba9d41b07da470fe340cbd439a42538795eb75b").unwrap(), - ); + let mut root = TrieNode::empty(); + let sender = Address::repeat_byte(0xAC); let one_rbtc = U256::from(10u64).pow(U256::from(18)); - let root = put_account(&root, store.as_ref(), &sender, 0, one_rbtc); + root = put_account(&root, store.as_ref(), &sender, 0, one_rbtc); - // addLockWhitelistAddress("1HqQJ9PxdG51NmFij43bBUrmQRDULbnSzW", 1 gwei) - let input = alloy_primitives::hex::decode( - "502bbbce0000000000000000000000000000000000000000000000000000000000000040\ - 000000000000000000000000000000000000000000000000000000003b9aca00\ - 0000000000000000000000000000000000000000000000000000000000000022\ - 314871514a395078644735314e6d46696a3433624255726d515244554c626e53\ - 7a57000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(); - let tx = rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(183_000_000u64), // mainnet tx gas price - gas_limit: U256::from(1_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, - }; + // An existing-but-empty account (nonce 0, balance 0, no code): present + // in the trie, yet EIP-161-empty. rskj -> keccak256(""); revm stock -> 0. + let existing_empty = Address::repeat_byte(0xE1); + root = put_account(&root, store.as_ref(), &existing_empty, 0, U256::ZERO); + + // An address never placed in the trie: rskj & rustock both -> 0. + let absent = Address::repeat_byte(0xE2); let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); - let header = dummy_header(764_123); - let result = executor - .execute_block(&header, &[(tx, sender)], &root, store.clone()) - .expect("block executes"); - assert!(result.tx_results[0].success, "receipt records SUCCESS"); - assert_eq!( - result.tx_results[0].gas_used, 47_408, - "flat releaseBtc cost, no data cost, no method cost" - ); - // The disabled method must NOT execute: no bridge storage writes. - if let Some(bridge) = result.state_changes.get(&crate::precompiles::BRIDGE_ADDR) { - assert!( - bridge.storage.is_empty(), - "disabled method must not touch bridge storage" - ); - } - // The FEE side sees the failure: the sender is charged the FULL gas - // limit and REMASC receives it (rskj summary.getFee when failed). - // Remote groundtruth: sender 0x6ba9...75b balance drops by exactly - // 1,000,000 * 183,000,000 = 183e12 wei across #764,122 -> #764,123. - let full_fee = U256::from(1_000_000u64) * U256::from(183_000_000u64); - assert_eq!( - result.state_changes.get(&sender).unwrap().info.balance, - one_rbtc - full_fee, - "sender pays for the full gas limit, no leftover refund" - ); - assert_eq!( - result - .state_changes - .get(&crate::precompiles::REMASC_ADDR) - .unwrap() - .info - .balance, - full_fee, - "REMASC receives gasLimit * gasPrice" - ); - assert_eq!(result.paid_fees, full_fee, "paid_fees tracks the full fee"); - } - - /// The same disabled-method profile routed through revm (post-RSKIP136, - /// mainnet #765,073 used gasPrice 60M wei): receipt SUCCESS with only - /// the spent 47,408 gas, but the sender pays for the full 1,000,000 - /// limit, REMASC receives it, and the endowment is NOT transferred - /// (rskj TransactionExecutor.call skips it when execute() throws). - #[test] - fn test_disabled_bridge_method_charges_full_gas_limit_post_rskip136() { - let store = Arc::new(MemoryTrieStore::new()); - let root = TrieNode::empty(); + // Runtime: EXTCODEHASH(existing_empty) -> slot 0; + // EXTCODEHASH(absent) -> slot 1; STOP. + let mut runtime: Vec = Vec::new(); + runtime.push(0x73); // PUSH20 existing_empty + runtime.extend_from_slice(existing_empty.as_slice()); + runtime.extend_from_slice(&[0x3f, 0x60, 0x00, 0x55]); // EXTCODEHASH; PUSH1 0; SSTORE + runtime.push(0x73); // PUSH20 absent + runtime.extend_from_slice(absent.as_slice()); + runtime.extend_from_slice(&[0x3f, 0x60, 0x01, 0x55]); // EXTCODEHASH; PUSH1 1; SSTORE + runtime.push(0x00); // STOP - 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); + // Init: CODECOPY the runtime to memory and RETURN it. + let rt_len = runtime.len() as u8; + let mut init = vec![ + 0x60, rt_len, // PUSH1 + 0x60, 0x0c, // PUSH1 0x0c (runtime offset in this init) + 0x60, 0x00, // PUSH1 0 + 0x39, // CODECOPY + 0x60, rt_len, // PUSH1 + 0x60, 0x00, // PUSH1 0 + 0xf3, // RETURN + ]; + init.extend_from_slice(&runtime); - // addLockWhitelistAddress selector + garbage args: disabled forever - // post-RSKIP87, parse failure at every later era. - let mut input = vec![0x50, 0x2b, 0xbb, 0xce]; - input.extend_from_slice(&[0u8; 32]); - let tx = rustock_core::Transaction { + let deploy = rustock_core::Transaction { nonce: 0, - gas_price: U256::from(60_000_000u64), + gas_price: U256::from(0), gas_limit: U256::from(1_000_000), - to: Bytes::copy_from_slice(crate::precompiles::BRIDGE_ADDR.as_slice()), - value: U256::from(7), // endowment must stay with the sender - input: Bytes::from(input), - v: 0, - r: U256::ZERO, - s: U256::ZERO, - cached_rlp: None, + to: Bytes::new(), + value: U256::ZERO, + input: Bytes::from(init), + v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, }; + // papyrus200 (>= 2,392,700) so EXTCODEHASH (RSKIP140) is active. + let header1 = dummy_header(2_392_800); + let r1 = executor + .execute_block(&header1, &[(deploy, sender)], &root, store.clone()) + .expect("deploy block"); + assert!(r1.tx_results[0].success, "deploy must succeed"); + let deployed = sender.create(0); + let root2 = + crate::state::apply_state_changes(&root, store.as_ref(), &r1.state_changes, &r1.markers); - 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_392_700); // papyrus200: RSKIP136 active - let result = executor - .execute_block(&header, &[(tx, sender)], &root, store.clone()) - .expect("block executes"); - assert!(result.tx_results[0].success, "receipt records SUCCESS"); - // 21,000 + 4 nonzero * 68 + 32 zero * 4 + 23,000 = 44,400 - assert_eq!( - result.tx_results[0].gas_used, - 21_000 + 4 * 68 + 32 * 4 + 23_000, - "receipt shows only the spent gas" - ); - let full_fee = U256::from(1_000_000u64) * U256::from(60_000_000u64); - assert_eq!( - result.state_changes.get(&sender).unwrap().info.balance, - one_rbtc - full_fee, - "sender pays the full limit and keeps the endowment" - ); - assert_eq!( - result - .state_changes - .get(&crate::precompiles::REMASC_ADDR) - .unwrap() - .info - .balance, - full_fee, - "REMASC receives gasLimit * gasPrice" - ); - let bridge_balance = result - .state_changes - .get(&crate::precompiles::BRIDGE_ADDR) - .map(|a| a.info.balance) - .unwrap_or_default(); - assert_eq!(bridge_balance, U256::ZERO, "no endowment on invisible exception"); - assert_eq!(result.paid_fees, full_fee); - } - - /// rskj TransactionExecutor.call transfers the endowment after a - /// non-throwing precompile execute(); the pre-RSKIP136 direct path must - /// do the same for successful Bridge calls. - #[test] - fn test_pre_rskip136_direct_bridge_success_transfers_endowment() { - 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); - - // voteFeePerKbChange(int256) — enabled at every era; an unauthorized - // sender gets an error CODE in the output but the call succeeds. - let mut input = vec![0x04, 0x61, 0x31, 0x3e]; - let mut arg = [0u8; 32]; - arg[30..].copy_from_slice(&30_000u16.to_be_bytes()); - input.extend_from_slice(&arg); - 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::from(5), - input: Bytes::from(input), - v: 0, - r: U256::ZERO, - s: U256::ZERO, - cached_rlp: None, + let call = rustock_core::Transaction { + nonce: 1, + gas_price: U256::from(0), + gas_limit: U256::from(1_000_000), + 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, }; + let header2 = dummy_header(2_392_801); + let r2 = executor + .execute_block(&header2, &[(call, sender)], &root2, store.clone()) + .expect("call block"); + assert!(r2.tx_results[0].success, "EXTCODEHASH call must succeed"); - let block_store = - Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); - let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + let storage = &r2.state_changes.get(&deployed).expect("contract").storage; + let slot0 = storage.get(&U256::ZERO).expect("slot 0").present_value; + let slot1 = storage.get(&U256::from(1)).map(|s| s.present_value).unwrap_or(U256::ZERO); - let header = dummy_header(764_123); // pre-RSKIP136 direct path - let result = executor - .execute_block(&header, &[(tx, sender)], &root, store.clone()) - .expect("block executes"); - assert!(result.tx_results[0].success); - assert_eq!( - result - .state_changes - .get(&crate::precompiles::BRIDGE_ADDR) - .unwrap() - .info - .balance, - U256::from(5), - "successful direct bridge call transfers the endowment" - ); + // Existing-but-empty account -> keccak256(""), NOT 0. assert_eq!( - result.state_changes.get(&sender).unwrap().info.balance, - one_rbtc - U256::from(5), + B256::from(slot0.to_be_bytes()), + crate::rsk_instructions::KECCAK_EMPTY, + "EXTCODEHASH of an existing-but-empty account must be keccak256(\"\")" ); + // Absent account -> 0. + assert_eq!(slot1, U256::ZERO, "EXTCODEHASH of an absent account must be 0"); } - /// Regression for mainnet #466,503: rskj charges NEW_ACCT_CALL (25,000) - /// on trie EXISTENCE — the first-ever internal call to a precompile - /// creates its account node (zero-value transfer), so the charge applies - /// once per address in the chain's lifetime, not once per call. The same - /// ecrecover call in the next block must cost exactly 25,000 less. + /// Regression for mainnet #2,430,894: CHAINID (RSKIP152) and SELFBALANCE + /// (RSKIP151) activate at papyrus200 while the SpecId stays PETERSBURG — + /// revm's spec-checked impls halted NotActivated and the tx consumed its + /// whole gas limit (rskj: success, 235,956 gas). Pre-papyrus both must + /// still be invalid opcodes consuming all frame gas. #[test] - fn test_precompile_new_account_charged_only_once_across_blocks() { + fn test_chainid_selfbalance_activate_at_papyrus() { let store = Arc::new(MemoryTrieStore::new()); let root = TrieNode::empty(); - - let sender = Address::repeat_byte(0xAA); - let contract = Address::repeat_byte(0xCC); + let sender = Address::repeat_byte(0xAB); 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); - - // PUSH1 0 x4 (ret/args) PUSH1 0 (value) PUSH20 0x..01 PUSH2 3000 CALL STOP - let mut code = vec![0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x73]; - let mut ecrecover = [0u8; 20]; - ecrecover[19] = 0x01; - code.extend_from_slice(&ecrecover); - code.extend_from_slice(&[0x61, 0x0b, 0xb8, 0xf1, 0x00]); // PUSH2 3000, CALL, STOP - let code_key_bytes = rustock_trie::code_key(&contract); - let root = root.put( - &TrieKeySlice::from_key(&code_key_bytes), - &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 call_tx = |nonce: u64| rustock_core::Transaction { - nonce, + // Runtime: CHAINID; PUSH1 0; SSTORE; SELFBALANCE; PUSH1 1; SSTORE; STOP + let runtime: Vec = vec![0x46, 0x60, 0x00, 0x55, 0x47, 0x60, 0x01, 0x55, 0x00]; + let mut init = vec![ + 0x60, 0x09, 0x60, 0x0c, 0x60, 0x00, 0x39, 0x60, 0x09, 0x60, 0x00, 0xf3, + ]; + init.extend_from_slice(&runtime); + let deploy = rustock_core::Transaction { + nonce: 0, gas_price: U256::from(0), - gas_limit: U256::from(200_000), - to: Bytes::copy_from_slice(contract.as_slice()), + gas_limit: U256::from(1_000_000), + to: Bytes::new(), value: U256::ZERO, - input: Bytes::new(), - v: 0, - r: U256::ZERO, - s: U256::ZERO, - cached_rlp: None, + input: Bytes::from(init), + v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, }; - - let header = dummy_header(466_503); let r1 = executor - .execute_block(&header, &[(call_tx(0), sender)], &root, store.clone()) - .expect("block 1"); + .execute_block(&dummy_header(2_392_700), &[(deploy, sender)], &root, store.clone()) + .expect("deploy block"); assert!(r1.tx_results[0].success); - - // Apply to the trie: the ecrecover account node must now exist. + let deployed = sender.create(0); let root2 = crate::state::apply_state_changes(&root, store.as_ref(), &r1.state_changes, &r1.markers); - let header2 = dummy_header(466_504); + let call = |nonce: u64| rustock_core::Transaction { + nonce, + gas_price: U256::from(0), + gas_limit: U256::from(1_000_000), + 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, + }; + // Post-papyrus: both opcodes execute; CHAINID pushes 30 (RSK mainnet). let r2 = executor - .execute_block(&header2, &[(call_tx(1), sender)], &root2, store.clone()) - .expect("block 2"); - assert!(r2.tx_results[0].success); - - let g1 = r1.tx_results[0].gas_used; - let g2 = r2.tx_results[0].gas_used; + .execute_block(&dummy_header(2_392_701), &[(call(1), sender)], &root2, store.clone()) + .expect("papyrus call block"); + assert!(r2.tx_results[0].success, "CHAINID/SELFBALANCE valid at papyrus200"); + let storage = &r2.state_changes.get(&deployed).expect("contract").storage; assert_eq!( - g1 - g2, - 25_000, - "first call pays NEW_ACCT_CALL once (got {g1} then {g2})" + storage.get(&U256::ZERO).unwrap().present_value, + U256::from(30), + "CHAINID == 30" + ); + assert_eq!( + storage.get(&U256::from(1)).unwrap().present_value, + U256::ZERO, + "SELFBALANCE of the fresh contract == 0" ); + + // Pre-papyrus: invalid opcode, the whole gas limit is consumed. + let r3 = executor + .execute_block(&dummy_header(2_392_699), &[(call(1), sender)], &root2, store.clone()) + .expect("pre-papyrus call block"); + assert!(!r3.tx_results[0].success, "CHAINID invalid before papyrus200"); + assert_eq!(r3.tx_results[0].gas_used, 1_000_000, "invalid opcode consumes all gas"); } - /// rskj charges NEW_ACCT_CALL (25,000) on trie EXISTENCE - /// (`track.isExist(addr)`), NOT on EIP-161 emptiness. An account that - /// exists in the unitrie with `(nonce=0, balance=0, no code)` — e.g. a - /// zero-paid miner, or an account re-created after a same-block - /// selfdestruct — is NOT charged, because rustock pins the journal spec to - /// HOMESTEAD (`rsk_handler`), making revm's `is_empty` collapse to - /// `is_loaded_as_not_existing` (DB returned `None`) rather than raw EIP-161 - /// emptiness. A CALL to a truly-absent address IS charged. The 25,000 delta - /// between the two cases proves the charge keys on existence, not emptiness. + /// RSKIP197 (iris300): a CALL to a precompile that fails with a non-OOG + /// error is *handled* — the call pushes 0, the surplus forwarded gas is + /// refunded, and execution continues; the failing precompile costs exactly + /// its `getGasForData` requiredGas. Ported from rskj `ProgramTest` + /// (`testCallToPrecompiledAddress_throwPrecompiledContractException`: + /// post-197 `gasUsed == gasCost`, stack pushes 0). + /// + /// Here a deployed contract CALLs BLAKE2F (0x09) with a 213-byte input + /// whose final-block byte is 0x02 (invalid: must be 0 or 1) and `rounds`=1, + /// forwarding 900,000 gas. rskj's `Blake2F.execute` throws + /// `BLAKE2F_ERROR_FINAL_BLOCK_BYTES`; post-197 the call costs only + /// `requiredGas == rounds == 1` (the surplus is refunded), so the whole tx + /// settles far below the forwarded gas. The CALL result (0) is stored at + /// slot 0 to prove the failure was observed without aborting the frame. + /// What a *failing* top-level Bridge call costs, and what its receipt says. + /// + /// Not what one would guess. rskj's precompile path in + /// `TransactionExecutor.call` sets `gasLeftover = txGasLimit - gasUsed` + /// before the try and its catch only calls `result.setException(e)` -- it + /// never zeroes the leftover the way the VM path does for a reverted + /// contract call. The exception is then recorded for FEE purposes but not + /// for RECEIPT purposes, so the transaction: + /// + /// * reports status SUCCESS, + /// * reports `gasUsed = requiredGas + basicTxCost`, well under the limit, + /// * yet charges the sender the ENTIRE gas limit, the surplus to REMASC. + /// + /// That is quirks-frozen-bugs.md §50, reported to the rskj security team in + /// docs/rskj-report-precompile-receipt-status.md, and consensus-frozen. + /// + /// This test exists where it does because the rskj-compat sender path now + /// returns `Err` for inputs rskj throws on, and that error flows through + /// exactly this mechanism. Pinning it here means a change to the quirk + /// cannot silently change what a rejected peg-in costs. #[test] - fn test_new_acct_call_charged_on_existence_not_emptiness() { + fn a_failing_bridge_call_reports_success_and_spends_only_the_precompile_gas() { + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + let sender = Address::repeat_byte(0xCE); let one_rbtc = U256::from(10u64).pow(U256::from(18)); - let sender = Address::repeat_byte(0xAA); - let contract = Address::repeat_byte(0xCC); - let target = Address::repeat_byte(0xDD); // not a precompile + let root = put_account(&root, store.as_ref(), &sender, 0, one_rbtc); + let block_store = + Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + // Post-RSKIP136, so this takes revm's ordinary transact path rather than + // the direct-bridge branch that handles the free and pre-136 cases. + let mut header = dummy_header(7_000_000); + header.gas_limit = U256::from(6_800_000); - // Contract: CALL(gas=3000, target, value=0) ; STOP. - // PUSH1 0 x4 (ret/args) PUSH1 0 (value) PUSH20 target PUSH2 3000 CALL STOP - let mut code = vec![0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x73]; - code.extend_from_slice(target.as_slice()); - code.extend_from_slice(&[0x61, 0x0b, 0xb8, 0xf1, 0x00]); // PUSH2 3000, CALL, STOP + // registerBtcTransaction with a truncated argument list: rejected + // before any work, which is rskj's BridgeIllegalArgumentException. + let mut input = { + use sha3::{Digest, Keccak256}; + let h = Keccak256::digest(b"registerBtcTransaction(bytes,int256,bytes)"); + h[..4].to_vec() + }; + input.extend_from_slice(&[0u8; 32]); // far short of the 96 bytes required - let call_tx = rustock_core::Transaction { + let limit = 500_000u64; + 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()), + gas_price: U256::from(1), + gas_limit: U256::from(limit), + to: Bytes::copy_from_slice(crate::precompiles::BRIDGE_ADDR.as_slice()), value: U256::ZERO, - input: Bytes::new(), - v: 0, - r: U256::ZERO, - s: U256::ZERO, + input: Bytes::from(input), + v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, }; + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + let result = executor + .execute_tx(&header, &tx, sender, &root, store) + .expect("a rejected Bridge call fails the transaction, never the block"); - // Run the identical CALL with the target either absent or present-but-empty. - let run = |target_exists: bool| { - let store = Arc::new(MemoryTrieStore::new()); - let root = TrieNode::empty(); - 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 = if target_exists { - // Existing-but-empty (0,0,no-code) account node in the trie. - put_account(&root, store.as_ref(), &target, 0, U256::ZERO) - } else { - root - }; - let code_key_bytes = rustock_trie::code_key(&contract); - let root = root.put( - &TrieKeySlice::from_key(&code_key_bytes), - &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 header = dummy_header(466_503); - let r = executor - .execute_block(&header, &[(call_tx.clone(), sender)], &root, store.clone()) - .expect("block"); - assert!(r.tx_results[0].success); - r.tx_results[0].gas_used - }; - - let gas_absent = run(false); - let gas_exists = run(true); - assert_eq!( - gas_absent - gas_exists, - 25_000, - "NEW_ACCT_CALL charged only when target absent \ - (absent={gas_absent}, exists-empty={gas_exists})" + assert!( + result.success, + "the invisible-exception quirk reports SUCCESS even though the call threw" + ); + assert!( + result.gas_used < limit, + "the receipt must report requiredGas + basicTxCost, not the limit: \ + got {} of {}", + result.gas_used, limit + ); + assert!( + result.gas_used > 22_000, + "and it must include the Bridge's fixed 22,000: got {}", + result.gas_used ); } - /// 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 - /// beneficiary top-up on `had_value && !target_exists` under Spurious Dragon - /// (always active for RSK eras), so a ZERO-balance contract self-destructing - /// to an absent beneficiary would skip the charge. This test pins the rskj - /// behavior: the absent-beneficiary case costs 25,000 more than the - /// existing-beneficiary case even with a zero contract balance. #[test] - fn test_new_acct_suicide_charged_on_existence_regardless_of_value() { + fn test_rskip197_failing_precompile_refunds_surplus_gas() { + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + let sender = Address::repeat_byte(0xCD); let one_rbtc = U256::from(10u64).pow(U256::from(18)); - let sender = Address::repeat_byte(0xAA); - let contract = Address::repeat_byte(0xCC); - let bene = Address::repeat_byte(0xDD); // not a precompile - - // Contract runtime: PUSH20 bene ; SELFDESTRUCT. Contract balance is 0. - let mut code = vec![0x73]; // PUSH20 - code.extend_from_slice(bene.as_slice()); - code.push(0xFF); // SELFDESTRUCT + 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 executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); - // Pad the tx with non-zero calldata so the gross gas is large enough - // that the selfdestruct refund is NOT capped (refund = min(refund, - // gross/2)) in either case. Otherwise the cheaper existing-beneficiary - // case hits the refund cap and the net-gas delta no longer isolates the - // 25,000 top-up. The contract ignores the calldata. - let tx = rustock_core::Transaction { + // Runtime: build a BLAKE2F input (rounds=1, bad final byte), CALL 0x09 + // forwarding 900,000 gas, then SSTORE the CALL result at slot 0. + let runtime: Vec = vec![ + 0x60, 0x01, 0x60, 0x03, 0x53, // PUSH1 1; PUSH1 3; MSTORE8 -> mem[3]=1 (rounds=1) + 0x60, 0x02, 0x60, 0xd4, 0x53, // PUSH1 2; PUSH1 212; MSTORE8 -> mem[212]=2 (bad flag) + 0x60, 0x00, // retLength 0 + 0x60, 0x00, // retOffset 0 + 0x60, 0xd5, // argsLength 213 + 0x60, 0x00, // argsOffset 0 + 0x60, 0x00, // value 0 + 0x60, 0x09, // addr 0x09 (blake2f) + 0x62, 0x0d, 0xbb, 0xa0, // PUSH3 900000 (gas) + 0xf1, // CALL + 0x60, 0x00, 0x55, // PUSH1 0; SSTORE -> slot 0 = CALL result + 0x00, // STOP + ]; + // init: copy `runtime` to memory and RETURN it as the deployed code. + let rt_len = runtime.len() as u8; + let mut init = vec![ + 0x60, rt_len, 0x60, 0x0c, 0x60, 0x00, 0x39, // CODECOPY(0, 12, len) + 0x60, rt_len, 0x60, 0x00, 0xf3, // RETURN(0, len) + ]; + init.extend_from_slice(&runtime); + let deploy = rustock_core::Transaction { nonce: 0, gas_price: U256::from(0), - gas_limit: U256::from(500_000), - to: Bytes::copy_from_slice(contract.as_slice()), + gas_limit: U256::from(1_000_000), + to: Bytes::new(), value: U256::ZERO, - input: Bytes::from(vec![1u8; 500]), - v: 0, - r: U256::ZERO, - s: U256::ZERO, - cached_rlp: None, + input: Bytes::from(init), + v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, }; + let r1 = executor + .execute_block(&dummy_header(3_614_800), &[(deploy, sender)], &root, store.clone()) + .expect("deploy block"); + assert!(r1.tx_results[0].success, "deploy ok"); + let deployed = sender.create(0); + let root2 = + crate::state::apply_state_changes(&root, store.as_ref(), &r1.state_changes, &r1.markers); - let run = |bene_exists: bool| { - let store = Arc::new(MemoryTrieStore::new()); - let root = TrieNode::empty(); - 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 = if bene_exists { - put_account(&root, store.as_ref(), &bene, 0, U256::ZERO) - } else { - root - }; - let code_key_bytes = rustock_trie::code_key(&contract); - let root = root.put( - &TrieKeySlice::from_key(&code_key_bytes), - &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 header = dummy_header(466_503); - let r = executor - .execute_block(&header, &[(tx.clone(), sender)], &root, store.clone()) - .expect("block"); - assert!(r.tx_results[0].success); - r.tx_results[0].gas_used + let call = rustock_core::Transaction { + nonce: 1, + gas_price: U256::from(0), + gas_limit: U256::from(1_000_000), + 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, }; - - let gas_absent = run(false); - let gas_exists = run(true); + let r2 = executor + .execute_block(&dummy_header(3_614_801), &[(call, sender)], &root2, store.clone()) + .expect("call block"); + assert!( + r2.tx_results[0].success, + "post-197 the failing precompile is handled; the caller frame does not abort" + ); + // The CALL pushed 0 (failure) — slot 0 must be 0. + let storage = &r2.state_changes.get(&deployed).expect("contract").storage; assert_eq!( - gas_absent - gas_exists, - 25_000, - "NEW_ACCT_SUICIDE charged on absent beneficiary regardless of value \ - (absent={gas_absent}, exists={gas_exists})" + storage.get(&U256::ZERO).map(|s| s.present_value).unwrap_or(U256::ZERO), + U256::ZERO, + "CALL to a failing precompile pushes 0" + ); + // The surplus of the 900,000 forwarded gas was refunded: the whole tx + // settles well below it (would be >= 900,000 if all child gas burned). + assert!( + r2.tx_results[0].gas_used < 100_000, + "post-197 refunds the surplus forwarded gas; gas_used = {}", + r2.tx_results[0].gas_used ); } - /// Multi-tx version of the #382,134 regression: a later transaction in - /// the SAME block re-reads (and possibly same-value re-writes) the bridge - /// slots written by an earlier one — the slots must still reach the trie. - /// Mirrors #378,129: registerBtcTransaction writes the rejection release, - /// the block's own updateCollections reads it, REMASC closes the block. + /// Regression for mainnet #2,814,761: RSKIP150 lowers the EVM call-stack + /// limit to 400 (rskj Program.getMaxDepth); revm hard-codes 1024. A + /// self-recursive contract must be cut off at depth 400 — frames at + /// depths 0..=400 run, the CALL at depth 400 is refused with the child + /// gas refunded. #[test] - fn test_paid_bridge_storage_survives_same_block_reread() { + fn test_rskip150_call_depth_capped_at_400() { let store = Arc::new(MemoryTrieStore::new()); let root = TrieNode::empty(); - - let sender = Address::repeat_byte(0xAA); - let caller2 = Address::repeat_byte(0xCD); + let sender = Address::repeat_byte(0xAD); let one_rbtc = U256::from(10u64).pow(U256::from(18)); - let root = put_account(&root, store.as_ref(), &sender, 0, one_rbtc * U256::from(2)); - let root = put_account(&root, store.as_ref(), &caller2, 0, one_rbtc); - + 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 header = dummy_header(8_000_000); + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); - // tx1: releaseBtc (writes the release request queue blob) - let tx1 = rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(0), - gas_limit: U256::from(100_000), - to: Bytes::copy_from_slice(crate::precompiles::BRIDGE_ADDR.as_slice()), - value: one_rbtc, - input: Bytes::new(), - v: 0, - r: U256::ZERO, - s: U256::ZERO, - cached_rlp: None, - }; - // tx2: updateCollections (reads the queue; building fails without - // UTXOs so the queue is re-stored byte-identical) - let tx2 = rustock_core::Transaction { + // Runtime: SSTORE(0, SLOAD(0)+1); CALL(self, all gas); STOP. + let runtime: Vec = vec![ + 0x60, 0x00, 0x54, 0x60, 0x01, 0x01, 0x60, 0x00, 0x55, // counter += 1 + 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, // ret/arg/value + 0x30, 0x5a, 0xf1, 0x50, // ADDRESS GAS CALL POP + 0x00, + ]; + let mut init = vec![ + 0x60, runtime.len() as u8, 0x60, 0x0c, 0x60, 0x00, 0x39, + 0x60, runtime.len() as u8, 0x60, 0x00, 0xf3, + ]; + init.extend_from_slice(&runtime); + let deploy = rustock_core::Transaction { nonce: 0, gas_price: U256::from(0), - gas_limit: U256::from(100_000), - to: Bytes::copy_from_slice(crate::precompiles::BRIDGE_ADDR.as_slice()), + gas_limit: U256::from(1_000_000), + to: Bytes::new(), value: U256::ZERO, - input: Bytes::from_static(&[0x0c, 0x5a, 0x99, 0x90]), - v: 0, - r: U256::ZERO, - s: U256::ZERO, - cached_rlp: None, + input: Bytes::from(init), + v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, }; + let r1 = executor + .execute_block(&dummy_header(2_500_000), &[(deploy, sender)], &root, store.clone()) + .expect("deploy block"); + assert!(r1.tx_results[0].success); + let contract = sender.create(0); + let root2 = + crate::state::apply_state_changes(&root, store.as_ref(), &r1.state_changes, &r1.markers); - let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); - let result = executor - .execute_block( - &header, - &[(tx1, sender), (tx2, caller2)], - &root, - store.clone(), - ) - .expect("block executes"); - assert!(result.tx_results[0].success, "releaseBtc succeeds"); - assert!(result.tx_results[1].success, "updateCollections succeeds"); - - // Apply to the trie and read the queue back through a fresh root, - // exactly like the next block would. - let new_root = - crate::state::apply_state_changes(&root, store.as_ref(), &result.state_changes, &result.markers); - let queue_key = { - use sha3::{Digest, Keccak256}; - let mut padded = [0u8; 32]; - let name = b"releaseRequestQueueWithTxHash"; - padded[32 - name.len()..].copy_from_slice(name); - let _ = Keccak256::digest(name); // (key is ascii-padded, not hashed) - padded + let call = rustock_core::Transaction { + nonce: 1, + gas_price: U256::from(0), + gas_limit: U256::from(6_000_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 slot_b256 = B256::from(queue_key); - let key_bytes = rustock_trie::storage_key(&crate::precompiles::BRIDGE_ADDR, &slot_b256); - let key = rustock_trie::TrieKeySlice::from_key(&key_bytes); - let stored = new_root.get(&key, store.as_ref()); - assert!( - stored.is_some(), - "queue length cell must survive into the trie after a same-block re-read" + let r2 = executor + .execute_block(&dummy_header(2_500_001), &[(call, sender)], &root2, store.clone()) + .expect("call block"); + assert!(r2.tx_results[0].success, "recursion must end cleanly at the cap"); + let slot0 = r2 + .state_changes + .get(&contract) + .expect("contract") + .storage + .get(&U256::ZERO) + .expect("slot 0"); + assert_eq!( + slot0.present_value, + U256::from(401), + "frames at depths 0..=400 run; the call at depth 400 is refused" ); } - /// Regression for mainnet #382,134: bridge storage written by a PAID - /// bridge transaction (post-RSKIP88, revm path) must survive into the - /// block's state changes. A paid releaseBtc enqueues a release request - /// (a multi-slot blob under the Bridge account). + /// Regression for mainnet #2,669,886: rskj resets the caller's + /// returnDataBuffer only when callee code actually runs (executeCode) or + /// on precompile calls; a CALL to an empty-code account (plain value + /// transfer) keeps the PREVIOUS call's return data, where canonical + /// EIP-211 clears it. RETURNDATASIZE after such a call must report the + /// prior call's output size. #[test] - fn test_paid_bridge_tx_persists_bridge_storage() { + fn test_empty_code_call_preserves_return_data_buffer() { let store = Arc::new(MemoryTrieStore::new()); let root = TrieNode::empty(); - - let sender = Address::repeat_byte(0xAA); + let sender = Address::repeat_byte(0xAC); let one_rbtc = U256::from(10u64).pow(U256::from(18)); - let root = put_account(&root, store.as_ref(), &sender, 0, one_rbtc * U256::from(2)); - + 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 header = dummy_header(8_000_000); // paid bridge era - - 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::BRIDGE_ADDR.as_slice()), - value: one_rbtc, // 1 RBTC -> well above the pegout minimum - input: Bytes::new(), // empty input = releaseBtc - v: 0, - r: U256::ZERO, - s: U256::ZERO, - cached_rlp: None, - }; - let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); - let result = executor - .execute_block(&header, &[(tx, sender)], &root, store) - .expect("block executes"); - assert!(result.tx_results[0].success, "releaseBtc succeeds"); - - let written = result - .markers - .raw_storage - .iter() - .filter(|(addr, _, v)| *addr == crate::precompiles::BRIDGE_ADDR && v.is_some()) - .count(); - assert!( - written >= 1, - "releaseBtc must persist the release request queue blob, got {written} raw writes" - ); - } - #[test] - fn test_simple_value_transfer() { - let store = Arc::new(MemoryTrieStore::new()); - let root = TrieNode::empty(); + let deploy_tx = |nonce: u64, runtime: &[u8]| { + // Init: copy `runtime` to memory and return it. + let mut init = vec![ + 0x60, runtime.len() as u8, 0x60, 0x0c, 0x60, 0x00, 0x39, + 0x60, runtime.len() as u8, 0x60, 0x00, 0xf3, + ]; + init.extend_from_slice(runtime); + rustock_core::Transaction { + nonce, + gas_price: U256::from(0), + gas_limit: U256::from(1_000_000), + to: Bytes::new(), + value: U256::ZERO, + input: Bytes::from(init), + v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, + } + }; - let sender = Address::repeat_byte(0xAA); - let recipient = Address::repeat_byte(0xBB); - let one_rbtc = U256::from(10u64).pow(U256::from(18)); + // Returner: RETURN 32 zero bytes. + let returner_runtime = vec![0x60, 0x20, 0x60, 0x00, 0xf3]; + let returner = sender.create(0); + let eoa = Address::repeat_byte(0xEE); - let root = put_account(&root, store.as_ref(), &sender, 0, one_rbtc); - - let block_store = Arc::new( - BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap(), - ); + // Main: CALL returner; CALL the EOA; SSTORE(0, RETURNDATASIZE); STOP. + let mut main_runtime = Vec::new(); + let call_to = |code: &mut Vec, addr: Address| { + code.extend_from_slice(&[0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x73]); + code.extend_from_slice(addr.as_slice()); + code.extend_from_slice(&[0x62, 0x0f, 0x42, 0x40, 0xf1, 0x50]); // PUSH3 1M gas; CALL; POP + }; + call_to(&mut main_runtime, returner); + call_to(&mut main_runtime, eoa); + main_runtime.extend_from_slice(&[0x3d, 0x60, 0x00, 0x55, 0x00]); + let main = sender.create(1); - let header = dummy_header(8_000_000); + let header1 = dummy_header(2_500_000); + let r1 = executor + .execute_block( + &header1, + &[(deploy_tx(0, &returner_runtime), sender), (deploy_tx(1, &main_runtime), sender)], + &root, + store.clone(), + ) + .expect("deploy block"); + assert!(r1.tx_results.iter().all(|r| r.success)); + let root2 = + crate::state::apply_state_changes(&root, store.as_ref(), &r1.state_changes, &r1.markers); - let tx = rustock_core::Transaction { - nonce: 0, + let call = rustock_core::Transaction { + nonce: 2, gas_price: U256::from(0), - gas_limit: U256::from(21_000), - to: Bytes::copy_from_slice(recipient.as_slice()), - value: U256::from(1_000), + gas_limit: U256::from(1_000_000), + to: Bytes::copy_from_slice(main.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 executor = RskExecutor::new( - RskHardforkConfig::all_active(33), - block_store, + let r2 = executor + .execute_block(&dummy_header(2_500_001), &[(call, sender)], &root2, store.clone()) + .expect("call block"); + assert!(r2.tx_results[0].success); + let slot0 = r2 + .state_changes + .get(&main) + .expect("main contract") + .storage + .get(&U256::ZERO) + .expect("slot 0"); + assert_eq!( + slot0.present_value, + U256::from(32), + "RETURNDATASIZE after a call to an empty-code account must keep \ + the previous call's 32-byte size (rskj returnDataBuffer)" ); - - let result = executor - .execute_tx(&header, &tx, sender, &root, store) - .unwrap(); - - assert!(result.success); - assert_eq!(result.gas_used, 21_000); } + /// Regression for mainnet #3,616,094: RSKIP171 (iris300) makes + /// `Program.cleanReturnDataBuffer` reset the caller's returnDataBuffer on + /// every call that runs no new frame, so a CALL to an empty-code account + /// CLEARS the buffer (canonical EIP-211) instead of preserving the prior + /// call's output. RETURNDATASIZE after such a call must report 0. + /// Ground truth: rskj VMTest.returnDataSizeAfterCallToNonExistentContract + /// (active = true → 0); the pre-iris twin asserts 32 above. #[test] - fn test_insufficient_balance_fails() { + fn test_empty_code_call_clears_return_data_buffer_post_rskip171() { let store = Arc::new(MemoryTrieStore::new()); let root = TrieNode::empty(); + let sender = Address::repeat_byte(0xAC); + 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 executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); - let sender = Address::repeat_byte(0xAA); - let recipient = Address::repeat_byte(0xBB); + let deploy_tx = |nonce: u64, runtime: &[u8]| { + let mut init = vec![ + 0x60, runtime.len() as u8, 0x60, 0x0c, 0x60, 0x00, 0x39, + 0x60, runtime.len() as u8, 0x60, 0x00, 0xf3, + ]; + init.extend_from_slice(runtime); + rustock_core::Transaction { + nonce, + gas_price: U256::from(0), + gas_limit: U256::from(1_000_000), + to: Bytes::new(), + value: U256::ZERO, + input: Bytes::from(init), + v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, + } + }; - let root = put_account(&root, store.as_ref(), &sender, 0, U256::from(100)); + // Returner: RETURN 32 zero bytes. + let returner_runtime = vec![0x60, 0x20, 0x60, 0x00, 0xf3]; + let returner = sender.create(0); + let eoa = Address::repeat_byte(0xEE); - let block_store = Arc::new( - BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap(), - ); + // Main: CALL returner; CALL the EOA; SSTORE(0, RETURNDATASIZE); STOP. + let mut main_runtime = Vec::new(); + let call_to = |code: &mut Vec, addr: Address| { + code.extend_from_slice(&[0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x73]); + code.extend_from_slice(addr.as_slice()); + code.extend_from_slice(&[0x62, 0x0f, 0x42, 0x40, 0xf1, 0x50]); // PUSH3 1M gas; CALL; POP + }; + call_to(&mut main_runtime, returner); + call_to(&mut main_runtime, eoa); + main_runtime.extend_from_slice(&[0x3d, 0x60, 0x00, 0x55, 0x00]); + let main = sender.create(1); - let header = dummy_header(8_000_000); + // Iris300 activates at mainnet #3,614,800 — pick a block past it. + let header1 = dummy_header(3_616_094); + let r1 = executor + .execute_block( + &header1, + &[(deploy_tx(0, &returner_runtime), sender), (deploy_tx(1, &main_runtime), sender)], + &root, + store.clone(), + ) + .expect("deploy block"); + assert!(r1.tx_results.iter().all(|r| r.success)); + let root2 = + crate::state::apply_state_changes(&root, store.as_ref(), &r1.state_changes, &r1.markers); - let tx = rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(1), - gas_limit: U256::from(21_000), - to: Bytes::copy_from_slice(recipient.as_slice()), - value: U256::from(1_000_000), + let call = rustock_core::Transaction { + nonce: 2, + gas_price: U256::from(0), + gas_limit: U256::from(1_000_000), + to: Bytes::copy_from_slice(main.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 executor = RskExecutor::new( - RskHardforkConfig::all_active(33), - block_store, + let r2 = executor + .execute_block(&dummy_header(3_616_095), &[(call, sender)], &root2, store.clone()) + .expect("call block"); + assert!(r2.tx_results[0].success); + let slot0 = r2 + .state_changes + .get(&main) + .expect("main contract") + .storage + .get(&U256::ZERO) + .map(|s| s.present_value) + .unwrap_or(U256::ZERO); + assert_eq!( + slot0, + U256::ZERO, + "RSKIP171: RETURNDATASIZE after a call to an empty-code account \ + must be 0 (the buffer is cleared, not preserved)" ); - - let result = executor.execute_tx(&header, &tx, sender, &root, store); - assert!(result.is_err() || !result.unwrap().success); } + /// 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_contract_creation_simple() { - let store = Arc::new(MemoryTrieStore::new()); - let root = TrieNode::empty(); - - let sender = Address::repeat_byte(0xDD); + fn test_refused_create_keeps_return_data_buffer_at_every_height() { 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 header = dummy_header(8_000_000); - - // PUSH1 0x00 PUSH1 0x00 RETURN (returns empty code) - let initcode = vec![0x60, 0x00, 0x60, 0x00, 0xF3]; - - let tx = rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(0), - gas_limit: U256::from(100_000), - to: Bytes::new(), - value: U256::ZERO, - input: Bytes::from(initcode), - 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(); + 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]); - assert!(result.success); - assert!(result.gas_used > 21_000); + 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 testnet #5,214,732: when a creation's init code reverts, + /// rskj's `Program.getProgramResult` sets the creator's returnDataBuffer to + /// `result.getHReturn()`, the creator's own result rather than the + /// creation's (`programResult`). A program still running has returned + /// nothing, so RETURNDATASIZE after a reverted CREATE reads 0 where revm + /// (EIP-211) keeps the revert data, and no activation guards it. A CALL + /// that reverts still hands its data over. RSKj's release VETIVER-9.0.4 + /// reads 0 after such a CREATE and 36 after such a CALL. #[test] - fn test_block_execution_multiple_txs() { - let store = Arc::new(MemoryTrieStore::new()); - let root = TrieNode::empty(); - - let sender = Address::repeat_byte(0xEE); - let recipient1 = Address::repeat_byte(0x11); - let recipient2 = Address::repeat_byte(0x22); - + fn test_reverted_create_leaves_return_data_empty_at_every_height() { let one_rbtc = U256::from(10u64).pow(U256::from(18)); - let root = put_account(&root, store.as_ref(), &sender, 0, one_rbtc); + let sender = Address::repeat_byte(0xAC); + // Reverter: REVERT with 36 zero bytes. + let reverter = Address::repeat_byte(0xB8); + let reverter_code = [0x60, 0x24, 0x60, 0x00, 0xfd]; + // Main: CALL reverter; POP; SSTORE(1, RETURNDATASIZE + 1); + // CREATE(0, init that REVERTs with 68 zero bytes); POP; + // SSTORE(0, RETURNDATASIZE + 1); 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(reverter.as_slice()); + main_code.extend_from_slice(&[0x5a, 0xf1, 0x50]); + main_code.extend_from_slice(&[0x3d, 0x60, 0x01, 0x01, 0x60, 0x01, 0x55]); + // PUSH5 0x60446000fd; PUSH1 0; MSTORE; CREATE(0, 27, 5); POP. + main_code.extend_from_slice(&[ + 0x64, 0x60, 0x44, 0x60, 0x00, 0xfd, 0x60, 0x00, 0x52, 0x60, 0x05, 0x60, 0x1b, 0x60, + 0x00, 0xf0, 0x50, + ]); + main_code.extend_from_slice(&[0x3d, 0x60, 0x01, 0x01, 0x60, 0x00, 0x55, 0x00]); - let block_store = Arc::new( - BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap(), + 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(), + &reverter, + 1, + U256::ZERO, + &reverter_code, ); - - let header = dummy_header(8_000_000); - - let tx1 = rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(0), - gas_limit: U256::from(21_000), - to: Bytes::copy_from_slice(recipient1.as_slice()), - value: U256::from(1_000), - input: Bytes::new(), - v: 0, - r: U256::ZERO, - s: U256::ZERO, - cached_rlp: None, - }; - - let tx2 = rustock_core::Transaction { - nonce: 1, - gas_price: U256::from(0), - gas_limit: U256::from(21_000), - to: Bytes::copy_from_slice(recipient2.as_slice()), - value: U256::from(2_000), - input: Bytes::new(), - v: 0, - r: U256::ZERO, - s: U256::ZERO, - cached_rlp: None, - }; - - let transactions = vec![(tx1, sender), (tx2, sender)]; - - let executor = RskExecutor::new( - RskHardforkConfig::all_active(33), - block_store, - ); - - let result = executor - .execute_block(&header, &transactions, &root, store) - .unwrap(); - - assert_eq!(result.tx_results.len(), 2); - assert!(result.tx_results[0].success); - assert!(result.tx_results[1].success); - assert_eq!(result.gas_used, 42_000); - } - - #[test] - fn test_hardfork_spec_selection() { - let cfg = RskHardforkConfig::mainnet(); - - assert_eq!(cfg.spec_id(0), SpecId::BYZANTIUM); - assert_eq!(cfg.spec_id(2_500_000), SpecId::PETERSBURG); - assert_eq!(cfg.spec_id(6_300_000), SpecId::ISTANBUL); - assert_eq!(cfg.spec_id(7_500_000), SpecId::SHANGHAI); + 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), after it, and after vetiver900 (#8,804,200). + for number in [3_000_000u64, 3_616_094, 8_900_000] { + 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 slot = |key: u64| { + r.state_changes + .get(&main) + .and_then(|a| a.storage.get(&U256::from(key))) + .map_or(U256::ZERO, |s| s.present_value) + }; + assert_eq!( + slot(1), + U256::from(37), + "#{number}: RETURNDATASIZE + 1 after the reverted CALL" + ); + assert_eq!( + slot(0), + U256::from(1), + "#{number}: RETURNDATASIZE + 1 after the reverted CREATE" + ); + } } - // ----------------------------------------------------------------------- - // Tests ported from rskj's BlockExecutorTest / TransactionExecutorTest - // ----------------------------------------------------------------------- - - /// Ported from rskj's BlockExecutorTest.executeBlockWithOneTransaction. - /// Validates exact balance deduction: gas_used * gas_price + value. + /// 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 + /// precompile (0x01000010) reverted in rustock (size 0) but not in rskj. #[test] - fn test_rskj_execute_block_with_one_transaction_balance_check() { + fn test_extcodesize_of_precompile_is_max() { 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 executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); - let sender = Address::repeat_byte(0xA1); - let recipient = Address::repeat_byte(0xB2); + // Runtime: PUSH20 0x..01000010; EXTCODESIZE; PUSH1 0; SSTORE; STOP. + let mut runtime: Vec = vec![0x73]; + runtime.extend_from_slice(crate::precompiles::BLOCK_HEADER_ADDR.as_slice()); + runtime.extend_from_slice(&[0x3b, 0x60, 0x00, 0x55, 0x00]); + let mut init = vec![ + 0x60, runtime.len() as u8, 0x60, 0x0c, 0x60, 0x00, 0x39, + 0x60, runtime.len() as u8, 0x60, 0x00, 0xf3, + ]; + init.extend_from_slice(&runtime); - // rskj test: sender starts with 30_000, sends 10 wei, gas_price=1, gas_limit=21_000 - let root = put_account(&root, store.as_ref(), &sender, 0, U256::from(30_000)); - let root = put_account(&root, store.as_ref(), &recipient, 0, U256::from(10)); + let deploy = rustock_core::Transaction { + nonce: 0, gas_price: U256::ZERO, gas_limit: U256::from(1_000_000), + to: Bytes::new(), value: U256::ZERO, input: Bytes::from(init), + v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, + }; + let header1 = dummy_header(1_700_000); + let r1 = executor + .execute_block(&header1, &[(deploy, sender)], &root, store.clone()) + .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 block_store = Arc::new( - BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap(), + let call = rustock_core::Transaction { + nonce: 1, gas_price: U256::ZERO, gas_limit: U256::from(1_000_000), + 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, + }; + let header2 = dummy_header(1_700_001); + let r2 = executor + .execute_block(&header2, &[(call, sender)], &root2, store.clone()) + .expect("call block"); + assert!(r2.tx_results[0].success); + let slot0 = r2.state_changes.get(&deployed).expect("contract") + .storage.get(&U256::ZERO).expect("slot 0"); + assert_eq!( + slot0.present_value, U256::MAX, + "EXTCODESIZE of an active precompile must be 2^256-1 (RSKIP90)" ); + } - let header = dummy_header(8_000_000); + /// rskj `VM.getMessageCall` adds the 2,300 stipend of a call that moves + /// value to the gas the call asks for. Before RSKIP150 (twoToThree, + /// mainnet #2,018,000) that is a plain `long` sum: a request of 2^63 - 2,300 + /// or more (`Program.limitToMaxLong` holds any larger word at 2^63 - 1) + /// takes it below zero, `GasCost.add` throws for the negative callee gas, + /// and the frame that made the call fails with all its gas. From RSKIP150 + /// the sum stops at 2^63 - 1 and the call goes ahead. + #[test] + fn test_pre_rskip150_stipend_sum_past_a_long_fails_the_caller() { + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let sender = Address::repeat_byte(0xAD); + // Main: CALL(gas = the calldata's word, sender, value 1, no input, no + // output); POP; SSTORE(0, 1); STOP. + let main = Address::repeat_byte(0xBA); + let mut main_code = vec![ + 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x01, 0x73, + ]; + main_code.extend_from_slice(sender.as_slice()); + main_code.extend_from_slice(&[ + 0x60, 0x00, 0x35, 0xf1, 0x50, 0x60, 0x01, 0x60, 0x00, 0x55, 0x00, + ]); - let tx = rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(1), - gas_limit: U256::from(21_000), - to: Bytes::copy_from_slice(recipient.as_slice()), - value: U256::from(10), - input: Bytes::new(), - v: 0, - r: U256::ZERO, - s: U256::ZERO, - cached_rlp: None, + 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(), + &main, + 1, + U256::from(1_000), + &main_code, + ); + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + let run = |number: u64, gas: U256| { + let call = rustock_core::Transaction { + input: Bytes::from(gas.to_be_bytes::<32>().to_vec()), + ..plain_tx(0, &main, U256::ZERO) + }; + let r = executor + .execute_block( + &dummy_header(number), + &[(call, sender)], + &root, + store.clone(), + ) + .expect("block"); + let stored = r + .state_changes + .get(&main) + .and_then(|a| a.storage.get(&U256::ZERO)) + .is_some_and(|s| s.present_value == U256::from(1)); + (r.tx_results[0].success, r.tx_results[0].gas_used, stored) }; - - let executor = RskExecutor::new( - RskHardforkConfig::all_active(33), - block_store, + // 2^63 - 2,300, the least a sum with the stipend takes past a long. + let wraps = U256::from(i64::MAX as u64 - 2_299); + assert!( + run(2_000_000, wraps - U256::from(1)).2, + "2^63 - 2,301 and the stipend make a long" ); + for gas in [wraps, U256::MAX] { + assert_eq!( + run(2_000_000, gas), + (false, 1_000_000, false), + "{gas:#x} before RSKIP150: the caller fails with all its gas" + ); + assert!(run(2_018_000, gas).2, "{gas:#x} from RSKIP150"); + } + } - let result = executor - .execute_block(&header, &[(tx, sender)], &root, store) - .unwrap(); - - assert_eq!(result.tx_results.len(), 1); - assert!(result.tx_results[0].success); - assert_eq!(result.tx_results[0].gas_used, 21_000); - assert_eq!(result.gas_used, 21_000); + /// rskj `VM.calculateCallValue`: before RSKIP103 (orchid060, mainnet + /// #1,052,700) STATICCALL takes a value off the stack as CALL does, seven + /// arguments in all. The value pays `VT_CALL` and brings the stipend, and + /// `Program.callToAddress` moves it to the callee, which runs static. Only + /// CALL refuses a value inside a static call, so a contract that runs + /// static moves one this way too. From RSKIP103 the value is always zero, + /// and STATICCALL takes six. + #[test] + fn test_pre_rskip103_staticcall_takes_a_value() { + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let sender = Address::repeat_byte(0xAE); + let payee = Address::repeat_byte(0xBC); + // STATICCALL(gas, `to`, `value`, no input, no output), as rskj reads it + // before RSKIP103. + let static_call = |to: &Address, value: u8| { + let mut code = vec![ + 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, value, 0x73, + ]; + code.extend_from_slice(to.as_slice()); + code.extend_from_slice(&[0x5a, 0xfa]); + code + }; + // Main: STATICCALL(payee, value 5); SSTORE(0, its result); STOP. + let main = Address::repeat_byte(0xBB); + let mut main_code = static_call(&payee, 5); + main_code.extend_from_slice(&[0x60, 0x00, 0x55, 0x00]); + // Relay, which runs static: STATICCALL(payee, value 5); STOP. + let relay = Address::repeat_byte(0xC1); + let mut relay_code = static_call(&payee, 5); + relay_code.push(0x00); + // Outer: STATICCALL(relay, value 0); SSTORE(0, its result); STOP. + let outer = Address::repeat_byte(0xC2); + let mut outer_code = static_call(&relay, 0); + outer_code.extend_from_slice(&[0x60, 0x00, 0x55, 0x00]); - // Verify state changes match rskj: - // sender final = 30_000 - 21_000 (gas*price) - 10 (value) = 8_990 - let sender_state = result.state_changes.get(&sender) - .expect("sender should have state changes"); + let store = Arc::new(MemoryTrieStore::new()); + let root = put_account(&TrieNode::empty(), store.as_ref(), &sender, 0, one_rbtc); + let root = put_account(&root, store.as_ref(), &payee, 0, U256::ZERO); + let root = put_account_with_code( + &root, + store.as_ref(), + &main, + 1, + U256::from(1_000), + &main_code, + ); + let root = put_account_with_code( + &root, + store.as_ref(), + &relay, + 1, + U256::from(1_000), + &relay_code, + ); + let root = put_account_with_code(&root, store.as_ref(), &outer, 1, U256::ZERO, &outer_code); + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + let run = |number: u64, to: &Address| { + let call = plain_tx(0, to, 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 result = r + .state_changes + .get(to) + .and_then(|a| a.storage.get(&U256::ZERO)) + .map_or(U256::ZERO, |s| s.present_value); + assert_eq!(result, U256::from(1), "#{number}: the STATICCALL succeeds"); + let paid = r + .state_changes + .get(&payee) + .map_or(U256::ZERO, |a| a.info.balance); + (paid, r.tx_results[0].gas_used) + }; + let (paid_before, gas_before) = run(1_000_000, &main); + let (paid_after, gas_after) = run(1_100_000, &main); assert_eq!( - sender_state.info.balance, - U256::from(8_990), - "sender balance should be 30000 - 21000*1 - 10 = 8990" + paid_before, + U256::from(5), + "before RSKIP103 the value moves" ); - assert_eq!(sender_state.info.nonce, 1, "sender nonce should increment to 1"); - - // recipient final = 10 (initial) + 10 (received) = 20 - let recipient_state = result.state_changes.get(&recipient) - .expect("recipient should have state changes"); + assert_eq!(paid_after, U256::ZERO, "from RSKIP103 there is none"); assert_eq!( - recipient_state.info.balance, - U256::from(20), - "recipient balance should be 10 + 10 = 20" + gas_before - gas_after, + 9_000, + "VT_CALL, the stipend coming back unspent" + ); + assert_eq!( + run(1_000_000, &outer).0, + U256::from(5), + "before RSKIP103, from a contract that runs static too" ); + assert_eq!(run(1_100_000, &outer).0, U256::ZERO); } - /// Ported from rskj TransactionExecutorTest.testInitHandlesFreeTransactionsOK. - /// Sender with zero balance but non-zero value transfer should fail. + /// rskj `Program.executePrecompiled`, which runs a contract's call to a + /// precompile before RSKIP197, makes the output the caller's return data + /// only from RSKIP90 (orchid, mainnet #729,000): before it RETURNDATASIZE + /// still reads the call before. A precompile call refused for its gas + /// cleans the return data only from RSKIP171 (iris300, #3,614,800), as + /// `cleanReturnDataBuffer` does nothing before it. #[test] - fn test_rskj_zero_balance_sender_with_value_fails() { - let store = Arc::new(MemoryTrieStore::new()); - let root = TrieNode::empty(); - - let sender = Address::repeat_byte(0x01); - let recipient = Address::repeat_byte(0x02); - - // Sender has zero balance - let root = put_account(&root, store.as_ref(), &sender, 0, U256::ZERO); + fn test_precompile_calls_and_the_return_data_by_height() { + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let sender = Address::repeat_byte(0xAF); + // Returner: RETURN 36 zero bytes. + let returner = Address::repeat_byte(0xBD); + let returner_code = [0x60, 0x24, 0x60, 0x00, 0xf3]; + // Main: CALL returner; POP; + // CALL identity (0x04) on 4 bytes with all the gas; POP; + // SSTORE(0, RETURNDATASIZE + 1); + // CALL identity on 32 bytes with no gas, which can't pay it; POP; + // SSTORE(1, RETURNDATASIZE + 1); STOP. + let main = Address::repeat_byte(0xBE); + let mut main_code = vec![ + 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x73, + ]; + main_code.extend_from_slice(returner.as_slice()); + main_code.extend_from_slice(&[0x5a, 0xf1, 0x50]); + main_code.extend_from_slice(&[ + 0x60, 0x00, 0x60, 0x00, 0x60, 0x04, 0x60, 0x00, 0x60, 0x00, 0x60, 0x04, 0x5a, 0xf1, + 0x50, + ]); + main_code.extend_from_slice(&[0x3d, 0x60, 0x01, 0x01, 0x60, 0x00, 0x55]); + main_code.extend_from_slice(&[ + 0x60, 0x00, 0x60, 0x00, 0x60, 0x20, 0x60, 0x00, 0x60, 0x00, 0x60, 0x04, 0x60, 0x00, + 0xf1, 0x50, + ]); + main_code.extend_from_slice(&[0x3d, 0x60, 0x01, 0x01, 0x60, 0x01, 0x55, 0x00]); - let block_store = Arc::new( - BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap(), + 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(), + &returner, + 1, + U256::ZERO, + &returner_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 orchid, before iris300, and after it. + for (number, after_return, after_refusal) in [ + (700_000u64, 37u64, 37u64), + (1_700_000, 5, 5), + (3_700_000, 5, 1), + ] { + 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 slot = |key: u64| { + r.state_changes + .get(&main) + .and_then(|a| a.storage.get(&U256::from(key))) + .map_or(U256::ZERO, |s| s.present_value) + }; + assert_eq!( + slot(0), + U256::from(after_return), + "#{number}: RETURNDATASIZE + 1 after identity returned" + ); + assert_eq!( + slot(1), + U256::from(after_refusal), + "#{number}: RETURNDATASIZE + 1 after identity was refused" + ); + } + } - let header = dummy_header(8_000_000); - - let tx = rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(1), - gas_limit: U256::from(21_000), - to: Bytes::copy_from_slice(recipient.as_slice()), - value: U256::from(68_000), - input: Bytes::new(), - v: 0, - r: U256::ZERO, - s: U256::ZERO, - cached_rlp: None, + /// Regression for mainnet #1,071,481: before RSKIP453 (lovell700) an + /// INTERNAL create whose constructor succeeds always deploys — an + /// unpayable code deposit leaves the contract with EMPTY code, the + /// constructor state committed and the address pushed (rskj + /// Program.finalizeContractCreation), instead of EIP-2's out-of-gas. + /// The RNS registrar deploys a Deed whose 944,800-gas deposit cannot + /// fit in the 600,000 tx limit; rskj deploys it codeless and the + /// auction proceeds (tx gas 259,460, status success). + #[test] + fn test_pre_rskip453_internal_create_with_unpayable_deposit_deploys_empty() { + // Child initcode: RETURN(0, 0x10000) — 65,536 zero bytes of "code", + // deposit 13.1M gas, unpayable within the 1M tx limit. + // Caller runtime: MSTORE initcode; CREATE; SSTORE(0, address). + let runtime: Vec = vec![ + 0x66, 0x62, 0x01, 0x00, 0x00, 0x60, 0x00, 0xf3, // PUSH7 initcode + 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 mut init = vec![ + 0x60, 0x15, 0x60, 0x0c, 0x60, 0x00, 0x39, 0x60, 0x15, 0x60, 0x00, 0xf3, + ]; + init.extend_from_slice(&runtime); + + let run_at = |block: u64, init: Vec| { + 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 executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + + let deploy = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(0), + gas_limit: U256::from(1_000_000), + to: Bytes::new(), + value: U256::ZERO, + input: Bytes::from(init), + v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, + }; + let header1 = dummy_header(block); + let r1 = executor + .execute_block(&header1, &[(deploy, sender)], &root, store.clone()) + .expect("deploy block"); + assert!(r1.tx_results[0].success); + let root2 = + crate::state::apply_state_changes(&root, store.as_ref(), &r1.state_changes, &r1.markers); + + let caller = sender.create(0); + let call = rustock_core::Transaction { + nonce: 1, + gas_price: U256::from(0), + gas_limit: U256::from(1_000_000), + to: Bytes::copy_from_slice(caller.as_slice()), + value: U256::ZERO, + input: Bytes::new(), + v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, + }; + let header2 = dummy_header(block + 1); + let r2 = executor + .execute_block(&header2, &[(call, sender)], &root2, store.clone()) + .expect("create block"); + let slot0 = r2.state_changes.get(&caller).and_then(|a| { + a.storage.get(&U256::ZERO).map(|s| s.present_value) + }); + (caller, slot0, r2) }; - let executor = RskExecutor::new( - RskHardforkConfig::all_active(33), - block_store, + // Pre-RSKIP453: the create "succeeds" — address pushed, empty code. + let (caller, slot0, r2) = run_at(1_071_481, init.clone()); + assert!(r2.tx_results[0].success); + let child = caller.create(0); + assert_eq!( + Address::from_slice(&slot0.expect("slot 0 written").to_be_bytes::<32>()[12..]), + child, + "unpayable deposit must still deploy (empty code) pre-RSKIP453" + ); + let child_acct = r2.state_changes.get(&child).expect("child account exists"); + assert!( + child_acct + .info + .code + .as_ref() + .map(|c| c.is_empty()) + .unwrap_or(true), + "child must have empty code" ); - let result = executor.execute_tx(&header, &tx, sender, &root, store); - // Should fail: sender can't cover gas_limit * gas_price + value - assert!(result.is_err(), "zero-balance sender with value transfer should fail"); + // Post-RSKIP453 (lovell700): EIP-2/EIP-170 semantics — the failed + // create consumes the forwarded gas (no 63/64 in RSK) and the + // caller goes out of gas with it. + let (_, _, r2) = run_at(7_338_024, init); + assert!( + !r2.tx_results[0].success, + "unpayable/oversize deposit must fail post-RSKIP453" + ); } - /// Ported from rskj's TransactionTest.testTransactionCost patterns. - /// Verifies that non-zero calldata bytes cost 16 gas each (post-RSKIP400/Istanbul). + /// Regression for mainnet #764,123: addLockWhitelistAddress is disabled + /// post-RSKIP87 (orchid), so rskj's parseData returns null. The gas is + /// the flat releaseBtc cost (23,000, no data cost) and — although + /// RSKIP88 makes execute() throw — a direct transaction records SUCCESS + /// with empty output because TransactionExecutor spends only + /// requiredGas + basicTxCost and go() (vm == null) just commits. + /// Real receipt: status 0x1, gasUsed 47,408 (21,000 + 3,408 + 23,000). #[test] - fn test_rskj_calldata_cost_post_istanbul() { + fn test_disabled_bridge_method_post_rskip87_charges_flat_cost() { let store = Arc::new(MemoryTrieStore::new()); let root = TrieNode::empty(); - let sender = Address::repeat_byte(0xCC); - let recipient = Address::repeat_byte(0xDD); + let sender = Address::from_slice( + &alloy_primitives::hex::decode("6ba9d41b07da470fe340cbd439a42538795eb75b").unwrap(), + ); 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(), - ); - - // Post-Arrowhead (ISTANBUL) → non-zero byte cost = 16 - let header = dummy_header(8_000_000); - - // 1 non-zero byte of calldata + // addLockWhitelistAddress("1HqQJ9PxdG51NmFij43bBUrmQRDULbnSzW", 1 gwei) + let input = alloy_primitives::hex::decode( + "502bbbce0000000000000000000000000000000000000000000000000000000000000040\ + 000000000000000000000000000000000000000000000000000000003b9aca00\ + 0000000000000000000000000000000000000000000000000000000000000022\ + 314871514a395078644735314e6d46696a3433624255726d515244554c626e53\ + 7a57000000000000000000000000000000000000000000000000000000000000", + ) + .unwrap(); let tx = rustock_core::Transaction { nonce: 0, - gas_price: U256::from(0), - gas_limit: U256::from(30_000), - to: Bytes::copy_from_slice(recipient.as_slice()), + gas_price: U256::from(183_000_000u64), // mainnet tx gas price + gas_limit: U256::from(1_000_000), + to: Bytes::copy_from_slice(crate::precompiles::BRIDGE_ADDR.as_slice()), value: U256::ZERO, - input: Bytes::from(vec![0xFF]), // 1 non-zero byte + 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 block_store = + Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + let header = dummy_header(764_123); let result = executor - .execute_tx(&header, &tx, sender, &root, store) - .unwrap(); - - assert!(result.success); - // Intrinsic gas = 21000 (base) + 16 (1 non-zero byte at Istanbul rate) - assert_eq!(result.gas_used, 21_016, "intrinsic gas = 21000 + 16*1"); + .execute_block(&header, &[(tx, sender)], &root, store.clone()) + .expect("block executes"); + assert!(result.tx_results[0].success, "receipt records SUCCESS"); + assert_eq!( + result.tx_results[0].gas_used, 47_408, + "flat releaseBtc cost, no data cost, no method cost" + ); + // The disabled method must NOT execute: no bridge storage writes. + if let Some(bridge) = result.state_changes.get(&crate::precompiles::BRIDGE_ADDR) { + assert!( + bridge.storage.is_empty(), + "disabled method must not touch bridge storage" + ); + } + // The FEE side sees the failure: the sender is charged the FULL gas + // limit and REMASC receives it (rskj summary.getFee when failed). + // Remote groundtruth: sender 0x6ba9...75b balance drops by exactly + // 1,000,000 * 183,000,000 = 183e12 wei across #764,122 -> #764,123. + let full_fee = U256::from(1_000_000u64) * U256::from(183_000_000u64); + assert_eq!( + result.state_changes.get(&sender).unwrap().info.balance, + one_rbtc - full_fee, + "sender pays for the full gas limit, no leftover refund" + ); + assert_eq!( + result + .state_changes + .get(&crate::precompiles::REMASC_ADDR) + .unwrap() + .info + .balance, + full_fee, + "REMASC receives gasLimit * gasPrice" + ); + assert_eq!(result.paid_fees, full_fee, "paid_fees tracks the full fee"); } - /// Complementary test: pre-Istanbul calldata cost is 68 per non-zero byte. + /// The same disabled-method profile routed through revm (post-RSKIP136, + /// mainnet #765,073 used gasPrice 60M wei): receipt SUCCESS with only + /// the spent 47,408 gas, but the sender pays for the full 1,000,000 + /// limit, REMASC receives it, and the endowment is NOT transferred + /// (rskj TransactionExecutor.call skips it when execute() throws). #[test] - fn test_rskj_calldata_cost_pre_istanbul() { + fn test_disabled_bridge_method_charges_full_gas_limit_post_rskip136() { let store = Arc::new(MemoryTrieStore::new()); let root = TrieNode::empty(); - let sender = Address::repeat_byte(0xCC); - let recipient = Address::repeat_byte(0xDD); + 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(), - ); - - // Pre-Orchid (BYZANTIUM) → non-zero byte cost = 68 - let mut header = dummy_header(100); - header.gas_limit = U256::from(6_800_000); - + // addLockWhitelistAddress selector + garbage args: disabled forever + // post-RSKIP87, parse failure at every later era. + let mut input = vec![0x50, 0x2b, 0xbb, 0xce]; + input.extend_from_slice(&[0u8; 32]); let tx = rustock_core::Transaction { nonce: 0, - gas_price: U256::from(0), - gas_limit: U256::from(30_000), - to: Bytes::copy_from_slice(recipient.as_slice()), - value: U256::ZERO, - input: Bytes::from(vec![0xFF]), + gas_price: U256::from(60_000_000u64), + gas_limit: U256::from(1_000_000), + to: Bytes::copy_from_slice(crate::precompiles::BRIDGE_ADDR.as_slice()), + value: U256::from(7), // endowment must stay with the sender + input: Bytes::from(input), v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, }; - let executor = RskExecutor::new( - RskHardforkConfig::mainnet(), - block_store, - ); + 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_392_700); // papyrus200: RSKIP136 active let result = executor - .execute_tx(&header, &tx, sender, &root, store) - .unwrap(); - - assert!(result.success); - // Pre-Istanbul: intrinsic gas = 21000 + 68*1 = 21068 - assert_eq!(result.gas_used, 21_068, "intrinsic gas = 21000 + 68*1"); - } - - /// Regression for mainnet #6,223,700 (the arrowhead600 activation block; - /// gas mismatch header=236343 vs computed=287919). RSKIP400 (Arrowhead600) - /// reduces the non-zero calldata byte cost from 68 to 16 (EIP-2028). revm's - /// intrinsic gas keys off the SpecId, so arrowhead600 maps to ISTANBUL to - /// pick up the calldata reduction; the other Istanbul gas repricings RSK - /// never adopted (EIP-1884 SLOAD/BALANCE/EXTCODEHASH) are pinned back to - /// rskj values in `rsk_instructions::install`. This test pins the FIRST - /// arrowhead600 block exactly: at #6,223,700 calldata is 16/byte; one block - /// earlier 68. + .execute_block(&header, &[(tx, sender)], &root, store.clone()) + .expect("block executes"); + assert!(result.tx_results[0].success, "receipt records SUCCESS"); + // 21,000 + 4 nonzero * 68 + 32 zero * 4 + 23,000 = 44,400 + assert_eq!( + result.tx_results[0].gas_used, + 21_000 + 4 * 68 + 32 * 4 + 23_000, + "receipt shows only the spent gas" + ); + let full_fee = U256::from(1_000_000u64) * U256::from(60_000_000u64); + assert_eq!( + result.state_changes.get(&sender).unwrap().info.balance, + one_rbtc - full_fee, + "sender pays the full limit and keeps the endowment" + ); + assert_eq!( + result + .state_changes + .get(&crate::precompiles::REMASC_ADDR) + .unwrap() + .info + .balance, + full_fee, + "REMASC receives gasLimit * gasPrice" + ); + let bridge_balance = result + .state_changes + .get(&crate::precompiles::BRIDGE_ADDR) + .map(|a| a.info.balance) + .unwrap_or_default(); + assert_eq!(bridge_balance, U256::ZERO, "no endowment on invisible exception"); + assert_eq!(result.paid_fees, full_fee); + } + + /// rskj TransactionExecutor.call transfers the endowment after a + /// non-throwing precompile execute(); the pre-RSKIP136 direct path must + /// do the same for successful Bridge calls. + #[test] + fn test_pre_rskip136_direct_bridge_success_transfers_endowment() { + 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); + + // voteFeePerKbChange(int256) — enabled at every era; an unauthorized + // sender gets an error CODE in the output but the call succeeds. + let mut input = vec![0x04, 0x61, 0x31, 0x3e]; + let mut arg = [0u8; 32]; + arg[30..].copy_from_slice(&30_000u16.to_be_bytes()); + input.extend_from_slice(&arg); + 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::from(5), + input: Bytes::from(input), + 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 header = dummy_header(764_123); // pre-RSKIP136 direct path + let result = executor + .execute_block(&header, &[(tx, sender)], &root, store.clone()) + .expect("block executes"); + assert!(result.tx_results[0].success); + assert_eq!( + result + .state_changes + .get(&crate::precompiles::BRIDGE_ADDR) + .unwrap() + .info + .balance, + U256::from(5), + "successful direct bridge call transfers the endowment" + ); + assert_eq!( + result.state_changes.get(&sender).unwrap().info.balance, + one_rbtc - U256::from(5), + ); + } + + /// Regression for mainnet #466,503: rskj charges NEW_ACCT_CALL (25,000) + /// on trie EXISTENCE — the first-ever internal call to a precompile + /// creates its account node (zero-value transfer), so the charge applies + /// once per address in the chain's lifetime, not once per call. The same + /// ecrecover call in the next block must cost exactly 25,000 less. + #[test] + fn test_precompile_new_account_charged_only_once_across_blocks() { + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + + let sender = Address::repeat_byte(0xAA); + let contract = Address::repeat_byte(0xCC); + 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); + + // PUSH1 0 x4 (ret/args) PUSH1 0 (value) PUSH20 0x..01 PUSH2 3000 CALL STOP + let mut code = vec![0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x73]; + let mut ecrecover = [0u8; 20]; + ecrecover[19] = 0x01; + code.extend_from_slice(&ecrecover); + code.extend_from_slice(&[0x61, 0x0b, 0xb8, 0xf1, 0x00]); // PUSH2 3000, CALL, STOP + let code_key_bytes = rustock_trie::code_key(&contract); + let root = root.put( + &TrieKeySlice::from_key(&code_key_bytes), + &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 call_tx = |nonce: u64| rustock_core::Transaction { + nonce, + 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 header = dummy_header(466_503); + let r1 = executor + .execute_block(&header, &[(call_tx(0), sender)], &root, store.clone()) + .expect("block 1"); + assert!(r1.tx_results[0].success); + + // Apply to the trie: the ecrecover account node must now exist. + let root2 = + crate::state::apply_state_changes(&root, store.as_ref(), &r1.state_changes, &r1.markers); + + let header2 = dummy_header(466_504); + let r2 = executor + .execute_block(&header2, &[(call_tx(1), sender)], &root2, store.clone()) + .expect("block 2"); + assert!(r2.tx_results[0].success); + + let g1 = r1.tx_results[0].gas_used; + let g2 = r2.tx_results[0].gas_used; + assert_eq!( + g1 - g2, + 25_000, + "first call pays NEW_ACCT_CALL once (got {g1} then {g2})" + ); + } + + /// rskj charges NEW_ACCT_CALL (25,000) on trie EXISTENCE + /// (`track.isExist(addr)`), NOT on EIP-161 emptiness. An account that + /// exists in the unitrie with `(nonce=0, balance=0, no code)` — e.g. a + /// zero-paid miner, or an account re-created after a same-block + /// selfdestruct — is NOT charged, because rustock pins the journal spec to + /// HOMESTEAD (`rsk_handler`), making revm's `is_empty` collapse to + /// `is_loaded_as_not_existing` (DB returned `None`) rather than raw EIP-161 + /// emptiness. A CALL to a truly-absent address IS charged. The 25,000 delta + /// between the two cases proves the charge keys on existence, not emptiness. + #[test] + fn test_new_acct_call_charged_on_existence_not_emptiness() { + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let sender = Address::repeat_byte(0xAA); + let contract = Address::repeat_byte(0xCC); + let target = Address::repeat_byte(0xDD); // not a precompile + + // Contract: CALL(gas=3000, target, value=0) ; STOP. + // PUSH1 0 x4 (ret/args) PUSH1 0 (value) PUSH20 target PUSH2 3000 CALL STOP + let mut code = vec![0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x73]; + code.extend_from_slice(target.as_slice()); + code.extend_from_slice(&[0x61, 0x0b, 0xb8, 0xf1, 0x00]); // PUSH2 3000, CALL, STOP + + let call_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, + }; + + // Run the identical CALL with the target either absent or present-but-empty. + let run = |target_exists: bool| { + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + 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 = if target_exists { + // Existing-but-empty (0,0,no-code) account node in the trie. + put_account(&root, store.as_ref(), &target, 0, U256::ZERO) + } else { + root + }; + let code_key_bytes = rustock_trie::code_key(&contract); + let root = root.put( + &TrieKeySlice::from_key(&code_key_bytes), + &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 header = dummy_header(466_503); + let r = executor + .execute_block(&header, &[(call_tx.clone(), sender)], &root, store.clone()) + .expect("block"); + assert!(r.tx_results[0].success); + r.tx_results[0].gas_used + }; + + let gas_absent = run(false); + let gas_exists = run(true); + assert_eq!( + gas_absent - gas_exists, + 25_000, + "NEW_ACCT_CALL charged only when target absent \ + (absent={gas_absent}, exists-empty={gas_exists})" + ); + } + + /// 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])) + ); + } + + /// One transaction at mainnet's `block` to a contract that reaches the + /// 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 a_precompile_called_from_a_contract( + block_store: Arc, + header: &Header, + precompile: Address, + 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(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]); + 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 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(header, &[(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'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 + /// beneficiary top-up on `had_value && !target_exists` under Spurious Dragon + /// (always active for RSK eras), so a ZERO-balance contract self-destructing + /// to an absent beneficiary would skip the charge. This test pins the rskj + /// behavior: the absent-beneficiary case costs 25,000 more than the + /// existing-beneficiary case even with a zero contract balance. + #[test] + fn test_new_acct_suicide_charged_on_existence_regardless_of_value() { + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let sender = Address::repeat_byte(0xAA); + let contract = Address::repeat_byte(0xCC); + let bene = Address::repeat_byte(0xDD); // not a precompile + + // Contract runtime: PUSH20 bene ; SELFDESTRUCT. Contract balance is 0. + let mut code = vec![0x73]; // PUSH20 + code.extend_from_slice(bene.as_slice()); + code.push(0xFF); // SELFDESTRUCT + + // Pad the tx with non-zero calldata so the gross gas is large enough + // that the selfdestruct refund is NOT capped (refund = min(refund, + // gross/2)) in either case. Otherwise the cheaper existing-beneficiary + // case hits the refund cap and the net-gas delta no longer isolates the + // 25,000 top-up. The contract ignores the calldata. + 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::from(vec![1u8; 500]), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + + let run = |bene_exists: bool| { + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + 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 = if bene_exists { + put_account(&root, store.as_ref(), &bene, 0, U256::ZERO) + } else { + root + }; + let code_key_bytes = rustock_trie::code_key(&contract); + let root = root.put( + &TrieKeySlice::from_key(&code_key_bytes), + &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 header = dummy_header(466_503); + let r = executor + .execute_block(&header, &[(tx.clone(), sender)], &root, store.clone()) + .expect("block"); + assert!(r.tx_results[0].success); + r.tx_results[0].gas_used + }; + + let gas_absent = run(false); + let gas_exists = run(true); + assert_eq!( + gas_absent - gas_exists, + 25_000, + "NEW_ACCT_SUICIDE charged on absent beneficiary regardless of value \ + (absent={gas_absent}, exists={gas_exists})" + ); + } + + /// Multi-tx version of the #382,134 regression: a later transaction in + /// the SAME block re-reads (and possibly same-value re-writes) the bridge + /// slots written by an earlier one — the slots must still reach the trie. + /// Mirrors #378,129: registerBtcTransaction writes the rejection release, + /// the block's own updateCollections reads it, REMASC closes the block. + #[test] + fn test_paid_bridge_storage_survives_same_block_reread() { + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + + let sender = Address::repeat_byte(0xAA); + let caller2 = Address::repeat_byte(0xCD); + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let root = put_account(&root, store.as_ref(), &sender, 0, one_rbtc * U256::from(2)); + let root = put_account(&root, store.as_ref(), &caller2, 0, one_rbtc); + + let block_store = + Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let header = dummy_header(8_000_000); + + // tx1: releaseBtc (writes the release request queue blob) + let tx1 = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(0), + gas_limit: U256::from(100_000), + to: Bytes::copy_from_slice(crate::precompiles::BRIDGE_ADDR.as_slice()), + value: one_rbtc, + input: Bytes::new(), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + // tx2: updateCollections (reads the queue; building fails without + // UTXOs so the queue is re-stored byte-identical) + let tx2 = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(0), + gas_limit: U256::from(100_000), + to: Bytes::copy_from_slice(crate::precompiles::BRIDGE_ADDR.as_slice()), + value: U256::ZERO, + input: Bytes::from_static(&[0x0c, 0x5a, 0x99, 0x90]), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + let result = executor + .execute_block( + &header, + &[(tx1, sender), (tx2, caller2)], + &root, + store.clone(), + ) + .expect("block executes"); + assert!(result.tx_results[0].success, "releaseBtc succeeds"); + assert!(result.tx_results[1].success, "updateCollections succeeds"); + + // Apply to the trie and read the queue back through a fresh root, + // exactly like the next block would. + let new_root = + crate::state::apply_state_changes(&root, store.as_ref(), &result.state_changes, &result.markers); + let queue_key = { + use sha3::{Digest, Keccak256}; + let mut padded = [0u8; 32]; + let name = b"releaseRequestQueueWithTxHash"; + padded[32 - name.len()..].copy_from_slice(name); + let _ = Keccak256::digest(name); // (key is ascii-padded, not hashed) + padded + }; + let slot_b256 = B256::from(queue_key); + let key_bytes = rustock_trie::storage_key(&crate::precompiles::BRIDGE_ADDR, &slot_b256); + let key = rustock_trie::TrieKeySlice::from_key(&key_bytes); + let stored = new_root.get(&key, store.as_ref()); + assert!( + stored.is_some(), + "queue length cell must survive into the trie after a same-block re-read" + ); + } + + /// Regression for mainnet #382,134: bridge storage written by a PAID + /// bridge transaction (post-RSKIP88, revm path) must survive into the + /// block's state changes. A paid releaseBtc enqueues a release request + /// (a multi-slot blob under the Bridge account). + #[test] + fn test_paid_bridge_tx_persists_bridge_storage() { + 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 * U256::from(2)); + + let block_store = + Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let header = dummy_header(8_000_000); // paid bridge era + + 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::BRIDGE_ADDR.as_slice()), + value: one_rbtc, // 1 RBTC -> well above the pegout minimum + input: Bytes::new(), // empty input = releaseBtc + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + let result = executor + .execute_block(&header, &[(tx, sender)], &root, store) + .expect("block executes"); + assert!(result.tx_results[0].success, "releaseBtc succeeds"); + + let written = result + .markers + .raw_storage + .iter() + .filter(|(addr, _, v)| *addr == crate::precompiles::BRIDGE_ADDR && v.is_some()) + .count(); + assert!( + written >= 1, + "releaseBtc must persist the release request queue blob, got {written} raw writes" + ); + } + + #[test] + fn test_simple_value_transfer() { + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + + let sender = Address::repeat_byte(0xAA); + let recipient = Address::repeat_byte(0xBB); + 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 header = dummy_header(8_000_000); + + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(0), + gas_limit: U256::from(21_000), + to: Bytes::copy_from_slice(recipient.as_slice()), + value: U256::from(1_000), + input: Bytes::new(), + 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); + assert_eq!(result.gas_used, 21_000); + } + + #[test] + fn test_insufficient_balance_fails() { + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + + let sender = Address::repeat_byte(0xAA); + let recipient = Address::repeat_byte(0xBB); + + let root = put_account(&root, store.as_ref(), &sender, 0, U256::from(100)); + + let block_store = Arc::new( + BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap(), + ); + + let header = dummy_header(8_000_000); + + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(1), + gas_limit: U256::from(21_000), + to: Bytes::copy_from_slice(recipient.as_slice()), + value: U256::from(1_000_000), + input: Bytes::new(), + 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); + assert!(result.is_err() || !result.unwrap().success); + } + + #[test] + fn test_contract_creation_simple() { + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + + let sender = Address::repeat_byte(0xDD); + 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 header = dummy_header(8_000_000); + + // PUSH1 0x00 PUSH1 0x00 RETURN (returns empty code) + let initcode = vec![0x60, 0x00, 0x60, 0x00, 0xF3]; + + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(0), + gas_limit: U256::from(100_000), + to: Bytes::new(), + value: U256::ZERO, + input: Bytes::from(initcode), + 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); + assert!(result.gas_used > 21_000); + } + + #[test] + fn test_block_execution_multiple_txs() { + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + + let sender = Address::repeat_byte(0xEE); + let recipient1 = Address::repeat_byte(0x11); + let recipient2 = Address::repeat_byte(0x22); + + 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 header = dummy_header(8_000_000); + + let tx1 = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(0), + gas_limit: U256::from(21_000), + to: Bytes::copy_from_slice(recipient1.as_slice()), + value: U256::from(1_000), + input: Bytes::new(), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + + let tx2 = rustock_core::Transaction { + nonce: 1, + gas_price: U256::from(0), + gas_limit: U256::from(21_000), + to: Bytes::copy_from_slice(recipient2.as_slice()), + value: U256::from(2_000), + input: Bytes::new(), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + + let transactions = vec![(tx1, sender), (tx2, sender)]; + + let executor = RskExecutor::new( + RskHardforkConfig::all_active(33), + block_store, + ); + + let result = executor + .execute_block(&header, &transactions, &root, store) + .unwrap(); + + assert_eq!(result.tx_results.len(), 2); + assert!(result.tx_results[0].success); + assert!(result.tx_results[1].success); + assert_eq!(result.gas_used, 42_000); + } + + #[test] + fn test_hardfork_spec_selection() { + let cfg = RskHardforkConfig::mainnet(); + + assert_eq!(cfg.spec_id(0), SpecId::BYZANTIUM); + assert_eq!(cfg.spec_id(2_500_000), SpecId::PETERSBURG); + assert_eq!(cfg.spec_id(6_300_000), SpecId::ISTANBUL); + assert_eq!(cfg.spec_id(7_500_000), SpecId::SHANGHAI); + } + + // ----------------------------------------------------------------------- + // Tests ported from rskj's BlockExecutorTest / TransactionExecutorTest + // ----------------------------------------------------------------------- + + /// Ported from rskj's BlockExecutorTest.executeBlockWithOneTransaction. + /// Validates exact balance deduction: gas_used * gas_price + value. + #[test] + fn test_rskj_execute_block_with_one_transaction_balance_check() { + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + + let sender = Address::repeat_byte(0xA1); + let recipient = Address::repeat_byte(0xB2); + + // rskj test: sender starts with 30_000, sends 10 wei, gas_price=1, gas_limit=21_000 + let root = put_account(&root, store.as_ref(), &sender, 0, U256::from(30_000)); + let root = put_account(&root, store.as_ref(), &recipient, 0, U256::from(10)); + + let block_store = Arc::new( + BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap(), + ); + + let header = dummy_header(8_000_000); + + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(1), + gas_limit: U256::from(21_000), + to: Bytes::copy_from_slice(recipient.as_slice()), + value: U256::from(10), + input: Bytes::new(), + 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_block(&header, &[(tx, sender)], &root, store) + .unwrap(); + + assert_eq!(result.tx_results.len(), 1); + assert!(result.tx_results[0].success); + assert_eq!(result.tx_results[0].gas_used, 21_000); + assert_eq!(result.gas_used, 21_000); + + // Verify state changes match rskj: + // sender final = 30_000 - 21_000 (gas*price) - 10 (value) = 8_990 + let sender_state = result.state_changes.get(&sender) + .expect("sender should have state changes"); + assert_eq!( + sender_state.info.balance, + U256::from(8_990), + "sender balance should be 30000 - 21000*1 - 10 = 8990" + ); + assert_eq!(sender_state.info.nonce, 1, "sender nonce should increment to 1"); + + // recipient final = 10 (initial) + 10 (received) = 20 + let recipient_state = result.state_changes.get(&recipient) + .expect("recipient should have state changes"); + assert_eq!( + recipient_state.info.balance, + U256::from(20), + "recipient balance should be 10 + 10 = 20" + ); + } + + /// Ported from rskj TransactionExecutorTest.testInitHandlesFreeTransactionsOK. + /// Sender with zero balance but non-zero value transfer should fail. + #[test] + fn test_rskj_zero_balance_sender_with_value_fails() { + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + + let sender = Address::repeat_byte(0x01); + let recipient = Address::repeat_byte(0x02); + + // Sender has zero balance + let root = put_account(&root, store.as_ref(), &sender, 0, U256::ZERO); + + let block_store = Arc::new( + BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap(), + ); + + let header = dummy_header(8_000_000); + + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(1), + gas_limit: U256::from(21_000), + to: Bytes::copy_from_slice(recipient.as_slice()), + value: U256::from(68_000), + input: Bytes::new(), + 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); + // Should fail: sender can't cover gas_limit * gas_price + value + assert!(result.is_err(), "zero-balance sender with value transfer should fail"); + } + + /// Ported from rskj's TransactionTest.testTransactionCost patterns. + /// Verifies that non-zero calldata bytes cost 16 gas each (post-RSKIP400/Istanbul). + #[test] + fn test_rskj_calldata_cost_post_istanbul() { + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + + let sender = Address::repeat_byte(0xCC); + let recipient = Address::repeat_byte(0xDD); + 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(), + ); + + // Post-Arrowhead (ISTANBUL) → non-zero byte cost = 16 + let header = dummy_header(8_000_000); + + // 1 non-zero byte of calldata + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(0), + gas_limit: U256::from(30_000), + to: Bytes::copy_from_slice(recipient.as_slice()), + value: U256::ZERO, + input: Bytes::from(vec![0xFF]), // 1 non-zero byte + 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); + // Intrinsic gas = 21000 (base) + 16 (1 non-zero byte at Istanbul rate) + assert_eq!(result.gas_used, 21_016, "intrinsic gas = 21000 + 16*1"); + } + + /// Complementary test: pre-Istanbul calldata cost is 68 per non-zero byte. + #[test] + fn test_rskj_calldata_cost_pre_istanbul() { + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + + let sender = Address::repeat_byte(0xCC); + let recipient = Address::repeat_byte(0xDD); + 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(), + ); + + // Pre-Orchid (BYZANTIUM) → non-zero byte cost = 68 + let mut header = dummy_header(100); + header.gas_limit = U256::from(6_800_000); + + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(0), + gas_limit: U256::from(30_000), + to: Bytes::copy_from_slice(recipient.as_slice()), + value: U256::ZERO, + input: Bytes::from(vec![0xFF]), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + + let executor = RskExecutor::new( + RskHardforkConfig::mainnet(), + block_store, + ); + + let result = executor + .execute_tx(&header, &tx, sender, &root, store) + .unwrap(); + + assert!(result.success); + // Pre-Istanbul: intrinsic gas = 21000 + 68*1 = 21068 + assert_eq!(result.gas_used, 21_068, "intrinsic gas = 21000 + 68*1"); + } + + /// Regression for mainnet #6,223,700 (the arrowhead600 activation block; + /// gas mismatch header=236343 vs computed=287919). RSKIP400 (Arrowhead600) + /// reduces the non-zero calldata byte cost from 68 to 16 (EIP-2028). revm's + /// intrinsic gas keys off the SpecId, so arrowhead600 maps to ISTANBUL to + /// pick up the calldata reduction; the other Istanbul gas repricings RSK + /// never adopted (EIP-1884 SLOAD/BALANCE/EXTCODEHASH) are pinned back to + /// rskj values in `rsk_instructions::install`. This test pins the FIRST + /// arrowhead600 block exactly: at #6,223,700 calldata is 16/byte; one block + /// earlier 68. + #[test] + fn test_rskip400_calldata_reduction_at_arrowhead_activation() { + let mainnet = RskHardforkConfig::mainnet(); + assert_eq!(mainnet.spec_id(6_223_699), SpecId::PETERSBURG); + assert_eq!(mainnet.spec_id(6_223_700), SpecId::ISTANBUL); + + let run = |number: u64| -> u64 { + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + let sender = Address::repeat_byte(0xCC); + let recipient = Address::repeat_byte(0xDD); + 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 header = dummy_header(number); + header.gas_limit = U256::from(6_800_000); + // 3 non-zero calldata bytes (no zeros): 68*3=204 pre, 16*3=48 post. + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(0), + gas_limit: U256::from(30_000), + to: Bytes::copy_from_slice(recipient.as_slice()), + value: U256::ZERO, + input: Bytes::from(vec![0x11, 0x22, 0x33]), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + executor.execute_tx(&header, &tx, sender, &root, store).unwrap().gas_used + }; + + // One block before activation: 21000 + 68*3 = 21204. + assert_eq!(run(6_223_699), 21_204, "pre-RSKIP400: 68 gas/non-zero byte"); + // Activation block: 21000 + 16*3 = 21048. + assert_eq!(run(6_223_700), 21_048, "RSKIP400 active: 16 gas/non-zero byte"); + } + + /// RSKIP398 (arrowhead600): the PUSH0 opcode (0x5f) becomes valid at + /// arrowhead600, NOT at the Shanghai-equivalent lovell700. rustock maps + /// arrowhead600 to revm's ISTANBUL (which lacks PUSH0), so without an explicit + /// install a PUSH0 would halt NotActivated and consume the frame's whole gas + /// — a consensus fork vs rskj, which executes it (push 0, 2 gas). Deploy a + /// contract whose runtime is `PUSH0 PUSH1 0 SSTORE STOP` and call it: at + /// arrowhead600 it succeeds, one block earlier (fingerroot500/PETERSBURG) the + /// PUSH0 is an invalid opcode that fails the call. + #[test] + fn test_push0_activates_at_arrowhead600() { + // Runtime: PUSH0; PUSH1 0; SSTORE; STOP (store 0 at slot 0) + let runtime: Vec = vec![0x5f, 0x60, 0x00, 0x55, 0x00]; + // Init: CODECOPY the 5-byte runtime and RETURN it. + // PUSH1 5; PUSH1 0x0c; PUSH1 0; CODECOPY; PUSH1 5; PUSH1 0; RETURN + let mut init = vec![ + 0x60, 0x05, 0x60, 0x0c, 0x60, 0x00, 0x39, 0x60, 0x05, 0x60, 0x00, 0xf3, + ]; + init.extend_from_slice(&runtime); + + let run = |number: u64| -> bool { + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + let sender = Address::repeat_byte(0x5F); + 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 executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + let deploy = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(0), + gas_limit: U256::from(1_000_000), + to: Bytes::new(), + value: U256::ZERO, + input: Bytes::from(init.clone()), + v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, + }; + let r1 = executor + .execute_block(&dummy_header(number), &[(deploy, sender)], &root, store.clone()) + .expect("deploy block"); + assert!(r1.tx_results[0].success, "deploy (no PUSH0 in init) always succeeds"); + let deployed = sender.create(0); + let root2 = crate::state::apply_state_changes( + &root, store.as_ref(), &r1.state_changes, &r1.markers, + ); + let call = rustock_core::Transaction { + nonce: 1, + gas_price: U256::from(0), + gas_limit: U256::from(1_000_000), + 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, + }; + executor + .execute_block(&dummy_header(number), &[(call, sender)], &root2, store) + .expect("call block") + .tx_results[0] + .success + }; + + assert!(!run(6_223_699), "PUSH0 invalid pre-arrowhead600 (call fails)"); + assert!(run(6_223_700), "PUSH0 valid at arrowhead600 (call succeeds)"); + } + + /// Audit finding FRCR-679 / FRCR-879. + /// + /// RSKIP412 (arrowhead600) makes BASEFEE (0x48) valid: rskj's + /// `VM.java` OP_BASEFEE throws `invalidOpCode` only while RSKIP412 is + /// inactive (`reference.conf`: `rskip412 = arrowhead600`), and + /// `VM.doBASEFEE` pushes `program.getMinimumGasPrice()` for 2 gas. + /// + /// revm bundles BASEFEE into LONDON. rustock maps arrowhead600 to ISTANBUL + /// (for the RSKIP400 calldata pricing) and only reaches SHANGHAI at + /// lovell700 — so across the whole arrowhead600..lovell700 window, about + /// 1.1M mainnet blocks, revm's stock BASEFEE halted `NotActivated` and + /// consumed the frame's entire gas where rskj pushed a value and continued. + /// + /// Same shape as `test_push0_activates_at_arrowhead600`; this one also + /// checks the value pushed, because installing the opcode is only half of + /// matching rskj — it has to push the block's minimum gas price. + #[test] + fn test_basefee_activates_at_arrowhead600() { + // Runtime: BASEFEE; PUSH1 0; MSTORE; PUSH1 32; PUSH1 0; RETURN + let runtime: Vec = vec![0x48, 0x60, 0x00, 0x52, 0x60, 0x20, 0x60, 0x00, 0xf3]; + // Init: CODECOPY the 9-byte runtime and RETURN it. + let mut init = vec![ + 0x60, 0x09, 0x60, 0x0c, 0x60, 0x00, 0x39, 0x60, 0x09, 0x60, 0x00, 0xf3, + ]; + init.extend_from_slice(&runtime); + + const MIN_GAS_PRICE: u64 = 59_240_000; + + // `None` = the call failed (invalid opcode); `Some(v)` = BASEFEE pushed v. + let run = |number: u64| -> Option { + let header = Header { + minimum_gas_price: U256::from(MIN_GAS_PRICE), + ..dummy_header(number) + }; + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + let sender = Address::repeat_byte(0x48); + 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 executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + let deploy = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(MIN_GAS_PRICE), + gas_limit: U256::from(1_000_000), + to: Bytes::new(), + value: U256::ZERO, + input: Bytes::from(init.clone()), + v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, + }; + let r1 = executor + .execute_block(&header, &[(deploy, sender)], &root, store.clone()) + .expect("deploy block"); + assert!(r1.tx_results[0].success, "deploy (no BASEFEE in init) always succeeds"); + let deployed = sender.create(0); + let root2 = crate::state::apply_state_changes( + &root, store.as_ref(), &r1.state_changes, &r1.markers, + ); + let call = rustock_core::Transaction { + nonce: 1, + gas_price: U256::from(MIN_GAS_PRICE), + gas_limit: U256::from(1_000_000), + 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, + }; + let r2 = executor + .execute_block(&header, &[(call, sender)], &root2, store) + .expect("call block"); + let res = &r2.tx_results[0]; + if !res.success { + return None; + } + assert_eq!(res.output.len(), 32, "BASEFEE returns one word"); + Some(U256::from_be_slice(&res.output)) + }; + + // fingerroot500 (PETERSBURG): rskj throws invalidOpCode, so must fail. + assert_eq!(run(6_223_699), None, "BASEFEE invalid pre-arrowhead600"); + // arrowhead600 (ISTANBUL in rustock): the window the audit found. + assert_eq!( + run(6_223_700), + Some(U256::from(MIN_GAS_PRICE)), + "RSKIP412 active at arrowhead600: pushes minimumGasPrice" + ); + // arrowhead631, still ISTANBUL. + assert_eq!(run(6_549_300), Some(U256::from(MIN_GAS_PRICE))); + // lovell700 (SHANGHAI >= LONDON): revm's own BASEFEE already worked + // here, so this pins that the explicit install did not change it. + assert_eq!(run(7_338_024), Some(U256::from(MIN_GAS_PRICE))); + } + + /// Audit finding FRCR-476. + /// + /// rskj's `Bridge.receiveHeader` (Bridge.java:568-586) checks the argument + /// size *before* anything else and, on a mismatch, **returns** + /// `RECEIVE_HEADER_ERROR_SIZE_MISTMATCH = -20` (Bridge.java:243). The call + /// succeeds; the caller reads -20 off the stack. The size rule is + /// `BtcTransactionFormatUtils.isBlockHeaderSize`: exactly 80 bytes once + /// RSKIP124 is active. + /// + /// rustock had neither half. Under 80 bytes it raised a precompile error + /// (post-RSKIP197 a revert pushing 0, pre-197 a failed call consuming all + /// gas); over 80 bytes it silently truncated to the first 80 and processed + /// the header as if it were valid. Both fork from rskj on inputs any + /// relayer can submit. + #[test] + fn test_receive_header_size_mismatch_returns_minus_20() { + // ABI-encode `receiveHeader(bytes)` with a payload of `len` bytes. + let call_input = |len: usize| -> Vec { + let mut input = crate::bridge::compute_selector("receiveHeader(bytes)").to_vec(); + let mut off = [0u8; 32]; + off[31] = 0x20; + input.extend_from_slice(&off); // offset to the bytes argument + let mut l = [0u8; 32]; + l[24..].copy_from_slice(&(len as u64).to_be_bytes()); + input.extend_from_slice(&l); // length + let mut payload = vec![0xABu8; len]; + payload.resize(len.div_ceil(32) * 32, 0); // right-pad to a word + input.extend_from_slice(&payload); + input + }; + + // Returns the int256 the Bridge produced, or None if the call failed. + let run = |len: usize| -> Option { + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + let sender = Address::repeat_byte(0x7E); + 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 executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + let tx = rustock_core::Transaction { + nonce: 0, + 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(call_input(len)), + v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, + }; + let r = executor + .execute_block(&dummy_header(7_000_000), &[(tx, sender)], &root, store) + .expect("block"); + let res = &r.tx_results[0]; + if !res.success || res.output.len() != 32 { + return None; + } + Some(alloy_primitives::I256::from_be_bytes::<32>(res.output[..32].try_into().unwrap())) + }; + + let minus_20 = alloy_primitives::I256::try_from(-20i64).unwrap(); + // Too short: rskj returns -20, it does not throw. + assert_eq!(run(79), Some(minus_20), "79-byte header -> -20"); + assert_eq!(run(0), Some(minus_20), "empty header -> -20"); + // Too long: rskj returns -20 rather than truncating to 80. + assert_eq!(run(81), Some(minus_20), "81-byte header -> -20"); + assert_eq!(run(85), Some(minus_20), "85-byte header -> -20 once RSKIP124 is on"); + + // Exactly 80 is accepted for processing and must NOT report -20. The + // bytes are garbage, so the Bridge reports an ordinary BridgeSupport + // code (-3, previous block not found) — the point is that it got that + // far. + let at_80 = run(80).expect("80-byte header is processed"); + assert_ne!(at_80, minus_20, "80 bytes must reach BridgeSupport"); + assert_eq!( + at_80, + alloy_primitives::I256::try_from(-3i64).unwrap(), + "unknown parent -> RECEIVE_HEADER_CANT_FOUND_PREVIOUS_BLOCK" + ); + } + + /// Audit findings FRCR-75 and FRCR-76. + /// + /// rskj `BridgeSupport.getBtcTransactionConfirmations` (BridgeSupport.java + /// :2194-2235) runs four checks in a fixed order, each with its own code + /// (BridgeSupport.java:90-94): + /// + /// ```text + /// block == null -> -1 INEXISTENT_BLOCK_HASH + /// blockDepth > 4320 -> -4 BLOCK_TOO_OLD + /// not the main-chain block at height -> -2 BLOCK_NOT_IN_BEST_CHAIN + /// (BlockStoreException) -> -3 INCONSISTENT_BLOCK + /// merkle branch does not reduce -> -5 INVALID_MERKLE_BRANCH + /// otherwise bestChainHeight - block.getHeight() + 1 + /// ``` + /// + /// rustock had -1, then jumped straight to the merkle check and reported + /// **-2** for a bad branch. It had no depth check and, more seriously, no + /// best-chain check at all: a block on an orphaned BTC fork is still in + /// Bridge storage, so rustock would confirm a transaction that exists only + /// on that fork — reporting a positive confirmation count where rskj + /// returns -2. Everything downstream of this method trusts that number. + /// + /// Driven end to end on regtest: build a 3-block chain plus a sibling that + /// forks at height 1, then query each case through the Bridge precompile. + #[test] + fn test_btc_transaction_confirmations_codes_and_best_chain_check() { + use bitcoin::block::Version; + use bitcoin::consensus::serialize as btc_serialize; + use bitcoin::hashes::Hash as _; + use bitcoin::{BlockHash, CompactTarget, TxMerkleNode}; + use crate::bridge::btc_chain::{b256_to_bitcoin_hash, bitcoin_hash_to_b256}; + + const BLOCK: u64 = 7_000_000; // every relevant RSKIP active + // Regtest difficulty: any nonce is a valid proof of work. + let bits = CompactTarget::from_consensus(0x207f_ffff); + let hdr = |prev: BlockHash, merkle_seed: u8, nonce: u32| bitcoin::block::Header { + version: Version::from_consensus(1), + prev_blockhash: prev, + merkle_root: TxMerkleNode::from_byte_array([merkle_seed; 32]), + time: 1_700_000_000, + bits, + nonce, + }; + + // Regtest's target is ~2^255, so roughly every other nonce works; + // search for one rather than assuming. + let mine = |prev: BlockHash, merkle_seed: u8| -> bitcoin::block::Header { + (0u32..10_000) + .map(|nonce| hdr(prev, merkle_seed, nonce)) + .find(|header| { + crate::bridge::btc_rules::verify_header( + header, + crate::bridge::constants::BtcNetwork::Regtest, + ) + }) + .expect("regtest PoW is trivial") + }; + + let genesis = bitcoin::constants::genesis_block(bitcoin::Network::Regtest) + .header + .block_hash(); + let h1 = mine(genesis, 0x11); + let h2 = mine(h1.block_hash(), 0x22); + let h3 = mine(h2.block_hash(), 0x33); + // Forks at height 1 with the same work as h1, so it never becomes the + // head — but it IS stored, which is the whole point. + let fork1 = mine(genesis, 0xF1); + assert_ne!(fork1.block_hash(), h1.block_hash()); + + let receive_header_tx = |nonce: u64, header: &bitcoin::block::Header| { + let raw = btc_serialize(header); + assert_eq!(raw.len(), 80); + let mut input = crate::bridge::compute_selector("receiveHeader(bytes)").to_vec(); + let mut off = [0u8; 32]; + off[31] = 0x20; + input.extend_from_slice(&off); + let mut l = [0u8; 32]; + l[31] = 80; + input.extend_from_slice(&l); + let mut payload = raw.clone(); + payload.resize(96, 0); + input.extend_from_slice(&payload); + rustock_core::Transaction { + nonce, + gas_price: U256::from(0), + gas_limit: U256::from(1_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 root = TrieNode::empty(); + let sender = Address::repeat_byte(0xC0); + 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 executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store) + .with_bridge_constants(crate::bridge::constants::BridgeConstants::regtest()); + + let txs: Vec<_> = [&h1, &h2, &h3, &fork1] + .iter() + .enumerate() + .map(|(i, h)| (receive_header_tx(i as u64, h), sender)) + .collect(); + let built = executor + .execute_block(&dummy_header(BLOCK), &txs, &root, store.clone()) + .expect("header block"); + for (i, r) in built.tx_results.iter().enumerate() { + assert!(r.success, "receiveHeader #{i} call succeeded"); + assert_eq!( + alloy_primitives::I256::from_be_bytes::<32>(r.output[..32].try_into().unwrap()), + alloy_primitives::I256::ZERO, + "receiveHeader #{i} returned SUCCESS(0)" + ); + } + let root = crate::state::apply_state_changes( + &root, store.as_ref(), &built.state_changes, &built.markers, + ); + + // getBtcTransactionConfirmations(bytes32,bytes32,uint256,bytes32[]) + // with an EMPTY merkle branch: the branch reduces to btcTxHash itself, + // so a single-transaction block's merkle root is the tx hash. + let confirmations = |nonce: u64, tx_hash_display: [u8; 32], block: &bitcoin::block::Header| { + let mut input = + crate::bridge::compute_selector("getBtcTransactionConfirmations(bytes32,bytes32,uint256,bytes32[])") + .to_vec(); + input.extend_from_slice(&tx_hash_display); + input.extend_from_slice(bitcoin_hash_to_b256(&block.block_hash()).as_slice()); + input.extend_from_slice(&[0u8; 32]); // merkleBranchPath = 0 + let mut off = [0u8; 32]; + off[31] = 0x80; + input.extend_from_slice(&off); // offset of the hashes array + input.extend_from_slice(&[0u8; 32]); // hashes.length = 0 + let tx = 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 r = executor + .execute_block(&dummy_header(BLOCK), &[(tx, sender)], &root, store.clone()) + .expect("query block"); + let res = &r.tx_results[0]; + assert!(res.success, "getBtcTransactionConfirmations must not throw"); + alloy_primitives::I256::from_be_bytes::<32>(res.output[..32].try_into().unwrap()) + }; + + // The merkle root in DISPLAY order is what an ABI caller passes as + // btcTxHash for a one-transaction block. + let tx_hash_of = |h: &bitcoin::block::Header| -> [u8; 32] { + let mut b = h.merkle_root.to_raw_hash().to_byte_array(); + b.reverse(); + b + }; + let code = |n: i64| alloy_primitives::I256::try_from(n).unwrap(); + + // Happy path: h1 is at height 1, the head is h3 at height 3. + assert_eq!( + confirmations(4, tx_hash_of(&h1), &h1), + code(3), + "bestChainHeight - height + 1" + ); + assert_eq!(confirmations(4, tx_hash_of(&h3), &h3), code(1), "the head itself"); + + // -1: the block hash is not stored at all. + let unknown = hdr(genesis, 0xEE, 999_999); + assert_eq!( + confirmations(4, tx_hash_of(&unknown), &unknown), + code(-1), + "INEXISTENT_BLOCK_HASH" + ); + + // -5: stored, in the best chain, but the branch does not reduce to the + // merkle root. rustock used to report -2 here. + assert_eq!( + confirmations(4, [0xAB; 32], &h1), + code(-5), + "INVALID_MERKLE_BRANCH" + ); + + // -2: stored, but on an orphaned fork. rustock used to report 3 — a + // confirmed transaction that is not on the BTC main chain. + assert_eq!( + confirmations(4, tx_hash_of(&fork1), &fork1), + code(-2), + "BLOCK_NOT_IN_BEST_CHAIN" + ); + + // Ordering: the best-chain check runs BEFORE the merkle check, so a + // fork block with a bad branch still reports -2, not -5. + assert_eq!( + confirmations(4, [0xAB; 32], &fork1), + code(-2), + "best-chain check precedes the merkle check" + ); + + // Sanity: the fork block really is stored, so -2 came from the + // best-chain check and not from the block being missing. + 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(|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; + + // 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 + /// `BridgeMethods` gives it `fixedPermission(false)` — so a contract can + /// CALL it and branch on the answer, which makes the value consensus. + /// rustock returned a hardcoded `0`. + /// + /// rskj `BridgeSupport.getEstimatedFeesForNextPegOutEvent`: + /// + /// ```text + /// shouldReturnZeroEstimatedFees() -> 0 + /// !RSKIP305 -> feePerKB * simulatePegoutTxSize(2 in, queued+2 out) / 1000 + /// otherwise -> inputSum - outputSum of a simulated batched peg-out, + /// falling back to the line above on failure + /// ``` + /// + /// Driven through the Bridge precompile: `releaseBtc` transactions build a + /// real release queue, then the getter is called and its int256 read back. + #[test] + fn test_estimated_fees_for_next_pegout_is_not_hardcoded_zero() { + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + + // Run `pegouts` releaseBtc transactions, then query the estimate. + let query = |number: u64, pegouts: u64| -> U256 { + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + let sender = Address::repeat_byte(0xE5); + let root = put_account(&root, store.as_ref(), &sender, 0, one_rbtc * U256::from(50)); + let block_store = + Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + let header = dummy_header(number); + + let mut root = root; + for nonce in 0..pegouts { + let tx = rustock_core::Transaction { + nonce, + gas_price: U256::from(0), + gas_limit: U256::from(200_000), + to: Bytes::copy_from_slice(crate::precompiles::BRIDGE_ADDR.as_slice()), + value: one_rbtc, // 1 RBTC, well above the peg-out minimum + input: Bytes::new(), // empty input = releaseBtc + v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, + }; + let r = executor + .execute_block(&header, &[(tx, sender)], &root, store.clone()) + .expect("releaseBtc block"); + assert!(r.tx_results[0].success, "releaseBtc must succeed"); + root = crate::state::apply_state_changes( + &root, store.as_ref(), &r.state_changes, &r.markers, + ); + } + + let call = rustock_core::Transaction { + nonce: pegouts, + gas_price: U256::from(0), + gas_limit: U256::from(500_000), + to: Bytes::copy_from_slice(crate::precompiles::BRIDGE_ADDR.as_slice()), + value: U256::ZERO, + input: Bytes::from( + crate::bridge::compute_selector("getEstimatedFeesForNextPegOutEvent()").to_vec(), + ), + v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, + }; + let r = executor + .execute_block(&header, &[(call, sender)], &root, store) + .expect("query block"); + let res = &r.tx_results[0]; + assert!(res.success, "getEstimatedFeesForNextPegOutEvent must not throw"); + assert_eq!(res.output.len(), 32); + U256::from_be_slice(&res.output) + }; + + // hop400 (#4,598,500) .. fingerroot500 (#5,468,000): RSKIP271 active, + // RSKIP385 not — shouldReturnZeroEstimatedFees is true for an EMPTY + // queue only. + assert_eq!(query(5_000_000, 0), U256::ZERO, "empty queue pre-RSKIP385 -> 0"); + let pre385_one = query(5_000_000, 1); + assert!(!pre385_one.is_zero(), "a queued peg-out pre-RSKIP385 must cost something"); + + // fingerroot500 onward RSKIP385 is active, so the zero short-circuit is + // gone: even an empty queue reports the 2-input / 2-output estimate. + let f500_empty = query(6_000_000, 0); + assert!(!f500_empty.is_zero(), "RSKIP385 removes the empty-queue zero"); + + // reed800 (#8,052,200): RSKIP305 switches to the simulation. With no + // federation UTXOs the builder cannot fund the peg-out, which is rskj's + // BridgeIllegalArgumentException path -> fall back to the size estimate. + let reed_empty = query(8_500_000, 0); + assert!(!reed_empty.is_zero(), "reed800 fallback must still produce a fee"); + + // vetiver900 (#8,804,200) activates RSKIP540, which only changes the + // hypothetical amount; with the fallback in play the answer is stable. + let vetiver_empty = query(9_000_000, 0); + assert!(!vetiver_empty.is_zero()); + + // More queued peg-outs mean more outputs, hence a bigger transaction + // and a strictly bigger fee. This is what a hardcoded 0 could never do. + let f500_three = query(6_000_000, 3); + assert!( + f500_three > f500_empty, + "3 queued peg-outs must cost more than 0: {f500_three} vs {f500_empty}" + ); + } + + /// Ported from rskj InitcodeCostCalculatorTest. + /// Validates that contract creation includes initcode word cost at Shanghai. + #[test] + fn test_rskj_initcode_cost_shanghai() { + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + + let sender = Address::repeat_byte(0xEE); + 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(), + ); + + // Post-Lovell = SHANGHAI (has initcode metering) + let header = dummy_header(8_000_000); + + // 4 bytes of initcode: ceil(4/32) = 1 word, INITCODE_WORD_COST = 2 + // PUSH1 0x00 PUSH1 0x00 RETURN + padding + let initcode = vec![0x60, 0x00, 0x60, 0x00]; // 4 bytes + + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(0), + gas_limit: U256::from(200_000), + to: Bytes::new(), // contract creation + value: U256::ZERO, + input: Bytes::from(initcode), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + + let executor = RskExecutor::new( + RskHardforkConfig::all_active(33), + block_store.clone(), + ); + + let result = executor + .execute_tx(&header, &tx, sender, &root, store.clone()) + .unwrap(); + + // Shanghai create intrinsic: 32000 (create base) + 16*4 (non-zero bytes at Istanbul) + 2 (initcode: 1 word * 2) + // = 32000 + 64 + 2 = 32066 + // Note: this tx will revert because the initcode doesn't end with RETURN, + // but we can still check gas_used >= intrinsic cost + assert!(result.gas_used >= 32_066, "should include initcode metering cost"); + } + + /// Runs a contract-creation tx with the given initcode at the given mainnet height. + fn run_create_at(initcode: Vec, block_number: u64, gas_limit: u64) -> TxExecutionResult { + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + let sender = Address::repeat_byte(0xEE); + 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 header = dummy_header(block_number); + + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(0), + gas_limit: U256::from(gas_limit), + to: Bytes::new(), // contract creation + value: U256::ZERO, + input: Bytes::from(initcode), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + executor.execute_tx(&header, &tx, sender, &root, store).unwrap() + } + + /// Ported from rskj ContractCodePrefixDslTest + /// (dsl/contract_code_prefix_rskip544/create_fails_with_ef_byte_activated.txt): + /// after RSKIP544 (vetiver900), creating a contract whose code starts with + /// 0xEF fails and burns all gas. + #[test] + fn test_rskip544_create_ef_code_fails_after_vetiver900() { + // PUSH1 0xEF, PUSH1 0, MSTORE8, PUSH1 1, PUSH1 0, RETURN -> code = [0xEF] + let initcode = hex::decode("60ef60005360016000f3").unwrap(); + let result = run_create_at(initcode, 8_804_200, 300_000); + assert!(!result.success, "0xEF-prefixed code must be rejected post-vetiver900"); + assert_eq!(result.gas_used, 300_000, "all gas is consumed (rskj DSL groundtruth)"); + } + + /// Ported from rskj ContractCodePrefixDslTest + /// (testCreateSucceedsWithEFByteBeforeRSKIP544Activation): before vetiver900, + /// 0xEF-prefixed code is accepted — rskj only enforces EIP-3541 from RSKIP544. + #[test] + fn test_rskip544_create_ef_code_succeeds_before_vetiver900() { + let initcode = hex::decode("60ef60005360016000f3").unwrap(); + let result = run_create_at(initcode, 8_804_199, 300_000); + assert!(result.success, "0xEF-prefixed code must be accepted pre-vetiver900"); + assert!(result.gas_used > 21_000 && result.gas_used < 300_000); + } + + /// Ported from rskj ContractCodePrefixDslTest (create_succeeds_with_fe_byte.txt): + /// 0xFE-prefixed code is fine even after RSKIP544. + #[test] + fn test_rskip544_create_fe_code_succeeds_after_vetiver900() { + let initcode = hex::decode("60fe60005360016000f3").unwrap(); + let result = run_create_at(initcode, 8_804_200, 300_000); + assert!(result.success); + assert!(result.gas_used > 21_000 && result.gas_used < 300_000); + } + + /// Ported from rskj ContractCodePrefixDslTest (create_succeeds_with_empty_code.txt): + /// empty contract code is allowed after RSKIP544. + #[test] + fn test_rskip544_create_empty_code_succeeds_after_vetiver900() { + let initcode = hex::decode("60006000f3").unwrap(); + let result = run_create_at(initcode, 8_804_200, 300_000); + assert!(result.success); + assert!(result.gas_used > 21_000 && result.gas_used < 300_000); + } + + /// Ported from rskj Create2Test patterns. + /// Verifies that CREATE2 produces the correct contract address. + #[test] + fn test_rskj_create2_address_computation() { + // CREATE2 address = keccak256(0xff ++ sender ++ salt ++ keccak256(init_code))[12..] + use sha3::{Digest, Keccak256}; + + let sender_bytes = hex_to_bytes("0f6510583d425cfcf94b99f8b073b44f60d1912b"); + let salt = [0u8; 32]; + let init_code = vec![0x60, 0x0b]; // 2 bytes of init code (from rskj test) + + let init_code_hash = Keccak256::digest(&init_code); + + let mut input = Vec::new(); + input.push(0xFF); + input.extend_from_slice(&sender_bytes); + input.extend_from_slice(&salt); + input.extend_from_slice(&init_code_hash); + + let result = Keccak256::digest(&input); + let address = &result[12..]; + + let expected = hex_to_bytes("3bc3efa1c487a1ebfc911b47b548e2c82436a212"); + assert_eq!( + address, expected.as_slice(), + "CREATE2 address should match rskj test vector" + ); + } + + /// CREATE2 with different salt → different address (rskj Create2Test.testCREATE2_SaltNumber). #[test] - fn test_rskip400_calldata_reduction_at_arrowhead_activation() { - let mainnet = RskHardforkConfig::mainnet(); - assert_eq!(mainnet.spec_id(6_223_699), SpecId::PETERSBURG); - assert_eq!(mainnet.spec_id(6_223_700), SpecId::ISTANBUL); + fn test_rskj_create2_salt_changes_address() { + use sha3::{Digest, Keccak256}; - let run = |number: u64| -> u64 { - let store = Arc::new(MemoryTrieStore::new()); - let root = TrieNode::empty(); - let sender = Address::repeat_byte(0xCC); - let recipient = Address::repeat_byte(0xDD); - 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 header = dummy_header(number); - header.gas_limit = U256::from(6_800_000); - // 3 non-zero calldata bytes (no zeros): 68*3=204 pre, 16*3=48 post. - let tx = rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(0), - gas_limit: U256::from(30_000), - to: Bytes::copy_from_slice(recipient.as_slice()), - value: U256::ZERO, - input: Bytes::from(vec![0x11, 0x22, 0x33]), - v: 0, - r: U256::ZERO, - s: U256::ZERO, - cached_rlp: None, - }; - let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); - executor.execute_tx(&header, &tx, sender, &root, store).unwrap().gas_used - }; + let sender_bytes = hex_to_bytes("0f6510583d425cfcf94b99f8b073b44f60d1912b"); + let mut salt = [0u8; 32]; + salt[28..].copy_from_slice(&0xCAFEBABEu32.to_be_bytes()); + let init_code = vec![0x60, 0x1b]; - // One block before activation: 21000 + 68*3 = 21204. - assert_eq!(run(6_223_699), 21_204, "pre-RSKIP400: 68 gas/non-zero byte"); - // Activation block: 21000 + 16*3 = 21048. - assert_eq!(run(6_223_700), 21_048, "RSKIP400 active: 16 gas/non-zero byte"); + let init_code_hash = Keccak256::digest(&init_code); + + let mut input = Vec::new(); + input.push(0xFF); + input.extend_from_slice(&sender_bytes); + input.extend_from_slice(&salt); + input.extend_from_slice(&init_code_hash); + + let result = Keccak256::digest(&input); + let address = &result[12..]; + + let expected = hex_to_bytes("19542b03f2d5d4e1910dbe096faf0842d928883d"); + assert_eq!( + address, expected.as_slice(), + "CREATE2 with salt 0xcafebabe should match rskj" + ); } - /// RSKIP398 (arrowhead600): the PUSH0 opcode (0x5f) becomes valid at - /// arrowhead600, NOT at the Shanghai-equivalent lovell700. rustock maps - /// arrowhead600 to revm's ISTANBUL (which lacks PUSH0), so without an explicit - /// install a PUSH0 would halt NotActivated and consume the frame's whole gas - /// — a consensus fork vs rskj, which executes it (push 0, 2 gas). Deploy a - /// contract whose runtime is `PUSH0 PUSH1 0 SSTORE STOP` and call it: at - /// arrowhead600 it succeeds, one block earlier (fingerroot500/PETERSBURG) the - /// PUSH0 is an invalid opcode that fails the call. + /// CREATE2 with different sender → different address (rskj Create2Test.testCREATE2_Address). #[test] - fn test_push0_activates_at_arrowhead600() { - // Runtime: PUSH0; PUSH1 0; SSTORE; STOP (store 0 at slot 0) - let runtime: Vec = vec![0x5f, 0x60, 0x00, 0x55, 0x00]; - // Init: CODECOPY the 5-byte runtime and RETURN it. - // PUSH1 5; PUSH1 0x0c; PUSH1 0; CODECOPY; PUSH1 5; PUSH1 0; RETURN - let mut init = vec![ - 0x60, 0x05, 0x60, 0x0c, 0x60, 0x00, 0x39, 0x60, 0x05, 0x60, 0x00, 0xf3, - ]; - init.extend_from_slice(&runtime); + fn test_rskj_create2_sender_changes_address() { + use sha3::{Digest, Keccak256}; - let run = |number: u64| -> bool { - let store = Arc::new(MemoryTrieStore::new()); - let root = TrieNode::empty(); - let sender = Address::repeat_byte(0x5F); - 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 executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); - let deploy = rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(0), - gas_limit: U256::from(1_000_000), - to: Bytes::new(), - value: U256::ZERO, - input: Bytes::from(init.clone()), - v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, - }; - let r1 = executor - .execute_block(&dummy_header(number), &[(deploy, sender)], &root, store.clone()) - .expect("deploy block"); - assert!(r1.tx_results[0].success, "deploy (no PUSH0 in init) always succeeds"); - let deployed = sender.create(0); - let root2 = crate::state::apply_state_changes( - &root, store.as_ref(), &r1.state_changes, &r1.markers, - ); - let call = rustock_core::Transaction { - nonce: 1, - gas_price: U256::from(0), - gas_limit: U256::from(1_000_000), - 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, - }; - executor - .execute_block(&dummy_header(number), &[(call, sender)], &root2, store) - .expect("call block") - .tx_results[0] - .success - }; + let sender_bytes = hex_to_bytes("deadbeef00000000000000000000000000000000"); + let mut salt = [0u8; 32]; + salt[28..].copy_from_slice(&0xCAFEBABEu32.to_be_bytes()); + let init_code = vec![0x60, 0x1b]; - assert!(!run(6_223_699), "PUSH0 invalid pre-arrowhead600 (call fails)"); - assert!(run(6_223_700), "PUSH0 valid at arrowhead600 (call succeeds)"); + let init_code_hash = Keccak256::digest(&init_code); + + let mut input = Vec::new(); + input.push(0xFF); + input.extend_from_slice(&sender_bytes); + input.extend_from_slice(&salt); + input.extend_from_slice(&init_code_hash); + + let result = Keccak256::digest(&input); + let address = &result[12..]; + + let expected = hex_to_bytes("3ba1dc70cc17e740f4bd85052af074b2b2a49e06"); + assert_eq!( + address, expected.as_slice(), + "CREATE2 with deadbeef sender should match rskj" + ); } - /// Audit finding FRCR-679 / FRCR-879. - /// - /// RSKIP412 (arrowhead600) makes BASEFEE (0x48) valid: rskj's - /// `VM.java` OP_BASEFEE throws `invalidOpCode` only while RSKIP412 is - /// inactive (`reference.conf`: `rskip412 = arrowhead600`), and - /// `VM.doBASEFEE` pushes `program.getMinimumGasPrice()` for 2 gas. - /// - /// revm bundles BASEFEE into LONDON. rustock maps arrowhead600 to ISTANBUL - /// (for the RSKIP400 calldata pricing) and only reaches SHANGHAI at - /// lovell700 — so across the whole arrowhead600..lovell700 window, about - /// 1.1M mainnet blocks, revm's stock BASEFEE halted `NotActivated` and - /// consumed the frame's entire gas where rskj pushed a value and continued. - /// - /// Same shape as `test_push0_activates_at_arrowhead600`; this one also - /// checks the value pushed, because installing the opcode is only half of - /// matching rskj — it has to push the block's minimum gas price. + /// Verifies nonce increments correctly across sequential transactions. #[test] - fn test_basefee_activates_at_arrowhead600() { - // Runtime: BASEFEE; PUSH1 0; MSTORE; PUSH1 32; PUSH1 0; RETURN - let runtime: Vec = vec![0x48, 0x60, 0x00, 0x52, 0x60, 0x20, 0x60, 0x00, 0xf3]; - // Init: CODECOPY the 9-byte runtime and RETURN it. - let mut init = vec![ - 0x60, 0x09, 0x60, 0x0c, 0x60, 0x00, 0x39, 0x60, 0x09, 0x60, 0x00, 0xf3, - ]; - init.extend_from_slice(&runtime); + fn test_rskj_nonce_tracking_across_block() { + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); - const MIN_GAS_PRICE: u64 = 59_240_000; + let sender = Address::repeat_byte(0xAB); + let r1 = Address::repeat_byte(0x01); + let r2 = Address::repeat_byte(0x02); + let r3 = Address::repeat_byte(0x03); - // `None` = the call failed (invalid opcode); `Some(v)` = BASEFEE pushed v. - let run = |number: u64| -> Option { - let header = Header { - minimum_gas_price: U256::from(MIN_GAS_PRICE), - ..dummy_header(number) - }; - let store = Arc::new(MemoryTrieStore::new()); - let root = TrieNode::empty(); - let sender = Address::repeat_byte(0x48); - 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 executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); - let deploy = rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(MIN_GAS_PRICE), - gas_limit: U256::from(1_000_000), - to: Bytes::new(), - value: U256::ZERO, - input: Bytes::from(init.clone()), - v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, - }; - let r1 = executor - .execute_block(&header, &[(deploy, sender)], &root, store.clone()) - .expect("deploy block"); - assert!(r1.tx_results[0].success, "deploy (no BASEFEE in init) always succeeds"); - let deployed = sender.create(0); - let root2 = crate::state::apply_state_changes( - &root, store.as_ref(), &r1.state_changes, &r1.markers, - ); - let call = rustock_core::Transaction { - nonce: 1, - gas_price: U256::from(MIN_GAS_PRICE), - gas_limit: U256::from(1_000_000), - to: Bytes::copy_from_slice(deployed.as_slice()), - value: U256::ZERO, + let root = put_account( + &root, store.as_ref(), &sender, 0, U256::from(1_000_000), + ); + + let block_store = Arc::new( + BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap(), + ); + + let header = dummy_header(8_000_000); + + let make_tx = |nonce: u64, to: &Address| { + rustock_core::Transaction { + nonce, + gas_price: U256::from(0), + gas_limit: U256::from(21_000), + to: Bytes::copy_from_slice(to.as_slice()), + value: U256::from(1), input: Bytes::new(), - v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, - }; - let r2 = executor - .execute_block(&header, &[(call, sender)], &root2, store) - .expect("call block"); - let res = &r2.tx_results[0]; - if !res.success { - return None; + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, } - assert_eq!(res.output.len(), 32, "BASEFEE returns one word"); - Some(U256::from_be_slice(&res.output)) }; - // fingerroot500 (PETERSBURG): rskj throws invalidOpCode, so must fail. - assert_eq!(run(6_223_699), None, "BASEFEE invalid pre-arrowhead600"); - // arrowhead600 (ISTANBUL in rustock): the window the audit found. - assert_eq!( - run(6_223_700), - Some(U256::from(MIN_GAS_PRICE)), - "RSKIP412 active at arrowhead600: pushes minimumGasPrice" + let txs = vec![ + (make_tx(0, &r1), sender), + (make_tx(1, &r2), sender), + (make_tx(2, &r3), sender), + ]; + + let executor = RskExecutor::new( + RskHardforkConfig::all_active(33), + block_store, ); - // arrowhead631, still ISTANBUL. - assert_eq!(run(6_549_300), Some(U256::from(MIN_GAS_PRICE))); - // lovell700 (SHANGHAI >= LONDON): revm's own BASEFEE already worked - // here, so this pins that the explicit install did not change it. - assert_eq!(run(7_338_024), Some(U256::from(MIN_GAS_PRICE))); + + let result = executor + .execute_block(&header, &txs, &root, store) + .unwrap(); + + assert_eq!(result.tx_results.len(), 3); + assert!(result.tx_results.iter().all(|r| r.success)); + + let sender_state = result.state_changes.get(&sender).unwrap(); + assert_eq!(sender_state.info.nonce, 3, "nonce should be 3 after 3 txs"); + + // Each recipient should have received 1 wei + for addr in [r1, r2, r3] { + let state = result.state_changes.get(&addr) + .expect("recipient should appear in state changes"); + assert_eq!(state.info.balance, U256::from(1)); + } } - /// Audit finding FRCR-476. - /// - /// rskj's `Bridge.receiveHeader` (Bridge.java:568-586) checks the argument - /// size *before* anything else and, on a mismatch, **returns** - /// `RECEIVE_HEADER_ERROR_SIZE_MISTMATCH = -20` (Bridge.java:243). The call - /// succeeds; the caller reads -20 off the stack. The size rule is - /// `BtcTransactionFormatUtils.isBlockHeaderSize`: exactly 80 bytes once - /// RSKIP124 is active. - /// - /// rustock had neither half. Under 80 bytes it raised a precompile error - /// (post-RSKIP197 a revert pushing 0, pre-197 a failed call consuming all - /// gas); over 80 bytes it silently truncated to the first 80 and processed - /// the header as if it were valid. Both fork from rskj on inputs any - /// relayer can submit. + /// Verifies SSTORE/SLOAD work correctly via a contract that stores a value. + /// Bytecode: PUSH1 0x42 PUSH1 0x00 SSTORE STOP + /// (stores 0x42 at slot 0) #[test] - fn test_receive_header_size_mismatch_returns_minus_20() { - // ABI-encode `receiveHeader(bytes)` with a payload of `len` bytes. - let call_input = |len: usize| -> Vec { - let mut input = crate::bridge::compute_selector("receiveHeader(bytes)").to_vec(); - let mut off = [0u8; 32]; - off[31] = 0x20; - input.extend_from_slice(&off); // offset to the bytes argument - let mut l = [0u8; 32]; - l[24..].copy_from_slice(&(len as u64).to_be_bytes()); - input.extend_from_slice(&l); // length - let mut payload = vec![0xABu8; len]; - payload.resize(len.div_ceil(32) * 32, 0); // right-pad to a word - input.extend_from_slice(&payload); - input - }; + fn test_contract_sstore_execution() { + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); - // Returns the int256 the Bridge produced, or None if the call failed. - let run = |len: usize| -> Option { - let store = Arc::new(MemoryTrieStore::new()); - let root = TrieNode::empty(); - let sender = Address::repeat_byte(0x7E); - 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 executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); - let tx = rustock_core::Transaction { - nonce: 0, - 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(call_input(len)), - v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, - }; - let r = executor - .execute_block(&dummy_header(7_000_000), &[(tx, sender)], &root, store) - .expect("block"); - let res = &r.tx_results[0]; - if !res.success || res.output.len() != 32 { - return None; - } - Some(alloy_primitives::I256::from_be_bytes::<32>(res.output[..32].try_into().unwrap())) - }; + 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 minus_20 = alloy_primitives::I256::try_from(-20i64).unwrap(); - // Too short: rskj returns -20, it does not throw. - assert_eq!(run(79), Some(minus_20), "79-byte header -> -20"); - assert_eq!(run(0), Some(minus_20), "empty header -> -20"); - // Too long: rskj returns -20 rather than truncating to 80. - assert_eq!(run(81), Some(minus_20), "81-byte header -> -20"); - assert_eq!(run(85), Some(minus_20), "85-byte header -> -20 once RSKIP124 is on"); + let block_store = Arc::new( + BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap(), + ); - // Exactly 80 is accepted for processing and must NOT report -20. The - // bytes are garbage, so the Bridge reports an ordinary BridgeSupport - // code (-3, previous block not found) — the point is that it got that - // far. - let at_80 = run(80).expect("80-byte header is processed"); - assert_ne!(at_80, minus_20, "80 bytes must reach BridgeSupport"); - assert_eq!( - at_80, - alloy_primitives::I256::try_from(-3i64).unwrap(), - "unknown parent -> RECEIVE_HEADER_CANT_FOUND_PREVIOUS_BLOCK" + let header = dummy_header(8_000_000); + + // Initcode that deploys a contract which stores 0x42 at slot 0: + // Runtime code: PUSH1 0x42 PUSH1 0x00 SSTORE STOP = 60 42 60 00 55 00 + // Initcode: copy runtime to memory and return it + // PUSH1 6 PUSH1 0x0c PUSH1 0 CODECOPY (copy 6 bytes from offset 12 to memory 0) + // PUSH1 6 PUSH1 0 RETURN (return 6 bytes from memory 0) + // + let initcode: Vec = vec![ + 0x60, 0x06, // PUSH1 6 (size) + 0x60, 0x0c, // PUSH1 12 (offset - after initcode) + 0x60, 0x00, // PUSH1 0 (destOffset) + 0x39, // CODECOPY + 0x60, 0x06, // PUSH1 6 (size) + 0x60, 0x00, // PUSH1 0 (offset) + 0xF3, // RETURN + // Runtime code starts here (offset 12): + 0x60, 0x42, // PUSH1 0x42 + 0x60, 0x00, // PUSH1 0x00 + 0x55, // SSTORE + 0x00, // STOP + ]; + + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(0), + gas_limit: U256::from(200_000), + to: Bytes::new(), + value: U256::ZERO, + input: Bytes::from(initcode), + 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, "contract creation should succeed"); + assert!(result.gas_used > 32_000, "should include CREATE base cost"); } - /// Audit findings FRCR-75 and FRCR-76. - /// - /// rskj `BridgeSupport.getBtcTransactionConfirmations` (BridgeSupport.java - /// :2194-2235) runs four checks in a fixed order, each with its own code - /// (BridgeSupport.java:90-94): - /// - /// ```text - /// block == null -> -1 INEXISTENT_BLOCK_HASH - /// blockDepth > 4320 -> -4 BLOCK_TOO_OLD - /// not the main-chain block at height -> -2 BLOCK_NOT_IN_BEST_CHAIN - /// (BlockStoreException) -> -3 INCONSISTENT_BLOCK - /// merkle branch does not reduce -> -5 INVALID_MERKLE_BRANCH - /// otherwise bestChainHeight - block.getHeight() + 1 - /// ``` - /// - /// rustock had -1, then jumped straight to the merkle check and reported - /// **-2** for a bad branch. It had no depth check and, more seriously, no - /// best-chain check at all: a block on an orphaned BTC fork is still in - /// Bridge storage, so rustock would confirm a transaction that exists only - /// on that fork — reporting a positive confirmation count where rskj - /// returns -2. Everything downstream of this method trusts that number. - /// - /// Driven end to end on regtest: build a 3-block chain plus a sibling that - /// forks at height 1, then query each case through the Bridge precompile. + /// Verifies zero-value calldata bytes cost 4 gas each (consistent across all specs). #[test] - fn test_btc_transaction_confirmations_codes_and_best_chain_check() { - use bitcoin::block::Version; - use bitcoin::consensus::serialize as btc_serialize; - use bitcoin::hashes::Hash as _; - use bitcoin::{BlockHash, CompactTarget, TxMerkleNode}; - use crate::bridge::btc_chain::{b256_to_bitcoin_hash, bitcoin_hash_to_b256}; + fn test_rskj_zero_calldata_bytes_cost_4() { + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); - const BLOCK: u64 = 7_000_000; // every relevant RSKIP active - // Regtest difficulty: any nonce is a valid proof of work. - let bits = CompactTarget::from_consensus(0x207f_ffff); - let hdr = |prev: BlockHash, merkle_seed: u8, nonce: u32| bitcoin::block::Header { - version: Version::from_consensus(1), - prev_blockhash: prev, - merkle_root: TxMerkleNode::from_byte_array([merkle_seed; 32]), - time: 1_700_000_000, - bits, - nonce, - }; + let sender = Address::repeat_byte(0xAA); + let recipient = Address::repeat_byte(0xBB); + let one_rbtc = U256::from(10u64).pow(U256::from(18)); - // Regtest's target is ~2^255, so roughly every other nonce works; - // search for one rather than assuming. - 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) - .expect("regtest PoW is trivial") - }; + let root = put_account(&root, store.as_ref(), &sender, 0, one_rbtc); - let genesis = bitcoin::constants::genesis_block(bitcoin::Network::Regtest) - .header - .block_hash(); - let h1 = mine(genesis, 0x11); - let h2 = mine(h1.block_hash(), 0x22); - let h3 = mine(h2.block_hash(), 0x33); - // Forks at height 1 with the same work as h1, so it never becomes the - // head — but it IS stored, which is the whole point. - let fork1 = mine(genesis, 0xF1); - assert_ne!(fork1.block_hash(), h1.block_hash()); + let block_store = Arc::new( + BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap(), + ); - let receive_header_tx = |nonce: u64, header: &bitcoin::block::Header| { - let raw = btc_serialize(header); - assert_eq!(raw.len(), 80); - let mut input = crate::bridge::compute_selector("receiveHeader(bytes)").to_vec(); - let mut off = [0u8; 32]; - off[31] = 0x20; - input.extend_from_slice(&off); - let mut l = [0u8; 32]; - l[31] = 80; - input.extend_from_slice(&l); - let mut payload = raw.clone(); - payload.resize(96, 0); - input.extend_from_slice(&payload); - rustock_core::Transaction { - nonce, - gas_price: U256::from(0), - gas_limit: U256::from(1_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 header = dummy_header(8_000_000); + + // 10 zero bytes + 0 non-zero bytes + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(0), + gas_limit: U256::from(30_000), + to: Bytes::copy_from_slice(recipient.as_slice()), + value: U256::ZERO, + input: Bytes::from(vec![0x00; 10]), + 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); + // 21000 base + 4*10 zero bytes = 21040 + assert_eq!(result.gas_used, 21_040, "intrinsic gas = 21000 + 4*10 zero bytes"); + } + + /// Verifies mixed zero/non-zero calldata gas cost. + #[test] + fn test_rskj_mixed_calldata_cost() { let store = Arc::new(MemoryTrieStore::new()); let root = TrieNode::empty(); - let sender = Address::repeat_byte(0xC0); + + let sender = Address::repeat_byte(0xAA); + let recipient = Address::repeat_byte(0xBB); 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 executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store) - .with_bridge_constants(crate::bridge::constants::BridgeConstants::regtest()); - let txs: Vec<_> = [&h1, &h2, &h3, &fork1] - .iter() - .enumerate() - .map(|(i, h)| (receive_header_tx(i as u64, h), sender)) - .collect(); - let built = executor - .execute_block(&dummy_header(BLOCK), &txs, &root, store.clone()) - .expect("header block"); - for (i, r) in built.tx_results.iter().enumerate() { - assert!(r.success, "receiveHeader #{i} call succeeded"); - assert_eq!( - alloy_primitives::I256::from_be_bytes::<32>(r.output[..32].try_into().unwrap()), - alloy_primitives::I256::ZERO, - "receiveHeader #{i} returned SUCCESS(0)" - ); - } - let root = crate::state::apply_state_changes( - &root, store.as_ref(), &built.state_changes, &built.markers, + let block_store = Arc::new( + BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap(), ); - // getBtcTransactionConfirmations(bytes32,bytes32,uint256,bytes32[]) - // with an EMPTY merkle branch: the branch reduces to btcTxHash itself, - // so a single-transaction block's merkle root is the tx hash. - let confirmations = |nonce: u64, tx_hash_display: [u8; 32], block: &bitcoin::block::Header| { - let mut input = - crate::bridge::compute_selector("getBtcTransactionConfirmations(bytes32,bytes32,uint256,bytes32[])") - .to_vec(); - input.extend_from_slice(&tx_hash_display); - input.extend_from_slice(bitcoin_hash_to_b256(&block.block_hash()).as_slice()); - input.extend_from_slice(&[0u8; 32]); // merkleBranchPath = 0 - let mut off = [0u8; 32]; - off[31] = 0x80; - input.extend_from_slice(&off); // offset of the hashes array - input.extend_from_slice(&[0u8; 32]); // hashes.length = 0 - let tx = 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 r = executor - .execute_block(&dummy_header(BLOCK), &[(tx, sender)], &root, store.clone()) - .expect("query block"); - let res = &r.tx_results[0]; - assert!(res.success, "getBtcTransactionConfirmations must not throw"); - alloy_primitives::I256::from_be_bytes::<32>(res.output[..32].try_into().unwrap()) - }; - - // The merkle root in DISPLAY order is what an ABI caller passes as - // btcTxHash for a one-transaction block. - let tx_hash_of = |h: &bitcoin::block::Header| -> [u8; 32] { - let mut b = h.merkle_root.to_raw_hash().to_byte_array(); - b.reverse(); - b - }; - let code = |n: i64| alloy_primitives::I256::try_from(n).unwrap(); - - // Happy path: h1 is at height 1, the head is h3 at height 3. - assert_eq!( - confirmations(4, tx_hash_of(&h1), &h1), - code(3), - "bestChainHeight - height + 1" - ); - assert_eq!(confirmations(4, tx_hash_of(&h3), &h3), code(1), "the head itself"); + let header = dummy_header(8_000_000); - // -1: the block hash is not stored at all. - let unknown = hdr(genesis, 0xEE, 999_999); - assert_eq!( - confirmations(4, tx_hash_of(&unknown), &unknown), - code(-1), - "INEXISTENT_BLOCK_HASH" - ); + // 5 zero bytes + 3 non-zero bytes at Shanghai/Istanbul rate + let mut data = vec![0x00; 5]; + data.extend_from_slice(&[0xFF, 0xAB, 0xCD]); - // -5: stored, in the best chain, but the branch does not reduce to the - // merkle root. rustock used to report -2 here. - assert_eq!( - confirmations(4, [0xAB; 32], &h1), - code(-5), - "INVALID_MERKLE_BRANCH" - ); + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(0), + gas_limit: U256::from(30_000), + to: Bytes::copy_from_slice(recipient.as_slice()), + value: U256::ZERO, + input: Bytes::from(data), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; - // -2: stored, but on an orphaned fork. rustock used to report 3 — a - // confirmed transaction that is not on the BTC main chain. - assert_eq!( - confirmations(4, tx_hash_of(&fork1), &fork1), - code(-2), - "BLOCK_NOT_IN_BEST_CHAIN" + let executor = RskExecutor::new( + RskHardforkConfig::all_active(33), + block_store, ); - // Ordering: the best-chain check runs BEFORE the merkle check, so a - // fork block with a bad branch still reports -2, not -5. - assert_eq!( - confirmations(4, [0xAB; 32], &fork1), - code(-2), - "best-chain check precedes the merkle check" - ); + let result = executor + .execute_tx(&header, &tx, sender, &root, store) + .unwrap(); - // Sanity: the fork block really is stored, so -2 came from the - // best-chain check and not from the block being missing. - let _ = b256_to_bitcoin_hash; + assert!(result.success); + // 21000 + 4*5 (zero) + 16*3 (non-zero at Istanbul) = 21000 + 20 + 48 = 21068 + assert_eq!(result.gas_used, 21_068, "21000 + 4*5 + 16*3 = 21068"); } - /// Audit findings FRCR-78 and FRCR-678. - /// - /// `getEstimatedFeesForNextPegOutEvent` is **transaction-callable** — rskj - /// `BridgeMethods` gives it `fixedPermission(false)` — so a contract can - /// CALL it and branch on the answer, which makes the value consensus. - /// rustock returned a hardcoded `0`. - /// - /// rskj `BridgeSupport.getEstimatedFeesForNextPegOutEvent`: + /// A block that declares two parallel sublists, executed both ways. /// - /// ```text - /// shouldReturnZeroEstimatedFees() -> 0 - /// !RSKIP305 -> feePerKB * simulatePegoutTxSize(2 in, queued+2 out) / 1000 - /// otherwise -> inputSum - outputSum of a simulated batched peg-out, - /// falling back to the line above on failure - /// ``` + /// Running the sublists on their own threads and running the whole block + /// as one must reach the same state. They are not the same computation: + /// one journal in transaction order against one journal per sublist + /// against the parent, merged in sublist order. They agree because the + /// sublists here are independent -- which is a condition of the block's + /// validity, not an accident of the fixture. /// - /// Driven through the Bridge precompile: `releaseBtc` transactions build a - /// real release queue, then the getter is called and its int256 read back. + /// The replay over testnet is the real evidence; this is here so that a + /// regression shows up without a 200 GB snapshot. #[test] - fn test_estimated_fees_for_next_pegout_is_not_hardcoded_zero() { - let one_rbtc = U256::from(10u64).pow(U256::from(18)); - - // Run `pegouts` releaseBtc transactions, then query the estimate. - let query = |number: u64, pegouts: u64| -> U256 { - let store = Arc::new(MemoryTrieStore::new()); - let root = TrieNode::empty(); - let sender = Address::repeat_byte(0xE5); - let root = put_account(&root, store.as_ref(), &sender, 0, one_rbtc * U256::from(50)); - let block_store = - Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); - let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); - let header = dummy_header(number); - - let mut root = root; - for nonce in 0..pegouts { - let tx = rustock_core::Transaction { - nonce, - gas_price: U256::from(0), - gas_limit: U256::from(200_000), - to: Bytes::copy_from_slice(crate::precompiles::BRIDGE_ADDR.as_slice()), - value: one_rbtc, // 1 RBTC, well above the peg-out minimum - input: Bytes::new(), // empty input = releaseBtc - v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, - }; - let r = executor - .execute_block(&header, &[(tx, sender)], &root, store.clone()) - .expect("releaseBtc block"); - assert!(r.tx_results[0].success, "releaseBtc must succeed"); - root = crate::state::apply_state_changes( - &root, store.as_ref(), &r.state_changes, &r.markers, - ); - } + fn a_split_block_executes_the_same_on_one_thread_and_on_two() { + let store: Arc = Arc::new(MemoryTrieStore::new()); + let block_store = Arc::new( + BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap(), + ); - let call = rustock_core::Transaction { - nonce: pegouts, - gas_price: U256::from(0), - gas_limit: U256::from(500_000), - to: Bytes::copy_from_slice(crate::precompiles::BRIDGE_ADDR.as_slice()), - value: U256::ZERO, - input: Bytes::from( - crate::bridge::compute_selector("getEstimatedFeesForNextPegOutEvent()").to_vec(), - ), - v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, - }; - let r = executor - .execute_block(&header, &[(call, sender)], &root, store) - .expect("query block"); - let res = &r.tx_results[0]; - assert!(res.success, "getEstimatedFeesForNextPegOutEvent must not throw"); - assert_eq!(res.output.len(), 32); - U256::from_be_slice(&res.output) - }; + // Three senders, so that no two transactions share a nonce and the + // sublists have nothing in common: same-sender transactions are + // connected by the nonce and would have to share a sublist. + let senders: Vec
= + (1u8..=3).map(|n| Address::repeat_byte(0x10 + n)).collect(); + let recipients: Vec
= + (1u8..=3).map(|n| Address::repeat_byte(0xA0 + n)).collect(); - // hop400 (#4,598,500) .. fingerroot500 (#5,468,000): RSKIP271 active, - // RSKIP385 not — shouldReturnZeroEstimatedFees is true for an EMPTY - // queue only. - assert_eq!(query(5_000_000, 0), U256::ZERO, "empty queue pre-RSKIP385 -> 0"); - let pre385_one = query(5_000_000, 1); - assert!(!pre385_one.is_zero(), "a queued peg-out pre-RSKIP385 must cost something"); + let mut root = TrieNode::empty(); + for s in &senders { + root = put_account(&root, store.as_ref(), s, 0, U256::from(10_000_000_000_000u64)); + } - // fingerroot500 onward RSKIP385 is active, so the zero short-circuit is - // gone: even an empty queue reports the 2-input / 2-output estimate. - let f500_empty = query(6_000_000, 0); - assert!(!f500_empty.is_zero(), "RSKIP385 removes the empty-queue zero"); + let mut header = dummy_header(8_000_000); // testnet reed810 is 7_139_600 + // Two parallel sublists -- {tx0} and {tx1} -- and tx2 sequential. + header.version_fields = Some(rustock_core::types::header::HeaderVersionFields { + version: 1, + 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); - // reed800 (#8,052,200): RSKIP305 switches to the simulation. With no - // federation UTXOs the builder cannot fund the peg-out, which is rskj's - // BridgeIllegalArgumentException path -> fall back to the size estimate. - let reed_empty = query(8_500_000, 0); - assert!(!reed_empty.is_zero(), "reed800 fallback must still produce a fee"); + let transactions: Vec<(rustock_core::Transaction, Address)> = (0..3) + .map(|i| { + ( + rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(1_000), + gas_limit: U256::from(30_000), + to: Bytes::copy_from_slice(recipients[i].as_slice()), + value: U256::from(1_000), + input: Bytes::new(), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }, + senders[i], + ) + }) + .collect(); - // vetiver900 (#8,804,200) activates RSKIP540, which only changes the - // hypothetical amount; with the fallback in play the answer is stable. - let vetiver_empty = query(9_000_000, 0); - assert!(!vetiver_empty.is_zero()); + let run = |threads: usize| { + // Testnet, above reed810: a header cannot carry edges on a chain + // where RSKIP-144 is unscheduled, and the parallel path requires + // the postponed fee settlement that comes with it. + let executor = RskExecutor::new(RskHardforkConfig::testnet(), block_store.clone()) + .with_execution_threads(threads); + let result = executor + .execute_block(&header, &transactions, &root, store.clone()) + .expect("block executes"); + let applied = result.applied_root.clone().unwrap_or_else(|| { + crate::state::apply_state_changes( + &root, + store.as_ref(), + &result.state_changes, + &result.markers, + ) + }); + let mut balances: Vec<(Address, U256, u64)> = result + .state_changes + .iter() + .map(|(a, acc)| (*a, acc.info.balance, acc.info.nonce)) + .collect(); + balances.sort_by_key(|(a, _, _)| *a); + ( + applied.compute_hash(store.as_ref()), + result.gas_used, + result.paid_fees, + result.tx_results.iter().map(|r| (r.success, r.gas_used)).collect::>(), + balances, + ) + }; - // More queued peg-outs mean more outputs, hence a bigger transaction - // and a strictly bigger fee. This is what a hardcoded 0 could never do. - let f500_three = query(6_000_000, 3); - assert!( - f500_three > f500_empty, - "3 queued peg-outs must cost more than 0: {f500_three} vs {f500_empty}" - ); + let one = run(1); + let two = run(2); + assert_eq!(one.3, two.3, "per-transaction results"); + assert_eq!(one.1, two.1, "gas used"); + assert_eq!(one.2, two.2, "paid fees"); + assert_eq!(one.4, two.4, "account balances"); + assert_eq!(one.0, two.0, "state root"); + assert_eq!(one.3.len(), 3, "every transaction ran exactly once"); } - /// Ported from rskj InitcodeCostCalculatorTest. - /// Validates that contract creation includes initcode word cost at Shanghai. + /// Two transactions from the same sender, placed in different parallel + /// sublists. This is the race RSKIP-144 exists to forbid, and it must be + /// caught when the sublists really do run apart. + /// + /// Each sublist runs against the state the block starts from, so both + /// transactions see nonce 0 and both succeed -- which is precisely the + /// problem, and why the rule is a condition on the block rather than + /// something sequential execution would have noticed. #[test] - fn test_rskj_initcode_cost_shanghai() { - let store = Arc::new(MemoryTrieStore::new()); - let root = TrieNode::empty(); - - let sender = Address::repeat_byte(0xEE); - let one_rbtc = U256::from(10u64).pow(U256::from(18)); - let root = put_account(&root, store.as_ref(), &sender, 0, one_rbtc); - + fn a_block_whose_sublists_share_a_sender_is_caught_in_parallel() { + let store: Arc = Arc::new(MemoryTrieStore::new()); let block_store = Arc::new( BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap(), ); + let sender = Address::repeat_byte(0x11); + let root = put_account( + &TrieNode::empty(), + store.as_ref(), + &sender, + 0, + U256::from(10_000_000_000_000u64), + ); - // Post-Lovell = SHANGHAI (has initcode metering) - let header = dummy_header(8_000_000); - - // 4 bytes of initcode: ceil(4/32) = 1 word, INITCODE_WORD_COST = 2 - // PUSH1 0x00 PUSH1 0x00 RETURN + padding - let initcode = vec![0x60, 0x00, 0x60, 0x00]; // 4 bytes + 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]), + base_event: None, + }); - let tx = rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(0), - gas_limit: U256::from(200_000), - to: Bytes::new(), // contract creation - value: U256::ZERO, - input: Bytes::from(initcode), + let tx = |nonce: u64, to: u8| rustock_core::Transaction { + nonce, + gas_price: U256::from(1_000), + gas_limit: U256::from(30_000), + to: Bytes::copy_from_slice(Address::repeat_byte(to).as_slice()), + value: U256::from(1_000), + input: Bytes::new(), v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, }; + // tx0 in sublist 0 and tx1 in sublist 1, both from `sender`, both + // with nonce 0 -- which is what each of them sees, each running + // against the state the block starts from. A miner would never build + // this; the validator still has to refuse it. + let transactions = vec![ + (tx(0, 0xA1), sender), + (tx(0, 0xA2), sender), + (tx(0, 0xA3), Address::repeat_byte(0x22)), + ]; - let executor = RskExecutor::new( - RskHardforkConfig::all_active(33), - block_store.clone(), + let edges = [1i16, 2]; + crate::collision::start( + &edges, + &[], + crate::collision::write_semantics(header.number), ); + let executor = RskExecutor::new(RskHardforkConfig::testnet(), block_store) + .with_execution_threads(2); + let _ = executor.execute_block(&header, &transactions, &root, store); + let collision = crate::collision::finish() + .expect("recording was started") + .first_collision() + .expect("both sublists write the sender's account cell"); - let result = executor - .execute_tx(&header, &tx, sender, &root, store.clone()) - .unwrap(); - - // Shanghai create intrinsic: 32000 (create base) + 16*4 (non-zero bytes at Istanbul) + 2 (initcode: 1 word * 2) - // = 32000 + 64 + 2 = 32066 - // Note: this tx will revert because the initcode doesn't end with RETURN, - // but we can still check gas_used >= intrinsic cost - assert!(result.gas_used >= 32_066, "should include initcode metering cost"); + assert_eq!(collision.kind, crate::collision::CollisionKind::WriteWrite); + assert_eq!( + collision.key, + rustock_trie::account_key(&sender), + "the nonce is what connects them, and it lives in the account cell" + ); } - /// Runs a contract-creation tx with the given initcode at the given mainnet height. - fn run_create_at(initcode: Vec, block_number: u64, gas_limit: u64) -> TxExecutionResult { - let store = Arc::new(MemoryTrieStore::new()); - let root = TrieNode::empty(); - let sender = Address::repeat_byte(0xEE); - let one_rbtc = U256::from(10u64).pow(U256::from(18)); - let root = put_account(&root, store.as_ref(), &sender, 0, one_rbtc); + /// 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 header = dummy_header(block_number); + 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 tx = rustock_core::Transaction { + 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(0), - gas_limit: U256::from(gas_limit), - to: Bytes::new(), // contract creation + 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::from(initcode), + 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]), + ]; - let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); - executor.execute_tx(&header, &tx, sender, &root, store).unwrap() + 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`. + #[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" + ); + } } - /// Ported from rskj ContractCodePrefixDslTest - /// (dsl/contract_code_prefix_rskip544/create_fails_with_ef_byte_activated.txt): - /// after RSKIP544 (vetiver900), creating a contract whose code starts with - /// 0xEF fails and burns all gas. + /// The count restarts with each sublist: what one used is not taken from + /// another's gas, and within one, what ran is. #[test] - fn test_rskip544_create_ef_code_fails_after_vetiver900() { - // PUSH1 0xEF, PUSH1 0, MSTORE8, PUSH1 1, PUSH1 0, RETURN -> code = [0xEF] - let initcode = hex::decode("60ef60005360016000f3").unwrap(); - let result = run_create_at(initcode, 8_804_200, 300_000); - assert!(!result.success, "0xEF-prefixed code must be rejected post-vetiver900"); - assert_eq!(result.gas_used, 300_000, "all gas is consumed (rskj DSL groundtruth)"); + 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()); } - /// Ported from rskj ContractCodePrefixDslTest - /// (testCreateSucceedsWithEFByteBeforeRSKIP544Activation): before vetiver900, - /// 0xEF-prefixed code is accepted — rskj only enforces EIP-3541 from RSKIP544. + /// 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 test_rskip544_create_ef_code_succeeds_before_vetiver900() { - let initcode = hex::decode("60ef60005360016000f3").unwrap(); - let result = run_create_at(initcode, 8_804_199, 300_000); - assert!(result.success, "0xEF-prefixed code must be accepted pre-vetiver900"); - assert!(result.gas_used > 21_000 && result.gas_used < 300_000); + 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()); } - /// Ported from rskj ContractCodePrefixDslTest (create_succeeds_with_fe_byte.txt): - /// 0xFE-prefixed code is fine even after RSKIP544. + /// 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 test_rskip544_create_fe_code_succeeds_after_vetiver900() { - let initcode = hex::decode("60fe60005360016000f3").unwrap(); - let result = run_create_at(initcode, 8_804_200, 300_000); - assert!(result.success); - assert!(result.gas_used > 21_000 && result.gas_used < 300_000); + 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" + ); } - /// Ported from rskj ContractCodePrefixDslTest (create_succeeds_with_empty_code.txt): - /// empty contract code is allowed after RSKIP544. + /// 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 test_rskip544_create_empty_code_succeeds_after_vetiver900() { - let initcode = hex::decode("60006000f3").unwrap(); - let result = run_create_at(initcode, 8_804_200, 300_000); - assert!(result.success); - assert!(result.gas_used > 21_000 && result.gas_used < 300_000); + 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}"); + } } - /// Ported from rskj Create2Test patterns. - /// Verifies that CREATE2 produces the correct contract address. + /// 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 test_rskj_create2_address_computation() { - // CREATE2 address = keccak256(0xff ++ sender ++ salt ++ keccak256(init_code))[12..] - use sha3::{Digest, Keccak256}; + 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}"); + } - let sender_bytes = hex_to_bytes("0f6510583d425cfcf94b99f8b073b44f60d1912b"); - let salt = [0u8; 32]; - let init_code = vec![0x60, 0x0b]; // 2 bytes of init code (from rskj test) + /// 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); - let init_code_hash = Keccak256::digest(&init_code); + // 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:?}"); + } - let mut input = Vec::new(); - input.push(0xFF); - input.extend_from_slice(&sender_bytes); - input.extend_from_slice(&salt); - input.extend_from_slice(&init_code_hash); + /// 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))); + } - let result = Keccak256::digest(&input); - let address = &result[12..]; + /// 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_fits_as_rskj_counts_it() { + const LONG_MAX: u64 = i64::MAX as u64; - let expected = hex_to_bytes("3bc3efa1c487a1ebfc911b47b548e2c82436a212"); - assert_eq!( - address, expected.as_slice(), - "CREATE2 address should match rskj test vector" + 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" ); } - /// CREATE2 with different salt → different address (rskj Create2Test.testCREATE2_SaltNumber). + /// 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 test_rskj_create2_salt_changes_address() { - use sha3::{Digest, Keccak256}; - - let sender_bytes = hex_to_bytes("0f6510583d425cfcf94b99f8b073b44f60d1912b"); - let mut salt = [0u8; 32]; - salt[28..].copy_from_slice(&0xCAFEBABEu32.to_be_bytes()); - let init_code = vec![0x60, 0x1b]; - - let init_code_hash = Keccak256::digest(&init_code); - - let mut input = Vec::new(); - input.push(0xFF); - input.extend_from_slice(&sender_bytes); - input.extend_from_slice(&salt); - input.extend_from_slice(&init_code_hash); + 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()); + } - let result = Keccak256::digest(&input); - let address = &result[12..]; + /// 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 expected = hex_to_bytes("19542b03f2d5d4e1910dbe096faf0842d928883d"); - assert_eq!( - address, expected.as_slice(), - "CREATE2 with salt 0xcafebabe should match rskj" + 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); } - /// CREATE2 with different sender → different address (rskj Create2Test.testCREATE2_Address). + /// 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 test_rskj_create2_sender_changes_address() { - use sha3::{Digest, Keccak256}; - - let sender_bytes = hex_to_bytes("deadbeef00000000000000000000000000000000"); - let mut salt = [0u8; 32]; - salt[28..].copy_from_slice(&0xCAFEBABEu32.to_be_bytes()); - let init_code = vec![0x60, 0x1b]; - - let init_code_hash = Keccak256::digest(&init_code); + 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 mut input = Vec::new(); - input.push(0xFF); - input.extend_from_slice(&sender_bytes); - input.extend_from_slice(&salt); - input.extend_from_slice(&init_code_hash); + 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] }); + } + } - let result = Keccak256::digest(&input); - let address = &result[12..]; + /// 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) + }; - let expected = hex_to_bytes("3ba1dc70cc17e740f4bd85052af074b2b2a49e06"); + assert_eq!(seen(RskHardforkConfig::testnet()), U256::ZERO, "postponed"); + let (moved, _) = RskHardforkConfig::testnet() + .with_rskj_overrides(&[], &[("rskip351".to_string(), None)]) + .unwrap(); assert_eq!( - address, expected.as_slice(), - "CREATE2 with deadbeef sender should match rskj" + seen(moved), + U256::from(21_000), + "tx0's fee, at a gas price of 1" ); } - /// Verifies nonce increments correctly across sequential transactions. + /// The thread setting only decides *how*, never *whether*: a block with + /// no sublists runs as one however many threads are allowed. #[test] - fn test_rskj_nonce_tracking_across_block() { - let store = Arc::new(MemoryTrieStore::new()); - let root = TrieNode::empty(); - - let sender = Address::repeat_byte(0xAB); - let r1 = Address::repeat_byte(0x01); - let r2 = Address::repeat_byte(0x02); - let r3 = Address::repeat_byte(0x03); - - let root = put_account( - &root, store.as_ref(), &sender, 0, U256::from(1_000_000), - ); - + fn a_block_without_sublists_ignores_the_thread_count() { + let store: Arc = Arc::new(MemoryTrieStore::new()); let block_store = Arc::new( BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap(), ); - - let header = dummy_header(8_000_000); - - let make_tx = |nonce: u64, to: &Address| { + let sender = Address::repeat_byte(0x11); + let root = put_account( + &TrieNode::empty(), + store.as_ref(), + &sender, + 0, + U256::from(10_000_000_000_000u64), + ); + let header = dummy_header(8_000_000); // version_fields: None + let transactions = vec![( rustock_core::Transaction { - nonce, - gas_price: U256::from(0), - gas_limit: U256::from(21_000), - to: Bytes::copy_from_slice(to.as_slice()), - value: U256::from(1), + nonce: 0, + gas_price: U256::from(1_000), + gas_limit: U256::from(30_000), + to: Bytes::copy_from_slice(Address::repeat_byte(0xA1).as_slice()), + value: U256::from(1_000), input: Bytes::new(), v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, - } - }; + }, + sender, + )]; + for threads in [1usize, 2] { + let executor = RskExecutor::new(RskHardforkConfig::all_active(33), block_store.clone()) + .with_execution_threads(threads); + let result = executor + .execute_block(&header, &transactions, &root, store.clone()) + .expect("block executes"); + assert!( + result.applied_root.is_none(), + "a block with no sublists is one run, so the caller applies it ({threads} threads)" + ); + } + } + + /// Verifies that the beneficiary (coinbase) receives gas fees in state changes. + #[test] + fn test_fees_routed_to_remasc() { + use crate::precompiles::REMASC_ADDR; + + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + + let sender = Address::repeat_byte(0xAA); + let recipient = Address::repeat_byte(0xBB); + let beneficiary = Address::repeat_byte(0x01); + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + + let root = put_account(&root, store.as_ref(), &sender, 0, one_rbtc); - let txs = vec![ - (make_tx(0, &r1), sender), - (make_tx(1, &r2), sender), - (make_tx(2, &r3), sender), - ]; + let block_store = Arc::new( + BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap(), + ); + + let mut header = dummy_header(8_000_000); + header.beneficiary = beneficiary; + + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(100), + gas_limit: U256::from(21_000), + to: Bytes::copy_from_slice(recipient.as_slice()), + value: U256::from(1_000), + input: Bytes::new(), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; let executor = RskExecutor::new( RskHardforkConfig::all_active(33), @@ -6047,28 +9794,38 @@ mod tests { ); let result = executor - .execute_block(&header, &txs, &root, store) + .execute_block(&header, &[(tx, sender)], &root, store) .unwrap(); - assert_eq!(result.tx_results.len(), 3); - assert!(result.tx_results.iter().all(|r| r.success)); + assert!(result.tx_results[0].success); - let sender_state = result.state_changes.get(&sender).unwrap(); - assert_eq!(sender_state.info.nonce, 3, "nonce should be 3 after 3 txs"); + // Fees are routed to REMASC, not the miner. gas_used * gas_price = 21000 * 100 + let remasc_state = result.state_changes.get(&REMASC_ADDR) + .expect("REMASC should appear in state changes"); + assert_eq!( + remasc_state.info.balance, + U256::from(2_100_000u64), + "REMASC receives 21000 * 100 = 2_100_000" + ); - // Each recipient should have received 1 wei - for addr in [r1, r2, r3] { - let state = result.state_changes.get(&addr) - .expect("recipient should appear in state changes"); - assert_eq!(state.info.balance, U256::from(1)); - } + // The miner (header.beneficiary) should NOT receive fees directly + assert!( + !result.state_changes.contains_key(&beneficiary), + "miner should not receive fees directly; REMASC distributes later" + ); } - /// Verifies SSTORE/SLOAD work correctly via a contract that stores a value. - /// Bytecode: PUSH1 0x42 PUSH1 0x00 SSTORE STOP - /// (stores 0x42 at slot 0) + // ----------------------------------------------------------------------- + // End-to-end precompile execution tests + // ----------------------------------------------------------------------- + + /// A USER tx to the REMASC precompile (0x01000008) is rskj's + /// RemascInvalidInvocationException ("Invoked Remasc outside last tx of + /// the block") — invisible for a direct tx: receipt SUCCESS with only + /// intrinsic gas (getGasForData is 0), no fee distribution, but the + /// sender is charged the FULL gas limit and REMASC receives it. #[test] - fn test_contract_sstore_execution() { + fn test_remasc_precompile_direct_call() { let store = Arc::new(MemoryTrieStore::new()); let root = TrieNode::empty(); @@ -6080,36 +9837,17 @@ mod tests { BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap(), ); - let header = dummy_header(8_000_000); + let header = dummy_header(100); - // Initcode that deploys a contract which stores 0x42 at slot 0: - // Runtime code: PUSH1 0x42 PUSH1 0x00 SSTORE STOP = 60 42 60 00 55 00 - // Initcode: copy runtime to memory and return it - // PUSH1 6 PUSH1 0x0c PUSH1 0 CODECOPY (copy 6 bytes from offset 12 to memory 0) - // PUSH1 6 PUSH1 0 RETURN (return 6 bytes from memory 0) - // - let initcode: Vec = vec![ - 0x60, 0x06, // PUSH1 6 (size) - 0x60, 0x0c, // PUSH1 12 (offset - after initcode) - 0x60, 0x00, // PUSH1 0 (destOffset) - 0x39, // CODECOPY - 0x60, 0x06, // PUSH1 6 (size) - 0x60, 0x00, // PUSH1 0 (offset) - 0xF3, // RETURN - // Runtime code starts here (offset 12): - 0x60, 0x42, // PUSH1 0x42 - 0x60, 0x00, // PUSH1 0x00 - 0x55, // SSTORE - 0x00, // STOP - ]; + let remasc_addr = crate::precompiles::REMASC_ADDR; let tx = rustock_core::Transaction { nonce: 0, - gas_price: U256::from(0), - gas_limit: U256::from(200_000), - to: Bytes::new(), + gas_price: U256::from(1_000), + gas_limit: U256::from(100_000), + to: Bytes::copy_from_slice(remasc_addr.as_slice()), value: U256::ZERO, - input: Bytes::from(initcode), + input: Bytes::new(), v: 0, r: U256::ZERO, s: U256::ZERO, @@ -6122,23 +9860,36 @@ mod tests { ); let result = executor - .execute_tx(&header, &tx, sender, &root, store) + .execute_block(&header, &[(tx, sender)], &root, store) .unwrap(); - assert!(result.success, "contract creation should succeed"); - assert!(result.gas_used > 32_000, "should include CREATE base cost"); + assert!(result.tx_results[0].success, "receipt records SUCCESS"); + assert_eq!( + result.tx_results[0].gas_used, 21_000, + "REMASC costs 0 gas, receipt shows intrinsic only" + ); + // No fee distribution may run for a user tx. + let remasc_state = result.state_changes.get(&remasc_addr).unwrap(); + assert!(remasc_state.storage.is_empty(), "no REMASC storage writes"); + let full_fee = U256::from(100_000u64) * U256::from(1_000u64); + assert_eq!( + result.state_changes.get(&sender).unwrap().info.balance, + one_rbtc - full_fee, + "sender pays for the full gas limit" + ); + assert_eq!(remasc_state.info.balance, full_fee); + assert_eq!(result.paid_fees, full_fee); } - /// Verifies zero-value calldata bytes cost 4 gas each (consistent across all specs). + /// Direct tx to Bridge precompile (0x01000006). + /// Bridge stub charges 10,000 gas. #[test] - fn test_rskj_zero_calldata_bytes_cost_4() { + fn test_bridge_precompile_direct_call() { let store = Arc::new(MemoryTrieStore::new()); let root = TrieNode::empty(); let sender = Address::repeat_byte(0xAA); - let recipient = Address::repeat_byte(0xBB); 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( @@ -6147,14 +9898,15 @@ mod tests { let header = dummy_header(8_000_000); - // 10 zero bytes + 0 non-zero bytes + let bridge_addr = crate::precompiles::BRIDGE_ADDR; + let tx = rustock_core::Transaction { nonce: 0, gas_price: U256::from(0), - gas_limit: U256::from(30_000), - to: Bytes::copy_from_slice(recipient.as_slice()), + gas_limit: U256::from(100_000), + to: Bytes::copy_from_slice(bridge_addr.as_slice()), value: U256::ZERO, - input: Bytes::from(vec![0x00; 10]), + input: Bytes::new(), v: 0, r: U256::ZERO, s: U256::ZERO, @@ -6170,21 +9922,24 @@ mod tests { .execute_tx(&header, &tx, sender, &root, store) .unwrap(); - assert!(result.success); - // 21000 base + 4*10 zero bytes = 21040 - assert_eq!(result.gas_used, 21_040, "intrinsic gas = 21000 + 4*10 zero bytes"); + assert!(result.success, "Bridge direct call should succeed"); + assert_eq!( + result.gas_used, + 21_000 + 23_000, + "Empty Bridge call dispatches releaseBtc (23k gas) on top of intrinsic" + ); } - /// Verifies mixed zero/non-zero calldata gas cost. + /// Direct tx to Identity precompile (0x04) with known input. + /// Ported from rskj PrecompiledContractTest.identityTest1: + /// input = 0x112233445566, output = 0x112233445566, gas = 18 #[test] - fn test_rskj_mixed_calldata_cost() { + fn test_identity_precompile_direct_call() { let store = Arc::new(MemoryTrieStore::new()); let root = TrieNode::empty(); let sender = Address::repeat_byte(0xAA); - let recipient = Address::repeat_byte(0xBB); 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( @@ -6193,17 +9948,16 @@ mod tests { let header = dummy_header(8_000_000); - // 5 zero bytes + 3 non-zero bytes at Shanghai/Istanbul rate - let mut data = vec![0x00; 5]; - data.extend_from_slice(&[0xFF, 0xAB, 0xCD]); + let identity_addr = Address::new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4]); + let input_data: Vec = vec![0x11, 0x22, 0x33, 0x44, 0x55, 0x66]; let tx = rustock_core::Transaction { nonce: 0, gas_price: U256::from(0), - gas_limit: U256::from(30_000), - to: Bytes::copy_from_slice(recipient.as_slice()), + gas_limit: U256::from(100_000), + to: Bytes::copy_from_slice(identity_addr.as_slice()), value: U256::ZERO, - input: Bytes::from(data), + input: Bytes::from(input_data.clone()), v: 0, r: U256::ZERO, s: U256::ZERO, @@ -6219,260 +9973,204 @@ mod tests { .execute_tx(&header, &tx, sender, &root, store) .unwrap(); - assert!(result.success); - // 21000 + 4*5 (zero) + 16*3 (non-zero at Istanbul) = 21000 + 20 + 48 = 21068 - assert_eq!(result.gas_used, 21_068, "21000 + 4*5 + 16*3 = 21068"); - } - - /// A block that declares two parallel sublists, executed both ways. - /// - /// Running the sublists on their own threads and running the whole block - /// as one must reach the same state. They are not the same computation: - /// one journal in transaction order against one journal per sublist - /// against the parent, merged in sublist order. They agree because the - /// sublists here are independent -- which is a condition of the block's - /// validity, not an accident of the fixture. - /// - /// The replay over testnet is the real evidence; this is here so that a - /// regression shows up without a 200 GB snapshot. - #[test] - fn a_split_block_executes_the_same_on_one_thread_and_on_two() { - let store: Arc = Arc::new(MemoryTrieStore::new()); - let block_store = Arc::new( - BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap(), - ); - - // Three senders, so that no two transactions share a nonce and the - // sublists have nothing in common: same-sender transactions are - // connected by the nonce and would have to share a sublist. - let senders: Vec
= - (1u8..=3).map(|n| Address::repeat_byte(0x10 + n)).collect(); - let recipients: Vec
= - (1u8..=3).map(|n| Address::repeat_byte(0xA0 + n)).collect(); - - let mut root = TrieNode::empty(); - for s in &senders { - root = put_account(&root, store.as_ref(), s, 0, U256::from(10_000_000_000_000u64)); - } - - let mut header = dummy_header(8_000_000); // testnet reed810 is 7_139_600 - // Two parallel sublists -- {tx0} and {tx1} -- and tx2 sequential. - header.version_fields = Some(rustock_core::types::header::HeaderVersionFields { - version: 1, - edges: Some(vec![1, 2]), - base_event: None, - }); - - let transactions: Vec<(rustock_core::Transaction, Address)> = (0..3) - .map(|i| { - ( - rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(1_000), - gas_limit: U256::from(30_000), - to: Bytes::copy_from_slice(recipients[i].as_slice()), - value: U256::from(1_000), - input: Bytes::new(), - v: 0, - r: U256::ZERO, - s: U256::ZERO, - cached_rlp: None, - }, - senders[i], - ) - }) - .collect(); - - let run = |threads: usize| { - // Testnet, above reed810: a header cannot carry edges on a chain - // where RSKIP-144 is unscheduled, and the parallel path requires - // the postponed fee settlement that comes with it. - let executor = RskExecutor::new(RskHardforkConfig::testnet(), block_store.clone()) - .with_execution_threads(threads); - let result = executor - .execute_block(&header, &transactions, &root, store.clone()) - .expect("block executes"); - let applied = result.applied_root.clone().unwrap_or_else(|| { - crate::state::apply_state_changes( - &root, - store.as_ref(), - &result.state_changes, - &result.markers, - ) - }); - let mut balances: Vec<(Address, U256, u64)> = result - .state_changes - .iter() - .map(|(a, acc)| (*a, acc.info.balance, acc.info.nonce)) - .collect(); - balances.sort_by_key(|(a, _, _)| *a); - ( - applied.compute_hash(store.as_ref()), - result.gas_used, - result.paid_fees, - result.tx_results.iter().map(|r| (r.success, r.gas_used)).collect::>(), - balances, - ) - }; + assert!(result.success, "Identity precompile should succeed"); + assert_eq!( + result.output, input_data, + "Identity precompile returns input unchanged" + ); - let one = run(1); - let two = run(2); - assert_eq!(one.3, two.3, "per-transaction results"); - assert_eq!(one.1, two.1, "gas used"); - assert_eq!(one.2, two.2, "paid fees"); - assert_eq!(one.4, two.4, "account balances"); - assert_eq!(one.0, two.0, "state root"); - assert_eq!(one.3.len(), 3, "every transaction ran exactly once"); + // Identity gas: 15 base + 3 * ceil(6/32) = 15 + 3 = 18 + // Intrinsic: 21000 + 6*16 = 21096 (all non-zero at Istanbul) + // Total: 21096 + 18 = 21114 + assert_eq!(result.gas_used, 21_114, "Identity: 21000 + 96 + 18 = 21114"); } - /// Two transactions from the same sender, placed in different parallel - /// sublists. This is the race RSKIP-144 exists to forbid, and it must be - /// caught when the sublists really do run apart. - /// - /// Each sublist runs against the state the block starts from, so both - /// transactions see nonce 0 and both succeed -- which is precisely the - /// problem, and why the rule is a condition on the block rather than - /// something sequential execution would have noticed. + /// Direct tx to SHA256 precompile (0x02) with empty input. + /// Ported from rskj PrecompiledContractTest.sha256Test1: + /// SHA256("") = e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 #[test] - fn a_block_whose_sublists_share_a_sender_is_caught_in_parallel() { - let store: Arc = Arc::new(MemoryTrieStore::new()); + fn test_sha256_precompile_direct_call() { + 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 sender = Address::repeat_byte(0x11); - let root = put_account( - &TrieNode::empty(), - store.as_ref(), - &sender, - 0, - U256::from(10_000_000_000_000u64), - ); - 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, - }); + let header = dummy_header(8_000_000); - let tx = |nonce: u64, to: u8| rustock_core::Transaction { - nonce, - gas_price: U256::from(1_000), - gas_limit: U256::from(30_000), - to: Bytes::copy_from_slice(Address::repeat_byte(to).as_slice()), - value: U256::from(1_000), + let sha256_addr = Address::new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]); + + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(0), + gas_limit: U256::from(100_000), + to: Bytes::copy_from_slice(sha256_addr.as_slice()), + value: U256::ZERO, input: Bytes::new(), v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, }; - // tx0 in sublist 0 and tx1 in sublist 1, both from `sender`, both - // with nonce 0 -- which is what each of them sees, each running - // against the state the block starts from. A miner would never build - // this; the validator still has to refuse it. - let transactions = vec![ - (tx(0, 0xA1), sender), - (tx(0, 0xA2), sender), - (tx(0, 0xA3), Address::repeat_byte(0x22)), - ]; - let edges = [1i16, 2]; - crate::collision::start( - &edges, - &[], - crate::collision::write_semantics(header.number), + let executor = RskExecutor::new( + RskHardforkConfig::all_active(33), + block_store, ); - let executor = RskExecutor::new(RskHardforkConfig::testnet(), block_store) - .with_execution_threads(2); - let _ = executor.execute_block(&header, &transactions, &root, store); - let collision = crate::collision::finish() - .expect("recording was started") - .first_collision() - .expect("both sublists write the sender's account cell"); - assert_eq!(collision.kind, crate::collision::CollisionKind::WriteWrite); - assert_eq!( - collision.key, - rustock_trie::account_key(&sender), - "the nonce is what connects them, and it lives in the account cell" + let result = executor + .execute_tx(&header, &tx, sender, &root, store) + .unwrap(); + + assert!(result.success, "SHA256 precompile should succeed"); + + let expected = hex_to_bytes( + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" ); + assert_eq!(result.output, expected, "SHA256('') matches rskj test vector"); + + // SHA256 gas: 60 base + 12 * ceil(0/32) = 60 + // Intrinsic: 21000 (no data) + // Total: 21060 + assert_eq!(result.gas_used, 21_060, "SHA256: 21000 + 60 = 21060"); } - /// The thread setting only decides *how*, never *whether*: a block with - /// no sublists runs as one however many threads are allowed. + /// Verify that SHA256 with non-empty input produces the correct hash. + /// Ported from rskj PrecompiledContractTest.sha256Test3: + /// SHA256(0x112233) = 49ee2bf93aac3b1fb4117e59095e07abe555c3383b38d608da37680a406096e8 #[test] - fn a_block_without_sublists_ignores_the_thread_count() { - let store: Arc = Arc::new(MemoryTrieStore::new()); + fn test_sha256_precompile_with_data() { + 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 sender = Address::repeat_byte(0x11); - let root = put_account( - &TrieNode::empty(), - store.as_ref(), - &sender, - 0, - U256::from(10_000_000_000_000u64), + + let header = dummy_header(8_000_000); + + let sha256_addr = Address::new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]); + let input_data = vec![0x11, 0x22, 0x33]; + + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(0), + gas_limit: U256::from(100_000), + to: Bytes::copy_from_slice(sha256_addr.as_slice()), + value: U256::ZERO, + input: Bytes::from(input_data), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + + let executor = RskExecutor::new( + RskHardforkConfig::all_active(33), + block_store, ); - let header = dummy_header(8_000_000); // version_fields: None - let transactions = vec![( - rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(1_000), - gas_limit: U256::from(30_000), - to: Bytes::copy_from_slice(Address::repeat_byte(0xA1).as_slice()), - value: U256::from(1_000), - input: Bytes::new(), - v: 0, - r: U256::ZERO, - s: U256::ZERO, - cached_rlp: None, - }, - sender, - )]; - for threads in [1usize, 2] { - let executor = RskExecutor::new(RskHardforkConfig::all_active(33), block_store.clone()) - .with_execution_threads(threads); - let result = executor - .execute_block(&header, &transactions, &root, store.clone()) - .expect("block executes"); - assert!( - result.applied_root.is_none(), - "a block with no sublists is one run, so the caller applies it ({threads} threads)" - ); - } + + let result = executor + .execute_tx(&header, &tx, sender, &root, store) + .unwrap(); + + assert!(result.success, "SHA256 precompile should succeed"); + + let expected = hex_to_bytes( + "49ee2bf93aac3b1fb4117e59095e07abe555c3383b38d608da37680a406096e8" + ); + assert_eq!(result.output, expected, "SHA256(0x112233) matches rskj test vector"); + + // SHA256 gas: 60 + 12*1 = 72 (3 bytes = 1 word) + // Intrinsic: 21000 + 3*16 = 21048 + // Total: 21048 + 72 = 21120 + assert_eq!(result.gas_used, 21_120, "SHA256: 21000 + 48 + 72 = 21120"); } - /// Verifies that the beneficiary (coinbase) receives gas fees in state changes. + /// Contract calls Identity precompile via STATICCALL and returns the result. + /// This tests the precompile-from-contract path (CALL opcode → precompile). #[test] - fn test_fees_routed_to_remasc() { - use crate::precompiles::REMASC_ADDR; - + fn test_identity_precompile_via_contract_call() { let store = Arc::new(MemoryTrieStore::new()); let root = TrieNode::empty(); let sender = Address::repeat_byte(0xAA); - let recipient = Address::repeat_byte(0xBB); - let beneficiary = Address::repeat_byte(0x01); 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 header = dummy_header(8_000_000); - header.beneficiary = beneficiary; + let header = dummy_header(8_000_000); + + // Deploy a contract whose runtime code: + // 1. Stores 0xDEADBEEF at memory[0:4] + // 2. STATICCALLs Identity (0x04) with that 4-byte input + // 3. RETURNs the 4-byte output + // + // Runtime bytecode: + // PUSH4 0xDEADBEEF ; 63 DEADBEEF + // PUSH1 0x00 ; 60 00 + // MSTORE ; 52 -> mem[28..32] = 0xDEADBEEF (right-aligned in 32 bytes) + // + // PUSH1 0x04 ; 60 04 retSize + // PUSH1 0x40 ; 60 40 retOffset + // PUSH1 0x04 ; 60 04 argsSize + // PUSH1 0x1c ; 60 1c argsOffset (28, where the 4 bytes are) + // PUSH1 0x04 ; 60 04 address (Identity) + // GAS ; 5A gas + // STATICCALL ; FA + // + // PUSH1 0x04 ; 60 04 size + // PUSH1 0x40 ; 60 40 offset + // RETURN ; F3 + let runtime: Vec = vec![ + 0x63, 0xDE, 0xAD, 0xBE, 0xEF, // PUSH4 0xDEADBEEF + 0x60, 0x00, // PUSH1 0 + 0x52, // MSTORE + 0x60, 0x04, // PUSH1 4 (retSize) + 0x60, 0x40, // PUSH1 64 (retOffset) + 0x60, 0x04, // PUSH1 4 (argsSize) + 0x60, 0x1c, // PUSH1 28 (argsOffset) + 0x60, 0x04, // PUSH1 4 (Identity address) + 0x5A, // GAS + 0xFA, // STATICCALL + 0x60, 0x04, // PUSH1 4 (return size) + 0x60, 0x40, // PUSH1 64 (return offset) + 0xF3, // RETURN + ]; + + let runtime_len = runtime.len() as u8; + let initcode_prefix: Vec = vec![ + 0x60, runtime_len, // PUSH1 runtime_len + 0x60, 0x0c, // PUSH1 12 (offset after initcode prefix) + 0x60, 0x00, // PUSH1 0 + 0x39, // CODECOPY + 0x60, runtime_len, // PUSH1 runtime_len + 0x60, 0x00, // PUSH1 0 + 0xF3, // RETURN + ]; + + let mut initcode = initcode_prefix; + initcode.extend_from_slice(&runtime); let tx = rustock_core::Transaction { nonce: 0, - gas_price: U256::from(100), - gas_limit: U256::from(21_000), - to: Bytes::copy_from_slice(recipient.as_slice()), - value: U256::from(1_000), - input: Bytes::new(), + gas_price: U256::from(0), + gas_limit: U256::from(500_000), + to: Bytes::new(), + value: U256::ZERO, + input: Bytes::from(initcode), v: 0, r: U256::ZERO, s: U256::ZERO, @@ -6481,42 +10179,73 @@ mod tests { let executor = RskExecutor::new( RskHardforkConfig::all_active(33), - block_store, + block_store.clone(), ); - let result = executor - .execute_block(&header, &[(tx, sender)], &root, store) + // Step 1: deploy the contract + let deploy_result = executor + .execute_block(&header, &[(tx, sender)], &root, store.clone()) .unwrap(); - assert!(result.tx_results[0].success); + assert!(deploy_result.tx_results[0].success, "deployment should succeed"); - // Fees are routed to REMASC, not the miner. gas_used * gas_price = 21000 * 100 - let remasc_state = result.state_changes.get(&REMASC_ADDR) - .expect("REMASC should appear in state changes"); - assert_eq!( - remasc_state.info.balance, - U256::from(2_100_000u64), - "REMASC receives 21000 * 100 = 2_100_000" - ); + // Find the created contract address from state changes + let contract_addr = deploy_result.state_changes.iter() + .find(|(addr, acct)| { + **addr != sender + && **addr != header.beneficiary + && acct.info.code.as_ref().is_some_and(|c| !c.is_empty()) + }) + .map(|(addr, _)| *addr) + .expect("should have created a contract"); - // The miner (header.beneficiary) should NOT receive fees directly - assert!( - !result.state_changes.contains_key(&beneficiary), - "miner should not receive fees directly; REMASC distributes later" + // Step 2: call the contract — it will STATICCALL Identity and return the result + let root2 = TrieNode::empty(); + let root2 = put_account(&root2, store.as_ref(), &sender, 1, one_rbtc); + + // Put the deployed code into the trie for the contract + 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}; + 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 call_tx = rustock_core::Transaction { + nonce: 1, + gas_price: U256::from(0), + gas_limit: U256::from(100_000), + to: Bytes::copy_from_slice(contract_addr.as_slice()), + value: U256::ZERO, + input: Bytes::new(), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + + let call_result = executor + .execute_tx(&header, &call_tx, sender, &root2, store) + .unwrap(); + + assert!(call_result.success, "contract call should succeed"); + assert_eq!( + call_result.output, + vec![0xDE, 0xAD, 0xBE, 0xEF], + "Identity precompile should return input unchanged via contract STATICCALL" ); } - // ----------------------------------------------------------------------- - // End-to-end precompile execution tests - // ----------------------------------------------------------------------- - - /// A USER tx to the REMASC precompile (0x01000008) is rskj's - /// RemascInvalidInvocationException ("Invoked Remasc outside last tx of - /// the block") — invisible for a direct tx: receipt SUCCESS with only - /// intrinsic gas (getGasForData is 0), no fee distribution, but the - /// sender is charged the FULL gas limit and REMASC receives it. + /// Direct tx to Environment precompile (0x01000011) with getCallStackDepth() selector. + /// Ported from rskj EnvironmentTest.getCallStackDepth: + /// selector = 0x80af2871, returns depth as uint32. + /// Direct tx call → depth = 1 (matching rskj's getCallDeep()==0 + 1). #[test] - fn test_remasc_precompile_direct_call() { + fn test_environment_precompile_direct_call() { let store = Arc::new(MemoryTrieStore::new()); let root = TrieNode::empty(); @@ -6528,17 +10257,18 @@ mod tests { BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap(), ); - let header = dummy_header(100); + let header = dummy_header(8_000_000); - let remasc_addr = crate::precompiles::REMASC_ADDR; + let env_addr = crate::precompiles::ENVIRONMENT_ADDR; + let selector = vec![0xe8, 0xce, 0x22, 0x74]; // keccak256("getCallStackDepth()")[0:4] let tx = rustock_core::Transaction { nonce: 0, - gas_price: U256::from(1_000), + gas_price: U256::from(0), gas_limit: U256::from(100_000), - to: Bytes::copy_from_slice(remasc_addr.as_slice()), + to: Bytes::copy_from_slice(env_addr.as_slice()), value: U256::ZERO, - input: Bytes::new(), + input: Bytes::from(selector), v: 0, r: U256::ZERO, s: U256::ZERO, @@ -6551,31 +10281,33 @@ mod tests { ); let result = executor - .execute_block(&header, &[(tx, sender)], &root, store) + .execute_tx(&header, &tx, sender, &root, store) .unwrap(); - assert!(result.tx_results[0].success, "receipt records SUCCESS"); - assert_eq!( - result.tx_results[0].gas_used, 21_000, - "REMASC costs 0 gas, receipt shows intrinsic only" - ); - // No fee distribution may run for a user tx. - let remasc_state = result.state_changes.get(&remasc_addr).unwrap(); - assert!(remasc_state.storage.is_empty(), "no REMASC storage writes"); - let full_fee = U256::from(100_000u64) * U256::from(1_000u64); - assert_eq!( - result.state_changes.get(&sender).unwrap().info.balance, - one_rbtc - full_fee, - "sender pays for the full gas limit" - ); - assert_eq!(remasc_state.info.balance, full_fee); - assert_eq!(result.paid_fees, full_fee); + assert!(result.success, "Environment direct call should succeed"); + + // Output: uint32 ABI-encoded as 32-byte big-endian + assert_eq!(result.output.len(), 32); + let depth = u32::from_be_bytes([ + result.output[28], + result.output[29], + result.output[30], + result.output[31], + ]); + assert_eq!(depth, 1, "direct tx call depth should be 1 (matching rskj)"); + + // Gas: 0 for Environment + intrinsic cost + // Intrinsic: 21000 base + 16*4 (4 non-zero selector bytes at Istanbul rate) = 21064 + assert_eq!(result.gas_used, 21_064, "Environment costs 0 gas, total = intrinsic only"); } - /// Direct tx to Bridge precompile (0x01000006). - /// Bridge stub charges 10,000 gas. + /// 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_bridge_precompile_direct_call() { + fn test_environment_precompile_unknown_selector() { let store = Arc::new(MemoryTrieStore::new()); let root = TrieNode::empty(); @@ -6589,43 +10321,40 @@ mod tests { let header = dummy_header(8_000_000); - let bridge_addr = crate::precompiles::BRIDGE_ADDR; + let env_addr = crate::precompiles::ENVIRONMENT_ADDR; + let bad_selector = vec![0xDE, 0xAD, 0xBE, 0xEF]; let tx = rustock_core::Transaction { nonce: 0, gas_price: U256::from(0), gas_limit: U256::from(100_000), - to: Bytes::copy_from_slice(bridge_addr.as_slice()), + to: Bytes::copy_from_slice(env_addr.as_slice()), value: U256::ZERO, - input: Bytes::new(), + input: Bytes::from(bad_selector), 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, "Bridge direct call should succeed"); + assert!(result.success, "an invisible exception records success"); + assert!(result.output.is_empty()); assert_eq!( - result.gas_used, - 21_000 + 23_000, - "Empty Bridge call dispatches releaseBtc (23k gas) on top of intrinsic" + result.gas_used, 21_064, + "21,000 and four nonzero bytes at 16" ); } - /// Direct tx to Identity precompile (0x04) with known input. - /// Ported from rskj PrecompiledContractTest.identityTest1: - /// input = 0x112233445566, output = 0x112233445566, gas = 18 + /// Environment called by the contract a transaction calls: rskj counts + /// that contract at depth 0, so the answer is 1 (`EnvironmentTest`). #[test] - fn test_identity_precompile_direct_call() { + fn test_environment_precompile_via_contract() { let store = Arc::new(MemoryTrieStore::new()); let root = TrieNode::empty(); @@ -6633,74 +10362,131 @@ 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); - let identity_addr = Address::new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4]); - let input_data: Vec = vec![0x11, 0x22, 0x33, 0x44, 0x55, 0x66]; + // Runtime code: + // Store selector 0xe8ce2274 at memory[0..4] + // STATICCALL Environment precompile (0x01000011) + // Return 32 bytes of output + // + // PUSH4 0xe8ce2274 ; 63 e8ce2274 + // PUSH1 0x00 ; 60 00 + // MSTORE ; 52 -> mem[28..32] = 0xe8ce2274 + // + // PUSH1 0x20 ; 60 20 retSize (32) + // PUSH1 0x40 ; 60 40 retOffset + // PUSH1 0x04 ; 60 04 argsSize (4) + // PUSH1 0x1c ; 60 1c argsOffset (28) + // PUSH4 0x01000011 ; 63 01000011 address (4 bytes) + // GAS ; 5A + // STATICCALL ; FA + // + // PUSH1 0x20 ; 60 20 size + // 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 + ]; + + let runtime_len = runtime.len() as u8; + let mut initcode: Vec = vec![ + 0x60, + runtime_len, + 0x60, + 0x0c, + 0x60, + 0x00, + 0x39, + 0x60, + runtime_len, + 0x60, + 0x00, + 0xF3, + ]; + initcode.extend_from_slice(&runtime); let tx = rustock_core::Transaction { nonce: 0, gas_price: U256::from(0), - gas_limit: U256::from(100_000), - to: Bytes::copy_from_slice(identity_addr.as_slice()), + gas_limit: U256::from(500_000), + to: Bytes::new(), value: U256::ZERO, - input: Bytes::from(input_data.clone()), + input: Bytes::from(initcode), 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.clone()); - let result = executor - .execute_tx(&header, &tx, sender, &root, store) + // Deploy + let deploy_result = executor + .execute_block(&header, &[(tx, sender)], &root, store.clone()) .unwrap(); - assert!(result.success, "Identity precompile should succeed"); - assert_eq!( - result.output, input_data, - "Identity precompile returns input unchanged" + assert!( + deploy_result.tx_results[0].success, + "deployment should succeed" ); - // Identity gas: 15 base + 3 * ceil(6/32) = 15 + 3 = 18 - // Intrinsic: 21000 + 6*16 = 21096 (all non-zero at Istanbul) - // Total: 21096 + 18 = 21114 - assert_eq!(result.gas_used, 21_114, "Identity: 21000 + 96 + 18 = 21114"); - } - - /// Direct tx to SHA256 precompile (0x02) with empty input. - /// Ported from rskj PrecompiledContractTest.sha256Test1: - /// SHA256("") = e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 - #[test] - fn test_sha256_precompile_direct_call() { - let store = Arc::new(MemoryTrieStore::new()); - let root = TrieNode::empty(); + let contract_addr = deploy_result + .state_changes + .iter() + .find(|(addr, acct)| { + **addr != sender + && **addr != header.beneficiary + && acct.info.code.as_ref().is_some_and(|c| !c.is_empty()) + }) + .map(|(addr, _)| *addr) + .expect("should have created a contract"); - 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); + // Set up state for calling the contract + let root2 = TrieNode::empty(); + let root2 = put_account(&root2, store.as_ref(), &sender, 1, one_rbtc); - let block_store = Arc::new( - BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap(), - ); + let code_bytes = deploy_result + .state_changes + .get(&contract_addr) + .unwrap() + .info + .code + .as_ref() + .unwrap() + .bytes_slice() + .to_vec(); - let header = dummy_header(8_000_000); + 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 sha256_addr = Address::new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]); + 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 tx = rustock_core::Transaction { - nonce: 0, + let call_tx = rustock_core::Transaction { + nonce: 1, gas_price: U256::from(0), gas_limit: U256::from(100_000), - to: Bytes::copy_from_slice(sha256_addr.as_slice()), + to: Bytes::copy_from_slice(contract_addr.as_slice()), value: U256::ZERO, input: Bytes::new(), v: 0, @@ -6709,479 +10495,2051 @@ mod tests { cached_rlp: None, }; - let executor = RskExecutor::new( - RskHardforkConfig::all_active(33), - block_store, - ); - - let result = executor - .execute_tx(&header, &tx, sender, &root, store) + let call_result = executor + .execute_tx(&header, &call_tx, sender, &root2, store) .unwrap(); - assert!(result.success, "SHA256 precompile should succeed"); - - let expected = hex_to_bytes( - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + assert!(call_result.success, "contract call should succeed"); + assert_eq!( + call_result.output.len(), + 32, + "should return 32-byte ABI-encoded uint32" ); - assert_eq!(result.output, expected, "SHA256('') matches rskj test vector"); - // SHA256 gas: 60 base + 12 * ceil(0/32) = 60 - // Intrinsic: 21000 (no data) - // Total: 21060 - assert_eq!(result.gas_used, 21_060, "SHA256: 21000 + 60 = 21060"); + let depth = u32::from_be_bytes([ + call_result.output[28], + call_result.output[29], + call_result.output[30], + call_result.output[31], + ]); + + assert_eq!( + depth, 1, + "the contract the transaction calls is rskj's depth 0" + ); } - /// Verify that SHA256 with non-empty input produces the correct hash. - /// Ported from rskj PrecompiledContractTest.sha256Test3: - /// SHA256(0x112233) = 49ee2bf93aac3b1fb4117e59095e07abe555c3383b38d608da37680a406096e8 + /// 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 test_sha256_precompile_with_data() { - let store = Arc::new(MemoryTrieStore::new()); - let root = TrieNode::empty(); + 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)); + } - 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); + /// 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::() + } - let block_store = Arc::new( - BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap(), - ); + /// The extended key of rskj's `ExtractPublicKeyFromExtendedPublicKeyTest`. + const XPUB: &str = "xpub661MyMwAqRbcFMGNG2YcHvj3x63bAZN9U5cKikaiQ4zu2D1cvpnZYyXNR9nH62sGp4RR39Ui7SVQSq1PY4JbPuEuu5prVJJC3d5Pogft712"; - let header = dummy_header(8_000_000); + /// 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 + } - let sha256_addr = Address::new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]); - let input_data = vec![0x11, 0x22, 0x33]; + /// 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 { + 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)); + 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(0), - gas_limit: U256::from(100_000), - to: Bytes::copy_from_slice(sha256_addr.as_slice()), - value: U256::ZERO, - input: Bytes::from(input_data), + 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()); + let executor = RskExecutor::new(cfg, block_store); + executor.execute_block(&dummy_header(height), &[(tx, sender)], &root, store) + } - let executor = RskExecutor::new( - RskHardforkConfig::all_active(33), - block_store, - ); - - let result = executor - .execute_tx(&header, &tx, sender, &root, store) - .unwrap(); + fn fee(gas: u64) -> U256 { + U256::from(gas) * U256::from(60_000_000u64) + } - assert!(result.success, "SHA256 precompile should succeed"); + /// 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 expected = hex_to_bytes( - "49ee2bf93aac3b1fb4117e59095e07abe555c3383b38d608da37680a406096e8" + 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!( + 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" ); - assert_eq!(result.output, expected, "SHA256(0x112233) matches rskj test vector"); - // SHA256 gas: 60 + 12*1 = 72 (3 bytes = 1 word) - // Intrinsic: 21000 + 3*16 = 21048 - // Total: 21048 + 72 = 21120 - assert_eq!(result.gas_used, 21_120, "SHA256: 21000 + 48 + 72 = 21120"); + 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)); } - /// Contract calls Identity precompile via STATICCALL and returns the result. - /// This tests the precompile-from-contract path (CALL opcode → precompile). + /// 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 test_identity_precompile_via_contract_call() { - let store = Arc::new(MemoryTrieStore::new()); - let root = TrieNode::empty(); + 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); + } + /// rskj `TransactionExecutor.call`: before RSKIP136 (bahamas, mainnet + /// #3,397) what a transaction's own call to a precompile asks for + /// (`getGasForData`) is held to the whole gas limit (`enoughGas`), and the + /// transaction uses it on top of its intrinsic cost, past the limit if it + /// comes to that. From RSKIP136 the two together are held to the limit. + /// The Bridge's calls ran this way already; ECRECOVER asks for 3,000 + /// whatever it is given. + #[test] + fn before_rskip136_a_direct_precompile_call_is_held_to_its_limit_by_its_price_alone() { + let ecrecover = Address::with_last_byte(1); 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 value = U256::from(7); + let run = |height| { + execute_alone( + RskHardforkConfig::mainnet(), + height, + ecrecover, + value, + 21_000, + &[], + &[], + ) + }; - let block_store = Arc::new( - BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap(), + let result = run(3_396); + let receipt = &result.tx_results[0]; + assert!(receipt.success); + assert_eq!(receipt.gas_used, 24_000, "3,000 on top of 21,000"); + assert_eq!(result.paid_fees, fee(24_000)); + assert_eq!( + result.state_changes.get(&sender).unwrap().info.balance, + one_rbtc - value - fee(24_000) + ); + assert_eq!( + result.state_changes.get(&ecrecover).unwrap().info.balance, + value + ); + assert!( + result.markers.precompiles.contains(&ecrecover), + "setupContract" ); - let header = dummy_header(8_000_000); + let result = run(3_397); + let receipt = &result.tx_results[0]; + assert!( + !receipt.success, + "from RSKIP136, 21,000 can't pay for 24,000" + ); + assert_eq!(receipt.gas_used, 21_000); + assert_eq!(result.paid_fees, fee(21_000)); + assert!(result + .state_changes + .get(&ecrecover) + .is_none_or(|account| account.info.balance.is_zero())); + assert!(!result.markers.precompiles.contains(&ecrecover)); + } - // Deploy a contract whose runtime code: - // 1. Stores 0xDEADBEEF at memory[0:4] - // 2. STATICCALLs Identity (0x04) with that 4-byte input - // 3. RETURNs the 4-byte output - // - // Runtime bytecode: - // PUSH4 0xDEADBEEF ; 63 DEADBEEF - // PUSH1 0x00 ; 60 00 - // MSTORE ; 52 -> mem[28..32] = 0xDEADBEEF (right-aligned in 32 bytes) - // - // PUSH1 0x04 ; 60 04 retSize - // PUSH1 0x40 ; 60 40 retOffset - // PUSH1 0x04 ; 60 04 argsSize - // PUSH1 0x1c ; 60 1c argsOffset (28, where the 4 bytes are) - // PUSH1 0x04 ; 60 04 address (Identity) - // GAS ; 5A gas - // STATICCALL ; FA - // - // PUSH1 0x04 ; 60 04 size - // PUSH1 0x40 ; 60 40 offset - // RETURN ; F3 - let runtime: Vec = vec![ - 0x63, 0xDE, 0xAD, 0xBE, 0xEF, // PUSH4 0xDEADBEEF - 0x60, 0x00, // PUSH1 0 - 0x52, // MSTORE - 0x60, 0x04, // PUSH1 4 (retSize) - 0x60, 0x40, // PUSH1 64 (retOffset) - 0x60, 0x04, // PUSH1 4 (argsSize) - 0x60, 0x1c, // PUSH1 28 (argsOffset) - 0x60, 0x04, // PUSH1 4 (Identity address) - 0x5A, // GAS - 0xFA, // STATICCALL - 0x60, 0x04, // PUSH1 4 (return size) - 0x60, 0x40, // PUSH1 64 (return offset) - 0xF3, // RETURN - ]; + /// The same for a native contract, with RSKIP136 moved off bahamas as a + /// `consensusRules` entry moves it: HDWalletUtils asks for 11,300, which + /// before RSKIP136 a transaction carrying only its intrinsic gas pays. A + /// call that throws there is rskj's invisible exception all the same, and + /// its sender pays for its whole limit. The blocks run as their sublists, + /// so each fee reaches REMASC once, with the block's others. + #[test] + fn before_rskip136_a_direct_native_contract_call_is_held_to_its_limit_by_its_price_alone() { + let hd_wallet = crate::precompiles::HD_WALLET_UTILS_ADDR; + let remasc = crate::precompiles::REMASC_ADDR; + let sender = Address::repeat_byte(0xAA); + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let value = U256::from(7); + let (cfg, _) = RskHardforkConfig::all_active_parallel(33) + .with_rskj_overrides(&[], &[("rskip136".to_string(), Some(9_000_000))]) + .unwrap(); + assert!(cfg.runs_sublists(8_000_000)); + let run = |height, input: &[u8]| { + let limit = intrinsic_gas(input, 16); + ( + limit, + execute_alone(cfg.clone(), height, hd_wallet, value, limit, input, &[]), + ) + }; + let remasc_holds = |result: &BlockExecutionResult| { + result + .state_changes + .get(&remasc) + .map_or(U256::ZERO, |account| account.info.balance) + }; - let runtime_len = runtime.len() as u8; - let initcode_prefix: Vec = vec![ - 0x60, runtime_len, // PUSH1 runtime_len - 0x60, 0x0c, // PUSH1 12 (offset after initcode prefix) - 0x60, 0x00, // PUSH1 0 - 0x39, // CODECOPY - 0x60, runtime_len, // PUSH1 runtime_len - 0x60, 0x00, // PUSH1 0 - 0xF3, // RETURN - ]; + let reads = extract_public_key_call(XPUB.as_bytes()); + let (limit, result) = run(8_000_000, &reads); + let receipt = &result.tx_results[0]; + assert!(receipt.success); + assert!(!receipt.output.is_empty()); + assert_eq!(receipt.gas_used, limit + 11_300, "past its limit"); + assert_eq!(result.paid_fees, fee(limit + 11_300)); + assert_eq!( + remasc_holds(&result), + fee(limit + 11_300), + "REMASC is paid once" + ); + assert_eq!( + result.state_changes.get(&sender).unwrap().info.balance, + one_rbtc - value - fee(limit + 11_300) + ); + assert_eq!( + result.state_changes.get(&hd_wallet).unwrap().info.balance, + value + ); + assert!(result.markers.precompiles.contains(&hd_wallet)); + + let throws = extract_public_key_call(b"this-is-not-an-xpub"); + let (limit, result) = run(8_000_000, &throws); + let receipt = &result.tx_results[0]; + assert!(receipt.success, "the receipt records success"); + assert!(receipt.output.is_empty()); + assert_eq!(receipt.gas_used, limit + 11_300); + assert_eq!( + result.paid_fees, + fee(limit), + "the sender pays for its whole limit" + ); + assert_eq!(remasc_holds(&result), fee(limit)); + assert_eq!( + result.state_changes.get(&sender).unwrap().info.balance, + one_rbtc - fee(limit), + "and keeps the value" + ); + assert!(result + .state_changes + .get(&hd_wallet) + .is_none_or(|account| !account.is_touched())); + assert!(!result.markers.precompiles.contains(&hd_wallet)); + + for input in [reads, throws] { + let (limit, result) = run(9_000_000, &input); + let receipt = &result.tx_results[0]; + assert!( + !receipt.success, + "from RSKIP136 its limit can't pay for 11,300 more" + ); + assert_eq!(receipt.gas_used, limit); + assert_eq!(result.paid_fees, fee(limit)); + assert_eq!(remasc_holds(&result), fee(limit)); + } + } - let mut initcode = initcode_prefix; - initcode.extend_from_slice(&runtime); + /// Where a block runs as its sublists, each fee reaches REMASC with the + /// block's others, just before the REMASC transaction or after the last + /// one (`postponeFeePayment`). A transaction's own call to the Bridge + /// before RSKIP136 paid its fee to REMASC straight away as well, and + /// REMASC had it twice. + #[test] + fn before_rskip136_a_direct_bridge_call_pays_remasc_once_where_fees_wait() { + let (cfg, _) = RskHardforkConfig::all_active_parallel(33) + .with_rskj_overrides(&[], &[("rskip136".to_string(), Some(9_000_000))]) + .unwrap(); + let input = crate::abi::selector("getBtcBlockchainBestChainHeight()").to_vec(); + let result = execute_alone( + cfg, + 8_000_000, + crate::precompiles::BRIDGE_ADDR, + U256::ZERO, + 1_000_000, + &input, + &[], + ); + let receipt = &result.tx_results[0]; + assert!(receipt.success); + assert_eq!(result.paid_fees, fee(receipt.gas_used)); + assert_eq!( + result + .state_changes + .get(&crate::precompiles::REMASC_ADDR) + .unwrap() + .info + .balance, + fee(receipt.gas_used) + ); + } + /// Before RSKIP136 a transaction's own call to a precompile can use more + /// gas than its limit, and rskj takes the excess from its sender whatever + /// the sender holds: `TransactionExecutor.finalization` adds a negative + /// leftover, and the balance goes below zero, which rskj stores signed + /// (`RLP.encodeSignedCoinNonNullZero`). This node's balances can't go + /// below zero, so it refuses such a block rather than reach another state. + #[test] + fn before_rskip136_a_sender_who_cannot_cover_the_excess_fails_the_block() { + let ecrecover = Address::with_last_byte(1); + let sender = Address::repeat_byte(0xAA); + let price = U256::from(60_000_000u64); + let value = U256::from(7); + let store = Arc::new(MemoryTrieStore::new()); + // What rskj asks a sender to hold before it runs the transaction: + // the value and the whole limit (`TransactionExecutor.init`). + let holds = value + U256::from(21_000) * price; + let root = put_account(&TrieNode::empty(), store.as_ref(), &sender, 0, holds); let tx = rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(0), - gas_limit: U256::from(500_000), - to: Bytes::new(), - value: U256::ZERO, - input: Bytes::from(initcode), - v: 0, - r: U256::ZERO, - s: U256::ZERO, - cached_rlp: None, + gas_price: price, + gas_limit: U256::from(21_000), + ..plain_tx(0, &ecrecover, value) }; - - let executor = RskExecutor::new( - RskHardforkConfig::all_active(33), - block_store.clone(), + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + let before = executor.execute_block( + &dummy_header(3_396), + &[(tx.clone(), sender)], + &root, + store.clone(), ); + match before { + Err(error) => assert!(error.to_string().contains("below zero"), "{error}"), + Ok(_) => panic!("the block executes"), + } + let after = executor + .execute_block(&dummy_header(3_397), &[(tx, sender)], &root, store) + .expect("from RSKIP136 the call fails within its limit"); + assert!(!after.tx_results[0].success); + } - // Step 1: deploy the contract - let deploy_result = executor - .execute_block(&header, &[(tx, sender)], &root, store.clone()) - .unwrap(); + /// rskj `TransactionExecutor.init` holds a transaction to its sender's + /// next nonce, a limit that pays its intrinsic cost, and a balance that + /// covers its value and whole limit, before anything runs. One that fails + /// makes its block invalid, and the miner drops it for the pool to forget. + /// A transaction's own call that rskj runs outside its VM, here before + /// RSKIP136, is held to the same. Mining also leaves one whose sender + /// couldn't pay for what it comes to past its limit, keeping it for later. + #[test] + fn a_direct_precompile_call_is_held_to_rskjs_checks_before_it_runs() { + let ecrecover = Address::with_last_byte(1); + let sender = Address::repeat_byte(0xAA); + let price = U256::from(60_000_000u64); + let value = U256::from(7); + let store = Arc::new(MemoryTrieStore::new()); + // The value and the 24,000 gas a 21,000 limit comes to. + let holds = value + U256::from(24_000) * price; + let root = put_account(&TrieNode::empty(), store.as_ref(), &sender, 0, holds); + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + let header = dummy_header(3_396); + let call = |nonce: u64, gas_limit: u64, value: U256| rustock_core::Transaction { + gas_price: price, + gas_limit: U256::from(gas_limit), + ..plain_tx(nonce, &ecrecover, value) + }; + let too_much = value + U256::from(3_000) * price + U256::from(1); + for (tx, refused) in [ + (call(1, 21_000, value), "Invalid nonce"), + (call(0, 20_999, value), "Not enough gas"), + (call(0, 21_000, too_much), "Not enough cash"), + ] { + match executor.execute_block(&header, &[(tx.clone(), sender)], &root, store.clone()) { + Err(error) => assert!(error.to_string().contains(refused), "{error}"), + Ok(_) => panic!("{refused}: the block executes"), + } + let (result, drops) = executor + .execute_block_for_mining(&header, &[(tx, sender)], &root, store.clone()) + .expect("mining"); + assert!(result.tx_results.is_empty(), "{refused}"); + assert_eq!(drops.invalid, vec![0], "{refused}"); + } - assert!(deploy_result.tx_results[0].success, "deployment should succeed"); + let (result, drops) = executor + .execute_block_for_mining( + &header, + &[(call(0, 21_000, value), sender)], + &root, + store.clone(), + ) + .expect("mining"); + assert!(drops.invalid.is_empty() && drops.unfitting.is_empty()); + assert_eq!(result.tx_results[0].gas_used, 24_000); + let short = put_account( + &TrieNode::empty(), + store.as_ref(), + &sender, + 0, + holds - U256::from(1), + ); + let (result, drops) = executor + .execute_block_for_mining(&header, &[(call(0, 21_000, value), sender)], &short, store) + .expect("mining"); + assert!(result.tx_results.is_empty()); + assert!(drops.invalid.is_empty()); + assert_eq!(drops.unfitting, vec![0], "kept for later"); + } - // Find the created contract address from state changes - let contract_addr = deploy_result.state_changes.iter() - .find(|(addr, acct)| { - **addr != sender - && **addr != header.beneficiary - && acct.info.code.as_ref().is_some_and(|c| !c.is_empty()) - }) - .map(|(addr, _)| *addr) - .expect("should have created a contract"); + /// 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)); + } - // Step 2: call the contract — it will STATICCALL Identity and return the result - let root2 = TrieNode::empty(); - let root2 = put_account(&root2, store.as_ref(), &sender, 1, one_rbtc); + /// 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())); + } - // Put the deployed code into the trie for the contract - let code_bytes = deploy_result.state_changes.get(&contract_addr) - .unwrap().info.code.as_ref().unwrap().bytes_slice().to_vec(); + /// 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 + }; - use rustock_trie::{TrieKeySlice, code_key}; - let ckey = code_key(&contract_addr); - let root2 = root2.put(&TrieKeySlice::from_key(&ckey), &code_bytes, store.as_ref()); + 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 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 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}" + ); + } + } - let call_tx = rustock_core::Transaction { - nonce: 1, - gas_price: U256::from(0), - gas_limit: U256::from(100_000), - to: Bytes::copy_from_slice(contract_addr.as_slice()), - value: U256::ZERO, - input: Bytes::new(), - v: 0, - r: U256::ZERO, - s: U256::ZERO, - cached_rlp: None, + /// 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), + ) }; - let call_result = executor - .execute_tx(&header, &call_tx, sender, &root2, store) - .unwrap(); + // 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 + ) + ); - assert!(call_result.success, "contract call should succeed"); + // 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!( - call_result.output, - vec![0xDE, 0xAD, 0xBE, 0xEF], - "Identity precompile should return input unchanged via contract STATICCALL" + run(lovell700, negative, false), + (sender, vec![sent, reason], plenty, U256::ZERO, U256::ZERO) ); } - /// Direct tx to Environment precompile (0x01000011) with getCallStackDepth() selector. - /// Ported from rskj EnvironmentTest.getCallStackDepth: - /// selector = 0x80af2871, returns depth as uint32. - /// Direct tx call → depth = 1 (matching rskj's getCallDeep()==0 + 1). + /// 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 test_environment_precompile_direct_call() { - let store = Arc::new(MemoryTrieStore::new()); - let root = TrieNode::empty(); + 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)); + } - let sender = Address::repeat_byte(0xAA); + /// **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.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 root = put_account(&root, store.as_ref(), &sender, 0, one_rbtc); + 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); + } + } - let block_store = Arc::new( - BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap(), + 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:?}" ); + } - let header = dummy_header(8_000_000); - - let env_addr = crate::precompiles::ENVIRONMENT_ADDR; - let selector = vec![0xe8, 0xce, 0x22, 0x74]; // keccak256("getCallStackDepth()")[0:4] - - let tx = rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(0), - gas_limit: U256::from(100_000), - to: Bytes::copy_from_slice(env_addr.as_slice()), + /// 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::from(selector), + 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::all_active(33), - block_store, + RskHardforkConfig::mainnet(), + Arc::new(BlockStore::open(dir.path()).unwrap()), ); - - let result = executor - .execute_tx(&header, &tx, sender, &root, store) + let mut block = executor + .execute_block( + &dummy_header(height), + &[(bare_call(0, contract), sender)], + &root, + store, + ) .unwrap(); + block.tx_results.remove(0) + } - assert!(result.success, "Environment direct call should succeed"); - - // Output: uint32 ABI-encoded as 32-byte big-endian - assert_eq!(result.output.len(), 32); - let depth = u32::from_be_bytes([ - result.output[28], - result.output[29], - result.output[30], - result.output[31], - ]); - assert_eq!(depth, 1, "direct tx call depth should be 1 (matching rskj)"); - - // Gas: 0 for Environment + intrinsic cost - // Intrinsic: 21000 base + 16*4 (4 non-zero selector bytes at Istanbul rate) = 21064 - assert_eq!(result.gas_used, 21_064, "Environment costs 0 gas, total = intrinsic only"); + /// 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); + } } - /// Environment precompile with unknown selector should fail. + /// rskj's code header (`Program.processAndSkipCodeHeader`, which + /// `Program.precompile` runs for every program it builds, on every + /// network from genesis, under no rule): code of four bytes or more that + /// starts with HEADER (0xFC) runs from byte 4 + code[3], the header's + /// length read unsigned, and its jump destinations are found from there, + /// so none lies in the header. Code whose header reaches its end fails + /// with all its gas, as rskj fails reading past it. The cases of rskj's + /// `VMTest.testVersioning_1` and `_2` and `testScriptVersion0` to `3`, + /// each made to return what it computes, then the jump destinations, a + /// contract's call to such code, and a creation whose init code has a + /// header. #[test] - fn test_environment_precompile_unknown_selector() { + fn a_code_header_is_skipped_as_rskj_skips_it() { + // Any height: the header is no upgrade's. + const HEIGHT: u64 = 1; + let word = |value: u8| { + let mut word = vec![0u8; 32]; + word[31] = value; + word + }; + // `testVersioning_1`'s code after its header leaves 0x12 on the + // stack (0x1234 stored at 0x20, the word at 0x1F loaded), returned. + let tail: &[u8] = &[ + 0x61, 0x12, 0x34, 0x60, 0x20, 0x52, 0x60, 0x1f, 0x51, // VMTest's + 0x60, 0x00, 0x52, 0x60, 0x20, 0x60, 0x00, 0xf3, // MSTORE at 0, RETURN 32 + ]; + let plain = run_contract(HEIGHT, tail); + assert!(plain.success); + assert_eq!(plain.output, word(0x12)); + // testVersioning_1, and testVersioning_2's header of 0x80 = 128 + // bytes, the length read unsigned. The header costs nothing. + let mut long = vec![0xfc, 0x81, 0x01, 0x80]; + long.extend([0u8; 128]); + for header in [&[0xfc, 0x01, 0x01, 0x00][..], &long[..]] { + let ran = run_contract(HEIGHT, &[header, tail].concat()); + assert!(ran.success, "a header of {} bytes", header.len()); + assert_eq!( + ran.output, + plain.output, + "a header of {} bytes", + header.len() + ); + assert_eq!( + ran.gas_used, + plain.gas_used, + "a header of {} bytes", + header.len() + ); + } + // testScriptVersion3: ten more bytes of header, then STOP. + let mut stops = vec![0xfc, 0x01, 0x01, 0x0a]; + stops.extend(1..=10u8); + stops.push(0x00); + let ran = run_contract(HEIGHT, &stops); + assert!(ran.success); + assert_eq!(ran.gas_used, run_contract(HEIGHT, &[0x00]).gas_used); + // Each of these fails with all its gas: HEADER with no header behind + // it (testScriptVersion0), HEADER after a header (testScriptVersion1 + // and 2), and a header that reaches the code's end or passes it. + for code in [ + &[0xfc][..], + &[0xfc, 0x00, 0x00, 0x00, 0xfc][..], + &[0xfc, 0x01, 0x01, 0x00, 0xfc][..], + &[0xfc, 0x00, 0x00, 0x00][..], + &[0xfc, 0x00, 0x00, 0x05, 0x00, 0x00][..], + ] { + let ran = run_contract(HEIGHT, code); + let hex = alloy_primitives::hex::encode(code); + assert!(!ran.success, "{hex}"); + assert_eq!(ran.gas_used, 1_000_000, "{hex}"); + } + // Jump destinations are found from the header's end: the PUSH32 that + // ends this header of one more byte doesn't hide the JUMPDEST at 8, + // as it would from byte 0 ... + let header: &[u8] = &[0xfc, 0x00, 0x00, 0x01, 0x7f]; + let jump_to_8: &[u8] = &[0x60, 0x08, 0x56]; + // At 8: JUMPDEST, then 42 returned. + let at_8: &[u8] = &[ + 0x5b, 0x60, 0x2a, 0x60, 0x00, 0x52, 0x60, 0x20, 0x60, 0x00, 0xf3, + ]; + let ran = run_contract(HEIGHT, &[header, jump_to_8, at_8].concat()); + assert!(ran.success); + assert_eq!(ran.output, word(0x2a)); + // ... and a JUMPDEST in the header is none: this header's two more + // bytes are a JUMPDEST, at 4, and a STOP. + let header: &[u8] = &[0xfc, 0x00, 0x00, 0x02, 0x5b, 0x00]; + let jump_to_4: &[u8] = &[0x60, 0x04, 0x56]; + let ran = run_contract(HEIGHT, &[header, jump_to_4].concat()); + assert!(!ran.success); + assert_eq!(ran.gas_used, 1_000_000); + + // A contract's call to code with a header runs it the same way: the + // caller is given 0x12 and a success. It CALLs with 100,000 gas, + // no value and no data, for 32 bytes at 0, stores the CALL's result + // at 0x20, and returns the two words. + let sender = Address::repeat_byte(0xC1); + let caller = Address::repeat_byte(0xC2); + let callee = Address::repeat_byte(0xC3); + let mut calls: Vec = vec![ + 0x60, 0x20, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x73, + ]; + calls.extend_from_slice(callee.as_slice()); + calls.extend([ + 0x62, 0x01, 0x86, 0xa0, 0xf1, 0x60, 0x20, 0x52, 0x60, 0x40, 0x60, 0x00, 0xf3, + ]); 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 header = dummy_header(8_000_000); - - let env_addr = crate::precompiles::ENVIRONMENT_ADDR; - let bad_selector = vec![0xDE, 0xAD, 0xBE, 0xEF]; - - let tx = rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(0), - gas_limit: U256::from(100_000), - to: Bytes::copy_from_slice(env_addr.as_slice()), - value: U256::ZERO, - input: Bytes::from(bad_selector), - v: 0, - r: U256::ZERO, - s: U256::ZERO, - cached_rlp: None, + let root = put_account(&TrieNode::empty(), store.as_ref(), &sender, 0, one_rbtc); + let root = put_account_with_code(&root, store.as_ref(), &caller, 1, U256::ZERO, &calls); + let headed: Vec = [&[0xfc, 0x01, 0x01, 0x00][..], tail].concat(); + let root = put_account_with_code(&root, store.as_ref(), &callee, 1, U256::ZERO, &headed); + // A creation whose init code has a header deploys what the same + // init code deploys without one. The header's four bytes are data + // the transaction pays for, 68 gas for the 0xFC and 4 for each zero + // before RSKIP400, and they cost nothing more. + let init: &[u8] = &[0x60, 0x2a, 0x60, 0x00, 0x52, 0x60, 0x20, 0x60, 0x00, 0xf3]; + let create = |nonce: u64, input: Vec| rustock_core::Transaction { + to: Bytes::new(), + input: Bytes::from(input), + ..bare_call(nonce, Address::ZERO) }; - + let dir = tempfile::tempdir().unwrap(); let executor = RskExecutor::new( - RskHardforkConfig::all_active(33), - block_store, + RskHardforkConfig::mainnet(), + Arc::new(BlockStore::open(dir.path()).unwrap()), ); - - let result = executor - .execute_tx(&header, &tx, sender, &root, store) + let block = executor + .execute_block( + &dummy_header(HEIGHT), + &[ + (bare_call(0, caller), sender), + (create(1, init.to_vec()), sender), + ( + create(2, [&[0xfc, 0x00, 0x00, 0x00][..], init].concat()), + sender, + ), + ], + &root, + store, + ) .unwrap(); + let [called, created, created_with_header] = &block.tx_results[..] else { + panic!("{} results", block.tx_results.len()); + }; + assert!(called.success); + assert_eq!(called.output, [word(0x12), word(1)].concat()); + assert!(created.success && created.created_address.is_some()); + assert!(created_with_header.success && created_with_header.created_address.is_some()); + assert_eq!(created_with_header.gas_used, created.gas_used + 68 + 3 * 4); + } - assert!(!result.success, "unknown selector should fail"); + /// 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); } - /// Environment precompile called via contract STATICCALL should return - /// depth > 1 (one extra level for the contract frame). + /// 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 test_environment_precompile_via_contract() { + 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 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 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"); + } - let header = dummy_header(8_000_000); + /// 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" + ); - // Runtime code: - // Store selector 0xe8ce2274 at memory[0..4] - // STATICCALL Environment precompile (0x01000011) - // Return 32 bytes of output - // - // PUSH4 0xe8ce2274 ; 63 e8ce2274 - // PUSH1 0x00 ; 60 00 - // MSTORE ; 52 -> mem[28..32] = 0xe8ce2274 - // - // PUSH1 0x20 ; 60 20 retSize (32) - // PUSH1 0x40 ; 60 40 retOffset - // PUSH1 0x04 ; 60 04 argsSize (4) - // PUSH1 0x1c ; 60 1c argsOffset (28) - // PUSH4 0x01000011 ; 63 01000011 address (4 bytes) - // GAS ; 5A - // STATICCALL ; FA - // - // PUSH1 0x20 ; 60 20 size - // 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 - ]; + 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" + ); + } - let runtime_len = runtime.len() as u8; - let mut initcode: Vec = vec![ - 0x60, runtime_len, - 0x60, 0x0c, - 0x60, 0x00, - 0x39, - 0x60, runtime_len, - 0x60, 0x00, - 0xF3, + /// 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 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 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, + 0x00, // no return, args 0..n, no value + 0x73, // PUSH20 ]; - initcode.extend_from_slice(&runtime); - - let tx = rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(0), - gas_limit: U256::from(500_000), - to: Bytes::new(), - value: U256::ZERO, - input: Bytes::from(initcode), - v: 0, - r: U256::ZERO, - s: U256::ZERO, - cached_rlp: None, + code.extend_from_slice(hd_wallet.as_slice()); + code.extend_from_slice(&[ + 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)]; + 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 executor = RskExecutor::new( - RskHardforkConfig::all_active(33), - block_store.clone(), + 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 ); - // Deploy - let deploy_result = executor - .execute_block(&header, &[(tx, sender)], &root, store.clone()) - .unwrap(); + 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" + ); - assert!(deploy_result.tx_results[0].success, "deployment should succeed"); + let before = call(2_000_000, &bad); + assert!(!before.success, "before RSKIP197 the contract fails"); + assert_eq!(before.gas_used, 1_000_000, "with all its gas"); + } - let contract_addr = deploy_result.state_changes.iter() - .find(|(addr, acct)| { - **addr != sender - && **addr != header.beneficiary - && acct.info.code.as_ref().is_some_and(|c| !c.is_empty()) - }) - .map(|(addr, _)| *addr) - .expect("should have created a contract"); + /// 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() + } - // Set up state for calling the contract - let root2 = TrieNode::empty(); - let root2 = put_account(&root2, store.as_ref(), &sender, 1, one_rbtc); + /// 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 + } - let code_bytes = deploy_result.state_changes.get(&contract_addr) - .unwrap().info.code.as_ref().unwrap().bytes_slice().to_vec(); + /// 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) + }; - use rustock_trie::{TrieKeySlice, code_key, AccountState}; - let ckey = code_key(&contract_addr); - let root2 = root2.put(&TrieKeySlice::from_key(&ckey), &code_bytes, store.as_ref()); + 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 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 (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" + ); + } - let call_tx = rustock_core::Transaction { - nonce: 1, - gas_price: U256::from(0), - gas_limit: U256::from(100_000), - to: Bytes::copy_from_slice(contract_addr.as_slice()), - value: U256::ZERO, - input: Bytes::new(), - v: 0, - r: U256::ZERO, - s: U256::ZERO, - cached_rlp: None, + /// 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 call_result = executor - .execute_tx(&header, &call_tx, sender, &root2, store) - .unwrap(); + 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"); - assert!(call_result.success, "contract call should succeed"); - assert_eq!(call_result.output.len(), 32, "should return 32-byte ABI-encoded uint32"); + 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"); + } - let depth = u32::from_be_bytes([ - call_result.output[28], - call_result.output[29], - call_result.output[30], - call_result.output[31], + /// `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); + } + + /// 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 + } - assert!( - depth >= 2, - "depth via contract call should be >= 2 (tx frame + contract frame), got {}", - depth - ); + /// 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"); + } + + /// 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 { @@ -7302,6 +12660,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(); @@ -7359,12 +12718,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}" + ); } } @@ -7608,6 +12975,31 @@ mod tests { assert_eq!(unique.len(), 22); } + /// rskj's regtest, from a genesis file: its three federators and its seven + /// authorizers (five of federation changes, one of the lock whitelist, one + /// of the fee per KB) send free Bridge transactions while areBridgeTxsPaid + /// is off (`BridgeUtils.isFreeBridgeTx`). rustock's own regtest has no + /// federators, and three of the five federation change authorizers. + #[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(), 10); + 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 { @@ -7650,6 +13042,43 @@ mod tests { assert_eq!(result.gas_used, 0); } + /// A free Bridge transaction is held to its sender's next nonce all the + /// same (`TransactionExecutor.init`): rskj refuses one that isn't, and + /// the block that holds it. + #[test] + fn a_free_bridge_tx_is_held_to_its_senders_nonce() { + let federator = rsk_address_from_pubkey_hex( + "03b53899c390573471ba30e5054f78376c5f797fda26dde7a760789f02908cbad2", + ) + .unwrap(); + let store = Arc::new(MemoryTrieStore::new()); + let root = put_account( + &TrieNode::empty(), + store.as_ref(), + &federator, + 0, + U256::ZERO, + ); + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let tx = rustock_core::Transaction { + nonce: 1, + gas_price: U256::ZERO, + gas_limit: U256::ZERO, + to: Bytes::copy_from_slice(crate::precompiles::BRIDGE_ADDR.as_slice()), + value: U256::ZERO, + input: Bytes::copy_from_slice(&block_header_selector("updateCollections()")), + v: 28, + r: U256::from(1), + s: U256::from(1), + cached_rlp: None, + }; + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + match executor.execute_block(&dummy_header(457), &[(tx, federator)], &root, store) { + Err(error) => assert!(error.to_string().contains("Invalid nonce"), "{error}"), + Ok(_) => panic!("the block executes"), + } + } + /// From areBridgeTxsPaid on, the same tx goes through normal gas /// validation and is rejected (gas_limit 0 < intrinsic cost). #[test] @@ -7661,15 +13090,200 @@ mod tests { assert!(run_bridge_tx_gas0(370_000, federator).is_err()); } - /// A non-federation sender never gets the free path. Pre-RSKIP136 the - /// call is still included: rskj execError (insufficient limit for the - /// precompile cost) yields a failed receipt consuming the (zero) limit. + /// A non-federation sender never gets the free path, and a limit of 0 + /// can't pay the intrinsic cost: rskj's `TransactionExecutor.init` refuses + /// the transaction before anything runs (`gasIsValid`), and + /// `TransactionListExecutor` stops the block there, which makes it + /// invalid. The call's own price is never reached. #[test] fn test_bridge_tx_gas0_fails_for_unknown_sender() { - let result = run_bridge_tx_gas0(457, Address::repeat_byte(0xEE)) - .expect("pre-RSKIP136 direct bridge call is included, not rejected"); - assert!(!result.tx_results[0].success); - assert_eq!(result.tx_results[0].gas_used, 0, "all of the zero gas limit consumed"); + match run_bridge_tx_gas0(457, Address::repeat_byte(0xEE)) { + Err(error) => assert!(error.to_string().contains("Not enough gas"), "{error}"), + Ok(_) => panic!("the block executes"), + } + } + + /// A free Bridge call that throws after it logged keeps its logs in the + /// receipt, as rskj keeps them: the Bridge writes its logs straight into + /// the result's list, which only the VM's path clears on a throw + /// (`TransactionExecutor.go` returns at `vm == null`). On rskj's regtest a + /// federator's Bridge transactions are always free; federator1's + /// `updateCollections` logs `update_collections` and then refuses a + /// confirmed peg-out of no creation hash, made before RSKIP146, as RSKIP375 + /// refuses it. The receipt is a success at no gas with that one log, and the + /// call's writes are dropped. + #[test] + fn a_free_bridge_call_that_throws_keeps_what_it_logged() { + use crate::bridge::peg::{ + serialize_pegouts_waiting_for_confirmations, PegoutWaitingForConfirmations, + }; + use crate::bridge::storage::{bridge_storage_key, PEGOUTS_WAITING_FOR_CONFIRMATIONS_KEY}; + let federator1 = rsk_address_from_pubkey_hex( + "0362634ab57dae9cb373a5d536e66a8c4f67468bbcfb063809bab643072d78a124", + ) + .unwrap(); + let store = Arc::new(MemoryTrieStore::new()); + let root = put_account(&TrieNode::empty(), store.as_ref(), &federator1, 0, U256::ZERO); + // A peg-out of one input and one output, kept in the cell of no hashes + // since block 0. + let mut pegout = vec![1, 0, 0, 0, 1]; + pegout.extend_from_slice(&[0x11; 32]); + pegout.extend_from_slice(&[0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 1]); + pegout.extend_from_slice(&100_000_000u64.to_le_bytes()); + pegout.extend_from_slice(&[1, 0x51, 0, 0, 0, 0]); + let cell = serialize_pegouts_waiting_for_confirmations( + &[PegoutWaitingForConfirmations { + btc_tx_raw: pegout, + rsk_block_height: 0, + rsk_tx_hash: None, + }], + false, + ); + let key = rustock_trie::storage_key( + &crate::precompiles::BRIDGE_ADDR, + &B256::from(bridge_storage_key(PEGOUTS_WAITING_FOR_CONFIRMATIONS_KEY)), + ); + let root = root.put(&TrieKeySlice::from_key(&key), &cell, store.as_ref()); + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::ZERO, + gas_limit: U256::ZERO, + to: Bytes::copy_from_slice(crate::precompiles::BRIDGE_ADDR.as_slice()), + value: U256::ZERO, + input: Bytes::copy_from_slice(&block_header_selector("updateCollections()")), + v: 28, + r: U256::from(1), + s: U256::from(1), + cached_rlp: None, + }; + 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, &[(tx, federator1)], &root, store) + .expect("a refused updateCollections fails the call, never the block"); + let outcome = &result.tx_results[0]; + assert!(outcome.success, "the throw is invisible in the receipt"); + assert_eq!(outcome.gas_used, 0, "a free transaction"); + assert_eq!(outcome.logs.len(), 1, "update_collections, logged before the throw"); + assert_eq!( + outcome.logs[0].topics()[0], + alloy_primitives::keccak256("update_collections(address)") + ); + assert!( + !result + .markers + .raw_storage + .iter() + .any(|(address, _, _)| *address == crate::precompiles::BRIDGE_ADDR), + "the call's writes are dropped" + ); + } + + /// 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::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 @@ -7755,22 +13369,27 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ let root = TrieNode::empty(); let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); - // The processing block (#1) whose fees REMASC distributes at #4001. - let processing = dummy_header(1); - let processing_hash = processing.hash(); - block_store.put_header(&processing).unwrap(); - block_store.put_canonical_hash(1, processing_hash).unwrap(); + // The processing block (#1) whose fees REMASC distributes at #4001, + // and the seven after it whose uncles could be its siblings, each on + // its parent; and #4,000, the parent of the block that runs, which + // the main chain's index takes down to them. + let mut parent_hash = B256::ZERO; + for number in 1..=8 { + let mut block = dummy_header(number); + block.parent_hash = parent_hash; + parent_hash = block.hash(); + block_store.put_header(&block).unwrap(); + block_store.put_canonical_hash(number, parent_hash).unwrap(); + } + let parent = dummy_header(4_000); + block_store.put_header(&parent).unwrap(); + block_store + .put_canonical_hash(4_000, parent.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 mut header = dummy_header(4_001); + header.parent_hash = parent.hash(); + let remasc_tx = rustock_core::Transaction::remasc(header.number); let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); let result = executor @@ -7793,15 +13412,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 @@ -8087,9 +13698,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(); @@ -8110,17 +13723,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. @@ -8254,6 +13871,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() { @@ -8377,7 +14043,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(); @@ -8452,7 +14119,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()); @@ -8486,23 +14154,28 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ 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, + value: U256::ZERO, + 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()); @@ -8516,9 +14189,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(); @@ -8546,7 +14222,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: @@ -8683,21 +14360,16 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ }; let mut header = dummy_header(block_number); + // On the chain the store holds: REMASC reads the blocks before the + // one it runs in up from its parent. + header.parent_hash = block_store + .canonical_hash(block_number.saturating_sub(1)) + .unwrap() + .unwrap_or_default(); 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 @@ -8900,20 +14572,10 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ let mut header = dummy_header(15); header.beneficiary = Address::repeat_byte(0x99); + header.parent_hash = bs.canonical_hash(14).unwrap().unwrap(); // 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), @@ -9236,20 +14898,10 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ let mut header = dummy_header(15); header.beneficiary = Address::repeat_byte(0x99); + header.parent_hash = bs.canonical_hash(14).unwrap().unwrap(); // 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), @@ -9389,6 +15041,315 @@ 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 mut header = dummy_header(chain.len() as u64); + header.parent_hash = parent_hash; + 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()) + } + + /// 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`. + #[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 mut header = dummy_header(chain.len() as u64); + header.parent_hash = parent_hash; + 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. + /// 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 = Some(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 @@ -9479,7 +15440,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"); @@ -9499,6 +15460,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. + 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: 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 @@ -9878,23 +15963,208 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ .expect("block"); assert!(r.tx_results[0].success); - let new_root = - crate::state::apply_state_changes(&root, store.as_ref(), &r.state_changes, &r.markers); + let new_root = + crate::state::apply_state_changes(&root, store.as_ref(), &r.state_changes, &r.markers); + assert!( + trie_account(&new_root, store.as_ref(), &victim).is_none(), + "self-beneficiary account deleted, balance burned" + ); + assert_subtree_deleted(&new_root, store.as_ref(), &victim); + let sender_acct = trie_account(&new_root, store.as_ref(), &sender).unwrap(); + 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" + ); + } + + /// 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!( - trie_account(&new_root, store.as_ref(), &victim).is_none(), - "self-beneficiary account deleted, balance burned" + !result.tx_results[0].success, + "the precompile had too little gas" ); - assert_subtree_deleted(&new_root, store.as_ref(), &victim); - let sender_acct = trie_account(&new_root, store.as_ref(), &sender).unwrap(); - assert_eq!(sender_acct.balance, one_rbtc, "burned balance goes nowhere"); + 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}"); + } } - /// 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); @@ -9906,11 +16176,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)); @@ -9928,43 +16198,833 @@ 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)), + 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, + ); + 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 code deleted" + }); + 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. The key + /// of one made with no code before the block holds nothing. But the + /// block's repository keeps what its transactions write in a cache until + /// the block ends (`RepositoryLocator.startTrackingAt`, `MutableTrieCache`), + /// and there the key of a contract made with no code in the block holds + /// the empty code its creation saved, which hashes to `keccak256("")` + /// (`MutableTrieCache.getValueHash`; rskj's + /// `ExtCodeHashDslTest.invokeEXTCodeHASHWithCacheEmptyDeploy`): here one + /// made in the same transaction, and one made earlier in the block. 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_before_rskip169_reads_the_blocks_cache() { + 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), + empty, + "#{number}: made in this transaction" + ); + assert_eq!(probed(&made), empty, "#{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); + } + } + + /// 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)>, + /// For each transaction, where the file says: the precompiles rskj + /// counts it as having called (`precompiledContractsCalled`). + called: 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, + } + + /// 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])); + } + "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])), + "called" => run.called.push( + fields[1] + .split(',') + .filter(|address| *address != "-") + .map(|address| Address::from_slice(&bytes(address))) + .collect(), + ), + "itx" | "end" | "enc" => {} + other => panic!("an unknown line: {other}"), + } + } + runs + } + + /// The state one of rskj's runs starts from: its accounts and its cells. + fn state_of_rskj_run(run: &RskjRun) -> (Arc, TrieNode) { + 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()); + } + (store, root) + } + + /// 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, whether it reached + /// REMASC or the Bridge where the file says what it called, the Bridge's + /// storage, and the state root. + fn replays_as_rskj_ran_it(run: &RskjRun) { + let (store, root) = state_of_rskj_run(run); + 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"); + if let Some(called) = run.called.get(index) { + let reached = called.iter().any(|address| { + crate::precompiles::SEQUENTIAL_ONLY_PRECOMPILES.contains(address) + }); + assert_eq!( + outcome.reached_sequential_only, reached, + "{what}: whether it reached REMASC or the Bridge (rskj called {called:?})" + ); + } + } + // 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()) + .chain(run.long_cells.keys()) + .collect(); + for key in keys { + 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, + store.as_ref(), + &result.state_changes, + &result.markers, + ); + assert_eq!( + after.compute_hash(store.as_ref()), + run.root, + "{}: the state after", + run.name ); + } + + /// 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 + /// 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 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). + /// + /// `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")); + 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)); + } + } + + /// Which transactions reach REMASC or the Bridge, as rskj counts them: + /// under RSKIP-144 such a transaction may only sit in a block's sequential + /// sublist, and a block with one in a parallel sublist is invalid + /// (`TransactionListExecutor`). `testdata/rskj_precompiles_called.txt` is + /// rskj's own `TransactionExecutor` running each transaction and saying + /// which precompiles it counted (`precompiledContractsCalled`). + /// + /// rskj counts a call to a precompile as the call starts + /// (`Program.callToPrecompiledAddress`), before it checks the depth and the + /// caller's balance: a call to the Bridge or REMASC that is then refused + /// counts (`calls-bridge-unpaid`, `callcodes-bridge-unpaid`, + /// `calls-remasc-unpaid`, and `deep-bridge`, at the depth limit). And a + /// program hands what it counted to its caller only from a call, whether + /// the call succeeded or not (`Program.executeCode`), and the transaction + /// keeps it though it then reverts (`calls-bridge-then-reverts`): what a + /// constructor run by CREATE or CREATE2 counted is dropped (the + /// `creates-*-calling-*` runs and `deploy-creating-child-calling-bridge`), + /// though not the constructor of a transaction that creates + /// (`deploy-calling-bridge`), nor what its creator calls once it has ended + /// (`creates-reverting-then-calls-bridge`, `creates-empty-then-calls-bridge`). + /// + /// A block's run, which a block is checked and built with, is held to all + /// rskj did; the runs of one transaction by itself (a local call, a + /// trace, an estimate) only to what it counted. + #[test] + fn a_transaction_reaches_remasc_or_the_bridge_as_rskj_counts_it() { + let runs = rskj_runs(include_str!("testdata/rskj_precompiles_called.txt")); + assert_eq!(runs.len(), 26); + let failed: Vec<&str> = runs + .iter() + .filter(|run| { + assert_eq!(run.called.len(), run.results.len(), "{}", run.name); + std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + replays_as_rskj_ran_it(run) + })) + .is_err() + }) + .map(|run| run.name.as_str()) + .collect(); assert!( - new_root - .get( - &TrieKeySlice::from_key(&rustock_trie::storage_prefix_key(&victim)), - store.as_ref(), + failed.is_empty(), + "{} of 26 differ: {failed:?}", + failed.len() + ); + + 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) + }; + for run in &runs { + let (store, root) = state_of_rskj_run(run); + let (tx, sender) = &run.transactions[0]; + let reached = run.called[0] + .iter() + .any(|address| crate::precompiles::SEQUENTIAL_ONLY_PRECOMPILES.contains(address)); + let alone = executor + .execute_tx(&header, tx, *sender, &root, store.clone()) + .unwrap(); + let (traced, _) = executor + .execute_tx_traced(&header, tx, *sender, &root, store.clone(), false, 0) + .unwrap(); + let (estimated, _) = executor + .execute_tx_for_estimate(&header, tx, *sender, &root, store.clone(), false) + .unwrap(); + assert_eq!( + [ + alone.reached_sequential_only, + traced.reached_sequential_only, + estimated.reached_sequential_only + ], + [reached; 3], + "{}: alone, traced and estimated", + run.name + ); + } + } + + /// The Bridge's unlogged decisions go with the block that made them + /// (`ProcessedBlock::unlogged_peg_events`), and only those that stand. In + /// `relay` the contract the transaction calls hands the registration to + /// the Bridge, which refunds the peg-in: the block carries that, and the + /// thread records nothing once the block is done. Where a contract calls + /// the relay instead, and the relay's code ends in REVERT, nothing of the + /// registration stands and the block carries nothing of it: a frame that + /// fails takes back the decisions made under it, as it does its writes. + /// Where the relay ends as it does and the contract then calls one that + /// reverts, the registration stands, and so does what it decided. + #[test] + fn a_block_carries_the_peg_decisions_that_stand() { + let runs = rskj_runs(include_str!("bridge/testdata/rskj_refunded_pegins.txt")); + let run = run_named(&runs, "relay"); + let relay = Address::repeat_byte(0xa1); + let moved = Address::repeat_byte(0xb2); + let reverts = Address::repeat_byte(0xc3); + // The relay's code, which ends in STOP, and the same ending in REVERT. + let relay_code = run + .accounts + .iter() + .find(|account| account.0 == relay) + .map(|account| account.3.clone()) + .unwrap(); + assert_eq!(relay_code.last(), Some(&0x00)); + let relay_reverting = [ + &relay_code[..relay_code.len() - 1], + &[0x60, 0x00, 0x60, 0x00, 0xfd], + ] + .concat(); + // A CALL of `to` with all the gas, with the call data or none, + // popping its flag. + let call = |to: &Address, data: bool| { + let size: &[u8] = if data { &[0x36] } else { &[0x60, 0x00] }; + [ + &[0x60, 0x00, 0x60, 0x00][..], + size, + &[0x60, 0x00, 0x60, 0x00, 0x73], + to.as_slice(), + &[0x5a, 0xf1, 0x50], + ] + .concat() + }; + let copy_call_data = [0x36, 0x60, 0x00, 0x60, 0x00, 0x37]; + let decisions = |codes: &[(Address, Vec)]| { + let (store, mut root) = state_of_rskj_run(run); + for (address, code) in codes { + if *address != relay { + root = put_account(&root, store.as_ref(), address, 1, U256::ZERO); + } + let key = rustock_trie::code_key(address); + root = root.put(&TrieKeySlice::from_key(&key), code, store.as_ref()); + } + let dir = tempfile::tempdir().unwrap(); + let events = Arc::new( + rustock_storage::legacy_event_store::LegacyEventStore::open( + dir.path().to_str().unwrap(), ) - .is_none(), - "victim marker deleted" + .unwrap(), + ); + let block_store = + Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let processor = crate::processor::BlockProcessor::new( + RskHardforkConfig::rskj_regtest(), + block_store, + ) + .with_bridge_constants(crate::bridge::constants::BridgeConstants::rskj_regtest()) + .with_legacy_event_store(events); + let block = rustock_core::Block { + header: Header { + difficulty: U256::from(1), + ..dummy_header(10) + }, + transactions: run.transactions.iter().map(|(tx, _)| tx.clone()).collect(), + ommers: vec![], + }; + let processed = processor + .execute_block(&block, &root, store.clone()) + .expect("block"); + assert!( + !crate::silent::is_recording(), + "the thread records once the block is done" + ); + processed + .unlogged_peg_events + .iter() + .map(|decision| decision.kind) + .collect::>() + }; + let stands = decisions(&[]); + assert!( + stands.contains(&crate::silent::SilentKind::PeginRefunded), + "{stands:?}" + ); + let under = + |then: &[u8]| [©_call_data[..], &call(&moved, true), then, &[0x00]].concat(); + assert_eq!( + decisions(&[(moved, relay_reverting), (relay, under(&[]))]), + Vec::new(), + "the relay reverts" + ); + assert_eq!( + decisions(&[ + (moved, relay_code.clone()), + (reverts, vec![0x60, 0x00, 0x60, 0x00, 0xfd]), + (relay, under(&call(&reverts, false))), + ]), + stands, + "a call after the relay's reverts" ); - // 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"); } } diff --git a/crates/execution/src/gas_estimator.rs b/crates/execution/src/gas_estimator.rs index b44b2a4..d6a86e8 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,12 +55,37 @@ //! 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. A CREATE +//! it refuses for the depth or for a balance short of the value spends +//! none of that: the refusal comes first. +//! - 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; use revm::inspector::Inspector; use revm::interpreter::{ - CallInputs, CallOutcome, CreateInputs, CreateOutcome, Interpreter, InterpreterTypes, + CallInputs, CallOutcome, CreateInputs, CreateOutcome, InstructionResult, Interpreter, + InterpreterTypes, }; /// rskj `GasCost.STIPEND_CALL`. @@ -69,17 +95,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 +122,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 +145,47 @@ 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, + /// What `max_committed` was before that program's last completed step, + /// for a CREATE that turns out to have spent nothing (`create_end`). + max_before_step: u64, } impl EstimationInspector { @@ -164,54 +194,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 +242,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 +254,35 @@ 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_before_step = self.max_committed; + 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 } @@ -254,9 +290,19 @@ where &mut self, _context: &mut CTX, _inputs: &CreateInputs, - _outcome: &mut CreateOutcome, + outcome: &mut CreateOutcome, ) { - self.leave_frame(); + // rskj refuses a CREATE whose program holds less than the value + // before it spends the gas (`Program.createContract`), so the + // transaction's own program never held what the step took off for + // the constructor: its peak is what it was before that step. What + // the opcode itself cost is in what the program has spent by its + // next step. (The depth, rskj's other such refusal, is never the + // reason in that program.) + if self.open.len() == 2 && outcome.result.result == InstructionResult::OutOfFunds { + self.max_committed = self.max_before_step; + } + self.close_program(false); } } @@ -283,57 +329,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 +377,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 b310e87..95020e9 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)] = &[ @@ -62,8 +64,9 @@ pub const MAINNET_ACTIVATIONS: &[(u64, RskNetworkUpgrade)] = &[ /// Testnet activation heights. /// /// Ground truth is rskj `rskj-core/src/main/resources/config/testnet.conf` -/// (rskj 9.1.0). Testnet starts already on wasabi100: bahamas, orchid, -/// orchid060 and wasabi100 are all height 0 there, unlike mainnet. +/// (rskj's release VETIVER-9.0.4). Testnet starts already on wasabi100: +/// bahamas, orchid, orchid060 and wasabi100 are all height 0 there, unlike +/// mainnet. pub const TESTNET_ACTIVATIONS: &[(u64, RskNetworkUpgrade)] = &[ (0, RskNetworkUpgrade::Genesis), (0, RskNetworkUpgrade::Orchid), @@ -86,6 +89,9 @@ 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 { + Rskip103, + Rskip136, + Rskip144, Rskip351, Rskip535, Rskip536, @@ -95,6 +101,9 @@ impl ConsensusRule { /// The rule rskj calls `name` in `consensusRules`. pub fn from_rskj_name(name: &str) -> Option { Some(match name { + "rskip103" => Self::Rskip103, + "rskip136" => Self::Rskip136, + "rskip144" => Self::Rskip144, "rskip351" => Self::Rskip351, "rskip535" => Self::Rskip535, "rskip536" => Self::Rskip536, @@ -104,6 +113,24 @@ impl ConsensusRule { } impl RskNetworkUpgrade { + /// The upgrades of rskj's `hardforkActivationHeights` that are rungs of + /// this ladder, in their order. + const SCHEDULED: [Self; 13] = [ + Self::Orchid, + Self::Wasabi100, + Self::TwoToThree, + Self::Papyrus200, + Self::Iris300, + Self::Hop400, + Self::Fingerroot500, + Self::Arrowhead600, + Self::Arrowhead631, + Self::Lovell700, + Self::Reed800, + Self::Reed810, + Self::Vetiver900, + ]; + /// 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 @@ -126,6 +153,35 @@ impl RskNetworkUpgrade { _ => return None, }) } + + /// rskj's name for the upgrade in `hardforkActivationHeights`. + pub fn rskj_name(self) -> &'static str { + match self { + Self::Genesis => "genesis", + Self::Orchid => "orchid", + Self::Wasabi100 => "wasabi100", + Self::TwoToThree => "twoToThree", + Self::Papyrus200 => "papyrus200", + Self::Iris300 => "iris300", + Self::Hop400 => "hop400", + Self::Fingerroot500 => "fingerroot500", + Self::Arrowhead600 => "arrowhead600", + Self::Arrowhead631 => "arrowhead631", + Self::Lovell700 => "lovell700", + Self::Reed800 => "reed800", + Self::Reed810 => "reed810", + Self::Vetiver900 => "vetiver900", + } + } + + /// Whether the ladder reads this upgrade's rules at the upgrade's own + /// height, as rskj reads every rule, and not from the upgrade in force. + /// The two upgrades a network leaves out are read so: reed810, which + /// mainnet never schedules (RSKIP144, RSKIP351, RSKIP502, RSKIP529 and + /// RSKIP536), and arrowhead631, which testnet never does (RSKIP434). + fn is_read_at_its_own_height(self) -> bool { + matches!(self, Self::Arrowhead631 | Self::Reed810) + } } /// Configuration that determines which RSK features are active at a given block. @@ -134,8 +190,15 @@ 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. 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 + /// (`BridgeUtils.isFreeBridgeTx`). `None` is rskj's `-1`, as on rskj's + /// regtest, where they stay free. + after_bridge_sync: Option, } impl RskHardforkConfig { @@ -143,7 +206,10 @@ 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), } } @@ -152,14 +218,45 @@ 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; 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)], - chain_id: 33, - rule_overrides: Vec::new(), + activations: Self::from_genesis_without(&[ + RskNetworkUpgrade::Arrowhead631, + RskNetworkUpgrade::Reed810, + ]), + chain_id: RSK_REGTEST_CHAIN_ID, + rule_overrides: vec![(ConsensusRule::Rskip536, Some(0))], + after_bridge_sync: Self::after_bridge_sync_for(RSK_REGTEST_CHAIN_ID), + } + } + + /// 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` leaves `tbd1000`, the + /// upgrade for RSKIP559, unscheduled, and this ladder doesn't model it. + pub fn rskj_regtest() -> Self { + Self { + activations: Self::from_genesis_without(&[RskNetworkUpgrade::Arrowhead631]), + chain_id: RSK_REGTEST_CHAIN_ID, + rule_overrides: vec![ + (ConsensusRule::Rskip144, Some(1)), + (ConsensusRule::Rskip351, Some(1)), + (ConsensusRule::Rskip535, Some(1)), + ], + after_bridge_sync: None, } } @@ -177,11 +274,19 @@ 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. RSKIP536 is on without reed810, as on mainnet. 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(), + rule_overrides: vec![(ConsensusRule::Rskip536, Some(0))], + after_bridge_sync: Self::after_bridge_sync_for(chain_id), } } @@ -198,6 +303,7 @@ impl RskHardforkConfig { ], chain_id, rule_overrides: Vec::new(), + after_bridge_sync: Self::after_bridge_sync_for(chain_id), } } @@ -213,6 +319,54 @@ 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)> { + RskNetworkUpgrade::SCHEDULED + .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() + } + + /// The first block this ladder has `upgrade`'s rules in force, or `None` + /// where it never has: the upgrade's own height for the two read that + /// way ([`RskNetworkUpgrade::is_read_at_its_own_height`]), and for every + /// other the lowest height of it or a later rung, as `active_upgrade` + /// reads it. + fn rules_in_force_from(&self, upgrade: RskNetworkUpgrade) -> Option { + if upgrade.is_read_at_its_own_height() { + self.activation_height(upgrade) + } else { + self.in_force_from(upgrade) + } + } + + /// 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 { @@ -235,8 +389,14 @@ impl RskHardforkConfig { .unwrap_or(RskNetworkUpgrade::Genesis) } - /// Schedule `upgrade` at `height`, or take it off the ladder with `None` - /// (rskj's `-1`). Activations stay ordered by height. + /// Schedule `upgrade` at `height`, or take it off the ladder with `None`. + /// Activations stay ordered by height. + /// + /// Off the ladder is rskj's `-1` only for a rung with no later rung + /// scheduled, and for reed810 and arrowhead631: every other rung's rules + /// are read from the upgrade in force, so a later rung still turns them + /// on. [`Self::with_rskj_overrides`], which takes rskj's own schedule, + /// refuses what this would read otherwise than rskj. pub fn with_hardfork(mut self, upgrade: RskNetworkUpgrade, height: Option) -> Self { self.activations.retain(|(_, u)| *u != upgrade); if let Some(h) = height { @@ -259,11 +419,11 @@ 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`, `orchid060` and `hop401`. fn fixed_rskj_height(&self, name: &str) -> Option { let (mainnet, testnet) = match name { "bahamas" => (3_397, 0), - "afterBridgeSync" => (370_000, 114_000), + "orchid060" => (1_052_700, 0), "hop401" => (4_976_300, 3_362_200), _ => return None, }; @@ -274,6 +434,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. @@ -281,16 +453,22 @@ impl RskHardforkConfig { /// What each name does here: /// /// - 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. - /// - `rskip351`, `rskip535`, `rskip536`: scheduled apart from their - /// upgrade. - /// - `rskipUMM`: follows papyrus200 here, so it is accepted at that height - /// and refused at any other. + /// height, or removed. Refused where the ladder would then read some + /// upgrade's rules from another height than rskj does + /// ([`Self::reads_as_rskj_does`]): with an upgrade left out below a + /// scheduled one, or scheduled out of its order. + /// - `afterBridgeSync`: where the federators' and authorizers' Bridge + /// transactions start to be paid, or never. + /// - `bahamas`, `orchid060`, `hop401`: read at one fixed height per chain, + /// so they are accepted at that height and refused at any other. + /// - `tbd1000`: nothing here reads it. It 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. + /// - `rskip103`, `rskip136`, `rskip144`, `rskip351`, `rskip535` and + /// `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( @@ -299,9 +477,18 @@ impl RskHardforkConfig { rules: &[(String, Option)], ) -> Result<(Self, Vec), String> { let mut unmodelled = Vec::new(); + // Where rskj reads each upgrade's rules from: where this ladder has + // them in force, until an override names another height. + let mut rskj_heights = RskNetworkUpgrade::SCHEDULED + .map(|upgrade| (upgrade, self.rules_in_force_from(upgrade))); for (name, height) in hardforks { if let Some(upgrade) = RskNetworkUpgrade::from_rskj_name(name) { self = self.with_hardfork(upgrade, *height); + for named in rskj_heights.iter_mut().filter(|(rung, _)| *rung == upgrade) { + named.1 = *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!( @@ -309,33 +496,67 @@ impl RskHardforkConfig { cannot move it" )); } - } else if matches!(name.as_str(), "orchid060" | "tbd1000") { + } else if name == "tbd1000" { unmodelled.push(name.clone()); } else { return Err(format!("hardfork {name}: not an rskj upgrade name")); } } + self.reads_as_rskj_does(&rskj_heights)?; for (name, height) in rules { 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)" + (rskip103, rskip136, rskip144, rskip351, rskip535, rskip536, rskipUMM)" )); } } Ok((self, unmodelled)) } + /// Whether this ladder has every upgrade's rules in force from the height + /// rskj reads them at, which `rskj_heights` gives for each. + /// + /// rskj reads each rule at the height of its own upgrade + /// (`ActivationConfig.isActive`): an upgrade at `-1` has its rules off for + /// good, whatever is scheduled after it, and one scheduled above a later + /// upgrade has them off until its own height. This ladder reads a rule + /// from the upgrade in force (`active_upgrade() >= X`), and has it on from + /// the later upgrade's height in both cases; and an upgrade scheduled + /// below an earlier one brings the earlier one's rules down with it. The + /// networks leave out only reed810 and arrowhead631, whose rules are read + /// at their own heights here as well; rskj's own tests leave out others. + /// A schedule this ladder would read otherwise than rskj is refused, not + /// run. + fn reads_as_rskj_does( + &self, + rskj_heights: &[(RskNetworkUpgrade, Option)], + ) -> Result<(), String> { + let from = |height: Option| match height { + Some(height) => format!("from #{height}"), + None => "never".to_string(), + }; + for (upgrade, rskj) in rskj_heights { + let here = self.rules_in_force_from(*upgrade); + if here != *rskj { + return Err(format!( + "hardfork overrides: rskj reads {name}'s rules {rskj}, and this node would \ + read them {here}. It reads a rule from the upgrade in force, so no upgrade \ + but reed810 and arrowhead631 can be left out below a scheduled one, or \ + scheduled out of its order", + name = upgrade.rskj_name(), + rskj = from(*rskj), + here = from(here), + )); + } + } + Ok(()) + } + /// Maps the RSK network upgrade to the closest revm SpecId. pub fn spec_id(&self, block_number: u64) -> SpecId { upgrade_to_spec_id(self.active_upgrade(block_number)) @@ -424,6 +645,14 @@ impl RskHardforkConfig { self.active_upgrade(block_number) >= RskNetworkUpgrade::Orchid } + /// RSKIP85 (orchid): REMASC holds back a reward, and a federator's share, + /// that comes to less than the executing block's minimum gas price times + /// its minimum payable gas. rskj reads it at the executing block + /// (`Remasc.activations`). + pub fn has_rskip85(&self, block_number: u64) -> bool { + self.active_upgrade(block_number) >= RskNetworkUpgrade::Orchid + } + /// RSKIP123: multikey federation members; disables the single-key "add" /// federation-change function (Wasabi100). pub fn has_rskip123(&self, block_number: u64) -> bool { @@ -498,6 +727,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 @@ -506,11 +741,25 @@ 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 } + /// RSKIP218 (iris300): REMASC pays the Core Developers Fund at its new + /// address (`Remasc.getRskLabsAddress`), read at the executing block. + pub fn has_rskip218(&self, block_number: u64) -> bool { + self.active_upgrade(block_number) >= RskNetworkUpgrade::Iris300 + } + /// RSKIP201: peg-out BTC transactions use version 2 (Iris300). pub fn has_rskip201(&self, block_number: u64) -> bool { self.active_upgrade(block_number) >= RskNetworkUpgrade::Iris300 @@ -523,7 +772,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 } @@ -580,15 +837,17 @@ 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 + /// 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 @@ -605,8 +864,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 @@ -635,13 +902,22 @@ 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 } + /// 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). /// @@ -709,17 +985,40 @@ 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 { self.active_upgrade(block_number) >= RskNetworkUpgrade::Lovell700 } + /// RSKIP103 (orchid060 — mainnet 1_052_700, testnet/regtest genesis): + /// STATICCALL takes no value. Before it rskj's `VM.calculateCallValue` + /// takes one off the stack for every call but DELEGATECALL + /// (`rsk_instructions::rsk_static_call_with_value`). orchid060 carries no + /// other rule, and isn't a rung of this ladder. + pub fn has_rskip103(&self, block_number: u64) -> bool { + if let Some(height) = self.rule_override(ConsensusRule::Rskip103) { + return height.is_some_and(|h| block_number >= h); + } + self.fixed_rskj_height("orchid060") + .is_some_and(|h| block_number >= h) + } + /// RSKIP136 (bahamas — mainnet 3_397, testnet/regtest genesis): correct /// gas accounting for direct precompile calls. Before it, rskj checked /// 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, @@ -758,17 +1057,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 @@ -787,7 +1081,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) } @@ -807,31 +1107,74 @@ 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 + /// 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 - /// 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; @@ -896,7 +1239,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. @@ -979,7 +1322,7 @@ mod tests { assert!(testnet.has_rskip91(0)); } - /// rskj `rskj-core/src/main/resources/config/testnet.conf` (rskj 9.1.0), + /// rskj `rskj-core/src/main/resources/config/testnet.conf` (VETIVER-9.0.4), /// transcribed as a block. rustock's testnet ladder had drifted: wasabi100 /// was 863,000 (rskj: 0 — that number is rskj's *papyrus200*), papyrus200 /// was 1,580,000 (a value that appears nowhere in rskj), twoToThree was @@ -1283,7 +1626,7 @@ mod override_tests { #[test] fn the_seed_schedule_turns_reed810_on_above_vetiver900() { let (cfg, unmodelled) = seed_ladder(); - assert_eq!(unmodelled, vec!["orchid060".to_string(), "tbd1000".to_string()]); + assert_eq!(unmodelled, vec!["tbd1000".to_string()]); assert_eq!(cfg.activation_height(RskNetworkUpgrade::Reed810), Some(9_002_504)); assert!(!cfg.has_rskip144(9_002_503)); assert!(cfg.has_rskip144(9_002_504)); @@ -1307,12 +1650,17 @@ 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)); - 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); @@ -1330,9 +1678,19 @@ 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), + ( + RskHardforkConfig::mainnet().rule_overrides, + Vec::::new() + ) + ); let unknown = RskHardforkConfig::mainnet() .with_rskj_overrides(&[h("quartz", 1)], &[]); assert!(unknown.is_err()); @@ -1351,4 +1709,418 @@ mod override_tests { assert!(!cfg.has_rskip535(8_000_000)); assert_eq!(cfg.active_upgrade(7_604_200), RskNetworkUpgrade::Vetiver900); } + + /// rskj reads each rule at its own upgrade's height, so an upgrade at -1 + /// has its rules off for good whatever comes after it; this ladder reads + /// a rule from the upgrade in force and would have them on. rskj's own + /// tests leave out lovell700, iris300 and arrowhead600 that way (RSKIP438, + /// RSKIP445 and RSKIP446; RSKIP174; RSKIP203). No network does, and the + /// schedule is refused. + #[test] + fn an_upgrade_left_out_below_a_scheduled_one_is_refused() { + for name in ["lovell700", "iris300", "arrowhead600", "orchid", "reed800"] { + for base in [ + RskHardforkConfig::rskj_regtest(), + RskHardforkConfig::regtest(), + RskHardforkConfig::mainnet(), + RskHardforkConfig::testnet(), + ] { + let refused = base.with_rskj_overrides(&[h(name, -1)], &[]).unwrap_err(); + let never = format!("rskj reads {name}'s rules never"); + assert!(refused.contains(&never), "{refused}"); + } + } + // With every later upgrade left out too, the ladder ends below it, and + // reads as rskj's does: lovell700's rules are off and arrowhead600's on. + let last_four = + [h("lovell700", -1), h("reed800", -1), h("reed810", -1), h("vetiver900", -1)]; + let (ends, _) = RskHardforkConfig::rskj_regtest() + .with_rskj_overrides(&last_four, &[]) + .unwrap(); + assert_eq!(ends.active_upgrade(u64::MAX), RskNetworkUpgrade::Arrowhead600); + assert!(!ends.has_initcode_metering(u64::MAX) && !ends.has_rskip445(u64::MAX)); + assert!(ends.has_push0(0)); + // A later upgrade put back over the ones left out would turn theirs on, + // though no override names them. + let refused = ends.with_rskj_overrides(&[h("vetiver900", 0)], &[]).unwrap_err(); + assert!(refused.contains("rskj reads lovell700's rules never"), "{refused}"); + // reed810 and arrowhead631 are read at their own heights, as on the + // networks that leave them out, so each may be left out anywhere. + let (testnet, _) = RskHardforkConfig::testnet() + .with_rskj_overrides(&[h("reed810", -1)], &[]) + .unwrap(); + assert!(!testnet.has_rskip144(u64::MAX) && testnet.has_rskip540(7_604_200)); + let (mainnet, _) = RskHardforkConfig::mainnet() + .with_rskj_overrides(&[h("arrowhead631", -1)], &[]) + .unwrap(); + assert!(!mainnet.has_rskip434(u64::MAX) && mainnet.has_rskip445(7_338_024)); + } + + /// The same for an upgrade moved out of its order: above a later one its + /// rules would be read as in force from the later one's height, and below + /// an earlier one it would bring that one's rules down with it. + #[test] + fn an_upgrade_scheduled_out_of_its_order_is_refused() { + let mainnet = RskHardforkConfig::mainnet; + // lovell700 above reed800 (#8,052,200). + assert!(mainnet().with_rskj_overrides(&[h("lovell700", 8_500_000)], &[]).is_err()); + // fingerroot500 below hop400 (#4,598,500). + assert!(mainnet().with_rskj_overrides(&[h("fingerroot500", 4_000_000)], &[]).is_err()); + // reed810 below every upgrade mainnet has above #100. + assert!(mainnet().with_rskj_overrides(&[h("reed810", 100)], &[]).is_err()); + // reed810 above vetiver900 is read at its own height, as rskj reads it. + assert!(mainnet().with_rskj_overrides(&[h("reed810", 9_002_504)], &[]).is_ok()); + // An upgrade moved within its order, and two moved together, are taken. + assert!(mainnet().with_rskj_overrides(&[h("fingerroot500", 6_000_000)], &[]).is_ok()); + assert!(mainnet() + .with_rskj_overrides(&[h("lovell700", 8_500_000), h("reed800", 8_600_000)], &[]) + .is_ok()); + // At one height with its neighbours, as on a regtest, nothing is out of order. + assert!(RskHardforkConfig::rskj_regtest() + .with_rskj_overrides(&[h("vetiver900", 0), h("orchid", 0)], &[]) + .is_ok()); + } + + /// 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] + 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)); + // 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)); + } + + /// `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. `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_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)); + 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("tbd1000", 0)], &[]) + .unwrap(); + assert_eq!(unmodelled, ["tbd1000"]); + assert_eq!( + named.activations, + RskHardforkConfig::rskj_regtest().activations + ); + assert!(RskHardforkConfig::rskj_regtest() + .with_rskj_overrides(&[h("cardamom1000", 0)], &[]) + .is_err()); + } +} + +/// 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 the config files of rskj's + /// release VETIVER-9.0.4. + /// `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)), + ("rskip103", RskHardforkConfig::has_rskip103, Some(1_052_700), 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)), + ("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)), + ("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") + ); + } + + /// 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`, 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), ("tbd1000", -1), + ]; + 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. + /// `tbd1000` 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, ["tbd1000"]); + 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/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 c5ad843..d424292 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; @@ -6,7 +7,9 @@ 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; pub mod processor; pub mod raw_storage; @@ -28,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, @@ -41,6 +46,9 @@ pub mod tracer; pub mod supply; pub use state::apply_state_changes; +#[cfg(test)] +mod rskj_tests; + /// A context for reading Bridge state at a committed state root. /// /// Read-only by construction: it carries no database and no journal, so the diff --git a/crates/execution/src/mining/dev.rs b/crates/execution/src/mining/dev.rs index 4de3068..4958df5 100644 --- a/crates/execution/src/mining/dev.rs +++ b/crates/execution/src/mining/dev.rs @@ -28,15 +28,21 @@ use std::sync::atomic::{AtomicI64, Ordering}; /// Seconds added to wall clock when stamping a mined block. /// -/// Signed, because rskj's `increaseTime` takes a value that can in principle -/// be negative, and an offset that silently clamped would make a test that -/// went backwards pass for the wrong reason. +/// A Java `long` in rskj (`MinerClock.timeAdjustment`), signed here too. It +/// only grows: rskj's `increaseTime` takes a value that can be negative, and +/// ignores it. static TIME_OFFSET: AtomicI64 = AtomicI64::new(0); /// Move the clock forward by `seconds` and return the new total offset, which -/// is what `evm_increaseTime` answers with. +/// is what `evm_increaseTime` answers with. As rskj's `MinerClock.increaseTime`, +/// a move of no seconds or fewer changes nothing and answers the offset as it +/// stands; a clock moved back could stamp a block before one already mined. pub fn increase_time(seconds: i64) -> i64 { - TIME_OFFSET.fetch_add(seconds, Ordering::SeqCst) + seconds + if seconds <= 0 { + return time_offset(); + } + // Java's `long` addition wraps, as `fetch_add` does. + TIME_OFFSET.fetch_add(seconds, Ordering::SeqCst).wrapping_add(seconds) } /// The current offset, in seconds. @@ -131,7 +137,11 @@ fn submit( let mut raw = Vec::new(); bitcoin::consensus::Encodable::consensus_encode(block, &mut raw) .map_err(|e| MineError::Encode(e.to_string()))?; - server.submit_bitcoin_block(&raw).map_err(MineError::Submit) + // The node's own miner reads a refused block as the rule's error, not + // as the `INVALID_BLOCK` answer a miner submitting over RPC gets. + server + .submit_bitcoin_block_as(&raw, crate::mining::server::Refused::Failed) + .map_err(MineError::Submit) } #[derive(Debug)] 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/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/min_gas_price.rs b/crates/execution/src/mining/min_gas_price.rs new file mode 100644 index 0000000..c0f9bf3 --- /dev/null +++ b/crates/execution/src/mining/min_gas_price.rs @@ -0,0 +1,503 @@ +//! Where the miner's minimum gas price comes from: rskj's +//! `co.rsk.mine.gas.provider`. +//! +//! A fixed price, rskj's `miner.minGasPrice`. Or, with `miner.stableGasPrice` +//! on, one held stable in a currency: its `minStableGasPrice` over the price of +//! a bitcoin in that currency, which a source fetches now and then, from a web +//! API or a contract. The price is kept between fetches, and the fixed one +//! stands in until a fetch brings one. A block steps its minimum toward +//! whichever it is by RSKIP-09's 1% (`MinimumGasPriceCalculator`). +//! +//! A fetch runs on a thread of its own, as rskj's does: a miner building a +//! block takes the price in hand and doesn't wait on the network. + +use alloy_primitives::U256; +use std::fmt; +use std::panic::AssertUnwindSafe; +use std::sync::atomic::{AtomicI32, AtomicI64, Ordering}; +use std::sync::{Arc, Condvar, Mutex, PoisonError}; +use std::time::{SystemTime, UNIX_EPOCH}; +use tracing::{debug, error, warn}; + +/// rskj `MinGasPriceProviderType`. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum MinGasPriceProviderType { + Fixed, + HttpGet, + EthCall, +} + +/// rskj `MinGasPriceProvider`. +pub trait MinGasPriceProvider: Send + Sync + fmt::Debug { + /// `getMinGasPrice`: the price, in wei, a block steers toward now, a Java + /// `long` as rskj holds it. + fn min_gas_price(&self) -> i64; + /// `getType`. + fn provider_type(&self) -> MinGasPriceProviderType; + /// `getMinGasPriceAsCoin`, as the miner steers by it. + fn min_gas_price_as_coin(&self) -> U256 { + as_coin(self.min_gas_price()) + } +} + +/// A price as `MinimumGasPriceCalculator` steers by it. rskj's `Coin` holds a +/// price below zero, which is in no band a block's minimum may move in, so +/// rskj steps to the bottom of the band, as it does for zero: it reads as zero. +pub fn as_coin(price: i64) -> U256 { + U256::from(u64::try_from(price).unwrap_or(0)) +} + +/// rskj `FixedMinGasPriceProvider`: `miner.minGasPrice`. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct FixedMinGasPriceProvider { + min_gas_price: i64, +} + +impl FixedMinGasPriceProvider { + pub fn new(min_gas_price: i64) -> Self { + Self { min_gas_price } + } +} + +impl MinGasPriceProvider for FixedMinGasPriceProvider { + fn min_gas_price(&self) -> i64 { + self.min_gas_price + } + + fn provider_type(&self) -> MinGasPriceProviderType { + MinGasPriceProviderType::Fixed + } +} + +/// What a stable price is fetched from: rskj's `getBtcExchangeRate`, which +/// each of `StableMinGasPriceProvider`'s two kinds gives. +pub trait BtcExchangeRateSource: Send + Sync + fmt::Debug { + /// The price of a bitcoin, or nothing where none could be had. + fn btc_exchange_rate(&self) -> Option; + /// `getType` of the provider it makes. + fn provider_type(&self) -> MinGasPriceProviderType; +} + +/// rskj `StableMinGasPriceProvider.ERR_NUM_OF_FAILURES`: failed fetches in a +/// row from which the log says so as an error rather than a warning. +const ERR_NUM_OF_FAILURES: i32 = 20; + +/// rskj `StableMinGasPriceProvider`. +#[derive(Debug)] +pub struct StableMinGasPriceProvider { + shared: Arc, +} + +/// What the provider and its fetches share. +#[derive(Debug)] +struct Shared { + source: Arc, + fallback: Arc, + min_stable_gas_price: i64, + refresh_rate_millis: i64, + min_valid_price: i64, + max_valid_price: i64, + num_of_failures: AtomicI32, + /// The fetch under way, rskj's `priceFuture`. + in_flight: Mutex>>, + last_min_gas_price: AtomicI64, + last_update_time_millis: AtomicI64, +} + +/// One fetch, rskj's `CompletableFuture`: the price it came to, once it +/// has. +#[derive(Debug, Default)] +struct Fetch { + price: Mutex>, + done: Condvar, +} + +impl Fetch { + fn complete(&self, price: i64) { + *self.price.lock().unwrap_or_else(PoisonError::into_inner) = Some(price); + self.done.notify_all(); + } + + fn is_done(&self) -> bool { + self.price + .lock() + .unwrap_or_else(PoisonError::into_inner) + .is_some() + } + + fn get(&self) -> i64 { + let mut price = self.price.lock().unwrap_or_else(PoisonError::into_inner); + loop { + if let Some(price) = *price { + return price; + } + price = self + .done + .wait(price) + .unwrap_or_else(PoisonError::into_inner); + } + } +} + +/// `System.currentTimeMillis()`. +fn now_millis() -> i64 { + SystemTime::now() + .duration_since(UNIX_EPOCH) + .map_or(0, |since| { + i64::try_from(since.as_millis()).unwrap_or(i64::MAX) + }) +} + +impl StableMinGasPriceProvider { + /// `refresh_rate_millis` is how long a price is kept before the next + /// fetch, and a bitcoin priced outside `min_valid_price` to + /// `max_valid_price` is no price. + pub fn new( + source: Arc, + fallback: Arc, + min_stable_gas_price: i64, + refresh_rate_millis: i64, + min_valid_price: i64, + max_valid_price: i64, + ) -> Self { + Self { + shared: Arc::new(Shared { + source, + fallback, + min_stable_gas_price, + refresh_rate_millis, + min_valid_price, + max_valid_price, + num_of_failures: AtomicI32::new(0), + in_flight: Mutex::new(None), + last_min_gas_price: AtomicI64::new(0), + last_update_time_millis: AtomicI64::new(0), + }), + } + } + + /// rskj `getMinGasPrice(wait)`: once the refresh rate has passed since a + /// price was last had, a fetch is started, unless one is under way, and + /// its price is the answer if it is in hand, or once it is if `wait`. + /// Otherwise the last price had, or the fallback's while there is none. + pub fn get_min_gas_price(&self, wait: bool) -> i64 { + let shared = &self.shared; + let since = + now_millis().wrapping_sub(shared.last_update_time_millis.load(Ordering::SeqCst)); + if since >= shared.refresh_rate_millis { + let fetch = Shared::fetch_price_async(shared); + if wait || fetch.is_done() { + let price = fetch.get(); + debug!(target: "rustock::mining", "getMinGasPrice returning fetched minGasPrice: {price}"); + return price; + } + } + let price = shared.last_min_gas_price(); + debug!(target: "rustock::mining", "getMinGasPrice returning cached minGasPrice: {price}"); + price + } + + /// rskj `getMinGasPriceAsCoin(wait)`. + pub fn get_min_gas_price_as_coin(&self, wait: bool) -> U256 { + as_coin(self.get_min_gas_price(wait)) + } + + /// rskj `valueBetweenConfiguredRange`. + pub fn value_between_configured_range(&self, value: i64) -> bool { + self.shared.value_between_configured_range(value) + } +} + +impl MinGasPriceProvider for StableMinGasPriceProvider { + fn min_gas_price(&self) -> i64 { + self.get_min_gas_price(false) + } + + fn provider_type(&self) -> MinGasPriceProviderType { + self.shared.source.provider_type() + } +} + +impl Shared { + /// rskj `getLastMinGasPrice`. + fn last_min_gas_price(&self) -> i64 { + let last = self.last_min_gas_price.load(Ordering::SeqCst); + if last > 0 { + last + } else { + self.fallback.min_gas_price() + } + } + + /// rskj `calculateMinGasPriceBasedOnBtcPrice`, in Java's `long` + /// arithmetic. + fn calculate_min_gas_price_based_on_btc_price(&self, btc_value: i64) -> i64 { + if self.min_stable_gas_price == 0 || btc_value == 0 { + return 0; + } + self.min_stable_gas_price.wrapping_div(btc_value) + } + + /// rskj `fetchPriceAsync`: the fetch under way, or a new one on a thread + /// of its own. + fn fetch_price_async(this: &Arc) -> Arc { + let mut in_flight = this + .in_flight + .lock() + .unwrap_or_else(PoisonError::into_inner); + if let Some(fetch) = in_flight.as_ref() { + debug!(target: "rustock::mining", "fetchPriceAsync skipped as there is already price fetching in progress..."); + return fetch.clone(); + } + let fetch = Arc::new(Fetch::default()); + *in_flight = Some(fetch.clone()); + drop(in_flight); + debug!(target: "rustock::mining", "fetchPriceAsync..."); + + let shared = this.clone(); + let theirs = fetch.clone(); + let spawned = std::thread::Builder::new() + .name("stable-min-gas-price".into()) + .spawn(move || { + let response = shared.fetch_price_sync(); + // `priceResponse.orElse(getLastMinGasPrice())`: Java reads the + // last price first, whatever the response. + let last = shared.last_min_gas_price(); + theirs.complete(response.unwrap_or(last)); + *shared + .in_flight + .lock() + .unwrap_or_else(PoisonError::into_inner) = None; + }); + if let Err(e) = spawned { + error!(target: "rustock::mining", "Min gas price fetching could not start: {e}"); + *this + .in_flight + .lock() + .unwrap_or_else(PoisonError::into_inner) = None; + fetch.complete(this.last_min_gas_price()); + } + fetch + } + + /// rskj `fetchPriceSync`. + fn fetch_price_sync(&self) -> Option { + debug!(target: "rustock::mining", "fetchPriceSync..."); + let response = + std::panic::catch_unwind(AssertUnwindSafe(|| self.source.btc_exchange_rate())) + .unwrap_or(None); + if let Some(price) = response.filter(|price| self.value_between_configured_range(*price)) { + let result = self.calculate_min_gas_price_based_on_btc_price(price); + self.last_min_gas_price.store(result, Ordering::SeqCst); + self.last_update_time_millis + .store(now_millis(), Ordering::SeqCst); + self.num_of_failures.store(0, Ordering::SeqCst); + debug!( + target: "rustock::mining", + "fetchPriceSync completed with priceResponse: {price}, lastMinGasPrice: {result}" + ); + return Some(result); + } + let failed_attempts = self + .num_of_failures + .fetch_add(1, Ordering::SeqCst) + .wrapping_add(1); + if failed_attempts >= ERR_NUM_OF_FAILURES { + error!( + target: "rustock::mining", + "Gas price was not updated as it was not possible to obtain valid price from provider. \ + Check your provider setup. Number of failed attempts: {failed_attempts}" + ); + } else { + warn!( + target: "rustock::mining", + "Gas price was not updated as it was not possible to obtain valid price from provider. \ + Number of failed attempts: {failed_attempts}" + ); + } + None + } + + fn value_between_configured_range(&self, value: i64) -> bool { + if value >= self.min_valid_price && value <= self.max_valid_price { + return true; + } + warn!( + target: "rustock::mining", + "Gas price value {value} is not in the valid range {} - {}", + self.min_valid_price, + self.max_valid_price + ); + false + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::sync::atomic::AtomicUsize; + + // ── rskj FixedMinGasPriceProviderTest ─────────────────────────────────── + + /// rskj `testConstructorAndGetMinGasPrice`. + #[test] + fn test_constructor_and_get_min_gas_price() { + assert_eq!(FixedMinGasPriceProvider::new(100).min_gas_price(), 100); + } + + /// rskj `testGetType`. + #[test] + fn test_get_type() { + let provider = FixedMinGasPriceProvider::new(100); + assert_eq!(provider.provider_type(), MinGasPriceProviderType::Fixed); + } + + /// rskj `testGetMinGasPriceAsCoin`. + #[test] + fn test_get_min_gas_price_as_coin() { + let provider = FixedMinGasPriceProvider::new(50); + assert_eq!(provider.min_gas_price_as_coin(), U256::from(50)); + } + + /// A price below zero steers as zero does, to the bottom of the band. + #[test] + fn a_price_below_zero_reads_as_zero() { + assert_eq!(as_coin(-5), U256::ZERO); + assert_eq!(as_coin(i64::MAX), U256::from(i64::MAX)); + } + + // ── rskj StableMinGasPriceProviderTest ────────────────────────────────── + + const FALLBACK_PRICE: i64 = 10; + + /// The test's fallback, a fixed price of 10 that counts how often it is + /// asked, as the test's spy does. + #[derive(Debug, Default)] + struct CountedFallback { + asked: AtomicUsize, + } + + impl MinGasPriceProvider for CountedFallback { + fn min_gas_price(&self) -> i64 { + self.asked.fetch_add(1, Ordering::SeqCst); + FALLBACK_PRICE + } + + fn provider_type(&self) -> MinGasPriceProviderType { + MinGasPriceProviderType::Fixed + } + } + + /// `TestStableMingGasPriceProvider`'s exchange rate: a bitcoin at 15, or + /// what a test says, counting how often it is asked. + #[derive(Debug)] + struct TestSource { + rate: Option, + asked: AtomicUsize, + } + + impl BtcExchangeRateSource for TestSource { + fn btc_exchange_rate(&self) -> Option { + self.asked.fetch_add(1, Ordering::SeqCst); + self.rate + } + + fn provider_type(&self) -> MinGasPriceProviderType { + MinGasPriceProviderType::Fixed + } + } + + /// `new TestStableMingGasPriceProvider(fallBackProvider, 100, + /// Duration.ofSeconds(10))`, which takes a bitcoin priced 10 to 100. + fn provider( + rate: Option, + ) -> ( + StableMinGasPriceProvider, + Arc, + Arc, + ) { + let fallback = Arc::new(CountedFallback::default()); + let source = Arc::new(TestSource { + rate, + asked: AtomicUsize::new(0), + }); + let provider = + StableMinGasPriceProvider::new(source.clone(), fallback.clone(), 100, 10_000, 10, 100); + (provider, fallback, source) + } + + /// rskj `testGetMinGasPrice`: 100 over a bitcoin at 15 is 6, and the + /// fallback isn't asked. + #[test] + fn test_get_min_gas_price() { + let (provider, fallback, _) = provider(Some(15)); + assert_eq!(provider.get_min_gas_price(true), 6); + assert_eq!(fallback.asked.load(Ordering::SeqCst), 0); + } + + /// rskj `GetMinGasPriceUsesFallbackWhenReturnIsNull`. + #[test] + fn get_min_gas_price_uses_fallback_when_return_is_null() { + let (provider, fallback, _) = provider(None); + assert_eq!(provider.get_min_gas_price(true), FALLBACK_PRICE); + assert_eq!(fallback.asked.load(Ordering::SeqCst), 1); + } + + /// rskj `whenRequestingTheValueTwiceCachedValueIsUsed`: the second asking + /// takes the first's fetch, or the price it brought. + #[test] + fn when_requesting_the_value_twice_cached_value_is_used() { + let (provider, _, source) = provider(Some(15)); + provider.min_gas_price(); + assert_eq!(provider.get_min_gas_price(true), 6); + assert_eq!(source.asked.load(Ordering::SeqCst), 1); + } + + /// rskj `testGetMinGasPriceAsCoin`. + #[test] + fn test_get_min_gas_price_as_coin_of_a_stable_price() { + let (provider, _, _) = provider(Some(15)); + assert_eq!(provider.get_min_gas_price_as_coin(true), U256::from(6)); + } + + /// rskj `fallBackPriceIsReturnedWhenProvidedValueIsAboveMaxRange`. + #[test] + fn fall_back_price_is_returned_when_provided_value_is_above_max_range() { + let (provider, _, _) = provider(Some(500)); + assert!(!provider.value_between_configured_range(500)); + assert_eq!(provider.get_min_gas_price(true), FALLBACK_PRICE); + } + + /// rskj `fallBackPriceIsReturnedWhenProvidedValueIsBelowMinRange`. + #[test] + fn fall_back_price_is_returned_when_provided_value_is_below_min_range() { + let (provider, _, _) = provider(Some(5)); + assert!(!provider.value_between_configured_range(5)); + assert_eq!(provider.get_min_gas_price(true), FALLBACK_PRICE); + } + + /// A price had is kept for the refresh rate: asked again, the provider + /// answers it without a fetch, and a price fetched as zero (a minimum + /// stable price under a bitcoin's) gives way to the fallback between + /// fetches, as rskj's `getLastMinGasPrice` does. + #[test] + fn a_price_is_kept_and_zero_gives_way_to_the_fallback() { + let (provider, _, source) = provider(Some(15)); + assert_eq!(provider.get_min_gas_price(true), 6); + assert_eq!(provider.get_min_gas_price(true), 6); + assert_eq!(source.asked.load(Ordering::SeqCst), 1); + + let fallback = Arc::new(CountedFallback::default()); + let source = Arc::new(TestSource { + rate: Some(50), + asked: AtomicUsize::new(0), + }); + let provider = StableMinGasPriceProvider::new(source, fallback, 10, 10_000, 10, 100); + assert_eq!(provider.get_min_gas_price(true), 0, "10 over 50, fetched"); + assert_eq!( + provider.get_min_gas_price(true), + FALLBACK_PRICE, + "then kept as none" + ); + } +} diff --git a/crates/execution/src/mining/mod.rs b/crates/execution/src/mining/mod.rs index 1ad6f1e..31cee45 100644 --- a/crates/execution/src/mining/mod.rs +++ b/crates/execution/src/mining/mod.rs @@ -23,11 +23,16 @@ pub mod partition; pub mod workload; pub mod fork_detection; pub mod merkle; +pub mod min_gas_price; pub mod server; pub mod uncles; pub mod template; pub mod timed; +pub use min_gas_price::{ + BtcExchangeRateSource, FixedMinGasPriceProvider, MinGasPriceProvider, MinGasPriceProviderType, + StableMinGasPriceProvider, +}; pub use server::{ ChainAccess, CommitOutcome, ImportResult, MinerServer, MinerWork, RebuildPolicy, StoreChainAccess, SubmissionRateLimiter, SubmitError, SubmittedBlockInfo, SyncGate, @@ -36,9 +41,9 @@ pub use server::{ pub use uncles::{UNCLE_GENERATION_LIMIT, UNCLE_LIST_LIMIT, select_uncles}; pub use timed::{Pow, TimedMiner, TimedSchedule}; pub use template::{ - BlockTemplate, BlockTemplateBuilder, MiningConfig, NoPendingTransactions, PendingTransaction, - PendingTransactionSource, TemplateError, difficulty_to_target, next_minimum_gas_price, - remasc_transaction, + BlockTemplate, BlockTemplateBuilder, GAS_LIMIT_TARGET, MiningConfig, NoPendingTransactions, + PendingTransaction, PendingTransactionSource, REGTEST_GAS_LIMIT_TARGET, TemplateError, + difficulty_to_target, next_minimum_gas_price, remasc_transaction, }; #[cfg(test)] diff --git a/crates/execution/src/mining/partition.rs b/crates/execution/src/mining/partition.rs index c042a60..02fdb3c 100644 --- a/crates/execution/src/mining/partition.rs +++ b/crates/execution/src/mining/partition.rs @@ -648,6 +648,26 @@ mod tests { assert_eq!(h.edges(), vec![1], "the sequential sublist is not an edge"); } + /// rskj `addRemascTransaction` tests no gas: a sequential sublist + /// already full still takes REMASC, and counts the gas it used past + /// its limit. + #[test] + fn the_remasc_transaction_goes_in_a_full_sequential_sublist() { + let mut h = handler(); + let (_, sequential_limit) = sublist_gas_limits(BLOCK_GAS_LIMIT, 2, MIN_SEQUENTIAL); + let filling = TxFootprint { + sender: sender(1), + gas_limit: sequential_limit, + gas_used: sequential_limit, + reads: vec![], + writes: vec![], + }; + assert_eq!(h.add_to_sequential(0, &filling), Some(SEQUENTIAL)); + assert_eq!(h.add_remasc(1, 21_000), SEQUENTIAL); + assert_eq!(h.transactions_in_order(), vec![0, 1]); + assert_eq!(h.gas_used_in_sequential(), sequential_limit + 21_000); + } + /// A Bridge call goes to the sequential sublist and its keys are still /// recorded, so later transactions are placed knowing about it. #[test] diff --git a/crates/execution/src/mining/server.rs b/crates/execution/src/mining/server.rs index e832049..dbceaea 100644 --- a/crates/execution/src/mining/server.rs +++ b/crates/execution/src/mining/server.rs @@ -14,6 +14,7 @@ use rustock_core::validation::merged_mining::{ BLOCK_HEADER_HASH_SIZE, CoinbaseCompressionError, RSK_TAG, compress_coinbase, compute_coinbase_hash, find_last_subsequence, }; +use rustock_core::selection_rule::{should_we_add_this_block, Contender}; use rustock_core::{Block, Header}; use rustock_storage::BlockStore; use rustock_trie::TrieStore; @@ -27,6 +28,7 @@ use crate::mining::merkle::{self, MerkleProofError}; use crate::mining::template::{ BlockTemplate, BlockTemplateBuilder, PendingTransactionSource, TemplateError, }; +use crate::silent::SilentRecord; /// What `mnr_getWork` answers with. rskj `co.rsk.mine.MinerWork`. #[derive(Debug, Clone)] @@ -54,12 +56,19 @@ pub struct SubmittedBlockInfo { /// The outcomes rskj's `ImportResult` can report for a mined block. A miner /// reads this to tell "my block won" from "my block was valid but somebody -/// else's arrived first". +/// else's arrived first", and from "a rule refuses my block". #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum ImportResult { ImportedBest, ImportedNotBest, Exist, + /// rskj `BlockChainImpl.internalTryToConnect`: the store holds no parent + /// for the block. Nothing is stored. + NoParent, + /// rskj `BlockChainImpl.tryToConnect`: a block that passes its proof of + /// work and that a rule then refuses, or whose connecting fails in any + /// other way, a failing store included. Nothing is stored. + InvalidBlock, } impl ImportResult { @@ -68,6 +77,8 @@ impl ImportResult { ImportResult::ImportedBest => "IMPORTED_BEST", ImportResult::ImportedNotBest => "IMPORTED_NOT_BEST", ImportResult::Exist => "EXIST", + ImportResult::NoParent => "NO_PARENT", + ImportResult::InvalidBlock => "INVALID_BLOCK", } } } @@ -84,8 +95,6 @@ pub enum SubmitError { NoTagInCoinbase, #[error("no work outstanding for {hash}: it was never handed out, or it has aged out of the cache")] UnknownWork { hash: B256 }, - #[error("the parent of this work ({parent}) is no longer the executed head ({head:?}); the chain moved on")] - ParentNoLongerHead { parent: B256, head: Option }, /// rskj `MinerClientImpl.mineBlock` returns false while /// `hasBetterBlockToSync()`: a node still catching up does not mine. #[error("not mining: the node has a better block to sync")] @@ -98,7 +107,7 @@ pub enum SubmitError { Compression(#[from] CoinbaseCompressionError), #[error("building the merkle proof: {0}")] MerkleProof(#[from] MerkleProofError), - #[error("the completed block does not satisfy the merged-mining rule: {0}")] + #[error("the completed block fails a rule every node holds it to: {0}")] SelfCheck(rustock_core::validation::ValidationError), #[error("re-executing the completed block: {0}")] Execution(String), @@ -108,6 +117,17 @@ pub enum SubmitError { Template(#[from] TemplateError), } +/// How a submission answers a solved block whose proof of work holds and +/// that a rule then refuses, or that the node fails to connect. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum Refused { + /// As rskj answers a miner: OK, with `INVALID_BLOCK` + /// (`MinerServerImpl.processSolution`, `BlockChainImpl.tryToConnect`). + Answered, + /// As the node's own miner reads it: the error. + Failed, +} + /// How the node's chain state is read and advanced. Split out so the server /// can be driven by a test without a RocksDB store behind it. pub trait ChainAccess: Send + Sync { @@ -118,6 +138,8 @@ pub trait ChainAccess: Send + Sync { fn executed_head(&self) -> Result, String>; /// Whether a block with this hash is already stored. fn has_block(&self, hash: B256) -> Result; + /// The header stored under `hash`, named by that key. + fn header(&self, hash: B256) -> Result, String>; /// Uncles to include in a block mined on `parent_hash`. fn select_uncles(&self, block_number: u64, parent_hash: B256) -> Result, String>; /// Store a fully executed block and make it the head if it wins. @@ -131,6 +153,19 @@ pub trait ChainAccess: Send + Sync { state_root: B256, laps: &mut Laps, ) -> Result; + /// Store a block whose parent is no longer the executed head, executed + /// beside the chain against its own parent's state, and answer whether + /// it takes the head, without moving the head: the sync service makes + /// that switch, as it does for a peer's block executed beside the chain, + /// once the hook has told it of the block. `unlogged` holds the peg + /// decisions the block made that the Bridge logs no event for, which are + /// stored by height, so only once the chain takes the block up. + fn commit_mined_block_beside( + &self, + block: &Block, + unlogged: Vec, + laps: &mut Laps, + ) -> Result; } /// What [`ChainAccess::commit_mined_block`] reports: the import result and @@ -150,8 +185,8 @@ pub struct CommitOutcome { pub struct StoreChainAccess { pub store: Arc, pub trie_store: Arc, - /// Called with every block this commits, so a transaction pool can - /// drop what has just been mined. + /// Called with every block this commits, and what the commit made of + /// it, so a transaction pool can drop what has just been mined. /// /// A locally mined block does not travel the sync service's import /// path, which is the only place that called `remove_mined`. So @@ -164,7 +199,15 @@ 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". - pub on_committed: Option>, + /// + /// 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. + /// For a block beside the chain ([`ChainAccess::commit_mined_block_beside`]) + /// the head hasn't moved when it is called: the sync service it tells + /// makes the switch. + pub on_committed: Option>, /// Best blocks between trie flushes: rskj's /// `blockchain.flushNumberOfBlocks`, whose `BlockChainFlusher` flushes /// every tenth block a node mines or connects. 1 flushes every block. @@ -178,6 +221,14 @@ pub struct StoreChainAccess { pub flush_every: u64, /// Best blocks committed since the last flush. pub since_flush: std::sync::atomic::AtomicU64, + /// Where [`ChainAccess::commit_mined_block_beside`] leaves the peg + /// decisions the Bridge logs no event for that a block it keeps beside + /// the chain made, with the block's hash and height. They are stored by + /// height, so not before the chain takes the block up, which is the sync + /// service's to do: it holds them till then, as it holds those of a + /// peer's block executed beside the chain. They are left before the + /// block is stored, so that no path can take the block up without them. + pub leave_unlogged: Option) + Send + Sync>>, } impl StoreChainAccess { @@ -186,7 +237,7 @@ impl StoreChainAccess { pub fn new( store: Arc, trie_store: Arc, - on_committed: Option>, + on_committed: Option>, ) -> Self { Self::with_flush_every(store, trie_store, on_committed, 1) } @@ -194,7 +245,7 @@ impl StoreChainAccess { pub fn with_flush_every( store: Arc, trie_store: Arc, - on_committed: Option>, + on_committed: Option>, flush_every: u64, ) -> Self { Self { @@ -203,9 +254,20 @@ impl StoreChainAccess { on_committed, flush_every: flush_every.max(1), since_flush: std::sync::atomic::AtomicU64::new(0), + leave_unlogged: None, } } + /// Leave the peg decisions of a block kept beside the chain with `leave` + /// ([`Self::leave_unlogged`]). + pub fn with_leave_unlogged( + mut self, + leave: Arc) + Send + Sync>, + ) -> Self { + self.leave_unlogged = Some(leave); + self + } + /// Count a best block and say whether it is time to flush. The counter /// starts at 0, so with `flush_every` 10 the flushes fall on the 10th, /// 20th, ... block mined, as rskj's flusher counts. @@ -219,6 +281,61 @@ impl StoreChainAccess { false } } + + /// Store a mined block's header, body and total difficulty, each keyed + /// by its hash, and return the total. + /// + /// Deliberately not `put_block`: that writes the canonical + /// `number -> hash` pointer unconditionally, which would hand the + /// height to a block that lost to the head and leave the canonical + /// chain naming a block that is not on it. Header and body are safe to + /// store either way, being keyed by hash. + fn store_mined(&self, block: &Block, hash: B256) -> Result { + let parent_td = self + .store + .total_difficulty(block.header.parent_hash) + .map_err(|e| e.to_string())? + .unwrap_or(U256::ZERO); + let td = parent_td + cumulative_difficulty(block); + self.store + .put_header_with_hash(hash, &block.header) + .map_err(|e| e.to_string())?; + self.store + .put_body(hash, &block.transactions, &block.ommers) + .map_err(|e| e.to_string())?; + self.store + .put_total_difficulty(hash, td) + .map_err(|e| e.to_string())?; + Ok(td) + } + + /// Whether a mined block of total difficulty `td` takes the head. rskj + /// connects a mined block through `blockchain.tryToConnect`, as it does a + /// peer's: it takes the head by `SelectionRule.shouldWeAddThisBlock`, + /// which settles a tie in total difficulty by fees and then hashes. + fn takes_head(&self, block: &Block, hash: B256, td: U256) -> Result { + let (current_td, current) = match self.store.head().map_err(|e| e.to_string())? { + Some(head) => { + let td = self + .store + .total_difficulty(head) + .map_err(|e| e.to_string())? + .unwrap_or(U256::ZERO); + let current = self.store.contender(head, td).map_err(|e| e.to_string())?; + (td, current) + } + None => (U256::ZERO, None), + }; + let mined = Contender { + total_difficulty: td, + paid_fees: block.header.paid_fees, + hash, + }; + Ok(match ¤t { + Some(current) => should_we_add_this_block(&mined, current), + None => td > current_td, + }) + } } impl ChainAccess for StoreChainAccess { @@ -268,6 +385,16 @@ impl ChainAccess for StoreChainAccess { self.store.header(hash).map(|h| h.is_some()).map_err(|e| e.to_string()) } + fn header(&self, hash: B256) -> Result, String> { + // Named by the key it is stored under, as `mainchain` names each: a + // genesis from an rskj file hashes otherwise when recomputed. + let header = self.store.header(hash).map_err(|e| e.to_string())?; + Ok(header.map(|mut header| { + header.cached_hash = Some(hash); + header + })) + } + fn executed_head(&self) -> Result, String> { match self.store.exec_head().map_err(|e| e.to_string())? { Some((hash, _)) => Ok(Some(hash)), @@ -311,42 +438,11 @@ impl ChainAccess for StoreChainAccess { }); } - let parent_td = self - .store - .total_difficulty(block.header.parent_hash) - .map_err(|e| e.to_string())? - .unwrap_or(U256::ZERO); - let td = parent_td + cumulative_difficulty(block); - - // Deliberately not `put_block`: that writes the canonical - // `number -> hash` pointer unconditionally, which would hand the - // height to a block that lost the total-difficulty comparison two - // lines below and leave the canonical chain naming a block that is - // not on it. Header and body are safe to store either way, being - // keyed by hash. - self.store.put_header_with_hash(hash, &block.header).map_err(|e| e.to_string())?; - self.store - .put_body(hash, &block.transactions, &block.ommers) - .map_err(|e| e.to_string())?; - self.store.put_total_difficulty(hash, td).map_err(|e| e.to_string())?; - - let current_td = match self.store.head().map_err(|e| e.to_string())? { - Some(head) => self - .store - .total_difficulty(head) - .map_err(|e| e.to_string())? - .unwrap_or(U256::ZERO), - None => U256::ZERO, - }; - + let td = self.store_mined(block, hash)?; + let takes_head = self.takes_head(block, hash, td)?; switch_chain += laps.lap(); - if let Some(hook) = &self.on_committed { - hook(block); - } - on_best_block += laps.lap(); - - let result = if td > current_td { + let result = if takes_head { 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())?; switch_chain += laps.lap(); @@ -357,18 +453,80 @@ 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, result); + } + on_best_block += laps.lap(); + info!( target: "rustock::mining", "Mined block #{} {:?} imported: {}", number, hash, result.as_str() ); Ok(CommitOutcome { result, switch_chain, on_best_block }) } + + fn commit_mined_block_beside( + &self, + block: &Block, + unlogged: Vec, + laps: &mut Laps, + ) -> Result { + let hash = block.hash(); + let number = block.header.number; + if self.store.has_block(hash).map_err(|e| e.to_string())? { + return Ok(CommitOutcome { + result: ImportResult::Exist, + switch_chain: laps.lap(), + on_best_block: std::time::Duration::ZERO, + }); + } + // Its state durable before the store may adopt its branch on the + // strength of it, as the sync service makes durable a block it + // executes beside the chain. + self.trie_store.flush(); + // Its peg decisions where the sync service takes them from, before the + // block is stored and any path can take it onto the chain. + if !unlogged.is_empty() { + if let Some(leave) = &self.leave_unlogged { + leave(hash, number, unlogged); + } + } + let td = self.store_mined(block, hash)?; + // rskj answers what `internalTryToConnect` made of the block: + // `IMPORTED_BEST` where the selection rule takes it, and + // `IMPORTED_NOT_BEST` where it extends a chain beside the best one. + let result = if self.takes_head(block, hash, td)? { + ImportResult::ImportedBest + } else { + ImportResult::ImportedNotBest + }; + let switch_chain = laps.lap(); + // The peers are sent it, best or not, and the sync service weighs it. + if let Some(hook) = &self.on_committed { + hook(block, result); + } + let on_best_block = laps.lap(); + + info!( + target: "rustock::mining", + "Mined block #{} {:?} imported beside the chain: {}", number, hash, result.as_str() + ); + Ok(CommitOutcome { + result, + switch_chain, + on_best_block, + }) + } } struct ServerState { @@ -684,6 +842,16 @@ impl MinerServer { /// A full Bitcoin block, from which everything else is derivable. /// rskj `mnr_submitBitcoinBlock`. pub fn submit_bitcoin_block(&self, raw_block: &[u8]) -> Result { + self.submit_bitcoin_block_as(raw_block, Refused::Answered) + } + + /// [`MinerServer::submit_bitcoin_block`], answering a block a rule refuses + /// as `refused` says. + pub(crate) fn submit_bitcoin_block_as( + &self, + raw_block: &[u8], + refused: Refused, + ) -> Result { use bitcoin::consensus::Decodable; let mut reader = raw_block; @@ -702,6 +870,7 @@ impl MinerServer { &coinbase::encode_header(&btc_block.header), &coinbase_bytes, proof, + refused, ) } @@ -716,7 +885,7 @@ impl MinerServer { ) -> Result { let hashes = merkle::parse_wire_hashes(tx_hashes)?; let proof = merkle::proof_from_tx_hashes(&hashes)?; - self.complete_and_import(raw_header, raw_coinbase, proof) + self.complete_and_import(raw_header, raw_coinbase, proof, Refused::Answered) } /// Header, coinbase and the merkle branch itself -- the cheapest form, and @@ -731,7 +900,7 @@ impl MinerServer { ) -> Result { let hashes = merkle::parse_wire_hashes(merkle_hashes)?; let proof = merkle::proof_from_merkle_hashes(&hashes)?; - self.complete_and_import(raw_header, raw_coinbase, proof) + self.complete_and_import(raw_header, raw_coinbase, proof, Refused::Answered) } /// The common tail of all three submit paths: match the solution to the @@ -742,6 +911,7 @@ impl MinerServer { raw_btc_header: &[u8], raw_coinbase: &[u8], merkle_proof: Vec, + refused: Refused, ) -> Result { // The clock of the connect, rskj `internalTryToConnect`'s scope: // from matching the solution to its template to the block being best @@ -774,37 +944,95 @@ impl MinerServer { block.header.bitcoin_merged_mining_merkle_proof = Some(Bytes::from(merkle_proof)); debug_assert_eq!(hash_before, block.header.hash_for_merged_mining()); + // rskj `processSolution` holds the solution to the proof of work before + // it connects anything (`isValid`, `ProofOfWorkRule`), and answers + // ERROR when it fails: the same rule every peer will run, so failing it + // means the node built something it would itself reject. + MergedMiningRule { config: self.chain_config.clone() } + .validate(&block.header) + .map_err(SubmitError::SelfCheck)?; + + // Then `ethereum.addNewMinedBlock`, whose `blockchain.tryToConnect` + // the miner is answered OK with: what it made of the block. A block a + // rule refuses is `INVALID_BLOCK` there, and is not stored; so is one + // whose connecting fails in any other way, the node's own store + // included, as `BlockChainImpl.tryToConnect` catches every `Throwable` + // and answers `INVALID_BLOCK`. The node's own miner reads the error. + let completed_hash = block.hash(); + let number = block.header.number; + match self.connect_mined(&block, &template, &mut laps, &mut t) { + Err(error) if refused == Refused::Answered => { + warn!(target: "rustock::mining", "Mined block #{number} {completed_hash} is invalid: {error}"); + Ok(SubmittedBlockInfo { + block_imported_result: ImportResult::InvalidBlock, + block_hash: completed_hash, + block_included_height: number, + }) + } + connected => connected, + } + } + + /// rskj `BlockChainImpl.internalTryToConnect` for a solved block of this + /// node's work, in its order: a block already stored, the parent, the + /// rules, the run, the store. A rule that refuses the block and a store + /// that fails to read or write are both errors here; the caller answers + /// what rskj answers for them. + fn connect_mined( + &self, + block: &Block, + template: &BlockTemplate, + laps: &mut Laps, + t: &mut ConnectTimings, + ) -> Result { + let completed_hash = block.hash(); + let number = block.header.number; + let answer = |result| SubmittedBlockInfo { + block_imported_result: result, + block_hash: completed_hash, + block_included_height: number, + }; + // The same solution submitted twice: rskj's `addNewMinedBlock` // answers `EXIST` and the submission is `OK`. Checked before the // parent, which the first submission moved. - let completed_hash = block.hash(); if self.chain.has_block(completed_hash).map_err(SubmitError::Storage)? { - return Ok(SubmittedBlockInfo { - block_imported_result: ImportResult::Exist, - block_hash: completed_hash, - block_included_height: block.header.number, - }); + return Ok(answer(ImportResult::Exist)); } + // The work's parent may no longer be the executed head: the chain moved + // on while the solution was found. rskj connects such a block as it + // connects any (`BlockChainImpl.internalTryToConnect`): against its own + // parent's state, beside the chain, stored whether it is best or not, + // and answers what that made of it. let head = self.chain.executed_head().map_err(SubmitError::Storage)?; - if head != Some(template.parent_hash()) { - return Err(SubmitError::ParentNoLongerHead { - parent: template.parent_hash(), - head, - }); - } + let beside = head != Some(template.parent_hash()); t.preamble = laps.lap(); - // The same rule every peer will run. Failing here means the node built - // something it would itself reject, which is worth catching before it - // is stored and announced rather than after. - MergedMiningRule { config: self.chain_config.clone() } - .validate(&block.header) + // A parent the store doesn't hold is `NO_PARENT`, before any rule. + let Some(parent) = self + .chain + .header(template.parent_hash()) + .map_err(SubmitError::Storage)? + else { + return Ok(answer(ImportResult::NoParent)); + }; + // rskj then holds a mined block to every rule a peer's block is held + // to before running it (`BlockValidatorImpl`): the header's own and + // against its parent, the body's, the uncles' and fork detection. + // RSKIP179 among them: a solution found in a Bitcoin block timestamped + // five minutes or more from the template is one every peer rejects. + (rustock_core::validation::BitcoinTimestampRule { + config: self.chain_config.clone(), + }) + .validate(&block.header) + .map_err(SubmitError::SelfCheck)?; + rustock_core::validation::HeaderVerifier::default_rsk(self.chain_config.clone()) + .verify(&block.header, Some(&parent)) .map_err(SubmitError::SelfCheck)?; - // RSKIP179: a solution found in a Bitcoin block timestamped five - // minutes or more from the template is one every peer rejects. - rustock_core::validation::BitcoinTimestampRule { config: self.chain_config.clone() } - .validate(&block.header) + self.builder + .processor() + .validate_block(block) .map_err(SubmitError::SelfCheck)?; t.initial_validation = laps.lap(); @@ -814,14 +1042,19 @@ impl MinerServer { .map_err(SubmitError::Template)?; // `process_and_commit`, in its two halves, so the receipts can be // timed apart from execution. The timed processor reads the clock - // only when measuring; the plain one never does. + // only when measuring; the plain one never does. A block beside the + // chain keeps what it keys by hash, its receipts and transactions, as + // it runs; what it keys by height waits for the chain to take it up + // (`BlockProcessor::process_beside`). let processor = self.builder.processor(); let trie_store = self.builder.trie_store(); let mut process = crate::ProcessTimings::default(); - let executed = if laps.enabled() { - processor.process_block_timed(&block, &state_root, trie_store, &mut process) + let executed = if beside { + processor.process_beside(block, &state_root, trie_store) + } else if laps.enabled() { + processor.process_block_timed(block, &state_root, trie_store, &mut process) } else { - processor.process_block(&block, &state_root, trie_store) + processor.process_block(block, &state_root, trie_store) } .map_err(|e| SubmitError::Execution(e.to_string()))?; laps.lap(); @@ -829,14 +1062,24 @@ impl MinerServer { t.execute = process.execute; t.trie_save = process.trie_save; t.post_execute_validation = process.post_validation; + if beside { + let outcome = self + .chain + .commit_mined_block_beside(block, executed.unlogged_peg_events, laps) + .map_err(SubmitError::Storage)?; + // The head hasn't moved by this. If the block takes it, the sync + // service's switch moves it and tells the miner of its new best + // block, which builds new work then. + return Ok(answer(outcome.result)); + } processor - .commit_receipts(&block, &executed) + .commit_receipts(block, &executed) .map_err(|e| SubmitError::Execution(e.to_string()))?; t.save_receipts = laps.lap(); let outcome = self .chain - .commit_mined_block(&block, executed.state_root_hash, &mut laps) + .commit_mined_block(block, executed.state_root_hash, laps) .map_err(SubmitError::Storage)?; t.switch_chain = outcome.switch_chain; t.on_best_block = outcome.on_best_block; @@ -845,7 +1088,7 @@ impl MinerServer { if let (Some(sink), ImportResult::ImportedBest) = (&self.breakdown, outcome.result) { sink(&t.breakdown_line( block.header.number, - block.hash(), + completed_hash, block.transactions.len(), block.header.gas_used, )); @@ -856,11 +1099,7 @@ impl MinerServer { warn!(target: "rustock::mining", "Could not rebuild work after a submission: {e}"); } - Ok(SubmittedBlockInfo { - block_imported_result: outcome.result, - block_hash: block.hash(), - block_included_height: block.header.number, - }) + Ok(answer(outcome.result)) } /// The cached template a solution answers. Templates are not removed: diff --git a/crates/execution/src/mining/template.rs b/crates/execution/src/mining/template.rs index b42f8d0..2d8aa55 100644 --- a/crates/execution/src/mining/template.rs +++ b/crates/execution/src/mining/template.rs @@ -8,8 +8,9 @@ //! 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::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; @@ -17,6 +18,7 @@ use std::sync::Arc; use crate::hardfork::RskHardforkConfig; use crate::mining::fork_detection; +use crate::mining::min_gas_price::{FixedMinGasPriceProvider, MinGasPriceProvider}; use crate::processor::{BlockProcessor, ProcessError, compute_ommers_hash}; /// Knobs an operator sets for the blocks this node builds. None of them are @@ -28,23 +30,37 @@ pub struct MiningConfig { pub coinbase_address: Address, /// `extra_data`, capped at 32 bytes by the header rules. pub extra_data: Bytes, - /// Gas limit to steer towards, one step per block. `None` inherits the - /// parent's, which is always valid and never drifts. - pub gas_limit_target: Option, - /// Minimum gas price to steer towards, one step per block. `None` - /// inherits the parent's. - pub min_gas_price_target: Option, + /// rskj `targetgaslimit`: the gas limit to steer towards, one step per + /// block. A target under the chain's minimum gas limit is the minimum. + pub gas_limit_target: u64, + /// rskj `forcegaslimit`: steer towards the target whatever the parent + /// used. Off, a block's gas limit follows the gas its parent used, up to + /// the target (`GasLimitCalculator`). + pub force_gas_limit: bool, + /// Where the minimum gas price a block steers toward comes from, rskj's + /// `MinGasPriceProvider`: `miner.minGasPrice`, or `miner.stableGasPrice` + /// over it. + pub min_gas_price: Arc, /// How many templates to keep for submissions that arrive late. pub work_cache_size: usize, } +/// rskj's `targetgaslimit` where its configuration sets none +/// (`RskSystemProperties.getTargetGasLimit`). +pub const GAS_LIMIT_TARGET: u64 = 6_800_000; + +/// rskj's `targetgaslimit` on its regtest, which `regtest.conf` sets. +pub const REGTEST_GAS_LIMIT_TARGET: u64 = 7_500_000; + impl Default for MiningConfig { fn default() -> Self { Self { coinbase_address: Address::ZERO, extra_data: Bytes::new(), - gas_limit_target: None, - min_gas_price_target: None, + gas_limit_target: GAS_LIMIT_TARGET, + force_gas_limit: true, + // rskj's `miner.minGasPrice`, 0 in its `reference.conf`. + min_gas_price: Arc::new(FixedMinGasPriceProvider::new(0)), // rskj MinerServerImpl.CACHE_SIZE. work_cache_size: 20, } @@ -68,7 +84,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 @@ -103,6 +119,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. @@ -171,6 +191,14 @@ impl BlockTemplateBuilder { &self.processor } + /// This builder with `processor` in place of its own, for a test whose + /// chain the processor's configuration has to match. + #[cfg(test)] + pub(crate) fn with_processor_for_test(mut self, processor: Arc) -> Self { + self.processor = processor; + self + } + pub fn trie_store(&self) -> Arc { self.trie_store.clone() } @@ -190,19 +218,43 @@ 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)?; let number = parent.number + 1; + let ommers = self.admissible_uncles(parent, number, ommers); 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 minimum_gas_price = - next_minimum_gas_price(parent.minimum_gas_price, self.mining_config.min_gas_price_target); + // rskj `BlockToMineBuilder`, through `GasLimitCalculator`, with the + // chain's minimum gas limit and the miner's `GasLimitConfig`. + let gas_limit = next_gas_limit( + parent, + self.chain_config.min_gas_limit, + self.mining_config.gas_limit_target, + self.mining_config.force_gas_limit, + self.chain_config.gas_limit_bound_divisor, + ); + // rskj `MinimumGasPriceCalculator.calculate`: a step from the parent's + // toward the provider's price, taken as it stands now. + let minimum_gas_price = next_minimum_gas_price( + &parent.minimum_gas_price_value(), + self.mining_config.min_gas_price.min_gas_price_as_coin(), + ); - 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() { @@ -225,15 +277,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 { @@ -250,12 +300,14 @@ 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(), 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, @@ -263,7 +315,10 @@ impl BlockTemplateBuilder { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + 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. @@ -291,7 +346,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 @@ -312,14 +367,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, }); } @@ -333,8 +398,21 @@ 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 fork_data = fork_detection::calculate(mainchain); let hash_for_merged_mining = fork_detection::apply_to_hash(header.hash_for_merged_mining(), &fork_data); @@ -360,8 +438,47 @@ 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. + /// `uncles`, if a block at `number` on `parent` may name them, and none + /// otherwise: rskj's `BlockToMineBuilder.createBlock` builds a block + /// whose uncles fail the uncle rule again without them, rather than hand + /// out work on a block every node refuses. The rule is the one a block is + /// held to on import ([`BlockProcessor::validate_uncles`]), which the + /// mined block meets again before it is stored + /// (`MinerServer::complete_and_import`), so no work is offered on a block + /// this node would refuse. + fn admissible_uncles(&self, parent: &Header, number: u64, uncles: Vec
) -> Vec
{ + if uncles.is_empty() { + return uncles; + } + // The rule reads the block's number, its parent's hash and its + // uncles, and nothing else of its header. + let block = Block { + header: Header { + number, + parent_hash: parent.hash(), + cached_hash: None, + cached_hash_for_merged_mining: None, + ..parent.clone() + }, + transactions: Vec::new(), + ommers: uncles, + }; + match self.processor.validate_uncles(&block) { + Ok(()) => block.ommers, + Err(e) => { + tracing::warn!( + target: "rustock::mining", + "Building the block at #{number} without its {} uncle(s), which the uncle rule refuses: {e}", + block.ommers.len() + ); + Vec::new() + } + } + } + + /// 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 @@ -369,11 +486,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 @@ -383,12 +500,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() @@ -398,7 +514,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( @@ -409,7 +525,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!( @@ -433,14 +553,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 = header.gas_limit_as_gas().ok_or_else(|| { + crate::executor::ExecutionError::Evm(format!( + "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( rustock_core::validation::block_rules::TX_EXECUTION_THREADS, - gas_limit.saturating_to::(), + block_gas, self.chain_config.min_sequential_set_gas_limit, ); @@ -453,16 +581,20 @@ 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; } + // 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(), @@ -660,6 +792,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 @@ -668,13 +813,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()); @@ -740,48 +878,73 @@ fn timestamp_for_child(parent_timestamp: u64) -> u64 { now.max(parent_timestamp + 1) } -/// One step towards `target`, bounded by what the parent allows. +/// rskj `GasLimitCalculator.calculateBlockGasLimit`: the gas limit of a block +/// on `parent`, which this reads whole, as the parent rule does, and of any +/// size. A step is a `divisor`th of the parent's gas limit, the most the +/// parent rule lets a block move it. /// -/// 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 { - 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) - } else { - target.max(parent_limit.saturating_sub(delta)) +/// Forced, the gas limit steps towards `target` whatever the parent used. Not +/// forced, it falls a step and rises by the parent's gas used times 3/2 over +/// the divisor: up when the parent used more than two thirds of its gas +/// limit, down otherwise; never under the minimum or over the target, and +/// kept at the parent's where that would take it more than a step away. +pub(super) fn next_gas_limit( + parent: &Header, + min_gas_limit: u64, + target: u64, + force_target: bool, + divisor: u64, +) -> BigUint { + let parent_limit = parent.gas_limit_value(); + let delta_max = &parent_limit / divisor; + let min_gas_limit = BigUint::from(min_gas_limit); + // rskj takes a target under the minimum as the minimum. + let target = BigUint::from(target).max(min_gas_limit.clone()); + let lowest = &parent_limit - &delta_max; + let highest = &parent_limit + &delta_max; + if force_target { + return if target < parent_limit { + target.max(lowest) + } else { + target.min(highest) + }; } + let contribution = BigUint::from(parent.gas_used) * 3u8 / 2u8 / divisor; + let mut next = &lowest + contribution; + if next < min_gas_limit { + next = min_gas_limit; + } + if next > target { + next = target; + } + if next < lowest || next > highest { + next = parent_limit; + } + next } /// One step towards `target`, bounded by what the parent allows. /// /// 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 { - let Some(target) = target else { - return parent_price; - }; - let mut delta = parent_price / U256::from(100); - if delta.is_zero() { - delta = U256::from(1); - } - let upper = parent_price + delta; - let lower = parent_price.saturating_sub(delta); - target.clamp(lower, upper) +/// one wei when 1% rounds to nothing; a target inside that band is taken, +/// and one outside it the nearer edge. 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: U256) -> BigUint { + let (lower, upper) = PrevMinGasPriceRule::range(parent_price); + BigUint::from_bytes_be(&target.to_be_bytes::<32>()).clamp(lower, upper) } -/// rskj `DifficultyUtils.difficultyToTarget`. -pub fn difficulty_to_target(difficulty: U256) -> U256 { - if difficulty.is_zero() { +/// `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::MAX / difficulty + U256::from_be_slice(&bytes) } } + +/// 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 c57b3b7..9000c43 100644 --- a/crates/execution/src/mining/tests.rs +++ b/crates/execution/src/mining/tests.rs @@ -8,9 +8,10 @@ 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}; +use rustock_core::validation::{HeaderValidator, MergedMiningRule, ValidationError}; use rustock_core::Header; use rustock_storage::BlockStore; use rustock_trie::{MemoryTrieStore, TrieNode, TrieStore}; @@ -23,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 { @@ -32,10 +33,10 @@ 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, - 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 { @@ -45,10 +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, + 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, @@ -67,8 +78,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), @@ -85,6 +96,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, } } @@ -123,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)) } @@ -157,10 +169,21 @@ fn fixture_with( /// rskj's blocks runs (`--skip-pow-validation`), at a difficulty no nonce /// search could meet. fn fixture_without_pow(height: u64, flush_every: u64, trie_store: Arc) -> MiningFixture { + fixture_without_pow_gated(height, flush_every, trie_store, None) +} + +/// The same chain, its miner's server wired to the node's sync state as a +/// node wires it: work is refused while `gate` has a better block to fetch. +fn fixture_without_pow_gated( + height: u64, + flush_every: u64, + trie_store: Arc, + gate: Option>, +) -> MiningFixture { let mut config = (*test_chain_config()).clone(); config.skip_pow_validation = true; config.min_difficulty = U256::from(1u64 << 28); - fixture_on(height, Arc::new(config), U256::from(1u64 << 28), trie_store, flush_every) + fixture_on_gated(height, Arc::new(config), U256::from(1u64 << 28), trie_store, flush_every, gate) } /// Counts the flushes asked of the trie store it wraps. @@ -197,9 +220,27 @@ fn fixture_on( parent_difficulty: U256, trie_store: Arc, flush_every: u64, +) -> MiningFixture { + fixture_on_gated(height, chain_config, parent_difficulty, trie_store, flush_every, None) +} + +fn fixture_on_gated( + height: u64, + chain_config: Arc, + parent_difficulty: U256, + trie_store: Arc, + flush_every: u64, + gate: Option>, ) -> 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; @@ -207,6 +248,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(); @@ -223,7 +265,12 @@ fn fixture_on( let hardfork_cfg = RskHardforkConfig::all_active(33); let builder = BlockTemplateBuilder::new( - Arc::new(BlockProcessor::new(hardfork_cfg.clone(), store.clone())), + // With the chain's configuration, as a node wires its miner's + // processor: the uncles a template names are held to its rules. + Arc::new( + BlockProcessor::new(hardfork_cfg.clone(), store.clone()) + .with_chain_config(chain_config.clone()), + ), trie_store.clone(), chain_config.clone(), hardfork_cfg, @@ -238,12 +285,16 @@ fn fixture_on( None, flush_every, )); - let server = Arc::new(MinerServer::new( + let mut server = MinerServer::new( builder, chain, Arc::new(NoPendingTransactions), chain_config.clone(), - )); + ); + if let Some(gate) = gate { + server = server.with_sync_gate(gate); + } + let server = Arc::new(server); MiningFixture { server, store, trie_store, chain_config, parent, _dir: dir } } @@ -252,9 +303,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 { @@ -332,7 +384,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, reference_merkle_root(&txids), "{count} transactions"); + assert_eq!( + root, + Some(reference_merkle_root(&txids)), + "{count} transactions" + ); } } @@ -355,7 +411,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] @@ -404,7 +460,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,12 +474,48 @@ 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"); } +/// rskj `ForkDataDetectionCalculatorTest.calculateWithDivisibleBy64height`: +/// mining block 512, a multiple of 64, the first seven bytes are the least +/// significant byte of the hash of the Bitcoin header of the blocks 63, 127, +/// 191, 255, 319, 383 and 447 below the best, counting the best as 0; then +/// the uncle count, none here, and the height. rskj's test names 446 for the +/// last, where `ForkDetectionDataCalculator.buildCommitToParentsVector` reads +/// 447 (`bestBlockHeight - cpvStartHeight + i * 64`), and passes because its +/// chain gives twenty blocks one Bitcoin header: #64 and #65 share theirs. +/// Here each block has its own, so the block read is the one asserted. +#[test] +fn fork_detection_data_commits_to_a_bitcoin_header_every_64_blocks() { + let chain: Vec
= (0..fork_detection::REQUIRED_MAINCHAIN_BLOCKS) + .map(|i| { + let mut header = header_at(511 - i as u64, B256::ZERO, B256::ZERO); + // A Bitcoin header of each block's own, told apart by its version. + let mut bitcoin_header = [0u8; 80]; + bitcoin_header[..4].copy_from_slice(&(i as u32).to_le_bytes()); + header.bitcoin_merged_mining_header = Some(Bytes::copy_from_slice(&bitcoin_header)); + header + }) + .collect(); + let least_significant_byte = |i: usize| { + let raw = chain[i].bitcoin_merged_mining_header.as_ref().unwrap(); + let header: bitcoin::block::Header = bitcoin::consensus::deserialize(raw).unwrap(); + // The hash read as a number is little-endian: its low byte is first. + header.block_hash().as_byte_array()[0] + }; + + let data = fork_detection::calculate(&chain).unwrap(); + for (k, i) in [63, 127, 191, 255, 319, 383, 447].into_iter().enumerate() { + assert_eq!(data[k], least_significant_byte(i), "byte {k}: the block {i} below the best"); + } + assert_eq!(data[7], 0, "no uncles"); + assert_eq!(&data[8..12], &512u32.to_be_bytes(), "the height being mined"); +} + /// The uncle count is one byte, and a chain busy enough to overflow it must /// saturate rather than wrap -- a wrapped count would read as a *lower* uncle /// rate, which is the opposite of what happened. @@ -436,7 +528,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 @@ -556,25 +703,38 @@ 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 -- -/// both rules are bounds around the parent's value, and the parent's own value -/// is in range. This is what the builder does when no target is configured. +/// rskj's miner aims for a gas limit of 6,800,000 unless set, forced: on a +/// parent already there, as on this chain, a template keeps it; and it keeps +/// the parent's minimum gas price where its target is too. rskj +/// `GasLimitCalculatorTest.NextBlockGasLimitRemainsTheSame`, through the +/// template. #[test] -fn a_template_inherits_gas_limit_and_minimum_gas_price() { +fn a_template_on_rskjs_defaults_keeps_a_parent_already_there() { let fx = fixture(20); let header = fx.build().unwrap().block.header; + assert_eq!(fx.parent.gas_limit, U256::from(GAS_LIMIT_TARGET)); assert_eq!(header.gas_limit, fx.parent.gas_limit); assert_eq!(header.minimum_gas_price, fx.parent.minimum_gas_price); } @@ -585,6 +745,8 @@ fn a_template_is_never_timestamped_at_or_before_its_parent() { // the rule on its own. let mut fx = fixture(20); fx.parent.timestamp = u64::MAX / 2; + // A miner builds on a head its store holds: REMASC reads up from it. + fx.store.put_header(&fx.parent).unwrap(); let header = fx.build_on(&fx.parent).unwrap().block.header; assert!(header.timestamp > fx.parent.timestamp); } @@ -849,7 +1011,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(); @@ -891,44 +1075,431 @@ 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), 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), 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), 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), 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, 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), 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. +/// rskj `cantGetMGPtoBeNegative`: a target below zero, which a provider's +/// price can be, reads as zero (`as_coin`), and 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); + use super::min_gas_price::as_coin; + assert_eq!( + next_minimum_gas_price(&BigUint::ZERO, as_coin(-100)), + BigUint::ZERO + ); + assert_eq!( + next_minimum_gas_price(&BigUint::from(1000u32), as_coin(-100)), + BigUint::from(990u32) + ); +} + +/// rskj `GasLimitCalculator`, forced: one step towards the target, at most a +/// 1024th of the parent's gas limit, read whole: past 256 bits, where the +/// typed field holds `U256::MAX`, the step is as exact. +#[test] +fn the_gas_limit_steps_a_1024th_at_any_size() { + use super::template::next_gas_limit; + let step_by = |gas_limit: &BigUint, target: u64, divisor: u64| { + let mut parent = header_at(1, B256::ZERO, B256::ZERO); + parent.set_gas_limit(gas_limit); + next_gas_limit(&parent, 0, target, true, divisor) + }; + let step = |gas_limit: &BigUint, target: u64| step_by(gas_limit, target, 1024); + let start = BigUint::from(1_024_000u32); + assert_eq!(step(&start, u64::MAX), BigUint::from(1_025_000u32)); + assert_eq!(step(&start, 0), BigUint::from(1_023_000u32)); + assert_eq!(step(&start, 1_024_500), BigUint::from(1_024_500u32)); + assert_eq!(step(&start, 1_024_000), start); + // The chain's own divisor: a 2048th steps half as far. + assert_eq!(step_by(&start, u64::MAX, 2048), BigUint::from(1_024_500u32)); + + let two = BigUint::from(2u8); + let past = two.pow(300); + assert_eq!(step(&past, u64::MAX), &past - two.pow(290)); +} + +// ── rskj GasLimitCalculatorTest ─────────────────────────────────────── + +/// The minimum gas limit rskj's test reads, `Constants.regtest()`'s. +const RSKJ_MIN_GAS_LIMIT: u64 = 3_000_000; +/// The target it reads, `TestSystemProperties.getTargetGasLimit()`: rskj's +/// regtest's, from `regtest.conf`. +const RSKJ_TEST_TARGET: u64 = REGTEST_GAS_LIMIT_TARGET; + +/// `GasLimitCalculator.calculateBlockGasLimit(parentGasLimit, parentGasUsed, +/// minGasLimit, targetGasLimit, forceTarget)`. +fn calculate_block_gas_limit( + parent_gas_limit: u64, + parent_gas_used: u64, + min_gas_limit: u64, + target: u64, + force_target: bool, +) -> u64 { + use super::template::next_gas_limit; + let mut parent = header_at(1, B256::ZERO, B256::ZERO); + parent.set_gas_limit(&BigUint::from(parent_gas_limit)); + parent.gas_used = parent_gas_used; + let next = next_gas_limit(&parent, min_gas_limit, target, force_target, 1024); + u64::try_from(next).unwrap() +} + +/// `validByConsensus`, which reads ethereumj's `ParentGasLimitRule(1024)`: a +/// gas limit within a 1024th of the parent's, either way. The rule a block is +/// held to, rskj's `BlockParentGasLimitRule`, bounds it below by `parent * +/// 1023 / 1024`, a gas lower where the 1024th leaves a remainder; whatever +/// the first takes, the second must take too, and this checks that as well. +fn valid_by_consensus(new_gas: u64, parent_gas: u64) -> bool { + use rustock_core::validation::{BlockParentGasLimitRule, ParentHeaderValidator}; + let delta = parent_gas / 1024; + let within = parent_gas - delta <= new_gas && new_gas <= parent_gas + delta; + if within { + let mut parent = header_at(1, B256::ZERO, B256::ZERO); + parent.set_gas_limit(&BigUint::from(parent_gas)); + let mut header = header_at(2, parent.hash(), B256::ZERO); + header.set_gas_limit(&BigUint::from(new_gas)); + let rule = BlockParentGasLimitRule { + config: test_chain_config(), + }; + assert_eq!( + rule.validate_with_parent(&header, &parent), + Ok(()), + "{new_gas} on {parent_gas}" + ); + } + within +} + +/// rskj `NextBlockGasLimitIsDecreasedByAFactor`: not forced, a parent that +/// used nothing steps down a 1024th. +#[test] +fn next_block_gas_limit_is_decreased_by_a_factor() { + let parent = RSKJ_MIN_GAS_LIMIT + 21_000; + let new = calculate_block_gas_limit(parent, 0, RSKJ_MIN_GAS_LIMIT, RSKJ_TEST_TARGET, false); + assert!(new < parent); + assert_eq!(new, parent - parent / 1024); +} + +/// rskj `NextBlockGasLimitIsNotDecreasedLowerThanMinGasLimit`. +#[test] +fn next_block_gas_limit_is_not_decreased_lower_than_min_gas_limit() { + let min = RSKJ_MIN_GAS_LIMIT; + assert_eq!( + calculate_block_gas_limit(min, 0, min, RSKJ_TEST_TARGET, false), + min + ); +} + +/// rskj `NextBlockGasLimitIsIncreasedBasedOnGasUsed`: 3,500,000 less a +/// 1024th, plus 3,000,000 times 3/2 over 1024. +#[test] +fn next_block_gas_limit_is_increased_based_on_gas_used() { + let new = calculate_block_gas_limit(3_500_000, 3_000_000, 0, RSKJ_TEST_TARGET, false); + assert_eq!(new, 3_500_977); +} + +/// rskj `NextBlockGasLimitIsIncreasedBasedOnFullGasUsed`. +#[test] +fn next_block_gas_limit_is_increased_based_on_full_gas_used() { + let new = calculate_block_gas_limit(3_500_000, 3_500_000, 0, RSKJ_TEST_TARGET, false); + assert_eq!(new, 3_501_709); +} + +/// rskj `NextBlockGasLimitIsNotIncreasedMoreThanTargetGasLimit`. +#[test] +fn next_block_gas_limit_is_not_increased_more_than_target_gas_limit() { + let target = RSKJ_TEST_TARGET; + assert_eq!( + calculate_block_gas_limit(target, target, 0, target, false), + target + ); +} + +/// rskj `NextBlockGasLimitRemainsTheSame`. +#[test] +fn next_block_gas_limit_remains_the_same() { + let target = RSKJ_TEST_TARGET; + let new = calculate_block_gas_limit(target, 0, target, target, true); + assert_eq!(new, target); + assert!(valid_by_consensus(new, target)); +} + +/// rskj `NextBlockGasLimitIsIncreasedByMaximumValue`. +#[test] +fn next_block_gas_limit_is_increased_by_maximum_value() { + let min = RSKJ_MIN_GAS_LIMIT; + let new = calculate_block_gas_limit(min, 0, min, RSKJ_TEST_TARGET, true); + let new2 = calculate_block_gas_limit(min, min, min, RSKJ_TEST_TARGET, true); + assert_eq!(new, new2); + assert_eq!(new, min + min / 1024); + assert!(valid_by_consensus(new, min)); + assert!(valid_by_consensus(new2, min)); + assert!(!valid_by_consensus(new + 1, min)); +} + +/// rskj `NextBlockGasLimitIsIncreasedToTarget`. +#[test] +fn next_block_gas_limit_is_increased_to_target() { + let min = RSKJ_MIN_GAS_LIMIT; + let target = min + 1; + let new = calculate_block_gas_limit(min, 0, min, target, true); + let new2 = calculate_block_gas_limit(min, min, min, target, true); + assert_eq!(new, target); + assert_eq!(new, new2); + assert!(valid_by_consensus(new, min)); + assert!(valid_by_consensus(new2, min)); +} + +/// rskj `NextBlockGasLimitIsDecreasedToTarget`: a target less than a step +/// below the parent's gas limit is reached in one block, whatever gas the +/// parent used. +#[test] +fn next_block_gas_limit_is_decreased_to_target() { + let min = RSKJ_MIN_GAS_LIMIT; + let target = min + 1; + let used = target + 1; + let new = calculate_block_gas_limit(used, used, min, target, true); + let new2 = calculate_block_gas_limit(used, 0, min, target, true); + assert_eq!(new, target); + assert_eq!(new, new2); + assert!(valid_by_consensus(new, used)); + assert!(valid_by_consensus(new2, used)); +} + +/// rskj `NextBlockGasLimitIsDecreasedToMinimum`: a target under the minimum +/// is the minimum. +#[test] +fn next_block_gas_limit_is_decreased_to_minimum() { + let min = RSKJ_MIN_GAS_LIMIT; + let target = min - 1; + let used = min + 1; + let new = calculate_block_gas_limit(used, used, min, target, true); + let new2 = calculate_block_gas_limit(used, 0, min, target, true); + assert_eq!(new, min); + assert_eq!(new, new2); + assert!(valid_by_consensus(new, used)); + assert!(valid_by_consensus(new2, used)); +} + +/// rskj `NextBlockGasLimitIsDecreasedByMaximumValue`. +#[test] +fn next_block_gas_limit_is_decreased_by_maximum_value() { + let min = RSKJ_MIN_GAS_LIMIT; + let target = RSKJ_TEST_TARGET; + let used = target * 2; + let new = calculate_block_gas_limit(used, 0, min, target, true); + let new2 = calculate_block_gas_limit(used, used, min, target, true); + assert_eq!(new, new2); + assert_eq!(new, used - used / 1024); + assert!(valid_by_consensus(new, used)); + assert!(valid_by_consensus(new2, used)); + assert!(!valid_by_consensus(new - 1, used)); +} + +/// Not forced, on a parent past 256 bits: the target, a u64, is under any +/// gas limit there, and rskj keeps the parent's rather than drop more than +/// a step. +#[test] +fn not_forced_a_gas_limit_past_256_bits_is_kept() { + use super::template::next_gas_limit; + let past = BigUint::from(2u8).pow(300); + let mut parent = header_at(1, B256::ZERO, B256::ZERO); + parent.set_gas_limit(&past); + parent.gas_used = u64::MAX; + assert_eq!(next_gas_limit(&parent, 0, u64::MAX, false, 1024), past); +} + +/// rskj `GasLimitCalculator` takes a target under the chain's minimum gas +/// limit as the minimum: from a parent at 6,800,000 on a chain whose +/// minimum is 6,799,000, a target of 1 steps down only to 6,799,000, not +/// the 1024th that would leave a block below the minimum, which the +/// miner's own block check then refuses. +#[test] +fn a_gas_limit_target_under_the_minimum_is_the_minimum() { + let mut config = (*test_chain_config()).clone(); + config.min_gas_limit = 6_799_000; + let chain_config = Arc::new(config); + let fx = fixture_with(20, chain_config.clone(), U256::from(2)); + let hardfork_cfg = RskHardforkConfig::all_active(33); + let processor = BlockProcessor::new(hardfork_cfg.clone(), fx.store.clone()) + .with_chain_config(chain_config.clone()); + let builder = BlockTemplateBuilder::new( + Arc::new(processor), + fx.trie_store.clone(), + chain_config, + hardfork_cfg, + MiningConfig { + gas_limit_target: 1, + ..Default::default() + }, + ); + let parent = std::slice::from_ref(&fx.parent); + let template = builder + .build(parent, Vec::new(), &NoPendingTransactions) + .unwrap(); + assert_eq!(template.block.header.gas_limit, U256::from(6_799_000)); +} + +/// On a parent whose gas limit is past 256 bits, a template steps a 1024th +/// down from the parent's value towards rskj's target, 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_steps_from_its_value() { + use rustock_core::validation::{BlockParentGasLimitRule, ParentHeaderValidator}; + let mut fx = fixture(20); + let two = BigUint::from(2u8); + let value = two.pow(300); + fx.parent.set_gas_limit(&value); + fx.store.put_header(&fx.parent).unwrap(); + let header = fx.build_on(&fx.parent).unwrap().block.header; + assert_eq!(header.gas_limit_value(), &value - two.pow(290)); + 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(())); +} + +/// 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, U256::ZERO), lower); + assert_eq!(next_minimum_gas_price(&parent, U256::MAX), lower); + // 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), 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 steps 1% +/// down from the parent's value, toward rskj's default price of 0, as rskj's +/// miner writes it, which the parent rule takes. +#[test] +fn a_template_on_a_minimum_gas_price_past_256_bits_steps_from_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); + fx.store.put_header(&fx.parent).unwrap(); + let header = fx.build_on(&fx.parent).unwrap().block.header; + assert_eq!(header.minimum_gas_price_value(), &value - &value / 100u32); + assert_eq!(header.minimum_gas_price, U256::MAX); + assert_eq!( + PrevMinGasPriceRule.validate_with_parent(&header, &fx.parent), + Ok(()) + ); +} + +/// rskj's `BlockToMineBuilder` asks its `MinGasPriceProvider` for the +/// target as it builds: a price of 2,000 on a parent at 1,000 steps to +/// 1,010, and the default, rskj's `miner.minGasPrice` of 0, to 990. +#[test] +fn a_template_steers_its_minimum_gas_price_toward_the_providers() { + let mut fx = fixture(20); + fx.parent.set_minimum_gas_price(&BigUint::from(1000u32)); + fx.store.put_header(&fx.parent).unwrap(); + let built = |min_gas_price: Arc| { + let hardfork_cfg = RskHardforkConfig::all_active(33); + let processor = BlockProcessor::new(hardfork_cfg.clone(), fx.store.clone()) + .with_chain_config(fx.chain_config.clone()); + BlockTemplateBuilder::new( + Arc::new(processor), + fx.trie_store.clone(), + fx.chain_config.clone(), + hardfork_cfg, + MiningConfig { + min_gas_price, + ..Default::default() + }, + ) + .build( + std::slice::from_ref(&fx.parent), + Vec::new(), + &NoPendingTransactions, + ) + .unwrap() + .block + .header + .minimum_gas_price + }; + assert_eq!( + built(Arc::new(FixedMinGasPriceProvider::new(2000))), + U256::from(1010) + ); + assert_eq!( + built(MiningConfig::default().min_gas_price), + U256::from(990) + ); } /// A coinbase tagged with a hash this node never handed out cannot be matched @@ -1018,6 +1589,33 @@ fn notify_is_reported_once() { assert!(!fx.server.get_work().unwrap().notify); } +/// rskj `MinerServerTest.gasUnitInDollarsIsInitializedOkAtConstructor`, whose +/// name is not what it asserts: work built on a new parent is a notify +/// again, though the work before it was read. +#[test] +fn work_on_a_new_parent_is_a_notify_again() { + let fx = fixture(20); + let first = fx.server.get_work().unwrap(); + assert!(first.notify); + assert!(!fx.server.get_work().unwrap().notify); + let info = fx + .server + .submit_bitcoin_block(&bitcoin::consensus::serialize(&solve(&first))) + .unwrap(); + // The submission built the next work, on the block it imported. + let next = fx.server.get_work().unwrap(); + assert_eq!(next.parent_block_hash, info.block_hash); + assert!(next.notify, "work on a new parent must be pushed to the miners"); +} + +/// rskj `MinerServerTest.workWithNoTransactionsZeroFees`: work on a block with +/// no transaction but REMASC's pays its miner nothing. +#[test] +fn work_with_no_transactions_pays_the_miner_nothing() { + let fx = fixture(20); + assert_eq!(fx.server.get_work().unwrap().fees_paid_to_miner, U256::ZERO); +} + /// The three submit methods differ only in how much of the Bitcoin block the /// miner sends; they must all complete the same header. #[test] @@ -1126,14 +1724,57 @@ fn a_solution_that_misses_the_target_is_refused() { assert_eq!(fx.store.head().unwrap(), Some(fx.parent.hash())); } +/// rskj `MainNetMinerTest.submitBitcoinBlockInvalidBlockDoesntEliminateCache`: +/// a solution that misses the target is refused, and the work it answered +/// stays on offer, so a solution found after it is taken. rskj asserts the +/// refusal and leaves the rest commented out, its difficulty too high to +/// solve in a test; this chain's is low enough. +#[test] +fn a_refused_solution_leaves_its_work_answerable() { + let fx = fixture(20); + let work = fx.server.get_work().unwrap(); + let coinbase = coinbase::build_coinbase(&work.block_hash_for_merged_mining, 0xDEAD_BEEF); + let missed = (0..100_000u32) + .map(|nonce| coinbase::build_bitcoin_block(coinbase.clone(), nonce)) + .find(|block| U256::from_le_slice(block.header.block_hash().as_byte_array()) > work.target) + .expect("a nonce that misses the target"); + + let err = fx + .server + .submit_bitcoin_block(&bitcoin::consensus::serialize(&missed)) + .unwrap_err(); + assert!(matches!(err, SubmitError::SelfCheck(_)), "unexpected error: {err}"); + assert_eq!(fx.store.head().unwrap(), Some(fx.parent.hash())); + + let info = fx + .server + .submit_bitcoin_block(&bitcoin::consensus::serialize(&solve(&work))) + .expect("the work is still on offer"); + assert_eq!(info.block_imported_result, ImportResult::ImportedBest); +} + /// `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 -- @@ -1172,6 +1813,105 @@ fn a_losing_block_is_stored_but_not_made_canonical() { assert_eq!(fx.store.head().unwrap(), Some(rival_hash), "the head must not move"); } +/// A block of another miner on the parent a template built on, made the head +/// and the executed head with `total_difficulty` behind it: the chain moved +/// on while the work was out. +fn move_the_chain_on(fx: &MiningFixture, total_difficulty: U256) -> B256 { + let rival = header_at(fx.parent.number + 1, fx.parent.hash(), fx.parent.state_root); + let rival_hash = rival.hash(); + fx.store.put_header_with_hash(rival_hash, &rival).unwrap(); + fx.store + .put_canonical_hash(rival.number, rival_hash) + .unwrap(); + fx.store + .put_total_difficulty(rival_hash, total_difficulty) + .unwrap(); + fx.store.set_head(rival_hash).unwrap(); + fx.store + .set_exec_head(rival_hash, fx.parent.state_root) + .unwrap(); + rival_hash +} + +/// A solution whose work's parent is no longer the executed head -- the chain +/// moved on while it was mined -- is connected as rskj connects any block +/// (`BlockChainImpl.internalTryToConnect`): run against its own parent's +/// state, kept with its receipts and its total difficulty, and answered +/// `IMPORTED_NOT_BEST` where the head outweighs it. The head, the canonical +/// index and the executed head stay where they were. The node refused such a +/// solution, and the block was lost. +#[test] +fn a_solution_on_a_parent_the_chain_left_is_kept_beside_the_chain() { + let fx = fixture(20); + let work = fx.server.get_work().unwrap(); + let height = fx.parent.number + 1; + let rival_hash = move_the_chain_on(&fx, U256::from(1_000_000)); + + let raw = bitcoin::consensus::serialize(&solve(&work)); + let info = fx + .server + .submit_bitcoin_block(&raw) + .expect("a solution on a parent the chain left is still a block"); + + assert_eq!(info.block_imported_result, ImportResult::ImportedNotBest); + let mined = fx + .store + .header(info.block_hash) + .unwrap() + .expect("the block is kept: it may yet win"); + assert_eq!(mined.parent_hash, fx.parent.hash()); + assert_eq!( + fx.store.total_difficulty(info.block_hash).unwrap(), + Some(U256::from(height) + mined.difficulty), + "its total is its parent's and its own" + ); + assert!( + fx.server + .builder_for_test() + .processor() + .was_executed_locally(info.block_hash), + "it ran against its parent's state, and its receipts are kept by its hash" + ); + assert_eq!(fx.store.head().unwrap(), Some(rival_hash), "the head stays"); + assert_eq!(fx.store.canonical_hash(height).unwrap(), Some(rival_hash)); + assert_eq!( + fx.store.exec_head().unwrap(), + Some((rival_hash, fx.parent.state_root)), + "the executed head stays" + ); +} + +/// The same where the solution outweighs the head: answered `IMPORTED_BEST`, +/// as rskj answers it, and the switch left to the sync service, which weighs +/// a block kept beside the chain against the head once it is told of it, and +/// takes the head by its own way of switching branches. +#[test] +fn a_solution_on_a_parent_the_chain_left_that_outweighs_the_head_is_answered_best() { + let fx = fixture(20); + let work = fx.server.get_work().unwrap(); + // A rival that adds nothing to its parent's total: the solution's own + // difficulty outweighs it. + let rival_hash = move_the_chain_on(&fx, U256::from(fx.parent.number + 1)); + + let raw = bitcoin::consensus::serialize(&solve(&work)); + let info = fx + .server + .submit_bitcoin_block(&raw) + .expect("a solution on a parent the chain left is still a block"); + + assert_eq!(info.block_imported_result, ImportResult::ImportedBest); + assert!(fx.store.header(info.block_hash).unwrap().is_some()); + assert_eq!( + fx.store.head().unwrap(), + Some(rival_hash), + "the switch is the sync service's" + ); + assert_eq!( + fx.store.exec_head().unwrap(), + Some((rival_hash, fx.parent.state_root)) + ); +} + /// The round trip again, but deep enough that REMASC actually pays out. /// /// Everywhere else these tests mine block #21, and REMASC's @@ -1206,12 +1946,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)); @@ -1491,11 +2231,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 { @@ -1593,14 +2343,15 @@ mod partitioned_templates { fixture_with_forks(senders, RskHardforkConfig::all_active_parallel(33)) } - /// The same fixture, for the RSKIP-252 tests next door. They need the - /// funded senders and the short chain; what they vary is the head's - /// minimum gas price, which they rewrite themselves. + /// The same fixture, for the tests next door: the funded senders and the + /// short chain, its blocks at a `minimum` gas price the miner's own + /// matches. #[allow(clippy::type_complexity)] pub(super) fn fixture_for_cap_test( senders: u8, + minimum: u64, ) -> (BlockTemplateBuilder, Header, Vec, Arc, Arc) { - fixture_with_forks(senders, RskHardforkConfig::all_active_parallel(33)) + fixture_with_forks_priced(senders, RskHardforkConfig::all_active_parallel(33), minimum) } #[allow(clippy::type_complexity)] @@ -1612,13 +2363,47 @@ mod partitioned_templates { } /// `fixture_with_forks` on a chain whose blocks carry `minimum_gas_price`, - /// which the template inherits -- the RSKIP252 cap is 100 times it, and - /// there is no cap at zero. + /// the miner's own `miner.minGasPrice` too, so a template keeps it -- the + /// RSKIP252 cap is 100 times it, and there is no cap at zero. #[allow(clippy::type_complexity)] fn fixture_with_forks_priced( 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()); @@ -1635,6 +2420,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 @@ -1666,10 +2452,13 @@ 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"), + min_gas_price: Arc::new(FixedMinGasPriceProvider::new( + i64::try_from(minimum_gas_price).unwrap(), + )), ..Default::default() }, ); @@ -1832,6 +2621,193 @@ 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 + /// 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 @@ -1894,11 +2870,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"); @@ -1928,11 +2901,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"); @@ -1947,6 +2917,48 @@ mod partitioned_templates { .expect("and executes"); } + /// rskj `BlockToMineBuilder.getTransactions` reads RSKIP252 at the height + /// of the block it builds, the parent's plus one: a template for + /// fingerroot500's first block leaves a transaction over the cap out, and + /// one for the block before it mines it. The ladder ends at fingerroot500, + /// every later upgrade left out, and vetiver900's RSKIP536 with it, as + /// rskj's schedule would have it. + #[test] + fn the_miner_caps_prices_from_the_block_rskip252_starts_at() { + use crate::hardfork::{ConsensusRule, RskNetworkUpgrade}; + let ending_at_fingerroot = |height: u64| { + RskHardforkConfig::all_active(33) + .with_hardfork(RskNetworkUpgrade::Arrowhead631, None) + .with_hardfork(RskNetworkUpgrade::Vetiver900, None) + .with_consensus_rule(ConsensusRule::Rskip536, None) + .with_hardfork(RskNetworkUpgrade::Hop400, Some(0)) + .with_hardfork(RskNetworkUpgrade::Fingerroot500, Some(height)) + }; + // The fixture builds on #20, so every template here is #21. + for (fingerroot, mined) in [(21, false), (22, true)] { + let ladder = ending_at_fingerroot(fingerroot); + assert_eq!(ladder.rskip252_height(), fingerroot); + let (builder, parent, keys, _, _) = fixture_with_forks_priced(1, ladder, 100); + let pool = Pool(vec![transfer_with( + &keys[0], + Address::repeat_byte(0xF1), + 0, + 33, + 10_001, + 21_000, + )]); + let template = builder + .build(std::slice::from_ref(&parent), Vec::new(), &pool) + .expect("the template builds"); + assert_eq!(template.block.header.number, 21); + assert_eq!( + but_remasc(&template.block.transactions).len(), + usize::from(mined), + "with fingerroot500 at #{fingerroot}, a price over the cap is mined: {mined}" + ); + } + } + // ── invalid pending transactions: dropped and evicted ──────────── @@ -1976,11 +2988,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"); @@ -2052,6 +3061,32 @@ 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, _, blocks) = + fixture_with_forks(1, RskHardforkConfig::all_active(33)); + parent.set_minimum_gas_price(&BigUint::from(2u8).pow(300)); + // A miner builds on a head its store holds: REMASC reads up from it. + blocks.put_header(&parent).unwrap(); + 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 ────────────────────────── @@ -2121,27 +3156,14 @@ mod rskip252_cap { PendingTransaction { tx, sender, hash } } - /// The partitioning fixture leaves every header at a zero minimum gas - /// price, and `TxGasPriceCap` short-circuits on zero -- so a fixture that - /// did not set one would pass whatever the miner did. Rewrite the head - /// with `minimum`, keeping the store consistent with it. + /// The partitioning fixture on a chain at `minimum`, which the miner's own + /// price matches. `TxGasPriceCap` short-circuits on a zero minimum, so a + /// fixture that left it at zero would pass whatever the miner did. fn fixture_with_minimum( senders: u8, minimum: u64, ) -> (BlockTemplateBuilder, Header, Vec, Arc, Arc) { - let (builder, parent, keys, trie_store, store) = - super::partitioned_templates::fixture_for_cap_test(senders); - let mut head = parent.clone(); - head.minimum_gas_price = U256::from(minimum); - head.cached_hash = None; - head.cached_hash_for_merged_mining = None; - let hash = head.hash(); - store.put_header_with_hash(hash, &head).unwrap(); - store.put_canonical_hash(head.number, hash).unwrap(); - store.put_total_difficulty(hash, U256::from(head.number + 1)).unwrap(); - store.set_head(hash).unwrap(); - store.set_exec_head(hash, head.state_root).unwrap(); - (builder, head, keys, trie_store, store) + super::partitioned_templates::fixture_for_cap_test(senders, minimum) } #[test] @@ -2262,10 +3284,35 @@ mod mining_with_uncles { orphan } + /// A processor for chain 33 that skips proof of work, as a regtest node + /// run with rskj's `miner.server.skipPowValidation` does. Each uncle is + /// held to rskj's uncle rules, its own proof of work among them + /// (`BlockProcessor::validate_block_rules`), and the uncles offered here + /// are copies of a block nobody mined: with the work checked, they would + /// be refused before the rules these tests are about were reached. + fn processor_without_pow(store: Arc) -> BlockProcessor { + let mut config = rustock_core::config::ChainConfig::regtest(); + config.skip_pow_validation = true; + BlockProcessor::new(RskHardforkConfig::all_active_parallel(33), store) + .with_chain_config(Arc::new(config)) + } + + /// `builder` with [`processor_without_pow`], as a miner on a chain that + /// skips proof of work is wired: a builder whose processor checks the + /// work leaves those uncles out, as rskj's `BlockToMineBuilder` leaves + /// out any the uncle rule refuses. + fn builder_without_pow( + builder: BlockTemplateBuilder, + store: &Arc, + ) -> BlockTemplateBuilder { + builder.with_processor_for_test(Arc::new(processor_without_pow(store.clone()))) + } + #[test] fn a_block_sealed_with_an_uncle_is_one_the_importer_accepts() { let (builder, parent, _keys, trie_store, store) = - super::partitioned_templates::fixture_for_cap_test(1); + super::partitioned_templates::fixture_for_cap_test(1, 0); + let builder = builder_without_pow(builder, &store); let orphan = orphan_sibling_of_head(&store, &parent, 0xAA); assert_ne!( @@ -2298,7 +3345,7 @@ mod mining_with_uncles { // consensus *body* rules -- including the uncle rule -- live behind // `validate_block_rules`, which only the sync path calls. A miner // test that runs execution alone never sees them. - let processor = BlockProcessor::new(RskHardforkConfig::all_active_parallel(33), store); + let processor = processor_without_pow(store); processor .validate_block(&template.block) .expect("a block this miner seals with an uncle must pass the body rules"); @@ -2313,27 +3360,34 @@ mod mining_with_uncles { /// The RSKIP-252 lesson is that a validator run over a fixture it has /// nothing to say about looks exactly like a validator that approves. /// So: hand the miner an uncle that is plainly inadmissible -- the - /// canonical parent, which is an ancestor and never an uncle -- and - /// require the importer to refuse the result. If this passes, the - /// positive test above is evidence of something. + /// canonical parent, which is an ancestor and never an uncle. The miner + /// builds the block without it, as rskj's `BlockToMineBuilder.createBlock` + /// does; and the importer refuses a block that carries it anyway. If + /// both hold, the positive test above is evidence of something. #[test] fn the_importer_rejects_a_block_whose_uncle_is_inadmissible() { let (builder, parent, _keys, trie_store, store) = - super::partitioned_templates::fixture_for_cap_test(1); + super::partitioned_templates::fixture_for_cap_test(1, 0); + let builder = builder_without_pow(builder, &store); // An ancestor, not a sibling. `select_uncles` would never offer it. let template = builder .build(std::slice::from_ref(&parent), vec![parent.clone()], &NoTransactions) - .expect("the miner builds whatever it is handed"); - assert_eq!(template.block.ommers.len(), 1, "the premise: the block carries it"); - - let processor = BlockProcessor::new(RskHardforkConfig::all_active_parallel(33), store); + .expect("the miner builds, without the uncle it was handed"); + assert!(template.block.ommers.is_empty(), "the miner leaves it out"); + + let mut carrying = template.block.clone(); + carrying.ommers = vec![parent.clone()]; + carrying.header.uncle_count = 1; + carrying.header.ommers_hash = crate::processor::compute_ommers_hash(&carrying.ommers); + let processor = processor_without_pow(store); let _ = trie_store; - let outcome = processor.validate_block(&template.block); + let outcome = processor.validate_block(&carrying); assert!( - outcome.is_err(), - "an ancestor offered as an uncle must be refused -- if this is accepted, \ - the uncle rule is not being applied to mined blocks at all" + matches!(outcome, Err(ValidationError::UncleIsAncestor { .. })), + "an ancestor offered as an uncle must be refused as one -- if this is \ + accepted, the uncle rule is not being applied to mined blocks at all: \ + {outcome:?}" ); } @@ -2343,7 +3397,8 @@ mod mining_with_uncles { #[test] fn the_header_commits_to_the_uncles_it_carries() { let (builder, parent, _keys, _trie_store, store) = - super::partitioned_templates::fixture_for_cap_test(1); + super::partitioned_templates::fixture_for_cap_test(1, 0); + let builder = builder_without_pow(builder, &store); let _ = orphan_sibling_of_head(&store, &parent, 0xBB); let offered = select_uncles(&store, parent.number + 1, parent.hash()); @@ -2450,8 +3505,216 @@ fn by_default_every_mined_block_flushes() { } } -/// The thread mines on its clock: at a 100 ms median (the floor), a second -/// of running yields several blocks, and stopping ends it. +/// 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, result: ImportResult| { + let exec_head = store.exec_head().unwrap(); + let canonical = store.canonical_hash(1).unwrap(); + seen.lock().unwrap().push((exec_head, canonical, result)); + }) + }; + 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), ImportResult::ImportedBest)], + "the hook ran once, with the block the executed head and canonical" + ); +} + +/// rskj connects a mined block as it does a peer's, through +/// `SelectionRule.shouldWeAddThisBlock`: a mined block that ties the head in +/// total difficulty, with fees like the head's and the lower hash, takes the +/// head from the block held first, and one with the higher hash doesn't. +#[test] +fn a_mined_block_that_ties_the_head_goes_by_the_selection_rule() { + use rustock_core::selection_rule::is_this_block_hash_smaller; + + 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(); + + // Blocks at #1 on that parent, told apart by their extra data. Each has + // the total a block mined there gets: the parent's 1 and its own 2. + let sibling = |extra: u8| { + let mut header = header_at(1, parent_hash, root); + header.paid_fees = U256::from(1_000); + header.extra_data = Bytes::from(vec![extra]); + header + }; + let held_first = sibling(0); + let held_first_hash = held_first.hash(); + store + .put_header_with_hash(held_first_hash, &held_first) + .unwrap(); + store.put_canonical_hash(1, held_first_hash).unwrap(); + store + .put_total_difficulty(held_first_hash, U256::from(3)) + .unwrap(); + store.set_head(held_first_hash).unwrap(); + store.set_exec_head(held_first_hash, root).unwrap(); + + let told = Arc::new(std::sync::Mutex::new(Vec::new())); + let hook: Arc = { + let told = told.clone(); + Arc::new(move |block: &rustock_core::Block, result: ImportResult| { + told.lock().unwrap().push((block.hash(), result)); + }) + }; + let chain = StoreChainAccess::new(store.clone(), trie_store, Some(hook)); + let mined = |header: Header| rustock_core::Block { + header, + transactions: Vec::new(), + ommers: Vec::new(), + }; + let lower = (1..=u8::MAX) + .map(sibling) + .find(|header| is_this_block_hash_smaller(&header.hash(), &held_first_hash)) + .expect("an extra data that hashes below the head"); + let lower = mined(lower); + let outcome = chain + .commit_mined_block(&lower, root, &mut crate::breakdown::Laps::new(false)) + .unwrap(); + assert_eq!(outcome.result, ImportResult::ImportedBest); + assert_eq!(store.head().unwrap(), Some(lower.hash())); + assert_eq!(store.canonical_hash(1).unwrap(), Some(lower.hash())); + + let higher = (1..=u8::MAX) + .map(sibling) + .find(|header| is_this_block_hash_smaller(&lower.hash(), &header.hash())) + .expect("an extra data that hashes above the new head"); + let higher = mined(higher); + let outcome = chain + .commit_mined_block(&higher, root, &mut crate::breakdown::Laps::new(false)) + .unwrap(); + assert_eq!(outcome.result, ImportResult::ImportedNotBest); + assert_eq!(store.head().unwrap(), Some(lower.hash()), "the head stays"); + assert_eq!(store.canonical_hash(1).unwrap(), Some(lower.hash())); + assert_eq!( + *told.lock().unwrap(), + vec![ + (lower.hash(), ImportResult::ImportedBest), + (higher.hash(), ImportResult::ImportedNotBest) + ], + "the hook is told what each commit made of its block" + ); +} + +/// A block kept beside the chain leaves the peg decisions it made that the +/// Bridge logs no event for with `leave_unlogged`, with its hash and height, +/// before it is stored, so that no path takes it onto the chain without +/// them. A block that made none leaves nothing, and one already stored is +/// not left again. +#[test] +fn a_block_kept_beside_the_chain_leaves_its_peg_decisions_before_it_is_stored() { + use crate::silent::{SilentKind, SilentRecord}; + + 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(); + + let left = Arc::new(std::sync::Mutex::new(Vec::new())); + let leave: Arc) + Send + Sync> = { + let (store, left) = (store.clone(), left.clone()); + Arc::new(move |hash: B256, number: u64, records: Vec| { + let stored = store.has_block(hash).unwrap(); + left.lock().unwrap().push((hash, number, records, stored)); + }) + }; + let chain = StoreChainAccess::new(store.clone(), trie_store, None).with_leave_unlogged(leave); + let block = |extra: u8| { + let mut header = header_at(1, parent_hash, root); + header.extra_data = Bytes::from(vec![extra]); + rustock_core::Block { + header, + transactions: Vec::new(), + ommers: Vec::new(), + } + }; + let decision = SilentRecord { + kind: SilentKind::BtcTxMarkedProcessed, + block: 1, + tx_index: 0, + fields: Vec::new(), + }; + let laps = || crate::breakdown::Laps::new(false); + + let made = block(1); + chain + .commit_mined_block_beside(&made, vec![decision.clone()], &mut laps()) + .unwrap(); + assert_eq!( + *left.lock().unwrap(), + vec![(made.hash(), 1, vec![decision.clone()], false)], + "left with its hash and height, before it is stored" + ); + assert!(store.has_block(made.hash()).unwrap()); + + let none = block(2); + chain + .commit_mined_block_beside(&none, Vec::new(), &mut laps()) + .unwrap(); + let again = chain + .commit_mined_block_beside(&made, vec![decision], &mut laps()) + .unwrap(); + assert_eq!(again.result, ImportResult::Exist); + assert_eq!( + left.lock().unwrap().len(), + 1, + "nothing is left for a block that made none, or for one already stored" + ); +} + +/// 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())); @@ -2460,22 +3723,135 @@ 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"); +} + +/// rskj `TimedMinerClientSimpleTest.testMultipleStartStopCycles` and +/// `TimedMinerClientTest.canStartAndStopMultipleTimes`: the miner starts and +/// stops again and again. A start right after a stop, while the stopped +/// thread still sleeps on its draw, runs one thread, not the stopped one +/// beside the new: each holds a handle on the server, so they are counted. +#[test] +fn a_timed_miner_started_again_at_once_runs_one_thread() { + use std::time::{Duration, Instant}; + let fx = fixture_without_pow(20, 10, Arc::new(MemoryTrieStore::new())); + let miner = TimedMiner::new(fx.server.clone(), Duration::from_millis(100), Pow::Skip); + let without_thread = Arc::strong_count(&fx.server); + let threads_settle_at = |threads: usize, when: &str| { + let deadline = Instant::now() + Duration::from_secs(60); + while Arc::strong_count(&fx.server) != without_thread + threads { + assert!( + Instant::now() < deadline, + "{} threads 60 s {when}, not {threads}", + Arc::strong_count(&fx.server) - without_thread + ); + std::thread::sleep(Duration::from_millis(10)); + } + }; + for _ in 0..3 { + miner.start(); + miner.stop(); + assert!(!miner.is_running()); + miner.start(); + assert!(miner.is_running()); + threads_settle_at(1, "after a stop and a start"); + // Longer than the stopped thread sleeps before it reads its flag. + std::thread::sleep(Duration::from_millis(300)); + assert_eq!(Arc::strong_count(&fx.server), without_thread + 1, "the stopped thread ran on"); + miner.stop(); + threads_settle_at(0, "after a stop"); + } + // And a start after all that mines. + let before = miner.mined(); + miner.start(); + let deadline = Instant::now() + Duration::from_secs(60); + while miner.mined() == before { + assert!(Instant::now() < deadline, "no block in 60 s"); + std::thread::sleep(Duration::from_millis(10)); + } + miner.stop(); + threads_settle_at(0, "after the last stop"); +} + +/// rskj `TimedMinerClientMiningTest.handlesMiningErrorsGracefully`: a block +/// the miner can't build is logged, and the next draw comes as usual. Here +/// the work is refused because the node has a better block to fetch; once it +/// has it, the miner mines. +#[test] +fn a_timed_miner_rides_out_work_it_cannot_build() { + use std::sync::atomic::{AtomicBool, Ordering}; + use std::time::{Duration, Instant}; + struct Syncing(AtomicBool); + impl SyncGate for Syncing { + fn has_better_block_to_sync(&self) -> bool { + self.0.load(Ordering::SeqCst) + } + } + let syncing = Arc::new(Syncing(AtomicBool::new(true))); + let gate: Arc = syncing.clone(); + let fx = fixture_without_pow_gated(20, 10, Arc::new(MemoryTrieStore::new()), Some(gate)); + let miner = TimedMiner::new(fx.server.clone(), Duration::from_millis(100), Pow::Skip); + let without_thread = Arc::strong_count(&fx.server); + miner.start(); + // Draws come every 100 ms or so, and each fails. + std::thread::sleep(Duration::from_secs(1)); + assert!(miner.is_running(), "a block it couldn't build stopped the miner"); + assert_eq!(miner.mined(), 0); + assert_eq!(fx.store.head().unwrap(), Some(fx.parent.hash())); + + syncing.0.store(false, Ordering::SeqCst); + let deadline = Instant::now() + Duration::from_secs(60); + while miner.mined() == 0 { + assert!(Instant::now() < deadline, "no block in 60 s once synced"); + std::thread::sleep(Duration::from_millis(10)); + } + miner.stop(); + while Arc::strong_count(&fx.server) > without_thread { + assert!(Instant::now() < deadline, "still running after stop"); + std::thread::sleep(Duration::from_millis(10)); + } } /// A header whose recomputed hash is not the hash it is stored under: what a @@ -2621,3 +3997,255 @@ fn cumulative_difficulty_counts_the_uncles() { let alone = rustock_core::Block { header, transactions: Vec::new(), ommers: Vec::new() }; assert_eq!(cumulative_difficulty(&alone), U256::from(1_000)); } + +// ── the rules a mined block meets ───────────────────────────────────── + +mod own_block_rules { + use super::*; + + /// A block that lost to the fixture's head, on the head's parent. Mined + /// 100 s after that parent, it keeps the parent's difficulty where that + /// is the floor, as the difficulty rule wants, and so meets every rule an + /// uncle meets but proof of work. + fn losing_sibling(fx: &MiningFixture) -> Header { + let grandparent = fx.store.header(fx.parent.parent_hash).unwrap().unwrap(); + let mut sibling = header_at( + fx.parent.number, + fx.parent.parent_hash, + fx.parent.state_root, + ); + sibling.beneficiary = Address::repeat_byte(0xAA); + sibling.difficulty = grandparent.difficulty; + sibling.timestamp = grandparent.timestamp + 100; + fx.store + .put_header_with_hash(sibling.hash(), &sibling) + .unwrap(); + sibling + } + + fn template_naming(fx: &MiningFixture, uncles: Vec
) -> BlockTemplate { + fx.server + .builder_for_test() + .build( + std::slice::from_ref(&fx.parent), + uncles, + &NoPendingTransactions, + ) + .unwrap() + } + + /// rskj `BlockToMineBuilder.createBlock`: a block whose uncles fail the + /// uncle rule is built again without them. Where proof of work goes + /// unchecked the sibling is an uncle and stays; where it is checked, the + /// sibling, never mined, is none, and the block is built without it. + #[test] + fn a_template_leaves_out_uncles_the_rule_refuses() { + let unchecked = fixture_without_pow(20, 1, Arc::new(MemoryTrieStore::new())); + let sibling = losing_sibling(&unchecked); + let template = template_naming(&unchecked, vec![sibling.clone()]); + let named: Vec = template.block.ommers.iter().map(Header::hash).collect(); + assert_eq!(named, vec![sibling.hash()]); + assert_eq!(template.block.header.uncle_count, 1); + + let checked = fixture(20); + let sibling = losing_sibling(&checked); + let template = template_naming(&checked, vec![sibling]); + assert!(template.block.ommers.is_empty()); + assert_eq!(template.block.header.uncle_count, 0); + assert_eq!( + template.block.header.ommers_hash, + crate::processor::compute_ommers_hash(&[]) + ); + } + + /// rskj connects a mined block through `blockchain.tryToConnect`, which + /// holds it to the uncle rule as it holds a peer's block. An uncle the + /// block's parent turns out to have named already is used, and a block + /// naming it again is refused, not stored and made the best block. + #[test] + fn a_mined_block_naming_an_uncle_already_used_is_refused() { + let fx = fixture_without_pow(20, 1, Arc::new(MemoryTrieStore::new())); + let sibling = losing_sibling(&fx); + let work = fx.server.build_work().unwrap(); + // The work names the sibling; now the parent has named it too. + fx.store + .put_body(fx.parent.hash(), &[], std::slice::from_ref(&sibling)) + .unwrap(); + + let err = dev::submit_unsolved(&fx.server, &work).unwrap_err(); + assert!( + matches!( + err, + dev::MineError::Submit(SubmitError::SelfCheck( + ValidationError::UncleAlreadyUsed { .. } + )) + ), + "unexpected error: {err:?}" + ); + assert_eq!(fx.store.head().unwrap(), Some(fx.parent.hash())); + } + + /// And to the header's rules against its parent, as rskj's + /// `BlockValidatorImpl` holds it: here the minimum gas limit, which a + /// miner keeping its parent's 6,800,000 falls below on a chain whose + /// minimum is 7,000,000. rskj answers the miner OK, with what + /// `tryToConnect` made of the block, `INVALID_BLOCK` + /// (`MinerServerImpl.processSolution`), and stores nothing; the node's + /// own miner reads the rule's error. + #[test] + fn a_mined_block_below_the_minimum_gas_limit_is_refused() { + let mut config = (*test_chain_config()).clone(); + config.min_gas_limit = 7_000_000; + let fx = fixture_with(20, Arc::new(config), U256::from(2)); + let work = fx.server.get_work().unwrap(); + let raw = bitcoin::consensus::serialize(&solve(&work)); + + let info = fx.server.submit_bitcoin_block(&raw).unwrap(); + assert_eq!(info.block_imported_result, ImportResult::InvalidBlock); + assert_eq!(info.block_imported_result.as_str(), "INVALID_BLOCK"); + assert_eq!(info.block_included_height, fx.parent.number + 1); + assert_eq!(fx.store.head().unwrap(), Some(fx.parent.hash())); + + let err = fx + .server + .submit_bitcoin_block_as(&raw, crate::mining::server::Refused::Failed) + .unwrap_err(); + assert!( + matches!( + err, + SubmitError::SelfCheck(ValidationError::GasLimitBelowMinimum { .. }) + ), + "unexpected error: {err}" + ); + assert_eq!(fx.store.head().unwrap(), Some(fx.parent.hash())); + } + + /// What goes wrong in a store, for [`FaultyChain`]. + #[derive(Clone, Copy)] + enum Fault { + /// Every read answers; writing a mined block fails. + Write, + /// The store holds no header for the work's parent. + NoParent, + } + + /// The node's store, with a `Fault`. + struct FaultyChain { + inner: StoreChainAccess, + fault: Fault, + } + + impl ChainAccess for FaultyChain { + fn mainchain(&self, limit: usize) -> Result, String> { + self.inner.mainchain(limit) + } + fn executed_head(&self) -> Result, String> { + self.inner.executed_head() + } + fn has_block(&self, hash: B256) -> Result { + self.inner.has_block(hash) + } + fn header(&self, hash: B256) -> Result, String> { + match self.fault { + Fault::NoParent => Ok(None), + Fault::Write => self.inner.header(hash), + } + } + fn select_uncles( + &self, + block_number: u64, + parent_hash: B256, + ) -> Result, String> { + self.inner.select_uncles(block_number, parent_hash) + } + fn commit_mined_block( + &self, + block: &rustock_core::Block, + state_root: B256, + laps: &mut crate::breakdown::Laps, + ) -> Result { + match self.fault { + Fault::Write => Err("the disk is full".into()), + Fault::NoParent => self.inner.commit_mined_block(block, state_root, laps), + } + } + fn commit_mined_block_beside( + &self, + block: &rustock_core::Block, + unlogged: Vec, + laps: &mut crate::breakdown::Laps, + ) -> Result { + match self.fault { + Fault::Write => Err("the disk is full".into()), + Fault::NoParent => self.inner.commit_mined_block_beside(block, unlogged, laps), + } + } + } + + /// A miner on the fixture's chain whose store has `fault`. + fn faulty_miner(fx: &MiningFixture, fault: Fault) -> MinerServer { + let hardfork_cfg = RskHardforkConfig::all_active(33); + let builder = BlockTemplateBuilder::new( + Arc::new( + BlockProcessor::new(hardfork_cfg.clone(), fx.store.clone()) + .with_chain_config(fx.chain_config.clone()), + ), + fx.trie_store.clone(), + fx.chain_config.clone(), + hardfork_cfg, + MiningConfig { + coinbase_address: address!("00000000000000000000000000000000c0117b45"), + ..Default::default() + }, + ); + let inner = StoreChainAccess::new(fx.store.clone(), fx.trie_store.clone(), None); + MinerServer::new( + builder, + Arc::new(FaultyChain { inner, fault }), + Arc::new(NoPendingTransactions), + fx.chain_config.clone(), + ) + } + + /// rskj `BlockChainImpl.tryToConnect` answers `INVALID_BLOCK` for + /// whatever goes wrong as it connects a block, a store that fails + /// included, and the pool is answered OK with it + /// (`MinerServerImpl.processSolution`). The node's own miner reads the + /// store's error. + #[test] + fn a_mined_block_the_store_fails_to_take_is_answered_invalid() { + let fx = fixture(20); + let miner = faulty_miner(&fx, Fault::Write); + let work = miner.get_work().unwrap(); + let raw = bitcoin::consensus::serialize(&solve(&work)); + + let info = miner.submit_bitcoin_block(&raw).unwrap(); + assert_eq!(info.block_imported_result, ImportResult::InvalidBlock); + assert_eq!(info.block_included_height, fx.parent.number + 1); + assert_eq!(fx.store.head().unwrap(), Some(fx.parent.hash())); + + let err = miner + .submit_bitcoin_block_as(&raw, crate::mining::server::Refused::Failed) + .unwrap_err(); + assert!( + matches!(err, SubmitError::Storage(ref e) if e == "the disk is full"), + "unexpected error: {err}" + ); + } + + /// rskj `BlockChainImpl.internalTryToConnect` answers `NO_PARENT` for a + /// block whose parent its store doesn't hold, before any rule, and + /// stores nothing. + #[test] + fn a_mined_block_whose_parent_the_store_lacks_is_answered_no_parent() { + let fx = fixture(20); + let miner = faulty_miner(&fx, Fault::NoParent); + let work = miner.get_work().unwrap(); + let raw = bitcoin::consensus::serialize(&solve(&work)); + + let info = miner.submit_bitcoin_block(&raw).unwrap(); + assert_eq!(info.block_imported_result, ImportResult::NoParent); + assert_eq!(info.block_imported_result.as_str(), "NO_PARENT"); + assert_eq!(fx.store.head().unwrap(), Some(fx.parent.hash())); + } +} diff --git a/crates/execution/src/mining/timed.rs b/crates/execution/src/mining/timed.rs index fcfea9a..609fd41 100644 --- a/crates/execution/src/mining/timed.rs +++ b/crates/execution/src/mining/timed.rs @@ -172,7 +172,12 @@ pub struct TimedMiner { server: Arc, median: Duration, pow: Pow, - running: Arc, + /// The flag of the thread started last. Each start gives its thread a + /// flag of its own, and a stop clears the flag of the thread running: a + /// thread stopped while it sleeps or mines reads its own flag, so a start + /// that comes before it wakes runs a new thread and the stopped one ends, + /// not both on. + running: Mutex>, /// Blocks mined so far, for tests and for the stop message. mined: Arc>, } @@ -183,13 +188,16 @@ impl TimedMiner { server, median, pow, - running: Arc::new(AtomicBool::new(false)), + running: Mutex::new(Arc::new(AtomicBool::new(false))), mined: Arc::new(Mutex::new(0)), } } pub fn is_running(&self) -> bool { - self.running.load(Ordering::Relaxed) + self.running + .lock() + .unwrap_or_else(|e| e.into_inner()) + .load(Ordering::Acquire) } pub fn median(&self) -> Duration { @@ -204,15 +212,16 @@ impl TimedMiner { /// Start the thread, or do nothing if it is running. The first block /// comes after a draw, as in rskj (`start`, line 83), not at once. pub fn start(&self) { - if self - .running - .compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire) - .is_err() - { - return; - } + let running = { + let mut current = self.running.lock().unwrap_or_else(|e| e.into_inner()); + if current.load(Ordering::Acquire) { + return; + } + let flag = Arc::new(AtomicBool::new(true)); + *current = flag.clone(); + flag + }; let server = self.server.clone(); - let running = self.running.clone(); let mined = self.mined.clone(); let pow = self.pow; let mut schedule = TimedSchedule::new(self.median); @@ -270,7 +279,10 @@ impl TimedMiner { /// Stop after the block in flight, if any. Idempotent. pub fn stop(&self) { - self.running.store(false, Ordering::Release); + self.running + .lock() + .unwrap_or_else(|e| e.into_inner()) + .store(false, Ordering::Release); } } diff --git a/crates/execution/src/mining/workload.rs b/crates/execution/src/mining/workload.rs index bb00e95..3c95c8f 100644 --- a/crates/execution/src/mining/workload.rs +++ b/crates/execution/src/mining/workload.rs @@ -578,12 +578,25 @@ 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); - let processor = Arc::new(BlockProcessor::new(hardfork_cfg.clone(), block_store.clone())); + // 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. + 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 +619,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}"))?; @@ -718,6 +732,7 @@ fn chain_header( bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } @@ -1069,6 +1084,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 new file mode 100644 index 0000000..92a0d19 --- /dev/null +++ b/crates/execution/src/native/block_header.rs @@ -0,0 +1,777 @@ +//! 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, +} + +/// 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), + ("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(&[])); + }; + match self.read(&call, hash, &header)? { + Some(value) => Ok(encode_bytes(&value)), + None => Err(Failure::Null), + } + } +} + +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 => coinbase_bytes(header), + Method::BlockHash => hash.to_vec(), + Method::MergedMiningTags => merged_mining_tags(header)?, + // `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(), + 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(coinbase_bytes) + .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()) +} + +/// `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. +fn big_integer_bytes(value: U256) -> Vec { + let mut bytes = shortest_bytes(value); + if bytes.first().is_none_or(|first| first & 0x80 != 0) { + bytes.insert(0, 0); + } + bytes +} + +/// 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() +} + +#[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, + kept: 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, .. } + )); + } + + /// 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| { + 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)]))); + } + + /// 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_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. + 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::Null { gas: 4_072 }, + "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 (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!(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!( + 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/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..293f49c --- /dev/null +++ b/crates/execution/src/native/hd_wallet.rs @@ -0,0 +1,760 @@ +//! 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::constants::CURVE_ORDER; +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, +} + +/// 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), + ( + "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 key = child_point(secp, self.key, field(&i, 0))?; + 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) + } +} + +/// `deriveChildKeyBytesFromPublic`'s point, I_L·G added to the parent's. +/// bitcoinj's `assertLessThanN` refuses an I_L above the curve's order, and +/// not one equal to it: that I_L multiplies G to the point at infinity, and +/// the child keeps its parent's point. A sum that is the point at infinity +/// is refused (`assertNonInfinity`). +fn child_point( + secp: &Secp256k1, + parent: PublicKey, + il: [u8; 32], +) -> Result { + if il == CURVE_ORDER { + return Ok(parent); + } + let tweak = + Scalar::from_be_bytes(il).map_err(|_| Failure::threw("I_L is above the curve's order"))?; + parent + .add_exp_tweak(secp, &tweak) + .map_err(|_| Failure::threw("the derived key is the point at infinity")) +} + +/// 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); + } + + /// bitcoinj refuses an I_L above the curve's order, and derives from one + /// equal to it: n·G is the point at infinity, so the child's point is its + /// parent's. One below it, n - 1, adds -G, and added to G itself makes the + /// point at infinity, which bitcoinj refuses. + #[test] + fn an_i_l_equal_to_the_curves_order_keeps_the_parents_point() { + let secp = Secp256k1::verification_only(); + let parent = ExtendedKey::from_base58(TPUB).unwrap().key; + assert_eq!(child_point(&secp, parent, CURVE_ORDER), Ok(parent)); + let mut above = CURVE_ORDER; + above[31] += 1; + assert!(matches!( + child_point(&secp, parent, above), + Err(Failure::Threw(_)) + )); + let mut below = CURVE_ORDER; + below[31] -= 1; + let child = child_point(&secp, parent, below).unwrap(); + assert_eq!(child.add_exp_tweak(&secp, &Scalar::ONE).unwrap(), parent); + let generator = PublicKey::from_slice( + &hex::decode("0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798") + .unwrap(), + ) + .unwrap(); + assert!(matches!( + child_point(&secp, generator, below), + Err(Failure::Threw(_)) + )); + } + + /// 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..dcdaa56 --- /dev/null +++ b/crates/execution/src/native/mod.rs @@ -0,0 +1,204 @@ +//! 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. 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; what a `null` does depends on the same and on +//! RSKIP150. `RskPrecompileProvider` settles both. + +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 }, + /// `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, + /// `execute` threw, and the call cost `gas`: its `getGasForData`, which is + /// 0 when no method was called. + Threw { gas: u64, reason: String }, + /// 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), +} + +/// 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), +} + +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::Null) => Outcome::Null { gas }, + 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..003a128 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,223 @@ fn bridge_run(_input: &[u8], gas_limit: u64) -> Result Result { + Err(PrecompileError::Fatal( + "a native contract runs through RskPrecompileProvider".into(), + )) +} + +/// ECRecover's address, 0x01. +const ECRECOVER_ADDR: Address = Address::new(hex_addr("0000000000000000000000000000000000000001")); + +/// rskj's ECRecover (`PrecompiledContracts.ECRecover`), which parts from +/// Ethereum's, the one revm runs, in two places: +/// +/// - only the last byte of the `v` word counts: `isValid` reads `v[31]`, so +/// a word whose other bytes aren't zero still recovers a key, where revm +/// answers nothing; +/// - a signature whose key is the point at infinity recovers the address +/// rskj gives that key, the infinity sender, where revm answers nothing. /// -/// 4 methods dispatched by 4-byte ABI selector: -/// - toBase58Check(bytes,int256) → string -/// - deriveExtendedPublicKey(string,string) → string -/// - extractPublicKeyFromExtendedPublicKey(string) → bytes -/// - getMultisigScriptHash(int256,bytes[]) → bytes -fn hd_wallet_utils_run(input: &[u8], gas_limit: u64) -> 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")) +/// The rest is as Ethereum's: 3,000 gas, the input's first 128 bytes read, +/// and nothing for a `v` byte other than 27 or 28, an r or s outside 1 to +/// below the curve's order, or a key that doesn't recover. An input too +/// short to hold the hash, `v` and r makes `execute`'s `System.arraycopy` +/// throw, and it answers nothing for any exception; an s cut short ends in +/// zeros. +fn rsk_ecrecover_run(input: &[u8], gas_limit: u64) -> 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())) } -/// 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 { +/// 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) +} + +/// 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_cost, Vec::new().into())) + Ok(PrecompileOutput::new(gas, modexp::execute(input)?.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())) +/// 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. @@ -200,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 } @@ -263,504 +443,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 // --------------------------------------------------------------------------- @@ -774,10 +456,21 @@ 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. + // 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([revm::precompile::modexp::BYZANTIUM]); + precompiles.extend([ + Precompile::new( + PrecompileId::custom("rsk-ecrecover"), + ECRECOVER_ADDR, + rsk_ecrecover_run, + ), + Precompile::new( + PrecompileId::custom("rsk-modexp"), + MODEXP_ADDR, + rsk_modexp_run, + ), + ]); // RSKIP137 (Papyrus200): BN128 add/mul/pairing, priced per EIP-1108 // (Istanbul: 150 / 6000 / 45000 + 34000 per pair) from activation. @@ -810,35 +503,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 @@ -874,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,51 +592,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 +644,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. @@ -969,13 +656,21 @@ 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 - /// 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`]. + /// Whether this transaction reached REMASC or the Bridge. `RskHandler` + /// sets it. See [`RskPrecompileProvider::reached_sequential_only_slot`]. reached_sequential_only: std::sync::Arc, } @@ -984,7 +679,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() } } @@ -1001,6 +700,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) @@ -1011,12 +715,14 @@ impl RskPrecompileProvider { /// /// Deliberately **not** the same question as `called_precompiles_slot`, /// which records a precompile that returned successfully because that is - /// when rskj writes its storage-prefix marker. This one records reaching - /// the precompile at all, whatever came of it, because that is what rskj - /// records: `Program.callToPrecompiledAddress` adds the address before - /// doing anything, and a child frame's set merges into its parent's - /// before the parent checks whether the child succeeded. A Bridge call - /// that reverts still bars the transaction. + /// when rskj writes its storage-prefix marker. This one records the call + /// as it starts, whatever came of it, as rskj does + /// (`Program.callToPrecompiledAddress` adds the address before it checks + /// the depth or the caller's balance), and the run sets it, not the + /// precompile, which a call refused before it opens never reaches + /// (`RskHandler::with_reached_sequential_only`). A Bridge call that + /// reverts still bars the transaction; one made under a constructor of + /// a CREATE or CREATE2 doesn't. pub fn reached_sequential_only_slot(&self) -> std::sync::Arc { std::sync::Arc::clone(&self.reached_sequential_only) } @@ -1035,10 +741,96 @@ 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, + } + } + + /// Whether a transaction's own call to `addr` with `input` is one 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, block: u64, addr: &Address, input: &[u8]) -> bool { + *addr == BLAKE2F_ADDR && input.is_empty() && !self.hardfork_cfg.has_rskip552(block) + } + + /// What the precompile at `addr`, not REMASC nor the Bridge, makes of + /// `input`, held to `gas_limit`: a transaction's own call where + /// `own_call`, a contract's otherwise. + fn outcome( + &self, + context: &CTX, + addr: Address, + precompile: &Precompile, + input: &[u8], + gas_limit: u64, + own_call: bool, + ) -> Outcome { + let block = context.block().number().to::(); + if own_call && self.blake2f_reads_null(block, &addr, input) { + Outcome::Fatal("BLAKE2F's getGasForData throws on null data".into()) + } else if NATIVE_CONTRACTS.contains(&addr) { + self.run_native(context, addr, input, gas_limit) + } else { + run_precompile( + precompile, + &addr, + input, + gas_limit, + self.hardfork_cfg.has_rskip197(block), + ) + } + } + + /// A transaction's own call to the precompile at `addr`, as rskj's + /// `TransactionExecutor.call` makes it: the transaction's data whole, held + /// to `gas_limit`. `None` where `addr` holds no precompile at this block, + /// and for REMASC and the Bridge, which the executor runs itself. The + /// executor runs such a call this way before RSKIP136, where the price is + /// held to the transaction's whole limit; a precompile writes nothing, so + /// what it does is known before the transaction is charged. + pub(crate) fn run_for_a_transaction( + &self, + context: &CTX, + addr: Address, + input: &[u8], + gas_limit: u64, + ) -> Option { + if addr == REMASC_ADDR || addr == BRIDGE_ADDR { + return None; + } + let precompile = self.precompiles.get(&addr)?; + Some(self.outcome(context, addr, precompile, input, gas_limit, true)) + } } -/// 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 +845,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,10 +858,12 @@ 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())), 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), @@ -1092,7 +878,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. @@ -1107,14 +893,8 @@ impl RskPrecompileProvider { return Ok(None); } - // Before dispatch, so the outcome cannot change the answer: under - // RSKIP-144 a transaction that reaches REMASC or the Bridge may only - // sit in the sequential sublist, and rskj records the address on the - // way in (`Program.callToPrecompiledAddress`). - if SEQUENTIAL_ONLY_PRECOMPILES.contains(addr) { - self.reached_sequential_only - .store(true, std::sync::atomic::Ordering::Relaxed); - } + // Whether the transaction reached REMASC or the Bridge is the run's + // to record, as each call starts (`RskHandler`'s `note_call`). // Copy input to owned bytes so we can pass &mut CTX to the handler // without conflicting with the SharedBuffer borrow. @@ -1130,23 +910,55 @@ 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 == 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 { - self.run_bridge(context, &input_bytes, inputs.gas_limit, inputs.caller, inputs.scheme, inputs.call_value()) + // 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 { + // 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()), + }; + // 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, + inputs.gas_limit, + 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) }; @@ -1186,35 +998,48 @@ impl RskPrecompileProvider { assert!(underflow, "gas pre-checked against the cost"); return Ok(Some(result)); } - // Internal (depth>1) Bridge method throw: rskj + // 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 // 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; 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; @@ -1266,205 +1091,39 @@ 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( + /// 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, - 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())) + 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, @@ -1473,6 +1132,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; @@ -1489,9 +1149,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, @@ -1514,29 +1179,176 @@ 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, and then saves as every REMASC method that returns + /// does (`RemascContract.execute`). 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); + // `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 - // (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. + 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 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, + 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::Null { 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::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"); + 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 { + self.aborts_caller + .store(true, std::sync::atomic::Ordering::Relaxed); + InstructionResult::PrecompileError + }; + } + Outcome::Fatal(reason) => return Err(reason), + } + Ok(result) } } @@ -1547,10 +1359,12 @@ impl Clone for RskPrecompileProvider { 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), + 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), } @@ -1574,96 +1388,55 @@ 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); }; - // rskj creates the precompile account node on first call (see the - // stateful path above). + // 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). { 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(inputs.target_address); + context.journal_mut().touch_account(inputs.target_address); } - 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 max_input = self.max_input(context, &addr); + 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) + let input = max_input.map_or(input, |max| &input[..input.len().min(max)]); + let own_call = context.journal().depth() == 1; + self.outcome(context, addr, precompile, input, inputs.gas_limit, own_call) }; - - 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()); - } - } - } - } - Ok(Some(result)) + self.finish(context, addr, inputs.gas_limit, outcome) + .map(Some) } fn warm_addresses(&self) -> Box> { @@ -1675,6 +1448,51 @@ 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), + 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. + 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 +1537,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 +1575,270 @@ 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!(gas_for_data(&BLAKE2F_ADDR, &blake_input), Some(12)); + assert_eq!(gas_for_data(&BLAKE2F_ADDR, &[0u8; 100]), Some(0)); + + // ecrecover never throws in rskj; it returns empty. + let ecrecover = Address::new(hex_addr("0000000000000000000000000000000000000001")); + 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!( - rskip197_required_gas_on_error(&blake2f, &blake_input), - Some(12) + run(Address::with_last_byte(4), b"abc", 1_000, true), + Outcome::Returned { + gas: 18, + output: Bytes::from_static(b"abc") + } ); - assert_eq!(rskip197_required_gas_on_error(&blake2f, &[0u8; 100]), Some(0)); + } - // ecrecover never throws a non-OOG error in rskj; it returns empty. - let ecrecover = Address::new(hex_addr("0000000000000000000000000000000000000001")); - assert_eq!(rskip197_required_gas_on_error(&ecrecover, &[]), None); + // ----------------------------------------------------------------------- + // 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] @@ -1830,25 +1870,121 @@ mod tests { assert!(precompiles.contains(&ecrecover)); } - #[test] - fn test_hdwallet_not_active_before_wasabi100() { - let cfg = RskHardforkConfig::mainnet(); - let precompiles = rsk_precompiles(&cfg, 0); - assert!(!precompiles.contains(&HD_WALLET_UTILS_ADDR)); - } + // ----------------------------------------------------------------------- + // ECRecover, as rskj's + // ----------------------------------------------------------------------- - #[test] - fn test_blockheader_active_after_wasabi() { - let cfg = RskHardforkConfig::mainnet(); + /// 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 + } - let pre = rsk_precompiles(&cfg, 1_590_999); - assert!(!pre.contains(&BLOCK_HEADER_ADDR)); + fn rskj_ecrecover(input: &[u8]) -> Bytes { + rsk_ecrecover_run(input, 3_000).unwrap().bytes + } - let post = rsk_precompiles(&cfg, 1_591_000); - assert!(post.contains(&BLOCK_HEADER_ADDR)); + fn ethereum_ecrecover(input: &[u8]) -> Bytes { + use revm::precompile::secp256k1::ec_recover_run; + ec_recover_run(input, 3_000).unwrap().bytes } - /// rskj reference.conf: rskip203 = arrowhead600 (mainnet 6_223_700). + /// 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(); + let precompiles = rsk_precompiles(&cfg, 0); + assert!(!precompiles.contains(&HD_WALLET_UTILS_ADDR)); + } + + #[test] + fn test_blockheader_active_after_wasabi() { + let cfg = RskHardforkConfig::mainnet(); + + let pre = rsk_precompiles(&cfg, 1_590_999); + assert!(!pre.contains(&BLOCK_HEADER_ADDR)); + + let post = rsk_precompiles(&cfg, 1_591_000); + assert!(post.contains(&BLOCK_HEADER_ADDR)); + } + + /// rskj reference.conf: rskip203 = arrowhead600 (mainnet 6_223_700). #[test] fn test_environment_active_after_arrowhead600() { let cfg = RskHardforkConfig::mainnet(); @@ -1918,6 +2054,59 @@ mod tests { assert!(!rsk_precompiles(&cfg, 8_804_200).contains(&kzg)); } + /// rskj's `getContractForAddress`, upgrade by upgrade, on mainnet and on + /// testnet (each network's `hardforkActivationHeights`): the addresses it + /// serves on each side of wasabi100, papyrus200, iris300, arrowhead600 and + /// reed800, and no other. None of Ethereum's later ones is among them. + #[test] + fn the_set_is_rskjs_on_each_side_of_each_upgrade() { + let low = Address::with_last_byte; + let genesis = [ + low(1), + low(2), + low(3), + low(4), + low(5), + BRIDGE_ADDR, + REMASC_ADDR, + ]; + let added: [&[Address]; 5] = [ + &[HD_WALLET_UTILS_ADDR, BLOCK_HEADER_ADDR], + &[low(6), low(7), low(8)], + &[low(9)], + &[ENVIRONMENT_ADDR], + &[SECP256K1_ADD_ADDR, SECP256K1_MUL_ADDR], + ]; + let networks = [ + ( + RskHardforkConfig::mainnet(), + [1_591_000, 2_392_700, 3_614_800, 6_223_700, 8_052_200], + ), + ( + RskHardforkConfig::testnet(), + [0, 863_000, 2_060_500, 4_927_100, 6_835_700], + ), + ]; + let served = |cfg: &RskHardforkConfig, number: u64| { + let mut set: Vec
= rsk_precompiles(cfg, number).addresses().copied().collect(); + set.sort(); + set + }; + for (cfg, heights) in networks { + let mut expected = genesis.to_vec(); + for (height, added) in heights.into_iter().zip(added) { + if height > 0 { + expected.sort(); + assert_eq!(served(&cfg, height - 1), expected, "#{}", height - 1); + } + expected.extend_from_slice(added); + expected.sort(); + assert_eq!(served(&cfg, height), expected, "#{height}"); + } + assert_eq!(served(&cfg, 20_000_000), expected); + } + } + /// Ported from rskj Blake2fNullDataTest (post-RSKIP552 semantics): calling /// Blake2F with empty input fails with an input-length error instead of /// crashing — revm's blake2 run() returns an error for non-213-byte input, @@ -1971,12 +2160,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(); @@ -2189,6 +2372,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( @@ -2201,6 +2404,20 @@ mod tests { assert!(secp256k1_mul_run(&input, 10_000).is_err()); } + /// rskj builds each point with BouncyCastle's `validatePoint`, whose field + /// element refuses a coordinate of p or more: 1 + p is no point's x though + /// it is 1 modulo p, and (1, V1Y) is a point. + #[test] + fn a_coordinate_of_p_or_more_is_no_point() { + let p = alloy_primitives::U256::from_str_radix(SECP256K1_P, 10).unwrap(); + let one_plus_p = (p + alloy_primitives::U256::from(1)).to_string(); + assert!(secp256k1_add_run(&build_add_input("1", V1Y, "0", "0"), 1_000).is_ok()); + assert!(secp256k1_add_run(&build_add_input(&one_plus_p, V1Y, "0", "0"), 1_000).is_err()); + assert!(secp256k1_add_run(&build_add_input("0", "0", &one_plus_p, V1Y), 1_000).is_err()); + assert!(secp256k1_mul_run(&build_mul_input("1", V1Y, "1"), 10_000).is_ok()); + assert!(secp256k1_mul_run(&build_mul_input(&one_plus_p, V1Y, "1"), 10_000).is_err()); + } + #[test] fn test_secp_mul_empty_input_is_zero() { let result = secp256k1_mul_run(&[], 10_000).unwrap(); @@ -2265,11 +2482,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 +2503,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 +2517,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/processor.rs b/crates/execution/src/processor.rs index 5dbe9c5..fd49b10 100644 --- a/crates/execution/src/processor.rs +++ b/crates/execution/src/processor.rs @@ -108,6 +108,11 @@ pub struct ProcessedBlock { pub state_root_hash: B256, pub receipts_root: B256, pub logs_bloom: Bloom, + /// The Bridge's decisions this block made that it logs no event for + /// (`crate::silent`), which [`BlockProcessor::commit_receipts`] stores + /// with it. Empty unless the processor records them + /// ([`BlockProcessor::with_legacy_event_store`]). + pub unlogged_peg_events: Vec, } /// The running gas total for the receipt at `index`. @@ -138,6 +143,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, /// Where to record the peg events the Bridge does not emit. /// /// `None` disables recording entirely, and the cost of that is one @@ -189,35 +198,48 @@ 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, legacy_events: None, } } - /// 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 - /// block carrying it executes. - /// Records peg events the Bridge does not emit, into `store`. - /// - /// Opens the thread-local sink here, because recording is a property of - /// the thread that executes and this is the thread that will. Sublists - /// running in parallel open their own and the coordinator absorbs them - /// (`silent::absorb`), so a parallel block is captured whole. + /// 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, 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 + } + + /// Records peg events the Bridge does not emit, into `store`: each block + /// this processor runs records its own (see `finish_execute`). pub fn with_legacy_event_store( mut self, store: Arc, ) -> Self { - crate::silent::start(); self.legacy_events = Some(store); 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 + /// block carrying it executes. pub fn with_sender_cache( mut self, cache: Arc, @@ -295,6 +317,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. @@ -316,16 +343,27 @@ impl BlockProcessor { trie_store: Arc, save_out: Option<&mut std::time::Duration>, ) -> Result { - // Recording is thread-local, and the thread that built this processor - // need not be the one executing. Opening the sink lazily here makes - // that irrelevant: whichever thread arrives, it records. Without it a - // node whose import loop runs off the main thread would record - // nothing and look exactly like a chain with no Flyover peg-ins. - if self.legacy_events.is_some() && !crate::silent::is_recording() { + // The Bridge's unlogged decisions are recorded on the thread that + // executes (`crate::silent`), so recording is opened here, fresh, and + // closed again when the block is done, and what was recorded goes + // with the block, to be stored where it is committed, on any thread. + // Nothing else the thread runs is recorded -- a local call, a trace, + // the miner's trial run of its candidates -- and a block that fails + // leaves nothing behind for the next to store. A template the miner + // builds runs here too, and what it records goes with it unstored: + // only the block a solution completes is committed. + let recording = self.legacy_events.is_some(); + if recording { crate::silent::start(); } let started = std::time::Instant::now(); - let out = self.execute_block_inner(block, state_root, trie_store, save_out); + let mut out = self.execute_block_inner(block, state_root, trie_store, save_out); + if recording { + let decisions = crate::silent::finish(); + if let Ok(processed) = &mut out { + processed.unlogged_peg_events = decisions; + } + } // Timed whether or not it succeeded: a block that fails still spent // the time, and a measurement that only counts successes reads as a // faster node the worse things get. @@ -367,23 +405,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 @@ -423,7 +468,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; @@ -484,6 +529,7 @@ impl BlockProcessor { trie_store.as_ref(), &exec_result.state_changes, ) + .with_null_account(exec_result.markers.null_account) } else { crate::supply::SupplyReport::default() }; @@ -549,6 +595,7 @@ impl BlockProcessor { state_root_hash, receipts_root, logs_bloom: block_bloom, + unlogged_peg_events: Vec::new(), }) } @@ -573,11 +620,14 @@ impl BlockProcessor { /// /// # Why the receipt index is the right witness /// - /// `put_receipts` is called from exactly one place in this crate -- the end - /// of `process_and_commit` -- so a receipt entry keyed by a block hash is a - /// record that *this* node executed *that* block. It is persisted, so it - /// survives a restart, and the pruner deletes it in the same batch as the - /// block and its state, so it cannot outlive what it vouches for. + /// `put_receipts` is called in this crate only right after a block has + /// run -- by `commit_by_hash`, which `process_and_commit` and + /// `process_beside` reach once execution has succeeded, and by + /// `execute_and_commit` -- so a receipt entry keyed by a block hash is a + /// record that *this* node executed *that* block, on the chain or beside + /// it. It is persisted, so it survives a restart, and the pruner deletes + /// it in the same batch as the block and its state, so it cannot outlive + /// what it vouches for. /// `executed_locally_is_only_written_by_execution` pins that invariant. /// /// # Why it does not have to be trusted @@ -609,7 +659,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. @@ -620,7 +670,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. @@ -713,8 +763,6 @@ impl BlockProcessor { block: &Block, parent: Option<&rustock_core::Header>, ) -> Result<(), ValidationError> { - use rustock_core::validation::{HeaderValidator, ParentHeaderValidator}; - let number = block.header.number; // rskj `BlockValidatorImpl.isValid` refuses genesis outright: it is @@ -726,20 +774,7 @@ 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. - let ancestry = self.ancestry(); - let header_rules: Vec> = Vec::new(); - let parent_rules: Vec> = Vec::new(); - rustock_core::validation::uncles::UnclesValidationRule { - store: &ancestry, - uncle_list_limit: rustock_core::validation::uncles::UNCLE_LIST_LIMIT, - uncle_generation_limit: rustock_core::validation::uncles::UNCLE_GENERATION_LIMIT, - header_rules: &header_rules, - parent_rules: &parent_rules, - } - .validate(block)?; + self.validate_uncles(block)?; // rskj ForkDetectionDataRule -- RSKIP110 (wasabi100). if self.validate_fork_detection_data { @@ -748,18 +783,54 @@ impl BlockProcessor { rustock_core::validation::fork_detection::validate( &block.header, actual, - &ancestry, + &self.ancestry(), self.rskip110_height(), )?; } - let _ = number; Ok(()) } + /// rskj `BlockUnclesValidationRule`, as a block is held to it on import. + /// The ommers HASH is checked in `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. + /// + /// Reads only the block's number, its parent's hash and its uncles, so + /// the miner holds the uncles it picked to it before building on them. + pub fn validate_uncles(&self, block: &Block) -> Result<(), ValidationError> { + use rustock_core::validation::uncles::{ + rsk_header_rules, rsk_parent_rules, UncleForkDetection, UnclesValidationRule, + UNCLE_GENERATION_LIMIT, UNCLE_LIST_LIMIT, + }; + + let ancestry = self.ancestry(); + 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: 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, + // 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) + } + /// 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 @@ -773,8 +844,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}; @@ -789,6 +860,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. @@ -797,6 +881,34 @@ impl BlockProcessor { Ok(()) } + /// What rskj holds each block of a snapshot to, past the rules its header + /// meets alone and against its parent, and the body its header commits to + /// (`RskContext.getSnapBlockValidationRule` and + /// `getSnapBlockParentDependantValidationRule`): the rules of + /// [`Self::validate_block_body_rules`], and `BlockTxsFieldsValidationRule`, + /// a signature the network takes on every transaction but REMASC. + /// + /// A block that is executed meets that last rule where its senders are + /// recovered. A snapshot's blocks are kept without being run, so nothing + /// else holds them to it. + /// + /// Block 0 passes: rskj reads it from its genesis file and holds it to no + /// rule, and its snapshot client stops at the first block whose parent it + /// holds (`SnapshotProcessor.validateAndSaveBlocks`), which block 1 is at + /// the latest. + pub fn validate_snapshot_block( + &self, + block: &Block, + parent: Option<&rustock_core::Header>, + ) -> Result<(), ProcessError> { + if block.header.number == 0 { + return Ok(()); + } + self.validate_block_body_rules(block, parent)?; + self.recover_senders(&block.transactions, self.hardfork_cfg.chain_id)?; + Ok(()) + } + /// `validate_block_rules` with the parent resolved from the block store — /// the shape the sync path wants, mirroring rskj `BlockValidatorImpl`, /// which looks the parent up the same way and returns invalid when it is @@ -815,15 +927,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. @@ -874,7 +983,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, @@ -882,7 +991,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, @@ -922,7 +1031,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, @@ -960,7 +1069,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!( @@ -984,7 +1093,7 @@ impl BlockProcessor { }); } - if result.logs_bloom != header.logs_bloom { + if !header.has_logs_bloom(&result.logs_bloom) { return Err(ProcessError::LogsBloomMismatch); } @@ -1029,10 +1138,27 @@ impl BlockProcessor { Ok(result) } - /// The commit half of [`BlockProcessor::process_and_commit`]: store the - /// receipts of a processed block and index its transactions and Bridge - /// events. - /// Records this block's peg events that the Bridge did not emit. + /// Validate and execute a block that is not on the chain, against its own + /// parent's state, and commit what it keys by hash: its receipts and its + /// transactions. What it keys by height waits for the chain to take it up + /// ([`Self::commit_by_height`]), since the chain gives that height to + /// another block meanwhile. rskj executes every block it connects so + /// (`BlockChainImpl.internalTryToConnect`), best or not, and a switch to + /// it later runs nothing again. + pub fn process_beside( + &self, + block: &Block, + parent_state: &TrieNode, + trie_store: Arc, + ) -> Result { + let result = self.process_block(block, parent_state, trie_store)?; + self.commit_by_hash(block, &result.receipts)?; + Ok(result) + } + + /// Records this block's peg events that the Bridge did not emit: the + /// decisions `decisions` holds, which are the block's own + /// ([`ProcessedBlock::unlogged_peg_events`]). /// /// Runs beside `index_bridge_events` deliberately: that keeps the index /// of what the Bridge *did* log current as blocks arrive, and this keeps @@ -1050,15 +1176,18 @@ impl BlockProcessor { /// Public so it can be driven without `commit_receipts`, which also /// writes receipts and so cannot run against a read-only store. The node /// reaches it through `commit_receipts`; a test reaches it directly. - pub fn record_unlogged_peg_events(&self, block: &Block) -> Result<(), ProcessError> { + pub fn record_unlogged_peg_events( + &self, + block: &Block, + decisions: &[crate::silent::SilentRecord], + ) -> Result<(), ProcessError> { let Some(store) = &self.legacy_events else { return Ok(()) }; - let decisions = crate::silent::drain(); if decisions.is_empty() { return Ok(()); } let mut rows = Vec::new(); let mut log_index: u32 = 0; - for d in &decisions { + for d in decisions { // A record that does not say whether the Bridge logged it is // refused, not guessed at -- see `legacy_events::reconstructable`. if !matches!(crate::bridge::legacy_events::reconstructable(d), Ok(true)) { @@ -1088,14 +1217,31 @@ impl BlockProcessor { Ok(()) } + /// The commit half of [`BlockProcessor::process_and_commit`]: store the + /// receipts of a processed block, index its transactions and Bridge + /// events, and record the peg events it made that the Bridge did not log. pub fn commit_receipts( &self, block: &Block, result: &ProcessedBlock, ) -> Result<(), ProcessError> { + self.commit_by_hash(block, &result.receipts)?; + self.commit_by_height(block, &result.receipts, &result.unlogged_peg_events) + } + + /// What a processed block commits under its own hash: its receipts, and + /// its transactions in the index that finds them. A block beside the + /// chain holds both as well as one on it, as rskj's receipt store holds + /// every block it connects, so a block executed as it arrived is ready + /// the moment the chain switches to it. + /// + /// Private, and reached only from right after a block has run: the receipts + /// it writes are this node's witness that it executed the block (see + /// [`Self::was_executed_locally`]). + fn commit_by_hash(&self, block: &Block, receipts: &[Receipt]) -> Result<(), ProcessError> { let hash = block.hash(); self.block_store - .put_receipts(hash, &result.receipts) + .put_receipts(hash, receipts) .map_err(ProcessError::Storage)?; // Receipts are stored by block hash, so without this the only way to // reach one is to already know which block it is in: @@ -1105,6 +1251,20 @@ impl BlockProcessor { self.block_store .index_block_transactions(hash, &block.transactions) .map_err(ProcessError::Storage)?; + Ok(()) + } + + /// What a processed block commits under its height: its Bridge events, and + /// the peg events it made that the Bridge did not log. Indexed by height, + /// so written once the block is on the chain, and not for a block beside + /// it, which would leave its events at a height the chain gives another + /// block. + pub fn commit_by_height( + &self, + block: &Block, + receipts: &[Receipt], + unlogged: &[crate::silent::SilentRecord], + ) -> Result<(), ProcessError> { // Keep the Bridge event index current as blocks arrive. Rebuilding it // from receipts is possible (--build-bridge-index) but an index that is // only ever rebuilt is stale the moment the node advances, which is @@ -1114,12 +1274,26 @@ impl BlockProcessor { self.block_store .index_bridge_events( block.header.number, - &result.receipts, + receipts, &block.transactions, crate::precompiles::BRIDGE_ADDR, ) .map_err(ProcessError::Storage)?; - self.record_unlogged_peg_events(block)?; + self.record_unlogged_peg_events(block, unlogged) + } + + /// Take a block the chain has left out of the Bridge event index: the + /// events [`Self::commit_by_height`] wrote at its height from these + /// receipts. The peg events the Bridge did not log stay, since nothing + /// says which of them a block made once it has run. + pub fn uncommit_by_height( + &self, + number: u64, + receipts: &[Receipt], + ) -> Result<(), ProcessError> { + self.block_store + .remove_bridge_events(number, receipts, crate::precompiles::BRIDGE_ADDR) + .map_err(ProcessError::Storage)?; Ok(()) } @@ -1168,8 +1342,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); @@ -1192,11 +1367,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), } }; @@ -1217,12 +1395,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) @@ -1262,23 +1441,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. @@ -1341,13 +1503,17 @@ 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, bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } @@ -1548,7 +1714,7 @@ mod tests { /// consensus, not a miner's preference. #[test] fn a_bridge_call_in_a_parallel_sublist_invalidates_the_block() { - let (block, root, store, block_store) = bridge_calling_block(Some(vec![1, 2])); + let (block, root, store, block_store) = bridge_calling_block(Some(vec![1, 2]), None); let processor = BlockProcessor::new( RskHardforkConfig::testnet(), // reed810 is scheduled there block_store, @@ -1573,7 +1739,7 @@ mod tests { // too, so put the Bridge call where the sequential set starts by // declaring a single transaction's worth of parallel sublists above // it: with no edges at all there is no partition and no rule. - let (block, root, store, block_store) = bridge_calling_block(None); + let (block, root, store, block_store) = bridge_calling_block(None, None); let processor = BlockProcessor::new(RskHardforkConfig::testnet(), block_store); let out = processor.execute_block(&block, &root, store); assert!( @@ -1585,6 +1751,127 @@ mod tests { ); } + /// rskj counts a call to the Bridge as it starts, before it checks the + /// caller's balance (`Program.callToPrecompiledAddress`): a contract's call + /// it refuses because the contract can't pay the value bars the + /// transaction from a parallel sublist all the same. The contract is + /// `calls-bridge-unpaid` of `testdata/rskj_precompiles_called.txt`. + #[test] + fn a_refused_bridge_call_in_a_parallel_sublist_invalidates_the_block() { + let (block, root, store, block_store) = bridge_calling_block( + Some(vec![1, 2]), + Some( + "7f6923fa8500000000000000000000000000000000000000000000000000000000\ + 6000526000600060046000600163010000065af15000", + ), + ); + let processor = BlockProcessor::new(RskHardforkConfig::testnet(), block_store); + let err = processor + .execute_block(&block, &root, store) + .expect_err("a refused Bridge call in a parallel sublist is invalid"); + assert!( + matches!( + err, + ProcessError::SequentialOnlyPrecompileInParallelSublist { + index: 0, + sublist: 0, + .. + } + ), + "got {err}" + ); + } + + /// What a constructor run by CREATE calls doesn't count for the + /// transaction in rskj (`Program.createContract` never hands it on), so a + /// transaction whose only Bridge call is a constructor's may sit in a + /// parallel sublist. The contract is `creates-child-calling-bridge` of + /// `testdata/rskj_precompiles_called.txt`. + #[test] + fn a_constructors_bridge_call_leaves_a_parallel_sublist_open() { + let (block, root, store, block_store) = bridge_calling_block( + Some(vec![1, 2]), + Some( + "60376010600039603760006000f050007f6923fa850000000000000000000000\ + 0000000000000000000000000000000000600052600060006004600060006301\ + 0000065af15000", + ), + ); + let processor = BlockProcessor::new(RskHardforkConfig::testnet(), block_store); + processor + .execute_block(&block, &root, store) + .expect("a constructor's Bridge call bars no sublist"); + } + + /// The Bridge's unlogged decisions are recorded fresh for each block and + /// go with it (`ProcessedBlock::unlogged_peg_events`). What the thread + /// held before the block -- here a decision as a local call made one on + /// a thread an earlier block had left recording -- does not go with it, + /// and the thread records nothing once the block is done, so nothing + /// else it runs is stored as the chain's. + #[test] + fn a_block_carries_only_its_own_peg_decisions() { + let dir = tempfile::tempdir().unwrap(); + let events = Arc::new( + rustock_storage::legacy_event_store::LegacyEventStore::open( + dir.path().to_str().unwrap(), + ) + .unwrap(), + ); + let (block, root, store, block_store) = bridge_calling_block(None, None); + let processor = BlockProcessor::new(RskHardforkConfig::testnet(), block_store) + .with_legacy_event_store(events); + crate::silent::start(); + crate::silent::record(crate::silent::SilentKind::PeginAccepted, 1, 0, vec![]); + let processed = processor + .execute_block(&block, &root, store) + .expect("block"); + assert!( + processed.unlogged_peg_events.is_empty(), + "{:?}", + processed.unlogged_peg_events + ); + assert!( + !crate::silent::is_recording(), + "the thread records once the block is done" + ); + } + + /// What a block carries is what its commit stores, on whatever thread it + /// commits: a decision the Bridge logs no event for becomes that event, + /// filed under the block. + #[test] + fn a_commit_stores_the_peg_decisions_its_block_carries() { + use crate::silent::{FieldValue, SilentKind, SilentRecord}; + let dir = tempfile::tempdir().unwrap(); + let events = Arc::new( + rustock_storage::legacy_event_store::LegacyEventStore::open( + dir.path().to_str().unwrap(), + ) + .unwrap(), + ); + let (block, root, store, block_store) = bridge_calling_block(None, None); + let processor = BlockProcessor::new(RskHardforkConfig::testnet(), block_store) + .with_legacy_event_store(events.clone()); + let mut processed = processor + .execute_block(&block, &root, store) + .expect("block"); + let number = block.header.number; + processed.unlogged_peg_events = vec![SilentRecord { + kind: SilentKind::PegoutRequested, + block: number, + tx_index: 0, + fields: vec![ + ("rskTxHash", FieldValue::Bytes(vec![1; 32])), + ("btcTxHash", FieldValue::Bytes(vec![2; 32])), + ("amountSat", FieldValue::U64(5)), + ], + }]; + processor.commit_receipts(&block, &processed).unwrap(); + let topic = crate::bridge::legacy_events::release_requested(&[1; 32], &[2; 32], 5).topic0; + assert_eq!(events.topics().unwrap(), vec![(topic, 1, number, number)]); + } + /// A block whose transactions reach nothing of the sort is unaffected, /// partition or no partition. #[test] @@ -1618,6 +1905,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]), @@ -1628,31 +1917,307 @@ 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(()) + ); + // 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), + Err(ValidationError::InvalidSublistEdges { .. }) + )); + + // 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(()) + ); + } + + /// `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. + #[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. + #[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 - /// given. Returns everything `execute_block` needs. + /// given, or calls a contract whose code is `through` (hex). Returns + /// everything `execute_block` needs. #[allow(clippy::type_complexity)] fn bridge_calling_block( edges: Option>, + through: Option<&str>, ) -> (Block, TrieNode, Arc, Arc) { let store: Arc = Arc::new(MemoryTrieStore::new()); let chain_id = 31u64; let key = SigningKey::from_slice(&[9u8; 32]).unwrap(); let sender = sender_address(&key); - let root = put_account( + let mut root = put_account( &TrieNode::empty(), store.as_ref(), &sender, 0, U256::from(10u64).pow(U256::from(18)), ); + let mut to = crate::precompiles::BRIDGE_ADDR; + let mut input = Bytes::from(vec![0x00, 0x00, 0x00, 0x00]); + if let Some(code) = through { + to = Address::repeat_byte(0xC1); + input = Bytes::new(); + root = put_account(&root, store.as_ref(), &to, 0, U256::ZERO); + let code_key = rustock_trie::code_key(&to); + root = root.put( + &TrieKeySlice::from_key(&code_key), + &hex::decode(code).unwrap(), + store.as_ref(), + ); + } let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); @@ -1662,9 +2227,9 @@ mod tests { nonce: 0, gas_price: U256::from(0), gas_limit: U256::from(500_000), - to: Bytes::copy_from_slice(crate::precompiles::BRIDGE_ADDR.as_slice()), + to: Bytes::copy_from_slice(to.as_slice()), value: U256::ZERO, - input: Bytes::from(vec![0x00, 0x00, 0x00, 0x00]), + input, v: 0, r: U256::ZERO, s: U256::ZERO, @@ -1673,6 +2238,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, @@ -2037,19 +2604,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 @@ -2255,9 +2811,16 @@ mod tests { assert_eq!( writes, 2, "put_receipts is called {writes} times in this crate's production code, not 2 \ - (the unitrie and Orchid arms of process_and_commit). A new writer means the \ - receipt index no longer proves this node executed the block -- see \ - was_executed_locally." + (commit_by_hash and execute_and_commit). A new writer means the receipt index no \ + longer proves this node executed the block -- see was_executed_locally." + ); + // The receipts of a block executed beside the chain are written by + // `process_beside`, through `commit_by_hash`, which must not be + // reachable from outside without a block having run. + assert!( + !production.contains("pub fn commit_by_hash"), + "commit_by_hash writes the receipt index and must stay private: only \ + process_and_commit and process_beside reach it, after the block has run" ); } @@ -2792,102 +3355,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] @@ -2926,6 +3424,186 @@ 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 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 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()); + 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, 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) + } + + /// 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)] @@ -3075,28 +3753,36 @@ 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" + ); -/// 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, + 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/raw_storage.rs b/crates/execution/src/raw_storage.rs index 1ccaca4..ffed322 100644 --- a/crates/execution/src/raw_storage.rs +++ b/crates/execution/src/raw_storage.rs @@ -12,15 +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). -//! -//! 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. +//! its calls' committed writes); +//! - 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; @@ -39,6 +41,108 @@ 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, + /// 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)>, + /// 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
, + /// The contracts whose code a creation in this block saved, empty or not: + /// rskj's block repository holds what the block writes in a cache until + /// the block ends, the code key of each among it + /// (`crate::rsk_instructions::ContractMarker::code_saved_in_block`). A + /// creation saves its code as it ends, a contract's constructor having + /// run (`Program.finalizeContractCreation`) or a transaction's + /// (`TransactionExecutor.createContract`); a creation's frame opening + /// at an address starts it with none. Before RSKIP453 a contract's + /// creation whose code is past the limit or can't be paid for keeps its + /// contract and saves no code. + pub code_saved: 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, + /// 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, + /// What a local call has taken accounts below zero by ([`Debts`]). + /// Nothing in a block's run. + pub debts: Debts, + /// 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, + /// Whether the Bridge call running has read the federation stored as the + /// new one. rskj's `FederationStorageProviderImpl.getNewFederation` keeps + /// what it read, and `saveNewFederation` writes it back as a call that + /// read it returns; the Bridge clears this as each call begins. + pub new_federation_read: bool, +} + +/// What accounts owe: each one's balance in rskj is what the journal holds +/// for it less what it owes here, and is below zero where it owes the more. +/// +/// rskj's `Repository.transfer` takes what it moves off the sender whatever +/// the sender holds, and where it is called without a check of the balance a +/// sender that held less is left below zero: a local call's own sender +/// (`TransactionExecutor.call`), and the Bridge as it pays +/// (`BridgeSupport.transferTo`). No balance here can show that, so the +/// account is lifted to what it sends before that moves, and what it was +/// lifted by is kept here (`RskHandler`, `bridge::storage::bridge_transfer`). +/// BALANCE and SELFBALANCE read the account less that +/// (`rsk_instructions::LocalCallWords`), an account that holds less than it +/// owes can call and create nothing (`rsk_handler::unpaid_by_debtor`), and +/// one that destroys itself hands what it still owes to its beneficiary +/// (`rsk_instructions::hand_on_debt`). +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct Debts(Vec<(Address, U256)>); + +impl Debts { + /// What `address` owes, which is nothing unless it is below zero. + pub fn of(&self, address: Address) -> U256 { + self.0 + .iter() + .find(|(debtor, _)| *debtor == address) + .map_or(U256::ZERO, |(_, owed)| *owed) + } + + /// `address` owes `amount` more, which is more than nothing. + pub fn add(&mut self, address: Address, amount: U256) { + match self.0.iter_mut().find(|(debtor, _)| *debtor == address) { + Some((_, owed)) => *owed = owed.saturating_add(amount), + None => self.0.push((address, amount)), + } + } + + /// `address` owes nothing from here on. What it owed. + pub fn take(&mut self, address: Address) -> U256 { + match self.0.iter().position(|(debtor, _)| *debtor == address) { + Some(at) => self.0.swap_remove(at).1, + None => U256::ZERO, + } + } } /// Raw-bytes storage overlay for the block being executed. @@ -50,6 +154,15 @@ 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, + /// 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)>, } @@ -101,43 +214,86 @@ 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(); + } + + /// 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). + /// 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(); + 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 nothing. - 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). pub fn drain(&mut self) -> Vec { self.pending.clear(); self.tx_start = 0; + self.kept.clear(); + self.kept_committed = 0; + self.reverted.clear(); std::mem::take(&mut self.committed) } } @@ -214,4 +370,124 @@ 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().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. + 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]))]); + } + + /// 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/remasc.rs b/crates/execution/src/remasc.rs index eef3dd9..2db658e 100644 --- a/crates/execution/src/remasc.rs +++ b/crates/execution/src/remasc.rs @@ -6,7 +6,7 @@ /// - Journal-based sload/sstore and balance transfer helpers /// - `process_miners_fees`: core fee distribution algorithm use alloy_primitives::{Address, U256}; -use revm::context_interface::{Block as BlockTr, ContextTr, JournalTr}; +use revm::context_interface::JournalTr; use revm::precompile::PrecompileError; use rustock_storage::BlockStore; use std::sync::Arc; @@ -14,6 +14,7 @@ use std::sync::Arc; use alloy_primitives::B256; use rustock_core::Header; +use crate::hardfork::RskHardforkConfig; use crate::precompiles::REMASC_ADDR; /// Maximum number of blocks after a block in which its uncle can be included. @@ -44,10 +45,14 @@ pub struct RemascConfig { pub rsk_labs_address: Address, /// RSK Labs reward address (post-RSKIP218). pub rsk_labs_address_rskip218: Address, - /// Block height at which RSKIP218 activates (changes the labs payout address). - pub rskip218_activation_height: u64, - /// Block height at which RSKIP85 activates (minimum payable reward gates). - pub rskip85_activation_height: u64, + /// Where RSKIP218 is scheduled apart from its upgrade, as rskj's + /// `consensusRules` can schedule any rule: from this height, or never at + /// `u64::MAX`. `None`, every network's, reads it with iris300 from the + /// schedule ([`RskHardforkConfig::has_rskip218`]). + pub rskip218_activation_height: Option, + /// Where RSKIP85 is scheduled apart from orchid, likewise: `None` reads it + /// from the schedule ([`RskHardforkConfig::has_rskip85`]). + pub rskip85_activation_height: Option, /// RSKIP85: synthetic reward must exceed mgp * this (rskj Constants 200_000). pub minimum_payable_gas: u64, /// RSKIP85: per-federator share must exceed mgp * this (rskj Constants 50_000). @@ -73,8 +78,8 @@ impl RemascConfig { rsk_labs_address_rskip218: "dcb12179ba4697350f66224c959bdd9c282818df" .parse() .unwrap(), - rskip218_activation_height: 3_614_800, // rskj: rskip218 = iris300 - rskip85_activation_height: 729_000, // rskj: rskip85 = orchid + rskip218_activation_height: None, + rskip85_activation_height: None, minimum_payable_gas: 200_000, federator_minimum_payable_gas: 50_000, genesis_federation_public_keys: @@ -97,8 +102,8 @@ impl RemascConfig { rsk_labs_address_rskip218: "dabadabadabadabadabadabadabadabadaba0003" .parse() .unwrap(), - rskip218_activation_height: 2_060_500, // rskj: rskip218 = iris300 - rskip85_activation_height: 0, // rskj: rskip85 = orchid (testnet 0) + rskip218_activation_height: None, + rskip85_activation_height: None, minimum_payable_gas: 200_000, federator_minimum_payable_gas: 50_000, genesis_federation_public_keys: @@ -121,8 +126,8 @@ impl RemascConfig { rsk_labs_address_rskip218: "dabadabadabadabadabadabadabadabadaba0001" .parse() .unwrap(), - rskip218_activation_height: 0, - rskip85_activation_height: 0, + rskip218_activation_height: None, + rskip85_activation_height: None, minimum_payable_gas: 200_000, federator_minimum_payable_gas: 50_000, genesis_federation_public_keys: @@ -130,14 +135,28 @@ impl RemascConfig { } } - /// Returns the correct RSK Labs address for the given block height. - pub fn labs_address_at(&self, block_number: u64) -> Address { - if block_number >= self.rskip218_activation_height { + /// rskj `Remasc.getRskLabsAddress`: the Core Developers Fund's address, + /// the new one where RSKIP218 is on at the executing block `block_number`. + pub fn labs_address_at(&self, hardfork_cfg: &RskHardforkConfig, block_number: u64) -> Address { + let rskip218 = match self.rskip218_activation_height { + Some(height) => block_number >= height, + None => hardfork_cfg.has_rskip218(block_number), + }; + if rskip218 { self.rsk_labs_address_rskip218 } else { self.rsk_labs_address } } + + /// rskj `Remasc.shouldRewardBeAboveMinimumPayableGas`: whether RSKIP85 is + /// on at the executing block `block_number`. + pub fn rskip85_active(&self, hardfork_cfg: &RskHardforkConfig, block_number: u64) -> bool { + match self.rskip85_activation_height { + Some(height) => block_number >= height, + None => hardfork_cfg.has_rskip85(block_number), + } + } } // --------------------------------------------------------------------------- @@ -184,6 +203,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 // --------------------------------------------------------------------------- @@ -217,64 +272,148 @@ 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, } -/// Collect siblings for the processing block from the canonical chain. -/// -/// Walks blocks `candidate_number + 1` through `candidate_number + UNCLE_GENERATION_LIMIT`, -/// checking each block's ommers for headers with `number == candidate_number`. -fn collect_siblings( - block_store: &BlockStore, - candidate_number: u64, - current_number: u64, -) -> Result, PrecompileError> { - let mut siblings = Vec::new(); - let end = (candidate_number + UNCLE_GENERATION_LIMIT).min(current_number); - - for height in (candidate_number + 1)..=end { - let hash = match block_store - .canonical_hash(height) - .map_err(|e| PrecompileError::other(format!("REMASC: {e}")))? - { - Some(h) => h, - None => continue, - }; +/// What REMASC reads of the chain the executing block is on: the processing +/// block, and after it the blocks whose uncles may be its siblings, each with +/// its uncles, nearest first. +struct Lineage { + processing: (B256, Header), + descendants: Vec<(Header, Vec
)>, +} - let including_header = block_store - .header(hash) - .map_err(|e| PrecompileError::other(format!("REMASC: {e}")))?; - let including_header = match including_header { - Some(h) => h, - None => continue, - }; +/// rskj `Remasc.processMinersFees` 128-148: from the executing block's +/// parent, the block `maturity - 1 - uncleGenerationLimit` below it, found as +/// `IndexedBlockStore.getBlockAtDepthStartingAt` finds it, then its parents, +/// `uncleGenerationLimit` blocks in all, each with its uncles +/// (`getSiblingsFromBlockByHash`); the parent of the last is the processing +/// block. Every one of them is the executing block's own ancestor, so a block +/// on a fork reads its fork, not the main chain's blocks at those heights. +fn lineage(store: &BlockStore, parent: B256, maturity: u64) -> Result { + // rskj's depth is a `long`: below 0, which no network's maturity gives, + // its walk starts at the parent itself, as at 0. + let depth = maturity.saturating_sub(1 + UNCLE_GENERATION_LIMIT); + let (mut hash, mut header) = block_at_depth(store, parent, depth)?; + let mut descendants = Vec::with_capacity(UNCLE_GENERATION_LIMIT as usize); + for _ in 0..UNCLE_GENERATION_LIMIT { + let uncles = uncles_of(store, hash, &header)?; + hash = header.parent_hash; + let parent = stored_header(store, hash)?; + descendants.push((std::mem::replace(&mut header, parent), uncles)); + } + descendants.reverse(); + Ok(Lineage { + processing: (hash, header), + descendants, + }) +} + +/// rskj `IndexedBlockStore.getBlockAtDepthStartingAt` (134-156): the block +/// `depth` below `start`, found up its parents until one is the main chain's +/// at its height, whose index then names the rest. +fn block_at_depth( + store: &BlockStore, + start: B256, + depth: u64, +) -> Result<(B256, Header), PrecompileError> { + let first = stored_header(store, start)?; + if depth == 0 { + return Ok((start, first)); + } + // rskj finds no block where `start` is numbered `depth` or lower, and + // REMASC then throws. + let target = first + .number + .checked_sub(depth) + .filter(|target| *target > 0) + .ok_or_else(|| missing(format!("ancestor {depth} below block #{}", first.number)))?; + let (mut hash, mut header) = (start, first); + for _ in 0..depth { + if store.canonical_hash(header.number).map_err(store_failed)? == Some(hash) { + let hash = store + .canonical_hash(target) + .map_err(store_failed)? + .ok_or_else(|| missing(format!("main-chain block #{target}")))?; + return Ok((hash, stored_header(store, hash)?)); + } + hash = header.parent_hash; + header = stored_header(store, hash)?; + } + Ok((hash, header)) +} - let body = block_store - .body(hash) - .map_err(|e| PrecompileError::other(format!("REMASC: {e}")))?; - let (_, ommers) = body.unwrap_or_default(); - - for ommer in &ommers { - if ommer.number == candidate_number { - siblings.push(Sibling { - hash: ommer.hash(), - coinbase: ommer.beneficiary, - paid_fees: ommer.paid_fees, - included_block_coinbase: including_header.beneficiary, - included_height: height, - uncle_count: ommer.uncle_count, - }); +/// The header stored under `hash`. +fn stored_header(store: &BlockStore, hash: B256) -> Result { + store + .header(hash) + .map_err(store_failed)? + .ok_or_else(|| missing(format!("header {hash:?}"))) +} + +/// A block's uncles (`Block.getUncleList`): its body's, or the freezer's once +/// the body of a main-chain block is pruned. A block with no uncles may be +/// kept without its body. +fn uncles_of( + store: &BlockStore, + hash: B256, + header: &Header, +) -> Result, PrecompileError> { + if let Some(uncles) = store.uncles(hash).map_err(store_failed)? { + return Ok(uncles); + } + if header.uncle_count == 0 { + return Ok(Vec::new()); + } + if store.canonical_hash(header.number).map_err(store_failed)? == Some(hash) { + if let Some(freezer) = store.freezer() { + if let Some(uncles) = freezer.uncles(header.number).map_err(store_failed)? { + return Ok(uncles); } } } + Err(missing(format!("uncles of block #{}", header.number))) +} + +/// The processing block's siblings, numbered `candidate`, among the +/// descendants' uncles: rskj's `getSiblingsToReward`, one for each uncle of +/// that number, in the order of the blocks and of their uncles, each paid as +/// the block that included it says (`Sibling`). +fn siblings_in(descendants: &[(Header, Vec
)], candidate: u64) -> Vec { + descendants + .iter() + .flat_map(|(including, uncles)| { + uncles + .iter() + .filter(move |uncle| uncle.number == candidate) + .map(move |uncle| Sibling { + hash: uncle.hash(), + coinbase: uncle.coinbase(), + paid_fees: uncle.paid_fees, + included_block_coinbase: including.coinbase(), + included_height: including.number, + uncle_count: uncle.uncle_count, + }) + }) + .collect() +} + +/// A store that fails to read leaves REMASC with no answer. +fn store_failed(error: impl std::fmt::Display) -> PrecompileError { + PrecompileError::other(format!("REMASC: {error}")) +} - Ok(siblings) +/// What the store lacks of the blocks REMASC reads, every one of which rskj +/// holds for any block it runs. +fn missing(what: String) -> PrecompileError { + PrecompileError::other(format!("REMASC: the block store has no {what}")) } // --------------------------------------------------------------------------- @@ -368,20 +507,29 @@ 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), + } + // 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); @@ -400,39 +548,83 @@ fn pay_mining_fees( )); } -/// 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 -/// the mainnet #4010 payout order. +/// 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; +} + +/// The active federation's members as REMASC pays them: 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 the mainnet #4010 payout order. +/// +/// rskj's REMASC builds its `FederationSupportImpl` on the processing block, +/// with the executing block's upgrades (`Remasc.payToFederation` 213-221): a +/// committed federation takes over once its age, counted to the processing +/// block, reaches the activation age the executing block's upgrades give +/// (`FederationSupportImpl` 72-103, `FederationConstants +/// .getFederationActivationAge`). From RSKIP415, also read at the executing +/// block (`Remasc.activations = forBlock(executionBlock.getNumber())`), a +/// federator is paid at the address of its RSK public key, and before it at +/// its BTC key's (`RemascFederationProvider.getFederatorAddress`). A member +/// whose key is the point at infinity is paid too, at that key's address +/// (`federation::member_rsk_address`). fn federation_rsk_addresses( ctx: &mut CTX, - config: &RemascConfig, bridge_config: &crate::bridge::constants::BridgeConstants, - hardfork_cfg: &crate::hardfork::RskHardforkConfig, + hardfork_cfg: &RskHardforkConfig, processing_block_number: u64, current_number: u64, ) -> Vec
{ + use crate::bridge::federation::{load_stored_federation, StoredFederation}; + use crate::bridge::storage::{NEW_FEDERATION_KEY, OLD_FEDERATION_KEY}; // The Bridge account may be cold outside revm's transact flow; journal // storage reads require the account to be loaded. - use revm::context_interface::JournalTr; let _ = ctx.journal_mut().load_account(crate::precompiles::BRIDGE_ADDR); - // RSKIP415 (arrowhead600): rskj's `RemascFederationProvider.getFederatorAddress` - // switches from `getRskAddressFromBtcKey` to `getRskAddressFromRskKey`, i.e. it - // derives the federator RSK address from the member's RSK public key instead of - // its BTC public key. The activation is gated on the EXECUTION block number - // (rskj `Remasc.activations = forBlock(executionBlock.getNumber())`), not the - // processing/matured block. - let stored = crate::bridge::peg::federation_keys_or_genesis( - ctx, - bridge_config, - hardfork_cfg, - processing_block_number, - hardfork_cfg.has_rskip415(current_number), - ); - let _ = config; - stored + let want_rsk = hardfork_cfg.has_rskip415(current_number); + let keys = |federation: &StoredFederation| { + if want_rsk { + federation.rsk_keys() + } else { + federation.btc_keys() + } + }; + let new = load_stored_federation(ctx, NEW_FEDERATION_KEY); + let old = load_stored_federation(ctx, OLD_FEDERATION_KEY); + let members = match (new, old) { + (None, _) => crate::bridge::peg::genesis_federation_keys(bridge_config), + (Some(new), None) => keys(&new), + (Some(new), Some(old)) => { + let age = crate::bridge::governance::federation_activation_age( + bridge_config, + hardfork_cfg, + current_number, + ); + if processing_block_number >= new.creation_block.saturating_add(age) { + keys(&new) + } else { + keys(&old) + } + } + }; + members .iter() - .filter_map(|k| crate::bridge::federation::rsk_address_from_public_key(k)) + .filter_map(crate::bridge::federation::member_rsk_address) .collect() } @@ -444,10 +636,10 @@ fn pay_to_federation( ctx: &mut CTX, config: &RemascConfig, bridge_config: &crate::bridge::constants::BridgeConstants, - hardfork_cfg: &crate::hardfork::RskHardforkConfig, + hardfork_cfg: &RskHardforkConfig, processing_block_hash: alloy_primitives::B256, processing_block_number: u64, - current_number: u64, + executing: &Header, synthetic_reward: U256, ) -> U256 { let federation_reward = synthetic_reward / U256::from(config.federation_divisor); @@ -461,11 +653,10 @@ fn pay_to_federation( // genesis federation). let federators = federation_rsk_addresses( ctx, - config, bridge_config, hardfork_cfg, processing_block_number, - current_number, + executing.number, ); if federators.is_empty() { remasc_sstore(ctx, fed_key, pay_total); @@ -476,9 +667,12 @@ fn pay_to_federation( let per_federator = pay_total / n; let remainder = pay_total % n; - if current_number >= config.rskip85_activation_height { - let min_payable = U256::from(ctx.block().basefee()) - * U256::from(config.federator_minimum_payable_gas); + if config.rskip85_active(hardfork_cfg, executing.number) { + // The executing block's minimum gas price, whole, as rskj's `Coin` + // multiplies it: revm's base fee holds no more than 64 bits of it. + let min_payable = executing + .minimum_gas_price + .saturating_mul(U256::from(config.federator_minimum_payable_gas)); if per_federator < min_payable { remasc_sstore(ctx, fed_key, pay_total); return federation_reward; @@ -490,7 +684,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 } @@ -498,15 +692,17 @@ fn pay_to_federation( /// Implements rskj's `Remasc.processMinersFees()`. /// /// This runs when the REMASC precompile is called (last tx of each block). -/// It distributes fees from the block that matured `config.maturity` blocks ago. +/// It distributes fees from the block that matured `config.maturity` blocks +/// ago on the chain of `executing`, the block it runs in. pub fn process_miners_fees( ctx: &mut CTX, config: &RemascConfig, bridge_config: &crate::bridge::constants::BridgeConstants, - hardfork_cfg: &crate::hardfork::RskHardforkConfig, + hardfork_cfg: &RskHardforkConfig, block_store: Option<&Arc>, + executing: &Header, ) -> Result<(), PrecompileError> { - let current_number = ctx.block().number().to::(); + let current_number = executing.number; let candidate_block_number = match current_number.checked_sub(config.maturity) { Some(n) if n >= 1 => n, @@ -516,16 +712,13 @@ pub fn process_miners_fees( let block_store = block_store .ok_or_else(|| PrecompileError::other("REMASC: no block store configured"))?; - // Find the processing block (the block whose fees are being distributed) - let processing_hash = block_store - .canonical_hash(candidate_block_number) - .map_err(|e| PrecompileError::other(format!("REMASC: {e}")))? - .ok_or_else(|| PrecompileError::other("REMASC: processing block not found"))?; - - let processing_header = block_store - .header(processing_hash) - .map_err(|e| PrecompileError::other(format!("REMASC: {e}")))? - .ok_or_else(|| PrecompileError::other("REMASC: processing header not found"))?; + // The processing block (the block whose fees are being distributed), and + // the blocks after it whose uncles may be its siblings: the executing + // block's own ancestors. + let Lineage { + processing: (processing_hash, processing_header), + descendants, + } = lineage(block_store, executing.parent_hash, config.maturity)?; // Accrue the processing block's fees into rewardBalance let reward_key = remasc_storage_key(REWARD_BALANCE_KEY); @@ -538,8 +731,8 @@ pub fn process_miners_fees( return Ok(()); } - // Collect siblings from descendant blocks - let siblings = collect_siblings(block_store, candidate_block_number, current_number)?; + // The siblings the descendant blocks included as uncles + let siblings = siblings_in(&descendants, candidate_block_number); // Selection rule let broken_key = remasc_storage_key(BROKEN_SELECTION_RULE_KEY); @@ -580,9 +773,12 @@ pub fn process_miners_fees( // RSKIP85: skip distribution while the synthetic reward is below the // minimum payable amount (checked before debiting the reward balance). - if current_number >= config.rskip85_activation_height { - let min_payable = - U256::from(ctx.block().basefee()) * U256::from(config.minimum_payable_gas); + // The executing block's minimum gas price goes in whole, as rskj's `Coin` + // multiplies it: revm's base fee holds no more than 64 bits of it. + if config.rskip85_active(hardfork_cfg, current_number) { + let min_payable = executing + .minimum_gas_price + .saturating_mul(U256::from(config.minimum_payable_gas)); if synthetic_reward < min_payable { return Ok(()); } @@ -594,8 +790,8 @@ 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); + let labs_addr = config.labs_address_at(hardfork_cfg, current_number); + 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) @@ -607,7 +803,7 @@ pub fn process_miners_fees( hardfork_cfg, processing_hash, candidate_block_number, - current_number, + executing, remaining, ); remaining -= federation_reward; @@ -621,7 +817,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(()) @@ -670,7 +871,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. @@ -803,6 +1009,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 // ----------------------------------------------------------------------- @@ -833,6 +1074,7 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } @@ -841,9 +1083,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, } @@ -1042,3 +1284,6 @@ mod chain_selection_tests { ); } } + +#[cfg(test)] +mod parity_tests; diff --git a/crates/execution/src/remasc/parity_tests.rs b/crates/execution/src/remasc/parity_tests.rs new file mode 100644 index 0000000..d0f1d11 --- /dev/null +++ b/crates/execution/src/remasc/parity_tests.rs @@ -0,0 +1,715 @@ +//! REMASC as rskj's `Remasc.processMinersFees` runs it: on the chain of the +//! block it runs in, read up from that block's parent; with the upgrades of +//! that block, read from the network's schedule; and against that block's +//! minimum gas price, whole. + +use std::sync::Arc; + +use alloy_primitives::{hex, Address, Bloom, Bytes, B256, U256}; +use rustock_core::Header; +use rustock_storage::freezer::Freezer; +use rustock_storage::BlockStore; +use rustock_trie::{ + account_key, storage_key, AccountState, MemoryTrieStore, TrieKeySlice, TrieNode, +}; + +use super::{ + remasc_storage_key, RemascConfig, BURNED_BALANCE_KEY, FEDERATION_BALANCE_KEY, + REWARD_BALANCE_KEY, +}; +use crate::bridge::federation::{ + rsk_address_from_public_key, serialize_federation_multikey, StoredMember, +}; +use crate::bridge::storage::{bridge_storage_key, NEW_FEDERATION_KEY, OLD_FEDERATION_KEY}; +use crate::executor::ExecutionError; +use crate::hardfork::{RskHardforkConfig, RskNetworkUpgrade}; +use crate::precompiles::{BRIDGE_ADDR, REMASC_ADDR}; +use crate::RskExecutor; + +/// Two federators' public keys. +const KEY_A: &str = "03a5e32151f974c35c5d08af36a8d6af0593248e8e4adca7ed0148192eb2f5d1bf"; +const KEY_B: &str = "02f3b5fb3121932db9450121b0a37f3f051dc8b3fd5f1ea5e7cf726947d8f69c28"; + +/// What REMASC holds before its transaction runs: enough to pay anything. +fn plenty() -> U256 { + U256::from(1) << 100 +} + +/// The miner of block `number` on the chain tagged `chain`. +fn miner(chain: u8, number: u64) -> Address { + let mut bytes = [chain; 20]; + bytes[19] = number as u8; + Address::from(bytes) +} + +/// Block `number` on `parent`, mined by `coinbase`, whose transactions paid +/// `fees`, naming `uncles` uncles. +fn block(number: u64, parent: B256, coinbase: Address, fees: U256, uncles: u64) -> Header { + Header { + parent_hash: parent, + ommers_hash: B256::ZERO, + beneficiary: coinbase, + state_root: B256::ZERO, + transactions_root: B256::ZERO, + receipts_root: B256::ZERO, + logs_bloom: Bloom::ZERO, + extension_data: None, + difficulty: U256::from(1_000_000), + number, + gas_limit: U256::from(6_800_000), + gas_used: 0, + timestamp: 1_700_000_000 + number, + extra_data: Bytes::new(), + paid_fees: fees, + minimum_gas_price: U256::ZERO, + uncle_count: uncles, + 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, + kept: None, + } +} + +/// A block store, and the folder it lives in. +fn block_store() -> (tempfile::TempDir, Arc) { + let folder = tempfile::tempdir().expect("a folder for the block store"); + let store = Arc::new(BlockStore::open(folder.path()).expect("a block store")); + (folder, store) +} + +/// Keeps blocks `first` to `last`, each on the one before it and the first +/// on `parent`, as `make` builds each from its number and its parent's hash, +/// with its uncles; as the main chain's blocks where `main`. Their hashes, in +/// order. +fn keep( + store: &BlockStore, + first: u64, + parent: B256, + last: u64, + main: bool, + mut make: impl FnMut(u64, B256) -> (Header, Vec
), +) -> Vec { + let mut hashes = Vec::new(); + let mut parent = parent; + for number in first..=last { + let (header, uncles) = make(number, parent); + let hash = header.hash(); + store.put_header_with_hash(hash, &header).unwrap(); + if !uncles.is_empty() { + store.put_body(hash, &[], &uncles).unwrap(); + } + if main { + store.put_canonical_hash(number, hash).unwrap(); + } + hashes.push(hash); + parent = hash; + } + hashes +} + +/// Blocks 0 to `last` of the main chain, mined by `miner(0xa0, number)`, +/// whose transactions paid `fees(number)`, with no uncles. +fn main_chain(store: &BlockStore, last: u64, fees: impl Fn(u64) -> U256) -> Vec { + keep(store, 0, B256::ZERO, last, true, |number, parent| { + ( + block(number, parent, miner(0xa0, number), fees(number), 0), + vec![], + ) + }) +} + +/// `account`'s balance in `state`: zero where it has no account. +fn balance(state: &TrieNode, trie: &MemoryTrieStore, account: Address) -> U256 { + let key = account_key(&account); + state + .get(&TrieKeySlice::from_key(&key), trie) + .map_or(U256::ZERO, |data| { + AccountState::decode(&data).expect("an account").balance + }) +} + +/// The word REMASC keeps under `name` in `state`. +fn remasc_word(state: &TrieNode, trie: &MemoryTrieStore, name: &str) -> U256 { + let key = storage_key(&REMASC_ADDR, &B256::from(remasc_storage_key(name))); + state + .get(&TrieKeySlice::from_key(&key), trie) + .map_or(U256::ZERO, |value| U256::from_be_slice(&value)) +} + +/// `state` with an account at `address` holding `balance`. +fn with_account( + state: TrieNode, + trie: &MemoryTrieStore, + address: Address, + balance: U256, +) -> TrieNode { + let key = account_key(&address); + let account = AccountState::new(U256::ZERO, balance).encode(); + state.put(&TrieKeySlice::from_key(&key), &account, trie) +} + +/// `state` with `value` at `key` of `address`'s storage, written as rskj +/// writes a word: without its leading zeros. +fn with_storage( + state: TrieNode, + trie: &MemoryTrieStore, + address: Address, + key: U256, + value: &[u8], +) -> TrieNode { + let key = storage_key(&address, &B256::from(key)); + state.put(&TrieKeySlice::from_key(&key), value, trie) +} + +/// `state` with a Bridge that keeps `new` as its new federation and `old`, +/// where there is one, as its old. +fn with_federations( + state: TrieNode, + trie: &MemoryTrieStore, + new: &[u8], + old: Option<&[u8]>, +) -> TrieNode { + let state = with_account(state, trie, BRIDGE_ADDR, U256::ZERO); + let new_key = bridge_storage_key(NEW_FEDERATION_KEY); + let state = with_storage(state, trie, BRIDGE_ADDR, new_key, new); + match old { + Some(old) => { + let old_key = bridge_storage_key(OLD_FEDERATION_KEY); + with_storage(state, trie, BRIDGE_ADDR, old_key, old) + } + None => state, + } +} + +/// Runs REMASC's transaction alone in `executing`, on `hardforks`, from a +/// state where REMASC holds plenty and that `seed` adds to: the state after +/// it, or what refused the block. +fn run_remasc( + store: Arc, + hardforks: RskHardforkConfig, + executing: &Header, + seed: impl FnOnce(TrieNode, &MemoryTrieStore) -> TrieNode, +) -> Result<(TrieNode, Arc), ExecutionError> { + let trie = Arc::new(MemoryTrieStore::new()); + let state = with_account(TrieNode::empty(), &trie, REMASC_ADDR, plenty()); + let state = seed(state, &trie); + let remasc = rustock_core::Transaction::remasc(executing.number); + let result = RskExecutor::new(hardforks, store).execute_block( + executing, + &[(remasc, Address::ZERO)], + &state, + trie.clone(), + )?; + let after = crate::state::apply_state_changes( + &state, + trie.as_ref(), + &result.state_changes, + &result.markers, + ); + Ok((after, trie)) +} + +/// On regtest (maturity 10, a synthetic span of 5), block 16 pays for block +/// 6. A fork from block 2 has a block 6 of its own, mined by its own miner +/// with fees of its own, and block 16 on that fork pays for that block 6, as +/// rskj finds it up from block 16's parent: of 42,000 / 5 = 8,400, RSK Labs +/// gets 1,680, the federation's 67 are kept, as this regtest has none, and +/// the fork's miner gets 6,653. The miner of the main chain's block 6 gets +/// nothing. +#[test] +fn on_a_fork_remasc_pays_the_block_that_matured_on_the_fork() { + let (_folder, store) = block_store(); + let fees_at_6 = |fees: u64| move |number: u64| U256::from(if number == 6 { fees } else { 0 }); + let main = main_chain(&store, 15, fees_at_6(21_000)); + let fork = keep(&store, 3, main[2], 15, false, |number, parent| { + let fees = fees_at_6(42_000)(number); + (block(number, parent, miner(0xf0, number), fees, 0), vec![]) + }); + let executing = block(16, fork[fork.len() - 1], miner(0xf0, 16), U256::ZERO, 0); + + let (state, trie) = run_remasc( + store, + RskHardforkConfig::regtest(), + &executing, + |state, _| state, + ) + .unwrap(); + + assert_eq!(balance(&state, &trie, miner(0xf0, 6)), U256::from(6_653)); + assert_eq!(balance(&state, &trie, miner(0xa0, 6)), U256::ZERO); + assert_eq!( + remasc_word(&state, &trie, REWARD_BALANCE_KEY), + U256::from(42_000 - 8_400) + ); +} + +/// A fork from block 7 shares block 6 with the main chain, and its block 8 +/// names an uncle numbered 6 that no main-chain block names. Block 16 on the +/// fork pays that sibling, as rskj reads the uncles of blocks 7 to 13 of the +/// fork: of block 6's 3,327, its publisher, block 8's miner, gets a tenth, +/// 332; the two miners 1,497 each, with 1 burned; and the sibling, a block +/// late, loses a twentieth of its share, 74, burned too. +#[test] +fn on_a_fork_remasc_pays_the_siblings_the_fork_included() { + let (_folder, store) = block_store(); + let main = main_chain(&store, 15, |number| { + U256::from(if number == 6 { 21_000 } else { 0 }) + }); + let sibling = block(6, main[5], miner(0x5b, 6), U256::ZERO, 0); + let fork = keep(&store, 8, main[7], 15, false, |number, parent| { + let uncles = if number == 8 { + vec![sibling.clone()] + } else { + vec![] + }; + let header = block( + number, + parent, + miner(0xf0, number), + U256::ZERO, + uncles.len() as u64, + ); + (header, uncles) + }); + let executing = block(16, fork[fork.len() - 1], miner(0xf0, 16), U256::ZERO, 0); + + let (state, trie) = run_remasc( + store, + RskHardforkConfig::regtest(), + &executing, + |state, _| state, + ) + .unwrap(); + + assert_eq!(balance(&state, &trie, miner(0xa0, 6)), U256::from(1_497)); + assert_eq!(balance(&state, &trie, miner(0x5b, 6)), U256::from(1_423)); + assert_eq!(balance(&state, &trie, miner(0xf0, 8)), U256::from(332)); + assert_eq!( + remasc_word(&state, &trie, BURNED_BALANCE_KEY), + U256::from(1 + 74) + ); +} + +/// The payments REMASC logs in `executing` on regtest, in their order: to +/// whom, and how much. +fn payments_in(store: Arc, executing: &Header) -> Vec<(Address, U256)> { + use alloy_rlp::Decodable; + let trie = Arc::new(MemoryTrieStore::new()); + let state = with_account(TrieNode::empty(), &trie, REMASC_ADDR, plenty()); + let remasc = rustock_core::Transaction::remasc(executing.number); + let result = RskExecutor::new(RskHardforkConfig::regtest(), store) + .execute_block(executing, &[(remasc, Address::ZERO)], &state, trie) + .unwrap(); + let topic = crate::bridge::events::legacy_topic("mining_fee_topic"); + 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() +} + +/// rskj pays the siblings in the order of the blocks that included them, the +/// processing block's child first (`Remasc.getSiblingsToReward` over its +/// stack of descendants): their publishers, then the siblings, then the +/// processing block's miner, and the receipt keeps the logs in that order. +/// Block 16 pays block 6, one of whose siblings block 7 included and the +/// other block 9: of block 6's 3,327, each publisher gets 166; each miner +/// 998, but the sibling block 9 included, two blocks late, 899. +#[test] +fn remasc_pays_siblings_in_the_order_of_the_blocks_that_included_them() { + let (_folder, store) = block_store(); + let main = main_chain(&store, 6, |number| { + U256::from(if number == 6 { 21_000 } else { 0 }) + }); + let in_7 = block(6, main[5], miner(0x57, 6), U256::ZERO, 0); + let in_9 = block(6, main[5], miner(0x59, 6), U256::ZERO, 0); + let after = keep(&store, 7, main[6], 15, true, |number, parent| { + let uncles = match number { + 7 => vec![in_7.clone()], + 9 => vec![in_9.clone()], + _ => vec![], + }; + let header = block( + number, + parent, + miner(0xa0, number), + U256::ZERO, + uncles.len() as u64, + ); + (header, uncles) + }); + let executing = block(16, after[after.len() - 1], miner(0xa0, 16), U256::ZERO, 0); + + let labs = RemascConfig::regtest().labs_address_at(&RskHardforkConfig::regtest(), 16); + let paid = |to: Address, amount: u64| (to, U256::from(amount)); + assert_eq!( + payments_in(store, &executing), + vec![ + paid(labs, 840), + paid(miner(0xa0, 7), 166), + paid(miner(0xa0, 9), 166), + paid(miner(0x57, 6), 998), + paid(miner(0x59, 6), 899), + paid(miner(0xa0, 6), 998), + ] + ); +} + +/// A block REMASC reads that names an uncle the store keeps no body for is a +/// gap in the node's store, which rskj never has: the block can't be run here +/// and now, and no rule refuses it. To pay as though block 8 had no uncles +/// would be to pay what rskj may not. +#[test] +fn uncles_the_store_lacks_are_the_stores_fault_and_not_the_blocks() { + let (_folder, store) = block_store(); + let main = keep(&store, 0, B256::ZERO, 15, true, |number, parent| { + let uncles = u64::from(number == 8); + ( + block(number, parent, miner(0xa0, number), U256::ZERO, uncles), + vec![], + ) + }); + let executing = block(16, main[15], miner(0xa0, 16), U256::ZERO, 0); + + let Err(refused) = run_remasc( + store, + RskHardforkConfig::regtest(), + &executing, + |state, _| state, + ) else { + panic!("the block can't be run without block 8's uncles"); + }; + assert!( + matches!(refused, ExecutionError::Database(_)), + "{refused:?}" + ); +} + +/// Once a main-chain block's body is pruned, its uncles are the freezer's, +/// which keeps them by height for the main chain alone. Block 16 pays from +/// there the sibling the main chain's block 8 included; on a fork whose own +/// block 8 names an uncle the store keeps no body for, block 16 is not +/// answered with the main chain's. +#[test] +fn the_freezer_answers_for_the_main_chains_uncles_alone() { + let (folder, store) = block_store(); + let freezer = Arc::new(Freezer::open(folder.path().join("freezer")).expect("a freezer")); + store.set_freezer(freezer.clone()); + let main = keep(&store, 0, B256::ZERO, 15, true, |number, parent| { + let fees = U256::from(if number == 6 { 21_000 } else { 0 }); + let uncles = u64::from(number == 8); + ( + block(number, parent, miner(0xa0, number), fees, uncles), + vec![], + ) + }); + let sibling = block(6, main[5], miner(0x5b, 6), U256::ZERO, 0); + freezer.put_uncles(8, &[sibling]).unwrap(); + freezer.sync().unwrap(); + let fork = keep(&store, 8, main[7], 15, false, |number, parent| { + let uncles = u64::from(number == 8); + ( + block(number, parent, miner(0xf0, number), U256::ZERO, uncles), + vec![], + ) + }); + + let on_main = block(16, main[15], miner(0xa0, 16), U256::ZERO, 0); + let regtest = RskHardforkConfig::regtest; + let (state, trie) = run_remasc(store.clone(), regtest(), &on_main, |state, _| state).unwrap(); + assert_eq!(balance(&state, &trie, miner(0x5b, 6)), U256::from(1_423)); + + let on_fork = block(16, fork[fork.len() - 1], miner(0xf0, 16), U256::ZERO, 0); + let Err(refused) = run_remasc(store, regtest(), &on_fork, |state, _| state) else { + panic!("the fork's block 8 names an uncle the store doesn't keep"); + }; + assert!( + matches!(refused, ExecutionError::Database(_)), + "{refused:?}" + ); +} + +/// Regtest with fingerroot500 and every upgrade after it at `height`. +fn fingerroot500_at(height: u64) -> RskHardforkConfig { + [ + RskNetworkUpgrade::Fingerroot500, + RskNetworkUpgrade::Arrowhead600, + RskNetworkUpgrade::Lovell700, + RskNetworkUpgrade::Reed800, + RskNetworkUpgrade::Vetiver900, + ] + .into_iter() + .fold(RskHardforkConfig::regtest(), |ladder, upgrade| { + ladder.with_hardfork(upgrade, Some(height)) + }) +} + +/// A federation of one member, whose three keys are `key`, committed at +/// block `created_at`; and the address its member is paid at. +fn federation_of(key: &str, created_at: u64) -> (Vec, Address) { + let key: [u8; 33] = hex::decode(key) + .expect("hex") + .try_into() + .expect("a compressed key"); + let member = StoredMember { + btc: key, + rsk: key, + mst: key, + }; + let address = rsk_address_from_public_key(&key).expect("a key"); + ( + serialize_federation_multikey(&[member], 0, created_at), + address, + ) +} + +/// REMASC runs its federation's rules on the processing block with the +/// executing block's upgrades. On regtest the activation age is 10 blocks +/// before RSKIP383 and 150 from it, which is at block 40 here. A federation +/// committed at block 19 is 10 blocks old at block 29 and 11 at block 30. +/// Block 39 pays for block 29 with the age of 10, which it has reached: the +/// new federation is the active one. Block 40 pays for block 30 with the age +/// of 150: the old one still is, though block 30 itself is below RSKIP383. +/// Each is paid 3,360 of block 29's or block 30's 2,100,000 in fees. +#[test] +fn remasc_holds_a_new_federation_to_the_activation_age_of_the_executing_block() { + let (new, to_new) = federation_of(KEY_A, 19); + let (old, to_old) = federation_of(KEY_B, 0); + let paid_at = |number: u64| { + let (_folder, store) = block_store(); + let fees = |n: u64| U256::from(if n == number - 10 { 2_100_000 } else { 0 }); + let main = main_chain(&store, number - 1, fees); + let executing = block( + number, + main[main.len() - 1], + miner(0xa0, number), + U256::ZERO, + 0, + ); + let (state, trie) = run_remasc(store, fingerroot500_at(40), &executing, |state, trie| { + with_federations(state, trie, &new, Some(&old)) + }) + .unwrap(); + ( + balance(&state, &trie, to_new), + balance(&state, &trie, to_old), + ) + }; + assert_eq!( + paid_at(39), + (U256::from(3_360), U256::ZERO), + "before RSKIP383" + ); + assert_eq!( + paid_at(40), + (U256::ZERO, U256::from(3_360)), + "from RSKIP383" + ); +} + +/// `value` written as rskj writes a word: without its leading zeros. +fn word(value: U256) -> Vec { + let bytes = value.to_be_bytes::<32>(); + let first = bytes.iter().position(|byte| *byte != 0).unwrap_or(31); + bytes[first..].to_vec() +} + +/// From RSKIP85 REMASC holds back a reward under the executing block's +/// minimum gas price times 200,000, the price read whole: at 2^64, which +/// revm's base fee can't hold, a reward one wei short waits in the reward +/// balance and one of exactly that is paid out of it. +#[test] +fn remasc_holds_back_a_reward_by_the_whole_minimum_gas_price() { + let price = U256::from(1) << 64; + let least = price * U256::from(200_000); + for (synthetic, paid) in [(least - U256::from(1), false), (least, true)] { + let (_folder, store) = block_store(); + let fees = synthetic * U256::from(5); + let main = main_chain( + &store, + 15, + |number| if number == 6 { fees } else { U256::ZERO }, + ); + let mut executing = block(16, main[15], miner(0xa0, 16), U256::ZERO, 0); + executing.minimum_gas_price = price; + let (state, trie) = run_remasc( + store, + RskHardforkConfig::regtest(), + &executing, + |state, _| state, + ) + .unwrap(); + let left = if paid { fees - synthetic } else { fees }; + assert_eq!( + remasc_word(&state, &trie, REWARD_BALANCE_KEY), + left, + "paid: {paid}" + ); + } +} + +/// Likewise a federator's share: from RSKIP85 it accrues in the federation's +/// balance while it is under the executing block's minimum gas price, read +/// whole, times 50,000, and is paid once it reaches that. +#[test] +fn remasc_holds_back_a_federators_share_by_the_whole_minimum_gas_price() { + let price = U256::from(1) << 64; + let least = price * U256::from(50_000); + // A reward well past its own minimum, whose federation cut, a 125th of + // it, the balance tops up to the share wanted. + let synthetic = price * U256::from(2_000_000); + let cut = synthetic / U256::from(125); + let (federation, _) = federation_of(KEY_A, 0); + for (share, paid) in [(least - U256::from(1), false), (least, true)] { + let (_folder, store) = block_store(); + let fees = synthetic * U256::from(5); + let main = main_chain( + &store, + 15, + |number| if number == 6 { fees } else { U256::ZERO }, + ); + let mut executing = block(16, main[15], miner(0xa0, 16), U256::ZERO, 0); + executing.minimum_gas_price = price; + let (state, trie) = run_remasc( + store, + RskHardforkConfig::regtest(), + &executing, + |state, trie| { + let state = with_federations(state, trie, &federation, None); + let kept = remasc_storage_key(FEDERATION_BALANCE_KEY); + with_storage(state, trie, REMASC_ADDR, kept, &word(share - cut)) + }, + ) + .unwrap(); + let kept = if paid { U256::ZERO } else { share }; + assert_eq!( + remasc_word(&state, &trie, FEDERATION_BALANCE_KEY), + kept, + "paid: {paid}" + ); + } +} + +/// Regtest with orchid and every upgrade after it at `height`. +fn orchid_at(height: u64) -> RskHardforkConfig { + [ + RskNetworkUpgrade::Orchid, + RskNetworkUpgrade::Wasabi100, + RskNetworkUpgrade::TwoToThree, + RskNetworkUpgrade::Papyrus200, + RskNetworkUpgrade::Iris300, + RskNetworkUpgrade::Hop400, + RskNetworkUpgrade::Fingerroot500, + RskNetworkUpgrade::Arrowhead600, + RskNetworkUpgrade::Lovell700, + RskNetworkUpgrade::Reed800, + RskNetworkUpgrade::Vetiver900, + ] + .into_iter() + .fold(RskHardforkConfig::regtest(), |ladder, upgrade| { + ladder.with_hardfork(upgrade, Some(height)) + }) +} + +/// RSKIP85 comes with orchid wherever the schedule puts it, read at the +/// executing block. With orchid at block 20 and a minimum gas price of 1, +/// block 19 pays block 9's synthetic reward of 4,200 and block 20 holds back +/// block 10's, 4,200 being under 200,000. Of a synthetic reward of +/// 1,000,000, past that, block 19 pays the federator its 8,000 and block 20 +/// keeps them in the federation's balance, 8,000 being under 50,000. +#[test] +fn rskip85_comes_with_orchid_where_the_schedule_puts_it() { + let (federation, federator) = federation_of(KEY_A, 0); + let run = |number: u64, fees: u64| { + let (_folder, store) = block_store(); + let main = main_chain(&store, number - 1, |n| { + U256::from(if n == number - 10 { fees } else { 0 }) + }); + let mut executing = block( + number, + main[main.len() - 1], + miner(0xa0, number), + U256::ZERO, + 0, + ); + executing.minimum_gas_price = U256::from(1); + let (state, trie) = run_remasc(store, orchid_at(20), &executing, |state, trie| { + with_federations(state, trie, &federation, None) + }) + .unwrap(); + ( + remasc_word(&state, &trie, REWARD_BALANCE_KEY), + balance(&state, &trie, federator), + remasc_word(&state, &trie, FEDERATION_BALANCE_KEY), + ) + }; + let (reward_balance, _, _) = run(19, 21_000); + assert_eq!(reward_balance, U256::from(21_000 - 4_200), "before orchid"); + let (reward_balance, _, _) = run(20, 21_000); + assert_eq!(reward_balance, U256::from(21_000), "from orchid"); + + let (_, paid, kept) = run(19, 5_000_000); + assert_eq!( + (paid, kept), + (U256::from(8_000), U256::ZERO), + "before orchid" + ); + let (_, paid, kept) = run(20, 5_000_000); + assert_eq!((paid, kept), (U256::ZERO, U256::from(8_000)), "from orchid"); +} + +/// rskj's two switches of REMASC, at each network's own heights. +#[test] +fn rskip85_and_rskip218_come_with_orchid_and_iris300() { + let mainnet = RskHardforkConfig::mainnet(); + assert!(!mainnet.has_rskip85(728_999)); + assert!(mainnet.has_rskip85(729_000)); + assert!(!mainnet.has_rskip218(3_614_799)); + assert!(mainnet.has_rskip218(3_614_800)); + let testnet = RskHardforkConfig::testnet(); + assert!(testnet.has_rskip85(0)); + assert!(!testnet.has_rskip218(2_060_499)); + assert!(testnet.has_rskip218(2_060_500)); + let regtest = RskHardforkConfig::regtest(); + assert!(regtest.has_rskip85(0)); + assert!(regtest.has_rskip218(0)); +} + +/// RSK Labs' cut goes to its RSKIP218 address from iris300 as the schedule +/// puts it, and from RSKIP218's own height where that is scheduled apart; so +/// does RSKIP85 from its own. +#[test] +fn remascs_switches_follow_the_schedule_and_a_rules_own_height() { + let config = RemascConfig::mainnet(); + let mainnet = RskHardforkConfig::mainnet(); + let (before, after) = (config.rsk_labs_address, config.rsk_labs_address_rskip218); + assert_ne!(before, after); + assert_eq!(config.labs_address_at(&mainnet, 3_614_799), before); + assert_eq!(config.labs_address_at(&mainnet, 3_614_800), after); + let later = + RskHardforkConfig::mainnet().with_hardfork(RskNetworkUpgrade::Iris300, Some(3_700_000)); + assert_eq!(config.labs_address_at(&later, 3_614_800), before); + assert_eq!(config.labs_address_at(&later, 3_700_000), after); + + let mut apart = config.clone(); + apart.rskip218_activation_height = Some(10); + apart.rskip85_activation_height = Some(u64::MAX); + assert_eq!(apart.labs_address_at(&mainnet, 9), before); + assert_eq!(apart.labs_address_at(&mainnet, 10), after); + assert!(config.rskip85_active(&mainnet, 729_000)); + assert!(!config.rskip85_active(&mainnet, 728_999)); + assert!(!apart.rskip85_active(&mainnet, 729_000)); +} diff --git a/crates/execution/src/rsk_handler.rs b/crates/execution/src/rsk_handler.rs index 631bcd6..e4e0063 100644 --- a/crates/execution/src/rsk_handler.rs +++ b/crates/execution/src/rsk_handler.rs @@ -31,7 +31,10 @@ //! 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::entry::SelfdestructionRevertStatus; use revm::context_interface::journaled_state::account::JournaledAccountTr; use revm::context_interface::{Cfg, ContextTr, JournalTr}; use revm::handler::instructions::InstructionProvider; @@ -41,13 +44,13 @@ 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; 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). @@ -68,14 +71,21 @@ 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` - /// 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 @@ -96,22 +106,109 @@ invisible_exception: Arc, /// returnDataBuffer semantics: `Program.callToAddress` resets the /// caller's buffer only inside `executeCode` (callee HAS code) and /// `callToPrecompiledAddress`; a call that executes nothing — empty-code - /// callee, depth limit, insufficient endowment — leaves the PREVIOUS - /// call's return data in place, where canonical EVM (EIP-211) clears it. - /// This pre-iris quirk is disabled once RSKIP171 activates. + /// callee, insufficient endowment — leaves the PREVIOUS call's return + /// data in place, where canonical EVM (EIP-211) clears it. This pre-iris + /// quirk is disabled once RSKIP171 activates. active_precompiles: Vec
, /// Post-RSKIP171 (iris300): `Program.cleanReturnDataBuffer` now resets - /// the buffer on EVERY call that runs no new frame (empty-code callee, - /// insufficient endowment, depth limit, precompile-out-of-gas), matching - /// canonical EVM / revm's default EIP-211 clearing. When `true`, the + /// the buffer on a call that runs no new frame for an empty-code callee, + /// an insufficient endowment or a precompile's gas, matching canonical + /// EVM / revm's default EIP-211 clearing. When `true`, the /// buffer-preservation above is disabled (mainnet #3,616,094: a DEX /// router does RETURNDATASIZE right after a value transfer to an empty /// account — preserving the prior call's 32-byte output sent it down a - /// different branch and out of gas). + /// different branch and out of gas). A call refused for the depth keeps + /// the buffer at every height (`Program.callToAddress` and + /// `callToPrecompiledAddress` return before any clean-up), though a + /// program that deep has made no call that could have filled it. rskip171_active: bool, + /// RSKIP90 (orchid): a precompile that returns makes its output the + /// caller's return data. Before it rskj's `Program.executePrecompiled`, + /// which runs a contract's call to a precompile before RSKIP197, leaves + /// the return data as the call before left it. + rskip90_active: bool, + /// Before RSKIP174 (iris300) a creation transaction's contract holds only + /// what the transaction sends it: rskj's `TransactionExecutor.create` + /// writes a new account over one already at its address + /// (`createAccount(addr, false)`), whose balance is lost. It writes it in + /// the transaction's `cacheTrack`, which a creation that fails rolls back + /// (`go`), so the old balance is taken in the creation's frame, which a + /// failure takes back. rskj's `create` runs so for a local call too, so + /// every run sets it (`with_rskip174`). + deploy_overwrites: 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, + /// 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>>>, + /// Each frame of contract code running, innermost last (see `OpenFrame`). + frames: Vec, + /// Shared with `RskPrecompileProvider`, which hands it to the executor: + /// the transaction reached REMASC or the Bridge, as rskj counts it + /// (`note_call`). `None` where the run doesn't ask. + reached_sequential_only: Option>, + /// 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, + /// 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)>, } +/// What `RskHandler` keeps of a frame of contract code while it runs. +#[derive(Debug, Clone)] +struct OpenFrame { + /// 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. + raw: usize, + called: usize, + /// What a local call's accounts owed as it opened + /// (`RskChainExt::debts`), which a frame that fails leaves as it was: the + /// journal gives back what they held. + debts: crate::raw_storage::Debts, + /// How many of the Bridge's unlogged decisions the thread held as it + /// opened (`crate::silent`): those made under a frame that fails stand + /// no more than its writes do, and go with them. + silent: usize, + /// It runs the constructor of a CREATE or CREATE2, where the transaction's + /// own program doesn't: rskj drops what such a program counts as called + /// (`note_call`). + creation: bool, +} + impl RskHandler { pub fn new( empty_create_on_unpayable_deposit: bool, @@ -121,6 +218,7 @@ impl RskHandler { empty_create_on_unpayable_deposit, false, invisible_exception, + Arc::default(), None, Vec::new(), false, @@ -134,11 +232,132 @@ 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 + } + + /// 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 + } + + /// Where to say that the transaction reached REMASC or the Bridge. See the + /// field. + pub fn with_reached_sequential_only(mut self, reached: Arc) -> Self { + self.reached_sequential_only = Some(reached); + 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 + } + + /// 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 RSKIP90 is active, under which a precompile's output becomes + /// the caller's return data. See the field. + pub fn with_rskip90(mut self, active: bool) -> Self { + self.rskip90_active = active; + self + } + + /// Whether RSKIP174 is active, before which a creation transaction's + /// contract holds only what it is sent. See `deploy_overwrites`. + pub fn with_rskip174(mut self, active: bool) -> Self { + self.deploy_overwrites = !active; + 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, rskip125_active: bool, invisible_exception: Arc, + aborts_caller: Arc, pre_rskip150_precompiles: Option>, active_precompiles: Vec
, rskip171_active: bool, @@ -147,11 +366,23 @@ impl RskHandler { empty_create_on_unpayable_deposit, rskip125_active, invisible_exception, + aborts_caller, pre_rskip150_precompiles, active_precompiles, rskip171_active, + rskip90_active: true, + deploy_overwrites: false, // Per-transaction, as before RSKIP-144; `postponing_fees` opts in. postpone_fees: false, + fail_programs: false, + called_precompiles: None, + frames: Vec::new(), + reached_sequential_only: None, + 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, } } @@ -169,8 +400,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, @@ -184,9 +420,181 @@ 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, and before RSKIP136 one runs all + /// the same (`runs_unmetered`). + 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()), + }; + 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, + initial_gas: gas.initial_gas, + } + .into(), + ); + } + gas.initial_gas = gas_limit; + } + self.starved.set(starved); + Ok(gas) + } + + /// A block's transaction is held to revm's checks of its sender, but for + /// EIP-3607's: rskj refuses no transaction for code at its sender's + /// address (`TransactionExecutor.init` reads the sender's nonce and + /// balance, and nothing else of its account). + /// + /// 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 what it was + /// lifted by is kept as what it owes (`RskChainExt::debts`): BALANCE and + /// SELFBALANCE read the account less that, as rskj reads one below zero, + /// and while it holds less than it owes it can call and create nothing + /// (`unpaid_by_debtor`), as rskj's can't. + /// + /// 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, + ) -> Result<(), Self::Error> { + if !self.local_call { + use revm::context_interface::{Cfg, Transaction}; + use revm::handler::pre_execution::{ + calculate_caller_fee, validate_account_nonce_and_code, + }; + let (block, tx, cfg, journal, _, _) = evm.ctx().all_mut(); + let mut caller = journal + .load_account_with_code_mut(tx.caller()) + .map_err(|e| ERROR::from(e))? + .data; + validate_account_nonce_and_code( + &caller.account().info, + tx.nonce(), + true, + cfg.is_nonce_check_disabled(), + )?; + let balance = calculate_caller_fee(*caller.balance(), tx, block, cfg)?; + caller.set_balance(balance); + if tx.kind().is_call() { + caller.bump_nonce(); + } + return Ok(()); + } + use revm::context_interface::Transaction; + use revm::primitives::TxKind; + let ctx = evm.ctx(); + let (caller, value) = (ctx.tx().caller(), ctx.tx().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 debts = crate::raw_storage::Debts::default(); + { + 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); + debts.add(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().debts = debts; + Ok(()) + } + 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. @@ -202,6 +610,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(()); } @@ -223,9 +636,13 @@ 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 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) { @@ -284,28 +701,203 @@ where exec_result.gas_mut().set_final_refund(false); } - /// The mainnet exec loop with two additions: + /// 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; + // 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, on gas it doesn't have (`runs_unmetered`). + 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; + } + 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 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. - fn run_exec_loop( + /// - 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`). + /// + /// 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 { - let res = evm.frame_init(first_frame_input)?; - if let ItemOrResult::Result(frame_result) = res { - return Ok(frame_result); + ) -> 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); + // What a call asked for and left unopened, where a transaction failed + // before its frame opened, is no part of this one's. + crate::rsk_instructions::take_callee_past_an_address(); + // 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 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.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(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) + }; + if let revm::interpreter::interpreter_action::FrameInput::Call(call) = + &first_frame_input.frame_input + { + self.note_call(call.bytecode_address); + } + 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(()) => {} + } + // 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( + "rskj can't build this transaction's program: a field of the \ + block's header is past 32 bytes" + .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); + begin_creation(evm); + if self.deploy_overwrites { + overwrite_deployed_balance(evm); + } + skip_code_header(evm); + self.open_frame(evm, false); + 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. + if self.starved.get() { + evm.frame_stack() + .get() + .interpreter + .halt(InstructionResult::OutOfGas); + } 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). @@ -314,9 +906,14 @@ where // return data, restored after `frame_return_result` when rskj // would not have touched the buffer (see field docs). let mut preserved_return_buffer: Option = None; + // A creation whose init code reverted: rskj leaves its creator's + // return data empty, after `frame_return_result` (see there). + let mut creation_reverted = false; let mut result = match call_or_result { ItemOrResult::Item(init) => { + // Taken for every frame asked for, whatever it is. + let past_an_address = crate::rsk_instructions::take_callee_past_an_address(); if *TRACE_FRAMES { trace_frame_init(&init); } @@ -326,52 +923,114 @@ where } _ => None, }; + // A callee named by a word with a bit set above its 20 + // bytes is no precompile to rskj (`VM.doCALL`), only the + // account at those bytes (`call_without_code`). + let to_account = past_an_address + && callee.is_some_and(|c| self.active_precompiles.contains(&c)); + let callee = callee.filter(|_| !to_account); + let creates = matches!( + init.frame_input, + revm::interpreter::interpreter_action::FrameInput::Create(_) + ); + // Before anything can refuse the call (`note_call`). + if let Some(callee) = callee { + self.note_call(callee); + } // RSKIP150 lowers the call-stack limit from 1024 to 400 // (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 { + refusal(&init.frame_input, InstructionResult::CallTooDeep) + } 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 - ); - } - 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 evm.frame_init(init)? { - ItemOrResult::Item(_) => { + // 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); + // An account a local call left below zero can pay for no + // call and no creation, and what it can't pay for comes + // back as what revm refuses for the balance does: no frame + // opens. rskj looks at the depth first, as is done above. + let unpaid = { + use revm::interpreter::interpreter_types::InputsTr; + let owner = evm.frame_stack().get().interpreter.input.target_address(); + unpaid_by_debtor(evm, owner, &init.frame_input) + }; + let made_here = match unpaid { + Some(unpaid) => Some(unpaid), + None if to_account => call_without_code(evm, &init), + None => None, + }; + let opened = match made_here { + Some(result) => ItemOrResult::Result(D::refuse(evm, init, result)), + None => D::init(evm, init)?, + }; + match opened { + 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. let nonce = u64::from(self.rskip125_active); set_created_contract_nonce(evm, nonce); + begin_creation(evm); + skip_code_header(evm); + self.open_frame(evm, creates); + // 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: @@ -379,6 +1038,28 @@ 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. + self.keep_precompile_logs(evm); if let (Some(precompiles), Some(callee), FrameResult::Call(outcome)) = (&self.pre_rskip150_precompiles, callee, &result) { @@ -392,28 +1073,40 @@ where )); } } - // rskj: only executed code and precompile calls - // reset the caller's returnDataBuffer; a CALL - // resolved without running anything keeps the - // 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 + // rskj: only executed code and a precompile that + // returns reset the caller's returnDataBuffer, the + // precompile only from RSKIP90 (orchid, + // `Program.executePrecompiled`). A CALL resolved + // without running anything, a precompile's too + // where it is refused for the endowment or its gas, + // keeps the previous buffer (mainnet #2,669,886: + // RETURNDATASIZE after a value transfer to an empty + // account still reports the prior call's 32-byte + // output). 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(outcome) => { + let precompile = callee + .is_some_and(|c| self.active_precompiles.contains(&c)); + if precompile && outcome.instruction_result().is_ok() { + !self.rskip90_active + } else { + !self.rskip171_active + } + } + 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( @@ -429,7 +1122,35 @@ where } } } - ItemOrResult::Result(result) => result, + // A frame of contract code ended, having committed exactly + // when its result is ok (`rsk_frame_return`). + ItemOrResult::Result(result) => { + creation_reverted = matches!( + &result, + FrameResult::Create(outcome) + if matches!(outcome.instruction_result(), InstructionResult::Revert) + ); + 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 + } }; if *TRACE_FRAMES { tracing::debug!( @@ -450,9 +1171,21 @@ where // futureRefund directly), so this only applies to nested creates // (depth > 0). revm propagates the refund unconditionally; drop it here // before it merges into the caller (#6,362,186). + // + // A self-destruct's refund is no part of `futureRefund`: rskj counts it + // when the transaction ends, 24,000 for each account in the set of those + // destroyed (`TransactionExecutor.refundGas`), a set the constructor does + // hand up. So what stays of the refund is the self-destructs' (testnet + // #3,088,983). The frame's journal holds an entry for each, its own and + // those of the calls and creations it made that did not fail. if let FrameResult::Create(outcome) = &mut result { - if evm.frame_stack().get().depth > 0 { - outcome.result.gas.set_refund(0); + let frame = evm.frame_stack().get(); + let (depth, ran, journal_i) = + (frame.depth, frame.is_finished(), frame.checkpoint.journal_i); + if depth > 0 { + // A creation refused before it ran had no frame of its own. + let kept = if ran { selfdestruct_refunds_since(evm, journal_i) } else { 0 }; + outcome.result.gas.set_refund(kept); } } // RSKIP-144: a frame that fails is about to have its journal @@ -472,10 +1205,272 @@ where // Undo revm's insert_call_outcome buffer clear (EIP-211). evm.frame_stack().get().interpreter.return_data.set_buffer(buffer); } + // rskj `Program.getProgramResult`, when a creation's init code + // reverts: `returnDataBuffer = result.getHReturn()`. `result` is the + // creator's own ProgramResult, not the creation's (`programResult`), + // and a program still running has returned nothing, so RETURNDATASIZE + // reads 0 where revm (EIP-211) keeps the revert data. No activation + // guards it (testnet #5,214,732). + if creation_reverted { + use revm::interpreter::interpreter_types::ReturnData; + evm.frame_stack().get().interpreter.return_data.clear(); + } + // 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); + } } } } +/// 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, a sender a local + /// call left below zero), with the result revm gives one it refuses; or a + /// call it makes itself that opens no frame (`call_without_code`). + 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< + Chain = crate::raw_storage::RskChainExt, + Journal: JournalTr, + >, + 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< + Chain = crate::raw_storage::RskChainExt, + Journal: JournalTr, + >, + 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, +{ + /// A frame of contract code opens (see `frames`), one that runs a + /// constructor of a CREATE or CREATE2 where `creation`. + fn open_frame(&mut self, evm: &mut EVM, creation: bool) { + 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(); + let debts = evm.ctx().chain().debts.clone(); + self.frames.push(OpenFrame { + raw, + called, + debts, + silent: crate::silent::len(), + creation, + }); + } + + /// 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, the Bridge's unlogged decisions made under + /// it, and what was done under it to what a local call's accounts owe + /// (see `frames`). + fn close_frame(&mut self, evm: &mut EVM, committed: bool) { + let Some(frame) = self.frames.pop() else { + return; + }; + if committed { + return; + } + evm.ctx().chain_mut().raw_storage.revert_frame(frame.raw); + crate::silent::truncate(frame.silent); + evm.ctx().chain_mut().debts = frame.debts; + if let Some(Ok(mut list)) = self.called_precompiles.as_ref().map(|list| list.lock()) { + list.truncate(frame.called); + } + } + + /// A program calls `callee`, or the transaction does, with no frame open. + /// rskj counts a call to a precompile as the call starts + /// (`Program.callToPrecompiledAddress`, `TransactionExecutor.call`), before + /// it checks the depth or the caller's balance, so a call it then refuses + /// counts too. A program hands what it counted to its caller only where + /// the caller called it, whatever became of the call + /// (`Program.executeCode`), and not where the caller ran its CREATE: so a + /// call counts for the transaction unless a constructor of a CREATE or + /// CREATE2 is running. Under RSKIP-144 a transaction that reached REMASC + /// or the Bridge may only sit in the sequential sublist + /// (`TransactionListExecutor`, `BlockExecutor`). + fn note_call(&self, callee: Address) { + if !crate::precompiles::SEQUENTIAL_ONLY_PRECOMPILES.contains(&callee) + || self.frames.iter().any(|frame| frame.creation) + { + return; + } + if let Some(reached) = &self.reached_sequential_only { + reached.store(true, Ordering::Relaxed); + } + } + + /// 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); + } + } +} + +/// 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 @@ -510,6 +1505,33 @@ where crate::collision::forget_entries_above(len); } +/// What `rsk_selfdestruct` refunded since the journal's `journal_i`th entry, +/// in frames that have not failed (a failed frame takes its entries with it): +/// rskj's `SUICIDE_REFUND` for each account destroyed for the first time in +/// the transaction, as rskj's set of deleted accounts holds each once. +fn selfdestruct_refunds_since(evm: &mut EVM, journal_i: usize) -> i64 +where + EVM: EvmTr + JournalExt>>, +{ + let refund = evm.ctx().cfg().gas_params().selfdestruct_refund(); + let destroyed = evm + .ctx() + .journal() + .journal() + .get(journal_i..) + .unwrap_or_default() + .iter() + .filter(|entry| { + matches!( + entry, + JournalEntry::AccountDestroyed { destroyed_status, .. } + if *destroyed_status != SelfdestructionRevertStatus::RepeatedSelfdestruction + ) + }) + .count(); + refund * destroyed as i64 +} + /// `EvmTr::frame_run` with the create-return path replaced by /// `rsk_return_create` (same shape as revm's default implementation). fn rsk_frame_run( @@ -518,7 +1540,10 @@ fn rsk_frame_run( ) -> Result>, ContextTrDbError> where EVM: EvmTr< - Context: ContextTr>, + Context: ContextTr< + Chain = crate::raw_storage::RskChainExt, + Journal: JournalTr, + >, Frame = EthFrame, Instructions: InstructionProvider< Context = ::Context, @@ -533,13 +1558,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, }; @@ -576,6 +1622,11 @@ where &mut interpreter_result, create.created_address, ); + // A creation that ends well saves its code + // (`RskChainExt::code_saved`). + if interpreter_result.result.is_ok() { + ctx.chain_mut().code_saved.insert(create.created_address); + } } ItemOrResult::Result(FrameResult::Create(CreateOutcome::new( interpreter_result, @@ -584,7 +1635,7 @@ where } }; frame.set_finished(true); - Ok(result) + result } /// rskj `Program.finalizeContractCreation` before RSKIP453: a constructor @@ -592,7 +1643,8 @@ where /// over-max-size code (0x6000, rskj Constants.MAX_CONTRACT_SIZE) leaves the /// contract with EMPTY code — without charging the deposit — while the /// constructor's state changes stay committed and the address is returned. -fn rsk_return_create( +/// Only a creation that keeps its code saves it (`RskChainExt::code_saved`). +fn rsk_return_create>( ctx: &mut CTX, checkpoint: revm::context_interface::journaled_state::JournalCheckpoint, interpreter_result: &mut revm::interpreter::InterpreterResult, @@ -609,7 +1661,8 @@ fn rsk_return_create( let deposit = cfg.gas_params().code_deposit_cost(code_len); // Short-circuit keeps the deposit uncharged in the oversize case, like // rskj (the exception set by either check skips spendGas + saveCode). - if oversize || !interpreter_result.gas.record_cost(deposit) { + let unsaved = oversize || !interpreter_result.gas.record_cost(deposit); + if unsaved { interpreter_result.output = Bytes::new(); } @@ -619,6 +1672,9 @@ fn rsk_return_create( revm::state::Bytecode::new_legacy(interpreter_result.output.clone()), ); interpreter_result.result = InstructionResult::Return; + if !unsaved { + ctx.chain_mut().code_saved.insert(address); + } } /// Divergence-hunt diagnostic (env-gated): log every sub-frame's input and @@ -649,16 +1705,60 @@ fn trace_frame_init(init: &FrameInit) { } } -/// Synthesize the CallTooDeep rejection revm would produce at its own -/// depth limit: the child's full gas refunded, empty output (rskj -/// stackPushZero + refundGas at the Program deep checks). -fn call_too_deep_result( +/// A call or a creation by an account a local call left below zero +/// (`RskChainExt::debts`), which `owner`, the account the running program +/// runs as, can't pay for. rskj's `Program.callToAddress`, +/// `callToPrecompiledAddress` and `createContract` each refuse one whose +/// sender's balance is less than the value it sends: 0 is pushed and the gas +/// comes back. A balance below zero is less than a value of nothing, so such +/// an account makes no call at all, a DELEGATECALL or a STATICCALL no more +/// than another, and creates nothing, until it is paid what it lacks. revm +/// reads what the account holds and not what it owes, so the refusal is made +/// here, with revm's result for a sender short of the value. `None` in a +/// block's run, where nothing is owed. +fn unpaid_by_debtor( + evm: &mut EVM, + owner: Address, frame_input: &revm::interpreter::interpreter_action::FrameInput, +) -> Option +where + EVM: EvmTr>, +{ + use revm::interpreter::interpreter_action::FrameInput; + let ctx = evm.ctx(); + let owed = ctx.chain().debts.of(owner); + if owed.is_zero() { + return None; + } + let value = match frame_input { + FrameInput::Call(inputs) => inputs.transfer_value().unwrap_or_default(), + FrameInput::Create(inputs) => inputs.value(), + FrameInput::Empty => return None, + }; + // What rskj holds for the account is what it holds here less what it + // owes. + let held = ctx.journal().evm_state().get(&owner)?.info.balance; + if held + .checked_sub(owed) + .is_some_and(|balance| balance >= value) + { + return None; + } + refusal(frame_input, InstructionResult::OutOfFunds) +} + +/// The result revm gives a call or a creation it refuses before it opens a +/// frame, for its depth (`CallTooDeep`) or for a sender short of the value +/// (`OutOfFunds`): the child's full gas refunded, empty output (rskj +/// stackPushZero + refundGas at the Program checks of depth and balance). +fn refusal( + frame_input: &revm::interpreter::interpreter_action::FrameInput, + reason: InstructionResult, ) -> Option { use revm::interpreter::interpreter_action::FrameInput; use revm::interpreter::{Gas, InterpreterResult}; let interp_result = |gas_limit: u64| InterpreterResult { - result: InstructionResult::CallTooDeep, + result: reason, gas: Gas::new(gas_limit), output: Bytes::new(), }; @@ -675,9 +1775,98 @@ fn call_too_deep_result( } } -/// 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. +/// The call rskj makes where a word with a bit set above its 20 bytes named +/// an active precompile's address: `VM.doCALL` finds no precompile for the +/// whole word, and `Program.callToAddress` calls the account at those 20 +/// bytes, which no creation reaches and no genesis gives code, so the call +/// runs nothing. revm would run the precompile, so the call is made here as +/// revm's `make_call_frame` makes one to an account without code: refused +/// past the depth; refused for a caller short of the value it moves, what +/// the transfer touched taken back; else the value moves and the callee is +/// touched, which creates it where it wasn't, as rskj's `addBalance` does, +/// and all the gas comes back. `None` for a frame that is no call. +fn call_without_code(evm: &mut EVM, init: &FrameInit) -> Option +where + EVM: EvmTr>>, +{ + use revm::interpreter::interpreter_action::{CallValue, FrameInput}; + use revm::interpreter::InterpreterResult; + use revm::primitives::constants::CALL_STACK_LIMIT; + let FrameInput::Call(inputs) = &init.frame_input else { + return None; + }; + let ran = |result: InstructionResult| { + FrameResult::Call(CallOutcome::new( + InterpreterResult { + result, + gas: Gas::new(inputs.gas_limit), + output: Bytes::new(), + }, + inputs.return_memory_offset.clone(), + )) + }; + if init.depth > CALL_STACK_LIMIT as usize { + return Some(ran(InstructionResult::CallTooDeep)); + } + let journal = evm.ctx().journal_mut(); + let checkpoint = journal.checkpoint(); + if let CallValue::Transfer(value) = inputs.value { + if let Some(error) = journal.transfer_loaded(inputs.caller, inputs.target_address, value) { + journal.checkpoint_revert(checkpoint); + return Some(ran(error.into())); + } + } + journal.checkpoint_commit(); + Some(ran(InstructionResult::Stop)) +} + +/// 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, + ))) +} + /// Pre-RSKIP150 `Program.memorySave(outOffs, out)`: write a successful /// precompile's FULL output into the caller's memory, extending it /// word-aligned WITHOUT charging gas (rskj `Memory.extend` grows the @@ -707,6 +1896,9 @@ where interpreter.memory.set(mem_start, output); } +/// 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. fn set_created_contract_nonce(evm: &mut EVM, nonce: u64) where EVM: EvmTr< @@ -722,21 +1914,116 @@ where } } -/// Inspection support, for `debug_traceTransaction` and the `trace_*` methods. +/// A creation's frame has opened (the top one): its contract has no code +/// saved until the creation ends, whatever a creation at the address before +/// it saved and a failure took back (`RskChainExt::code_saved`). +fn begin_creation(evm: &mut EVM) +where + EVM: EvmTr< + Context: ContextTr, + Frame = EthFrame, + >, +{ + let FrameData::Create(CreateFrame { created_address }) = evm.frame_stack().get().data else { + return; + }; + evm.ctx().chain_mut().code_saved.remove(&created_address); +} + +/// A creation transaction's frame has opened before RSKIP174 (see +/// `deploy_overwrites`): its contract holds only the value it was sent, a +/// balance already at the address lost, until a failure of the frame takes +/// that back. +fn overwrite_deployed_balance(evm: &mut EVM) +where + EVM: EvmTr< + Context: ContextTr>, + Frame = EthFrame, + >, +{ + use revm::context_interface::Transaction; + let FrameData::Create(CreateFrame { created_address }) = evm.frame_stack().get().data else { + return; + }; + let sent = evm.ctx().tx().value(); + if let Ok(mut account) = evm.ctx().journal_mut().load_account_mut(created_address) { + account.set_balance(sent); + } +} + +/// rskj's code header (`Program.processAndSkipCodeHeader`, which +/// `Program.precompile` runs for every program it builds, from genesis and +/// under no rule): code of four bytes or more whose first is HEADER (0xFC) +/// starts at byte 4 + code[3], and its jump destinations are found from +/// there (`computeJumpDests(i)`), so none lies in the header. rskj reads the +/// header's two versions too, but no opcode other than HEADER asks for one, +/// and HEADER is invalid under any. Code whose header reaches its end is left +/// to fail at the HEADER it starts with: rskj reads past `ops` there and +/// fails the program with all its gas, as revm fails an invalid opcode. +/// Applied to the top frame as it opens, before it runs or anything halts it. +fn skip_code_header(evm: &mut EVM) +where + EVM: EvmTr>, +{ + use revm::bytecode::{Bytecode, JumpTable}; + use revm::interpreter::interpreter::ExtBytecode; + use revm::interpreter::interpreter_types::{Jumps, LoopControl}; + let bytecode = &mut evm.frame_stack().get().interpreter.bytecode; + let code = bytecode.original_byte_slice(); + if bytecode.is_end() || !bytecode.is_legacy() || code.len() < 4 || code[0] != 0xfc { + return; + } + let start = 4 + usize::from(code[3]); + if start >= code.len() { + return; + } + // `computeJumpDests` from the header's end: a JUMPDEST is a destination, + // and a PUSH's data is stepped over. + let mut jumps = vec![0u8; code.len().div_ceil(8)]; + let mut at = start; + while at < code.len() { + match code[at] { + 0x5b => jumps[at / 8] |= 1 << (at % 8), + push @ 0x60..=0x7f => at += usize::from(push - 0x5f), + _ => {} + } + at += 1; + } + let skipped = Bytecode::new_analyzed( + bytecode.bytecode().clone(), + code.len(), + JumpTable::from_slice(&jumps, code.len()), + ); + let hash = bytecode.hash(); + *bytecode = ExtBytecode::new_with_optional_hash(skipped, hash); + bytecode.absolute_jump(start); +} + +/// 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< - Context: ContextTr>, + Context: ContextTr< + Chain = crate::raw_storage::RskChainExt, + Journal: JournalTr, + >, Frame = EthFrame, Instructions: InstructionProvider< Context = ::Context, @@ -748,4 +2035,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) + } } diff --git a/crates/execution/src/rsk_instructions.rs b/crates/execution/src/rsk_instructions.rs index ed7945c..fbd6425 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). @@ -59,28 +66,67 @@ 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) }; + /// RSKIP150's absence: rskj adds a call's stipend to the gas it asks for + /// as a plain `long`, which a request near 2^63 takes below zero (see + /// `rsk_callee_gas`). Set by `install`. + static STIPEND_SUM_WRAPS: core::cell::Cell = const { core::cell::Cell::new(false) }; /// 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 /// 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 @@ -99,27 +145,61 @@ 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. +/// +/// `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. +/// +/// `staticcall_takes_value` is RSKIP103's absence: before orchid060 rskj's +/// STATICCALL takes a value as CALL does ([`rsk_static_call_with_value`]). +/// +/// `stipend_sum_wraps` is RSKIP150's absence: before twoToThree rskj adds a +/// call's stipend to the gas it asks for as a plain `long` (`rsk_callee_gas`). +/// +/// `block` is what the block's header gives COINBASE, GASLIMIT and BASEFEE. pub fn install( instructions: &mut EthInstructions, extcodehash_enabled: bool, + extcodehash_standard: bool, istanbul_opcodes_enabled: bool, push0_enabled: bool, basefee_enabled: bool, staticcall_enabled: bool, + staticcall_takes_value: bool, + stipend_sum_wraps: bool, transient_storage_enabled: bool, mcopy_enabled: bool, + dupn_swapn_txindex_enabled: bool, extcodesize_max_precompiles: &[Address], extcodehash_precompiles: &[Address], - real_coinbase: Address, + block: BlockWords, ) where WIRE: InterpreterTypes, - HOST: Host, + HOST: Host + ContractMarker + BeneficiaryTouch + LocalCallWords, { // RSKIP446 (lovell700): transient storage TLOAD/TSTORE (0x5c/0x5d, // EIP-1153, rskj `GasCost.TLOAD`/`TSTORE` = 100 each). RSKIP445 (lovell700): @@ -180,6 +260,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), @@ -199,9 +280,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), @@ -218,9 +300,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( @@ -230,10 +314,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, @@ -281,18 +368,22 @@ pub fn install( opcode::DELEGATECALL, Instruction::new(rsk_delegate_call::, CALL_STATIC_GAS), ); + STIPEND_SUM_WRAPS.with(|w| w.set(stipend_sum_wraps)); // RSKIP91 (orchid, mainnet #729,000) introduced STATICCALL. rskj's // `VM.java` OP_STATICCALL throws `invalidOpCode` (consuming the frame's // gas) while RSKIP91 is inactive; rustock maps Genesis to BYZANTIUM, where // revm already provides the opcode, so before orchid it must be replaced // with the invalid-opcode handler -- same pattern as EXTCODEHASH before - // papyrus. + // papyrus. Until RSKIP103 (orchid060, mainnet #1,052,700) it takes a + // value. instructions.insert_instruction( opcode::STATICCALL, - if staticcall_enabled { - Instruction::new(rsk_static_call::, CALL_STATIC_GAS) - } else { + if !staticcall_enabled { Instruction::new(invalid_opcode::, 0) + } else if staticcall_takes_value { + Instruction::new(rsk_static_call_with_value::, CALL_STATIC_GAS) + } else { + Instruction::new(rsk_static_call::, CALL_STATIC_GAS) }, ); instructions.insert_instruction( @@ -303,12 +394,39 @@ 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 + // 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 @@ -320,6 +438,8 @@ 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)); + STATICCALL_TAKES_VALUE.with(|f| f.set(staticcall_takes_value)); + 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 +456,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( @@ -384,13 +506,50 @@ pub(crate) const KECCAK_EMPTY: B256 = B256::new([ 0xe5, 0x00, 0xb6, 0x53, 0xca, 0x82, 0x27, 0x3b, 0x7b, 0xfa, 0xd8, 0x04, 0x5d, 0x85, 0xa4, 0x70, ]); +/// Whether a word that names an address holds more than one: a bit set above +/// the address's 20 bytes. rskj looks for a precompile with the whole word +/// (`PrecompiledContracts.getContractForAddress` compares it with each +/// precompile's, in `VM.doCALL`, `doCODESIZE` and `doEXTCODEHASH`), where all +/// else it reads of an address takes the word's last 20 bytes +/// (`RskAddress(DataWord)`). Such a word names the account at those 20 bytes, +/// never a precompile. +fn past_an_address(word: U256) -> bool { + word.bit_len() > 160 +} + +thread_local! { + /// Whether the word that named the callee of the call a frame asks for + /// was [`past_an_address`]: set by each call opcode as it asks, taken + /// by the handler as it opens the frame. + static CALLEE_PAST_AN_ADDRESS: core::cell::Cell = const { core::cell::Cell::new(false) }; +} + +/// A call opcode asks for its frame, its callee named by a word +/// [`past_an_address`] or not. +fn ask_for_call(past_an_address: bool) { + CALLEE_PAST_AN_ADDRESS.with(|past| past.set(past_an_address)); +} + +/// Whether the call whose frame is about to open had its callee named by a +/// word [`past_an_address`]; what any other frame reads after it is `false`. +pub(crate) fn take_callee_past_an_address() -> bool { + CALLEE_PAST_AN_ADDRESS.with(|past| past.replace(false)) +} + /// EXTCODEHASH with rskj's `VM.doEXTCODEHASH` / `getCodeHashStandard` /// semantics, which key on TRIE EXISTENCE (RSK never adopted EIP-161/1052 /// emptiness): -/// - active precompile -> `keccak256("")` (rskj special-cases precompiles); +/// - active precompile -> `keccak256("")` (rskj special-cases precompiles), +/// named by its address alone: a word [`past_an_address`] names the +/// account, whatever its last 20 bytes; /// - 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`]. Before +/// it too, one whose empty code a creation in the block saved answers +/// `keccak256("")`, the hash of the empty value the block's cache holds +/// for the key ([`ContractMarker::code_saved_in_block`]). /// /// revm's stock EXTCODEHASH instead returns `0` whenever `AccountInfo::is_empty()` /// (EIP-161: balance==0 && nonce==0 && no code), so an existing-but-empty @@ -400,7 +559,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 { @@ -410,7 +569,7 @@ fn rsk_extcodehash( let addr = Address::from_word(B256::from(*top)); assert_configured("EXTCODEHASH_PRECOMPILES"); - if EXTCODEHASH_PRECOMPILES.with(|p| p.borrow().contains(&addr)) { + if !past_an_address(*top) && EXTCODEHASH_PRECOMPILES.with(|p| p.borrow().contains(&addr)) { *top = KECCAK_EMPTY.into(); return; } @@ -419,18 +578,277 @@ 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() || context.host.code_saved_in_block(addr) { 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; + + /// Whether a creation in this block saved the code of `address`, empty + /// or not. rskj's block repository keeps what the block's transactions + /// write in a cache until the block ends (`RepositoryLocator + /// .startTrackingAt`, `MutableTrieCache`), where the code key of such a + /// contract holds the code saved, and an empty value hashes to + /// `keccak256("")` (`MutableTrieCache.getValueHash`). A contract whose + /// constructor is still running has the marker and no code yet. + fn code_saved_in_block(&self, address: Address) -> bool; +} + +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 + } + } + } + + fn code_saved_in_block(&self, address: Address) -> bool { + self.chain.code_saved.contains(&address) + } +} + +/// 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); + } + } +} + +/// What BALANCE, SELFBALANCE, GASPRICE and SELFDESTRUCT ask 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: what a local call took its balance below zero by + /// (`RskChainExt::debts`), and nothing for any other account. + fn owed_by(&self, address: Address) -> U256; + + /// `address` is about to destroy itself, naming `beneficiary`: it pays + /// what it owes out of what it holds, and what it still owes becomes the + /// beneficiary's ([`hand_on_debt`]). Whether anything was handed on. + fn hand_on_debt(&mut self, address: Address, beneficiary: Address) -> bool; + + /// 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 { + self.chain.debts.of(address) + } + + fn hand_on_debt(&mut self, address: Address, beneficiary: Address) -> bool { + let journal = &mut self.journaled_state.inner; + hand_on_debt( + &mut journal.state, + &mut journal.journal, + &mut self.chain, + address, + beneficiary, + ) + } + + fn whole_gas_price(&self) -> Option { + self.chain.whole_gas_price + } +} + +/// What rskj's `Program.suicide` does with a balance below zero, which only a +/// local call can leave an account with (`RskChainExt::debts`). It moves it +/// to the beneficiary like any balance that isn't zero, so the beneficiary's +/// falls by it, and makes an internal transaction of it; where the account +/// names itself, it brings the balance to zero. +/// +/// So before SELFDESTRUCT moves what `address` holds, the account pays what +/// it owes out of that. What it still owes is the beneficiary's to owe from +/// here, nobody's where it names itself, and counts as the internal +/// transaction, which the nothing it has left to move won't. Whether +/// anything was handed on: the beneficiary is an account from then on, +/// whatever it was before. +pub(crate) fn hand_on_debt( + state: &mut revm::state::EvmState, + journal: &mut Vec, + chain: &mut crate::raw_storage::RskChainExt, + address: Address, + beneficiary: Address, +) -> bool { + let Some(account) = state.get_mut(&address) else { + return false; + }; + let owed = chain.debts.take(address); + let held = account.info.balance; + let paid = held.min(owed); + if !paid.is_zero() { + account.info.balance = held - paid; + journal.push(revm::context::JournalEntry::BalanceChange { + address, + old_balance: held, + }); + } + let left = owed - paid; + if left.is_zero() { + return false; + } + if beneficiary != address { + chain.debts.add(beneficiary, left); + } + chain.internal_txs.count(); + true +} + +/// 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( @@ -453,14 +871,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(); } } @@ -555,15 +974,139 @@ fn rsk_coinbase( } } -/// SELFBALANCE without revm's ISTANBUL spec check (RSKIP151, papyrus200). -fn rsk_selfbalance( +/// 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>, ) { - let Some(balance) = context.host.balance(context.interpreter.input.target_address()) else { + 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 +/// (`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), +/// 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; }; - 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 !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 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; + }; + 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(); } } @@ -571,19 +1114,20 @@ fn rsk_selfbalance( /// EXTCODESIZE with rskj's RSKIP90 quirk: the code size of an active precompile /// is reported as `2^256-1` (`VM.doCODESIZE` → `DataWord.MAX_VALUE`), so a proxy /// guarding `extcodesize(precompile) != 0` works. All other addresses fall -/// through to revm's standard EXTCODESIZE. +/// through to revm's standard EXTCODESIZE, a precompile's among them where +/// the word that names it is [`past_an_address`]. fn rsk_extcodesize( context: InstructionContext<'_, H, WIRE>, ) { - let addr = { + let (addr, past) = { let Some(([], top)) = context.interpreter.stack.popn_top::<0>() else { context.interpreter.halt_underflow(); return; }; - Address::from_word(B256::from(*top)) + (Address::from_word(B256::from(*top)), past_an_address(*top)) }; assert_configured("EXTCODESIZE_MAX_PRECOMPILES"); - let is_precompile = EXTCODESIZE_MAX_PRECOMPILES.with(|p| p.borrow().contains(&addr)); + let is_precompile = !past && EXTCODESIZE_MAX_PRECOMPILES.with(|p| p.borrow().contains(&addr)); if is_precompile { if let Some(([], top)) = context.interpreter.stack.popn_top::<0>() { *top = U256::MAX; @@ -627,11 +1171,18 @@ 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) }; + /// RSKIP103's absence, for the all-ops tracer's STATICCALL dispatch. + static STATICCALL_TAKES_VALUE: core::cell::Cell = const { core::cell::Cell::new(false) }; + /// 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 /// 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}; @@ -654,17 +1205,23 @@ fn traced_all( // NOTE: addr= field added for the per-opcode trace oracle (rskj diff). match op { opcode::EXTCODESIZE => 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), opcode::STATICCALL => { - if STATICCALL_ENABLED.with(|f| f.get()) { - rsk_static_call(context) - } else { + if !STATICCALL_ENABLED.with(|f| f.get()) { invalid_opcode(context) + } else if STATICCALL_TAKES_VALUE.with(|f| f.get()) { + rsk_static_call_with_value(context) + } else { + rsk_static_call(context) } } 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) } @@ -697,6 +1254,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, @@ -763,6 +1326,13 @@ fn rsk_call_memory_range( Some(offset..offset.saturating_add(len)) } +/// Whether a call's stipend and the gas it asks for make a plain `long` sum, +/// before RSKIP150 (see `STIPEND_SUM_WRAPS`). +fn stipend_sum_wraps() -> bool { + assert_configured("STIPEND_SUM_WRAPS"); + STIPEND_SUM_WRAPS.with(|w| w.get()) +} + /// rskj `VM.getMessageCall` gas math, shared by the four call opcodes. /// /// Assumes the base costs (static 700, memory expansion, value transfer, @@ -788,6 +1358,19 @@ where context.interpreter.halt_oog(); return None; } + // Before RSKIP150 rskj adds the stipend to the gas asked for, which + // `Program.limitToMaxLong` holds at 2^63 - 1, as a plain `long` + // (`VM.getMessageCall`): past 2^63 - 1 the sum goes below zero, + // `GasCost.add` throws for the negative callee gas, and the frame that + // calls fails with all its gas. From RSKIP150 the sum stops at 2^63 - 1, + // more than any frame has, so the `min` below gives what rskj gives. + if stipend > 0 && stipend_sum_wraps() { + let held = requested.min(i64::MAX as u64); + if held > i64::MAX as u64 - stipend { + context.interpreter.halt_oog(); + return None; + } + } let callee_gas = core::cmp::min(remaining, requested.saturating_add(stipend)); if !context.interpreter.gas.record_cost(callee_gas) { context.interpreter.halt_oog(); @@ -824,8 +1407,9 @@ where // rskj charges NEW_ACCT_CALL for op CALL whenever the callee does not // exist, regardless of value: passing transfers_value=true makes revm's // new_account_cost unconditional on emptiness. - // (Approximation: revm reports EIP-161 emptiness, rskj checks trie - // existence; they differ only for existing-but-empty accounts.) + // The journal keeps HOMESTEAD's account rules (`load_accounts` in + // rsk_handler), under which revm's `is_empty` is an account the trie + // doesn't hold and the transaction hasn't touched: rskj's `!isExist`. let (cost, bytecode, code_hash) = load_account_delegated_handle_error(context, to, true, charge_new_account)?; if !context.interpreter.gas.record_cost(cost) { @@ -845,6 +1429,7 @@ pub fn rsk_call( context.interpreter.halt_underflow(); return; }; + let past = past_an_address(to); let to = Address::from_word(B256::from(to)); let local_gas_limit = u64::try_from(local_gas_limit).unwrap_or(u64::MAX); let has_transfer = !value.is_zero(); @@ -868,6 +1453,7 @@ pub fn rsk_call( return; }; + ask_for_call(past); context .interpreter .bytecode @@ -898,7 +1484,19 @@ 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`]). A balance below zero, +/// which a local call can leave, is one to hand over too +/// ([`LocalCallWords::hand_on_debt`]). +pub fn rsk_selfdestruct< + WIRE: InterpreterTypes, + H: Host + BeneficiaryTouch + LocalCallWords + ?Sized, +>( context: InstructionContext<'_, H, WIRE>, ) { if context.interpreter.runtime_flag.is_static() { @@ -913,18 +1511,23 @@ 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 hands_on_debt = context.host.hand_on_debt(address, 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 && !hands_on_debt { + 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; @@ -1026,6 +1629,7 @@ pub fn rsk_call_code( context.interpreter.halt_underflow(); return; }; + let past = past_an_address(to); let to = Address::from_word(B256::from(to)); let local_gas_limit = u64::try_from(local_gas_limit).unwrap_or(u64::MAX); @@ -1041,6 +1645,7 @@ pub fn rsk_call_code( return; }; + ask_for_call(past); context .interpreter .bytecode @@ -1068,6 +1673,7 @@ pub fn rsk_delegate_call( context.interpreter.halt_underflow(); return; }; + let past = past_an_address(to); let to = Address::from_word(B256::from(to)); let local_gas_limit = u64::try_from(local_gas_limit).unwrap_or(u64::MAX); @@ -1083,6 +1689,7 @@ pub fn rsk_delegate_call( return; }; + ask_for_call(past); context .interpreter .bytecode @@ -1110,6 +1717,7 @@ pub fn rsk_static_call( context.interpreter.halt_underflow(); return; }; + let past = past_an_address(to); let to = Address::from_word(B256::from(to)); let local_gas_limit = u64::try_from(local_gas_limit).unwrap_or(u64::MAX); @@ -1130,6 +1738,7 @@ pub fn rsk_static_call( return; }; + ask_for_call(past); context .interpreter .bytecode @@ -1149,6 +1758,57 @@ pub fn rsk_static_call( )))); } +/// STATICCALL as rskj runs it before RSKIP103 (orchid060, mainnet +/// #1,052,700): `VM.calculateCallValue` takes a value off the stack for every +/// call but DELEGATECALL, so STATICCALL takes seven arguments, as CALL does. +/// A value that isn't zero pays `VT_CALL` and brings the stipend +/// (`computeCallGas`, `calculateGetMinimumTransferGas`), and +/// `Program.callToAddress` moves it to the callee, which runs static. Only +/// CALL refuses a value inside a static call, and only CALL pays for an +/// account it creates. +pub fn rsk_static_call_with_value( + mut context: InstructionContext<'_, H, WIRE>, +) { + let Some([local_gas_limit, to, value]) = context.interpreter.stack.popn() else { + context.interpreter.halt_underflow(); + return; + }; + let past = past_an_address(to); + let to = Address::from_word(B256::from(to)); + let local_gas_limit = u64::try_from(local_gas_limit).unwrap_or(u64::MAX); + + let Some((input, return_memory_offset)) = + rsk_memory_input_and_out_ranges(context.interpreter, context.host.gas_params()) + else { + return; + }; + + let Some((gas_limit, bytecode, bytecode_hash)) = + rsk_call_gas_common(&mut context, to, local_gas_limit, value, false) + else { + return; + }; + + ask_for_call(past); + context + .interpreter + .bytecode + .set_action(InterpreterAction::NewFrame(FrameInput::Call(Box::new( + CallInputs { + input: CallInput::SharedBuffer(input), + gas_limit, + target_address: to, + caller: context.interpreter.input.target_address(), + bytecode_address: to, + known_bytecode: Some((bytecode_hash, bytecode)), + value: CallValue::Transfer(value), + scheme: CallScheme::StaticCall, + is_static: true, + return_memory_offset, + }, + )))); +} + #[cfg(test)] mod tests { use super::*; @@ -1174,6 +1834,129 @@ 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); + } + + /// An account that destroys itself pays what it owes out of what it + /// holds, and hands the rest of it to its beneficiary, as rskj's + /// `Program.suicide` hands on a balance below zero: it makes an internal + /// transaction of it, as of any balance that isn't zero. rskj's release + /// answered what each account then holds through `eth_call` (the rpc + /// crate's `testdata/rskj_local_calls.txt`, the scenario `below`); the + /// internal transaction is its source's (`addInternalTx`, in the branch + /// that moves the balance). + #[test] + fn a_self_destruction_hands_on_what_the_account_owes() { + use crate::internal_tx::InternalTxs; + use crate::raw_storage::RskChainExt; + use revm::context::JournalEntry; + use revm::state::{Account, EvmState}; + + let (debtor, heir, other) = ( + Address::repeat_byte(0xd1), + Address::repeat_byte(0xe1), + Address::repeat_byte(0xf1), + ); + // How many internal transactions the running program has made shows + // in the hash of the next one it would hand the Bridge. + let started = || { + let mut txs = InternalTxs::default(); + txs.begin(U256::from(1), U256::from(6_800_000)); + txs.enter(None); + txs + }; + let next = |txs: &mut InternalTxs| { + txs.reach_bridge(B256::repeat_byte(0x77), debtor, 0, heir, U256::ZERO) + }; + let none_made = next(&mut started()); + let one_made = { + let mut txs = started(); + txs.count(); + next(&mut txs) + }; + assert_ne!(none_made, one_made); + + // What the account holds and owes, and whom it names: whether it + // handed anything on, what it is left holding, what the journal took + // down, what it and its beneficiary owe, and whether an internal + // transaction was made. + let run = |held: u64, owed: u64, beneficiary: Address| { + let mut state = EvmState::default(); + let mut account = Account::default(); + account.info.balance = U256::from(held); + state.insert(debtor, account); + let mut journal = Vec::new(); + let mut chain = RskChainExt { + internal_txs: started(), + ..Default::default() + }; + chain.debts.add(other, U256::from(9)); + if owed > 0 { + chain.debts.add(debtor, U256::from(owed)); + } + let handed_on = hand_on_debt(&mut state, &mut journal, &mut chain, debtor, beneficiary); + assert_eq!(chain.debts.of(other), U256::from(9), "another's is its own"); + let was_held = match journal.as_slice() { + [] => None, + [JournalEntry::BalanceChange { + address, + old_balance, + }] if *address == debtor => Some(old_balance.to::()), + entries => panic!("{entries:?}"), + }; + ( + handed_on, + state[&debtor].info.balance.to::(), + was_held, + chain.debts.of(debtor).to::(), + chain.debts.of(heir).to::(), + next(&mut chain.internal_txs) == one_made, + ) + }; + // It owes nothing: SELFDESTRUCT is revm's alone. + assert_eq!(run(4, 0, heir), (false, 4, None, 0, 0, false)); + // It holds nothing of what it owes: all of it is handed on. + assert_eq!(run(0, 5, heir), (true, 0, None, 0, 5, true)); + // It holds part of it: that is paid, and the rest handed on. + assert_eq!(run(2, 5, heir), (true, 0, Some(2), 0, 3, true)); + // It holds all of it, or more: it is paid, and what is left is the + // account's to move as any balance. + assert_eq!(run(5, 5, heir), (false, 0, Some(5), 0, 0, false)); + assert_eq!(run(8, 5, heir), (false, 3, Some(8), 0, 0, false)); + // It names itself: rskj brings its balance to zero, and counts it. + assert_eq!(run(0, 5, debtor), (true, 0, None, 0, 0, true)); + assert_eq!(run(2, 5, debtor), (true, 0, Some(2), 0, 0, true)); + } } #[cfg(test)] @@ -1198,18 +1981,27 @@ mod thread_affinity_tests { &mut table, true, true, + true, false, false, true, false, false, + false, + false, + 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()), ) }) @@ -1218,7 +2010,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!( @@ -1229,11 +2021,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/rskj_tests/blocks/block_executor_test.rs b/crates/execution/src/rskj_tests/blocks/block_executor_test.rs new file mode 100644 index 0000000..feb2bf6 --- /dev/null +++ b/crates/execution/src/rskj_tests/blocks/block_executor_test.rs @@ -0,0 +1,345 @@ +//! rskj `co.rsk.core.bc.BlockExecutorTest`: what a block's run comes to, +//! and that the block carries it (`BlockExecutor.executeAndValidate`), run +//! as one list or as its sublists. The miner's own run, which picks a +//! block's transactions and lays them out (`executeForMining`, +//! `executeAndFill`), is not here. + +use super::support::header_at; +use crate::hardfork::RskHardforkConfig; +use crate::mining::workload::{address_of, sign}; +use crate::processor::{compute_ommers_hash, BlockProcessor, ProcessError}; +use alloy_primitives::{Address, Bytes, B256, U256}; +use k256::ecdsa::SigningKey; +use rustock_core::types::header::HeaderVersionFields; +use rustock_core::{ordered_tx_trie_root, Block, Header, Transaction}; +use rustock_storage::BlockStore; +use rustock_trie::{account_key, AccountState, MemoryTrieStore, TrieKeySlice, TrieNode, TrieStore}; +use std::sync::Arc; + +/// Testnet's chain id. +const TESTNET: u64 = 31; + +/// A height of testnet's where a block runs as its sublists. +const SUBLISTS: u64 = 8_000_000; + +/// Where RSKIP126 activates on mainnet: wasabi100. +const RSKIP126: u64 = 1_591_000; + +/// The key of `seed`. +fn key(seed: u8) -> SigningKey { + SigningKey::from_slice(&[seed; 32]).unwrap() +} + +/// What a block runs on: a state, and the store it is in. +struct World { + store: Arc, + root: TrieNode, +} + +impl World { + /// The accounts of `accounts`' keys, each holding the wei given. + fn of(accounts: &[(&SigningKey, u64)]) -> Self { + let store: Arc = Arc::new(MemoryTrieStore::new()); + let root = accounts + .iter() + .fold(TrieNode::empty(), |root, (key, balance)| { + let state = AccountState::new(U256::ZERO, U256::from(*balance)); + root.put( + &TrieKeySlice::from_key(&account_key(&address_of(key))), + &state.encode(), + store.as_ref(), + ) + }); + World { store, root } + } + + /// `account`'s balance in `state`. + fn balance(&self, state: &TrieNode, account: Address) -> U256 { + let key = account_key(&account); + let encoded = state + .get(&TrieKeySlice::from_key(&key), self.store.as_ref()) + .expect("the account is there"); + AccountState::decode(&encoded).unwrap().balance + } +} + +/// A transfer of 10 wei to `to` at `nonce`, at a gas price of 1, signed +/// by `from` for testnet. +fn transfer(from: &SigningKey, nonce: u64, to: Address) -> Transaction { + let mut transaction = Transaction { + nonce, + gas_price: U256::from(1), + gas_limit: U256::from(21_000), + to: Bytes::copy_from_slice(to.as_slice()), + value: U256::from(10), + input: Bytes::new(), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + sign(&mut transaction, from, TESTNET); + transaction +} + +/// A block at `number` of `transactions`, its header laid out with `edges` +/// where they are given, and with testnet's gas limit, which leaves each +/// parallel sublist 1,000,000. +fn block(number: u64, transactions: Vec, edges: Option>) -> Block { + let mut header = header_at(number); + header.gas_limit = U256::from(12_000_000); + header.version_fields = edges.map(|edges| HeaderVersionFields { + version: 1, + edges: Some(edges), + base_event: None, + }); + Block { + header, + transactions, + ommers: vec![], + } +} + +/// A processor of `chain`'s blocks, and the folder its store is in. +fn processor(chain: RskHardforkConfig) -> (BlockProcessor, tempfile::TempDir) { + let dir = tempfile::tempdir().unwrap(); + let blocks = Arc::new(BlockStore::open(dir.path()).unwrap()); + (BlockProcessor::new(chain, blocks), dir) +} + +/// `block`, its header carrying what its run on `world` comes to, its +/// transactions' root taken as the Unitrie's where `unitrie`. +fn carrying(processor: &BlockProcessor, world: &World, mut block: Block, unitrie: bool) -> Block { + let ran = processor + .execute_block(&block, &world.root, world.store.clone()) + .expect("the block runs"); + let header = &mut block.header; + header.transactions_root = ordered_tx_trie_root(&block.transactions, unitrie); + header.ommers_hash = compute_ommers_hash(&block.ommers); + header.state_root = ran.state_root_hash; + header.receipts_root = ran.receipts_root; + header.gas_used = ran.gas_used; + header.paid_fees = ran.paid_fees; + header.logs_bloom = ran.logs_bloom; + block +} + +/// rskj `executeBlockWithoutTransaction`: a block of nothing leaves the +/// state as it was, with no receipts, laid out or not. +#[test] +fn a_block_without_transactions_leaves_the_state_as_it_was() { + let world = World::of(&[(&key(1), 30_000)]); + let (processor, _dir) = processor(RskHardforkConfig::testnet()); + for edges in [None, Some(vec![])] { + let ran = processor + .execute_block( + &block(SUBLISTS, vec![], edges), + &world.root, + world.store.clone(), + ) + .unwrap(); + assert!(ran.receipts.is_empty()); + assert_eq!( + ran.state_root_hash, + world.root.compute_hash(world.store.as_ref()) + ); + } +} + +/// rskj `executeBlockWithTwoTransactions`: two transfers from one sender at +/// a gas price of 1 use 21,000 each, counted up across the block, and pay +/// 42,000 in fees; the sender is left 60,000 less those and the 20 wei +/// sent. +#[test] +fn two_transfers_count_their_gas_up_and_pay_their_fees() { + let (sender, recipient) = (key(1), key(2)); + let world = World::of(&[(&sender, 60_000)]); + let (processor, _dir) = processor(RskHardforkConfig::testnet()); + let to = address_of(&recipient); + let two = block( + SUBLISTS, + vec![transfer(&sender, 0, to), transfer(&sender, 1, to)], + None, + ); + let ran = processor + .execute_block(&two, &world.root, world.store.clone()) + .unwrap(); + let gas: Vec<_> = ran + .receipts + .iter() + .map(|receipt| (receipt.gas_used, receipt.cumulative_gas_used)) + .collect(); + assert_eq!(gas, [(21_000, 21_000), (21_000, 42_000)]); + assert!(ran.receipts.iter().all(|receipt| receipt.status)); + assert_eq!(ran.gas_used, 42_000); + assert_eq!(ran.paid_fees, U256::from(42_000)); + let left = world.balance(&ran.new_state_root, address_of(&sender)); + assert_eq!(left, U256::from(60_000 - 42_000 - 20)); + assert_eq!(world.balance(&ran.new_state_root, to), U256::from(20)); +} + +/// rskj `executeBlockWithTxThatMakesBlockInvalidSenderHasNoBalance`: a +/// transfer from an account with nothing to pay for it makes its block +/// invalid. +#[test] +fn a_sender_with_nothing_to_pay_makes_its_block_invalid() { + let (payer, recipient, penniless) = (key(1), key(2), key(3)); + let world = World::of(&[(&payer, 30_000), (&recipient, 10), (&penniless, 0)]); + let (processor, _dir) = processor(RskHardforkConfig::testnet()); + let to = address_of(&recipient); + let paid = block(SUBLISTS, vec![transfer(&payer, 0, to)], None); + assert!(processor + .execute_block(&paid, &world.root, world.store.clone()) + .is_ok()); + let unpaid = block( + SUBLISTS, + vec![transfer(&payer, 0, to), transfer(&penniless, 0, to)], + None, + ); + let refused = processor + .execute_block(&unpaid, &world.root, world.store.clone()) + .unwrap_err(); + assert!(matches!(refused, ProcessError::Execution(_)), "{refused}"); +} + +/// rskj `validateBlock`, `invalidBlockBadStateRoot`, +/// `invalidBlockBadReceiptsRoot`, `invalidBlockBadGasUsed`, +/// `invalidBlockBadPaidFees` and `invalidBlockBadLogsBloom`: a block +/// carrying what its run comes to is valid, and one whose state root, +/// receipts root, gas used, fees or logs bloom is off is refused for it, +/// run as one list or as its sublists. +#[test] +fn a_block_is_held_to_what_its_run_comes_to() { + let (sender, recipient) = (key(1), key(2)); + let world = World::of(&[(&sender, 30_000), (&recipient, 10)]); + let (processor, _dir) = processor(RskHardforkConfig::testnet()); + for edges in [None, Some(vec![1])] { + let one = block( + SUBLISTS, + vec![transfer(&sender, 0, address_of(&recipient))], + edges, + ); + let valid = carrying(&processor, &world, one, true); + assert!(processor + .process_block(&valid, &world.root, world.store.clone()) + .is_ok()); + let refused = |off: fn(&mut Header)| { + let mut block = valid.clone(); + off(&mut block.header); + processor + .process_block(&block, &world.root, world.store.clone()) + .unwrap_err() + }; + assert!(matches!( + refused(|header| header.state_root.0[0] = header.state_root.0[0].wrapping_add(1)), + ProcessError::StateRootMismatch { .. } + )); + assert!(matches!( + refused(|header| header.receipts_root.0[0] = header.receipts_root.0[0].wrapping_add(1)), + ProcessError::ReceiptsRootMismatch { .. } + )); + assert!(matches!( + refused(|header| header.gas_used = 0), + ProcessError::GasUsedMismatch { .. } + )); + assert!(matches!( + refused(|header| header.paid_fees = U256::ZERO), + ProcessError::PaidFeesMismatch { .. } + )); + assert!(matches!( + refused(|header| header.logs_bloom.0[0] = header.logs_bloom.0[0].wrapping_add(1)), + ProcessError::LogsBloomMismatch + )); + } +} + +/// rskj `validateStateRootWithRskip126DisabledAndValidStateRoot` and +/// `validateStateRootWithRskip126DisabledAndInvalidStateRoot`: below +/// RSKIP126 a block's state root isn't held to its run, so one carrying +/// the state's root and one carrying 0x01020304 both pass +/// (`BlockExecutor.validateStateRoot`, 319-332). From RSKIP126 the second +/// is refused. +#[test] +fn below_rskip126_a_blocks_state_root_is_not_held_to_its_run() { + let world = World::of(&[(&key(1), 30_000)]); + let (processor, _dir) = processor(RskHardforkConfig::mainnet()); + for number in [RSKIP126 - 1, RSKIP126] { + let unitrie = number >= RSKIP126; + let valid = carrying(&processor, &world, block(number, vec![], None), unitrie); + assert!(processor + .process_block(&valid, &world.root, world.store.clone()) + .is_ok()); + let mut other = valid; + other.header.state_root = B256::left_padding_from(&[1, 2, 3, 4]); + let other = processor.process_block(&other, &world.root, world.store.clone()); + if unitrie { + assert!(matches!(other, Err(ProcessError::StateRootMismatch { .. }))); + } else { + assert!(other.is_ok(), "#{number}"); + } + } +} + +/// Ten transfers, each from one of the first ten keys to the one ten on. +fn ten_transfers(keys: &[SigningKey]) -> Vec { + (0..10) + .map(|i| transfer(&keys[i], 0, address_of(&keys[i + 10]))) + .collect() +} + +/// rskj `executeParallelBlockAgainstSequentialBlock`, +/// `executeParallelBlocksWithDifferentSubsets` and +/// `executeParallelBlockTwice`: ten transfers that touch nothing of each +/// other's come to one state run as one list, as sublists of four, four +/// and two, as sublists of five and five, and again. +#[test] +fn independent_transfers_come_to_one_state_however_their_block_splits_them() { + let keys: Vec = (1..=20).map(key).collect(); + let funded: Vec<_> = keys.iter().map(|key| (key, 60_000)).collect(); + let world = World::of(&funded); + let (processor, _dir) = processor(RskHardforkConfig::testnet()); + let state = |edges: Vec| { + let ten = block(SUBLISTS, ten_transfers(&keys), Some(edges)); + let ran = processor + .execute_block(&ten, &world.root, world.store.clone()) + .unwrap(); + assert_eq!(ran.receipts.len(), 10); + ran.state_root_hash + }; + let as_one_list = state(vec![]); + assert_eq!(state(vec![4, 8]), as_one_list); + assert_eq!(state(vec![5]), as_one_list); + assert_eq!(state(vec![4, 8]), as_one_list, "and again"); +} + +/// rskj `executeInvalidParallelBlockDueToCollision` and +/// `ifThereIsACollisionBetweenParallelAndSequentialSublistsTxShouldNotBeConsidered`: +/// two accounts each sending to the other collide, each in a parallel +/// sublist of its own, and the block is invalid; with the second in the +/// sequential sublist they don't, and both run. +#[test] +fn parallel_sublists_may_not_touch_what_another_writes() { + let (one, other) = (key(1), key(2)); + let world = World::of(&[(&one, 60_000), (&other, 60_000)]); + let (processor, _dir) = processor(RskHardforkConfig::testnet()); + let each_to_the_other = || { + vec![ + transfer(&one, 0, address_of(&other)), + transfer(&other, 0, address_of(&one)), + ] + }; + let apart = block(SUBLISTS, each_to_the_other(), Some(vec![1, 2])); + let refused = processor + .execute_block(&apart, &world.root, world.store.clone()) + .unwrap_err(); + assert!( + matches!(refused, ProcessError::SublistCollision { .. }), + "{refused}" + ); + let one_in_sequence = block(SUBLISTS, each_to_the_other(), Some(vec![1])); + let ran = processor + .execute_block(&one_in_sequence, &world.root, world.store.clone()) + .unwrap(); + assert_eq!(ran.receipts.len(), 2); + assert!(ran.receipts.iter().all(|receipt| receipt.status)); +} diff --git a/crates/execution/src/rskj_tests/blocks/block_txs_validation_rule_test.rs b/crates/execution/src/rskj_tests/blocks/block_txs_validation_rule_test.rs new file mode 100644 index 0000000..c6bb3d5 --- /dev/null +++ b/crates/execution/src/rskj_tests/blocks/block_txs_validation_rule_test.rs @@ -0,0 +1,60 @@ +//! rskj `co.rsk.validators.BlockTxsValidationRuleTest`: each transaction of +//! a block carries its sender's next nonce, counting from the parent's state +//! up through the block (`BlockTxsValidationRule`). rskj refuses a block +//! that doesn't before it runs; rustock refuses it as it runs, when the +//! transaction's nonce isn't its sender's. + +use super::support::run_block_on_mainnet; +use crate::executor::ExecutionError; +use alloy_primitives::Address; + +/// A block at mainnet's #8,000,000. +const NUMBER: u64 = 8_000_000; + +/// Whether a block of `transactions` runs where `accounts` hold their +/// nonces; a refusal must be the nonce's. +fn runs(accounts: &[(Address, u64)], transactions: &[(Address, u64)]) -> bool { + match run_block_on_mainnet(NUMBER, accounts, transactions) { + Ok(()) => true, + Err(ExecutionError::Evm(why)) if why.to_lowercase().contains("nonce") => false, + Err(other) => panic!("refused for another reason: {other}"), + } +} + +const SENDER: Address = Address::repeat_byte(0x5e); + +/// rskj `validNonceSame`: a sender at 42 sends at 42. +#[test] +fn a_transaction_at_its_senders_nonce_is_valid() { + assert!(runs(&[(SENDER, 42)], &[(SENDER, 42)])); +} + +/// rskj `invalidNonceLower` and `invalidNonceHigher`: at 41 or 43 the +/// block is refused. +#[test] +fn a_transaction_below_or_above_its_senders_nonce_is_refused() { + assert!(!runs(&[(SENDER, 42)], &[(SENDER, 41)])); + assert!(!runs(&[(SENDER, 42)], &[(SENDER, 43)])); +} + +/// rskj `validTransactionsWithSameNonceAndDifferentSenders`: two senders +/// at 64 each send at 64. +#[test] +fn two_senders_may_send_at_the_same_nonce() { + let other = Address::repeat_byte(0x6f); + assert!(runs( + &[(SENDER, 64), (other, 64)], + &[(SENDER, 64), (other, 64)] + )); +} + +/// rskj `validConsecutiveTransactionsFromSameSender`, +/// `invalidTransactionsFromSameSenderWithSkippedNonce` and +/// `invalidTransactionsWithSameNonceAndSameSender`: a sender at 42 sends at +/// 42 and 43, and not at 42 and 44, nor twice at 42. +#[test] +fn a_senders_nonce_counts_up_through_the_block() { + assert!(runs(&[(SENDER, 42)], &[(SENDER, 42), (SENDER, 43)])); + assert!(!runs(&[(SENDER, 42)], &[(SENDER, 42), (SENDER, 44)])); + assert!(!runs(&[(SENDER, 42)], &[(SENDER, 42), (SENDER, 42)])); +} diff --git a/crates/execution/src/rskj_tests/blocks/constants_test.rs b/crates/execution/src/rskj_tests/blocks/constants_test.rs new file mode 100644 index 0000000..4c41724 --- /dev/null +++ b/crates/execution/src/rskj_tests/blocks/constants_test.rs @@ -0,0 +1,17 @@ +//! rskj `org.ethereum.config.ConstantsTest`: the limits rskj keeps as +//! constants, where a block's run reads them. + +use super::support::{creation, run_on_mainnet, LOVELL700}; + +/// rskj `maxInitcodeSizeTest`, from the rest of the class: from RSKIP438 a +/// creation's initcode may be 49,152 bytes (`Constants.getMaxInitCodeSize`), +/// and a transaction carrying one byte more is refused +/// (`TransactionExecutor.init`); before RSKIP438 none is. +#[test] +fn from_rskip438_a_creations_initcode_is_held_to_49152_bytes() { + let at_limit = run_on_mainnet(LOVELL700, &creation(vec![0; 49_152])).unwrap(); + assert!(at_limit.success); + assert!(run_on_mainnet(LOVELL700, &creation(vec![0; 49_153])).is_err()); + let before = run_on_mainnet(LOVELL700 - 1, &creation(vec![0; 49_153])).unwrap(); + assert!(before.success); +} diff --git a/crates/execution/src/rskj_tests/blocks/mod.rs b/crates/execution/src/rskj_tests/blocks/mod.rs new file mode 100644 index 0000000..7fe0c00 --- /dev/null +++ b/crates/execution/src/rskj_tests/blocks/mod.rs @@ -0,0 +1,8 @@ +//! A block's execution, the parallel run, and the miner. + +mod support; + +mod block_executor_test; +mod block_txs_validation_rule_test; +mod constants_test; +mod transaction_test; diff --git a/crates/execution/src/rskj_tests/blocks/support.rs b/crates/execution/src/rskj_tests/blocks/support.rs new file mode 100644 index 0000000..d03a95d --- /dev/null +++ b/crates/execution/src/rskj_tests/blocks/support.rs @@ -0,0 +1,134 @@ +//! What the ported tests of a block's run share. + +use crate::executor::{ExecutionError, RskExecutor, TxExecutionResult}; +use crate::hardfork::RskHardforkConfig; +use alloy_primitives::{Address, Bloom, Bytes, B256, U256}; +use rustock_core::{Header, Transaction}; +use rustock_storage::BlockStore; +use rustock_trie::{account_key, AccountState, MemoryTrieStore, TrieKeySlice, TrieNode}; +use std::sync::Arc; + +/// Mainnet's lovell700, where RSKIP438 activates. +pub(super) const LOVELL700: u64 = 7_338_024; + +/// The account that sends the transactions run here. +pub(super) const SENDER: Address = Address::repeat_byte(0x5e); + +/// A header at `number`, with nothing a lone transaction's run reads but +/// its height, its gas limit and its coinbase. +pub(super) fn header_at(number: u64) -> Header { + Header { + parent_hash: B256::ZERO, + ommers_hash: B256::ZERO, + beneficiary: Address::repeat_byte(0x01), + state_root: B256::ZERO, + transactions_root: B256::ZERO, + receipts_root: B256::ZERO, + logs_bloom: Bloom::ZERO, + extension_data: None, + difficulty: U256::from(1_000_000), + number, + gas_limit: U256::from(6_800_000), + gas_used: 0, + timestamp: 1_700_000_000, + 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: None, + bitcoin_merged_mining_merkle_proof: None, + bitcoin_merged_mining_coinbase_transaction: None, + cached_hash: None, + cached_hash_for_merged_mining: None, + kept: None, + } +} + +/// A creation carrying `init_code`, priced at nothing, with a gas limit +/// of 1,000,000. Unsigned: who sends it is given where it runs. +pub(super) fn creation(init_code: Vec) -> Transaction { + Transaction { + nonce: 0, + gas_price: U256::ZERO, + gas_limit: U256::from(1_000_000), + to: Bytes::new(), + value: U256::ZERO, + input: Bytes::from(init_code), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + } +} + +/// A state of `accounts`, each holding one RBTC at the nonce given, in +/// `store`. +fn funded(store: &MemoryTrieStore, accounts: &[(Address, u64)]) -> TrieNode { + accounts + .iter() + .fold(TrieNode::empty(), |root, (account, nonce)| { + let key = account_key(account); + let state = + AccountState::new(U256::from(*nonce), U256::from(10u64).pow(U256::from(18))); + root.put(&TrieKeySlice::from_key(&key), &state.encode(), store) + }) +} + +/// `transaction`, from [`SENDER`] holding one RBTC, run alone on mainnet at +/// block `number`. +pub(super) fn run_on_mainnet( + number: u64, + transaction: &Transaction, +) -> Result { + let store = Arc::new(MemoryTrieStore::new()); + let root = funded(&store, &[(SENDER, 0)]); + let blocks_dir = tempfile::tempdir().unwrap(); + let blocks = Arc::new(BlockStore::open(blocks_dir.path()).unwrap()); + RskExecutor::new(RskHardforkConfig::mainnet(), blocks).execute_tx( + &header_at(number), + transaction, + SENDER, + &root, + store, + ) +} + +/// A transfer of one wei at `nonce`, priced at nothing. Unsigned: who sends +/// it is given where it runs. +fn transfer(nonce: u64) -> Transaction { + Transaction { + nonce, + gas_price: U256::ZERO, + gas_limit: U256::from(21_000), + to: Bytes::copy_from_slice(Address::repeat_byte(0x02).as_slice()), + value: U256::from(1), + input: Bytes::new(), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + } +} + +/// A block on mainnet at `number` of `transactions`, each a transfer from +/// its sender at its nonce, run on a state where `accounts` hold one RBTC +/// at theirs. +pub(super) fn run_block_on_mainnet( + number: u64, + accounts: &[(Address, u64)], + transactions: &[(Address, u64)], +) -> Result<(), ExecutionError> { + let store = Arc::new(MemoryTrieStore::new()); + let root = funded(&store, accounts); + let transactions: Vec<(Transaction, Address)> = transactions + .iter() + .map(|(sender, nonce)| (transfer(*nonce), *sender)) + .collect(); + let blocks_dir = tempfile::tempdir().unwrap(); + let blocks = Arc::new(BlockStore::open(blocks_dir.path()).unwrap()); + RskExecutor::new(RskHardforkConfig::mainnet(), blocks) + .execute_block(&header_at(number), &transactions, &root, store) + .map(|_| ()) +} diff --git a/crates/execution/src/rskj_tests/blocks/transaction_test.rs b/crates/execution/src/rskj_tests/blocks/transaction_test.rs new file mode 100644 index 0000000..dcced41 --- /dev/null +++ b/crates/execution/src/rskj_tests/blocks/transaction_test.rs @@ -0,0 +1,32 @@ +//! rskj `co.rsk.core.TransactionTest`: what a transaction costs before it +//! runs (`Transaction.transactionCost`), which a transaction's gas must +//! cover and its block pays. + +use super::support::{creation, run_on_mainnet, LOVELL700}; + +/// rskj `testTransactionCostForContractCreationWithRSKIP438Disabled`, +/// `testTransactionCostForContractCreationWithRSKIP438Enabled` and +/// `testTransactionCostForContractCreationWithNullDataAndRSKIP438Enabled`: +/// a creation costs 53,000 and its data; from RSKIP438 2 more for each 32 +/// bytes of its initcode, the last counted whole, and nothing more for +/// none (`InitcodeCostCalculator`). rskj's test prices one byte of 0xff +/// with RSKIP400 off, which no network has where RSKIP438 is on; here the +/// initcode is zero bytes, 4 each on either side of RSKIP400, which run as +/// STOP and leave nothing to pay for running or deploying. +#[test] +fn from_rskip438_a_creation_pays_for_its_initcode_by_the_word() { + let cost = |number: u64, init_code: Vec| { + let result = run_on_mainnet(number, &creation(init_code)).unwrap(); + assert!(result.success, "block {number}"); + result.gas_used + }; + // One word. + assert_eq!(cost(LOVELL700 - 1, vec![0]), 53_004); + assert_eq!(cost(LOVELL700, vec![0]), 53_006); + // Thirty-three bytes: two words. + assert_eq!(cost(LOVELL700 - 1, vec![0; 33]), 53_132); + assert_eq!(cost(LOVELL700, vec![0; 33]), 53_136); + // None. + assert_eq!(cost(LOVELL700 - 1, Vec::new()), 53_000); + assert_eq!(cost(LOVELL700, Vec::new()), 53_000); +} diff --git a/crates/execution/src/rskj_tests/bridge_federation/bridge_utils_legacy_test.rs b/crates/execution/src/rskj_tests/bridge_federation/bridge_utils_legacy_test.rs new file mode 100644 index 0000000..13ad597 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_federation/bridge_utils_legacy_test.rs @@ -0,0 +1,109 @@ +//! rskj `co.rsk.peg.BridgeUtilsLegacyTest`: the utilities rskj ran before +//! RSKIP284, RSKIP293 and RSKIP271, through the Bridge methods that reach +//! them. + +use alloy_primitives::Address as RskAddress; + +use super::support::{self, flyover_code, FlyoverCall}; +use crate::bridge::constants::BridgeConstants; +use crate::hardfork::RskHardforkConfig; + +/// Mainnet's first block with RSKIP271 (hop400). +const MAINNET_RSKIP271: u64 = 4_598_500; + +/// rskj `BridgeUtilsLegacy.calculatePegoutTxSize`, the size of the regular +/// peg-out (two inputs, two outputs) from RSKIP219 until RSKIP271 +/// (BridgeUtilsLegacy.java:42-55): for each input its signatures at 72 bytes +/// each, its redeem script and 41 bytes; 35 bytes for each output, and 4. For +/// 13 members it is 2,054 bytes, where from RSKIP271 bitcoinj's serialization +/// gives 2,058. `releaseBtc` asks a peg-out to be worth the fee per KB times +/// that size, with the network's percentage on top (BridgeSupport.java: +/// 958-983): what the legacy size asks is queued at the last block before +/// RSKIP271, and is short at its first. +#[test] +fn before_rskip271_the_legacy_pegout_size_sets_releasebtcs_minimum() { + let (config, cfg) = (BridgeConstants::mainnet(), RskHardforkConfig::mainnet()); + let keys = support::keys(1, 13); + let redeem = support::standard_redeem(&keys); + let legacy_size = 4 + 2 * (7 * 72 + redeem.len() + 1 + 40) + 2 * (25 + 1 + 9); + assert_eq!(legacy_size, 2054); + let fee_per_kb = 1_000_000u64; + let minimum = |size: u64| { + let fee = fee_per_kb * size / 1000; + fee + fee * config.minimum_pegout_value_percentage_to_receive_after_fee / 100 + }; + let queued = |block, satoshis| { + support::release_queued((&config, &cfg), block, &keys, fee_per_kb, satoshis) + }; + assert_eq!(queued(MAINNET_RSKIP271 - 1, minimum(2054)), 1); + assert_eq!(queued(MAINNET_RSKIP271 - 1, minimum(2054) - 1), 0); + assert_eq!(queued(MAINNET_RSKIP271, minimum(2054)), 0); + assert_eq!(queued(MAINNET_RSKIP271, minimum(2058)), 1); +} + +/// What `registerFastBridgeBtcTransaction` answers for the two Bitcoin +/// addresses `user` and `lp`, with a transaction it never gets to. +fn answer(net: (BridgeConstants, RskHardforkConfig), block: u64, user: &[u8], lp: &[u8]) -> i128 { + let call = FlyoverCall { + tx: &[1, 0, 0, 0, 1], + height: i64::from(i32::MAX), + derivation_arguments: [0x11; 32], + user, + lbc: RskAddress::repeat_byte(0x42), + lp, + }; + call.answer(&mut support::ctx_at(block), &net.0, &net.1) +} + +/// rskj `BridgeUtilsLegacy.deserializeBtcAddressWithVersionLegacy`, which +/// `Bridge.registerFlyoverBtcTransaction` reads both its Bitcoin addresses +/// with before RSKIP284 (BridgeUtilsLegacy.java:59-79): no bytes throw, and +/// the hash is the 20 bytes after the version, which throws for fewer +/// (`System.arraycopy`). bitcoinj then refuses a version its network doesn't +/// take, and the method answers GENERIC_ERROR for each. The blocks are past +/// iris300 and before RSKIP284: testnet #2,122,593 and mainnet #4,000,000. +#[test] +fn before_rskip284_a_flyover_address_is_refused_short_or_of_another_network() { + let testnet = || (BridgeConstants::testnet(), RskHardforkConfig::testnet()); + let mainnet = || (BridgeConstants::mainnet(), RskHardforkConfig::mainnet()); + let with = |version: u8| [&[version][..], &[0x41; 20][..]].concat(); + for (case, net, block, user, lp) in [ + ("no bytes", testnet(), 2_122_593, vec![], with(0x6f)), + ( + "a hash of one byte", + testnet(), + 2_122_593, + vec![0x6f, 0x41], + with(0x6f), + ), + ( + "a testnet p2sh version on mainnet", + mainnet(), + 4_000_000, + with(0xc4), + with(0x00), + ), + ( + "a mainnet p2sh version on testnet", + testnet(), + 2_122_593, + with(0x05), + with(0x6f), + ), + ] { + assert_eq!( + answer(net, block, &user, &lp), + flyover_code::GENERIC_ERROR, + "{case}" + ); + } + // The same addresses on their own networks get past them. + assert_ne!( + answer(mainnet(), 4_000_000, &with(0x05), &with(0x00)), + flyover_code::GENERIC_ERROR + ); + assert_ne!( + answer(testnet(), 2_122_593, &with(0x6f), &with(0x6f)), + flyover_code::GENERIC_ERROR + ); +} diff --git a/crates/execution/src/rskj_tests/bridge_federation/bridge_utils_test.rs b/crates/execution/src/rskj_tests/bridge_federation/bridge_utils_test.rs new file mode 100644 index 0000000..0bedd13 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_federation/bridge_utils_test.rs @@ -0,0 +1,726 @@ +//! rskj `co.rsk.peg.BridgeUtilsTest`: a Bitcoin address written and read with +//! its version (RSKIP284), the size of a peg-out (RSKIP271, RSKIP305), and the +//! value a Flyover peg-in sends to its federations (RSKIP293). +//! +//! rskj reaches these utilities from three Bridge methods, and so do these +//! tests: `registerFastBridgeBtcTransaction` reads and writes the addresses +//! and counts the value, `getEstimatedFeesForNextPegOutEvent` sizes a peg-out +//! of two inputs, and `releaseBtc` sizes the regular one for its minimum. + +use alloy_primitives::{hex, Address as RskAddress, U256}; + +use super::support::{ + self, flyover_code, FlyoverCall, NEW_FEDERATION, OLD_FEDERATION, PEGIN_HEIGHT, WEI_PER_SATOSHI, +}; +use crate::bridge::constants::BridgeConstants; +use crate::bridge::{peg, storage}; +use crate::hardfork::{RskHardforkConfig, RskNetworkUpgrade}; +use crate::precompiles::BridgeTxContext; + +// --- the address with its version (RSKIP284) -------------------------------- + +/// The hash160 of rskj's test addresses. +const HASH: [u8; 20] = hex!("41aec8ca3fcf17e62077e9f35961385360d6a570"); + +/// `[version || hash160]`. +fn address(version: u8) -> Vec { + [&[version][..], &HASH[..]].concat() +} + +/// A network, and the first block it has RSKIP284 at: testnet moves it to +/// #2,581,800, mainnet has it with hop400. The Flyover method opens with +/// iris300, before either. +struct Net { + config: BridgeConstants, + cfg: RskHardforkConfig, + rskip284: u64, +} + +fn testnet() -> Net { + Net { + config: BridgeConstants::testnet(), + cfg: RskHardforkConfig::testnet(), + rskip284: 2_581_800, + } +} + +fn mainnet() -> Net { + Net { + config: BridgeConstants::mainnet(), + cfg: RskHardforkConfig::mainnet(), + rskip284: 4_598_500, + } +} + +/// What a crafted call needs to reach the addresses: a version and an input +/// count of one. +const ONE_INPUT: [u8; 5] = [1, 0, 0, 0, 1]; + +/// A Flyover call made with `user` and `lp` as the two Bitcoin addresses. +fn call_with<'a>(user: &'a [u8], lp: &'a [u8]) -> FlyoverCall<'a> { + FlyoverCall { + tx: &ONE_INPUT, + height: i64::from(i32::MAX), + derivation_arguments: [0x11; 32], + user, + lbc: RskAddress::repeat_byte(0x42), + lp, + } +} + +/// What the Flyover method answers at `block` when the derivation hash taken +/// over rskj's bytes for both addresses, `rskj_user` and `rskj_lp`, is marked +/// used: ALREADY_PROCESSED where rustock read and wrote them as rskj does. +fn answer_with_rskjs_bytes_used( + net: &Net, + block: u64, + (user, lp): (&[u8], &[u8]), + (rskj_user, rskj_lp): (&[u8], &[u8]), +) -> i128 { + let call = call_with(user, lp); + let mut ctx = support::ctx_at(block); + support::mark_flyover_hash_used(&mut ctx, call.tx, &call.derivation_hash(rskj_user, rskj_lp)); + call.answer(&mut ctx, &net.config, &net.cfg) +} + +/// rskj `BridgeUtils.serializeBtcAddressWithVersion` writes an address as its +/// version and its hash160: before RSKIP284 the version as +/// `BigInteger.valueOf(version).toByteArray()`, from it as one byte. Each +/// version an address can have there is written as one byte either way (a +/// testnet P2SH address, version 196, is refused before RSKIP284, so its +/// two-byte form is never written), and the Flyover derivation hash is taken +/// over it: a payment marked used under the hash of rskj's bytes is found +/// used, and one marked under nothing is not. +#[test] +fn a_flyover_address_is_written_as_its_version_byte_and_hash() { + let (testnet, mainnet) = (testnet(), mainnet()); + for (case, net, block, version) in [ + ( + "p2pkh testnet before rskip284", + &testnet, + testnet.rskip284 - 1, + 0x6f, + ), + ( + "p2pkh mainnet before rskip284", + &mainnet, + mainnet.rskip284 - 1, + 0x00, + ), + ( + "p2sh mainnet before rskip284", + &mainnet, + mainnet.rskip284 - 1, + 0x05, + ), + ( + "p2pkh testnet after rskip284", + &testnet, + testnet.rskip284, + 0x6f, + ), + ( + "p2sh testnet after rskip284", + &testnet, + testnet.rskip284, + 0xc4, + ), + ( + "p2pkh mainnet after rskip284", + &mainnet, + mainnet.rskip284, + 0x00, + ), + ( + "p2sh mainnet after rskip284", + &mainnet, + mainnet.rskip284, + 0x05, + ), + ] { + let rskj_bytes = [&[version][..], &HASH[..]].concat(); + let given = address(version); + assert_eq!( + answer_with_rskjs_bytes_used(net, block, (&given, &given), (&rskj_bytes, &rskj_bytes)), + flyover_code::ALREADY_PROCESSED, + "{case}" + ); + let call = call_with(&given, &given); + assert_ne!( + call.answer(&mut support::ctx_at(block), &net.config, &net.cfg), + flyover_code::ALREADY_PROCESSED, + "{case}, nothing marked" + ); + } +} + +/// rskj `BridgeUtils.deserializeBtcAddressWithVersion` from RSKIP284: exactly +/// 21 bytes, the first an unsigned version the network takes, and the +/// address is that version and the 20 bytes after it. Where it throws, +/// `Bridge.registerFlyoverBtcTransaction` answers GENERIC_ERROR. +#[test] +fn from_rskip284_a_flyover_address_is_21_bytes_of_a_version_its_network_takes() { + let (testnet, mainnet) = (testnet(), mainnet()); + for (case, net, version) in [ + ("p2pkh testnet", &testnet, 0x6f), + ("p2sh testnet", &testnet, 0xc4), + ("p2pkh mainnet", &mainnet, 0x00), + ("p2sh mainnet", &mainnet, 0x05), + ] { + let given = address(version); + assert_eq!( + answer_with_rskjs_bytes_used(net, net.rskip284, (&given, &given), (&given, &given)), + flyover_code::ALREADY_PROCESSED, + "{case} is read as given" + ); + } + + let with_extra_bytes = [address(0x6f), hex!("0000aaaaeeee1111ffff").to_vec()].concat(); + for (case, net, user) in [ + ( + "a testnet p2pkh address on mainnet", + &mainnet, + address(0x6f), + ), + ("a testnet p2sh address on mainnet", &mainnet, address(0xc4)), + ("a mainnet p2sh address on testnet", &testnet, address(0x05)), + ("bytes after the hash", &testnet, with_extra_bytes), + ("a hash of one byte", &testnet, vec![0x6f, 0x41]), + ("no bytes", &testnet, vec![]), + ] { + let lp = address(if std::ptr::eq(net, &mainnet) { + 0x00 + } else { + 0x6f + }); + let call = call_with(&user, &lp); + assert_eq!( + call.answer(&mut support::ctx_at(net.rskip284), &net.config, &net.cfg), + flyover_code::GENERIC_ERROR, + "{case}" + ); + } +} + +// --- the size of a peg-out (RSKIP271, RSKIP305) ------------------------------ + +/// rskj `BridgeUtils.calculateLegacyTxSize` and `calculateSegwitTxSize` +/// (BridgeUtils.java:658-704), for a federation whose redeem script is +/// `redeem` bytes long and takes `signatures` signatures. A legacy spend is +/// what bitcoinj serializes for `inputs` inputs carrying the redeem script and +/// `outputs` outputs to the federation's P2SH address, and 72 bytes for each +/// signature of each input. A segwit spend is the BIP141 virtual size of an +/// input-less transaction with those outputs, 36 bytes of script for each +/// input, and the redeem script and the signatures as its witness. +fn rskj_pegout_size( + redeem: usize, + signatures: usize, + inputs: usize, + outputs: usize, + segwit: bool, +) -> usize { + let var_int = |n: usize| if n < 0xfd { 1 } else { 3 }; + // value, script length, OP_HASH160 <20 bytes> OP_EQUAL. + let p2sh_output = 8 + 1 + 23; + let signing = signatures * inputs * 72; + if !segwit { + // outpoint, script length, script, sequence. + let input = 36 + var_int(redeem) + redeem + 4; + return 4 + + var_int(inputs) + + inputs * input + + var_int(outputs) + + outputs * p2sh_output + + 4 + + signing; + } + let base = 4 + var_int(0) + var_int(outputs) + outputs * p2sh_output + 4 + inputs * 36; + let total = base + signing + inputs * redeem; + (total + 3 * base) / 4 +} + +/// What rustock's `getEstimatedFeesForNextPegOutEvent` answers on mainnet at +/// block 6,000,000, between RSKIP385 and RSKIP305, for `keys` stored as the +/// active federation in `format`, with a peg-out queue that makes `outputs` +/// outputs. There rskj sizes a peg-out of two inputs and two outputs more +/// than the queue has (`getEstimatedFeesFromInputsAndOutputsCount`, +/// BridgeSupport.java:2647), and at a fee of 1,000 satoshis per KB the fee is +/// the size. +fn estimated_pegout_size( + config: &BridgeConstants, + keys: &[[u8; 33]], + format: u64, + outputs: usize, +) -> usize { + let cfg = RskHardforkConfig::mainnet(); + let mut ctx = support::ctx_at(6_000_000); + support::store_federation(&mut ctx, NEW_FEDERATION, keys, 1, format); + storage::bridge_store_u256(&mut ctx, storage::FEE_PER_KB_KEY, U256::from(1_000)); + let queued: Vec = (2..outputs) + .map(|_| peg::ReleaseRequest { + btc_dest_hash160: [0x01; 20], + amount_satoshis: 1_000_000, + rsk_tx_hash: None, + }) + .collect(); + storage::bridge_store_bytes( + &mut ctx, + storage::bridge_storage_key(storage::RELEASE_REQUEST_QUEUE_KEY), + &peg::serialize_release_queue_legacy(&queued), + ); + let out = + peg::get_estimated_fees_for_next_pegout(&mut ctx, 0, config, &cfg).expect("an estimate"); + U256::from_be_slice(&out.bytes).to::() +} + +/// `count` hex keys, from the private keys given, for a network's ERP keys. +fn erp_keys(privates: &[u16]) -> &'static [&'static str] { + let hexes: Vec<&'static str> = privates + .iter() + .map(|private| { + let key = hex::encode(support::public_key(&private.to_be_bytes())); + &*Box::leak(key.into_boxed_str()) + }) + .collect(); + Box::leak(hexes.into_boxed_slice()) +} + +/// rskj's peg-out sizes, each for its test's federation and its shape: a +/// standard federation of 13 members (7 signatures), the P2SH-P2WSH ERP +/// federation of the same members (mainnet's four emergency keys and +/// 52,560-block delay, `P2shP2wshErpFederationBuilder`), a non-standard ERP +/// federation of the keys fa01 to fa03 with the emergency keys fa03 to fa05 +/// and a 500-block delay, and the P2SH-P2WSH ERP federation of fa01 to fa03. +/// +/// rskj calls the sizer with two inputs and one or two outputs more than the +/// peg-out queue holds (BridgeSupport.java:959, :1228, :2647); its tests also +/// size spends of 9 to 100 inputs, which no caller makes. For each federation +/// rustock's estimate is held to the sizer at two inputs and each of the +/// test's output counts, and the sizer to every number rskj's tests assert. +#[test] +fn pegout_sizes_are_rskjs_for_each_federation_format() { + let mainnet = BridgeConstants::mainnet(); + let non_standard = BridgeConstants { + erp_fed_pubkeys: erp_keys(&[0xfa03, 0xfa04, 0xfa05]), + erp_fed_activation_delay: 500, + ..BridgeConstants::mainnet() + }; + let thirteen = support::keys(1, 13); + let three: Vec<[u8; 33]> = [0xfa01u16, 0xfa02, 0xfa03] + .iter() + .map(|k| support::public_key(&k.to_be_bytes())) + .collect(); + // (federation, its keys, format, segwit, rskj's (inputs, outputs, size)). + type Sizes = &'static [(usize, usize, usize)]; + let federations: [(&str, &BridgeConstants, &[[u8; 33]], u64, bool, Sizes); 4] = [ + ( + "standard, 13 members", + &mainnet, + &thirteen, + 1000, + false, + &[ + (2, 2, 2058), + (9, 2, 9002), + (10, 20, 10570), + (50, 200, 56010), + ], + ), + ( + "p2sh-p2wsh erp, 13 members", + &mainnet, + &thirteen, + 4000, + true, + &[(2, 2, 694), (9, 2, 2866), (10, 20, 3752), (50, 200, 21922)], + ), + ( + "non-standard erp, 3 members", + &non_standard, + &three, + 2000, + false, + &[(50, 200, 26510), (100, 50, 41810)], + ), + ( + "p2sh-p2wsh erp, 3 members", + &mainnet, + &three, + 4000, + true, + &[(50, 200, 13172), (100, 50, 15135)], + ), + ]; + let cfg = RskHardforkConfig::mainnet(); + for (federation, config, keys, format, segwit, sizes) in federations { + let redeem = peg::federation_redeem_for_format_pub(keys, format, config, &cfg, 6_000_000); + let signatures = keys.len() / 2 + 1; + for &(inputs, outputs, size) in sizes { + assert_eq!( + rskj_pegout_size(redeem.len(), signatures, inputs, outputs, segwit), + size, + "{federation}: {inputs} inputs, {outputs} outputs" + ); + assert_eq!( + estimated_pegout_size(config, keys, format, outputs), + rskj_pegout_size(redeem.len(), signatures, 2, outputs, segwit), + "{federation}: 2 inputs, {outputs} outputs" + ); + } + } +} + +/// rskj `BridgeUtils.getRegularPegoutTxSize`: a peg-out of two inputs and two +/// outputs, within 10% of a real one, signed by two of three members. It is +/// what `releaseBtc` asks a peg-out to be worth from RSKIP219: the fee per KB +/// times that size, plus the network's percentage on top +/// (`BridgeSupport.requestRelease`, BridgeSupport.java:958-983). A request +/// worth that much is queued, and one a satoshi short is not. +#[test] +fn the_regular_pegout_size_sets_releasebtcs_minimum() { + let (config, cfg) = (BridgeConstants::mainnet(), RskHardforkConfig::mainnet()); + let keys = support::keys(1, 3); + let redeem = support::standard_redeem(&keys); + let size = rskj_pegout_size(redeem.len(), 2, 2, 2, false); + assert_eq!(size, 654); + + // A real one: each input's script an OP_0, two 72-byte signatures and the + // redeem script, which takes OP_PUSHDATA1; two P2PKH outputs. + let signed_input = 1 + 2 * (1 + 72) + 2 + redeem.len(); + let real = 4 + 1 + 2 * (36 + 3 + signed_input + 4) + 1 + 2 * (8 + 1 + 25) + 4; + assert!(real.abs_diff(size) * 10 < size, "{real} against {size}"); + + let fee_per_kb = 1_000_000u64; + let fee = fee_per_kb * size as u64 / 1000; + let minimum = fee + fee * config.minimum_pegout_value_percentage_to_receive_after_fee / 100; + assert!( + minimum > config.minimum_pegout_tx_value, + "the fee is what binds" + ); + let queued = + |satoshis| support::release_queued((&config, &cfg), 6_000_000, &keys, fee_per_kb, satoshis); + assert_eq!(queued(minimum), 1); + assert_eq!(queued(minimum - 1), 0); +} + +// --- the value a Flyover peg-in sends (RSKIP293) ----------------------------- + +/// The RSK block the payments are registered at: past the regtest federation +/// activation age of the new federation, created at block 1, either side of +/// RSKIP383. +const RSK_BLOCK: u64 = 1_000; + +/// The user's and the liquidity provider's address: regtest P2PKH. +const USER: [u8; 21] = { + let mut address = [0x51; 21]; + address[0] = 0x6f; + address +}; + +/// The Flyover call that registers `raw`, at the height the chain holds it. +fn call(raw: &[u8]) -> FlyoverCall<'_> { + FlyoverCall { + tx: raw, + height: PEGIN_HEIGHT, + derivation_arguments: [0x11; 32], + user: &USER, + lbc: RskAddress::repeat_byte(0x42), + lp: &USER, + } +} + +/// The active federation's members, and the retiring one's. +fn active() -> Vec<[u8; 33]> { + support::keys(1, 3) +} + +fn retiring() -> Vec<[u8; 33]> { + support::keys(11, 3) +} + +/// The address a federation of `keys` takes this Flyover payment at. +fn flyover_address(keys: &[[u8; 33]]) -> bitcoin::ScriptBuf { + let hash = call(&[]).derivation_hash(&USER, &USER); + support::flyover_output(&hash, &support::standard_redeem(keys)) +} + +/// RSKj's regtest before RSKIP293: hop400 and what follows it past the block +/// the payments are registered at. +fn before_rskip293() -> RskHardforkConfig { + support::upgrades_from_at( + RskHardforkConfig::rskj_regtest(), + RskNetworkUpgrade::Hop400, + 1_000_000, + ) +} + +/// What a Flyover payment `tx` is answered with, its Bitcoin chain and the +/// Bridge's funds in place, the active federation `active()`, and with +/// `with_retiring` the retiring one `retiring()`; and the Bridge after it. +fn pay( + tx: &bitcoin::Transaction, + cfg: &RskHardforkConfig, + with_retiring: bool, +) -> (i128, impl crate::RskContextTr) { + let raw = bitcoin::consensus::serialize(tx); + let mut ctx = support::ctx_at(RSK_BLOCK); + support::btc_chain_holding(&mut ctx, &raw, cfg); + support::store_federation(&mut ctx, NEW_FEDERATION, &active(), 1, 1000); + if with_retiring { + support::store_federation(&mut ctx, OLD_FEDERATION, &retiring(), 1, 1000); + } + support::fund_bridge(&mut ctx); + let answer = call(&raw).answer(&mut ctx, &BridgeConstants::rskj_regtest(), cfg); + (answer, ctx) +} + +/// The UTXOs, by output index and value, the active federation holds. +fn active_utxos( + ctx: &mut CTX, + cfg: &RskHardforkConfig, +) -> Vec<(u32, u64)> { + storage::load_federation_utxos(ctx, cfg, RSK_BLOCK) + .iter() + .map(|utxo| (utxo.vout, utxo.value_satoshis)) + .collect() +} + +/// The same for the retiring federation. +fn retiring_utxos(ctx: &mut CTX) -> Vec<(u32, u64)> { + storage::load_old_federation_utxos(ctx) + .iter() + .map(|utxo| (utxo.vout, utxo.value_satoshis)) + .collect() +} + +/// The answer of a peg-in of `satoshis`: their value in wei. +fn locked(satoshis: u64) -> i128 { + i128::from(satoshis) * i128::from(WEI_PER_SATOSHI) +} + +const BTC: u64 = 100_000_000; + +/// rskj `BridgeUtils.validateFlyoverPeginValue` turns a payment of nothing to +/// the federation down with UNPROCESSABLE_TX_VALUE_ZERO_ERROR, before and from +/// RSKIP293: an output of 0 to it, or no output to it at all, whatever the +/// payment sends elsewhere. Nothing is registered. +#[test] +fn a_flyover_payment_of_nothing_to_the_federation_is_refused() { + let others = |n: u8| support::p2pkh(&[n; 20]); + for cfg in [before_rskip293(), RskHardforkConfig::rskj_regtest()] { + let rskip293 = cfg.has_rskip293(RSK_BLOCK); + for tx in [ + support::tx_paying(&[ + (0, flyover_address(&active())), + (BTC, others(1)), + (BTC, others(2)), + ]), + support::tx_paying(&[(BTC, others(1)), (BTC, others(2))]), + ] { + let (answer, mut ctx) = pay(&tx, &cfg, false); + assert_eq!(answer, flyover_code::VALUE_ZERO, "rskip293 {rskip293}"); + assert!(active_utxos(&mut ctx, &cfg).is_empty()); + } + } +} + +/// Before RSKIP293 a Flyover payment is checked against no minimum: a UTXO +/// below the minimum peg-in is taken, and every output to the federation's +/// address is registered, a 0-value one too (`BridgeUtilsLegacy +/// .getUTXOsSentToAddress`). +#[test] +fn before_rskip293_every_flyover_output_is_taken_whatever_its_value() { + let cfg = before_rskip293(); + let minimum = BridgeConstants::rskj_regtest().minimum_pegin_tx_value; + let elsewhere = support::p2pkh(&[0x01; 20]); + + let tx = support::tx_paying(&[ + (minimum - 1, flyover_address(&active())), + (BTC, elsewhere.clone()), + ]); + let (answer, mut ctx) = pay(&tx, &cfg, false); + assert_eq!(answer, locked(minimum - 1)); + assert_eq!(active_utxos(&mut ctx, &cfg), [(0, minimum - 1)]); + + let tx = support::tx_paying(&[ + (BTC, flyover_address(&active())), + (0, flyover_address(&active())), + (BTC, elsewhere), + ]); + let (answer, mut ctx) = pay(&tx, &cfg, false); + assert_eq!(answer, locked(BTC)); + assert_eq!(active_utxos(&mut ctx, &cfg), [(0, BTC), (1, 0)]); +} + +/// From RSKIP293 each UTXO a Flyover payment sends its federations must be +/// worth the minimum peg-in (`PegUtils.allUTXOsToFedAreAboveMinimumPeginValue`): +/// one a satoshi short of it is answered +/// UNPROCESSABLE_TX_UTXO_AMOUNT_SENT_BELOW_MINIMUM_ERROR, whatever the others +/// are worth, and UTXOs of the minimum and more are taken. +#[test] +fn from_rskip293_each_flyover_utxo_must_be_worth_the_minimum() { + let cfg = RskHardforkConfig::rskj_regtest(); + let minimum = BridgeConstants::rskj_regtest().minimum_pegin_tx_value; + + let tx = support::tx_paying(&[ + (minimum - 1, flyover_address(&active())), + (BTC, flyover_address(&active())), + ]); + let (answer, mut ctx) = pay(&tx, &cfg, false); + assert_eq!(answer, flyover_code::UTXO_BELOW_MINIMUM); + assert!(active_utxos(&mut ctx, &cfg).is_empty()); + + for second in [minimum, minimum + 1] { + let tx = support::tx_paying(&[ + (minimum, flyover_address(&active())), + (second, flyover_address(&retiring())), + ]); + let (answer, _) = pay(&tx, &cfg, true); + assert_eq!(answer, locked(minimum + second)); + } +} + +/// rskj `BridgeUtils.getAmountSentToAddresses` from RSKIP293: what a Flyover +/// payment sends to the active federation's address and the retiring one's, +/// together. +#[test] +fn from_rskip293_a_flyover_payment_counts_what_both_federations_are_sent() { + let cfg = RskHardforkConfig::rskj_regtest(); + for (case, outputs, expected) in [ + ( + "both", + vec![ + (BTC, flyover_address(&active())), + (BTC, flyover_address(&retiring())), + ], + 2 * BTC, + ), + ( + "the active one", + vec![(BTC, flyover_address(&active()))], + BTC, + ), + ( + "the retiring one", + vec![(BTC, flyover_address(&retiring()))], + BTC, + ), + ] { + let (answer, _) = pay(&support::tx_paying(&outputs), &cfg, true); + assert_eq!(answer, locked(expected), "{case}"); + } +} + +/// rskj `BridgeUtils.getUTXOsSentToAddresses` from RSKIP293, called for each +/// federation's address (BridgeSupport.java:2851, :2871): each federation +/// keeps the UTXOs sent to it, by their index in the payment, and an output +/// to anyone else, a native segwit one too, is passed over. +#[test] +fn from_rskip293_each_federation_keeps_the_flyover_utxos_it_is_sent() { + let cfg = RskHardforkConfig::rskj_regtest(); + let tx = support::tx_paying(&[ + (BTC, flyover_address(&active())), + (2 * BTC, flyover_address(&retiring())), + (BTC, support::p2pkh(&[0x01; 20])), + (3 * BTC, flyover_address(&active())), + ]); + let (answer, mut ctx) = pay(&tx, &cfg, true); + assert_eq!(answer, locked(6 * BTC)); + assert_eq!(active_utxos(&mut ctx, &cfg), [(0, BTC), (3, 3 * BTC)]); + assert_eq!(retiring_utxos(&mut ctx), [(1, 2 * BTC)]); + + let tx = support::tx_paying(&[ + (BTC, support::p2wpkh(&[0x01; 20])), + (BTC, flyover_address(&active())), + ]); + let (answer, mut ctx) = pay(&tx, &cfg, false); + assert_eq!(answer, locked(BTC)); + assert_eq!(active_utxos(&mut ctx, &cfg), [(1, BTC)]); +} + +/// Before RSKIP293 rskj counts a Flyover payment by reading every one of its +/// outputs as an address (`BridgeUtilsLegacy.getAmountSentToAddress`, +/// BridgeUtilsLegacy.java:104-108). bitcoinj's `Script.getToAddress` throws +/// for an output that is neither P2PKH nor P2SH (Script.java:320-329), a +/// native segwit one or an OP_RETURN, and `Bridge.registerFlyoverBtcTransaction` +/// answers the throw with GENERIC_ERROR (Bridge.java:1382-1385): nothing is +/// transferred or registered. +#[test] +fn before_rskip293_a_flyover_payment_with_an_output_that_is_no_address_is_a_generic_error() { + let cfg = before_rskip293(); + let op_return = bitcoin::ScriptBuf::from_bytes(vec![0x6a, 4, 1, 2, 3, 4]); + for no_address in [support::p2wpkh(&[0x01; 20]), op_return] { + let tx = support::tx_paying(&[(BTC, no_address), (BTC, flyover_address(&active()))]); + let (answer, mut ctx) = pay(&tx, &cfg, false); + assert_eq!(answer, flyover_code::GENERIC_ERROR); + assert!(active_utxos(&mut ctx, &cfg).is_empty()); + } +} + +// --- the signatures of a peg-out --------------------------------------------- + +/// rskj `BridgeUtils.hasEnoughSignatures`, which `addSignature` asks before it +/// releases a peg-out (BridgeSupport.java:1741): each input has a signature +/// in every place its script left one for, the OP_0s the transaction was +/// built with all replaced. One missing on any input, or all of them, and +/// the peg-out is not released. +#[test] +fn a_pegout_missing_a_signature_on_any_input_has_not_enough_signatures() { + let redeem = support::standard_redeem(&support::keys(1, 3)); + // `ScriptBuilder.createMultiSigInputScriptBytes`: OP_0, each signature, + // an empty one as OP_0, then the redeem script, which takes OP_PUSHDATA1. + let input_script = |signatures: [&[u8]; 2]| { + let mut script = vec![0x00]; + for signature in signatures { + script.push(signature.len() as u8); + script.extend_from_slice(signature); + } + script.extend_from_slice(&[0x4c, redeem.len() as u8]); + script.extend_from_slice(&redeem); + bitcoin::ScriptBuf::from_bytes(script) + }; + let pegout = |inputs: usize, signatures: [&[u8]; 2]| { + let mut tx = support::tx_paying(&[(BTC, support::p2pkh(&[0x01; 20]))]); + tx.input = vec![tx.input[0].clone(); inputs]; + for input in &mut tx.input { + input.script_sig = input_script(signatures); + } + crate::bridge::release_tx::has_enough_signatures(&tx) + }; + let (one, other, missing): (&[u8], &[u8], &[u8]) = (&[0x79], &[0x78], &[]); + assert!(pegout(1, [one, other])); + assert!(!pegout(1, [one, missing])); + assert!(!pegout(1, [missing, missing])); + assert!(pegout(3, [one, other])); + assert!(!pegout(3, [one, missing])); +} + +// --- a Coin from the ABI ------------------------------------------------------ + +/// rskj `BridgeUtils.getCoinFromBigInteger`, with which +/// `Bridge.increaseLockingCap` reads its argument (Bridge.java:1306): the +/// number's `longValueExact`, which throws for one past a Java long, and the +/// call fails. A number that fits is read, and the increase taken. +#[test] +fn a_locking_cap_past_a_java_long_fails_the_increase() { + use crate::abi::encode::{self, Arg}; + let config = BridgeConstants::mainnet(); + let authorizer = hex::decode(config.increase_locking_cap_authorizer_keys[0]).expect("hex"); + let tx_ctx = BridgeTxContext { + rsk_sender: crate::bridge::federation::rsk_address_from_public_key(&authorizer) + .expect("a key"), + ..Default::default() + }; + let mut ctx = support::ctx_at(6_000_000); + let mut increase = |cap: U256| { + let args = encode::args(&[Arg::Word(cap.to_be_bytes::<32>())]); + crate::bridge::governance::increase_locking_cap(&mut ctx, &args, 0, &config, &tx_ctx) + .map(|out| out.bytes[31] == 1) + }; + assert!(increase(U256::from(1u64 << 63)).is_err(), "one past a long"); + assert_eq!( + increase(U256::from(config.initial_locking_cap + BTC)), + Ok(true), + "a cap that fits" + ); +} diff --git a/crates/execution/src/rskj_tests/bridge_federation/mod.rs b/crates/execution/src/rskj_tests/bridge_federation/mod.rs new file mode 100644 index 0000000..c439fe9 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_federation/mod.rs @@ -0,0 +1,10 @@ +//! The federation, its scripts, and the Bridge's utilities (rskj's +//! `co.rsk.peg.federation`, `BridgeUtils` and `PegUtilsLegacy`). + +mod support; + +mod bridge_utils_legacy_test; +mod bridge_utils_test; +mod non_standard_erp_federations_test; +mod peg_utils_legacy_test; +mod pending_federation_test; diff --git a/crates/execution/src/rskj_tests/bridge_federation/non_standard_erp_federations_test.rs b/crates/execution/src/rskj_tests/bridge_federation/non_standard_erp_federations_test.rs new file mode 100644 index 0000000..027a7e4 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_federation/non_standard_erp_federations_test.rs @@ -0,0 +1,342 @@ +//! rskj `co.rsk.peg.federation.NonStandardErpFederationsTest`, with +//! `NonStandardErpRedeemScriptBuilderFactoryTest` and `FederationFactoryTest`: +//! the redeem script of a non-standard ERP federation (format 2000), which +//! rskj builds anew with the activations of the block being run. + +use super::support::{self, key}; +use crate::bridge::constants::BridgeConstants; +use crate::bridge::peg; +use crate::hardfork::RskHardforkConfig; + +type Network = (BridgeConstants, RskHardforkConfig); + +fn testnet() -> Network { + (BridgeConstants::testnet(), RskHardforkConfig::testnet()) +} + +fn mainnet() -> Network { + (BridgeConstants::mainnet(), RskHardforkConfig::mainnet()) +} + +/// Testnet's last block before RSKIP284, its first with it, and its first +/// with RSKIP293; mainnet's first block with RSKIP201 (iris300) and its first +/// with RSKIP284 and RSKIP293 (hop400). +const TESTNET_BEFORE_RSKIP284: u64 = 2_581_799; +const TESTNET_RSKIP284: u64 = 2_581_800; +const TESTNET_RSKIP293: u64 = 3_103_000; +const MAINNET_RSKIP201: u64 = 3_614_800; +const MAINNET_RSKIP293: u64 = 4_598_500; + +/// 52,560, the delay of mainnet and testnet, as each builder pushes it: +/// before RSKIP293 two unsigned big-endian bytes (bitcoinj's +/// `unsignedLongToByteArrayBE(delay, 2)`), from it Java's +/// `BigInteger.toByteArray` reversed (`signedLongToByteArrayLE`). +const DELAY_BEFORE_RSKIP293: &[u8] = &[0xcd, 0x50]; +const DELAY_FROM_RSKIP293: &[u8] = &[0x50, 0xcd, 0x00]; + +/// The nine members of the federation rskj's tests build. +fn members() -> Vec<[u8; 33]> { + [ + "03b53899c390573471ba30e5054f78376c5f797fda26dde7a760789f02908cbad2", + "027319afb15481dbeb3c426bcc37f9a30e7f51ceff586936d85548d9395bcc2344", + "0355a2e9bf100c00fc0a214afd1bf272647c7824eb9cb055480962f0c382596a70", + "02566d5ded7c7db1aa7ee4ef6f76989fb42527fcfdcddcd447d6793b7d869e46f7", + "0294c817150f78607566e961b3c71df53a22022a80acbb982f83c0c8baac040adc", + "0372cd46831f3b6afd4c044d160b7667e8ebf659d6cb51a825a3104df6ee0638c6", + "0340df69f28d69eef60845da7d81ff60a9060d4da35c767f017b0dd4e20448fb44", + "02ac1901b6fba2c1dbd47d894d2bd76c8ba1d296d65f6ab47f1c6b22afb53e73eb", + "031aabbeb9b27258f98c2bf21f36677ae7bae09eb2d8c958ef41a20a6e88626d26", + ] + .map(key) + .to_vec() +} + +/// The redeem script rustock gives a non-standard ERP federation of +/// `members` at `block` of `network`. +fn redeem(members: &[[u8; 33]], network: &Network, block: u64) -> Vec { + peg::federation_redeem_for_format_pub(members, 2000, &network.0, &network.1, block) +} + +/// rskj's `getRedeemScript_*` and `createErpFederation_*` tests: from RSKIP201 +/// the script is the members' multisig and the network's emergency multisig, +/// laid out as rskj lays it out, with the delay in two big-endian bytes before +/// RSKIP293 and as a Java number from it. Before RSKIP284 testnet's +/// federation has one script, whatever its members (`FederationFactoryTest`): +/// so the same members have three scripts on testnet, and two on mainnet, +/// where the script before RSKIP284 is the one after it. +#[test] +fn a_nonstandard_erp_redeem_script_is_laid_out_as_rskjs_in_each_era() { + let members = members(); + for (era, network, block, delay) in [ + ( + "mainnet from RSKIP201", + mainnet(), + MAINNET_RSKIP201, + DELAY_BEFORE_RSKIP293, + ), + ( + "mainnet before RSKIP293", + mainnet(), + MAINNET_RSKIP293 - 1, + DELAY_BEFORE_RSKIP293, + ), + ( + "mainnet from RSKIP293", + mainnet(), + MAINNET_RSKIP293, + DELAY_FROM_RSKIP293, + ), + ( + "testnet from RSKIP284", + testnet(), + TESTNET_RSKIP284, + DELAY_BEFORE_RSKIP293, + ), + ( + "testnet from RSKIP293", + testnet(), + TESTNET_RSKIP293, + DELAY_FROM_RSKIP293, + ), + ] { + let emergency = support::erp_keys_of(&network.0); + assert_eq!( + redeem(&members, &network, block), + support::non_standard_erp_redeem(&members, &emergency, delay), + "{era}" + ); + } + + let hardcoded = redeem(&members, &testnet(), TESTNET_BEFORE_RSKIP284); + assert_eq!( + redeem(&support::keys(1, 5), &testnet(), TESTNET_BEFORE_RSKIP284), + hardcoded + ); + let unsigned_delay = redeem(&members, &testnet(), TESTNET_RSKIP284); + let signed_delay = redeem(&members, &testnet(), TESTNET_RSKIP293); + assert!( + hardcoded != unsigned_delay && hardcoded != signed_delay && unsigned_delay != signed_delay + ); + + assert_eq!( + redeem(&members, &mainnet(), MAINNET_RSKIP201), + redeem(&members, &mainnet(), MAINNET_RSKIP293 - 1) + ); + assert_ne!( + redeem(&members, &mainnet(), MAINNET_RSKIP293 - 1), + redeem(&members, &mainnet(), MAINNET_RSKIP293) + ); +} + +/// rskj's `createFederation_postRSKIP293_withValidCsvValues_valid` (130, 500, +/// 33,000 and 65,535, the largest delay rskj takes) and +/// `createFederation_postRSKIP284_preRSKIP293_withValidCsvValueOneByteLong_valid` +/// (20, padded to two bytes on testnet before RSKIP293). +#[test] +fn a_delay_is_pushed_in_two_big_endian_bytes_before_rskip293_and_as_a_java_number_from_it() { + let members = members(); + let emergency = support::erp_keys_of(&BridgeConstants::mainnet()); + for (delay, network, block, pushed) in [ + (20, testnet(), TESTNET_RSKIP284, &[0x00, 0x14][..]), + (130, mainnet(), MAINNET_RSKIP293, &[0x82, 0x00][..]), + (500, mainnet(), MAINNET_RSKIP293, &[0xf4, 0x01][..]), + (33_000, mainnet(), MAINNET_RSKIP293, &[0xe8, 0x80, 0x00][..]), + (65_535, mainnet(), MAINNET_RSKIP293, &[0xff, 0xff, 0x00][..]), + ] { + let network = (support::with_erp(network.0, &emergency, delay), network.1); + assert_eq!( + redeem(&members, &network, block), + support::non_standard_erp_redeem(&members, &emergency, pushed), + "a delay of {delay}" + ); + } +} + +/// rskj's `createdFederationInfo_withRealValues_equalsExistingFederationInfo +/// _testnet`: the last non-standard ERP federation testnet had, of five +/// members with testnet's emergency keys and delay, from RSKIP293. +#[test] +fn the_last_nonstandard_erp_federation_of_testnet_has_its_real_address() { + let members = [ + "0208f40073a9e43b3e9103acec79767a6de9b0409749884e989960fee578012fce", + "0225e892391625854128c5c4ea4340de0c2a70570f33db53426fc9c746597a03f4", + "025a2f522aea776fab5241ad72f7f05918e8606676461cb6ce38265a52d4ca9ed6", + "02afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da", + "0344a3c38cd59afcba3edcebe143e025574594b001700dec41e59409bdbd0f2a09", + ] + .map(key); + let redeem = redeem(&members, &testnet(), TESTNET_RSKIP293); + assert_eq!( + hex::encode(support::p2sh(&support::hash160(&redeem)).as_bytes()), + "a91412d5d2996618c8abcb1e6fc17be3cd8e2790c25f87" + ); + let address = bitcoin::Address::p2sh( + bitcoin::Script::from_bytes(&redeem), + bitcoin::Network::Testnet, + ) + .expect("a redeem script within 520 bytes"); + assert_eq!(address.to_string(), "2MtxpJPt2xCa3AyFYUjTT7Aop9Z6gGf4rqA"); +} + +/// Four of the vectors in rskj's `redeemScripts.json`, written by another +/// implementation: the smallest script, one with a delay in two bytes and one +/// in three (each with both its key lists out of order), and the largest +/// within 520 bytes. (members, emergency keys, delay, script). +const OTHER_IMPLEMENTATION: [(&[&str], &[&str], i64, &str); 4] = [ + ( + &["0366f012edffa9160f0fa4cade9c8d58e1f092d99a9aa0500cfe54a0299fb0ed8c"], + &["022eaf5c60b49ca7902296cf692e6a3533dadf47cca8a5fce95145dedd8492c66b"], + 7065, + "6451210366f012edffa9160f0fa4cade9c8d58e1f092d99a9aa0500cfe54a0299fb0ed8c516702991bb2755121022eaf5c60b49ca7902296cf692e6a3533dadf47cca8a5fce95145dedd8492c66b5168ae", + ), + ( + &[ + "03e54b21e1b8e60d145f63ada38438be62030f1755695ce9b0486dc387784e2065", + "0325ff8a972ab62d46a35ab8faa0cde7d325e9d838abbddfe222143c7b9b869e22", + "03beb566feb61d8220ae4b63c91512386cb94047ad10d70106fe41232d81c28970", + "0225917ca941b51891a2319ab36e1401bfe3a3bdf42cc3acb2d25922aebc99926d", + ], + &[ + "03e3191c756eb874a49a26a574772ab69be0961178fa6721019e81e3bb67ab1b55", + "025e0059d88266f1b6100d5f631e21ea58c166212f5e311e33d8664560af44b396", + "03be26cbfdd5139c5a6354972ad3f55e739832a7baf6083be1d26b0902cd8bc0e2", + ], + 29405, + "6453210225917ca941b51891a2319ab36e1401bfe3a3bdf42cc3acb2d25922aebc99926d210325ff8a972ab62d46a35ab8faa0cde7d325e9d838abbddfe222143c7b9b869e222103beb566feb61d8220ae4b63c91512386cb94047ad10d70106fe41232d81c289702103e54b21e1b8e60d145f63ada38438be62030f1755695ce9b0486dc387784e2065546702dd72b2755221025e0059d88266f1b6100d5f631e21ea58c166212f5e311e33d8664560af44b3962103be26cbfdd5139c5a6354972ad3f55e739832a7baf6083be1d26b0902cd8bc0e22103e3191c756eb874a49a26a574772ab69be0961178fa6721019e81e3bb67ab1b555368ae", + ), + ( + &[ + "02d24ce2e6771690763ea3277da12d73f9177ce0e45981a81e6c953fb9fdd3b1e3", + "03c90c67fb3943b2208ef36ce0389e9b0ec4ac5bdc0bb4a77838cb7442c8edcc9d", + "02272166c3f5f5a8b998dbe78675f8cec7cf77eefc1146d4c3c67796604d25495c", + ], + &[ + "02eb7bb04e139caf711b3eda305a39ba94b85d0460ad391624bc01901ea2a33f29", + "02d741e20d65d229748f7cc4e0b4fe8d33095f9ecb2111876f16291a6914410a9b", + ], + 48635, + "64522102272166c3f5f5a8b998dbe78675f8cec7cf77eefc1146d4c3c67796604d25495c2102d24ce2e6771690763ea3277da12d73f9177ce0e45981a81e6c953fb9fdd3b1e32103c90c67fb3943b2208ef36ce0389e9b0ec4ac5bdc0bb4a77838cb7442c8edcc9d536703fbbd00b275522102d741e20d65d229748f7cc4e0b4fe8d33095f9ecb2111876f16291a6914410a9b2102eb7bb04e139caf711b3eda305a39ba94b85d0460ad391624bc01901ea2a33f295268ae", + ), + ( + &[ + "0261e78783cb9d3097abaccf9ac91eba0a6407075a7fccffe9c95273d863cec2ca", + "022d63d0ee9148e194eb6b44f9bcf4dd6cc15428d4c65d879cd5ff656bca399c22", + "02b65f56a85e04d478b0af7f71cde1a0b7615027ce563e22f0ab318c0f29278d7a", + "02114ba2c7866000c85bfc47cf88ba0b395e7674d424dfa5a4388cf2a807679196", + "03d6f2ec1a41c30bf5b6f751562ad7096fc3eeecd274ef588c1b7dc0c97164472f", + "0363ecb2e65648242f4d4e0d5194c7f22a9ffee217e09a24d8c3e9780eae7b0df0", + "0261347be3cc4e1a72a473215a1d921d272c07e12ac9872915f78c689a39e465e9", + "02d3a2e74cdf240964c48e1a1e3d27a411d7b5f39eb1e565a744f96732a4015a72", + "034907d2a94d603181dfc83106cec71f2e1c1f04b55b66c947379399560748bb8e", + "0268766a465d446db3e093c8e5ae260c344718c26ff2c95c2bc64dccb1e61b0be9", + "033846e3a41c46bbb301e74bc70d9e61b83ca93dd3d83302c6e121b1da6f71483f", + ], + &[ + "03e7c8d6300e410029c0a12d33c40754093f7d827d60c306125c0895c0d3304567", + "020f829a7fd6bc4414024269a966ce4fa8427655fa58f72930f38e97e7d15d5073", + "03d9ad29198e900cc2dcfc7661386daa4ad6fba733f310a2f4e46d512371decb2b", + ], + 48979, + "64562102114ba2c7866000c85bfc47cf88ba0b395e7674d424dfa5a4388cf2a80767919621022d63d0ee9148e194eb6b44f9bcf4dd6cc15428d4c65d879cd5ff656bca399c22210261347be3cc4e1a72a473215a1d921d272c07e12ac9872915f78c689a39e465e9210261e78783cb9d3097abaccf9ac91eba0a6407075a7fccffe9c95273d863cec2ca210268766a465d446db3e093c8e5ae260c344718c26ff2c95c2bc64dccb1e61b0be92102b65f56a85e04d478b0af7f71cde1a0b7615027ce563e22f0ab318c0f29278d7a2102d3a2e74cdf240964c48e1a1e3d27a411d7b5f39eb1e565a744f96732a4015a7221033846e3a41c46bbb301e74bc70d9e61b83ca93dd3d83302c6e121b1da6f71483f21034907d2a94d603181dfc83106cec71f2e1c1f04b55b66c947379399560748bb8e210363ecb2e65648242f4d4e0d5194c7f22a9ffee217e09a24d8c3e9780eae7b0df02103d6f2ec1a41c30bf5b6f751562ad7096fc3eeecd274ef588c1b7dc0c97164472f5b670353bf00b2755221020f829a7fd6bc4414024269a966ce4fa8427655fa58f72930f38e97e7d15d50732103d9ad29198e900cc2dcfc7661386daa4ad6fba733f310a2f4e46d512371decb2b2103e7c8d6300e410029c0a12d33c40754093f7d827d60c306125c0895c0d33045675368ae", + ), +]; + +/// rskj's `getNonStandardErpRedeemScript_compareOtherImplementation`: on +/// testnet from RSKIP293, the members and emergency keys of each vector, with +/// its delay, give its script. +#[test] +fn nonstandard_erp_redeem_scripts_are_those_another_implementation_writes() { + for (members, emergency, delay, script) in OTHER_IMPLEMENTATION { + let members: Vec<[u8; 33]> = members.iter().map(|hex| key(hex)).collect(); + let emergency: Vec<[u8; 33]> = emergency.iter().map(|hex| key(hex)).collect(); + let network = ( + support::with_erp(BridgeConstants::testnet(), &emergency, delay), + RskHardforkConfig::testnet(), + ); + assert_eq!( + hex::encode(redeem(&members, &network, TESTNET_RSKIP293)), + script, + "a delay of {delay}" + ); + } +} + +/// The delay Bitcoin's `OP_CHECKSEQUENCEVERIFY` reads from a non-standard ERP +/// script of `members` members: the push after `OP_ELSE`, read as a script +/// number, little-endian. +fn delay_bitcoin_reads(script: &[u8], members: usize) -> i64 { + let at = 3 + 34 * members; + assert_eq!(script[at], 0x67, "OP_ELSE"); + let length = usize::from(script[at + 1]); + bitcoin::script::read_scriptint(&script[at + 2..at + 2 + length]).expect("a script number") +} + +/// rskj's `spendFromNonStandardErpFed_*_using_erp_multisig_*`: the emergency +/// keys spend an input whose sequence is the network's delay, and +/// `OP_CHECKSEQUENCEVERIFY` passes when the delay the script holds is no more +/// than it. Before RSKIP293 the delay's two bytes are big-endian and Bitcoin +/// reads them little-endian: 52,560 is read as 20,685, so the emergency keys +/// spend early, and 300 as 11,265, so they cannot spend at 300. From RSKIP293 +/// the script holds the delay itself. +#[test] +fn before_rskip293_bitcoin_reads_the_emergency_delay_little_endian_from_big_endian_bytes() { + let members = support::keys(1, 9); + for (era, network, block, delay, read) in [ + ( + "testnet before RSKIP293", + testnet(), + TESTNET_RSKIP284, + 52_560, + 20_685, + ), + ( + "testnet before RSKIP293", + testnet(), + TESTNET_RSKIP284, + 300, + 11_265, + ), + ( + "mainnet before RSKIP293", + mainnet(), + MAINNET_RSKIP293 - 1, + 52_560, + 20_685, + ), + ( + "mainnet before RSKIP293", + mainnet(), + MAINNET_RSKIP293 - 1, + 300, + 11_265, + ), + ( + "testnet from RSKIP293", + testnet(), + TESTNET_RSKIP293, + 52_560, + 52_560, + ), + ( + "mainnet from RSKIP293", + mainnet(), + MAINNET_RSKIP293, + 52_560, + 52_560, + ), + ] { + let emergency = support::erp_keys_of(&network.0); + let network = (support::with_erp(network.0, &emergency, delay), network.1); + let script = redeem(&members, &network, block); + assert_eq!( + delay_bitcoin_reads(&script, members.len()), + read, + "{era}, a delay of {delay}" + ); + assert_eq!( + read <= delay, + delay != 300, + "{era}, a delay of {delay}: the emergency keys spend" + ); + } +} diff --git a/crates/execution/src/rskj_tests/bridge_federation/peg_utils_legacy_test.rs b/crates/execution/src/rskj_tests/bridge_federation/peg_utils_legacy_test.rs new file mode 100644 index 0000000..2cd01a3 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_federation/peg_utils_legacy_test.rs @@ -0,0 +1,725 @@ +//! rskj `co.rsk.peg.PegUtilsLegacyTest` and `PegUtilsLegacyGetTransactionTypeTest`: +//! how the legacy rules take a Bitcoin transaction, as a peg-in +//! (`isValidPegInTx`), a migration (`isMigrationTx`, or from RSKIP199 a spend of +//! the old federation) or a peg-out (`isPegOutTx`), by era. +//! +//! rskj applies them where `registerBtcTransaction` reads no pegout index: +//! before RSKIP379, and below the index's height after it. These tests +//! register each transaction at Bitcoin height 349, below rskj's regtest +//! index (250 and a grace of 100), and read what the Bridge did: whether the +//! call succeeded, whether it marked the transaction processed, the events +//! it logged, and the UTXOs the active federation holds. A transaction no +//! rule takes is left alone, `(true, false, 0, 0)`; a peg-out or a migration +//! is marked processed with no event, its outputs to the federation kept. + +use bitcoin::hashes::Hash as _; +use revm::context_interface::{ContextTr, JournalTr}; + +use super::support::{self, NEW_FEDERATION, OLD_FEDERATION}; +use crate::abi::encode::{self, Arg}; +use crate::bridge::constants::BridgeConstants; +use crate::bridge::{peg, serialization, storage}; +use crate::hardfork::{RskHardforkConfig, RskNetworkUpgrade}; +use crate::precompiles::BridgeTxContext; + +const BLOCK: u64 = 1_000; +const BTC: u64 = 100_000_000; + +/// rskj's regtest from genesis, or with `upgrade` and every one after it moved +/// past the block these tests run at. +fn regtest(before: Option) -> RskHardforkConfig { + let all = RskHardforkConfig::rskj_regtest(); + before.map_or(all.clone(), |upgrade| { + support::upgrades_from_at(all, upgrade, 1_000_000) + }) +} + +/// What the Bridge holds for a federation: its keys, its format and its +/// redeem script. +struct Federation { + keys: Vec<[u8; 33]>, + format: u64, + redeem: Vec, +} + +/// rskj's regtest delay, 500, as a Java number little-endian. +const REGTEST_DELAY: &[u8] = &[0xf4, 0x01]; + +fn standard(first: u8) -> Federation { + let keys = support::keys(first, 3); + let redeem = support::standard_redeem(&keys); + Federation { + keys, + format: 1000, + redeem, + } +} + +fn p2sh_erp(first: u8) -> Federation { + let keys = support::keys(first, 3); + let emergency = support::erp_keys_of(&BridgeConstants::rskj_regtest()); + let redeem = support::p2sh_erp_redeem(&keys, &emergency, REGTEST_DELAY); + Federation { + keys, + format: 3000, + redeem, + } +} + +fn address_of(redeem: &[u8]) -> bitcoin::ScriptBuf { + support::p2sh(&support::hash160(redeem)) +} + +/// `redeem` behind a Flyover derivation hash: ` OP_DROP `. +fn flyover(redeem: &[u8]) -> Vec { + [&[0x20][..], &[0x01; 32], &[0x75], redeem].concat() +} + +/// A non-standard ERP redeem script of `keys`, with the emergency keys of +/// the private keys 0xfa01 and 0xfa02 and a delay of 500, as rskj's tests +/// build it. +fn non_standard_erp(keys: &[[u8; 33]]) -> Vec { + let emergency = [0xfa01u16, 0xfa02].map(|k| support::public_key(&k.to_be_bytes())); + support::non_standard_erp_redeem(keys, &emergency, &[0x01, 0xf4]) +} + +/// `data` pushed as bitcoinj pushes it. +fn push(data: &[u8]) -> Vec { + let mut out = match data.len() { + n @ 0..=75 => vec![n as u8], + n @ 76..=255 => vec![0x4c, n as u8], + n => [&[0x4d][..], &(n as u16).to_le_bytes()].concat(), + }; + out.extend_from_slice(data); + out +} + +/// A multisig spend of `redeem`: OP_0, two signatures' worth of bytes, and +/// the redeem script pushed last. +fn spending(redeem: &[u8]) -> Vec { + [ + &[0x00][..], + &push(&[0x30; 71]), + &push(&[0x31; 71]), + &push(redeem), + ] + .concat() +} + +/// `tx` with its first input a spend of `redeem` signed by the private keys +/// `signers`, as rskj's `FederationTestUtils.addSignatures` signs one: OP_0, +/// their signatures for SIGHASH_ALL in the order their public keys stand in +/// `redeem` (first a signer's whose key isn't in it), an OP_0 more to take an +/// ERP script's default branch, and `redeem`. The signature hash is Bitcoin's +/// legacy one, which bitcoinj's is for a transaction with no witness. +fn signed( + tx: bitcoin::Transaction, + redeem: &[u8], + signers: &[&[u8]], + erp: bool, +) -> bitcoin::Transaction { + let hash = bitcoin::sighash::SighashCache::new(&tx) + .legacy_signature_hash(0, bitcoin::Script::from_bytes(redeem), 1) + .expect("the first input") + .to_byte_array(); + signed_over(tx, hash, redeem, signers, erp) +} + +/// bitcoinj's signature hash of the first input for SIGHASH_ALL: Bitcoin's +/// legacy one, but over the copy serialized with its witnesses where the +/// transaction has any (`BtcTransaction.hashForSignature`). +fn bitcoinj_sighash(tx: &bitcoin::Transaction, redeem: &[u8]) -> [u8; 32] { + let mut copy = tx.clone(); + for input in &mut copy.input { + input.script_sig = bitcoin::ScriptBuf::new(); + } + copy.input[0].script_sig = bitcoin::ScriptBuf::from_bytes(redeem.to_vec()); + let mut preimage = bitcoin::consensus::serialize(©); + preimage.extend_from_slice(&1u32.to_le_bytes()); + bitcoin::hashes::sha256d::Hash::hash(&preimage).to_byte_array() +} + +/// [`signed`], over the signature hash `hash`. +fn signed_over( + mut tx: bitcoin::Transaction, + hash: [u8; 32], + redeem: &[u8], + signers: &[&[u8]], + erp: bool, +) -> bitcoin::Transaction { + use k256::ecdsa::signature::hazmat::PrehashSigner as _; + let mut signers = signers.to_vec(); + signers.sort_by_key(|private| { + let key = support::public_key(private); + redeem.windows(33).position(|window| window == key) + }); + let mut script_sig = vec![0x00]; + for private in signers { + let mut scalar = [0u8; 32]; + scalar[32 - private.len()..].copy_from_slice(private); + let key = k256::ecdsa::SigningKey::from_slice(&scalar).expect("a private key in range"); + let signature: k256::ecdsa::Signature = key.sign_prehash(&hash).expect("a signature"); + script_sig.extend(push(&[signature.to_der().as_bytes(), &[0x01]].concat())); + } + if erp { + script_sig.push(0x00); + } + script_sig.extend(push(redeem)); + tx.input[0].script_sig = bitcoin::ScriptBuf::from_bytes(script_sig); + tx +} + +/// A transaction whose inputs have the scriptSigs `inputs`, paying `outputs`. +fn tx(inputs: &[Vec], outputs: &[(u64, bitcoin::ScriptBuf)]) -> bitcoin::Transaction { + let mut tx = support::tx_paying(outputs); + tx.input = inputs + .iter() + .enumerate() + .map(|(i, script_sig)| bitcoin::TxIn { + previous_output: bitcoin::OutPoint { + txid: bitcoin::Txid::from_byte_array([0x5b; 32]), + vout: i as u32, + }, + script_sig: bitcoin::ScriptBuf::from_bytes(script_sig.clone()), + sequence: bitcoin::Sequence::MAX, + witness: bitcoin::Witness::new(), + }) + .collect(); + tx +} + +/// A P2PKH spend's scriptSig: a signature's bytes and a public key. +fn p2pkh_spend() -> Vec { + [push(&[0x30; 71]), push(&support::public_key(&[0x77]))].concat() +} + +/// What `registerBtcTransaction` does with `tx` at height 349, at block +/// 1,000 under `cfg`, with `active` the active federation, `retiring` the +/// retiring one, and `retired` the last retired federation's P2SH hash. +fn registered( + cfg: &RskHardforkConfig, + active: &Federation, + retiring: Option<&Federation>, + retired: Option<[u8; 20]>, + tx: &bitcoin::Transaction, +) -> (bool, bool, usize, usize) { + let raw = bitcoin::consensus::serialize(tx); + let mut ctx = support::ctx_at(BLOCK); + support::btc_chain_holding(&mut ctx, &raw, cfg); + support::fund_bridge(&mut ctx); + support::store_federation(&mut ctx, NEW_FEDERATION, &active.keys, 1, active.format); + if let Some(retiring) = retiring { + support::store_federation(&mut ctx, OLD_FEDERATION, &retiring.keys, 0, retiring.format); + } + if let Some(retired) = retired { + let script = support::p2sh(&retired).into_bytes(); + storage::bridge_store_bytes_named( + &mut ctx, + storage::LAST_RETIRED_FEDERATION_P2SH_SCRIPT_KEY, + &serialization::rlp_encode_list(&[serialization::rlp_encode_element(&script)]), + ); + } + let tree = support::one_leaf_tree(&raw); + let args = encode::args(&[ + Arg::Bytes(&raw), + Arg::Word(encode::int(349)), + Arg::Bytes(&tree), + ]); + let config = BridgeConstants::rskj_regtest(); + let ok = peg::register_btc_transaction( + &mut ctx, + &args, + 0, + &config, + cfg, + &BridgeTxContext::default(), + ) + .is_ok(); + let logs = ctx.journal_mut().take_logs().len(); + let txid = crate::bridge::tx::legacy_btc_txid(tx); + let processed = crate::bridge::tx::is_btc_tx_processed(&mut ctx, &txid); + let utxos = storage::load_federation_utxos(&mut ctx, cfg, BLOCK).len(); + (ok, processed, logs, utxos) +} + +const LEFT_ALONE: (bool, bool, usize, usize) = (true, false, 0, 0); + +/// A peg-out or a migration: marked processed with no event, keeping +/// `utxos` outputs to the active federation. +fn moved(utxos: usize) -> (bool, bool, usize, usize) { + (true, true, 0, utxos) +} + +/// Whether the transaction was taken and marked processed, as a peg-in is, +/// accepted or refunded. +fn taken(outcome: (bool, bool, usize, usize)) -> bool { + outcome.0 && outcome.1 +} + +/// rskj's `isValidPegInTx_*_iris` tests: from RSKIP219 a peg-in must send the +/// federation 500,000 satoshis, before it 1,000,000 (rskj's regtest); one +/// that sends less is no peg-in, and is left alone. These come from a P2PKH +/// sender, whose peg-in is taken. +#[test] +fn a_peg_in_must_send_the_minimum_of_its_era() { + let active = standard(1); + let paying = |value| tx(&[p2pkh_spend()], &[(value, address_of(&active.redeem))]); + let after = regtest(None); + let before = regtest(Some(RskNetworkUpgrade::Iris300)); + assert_eq!( + registered(&after, &active, None, None, &paying(250_000)), + LEFT_ALONE + ); + for value in [750_000, 50 * BTC] { + assert!( + taken(registered(&after, &active, None, None, &paying(value))), + "{value} from RSKIP219" + ); + } + assert_eq!( + registered(&before, &active, None, None, &paying(750_000)), + LEFT_ALONE + ); + assert!(taken(registered( + &before, + &active, + None, + None, + &paying(1_000_000) + ))); +} + +/// rskj's `isValidPegInTx_spending_from_federation_is_pegout_after_iris`: a +/// spend of the active federation that pays it back is no peg-in but a +/// peg-out. +#[test] +fn a_spend_of_the_federation_is_a_pegout() { + let active = standard(1); + let tx = tx( + &[spending(&active.redeem)], + &[(BTC, address_of(&active.redeem))], + ); + assert_eq!( + registered(®test(None), &active, None, None, &tx), + moved(1) + ); +} + +/// Before RSKIP201 `isValidPegInTx` refuses a spend of the federation only as +/// one of its script, its signatures checked: its members' spend that pays it +/// back is no peg-in but a peg-out, and the same redeem script under +/// signatures that don't verify is a peg-in. +#[test] +fn before_rskip201_a_spend_of_the_federation_is_a_pegout_where_its_members_sign() { + let active = standard(1); + let paying_it_back = || { + tx( + &[spending(&active.redeem)], + &[(BTC, address_of(&active.redeem))], + ) + }; + let cfg = regtest(Some(RskNetworkUpgrade::Iris300)); + let by_members = signed(paying_it_back(), &active.redeem, &[&[1], &[3]], false); + assert_eq!(registered(&cfg, &active, None, None, &by_members), moved(1)); + let unsigned = registered(&cfg, &active, None, None, &paying_it_back()); + assert!(taken(unsigned) && unsigned.2 > 0); +} + +/// From RSKIP201 `isValidPegInTx` compares an input's standard redeem script +/// with the federation's byte for byte: a multisig of the federation's keys +/// in another order is another script, its spend no pegout, and the +/// transaction, paying the federation, a peg-in. +#[test] +fn from_rskip201_a_multisig_of_the_federations_keys_in_another_order_is_not_its() { + let active = standard(1); + let mut keys = active.keys.clone(); + keys.sort(); + keys.reverse(); + let mut reversed = vec![0x52]; + for key in &keys { + reversed.extend(push(key)); + } + reversed.extend([0x53, 0xae]); + let tx = tx(&[spending(&reversed)], &[(BTC, address_of(&active.redeem))]); + let outcome = registered(®test(None), &active, None, None, &tx); + assert!(taken(outcome) && outcome.2 > 0); +} + +/// `isValidPegInTx` reads each input's redeem script, whatever the upgrade, +/// and bitcoinj parses a scriptSig to find it there: one it can't parse +/// throws, nothing catches it, and the call fails, though the transaction +/// pays the federation nothing. +#[test] +fn a_script_sig_bitcoinj_cannot_parse_fails_the_call() { + let active = standard(1); + let tx = tx(&[vec![0x4c]], &[(BTC, support::p2pkh(&[0x99; 20]))]); + for cfg in [regtest(None), regtest(Some(RskNetworkUpgrade::Iris300))] { + assert_eq!( + registered(&cfg, &active, None, None, &tx), + (false, false, 0, 0) + ); + } +} + +/// A federation's redeem script behind a Flyover prefix, within a +/// non-standard ERP script, and both. +fn wrapped(federation: &Federation) -> [(&'static str, Vec); 3] { + [ + ("flyover", flyover(&federation.redeem)), + ("non-standard erp", non_standard_erp(&federation.keys)), + ( + "flyover non-standard erp", + flyover(&non_standard_erp(&federation.keys)), + ), + ] +} + +/// rskj's `isValidPegInTx_hasChangeUtxoFrom*_afterRskip201_notPegin`: from +/// RSKIP201 a spend of the active federation's redeem script wrapped is no +/// peg-in but a peg-out, and one of the last retired federation's, paying +/// the active one, is a migration. +#[test] +fn from_rskip201_the_change_of_a_flyover_or_erp_federation_is_no_peg_in() { + let active = standard(1); + let retired = standard(11); + let to_active = [(BTC, address_of(&active.redeem))]; + for (of, federation, retired_hash) in [ + ("active", &active, None), + ("retired", &retired, Some(support::hash160(&retired.redeem))), + ] { + for (shape, redeem) in wrapped(federation) { + let tx = tx(&[spending(&redeem)], &to_active); + assert_eq!( + registered(®test(None), &active, None, retired_hash, &tx), + moved(1), + "{shape} of the {of} federation" + ); + } + } +} + +/// rskj's `isValidPegInTx_hasChangeUtxoFrom{Flyover,FlyoverErp,Erp}Federation +/// _beforeRskip201_isPegin`, with the change input as rskj writes it, the +/// redeem script itself its scriptSig: before RSKIP201 only an input that +/// spends a federation's P2SH script makes a transaction no peg-in +/// (PegUtilsLegacy.java:186-194), and this one spends none, so it is taken +/// for a peg-in, not a peg-out. Its sender is read next, and the reader of a +/// multisig sender takes the scriptSig's last chunk, here an opcode, for the +/// redeem script, whose null bytes bitcoinj cannot parse +/// (P2shMultisigBtcLockSender.java:52-54): the call fails, where a peg-out +/// would have been marked processed. +#[test] +fn before_rskip201_the_change_of_a_flyover_or_erp_federation_is_a_peg_in() { + let active = standard(1); + for (shape, redeem) in wrapped(&active) { + let tx = tx(&[redeem], &[(BTC, address_of(&active.redeem))]); + assert_eq!( + registered( + ®test(Some(RskNetworkUpgrade::Iris300)), + &active, + None, + None, + &tx + ), + (false, false, 0, 0), + "{shape}" + ); + } +} + +/// rskj's `isPegOutTx_from{Flyover,Erp,FlyoverErp}Federation`, from RSKIP201: +/// a spend of the active federation's redeem script wrapped, paying +/// elsewhere, is a peg-out (PegUtilsLegacy.java isPegOutTx, the input's +/// standard redeem script against the federation's). +#[test] +fn from_rskip201_a_spend_of_a_wrapped_federation_redeem_script_is_a_pegout() { + let active = standard(1); + for (shape, redeem) in wrapped(&active) { + let tx = tx(&[spending(&redeem)], &[(BTC, support::p2pkh(&[0x99; 20]))]); + assert_eq!( + registered(®test(None), &active, None, None, &tx), + moved(0), + "{shape}" + ); + } +} + +/// The same tests before RSKIP201: rskj's `isPegOutTx` compares the P2SH +/// script of the input's redeem script as it is, wrapped, to the +/// federation's, and `isValidPegInTx` finds no spend of the federation's P2SH +/// script either, while the transaction sends the federation nothing. It is +/// neither, and left alone. +#[test] +fn before_rskip201_a_spend_of_a_wrapped_federation_redeem_script_is_no_pegout() { + let active = standard(1); + for (shape, redeem) in wrapped(&active) { + let tx = tx(&[spending(&redeem)], &[(BTC, support::p2pkh(&[0x99; 20]))]); + assert_eq!( + registered( + ®test(Some(RskNetworkUpgrade::Iris300)), + &active, + None, + None, + &tx + ), + LEFT_ALONE, + "{shape}" + ); + } +} + +/// rskj's `isValidPegInTx_has_*_RSKIP293` tests: before RSKIP293 what the +/// federation is sent counts in total; from it each UTXO must be worth the +/// minimum, 500,000 satoshis. +#[test] +fn from_rskip293_each_utxo_of_a_peg_in_must_be_worth_the_minimum() { + let active = standard(1); + let to = |values: &[u64]| { + let outputs: Vec<_> = values + .iter() + .map(|&v| (v, address_of(&active.redeem))) + .collect(); + tx(&[p2pkh_spend()], &outputs) + }; + let min = 500_000; + let before = regtest(Some(RskNetworkUpgrade::Hop400)); + let after = regtest(None); + assert!(taken(registered( + &before, + &active, + None, + None, + &to(&[min / 4; 4]) + ))); + assert_eq!( + registered( + &before, + &active, + None, + None, + &to(&[min / 4, min / 4, min / 4, min / 5]) + ), + LEFT_ALONE + ); + assert_eq!( + registered( + &after, + &active, + None, + None, + &to(&[min / 2, min / 2, min / 2, min, min + BTC]) + ), + LEFT_ALONE + ); + assert!(taken(registered(&after, &active, None, None, &to(&[min])))); +} + +/// rskj's `isValidPegInTx_*P2shErpScript_sends_funds_to_*_after_RSKIP353`: a +/// spend of a P2SH ERP script of the active federation's members, or of its +/// Flyover, is no peg-in, whether it pays the federation or elsewhere: it is +/// a peg-out. +#[test] +fn a_spend_of_a_p2sh_erp_script_of_the_federations_members_is_a_pegout() { + let active = standard(1); + let p2sh_erp = support::p2sh_erp_redeem(&active.keys, &support::keys(31, 3), &[0x00, 0x01]); + for redeem in [p2sh_erp.clone(), flyover(&p2sh_erp)] { + for (to, utxos) in [ + (address_of(&active.redeem), 1), + (support::p2pkh(&[0x99; 20]), 0), + ] { + let tx = tx(&[spending(&redeem)], &[(BTC, to)]); + assert_eq!( + registered(®test(None), &active, None, None, &tx), + moved(utxos) + ); + } + } +} + +/// rskj's `testTxIsProcessableInLegacyVersion`: before RSKIP143 a legacy +/// peg-in is registered only from a P2PKH sender, from it also from a P2SH +/// multisig one (BridgeUtils.txIsProcessableInLegacyVersion); before it the +/// peg-in is left alone (BridgeSupport.java:659-666), from it, its sender +/// being one rskj does not lock for, refunded. +#[test] +fn before_rskip143_a_peg_in_is_taken_only_from_a_p2pkh_sender() { + let active = standard(1); + let to_federation = [(BTC, address_of(&active.redeem))]; + let multisig = tx( + &[spending(&support::standard_redeem(&support::keys(21, 3)))], + &to_federation, + ); + let before = regtest(Some(RskNetworkUpgrade::Papyrus200)); + let after = regtest(Some(RskNetworkUpgrade::Iris300)); + assert_eq!( + registered(&before, &active, None, None, &multisig), + LEFT_ALONE, + "before RSKIP143" + ); + assert!( + taken(registered(&after, &active, None, None, &multisig)), + "from RSKIP143" + ); +} + +/// rskj's `isMigrationTx_sending_funds_from_*`: a spend of the retiring or the +/// last retired federation, P2SH ERP or standard, that pays the active one, +/// P2SH ERP or standard, is a migration. The last retired P2SH ERP +/// federation is known by the P2SH script of its members' multisig; by its +/// whole ERP script it is not, and its members' spend, which `isValidPegInTx` +/// refuses as a peg-in for spending that script, is left alone. +#[test] +fn a_spend_of_a_retiring_or_retired_federation_to_the_active_one_is_a_migration() { + let cfg = regtest(None); + for (from, to) in [ + (p2sh_erp(11), p2sh_erp(1)), + (standard(11), p2sh_erp(1)), + (standard(11), standard(1)), + ] { + let tx = tx(&[spending(&from.redeem)], &[(BTC, address_of(&to.redeem))]); + assert_eq!( + registered(&cfg, &to, Some(&from), None, &tx), + moved(1), + "retiring" + ); + let members = support::hash160(&support::standard_redeem(&from.keys)); + assert_eq!( + registered(&cfg, &to, None, Some(members), &tx), + moved(1), + "retired" + ); + } + let (from, to) = (p2sh_erp(11), p2sh_erp(1)); + let tx = signed( + tx(&[spending(&from.redeem)], &[(BTC, address_of(&to.redeem))]), + &from.redeem, + &[&[11], &[12]], + true, + ); + assert_eq!( + registered(&cfg, &to, None, Some(support::hash160(&from.redeem)), &tx), + LEFT_ALONE, + "retired, known by its whole script" + ); +} + +/// rskj's regtest old federation's private keys +/// (`PegUtilsLegacyGetTransactionTypeTest`'s +/// `REGTEST_OLD_FEDERATION_PRIVATE_KEYS`). +const REGTEST_OLD_FEDERATION_PRIVATE_KEYS: [&str; 3] = [ + "47129ffed2c0273c75d21bb8ba020073bb9a1638df0e04853407461fdd9e8b83", + "9f72d27ba603cfab5a0201974a6783ca2476ec3d6b4e2625282c682e0e5f1c35", + "e1b17fcd0ef1942465eee61b20561b16750191143d365e71de08b33dd84a9788", +]; + +/// The old federation's private keys and its redeem script, two of the three. +fn old_federation() -> (Vec>, Vec) { + let privates: Vec> = REGTEST_OLD_FEDERATION_PRIVATE_KEYS + .iter() + .map(|private| hex::decode(private).expect("a private key in hex")) + .collect(); + let keys: Vec<[u8; 33]> = privates + .iter() + .map(|private| support::public_key(private)) + .collect(); + (privates, support::standard_redeem(&keys)) +} + +/// rskj's `PegUtilsLegacyGetTransactionTypeTest.test_sentFromOldFed`: a +/// transaction two of the old federation's three members sign, paying the +/// active federation, is a peg-in before RSKIP199 and from it a migration, +/// whose output the active federation keeps. From RSKIP186 the old federation +/// is the last retired one too. The old federation is rskj regtest's +/// `oldFederationAddress`. +#[test] +fn from_rskip199_a_spend_of_the_old_federation_is_a_migration() { + let (privates, old) = old_federation(); + assert_eq!( + support::hash160(&old), + BridgeConstants::rskj_regtest().old_federation_address_hash160 + ); + let active = standard(1); + let tx = signed( + tx(&[spending(&old)], &[(BTC, address_of(&active.redeem))]), + &old, + &[&privates[0], &privates[1]], + false, + ); + let before = registered( + ®test(Some(RskNetworkUpgrade::Iris300)), + &active, + None, + None, + &tx, + ); + assert!(taken(before) && before.2 > 0, "a peg-in before RSKIP199"); + assert_eq!( + registered( + ®test(None), + &active, + None, + Some(support::hash160(&old)), + &tx + ), + moved(1) + ); +} + +/// From RSKIP199 an input is the old federation's where it spends the old +/// federation's script as bitcoinj runs it, its signatures checked +/// (`PegUtilsLegacy.scriptCorrectlySpendsTx`). With no retired federation to +/// know it by otherwise, the old federation's redeem script under signatures +/// that don't all verify, one by a key not among its members' or none at all, +/// makes no migration: the transaction, paying the active federation, is a +/// peg-in. +#[test] +fn a_spend_of_the_old_federation_counts_only_where_its_signatures_verify() { + let (privates, old) = old_federation(); + let active = standard(1); + let paying = || tx(&[spending(&old)], &[(BTC, address_of(&active.redeem))]); + let cfg = regtest(None); + let members = signed(paying(), &old, &[&privates[0], &privates[2]], false); + assert_eq!(registered(&cfg, &active, None, None, &members), moved(1)); + let outsider = signed(paying(), &old, &[&privates[0], &[0x77]], false); + for (signed_by, tx) in [("an outsider", outsider), ("no one", paying())] { + let outcome = registered(&cfg, &active, None, None, &tx); + assert!(taken(outcome) && outcome.2 > 0, "signed by {signed_by}"); + } +} + +/// bitcoinj reads a spend from the scriptSig, and hashes a transaction for a +/// legacy signature with its witnesses where it has any, as Bitcoin doesn't. +/// In a transaction with a witness on another input, the old federation's +/// spend signed as Bitcoin signs is no spend of it to rskj, and the +/// transaction, paying the active federation, is a peg-in; signed over +/// bitcoinj's hash, it is a migration. An input that carries the old +/// federation's redeem script in its witness, its scriptSig empty, spends +/// nothing bitcoinj runs either. +#[test] +fn a_spend_of_the_old_federation_is_read_from_its_script_sig_as_bitcoinj_hashes_it() { + let (privates, old) = old_federation(); + let active = standard(1); + let cfg = regtest(None); + let to_active = [(BTC, address_of(&active.redeem))]; + let mut paying = tx(&[spending(&old), p2pkh_spend()], &to_active); + paying.input[1].witness = + bitcoin::Witness::from_slice(&[vec![0x30; 71], support::public_key(&[0x77]).to_vec()]); + let signers: [&[u8]; 2] = [&privates[0], &privates[1]]; + let as_bitcoin_signs = signed(paying.clone(), &old, &signers, false); + let outcome = registered(&cfg, &active, None, None, &as_bitcoin_signs); + assert!(taken(outcome) && outcome.2 > 0, "signed as Bitcoin signs"); + let hash = bitcoinj_sighash(&paying, &old); + let over_bitcoinjs_hash = signed_over(paying, hash, &old, &signers, false); + assert_eq!( + registered(&cfg, &active, None, None, &over_bitcoinjs_hash), + moved(1) + ); + let mut in_a_witness = tx(&[p2pkh_spend(), vec![]], &to_active); + in_a_witness.input[1].witness = + bitcoin::Witness::from_slice(&[vec![], vec![0x30; 71], vec![0x31; 71], old.clone()]); + let outcome = registered(&cfg, &active, None, None, &in_a_witness); + assert!( + taken(outcome) && outcome.2 > 0, + "the redeem script in a witness" + ); +} diff --git a/crates/execution/src/rskj_tests/bridge_federation/pending_federation_test.rs b/crates/execution/src/rskj_tests/bridge_federation/pending_federation_test.rs new file mode 100644 index 0000000..87d380d --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_federation/pending_federation_test.rs @@ -0,0 +1,244 @@ +//! rskj `co.rsk.peg.federation.PendingFederationTest`: the federation a +//! commit builds from the pending one, in the format of the commit's era, +//! and the commits that throw because Bitcoin could not spend from what +//! they would build. +//! +//! rskj builds the federation as the commit's deciding vote goes through +//! (`PendingFederation.buildFederation`), so these tests vote as the +//! network's federation-change authorizers do: create, add each member, +//! commit. What the commit built is read from the address its +//! `commit_federation` event logs. + +use alloy_primitives::{B256, U256}; +use revm::context_interface::{ContextTr, JournalTr}; +use sha3::{Digest as _, Keccak256}; + +use super::support; +use crate::abi::encode::{self, Arg}; +use crate::bridge::constants::BridgeConstants; +use crate::bridge::governance; +use crate::hardfork::RskHardforkConfig; + +type Network = (BridgeConstants, RskHardforkConfig, bitcoin::Network); + +fn mainnet() -> Network { + ( + BridgeConstants::mainnet(), + RskHardforkConfig::mainnet(), + bitcoin::Network::Bitcoin, + ) +} + +fn testnet() -> Network { + ( + BridgeConstants::testnet(), + RskHardforkConfig::testnet(), + bitcoin::Network::Testnet, + ) +} + +/// A block of each era a commit builds in. Mainnet: before RSKIP201 +/// (iris300); from it, before RSKIP293 (hop400); from that, before RSKIP353; +/// from RSKIP353, before RSKIP305 (reed800); from RSKIP305. Testnet: before +/// RSKIP201; from RSKIP293, before RSKIP353; from RSKIP353; from RSKIP305. +const MAINNET_STANDARD: u64 = 3_000_000; +const MAINNET_NON_STANDARD_BEFORE_RSKIP293: u64 = 4_000_000; +const MAINNET_NON_STANDARD: u64 = 4_700_000; +const MAINNET_P2SH_ERP: u64 = 5_000_000; +const MAINNET_P2SH_P2WSH_ERP: u64 = 8_100_000; +const TESTNET_STANDARD: u64 = 2_000_000; +const TESTNET_NON_STANDARD: u64 = 3_200_000; +const TESTNET_P2SH_ERP: u64 = 4_000_000; +const TESTNET_P2SH_P2WSH_ERP: u64 = 6_900_000; + +/// Mainnet's and testnet's delay, 52,560, pushed before RSKIP293 as two +/// unsigned big-endian bytes, and as a Java number from it and in every P2SH +/// ERP script. +const DELAY_BEFORE_RSKIP293: &[u8] = &[0xcd, 0x50]; +const DELAY: &[u8] = &[0x50, 0xcd, 0x00]; + +/// `count` members, with the keys of the private keys 100, 200, 300, ..., as +/// rskj's tests make them. +fn members(count: u32) -> Vec<[u8; 33]> { + (1..=count) + .map(|i| support::public_key(&(i * 100).to_be_bytes())) + .collect() +} + +/// The string at head word `index` of an ABI-encoded event. +fn abi_string(data: &[u8], index: usize) -> String { + let word = |at: usize| U256::from_be_slice(&data[at..at + 32]).to::(); + let at = word(32 * index); + let length = word(at); + String::from_utf8(data[at + 32..at + 32 + length].to_vec()).expect("an address") +} + +/// What committing a pending federation of `members` at `block` answers +/// (`None` where the deciding vote throws), and the address its +/// `commit_federation` event gives the federation built. +fn commit(network: &Network, block: u64, members: &[[u8; 33]]) -> (Option, Option) { + let mut ctx = support::ctx_at(block); + let answer = support::elect_federation(&mut ctx, (&network.0, &network.1), members); + let topic = B256::from_slice(&Keccak256::digest( + b"commit_federation(bytes,string,bytes,string,int256)", + )); + let address = ctx + .journal_mut() + .take_logs() + .iter() + .find(|log| log.data.topics().first() == Some(&topic)) + .map(|log| abi_string(&log.data.data, 3)); + (answer, address) +} + +/// A federation's address, the P2SH of its redeem script. +fn p2sh(redeem: &[u8], network: bitcoin::Network) -> String { + bitcoin::Address::p2sh(bitcoin::Script::from_bytes(redeem), network) + .expect("a redeem script within 520 bytes") + .to_string() +} + +/// A P2SH-P2WSH federation's address, the P2SH of the witness program of its +/// redeem script. +fn p2sh_p2wsh(redeem: &[u8], network: bitcoin::Network) -> String { + bitcoin::Address::p2shwsh(bitcoin::Script::from_bytes(redeem), network).to_string() +} + +/// The federations a commit builds, by era. +#[derive(Clone, Copy, Debug)] +enum Built { + Standard, + /// With the delay pushed as these bytes. + NonStandardErp(&'static [u8]), + P2shErp, + P2shP2wshErp, +} + +/// The address rskj gives the federation of `members` built as `built`. +fn address_of( + built: Built, + members: &[[u8; 33]], + emergency: &[[u8; 33]], + net: bitcoin::Network, +) -> String { + match built { + Built::Standard => p2sh(&support::standard_redeem(members), net), + Built::NonStandardErp(delay) => p2sh( + &support::non_standard_erp_redeem(members, emergency, delay), + net, + ), + Built::P2shErp => p2sh(&support::p2sh_erp_redeem(members, emergency, DELAY), net), + Built::P2shP2wshErp => { + p2sh_p2wsh(&support::p2sh_erp_redeem(members, emergency, DELAY), net) + } + } +} + +/// rskj's `buildFederation_with9Members_*`, `with15Members_*` and +/// `with20Members_*`: a standard multisig before RSKIP201; a non-standard +/// ERP federation from it, before RSKIP353; a P2SH ERP federation from +/// RSKIP353, before RSKIP305; a P2SH-P2WSH ERP federation from RSKIP305. The +/// ERP federations have the network's emergency keys and delay. +#[test] +fn a_commit_builds_the_federation_of_its_era() { + let mainnet_eras = [ + (Built::Standard, MAINNET_STANDARD, 9), + (Built::Standard, MAINNET_STANDARD, 15), + ( + Built::NonStandardErp(DELAY_BEFORE_RSKIP293), + MAINNET_NON_STANDARD_BEFORE_RSKIP293, + 9, + ), + (Built::NonStandardErp(DELAY), MAINNET_NON_STANDARD, 9), + (Built::P2shErp, MAINNET_P2SH_ERP, 9), + (Built::P2shP2wshErp, MAINNET_P2SH_P2WSH_ERP, 9), + (Built::P2shP2wshErp, MAINNET_P2SH_P2WSH_ERP, 20), + ]; + let testnet_eras = [ + (Built::Standard, TESTNET_STANDARD, 9), + (Built::Standard, TESTNET_STANDARD, 15), + (Built::NonStandardErp(DELAY), TESTNET_NON_STANDARD, 9), + (Built::P2shErp, TESTNET_P2SH_ERP, 9), + (Built::P2shP2wshErp, TESTNET_P2SH_P2WSH_ERP, 9), + (Built::P2shP2wshErp, TESTNET_P2SH_P2WSH_ERP, 20), + ]; + for (network, eras) in [ + (mainnet(), &mainnet_eras[..]), + (testnet(), &testnet_eras[..]), + ] { + let emergency = support::erp_keys_of(&network.0); + let net = network.2; + for &(built, block, count) in eras { + let members = members(count); + assert_eq!( + commit(&network, block, &members), + (Some(1), Some(address_of(built, &members, &emergency, net))), + "{net}: {built:?} of {count} at #{block}" + ); + } + } +} + +/// rskj's `buildFederation_with10Members_inMainnet_*`, `with11Members_in +/// Testnet_*`, `with21Members_*` and `with102Members_*`: an ERP federation +/// whose redeem script goes in a scriptSig is built only if the script is +/// within 486 bytes, a scriptSig element less a Flyover prefix +/// (`ScriptValidations.java:11`), which takes nine members with mainnet's four +/// emergency keys and ten with testnet's three; bitcoinj makes no multisig of +/// more than 20 keys (`ScriptBuilder.java:272`). Where the build throws, the +/// commit's deciding vote throws, and nothing is committed. +#[test] +fn a_commit_throws_where_bitcoin_could_not_spend_from_the_federation_it_builds() { + for (network, block, largest, too_many) in [ + ( + mainnet(), + MAINNET_NON_STANDARD_BEFORE_RSKIP293, + 9, + &[10][..], + ), + (mainnet(), MAINNET_NON_STANDARD, 9, &[10][..]), + (testnet(), TESTNET_NON_STANDARD, 10, &[11][..]), + (mainnet(), MAINNET_P2SH_ERP, 9, &[10][..]), + (testnet(), TESTNET_P2SH_ERP, 10, &[11][..]), + (mainnet(), MAINNET_P2SH_P2WSH_ERP, 20, &[21, 102][..]), + (testnet(), TESTNET_P2SH_P2WSH_ERP, 20, &[21][..]), + ] { + let net = network.2; + assert_eq!( + commit(&network, block, &members(largest)).0, + Some(1), + "{net}: {largest} members at #{block}" + ); + for &count in too_many { + assert_eq!( + commit(&network, block, &members(count)), + (None, None), + "{net}: {count} members at #{block}" + ); + } + } +} + +/// rskj's `isComplete_*` and `buildFederation_withLessMembersThanRequired_*`: +/// a pending federation of one member is incomplete, and a vote to commit it +/// answers INSUFFICIENT_MEMBERS (-2) before its hash is looked at +/// (FederationSupportImpl.java:666-669), so none is built. +#[test] +fn a_pending_federation_of_one_member_is_not_committed() { + let network = mainnet(); + let (config, cfg) = (&network.0, &network.1); + let mut ctx = support::ctx_at(MAINNET_P2SH_ERP); + support::pend_federation(&mut ctx, (config, cfg), &members(1)); + let authorizer = support::deciding_authorizers(config)[0]; + let args = encode::args(&[Arg::Bytes(&[0; 32])]); + let out = governance::commit_federation( + &mut ctx, + &args, + 0, + config, + cfg, + &support::sent_by(authorizer), + ) + .expect("commitFederation answers"); + assert_eq!(support::int_answer(&out.bytes), -2); +} diff --git a/crates/execution/src/rskj_tests/bridge_federation/support.rs b/crates/execution/src/rskj_tests/bridge_federation/support.rs new file mode 100644 index 0000000..3b9d416 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_federation/support.rs @@ -0,0 +1,570 @@ +//! What this folder's ports share: a Bridge to call, keys and federations to +//! put in it, a Bitcoin chain that holds a transaction, and the Bridge's +//! methods called as a transaction calls them. +//! +//! What rskj computes and rustock is being held to (a redeem script, an +//! address, a hash) is computed here from rskj's definition, not by calling +//! rustock's own code for it. + +use alloy_primitives::{Address as RskAddress, U256}; +use bitcoin::hashes::Hash as _; +use revm::context_interface::journaled_state::account::JournaledAccountTr; +use revm::context_interface::{ContextTr, JournalTr}; +use revm::MainContext; +use sha2::Digest as _; + +use crate::abi::encode::{self, Arg}; +use crate::bridge::constants::{BridgeConstants, BtcNetwork}; +use crate::bridge::federation::{serialize_federation_multikey, StoredMember}; +use crate::bridge::{ + btc_chain, btc_rules, federation, getters, governance, peg, serialization, storage, tx, +}; +use crate::hardfork::{RskHardforkConfig, RskNetworkUpgrade}; +use crate::precompiles::{BridgeTxContext, BRIDGE_ADDR}; + +/// rskj's `FlyoverTxResponseCodes`, as `registerFastBridgeBtcTransaction` +/// answers them. +pub(super) mod flyover_code { + pub(in super::super) const ALREADY_PROCESSED: i128 = -302; + pub(in super::super) const VALUE_ZERO: i128 = -304; + pub(in super::super) const UTXO_BELOW_MINIMUM: i128 = -305; + pub(in super::super) const GENERIC_ERROR: i128 = -900; +} + +/// One RBTC satoshi in wei. +pub(super) const WEI_PER_SATOSHI: u64 = 10_000_000_000; + +/// A Bridge with nothing stored, at RSK block `number`. +pub(super) fn ctx_at(number: u64) -> impl crate::RskContextTr { + revm::Context::mainnet() + .with_block(revm::context::BlockEnv { + number: U256::from(number), + timestamp: U256::from(1_700_000_000u64), + ..Default::default() + }) + .with_chain(crate::raw_storage::RskChainExt::default()) +} + +/// The RSK block a context is at. +pub(super) fn block_of(ctx: &mut CTX) -> u64 { + revm::context_interface::Block::number(ctx.block()).to::() +} + +/// The compressed public key of the private key `private`, read as rskj's +/// `BtcECKey.fromPrivate(Hex.decode(..))` reads it: a big-endian number. +pub(super) fn public_key(private: &[u8]) -> [u8; 33] { + let mut scalar = [0u8; 32]; + scalar[32 - private.len()..].copy_from_slice(private); + let key = k256::ecdsa::SigningKey::from_slice(&scalar).expect("a private key in range"); + key.verifying_key() + .to_encoded_point(true) + .as_bytes() + .try_into() + .expect("33 bytes") +} + +/// `count` keys, from the private keys `first`, `first + 1`, ... +pub(super) fn keys(first: u8, count: u8) -> Vec<[u8; 33]> { + (first..first + count) + .map(|private| public_key(&[private])) + .collect() +} + +/// bitcoinj-thin's `ScriptBuilder.createRedeemScript(threshold, keys)`: the +/// keys sorted, and the counts as its `ScriptBuilder.number` writes them, OP_1 +/// to OP_15 below 16 and a one-byte push from 16 on, to the 20 keys it takes. +pub(super) fn multisig(keys: &[[u8; 33]], threshold: usize) -> Vec { + assert!(keys.len() <= 20 && (1..=keys.len()).contains(&threshold)); + let number = |n: usize| { + if n < 16 { + vec![0x50 + n as u8] + } else { + vec![1, n as u8] + } + }; + let mut sorted = keys.to_vec(); + sorted.sort(); + let mut script = number(threshold); + for key in &sorted { + script.push(33); + script.extend_from_slice(key); + } + script.extend(number(sorted.len())); + script.push(0xae); + script +} + +/// A federation's redeem script, as rskj's `StandardMultiSigFederation` has +/// it: `size / 2 + 1` of its members. +pub(super) fn standard_redeem(keys: &[[u8; 33]]) -> Vec { + multisig(keys, keys.len() / 2 + 1) +} + +/// A compressed public key written in hex. +pub(super) fn key(hex: &str) -> [u8; 33] { + hex::decode(hex) + .expect("a key in hex") + .try_into() + .expect("a compressed key") +} + +/// The emergency keys of a network's Bridge constants. +pub(super) fn erp_keys_of(config: &BridgeConstants) -> Vec<[u8; 33]> { + config.erp_fed_pubkeys.iter().map(|hex| key(hex)).collect() +} + +/// `network`'s Bridge constants with the emergency keys `erp` and the delay +/// `delay` in place of its own. +pub(super) fn with_erp(network: BridgeConstants, erp: &[[u8; 33]], delay: i64) -> BridgeConstants { + let hexes: Vec<&'static str> = erp + .iter() + .map(|key| &*Box::leak(hex::encode(key).into_boxed_str())) + .collect(); + BridgeConstants { + erp_fed_pubkeys: Box::leak(hexes.into_boxed_slice()), + erp_fed_activation_delay: delay, + ..network + } +} + +/// A non-standard ERP federation's redeem script as rskj's builders write +/// it and its tests read it (`NonStandardErpFederationsTest +/// .validateErpRedeemScript`): the members' multisig and the emergency +/// multisig, each without its `OP_CHECKMULTISIG`, in `OP_NOTIF +/// OP_ELSE OP_CHECKSEQUENCEVERIFY OP_DROP OP_ENDIF +/// OP_CHECKMULTISIG`. `delay` is the bytes the delay is pushed as. +pub(super) fn non_standard_erp_redeem( + members: &[[u8; 33]], + emergency: &[[u8; 33]], + delay: &[u8], +) -> Vec { + let members = standard_redeem(members); + let emergency = standard_redeem(emergency); + let mut script = vec![0x64]; + script.extend_from_slice(&members[..members.len() - 1]); + script.extend_from_slice(&[0x67, delay.len() as u8]); + script.extend_from_slice(delay); + script.extend_from_slice(&[0xb2, 0x75]); + script.extend_from_slice(&emergency[..emergency.len() - 1]); + script.extend_from_slice(&[0x68, 0xae]); + script +} + +/// A P2SH ERP federation's redeem script, which a P2SH-P2WSH ERP federation +/// has too, as rskj's `P2shErpRedeemScriptBuilder` writes it: `OP_NOTIF +/// OP_ELSE OP_CHECKSEQUENCEVERIFY OP_DROP +/// OP_ENDIF`, each multisig whole. `delay` is the bytes +/// the delay is pushed as, Java's `BigInteger.toByteArray` reversed. +pub(super) fn p2sh_erp_redeem( + members: &[[u8; 33]], + emergency: &[[u8; 33]], + delay: &[u8], +) -> Vec { + let mut script = vec![0x64]; + script.extend(standard_redeem(members)); + script.extend_from_slice(&[0x67, delay.len() as u8]); + script.extend_from_slice(delay); + script.extend_from_slice(&[0xb2, 0x75]); + script.extend(standard_redeem(emergency)); + script.push(0x68); + script +} + +/// RIPEMD160(SHA256(data)). +pub(super) fn hash160(data: &[u8]) -> [u8; 20] { + ripemd::Ripemd160::digest(sha2::Sha256::digest(data)).into() +} + +/// `OP_HASH160 OP_EQUAL`. +pub(super) fn p2sh(hash: &[u8; 20]) -> bitcoin::ScriptBuf { + let mut script = vec![0xa9, 20]; + script.extend_from_slice(hash); + script.push(0x87); + bitcoin::ScriptBuf::from_bytes(script) +} + +/// `OP_DUP OP_HASH160 OP_EQUALVERIFY OP_CHECKSIG`. +pub(super) fn p2pkh(hash: &[u8; 20]) -> bitcoin::ScriptBuf { + let mut script = vec![0x76, 0xa9, 20]; + script.extend_from_slice(hash); + script.extend_from_slice(&[0x88, 0xac]); + bitcoin::ScriptBuf::from_bytes(script) +} + +/// `OP_0 `: a native segwit (bech32) output, P2WPKH. +pub(super) fn p2wpkh(hash: &[u8; 20]) -> bitcoin::ScriptBuf { + let mut script = vec![0x00, 20]; + script.extend_from_slice(hash); + bitcoin::ScriptBuf::from_bytes(script) +} + +/// Stores a federation of `keys` under `key_name`, in rskj's multikey +/// format, created at RSK block `creation_block`, with its format version +/// under `format_key`. +pub(super) fn store_federation( + ctx: &mut CTX, + (key_name, format_key): (&str, &str), + keys: &[[u8; 33]], + creation_block: u64, + format: u64, +) { + let members: Vec = keys.iter().copied().map(StoredMember::from_btc).collect(); + storage::bridge_store_bytes_named( + ctx, + key_name, + &serialize_federation_multikey(&members, 0, creation_block), + ); + storage::bridge_store_bytes_named(ctx, format_key, &serialization::rlp_encode_u64(format)); +} + +/// Where the federation that is, or is to be, active is stored. +pub(super) const NEW_FEDERATION: (&str, &str) = ( + storage::NEW_FEDERATION_KEY, + storage::NEW_FEDERATION_FORMAT_VERSION_KEY, +); +/// Where the federation before it is stored. +pub(super) const OLD_FEDERATION: (&str, &str) = ( + storage::OLD_FEDERATION_KEY, + storage::OLD_FEDERATION_FORMAT_VERSION_KEY, +); + +/// `cfg` with every upgrade from `from` on moved to block `height`, so that +/// below it none of what they bring is active. +pub(super) fn upgrades_from_at( + cfg: RskHardforkConfig, + from: RskNetworkUpgrade, + height: u64, +) -> RskHardforkConfig { + use RskNetworkUpgrade::*; + let moved: Vec = [ + Orchid, + Wasabi100, + TwoToThree, + Papyrus200, + Iris300, + Hop400, + Fingerroot500, + Arrowhead600, + Arrowhead631, + Lovell700, + Reed800, + Reed810, + Vetiver900, + ] + .into_iter() + .filter(|upgrade| *upgrade >= from && cfg.activation_height(*upgrade).is_some()) + .collect(); + moved + .into_iter() + .fold(cfg, |cfg, upgrade| cfg.with_hardfork(upgrade, Some(height))) +} + +/// The Bitcoin height a peg-in in [`btc_chain_holding`] is registered at: it +/// has there the three confirmations rskj's regtest asks of one. +pub(super) const PEGIN_HEIGHT: i64 = 350; + +/// Gives `ctx`'s Bridge Bitcoin regtest blocks 1 to 352, the only +/// transaction of blocks 349 and 350 being `raw`, taken under `cfg`. +pub(super) fn btc_chain_holding( + ctx: &mut CTX, + raw: &[u8], + cfg: &RskHardforkConfig, +) { + let root = tx::calculate_btc_tx_hash(raw); + 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| btc_rules::verify_header(header, BtcNetwork::Regtest)) + .expect("a nonce under regtest's target"); + prev = header.block_hash(); + bitcoin::consensus::serialize(&header) + }) + .collect(); + let slices: Vec<&[u8]> = headers.iter().map(Vec::as_slice).collect(); + let args = encode::args(&[Arg::BytesArray(&slices)]); + let number = block_of(ctx); + btc_chain::receive_headers( + ctx, + &args, + 0, + &BridgeConstants::rskj_regtest(), + cfg.has_stored_block_v2(number), + cfg, + ) + .expect("the headers are taken"); +} + +/// A partial merkle tree of one transaction, `raw`, matched: its root is the +/// transaction's hash. +pub(super) fn one_leaf_tree(raw: &[u8]) -> Vec { + let mut tree = 1u32.to_le_bytes().to_vec(); + tree.push(1); + tree.extend_from_slice(&tx::calculate_btc_tx_hash(raw)); + tree.extend_from_slice(&[1, 0x01]); + tree +} + +/// A transaction with one input, a P2PKH spend, paying `outputs`. +pub(super) fn tx_paying(outputs: &[(u64, bitcoin::ScriptBuf)]) -> bitcoin::Transaction { + let mut script_sig = vec![71]; + script_sig.extend_from_slice(&[0x30; 71]); + script_sig.push(33); + script_sig.extend_from_slice(&public_key(&[0x77])); + bitcoin::Transaction { + version: bitcoin::transaction::Version::TWO, + lock_time: bitcoin::absolute::LockTime::ZERO, + input: vec![bitcoin::TxIn { + previous_output: bitcoin::OutPoint { + txid: bitcoin::Txid::from_byte_array([0x5a; 32]), + vout: 0, + }, + script_sig: bitcoin::ScriptBuf::from_bytes(script_sig), + sequence: bitcoin::Sequence::MAX, + witness: bitcoin::Witness::new(), + }], + output: outputs + .iter() + .map(|(value, script_pubkey)| bitcoin::TxOut { + value: bitcoin::Amount::from_sat(*value), + script_pubkey: script_pubkey.clone(), + }) + .collect(), + } +} + +/// Gives the Bridge every RBTC there is, so that no peg-in passes the +/// locking cap for want of it. +pub(super) fn fund_bridge(ctx: &mut CTX) { + let all_rbtc = U256::from(2_100_000_000_000_000u64) * U256::from(WEI_PER_SATOSHI); + ctx.journal_mut() + .load_account_mut(BRIDGE_ADDR) + .expect("the Bridge's account") + .set_balance(all_rbtc); +} + +/// How many requests the peg-out queue holds once `releaseBtc` is asked for +/// `satoshis` at `block` of `network`, with the federation of `keys` active +/// and a fee per KB of `fee_per_kb`. +pub(super) fn release_queued( + network: (&BridgeConstants, &RskHardforkConfig), + block: u64, + keys: &[[u8; 33]], + fee_per_kb: u64, + satoshis: u64, +) -> usize { + let mut ctx = ctx_at(block); + store_federation(&mut ctx, NEW_FEDERATION, keys, 1, 1000); + storage::bridge_store_u256(&mut ctx, storage::FEE_PER_KB_KEY, U256::from(fee_per_kb)); + peg::release_btc( + &mut ctx, + 0, + network.0, + network.1, + &BridgeTxContext::default(), + RskAddress::repeat_byte(0x33), + 1, + U256::from(satoshis) * U256::from(WEI_PER_SATOSHI), + ) + .expect("releaseBtc answers"); + peg::load_release_request_queue(&mut ctx, true).len() +} + +/// A majority of the network's federation-change authorizers, as many as +/// decide a vote (`FederationConstants.federationChangeAuthorizer`). +pub(super) fn deciding_authorizers(config: &BridgeConstants) -> Vec { + let all: Vec = config + .federation_change_authorizer_keys + .iter() + .map(|key| { + let key = hex::decode(key).expect("a key in hex"); + federation::rsk_address_from_public_key(&key).expect("a public key") + }) + .collect(); + all[..all.len() / 2 + 1].to_vec() +} + +/// The context of a transaction `sender` sends. +pub(super) fn sent_by(sender: RskAddress) -> BridgeTxContext { + BridgeTxContext { + rsk_sender: sender, + ..BridgeTxContext::default() + } +} + +/// What a Bridge method answers, where it does not throw. +fn answer( + out: Result, + method: &str, +) -> i128 { + out.map(|out| int_answer(&out.bytes)) + .unwrap_or_else(|e| panic!("{method} throws: {e:?}")) +} + +/// Votes, as a majority of the network's federation-change authorizers, at +/// the block of `ctx`, to create a pending federation and add `members` to it +/// (each key as its member's BTC, RSK and MST key). Each vote answers 1. +pub(super) fn pend_federation( + ctx: &mut CTX, + network: (&BridgeConstants, &RskHardforkConfig), + members: &[[u8; 33]], +) { + let (config, cfg) = network; + let deciding = deciding_authorizers(config); + for &sender in &deciding { + let out = governance::create_federation(ctx, 0, config, cfg, &sent_by(sender)); + assert_eq!(answer(out, "createFederation"), 1, "createFederation"); + } + for member in members { + let args = encode::args(&[Arg::Bytes(member), Arg::Bytes(member), Arg::Bytes(member)]); + for &sender in &deciding { + let out = governance::add_federator_public_key_multikey( + ctx, + &args, + 0, + config, + cfg, + &sent_by(sender), + ); + assert_eq!( + answer(out, "addFederatorPublicKeyMultikey"), + 1, + "addFederatorPublicKeyMultikey" + ); + } + } +} + +/// [`pend_federation`], then the votes to commit it with the hash +/// `getPendingFederationHash` answers. Each vote before the commit's deciding +/// one answers 1; the commit's answer is `None` where its deciding vote +/// throws. +pub(super) fn elect_federation( + ctx: &mut CTX, + network: (&BridgeConstants, &RskHardforkConfig), + members: &[[u8; 33]], +) -> Option { + pend_federation(ctx, network, members); + let (config, cfg) = network; + let hash = + getters::get_pending_federation_hash(ctx, 0).expect("getPendingFederationHash answers"); + let length = U256::from_be_slice(&hash.bytes[32..64]).to::(); + let args = encode::args(&[Arg::Bytes(&hash.bytes[64..64 + length])]); + let deciding = deciding_authorizers(config); + let (deciding_vote, before) = deciding.split_last().expect("authorizers"); + for &sender in before { + let out = governance::commit_federation(ctx, &args, 0, config, cfg, &sent_by(sender)); + assert_eq!(answer(out, "commitFederation"), 1, "commitFederation"); + } + governance::commit_federation(ctx, &args, 0, config, cfg, &sent_by(*deciding_vote)) + .ok() + .map(|out| int_answer(&out.bytes)) +} + +/// An `int256` result, which here always fits an `i128`. +pub(super) fn int_answer(out: &[u8]) -> i128 { + assert_eq!(out.len(), 32, "an int256 result"); + let (high, low) = out.split_at(16); + let low = i128::from_be_bytes(low.try_into().expect("16 bytes")); + let fill = if low < 0 { 0xff } else { 0x00 }; + assert!(high.iter().all(|&b| b == fill), "an int256 past an i128"); + low +} + +/// A call of `registerFastBridgeBtcTransaction`, as the contract `lbc` makes it. +pub(super) struct FlyoverCall<'a> { + pub(super) tx: &'a [u8], + pub(super) height: i64, + pub(super) derivation_arguments: [u8; 32], + pub(super) user: &'a [u8], + pub(super) lbc: RskAddress, + pub(super) lp: &'a [u8], +} + +impl FlyoverCall<'_> { + /// rskj `PegUtils.getFlyoverDerivationHash`, over the addresses as + /// `BridgeUtils.serializeBtcAddressWithVersion` writes them. + pub(super) fn derivation_hash(&self, user: &[u8], lp: &[u8]) -> [u8; 32] { + let mut preimage = self.derivation_arguments.to_vec(); + preimage.extend_from_slice(user); + preimage.extend_from_slice(self.lbc.as_slice()); + preimage.extend_from_slice(lp); + sha3::Keccak256::digest(&preimage).into() + } + + /// What the method answers, its liquidity provider's flag false. + pub(super) fn answer( + &self, + ctx: &mut CTX, + config: &BridgeConstants, + cfg: &RskHardforkConfig, + ) -> i128 { + let tree = one_leaf_tree(self.tx); + let mut lbc_word = [0u8; 32]; + lbc_word[12..].copy_from_slice(self.lbc.as_slice()); + let args = encode::args(&[ + Arg::Bytes(self.tx), + Arg::Word(encode::int(self.height)), + Arg::Bytes(&tree), + Arg::Word(self.derivation_arguments), + Arg::Bytes(self.user), + Arg::Word(lbc_word), + Arg::Bytes(self.lp), + Arg::Word([0; 32]), + ]); + let out = peg::register_fast_bridge_btc_transaction( + ctx, + &args, + 0, + config, + cfg, + &BridgeTxContext::default(), + self.lbc, + 2, + ) + .expect("the method answers"); + int_answer(&out.bytes) + } +} + +/// rskj `FlyoverRedeemScriptBuilderImpl.of`: ` OP_DROP` before the +/// federation's redeem script, and its P2SH output. +pub(super) fn flyover_output( + derivation_hash: &[u8; 32], + federation_redeem: &[u8], +) -> bitcoin::ScriptBuf { + let mut script = vec![32]; + script.extend_from_slice(derivation_hash); + script.push(0x75); + script.extend_from_slice(federation_redeem); + p2sh(&hash160(&script)) +} + +/// Marks a Flyover derivation hash used for the Bitcoin transaction `raw`, +/// as rskj's `BridgeStorageProvider.markFlyoverDerivationHashAsUsed` does. +pub(super) fn mark_flyover_hash_used( + ctx: &mut CTX, + raw: &[u8], + derivation_hash: &[u8; 32], +) { + let mut display = tx::calculate_btc_tx_hash(raw); + display.reverse(); + let identifier = format!("{}{}", hex::encode(display), hex::encode(derivation_hash)); + let key = storage::compound_key(storage::FAST_BRIDGE_HASH_USED_KEY, "-", &identifier); + storage::bridge_store_raw(ctx, key, Some(vec![1])); +} diff --git a/crates/execution/src/rskj_tests/bridge_federation_state/bridge_serialization_utils_test.rs b/crates/execution/src/rskj_tests/bridge_federation_state/bridge_serialization_utils_test.rs new file mode 100644 index 0000000..ef4749d --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_federation_state/bridge_serialization_utils_test.rs @@ -0,0 +1,134 @@ +//! rskj's `BridgeSerializationUtilsTest` +//! (`co/rsk/peg/BridgeSerializationUtilsTest.java`), its listed tests of a +//! non-standard ERP federation (format 2000) written and read back: the same +//! federation, its redeem script built anew with the activations it is read +//! under. +//! +//! rskj reads a stored federation with the block's activations, so each port +//! keeps the federation as rskj writes it (`serializeFederation`, format 2000) +//! as the only one, at a block of the era rskj's test names, and reads back +//! its members, creation time and block, and the redeem script the Bridge +//! takes for it. Each era's script is the `bridge_federation` ports' +//! (`non_standard_erp_federations_test.rs`), here read through the store. +//! +//! Read in full. Its other tests are of other values' encodings (votes, +//! locks, peg-outs, UTXOs, coinbase information), each held where the value +//! is used. + +use crate::bridge::constants::BridgeConstants; +use crate::bridge::{federation, peg, storage}; +use crate::hardfork::RskHardforkConfig; + +use super::support::*; + +/// rskj's `TestConstants.NON_STANDARD_ERP_REDEEM_SCRIPT_HARDCODED`: testnet's +/// non-standard ERP redeem script before RSKIP284, whatever the members. +const TESTNET_HARDCODED: &str = "6453210208f40073a9e43b3e9103acec79767a6de9b0409749884e989960fee578012fce210225e892391625854128c5c4ea4340de0c2a70570f33db53426fc9c746597a03f42102afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da210344a3c38cd59afcba3edcebe143e025574594b001700dec41e59409bdbd0f2a0921039a060badbeb24bee49eb2063f616c0f0f0765d4ca646b20a88ce828f259fcdb955670300cd50b27552210216c23b2ea8e4f11c3f9e22711addb1d16a93964796913830856b568cc3ea21d3210275562901dd8faae20de0a4166362a4f82188db77dbed4ca887422ea1ec185f1421034db69f2112f4fb1bb6141bf6e2bd6631f0484d0bd95b16767902c9fe219d4a6f5368ae"; + +/// 52,560, the emergency delay of mainnet and testnet, as each builder +/// pushes it: before RSKIP293 in two big-endian bytes, from it as +/// `signedLongToByteArrayLE` writes it. +const DELAY_BEFORE_RSKIP293: &[u8] = &[0xcd, 0x50]; +const DELAY_FROM_RSKIP293: &[u8] = &[0x50, 0xcd, 0x00]; + +/// The federation, kept as rskj keeps it with format 2000 at `block` of +/// `network`, read back: whether it reads as written, and the redeem script +/// the Bridge takes for it. +fn written_and_read(network: (BridgeConstants, RskHardforkConfig), block: u64) -> Vec { + let (config, cfg) = network; + let keys = members(9); + let mut ctx = ctx_at(block); + store_federation(&mut ctx, NEW, &keys, 1_600_000_000, block - 100, Some(2000)); + let read = federation::load_stored_federation(&mut ctx, storage::NEW_FEDERATION_KEY) + .expect("the federation reads back"); + let mut read_keys = read.btc_keys(); + read_keys.sort(); + assert_eq!(read_keys, keys); + assert_eq!( + (read.creation_time_millis, read.creation_block), + (1_600_000_000, block - 100) + ); + peg::active_federation_keys_and_redeem(&mut ctx, &config, &cfg, block).1 +} + +fn mainnet() -> (BridgeConstants, RskHardforkConfig) { + (BridgeConstants::mainnet(), RskHardforkConfig::mainnet()) +} + +fn testnet() -> (BridgeConstants, RskHardforkConfig) { + (BridgeConstants::testnet(), RskHardforkConfig::testnet()) +} + +/// The non-standard ERP script of the members with `config`'s emergency keys. +fn era_script(config: &BridgeConstants, delay: &[u8]) -> Vec { + non_standard_erp_redeem(&members(9), &keys_of(config.erp_fed_pubkeys), delay) +} + +/// rskj `serializeAndDeserializeNonStandardErpFederation_preRSKIP284_testnet`: +/// testnet from RSKIP201 (iris300) to RSKIP284 reads the federation back with +/// its hardcoded script. +#[test] +fn on_testnet_before_rskip284_the_federation_reads_back_with_the_hardcoded_script() { + let block = 2_500_000; + let cfg = RskHardforkConfig::testnet(); + assert!(cfg.has_rskip201(block) && !cfg.has_rskip284(block)); + assert_eq!( + written_and_read(testnet(), block), + hex::decode(TESTNET_HARDCODED).expect("hex") + ); +} + +/// rskj `serializeAndDeserializeNonStandardErpFederation_postRSKIP284_preRSKIP293_testnet`: +/// testnet from RSKIP284 to RSKIP293 (2,581,800 to 3,103,000) reads it back +/// with its own script, the delay in two big-endian bytes, not the +/// hardcoded one. +#[test] +fn on_testnet_from_rskip284_until_rskip293_the_federation_reads_back_with_its_own_script() { + let block = 2_600_000; + let cfg = RskHardforkConfig::testnet(); + assert!(cfg.has_rskip284(block) && !cfg.has_rskip293(block)); + let script = written_and_read(testnet(), block); + assert_eq!( + script, + era_script(&BridgeConstants::testnet(), DELAY_BEFORE_RSKIP293) + ); + assert_ne!(script, hex::decode(TESTNET_HARDCODED).expect("hex")); +} + +/// rskj `serializeAndDeserializeNonStandardErpFederation_preRSKIP293_mainnet`: +/// mainnet from RSKIP201 to RSKIP293 (iris300 to hop400) reads it back with +/// its own script, the delay in two big-endian bytes. +#[test] +fn on_mainnet_before_rskip293_the_federation_reads_back_with_its_own_script() { + let block = 4_000_000; + let cfg = RskHardforkConfig::mainnet(); + assert!(cfg.has_rskip201(block) && !cfg.has_rskip293(block)); + assert_eq!( + written_and_read(mainnet(), block), + era_script(&BridgeConstants::mainnet(), DELAY_BEFORE_RSKIP293) + ); +} + +/// rskj `serializeAndDeserializeNonStandardErpFederation_postRSKIP293_testnet`: +/// from RSKIP293 testnet pushes the delay as a Java number. +#[test] +fn on_testnet_from_rskip293_the_federation_reads_back_with_the_delay_as_a_java_number() { + let block = 3_200_000; + assert!(RskHardforkConfig::testnet().has_rskip293(block)); + assert_eq!( + written_and_read(testnet(), block), + era_script(&BridgeConstants::testnet(), DELAY_FROM_RSKIP293) + ); +} + +/// rskj `serializeAndDeserializeNonStandardErpFederation_postRSKIP293_mainnet`: +/// from RSKIP293 (hop400) mainnet pushes it as a Java number too. +#[test] +fn on_mainnet_from_rskip293_the_federation_reads_back_with_the_delay_as_a_java_number() { + let block = 4_700_000; + assert!(RskHardforkConfig::mainnet().has_rskip293(block)); + assert_eq!( + written_and_read(mainnet(), block), + era_script(&BridgeConstants::mainnet(), DELAY_FROM_RSKIP293) + ); +} diff --git a/crates/execution/src/rskj_tests/bridge_federation_state/federation_constants_test.rs b/crates/execution/src/rskj_tests/bridge_federation_state/federation_constants_test.rs new file mode 100644 index 0000000..5243e3f --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_federation_state/federation_constants_test.rs @@ -0,0 +1,94 @@ +//! rskj's `FederationConstantsTest` +//! (`co/rsk/peg/federation/constants/FederationConstantsTest.java`), its +//! listed test of how long after a new federation's activation the old one +//! is migrated from (`getFundsMigrationAgeSinceActivationEnd`): 10,585 blocks +//! on mainnet, but 172,800 while RSKIP357 is active and RSKIP374 isn't +//! (hop401 to fingerroot500), 900 on testnet and 150 on regtest throughout. +//! +//! On mainnet the port runs `updateCollections` around the migration's end, +//! where the old federation is cleared. +//! +//! Read in full. Its other tests check each network's values, which +//! `bridge/constants.rs` holds, and the activation age's, which +//! `bridge/governance.rs`'s `testnets_activation_age_has_a_step_before_lovell` +//! and the port of `FederationSupportImplTest` reach. + +use crate::bridge::constants::BridgeConstants; +use crate::bridge::peg; +use crate::hardfork::RskHardforkConfig; +use crate::precompiles::BridgeTxContext; + +use super::support::*; + +/// Whether mainnet's old federation is still kept at `block`, with a new +/// federation created at `created` (in `format`) and nothing to migrate, +/// after `updateCollections`. +fn old_federation_kept(block: u64, created: u64, format: u64) -> bool { + let (config, cfg) = (BridgeConstants::mainnet(), RskHardforkConfig::mainnet()); + let mut ctx = ctx_at(block); + let old: Vec<[u8; 33]> = (0..3u8).map(|i| public_key(&[0x61 + i])).collect(); + store_federation(&mut ctx, NEW, &members(9), 1_000, created, Some(format)); + store_federation(&mut ctx, OLD, &old, 500, created - 1_000, Some(1000)); + peg::update_collections(&mut ctx, 0, &config, &cfg, &BridgeTxContext::default()) + .expect("updateCollections returns"); + !cell(&mut ctx, OLD.0).is_empty() +} + +/// rskj `getFundsMigrationAgeSinceActivationEnd`: the values, and on +/// mainnet the block the old federation is cleared at, the activation age +/// (18,500 before fingerroot500, 40,320 from it) and the migration's end +/// after the new federation's creation. +#[test] +fn the_migration_ends_later_on_mainnet_between_rskip357_and_rskip374() { + let mainnet = BridgeConstants::mainnet(); + assert_eq!( + ( + mainnet.funds_migration_age_end, + mainnet.special_case_funds_migration_age_end + ), + (10_585, 172_800) + ); + let testnet = BridgeConstants::testnet(); + assert_eq!( + ( + testnet.funds_migration_age_end, + testnet.special_case_funds_migration_age_end + ), + (900, 900) + ); + let regtest = BridgeConstants::rskj_regtest(); + assert_eq!( + ( + regtest.funds_migration_age_end, + regtest.special_case_funds_migration_age_end + ), + (150, 150) + ); + + let cfg = RskHardforkConfig::mainnet(); + // Before RSKIP357 (hop401): 10,585. + let created = 4_900_000; + let end = created + 18_500 + 10_585; + assert!(!cfg.has_rskip357(end)); + assert!(old_federation_kept(end - 1, created, 2000)); + assert!(!old_federation_kept(end, created, 2000)); + + // With RSKIP357 and not RSKIP374: 172,800. + let created = 4_980_000; + let end = created + 18_500 + 172_800; + assert!(cfg.has_rskip357(created) && !cfg.has_rskip374(end)); + assert!(old_federation_kept( + created + 18_500 + 10_585, + created, + 3000 + )); + assert!(old_federation_kept(end - 1, created, 3000)); + assert!(!old_federation_kept(end, created, 3000)); + + // From RSKIP374 (fingerroot500): 10,585 again, after 40,320. + let created = 5_500_000; + let end = created + 40_320 + 10_585; + assert!(cfg.has_rskip374(created)); + assert!(old_federation_kept(end - 1, created, 3000)); + assert!(!old_federation_kept(end, created, 3000)); +} diff --git a/crates/execution/src/rskj_tests/bridge_federation_state/federation_storage_provider_impl_tests.rs b/crates/execution/src/rskj_tests/bridge_federation_state/federation_storage_provider_impl_tests.rs new file mode 100644 index 0000000..af66ade --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_federation_state/federation_storage_provider_impl_tests.rs @@ -0,0 +1,293 @@ +//! rskj's `FederationStorageProviderImplTests` +//! (`co/rsk/peg/federation/FederationStorageProviderImplTests.java`): how the +//! Bridge keeps its federations and their UTXOs. +//! +//! rskj's tests set a federation on the provider and save it; here the +//! federation is put where rskj's own code puts it, by a commit or by the +//! end of a funds migration, and the cells are read after it. +//! +//! Read in full. Its other tests read back what was stored (each format, a +//! cached value, the pending federation, the election, the creation heights, +//! the last retired script), which the ports of `FederationSupportImplTest` +//! and `bridge/governance.rs`'s tests reach through the methods that read +//! them. + +use crate::bridge::constants::BridgeConstants; +use crate::bridge::peg; +use crate::bridge::storage::{self, BridgeUtxo}; +use crate::hardfork::RskHardforkConfig; +use crate::precompiles::BridgeTxContext; + +use super::support::*; + +/// Mainnet blocks of each era: before RSKIP123, papyrus200's (standard +/// multisig), iris300's (non-standard ERP) and hop401's (P2SH ERP). +const ERAS: [(u64, Option); 4] = [ + (1_000_000, None), + (2_500_000, Some(1000)), + (4_000_000, Some(2000)), + (5_000_000, Some(3000)), +]; + +/// A federation of nine committed at `block` of mainnet, with nothing +/// stored before it. +fn committed_at(block: u64) -> Ctx { + let (config, cfg) = (BridgeConstants::mainnet(), RskHardforkConfig::mainnet()); + let mut ctx = ctx_at(block); + assert_eq!( + elect(&mut ctx, &config, &cfg, &members(9)), + committed(&config) + ); + ctx +} + +/// rskj `testSaveNewFederation`: the new federation is kept with all its +/// members' keys and its format version from RSKIP123, and with its members' +/// BTC keys alone and no format before it, in each era's format. +#[test] +fn a_committed_federation_is_kept_as_the_new_one_as_rskj_keeps_it_in_each_era() { + let cfg = RskHardforkConfig::mainnet(); + for (block, format) in ERAS { + assert_eq!(cfg.has_rskip123(block), format.is_some()); + let mut ctx = committed_at(block); + let keys = members(9); + match format { + Some(format) => { + assert_eq!( + cell(&mut ctx, NEW.0), + serialize_federation(&keys, TIMESTAMP, block) + ); + assert_eq!(cell(&mut ctx, NEW.1), rlp_number(format), "at {block}"); + } + None => { + assert_eq!( + cell(&mut ctx, NEW.0), + serialize_federation_only_btc_keys(&keys, TIMESTAMP, block) + ); + assert!( + cell(&mut ctx, NEW.1).is_empty(), + "no format before RSKIP123" + ); + } + } + } +} + +/// rskj `testSaveOldFederation`: the federation committed over, here +/// mainnet's genesis one, is kept as the old one, as the new one is. +#[test] +fn the_federation_committed_over_is_kept_as_the_old_one_as_rskj_keeps_it_in_each_era() { + let genesis = keys_of(BridgeConstants::mainnet().genesis_federation_public_keys); + for (block, format) in ERAS { + let mut ctx = committed_at(block); + match format { + Some(_) => { + assert_eq!( + cell(&mut ctx, OLD.0), + serialize_federation(&genesis, MAINNET_GENESIS_CREATION_MILLIS, 1) + ); + assert_eq!(cell(&mut ctx, OLD.1), rlp_number(1000), "at {block}"); + } + None => { + assert_eq!( + cell(&mut ctx, OLD.0), + serialize_federation_only_btc_keys( + &genesis, + MAINNET_GENESIS_CREATION_MILLIS, + 1 + ) + ); + assert!( + cell(&mut ctx, OLD.1).is_empty(), + "no format before RSKIP123" + ); + } + } + } +} + +/// rskj `saveProposedFederation_preRSKIP419_whenProposedFederationIsSet_shouldNotSave`: +/// before RSKIP419 (arrowhead631) a commit keeps no proposed federation. +#[test] +fn before_rskip419_no_proposed_federation_is_kept() { + let block = 6_600_000; + assert!(!RskHardforkConfig::mainnet().has_rskip419(block)); + let mut ctx = committed_at(block); + assert!(!cell(&mut ctx, NEW.0).is_empty()); + assert!(cell(&mut ctx, PROPOSED.0).is_empty() && cell(&mut ctx, PROPOSED.1).is_empty()); +} + +/// A mainnet Bridge at `block`, whose new federation of nine was created at +/// `created` in `format` (or before RSKIP123, with `None`), with an old +/// federation of three kept the same way, after `updateCollections`. +fn retired_at(block: u64, created: u64, format: Option) -> Ctx { + let (config, cfg) = (BridgeConstants::mainnet(), RskHardforkConfig::mainnet()); + let mut ctx = ctx_at(block); + let old: Vec<[u8; 33]> = (0..3u8).map(|i| public_key(&[0x61 + i])).collect(); + store_federation(&mut ctx, NEW, &members(9), 1_000, created, format); + store_federation( + &mut ctx, + OLD, + &old, + 500, + created - 1_000, + format.map(|_| 1000), + ); + peg::update_collections(&mut ctx, 0, &config, &cfg, &BridgeTxContext::default()) + .expect("updateCollections returns"); + ctx +} + +/// rskj `saveOldFederation_before_RSKIP123_should_save_null`: before RSKIP123, +/// the end of the funds migration clears the old federation's cell and +/// writes no format. +#[test] +fn before_rskip123_the_retired_federation_is_cleared_without_a_format() { + let created = 1_000_000; + let end = created + 18_500 + 10_585; + assert!(!RskHardforkConfig::mainnet().has_rskip123(end)); + let mut ctx = retired_at(end - 1, created, None); + assert!(!cell(&mut ctx, OLD.0).is_empty(), "migrating, still kept"); + let mut ctx = retired_at(end, created, None); + assert!(cell(&mut ctx, OLD.0).is_empty(), "cleared"); + assert!(cell(&mut ctx, OLD.1).is_empty(), "no format"); +} + +/// rskj `saveOldFederation_after_RSKIP123_should_save_null`: from RSKIP123, +/// clearing the old federation also writes its format cell, as a standard +/// multisig's (1000), which rskj takes a federation of none to be. The +/// federations here were kept before RSKIP123, without format cells, and the +/// migration ends after it. +#[test] +fn from_rskip123_the_retired_federation_is_cleared_with_a_standard_format() { + let created = 1_570_000; + let end = created + 18_500 + 10_585; + let cfg = RskHardforkConfig::mainnet(); + assert!(!cfg.has_rskip123(created) && cfg.has_rskip123(end)); + let mut ctx = retired_at(end, created, None); + assert!(cell(&mut ctx, OLD.0).is_empty(), "cleared"); + assert_eq!(cell(&mut ctx, OLD.1), rlp_number(1000)); +} + +/// Three UTXOs of a P2PKH output, as rskj's `UTXOBuilder` makes them. +fn utxos(count: u8) -> Vec { + (1..=count) + .map(|i| BridgeUtxo { + tx_hash: [i; 32], + vout: 0, + value_satoshis: 100_000_000, + height: 0, + script: [&[0x76, 0xa9, 20][..], &[0x44; 20], &[0x88, 0xac]].concat(), + coinbase: false, + }) + .collect() +} + +/// `serializeUTXOList`: each UTXO as bitcoinj-thin's `UTXO.serializeToStream` +/// writes it (its value, its script's length in four bytes and its script, +/// its transaction's hash, its index, its height and whether it is a +/// coinbase's), as an element of a list. +fn serialize_utxo_list(utxos: &[BridgeUtxo]) -> Vec { + let items: Vec> = utxos + .iter() + .map(|utxo| { + let mut bytes = utxo.value_satoshis.to_le_bytes().to_vec(); + bytes.extend_from_slice(&(utxo.script.len() as u32).to_le_bytes()); + bytes.extend_from_slice(&utxo.script); + bytes.extend_from_slice(&utxo.tx_hash); + bytes.extend_from_slice(&utxo.vout.to_le_bytes()); + bytes.extend_from_slice(&utxo.height.to_le_bytes()); + bytes.push(u8::from(utxo.coinbase)); + rlp_element(&bytes) + }) + .collect(); + rlp_list(&items) +} + +/// The UTXO cells of `provideFederationBtcUTXOsTestArguments`: testnet's +/// general cell before RSKIP284, its pre-hop cell from it to RSKIP293 (hop400) +/// and its post-hop cell from that; mainnet's general cell throughout. +fn utxo_cells() -> [(RskHardforkConfig, u64, &'static str); 5] { + [ + ( + RskHardforkConfig::testnet(), + 2_581_799, + storage::NEW_FEDERATION_BTC_UTXOS_KEY, + ), + ( + RskHardforkConfig::testnet(), + 2_581_800, + storage::NEW_FEDERATION_BTC_UTXOS_TESTNET_PRE_HOP_KEY, + ), + ( + RskHardforkConfig::testnet(), + 3_103_000, + storage::NEW_FEDERATION_BTC_UTXOS_TESTNET_POST_HOP_KEY, + ), + ( + RskHardforkConfig::mainnet(), + 2_581_800, + storage::NEW_FEDERATION_BTC_UTXOS_KEY, + ), + ( + RskHardforkConfig::mainnet(), + 4_598_500, + storage::NEW_FEDERATION_BTC_UTXOS_KEY, + ), + ] +} + +const UTXO_CELLS: [&str; 3] = [ + storage::NEW_FEDERATION_BTC_UTXOS_KEY, + storage::NEW_FEDERATION_BTC_UTXOS_TESTNET_PRE_HOP_KEY, + storage::NEW_FEDERATION_BTC_UTXOS_TESTNET_POST_HOP_KEY, +]; + +/// rskj `getNewFederationBtcUTXOs`: the active federation's UTXOs are read +/// from the cell of the network and the era, and from no other. +#[test] +fn the_active_federations_utxos_are_read_from_the_cell_of_the_network_and_era() { + for (cfg, block, key) in utxo_cells() { + let mut ctx = ctx_at(block); + put(&mut ctx, key, &serialize_utxo_list(&utxos(2))); + let read = storage::load_federation_utxos(&mut ctx, &cfg, block); + assert_eq!( + serialize_utxo_list(&read), + serialize_utxo_list(&utxos(2)), + "{key} at {block}" + ); + for other in UTXO_CELLS.into_iter().filter(|other| *other != key) { + let mut ctx = ctx_at(block); + put(&mut ctx, other, &serialize_utxo_list(&utxos(2))); + assert!( + storage::load_federation_utxos(&mut ctx, &cfg, block).is_empty(), + "{other} at {block}" + ); + } + } +} + +/// rskj `saveNewFederationBtcUTXOs_utxosShouldBeSavedToStorage`: UTXOs added +/// to an empty set are written, as rskj writes them, in the cell of the +/// network and the era, and read back. +#[test] +fn the_active_federations_utxos_are_written_in_the_cell_of_the_network_and_era() { + for (cfg, block, key) in utxo_cells() { + let mut ctx = ctx_at(block); + put(&mut ctx, key, &[]); + let mut held = storage::load_federation_utxos(&mut ctx, &cfg, block); + assert!(held.is_empty()); + held.extend(utxos(3)); + storage::store_federation_utxos(&mut ctx, &cfg, block, &held); + assert_eq!( + cell(&mut ctx, key), + serialize_utxo_list(&utxos(3)), + "{key} at {block}" + ); + for other in UTXO_CELLS.into_iter().filter(|other| *other != key) { + assert!(cell(&mut ctx, other).is_empty(), "{other} at {block}"); + } + let read = storage::load_federation_utxos(&mut ctx, &cfg, block); + assert_eq!(serialize_utxo_list(&read), serialize_utxo_list(&utxos(3))); + } +} diff --git a/crates/execution/src/rskj_tests/bridge_federation_state/federation_support_impl_test.rs b/crates/execution/src/rskj_tests/bridge_federation_state/federation_support_impl_test.rs new file mode 100644 index 0000000..cf98e17 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_federation_state/federation_support_impl_test.rs @@ -0,0 +1,130 @@ +//! rskj's `FederationSupportImplTest` +//! (`co/rsk/peg/federation/FederationSupportImplTest.java`): which +//! federation is active, and the creation heights a federation change keeps +//! (RSKIP186). +//! +//! Read in full. Its listed tests of `getActiveFederationRedeemScript` before +//! RSKIP293, and of `getActiveFederationCreationBlockHeight` before RSKIP186, +//! are of answers no call reaches: only the methods +//! `getActivePowpegRedeemScript` and `getActiveFederationCreationBlockHeight` +//! read them, and rskj enables those from RSKIP293 and RSKIP186. Its tests of +//! the active federation's redeem script from RSKIP293 are held by +//! `bridge/governance.rs`'s `the_active_powpeg_redeem_script_is_rskjs`. From +//! the rest of the class the creation height's answer around the new +//! federation's activation is ported here. + +use crate::bridge::constants::BridgeConstants; +use crate::bridge::{governance, storage}; +use crate::hardfork::RskHardforkConfig; + +use super::support::*; + +fn mainnet() -> (BridgeConstants, RskHardforkConfig) { + (BridgeConstants::mainnet(), RskHardforkConfig::mainnet()) +} + +/// rskj `updateFederationCreationBlockHeights_beforeRSKIP186_doesNothing`: +/// before RSKIP186 (papyrus200) the update `updateCollections` makes leaves +/// the creation heights as they are, even with a next one whose federation +/// is past its activation age; from RSKIP186 (iris300) the same update +/// makes it the active one. +#[test] +fn before_rskip186_the_creation_heights_are_left_as_they_are() { + let (config, cfg) = mainnet(); + for (next, block, promoted) in [(2_400_000, 2_500_000, false), (3_700_000, 3_800_000, true)] { + assert_eq!(cfg.has_rskip186(block), promoted); + let mut ctx = ctx_at(block); + put( + &mut ctx, + storage::NEXT_FEDERATION_CREATION_BLOCK_HEIGHT_KEY, + &rlp_number(next), + ); + governance::update_federation_creation_block_heights(&mut ctx, &config, &cfg, block); + let (active, next_cell) = ( + cell( + &mut ctx, + storage::ACTIVE_FEDERATION_CREATION_BLOCK_HEIGHT_KEY, + ), + cell(&mut ctx, storage::NEXT_FEDERATION_CREATION_BLOCK_HEIGHT_KEY), + ); + if promoted { + assert_eq!((active, next_cell), (rlp_number(next), vec![])); + } else { + assert_eq!((active, next_cell), (vec![], rlp_number(next))); + } + } +} + +/// `getActiveFederationCreationBlockHeight` at `block` of mainnet, with the +/// next creation height `next` and the active one `active` kept. +fn creation_height_answered(block: u64, next: u64, active: Option) -> u64 { + let (config, cfg) = mainnet(); + let mut ctx = ctx_at(block); + put( + &mut ctx, + storage::NEXT_FEDERATION_CREATION_BLOCK_HEIGHT_KEY, + &rlp_number(next), + ); + if let Some(active) = active { + put( + &mut ctx, + storage::ACTIVE_FEDERATION_CREATION_BLOCK_HEIGHT_KEY, + &rlp_number(active), + ); + } + let out = governance::get_active_federation_creation_block_height(&mut ctx, 0, &config, &cfg) + .expect("getActiveFederationCreationBlockHeight answers"); + u64::try_from(int_answer(&out.bytes)).expect("a height") +} + +/// rskj `getActiveFederationCreationBlockHeight_withNextAndActiveFederationCreationBlockHeightsUnset_returnsZero`, +/// `..._withNextFederationCreationBlockHeightUnsetAndActiveFederationCreationBlockHeightSet_returnsActiveFederationCreationBlockHeight`, +/// and the rows of the two tests after them where the new federation isn't +/// active yet: the answer is the active creation height, or 0 with none. +/// The activation age is mainnet's 18,500 before RSKIP383 (fingerroot500) +/// and 40,320 from it. +#[test] +fn before_the_new_federation_is_active_the_active_creation_height_is_answered() { + for (next, block) in [ + (4_000_000, 4_000_000 + 18_500 - 1), + (5_500_000, 5_500_000 + 40_320 - 1), + (5_500_000, 5_500_000 + 18_500), + ] { + assert_eq!(creation_height_answered(block, next, None), 0, "at {block}"); + assert_eq!( + creation_height_answered(block, next, Some(3_700_000)), + 3_700_000, + "at {block}" + ); + } + let (config, cfg) = mainnet(); + let mut ctx = ctx_at(4_000_000); + let out = governance::get_active_federation_creation_block_height(&mut ctx, 0, &config, &cfg) + .expect("an answer"); + assert_eq!(int_answer(&out.bytes), 0, "nothing kept"); +} + +/// rskj `getActiveFederationCreationBlockHeight_withNewFederationCreationBlockHeightSetAndActiveFederationCreationBlockHeightUnset_returnsCreationBlockHeightAccordingToCurrentBlockAndActivations` +/// and `..._withNewAndActiveFederationCreationBlockHeightsSet_...`, their rows +/// where the new federation is active: from the block where it is, the +/// answer is its creation height, before `updateCollections` keeps it as the +/// active one (`FederationSupportImpl.getActiveFederationCreationBlockHeight` +/// reads the next height first). +#[test] +fn once_the_new_federation_is_active_its_creation_height_is_answered() { + for (next, block) in [ + (4_000_000, 4_000_000 + 18_500), + (5_500_000, 5_500_000 + 40_320), + ] { + assert_eq!( + creation_height_answered(block, next, None), + next, + "at {block}" + ); + assert_eq!( + creation_height_answered(block, next, Some(3_700_000)), + next, + "at {block}" + ); + } +} diff --git a/crates/execution/src/rskj_tests/bridge_federation_state/mod.rs b/crates/execution/src/rskj_tests/bridge_federation_state/mod.rs new file mode 100644 index 0000000..3c38268 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_federation_state/mod.rs @@ -0,0 +1,11 @@ +//! The federation's stored state and the votes that change it (rskj's +//! `FederationStorageProviderImpl`, `FederationSupportImpl`, the vote on a +//! federation change, `FederationConstants` and `BridgeSerializationUtils`). + +mod bridge_serialization_utils_test; +mod federation_constants_test; +mod federation_storage_provider_impl_tests; +mod federation_support_impl_test; +mod point_at_infinity_member_test; +mod support; +mod vote_federation_change_test; diff --git a/crates/execution/src/rskj_tests/bridge_federation_state/point_at_infinity_member_test.rs b/crates/execution/src/rskj_tests/bridge_federation_state/point_at_infinity_member_test.rs new file mode 100644 index 0000000..c592a91 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_federation_state/point_at_infinity_member_test.rs @@ -0,0 +1,469 @@ +//! A federation member whose key is the one byte `00`, the point at +//! infinity. +//! +//! Not a port: no test of rskj's gives a member such a key. Each test holds +//! rustock to what rskj's code, read in the release, does with one. Bouncy +//! Castle's `decodePoint`, under `BtcECKey.fromPublicOnly` and +//! `ECKey.fromPublicOnly`, takes `00` as the point at infinity; the member +//! keeps it, `getPubKey()` gives `00` back, and `00` sorts before every other +//! key (`BtcECKey.PUBKEY_COMPARATOR`). So the vote takes the key, the pending +//! and the committed federation store it, and their hash, redeem script, +//! address, `commit_federation` event and getters carry it. Its RSK address +//! (`ECKey.getAddress`) is the last 20 bytes of the Keccak-256 hash of +//! nothing. +//! +//! On rskj's regtest three of the five federation-change authorizers +//! ('auth-a' to 'auth-e') vote such a key in, every upgrade active from block +//! 0; on mainnet and testnet two of their three. + +use alloy_primitives::Address; + +use crate::abi::encode::{self, Arg}; +use crate::bridge::constants::BridgeConstants; +use crate::bridge::{getters, governance, peg, storage}; +use crate::hardfork::RskHardforkConfig; +use crate::precompiles::BridgeTxContext; + +use super::support::*; + +/// The point at infinity's RSK address. +const INFINITY_ADDRESS: Address = + alloy_primitives::address!("dcc703c0e500b653ca82273b7bfad8045d85a470"); + +/// The point at infinity's encoding. +const INFINITY: &[u8] = &[0x00]; + +type Answer = Result; + +/// Each deciding authorizer's vote through `vote`: their answers, `None` +/// where a vote throws. +fn votes( + ctx: &mut Ctx, + config: &BridgeConstants, + mut vote: impl FnMut(&mut Ctx, &BridgeTxContext) -> Answer, +) -> Vec> { + deciding_authorizers(config) + .into_iter() + .map(|sender| { + let tx = BridgeTxContext { + rsk_sender: sender, + ..BridgeTxContext::default() + }; + vote(ctx, &tx).ok().map(|out| int_answer(&out.bytes)) + }) + .collect() +} + +/// The deciding authorizers' votes to create a pending federation. +fn create(ctx: &mut Ctx, config: &BridgeConstants, cfg: &RskHardforkConfig) -> Vec> { + votes(ctx, config, |ctx, tx| { + governance::create_federation(ctx, 0, config, cfg, tx) + }) +} + +/// The deciding authorizers' votes to add a member of `keys`: its one key +/// (`addFederatorPublicKey`), or its BTC, RSK and MST keys +/// (`addFederatorPublicKeyMultikey`). +fn add( + ctx: &mut Ctx, + config: &BridgeConstants, + cfg: &RskHardforkConfig, + keys: &[&[u8]], +) -> Vec> { + let args: Vec = keys.iter().map(|key| Arg::Bytes(key)).collect(); + let args = encode::args(&args); + votes(ctx, config, |ctx, tx| { + if keys.len() == 1 { + governance::add_federator_public_key(ctx, &args, 0, config, cfg, tx) + } else { + governance::add_federator_public_key_multikey(ctx, &args, 0, config, cfg, tx) + } + }) +} + +/// The deciding authorizers' votes to commit the pending federation of hash +/// `hash`. +fn commit( + ctx: &mut Ctx, + config: &BridgeConstants, + cfg: &RskHardforkConfig, + hash: &[u8], +) -> Vec> { + let args = encode::args(&[Arg::Bytes(hash)]); + votes(ctx, config, |ctx, tx| { + governance::commit_federation(ctx, &args, 0, config, cfg, tx) + }) +} + +/// Keccak-256. +fn keccak(data: &[u8]) -> Vec { + use sha3::Digest as _; + sha3::Keccak256::digest(data).to_vec() +} + +/// What a `bytes` or `string` answer holds. +fn bytes_answer(answer: Answer) -> Vec { + let out = answer.expect("the getter answers").bytes; + let offset = alloy_primitives::U256::from_be_slice(&out[..32]).to::(); + let length = alloy_primitives::U256::from_be_slice(&out[offset..offset + 32]).to::(); + out[offset + 32..offset + 32 + length].to_vec() +} + +/// A `string` answer. +fn string_answer(answer: Answer) -> String { + String::from_utf8(bytes_answer(answer)).expect("UTF-8") +} + +/// The arguments of a getter of a member's key: the index, and a key type +/// where one is named. +fn at(index: i64, key_type: Option<&str>) -> Vec { + match key_type { + None => encode::args(&[Arg::Word(encode::int(index))]), + Some(key_type) => encode::args(&[ + Arg::Word(encode::int(index)), + Arg::Bytes(key_type.as_bytes()), + ]), + } +} + +/// `serializeFederationMember` of a member of one key for all three, `key` +/// as rskj encodes it. +fn member(key: &[u8]) -> Vec { + rlp_list(&[rlp_element(key), rlp_element(key), rlp_element(key)]) +} + +/// `serializeFederation`: a federation of members of one key each, given +/// sorted and as rskj encodes them, each member an element of the list. +fn federation(keys: &[&[u8]], time_millis: u64, block: u64) -> Vec { + let members: Vec> = keys.iter().map(|key| rlp_element(&member(key))).collect(); + rlp_list(&[ + rlp_number(time_millis), + rlp_number(block), + rlp_list(&members), + ]) +} + +/// `ScriptBuilder.createRedeemScript`: `size / 2 + 1` of `keys`, given +/// sorted and as rskj encodes them, each pushed by `ScriptBuilder.data`, the +/// point at infinity's one byte as a one-byte push (`01 00`). +fn multisig_of(keys: &[&[u8]]) -> Vec { + let mut script = vec![0x50 + (keys.len() / 2 + 1) as u8]; + for key in keys { + script.push(key.len() as u8); + script.extend_from_slice(key); + } + script.push(0x50 + keys.len() as u8); + script.push(0xae); + script +} + +/// `P2shErpRedeemScriptBuilder`, which P2SH-P2WSH ERP federations' scripts +/// are built by too: the members' multisig, and after the network's delay +/// its emergency keys'. +fn p2sh_erp_of(keys: &[&[u8]], config: &BridgeConstants) -> Vec { + let csv = signed_le(config.erp_fed_activation_delay); + let mut script = vec![0x64]; + script.extend(multisig_of(keys)); + script.push(0x67); + script.push(csv.len() as u8); + script.extend(csv); + script.extend_from_slice(&[0xb2, 0x75]); + script.extend(standard_redeem(&keys_of(config.erp_fed_pubkeys))); + script.push(0x68); + script +} + +/// rskj's regtest, every upgrade active: three authorizers vote a member of +/// the point at infinity into a pending federation, and two of 33-byte keys +/// beside it, and a key of that point again, of any of the three types, is a +/// member's already (-2). The pending federation, its getters and its hash +/// keep `00`, sorted first, and so do the proposed federation the commit +/// makes (RSKIP419), its P2SH-P2WSH ERP redeem script behind its address +/// (RSKIP305), the `commit_federation` event and the proposed federation's +/// getters; and a member's RSK key of that point is at its address. +#[test] +fn from_rskip123_the_vote_takes_the_point_at_infinity_and_the_proposed_federation_keeps_it() { + const BLOCK: u64 = 100; + let config = BridgeConstants::rskj_regtest(); + let cfg = RskHardforkConfig::rskj_regtest(); + assert!(cfg.has_rskip123(BLOCK) && cfg.has_rskip419(BLOCK) && cfg.has_rskip305(BLOCK)); + let decided = vec![Some(1); 3]; + assert_eq!(deciding_authorizers(&config).len(), 3); + let mut ctx = ctx_at(BLOCK); + let others = members(2); + let (a, b): (&[u8], &[u8]) = (&others[0], &others[1]); + + assert_eq!(create(&mut ctx, &config, &cfg), decided, "createFederation"); + assert_eq!(add(&mut ctx, &config, &cfg, &[INFINITY; 3]), decided); + assert_eq!( + cell(&mut ctx, storage::PENDING_FEDERATION_KEY), + rlp_list(&[member(INFINITY)]) + ); + for keys in [[INFINITY, a, a], [a, INFINITY, a], [a, a, INFINITY]] { + assert_eq!( + add(&mut ctx, &config, &cfg, &keys), + vec![Some(-2); 3], + "{keys:02x?}" + ); + } + assert_eq!(add(&mut ctx, &config, &cfg, &[b; 3]), decided); + assert_eq!(add(&mut ctx, &config, &cfg, &[a; 3]), decided); + // serializePendingFederation: its members sorted, each not an element. + assert_eq!( + cell(&mut ctx, storage::PENDING_FEDERATION_KEY), + rlp_list(&[member(INFINITY), member(a), member(b)]) + ); + let pending_key = |ctx: &mut Ctx, key_type: Option<&str>| match key_type { + None => bytes_answer(getters::get_pending_federator_public_key( + ctx, + &at(0, None), + 0, + )), + Some(_) => bytes_answer(getters::get_pending_federator_public_key_of_type( + ctx, + &at(0, key_type), + 0, + )), + }; + assert_eq!(pending_key(&mut ctx, None), INFINITY); + for key_type in ["btc", "rsk", "mst"] { + assert_eq!( + pending_key(&mut ctx, Some(key_type)), + INFINITY, + "{key_type}" + ); + } + // PendingFederation.getHash: its BTC keys alone, sorted (serializeOnlyBtcKeys). + let hash = keccak(&rlp_list(&[ + rlp_element(INFINITY), + rlp_element(a), + rlp_element(b), + ])); + assert_eq!(pending_hash(&mut ctx), hash); + + let old_address = string_answer(getters::get_federation_address(&mut ctx, 0, &config, &cfg)); + let _ = logs(&mut ctx); + assert_eq!( + commit(&mut ctx, &config, &cfg, &hash), + decided, + "commitFederation" + ); + assert_eq!( + cell(&mut ctx, PROPOSED.0), + federation(&[INFINITY, a, b], TIMESTAMP * 1000, BLOCK) + ); + assert_eq!(cell(&mut ctx, PROPOSED.1), rlp_number(4000)); + let new_address = governance::p2sh_base58_address( + &p2sh_p2wsh_hash(&p2sh_erp_of(&[INFINITY, a, b], &config)), + &config, + ); + let activation = BLOCK + governance::federation_activation_age(&config, &cfg, BLOCK); + let event = ( + vec![alloy_primitives::keccak256( + "commit_federation(bytes,string,bytes,string,int256)", + )], + encode::args(&[ + Arg::Bytes(&keys_of(config.genesis_federation_public_keys).concat()), + Arg::Bytes(old_address.as_bytes()), + Arg::Bytes(&[INFINITY, a, b].concat()), + Arg::Bytes(new_address.as_bytes()), + Arg::Word(encode::int(activation as i64)), + ]), + ); + assert!(logs(&mut ctx).contains(&event), "commit_federation"); + + let proposed_key = + getters::get_proposed_federator_public_key_of_type(&mut ctx, &at(0, Some("btc")), 0); + assert_eq!(bytes_answer(proposed_key), INFINITY); + assert_eq!( + string_answer(getters::get_proposed_federation_address( + &mut ctx, 0, &config, &cfg + )), + new_address + ); + let size = getters::get_proposed_federation_size(&mut ctx, 0).expect("an answer"); + assert_eq!(int_answer(&size.bytes), 3); + assert!(peg::is_sender_proposed_fed_member( + &mut ctx, + &cfg, + BLOCK, + INFINITY_ADDRESS + )); +} + +/// Mainnet before RSKIP123: two of the three authorizers vote the point at +/// infinity in with the vote of one key (`addFederatorPublicKey`), and again +/// it is a member's already. The pending federation (its BTC keys alone), +/// the new federation the commit hands over to (before RSKIP419), and the +/// legacy `commit_federation` event (before RSKIP146) keep `00`, sorted +/// first, and so does the standard multisig whose hash the event names. +#[test] +fn before_rskip123_the_vote_takes_the_point_at_infinity_and_the_new_federation_keeps_it() { + const BLOCK: u64 = 1_000_000; + let config = BridgeConstants::mainnet(); + let cfg = RskHardforkConfig::mainnet(); + assert!(!cfg.has_rskip123(BLOCK) && !cfg.has_rskip146(BLOCK) && !cfg.has_rskip201(BLOCK)); + let decided = vec![Some(1); 2]; + assert_eq!(deciding_authorizers(&config).len(), 2); + let mut ctx = ctx_at(BLOCK); + let others = members(2); + let (a, b): (&[u8], &[u8]) = (&others[0], &others[1]); + + assert_eq!(create(&mut ctx, &config, &cfg), decided, "createFederation"); + for key in [b, INFINITY, a] { + assert_eq!(add(&mut ctx, &config, &cfg, &[key]), decided, "{key:02x?}"); + } + assert_eq!(add(&mut ctx, &config, &cfg, &[INFINITY]), vec![Some(-2); 2]); + let btc_keys = rlp_list(&[rlp_element(INFINITY), rlp_element(a), rlp_element(b)]); + assert_eq!(cell(&mut ctx, storage::PENDING_FEDERATION_KEY), btc_keys); + assert_eq!( + bytes_answer(getters::get_pending_federator_public_key( + &mut ctx, + &at(0, None), + 0 + )), + INFINITY + ); + let hash = keccak(&btc_keys); + assert_eq!(pending_hash(&mut ctx), hash); + + let _ = logs(&mut ctx); + assert_eq!( + commit(&mut ctx, &config, &cfg, &hash), + decided, + "commitFederation" + ); + // serializeFederationOnlyBtcKeys, created at the block with its timestamp + // read as milliseconds. + assert_eq!( + cell(&mut ctx, NEW.0), + rlp_list(&[rlp_number(TIMESTAMP), rlp_number(BLOCK), btc_keys]) + ); + let genesis = keys_of(config.genesis_federation_public_keys); + let genesis: Vec<&[u8]> = genesis.iter().map(|key| &key[..]).collect(); + // BrigeEventLoggerLegacyImpl.logCommitFederation: a federation's address's + // hash, then its BTC keys, each an element. + let federation_data = |keys: &[&[u8]]| { + let elements: Vec> = keys.iter().map(|key| rlp_element(key)).collect(); + rlp_list(&[ + rlp_element(&hash160(&multisig_of(keys))), + rlp_list(&elements), + ]) + }; + let activation = BLOCK + governance::federation_activation_age(&config, &cfg, BLOCK); + let mut topic = [0u8; 32]; + topic[32 - b"commit_federation_topic".len()..].copy_from_slice(b"commit_federation_topic"); + let event = ( + vec![topic.into()], + rlp_list(&[ + federation_data(&genesis), + federation_data(&[INFINITY, a, b]), + rlp_element(activation.to_string().as_bytes()), + ]), + ); + assert!(logs(&mut ctx).contains(&event), "commit_federation_topic"); +} + +/// A committed federation keeps a member of the point at infinity: the +/// getters of the active federation and of the retiring one answer `00` for +/// its keys, each one's address is that of its multisig with `00` in it, and +/// that key's RSK address is a federator's, whose transactions the Bridge's +/// federators-only methods take (`isFromFederateMember`), though no one can +/// sign one. +#[test] +fn the_active_and_the_retiring_federation_answer_00_for_the_point_at_infinity() { + const BLOCK: u64 = 2_000_000; + let config = BridgeConstants::mainnet(); + let cfg = RskHardforkConfig::mainnet(); + assert!(cfg.has_rskip123(BLOCK) && !cfg.has_rskip201(BLOCK)); + let mut ctx = ctx_at(BLOCK); + let keys = members(4); + let (a, b, c, d): (&[u8], &[u8], &[u8], &[u8]) = (&keys[0], &keys[1], &keys[2], &keys[3]); + put(&mut ctx, NEW.0, &federation(&[INFINITY, a, b], 0, 1)); + put(&mut ctx, NEW.1, &rlp_number(1000)); + put(&mut ctx, OLD.0, &federation(&[INFINITY, c, d], 0, 0)); + put(&mut ctx, OLD.1, &rlp_number(1000)); + + let key = |ctx: &mut Ctx, key_type: Option<&str>, retiring: bool| { + let args = at(0, key_type); + bytes_answer(match (key_type, retiring) { + (None, false) => getters::get_federator_public_key(ctx, &args, 0, &config, &cfg), + (Some(_), false) => { + getters::get_federator_public_key_of_type(ctx, &args, 0, &config, &cfg) + } + (None, true) => { + getters::get_retiring_federator_public_key(ctx, &args, 0, &config, &cfg) + } + (Some(_), true) => { + getters::get_retiring_federator_public_key_of_type(ctx, &args, 0, &config, &cfg) + } + }) + }; + for retiring in [false, true] { + assert_eq!( + key(&mut ctx, None, retiring), + INFINITY, + "retiring: {retiring}" + ); + for key_type in ["btc", "rsk", "mst"] { + let answer = key(&mut ctx, Some(key_type), retiring); + assert_eq!(answer, INFINITY, "{key_type}, retiring: {retiring}"); + } + } + let size = getters::get_federation_size(&mut ctx, 0, &config, &cfg).expect("an answer"); + assert_eq!(int_answer(&size.bytes), 3); + assert_eq!( + string_answer(getters::get_federation_address(&mut ctx, 0, &config, &cfg)), + mainnet_p2sh_address(&p2sh_hash(&multisig_of(&[INFINITY, a, b]))) + ); + assert_eq!( + string_answer(getters::get_retiring_federation_address( + &mut ctx, 0, &config, &cfg + )), + mainnet_p2sh_address(&p2sh_hash(&multisig_of(&[INFINITY, c, d]))) + ); + assert!(peg::is_sender_active_or_retiring_fed_member( + &mut ctx, + &config, + &cfg, + BLOCK, + INFINITY_ADDRESS + )); +} + +/// A non-standard ERP federation, RSKIP201's before RSKIP353, keeps a member +/// of the point at infinity too: `NonStandardErpRedeemScriptBuilder` takes +/// the members' multisig, `00` pushed first, and after the network's delay +/// the emergency keys', each without its `OP_CHECKMULTISIG`, and ends with +/// one. The federation's address is that script's. +#[test] +fn a_non_standard_erp_federations_address_is_that_of_its_script_with_00_first() { + const BLOCK: u64 = 4_700_000; + let config = BridgeConstants::mainnet(); + let cfg = RskHardforkConfig::mainnet(); + assert_eq!(governance::federation_format_version(&cfg, BLOCK), 2000); + assert!(cfg.has_rskip293(BLOCK) && !cfg.has_rskip353(BLOCK)); + let mut ctx = ctx_at(BLOCK); + let keys = members(2); + let (a, b): (&[u8], &[u8]) = (&keys[0], &keys[1]); + put(&mut ctx, NEW.0, &federation(&[INFINITY, a, b], 0, 1)); + put(&mut ctx, NEW.1, &rlp_number(2000)); + + let without_checkmultisig = |script: Vec| script[..script.len() - 1].to_vec(); + let csv = signed_le(config.erp_fed_activation_delay); + let mut script = vec![0x64]; + script.extend(without_checkmultisig(multisig_of(&[INFINITY, a, b]))); + script.push(0x67); + script.push(csv.len() as u8); + script.extend(csv); + script.extend_from_slice(&[0xb2, 0x75]); + script.extend(without_checkmultisig(standard_redeem(&keys_of( + config.erp_fed_pubkeys, + )))); + script.extend_from_slice(&[0x68, 0xae]); + assert_eq!(script[1..4], [0x52, 0x01, 0x00]); + assert_eq!( + string_answer(getters::get_federation_address(&mut ctx, 0, &config, &cfg)), + mainnet_p2sh_address(&p2sh_hash(&script)) + ); +} diff --git a/crates/execution/src/rskj_tests/bridge_federation_state/support.rs b/crates/execution/src/rskj_tests/bridge_federation_state/support.rs new file mode 100644 index 0000000..4847ba4 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_federation_state/support.rs @@ -0,0 +1,506 @@ +//! What this folder's ports share: a Bridge at an RSK block, the votes that +//! change its federation, the cells it keeps them in, and what rskj writes in +//! them, written here from rskj's definitions (`BridgeSerializationUtils`, +//! `FederationMember.serialize`, the redeem script builders) rather than by +//! rustock's own code. + +use alloy_primitives::{Address as RskAddress, B256, U256}; +use revm::context_interface::{ContextTr, JournalTr}; +use sha2::Digest as _; + +use crate::abi::encode::{self, Arg}; +use crate::bridge::constants::BridgeConstants; +use crate::bridge::{getters, governance, storage}; +use crate::hardfork::RskHardforkConfig; +use crate::precompiles::BridgeTxContext; + +/// The world a ported test calls the Bridge in: no trie and no database. +pub(super) type Ctx = revm::Context< + revm::context::BlockEnv, + revm::context::TxEnv, + revm::context::CfgEnv, + revm::database::EmptyDB, + revm::Journal, + crate::raw_storage::RskChainExt, +>; + +/// The timestamp of the RSK blocks here, rskj's `RSK_EXECUTION_BLOCK_TIMESTAMP` +/// in `VoteFederationChangeTest`. +pub(super) const TIMESTAMP: u64 = 10; + +/// rskj's mainnet genesis federation's creation time, 1970-01-18T12:49:08.400Z +/// (`FederationMainNetConstants`), in milliseconds. +pub(super) const MAINNET_GENESIS_CREATION_MILLIS: u64 = 1_514_948_400; + +/// A Bridge with nothing stored, at RSK block `number`. +pub(super) fn ctx_at(number: u64) -> Ctx { + use revm::MainContext; + let mut ctx: Ctx = + revm::Context::mainnet().with_chain(crate::raw_storage::RskChainExt::default()); + ctx.block.number = U256::from(number); + ctx.block.timestamp = U256::from(TIMESTAMP); + ctx +} + +/// The bytes the Bridge keeps in its cell `name`. +pub(super) fn cell(ctx: &mut Ctx, name: &str) -> Vec { + storage::bridge_load_bytes_named(ctx, name) +} + +/// Writes `value` in the Bridge's cell `name`. +pub(super) fn put(ctx: &mut Ctx, name: &str, value: &[u8]) { + storage::bridge_store_bytes_named(ctx, name, value); +} + +// --------------------------------------------------------------------------- +// Keys and scripts +// --------------------------------------------------------------------------- + +/// The compressed public key of the private key `private`, a big-endian +/// number. +pub(super) fn public_key(private: &[u8]) -> [u8; 33] { + use k256::elliptic_curve::sec1::ToEncodedPoint; + let mut scalar = [0u8; 32]; + scalar[32 - private.len()..].copy_from_slice(private); + let key = k256::SecretKey::from_slice(&scalar).expect("a private key in range"); + key.public_key() + .to_encoded_point(true) + .as_bytes() + .try_into() + .expect("33 bytes") +} + +/// `count` members' keys, of the private keys 0x21, 0x22, and so on, +/// sorted as rskj sorts a federation's members. +pub(super) fn members(count: u8) -> Vec<[u8; 33]> { + let mut keys: Vec<[u8; 33]> = (0..count).map(|i| public_key(&[0x21 + i])).collect(); + keys.sort(); + keys +} + +/// The keys of a network's constants, written in hex, sorted. +pub(super) fn keys_of(hexes: &[&str]) -> Vec<[u8; 33]> { + let mut keys: Vec<[u8; 33]> = hexes + .iter() + .map(|key| hex::decode(key).expect("hex").try_into().expect("33 bytes")) + .collect(); + keys.sort(); + keys +} + +/// The RSK address of a public key, compressed or not: the last 20 bytes of +/// the Keccak-256 hash of its uncompressed point. +pub(super) fn rsk_address_of(key: &[u8]) -> RskAddress { + use k256::elliptic_curve::sec1::ToEncodedPoint; + use sha3::Digest as _; + let point = k256::PublicKey::from_sec1_bytes(key).expect("a public key"); + let uncompressed = point.to_encoded_point(false); + RskAddress::from_slice(&sha3::Keccak256::digest(&uncompressed.as_bytes()[1..])[12..]) +} + +/// RIPEMD-160 of SHA-256. +pub(super) fn hash160(data: &[u8]) -> [u8; 20] { + use ripemd::Digest as _; + ripemd::Ripemd160::digest(sha2::Sha256::digest(data)).into() +} + +/// bitcoinj-thin's `ScriptBuilder.createRedeemScript`: the keys sorted, the +/// counts as `ScriptBuilder.number` writes them. +pub(super) fn multisig(keys: &[[u8; 33]], threshold: usize) -> Vec { + let number = |n: usize| { + if n < 16 { + vec![0x50 + n as u8] + } else { + vec![1, n as u8] + } + }; + let mut sorted = keys.to_vec(); + sorted.sort(); + let mut script = number(threshold); + for key in &sorted { + script.push(33); + script.extend_from_slice(key); + } + script.extend(number(sorted.len())); + script.push(0xae); + script +} + +/// A standard multisig federation's redeem script, `size / 2 + 1` of its +/// members. +pub(super) fn standard_redeem(keys: &[[u8; 33]]) -> Vec { + multisig(keys, keys.len() / 2 + 1) +} + +/// bitcoinj-thin's `Utils.signedLongToByteArrayLE`: Java's +/// `BigInteger.toByteArray`, reversed. +pub(super) fn signed_le(n: i64) -> Vec { + let mut bytes: Vec = n + .to_be_bytes() + .into_iter() + .skip_while(|byte| *byte == 0) + .collect(); + if bytes.first().is_none_or(|byte| byte & 0x80 != 0) { + bytes.insert(0, 0); + } + bytes.reverse(); + bytes +} + +/// rskj's `P2shErpRedeemScriptBuilder`, which the P2SH-P2WSH ERP federations' +/// scripts are built by too: `OP_NOTIF OP_ELSE +/// OP_CHECKSEQUENCEVERIFY OP_DROP OP_ENDIF`. +pub(super) fn p2sh_erp_redeem(members: &[[u8; 33]], emergency: &[[u8; 33]], delay: i64) -> Vec { + let csv = signed_le(delay); + let mut script = vec![0x64]; + script.extend(standard_redeem(members)); + script.push(0x67); + script.push(csv.len() as u8); + script.extend(csv); + script.extend_from_slice(&[0xb2, 0x75]); + script.extend(standard_redeem(emergency)); + script.push(0x68); + script +} + +/// rskj's non-standard ERP redeem script (`NonStandardErpRedeemScriptBuilder`): +/// the two multisigs without their `OP_CHECKMULTISIG`, and one after +/// `OP_ENDIF`. `delay` is the bytes the delay is pushed as. +pub(super) fn non_standard_erp_redeem( + members: &[[u8; 33]], + emergency: &[[u8; 33]], + delay: &[u8], +) -> Vec { + let members = standard_redeem(members); + let emergency = standard_redeem(emergency); + let mut script = vec![0x64]; + script.extend_from_slice(&members[..members.len() - 1]); + script.extend_from_slice(&[0x67, delay.len() as u8]); + script.extend_from_slice(delay); + script.extend_from_slice(&[0xb2, 0x75]); + script.extend_from_slice(&emergency[..emergency.len() - 1]); + script.extend_from_slice(&[0x68, 0xae]); + script +} + +/// The hash a P2SH output of `redeem` pays, as a P2SH address holds it. +pub(super) fn p2sh_hash(redeem: &[u8]) -> [u8; 20] { + hash160(redeem) +} + +/// The hash a P2SH-P2WSH output of `redeem` pays: its witness program's. +pub(super) fn p2sh_p2wsh_hash(redeem: &[u8]) -> [u8; 20] { + let mut program = vec![0x00, 32]; + program.extend_from_slice(&sha2::Sha256::digest(redeem)); + hash160(&program) +} + +/// `OP_HASH160 OP_EQUAL`. +pub(super) fn p2sh_script(hash: &[u8; 20]) -> Vec { + let mut script = vec![0xa9, 20]; + script.extend_from_slice(hash); + script.push(0x87); + script +} + +/// A mainnet P2SH address, base58 with version 5. +pub(super) fn mainnet_p2sh_address(hash: &[u8; 20]) -> String { + let mut payload = vec![5]; + payload.extend_from_slice(hash); + bitcoin::base58::encode_check(&payload) +} + +// --------------------------------------------------------------------------- +// What rskj writes (`BridgeSerializationUtils`, RLP as ethereumj encodes it) +// --------------------------------------------------------------------------- + +/// `RLP.encodeElement`. +pub(super) fn rlp_element(data: &[u8]) -> Vec { + let mut out = match data.len() { + 0 => return vec![0x80], + 1 if data[0] < 0x80 => return data.to_vec(), + len if len < 56 => vec![0x80 + len as u8], + len => { + let size: Vec = (len as u64) + .to_be_bytes() + .into_iter() + .skip_while(|byte| *byte == 0) + .collect(); + let mut prefix = vec![0xb7 + size.len() as u8]; + prefix.extend(size); + prefix + } + }; + out.extend_from_slice(data); + out +} + +/// `RLP.encodeList`. +pub(super) fn rlp_list(items: &[Vec]) -> Vec { + let payload: Vec = items.concat(); + let mut out = if payload.len() < 56 { + vec![0xc0 + payload.len() as u8] + } else { + let size: Vec = (payload.len() as u64) + .to_be_bytes() + .into_iter() + .skip_while(|byte| *byte == 0) + .collect(); + let mut prefix = vec![0xf7 + size.len() as u8]; + prefix.extend(size); + prefix + }; + out.extend(payload); + out +} + +/// `RLP.encodeBigInteger(BigInteger.valueOf(n))`, which +/// `serializeInteger` and `serializeLong` write. +pub(super) fn rlp_number(n: u64) -> Vec { + if n == 0 { + return vec![0x80]; + } + let bytes: Vec = n + .to_be_bytes() + .into_iter() + .skip_while(|byte| *byte == 0) + .collect(); + rlp_element(&bytes) +} + +/// `serializeFederation`: its creation time in milliseconds, its creation +/// block, and its members sorted, each `FederationMember.serialize` (its BTC, +/// RSK and MST keys, here one key for all three) as an element. +pub(super) fn serialize_federation(keys: &[[u8; 33]], time_millis: u64, block: u64) -> Vec { + let mut sorted = keys.to_vec(); + sorted.sort(); + let members: Vec> = sorted + .iter() + .map(|key| { + rlp_element(&rlp_list(&[ + rlp_element(key), + rlp_element(key), + rlp_element(key), + ])) + }) + .collect(); + rlp_list(&[ + rlp_number(time_millis), + rlp_number(block), + rlp_list(&members), + ]) +} + +/// `serializeFederationOnlyBtcKeys`: as [`serialize_federation`], each member +/// its BTC key alone. +pub(super) fn serialize_federation_only_btc_keys( + keys: &[[u8; 33]], + time_millis: u64, + block: u64, +) -> Vec { + let mut sorted = keys.to_vec(); + sorted.sort(); + let members: Vec> = sorted.iter().map(|key| rlp_element(key)).collect(); + rlp_list(&[ + rlp_number(time_millis), + rlp_number(block), + rlp_list(&members), + ]) +} + +/// `serializeScript`: a list holding the script. +pub(super) fn serialize_script(script: &[u8]) -> Vec { + rlp_list(&[rlp_element(script)]) +} + +/// Where a federation is kept: its cell and its format version's. +pub(super) type Slot = (&'static str, &'static str); + +/// The federation that is, or is to be, active. +pub(super) const NEW: Slot = ( + storage::NEW_FEDERATION_KEY, + storage::NEW_FEDERATION_FORMAT_VERSION_KEY, +); + +/// The federation before it. +pub(super) const OLD: Slot = ( + storage::OLD_FEDERATION_KEY, + storage::OLD_FEDERATION_FORMAT_VERSION_KEY, +); + +/// The federation proposed from RSKIP419. +pub(super) const PROPOSED: Slot = ( + storage::PROPOSED_FEDERATION_KEY, + storage::PROPOSED_FEDERATION_FORMAT_VERSION_KEY, +); + +/// Keeps a federation of `keys` in `slot` as rskj does from RSKIP123, with +/// its format version, or, with no format, as it did before (its BTC keys +/// alone and no format cell). +pub(super) fn store_federation( + ctx: &mut Ctx, + slot: Slot, + keys: &[[u8; 33]], + time_millis: u64, + block: u64, + format: Option, +) { + match format { + Some(format) => { + put(ctx, slot.0, &serialize_federation(keys, time_millis, block)); + put(ctx, slot.1, &rlp_number(format)); + } + None => put( + ctx, + slot.0, + &serialize_federation_only_btc_keys(keys, time_millis, block), + ), + } +} + +// --------------------------------------------------------------------------- +// Votes +// --------------------------------------------------------------------------- + +/// As many of the network's federation-change authorizers as decide a vote, +/// a majority (`FederationConstants.federationChangeAuthorizer`). +pub(super) fn deciding_authorizers(config: &BridgeConstants) -> Vec { + let all: Vec = config + .federation_change_authorizer_keys + .iter() + .map(|key| rsk_address_of(&hex::decode(key).expect("a key in hex"))) + .collect(); + all[..all.len() / 2 + 1].to_vec() +} + +/// The context of a transaction `sender` sends. +fn sent_by(sender: RskAddress) -> BridgeTxContext { + BridgeTxContext { + rsk_sender: sender, + ..BridgeTxContext::default() + } +} + +/// An `int256` answer, which here always fits an `i128`. +pub(super) fn int_answer(out: &[u8]) -> i128 { + assert_eq!(out.len(), 32, "an int256 answer"); + let (high, low) = out.split_at(16); + let low = i128::from_be_bytes(low.try_into().expect("16 bytes")); + let fill = if low < 0 { 0xff } else { 0x00 }; + assert!(high.iter().all(|&b| b == fill), "an int256 past an i128"); + low +} + +type Answer = Result; + +/// Each deciding authorizer's vote through `vote`: their answers, `None` +/// where a vote throws. +fn votes( + ctx: &mut Ctx, + config: &BridgeConstants, + mut vote: impl FnMut(&mut Ctx, &BridgeTxContext) -> Answer, +) -> Vec> { + deciding_authorizers(config) + .into_iter() + .map(|sender| { + vote(ctx, &sent_by(sender)) + .ok() + .map(|out| int_answer(&out.bytes)) + }) + .collect() +} + +/// The votes to create a pending federation and add `keys` to it, each its +/// member's BTC, RSK and MST key from RSKIP123 and its BTC key before it. +/// Every vote answers 1. +pub(super) fn pend_federation( + ctx: &mut Ctx, + config: &BridgeConstants, + cfg: &RskHardforkConfig, + keys: &[[u8; 33]], +) { + let all_one = vec![Some(1); deciding_authorizers(config).len()]; + let created = votes(ctx, config, |ctx, tx_ctx| { + governance::create_federation(ctx, 0, config, cfg, tx_ctx) + }); + assert_eq!(created, all_one, "createFederation"); + let multikey = cfg.has_rskip123(ctx.block.number.to::()); + for key in keys { + let added = if multikey { + let args = encode::args(&[Arg::Bytes(key), Arg::Bytes(key), Arg::Bytes(key)]); + votes(ctx, config, |ctx, tx_ctx| { + governance::add_federator_public_key_multikey(ctx, &args, 0, config, cfg, tx_ctx) + }) + } else { + let args = encode::args(&[Arg::Bytes(key)]); + votes(ctx, config, |ctx, tx_ctx| { + governance::add_federator_public_key(ctx, &args, 0, config, cfg, tx_ctx) + }) + }; + assert_eq!(added, all_one, "addFederatorPublicKey"); + } +} + +/// The hash `getPendingFederationHash` answers. +pub(super) fn pending_hash(ctx: &mut Ctx) -> Vec { + let out = + getters::get_pending_federation_hash(ctx, 0).expect("getPendingFederationHash answers"); + let length = U256::from_be_slice(&out.bytes[32..64]).to::(); + out.bytes[64..64 + length].to_vec() +} + +/// [`pend_federation`], then each deciding authorizer's vote to commit it: +/// their answers, `None` where a vote throws. The logs of the votes before +/// the commit are dropped, and the commit's kept. +pub(super) fn elect( + ctx: &mut Ctx, + config: &BridgeConstants, + cfg: &RskHardforkConfig, + keys: &[[u8; 33]], +) -> Vec> { + pend_federation(ctx, config, cfg, keys); + let _ = ctx.journal_mut().take_logs(); + let args = encode::args(&[Arg::Bytes(&pending_hash(ctx))]); + votes(ctx, config, |ctx, tx_ctx| { + governance::commit_federation(ctx, &args, 0, config, cfg, tx_ctx) + }) +} + +/// The answers of a commit every deciding authorizer's vote takes. +pub(super) fn committed(config: &BridgeConstants) -> Vec> { + vec![Some(1); deciding_authorizers(config).len()] +} + +/// An event as the Bridge logs it: its topics, then its data. +pub(super) type Event = (Vec, Vec); + +/// The events logged since the last taken. +pub(super) fn logs(ctx: &mut Ctx) -> Vec { + ctx.journal_mut() + .take_logs() + .into_iter() + .map(|log| (log.data.topics().to_vec(), log.data.data.to_vec())) + .collect() +} + +/// `commit_federation(bytes oldFederationBtcPublicKeys, string +/// oldFederationBtcAddress, bytes newFederationBtcPublicKeys, string +/// newFederationBtcAddress, int256 activationHeight)`, none of it indexed +/// (`BridgeEventLoggerImpl.logCommitFederation`). +pub(super) fn commit_federation_event( + old: (&[[u8; 33]], &str), + new: (&[[u8; 33]], &str), + activation_height: u64, +) -> Event { + let flat = |keys: &[[u8; 33]]| keys.concat(); + let (old_keys, new_keys) = (flat(old.0), flat(new.0)); + let data = encode::args(&[ + Arg::Bytes(&old_keys), + Arg::Bytes(old.1.as_bytes()), + Arg::Bytes(&new_keys), + Arg::Bytes(new.1.as_bytes()), + Arg::Word(encode::int(activation_height as i64)), + ]); + let topic = alloy_primitives::keccak256("commit_federation(bytes,string,bytes,string,int256)"); + (vec![topic], data) +} diff --git a/crates/execution/src/rskj_tests/bridge_federation_state/vote_federation_change_test.rs b/crates/execution/src/rskj_tests/bridge_federation_state/vote_federation_change_test.rs new file mode 100644 index 0000000..c06844d --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_federation_state/vote_federation_change_test.rs @@ -0,0 +1,267 @@ +//! rskj's `VoteFederationChangeTest` +//! (`co/rsk/peg/federation/VoteFederationChangeTest.java`): the votes that +//! commit a pending federation, and what the commit writes in each era. +//! +//! Each port runs on mainnet, at a block of the era rskj's activations name, +//! voting as a majority of mainnet's federation-change authorizers. Nothing is +//! stored before it, so the federation committed over is mainnet's genesis +//! federation, where rskj's test stores one of its own. +//! +//! Read in full. Its other tests are of each vote's answers (an unknown +//! function, an unauthorized caller, a vote short of a majority, a key added +//! twice, a rollback), which `bridge/governance.rs`'s tests and the +//! `bridge_federation` ports hold, and of `commitProposedFederation`, which +//! `bridge/governance.rs`'s `the_genesis_federation_leaves_in_its_own_format` +//! reaches. + +use crate::bridge::constants::BridgeConstants; +use crate::bridge::storage::{self, BridgeUtxo}; +use crate::hardfork::RskHardforkConfig; + +use super::support::*; + +/// Mainnet blocks of each era: papyrus200's (RSKIP123, not RSKIP186), +/// arrowhead631's (RSKIP186, not RSKIP419), lovell700's (RSKIP419, not +/// RSKIP305) and vetiver900's. +const PAPYRUS200: u64 = 2_500_000; +const ARROWHEAD631: u64 = 6_600_000; +const LOVELL700: u64 = 7_400_000; +const VETIVER900: u64 = 9_000_000; + +fn mainnet() -> (BridgeConstants, RskHardforkConfig) { + (BridgeConstants::mainnet(), RskHardforkConfig::mainnet()) +} + +/// Mainnet's genesis federation: its keys and its address. +fn genesis() -> (Vec<[u8; 33]>, String) { + let keys = keys_of(BridgeConstants::mainnet().genesis_federation_public_keys); + let address = mainnet_p2sh_address(&p2sh_hash(&standard_redeem(&keys))); + (keys, address) +} + +/// Mainnet's emergency keys. +fn emergency() -> Vec<[u8; 33]> { + keys_of(BridgeConstants::mainnet().erp_fed_pubkeys) +} + +/// Two UTXOs of the active federation's, as rskj keeps them. +fn utxos() -> Vec { + (1..=2u8) + .map(|i| BridgeUtxo { + tx_hash: [i; 32], + vout: u32::from(i), + value_satoshis: 100_000_000, + height: 0, + script: p2sh_script(&p2sh_hash(&standard_redeem(&genesis().0))), + coinbase: false, + }) + .collect() +} + +/// A Bridge at `block` of mainnet holding [`utxos`] for the active +/// federation: the UTXO cell's bytes, and the Bridge. +fn holding_utxos(block: u64) -> (Vec, Ctx) { + let (_, cfg) = mainnet(); + let mut ctx = ctx_at(block); + storage::store_federation_utxos(&mut ctx, &cfg, block, &utxos()); + (cell(&mut ctx, storage::NEW_FEDERATION_BTC_UTXOS_KEY), ctx) +} + +/// The pending federation's votes are gone (`assertPendingFederationVotingWasCleaned`): +/// no pending federation, and an election with no votes, which rskj writes +/// as an empty list. +fn voting_cleaned(ctx: &mut Ctx) { + assert!( + cell(ctx, storage::PENDING_FEDERATION_KEY).is_empty(), + "no pending federation" + ); + assert_eq!( + cell(ctx, storage::FEDERATION_ELECTION_KEY), + vec![0xc0], + "no votes" + ); +} + +/// rskj `voteCommitFederation_preRSKIP305_commitFederationWithOver10Members_throwsException`: +/// before RSKIP305 (lovell700) the federation a commit builds is a P2SH ERP +/// one, whose redeem script, with a flyover prefix, must fit a scriptSig +/// element; eleven members' don't, and the deciding vote throws. Nothing is +/// committed. +#[test] +fn before_rskip305_a_commit_of_eleven_members_throws() { + let (config, cfg) = mainnet(); + assert!(cfg.has_rskip419(LOVELL700) && !cfg.has_rskip305(LOVELL700)); + let mut ctx = ctx_at(LOVELL700); + let answers = elect(&mut ctx, &config, &cfg, &members(11)); + let mut thrown = committed(&config); + *thrown.last_mut().expect("a vote") = None; + assert_eq!(answers, thrown); + assert!( + cell(&mut ctx, PROPOSED.0).is_empty(), + "no proposed federation" + ); + assert!( + !cell(&mut ctx, storage::PENDING_FEDERATION_KEY).is_empty(), + "still pending" + ); +} + +/// rskj `voteCommitFederation_postRSKIP305_commitFederationWith20Members_shouldNotThrow`: +/// from RSKIP305 a commit builds a P2SH-P2WSH ERP federation, whose script +/// goes in the witness: twenty members commit, as a proposed federation of +/// twenty. +#[test] +fn from_rskip305_a_commit_of_twenty_members_is_proposed() { + let (config, cfg) = mainnet(); + let keys = members(20); + let mut ctx = ctx_at(VETIVER900); + assert_eq!(elect(&mut ctx, &config, &cfg, &keys), committed(&config)); + assert_eq!( + cell(&mut ctx, PROPOSED.0), + serialize_federation(&keys, TIMESTAMP * 1000, VETIVER900) + ); + assert_eq!(cell(&mut ctx, PROPOSED.1), rlp_number(4000)); +} + +/// rskj `voteCommitFederation_preRSKIP186_whenPendingFederationIsSet_shouldPerformLegacyCommitFederationActionsButNotSetFederationChangeInfo`: +/// before RSKIP186 (papyrus200) a commit hands over at once: the federation +/// built from the pending one, created at the block with its timestamp read +/// as milliseconds, is the new federation, the active one the old, the +/// active one's UTXOs the old one's; the votes are cleaned and +/// `commit_federation` logged. No creation height and no retired script are +/// kept. +#[test] +fn before_rskip186_a_commit_hands_over_without_the_federation_change_information() { + let (config, cfg) = mainnet(); + assert!(cfg.has_rskip123(PAPYRUS200) && !cfg.has_rskip186(PAPYRUS200)); + let keys = members(9); + let (genesis_keys, genesis_address) = genesis(); + let (held, mut ctx) = holding_utxos(PAPYRUS200); + + assert_eq!(elect(&mut ctx, &config, &cfg, &keys), committed(&config)); + assert_eq!( + logs(&mut ctx), + vec![commit_federation_event( + (&genesis_keys, &genesis_address), + ( + &keys, + &mainnet_p2sh_address(&p2sh_hash(&standard_redeem(&keys))) + ), + PAPYRUS200 + 18_500, + )] + ); + assert_eq!( + cell(&mut ctx, NEW.0), + serialize_federation(&keys, TIMESTAMP, PAPYRUS200) + ); + assert_eq!(cell(&mut ctx, NEW.1), rlp_number(1000)); + assert_eq!( + cell(&mut ctx, OLD.0), + serialize_federation(&genesis_keys, MAINNET_GENESIS_CREATION_MILLIS, 1) + ); + assert_eq!(cell(&mut ctx, OLD.1), rlp_number(1000)); + assert_eq!(cell(&mut ctx, storage::OLD_FEDERATION_BTC_UTXOS_KEY), held); + assert_eq!( + cell(&mut ctx, storage::NEW_FEDERATION_BTC_UTXOS_KEY), + vec![0xc0] + ); + for unset in [ + storage::ACTIVE_FEDERATION_CREATION_BLOCK_HEIGHT_KEY, + storage::NEXT_FEDERATION_CREATION_BLOCK_HEIGHT_KEY, + storage::LAST_RETIRED_FEDERATION_P2SH_SCRIPT_KEY, + ] { + assert!(cell(&mut ctx, unset).is_empty(), "{unset}"); + } + voting_cleaned(&mut ctx); +} + +/// rskj `voteCommitFederation_postRSKIP186_preRSKIP419_whenPendingFederationIsSet_shouldPerformLegacyCommitFederationActions`: +/// from RSKIP186 to RSKIP419 (arrowhead631) the handover also keeps the +/// retiring federation's P2SH script and the new federation's creation +/// block as the next one; the federation built is a P2SH ERP one. +#[test] +fn from_rskip186_until_rskip419_a_commit_hands_over_with_the_federation_change_information() { + let (config, cfg) = mainnet(); + assert!(cfg.has_rskip186(ARROWHEAD631) && !cfg.has_rskip419(ARROWHEAD631)); + let keys = members(9); + let (genesis_keys, genesis_address) = genesis(); + let (held, mut ctx) = holding_utxos(ARROWHEAD631); + + assert_eq!(elect(&mut ctx, &config, &cfg, &keys), committed(&config)); + let redeem = p2sh_erp_redeem(&keys, &emergency(), 52_560); + assert_eq!( + logs(&mut ctx), + vec![commit_federation_event( + (&genesis_keys, &genesis_address), + (&keys, &mainnet_p2sh_address(&p2sh_hash(&redeem))), + ARROWHEAD631 + 40_320, + )] + ); + assert_eq!( + cell(&mut ctx, NEW.0), + serialize_federation(&keys, TIMESTAMP, ARROWHEAD631) + ); + assert_eq!(cell(&mut ctx, NEW.1), rlp_number(3000)); + assert_eq!( + cell(&mut ctx, OLD.0), + serialize_federation(&genesis_keys, MAINNET_GENESIS_CREATION_MILLIS, 1) + ); + assert_eq!(cell(&mut ctx, storage::OLD_FEDERATION_BTC_UTXOS_KEY), held); + assert_eq!( + cell(&mut ctx, storage::NEW_FEDERATION_BTC_UTXOS_KEY), + vec![0xc0] + ); + assert_eq!( + cell(&mut ctx, storage::LAST_RETIRED_FEDERATION_P2SH_SCRIPT_KEY), + serialize_script(&p2sh_script(&p2sh_hash(&standard_redeem(&genesis_keys)))) + ); + assert_eq!( + cell(&mut ctx, storage::NEXT_FEDERATION_CREATION_BLOCK_HEIGHT_KEY), + rlp_number(ARROWHEAD631) + ); + assert!( + cell(&mut ctx, PROPOSED.0).is_empty(), + "no proposed federation" + ); + voting_cleaned(&mut ctx); +} + +/// rskj `voteCommitFederation_postRSKIP419_whenPendingFederationIsSet_shouldPerformCommitFederationActions`: +/// from RSKIP419 a commit proposes the federation, created at the block with +/// its timestamp in seconds, and hands nothing over: the federations and +/// their UTXOs stay as they were. The votes are cleaned and +/// `commit_federation` logged. +#[test] +fn from_rskip419_a_commit_proposes_the_federation_and_hands_nothing_over() { + let (config, cfg) = mainnet(); + let keys = members(9); + let (genesis_keys, genesis_address) = genesis(); + let (held, mut ctx) = holding_utxos(VETIVER900); + + assert_eq!(elect(&mut ctx, &config, &cfg, &keys), committed(&config)); + let redeem = p2sh_erp_redeem(&keys, &emergency(), 52_560); + assert_eq!( + logs(&mut ctx), + vec![commit_federation_event( + (&genesis_keys, &genesis_address), + (&keys, &mainnet_p2sh_address(&p2sh_p2wsh_hash(&redeem))), + VETIVER900 + 40_320, + )] + ); + assert_eq!( + cell(&mut ctx, PROPOSED.0), + serialize_federation(&keys, TIMESTAMP * 1000, VETIVER900) + ); + assert_eq!(cell(&mut ctx, PROPOSED.1), rlp_number(4000)); + for untouched in [ + NEW.0, + NEW.1, + OLD.0, + OLD.1, + storage::OLD_FEDERATION_BTC_UTXOS_KEY, + ] { + assert!(cell(&mut ctx, untouched).is_empty(), "{untouched}"); + } + assert_eq!(cell(&mut ctx, storage::NEW_FEDERATION_BTC_UTXOS_KEY), held); + voting_cleaned(&mut ctx); +} diff --git a/crates/execution/src/rskj_tests/bridge_methods/bridge_it.rs b/crates/execution/src/rskj_tests/bridge_methods/bridge_it.rs new file mode 100644 index 0000000..c49665c --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_methods/bridge_it.rs @@ -0,0 +1,1371 @@ +//! rskj's `BridgeIT`: the Bridge on rskj's regtest, from a genesis that has +//! nothing active, with an upgrade's rules switched on where a test switches +//! them on. +//! +//! rskj's tests switch single RSKIPs on (`doReturn(true).when(activationConfig) +//! .isActive(eq(RSKIPxx), anyLong())`). A network switches them on with their +//! upgrade, and so does this port: orchid where a test turns on RSKIP87, 88 +//! or 89, wasabi100 where it turns on RSKIP122, 123 or 124. Where a test turns +//! a later rule on and leaves RSKIP88 off, a combination no network's upgrades +//! make, the port takes the upgrade's, with RSKIP88 on, and the method then +//! throws where rskj's test has it answer null. + +use super::support::*; +use crate::bridge::federation::{ + rsk_address_from_public_key, serialize_federation_multikey, serialize_federation_only_btc_keys, + StoredMember, +}; +use crate::bridge::serialization::{rlp_encode_element, rlp_encode_list, rlp_encode_u64}; +use crate::bridge::storage::{ + bridge_load_bytes_named, bridge_store_bytes_named, NEW_FEDERATION_FORMAT_VERSION_KEY, + NEW_FEDERATION_KEY, OLD_FEDERATION_FORMAT_VERSION_KEY, OLD_FEDERATION_KEY, + PEGOUTS_WAITING_FOR_SIGNATURES_KEY, PENDING_FEDERATION_KEY, + PROPOSED_FEDERATION_FORMAT_VERSION_KEY, PROPOSED_FEDERATION_KEY, +}; +use alloy_primitives::{Address, Bytes, U256}; +use revm::precompile::{PrecompileError, PrecompileOutput}; +use RskNetworkUpgrade::{Genesis, Iris300, Orchid, Vetiver900, Wasabi100}; + +/// At block `number` of rskj's regtest, every upgrade to `last` from block 0. +fn regtest_at(last: RskNetworkUpgrade, number: u64) -> Bridge { + Bridge::new( + up_to(last, RSK_REGTEST_CHAIN_ID), + BridgeConstants::rskj_regtest(), + number, + ) +} + +fn regtest(last: RskNetworkUpgrade) -> Bridge { + regtest_at(last, 0) +} + +/// What rskj's `bridge.execute` answers null with: no output. +fn null( + result: Result, +) -> bool { + matches!(result, Ok(output) if output.bytes.is_empty()) +} + +/// Keys that are points on the curve: mainnet's genesis federation's. +fn keys() -> Vec<[u8; 33]> { + BridgeConstants::mainnet() + .genesis_federation_public_keys + .iter() + .map(|key| { + alloy_primitives::hex::decode(key) + .unwrap() + .try_into() + .unwrap() + }) + .collect() +} + +/// Three members whose BTC, RSK and MST keys differ, from `first` on. +fn members(first: usize) -> Vec { + let keys = keys(); + let mut members: Vec = (first..first + 3) + .map(|i| StoredMember { + btc: keys[i], + rsk: keys[i + 6], + mst: keys[i + 9], + }) + .collect(); + members.sort(); + members +} + +/// A retiring federation of `retiring`, with an active one created after +/// it that the Bridge's block has let take over. Before RSKIP123 rskj keeps +/// a federation's BTC keys only; from it, each member's three keys. +fn store_retiring_federation(bridge: &mut Bridge, retiring: &[StoredMember], multikey: bool) { + let active = members(3); + let serialize = |members: &[StoredMember], time: u64, block: u64| { + if multikey { + serialize_federation_multikey(members, time, block) + } else { + let btc: Vec<[u8; 33]> = members.iter().map(|m| m.btc).collect(); + serialize_federation_only_btc_keys(&btc, time, block) + } + }; + bridge_store_bytes_named( + &mut bridge.ctx, + OLD_FEDERATION_KEY, + &serialize(retiring, 1_000, 1), + ); + bridge_store_bytes_named( + &mut bridge.ctx, + NEW_FEDERATION_KEY, + &serialize(&active, 2_000, 2), + ); +} + +/// A pending federation of `pending`: its BTC keys before RSKIP123, each +/// member's three keys from it. +fn store_pending_federation(bridge: &mut Bridge, pending: &[StoredMember], multikey: bool) { + let items: Vec> = if multikey { + pending.iter().map(StoredMember::to_rlp).collect() + } else { + pending.iter().map(|m| rlp_encode_element(&m.btc)).collect() + }; + bridge_store_bytes_named( + &mut bridge.ctx, + PENDING_FEDERATION_KEY, + &rlp_encode_list(&items), + ); +} + +fn key_of_type(member: &StoredMember, key_type: &str) -> Vec { + match key_type { + "btc" => member.btc.to_vec(), + "rsk" => member.rsk.to_vec(), + _ => member.mst.to_vec(), + } +} + +fn index(signature: &str, index: usize) -> Vec { + call_data(signature, &[int(index as i64)]) +} + +fn index_and_type(signature: &str, index: usize, key_type: &str) -> Vec { + call_data( + signature, + &[int(index as i64), Arg::Bytes(key_type.as_bytes())], + ) +} + +const KEY_TYPES: [&str; 3] = ["btc", "rsk", "mst"]; + +#[test] +fn a_call_too_short_for_a_selector_is_no_call_before_orchid_and_throws_from_it() { + assert!(null(regtest(Genesis).execute(&[0; 3]))); + assert!(threw(®test(Orchid).execute(&[0; 3]))); +} + +#[test] +fn a_selector_of_no_method_is_no_call_before_orchid_and_throws_from_it() { + assert!(null(regtest(Genesis).execute(&[0; 4]))); + assert!(threw(®test(Orchid).execute(&[0; 4]))); +} + +/// rskj's test has a mock answer one hash. The chain here is the one the +/// Bridge starts from on regtest, Bitcoin's regtest genesis block alone, so +/// the locator is its hash. +#[test] +fn the_block_locator_is_the_chains_before_orchid() { + let mut bridge = regtest(Genesis); + let locator = strings_of(&bridge.answer(&selector_only("getBtcBlockchainBlockLocator()"))); + let genesis = bitcoin::constants::genesis_block(bitcoin::Network::Regtest); + assert_eq!(locator, vec![genesis.header.block_hash().to_string()]); +} + +#[test] +fn the_block_locator_is_closed_from_orchid() { + let mut bridge = regtest(Orchid); + bridge.local_call = true; + assert!(threw( + &bridge.execute(&selector_only("getBtcBlockchainBlockLocator()")) + )); +} + +/// rskj's test has a mock answer each index's bytes. Here each index of the +/// genesis federation answers that member's key, and one past them throws. +#[test] +fn each_federators_key_is_answered_before_wasabi100() { + let mut bridge = regtest(Genesis); + let keys = crate::bridge::peg::genesis_federation_keys(&bridge.constants); + let signature = "getFederatorPublicKey(int256)"; + for (i, key) in keys.iter().enumerate() { + assert_eq!(bytes_of(&bridge.answer(&index(signature, i))), key.to_vec()); + } + assert!(threw(&bridge.execute(&index(signature, keys.len())))); +} + +#[test] +fn the_federators_key_is_closed_from_wasabi100() { + let mut bridge = regtest(Wasabi100); + bridge.local_call = true; + assert!(threw( + &bridge.execute(&index("getFederatorPublicKey(int256)", 0)) + )); +} + +#[test] +fn the_federators_key_of_a_type_is_no_call_before_wasabi100() { + let data = index_and_type("getFederatorPublicKeyOfType(int256,string)", 0, "btc"); + assert!(null(regtest(Genesis).execute(&data))); +} + +/// A genesis member has one key for all three types. +#[test] +fn the_federators_key_of_each_type_is_answered_from_wasabi100() { + let mut bridge = regtest(Wasabi100); + bridge.local_call = true; + let first = crate::bridge::peg::genesis_federation_keys(&bridge.constants)[0]; + let signature = "getFederatorPublicKeyOfType(int256,string)"; + for key_type in KEY_TYPES { + let key = bytes_of(&bridge.answer(&index_and_type(signature, 0, key_type))); + assert_eq!(key, first.to_vec()); + } + assert!(threw(&bridge.execute(&index_and_type(signature, 3, "btc")))); +} + +#[test] +fn a_retiring_federators_key_is_answered_before_wasabi100() { + let mut bridge = regtest_at(Genesis, 50); + let retiring = members(0); + store_retiring_federation(&mut bridge, &retiring, false); + let key = bytes_of(&bridge.answer(&index("getRetiringFederatorPublicKey(int256)", 0))); + assert_eq!(key, retiring[0].btc.to_vec()); +} + +#[test] +fn no_retiring_federation_has_an_empty_key_before_wasabi100() { + let mut bridge = regtest_at(Genesis, 50); + let key = bytes_of(&bridge.answer(&index("getRetiringFederatorPublicKey(int256)", 0))); + assert!(key.is_empty()); +} + +#[test] +fn the_retiring_federators_key_is_closed_from_wasabi100() { + let mut bridge = regtest_at(Wasabi100, 50); + bridge.local_call = true; + let data = index("getRetiringFederatorPublicKey(int256)", 10); + assert!(threw(&bridge.execute(&data))); +} + +#[test] +fn the_retiring_federators_key_of_a_type_is_no_call_before_wasabi100() { + let mut bridge = regtest_at(Genesis, 50); + for key_type in KEY_TYPES { + let data = index_and_type( + "getRetiringFederatorPublicKeyOfType(int256,string)", + 10, + key_type, + ); + assert!(null(bridge.execute(&data))); + } +} + +#[test] +fn a_retiring_federators_key_of_each_type_is_answered_from_wasabi100() { + let mut bridge = regtest_at(Wasabi100, 50); + bridge.local_call = true; + let retiring = members(0); + store_retiring_federation(&mut bridge, &retiring, true); + let signature = "getRetiringFederatorPublicKeyOfType(int256,string)"; + for key_type in KEY_TYPES { + let key = bytes_of(&bridge.answer(&index_and_type(signature, 0, key_type))); + assert_eq!(key, key_of_type(&retiring[0], key_type)); + } +} + +#[test] +fn no_retiring_federation_has_an_empty_key_of_each_type_from_wasabi100() { + let mut bridge = regtest_at(Wasabi100, 50); + bridge.local_call = true; + let signature = "getRetiringFederatorPublicKeyOfType(int256,string)"; + for key_type in KEY_TYPES { + assert!(bytes_of(&bridge.answer(&index_and_type(signature, 0, key_type))).is_empty()); + } +} + +/// rskj's test has a mock answer each index's bytes. Here a pending +/// federation is stored, each of its indexes answers its member's key, and +/// one past them throws. +#[test] +fn each_pending_federators_key_is_answered_before_wasabi100() { + let mut bridge = regtest(Genesis); + let pending = members(0); + store_pending_federation(&mut bridge, &pending, false); + let signature = "getPendingFederatorPublicKey(int256)"; + for (i, member) in pending.iter().enumerate() { + assert_eq!( + bytes_of(&bridge.answer(&index(signature, i))), + member.btc.to_vec() + ); + } + assert!(threw(&bridge.execute(&index(signature, pending.len())))); +} + +#[test] +fn the_pending_federators_key_is_closed_from_wasabi100() { + let mut bridge = regtest(Wasabi100); + bridge.local_call = true; + assert!(threw( + &bridge.execute(&index("getPendingFederatorPublicKey(int256)", 10)) + )); +} + +#[test] +fn the_pending_federators_key_of_a_type_is_no_call_before_wasabi100() { + let data = index_and_type( + "getPendingFederatorPublicKeyOfType(int256,string)", + 10, + "btc", + ); + assert!(null(regtest(Genesis).execute(&data))); +} + +#[test] +fn a_pending_federators_key_of_each_type_is_answered_from_wasabi100() { + let mut bridge = regtest(Wasabi100); + bridge.local_call = true; + let pending = members(0); + store_pending_federation(&mut bridge, &pending, true); + let signature = "getPendingFederatorPublicKeyOfType(int256,string)"; + for (i, member) in pending.iter().enumerate() { + for key_type in KEY_TYPES { + let key = bytes_of(&bridge.answer(&index_and_type(signature, i, key_type))); + assert_eq!(key, key_of_type(member, key_type)); + } + } +} + +/// rskj's test has a mock count the vote. The real vote, from no +/// authorizer, is turned down: -10. +#[test] +fn a_federator_public_key_is_voted_before_wasabi100() { + let data = call_data( + "addFederatorPublicKey(bytes)", + &[Arg::Bytes(&[0xaa, 0xbb, 0xcc, 0xdd])], + ); + assert_eq!(int_of(®test(Genesis).answer(&data)), -10); +} + +#[test] +fn a_federator_public_key_is_voted_no_more_from_wasabi100() { + let data = call_data( + "addFederatorPublicKey(bytes)", + &[Arg::Bytes(&[0xaa, 0xbb, 0xcc, 0xdd])], + ); + assert!(threw(®test(Wasabi100).execute(&data))); +} + +fn add_multikey_data() -> Vec { + call_data( + "addFederatorPublicKeyMultikey(bytes,bytes,bytes)", + &[ + Arg::Bytes(&[0xaa, 0xbb]), + Arg::Bytes(&[0xcc, 0xdd]), + Arg::Bytes(&[0xee, 0xff]), + ], + ) +} + +#[test] +fn a_federators_three_keys_are_no_call_before_wasabi100() { + assert!(null(regtest(Genesis).execute(&add_multikey_data()))); +} + +/// rskj's test has a mock count the vote. The real vote, from no +/// authorizer, is turned down: -10. +#[test] +fn a_federators_three_keys_are_voted_from_wasabi100() { + assert_eq!( + int_of(®test(Wasabi100).answer(&add_multikey_data())), + -10 + ); +} + +/// A regtest P2PKH address. +fn regtest_address(tag: u8) -> String { + bitcoin::base58::encode_check(&[vec![111], vec![tag; 20]].concat()) +} + +fn entry_by_address(address: &str) -> Vec { + call_data( + "getLockWhitelistEntryByAddress(string)", + &[Arg::Bytes(address.as_bytes())], + ) +} + +#[test] +fn a_whitelist_entry_is_no_call_before_orchid() { + assert!(null( + regtest(Genesis).execute(&entry_by_address(®test_address(1))) + )); +} + +/// rskj's test has a mock hold an unlimited and a one-off entry. Here the +/// whitelist's authorizer adds them, and they are read back: 0 for the +/// unlimited one, the one-off's value, and -1 for an address not listed. +#[test] +fn whitelist_entries_are_answered_from_orchid() { + let mut bridge = regtest(Orchid); + bridge.caller = address_of_key(bridge.constants.whitelist_authorizer_keys[0]); + let (unlimited, one_off) = (regtest_address(1), regtest_address(2)); + let add_unlimited = call_data( + "addUnlimitedLockWhitelistAddress(string)", + &[Arg::Bytes(unlimited.as_bytes())], + ); + assert_eq!(int_of(&bridge.answer(&add_unlimited)), 1); + let add_one_off = call_data( + "addOneOffLockWhitelistAddress(string,int256)", + &[Arg::Bytes(one_off.as_bytes()), int(100_000_000)], + ); + assert_eq!(int_of(&bridge.answer(&add_one_off)), 1); + + bridge.local_call = true; + assert_eq!(int_of(&bridge.answer(&entry_by_address(&unlimited))), 0); + assert_eq!( + int_of(&bridge.answer(&entry_by_address(&one_off))), + 100_000_000 + ); + assert_eq!( + int_of(&bridge.answer(&entry_by_address(®test_address(3)))), + -1 + ); +} + +fn add_lock_whitelist_data(address: &str) -> Vec { + call_data( + "addLockWhitelistAddress(string,int256)", + &[Arg::Bytes(address.as_bytes()), int(100_000_000)], + ) +} + +/// From a federator, who is no authorizer of the whitelist's: -10. +#[test] +fn a_whitelist_address_from_no_authorizer_is_turned_down_before_orchid() { + let mut bridge = regtest(Genesis); + bridge.caller = genesis_federator(&bridge.constants); + assert_eq!( + int_of(&bridge.answer(&add_lock_whitelist_data(®test_address(1)))), + -10 + ); +} + +#[test] +fn add_lock_whitelist_address_is_closed_from_orchid() { + assert!(threw( + ®test(Orchid).execute(&add_lock_whitelist_data("i-am-an-address")) + )); +} + +fn add_one_off_data(address: &str) -> Vec { + call_data( + "addOneOffLockWhitelistAddress(string,int256)", + &[Arg::Bytes(address.as_bytes()), int(100_000_000)], + ) +} + +#[test] +fn a_one_off_whitelist_address_is_no_call_before_orchid() { + assert!(null( + regtest(Genesis).execute(&add_one_off_data("i-am-an-address")) + )); +} + +/// From a federator, who is no authorizer of the whitelist's: -10. +#[test] +fn a_one_off_whitelist_address_from_no_authorizer_is_turned_down_from_orchid() { + let mut bridge = regtest(Orchid); + bridge.caller = genesis_federator(&bridge.constants); + assert_eq!( + int_of(&bridge.answer(&add_one_off_data(®test_address(1)))), + -10 + ); +} + +fn add_unlimited_data(address: &str) -> Vec { + call_data( + "addUnlimitedLockWhitelistAddress(string)", + &[Arg::Bytes(address.as_bytes())], + ) +} + +#[test] +fn an_unlimited_whitelist_address_is_no_call_before_orchid() { + assert!(null( + regtest(Genesis).execute(&add_unlimited_data("i-am-an-address")) + )); +} + +/// From a federator, who is no authorizer of the whitelist's: -10. +#[test] +fn an_unlimited_whitelist_address_from_no_authorizer_is_turned_down_from_orchid() { + let mut bridge = regtest(Orchid); + bridge.caller = genesis_federator(&bridge.constants); + assert_eq!( + int_of(&bridge.answer(&add_unlimited_data(®test_address(1)))), + -10 + ); +} + +/// rskj's test replays the data of mainnet block 457's first federator +/// call, kept in a file of rskj's: `receiveHeaders` with five hundred +/// elements of 81 bytes, each a header and a count of no transactions, +/// 80,068 bytes in all. It checks that the call decodes and runs before +/// orchid without asking whether it is a local call. The call here has that +/// shape, from a federator, not a local call. +#[test] +fn a_call_of_five_hundred_headers_decodes_and_runs_before_orchid() { + let mut bridge = regtest(Genesis); + bridge.caller = genesis_federator(&bridge.constants); + let headers: Vec> = (0..500u32) + .map(|i| [i.to_le_bytes().to_vec(), vec![0; 77]].concat()) + .collect(); + let headers: Vec<&[u8]> = headers.iter().map(Vec::as_slice).collect(); + let data = call_data("receiveHeaders(bytes[])", &[Arg::BytesArray(&headers)]); + assert_eq!(data.len(), 80_068); + assert!(null(bridge.execute(&data))); +} + +#[test] +fn a_local_only_method_takes_a_transaction_before_orchid() { + let mut bridge = regtest(Genesis); + let address = string_of(&bridge.answer(&selector_only("getFederationAddress()"))); + assert!( + address.starts_with('2'), + "a regtest P2SH address: {address}" + ); +} + +#[test] +fn a_local_only_method_throws_for_a_transaction_from_orchid() { + let mut bridge = regtest(Orchid); + assert!(threw( + &bridge.execute(&selector_only("getFederationAddress()")) + )); +} + +#[test] +fn btc_transaction_confirmations_are_no_call_before_wasabi100() { + let data = call_data( + "getBtcTransactionConfirmations(bytes32,bytes32,uint256,bytes32[])", + &[ + Arg::Word([0; 32]), + Arg::Word([0; 32]), + int(1), + Arg::Words(&[[0; 32]]), + ], + ); + assert!(null(regtest(Genesis).execute(&data))); +} + +/// What `getGasForData` charges for a call: its method's cost and two gas a +/// byte (`Bridge.getGasForData`). +fn gas_for(bridge: &mut Bridge, data: &[u8]) -> u64 { + crate::bridge::bridge_call_gas_cost(&mut bridge.ctx, data, &bridge.constants, &bridge.cfg) + .unwrap() +} + +/// rskj's test has a mock cost the lookup 1,234. The real one costs 27,000 +/// for a block the chain doesn't have, and for one it has, 315 more for +/// each block above it and 144 for each hash of the branch. +#[test] +fn the_gas_for_btc_transaction_confirmations_is_two_a_byte_and_the_lookups() { + use sha2::Digest; + let hash = |data: &[u8]| -> [u8; 32] { sha2::Sha256::digest(data).into() }; + let branch = [hash(&[0x11]), hash(&[0x22]), hash(&[0x33])]; + let signature = "getBtcTransactionConfirmations(bytes32,bytes32,uint256,bytes32[])"; + let mut bridge = regtest(Wasabi100); + + let data = call_data( + signature, + &[ + Arg::Word(hash(&[0xaa, 0xbb, 0xcc])), + Arg::Word(hash(&[0xdd, 0xee, 0xff])), + int(123), + Arg::Words(&branch), + ], + ); + assert_eq!(gas_for(&mut bridge, &data), 2 * data.len() as u64 + 27_000); + + let genesis = bitcoin::constants::genesis_block(bitcoin::Network::Regtest); + let genesis_hash = crate::bridge::btc_chain::bitcoin_hash_to_b256(&genesis.header.block_hash()); + let data = call_data( + signature, + &[ + Arg::Word(hash(&[0xaa, 0xbb, 0xcc])), + Arg::Word(genesis_hash.0), + int(123), + Arg::Words(&branch), + ], + ); + assert_eq!( + gas_for(&mut bridge, &data), + 2 * data.len() as u64 + 27_000 + 3 * 144 + ); +} + +/// A transaction to a contract that DELEGATECALLs the Bridge's +/// `getMinimumLockTxValue`, as rskj's test assembles it, forwarding 0x6000 +/// gas, on rskj's regtest at block 1 of `last`. Whether it ended well and +/// the gas it used. +fn a_contract_calls_get_minimum_lock_tx_value(last: RskNetworkUpgrade) -> (bool, u64) { + use alloy_primitives::{Bloom, B256}; + use rustock_trie::{ + account_key, code_key, AccountState, MemoryTrieStore, TrieKeySlice, TrieNode, TrieStore, + }; + let selector = selector_only("getMinimumLockTxValue()"); + let mut code = vec![0x7f]; + code.extend_from_slice(&[selector, vec![0; 28]].concat()); + code.extend_from_slice(&[0x60, 0x00, 0x52]); // MSTORE at 0 + code.extend_from_slice(&[0x60, 0x20, 0x60, 0x30, 0x60, 0x20, 0x60, 0x00]); // out, in + code.push(0x73); + code.extend_from_slice(crate::precompiles::BRIDGE_ADDR.as_slice()); + code.extend_from_slice(&[0x61, 0x60, 0x00, 0xf4, 0x00]); // gas, DELEGATECALL, STOP + + let (sender, contract) = (Address::repeat_byte(0xaa), Address::repeat_byte(0xbb)); + let store = std::sync::Arc::new(MemoryTrieStore::new()); + let put = |root: &TrieNode, address: &Address, nonce: u64, balance: U256| { + let key = account_key(address); + let account = AccountState::new(U256::from(nonce), balance); + root.put( + &TrieKeySlice::from_key(&key), + &account.encode(), + store.as_ref() as &dyn TrieStore, + ) + }; + let root = put( + &TrieNode::empty(), + &sender, + 0, + U256::from(10u64).pow(U256::from(18)), + ); + let root = put(&root, &contract, 1, U256::ZERO); + let root = root.put( + &TrieKeySlice::from_key(&code_key(&contract)), + &code, + store.as_ref(), + ); + + let number = 1; + let header = rustock_core::Header { + parent_hash: B256::ZERO, + ommers_hash: B256::ZERO, + beneficiary: Address::repeat_byte(0x01), + state_root: B256::ZERO, + transactions_root: B256::ZERO, + receipts_root: B256::ZERO, + logs_bloom: Bloom::ZERO, + extension_data: None, + difficulty: U256::from(1_000_000), + number, + gas_limit: U256::from(6_800_000), + gas_used: 0, + timestamp: 1_700_000_000, + extra_data: Bytes::new(), + paid_fees: U256::ZERO, + minimum_gas_price: U256::ZERO, + uncle_count: 0, + 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, + bitcoin_merged_mining_coinbase_transaction: None, + cached_hash: None, + cached_hash_for_merged_mining: None, + kept: None, + }; + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::ZERO, + gas_limit: U256::from(1_000_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 blocks = rustock_storage::BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap(); + let executor = crate::executor::RskExecutor::new( + up_to(last, RSK_REGTEST_CHAIN_ID), + std::sync::Arc::new(blocks), + ); + let result = executor + .execute_block(&header, &[(tx, sender)], &root, store) + .unwrap(); + (result.tx_results[0].success, result.tx_results[0].gas_used) +} + +/// Before orchid the Bridge can't price a contract's call +/// (`getGasForData` reads a transaction it hasn't got), and from orchid +/// it throws for a contract's call of a local-only method. Before RSKIP197 +/// (iris300) either leaves the VM and fails the contract with all its gas; +/// from it the call fails and the contract goes on. +#[test] +fn a_contracts_call_of_a_local_only_method_fails_the_contract_before_iris300() { + assert_eq!( + a_contract_calls_get_minimum_lock_tx_value(Genesis), + (false, 1_000_000) + ); + assert_eq!( + a_contract_calls_get_minimum_lock_tx_value(Orchid), + (false, 1_000_000) + ); + let (ended_well, gas) = a_contract_calls_get_minimum_lock_tx_value(Iris300); + assert!(ended_well && gas < 1_000_000, "{gas}"); +} + +fn headers_data(count: usize) -> Vec { + let header = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99]; + let headers: Vec<&[u8]> = vec![&header[..]; count]; + call_data("receiveHeaders(bytes[])", &[Arg::BytesArray(&headers)]) +} + +#[test] +fn receive_headers_costs_22000_and_two_a_byte_before_wasabi100() { + let mut bridge = regtest(Genesis); + for count in 0..10 { + let data = headers_data(count); + assert_eq!(gas_for(&mut bridge, &data), 22_000 + 2 * data.len() as u64); + } +} + +/// RSKIP124 without RSKIP132 is testnet's first blocks: wasabi100 from block +/// 0, RSKIP132 from 43,550. +#[test] +fn receive_headers_costs_66000_and_1650_a_header_past_the_first_with_rskip124() { + let mut bridge = Bridge::new(RskHardforkConfig::testnet(), BridgeConstants::testnet(), 0); + assert!(bridge.cfg.has_rskip124(0) && !bridge.cfg.has_rskip132(0)); + for count in 0..10u64 { + let data = headers_data(count as usize); + let per_header = 1_650 * count.saturating_sub(1); + assert_eq!( + gas_for(&mut bridge, &data), + 66_000 + 2 * data.len() as u64 + per_header + ); + } +} + +fn access_headers_data() -> Vec { + let header = alloy_primitives::hex::decode( + "0000002006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910ff698ee112158f5573a90b7403cfba074addd61c547b3639c6afdcf52588eb8e2a1ef825cffff7f2000000000", + ) + .unwrap(); + call_data("receiveHeaders(bytes[])", &[Arg::BytesArray(&[&header])]) +} + +/// The header as rskj's test sends it, and nothing but the selector, as +/// `receiveHeadersNotFromTheFederation` sends it. +#[test] +fn receive_headers_turns_a_stranger_down_before_wasabi100() { + let mut bridge = regtest(Genesis); + bridge.caller = Address::from_slice( + &alloy_primitives::hex::decode("2acc95758f8b5f583470ba265eb685a8f45fc9d5").unwrap(), + ); + assert!(threw(&bridge.execute(&access_headers_data()))); + assert!(threw( + &bridge.execute(&selector_only("receiveHeaders(bytes[])")) + )); +} + +#[test] +fn receive_headers_takes_a_federator_before_wasabi100() { + let mut bridge = regtest(Genesis); + bridge.caller = genesis_federator(&bridge.constants); + assert!(null(bridge.execute(&access_headers_data()))); +} + +#[test] +fn receive_headers_takes_anyone_from_wasabi100() { + assert!(null(regtest(Wasabi100).execute(&access_headers_data()))); +} + +/// rskj's `getGasForData` tests, from a genesis with nothing active: a parse +/// failure costs releaseBtc's 23,000 and nothing a byte; a method, its cost +/// and two gas a byte. +#[test] +fn the_gas_for_a_call_is_its_methods_cost_and_two_a_byte() { + let mut bridge = regtest(Genesis); + assert_eq!(gas_for(&mut bridge, &[1, 2, 3]), 23_000); + for (signature, cost) in [ + ("updateCollections()", 48_000), + ("receiveHeaders(bytes[])", 22_000), + ("releaseBtc()", 23_000), + ("getStateForBtcReleaseClient()", 4_000), + ("getStateForDebugging()", 3_000_000), + ("getBtcBlockchainBestChainHeight()", 19_000), + ("getBtcBlockchainBlockLocator()", 76_000), + ("getFederationAddress()", 11_000), + ("getMinimumLockTxValue()", 2_000), + ] { + assert_eq!( + gas_for(&mut bridge, &selector_only(signature)), + cost + 8, + "{signature}" + ); + } + let register = call_data( + "registerBtcTransaction(bytes,int256,bytes)", + &[Arg::Bytes(&[0; 3]), int(1), Arg::Bytes(&[0; 3])], + ); + assert_eq!(register.len(), 228); + assert_eq!(gas_for(&mut bridge, ®ister), 22_000 + 228 * 2); + let add_signature = call_data( + "addSignature(bytes,bytes[],bytes)", + &[ + Arg::Bytes(&[0; 3]), + Arg::BytesArray(&[&[0; 2], &[0; 2], &[0; 2]]), + Arg::Bytes(&[0; 3]), + ], + ); + assert_eq!(add_signature.len(), 548); + assert_eq!(gas_for(&mut bridge, &add_signature), 70_000 + 548 * 2); +} + +/// rskj's `localCallOnlyMethodsDefinition` and `mineableMethodsDefinition`. +#[test] +fn the_local_only_methods_and_the_mineable_ones_are_rskjs() { + use crate::bridge::BridgePermission::LocalOnly; + let permission = |signature: &str| { + let selector = crate::abi::selector(signature); + crate::bridge::find_bridge_method(&selector) + .unwrap() + .permission + }; + for signature in [ + "getBtcBlockchainInitialBlockHeight()", + "getBtcBlockchainBlockLocator()", + "getBtcBlockchainBlockHashAtDepth(int256)", + "getBtcTxHashProcessedHeight(string)", + "getFederationAddress()", + "getFederationCreationBlockNumber()", + "getFederationCreationTime()", + "getFederationSize()", + "getFederationThreshold()", + "getFederatorPublicKey(int256)", + "getFeePerKb()", + "getLockWhitelistAddress(int256)", + "getLockWhitelistEntryByAddress(string)", + "getLockWhitelistSize()", + "getMinimumLockTxValue()", + "getPendingFederationHash()", + "getPendingFederationSize()", + "getPendingFederatorPublicKey(int256)", + "getRetiringFederationAddress()", + "getRetiringFederationCreationBlockNumber()", + "getRetiringFederationCreationTime()", + "getRetiringFederationSize()", + "getRetiringFederationThreshold()", + "getRetiringFederatorPublicKey(int256)", + "getStateForBtcReleaseClient()", + "getStateForDebugging()", + "isBtcTxHashAlreadyProcessed(string)", + ] { + assert_eq!(permission(signature), LocalOnly, "{signature}"); + } + for signature in [ + "addFederatorPublicKey(bytes)", + "addLockWhitelistAddress(string,int256)", + "addOneOffLockWhitelistAddress(string,int256)", + "addUnlimitedLockWhitelistAddress(string)", + "addSignature(bytes,bytes[],bytes)", + "commitFederation(bytes)", + "createFederation()", + "receiveHeaders(bytes[])", + "registerBtcTransaction(bytes,int256,bytes)", + "releaseBtc()", + "removeLockWhitelistAddress(string)", + "rollbackFederation()", + "setLockWhitelistDisableBlockDelay(int256)", + "updateCollections()", + "voteFeePerKbChange(int256)", + "getActiveFederationCreationBlockHeight()", + ] { + assert_ne!(permission(signature), LocalOnly, "{signature}"); + } +} + +/// rskj's `callUpdateCollectionsWithSignatureNotFromFederation`: before +/// orchid, `updateCollections` from anyone outside the federations throws, +/// and a federator's runs. rskj also reads the exception's message; a throw +/// here carries none. +#[test] +fn update_collections_throws_for_a_caller_outside_the_federations() { + let mut bridge = regtest(Genesis); + assert!(threw( + &bridge.execute(&selector_only("updateCollections()")) + )); + bridge.caller = genesis_federator(&bridge.constants); + assert!(null(bridge.execute(&selector_only("updateCollections()")))); +} + +/// A Bitcoin transaction of one input and one output of `value` satoshis. +fn btc_tx(tag: u8, value: u64) -> Vec { + use bitcoin::hashes::Hash; + bitcoin::consensus::serialize(&bitcoin::Transaction { + version: bitcoin::transaction::Version::ONE, + lock_time: bitcoin::absolute::LockTime::ZERO, + input: vec![bitcoin::TxIn { + previous_output: bitcoin::OutPoint { + txid: bitcoin::Txid::from_byte_array([tag; 32]), + vout: 0, + }, + script_sig: bitcoin::ScriptBuf::new(), + sequence: bitcoin::Sequence::MAX, + witness: bitcoin::Witness::new(), + }], + output: vec![bitcoin::TxOut { + value: bitcoin::Amount::from_sat(value), + script_pubkey: bitcoin::ScriptBuf::new_p2pkh(&bitcoin::PubkeyHash::from_byte_array( + [tag; 20], + )), + }], + }) +} + +/// rskj's `callUpdateCollectionsWithTransactionsWaitingForConfirmation` and +/// `…WithEnoughConfirmations`: peg-outs made at blocks 1, 2 and 3 wait for +/// regtest's three confirmations. At block 0 none has them; at block 10 all +/// have, and a federator's call moves one on to be signed. The limit +/// between: the one made at block 1 has its three at block 4. +#[test] +fn update_collections_moves_one_confirmed_pegout_on_to_be_signed() { + use crate::bridge::peg::{ + deserialize_rsk_txs_waiting_for_signatures, load_pegout_confirmation_set, + store_pegout_confirmation_set, PegoutWaitingForConfirmations, + }; + let minimum = BridgeConstants::rskj_regtest().minimum_pegout_tx_value; + let after_a_call_at = |number: u64| { + let mut bridge = regtest_at(Genesis, number); + let pegouts: Vec = (1..=3u8) + .map(|made_at| PegoutWaitingForConfirmations { + btc_tx_raw: btc_tx(made_at + 1, minimum + 100_000 * u64::from(made_at - 1)), + rsk_block_height: u64::from(made_at), + rsk_tx_hash: None, + }) + .collect(); + store_pegout_confirmation_set(&mut bridge.ctx, &pegouts, false); + bridge.caller = genesis_federator(&bridge.constants); + assert!(null(bridge.execute(&selector_only("updateCollections()")))); + let waiting = load_pegout_confirmation_set(&mut bridge.ctx, false).len(); + let signing = bridge_load_bytes_named(&mut bridge.ctx, PEGOUTS_WAITING_FOR_SIGNATURES_KEY); + ( + waiting, + deserialize_rsk_txs_waiting_for_signatures(&signing).len(), + ) + }; + assert_eq!(after_a_call_at(0), (3, 0)); + assert_eq!(after_a_call_at(10), (2, 1)); + assert_eq!(after_a_call_at(3), (3, 0)); + assert_eq!(after_a_call_at(4), (2, 1)); +} + +/// rskj's `getPegoutWaitingForSignatureKey`: a confirmed peg-out waits to be +/// signed under the transaction that made it from RSKIP146, under the call +/// that moves it on from RSKIP176 ("moving back to using the +/// updateCollections related txHash"), and under the one that made it again +/// from RSKIP375. +#[test] +fn from_rskip176_a_confirmed_pegout_waits_to_be_signed_under_the_call_that_moves_it() { + use crate::bridge::peg::{ + deserialize_rsk_txs_waiting_for_signatures, store_pegout_confirmation_set, + PegoutWaitingForConfirmations, + }; + let made_by = [0x55u8; 32]; + let key_after_a_call_under = |last: RskNetworkUpgrade| { + let mut bridge = regtest_at(last, 10); + let minimum = bridge.constants.minimum_pegout_tx_value; + let pegout = PegoutWaitingForConfirmations { + btc_tx_raw: btc_tx(2, minimum), + rsk_block_height: 1, + rsk_tx_hash: Some(made_by), + }; + store_pegout_confirmation_set(&mut bridge.ctx, &[pegout], true); + bridge.caller = genesis_federator(&bridge.constants); + assert!(null(bridge.execute(&selector_only("updateCollections()")))); + let signing = bridge_load_bytes_named(&mut bridge.ctx, PEGOUTS_WAITING_FOR_SIGNATURES_KEY); + let keys: Vec<[u8; 32]> = deserialize_rsk_txs_waiting_for_signatures(&signing) + .into_keys() + .collect(); + keys + }; + // The call's own hash is the default context's, all zeros. + assert_eq!( + key_after_a_call_under(RskNetworkUpgrade::Papyrus200), + vec![made_by] + ); + assert_eq!(key_after_a_call_under(Iris300), vec![[0u8; 32]]); + assert_eq!( + key_after_a_call_under(RskNetworkUpgrade::Hop400), + vec![[0u8; 32]] + ); + assert_eq!(key_after_a_call_under(Vetiver900), vec![made_by]); +} + +/// A header of Bitcoin's regtest on its genesis block, mined to regtest's +/// easiest target. +fn header_on_genesis() -> Vec { + use bitcoin::hashes::Hash; + let genesis = bitcoin::constants::genesis_block(bitcoin::Network::Regtest); + let header = (0..) + .map(|nonce| bitcoin::block::Header { + version: bitcoin::block::Version::from_consensus(1), + prev_blockhash: genesis.block_hash(), + merkle_root: bitcoin::TxMerkleNode::from_byte_array([0x11; 32]), + time: 1_700_000_000, + bits: bitcoin::CompactTarget::from_consensus(0x207f_ffff), + nonce, + }) + .find(|header| header.validate_pow(header.target()).is_ok()) + .unwrap(); + bitcoin::consensus::serialize(&header) +} + +fn one_header(header: &[u8]) -> Vec { + call_data("receiveHeaders(bytes[])", &[Arg::BytesArray(&[header])]) +} + +/// What a federator's call answers before orchid, and the height of the +/// Bridge's chain after it. +fn receive_before_orchid(data: &[u8]) -> (bool, i64) { + let mut bridge = regtest(Genesis); + bridge.caller = genesis_federator(&bridge.constants); + let answered_null = null(bridge.execute(data)); + let height = selector_only("getBtcBlockchainBestChainHeight()"); + (answered_null, int_of(&bridge.answer(&height))) +} + +/// rskj's `receiveHeadersWithCorrectSizeHeaders` and the three tests that +/// hand it what isn't a header: before RSKIP124 a header is its 80 bytes, +/// and may be followed by a count of no transactions up to 85 bytes in all. +/// Anything else is no call before orchid, and the chain takes nothing from +/// it: rskj's 60 bytes, its count of `Integer.MAX_VALUE` transactions, its +/// whole block of two transactions, and one byte short of or past the +/// limits. +#[test] +fn before_wasabi100_a_header_is_80_to_85_bytes_with_no_transactions() { + let header = header_on_genesis(); + let with = |tail: &[u8]| [header.as_slice(), tail].concat(); + for taken in [header.clone(), with(&[0]), with(&[0xfe, 0, 0, 0, 0])] { + let size = taken.len(); + assert_eq!( + receive_before_orchid(&one_header(&taken)), + (true, 1), + "{size} bytes" + ); + } + let block = [ + header.as_slice(), + &[2], + &btc_tx(0x01, 5_000_000_000), + &btc_tx(0x02, 100_000_000), + ] + .concat(); + for refused in [ + header[..60].to_vec(), + header[..79].to_vec(), + with(&[0xfe, 0xff, 0xff, 0xff, 0x7f]), + with(&[0xfe, 0, 0, 0, 0, 0]), + block, + ] { + let size = refused.len(); + assert_eq!( + receive_before_orchid(&one_header(&refused)), + (true, 0), + "{size} bytes" + ); + } +} + +/// rskj's `sendNoBlockHeader`: `receiveHeaders` with nothing but its +/// selector reads as no headers (rskj decodes an empty payload's array as +/// empty), runs, and receives none, before orchid and from it. +#[test] +fn receive_headers_with_nothing_but_its_selector_receives_no_header() { + let data = selector_only("receiveHeaders(bytes[])"); + assert_eq!(receive_before_orchid(&data), (true, 0)); + let mut bridge = regtest(Orchid); + bridge.caller = genesis_federator(&bridge.constants); + assert!(null(bridge.execute(&data))); +} + +/// rskj's `sendOrphanBlockHeader`: a header whose parent the chain hasn't +/// got leaves the chain's height as it was. +#[test] +fn an_orphan_header_leaves_the_chain_as_it_was() { + let orphan = [ + 1u32.to_le_bytes().to_vec(), + vec![0x01; 32], + vec![0x02; 32], + 1u32.to_le_bytes().to_vec(), + 0x207f_ffffu32.to_le_bytes().to_vec(), + 1u32.to_le_bytes().to_vec(), + ] + .concat(); + assert_eq!(receive_before_orchid(&one_header(&orphan)), (true, 0)); +} + +/// rskj's `registerBtcTransactionNotFromFederation`: before iris300 a caller +/// outside the federations throws. +#[test] +fn register_btc_transaction_throws_for_a_caller_outside_the_federations() { + let data = call_data( + "registerBtcTransaction(bytes,int256,bytes)", + &[Arg::Bytes(&[0; 3]), int(1), Arg::Bytes(&[0; 30])], + ); + assert!(threw(®test(Genesis).execute(&data))); +} + +/// rskj's `HugeDeclaredSizeBtcTransaction`: a transaction of one input, one +/// output and a one-byte witness push, written with one of its four counts, +/// `huge`, as `Integer.MAX_VALUE`. +fn huge_declared_size(huge: usize) -> Vec { + let count = |which: usize| { + if which == huge { + vec![0xfe, 0xff, 0xff, 0xff, 0x7f] + } else { + vec![1] + } + }; + [ + vec![1, 0, 0, 0, 0, 1], + count(0), + vec![0x11; 32], + vec![0; 5], + vec![0xff; 4], + count(1), + 100_000_000u64.to_le_bytes().to_vec(), + vec![0x19, 0x76, 0xa9, 0x14], + vec![0x22; 20], + vec![0x88, 0xac], + count(2), + count(3), + vec![0], + vec![0; 4], + ] + .concat() +} + +/// rskj's partial merkle tree of three transactions that declares +/// `Integer.MAX_VALUE` hashes and holds three. +fn huge_declared_size_pmt() -> Vec { + let hash = |n: u8| [vec![n], vec![0; 31]].concat(); + [ + 3u32.to_le_bytes().to_vec(), + vec![0xfe, 0xff, 0xff, 0xff, 0x7f], + hash(1), + hash(2), + hash(3), + vec![1, 0x3f], + ] + .concat() +} + +/// rskj's `registerBtcTransactionWithNonParseableTx`, its four +/// `…WithHugeDeclared…Size` tests, its two `…WithNonParseableMerkleeProof` +/// tests and `…WithHugeDeclaredSizeMerkleeProof`: before orchid, a +/// federator's call with a transaction or a proof the Bridge can't read +/// answers nothing. rskj reads neither: on a chain at regtest's genesis, +/// height 1 has no confirmations, which it asks first. +#[test] +fn register_btc_transaction_answers_nothing_for_bytes_it_cannot_read_before_orchid() { + let register = |tx: &[u8], pmt: &[u8]| { + let mut bridge = regtest(Genesis); + bridge.caller = genesis_federator(&bridge.constants); + let data = call_data( + "registerBtcTransaction(bytes,int256,bytes)", + &[Arg::Bytes(tx), int(1), Arg::Bytes(pmt)], + ); + null(bridge.execute(&data)) + }; + assert!(register(&[0; 3], &[0; 30])); + for huge in 0..4 { + assert!( + register(&huge_declared_size(huge), &[0; 30]), + "count {huge}" + ); + } + let tx = btc_tx(0x33, 100_000_000); + for pmt in [vec![0; 3], vec![0; 30], huge_declared_size_pmt()] { + assert!(register(&tx, &pmt)); + } +} + +/// A DER signature: r and s of 1. +const DER: &[u8] = &[0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01]; + +fn add_signature_data(key: &[u8], signatures: &[&[u8]], hash: &[u8]) -> Vec { + call_data( + "addSignature(bytes,bytes[],bytes)", + &[ + Arg::Bytes(key), + Arg::BytesArray(signatures), + Arg::Bytes(hash), + ], + ) +} + +/// rskj's `addSignatureWithNonParseablePublicKey`, `…WithEmptySignatureArray`, +/// `…WithNonParseableSignature` and `…WithNonParseableRskTx`: from a +/// federator before orchid, a key bitcoinj can't read, no signatures, a +/// signature that isn't DER and a hash that isn't 32 bytes are no call. +/// From orchid each throws, while the call with all four sound runs. +#[test] +fn add_signature_is_no_call_for_what_it_cannot_read_before_orchid() { + let key = crate::bridge::peg::genesis_federation_keys(&BridgeConstants::rskj_regtest())[0]; + let from_a_federator = |upgrade: RskNetworkUpgrade, data: &[u8]| { + let mut bridge = regtest(upgrade); + bridge.caller = genesis_federator(&bridge.constants); + bridge.execute(data) + }; + let unread: [(&[u8], &[&[u8]], &[u8]); 4] = [ + (&[0; 3], &[], &[0; 32]), + (&key, &[], &[0; 32]), + (&key, &[&[0; 3]], &[0; 32]), + (&key, &[DER], &[0; 3]), + ]; + for (key, signatures, hash) in unread { + let data = add_signature_data(key, signatures, hash); + assert!(null(from_a_federator(Genesis, &data))); + assert!(threw(&from_a_federator(Orchid, &data))); + } + let sound = add_signature_data(&key, &[DER], &[0; 32]); + assert!(null(from_a_federator(Orchid, &sound))); +} + +/// rskj's `isBtcTxHashAlreadyProcessed_exception` and +/// `getBtcTxHashProcessedHeight_exception`: a hash that isn't 64 hex digits +/// throws, before orchid and from it. One that is gets its answer. +#[test] +fn a_btc_tx_hash_that_is_not_one_throws() { + let hash = "0000000000000000000000000000000000000000000000000000000000000001"; + for signature in [ + "isBtcTxHashAlreadyProcessed(string)", + "getBtcTxHashProcessedHeight(string)", + ] { + let data = |text: &str| call_data(signature, &[Arg::Bytes(text.as_bytes())]); + for upgrade in [Genesis, Orchid] { + let mut bridge = regtest(upgrade); + bridge.local_call = true; + assert!( + threw(&bridge.execute(&data("notahash"))), + "{signature} at {upgrade:?}" + ); + assert_eq!( + bridge.answer(&data(hash)).len(), + 32, + "{signature} at {upgrade:?}" + ); + } + } +} + +/// rskj's `getBtcTransactionConfirmationsAfterWasabi_merkleBranchConstructionError`, +/// without its mock: a path with more significant bits than the branch has +/// hashes can't make rskj's `MerkleBranch`, and the call throws. With as +/// many bits as hashes it answers, here that the block is unknown: -1. +#[test] +fn confirmations_throw_for_a_path_longer_than_its_branch() { + let data = |path: i64| { + call_data( + "getBtcTransactionConfirmations(bytes32,bytes32,uint256,bytes32[])", + &[ + Arg::Word([0xaa; 32]), + Arg::Word([0xdd; 32]), + int(path), + Arg::Words(&[[0x11; 32], [0x22; 32], [0x33; 32]]), + ], + ) + }; + let mut bridge = regtest(Wasabi100); + assert!(threw(&bridge.execute(&data(123)))); + assert!(threw(&bridge.execute(&data(0b1000)))); + assert_eq!(int_of(&bridge.answer(&data(0b111))), -1); +} + +/// The nine keys rskj's `addSignature` tests make from `1` to +/// `9`, sorted as it sorts them. +fn seeded_keys(seed: &str) -> Vec<[u8; 33]> { + let mut keys: Vec<[u8; 33]> = (1..=9) + .map(|i| key_from_seed(&format!("{seed}{i}"))) + .collect(); + keys.sort(); + keys +} + +/// Where a federation is stored, where its format is, and the seed of its +/// members' keys. +type Stored = (&'static str, &'static str, &'static str); +const NEW: Stored = ( + NEW_FEDERATION_KEY, + NEW_FEDERATION_FORMAT_VERSION_KEY, + "activeFedMember", +); +const OLD: Stored = ( + OLD_FEDERATION_KEY, + OLD_FEDERATION_FORMAT_VERSION_KEY, + "retiringFedMember", +); +const PROPOSED: Stored = ( + PROPOSED_FEDERATION_KEY, + PROPOSED_FEDERATION_FORMAT_VERSION_KEY, + "proposedFedMember", +); + +/// Mainnet at `number` with every upgrade, holding `federations` as rskj's +/// `P2shErpFederationBuilder` builds them: created at block 1, in the P2SH +/// ERP format. +fn mainnet_with(number: u64, federations: &[Stored]) -> Bridge { + let cfg = up_to(Vetiver900, RSK_MAINNET_CHAIN_ID); + let mut bridge = Bridge::new(cfg, BridgeConstants::mainnet(), number); + for (key, format_key, seed) in federations { + let members: Vec = seeded_keys(seed) + .into_iter() + .map(StoredMember::from_btc) + .collect(); + let stored = serialize_federation_multikey(&members, 100_000_000_000, 1); + bridge_store_bytes_named(&mut bridge.ctx, format_key, &rlp_encode_u64(3000)); + bridge_store_bytes_named(&mut bridge.ctx, key, &stored); + } + bridge +} + +/// `addSignature` from the holder of `key`, signing with it. +fn add_signature_from( + bridge: &mut Bridge, + key: [u8; 33], +) -> Result { + bridge.caller = rsk_address_from_public_key(&key).unwrap(); + bridge.execute(&add_signature_data(&key, &[DER], &[0; 32])) +} + +/// Whether the first member of the federation from `seed` may sign: the +/// method runs, or it throws. +fn signs(bridge: &mut Bridge, seed: &str) -> bool { + let result = add_signature_from(bridge, seeded_keys(seed)[0]); + assert!(result.is_ok() || threw(&result), "{result:?}"); + result.is_ok() +} + +/// rskj's four `addSignature_…whenNewAndOldFederations…` tests: the new +/// federation, made at block 1, is active from mainnet's activation age +/// later, 40,321. From there its members and the old one's may sign; a block +/// before, only the old one's. +#[test] +fn add_signature_takes_the_new_federation_once_active_and_the_old_one_either_side() { + for (number, new_signs) in [(40_321, true), (40_320, false)] { + let mut bridge = mainnet_with(number, &[NEW, OLD]); + assert_eq!(signs(&mut bridge, NEW.2), new_signs, "at {number}"); + assert!(signs(&mut bridge, OLD.2), "at {number}"); + } +} + +/// rskj's `addSignature_fromNewFederator_whenNoOldFederation_shouldCall`. +#[test] +fn add_signature_takes_a_new_federation_with_no_old_one() { + assert!(signs(&mut mainnet_with(0, &[NEW]), NEW.2)); +} + +/// rskj's `addSignature_fromProposedFederator_whenProposedFederation_shouldCall` +/// and `…whenNoProposedFederation_shouldThrow`. +#[test] +fn add_signature_takes_the_proposed_federation_while_there_is_one() { + assert!(signs(&mut mainnet_with(0, &[PROPOSED]), PROPOSED.2)); + assert!(!signs(&mut mainnet_with(0, &[]), PROPOSED.2)); +} + +/// rskj's `addSignature_fromNotFederatorSigner_shouldThrow`. +#[test] +fn add_signature_throws_for_a_signer_in_no_federation() { + let mut bridge = mainnet_with(0, &[NEW, OLD, PROPOSED]); + let signer = key_from_seed("notFederator"); + assert!(threw(&add_signature_from(&mut bridge, signer))); +} diff --git a/crates/execution/src/rskj_tests/bridge_methods/bridge_support_flyover_test.rs b/crates/execution/src/rskj_tests/bridge_methods/bridge_support_flyover_test.rs new file mode 100644 index 0000000..4559904 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_methods/bridge_support_flyover_test.rs @@ -0,0 +1,1256 @@ +//! rskj's `BridgeSupportFlyoverTest`: Flyover peg-ins, which the liquidity +//! bridge contract (the LBC) registers with `registerFastBridgeBtcTransaction`. +//! +//! rskj's tests call `BridgeSupport.registerFlyoverBtcTransaction` with mocks +//! for what it reads: the federations its storage provider holds, a Bitcoin +//! block store whose block at the transaction's height holds it with enough +//! blocks above, the locking cap, the Bridge's balance. Here the same state is +//! written where the Bridge reads it, and the call is the contract's: the +//! Bridge's method, from the LBC, a call deep. rskj's tests switch single +//! RSKIPs on; this port takes the upgrade that brings them: iris300 for +//! RSKIP176 (Flyover), hop400 for RSKIP293 (the retiring federation's +//! Flyover address). +//! +//! Some of the set-up differs. The Bitcoin addresses the call names are of +//! the test's network, as the Bridge's method reads them, where rskj's tests +//! hand `BridgeSupport` mainnet ones. On mainnet the block is the first at +//! which the new federation is active by mainnet's own activation age, where +//! rskj's tests lend mainnet regtest's. Where rskj's tests make the active +//! federation the network's genesis keys as a standard multisig +//! (`getGenesisFederationLegacy`), those keys are stored so, as the new +//! federation. And where rskj's tests leave the locking cap (RSKIP134) off or +//! mock it away, it is on here and the Bridge holds all the bitcoin there is, +//! so that none is locked. + +use super::support::*; +use crate::bridge::btc_store::{put_stored_block, store_chain_head, StoredBlock}; +use crate::bridge::federation::{serialize_federation_multikey, StoredMember}; +use crate::bridge::serialization::{rlp_encode_element, rlp_encode_list, rlp_encode_u64}; +use crate::bridge::storage::{ + bridge_load_raw, bridge_store_bytes_named, bridge_store_u256, compound_key, + FAST_BRIDGE_FEDERATION_INFO_KEY, LOCKING_CAP_KEY, NEW_FEDERATION_FORMAT_VERSION_KEY, + NEW_FEDERATION_KEY, OLD_FEDERATION_FORMAT_VERSION_KEY, OLD_FEDERATION_KEY, + RELEASES_OUTPOINTS_VALUES_KEY, +}; +use crate::precompiles::BRIDGE_ADDR; +use alloy_primitives::{Address, B256, U256}; +use bitcoin::hashes::Hash; +use RskNetworkUpgrade::{Hop400, Iris300, Lovell700, Vetiver900}; + +const COIN: u64 = 100_000_000; +const CENT: u64 = 1_000_000; + +const SIGNATURE: &str = + "registerFastBridgeBtcTransaction(bytes,uint256,bytes,bytes32,bytes,address,bytes,bool)"; + +/// The LBC. rskj's tests call from the zero address. +const LBC: Address = Address::ZERO; + +/// rskj's `FlyoverTxResponseCodes`. +const REFUNDED_USER: i64 = -100; +const REFUNDED_LP: i64 = -200; +const NOT_CONTRACT: i64 = -300; +const INVALID_SENDER: i64 = -301; +const ALREADY_PROCESSED: i64 = -302; +const VALIDATIONS: i64 = -303; +const VALUE_ZERO: i64 = -304; +const UTXO_BELOW_MINIMUM: i64 = -305; +const GENERIC_ERROR: i64 = -900; + +#[derive(Clone, Copy, Debug)] +enum Net { + Regtest, + Mainnet, +} + +impl Net { + fn constants(self) -> BridgeConstants { + match self { + Net::Regtest => BridgeConstants::rskj_regtest(), + Net::Mainnet => BridgeConstants::mainnet(), + } + } + + fn chain_id(self) -> u64 { + match self { + Net::Regtest => RSK_REGTEST_CHAIN_ID, + Net::Mainnet => RSK_MAINNET_CHAIN_ID, + } + } + + /// The version byte of a P2PKH address. + fn p2pkh(self) -> u8 { + match self { + Net::Regtest => 111, + Net::Mainnet => 0, + } + } + + /// rskj's `createP2pkhOutput`, `createP2shOutput` and `createBech32Output`. + fn other_outputs(self) -> [Vec; 3] { + let (p2pkh, p2sh, bech32) = match self { + Net::Regtest => ( + "mipcBbFg9gMiCh81Kj8tqqdgoZub1ZJRfn", + "2N7TdMZSYfwFMUwyr8YADSKUhLUyH6eqohz", + "0014ef57424d0d625cf82fabe4fd7657d24a5f13dfb2", + ), + Net::Mainnet => ( + "1JMaBRALrJQArLrqe5zRSn3bVk1z9RGzML", + "3FiMaJinRy86WgJZDfAfRUt2ZLEBGZSfLL", + "001437c383ea78269585c73289daa36d7b7014b65294", + ), + }; + [ + script_of(p2pkh), + script_of(p2sh), + alloy_primitives::hex::decode(bech32).unwrap(), + ] + } + + /// The first block at which a new federation made at block 0 is active, + /// by its activation age before RSKIP383. + fn block(self) -> u64 { + self.constants().federation_activation_age_legacy + } +} + +fn p2pkh_script(hash: &[u8]) -> Vec { + [&[0x76, 0xa9, 0x14][..], hash, &[0x88, 0xac]].concat() +} + +fn p2sh_script(hash: &[u8]) -> Vec { + [&[0xa9, 0x14][..], hash, &[0x87]].concat() +} + +/// The output script that pays a base58 address. +fn script_of(address: &str) -> Vec { + let bytes = bitcoin::base58::decode_check(address).unwrap(); + match bytes[0] { + 0 | 111 => p2pkh_script(&bytes[1..]), + _ => p2sh_script(&bytes[1..]), + } +} + +/// rskj's `PegTestUtils.createFederation`: the keys of the private numbers, +/// sorted. +fn keys(privates: &[&str]) -> Vec<[u8; 33]> { + let mut keys: Vec<[u8; 33]> = privates + .iter() + .map(|private| key_of_private(private)) + .collect(); + keys.sort(); + keys +} + +/// rskj's tests' active federation, `fa03` to `fa05`. +fn active() -> Vec<[u8; 33]> { + keys(&["fa03", "fa04", "fa05"]) +} + +/// rskj's tests' retiring federation, `fa01` and `fa02`. +fn retiring() -> Vec<[u8; 33]> { + keys(&["fa01", "fa02"]) +} + +/// A standard multisig federation's redeem script: a majority of `keys`, +/// sorted as bitcoinj sorts them. +fn redeem_script(keys: &[[u8; 33]]) -> Vec { + let mut keys = keys.to_vec(); + keys.sort(); + let mut script = vec![0x50 + (keys.len() / 2 + 1) as u8]; + for key in &keys { + script.push(0x21); + script.extend_from_slice(key); + } + script.extend_from_slice(&[0x50 + keys.len() as u8, 0xae]); + script +} + +/// Where a federation is stored and where its format is. +type Slot = (&'static str, &'static str); +const NEW: Slot = (NEW_FEDERATION_KEY, NEW_FEDERATION_FORMAT_VERSION_KEY); +const OLD: Slot = (OLD_FEDERATION_KEY, OLD_FEDERATION_FORMAT_VERSION_KEY); + +/// A Bitcoin transaction paying `outputs`, with rskj's one input: output 0 of +/// the zero hash, its signature left empty, and the one-byte witness `push` if +/// there is one. +fn btc_tx(outputs: &[(u64, Vec)], push: Option) -> bitcoin::Transaction { + bitcoin::Transaction { + version: bitcoin::transaction::Version::ONE, + lock_time: bitcoin::absolute::LockTime::ZERO, + input: vec![bitcoin::TxIn { + previous_output: bitcoin::OutPoint { + txid: bitcoin::Txid::all_zeros(), + vout: 0, + }, + script_sig: bitcoin::ScriptBuf::from_bytes( + [&[0x00, 0x21][..], &key_from_seed("input")].concat(), + ), + sequence: bitcoin::Sequence::MAX, + witness: push.map_or_else(bitcoin::Witness::new, |push| { + bitcoin::Witness::from_slice(&[[push]]) + }), + }], + output: outputs + .iter() + .map(|(value, script)| bitcoin::TxOut { + value: bitcoin::Amount::from_sat(*value), + script_pubkey: bitcoin::ScriptBuf::from_bytes(script.clone()), + }) + .collect(), + } +} + +fn txid(tx: &bitcoin::Transaction) -> [u8; 32] { + tx.compute_txid().to_byte_array() +} + +fn wtxid(tx: &bitcoin::Transaction) -> [u8; 32] { + tx.compute_wtxid().to_byte_array() +} + +/// A partial merkle tree of one transaction, `hash`, matched. +fn one_leaf_tree(hash: &[u8; 32]) -> Vec { + [&1u32.to_le_bytes()[..], &[1], hash, &[1, 0x3f]].concat() +} + +fn wei(satoshis: u64) -> U256 { + U256::from(satoshis) * U256::from(10_000_000_000u64) +} + +/// A Flyover registration as rskj's tests set it up. +struct Flyover { + bridge: Bridge, + net: Net, + args_hash: [u8; 32], + user: Vec, + lbc: Address, + lp: Vec, +} + +impl Flyover { + /// `net` at `block` with every upgrade to `last`, called by the LBC. + fn new(net: Net, last: RskNetworkUpgrade, block: u64) -> Self { + let mut bridge = Bridge::new(up_to(last, net.chain_id()), net.constants(), block); + bridge.caller = LBC; + bridge.depth = 2; + let address = + |seed: &str| [vec![net.p2pkh()], hash160(&key_from_seed(seed)).to_vec()].concat(); + let mut args_hash = [0; 32]; + args_hash[0] = 1; + Flyover { + bridge, + net, + args_hash, + user: address("refund"), + lbc: LBC, + lp: address("lp"), + } + } + + /// The Bridge holding all the bitcoin there is, so that none is locked. + fn nothing_locked(&mut self) { + self.bridge.credit(BRIDGE_ADDR, wei(2_100_000_000_000_000)); + } + + fn cap(&mut self, satoshis: u64) { + bridge_store_u256(&mut self.bridge.ctx, LOCKING_CAP_KEY, U256::from(satoshis)); + } + + /// `keys` as a standard multisig federation made at block 0, in `slot`. + fn store(&mut self, slot: Slot, keys: &[[u8; 33]]) { + let (key, format_key) = slot; + let members: Vec = keys.iter().copied().map(StoredMember::from_btc).collect(); + bridge_store_bytes_named(&mut self.bridge.ctx, format_key, &rlp_encode_u64(1000)); + let stored = serialize_federation_multikey(&members, 1_000, 0); + bridge_store_bytes_named(&mut self.bridge.ctx, key, &stored); + } + + /// rskj's `PegUtils.getFlyoverDerivationHash`. + fn derivation_hash(&self) -> [u8; 32] { + use sha3::Digest; + let data = [ + &self.args_hash[..], + &self.user, + self.lbc.as_slice(), + &self.lp, + ] + .concat(); + sha3::Keccak256::digest(data).into() + } + + /// The Flyover redeem script of `keys`' federation: the derivation hash + /// pushed and dropped before the federation's redeem script. + fn redeem(&self, keys: &[[u8; 33]]) -> Vec { + [ + &[0x20][..], + &self.derivation_hash(), + &[0x75], + &redeem_script(keys), + ] + .concat() + } + + /// The output script of `keys`' federation's Flyover address, the P2SH of + /// its Flyover redeem script. + fn pays(&self, keys: &[[u8; 33]]) -> Vec { + p2sh_script(&hash160(&self.redeem(keys))) + } + + /// rskj's `mockChainOfStoredBlocks`: stored blocks from `height` up, the + /// one at `height` with `merkle_root` and the network's confirmations + /// above it, the last the chain's head. The hash of the one at `height`, in + /// display order. + fn chain(&mut self, height: u32, merkle_root: [u8; 32]) -> [u8; 32] { + let use_v2 = self.bridge.cfg.has_stored_block_v2(self.bridge.number()); + let head = height + + self + .bridge + .constants + .btc2rsk_minimum_acceptable_confirmations; + let mut prev = bitcoin::BlockHash::from_byte_array([2; 32]); + let mut at_height = [0; 32]; + for at in height..=head { + let root = if at == height { + merkle_root + } else { + [0x11; 32] + }; + let header = bitcoin::block::Header { + version: bitcoin::block::Version::from_consensus(1), + prev_blockhash: prev, + merkle_root: bitcoin::TxMerkleNode::from_byte_array(root), + time: 1, + bits: bitcoin::CompactTarget::from_consensus(1), + nonce: at, + }; + let stored = StoredBlock::new(header, at, U256::from(at)); + put_stored_block(&mut self.bridge.ctx, &stored, use_v2); + if at == head { + store_chain_head(&mut self.bridge.ctx, &stored, use_v2); + } + if at == height { + at_height = header.block_hash().to_byte_array(); + at_height.reverse(); + } + prev = header.block_hash(); + } + at_height + } + + /// The Bridge's answer to the LBC's registration of `tx` at `height`. + fn register(&mut self, tx: &[u8], height: i64, pmt: &[u8], to_lp: bool) -> Vec { + let data = call_data( + SIGNATURE, + &[ + Arg::Bytes(tx), + int(height), + Arg::Bytes(pmt), + Arg::Word(self.args_hash), + Arg::Bytes(&self.user), + address(self.lbc), + Arg::Bytes(&self.lp), + boolean(to_lp), + ], + ); + self.bridge.answer(&data) + } + + /// `tx` in the block at height 1 of a chain stored for it, registered + /// there. + fn register_in_chain(&mut self, tx: &bitcoin::Transaction, to_lp: bool) -> Vec { + self.chain(1, txid(tx)); + let raw = bitcoin::consensus::serialize(tx); + self.register(&raw, 1, &one_leaf_tree(&wtxid(tx)), to_lp) + } + + fn balance_of_the_lbc(&mut self) -> U256 { + self.bridge.balance(LBC) + } + + /// The peg-outs waiting for confirmations. + fn waiting_for_confirmations(&mut self) -> Vec { + let with_hash = self.bridge.cfg.has_rskip146(self.bridge.number()); + crate::bridge::peg::load_pegout_confirmation_set(&mut self.bridge.ctx, with_hash) + .iter() + .map(|entry| bitcoin::consensus::deserialize(&entry.btc_tx_raw).unwrap()) + .collect() + } +} + +/// rskj's `sendFundsToAnyAddress`: the federations `active` as the new one +/// and `retiring` as the old, a payment with `outputs`, given what pays the +/// active federation's Flyover address and the retiring one's. +fn send_to_any_address( + net: Net, + rskip293: bool, + outputs: impl Fn(&[u8], &[u8]) -> Vec<(u64, Vec)>, +) -> Vec { + let last = if rskip293 { Hop400 } else { Iris300 }; + let mut flyover = Flyover::new(net, last, net.block()); + flyover.nothing_locked(); + flyover.store(NEW, &active()); + flyover.store(OLD, &retiring()); + let tx = btc_tx( + &outputs(&flyover.pays(&active()), &flyover.pays(&retiring())), + None, + ); + flyover.register_in_chain(&tx, true) +} + +/// rskj's `registerFlyoverBtcTransaction_output_to_bech32_before_RSKIP293_*` +/// and `…multiple_output_to_all_address_type_before_RSKIP293_*`: before +/// RSKIP293 rskj reads every output of the payment as an address +/// (`BridgeUtilsLegacy.getAmountSentToAddress`, which calls bitcoinj's +/// `Script.getToAddress` on each), and bitcoinj throws a `ScriptException` +/// for any but P2PKH and P2SH. The Bridge's method answers the throw with +/// GENERIC_ERROR. +#[test] +fn before_rskip293_an_output_bitcoinj_cannot_read_as_an_address_is_a_generic_error() { + for net in [Net::Regtest, Net::Mainnet] { + let [p2pkh, p2sh, bech32] = net.other_outputs(); + let bech32_alone = send_to_any_address(net, false, |_, _| vec![(COIN, bech32.clone())]); + assert_eq!(int_of(&bech32_alone), GENERIC_ERROR, "{net:?}"); + let every_type = send_to_any_address(net, false, |active, _| { + vec![ + (COIN, active.to_vec()), + (COIN, bech32.clone()), + (COIN, p2pkh.clone()), + (COIN, p2sh.clone()), + ] + }); + assert_eq!(int_of(&every_type), GENERIC_ERROR, "{net:?}"); + } +} + +/// rskj's `registerFlyoverBtcTransaction_output_to_bech32_after_RSKIP293_*` and +/// `…multiple_output_to_all_address_type_after_RSKIP293_*`: from RSKIP293 rskj +/// asks a wallet which outputs are the federations', and what pays +/// elsewhere, in whatever script, is passed over. +#[test] +fn from_rskip293_outputs_to_other_addresses_are_passed_over() { + for net in [Net::Regtest, Net::Mainnet] { + let [p2pkh, p2sh, bech32] = net.other_outputs(); + let with_bech32 = send_to_any_address(net, true, |active, _| { + vec![(COIN, active.to_vec()), (COIN, bech32.clone())] + }); + assert_eq!(uint_of(&with_bech32), wei(COIN), "{net:?}"); + let every_type = send_to_any_address(net, true, |active, _| { + vec![ + (COIN, active.to_vec()), + (COIN, bech32.clone()), + (COIN, p2pkh.clone()), + (COIN, p2sh.clone()), + ] + }); + assert_eq!(uint_of(&every_type), wei(COIN), "{net:?}"); + } +} + +/// From RSKIP293 the wallet reads an output through bitcoinj-thin's +/// `TransactionOutput.isMine` and `isWatched`, which catch the +/// `ScriptException` of a script that doesn't parse and pass the output over: +/// a payment with such an output beside the one to the active federation's +/// Flyover address is registered for what it sends there. Before RSKIP293 +/// rskj reads that output as an address, bitcoinj throws, and the method +/// answers GENERIC_ERROR. The outputs that don't parse: an OP_PUSHDATA1 with +/// no length after it, and a push of two bytes with one. +#[test] +fn from_rskip293_an_output_whose_script_does_not_parse_is_passed_over() { + for net in [Net::Regtest, Net::Mainnet] { + for unparseable in [vec![0x4c], vec![0x02, 0x01]] { + let outputs = |active: &[u8], _: &[u8]| { + vec![(COIN, active.to_vec()), (COIN, unparseable.clone())] + }; + let registered = send_to_any_address(net, true, outputs); + assert_eq!( + uint_of(®istered), + wei(COIN), + "{net:?} {unparseable:02x?}" + ); + let before = send_to_any_address(net, false, outputs); + assert_eq!(int_of(&before), GENERIC_ERROR, "{net:?} {unparseable:02x?}"); + } + } +} + +/// rskj's `registerFlyoverBtcTransaction_multiple_output_to_random_addresses_*`: +/// a payment to other addresses alone sends the federations nothing. +#[test] +fn a_payment_to_other_addresses_alone_is_of_no_value() { + for net in [Net::Regtest, Net::Mainnet] { + let [p2pkh, p2sh, _] = net.other_outputs(); + let answer = send_to_any_address(net, true, |_, _| { + vec![ + (COIN, p2pkh.clone()), + (COIN, p2pkh.clone()), + (COIN, p2pkh.clone()), + (COIN, p2sh.clone()), + (COIN, p2sh.clone()), + ] + }); + assert_eq!(int_of(&answer), VALUE_ZERO, "{net:?}"); + } +} + +/// rskj's `registerFlyoverBtcTransaction_output_to_P2PKH_*`, `…output_to_P2SH_*`, +/// `…output_to_different_address_type_*`, `…multiple_output_to_the_same_address_*` +/// and `…multiple_output_to_different_addresses_*`: from RSKIP293 each output +/// to either federation's Flyover address counts, and nothing else does. The +/// outputs, a bitcoin each: `a` to the active federation, `r` to the retiring +/// one, `k` to a P2PKH address, `s` to a P2SH one. +#[test] +fn from_rskip293_what_pays_either_federation_counts_and_nothing_else() { + for net in [Net::Regtest, Net::Mainnet] { + let [p2pkh, p2sh, _] = net.other_outputs(); + for (outputs, coins) in [ + ("ak", 1), + ("as", 1), + ("arsk", 2), + ("aaaa", 4), + ("aarrrkkss", 5), + ] { + let answer = send_to_any_address(net, true, |active, retiring| { + outputs + .chars() + .map(|to| match to { + 'a' => (COIN, active.to_vec()), + 'r' => (COIN, retiring.to_vec()), + 'k' => (COIN, p2pkh.clone()), + _ => (COIN, p2sh.clone()), + }) + .collect() + }); + assert_eq!(uint_of(&answer), wei(coins * COIN), "{net:?} {outputs}"); + } + } +} + +/// rskj's `registerFlyoverBtcTransaction_sent_multiple_output_and_one_with_amount_below_minimum_*`: +/// from RSKIP293, one output to a federation a satoshi short of the minimum +/// peg-in turns the whole payment down, the others at or past it. +#[test] +fn from_rskip293_one_output_below_the_minimum_turns_the_payment_down() { + for net in [Net::Regtest, Net::Mainnet] { + let minimum = net.constants().minimum_pegin_tx_value; + let [p2pkh, _, _] = net.other_outputs(); + let answer = send_to_any_address(net, true, |active, retiring| { + vec![ + (minimum, p2pkh.clone()), + (COIN, p2pkh.clone()), + (COIN, p2pkh.clone()), + (minimum, active.to_vec()), + (minimum - 1, active.to_vec()), + (minimum + 1, retiring.to_vec()), + (minimum + COIN, retiring.to_vec()), + ] + }); + assert_eq!(int_of(&answer), UTXO_BELOW_MINIMUM, "{net:?}"); + } +} + +/// rskj's `sendFundsToActiveFederation`: only the new federation, `active`, +/// stored, and `value` sent to its Flyover address. On success the LBC is +/// paid the value. +fn send_to_the_active_federation(rskip293: bool, value: u64) -> Vec { + let last = if rskip293 { Hop400 } else { Iris300 }; + let mut flyover = Flyover::new(Net::Regtest, last, Net::Regtest.block()); + flyover.nothing_locked(); + flyover.store(NEW, &active()); + let tx = btc_tx(&[(value, flyover.pays(&active()))], None); + let before = flyover.balance_of_the_lbc(); + let answer = flyover.register_in_chain(&tx, true); + if int_of(&answer) > 0 { + assert_eq!(flyover.balance_of_the_lbc(), before + wei(value)); + } + answer +} + +/// rskj's `registerFlyoverBtcTransaction_amount_sent_is_below_minimum_before_RSKIP293_activation`. +#[test] +fn before_rskip293_any_amount_to_the_active_federation_is_taken() { + let half = Net::Regtest.constants().minimum_pegin_tx_value / 2; + assert_eq!( + uint_of(&send_to_the_active_federation(false, half)), + wei(half) + ); +} + +/// rskj's `registerFlyoverBtcTransaction_amount_sent_is_below_minimum_after_RSKIP293_activation`, +/// `…amount_sent_is_equal_to_minimum` and `…amount_sent_is_over_minimum`. +#[test] +fn from_rskip293_a_payment_must_reach_the_minimum_peg_in() { + let minimum = Net::Regtest.constants().minimum_pegin_tx_value; + assert_eq!( + int_of(&send_to_the_active_federation(true, minimum / 2)), + UTXO_BELOW_MINIMUM + ); + assert_eq!( + uint_of(&send_to_the_active_federation(true, minimum)), + wei(minimum) + ); + let over = minimum + CENT; + assert_eq!( + uint_of(&send_to_the_active_federation(true, over)), + wei(over) + ); +} + +/// rskj's `registerFlyoverBtcTransaction_funds_sent_to_active_fed_after_RSKIP293`. +#[test] +fn from_rskip293_a_payment_to_the_active_federation_is_taken() { + assert_eq!( + uint_of(&send_to_the_active_federation(true, COIN)), + wei(COIN) + ); +} + +/// rskj's `sendFundsToRetiringFederation` and +/// `sendFundsToActiveAndRetiringFederation`: what a payment of a bitcoin to +/// the retiring federation, and to both, is answered. +fn send_to_the_retiring_federation(rskip293: bool) -> (Vec, Vec) { + let to = |both: bool| { + send_to_any_address(Net::Regtest, rskip293, |active, retiring| { + let mut outputs = vec![(COIN, retiring.to_vec())]; + if both { + outputs.insert(0, (COIN, active.to_vec())); + } + outputs + }) + }; + (to(false), to(true)) +} + +/// rskj's `registerFlyoverBtcTransaction_funds_sent_to_current_retiring_fed_before_RSKIP293_activation` +/// and `…funds_sent_to_active_and_retiring_fed_before_RSKIP293_activation`. +#[test] +fn before_rskip293_what_pays_the_retiring_federation_does_not_count() { + let (retiring, both) = send_to_the_retiring_federation(false); + assert_eq!(int_of(&retiring), VALUE_ZERO); + assert_eq!(uint_of(&both), wei(COIN)); +} + +/// rskj's `registerFlyoverBtcTransaction_funds_sent_to_retiring_fed_after_RSKIP293` +/// and `…funds_sent_to_active_and_retiring_fed_after_RSKIP293_activation`. +#[test] +fn from_rskip293_what_pays_the_retiring_federation_counts() { + let (retiring, both) = send_to_the_retiring_federation(true); + assert_eq!(uint_of(&retiring), wei(COIN)); + assert_eq!(uint_of(&both), wei(2 * COIN)); +} + +/// rskj's `sendFundsSurpassesLockingCapToAnyAddress`: a cap of 50 bitcoin, +/// none locked, the federations `active` as the new one and `retiring` as the +/// old. A refund is one peg-out waiting for confirmations, of one output to +/// the LP or the user, worth less than what the federations were sent and +/// more than nine tenths of it. +fn send_over_the_cap( + rskip293: bool, + outputs: impl Fn(&[u8], &[u8]) -> Vec<(u64, Vec)>, + to_lp: bool, +) -> Vec { + let last = if rskip293 { Hop400 } else { Iris300 }; + let mut flyover = Flyover::new(Net::Regtest, last, Net::Regtest.block()); + flyover.nothing_locked(); + flyover.cap(50 * COIN); + flyover.store(NEW, &active()); + flyover.store(OLD, &retiring()); + let (to_active, to_retiring) = (flyover.pays(&active()), flyover.pays(&retiring())); + let tx = btc_tx(&outputs(&to_active, &to_retiring), None); + let answer = flyover.register_in_chain(&tx, to_lp); + if [REFUNDED_LP, REFUNDED_USER].contains(&int_of(&answer)) { + let counted = |script: &bitcoin::ScriptBuf| { + script.as_bytes() == to_active || (rskip293 && script.as_bytes() == to_retiring) + }; + let sent: u64 = tx + .output + .iter() + .filter(|output| counted(&output.script_pubkey)) + .map(|output| output.value.to_sat()) + .sum(); + let refund = flyover.waiting_for_confirmations(); + assert_eq!(refund.len(), 1); + assert_eq!(refund[0].output.len(), 1); + let to = if to_lp { &flyover.lp } else { &flyover.user }; + assert_eq!( + refund[0].output[0].script_pubkey.as_bytes(), + p2pkh_script(&to[1..]) + ); + let refunded = refund[0].output[0].value.to_sat(); + assert!( + refunded > sent - sent / 10 && refunded < sent, + "{refunded} of {sent}" + ); + } + answer +} + +/// rskj's `registerFlyoverBtcTransaction_sum_of_funds_sent_to_active_and_retiring_fed_surpasses_locking_cap_before_RSKIP293_no_refund` +/// and the two `…before_RSKIP293_only_funds_sent_to_active_fed_should_be_refunded_to_*`: +/// before RSKIP293 only what pays the active federation counts against the +/// cap, and is what a refund returns. +#[test] +fn before_rskip293_only_the_active_federations_part_counts_against_the_cap() { + let under = send_over_the_cap( + false, + |active, retiring| vec![(25 * COIN, active.to_vec()), (50 * COIN, retiring.to_vec())], + true, + ); + assert_eq!(uint_of(&under), wei(25 * COIN)); + for (to_lp, refunded) in [(true, REFUNDED_LP), (false, REFUNDED_USER)] { + let over = send_over_the_cap( + false, + |active, retiring| vec![(51 * COIN, active.to_vec()), (51 * COIN, retiring.to_vec())], + to_lp, + ); + assert_eq!(int_of(&over), refunded); + } +} + +/// rskj's `registerFlyoverBtcTransaction_funds_sent_to_retiring_fed_surpasses_locking_cap_should_refund_user_after_RSKIP293`, +/// `…sum_of_funds_sent_to_active_and_retiring_fed_surpasses_locking_cap_should_refund_lp_after_RSKIP293`, +/// `…sum_of_all_utxos_surpass_locking_cap_but_not_funds_sent_to_fed_should_not_refund_after_RSKIP293` +/// and `…funds_sent_to_random_addresses_surpass_locking_cap_should_not_refund_after_RSKIP293`: +/// from RSKIP293 what pays either federation counts against the cap, and +/// what pays elsewhere doesn't. +#[test] +fn from_rskip293_both_federations_parts_count_against_the_cap() { + let retiring_over = send_over_the_cap( + true, + |_, retiring| vec![(51 * COIN, retiring.to_vec())], + false, + ); + assert_eq!(int_of(&retiring_over), REFUNDED_USER); + let both_over = send_over_the_cap( + true, + |active, retiring| vec![(50 * COIN, active.to_vec()), (50 * COIN, retiring.to_vec())], + true, + ); + assert_eq!(int_of(&both_over), REFUNDED_LP); + let elsewhere = script_of("mipcBbFg9gMiCh81Kj8tqqdgoZub1ZJRfn"); + let mostly_elsewhere = send_over_the_cap( + true, + |active, retiring| { + vec![ + (COIN, active.to_vec()), + (COIN, retiring.to_vec()), + (50 * COIN, elsewhere.clone()), + ] + }, + false, + ); + assert_eq!(uint_of(&mostly_elsewhere), wei(2 * COIN)); + let all_elsewhere = send_over_the_cap( + true, + |_, _| { + vec![ + (50 * COIN, elsewhere.clone()), + (COIN, p2sh_script(&[0x22; 20])), + ] + }, + false, + ); + assert_eq!(int_of(&all_elsewhere), VALUE_ZERO); +} + +/// The witness tests' set-up: regtest at iris300, only the new federation, +/// `active`, stored, a payment of `value` to its Flyover address, and, below +/// that, a cap of `cap` where one is given. +fn witness_flyover(value: u64, cap: Option) -> (Flyover, bitcoin::Transaction) { + let mut flyover = Flyover::new(Net::Regtest, Iris300, Net::Regtest.block()); + flyover.nothing_locked(); + if let Some(cap) = cap { + flyover.cap(cap); + } + flyover.store(NEW, &active()); + let tx = btc_tx(&[(value, flyover.pays(&active()))], None); + (flyover, tx) +} + +/// `tx` with rskj's one-byte witness, in the block at height 1 of a chain +/// whose coinbase registers `witness_root` (display order); its answer when +/// proven by `wtxid`'s tree. +fn register_with_a_witness( + flyover: &mut Flyover, + tx: &bitcoin::Transaction, + witness_root: [u8; 32], +) -> Vec { + let mut with = tx.clone(); + with.input[0].witness = bitcoin::Witness::from_slice(&[[1u8]]); + let block = flyover.chain(1, txid(&with)); + crate::bridge::tx::set_coinbase_information(&mut flyover.bridge.ctx, &block, &witness_root); + let raw = bitcoin::consensus::serialize(&with); + flyover.register(&raw, 1, &one_leaf_tree(&wtxid(&with)), false) +} + +fn reversed(mut hash: [u8; 32]) -> [u8; 32] { + hash.reverse(); + hash +} + +fn witness_root_of(tx: &bitcoin::Transaction) -> [u8; 32] { + let mut with = tx.clone(); + with.input[0].witness = bitcoin::Witness::from_slice(&[[1u8]]); + reversed(wtxid(&with)) +} + +/// rskj's `registerFlyoverBtcTransaction_failed_when_tx_with_witness_already_saved_in_storage`: +/// a payment with a witness, proven by its wtxid against the coinbase's +/// witness root, is registered once. +#[test] +fn a_payment_with_a_witness_is_registered_once() { + let (mut flyover, tx) = witness_flyover(COIN, None); + let root = witness_root_of(&tx); + assert_eq!( + uint_of(®ister_with_a_witness(&mut flyover, &tx, root)), + wei(COIN) + ); + assert_eq!( + int_of(®ister_with_a_witness(&mut flyover, &tx, root)), + ALREADY_PROCESSED + ); +} + +/// rskj's `registerFlyoverBtcTransaction_failed_when_tx_with_witness_surpassing_locking_already_saved_in_storage`. +#[test] +fn a_refunded_payment_with_a_witness_is_not_registered_again() { + let (mut flyover, tx) = witness_flyover(2 * COIN, Some(COIN)); + let root = witness_root_of(&tx); + assert_eq!( + int_of(®ister_with_a_witness(&mut flyover, &tx, root)), + REFUNDED_USER + ); + assert_eq!( + int_of(®ister_with_a_witness(&mut flyover, &tx, root)), + ALREADY_PROCESSED + ); +} + +/// rskj's `registerFlyoverBtcTransaction_failed_when_tx_with_witness_surpassing_locking_already_saved_in_storage_and_then_send_same_without_witness`: +/// what a refund marks is the payment's txid, so the payment without its +/// witness is turned down too, though the cap would now take it. +#[test] +fn a_payment_refunded_with_a_witness_is_not_registered_without_it() { + let (mut flyover, tx) = witness_flyover(2 * COIN, Some(COIN)); + let root = witness_root_of(&tx); + assert_eq!( + int_of(®ister_with_a_witness(&mut flyover, &tx, root)), + REFUNDED_USER + ); + flyover.cap(3 * COIN); + let raw = bitcoin::consensus::serialize(&tx); + let answer = flyover.register(&raw, 1, &one_leaf_tree(&txid(&tx)), false); + assert_eq!(int_of(&answer), ALREADY_PROCESSED); +} + +/// rskj's `registerFlyoverBtcTransaction_failed_when_tx_without_witness_surpassing_locking_already_saved_in_storage`. +#[test] +fn a_refunded_payment_is_not_registered_again() { + let (mut flyover, tx) = witness_flyover(2 * COIN, Some(COIN)); + assert_eq!( + int_of(&flyover.register_in_chain(&tx, false)), + REFUNDED_USER + ); + assert_eq!( + int_of(&flyover.register_in_chain(&tx, false)), + ALREADY_PROCESSED + ); +} + +/// rskj's `registerFlyoverBtcTransaction_failed_when_tx_without_witness_surpassing_locking_already_saved_in_storage_and_then_send_same_tx_with_witness`: +/// the payment again with a witness, proven by its wtxid where the coinbase +/// registers the root of the tree without it, as rskj's test registers it, +/// fails the validations. +#[test] +fn a_refunded_payment_again_with_a_witness_fails_the_validations() { + let (mut flyover, tx) = witness_flyover(2 * COIN, Some(COIN)); + assert_eq!( + int_of(&flyover.register_in_chain(&tx, false)), + REFUNDED_USER + ); + let answer = register_with_a_witness(&mut flyover, &tx, reversed(txid(&tx))); + assert_eq!(int_of(&answer), VALIDATIONS); +} + +/// rskj's `registerFlyoverBtcTransaction_failed_when_tx_without_witness_already_saved_in_storage`. +#[test] +fn a_payment_is_registered_once() { + let (mut flyover, tx) = witness_flyover(COIN, None); + assert_eq!(uint_of(&flyover.register_in_chain(&tx, false)), wei(COIN)); + assert_eq!( + int_of(&flyover.register_in_chain(&tx, false)), + ALREADY_PROCESSED + ); +} + +/// rskj's `registerFlyoverBtcTransaction_is_not_contract`: a call that isn't +/// a contract's is turned down first. +#[test] +fn a_registration_not_from_a_contract_is_turned_down() { + let mut flyover = Flyover::new(Net::Regtest, Iris300, Net::Regtest.block()); + flyover.bridge.depth = 1; + assert_eq!(int_of(&flyover.register(&[], 0, &[], false)), NOT_CONTRACT); +} + +/// rskj's `registerFlyoverBtcTransaction_sender_is_not_lbc`: a contract +/// other than the LBC the call names is turned down. +#[test] +fn a_registration_from_another_contract_than_the_lbc_is_turned_down() { + let mut flyover = Flyover::new(Net::Regtest, Iris300, Net::Regtest.block()); + flyover.bridge.caller = Address::repeat_byte(0x01); + // rskj's `new BtcTransaction(params).bitcoinSerialize()`: version 1, no + // inputs, no outputs, lock time 0. + let empty = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + assert_eq!( + int_of(&flyover.register(&empty, 100, &[0xab], false)), + INVALID_SENDER + ); +} + +/// rskj's `registerFlyoverBtcTransaction_validationsForRegisterBtcTransaction_returns_false`: +/// with the chain at regtest's genesis, a payment at height 100 has no +/// confirmations. +#[test] +fn a_payment_the_chain_cannot_confirm_fails_the_validations() { + let mut flyover = Flyover::new(Net::Regtest, Iris300, Net::Regtest.block()); + flyover.store(NEW, &active()); + let tx = btc_tx(&[(COIN, flyover.pays(&active()))], None); + let raw = bitcoin::consensus::serialize(&tx); + assert_eq!( + int_of(&flyover.register(&raw, 100, &[0xab], false)), + VALIDATIONS + ); +} + +/// rskj's tests' `getGenesisFederationLegacy`: `net`'s genesis keys as a +/// standard multisig federation, stored as the new one, and, as those tests +/// name them, a zero arguments' hash and one address for both the user and +/// the LP, `n3PLxDiwWqa5uH7fSbHCxS6VAjD9Y7Rwkj`'s key hash. A payment of +/// `value` to the federation's Flyover address, in the block at height 100 of +/// a chain stored for it. +fn pay_the_genesis_federation(net: Net, value: u64) -> (Flyover, Vec, Vec) { + let mut flyover = Flyover::new(net, Iris300, net.block()); + let genesis = crate::bridge::peg::genesis_federation_keys(&flyover.bridge.constants); + flyover.store(NEW, &genesis); + flyover.args_hash = [0; 32]; + let address = bitcoin::base58::decode_check("n3PLxDiwWqa5uH7fSbHCxS6VAjD9Y7Rwkj").unwrap(); + flyover.user = [&[flyover.net.p2pkh()][..], &address[1..]].concat(); + flyover.lp = flyover.user.clone(); + let tx = btc_tx(&[(value, flyover.pays(&genesis))], None); + flyover.chain(100, txid(&tx)); + ( + flyover, + bitcoin::consensus::serialize(&tx), + one_leaf_tree(&txid(&tx)), + ) +} + +/// rskj's `registerFlyoverBtcTransaction_amount_sent_is_0`. +#[test] +fn a_payment_of_nothing_is_of_no_value() { + let (mut flyover, tx, pmt) = pay_the_genesis_federation(Net::Mainnet, 0); + flyover.nothing_locked(); + assert_eq!(int_of(&flyover.register(&tx, 100, &pmt, false)), VALUE_ZERO); +} + +/// rskj's `registerFlyoverBtcTransaction_surpasses_locking_cap_and_shouldTransfer_is_true` +/// and `…_is_false`: with a cap of a bitcoin and a Bridge of one wei, all +/// but nothing is locked, and a bitcoin is refunded to the LP or the user. +#[test] +fn over_the_cap_a_payment_is_refunded_to_the_lp_or_the_user() { + for (to_lp, refunded) in [(true, REFUNDED_LP), (false, REFUNDED_USER)] { + let (mut flyover, tx, pmt) = pay_the_genesis_federation(Net::Regtest, COIN); + flyover.bridge.credit(BRIDGE_ADDR, U256::from(1)); + flyover.cap(COIN); + assert_eq!(int_of(&flyover.register(&tx, 100, &pmt, to_lp)), refunded); + } +} + +/// rskj's `registerFlyoverBtcTransaction_surpasses_locking_cap_and_tries_to_register_again`. +#[test] +fn a_payment_refunded_over_the_cap_is_not_registered_again() { + let (mut flyover, tx, pmt) = pay_the_genesis_federation(Net::Regtest, COIN); + flyover.bridge.credit(BRIDGE_ADDR, U256::from(1)); + flyover.cap(COIN); + assert_eq!( + int_of(&flyover.register(&tx, 100, &pmt, false)), + REFUNDED_USER + ); + flyover.cap(50 * COIN); + assert_eq!( + int_of(&flyover.register(&tx, 100, &pmt, false)), + ALREADY_PROCESSED + ); +} + +/// rskj's `registerFlyoverBtcTransaction_OK` and `saveFlyoverDataInStorage_OK`: +/// a payment is registered, the LBC is paid its value, the federation gets +/// its output, the Flyover address's information is stored under the hash of +/// its redeem script, and the payment is not registered again. +#[test] +fn a_payment_is_registered_and_its_value_goes_to_the_lbc() { + let (mut flyover, tx, pmt) = pay_the_genesis_federation(Net::Regtest, COIN); + flyover.nothing_locked(); + let genesis = crate::bridge::peg::genesis_federation_keys(&flyover.bridge.constants); + let before = flyover.balance_of_the_lbc(); + assert_eq!(uint_of(&flyover.register(&tx, 100, &pmt, true)), wei(COIN)); + assert_eq!(flyover.balance_of_the_lbc(), before + wei(COIN)); + let number = flyover.bridge.number(); + let cfg = flyover.bridge.cfg.clone(); + let utxos = + crate::bridge::storage::load_federation_utxos(&mut flyover.bridge.ctx, &cfg, number); + assert_eq!(utxos.len(), 1); + assert_eq!((utxos[0].vout, utxos[0].value_satoshis), (0, COIN)); + assert_eq!(utxos[0].script, flyover.pays(&genesis)); + let redeem = flyover.redeem(&genesis); + let information = compound_key( + FAST_BRIDGE_FEDERATION_INFO_KEY, + "-", + &alloy_primitives::hex::encode(hash160(&redeem)), + ); + let expected = rlp_encode_list(&[ + rlp_encode_element(&flyover.derivation_hash()), + rlp_encode_element(&hash160(&redeem_script(&genesis))), + ]); + assert_eq!( + bridge_load_raw(&mut flyover.bridge.ctx, information), + Some(expected) + ); + assert_eq!( + int_of(&flyover.register(&tx, 100, &pmt, true)), + ALREADY_PROCESSED + ); +} + +/// rskj's `getFlyoverDerivationHash_ok`: from RSKIP284 the derivation hash is +/// the Keccak-256 of the arguments' hash, the user's refund address, the LBC +/// and the LP's address, each address its version and its key hash. A +/// payment to the Flyover address of that hash, with rskj's test's +/// arguments' hash, addresses and LBC, is taken. +#[test] +fn the_derivation_hash_is_of_the_arguments_the_addresses_and_the_lbc() { + let mut flyover = Flyover::new(Net::Regtest, Hop400, Net::Regtest.block()); + flyover.nothing_locked(); + flyover.store(NEW, &active()); + flyover.args_hash = [0; 32]; + flyover.args_hash[31] = 1; + flyover.user = bitcoin::base58::decode_check("mgy8yiUZYB7o9vvCu2Yi8GB3Vr32MQsyQJ").unwrap(); + flyover.lp = bitcoin::base58::decode_check("mhoDGMzHHDq2ZD6cFrKV9USnMfpxEtLwGm").unwrap(); + flyover.lbc = Address::with_last_byte(3); + flyover.bridge.caller = flyover.lbc; + let tx = btc_tx(&[(COIN, flyover.pays(&active()))], None); + assert_eq!(uint_of(&flyover.register_in_chain(&tx, true)), wei(COIN)); +} + +/// rskj's nested `FlyoverPeginThatSurpassesLockingCap`: on mainnet, a payment +/// to the active federation of a satoshi over the initial cap, in a block +/// past where the peg-out index is read. The answer, the refund, and the +/// Bridge with it. +fn over_the_initial_cap( + last: RskNetworkUpgrade, + to_lp: bool, +) -> (i64, Flyover, bitcoin::Transaction) { + let mut flyover = Flyover::new(Net::Mainnet, last, 1); + flyover.store(NEW, &active()); + let amount = flyover.bridge.constants.initial_locking_cap + 1; + let tx = btc_tx(&[(amount, flyover.pays(&active()))], None); + let constants = &flyover.bridge.constants; + let height = constants.btc_height_when_pegout_tx_index_activates + + constants.pegout_tx_index_grace_period_in_btc_blocks; + flyover.chain(height, txid(&tx)); + let raw = bitcoin::consensus::serialize(&tx); + let answer = flyover.register(&raw, i64::from(height), &one_leaf_tree(&txid(&tx)), to_lp); + let mut refund = flyover.waiting_for_confirmations(); + assert_eq!(refund.len(), 1); + (int_of(&answer), flyover, refund.remove(0)) +} + +/// `pegout_transaction_created` for `refund`, with the value of the output +/// it spends: in the Bridge's logs. +fn logs_pegout_transaction_created( + flyover: &Flyover, + refund: &bitcoin::Transaction, + spent: u64, +) -> bool { + use revm::context_interface::{ContextTr, JournalTr}; + use sha3::Digest; + let topic: [u8; 32] = + sha3::Keccak256::digest("pegout_transaction_created(bytes32,bytes)").into(); + let topics = [B256::from(topic), B256::from(reversed(txid(refund)))]; + let varint = [&[0xff][..], &spent.to_le_bytes()].concat(); + let data = encode::args(&[Arg::Bytes(&varint)]); + flyover + .bridge + .ctx + .journal() + .logs() + .iter() + .any(|log| log.topics() == topics.as_slice() && log.data.data.as_ref() == data.as_slice()) +} + +/// The outpoint values stored for `refund`. +fn stored_outpoint_values(flyover: &mut Flyover, refund: &bitcoin::Transaction) -> Option> { + let hash = alloy_primitives::hex::encode(reversed(txid(refund))); + let key = compound_key(RELEASES_OUTPOINTS_VALUES_KEY, "-", &hash); + bridge_load_raw(&mut flyover.bridge.ctx, key) +} + +/// rskj's `registerFlyoverBtcTransaction_whenAmountRequestedSurpassesLockingCap_beforeRSKIP305_justEmitsPegoutTransactionCreatedEvent`: +/// at lovell700 the refund is logged with the value it spends, which is not +/// stored. +#[test] +fn before_rskip305_a_refund_over_the_cap_is_logged_and_its_values_not_stored() { + for (to_lp, refunded) in [(true, REFUNDED_LP), (false, REFUNDED_USER)] { + let (answer, mut flyover, refund) = over_the_initial_cap(Lovell700, to_lp); + assert_eq!(answer, refunded); + let spent = flyover.bridge.constants.initial_locking_cap + 1; + assert!(logs_pegout_transaction_created(&flyover, &refund, spent)); + assert_eq!(stored_outpoint_values(&mut flyover, &refund), None); + } +} + +/// rskj's `registerFlyoverBtcTransaction_whenAmountRequestedSurpassesLockingCap_afterRSKIP305_processReleaseRejectionTransactionInfo`: +/// with every upgrade the refund is logged and the value it spends stored. +#[test] +fn from_rskip305_a_refund_over_the_cap_is_logged_and_its_values_stored() { + for (to_lp, refunded) in [(true, REFUNDED_LP), (false, REFUNDED_USER)] { + let (answer, mut flyover, refund) = over_the_initial_cap(Vetiver900, to_lp); + assert_eq!(answer, refunded); + let spent = flyover.bridge.constants.initial_locking_cap + 1; + assert!(logs_pegout_transaction_created(&flyover, &refund, spent)); + let varint = [&[0xff][..], &spent.to_le_bytes()].concat(); + assert_eq!(stored_outpoint_values(&mut flyover, &refund), Some(varint)); + } +} + +/// rskj's `FederationFormatVersion.P2SH_ERP_FEDERATION`. +const P2SH_ERP: u64 = 3000; +/// rskj's `FederationFormatVersion.P2SH_P2WSH_ERP_FEDERATION`. +const P2SH_P2WSH_ERP: u64 = 4000; + +/// The keys of the seeds `prefix` and 01 up to `count`, sorted, as rskj's +/// `P2shErpFederationBuilder` and `P2shP2wshErpFederationBuilder` take them. +fn seeded_keys(prefix: &str, count: usize) -> Vec<[u8; 33]> { + let mut keys: Vec<[u8; 33]> = (1..=count) + .map(|n| key_from_seed(&format!("{prefix}{n:02}"))) + .collect(); + keys.sort(); + keys +} + +impl Flyover { + /// `keys` as a federation of `format` made at block 1, in `slot`, as + /// rskj's federation builders make one. + fn store_as(&mut self, slot: Slot, keys: &[[u8; 33]], format: u64) { + let (key, format_key) = slot; + let members: Vec = keys.iter().copied().map(StoredMember::from_btc).collect(); + bridge_store_bytes_named(&mut self.bridge.ctx, format_key, &rlp_encode_u64(format)); + let stored = serialize_federation_multikey(&members, 100_000_000_000, 1); + bridge_store_bytes_named(&mut self.bridge.ctx, key, &stored); + } + + /// The Flyover redeem script of the federation of `keys` and `format`. + fn redeem_of(&self, keys: &[[u8; 33]], format: u64) -> Vec { + let federation = crate::bridge::peg::federation_redeem_for_format_pub( + keys, + format, + &self.bridge.constants, + &self.bridge.cfg, + self.bridge.number(), + ); + [&[0x20][..], &self.derivation_hash(), &[0x75], &federation].concat() + } + + /// The output script of that federation's Flyover address: the P2SH of + /// its Flyover redeem script, or of that script's P2WSH program where the + /// federation is P2SH-P2WSH. + fn pays_to(&self, keys: &[[u8; 33]], format: u64) -> Vec { + let redeem = self.redeem_of(keys, format); + p2sh_script(&crate::bridge::peg::federation_output_hash160( + &redeem, format, + )) + } +} + +/// rskj's `FlyoverPeginThatSurpassesLockingCap` with both federations paid: +/// on mainnet with every upgrade, at the block at which the new federation, +/// made at block 1, is active and the old one retiring, a payment of a +/// satoshi over the initial cap to each one's Flyover address, refunded to +/// the LP. The refund, and the active and the retiring federation's Flyover +/// redeem scripts. +fn over_the_cap_to_both( + active: (&[[u8; 33]], u64), + retiring: (&[[u8; 33]], u64), +) -> (bitcoin::Transaction, Vec, Vec) { + let block = 1 + Net::Mainnet.constants().federation_activation_age; + let mut flyover = Flyover::new(Net::Mainnet, Vetiver900, block); + flyover.store_as(NEW, active.0, active.1); + flyover.store_as(OLD, retiring.0, retiring.1); + let amount = flyover.bridge.constants.initial_locking_cap + 1; + let tx = btc_tx( + &[ + (amount, flyover.pays_to(active.0, active.1)), + (amount, flyover.pays_to(retiring.0, retiring.1)), + ], + None, + ); + let constants = &flyover.bridge.constants; + let height = constants.btc_height_when_pegout_tx_index_activates + + constants.pegout_tx_index_grace_period_in_btc_blocks; + flyover.chain(height, txid(&tx)); + let raw = bitcoin::consensus::serialize(&tx); + let answer = flyover.register(&raw, i64::from(height), &one_leaf_tree(&txid(&tx)), true); + assert_eq!(int_of(&answer), REFUNDED_LP); + let mut refund = flyover.waiting_for_confirmations(); + assert_eq!(refund.len(), 1); + ( + refund.remove(0), + flyover.redeem_of(active.0, active.1), + flyover.redeem_of(retiring.0, retiring.1), + ) +} + +/// What the last element of `input`'s script pushes, where rskj's +/// `assertScriptSigHasExpectedInputRedeemData` finds the redeem script. +fn last_push_of_script_sig(input: &bitcoin::TxIn) -> Vec { + match input.script_sig.instructions().last() { + Some(Ok(bitcoin::script::Instruction::PushBytes(bytes))) => bytes.as_bytes().to_vec(), + other => panic!("the script ends with {other:?}"), + } +} + +/// rskj's `registerFlyoverBtcTransaction_fundsThatSurpassLockingCapSentToP2shErpActiveAndP2shErpRetiringFeds_shouldSetRedeemDataInInputsScriptSig`: +/// with both federations P2SH-ERP, the refund spends what each was paid, the +/// active federation's first, each input's script ending with its own +/// federation's Flyover redeem script. +#[test] +fn a_refund_from_two_p2sh_erp_federations_spends_each_with_its_own_flyover_script() { + let (refund, active, retiring) = over_the_cap_to_both( + (&seeded_keys("newMember", 9), P2SH_ERP), + (&seeded_keys("member", 9), P2SH_ERP), + ); + assert_eq!(refund.input.len(), 2); + assert_eq!(last_push_of_script_sig(&refund.input[0]), active); + assert_eq!(last_push_of_script_sig(&refund.input[1]), retiring); +} + +/// rskj's `registerFlyoverBtcTransaction_fundsThatSurpassLockingCapSentToP2shP2wshErpActiveAndP2shErpRetiringFeds_shouldSetFixedScriptSig_shouldSetRedeemDataInInputsWitness`: +/// the refund is built in the active federation's format, so with a +/// P2SH-P2WSH one both inputs, the retiring P2SH-ERP federation's too, carry +/// their Flyover redeem script last in the witness and, as their script, a +/// push of its P2WSH program alone. +#[test] +fn a_refund_built_for_a_p2sh_p2wsh_federation_carries_both_redeem_scripts_in_witnesses() { + use sha2::Digest; + let (refund, active, retiring) = over_the_cap_to_both( + (&seeded_keys("member", 20), P2SH_P2WSH_ERP), + (&seeded_keys("member", 9), P2SH_ERP), + ); + assert_eq!(refund.input.len(), 2); + for (input, redeem) in refund.input.iter().zip([active, retiring]) { + assert_eq!(input.witness.last(), Some(&redeem[..])); + let program = [&[0x00, 0x20][..], &sha2::Sha256::digest(&redeem)].concat(); + let script = [&[program.len() as u8][..], &program].concat(); + assert_eq!(input.script_sig.as_bytes(), &script[..]); + } +} diff --git a/crates/execution/src/rskj_tests/bridge_methods/bridge_support_it.rs b/crates/execution/src/rskj_tests/bridge_methods/bridge_support_it.rs new file mode 100644 index 0000000..7604730 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_methods/bridge_support_it.rs @@ -0,0 +1,79 @@ +//! rskj's `BridgeSupportIT`, its three `initialChainHeadWithBtcCheckpoints_*` +//! tests, of RSKIP454. rskj swaps the checkpoints file mainnet's Bridge seeds +//! its Bitcoin store from, for files of chain work written in 12 bytes, in 32, +//! and in both. rustock carries no checkpoints file, only the checkpoint each +//! network's store is seeded with (`BridgeConstants::btc_checkpoint`), so the +//! ports give mainnet's Bridge its own checkpoint with the chain work written +//! in 12 bytes, written in 32, and grown past what 12 bytes hold. Before +//! RSKIP454 the store writes the checkpoint in the legacy format, whose 12 +//! bytes of chain work refuse the last (`StoredBlock.serializeCompactLegacy`), +//! and the call throws; from it, in the 32-byte format, which takes all three. + +use super::support::*; + +/// Mainnet's lovell700, which brings RSKIP454. +const LOVELL700: u64 = 7_338_024; +/// The height of mainnet's checkpoint. +const CHECKPOINT_HEIGHT: i64 = 499_968; +/// The chain work of mainnet's checkpoint, in 12 bytes. +const IN_12_BYTES: &str = "00cd6eae36c0cee5640896d3"; +/// The same, in 32 bytes. +const IN_32_BYTES: &str = "000000000000000000000000000000000000000000cd6eae36c0cee5640896d3"; +/// A chain work past what 12 bytes hold. +const PAST_12_BYTES: &str = "0100cd6eae36c0cee5640896d3"; + +/// `getBtcBlockchainBestChainHeight` at block `number` of mainnet, whose +/// Bridge seeds its store from mainnet's checkpoint with `chain_work`: the +/// height it answers, or none where the call throws. +fn best_chain_height(number: u64, chain_work: &'static str) -> Option { + let mut constants = BridgeConstants::mainnet(); + let (header, height, kept) = constants.btc_checkpoint.expect("mainnet's checkpoint"); + assert_eq!((i64::from(height), kept), (CHECKPOINT_HEIGHT, IN_12_BYTES)); + constants.btc_checkpoint = Some((header, height, chain_work)); + let mut bridge = Bridge::new(RskHardforkConfig::mainnet(), constants, number); + let answer = bridge.execute(&selector_only("getBtcBlockchainBestChainHeight()")); + if threw(&answer) { + return None; + } + Some(int_of(&answer.expect("an answer").bytes)) +} + +/// rskj's +/// `initialChainHeadWithBtcCheckpoints_whenCheckpointsWith12BytesChainWork_before_RSKIP454_ok`, +/// of a file written in 12 bytes and one written in both: before RSKIP454 +/// the store is seeded from the checkpoint, the head of its best chain. +#[test] +fn before_rskip454_a_checkpoint_whose_chain_work_fits_12_bytes_seeds_the_store() { + assert_eq!( + best_chain_height(LOVELL700 - 1, IN_12_BYTES), + Some(CHECKPOINT_HEIGHT) + ); + assert_eq!( + best_chain_height(LOVELL700 - 1, IN_32_BYTES), + Some(CHECKPOINT_HEIGHT) + ); +} + +/// rskj's +/// `initialChainHeadWithBtcCheckpoints_whenCheckpointsWith32BytesChainWork_before_RSKIP454_shouldFail`: +/// before RSKIP454 a checkpoint whose chain work 12 bytes can't hold fails +/// the call ("The given number does not fit in 12"). +#[test] +fn before_rskip454_a_checkpoint_whose_chain_work_is_past_12_bytes_fails_the_call() { + assert_eq!(best_chain_height(LOVELL700 - 1, PAST_12_BYTES), None); +} + +/// rskj's +/// `initialChainHeadWithBtcCheckpoints_whenCheckpointsWith32BytesChainWork_after_RSKIP454_ok`, +/// of its files: from RSKIP454 the store is seeded from the checkpoint +/// whatever its chain work. +#[test] +fn from_rskip454_a_checkpoint_of_any_chain_work_seeds_the_store() { + for chain_work in [IN_12_BYTES, IN_32_BYTES, PAST_12_BYTES] { + assert_eq!( + best_chain_height(LOVELL700, chain_work), + Some(CHECKPOINT_HEIGHT), + "{chain_work}" + ); + } +} diff --git a/crates/execution/src/rskj_tests/bridge_methods/bridge_support_process_funds_migration_test.rs b/crates/execution/src/rskj_tests/bridge_methods/bridge_support_process_funds_migration_test.rs new file mode 100644 index 0000000..f042b78 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_methods/bridge_support_process_funds_migration_test.rs @@ -0,0 +1,354 @@ +//! rskj's `BridgeSupportProcessFundsMigrationTest`: `updateCollections` +//! moving the retiring federation's funds to the active one. +//! +//! rskj's tests run every upgrade from block 0, with the active federation +//! created at block 100. Here it is created at a mainnet height in the era a +//! test is about, and `updateCollections` is called where rskj's tests call +//! it: one block into the migration age (`duringMigrationBlockNumber`) or at +//! its end (`pastMigrationBlockNumber`). The federations are rskj's +//! builders': 'member01' on retires and 'newActiveMember-0' on is active. + +use super::support::*; +use crate::bridge::federation::{ + rsk_address_from_public_key, serialize_federation_multikey, StoredMember, +}; +use crate::bridge::peg::{ + build_federation_redeem_script, federation_output_hash160, federation_redeem_for_format_pub, + load_pegout_confirmation_set, p2sh_output_script_program, +}; +use crate::bridge::serialization::rlp_encode_u64; +use crate::bridge::storage::{ + bridge_load_bytes_named, bridge_store_bytes_named, bridge_store_u256, + load_old_federation_utxos, store_old_federation_utxos, BridgeUtxo, FEE_PER_KB_KEY, + NEW_FEDERATION_FORMAT_VERSION_KEY, NEW_FEDERATION_KEY, OLD_FEDERATION_FORMAT_VERSION_KEY, + OLD_FEDERATION_KEY, +}; +use alloy_primitives::U256; + +/// rskj's test's fee per kB. +const FEE_PER_KB: u64 = 8_000; +/// The least a P2SH output may carry (`MIN_NON_DUST_VALUE_FOR_P2SH_OUTPUT_SCRIPT`). +const MIN_NON_DUST: u64 = 2_700; +const COIN: u64 = 100_000_000; +/// Mainnet's federation activation age before RSKIP383 (fingerroot500). +const LEGACY_AGE: u64 = 18_500; +/// Mainnet's federation activation age from RSKIP383. +const AGE: u64 = 40_320; +/// How long after the activation age mainnet's funds migrate, outside +/// RSKIP357's special case. +const MIGRATION_END: u64 = 10_585; + +/// A federation: its members, sorted as rskj sorts them, and its format +/// version. +struct Federation { + members: Vec, + format: u64, +} + +impl Federation { + /// Members whose keys are those of `seeds` (`getBtcEcKeysFromSeeds`). + fn of(seeds: impl Iterator, format: u64) -> Self { + let mut members: Vec = seeds + .map(|seed| StoredMember::from_btc(key_from_seed(&seed))) + .collect(); + members.sort(); + Self { members, format } + } + + fn keys(&self) -> Vec<[u8; 33]> { + self.members.iter().map(|m| m.btc).collect() + } +} + +/// The builders' members, 'member01' on. +fn retiring(count: usize, format: u64) -> Federation { + Federation::of((1..=count).map(|i| format!("member{i:02}")), format) +} + +/// The test's active members, 'newActiveMember-0' on. +fn active(count: usize, format: u64) -> Federation { + Federation::of((0..count).map(|i| format!("newActiveMember-{i}")), format) +} + +/// UTXOs of `values` paid to `federation` as block `number` reads it, in +/// transactions whose hashes sort as the values are listed. +fn utxos(federation: &Federation, number: u64, values: &[u64]) -> Vec { + let redeem = federation_redeem_for_format_pub( + &federation.keys(), + federation.format, + &BridgeConstants::mainnet(), + &RskHardforkConfig::mainnet(), + number, + ); + let script = p2sh_output_script_program(&federation_output_hash160(&redeem, federation.format)); + values + .iter() + .zip(1u32..) + .map(|(&value_satoshis, n)| { + let mut tx_hash = [0u8; 32]; + tx_hash[..4].copy_from_slice(&n.to_be_bytes()); + BridgeUtxo { + tx_hash, + vout: 0, + value_satoshis, + height: 1, + script: script.clone(), + coinbase: false, + } + }) + .collect() +} + +/// The Bridge at block `number` of mainnet: `active` created at block +/// `created`, `retiring` before it holding `held`, the fee per kB at +/// `fee_per_kb`, and a member of `active` to call. +fn mainnet_at( + number: u64, + created: u64, + active: &Federation, + retiring: &Federation, + held: &[BridgeUtxo], + fee_per_kb: u64, +) -> Bridge { + let mut bridge = Bridge::new( + RskHardforkConfig::mainnet(), + BridgeConstants::mainnet(), + number, + ); + let ctx = &mut bridge.ctx; + bridge_store_u256(ctx, FEE_PER_KB_KEY, U256::from(fee_per_kb)); + for (key, version_key, federation, block) in [ + ( + NEW_FEDERATION_KEY, + NEW_FEDERATION_FORMAT_VERSION_KEY, + active, + created, + ), + ( + OLD_FEDERATION_KEY, + OLD_FEDERATION_FORMAT_VERSION_KEY, + retiring, + 1, + ), + ] { + let stored = serialize_federation_multikey(&federation.members, 100_000_000_000, block); + bridge_store_bytes_named(ctx, key, &stored); + bridge_store_bytes_named(ctx, version_key, &rlp_encode_u64(federation.format)); + } + store_old_federation_utxos(ctx, held); + bridge.caller = rsk_address_from_public_key(&active.members[0].rsk).unwrap(); + bridge +} + +/// What a call of `updateCollections` leaves. +struct After { + /// Whether the call threw. + failed: bool, + /// The migration transactions waiting for confirmations. + migrations: Vec, + /// Whether the retiring federation is still there. + retiring_kept: bool, + /// How many UTXOs it holds. + retiring_utxos: usize, +} + +fn update_collections(mut bridge: Bridge) -> After { + let result = bridge.execute(&selector_only("updateCollections()")); + let failed = threw(&result); + assert!(failed || result.is_ok(), "{result:?}"); + let migrations = load_pegout_confirmation_set(&mut bridge.ctx, true) + .iter() + .map(|entry| bitcoin::consensus::deserialize(&entry.btc_tx_raw).unwrap()) + .collect(); + After { + failed, + migrations, + retiring_kept: !bridge_load_bytes_named(&mut bridge.ctx, OLD_FEDERATION_KEY).is_empty(), + retiring_utxos: load_old_federation_utxos(&mut bridge.ctx).len(), + } +} + +/// rskj's `…withNewFederationAgeBeforeMigrationBegins…`, +/// `…duringMigration_withOneSpendableRetiringUtxo…`, +/// `…duringMigration_withBalanceBelowThreshold…` and +/// `…pastMigrationAge_withOneSpendableRetiringUtxo…`, between P2SH-P2WSH +/// federations: funds migrate from the block after the activation age when +/// they are more than half the fee per kB, and the federation retires at the +/// migration age's end. +#[test] +fn a_retiring_federations_funds_migrate_after_the_activation_age() { + let created = 9_000_000; + let (active, retiring) = (active(20, 4000), retiring(20, 4000)); + let begins = created + AGE; + for (at, value, migrations, kept) in [ + (begins, COIN, 0, true), + (begins + 1, COIN, 1, true), + (begins + 1, FEE_PER_KB / 2, 0, true), + (begins + MIGRATION_END, COIN, 1, false), + ] { + let held = utxos(&retiring, at, &[value]); + let after = update_collections(mainnet_at( + at, created, &active, &retiring, &held, FEE_PER_KB, + )); + assert!(!after.failed, "#{at}"); + assert_eq!(after.migrations.len(), migrations, "#{at}"); + assert_eq!(after.retiring_kept, kept, "#{at}"); + assert_eq!(after.retiring_utxos, 1 - migrations, "#{at}"); + } +} + +/// rskj's `…duringMigration_withManyMinNonDustRetiringUtxos_whenMigrationBuildFails_shouldThrowIllegalStateException`: +/// funds whose inputs cost more in fees than they hold leave in no amount +/// down to the dust limit, and `createMigrationTransaction` throws. In the +/// migration age that fails the call, and nothing it wrote stays. +#[test] +fn in_the_migration_age_funds_that_cannot_leave_fail_update_collections() { + let created = 9_000_000; + let (active, retiring) = (active(20, 4000), retiring(20, 4000)); + let during = created + AGE + 1; + let held = utxos(&retiring, during, &[MIN_NON_DUST; 5]); + let after = update_collections(mainnet_at( + during, created, &active, &retiring, &held, FEE_PER_KB, + )); + assert!(after.failed); + assert!(after.migrations.is_empty()); + assert!(after.retiring_kept); + assert_eq!(after.retiring_utxos, 5); +} + +/// rskj's `…pastMigrationAge_withMinNonDustRetiringUtxo…`, +/// `…withManyMinNonDustRetiringUtxo…` and +/// `…withHighFees_shouldClearRetiringFedWithoutMigrationTx`: at the migration +/// age's end `processFundsMigration` catches what `migrateFunds` throws, and +/// the federation retires with the funds that could not leave. +#[test] +fn past_the_migration_age_funds_that_cannot_leave_retire_with_the_federation() { + let created = 9_000_000; + let (active, retiring) = (active(20, 4000), retiring(20, 4000)); + let past = created + AGE + MIGRATION_END; + for (values, fee_per_kb) in [ + (vec![MIN_NON_DUST], FEE_PER_KB), + (vec![MIN_NON_DUST; 5], FEE_PER_KB), + (vec![COIN], 2 * COIN), + ] { + let held = utxos(&retiring, past, &values); + let after = update_collections(mainnet_at( + past, created, &active, &retiring, &held, fee_per_kb, + )); + assert!(!after.failed, "{values:?}"); + assert!(after.migrations.is_empty(), "{values:?}"); + assert!(!after.retiring_kept, "{values:?}"); + assert_eq!(after.retiring_utxos, values.len()); + } +} + +/// rskj's `createMigrationTransaction` asks bitcoinj for the whole balance, +/// then half of it, and so on, until the transaction can be built or the +/// amount is dust: when the 18,000 satoshis held in thirty-one UTXOs would +/// cost more in fees than they are, 9,000 leave from the one UTXO that holds +/// them. +#[test] +fn a_migration_asks_for_half_again_until_it_can_be_built() { + let created = 9_000_000; + let (active, retiring) = (active(9, 1000), retiring(9, 1000)); + let during = created + AGE + 1; + let mut values = vec![9_000]; + values.extend([300; 30]); + let held = utxos(&retiring, during, &values); + let after = update_collections(mainnet_at( + during, created, &active, &retiring, &held, FEE_PER_KB, + )); + assert!(!after.failed); + assert_eq!(after.migrations.len(), 1); + assert_eq!(after.migrations[0].input.len(), 1); + assert!(after.retiring_kept); + assert_eq!(after.retiring_utxos, 30); +} + +/// rskj sends a migration to `getActiveFederationAddress()`, the active +/// federation read back in the format it was stored in: a standard multisig +/// federation is still one after RSKIP201 (iris300), when a federation +/// committed would be an ERP one. +#[test] +fn a_migration_pays_the_active_federation_in_its_stored_format() { + let created = 3_700_000; + let (active, retiring) = (active(9, 1000), retiring(9, 1000)); + let during = created + LEGACY_AGE + 1; + assert!(RskHardforkConfig::mainnet().has_rskip201(during)); + let held = utxos(&retiring, during, &[COIN]); + let after = update_collections(mainnet_at( + during, created, &active, &retiring, &held, FEE_PER_KB, + )); + let keys = active.keys(); + let multisig = build_federation_redeem_script(&keys, keys.len() / 2 + 1); + let to = p2sh_output_script_program(&hash160(&multisig)); + assert_eq!(after.migrations.len(), 1); + assert_eq!( + after.migrations[0].output[0].script_pubkey.as_bytes(), + to.as_slice() + ); +} + +/// rskj's `…withMoreUtxosThanMaxInputs…`: from RSKIP294 (hop400) the retiring +/// federation's wallet holds only its first 50 UTXOs, the most inputs a +/// peg-out has, and a migration spends those; before it, all of them. +#[test] +fn from_rskip294_a_migration_spends_at_most_a_pegouts_inputs() { + let cfg = RskHardforkConfig::mainnet(); + let (active, retiring) = (active(9, 1000), retiring(9, 1000)); + for (created, inputs, left) in [(3_700_000, 51, 0), (4_700_000, 50, 1)] { + let during = created + LEGACY_AGE + 1; + assert_eq!(cfg.has_rskip294(during), inputs == 50); + let held = utxos(&retiring, during, &[COIN; 51]); + let after = update_collections(mainnet_at( + during, created, &active, &retiring, &held, FEE_PER_KB, + )); + assert_eq!(after.migrations.len(), 1, "#{during}"); + assert_eq!(after.migrations[0].input.len(), inputs, "#{during}"); + assert_eq!(after.retiring_utxos, left, "#{during}"); + } +} + +/// rskj's `FederationConstants.getFundsMigrationAgeSinceActivationEnd`: +/// between RSKIP357 (hop401) and RSKIP374 (fingerroot500) funds migrate until +/// 172,800 blocks after the activation age, not 10,585. At the usual end a +/// federation retires before hop401 and from fingerroot500, and is still +/// migrating between them. +#[test] +fn between_rskip357_and_rskip374_the_migration_age_ends_later() { + let cfg = RskHardforkConfig::mainnet(); + let (active, retiring) = (active(9, 1000), retiring(9, 1000)); + for (created, age, kept) in [ + (4_900_000, LEGACY_AGE, false), + (5_000_000, LEGACY_AGE, true), + (5_500_000, AGE, false), + ] { + let at = created + age + MIGRATION_END; + assert_eq!(cfg.has_rskip357(at) && !cfg.has_rskip374(at), kept); + let held = utxos(&retiring, at, &[COIN]); + let after = update_collections(mainnet_at( + at, created, &active, &retiring, &held, FEE_PER_KB, + )); + assert_eq!(after.migrations.len(), 1, "#{at}"); + assert_eq!(after.retiring_kept, kept, "#{at}"); + } +} + +/// rskj's `buildMigrationTransaction` keeps a migration at version 1, where +/// RSKIP201 left it, until RSKIP376 (arrowhead600) leaves it at version 2 +/// as every peg-out. +#[test] +fn from_rskip376_a_migration_transaction_is_version_2() { + let cfg = RskHardforkConfig::mainnet(); + let (active, retiring) = (active(9, 1000), retiring(9, 1000)); + for (created, version) in [(5_500_000, 1), (6_300_000, 2)] { + let during = created + AGE + 1; + assert_eq!(cfg.has_rskip376(during), version == 2); + let held = utxos(&retiring, during, &[COIN]); + let after = update_collections(mainnet_at( + during, created, &active, &retiring, &held, FEE_PER_KB, + )); + assert_eq!(after.migrations.len(), 1, "#{during}"); + assert_eq!(after.migrations[0].version.0, version, "#{during}"); + } +} diff --git a/crates/execution/src/rskj_tests/bridge_methods/bridge_test.rs b/crates/execution/src/rskj_tests/bridge_methods/bridge_test.rs new file mode 100644 index 0000000..729eb43 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_methods/bridge_test.rs @@ -0,0 +1,1897 @@ +//! rskj's `BridgeTest`: the Bridge's methods called through `Bridge.execute`, +//! each where it opens and where it closes, each by every kind of call at +//! every upgrade from orchid to lovell700 (`msgTypesAndActivations`), and the +//! union bridge's (`UnionBridgeTest`). +//! +//! rskj's tests give the Bridge a mocked `BridgeSupport`, so that a method +//! that runs is one the mock saw called. Here every method is the real one, +//! and each test gives it what it needs to run to an answer, so that a +//! method that runs is one that answers rather than throws. + +use super::support::*; +use alloy_primitives::{Address, U256}; +use RskNetworkUpgrade::{ + Arrowhead600, Arrowhead631, Fingerroot500, Hop400, Iris300, Lovell700, Orchid, Papyrus200, + Reed800, Vetiver900, Wasabi100, +}; + +const PUBLIC_KEY: &str = "039a060badbeb24bee49eb2063f616c0f0f0765d4ca646b20a88ce828f259fcdb9"; +const SIGNATURE: &str = "3045022100a0963cea7551eb3174a3470c6ed25cda901c4b1093818d4d54792b87508820220220325f93b5aecc98385a664328e68d1cec7a2a2fe81810a7692358bd870aeecb74"; +/// A mainnet P2PKH address, as the whitelist tests name it. +const BTC_ADDRESS: &str = "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa"; + +fn hex(s: &str) -> Vec { + alloy_primitives::hex::decode(s).unwrap() +} + +/// rskj's `BitcoinTestUtils.createHash(n)`: n's four bytes, low first, then +/// zeros. +fn btc_hash(n: u32) -> [u8; 32] { + let mut hash = [0u8; 32]; + hash[..4].copy_from_slice(&n.to_le_bytes()); + hash +} + +/// rskj's `PegTestUtils.createHash3(n)`: n's two bytes, low first, then zeros. +fn rsk_hash(n: u16) -> [u8; 32] { + let mut hash = [0u8; 32]; + hash[..2].copy_from_slice(&n.to_le_bytes()); + hash +} + +/// A Bitcoin block header as bitcoinj serializes it, its hashes given in the +/// order `Sha256Hash` holds them. +fn btc_header(prev: [u8; 32], merkle_root: [u8; 32], time: u32, bits: u32, nonce: u32) -> Vec { + let mut header = 1u32.to_le_bytes().to_vec(); + header.extend(prev.iter().rev()); + header.extend(merkle_root.iter().rev()); + header.extend(time.to_le_bytes()); + header.extend(bits.to_le_bytes()); + header.extend(nonce.to_le_bytes()); + header +} + +/// A partial merkle tree of a block of one transaction, `leaf`, which it +/// matches. +fn one_leaf_tree(leaf: [u8; 32]) -> Vec { + let mut tree = 1u32.to_le_bytes().to_vec(); + tree.push(1); + tree.extend_from_slice(&leaf); + tree.extend_from_slice(&[1, 0x01]); + tree +} + +/// A `uint256` argument too big for an `i64`. +fn word(value: U256) -> Arg<'static> { + Arg::Word(value.to_be_bytes()) +} + +fn one_rbtc() -> U256 { + U256::from(10u64).pow(U256::from(18)) +} + +// ---- every kind of call at every upgrade (`msgTypesAndActivations`) ---- + +/// Where rskj's test has a method open. +#[derive(Clone, Copy)] +enum Open { + Always, + /// From this upgrade on. + Since(RskNetworkUpgrade), + /// Before this upgrade only. + Until(RskNetworkUpgrade), +} + +impl Open { + fn at(self, upgrade: RskNetworkUpgrade) -> bool { + match self { + Open::Always => true, + Open::Since(first) => upgrade >= first, + Open::Until(closed) => upgrade < closed, + } + } +} + +/// The kinds of call a method takes from RSKIP417 (arrowhead600) on: rskj's +/// `RESTRICTED_TO_CALL` and `ALLOW_STATIC_CALL`. Before it, every kind. +#[derive(Clone, Copy, PartialEq)] +enum Takes { + Call, + CallOrStaticCall, +} + +const MATRIX_UPGRADES: [RskNetworkUpgrade; 8] = [ + Orchid, + Wasabi100, + Papyrus200, + Iris300, + Hop400, + Fingerroot500, + Arrowhead600, + Lovell700, +]; + +/// The `MsgType`s an opcode makes (`MsgType.fromOpcode`). rskj's fifth, POST, +/// no opcode makes, and nothing here stands for it. +const KINDS: [(&str, BridgeCallKind); 4] = [ + ("CALL", BridgeCallKind::Call), + ("CALLCODE", BridgeCallKind::DelegateOrCallCode), + ("DELEGATECALL", BridgeCallKind::DelegateOrCallCode), + ("STATICCALL", BridgeCallKind::StaticCall), +]; + +/// One of rskj's tests over `msgTypesAndActivations`: at each upgrade, by each +/// kind of call, the method throws where it is closed and, from arrowhead600, +/// where it doesn't take that kind; everywhere else it runs, and `answered` +/// looks at its answer. +fn by_every_kind_at_every_upgrade( + data: &[u8], + open: Open, + takes: Takes, + prepare: impl Fn(&mut Bridge, RskNetworkUpgrade), + answered: impl Fn(&[u8]), +) { + for upgrade in MATRIX_UPGRADES { + for (name, kind) in KINDS { + let mut bridge = Bridge::mainnet_up_to(upgrade); + bridge.kind = kind; + prepare(&mut bridge, upgrade); + let result = bridge.execute(data); + let taken = kind == BridgeCallKind::Call + || (takes == Takes::CallOrStaticCall && kind == BridgeCallKind::StaticCall); + if !open.at(upgrade) || (upgrade >= Arrowhead600 && !taken) { + assert!( + threw(&result), + "{name} at {upgrade:?} must throw: {result:?}" + ); + } else { + match result { + Ok(output) => answered(&output.bytes), + Err(e) => panic!("{name} at {upgrade:?} must run: {e:?}"), + } + } + } + } +} + +/// A transaction of a stranger's, as rskj's `BridgeBuilder` gives one. +fn as_is(_: &mut Bridge, _: RskNetworkUpgrade) {} + +/// rskj's `isLocalCallTransaction()` true, which a local-only getter needs. +fn locally(bridge: &mut Bridge, _: RskNetworkUpgrade) { + bridge.local_call = true; +} + +/// From a member of the active federation, which the genesis one is here. +fn from_a_federator(bridge: &mut Bridge, _: RskNetworkUpgrade) { + bridge.caller = genesis_federator(&bridge.constants); +} + +/// A method with nothing to answer, to a transaction's own call. +fn nothing(answer: &[u8]) { + assert!(answer.is_empty(), "{answer:?}"); +} + +/// One word: a number, a flag or an address. +fn one_word(answer: &[u8]) { + assert_eq!(answer.len(), 32, "{answer:?}"); +} + +/// Bytes or a string: where they are, how long, then what they are. +fn dynamic(answer: &[u8]) { + assert!( + answer.len() >= 64 && answer.len().is_multiple_of(32), + "{answer:?}" + ); +} + +/// A vote or a change of the whitelist from no authorizer: `-10`. +fn unauthorized(answer: &[u8]) { + assert_eq!(int_of(answer), -10); +} + +#[test] +fn add_federator_public_key_closes_at_wasabi100() { + let key = hex(PUBLIC_KEY); + let data = call_data("addFederatorPublicKey(bytes)", &[Arg::Bytes(&key)]); + by_every_kind_at_every_upgrade( + &data, + Open::Until(Wasabi100), + Takes::Call, + as_is, + unauthorized, + ); +} + +#[test] +fn add_federator_public_key_multikey_opens_at_wasabi100() { + let key = hex(PUBLIC_KEY); + let data = call_data( + "addFederatorPublicKeyMultikey(bytes,bytes,bytes)", + &[Arg::Bytes(&key), Arg::Bytes(&key), Arg::Bytes(&key)], + ); + by_every_kind_at_every_upgrade( + &data, + Open::Since(Wasabi100), + Takes::Call, + as_is, + unauthorized, + ); +} + +#[test] +fn add_lock_whitelist_address_closes_at_orchid() { + let data = call_data( + "addLockWhitelistAddress(string,int256)", + &[Arg::Bytes(BTC_ADDRESS.as_bytes()), int(100_000)], + ); + by_every_kind_at_every_upgrade(&data, Open::Until(Orchid), Takes::Call, as_is, unauthorized); +} + +#[test] +fn add_one_off_lock_whitelist_address_opens_at_orchid() { + let data = call_data( + "addOneOffLockWhitelistAddress(string,int256)", + &[Arg::Bytes(BTC_ADDRESS.as_bytes()), int(100_000)], + ); + by_every_kind_at_every_upgrade(&data, Open::Since(Orchid), Takes::Call, as_is, unauthorized); +} + +#[test] +fn add_unlimited_lock_whitelist_address_opens_at_orchid() { + let data = call_data( + "addUnlimitedLockWhitelistAddress(string)", + &[Arg::Bytes(BTC_ADDRESS.as_bytes())], + ); + by_every_kind_at_every_upgrade(&data, Open::Since(Orchid), Takes::Call, as_is, unauthorized); +} + +#[test] +fn add_signature_takes_only_a_call_from_arrowhead600() { + let (key, signature) = (hex(PUBLIC_KEY), hex(SIGNATURE)); + let data = call_data( + "addSignature(bytes,bytes[],bytes)", + &[ + Arg::Bytes(&key), + Arg::BytesArray(&[&signature]), + Arg::Bytes(&rsk_hash(1)), + ], + ); + by_every_kind_at_every_upgrade(&data, Open::Always, Takes::Call, from_a_federator, nothing); +} + +#[test] +fn commit_federation_takes_only_a_call_from_arrowhead600() { + let data = call_data("commitFederation(bytes)", &[Arg::Bytes(&rsk_hash(2))]); + by_every_kind_at_every_upgrade(&data, Open::Always, Takes::Call, as_is, unauthorized); +} + +#[test] +fn create_federation_takes_only_a_call_from_arrowhead600() { + let data = selector_only("createFederation()"); + by_every_kind_at_every_upgrade(&data, Open::Always, Takes::Call, as_is, unauthorized); +} + +#[test] +fn btc_blockchain_best_chain_height_takes_a_call_or_staticcall_from_arrowhead600() { + let data = selector_only("getBtcBlockchainBestChainHeight()"); + by_every_kind_at_every_upgrade( + &data, + Open::Always, + Takes::CallOrStaticCall, + locally, + one_word, + ); +} + +#[test] +fn btc_blockchain_initial_block_height_takes_a_call_or_staticcall_from_arrowhead600() { + let data = selector_only("getBtcBlockchainInitialBlockHeight()"); + by_every_kind_at_every_upgrade( + &data, + Open::Always, + Takes::CallOrStaticCall, + locally, + one_word, + ); +} + +#[test] +fn btc_blockchain_block_locator_closes_at_orchid() { + let data = selector_only("getBtcBlockchainBlockLocator()"); + by_every_kind_at_every_upgrade( + &data, + Open::Until(Orchid), + Takes::CallOrStaticCall, + locally, + dynamic, + ); +} + +/// rskj's test asks for depth 1000 of a mocked chain. The chain here is the +/// one the Bridge starts from, its head alone, so it asks for depth 0. +#[test] +fn btc_blockchain_block_hash_at_depth_opens_at_orchid() { + let data = call_data("getBtcBlockchainBlockHashAtDepth(int256)", &[int(0)]); + by_every_kind_at_every_upgrade( + &data, + Open::Since(Orchid), + Takes::CallOrStaticCall, + locally, + dynamic, + ); +} + +#[test] +fn btc_transaction_confirmations_open_at_wasabi100() { + let branch = [btc_hash(10), btc_hash(11), btc_hash(12)]; + let data = call_data( + "getBtcTransactionConfirmations(bytes32,bytes32,uint256,bytes32[])", + &[ + Arg::Word(btc_hash(1)), + Arg::Word(btc_hash(2)), + int(1), + Arg::Words(&branch), + ], + ); + by_every_kind_at_every_upgrade( + &data, + Open::Since(Wasabi100), + Takes::CallOrStaticCall, + as_is, + one_word, + ); +} + +#[test] +fn btc_tx_hash_processed_height_takes_a_call_or_staticcall_from_arrowhead600() { + let hash = alloy_primitives::hex::encode(btc_hash(1)); + let data = call_data( + "getBtcTxHashProcessedHeight(string)", + &[Arg::Bytes(hash.as_bytes())], + ); + by_every_kind_at_every_upgrade( + &data, + Open::Always, + Takes::CallOrStaticCall, + locally, + one_word, + ); +} + +#[test] +fn federation_address_takes_a_call_or_staticcall_from_arrowhead600() { + let data = selector_only("getFederationAddress()"); + by_every_kind_at_every_upgrade( + &data, + Open::Always, + Takes::CallOrStaticCall, + locally, + dynamic, + ); +} + +#[test] +fn federation_creation_block_number_takes_a_call_or_staticcall_from_arrowhead600() { + let data = selector_only("getFederationCreationBlockNumber()"); + by_every_kind_at_every_upgrade( + &data, + Open::Always, + Takes::CallOrStaticCall, + locally, + one_word, + ); +} + +#[test] +fn federation_creation_time_takes_a_call_or_staticcall_from_arrowhead600() { + let data = selector_only("getFederationCreationTime()"); + by_every_kind_at_every_upgrade( + &data, + Open::Always, + Takes::CallOrStaticCall, + locally, + one_word, + ); +} + +#[test] +fn federation_size_takes_a_call_or_staticcall_from_arrowhead600() { + let data = selector_only("getFederationSize()"); + by_every_kind_at_every_upgrade( + &data, + Open::Always, + Takes::CallOrStaticCall, + locally, + one_word, + ); +} + +#[test] +fn federation_threshold_takes_a_call_or_staticcall_from_arrowhead600() { + let data = selector_only("getFederationThreshold()"); + by_every_kind_at_every_upgrade( + &data, + Open::Always, + Takes::CallOrStaticCall, + locally, + one_word, + ); +} + +#[test] +fn federator_public_key_closes_at_wasabi100() { + let data = call_data("getFederatorPublicKey(int256)", &[int(1)]); + by_every_kind_at_every_upgrade( + &data, + Open::Until(Wasabi100), + Takes::CallOrStaticCall, + locally, + dynamic, + ); +} + +#[test] +fn federator_public_key_of_type_opens_at_wasabi100() { + let data = call_data( + "getFederatorPublicKeyOfType(int256,string)", + &[int(1), Arg::Bytes(b"btc")], + ); + by_every_kind_at_every_upgrade( + &data, + Open::Since(Wasabi100), + Takes::CallOrStaticCall, + locally, + dynamic, + ); +} + +#[test] +fn fee_per_kb_takes_a_call_or_staticcall_from_arrowhead600() { + let data = selector_only("getFeePerKb()"); + by_every_kind_at_every_upgrade( + &data, + Open::Always, + Takes::CallOrStaticCall, + locally, + one_word, + ); +} + +#[test] +fn lock_whitelist_address_takes_a_call_or_staticcall_from_arrowhead600() { + let data = call_data("getLockWhitelistAddress(int256)", &[int(1)]); + by_every_kind_at_every_upgrade( + &data, + Open::Always, + Takes::CallOrStaticCall, + locally, + dynamic, + ); +} + +#[test] +fn lock_whitelist_entry_by_address_opens_at_orchid() { + let data = call_data( + "getLockWhitelistEntryByAddress(string)", + &[Arg::Bytes(BTC_ADDRESS.as_bytes())], + ); + by_every_kind_at_every_upgrade( + &data, + Open::Since(Orchid), + Takes::CallOrStaticCall, + locally, + one_word, + ); +} + +#[test] +fn lock_whitelist_size_takes_a_call_or_staticcall_from_arrowhead600() { + let data = selector_only("getLockWhitelistSize()"); + by_every_kind_at_every_upgrade( + &data, + Open::Always, + Takes::CallOrStaticCall, + locally, + one_word, + ); +} + +#[test] +fn minimum_lock_tx_value_takes_a_call_or_staticcall_from_arrowhead600() { + let data = selector_only("getMinimumLockTxValue()"); + by_every_kind_at_every_upgrade( + &data, + Open::Always, + Takes::CallOrStaticCall, + locally, + one_word, + ); +} + +#[test] +fn pending_federation_hash_takes_a_call_or_staticcall_from_arrowhead600() { + let data = selector_only("getPendingFederationHash()"); + by_every_kind_at_every_upgrade( + &data, + Open::Always, + Takes::CallOrStaticCall, + locally, + dynamic, + ); +} + +#[test] +fn pending_federation_size_takes_a_call_or_staticcall_from_arrowhead600() { + let data = selector_only("getPendingFederationSize()"); + by_every_kind_at_every_upgrade( + &data, + Open::Always, + Takes::CallOrStaticCall, + locally, + one_word, + ); +} + +#[test] +fn pending_federator_public_key_closes_at_wasabi100() { + let data = call_data("getPendingFederatorPublicKey(int256)", &[int(1)]); + by_every_kind_at_every_upgrade( + &data, + Open::Until(Wasabi100), + Takes::CallOrStaticCall, + locally, + dynamic, + ); +} + +#[test] +fn pending_federator_public_key_of_type_opens_at_wasabi100() { + let data = call_data( + "getPendingFederatorPublicKeyOfType(int256,string)", + &[int(1), Arg::Bytes(b"btc")], + ); + by_every_kind_at_every_upgrade( + &data, + Open::Since(Wasabi100), + Takes::CallOrStaticCall, + locally, + dynamic, + ); +} + +#[test] +fn retiring_federation_address_takes_a_call_or_staticcall_from_arrowhead600() { + let data = selector_only("getRetiringFederationAddress()"); + by_every_kind_at_every_upgrade( + &data, + Open::Always, + Takes::CallOrStaticCall, + locally, + dynamic, + ); +} + +#[test] +fn retiring_federation_creation_block_number_takes_a_call_or_staticcall_from_arrowhead600() { + let data = selector_only("getRetiringFederationCreationBlockNumber()"); + by_every_kind_at_every_upgrade( + &data, + Open::Always, + Takes::CallOrStaticCall, + locally, + one_word, + ); +} + +#[test] +fn retiring_federation_creation_time_takes_a_call_or_staticcall_from_arrowhead600() { + let data = selector_only("getRetiringFederationCreationTime()"); + by_every_kind_at_every_upgrade( + &data, + Open::Always, + Takes::CallOrStaticCall, + locally, + one_word, + ); +} + +#[test] +fn retiring_federation_size_takes_a_call_or_staticcall_from_arrowhead600() { + let data = selector_only("getRetiringFederationSize()"); + by_every_kind_at_every_upgrade( + &data, + Open::Always, + Takes::CallOrStaticCall, + locally, + one_word, + ); +} + +#[test] +fn retiring_federation_threshold_takes_a_call_or_staticcall_from_arrowhead600() { + let data = selector_only("getRetiringFederationThreshold()"); + by_every_kind_at_every_upgrade( + &data, + Open::Always, + Takes::CallOrStaticCall, + locally, + one_word, + ); +} + +#[test] +fn retiring_federator_public_key_closes_at_wasabi100() { + let data = call_data("getRetiringFederatorPublicKey(int256)", &[int(1)]); + by_every_kind_at_every_upgrade( + &data, + Open::Until(Wasabi100), + Takes::CallOrStaticCall, + locally, + dynamic, + ); +} + +#[test] +fn retiring_federator_public_key_of_type_opens_at_wasabi100() { + let data = call_data( + "getRetiringFederatorPublicKeyOfType(int256,string)", + &[int(1), Arg::Bytes(b"btc")], + ); + by_every_kind_at_every_upgrade( + &data, + Open::Since(Wasabi100), + Takes::CallOrStaticCall, + locally, + dynamic, + ); +} + +#[test] +fn proposed_federation_address_opens_at_lovell700() { + let data = selector_only("getProposedFederationAddress()"); + by_every_kind_at_every_upgrade( + &data, + Open::Since(Lovell700), + Takes::CallOrStaticCall, + locally, + dynamic, + ); +} + +#[test] +fn proposed_federation_size_opens_at_lovell700() { + let data = selector_only("getProposedFederationSize()"); + by_every_kind_at_every_upgrade( + &data, + Open::Since(Lovell700), + Takes::CallOrStaticCall, + locally, + one_word, + ); +} + +#[test] +fn proposed_federation_creation_time_opens_at_lovell700() { + let data = selector_only("getProposedFederationCreationTime()"); + by_every_kind_at_every_upgrade( + &data, + Open::Since(Lovell700), + Takes::CallOrStaticCall, + locally, + one_word, + ); +} + +#[test] +fn proposed_federation_creation_block_number_opens_at_lovell700() { + let data = selector_only("getProposedFederationCreationBlockNumber()"); + by_every_kind_at_every_upgrade( + &data, + Open::Since(Lovell700), + Takes::CallOrStaticCall, + locally, + one_word, + ); +} + +#[test] +fn proposed_federator_public_key_of_type_opens_at_lovell700() { + let data = call_data( + "getProposedFederatorPublicKeyOfType(int256,string)", + &[int(1), Arg::Bytes(b"btc")], + ); + by_every_kind_at_every_upgrade( + &data, + Open::Since(Lovell700), + Takes::CallOrStaticCall, + locally, + dynamic, + ); +} + +#[test] +fn state_for_btc_release_client_takes_a_call_or_staticcall_from_arrowhead600() { + let data = selector_only("getStateForBtcReleaseClient()"); + by_every_kind_at_every_upgrade( + &data, + Open::Always, + Takes::CallOrStaticCall, + locally, + dynamic, + ); +} + +#[test] +fn state_for_svp_client_opens_at_lovell700() { + let data = selector_only("getStateForSvpClient()"); + by_every_kind_at_every_upgrade( + &data, + Open::Since(Lovell700), + Takes::CallOrStaticCall, + locally, + dynamic, + ); +} + +#[test] +fn state_for_debugging_takes_a_call_or_staticcall_from_arrowhead600() { + let data = selector_only("getStateForDebugging()"); + by_every_kind_at_every_upgrade( + &data, + Open::Always, + Takes::CallOrStaticCall, + locally, + dynamic, + ); +} + +#[test] +fn locking_cap_opens_at_papyrus200() { + let data = selector_only("getLockingCap()"); + by_every_kind_at_every_upgrade( + &data, + Open::Since(Papyrus200), + Takes::CallOrStaticCall, + locally, + one_word, + ); +} + +#[test] +fn active_powpeg_redeem_script_opens_at_hop400() { + let data = selector_only("getActivePowpegRedeemScript()"); + by_every_kind_at_every_upgrade( + &data, + Open::Since(Hop400), + Takes::CallOrStaticCall, + as_is, + dynamic, + ); +} + +#[test] +fn active_federation_creation_block_height_opens_at_iris300() { + let data = selector_only("getActiveFederationCreationBlockHeight()"); + by_every_kind_at_every_upgrade( + &data, + Open::Since(Iris300), + Takes::CallOrStaticCall, + as_is, + one_word, + ); +} + +/// From a stranger, so the cap stays as it is and the answer is false. +#[test] +fn increase_locking_cap_opens_at_papyrus200() { + let data = call_data("increaseLockingCap(int256)", &[int(1)]); + by_every_kind_at_every_upgrade( + &data, + Open::Since(Papyrus200), + Takes::Call, + as_is, + |answer| assert!(!bool_of(answer)), + ); +} + +#[test] +fn btc_tx_hash_already_processed_takes_a_call_or_staticcall_from_arrowhead600() { + use sha2::Digest; + let hash = alloy_primitives::hex::encode(sha2::Sha256::digest(b"btcTxHash")); + let data = call_data( + "isBtcTxHashAlreadyProcessed(string)", + &[Arg::Bytes(hash.as_bytes())], + ); + by_every_kind_at_every_upgrade( + &data, + Open::Always, + Takes::CallOrStaticCall, + locally, + one_word, + ); +} + +/// From a federator before wasabi100 and from iris300 on, where only the +/// federations may send headers; from anyone in between. +#[test] +fn receive_headers_takes_only_a_call_from_arrowhead600() { + let data = selector_only("receiveHeaders(bytes[])"); + let prepare = |bridge: &mut Bridge, upgrade| { + if !(Wasabi100..Iris300).contains(&upgrade) { + bridge.caller = genesis_federator(&bridge.constants); + } + }; + by_every_kind_at_every_upgrade(&data, Open::Always, Takes::Call, prepare, nothing); +} + +#[test] +fn receive_header_opens_at_iris300() { + let header = btc_header(btc_hash(1), btc_hash(2), 1, 100, 1); + let data = call_data("receiveHeader(bytes)", &[Arg::Bytes(&header)]); + by_every_kind_at_every_upgrade(&data, Open::Since(Iris300), Takes::Call, as_is, one_word); +} + +/// rskj's test registers `[1]` at height 0 with the tree `[2]` into a mock. +/// The real method throws for a tree of the wrong size once the transaction +/// has its confirmations, so the height here is past the chain's head, which +/// the method lets go without registering anything. +#[test] +fn register_btc_transaction_takes_only_a_call_from_arrowhead600() { + let data = call_data( + "registerBtcTransaction(bytes,int256,bytes)", + &[Arg::Bytes(&[1]), int(i64::from(i32::MAX)), Arg::Bytes(&[2])], + ); + let prepare = |bridge: &mut Bridge, upgrade| { + if upgrade < Iris300 { + bridge.caller = genesis_federator(&bridge.constants); + } + }; + by_every_kind_at_every_upgrade(&data, Open::Always, Takes::Call, prepare, nothing); +} + +#[test] +fn release_btc_takes_only_a_call_from_arrowhead600() { + let data = selector_only("releaseBtc()"); + by_every_kind_at_every_upgrade(&data, Open::Always, Takes::Call, as_is, nothing); +} + +#[test] +fn remove_lock_whitelist_address_takes_only_a_call_from_arrowhead600() { + let data = call_data( + "removeLockWhitelistAddress(string)", + &[Arg::Bytes(BTC_ADDRESS.as_bytes())], + ); + by_every_kind_at_every_upgrade(&data, Open::Always, Takes::Call, as_is, unauthorized); +} + +#[test] +fn rollback_federation_takes_only_a_call_from_arrowhead600() { + let data = selector_only("rollbackFederation()"); + by_every_kind_at_every_upgrade(&data, Open::Always, Takes::Call, as_is, unauthorized); +} + +#[test] +fn set_lock_whitelist_disable_block_delay_takes_only_a_call_from_arrowhead600() { + let data = call_data("setLockWhitelistDisableBlockDelay(int256)", &[int(100)]); + by_every_kind_at_every_upgrade(&data, Open::Always, Takes::Call, as_is, unauthorized); +} + +#[test] +fn update_collections_takes_only_a_call_from_arrowhead600() { + let data = selector_only("updateCollections()"); + by_every_kind_at_every_upgrade(&data, Open::Always, Takes::Call, from_a_federator, nothing); +} + +#[test] +fn vote_fee_per_kb_takes_only_a_call_from_arrowhead600() { + let data = call_data("voteFeePerKbChange(int256)", &[int(10_000)]); + by_every_kind_at_every_upgrade(&data, Open::Always, Takes::Call, as_is, unauthorized); +} + +/// rskj's test hands a mock an empty transaction and an empty tree. The real +/// method throws for a tree of the wrong size, so the tree here is a whole +/// one that doesn't hold the transaction, which the method lets go. +#[test] +fn register_btc_coinbase_transaction_opens_at_papyrus200() { + let tree = one_leaf_tree([0x11; 32]); + let data = call_data( + "registerBtcCoinbaseTransaction(bytes,bytes32,bytes,bytes32,bytes32)", + &[ + Arg::Bytes(&[0u8; 32]), + Arg::Word(btc_hash(1)), + Arg::Bytes(&tree), + Arg::Word(btc_hash(2)), + Arg::Word([0; 32]), + ], + ); + by_every_kind_at_every_upgrade(&data, Open::Since(Papyrus200), Takes::Call, as_is, nothing); +} + +#[test] +fn btc_block_coinbase_transaction_information_opens_at_papyrus200() { + let data = call_data( + "hasBtcBlockCoinbaseTransactionInformation(bytes32)", + &[Arg::Word(btc_hash(2))], + ); + by_every_kind_at_every_upgrade( + &data, + Open::Since(Papyrus200), + Takes::CallOrStaticCall, + as_is, + one_word, + ); +} + +/// A transaction's own call, which the Flyover's checks turn down for not +/// coming from a contract (`UNPROCESSABLE_TX_NOT_CONTRACT_ERROR`, -300). +#[test] +fn register_fast_bridge_btc_transaction_opens_at_iris300() { + let lbc = Address::from_slice(&hex("FcE93641243D1EFB6131277cCD1c0a60460d5610")); + let data = call_data( + "registerFastBridgeBtcTransaction(bytes,uint256,bytes,bytes32,bytes,address,bytes,bool)", + &[ + Arg::Bytes(&[1]), + int(1), + Arg::Bytes(&[2]), + Arg::Word(rsk_hash(2)), + Arg::Bytes(&[0u8; 21]), + address(lbc), + Arg::Bytes(&[0u8; 21]), + boolean(true), + ], + ); + by_every_kind_at_every_upgrade(&data, Open::Since(Iris300), Takes::Call, as_is, |answer| { + assert_eq!(int_of(answer), -300) + }); +} + +#[test] +fn btc_blockchain_best_block_header_opens_at_iris300() { + let data = selector_only("getBtcBlockchainBestBlockHeader()"); + by_every_kind_at_every_upgrade( + &data, + Open::Since(Iris300), + Takes::CallOrStaticCall, + as_is, + dynamic, + ); +} + +#[test] +fn btc_blockchain_block_header_by_hash_opens_at_iris300() { + let data = call_data( + "getBtcBlockchainBlockHeaderByHash(bytes32)", + &[Arg::Word([0; 32])], + ); + by_every_kind_at_every_upgrade( + &data, + Open::Since(Iris300), + Takes::CallOrStaticCall, + as_is, + dynamic, + ); +} + +#[test] +fn btc_blockchain_block_header_by_height_opens_at_iris300() { + let data = call_data("getBtcBlockchainBlockHeaderByHeight(uint256)", &[int(20)]); + by_every_kind_at_every_upgrade( + &data, + Open::Since(Iris300), + Takes::CallOrStaticCall, + as_is, + dynamic, + ); +} + +#[test] +fn btc_blockchain_parent_block_header_by_hash_opens_at_iris300() { + let data = call_data( + "getBtcBlockchainParentBlockHeaderByHash(bytes32)", + &[Arg::Word([0; 32])], + ); + by_every_kind_at_every_upgrade( + &data, + Open::Since(Iris300), + Takes::CallOrStaticCall, + as_is, + dynamic, + ); +} + +#[test] +fn next_pegout_creation_block_number_opens_at_hop400() { + let data = selector_only("getNextPegoutCreationBlockNumber()"); + by_every_kind_at_every_upgrade( + &data, + Open::Since(Hop400), + Takes::CallOrStaticCall, + as_is, + one_word, + ); +} + +#[test] +fn queued_pegouts_count_opens_at_hop400() { + let data = selector_only("getQueuedPegoutsCount()"); + by_every_kind_at_every_upgrade( + &data, + Open::Since(Hop400), + Takes::CallOrStaticCall, + as_is, + one_word, + ); +} + +#[test] +fn estimated_fees_for_next_pegout_event_open_at_hop400() { + let data = selector_only("getEstimatedFeesForNextPegOutEvent()"); + by_every_kind_at_every_upgrade( + &data, + Open::Since(Hop400), + Takes::CallOrStaticCall, + as_is, + one_word, + ); +} + +/// rskj's `assertVoidMethodResult`, which its tests apply to a transaction's +/// own call, where it can't be seen: a method with nothing to answer answers +/// null before RSKIP417 (arrowhead600) and an empty array from it. A +/// transaction's call ends the same either way; a contract's call to +/// `updateCollections` fails on the null from RSKIP197 (iris300), as +/// `Program.executePrecompiledAndHandleError` writes it, and gets the empty +/// array from arrowhead600. +#[test] +fn a_method_with_nothing_to_answer_answers_a_contract_from_arrowhead600() { + let data = selector_only("updateCollections()"); + for (upgrade, answers) in [ + (Iris300, false), + (Fingerroot500, false), + (Arrowhead600, true), + ] { + let mut bridge = Bridge::mainnet_up_to(upgrade); + bridge.caller = genesis_federator(&bridge.constants); + bridge.depth = 2; + let result = bridge.execute(&data); + if answers { + assert!(result.unwrap().bytes.is_empty()); + } else { + assert!(threw(&result), "{upgrade:?}: {result:?}"); + } + } +} + +// ---- where each method opens and closes ---- + +#[test] +fn the_active_powpeg_redeem_script_is_the_genesis_federations_from_hop400() { + let data = selector_only("getActivePowpegRedeemScript()"); + assert!(threw(&Bridge::mainnet_up_to(Iris300).execute(&data))); + + let mut bridge = Bridge::mainnet_up_to(Hop400); + let script = bytes_of(&bridge.answer(&data)); + // The genesis federation's standard multisig, built here from its keys: + // OP_m, each key sorted, OP_n, OP_CHECKMULTISIG. + let keys = crate::bridge::peg::genesis_federation_keys(&bridge.constants); + let (n, m) = (keys.len() as u8, keys.len() as u8 / 2 + 1); + let mut expected = vec![0x50 + m]; + for key in &keys { + expected.push(33); + expected.extend_from_slice(key); + } + expected.extend_from_slice(&[0x50 + n, 0xae]); + assert_eq!(script, expected); +} + +#[test] +fn the_locking_cap_is_its_initial_value_from_papyrus200() { + let data = selector_only("getLockingCap()"); + assert!(threw(&Bridge::mainnet_up_to(Wasabi100).execute(&data))); + + let mut bridge = Bridge::mainnet_up_to(Papyrus200); + bridge.local_call = true; + let cap = int_of(&bridge.answer(&data)); + assert_eq!(cap, bridge.constants.initial_locking_cap as i64); +} + +#[test] +fn increase_locking_cap_is_closed_before_papyrus200() { + let data = selector_only("increaseLockingCap(int256)"); + assert!(threw(&Bridge::mainnet_up_to(Wasabi100).execute(&data))); +} + +/// rskj's test has a mock answer true for 1 and for 210,000,000 from anyone. +/// The real method answers false to a caller who is no authorizer, and to +/// its authorizer true for a cap from the current one up to the current one +/// times the multiplier, and false on either side of that. +#[test] +fn increase_locking_cap_raises_the_cap_up_to_its_multiple_for_its_authorizer() { + let cap_data = |cap: u64| call_data("increaseLockingCap(int256)", &[int(cap as i64)]); + let mut bridge = Bridge::mainnet_up_to(Vetiver900); + for value in [1, 210_000_000] { + assert!(!bool_of(&bridge.answer(&cap_data(value)))); + } + + bridge.caller = address_of_key(bridge.constants.increase_locking_cap_authorizer_keys[0]); + let current = bridge.constants.initial_locking_cap; + let multiplier = bridge.constants.locking_cap_increments_multiplier; + assert!(!bool_of(&bridge.answer(&cap_data(current - 1)))); + assert!(!bool_of( + &bridge.answer(&cap_data(current * multiplier + 1)) + )); + assert!(bool_of(&bridge.answer(&cap_data(current * multiplier)))); + // No more than the cap is no increase, and no less is none either. + assert!(bool_of(&bridge.answer(&cap_data(current * multiplier)))); + + bridge.local_call = true; + let cap = int_of(&bridge.answer(&selector_only("getLockingCap()"))); + assert_eq!(cap, (current * multiplier) as i64); +} + +/// What rskj's four tests of an invalid cap send: a byte where a word goes, a +/// cap past a `long`, and, without the method's selector as rskj's +/// `encodeArguments` writes them, -1 and 0, which no method's selector +/// starts. The real method throws for -1 and 0 with its selector too, before +/// it asks who calls. +#[test] +fn increase_locking_cap_throws_for_a_cap_that_is_no_positive_long() { + let mut bridge = Bridge::mainnet_up_to(Vetiver900); + let selector = selector_only("increaseLockingCap(int256)"); + let past_a_long = hex("0000000000000000000000000000000000000000000000080000000000000000"); + for arguments in [hex("ab"), past_a_long] { + let data = [selector.clone(), arguments].concat(); + assert!(threw(&bridge.execute(&data))); + } + for cap in [-1, 0] { + assert!(threw(&bridge.execute(&encode::int(cap)))); + let data = call_data("increaseLockingCap(int256)", &[int(cap)]); + assert!(threw(&bridge.execute(&data))); + } +} + +#[test] +fn register_btc_coinbase_transaction_is_closed_before_papyrus200() { + let data = call_data( + "registerBtcCoinbaseTransaction(bytes,bytes32,bytes,bytes32,bytes32)", + &[ + Arg::Bytes(&[0; 32]), + int(0), + Arg::Bytes(&[0; 32]), + int(0), + int(0), + ], + ); + assert!(threw(&Bridge::mainnet_up_to(Wasabi100).execute(&data))); +} + +/// rskj's test hands a mock a 32-byte tree. The real method throws for a +/// tree of that size, so the tree here is a whole one that doesn't hold +/// the transaction, which the method lets go. +#[test] +fn register_btc_coinbase_transaction_runs_from_papyrus200() { + let tree = one_leaf_tree([0x11; 32]); + let data = call_data( + "registerBtcCoinbaseTransaction(bytes,bytes32,bytes,bytes32,bytes32)", + &[ + Arg::Bytes(&[0; 32]), + int(0), + Arg::Bytes(&tree), + int(0), + int(0), + ], + ); + let mut bridge = Bridge::mainnet_up_to(Papyrus200); + assert!(bridge.answer(&data).is_empty()); +} + +#[test] +fn register_btc_coinbase_transaction_throws_for_arguments_that_do_not_decode() { + let mut bridge = Bridge::mainnet_up_to(Papyrus200); + let selector = + selector_only("registerBtcCoinbaseTransaction(bytes,bytes32,bytes,bytes32,bytes32)"); + let past_a_long = hex("0000000000000000000000000000000000000000000000080000000000000000"); + for arguments in [vec![], hex("ab"), past_a_long] { + let data = [selector.clone(), arguments].concat(); + assert!(threw(&bridge.execute(&data))); + } +} + +fn register_zero_transaction() -> Vec { + call_data( + "registerBtcTransaction(bytes,int256,bytes)", + &[ + Arg::Bytes(&[0; 32]), + int(i64::from(i32::MAX)), + Arg::Bytes(&[0; 32]), + ], + ) +} + +#[test] +fn register_btc_transaction_turns_a_stranger_down_before_iris300() { + let mut bridge = Bridge::mainnet_up_to(Papyrus200); + bridge.caller = Address::with_last_byte(1); + assert!(threw(&bridge.execute(®ister_zero_transaction()))); +} + +#[test] +fn register_btc_transaction_takes_a_federator_before_iris300() { + let mut bridge = Bridge::mainnet_up_to(Papyrus200); + bridge.caller = genesis_federator(&bridge.constants); + assert!(bridge.answer(®ister_zero_transaction()).is_empty()); +} + +#[test] +fn register_btc_transaction_takes_anyone_from_iris300() { + let mut bridge = Bridge::mainnet_up_to(Iris300); + assert!(bridge.answer(®ister_zero_transaction()).is_empty()); +} + +#[test] +fn the_active_federation_creation_block_height_is_read_from_iris300() { + let data = selector_only("getActiveFederationCreationBlockHeight()"); + assert!(threw(&Bridge::mainnet_up_to(Papyrus200).execute(&data))); + + let mut bridge = Bridge::mainnet_up_to(Iris300); + crate::bridge::storage::bridge_store_u256( + &mut bridge.ctx, + crate::bridge::storage::ACTIVE_FEDERATION_CREATION_BLOCK_HEIGHT_KEY, + U256::from(1), + ); + assert_eq!(int_of(&bridge.answer(&data)), 1); +} + +/// rskj's `FederationSupportImplTest.getActiveFederationCreationBlockHeight_with…`: +/// from the block that is the activation age past the next federation's +/// creation block, the answer is that block, though no `updateCollections` +/// has moved it to the active federation's cell yet; before, the active +/// federation's, or 0 where neither is stored. On mainnet, at hop400's +/// activation age (18,500 blocks) and at fingerroot500's (40,320). +#[test] +fn the_active_federation_creation_block_height_is_the_next_ones_from_its_activation_age() { + use crate::bridge::serialization::rlp_encode_u64; + use crate::bridge::storage::{ + bridge_store_bytes_named, ACTIVE_FEDERATION_CREATION_BLOCK_HEIGHT_KEY, + NEXT_FEDERATION_CREATION_BLOCK_HEIGHT_KEY, + }; + let data = selector_only("getActiveFederationCreationBlockHeight()"); + let answer = |number: u64, next: Option, active: Option| { + let mut bridge = Bridge::new( + RskHardforkConfig::mainnet(), + BridgeConstants::mainnet(), + number, + ); + for (key, height) in [ + (NEXT_FEDERATION_CREATION_BLOCK_HEIGHT_KEY, next), + (ACTIVE_FEDERATION_CREATION_BLOCK_HEIGHT_KEY, active), + ] { + if let Some(height) = height { + bridge_store_bytes_named(&mut bridge.ctx, key, &rlp_encode_u64(height)); + } + } + int_of(&bridge.answer(&data)) + }; + let next = 4_700_000; + assert_eq!(answer(next + 18_499, Some(next), None), 0); + assert_eq!(answer(next + 18_499, Some(next), Some(1)), 1); + assert_eq!(answer(next + 18_500, Some(next), None), 4_700_000); + assert_eq!(answer(next + 18_500, Some(next), Some(1)), 4_700_000); + let next = 5_500_000; + assert_eq!(answer(next + 18_500, Some(next), Some(1)), 1); + assert_eq!(answer(next + 40_319, Some(next), None), 0); + assert_eq!(answer(next + 40_320, Some(next), Some(1)), 5_500_000); + assert_eq!(answer(next, None, None), 0); + assert_eq!(answer(next, None, Some(1)), 1); +} + +/// rskj's `FederationSupportImplTest.getActiveFederationCreationBlockHeight_beforeRSKIP186_returnsZero`: +/// before RSKIP-186 the federation support answers 0, whatever is stored. The +/// Bridge has the method only from RSKIP-186, so this asks the federation +/// support itself, on mainnet at block 3,000,000, before Iris. +#[test] +fn before_rskip186_the_active_federation_creation_block_height_is_zero() { + use crate::bridge::serialization::rlp_encode_u64; + use crate::bridge::storage::{ + bridge_store_bytes_named, ACTIVE_FEDERATION_CREATION_BLOCK_HEIGHT_KEY, + NEXT_FEDERATION_CREATION_BLOCK_HEIGHT_KEY, + }; + let mut bridge = Bridge::new( + RskHardforkConfig::mainnet(), + BridgeConstants::mainnet(), + 3_000_000, + ); + for (key, height) in [ + (NEXT_FEDERATION_CREATION_BLOCK_HEIGHT_KEY, 1_000_000), + (ACTIVE_FEDERATION_CREATION_BLOCK_HEIGHT_KEY, 1), + ] { + bridge_store_bytes_named(&mut bridge.ctx, key, &rlp_encode_u64(height)); + } + let output = crate::bridge::governance::get_active_federation_creation_block_height( + &mut bridge.ctx, + 3_000, + &BridgeConstants::mainnet(), + &RskHardforkConfig::mainnet(), + ) + .expect("an answer"); + assert_eq!(output.bytes.as_ref(), &[0u8; 32][..]); +} + +fn flyover_data(refund: &[u8], lbc: Address, lp: &[u8]) -> Vec { + call_data( + "registerFastBridgeBtcTransaction(bytes,uint256,bytes,bytes32,bytes,address,bytes,bool)", + &[ + Arg::Bytes(&[0; 32]), + int(1), + Arg::Bytes(&[0; 32]), + Arg::Word([0; 32]), + Arg::Bytes(refund), + address(lbc), + Arg::Bytes(lp), + boolean(true), + ], + ) +} + +#[test] +fn register_flyover_btc_transaction_is_closed_before_iris300() { + let hash160 = [0x42; 20]; + let data = flyover_data(&hash160, Address::repeat_byte(0x2e), &hash160); + assert!(threw(&Bridge::mainnet_up_to(Papyrus200).execute(&data))); +} + +/// rskj's testnet P2SH refund address, `2MyEXHyt2fXqdFm3r4xXEkTdbwdZm7qFiDP`, +/// as `serializeBtcAddressWithVersion` writes it before RSKIP284: its +/// version 196 as `BigInteger.toByteArray()` writes it, two bytes, then its +/// hash; and from RSKIP284, one byte of version. +fn p2sh_refund_address(rskip284: bool) -> Vec { + let decoded = bitcoin::base58::decode_check("2MyEXHyt2fXqdFm3r4xXEkTdbwdZm7qFiDP").unwrap(); + assert_eq!(decoded[0], 196); + if rskip284 { + decoded + } else { + [vec![0], decoded].concat() + } +} + +/// A testnet P2PKH address, version 111, for the liquidity provider. +fn p2pkh_lp_address() -> Vec { + [vec![111], vec![0x24; 20]].concat() +} + +/// On rskj's regtest (`Constants.regtest()`), at block 0 of `last`. +fn regtest_up_to(last: RskNetworkUpgrade) -> Bridge { + Bridge::new( + up_to(last, RSK_REGTEST_CHAIN_ID), + BridgeConstants::rskj_regtest(), + 0, + ) +} + +#[test] +fn a_p2sh_refund_address_is_a_generic_error_before_hop400() { + let mut bridge = regtest_up_to(Iris300); + let data = flyover_data(&p2sh_refund_address(false), STRANGER, &p2pkh_lp_address()); + assert_eq!(int_of(&bridge.answer(&data)), -900); +} + +/// rskj's test has a mock answer 2 once the address gets through. The real +/// checks it reaches turn a transaction's own call down for not coming +/// from a contract. +#[test] +fn a_p2sh_refund_address_gets_through_from_hop400() { + let mut bridge = regtest_up_to(Hop400); + let data = flyover_data(&p2sh_refund_address(true), STRANGER, &p2pkh_lp_address()); + assert_eq!(int_of(&bridge.answer(&data)), -300); +} + +/// A refund address of twenty bytes, a bare hash. +#[test] +fn a_refund_address_that_does_not_parse_is_a_generic_error() { + let mut bridge = Bridge::mainnet_up_to(Iris300); + let data = flyover_data(&[0x42; 20], STRANGER, &[0x24; 20]); + assert_eq!(int_of(&bridge.answer(&data)), -900); +} + +#[test] +fn register_flyover_btc_transaction_throws_for_arguments_that_do_not_decode() { + let mut bridge = Bridge::mainnet_up_to(Iris300); + let selector = selector_only( + "registerFastBridgeBtcTransaction(bytes,uint256,bytes,bytes32,bytes,address,bytes,bool)", + ); + assert!(threw(&bridge.execute(&selector))); + assert!(threw(&bridge.execute(&[selector, vec![0; 32]].concat()))); +} + +#[test] +fn receive_header_is_closed_before_iris300() { + let header = btc_header(btc_hash(1), btc_hash(1), 1, 0x207fffff, 1); + let data = call_data("receiveHeader(bytes)", &[Arg::Bytes(&header)]); + assert!(threw(&Bridge::mainnet_up_to(Papyrus200).execute(&data))); +} + +/// rskj's test has a mock take the header, and its answer, 0, is the mock's. +/// The real method looks for the header's parent, which no chain here has: +/// `RECEIVE_HEADER_CANT_FOUND_PREVIOUS_BLOCK`, -3. +#[test] +fn receive_header_answers_for_a_header_with_no_parent_from_iris300() { + let header = btc_header(btc_hash(1), btc_hash(2), 1, 100, 1); + let data = call_data("receiveHeader(bytes)", &[Arg::Bytes(&header)]); + let mut bridge = Bridge::mainnet_up_to(Iris300); + assert_eq!(int_of(&bridge.answer(&data)), -3); +} + +#[test] +fn receive_header_throws_for_no_argument() { + let mut bridge = Bridge::mainnet_up_to(Iris300); + assert!(threw( + &bridge.execute(&selector_only("receiveHeader(bytes)")) + )); +} + +#[test] +fn receive_headers_turns_a_stranger_down_from_iris300() { + let mut bridge = Bridge::mainnet_up_to(Iris300); + assert!(threw( + &bridge.execute(&selector_only("receiveHeaders(bytes[])")) + )); +} + +#[test] +fn a_header_of_the_wrong_size_is_answered_minus_20() { + let data = call_data("receiveHeader(bytes)", &[Arg::Bytes(&[0; 32])]); + let mut bridge = Bridge::mainnet_up_to(Iris300); + assert_eq!(int_of(&bridge.answer(&data)), -20); +} + +#[test] +fn the_next_pegout_creation_block_number_is_read_from_hop400() { + let data = selector_only("getNextPegoutCreationBlockNumber()"); + assert!(threw(&Bridge::mainnet_up_to(Iris300).execute(&data))); + + let mut bridge = Bridge::mainnet_up_to(Hop400); + crate::bridge::storage::bridge_store_u256( + &mut bridge.ctx, + crate::bridge::storage::NEXT_PEGOUT_HEIGHT_KEY, + U256::from(1), + ); + assert_eq!(int_of(&bridge.answer(&data)), 1); +} + +/// rskj's test has a mock count one peg-out. Here a peg-out is asked for, +/// with `releaseBtc` and a bitcoin, which pays the fees a peg-out of the +/// genesis federation's needs, and then counted. +#[test] +fn the_queued_pegouts_are_counted_from_hop400() { + let data = selector_only("getQueuedPegoutsCount()"); + assert!(threw(&Bridge::mainnet_up_to(Iris300).execute(&data))); + + let mut bridge = Bridge::mainnet_up_to(Hop400); + assert_eq!(int_of(&bridge.answer(&data)), 0); + let value = one_rbtc(); + bridge.credit(crate::precompiles::BRIDGE_ADDR, value); + bridge.value = value; + assert!(bridge.answer(&selector_only("releaseBtc()")).is_empty()); + bridge.value = U256::ZERO; + assert_eq!(int_of(&bridge.answer(&data)), 1); +} + +/// rskj's test has a mock answer a satoshi. With no peg-out queued and +/// before RSKIP385 (fingerroot500), the real method answers 0. +#[test] +fn the_estimated_fees_for_the_next_pegout_are_answered_from_hop400() { + let data = selector_only("getEstimatedFeesForNextPegOutEvent()"); + assert!(threw(&Bridge::mainnet_up_to(Iris300).execute(&data))); + + let mut bridge = Bridge::mainnet_up_to(Hop400); + assert_eq!(int_of(&bridge.answer(&data)), 0); +} + +/// rskj's test has a mock answer a satoshi for 1234 wei, which the real +/// method throws for: it is below the minimum peg-out. Here the amount is a +/// tenth of a bitcoin, and the federation has a bitcoin to pay it from, so +/// the simulated peg-out is built and its fee answered. +#[test] +fn the_estimated_fees_for_a_pegout_amount_are_answered_from_vetiver900() { + let mut bridge = Bridge::mainnet_up_to(Vetiver900); + let (_, redeem_script) = crate::bridge::peg::active_federation_keys_and_redeem( + &mut bridge.ctx, + &bridge.constants, + &bridge.cfg, + 0, + ); + let format = bridge.constants.genesis_federation_format; + let hash160 = crate::bridge::peg::federation_output_hash160(&redeem_script, format); + let script = [vec![0xa9, 20], hash160.to_vec(), vec![0x87]].concat(); + let utxo = crate::bridge::storage::BridgeUtxo { + tx_hash: [0x77; 32], + vout: 0, + value_satoshis: 100_000_000, + height: 1, + script, + coinbase: false, + }; + let cfg = bridge.cfg.clone(); + crate::bridge::storage::store_federation_utxos(&mut bridge.ctx, &cfg, 0, &[utxo]); + + let amount = one_rbtc() / U256::from(10); + let data = call_data("getEstimatedFeesForPegOutAmount(uint256)", &[word(amount)]); + let fee = int_of(&bridge.answer(&data)); + assert!(fee > 0, "{fee}"); +} + +// ---- the union bridge (`UnionBridgeTest`) ---- + +/// A Bridge on `chain_id`'s network at block 0 of rskj's test configuration +/// `last`, with the network's constants. +fn union_bridge(last: RskNetworkUpgrade, chain_id: u64) -> Bridge { + let constants = match chain_id { + RSK_MAINNET_CHAIN_ID => BridgeConstants::mainnet(), + RSK_TESTNET_CHAIN_ID => BridgeConstants::testnet(), + _ => BridgeConstants::rskj_regtest(), + }; + Bridge::new(up_to(last, chain_id), constants, 0) +} + +fn union_constants(bridge: &Bridge) -> crate::bridge::union::UnionConstants { + crate::bridge::union::union_constants(&bridge.cfg) +} + +const SUCCESS: i64 = 0; +const UNAUTHORIZED_CALLER: i64 = -1; +const INVALID_VALUE: i64 = -2; +const DISABLED: i64 = -3; + +/// Each of the union bridge's methods throws on lovell700, before RSKIP502 +/// opens them, on testnet and mainnet alike, even from its authorizer. +#[test] +fn the_union_bridge_methods_are_closed_before_reed810() { + for chain_id in [RSK_TESTNET_CHAIN_ID, RSK_MAINNET_CHAIN_ID] { + let mut bridge = union_bridge(Lovell700, chain_id); + let constants = union_constants(&bridge); + let new_address = Address::repeat_byte(0x5a); + let calls = [ + ( + constants.change_address_authorizer, + call_data( + "setUnionBridgeContractAddressForTestnet(address)", + &[address(new_address)], + ), + ), + (STRANGER, selector_only("getUnionBridgeContractAddress()")), + (STRANGER, selector_only("getUnionBridgeLockingCap()")), + ( + constants.change_locking_cap_authorizer, + call_data( + "increaseUnionBridgeLockingCap(uint256)", + &[word(one_rbtc() * U256::from(2))], + ), + ), + ( + constants.address, + selector_only("requestUnionBridgeRbtc(uint256)"), + ), + (constants.address, selector_only("releaseUnionBridgeRbtc()")), + ( + constants.change_transfer_permissions_authorizer, + call_data( + "setUnionBridgeTransferPermissions(bool,bool)", + &[boolean(true), boolean(true)], + ), + ), + ]; + for (caller, data) in calls { + bridge.caller = caller; + assert!(threw(&bridge.execute(&data)), "{chain_id}: {data:?}"); + } + } +} + +/// The six event methods throw on reed800, before RSKIP529 opens them. +#[test] +fn the_union_bridge_events_are_closed_before_reed810() { + let mut bridge = union_bridge(Reed800, RSK_MAINNET_CHAIN_ID); + bridge.caller = union_constants(&bridge).address; + let event = [0x56u8]; + for data in [ + selector_only("getSuperEvent()"), + call_data("setSuperEvent(bytes)", &[Arg::Bytes(&event)]), + selector_only("clearSuperEvent()"), + selector_only("getBaseEvent()"), + call_data("setBaseEvent(bytes)", &[Arg::Bytes(&event)]), + selector_only("clearBaseEvent()"), + ] { + assert!(threw(&bridge.execute(&data)), "{data:?}"); + } +} + +fn union_contract_address(bridge: &mut Bridge) -> Address { + address_of(&bridge.answer(&selector_only("getUnionBridgeContractAddress()"))) +} + +#[test] +fn the_union_bridge_contract_address_is_set_by_its_authorizer_on_testnet() { + let mut bridge = union_bridge(Vetiver900, RSK_TESTNET_CHAIN_ID); + bridge.caller = union_constants(&bridge).change_address_authorizer; + let new_address = Address::repeat_byte(0x5a); + let data = call_data( + "setUnionBridgeContractAddressForTestnet(address)", + &[address(new_address)], + ); + assert_eq!(int_of(&bridge.answer(&data)), SUCCESS); + assert_eq!(union_contract_address(&mut bridge), new_address); +} + +/// No argument reads as the zero address, which is set. +#[test] +fn no_union_bridge_contract_address_sets_the_zero_address() { + let mut bridge = union_bridge(Vetiver900, RSK_TESTNET_CHAIN_ID); + bridge.caller = union_constants(&bridge).change_address_authorizer; + let set = call_data( + "setUnionBridgeContractAddressForTestnet(address)", + &[address(Address::repeat_byte(0x5a))], + ); + assert_eq!(int_of(&bridge.answer(&set)), SUCCESS); + let data = selector_only("setUnionBridgeContractAddressForTestnet(address)"); + assert_eq!(int_of(&bridge.answer(&data)), SUCCESS); + assert_eq!(union_contract_address(&mut bridge), Address::ZERO); +} + +#[test] +fn the_union_bridge_contract_address_throws_for_anyone_but_its_authorizer() { + let mut bridge = union_bridge(Vetiver900, RSK_TESTNET_CHAIN_ID); + bridge.caller = Address::with_last_byte(1); + let data = call_data( + "setUnionBridgeContractAddressForTestnet(address)", + &[address(Address::repeat_byte(0x5a))], + ); + assert!(threw(&bridge.execute(&data))); + assert_eq!( + union_contract_address(&mut bridge), + union_constants(&bridge).address + ); +} + +/// On mainnet even its authorizer, the zero address, can't set it. +#[test] +fn the_union_bridge_contract_address_is_never_set_on_mainnet() { + let mut bridge = union_bridge(Vetiver900, RSK_MAINNET_CHAIN_ID); + bridge.caller = union_constants(&bridge).change_address_authorizer; + let data = call_data( + "setUnionBridgeContractAddressForTestnet(address)", + &[address(Address::repeat_byte(0x5a))], + ); + assert!(threw(&bridge.execute(&data))); +} + +#[test] +fn the_union_bridge_contract_address_is_its_networks() { + for chain_id in [ + RSK_REGTEST_CHAIN_ID, + RSK_TESTNET_CHAIN_ID, + RSK_MAINNET_CHAIN_ID, + ] { + let mut bridge = union_bridge(Vetiver900, chain_id); + assert_eq!( + union_contract_address(&mut bridge), + union_constants(&bridge).address + ); + } +} + +#[test] +fn the_union_bridge_locking_cap_starts_at_one_rbtc() { + for chain_id in [ + RSK_REGTEST_CHAIN_ID, + RSK_TESTNET_CHAIN_ID, + RSK_MAINNET_CHAIN_ID, + ] { + let mut bridge = union_bridge(Vetiver900, chain_id); + let cap = uint_of(&bridge.answer(&selector_only("getUnionBridgeLockingCap()"))); + assert_eq!(cap, one_rbtc()); + } +} + +fn increase_union_cap(bridge: &mut Bridge, cap: U256) -> i64 { + int_of(&bridge.answer(&call_data( + "increaseUnionBridgeLockingCap(uint256)", + &[word(cap)], + ))) +} + +#[test] +fn the_union_locking_cap_may_be_doubled_by_its_authorizer() { + for chain_id in [RSK_TESTNET_CHAIN_ID, RSK_MAINNET_CHAIN_ID] { + let mut bridge = union_bridge(Vetiver900, chain_id); + bridge.caller = union_constants(&bridge).change_locking_cap_authorizer; + assert_eq!( + increase_union_cap(&mut bridge, one_rbtc() * U256::from(2)), + SUCCESS + ); + let cap = uint_of(&bridge.answer(&selector_only("getUnionBridgeLockingCap()"))); + assert_eq!(cap, one_rbtc() * U256::from(2)); + } +} + +#[test] +fn the_union_locking_cap_may_not_rise_past_double() { + for chain_id in [RSK_TESTNET_CHAIN_ID, RSK_MAINNET_CHAIN_ID] { + let mut bridge = union_bridge(Vetiver900, chain_id); + bridge.caller = union_constants(&bridge).change_locking_cap_authorizer; + let past_double = one_rbtc() * U256::from(2) + U256::from(1); + assert_eq!(increase_union_cap(&mut bridge, past_double), INVALID_VALUE); + } +} + +#[test] +fn a_union_locking_cap_of_zero_is_an_invalid_value() { + for chain_id in [RSK_TESTNET_CHAIN_ID, RSK_MAINNET_CHAIN_ID] { + let mut bridge = union_bridge(Vetiver900, chain_id); + bridge.caller = union_constants(&bridge).change_locking_cap_authorizer; + assert_eq!(increase_union_cap(&mut bridge, U256::ZERO), INVALID_VALUE); + } +} + +/// No argument reads as a cap of zero. +#[test] +fn no_union_locking_cap_is_an_invalid_value() { + for chain_id in [RSK_TESTNET_CHAIN_ID, RSK_MAINNET_CHAIN_ID] { + let mut bridge = union_bridge(Vetiver900, chain_id); + bridge.caller = union_constants(&bridge).change_locking_cap_authorizer; + let data = selector_only("increaseUnionBridgeLockingCap(uint256)"); + assert_eq!(int_of(&bridge.answer(&data)), INVALID_VALUE); + } +} + +#[test] +fn the_union_locking_cap_throws_for_anyone_but_its_authorizer() { + let mut bridge = union_bridge(Vetiver900, RSK_MAINNET_CHAIN_ID); + bridge.caller = Address::with_last_byte(1); + let data = call_data( + "increaseUnionBridgeLockingCap(uint256)", + &[word(one_rbtc() * U256::from(2))], + ); + assert!(threw(&bridge.execute(&data))); +} + +/// On mainnet the union bridge's address and its authorizers are all the +/// zero address, so one caller does what each test needs. +fn mainnet_union_bridge() -> Bridge { + let mut bridge = union_bridge(Vetiver900, RSK_MAINNET_CHAIN_ID); + let constants = union_constants(&bridge); + assert_eq!(constants.address, constants.change_locking_cap_authorizer); + assert_eq!( + constants.address, + constants.change_transfer_permissions_authorizer + ); + bridge.caller = constants.address; + bridge +} + +fn request_union_rbtc(bridge: &mut Bridge, amount: U256) -> i64 { + int_of(&bridge.answer(&call_data( + "requestUnionBridgeRbtc(uint256)", + &[word(amount)], + ))) +} + +/// `releaseUnionBridgeRbtc` with `amount`, which the call has moved into +/// the Bridge before the Bridge runs. +fn release_union_rbtc(bridge: &mut Bridge, amount: U256) -> i64 { + bridge.credit(crate::precompiles::BRIDGE_ADDR, amount); + bridge.value = amount; + let code = int_of(&bridge.answer(&selector_only("releaseUnionBridgeRbtc()"))); + bridge.value = U256::ZERO; + code +} + +fn set_union_transfer_permissions(bridge: &mut Bridge, request: bool, release: bool) { + let data = call_data( + "setUnionBridgeTransferPermissions(bool,bool)", + &[boolean(request), boolean(release)], + ); + assert_eq!(int_of(&bridge.answer(&data)), SUCCESS); +} + +/// rskj's test has a mock answer success to a release of ten RBTC and checks +/// that nothing is sent back. Here the cap is raised to sixteen, ten are +/// asked for, which the union bridge's contract gets, and ten are returned: +/// the Bridge keeps them. +#[test] +fn a_release_of_what_was_requested_succeeds_and_sends_nothing_back() { + let mut bridge = mainnet_union_bridge(); + let union = bridge.caller; + for times in [2u64, 4, 8, 16] { + assert_eq!( + increase_union_cap(&mut bridge, one_rbtc() * U256::from(times)), + SUCCESS + ); + } + let ten = one_rbtc() * U256::from(10); + let held = bridge.balance(union); + assert_eq!(request_union_rbtc(&mut bridge, ten), SUCCESS); + assert_eq!(bridge.balance(union), held + ten); + + let bridge_held = bridge.balance(crate::precompiles::BRIDGE_ADDR); + assert_eq!(release_union_rbtc(&mut bridge, ten), SUCCESS); + assert_eq!( + bridge.balance(crate::precompiles::BRIDGE_ADDR), + bridge_held + ten + ); +} + +/// A release that fails sends the value back to whoever sent it: from +/// anyone but the union bridge's contract (`UNAUTHORIZED_CALLER`), while +/// releases are off (`RELEASE_DISABLED`), and of more than was requested +/// (`INVALID_VALUE`). +#[test] +fn a_failed_release_sends_the_value_back() { + let ten = one_rbtc() * U256::from(10); + let bridge_address = crate::precompiles::BRIDGE_ADDR; + + let mut bridge = mainnet_union_bridge(); + bridge.caller = STRANGER; + let held = bridge.balance(STRANGER); + assert_eq!(release_union_rbtc(&mut bridge, ten), UNAUTHORIZED_CALLER); + assert_eq!(bridge.balance(STRANGER), held + ten); + assert_eq!(bridge.balance(bridge_address), U256::ZERO); + + let mut bridge = mainnet_union_bridge(); + set_union_transfer_permissions(&mut bridge, true, false); + assert_eq!(release_union_rbtc(&mut bridge, ten), DISABLED); + assert_eq!(bridge.balance(bridge_address), U256::ZERO); + + let mut bridge = mainnet_union_bridge(); + assert_eq!(release_union_rbtc(&mut bridge, ten), INVALID_VALUE); + assert_eq!(bridge.balance(bridge_address), U256::ZERO); +} + +/// A request that fails sends nothing: from anyone but the union bridge's +/// contract (`UNAUTHORIZED_CALLER`), while requests are off +/// (`REQUEST_DISABLED`), past the cap (`INVALID_VALUE`), and of nothing, +/// which no argument reads as (`INVALID_VALUE`). +#[test] +fn a_failed_request_sends_nothing() { + let mut bridge = mainnet_union_bridge(); + let union = bridge.caller; + let held = bridge.balance(union); + + bridge.caller = STRANGER; + assert_eq!( + request_union_rbtc(&mut bridge, one_rbtc()), + UNAUTHORIZED_CALLER + ); + bridge.caller = union; + assert_eq!( + request_union_rbtc(&mut bridge, one_rbtc() * U256::from(2)), + INVALID_VALUE + ); + let data = selector_only("requestUnionBridgeRbtc(uint256)"); + assert_eq!(int_of(&bridge.answer(&data)), INVALID_VALUE); + set_union_transfer_permissions(&mut bridge, false, true); + assert_eq!(request_union_rbtc(&mut bridge, one_rbtc()), DISABLED); + + assert_eq!(bridge.balance(union), held); +} + +#[test] +fn the_union_transfer_permissions_throw_for_anyone_but_their_authorizer() { + for chain_id in [RSK_TESTNET_CHAIN_ID, RSK_MAINNET_CHAIN_ID] { + let mut bridge = union_bridge(Vetiver900, chain_id); + bridge.caller = Address::with_last_byte(1); + let data = call_data( + "setUnionBridgeTransferPermissions(bool,bool)", + &[boolean(true), boolean(true)], + ); + assert!(threw(&bridge.execute(&data))); + } +} + +#[test] +fn arrowhead631_is_on_the_ladder_of_lovell700() { + // rskj's lovell700() has arrowhead631's RSKIP434, as `up_to` schedules it. + let cfg = mainnet_up_to(Lovell700); + assert_eq!(cfg.activation_height(Arrowhead631), Some(0)); + assert_eq!(cfg.activation_height(RskNetworkUpgrade::Reed810), None); +} diff --git a/crates/execution/src/rskj_tests/bridge_methods/mod.rs b/crates/execution/src/rskj_tests/bridge_methods/mod.rs new file mode 100644 index 0000000..fa49893 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_methods/mod.rs @@ -0,0 +1,11 @@ +//! The Bridge called through its methods (rskj's `BridgeTest` and `BridgeIT`), +//! Flyover (rskj's `BridgeSupportFlyoverTest`), the retiring federation's +//! funds migration (rskj's `BridgeSupportProcessFundsMigrationTest`), and the +//! Bitcoin store seeded from its checkpoint (rskj's `BridgeSupportIT`). + +mod bridge_it; +mod bridge_support_flyover_test; +mod bridge_support_it; +mod bridge_support_process_funds_migration_test; +mod bridge_test; +mod support; diff --git a/crates/execution/src/rskj_tests/bridge_methods/support.rs b/crates/execution/src/rskj_tests/bridge_methods/support.rs new file mode 100644 index 0000000..c173b31 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_methods/support.rs @@ -0,0 +1,342 @@ +//! What the ported tests of the Bridge's methods share: a Bridge to call at a +//! block of an upgrade ladder, the calls rskj's tests make to it, and how its +//! answers read. +//! +//! rskj's `BridgeBuilder` builds a Bridge over its genesis block, with an +//! `ActivationConfig` that has every RSKIP of the upgrades up to the one a test +//! names active from block 0 (`ActivationConfigsForTest`), a transaction and +//! the kind of call that reaches the Bridge (`MessageCall.MsgType`). Here the +//! ladder is rustock's own, scheduled the same way ([`up_to`]), and the Bridge +//! is called through `execute_bridge`, as the executor and the precompile +//! frame call it. + +use alloy_primitives::{Address, U256}; +use revm::context_interface::{ContextTr, JournalTr}; +use revm::precompile::{PrecompileError, PrecompileOutput}; + +pub(super) use crate::abi::encode::{self, Arg}; +pub(super) use crate::bridge::constants::BridgeConstants; +pub(super) use crate::bridge::BridgeCallKind; +pub(super) use crate::hardfork::{ + RskHardforkConfig, RskNetworkUpgrade, RSK_MAINNET_CHAIN_ID, RSK_REGTEST_CHAIN_ID, + RSK_TESTNET_CHAIN_ID, +}; +use crate::precompiles::BridgeTxContext; + +/// The world a ported test calls the Bridge in: no trie and no database, only +/// what the test and the Bridge write. +pub(super) type Ctx = revm::Context< + revm::context::BlockEnv, + revm::context::TxEnv, + revm::context::CfgEnv, + revm::database::EmptyDB, + revm::Journal, + crate::raw_storage::RskChainExt, +>; + +/// The upgrades after genesis, in order. +const LADDER: [RskNetworkUpgrade; 13] = [ + RskNetworkUpgrade::Orchid, + RskNetworkUpgrade::Wasabi100, + RskNetworkUpgrade::TwoToThree, + RskNetworkUpgrade::Papyrus200, + RskNetworkUpgrade::Iris300, + RskNetworkUpgrade::Hop400, + RskNetworkUpgrade::Fingerroot500, + RskNetworkUpgrade::Arrowhead600, + RskNetworkUpgrade::Arrowhead631, + RskNetworkUpgrade::Lovell700, + RskNetworkUpgrade::Reed800, + RskNetworkUpgrade::Reed810, + RskNetworkUpgrade::Vetiver900, +]; + +/// rskj's `ActivationConfigsForTest.()`: every upgrade up to `last` +/// active from block 0 and none after it, on the network `chain_id` names. +/// `Genesis` is rskj's `genesis()`, with nothing active, and `Vetiver900` +/// its `all()`, which has reed810's rules too. +pub(super) fn up_to(last: RskNetworkUpgrade, chain_id: u64) -> RskHardforkConfig { + LADDER + .into_iter() + .fold(RskHardforkConfig::all_active(chain_id), |cfg, upgrade| { + cfg.with_hardfork(upgrade, (upgrade <= last).then_some(0)) + }) +} + +/// [`up_to`] on mainnet, whose constants rskj's `BridgeBuilder` takes unless +/// a test gives others. +pub(super) fn mainnet_up_to(last: RskNetworkUpgrade) -> RskHardforkConfig { + up_to(last, RSK_MAINNET_CHAIN_ID) +} + +/// A Bridge to call, as rskj's `BridgeBuilder` builds one: an execution block, +/// the transaction that reaches the Bridge and the kind of call it is. +pub(super) struct Bridge { + pub(super) ctx: Ctx, + pub(super) cfg: RskHardforkConfig, + pub(super) constants: BridgeConstants, + /// Who calls: the transaction's sender, or the contract that calls. + pub(super) caller: Address, + /// rskj's `MessageCall.MsgType`. + pub(super) kind: BridgeCallKind, + /// rskj's `Transaction.isLocalCallTransaction`: an `eth_call`. + pub(super) local_call: bool, + /// 1 for a transaction's own call, 2 for a contract's + /// (`BridgeUtils.isContractTx`). + pub(super) depth: usize, + /// What the call carries. + pub(super) value: U256, +} + +/// Nobody in particular: no federator, no authorizer. +pub(super) const STRANGER: Address = Address::repeat_byte(0x99); + +impl Bridge { + /// The Bridge at block `number` of `cfg`, under `constants`, called with a + /// plain CALL by a transaction of [`STRANGER`]'s. + pub(super) fn new(cfg: RskHardforkConfig, constants: BridgeConstants, number: u64) -> Self { + use revm::MainContext; + let mut ctx: Ctx = + revm::Context::mainnet().with_chain(crate::raw_storage::RskChainExt::default()); + ctx.block.number = U256::from(number); + ctx.block.timestamp = U256::from(1_700_000_000u64); + Self { + ctx, + cfg, + constants, + caller: STRANGER, + kind: BridgeCallKind::Call, + local_call: false, + depth: 1, + value: U256::ZERO, + } + } + + /// At block 0 of rskj's `ActivationConfigsForTest.()` on mainnet. + pub(super) fn mainnet_up_to(last: RskNetworkUpgrade) -> Self { + Self::new(mainnet_up_to(last), BridgeConstants::mainnet(), 0) + } + + /// The number of the block the Bridge runs in. + pub(super) fn number(&self) -> u64 { + revm::context_interface::Block::number(self.ctx.block()).to::() + } + + /// rskj's `bridge.execute(data)`, with all the gas it asks for. The call's + /// writes are kept when it returns and dropped when it fails, as the + /// executor and the precompile frame keep and drop them. What a test wrote + /// before the call is the state the call starts from, and stays. + pub(super) fn execute(&mut self, data: &[u8]) -> Result { + self.ctx.chain_mut().raw_storage.commit_call(); + let number = self.number(); + let tx = BridgeTxContext { + rsk_sender: self.caller, + local_call: self.local_call, + ..Default::default() + }; + let checkpoints: Vec<_> = (0..self.depth) + .map(|_| self.ctx.journal_mut().checkpoint()) + .collect(); + let result = crate::bridge::execute_bridge( + &mut self.ctx, + data, + u64::MAX, + &self.constants, + None, + self.cfg.has_stored_block_v2(number), + &self.cfg, + &tx, + self.caller, + self.depth, + self.kind, + self.value, + ); + if result.is_ok() { + self.ctx.chain_mut().raw_storage.commit_call(); + for _ in &checkpoints { + self.ctx.journal_mut().checkpoint_commit(); + } + } else { + self.ctx.chain_mut().raw_storage.discard_call(); + for checkpoint in checkpoints.into_iter().rev() { + self.ctx.journal_mut().checkpoint_revert(checkpoint); + } + } + result + } + + /// [`Bridge::execute`] of a call that must return: what it answered. + pub(super) fn answer(&mut self, data: &[u8]) -> Vec { + match self.execute(data) { + Ok(output) => output.bytes.to_vec(), + Err(e) => panic!("the call failed: {e:?}"), + } + } + + /// What `address` holds. + pub(super) fn balance(&mut self, address: Address) -> U256 { + use revm::context_interface::journaled_state::account::JournaledAccountTr; + *self + .ctx + .journal_mut() + .load_account_mut(address) + .unwrap() + .balance() + } + + /// Gives `address` `amount` more, as a call's value reaches its callee + /// before the callee runs. + pub(super) fn credit(&mut self, address: Address, amount: U256) { + use revm::context_interface::journaled_state::account::JournaledAccountTr; + let held = self.balance(address); + self.ctx + .journal_mut() + .load_account_mut(address) + .unwrap() + .set_balance(held + amount); + } +} + +/// `Function.encode(args)`: the selector of `signature`, then its arguments. +pub(super) fn call_data(signature: &str, args: &[Arg]) -> Vec { + let mut data = selector_only(signature); + data.extend(encode::args(args)); + data +} + +/// `Function.encodeSignature()`: the selector alone, no arguments. The +/// signature must be one of the Bridge's, or a misspelt one would read as a +/// method refused. +pub(super) fn selector_only(signature: &str) -> Vec { + let selector = crate::abi::selector(signature); + assert!( + crate::bridge::find_bridge_method(&selector).is_some(), + "{signature} is no method of the Bridge's" + ); + selector.to_vec() +} + +/// An integer argument. +pub(super) fn int(n: i64) -> Arg<'static> { + Arg::Word(encode::int(n)) +} + +/// An `address` argument. +pub(super) fn address(a: Address) -> Arg<'static> { + let mut word = [0u8; 32]; + word[12..].copy_from_slice(a.as_slice()); + Arg::Word(word) +} + +/// A `bool` argument. +pub(super) fn boolean(b: bool) -> Arg<'static> { + Arg::Word(encode::int(i64::from(b))) +} + +/// Whether the call threw as rskj's `Bridge.execute` throws a `VMException`: +/// the failure a transaction's own call or a contract's call is given, its +/// cost spent and nothing it wrote kept. +pub(super) fn threw(result: &Result) -> bool { + matches!( + result, + Err(e) if crate::bridge::invisible_exception_gas(e).is_some() + || crate::bridge::internal_throw_gas(e).is_some() + ) +} + +/// An `int256` answer, as a Java `long`. +pub(super) fn int_of(answer: &[u8]) -> i64 { + assert_eq!(answer.len(), 32, "an int256 answer is one word"); + i64::from_be_bytes(answer[24..32].try_into().unwrap()) +} + +/// A `uint256` answer. +pub(super) fn uint_of(answer: &[u8]) -> U256 { + assert_eq!(answer.len(), 32, "a uint256 answer is one word"); + U256::from_be_slice(answer) +} + +/// A `bool` answer. +pub(super) fn bool_of(answer: &[u8]) -> bool { + uint_of(answer) != U256::ZERO +} + +/// An `address` answer. +pub(super) fn address_of(answer: &[u8]) -> Address { + assert_eq!(answer.len(), 32, "an address answer is one word"); + Address::from_slice(&answer[12..]) +} + +/// A `bytes` answer: its offset, its length, then its data. +pub(super) fn bytes_of(answer: &[u8]) -> Vec { + let offset = uint_of(&answer[..32]).to::(); + let len = uint_of(&answer[offset..offset + 32]).to::(); + answer[offset + 32..offset + 32 + len].to_vec() +} + +/// A `string` answer. +pub(super) fn string_of(answer: &[u8]) -> String { + String::from_utf8(bytes_of(answer)).expect("a string answer is UTF-8") +} + +/// A `string[]` answer: where the array is, how many, where each string is +/// from the first head, then each string. +pub(super) fn strings_of(answer: &[u8]) -> Vec { + let offset = uint_of(&answer[..32]).to::(); + let count = uint_of(&answer[offset..offset + 32]).to::(); + let heads = offset + 32; + (0..count) + .map(|i| { + let head = heads + 32 * i; + let at = heads + uint_of(&answer[head..head + 32]).to::(); + let len = uint_of(&answer[at..at + 32]).to::(); + String::from_utf8(answer[at + 32..at + 32 + len].to_vec()).unwrap() + }) + .collect() +} + +/// The RSK address of a public key, given in hex. +pub(super) fn address_of_key(hex: &str) -> Address { + let key = alloy_primitives::hex::decode(hex).unwrap(); + crate::bridge::federation::rsk_address_from_public_key(&key).unwrap() +} + +/// The RSK address of a member of `constants`' genesis federation, which is +/// the active one while no other is stored. +pub(super) fn genesis_federator(constants: &BridgeConstants) -> Address { + let keys = crate::bridge::peg::genesis_federation_keys(constants); + crate::bridge::federation::rsk_address_from_public_key(&keys[0]).unwrap() +} + +/// The compressed public key of a private key. +pub(super) fn public_key_of(private: &[u8; 32]) -> [u8; 33] { + use k256::elliptic_curve::sec1::ToEncodedPoint; + let key = k256::SecretKey::from_slice(private).unwrap(); + key.public_key() + .to_encoded_point(true) + .as_bytes() + .try_into() + .unwrap() +} + +/// rskj's `BitcoinTestUtils.getBtcEcKeyFromSeed`: the key whose private part +/// is the seed's Keccak-256 hash. +pub(super) fn key_from_seed(seed: &str) -> [u8; 33] { + use sha3::Digest; + public_key_of(&sha3::Keccak256::digest(seed.as_bytes()).into()) +} + +/// rskj's `BtcECKey.fromPrivate(Hex.decode(hex))`: the key of a private +/// number written in a few hex digits. +pub(super) fn key_of_private(hex: &str) -> [u8; 33] { + let digits = alloy_primitives::hex::decode(hex).unwrap(); + let mut private = [0u8; 32]; + private[32 - digits.len()..].copy_from_slice(&digits); + public_key_of(&private) +} + +/// RIPEMD-160 of SHA-256, Bitcoin's hash of a key or a script. +pub(super) fn hash160(data: &[u8]) -> [u8; 20] { + use ripemd::Digest as _; + ripemd::Ripemd160::digest(sha2::Sha256::digest(data)).into() +} diff --git a/crates/execution/src/rskj_tests/bridge_pegins/bitcoin_utils_test.rs b/crates/execution/src/rskj_tests/bridge_pegins/bitcoin_utils_test.rs new file mode 100644 index 0000000..9171dc6 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegins/bitcoin_utils_test.rs @@ -0,0 +1,194 @@ +//! rskj's `BitcoinUtilsTest` (`co/rsk/peg/bitcoin/BitcoinUtilsTest.java`), its +//! listed tests of where a coinbase transaction's witness commitment is +//! found, `BitcoinUtils.findWitnessCommitment`, before and from RSKIP460 +//! (lovell700). +//! +//! rskj's tests read three coinbase transactions, two of them mined, with an +//! output whose script bitcoinj can't parse: before RSKIP460 the search +//! throws a `ScriptException`, and from it the search skips that output and +//! finds the commitment. rustock looks for it inside +//! `registerBtcCoinbaseTransaction`, which stores the block's witness root +//! only when the commitment found is the one the root and the reserved value +//! make. Each port therefore registers rskj's transaction with the 32 bytes +//! rskj finds replaced by the commitment of a witness root chosen here, every +//! other byte as rskj has it: the call stores the root only if the +//! commitment is found where rskj finds it. +//! +//! Read in full. Its listed test of `getMultiSigTransactionHashWithoutSignatures` +//! from RSKIP305 is held by `bridge/peg.rs`'s +//! `svp_hashes_strip_as_rskj_before_and_after_rskip305`. Its other tests are +//! of the sighashes, redeem scripts and signatures of peg-outs and +//! migrations, the peg-out ports' part, and of commitments in well-formed +//! coinbases, which `bridge/tx.rs`'s tests hold. + +use sha2::Digest as _; + +use crate::abi::encode::{self, Arg}; +use crate::bridge::constants::BridgeConstants; +use crate::bridge::{btc_chain, tx}; +use crate::hardfork::{RskHardforkConfig, RskNetworkUpgrade, RSK_MAINNET_CHAIN_ID}; + +use super::support::*; + +/// rskj's `malFormedTestnetCoinbaseTx`, testnet's +/// ae0a6c774908d3ddd334d40cc385ef1c2ad7e6381a69058114899f5ce567f26c. Its last +/// output pushes 75 bytes where 39 are left. +const MALFORMED_TESTNET: &str = "010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff32030a0e250004fee5346404196a763b0cc3dd196400000000000000000a636b706f6f6c0e2f6d696e65642062792072736b2fffffffff039cce2a00000000001976a914ec2f9ffaba0d68ea6bd7c25cedfe2ae710938e6088ac0000000000000000266a24aa21a9ede46b6d3bc71412e8905cedfad91532bdccb693d93a1335fee0b6223a7ed1ee5b00000000000000002a6a52534b424c4f434b3a8bc552daa25a7a473ac4640ba2b9d621c95652c61488143ca02bbf1b00392fb10120000000000000000000000000000000000000000000000000000000000000000000000000"; + +/// rskj's `malFormedMainnetCoinbaseTx`, mainnet's +/// 079e68032d9a4cdb222f464b9966756ccb749633aee678c6a51536b4fc38e29c. Its last +/// output pushes 37 bytes where 16 are left. +const MALFORMED_MAINNET: &str = "010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff3c03ebba0c040c93ef652f4d41524120506f6f6c202876303232373234292f76649b3c094f135bf4b83108c14ea85f12307cd4bf00d6010000ffffffffffffffff0371c71a27000000001976a9142fc701e2049ee4957b07134b6c1d771dd5a96b2188ac0000000000000000266a24aa21a9ed5e4aae37309d88e9f49d3a4c6fb424e491cbdbac754b8ef06bb90d2a149bd96900000000000000002cfabe6d6d9797129041127735e99e277241fbc539b327b16ad3e8537125cdc12ccf2d0586010000000000000001200000000000000000000000000000000000000000000000000000000000000000e5a28d27"; + +/// rskj's `coinbaseTxWithWitnessCommitmentOutputInNonStandardFormat`: its last +/// output is a commitment followed by four bytes, the last two of which push +/// 57 bytes where one is left. +const NON_STANDARD: &str = "010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff32030a0e250004fee5346404196a763b0cc3dd196400000000000000000a636b706f6f6c0e2f6d696e65642062792072736b2fffffffff039cce2a00000000001976a914ec2f9ffaba0d68ea6bd7c25cedfe2ae710938e6088ac0000000000000000266a24aa21a9ede46b6d3bc71412e8905cedfad91532bdccb693d93a1335fee0b6223a7ed1ee5b00000000000000002a6a24aa21a9ed4f434b3a8bc552daa25a7a473ac4640ba2b9d621c95652c61488143ca02bbf1b00392fb10120000000000000000000000000000000000000000000000000000000000000000000000000"; + +/// Each transaction, and the commitment rskj finds in it from RSKIP460. +const TRANSACTIONS: [(&str, &str); 3] = [ + ( + MALFORMED_TESTNET, + "e46b6d3bc71412e8905cedfad91532bdccb693d93a1335fee0b6223a7ed1ee5b", + ), + ( + MALFORMED_MAINNET, + "5e4aae37309d88e9f49d3a4c6fb424e491cbdbac754b8ef06bb90d2a149bd969", + ), + ( + NON_STANDARD, + "4f434b3a8bc552daa25a7a473ac4640ba2b9d621c95652c61488143ca02bbf1b", + ), +]; + +/// The witness root registered, in the order the call is given it. +const WITNESS_ROOT: [u8; 32] = [0x3c; 32]; + +/// The witness reserved value the transactions' coinbase inputs carry. +const RESERVED: [u8; 32] = [0; 32]; + +/// rskj's `validateWitnessInformation`: the commitment a witness root and a +/// reserved value make, `Sha256Hash.twiceOf(root.getReversedBytes(), reserved)`. +fn commitment(root: [u8; 32], reserved: [u8; 32]) -> [u8; 32] { + let mut data = root.to_vec(); + data.reverse(); + data.extend_from_slice(&reserved); + sha2::Sha256::digest(sha2::Sha256::digest(&data)).into() +} + +/// `raw` with the commitment header and `found` after it, the bytes rskj +/// finds, followed by `replacement` instead: found once and only once. +fn replacing(raw: &[u8], found: &[u8; 32], replacement: &[u8; 32]) -> Vec { + let mut needle = vec![0xaa, 0x21, 0xa9, 0xed]; + needle.extend_from_slice(found); + let at: Vec = raw + .windows(needle.len()) + .enumerate() + .filter(|(_, window)| *window == needle) + .map(|(at, _)| at + 4) + .collect(); + assert_eq!( + at.len(), + 1, + "the commitment rskj finds is in the transaction once" + ); + let mut replaced = raw.to_vec(); + replaced[at[0]..at[0] + 32].copy_from_slice(replacement); + replaced +} + +/// `registerBtcCoinbaseTransaction` of the coinbase `raw`, held in a mainnet +/// block, at RSK block 10 of the ladder up to `last`, with [`WITNESS_ROOT`]: +/// whether it returned, and the witness root then stored for the block. +fn register_coinbase(raw: &[u8], last: RskNetworkUpgrade) -> (bool, Option<[u8; 32]>) { + let (config, cfg) = ( + BridgeConstants::mainnet(), + up_to(last, RSK_MAINNET_CHAIN_ID), + ); + let mut ctx = ctx_at(10); + let header = hold_with_root( + &mut ctx, + tx::calculate_btc_tx_hash(raw), + 850_000, + 850_000, + &cfg, + ); + let block_hash = btc_chain::bitcoin_hash_to_b256(&header.block_hash()).0; + let tree = one_leaf_tree(raw); + let args = encode::args(&[ + Arg::Bytes(raw), + Arg::Word(block_hash), + Arg::Bytes(&tree), + Arg::Word(WITNESS_ROOT), + Arg::Word(RESERVED), + ]); + let returned = tx::register_btc_coinbase_transaction(&mut ctx, &args, 0, &config, &cfg).is_ok(); + ( + returned, + tx::get_coinbase_information(&mut ctx, &block_hash), + ) +} + +/// Whether the ladder up to `last` has RSKIP460. +fn has_rskip460(last: RskNetworkUpgrade) -> bool { + let cfg: RskHardforkConfig = up_to(last, RSK_MAINNET_CHAIN_ID); + cfg.has_rskip460(10) +} + +/// The transaction `raw`, with what rskj finds in it from RSKIP460 replaced +/// by [`WITNESS_ROOT`]'s commitment. +fn committing_to_the_root(raw: &str, found: &str) -> Vec { + let raw = hex::decode(raw).expect("a transaction in hex"); + let found: [u8; 32] = hex::decode(found) + .expect("hex") + .try_into() + .expect("32 bytes"); + let replaced = replacing(&raw, &found, &commitment(WITNESS_ROOT, RESERVED)); + // Still a coinbase rust-bitcoin reads, with the scripts as they were. + let read: bitcoin::Transaction = + bitcoin::consensus::deserialize(&replaced).expect("a transaction"); + assert!(read.is_coinbase()); + replaced +} + +/// rskj `findWitnessCommitment_whenMalFormedTxBeforeRSKIP460_shouldThrowException`: +/// before RSKIP460 (arrowhead600), the search reads the last output's script +/// first, which bitcoinj can't parse, and throws: the call fails and no +/// witness root is stored. +#[test] +fn before_rskip460_a_coinbase_with_a_script_bitcoinj_cannot_parse_is_refused() { + assert!(!has_rskip460(RskNetworkUpgrade::Arrowhead600)); + for (raw, found) in TRANSACTIONS { + let raw = committing_to_the_root(raw, found); + assert_eq!( + register_coinbase(&raw, RskNetworkUpgrade::Arrowhead600), + (false, None), + "{found}" + ); + } +} + +/// rskj `findWitnessCommitment_whenMalFormedTxAfterRSKIP460_shouldIgnoreMalformedOutputAndReturnFoundWitnessCommitment`: +/// from RSKIP460 (every upgrade on), the output bitcoinj can't parse is +/// skipped and the commitment found is the one rskj's test names, the last +/// one, standard or not: the call stores the witness root it was given. +#[test] +fn from_rskip460_the_unparseable_output_is_skipped_and_the_commitment_found() { + assert!(has_rskip460(RskNetworkUpgrade::Vetiver900)); + for (raw, found) in TRANSACTIONS { + let raw = committing_to_the_root(raw, found); + assert_eq!( + register_coinbase(&raw, RskNetworkUpgrade::Vetiver900), + (true, Some(WITNESS_ROOT)), + "{found}" + ); + } + // The transaction as mined commits to no root of ours: the commitment + // found isn't this root's, and the call fails. + let (raw, _) = TRANSACTIONS[0]; + let mined = hex::decode(raw).expect("hex"); + assert_eq!( + register_coinbase(&mined, RskNetworkUpgrade::Vetiver900), + (false, None) + ); +} diff --git a/crates/execution/src/rskj_tests/bridge_pegins/bridge_constants_test.rs b/crates/execution/src/rskj_tests/bridge_pegins/bridge_constants_test.rs new file mode 100644 index 0000000..e8eb739 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegins/bridge_constants_test.rs @@ -0,0 +1,66 @@ +//! rskj's `BridgeConstantsTest` (`co/rsk/peg/constants/BridgeConstantsTest.java`). +//! +//! Read in full. Its other tests check each network's constants: the minimum +//! peg-out value and the pegout index's two heights, which +//! `bridge/constants.rs`'s own tests hold, the value of an SVP fund +//! transaction's outputs and the proposed federation's Flyover prefix, which +//! are values and no limit, and which constants object rskj builds for each +//! network, which has no counterpart here. + +use crate::bridge::constants::BridgeConstants; +use crate::hardfork::{ + RskNetworkUpgrade, RSK_MAINNET_CHAIN_ID, RSK_REGTEST_CHAIN_ID, RSK_TESTNET_CHAIN_ID, +}; + +use super::support::*; + +/// A whitelisted P2PKH sender's peg-in of `amount` satoshis to `config`'s +/// genesis federation, registered at RSK block 10 of the ladder up to `last` +/// on `chain_id`'s network, at a Bitcoin height `config` reads no block below: +/// whether the sender was credited. +fn credited(config: &BridgeConstants, chain_id: u64, last: RskNetworkUpgrade, amount: u64) -> bool { + let cfg = up_to(last, chain_id); + let sender = public_key(&[0x51]); + let pegin = tx( + vec![p2pkh_spend(&sender)], + &[(amount, genesis_federation_output(config))], + ); + let height = config.btc_height_when_block_index_activates + 100_000; + let mut ctx = bridge_holding(&pegin, height, 10, config, &cfg); + whitelist_once(&mut ctx, hash160(&sender), amount); + let registered = register(&mut ctx, &pegin, i64::from(height), config, &cfg); + assert!(registered.returned); + let credited = balance(&mut ctx, rsk_address_of(&sender)) == wei(amount); + // A peg-in under the minimum is no peg-in: nothing of it is kept. + assert_eq!(registered.processed, credited, "{amount} on {chain_id}"); + credited +} + +/// rskj `getMinimumPeginTxValue`: the minimum a peg-in must reach is the +/// legacy one, 0.01 BTC, before RSKIP219, and 0.005 BTC from it, on mainnet, +/// testnet and regtest. Run at papyrus200 and at iris300, which brings +/// RSKIP219. +#[test] +fn the_minimum_peg_in_is_the_legacy_one_before_rskip219_and_the_new_one_from_it() { + use RskNetworkUpgrade::{Iris300, Papyrus200}; + for (config, chain_id) in [ + (BridgeConstants::mainnet(), RSK_MAINNET_CHAIN_ID), + (BridgeConstants::testnet(), RSK_TESTNET_CHAIN_ID), + (BridgeConstants::rskj_regtest(), RSK_REGTEST_CHAIN_ID), + ] { + assert!(!up_to(Papyrus200, chain_id).has_rskip219(10)); + assert!(up_to(Iris300, chain_id).has_rskip219(10)); + let (legacy, minimum) = ( + config.legacy_minimum_pegin_tx_value, + config.minimum_pegin_tx_value, + ); + assert_eq!((legacy, minimum), (1_000_000, 500_000)); + + assert!(!credited(&config, chain_id, Papyrus200, legacy - 1)); + assert!(credited(&config, chain_id, Papyrus200, legacy)); + assert!(!credited(&config, chain_id, Papyrus200, minimum)); + + assert!(!credited(&config, chain_id, Iris300, minimum - 1)); + assert!(credited(&config, chain_id, Iris300, minimum)); + } +} diff --git a/crates/execution/src/rskj_tests/bridge_pegins/bridge_costs_test.rs b/crates/execution/src/rskj_tests/bridge_pegins/bridge_costs_test.rs new file mode 100644 index 0000000..3126b4e --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegins/bridge_costs_test.rs @@ -0,0 +1,117 @@ +//! rskj's `BridgeCostsTest` (`co/rsk/peg/BridgeCostsTest.java`): what a call +//! of the Bridge costs before it runs, rskj's `Bridge.getGasForData`. +//! +//! Read in full. Besides `receiveHeaders`' cost it holds each method's fixed +//! cost, which the ports of `BridgeTest` hold, a free Bridge transaction's, +//! which is the executor's, and the cost of a call that names no method, +//! ported here. + +use crate::abi::encode::{self, Arg}; +use crate::bridge::constants::BridgeConstants; +use crate::hardfork::RskHardforkConfig; + +use super::support::ctx_at; + +/// A `receiveHeaders` call carrying `count` headers, each the ten bytes +/// rskj's test sends (`00112233445566778899`). +fn receive_headers(count: usize) -> Vec { + let header: &[u8] = &[0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99]; + let headers = vec![header; count]; + let mut data = crate::abi::selector("receiveHeaders(bytes[])").to_vec(); + data.extend(encode::args(&[Arg::BytesArray(&headers)])); + data +} + +/// What `data` costs at block `number` of `cfg`, under `config`. +fn cost(data: &[u8], config: &BridgeConstants, cfg: &RskHardforkConfig, number: u64) -> u64 { + let mut ctx = ctx_at(number); + crate::bridge::bridge_call_gas_cost(&mut ctx, data, config, cfg).expect("a cost") +} + +/// rskj `receiveHeadersGasCost_beforeDynamicCost`: before RSKIP124, +/// `receiveHeaders` costs 22,000 and two gas a byte of its data, however many +/// headers it carries. Mainnet's last block before wasabi100. +#[test] +fn before_rskip124_receive_headers_costs_22000_whatever_it_carries() { + let (config, cfg) = (BridgeConstants::mainnet(), RskHardforkConfig::mainnet()); + assert!(!cfg.has_rskip124(1_590_999)); + for count in 0..10 { + let data = receive_headers(count); + assert_eq!( + cost(&data, &config, &cfg, 1_590_999), + 22_000 + 2 * data.len() as u64, + "{count} headers" + ); + } +} + +/// rskj `receiveHeadersGasCost_afterDynamicCost`: from RSKIP124 without +/// RSKIP132, 66,000, 1,650 for each header past the first, and two gas a byte +/// of its data. Testnet has RSKIP124 from block 0 and RSKIP132 from 43,550 +/// (`testnet.conf`), so its blocks below 43,550 are the ones that charge it. +#[test] +fn from_rskip124_until_rskip132_receive_headers_costs_66000_and_1650_a_header() { + let (config, cfg) = (BridgeConstants::testnet(), RskHardforkConfig::testnet()); + assert!(cfg.has_rskip124(43_549) && !cfg.has_rskip132(43_549)); + for count in 0..10u64 { + let data = receive_headers(count as usize); + assert_eq!( + cost(&data, &config, &cfg, 43_549), + 66_000 + 2 * data.len() as u64 + 1_650 * count.saturating_sub(1), + "{count} headers" + ); + } +} + +/// rskj `receiveHeadersGasCost_afterDynamicCost_review`: from RSKIP132, +/// 25,000, 3,500 for each header past the first, and two gas a byte of its +/// data: testnet from block 43,550 and mainnet from wasabi100. +#[test] +fn from_rskip132_receive_headers_costs_25000_and_3500_a_header() { + for (config, cfg, number) in [ + ( + BridgeConstants::testnet(), + RskHardforkConfig::testnet(), + 43_550, + ), + ( + BridgeConstants::mainnet(), + RskHardforkConfig::mainnet(), + 1_591_000, + ), + ] { + assert!(cfg.has_rskip124(number) && cfg.has_rskip132(number)); + for count in 0..10u64 { + let data = receive_headers(count as usize); + assert_eq!( + cost(&data, &config, &cfg, number), + 25_000 + 2 * data.len() as u64 + 3_500 * count.saturating_sub(1), + "{count} headers at {number}" + ); + } + } +} + +/// rskj `getGasForDataInvalidFunction`: a call rskj can't parse as one of the +/// Bridge's methods (`Bridge.parseData` answers null) costs what `releaseBtc` +/// costs, 23,000, and nothing for its data: data of one to three bytes, a +/// selector no method has, arguments that don't decode, or a method that +/// isn't enabled yet. +#[test] +fn a_call_that_names_no_method_costs_23000_and_nothing_for_its_data() { + let (config, cfg) = (BridgeConstants::mainnet(), RskHardforkConfig::mainnet()); + let number = 8_804_200; + let mut unknown = vec![0xde, 0xad, 0xbe, 0xef]; + unknown.extend_from_slice(&[0x01; 64]); + let mut undecodable = crate::abi::selector("receiveHeaders(bytes[])").to_vec(); + undecodable.extend_from_slice(&[0xff; 40]); + for data in [vec![1u8, 2, 3], unknown, undecodable] { + assert_eq!(cost(&data, &config, &cfg, number), 23_000, "{data:02x?}"); + } + // getQueuedPegoutsCount comes with RSKIP271 (hop400): before it, it is + // no method rskj will parse. + let queued = crate::abi::selector("getQueuedPegoutsCount()").to_vec(); + assert!(!cfg.has_rskip271(4_598_499)); + assert_eq!(cost(&queued, &config, &cfg, 4_598_499), 23_000); + assert_ne!(cost(&queued, &config, &cfg, 4_598_500), 23_000); +} diff --git a/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_rejected_pegin_test.rs b/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_rejected_pegin_test.rs new file mode 100644 index 0000000..cfa8018 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_rejected_pegin_test.rs @@ -0,0 +1,261 @@ +//! rskj's `BridgeSupportRejectedPeginTest` +//! (`co/rsk/peg/BridgeSupportRejectedPeginTest.java`): from RSKIP379, peg-ins +//! rskj turns down and can't refund, and transactions it leaves alone. +//! +//! Each runs at arrowhead600, reed800 and vetiver900 on mainnet, with rskj's +//! test's federations: an active P2SH ERP federation of five members +//! (`fa07` to `fa11`) created at block 2, and for one test a P2SH ERP +//! federation of three (`fa04` to `fa06`) created at block 1 before it, both +//! with mainnet's emergency keys. The Bridge runs at block 3, so a newer +//! federation with an older one beside it isn't active yet. A peg-in is +//! registered at mainnet's pegout index's height, with one more confirmation +//! than it needs. Between RSKIP459 (lovell700) and RSKIP551 (vetiver900) a +//! peg-in turned down without a refund is marked processed; outside it, it +//! isn't (`BridgeSupportTestUtil.shouldMarkRejectedPeginAsProcessed`). +//! +//! Read in full: its two other tests, of a Flyover payment and of a +//! transaction that pays no federation, are ported too. + +use alloy_primitives::U256; +use bitcoin::{ScriptBuf, Transaction, Witness}; + +use crate::bridge::constants::BridgeConstants; +use crate::bridge::storage; +use crate::hardfork::{RskHardforkConfig, RskNetworkUpgrade, RSK_MAINNET_CHAIN_ID}; + +use super::support::*; + +/// The ladders rskj's `activationsProvider` gives: arrowhead600, reed800 and +/// every upgrade. +fn ladders() -> [RskHardforkConfig; 3] { + [ + RskNetworkUpgrade::Arrowhead600, + RskNetworkUpgrade::Reed800, + RskNetworkUpgrade::Vetiver900, + ] + .map(|last| up_to(last, RSK_MAINNET_CHAIN_ID)) +} + +/// rskj's `shouldMarkRejectedPeginAsProcessed`: RSKIP459 and not RSKIP551. +fn marks(cfg: &RskHardforkConfig) -> bool { + cfg.has_rskip459(3) && !cfg.has_rskip551(3) +} + +/// The members of a federation, from rskj's seeds. +fn members(seeds: &[&str]) -> Vec<[u8; 33]> { + seeds.iter().map(|seed| key_from_seed(seed)).collect() +} + +/// The active federation's members. +fn active() -> Vec<[u8; 33]> { + members(&["fa07", "fa08", "fa09", "fa10", "fa11"]) +} + +/// The older federation's members. +fn older() -> Vec<[u8; 33]> { + members(&["fa04", "fa05", "fa06"]) +} + +/// The redeem script of mainnet's P2SH ERP federation of `members`. +fn redeem(members: &[[u8; 33]]) -> Vec { + let config = BridgeConstants::mainnet(); + erp_redeem( + members, + &keys_of(config.erp_fed_pubkeys), + config.erp_fed_activation_delay, + ) +} + +/// The output a P2SH ERP federation of `members` is paid at. +fn output(members: &[[u8; 33]]) -> ScriptBuf { + p2sh_of(&redeem(members)) +} + +/// The Bitcoin height a peg-in is registered at: where mainnet starts reading +/// the pegout index. +fn index_height() -> u32 { + let config = BridgeConstants::mainnet(); + config.btc_height_when_pegout_tx_index_activates + + config.pegout_tx_index_grace_period_in_btc_blocks +} + +/// `tx` registered at RSK block 3 of `cfg` on mainnet, the active federation +/// stored and, with `with_older`, the older one too: what it answered and left +/// behind, and the Bridge after it. +fn registered(tx: &Transaction, cfg: &RskHardforkConfig, with_older: bool) -> (Registered, Ctx) { + let config = BridgeConstants::mainnet(); + let height = index_height(); + let mut ctx = ctx_at(3); + let raw = bitcoin::consensus::serialize(tx); + hold( + &mut ctx, + &raw, + height, + height + config.btc2rsk_minimum_acceptable_confirmations, + cfg, + ); + fund_bridge_but(&mut ctx, 0); + store_federation(&mut ctx, NEW_FEDERATION, &active(), 2, 3000); + if with_older { + store_federation(&mut ctx, OLD_FEDERATION, &older(), 1, 3000); + } + let registered = register(&mut ctx, tx, i64::from(height), &config, cfg); + (registered, ctx) +} + +/// No UTXO of either federation's is registered. +fn no_utxos(ctx: &mut Ctx, cfg: &RskHardforkConfig) { + assert!(storage::load_federation_utxos(ctx, cfg, 3).is_empty()); + assert!(storage::load_old_federation_utxos(ctx).is_empty()); +} + +/// A spend from an input rskj names no sender for. +fn from_no_one() -> (ScriptBuf, Witness) { + no_sender() +} + +/// rskj `registerBtcTransaction_whenBelowTheMinimum_shouldRejectPegin`: a +/// peg-in of a satoshi under the minimum is turned down as INVALID_AMOUNT (5), +/// with no refund (3), nothing credited or registered. +#[test] +fn a_peg_in_below_the_minimum_is_turned_down_without_a_refund() { + let minimum = BridgeConstants::mainnet().minimum_pegin_tx_value; + let pegin = tx(vec![from_no_one()], &[(minimum - 1, output(&active()))]); + for cfg in ladders() { + let (refused, mut ctx) = registered(&pegin, &cfg, false); + assert_eq!( + refused, + Registered { + returned: true, + processed: marks(&cfg), + logs: vec![rejected_pegin(&pegin, 5), unrefundable_pegin(&pegin, 3)], + } + ); + no_utxos(&mut ctx, &cfg); + } +} + +/// rskj `registerBtcTransaction_whenUndeterminedSender_shouldRejectPegin`: a +/// peg-in from no sender rskj can name, with no instructions, is turned down +/// as LEGACY_PEGIN_UNDETERMINED_SENDER (3) with no refund (1): no refund is +/// built, nothing credited or registered. +#[test] +fn a_peg_in_from_an_undetermined_sender_is_turned_down_without_a_refund() { + let pegin = tx(vec![from_no_one()], &[(COIN, output(&active()))]); + for cfg in ladders() { + let (refused, mut ctx) = registered(&pegin, &cfg, false); + assert_eq!( + refused, + Registered { + returned: true, + processed: marks(&cfg), + logs: vec![rejected_pegin(&pegin, 3), unrefundable_pegin(&pegin, 1)], + } + ); + no_utxos(&mut ctx, &cfg); + assert!(waiting_pegouts(&mut ctx).is_empty()); + } +} + +/// rskj `registerBtcTransaction_whenPeginV1WithInvalidPayloadAndUnderminedSender_shouldRejectPegin`: +/// instructions of version 1 with nothing after the version don't parse: from +/// no sender rskj can name the peg-in is turned down as +/// PEGIN_V1_INVALID_PAYLOAD (4) with no refund, LEGACY_PEGIN_UNDETERMINED_SENDER +/// (1) since the version read is still 0. +#[test] +fn a_peg_in_whose_instructions_do_not_parse_from_no_sender_is_not_refunded() { + let pegin = tx( + vec![from_no_one()], + &[(COIN, output(&active())), (0, op_return(b"RSKT\x01"))], + ); + for cfg in ladders() { + let (refused, mut ctx) = registered(&pegin, &cfg, false); + assert_eq!( + refused, + Registered { + returned: true, + processed: marks(&cfg), + logs: vec![rejected_pegin(&pegin, 4), unrefundable_pegin(&pegin, 1)], + } + ); + no_utxos(&mut ctx, &cfg); + assert!(waiting_pegouts(&mut ctx).is_empty()); + } +} + +/// rskj `registerBtcTransaction_whenUtxoToActiveFedBelowMinimumAndUtxoToRetiringFedAboveMinimum_shouldRejectPegin`: +/// with a newer federation stored beside an older one, and not yet old enough +/// to be active, the older one is the active one and the newer one no live +/// federation. A UTXO under the minimum to the newer one doesn't count, and +/// the minimum to the older one does: the peg-in is no invalid amount but +/// one from an undetermined sender (3, then 1). +#[test] +fn only_what_live_federations_are_paid_counts_toward_the_minimum() { + let minimum = BridgeConstants::mainnet().minimum_pegin_tx_value; + let pegin = tx( + vec![from_no_one()], + &[ + (minimum - 1, output(&active())), + (minimum, output(&older())), + ], + ); + for cfg in ladders() { + let (refused, mut ctx) = registered(&pegin, &cfg, true); + assert_eq!( + refused, + Registered { + returned: true, + processed: marks(&cfg), + logs: vec![rejected_pegin(&pegin, 3), unrefundable_pegin(&pegin, 1)], + } + ); + no_utxos(&mut ctx, &cfg); + } +} + +/// rskj `registerBtcTransaction_whenAttemptToRegisterFlyoverPegin_shouldIgnorePegin`: +/// a payment to a Flyover address of the active federation (` OP_DROP` +/// before its redeem script) pays no live federation: +/// `registerBtcTransaction` leaves it alone, logging nothing and not marking +/// it processed. +#[test] +fn a_payment_to_a_flyover_address_is_left_alone() { + let mut flyover = push(&[0x33; 32]); + flyover.push(0x75); + flyover.extend(redeem(&active())); + let payment = tx(vec![from_no_one()], &[(COIN, p2sh_of(&flyover))]); + for cfg in ladders() { + let (ignored, mut ctx) = registered(&payment, &cfg, false); + assert_eq!( + ignored, + Registered { + returned: true, + processed: false, + logs: vec![], + } + ); + no_utxos(&mut ctx, &cfg); + } +} + +/// rskj `registerBtcTransaction_whenNoUtxoToFed_shouldIgnorePegin`: a +/// transaction that pays no federation is left alone. +#[test] +fn a_transaction_that_pays_no_federation_is_left_alone() { + let minimum = BridgeConstants::mainnet().minimum_pegin_tx_value; + let user = key_from_seed("userAddress"); + let payment = tx(vec![from_no_one()], &[(minimum, p2pkh(&hash160(&user)))]); + for cfg in ladders() { + let (ignored, mut ctx) = registered(&payment, &cfg, false); + assert_eq!( + ignored, + Registered { + returned: true, + processed: false, + logs: vec![], + } + ); + no_utxos(&mut ctx, &cfg); + assert_eq!(balance(&mut ctx, rsk_address_of(&user)), U256::ZERO); + } +} diff --git a/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_test/chain_work.rs b/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_test/chain_work.rs new file mode 100644 index 0000000..6636b65 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_test/chain_work.rs @@ -0,0 +1,201 @@ +//! rskj's `BridgeSupportTest.ChainWorkTests`: Bitcoin mainnet's block +//! 849,138 is the first whose chain work bitcoinj's twelve bytes could not +//! hold, and before RSKIP434 rskj takes no mainnet header from that height on +//! (`BridgeSupport.cannotProcessNextBlock`); from RSKIP434, and on testnet and +//! regtest always, it takes them. rskj's real headers 849,134 to 849,139, and +//! the chain work rskj gives the ones it stores first; Arrowhead600 is before +//! RSKIP434 and Arrowhead631 brings it. + +use alloy_primitives::U256; +use bitcoin::block::Header; + +use crate::abi::encode::{self, Arg}; +use crate::bridge::btc_chain; +use crate::bridge::btc_store::{self, StoredBlock}; +use crate::bridge::constants::BridgeConstants; +use crate::hardfork::{ + RskHardforkConfig, RskNetworkUpgrade, RSK_MAINNET_CHAIN_ID, RSK_REGTEST_CHAIN_ID, + RSK_TESTNET_CHAIN_ID, +}; + +use super::super::support::*; + +use RskNetworkUpgrade::{Arrowhead600, Arrowhead631}; + +const H849134: &str = "0080b92c24f123130ae29e899f0cab72653722e54cdf3b30445202000000000000000000c72ead65a3b78ab637d1876c00414a77e47bcc5b52667ac1e573633563bea5a695aa7766255d031728d182a8"; +const H849135: &str = "00004020bf67910b5d3996ee594848b482ee84d0e28c97a9a2d601000000000000000000865e218552bb92df36c962f5163e84a6c2542584fb36be2fa8b2a4246c73a701f1ae7766255d031791836b22"; +const H849136: &str = "0000003a796f8b7a9d6ba6e13064e7c64e94570f877170262f1f0200000000000000000036b2ab17565a24a9be4626ca801cb31f91232034ba848295475f931a58dd5446e5b07766255d03173b01a491"; +const H849137: &str = "00e00820925b77c9ff4d0036aa29f3238cde12e9af9d55c34ed30200000000000000000032a9fa3e12ef87a2327b55db6a16a1227bb381db8b269d90aa3a6e38cf39665f91b47766255d0317c1b1575f"; +const H849138: &str = "006001207ca158816ffc9d45b9ecd6a49ffbf3038f3646cf13fc01000000000000000000e0182ce7cc10db785b5fb2fb4314053f5b12cd6116168797cb461aa339fc725078b87766255d0317ba5261e2"; +const H849139: &str = "00a0b625ffa2f7cbf95219fc74c3db38f84ae265784bc1417c71020000000000000000008e5b319a229376089f4a7b77c90ed90ac19a0532fc4c62426f5a5931ee7e3e8dd2c67766255d03171e91a015"; + +/// The chain work rskj's tests give blocks 849,134 and 849,137. +const CHAIN_WORK_849134: &str = "7ffef81fa11393037c9df17b"; +const CHAIN_WORK_849137: &str = "7fffdc6f043e4a69ea179a7a"; + +/// `receiveHeader`'s answers (`BridgeSupport.RECEIVE_HEADER_*`). +const SUCCESSFUL: i64 = 0; +const UNEXPECTED_EXCEPTION: i64 = -99; + +fn header(hex: &str) -> Header { + bitcoin::consensus::deserialize(&hex::decode(hex).expect("hex")).expect("a header") +} + +/// A Bridge at RSK block 1 whose Bitcoin store holds the block `hex` at +/// `height` with the chain work `work`, its head. +fn holding(cfg: &RskHardforkConfig, hex: &str, height: u32, work: &str) -> Ctx { + let mut ctx = ctx_at(1); + let work = U256::from_str_radix(work, 16).expect("a chain work"); + let block = StoredBlock::new(header(hex), height, work); + let v2 = cfg.has_stored_block_v2(1); + btc_store::put_stored_block(&mut ctx, &block, v2); + btc_store::store_chain_head(&mut ctx, &block, v2); + ctx +} + +/// `receiveHeader` of the block `hex`: its answer. +fn receive_header( + ctx: &mut Ctx, + hex: &str, + config: &BridgeConstants, + cfg: &RskHardforkConfig, +) -> i64 { + let raw = hex::decode(hex).expect("hex"); + let args = encode::args(&[Arg::Bytes(&raw)]); + let use_v2 = cfg.has_stored_block_v2(1); + let answer = btc_chain::receive_header(ctx, &args, 0, config, use_v2, cfg).expect("an answer"); + i64::from_be_bytes(answer.bytes[24..32].try_into().expect("eight bytes")) +} + +/// `receiveHeaders` of the blocks `hexes`. +fn receive_headers( + ctx: &mut Ctx, + hexes: &[&str], + config: &BridgeConstants, + cfg: &RskHardforkConfig, +) { + let raws: Vec> = hexes + .iter() + .map(|hex| hex::decode(hex).expect("hex")) + .collect(); + let headers: Vec<&[u8]> = raws.iter().map(Vec::as_slice).collect(); + let args = encode::args(&[Arg::BytesArray(&headers)]); + let use_v2 = cfg.has_stored_block_v2(1); + btc_chain::receive_headers(ctx, &args, 0, config, use_v2, cfg).expect("an answer"); +} + +/// Whether the Bridge's Bitcoin store holds the block `hex`. +fn stored(ctx: &mut Ctx, hex: &str) -> bool { + btc_store::get_stored_block(ctx, &header(hex).block_hash()).is_some() +} + +/// The networks other than mainnet rskj's `notMainnetAndActivationsArgs` +/// gives, each before RSKIP434 and from it. +fn off_mainnet() -> Vec<(BridgeConstants, RskHardforkConfig)> { + let mut cases = Vec::new(); + for (config, chain_id) in [ + (BridgeConstants::testnet(), RSK_TESTNET_CHAIN_ID), + (BridgeConstants::rskj_regtest(), RSK_REGTEST_CHAIN_ID), + ] { + for last in [Arrowhead600, Arrowhead631] { + cases.push((config.clone(), up_to(last, chain_id))); + } + } + cases +} + +/// `receiveHeader_beforeRSKIP434_returnsUnexpectedExceptionResponseCodeAndDoesNotSaveTheBlock`. +#[test] +fn before_rskip434_receive_header_does_not_take_mainnets_block_849138() { + let (config, cfg) = ( + BridgeConstants::mainnet(), + up_to(Arrowhead600, RSK_MAINNET_CHAIN_ID), + ); + let mut ctx = holding(&cfg, H849137, 849_137, CHAIN_WORK_849137); + assert_eq!( + receive_header(&mut ctx, H849138, &config, &cfg), + UNEXPECTED_EXCEPTION + ); + assert!(!stored(&mut ctx, H849138)); +} + +/// `receiveHeaders_beforeRSKIP434_savesBlocksUntilReachesBlockWithTooMuchChainWork`. +#[test] +fn before_rskip434_receive_headers_stops_at_mainnets_block_849138() { + let (config, cfg) = ( + BridgeConstants::mainnet(), + up_to(Arrowhead600, RSK_MAINNET_CHAIN_ID), + ); + let mut ctx = holding(&cfg, H849134, 849_134, CHAIN_WORK_849134); + receive_headers( + &mut ctx, + &[H849135, H849136, H849137, H849138, H849139], + &config, + &cfg, + ); + for (hex, taken) in [ + (H849135, true), + (H849136, true), + (H849137, true), + (H849138, false), + (H849139, false), + ] { + assert_eq!(stored(&mut ctx, hex), taken, "{hex}"); + } +} + +/// `receiveHeader_afterRSKIP434_returnsSuccessfulAndSavesTheBlock`. +#[test] +fn from_rskip434_receive_header_takes_mainnets_block_849138() { + let (config, cfg) = ( + BridgeConstants::mainnet(), + up_to(Arrowhead631, RSK_MAINNET_CHAIN_ID), + ); + let mut ctx = holding(&cfg, H849137, 849_137, CHAIN_WORK_849137); + assert_eq!(receive_header(&mut ctx, H849138, &config, &cfg), SUCCESSFUL); + assert!(stored(&mut ctx, H849138)); +} + +/// `receiveHeaders_afterRSKIP434_savesAllBlocks`. +#[test] +fn from_rskip434_receive_headers_takes_mainnets_blocks_from_849138() { + let (config, cfg) = ( + BridgeConstants::mainnet(), + up_to(Arrowhead631, RSK_MAINNET_CHAIN_ID), + ); + let mut ctx = holding(&cfg, H849134, 849_134, CHAIN_WORK_849134); + let hexes = [H849135, H849136, H849137, H849138, H849139]; + receive_headers(&mut ctx, &hexes, &config, &cfg); + for hex in hexes { + assert!(stored(&mut ctx, hex), "{hex}"); + } +} + +/// `receiveHeader_networkNotMainnet_returnsSuccessfulAndSavesTheBlock`. +#[test] +fn off_mainnet_receive_header_takes_block_849138() { + for (config, cfg) in off_mainnet() { + let mut ctx = holding(&cfg, H849137, 849_137, CHAIN_WORK_849137); + let case = format!("{:?} {}", config.btc_network, cfg.has_rskip434(1)); + assert_eq!( + receive_header(&mut ctx, H849138, &config, &cfg), + SUCCESSFUL, + "{case}" + ); + assert!(stored(&mut ctx, H849138), "{case}"); + } +} + +/// `receiveHeaders_networkNotMainnet_savesAllBlocks`. +#[test] +fn off_mainnet_receive_headers_takes_the_blocks_from_849138() { + for (config, cfg) in off_mainnet() { + let mut ctx = holding(&cfg, H849134, 849_134, CHAIN_WORK_849134); + let hexes = [H849135, H849136, H849137, H849138, H849139]; + receive_headers(&mut ctx, &hexes, &config, &cfg); + let case = format!("{:?} {}", config.btc_network, cfg.has_rskip434(1)); + for hex in hexes { + assert!(stored(&mut ctx, hex), "{case} {hex}"); + } + } +} diff --git a/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_test/coinbase.rs b/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_test/coinbase.rs new file mode 100644 index 0000000..13d2574 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_test/coinbase.rs @@ -0,0 +1,242 @@ +//! rskj's `BridgeSupportTest` tests of `registerBtcCoinbaseTransaction` +//! (`when_RegisterBtcCoinbaseTransaction_*`) and of +//! `hasBtcBlockCoinbaseTransactionInformation` (`hasBtcCoinbaseTransaction_*`). +//! +//! Each registration turned down is set beside the one that is kept: the +//! same coinbase, tree and block, as they should be, is kept, so that what is +//! turned down is turned down for what was changed. They run on mainnet at +//! Papyrus200, which brings RSKIP143, and the method with it. + +use sha2::Digest as _; + +use crate::abi::encode::{self, Arg}; +use crate::bridge::constants::BridgeConstants; +use crate::bridge::{btc_chain, tx as bridge_tx}; +use crate::hardfork::{RskHardforkConfig, RskNetworkUpgrade, RSK_MAINNET_CHAIN_ID}; + +use super::super::support::*; + +/// The witness merkle root the coinbase commits to, and its reserved value. +const WITNESS_ROOT: [u8; 32] = [0x3c; 32]; +const RESERVED: [u8; 32] = [0; 32]; + +/// The Bitcoin height the coinbase's block is held at. +const HEIGHT: u32 = 850_000; + +/// A coinbase whose scriptSig is `script_sig`, committing to [`WITNESS_ROOT`] +/// with [`RESERVED`]: BIP-141's `OP_RETURN aa21a9ed `, the root in the order rskj reverses it to +/// (`witnessMerkleRoot.getReversedBytes()`). +fn coinbase(script_sig: &[u8]) -> Vec { + let mut root = WITNESS_ROOT; + root.reverse(); + let commitment = sha2::Sha256::digest(sha2::Sha256::digest([root, RESERVED].concat())); + 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::ONE, + lock_time: bitcoin::absolute::LockTime::ZERO, + input: vec![bitcoin::TxIn { + previous_output: bitcoin::OutPoint::null(), + script_sig: bitcoin::ScriptBuf::from_bytes(script_sig.to_vec()), + sequence: bitcoin::Sequence::MAX, + witness: bitcoin::Witness::new(), + }], + output: vec![ + bitcoin::TxOut { + value: bitcoin::Amount::from_sat(50 * COIN), + script_pubkey: p2pkh(&[0x77; 20]), + }, + bitcoin::TxOut { + value: bitcoin::Amount::ZERO, + script_pubkey: bitcoin::ScriptBuf::from_bytes(commitment_script), + }, + ], + }; + bitcoin::consensus::serialize(&coinbase) +} + +/// The coinbase the registrations here are of. +fn the_coinbase() -> Vec { + coinbase(&[0x03, 0x50, 0xf8, 0x0c]) +} + +/// A Bridge at RSK block 10 of mainnet's ladder up to Papyrus200, whose +/// Bitcoin store holds a block for each of `roots`, from [`HEIGHT`] up: the +/// Bridge and the blocks' hashes. +fn holding(roots: &[[u8; 32]]) -> (Ctx, BridgeConstants, RskHardforkConfig, Vec<[u8; 32]>) { + let config = BridgeConstants::mainnet(); + let cfg = up_to(RskNetworkUpgrade::Papyrus200, RSK_MAINNET_CHAIN_ID); + let mut ctx = ctx_at(10); + let head = HEIGHT + roots.len() as u32 - 1; + let hashes = hold_roots(&mut ctx, roots, HEIGHT, head, &cfg) + .iter() + .map(|header| btc_chain::bitcoin_hash_to_b256(&header.block_hash()).0) + .collect(); + (ctx, config, cfg, hashes) +} + +/// `registerBtcCoinbaseTransaction` of `raw`, in the block `block_hash`, by +/// the tree `tree`: whether it returned. +fn registers( + ctx: &mut Ctx, + config: &BridgeConstants, + cfg: &RskHardforkConfig, + raw: &[u8], + block_hash: [u8; 32], + tree: &[u8], +) -> bool { + let args = encode::args(&[ + Arg::Bytes(raw), + Arg::Word(block_hash), + Arg::Bytes(tree), + Arg::Word(WITNESS_ROOT), + Arg::Word(RESERVED), + ]); + in_a_call(ctx, |ctx| { + bridge_tx::register_btc_coinbase_transaction(ctx, &args, 0, config, cfg) + }) + .is_ok() +} + +/// `when_RegisterBtcCoinbaseTransaction_MerkleTreeWrongFormat_noSent`: a tree +/// rskj cannot read fails the call, and nothing is kept. +#[test] +fn a_coinbase_by_a_tree_rskj_cannot_read_fails_its_registration() { + let raw = the_coinbase(); + let (mut ctx, config, cfg, blocks) = holding(&[bridge_tx::calculate_btc_tx_hash(&raw)]); + assert!(!registers( + &mut ctx, + &config, + &cfg, + &raw, + blocks[0], + &[0x01, 0x02, 0x03] + )); + assert_eq!( + bridge_tx::get_coinbase_information(&mut ctx, &blocks[0]), + None + ); + assert!(registers( + &mut ctx, + &config, + &cfg, + &raw, + blocks[0], + &one_leaf_tree(&raw) + )); + assert_eq!( + bridge_tx::get_coinbase_information(&mut ctx, &blocks[0]), + Some(WITNESS_ROOT) + ); +} + +/// `when_RegisterBtcCoinbaseTransaction_HashNotInPmt_noSent`: a tree of +/// another transaction keeps nothing, and the call returns. +#[test] +fn a_coinbase_by_a_tree_of_another_transaction_is_not_kept() { + let raw = the_coinbase(); + let (mut ctx, config, cfg, blocks) = holding(&[bridge_tx::calculate_btc_tx_hash(&raw)]); + let another = one_leaf_tree(&coinbase(&[0x03, 0x51, 0xf8, 0x0c])); + assert!(registers( + &mut ctx, &config, &cfg, &raw, blocks[0], &another + )); + assert_eq!( + bridge_tx::get_coinbase_information(&mut ctx, &blocks[0]), + None + ); + assert!(registers( + &mut ctx, + &config, + &cfg, + &raw, + blocks[0], + &one_leaf_tree(&raw) + )); + assert_eq!( + bridge_tx::get_coinbase_information(&mut ctx, &blocks[0]), + Some(WITNESS_ROOT) + ); +} + +/// `when_RegisterBtcCoinbaseTransaction_not_equal_merkle_root_noSent`: in a +/// block whose merkle root is not the tree's, nothing is kept, and the call +/// returns. +#[test] +fn a_coinbase_in_a_block_of_another_merkle_root_is_not_kept() { + let raw = the_coinbase(); + let (mut ctx, config, cfg, blocks) = + holding(&[[0x11; 32], bridge_tx::calculate_btc_tx_hash(&raw)]); + let (other, its_block) = (blocks[0], blocks[1]); + assert!(registers( + &mut ctx, + &config, + &cfg, + &raw, + other, + &one_leaf_tree(&raw) + )); + assert_eq!(bridge_tx::get_coinbase_information(&mut ctx, &other), None); + assert!(registers( + &mut ctx, + &config, + &cfg, + &raw, + its_block, + &one_leaf_tree(&raw) + )); + assert_eq!( + bridge_tx::get_coinbase_information(&mut ctx, &its_block), + Some(WITNESS_ROOT) + ); +} + +/// `when_RegisterBtcCoinbaseTransaction_null_stored_block_noSent`: a block +/// the store does not hold fails the call, and nothing is kept. +#[test] +fn a_coinbase_in_a_block_not_held_fails_its_registration() { + let raw = the_coinbase(); + let (mut ctx, config, cfg, blocks) = holding(&[bridge_tx::calculate_btc_tx_hash(&raw)]); + assert!(!registers( + &mut ctx, + &config, + &cfg, + &raw, + [0x99; 32], + &one_leaf_tree(&raw) + )); + assert_eq!( + bridge_tx::get_coinbase_information(&mut ctx, &[0x99; 32]), + None + ); + assert!(registers( + &mut ctx, + &config, + &cfg, + &raw, + blocks[0], + &one_leaf_tree(&raw) + )); + assert_eq!( + bridge_tx::get_coinbase_information(&mut ctx, &blocks[0]), + Some(WITNESS_ROOT) + ); +} + +/// `hasBtcCoinbaseTransaction_after_rskip_143_activation` and +/// `hasBtcCoinbaseTransaction_fails_with_null_coinbase_information_after_rskip_143_activation`: +/// a block has coinbase information once its coinbase's is kept, and not +/// otherwise. +#[test] +fn a_block_has_coinbase_information_once_it_is_kept() { + let mut ctx = ctx_at(10); + let (kept, other) = ([0x5b; 32], [0x5c; 32]); + bridge_tx::set_coinbase_information(&mut ctx, &kept, &WITNESS_ROOT); + let has = |ctx: &mut Ctx, hash: [u8; 32]| { + let args = encode::args(&[Arg::Word(hash)]); + let answer = bridge_tx::has_btc_block_coinbase_info(ctx, &args, 0).expect("an answer"); + answer.bytes[..] == encode::int(1)[..] + }; + assert!(has(&mut ctx, kept)); + assert!(!has(&mut ctx, other)); +} diff --git a/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_test/collections.rs b/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_test/collections.rs new file mode 100644 index 0000000..542f534 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_test/collections.rs @@ -0,0 +1,424 @@ +//! rskj's `BridgeSupportTest` tests of what `updateCollections` moves: a +//! retiring federation's funds (`callProcessFundsMigration_*`), the +//! federations' creation heights (`updateFederationCreationBlockHeights_*`), +//! and a confirmed peg-out into the set waiting for signatures +//! (`rskTxWaitingForSignature_*`); and of the RSKIP271 getter of the next +//! peg-out's block (`getNextPegoutCreationBlockNumber_after_RSKIP271_activation`). +//! +//! rskj's tests mock the rules they turn on, alone; here each runs on the +//! ladder up to the upgrade that brings it: RSKIP146 Papyrus200, RSKIP176 +//! and RSKIP186 Iris300, RSKIP326 and RSKIP375 Fingerroot500. rskj gives a +//! peg-out the creation hash `Keccak256.ZERO_HASH`; here it is another, as no +//! transaction hashes to zero. + +use std::collections::BTreeMap; + +use alloy_primitives::U256; +use bitcoin::Transaction; + +use crate::bridge::constants::BridgeConstants; +use crate::bridge::peg::{self, PegoutWaitingForConfirmations}; +use crate::bridge::serialization::rlp_encode_u64; +use crate::bridge::storage::{self, BridgeUtxo}; +use crate::hardfork::{ + RskHardforkConfig, RskNetworkUpgrade, RSK_MAINNET_CHAIN_ID, RSK_REGTEST_CHAIN_ID, + RSK_TESTNET_CHAIN_ID, +}; + +use super::super::support::*; +use super::waiting_in; + +use RskNetworkUpgrade::{Fingerroot500, Hop400, Iris300, Papyrus200, Wasabi100}; + +const RELEASE_REQUESTED: &str = "release_requested(bytes32,bytes32,uint256)"; +const PEGOUT_CONFIRMED: &str = "pegout_confirmed(bytes32,uint256)"; + +/// The RSK transaction a peg-out here was created in. +const CREATION: [u8; 32] = [0x33; 32]; + +/// `updateCollections` in the transaction [`RSK_TX_HASH`]: whether it +/// returned, and the events it logged. +fn update(ctx: &mut Ctx, config: &BridgeConstants, cfg: &RskHardforkConfig) -> (bool, Vec) { + let returned = in_a_call(ctx, |ctx| { + peg::update_collections(ctx, 0, config, cfg, &tx_context()) + }) + .is_ok(); + (returned, take_logs(ctx)) +} + +/// rskj's funds-migration tests: mainnet at RSK block 18,510, the genesis +/// federation retiring with a UTXO of one bitcoin, and a one-member +/// federation created at block 5, so active from block 18,505 and inside its +/// migration window. +fn migrating(last: RskNetworkUpgrade) -> (Ctx, BridgeConstants, RskHardforkConfig) { + let config = BridgeConstants::mainnet(); + let cfg = up_to(last, RSK_MAINNET_CHAIN_ID); + let mut ctx = ctx_at(18_510); + fund_bridge_but(&mut ctx, 0); + set_fee_per_kb(&mut ctx, MILLICOIN); + let genesis = keys_of(config.genesis_federation_public_keys); + store_federation(&mut ctx, OLD_FEDERATION, &genesis, 1, 1000); + // `FederationTestUtils.getFederationMembers(1)`: the key of private key 100. + store_federation(&mut ctx, NEW_FEDERATION, &[public_key(&[100])], 5, 1000); + storage::store_old_federation_utxos( + &mut ctx, + &[BridgeUtxo { + tx_hash: [0x01; 32], + vout: 0, + value_satoshis: COIN, + height: 10, + script: p2sh_of(&standard_redeem(&genesis)).to_bytes(), + coinbase: false, + }], + ); + (ctx, config, cfg) +} + +/// The migration `updateCollections` makes: waiting without the hash of the +/// transaction that made it before RSKIP146, and with it, logged as +/// requested, from RSKIP146. +fn migrated(last: RskNetworkUpgrade) { + let (mut ctx, config, cfg) = migrating(last); + let (returned, logs) = update(&mut ctx, &config, &cfg); + assert!(returned); + let without_hash = waiting_in( + &mut ctx, + storage::PEGOUTS_WAITING_FOR_CONFIRMATIONS_KEY, + false, + ); + let with_hash = waiting_in( + &mut ctx, + storage::PEGOUTS_WAITING_FOR_CONFIRMATIONS_WITH_TXHASH_KEY, + true, + ); + if cfg.has_rskip146(18_510) { + assert_eq!((without_hash.len(), with_hash.len()), (0, 1)); + assert!( + logs.contains(&release_requested(&with_hash[0], COIN)), + "{logs:?}" + ); + } else { + assert_eq!((without_hash.len(), with_hash.len()), (1, 0)); + assert_eq!(count_of(&logs, RELEASE_REQUESTED), 0); + } +} + +/// `callProcessFundsMigration_is_migrating_before_rskip_146_activation` and +/// `callProcessFundsMigration_is_migrated_before_rskip_146_activation`, which +/// rskj sets up alike. +#[test] +fn before_rskip146_a_migration_waits_without_its_transactions_hash() { + migrated(Wasabi100); +} + +/// `callProcessFundsMigration_is_migrating_after_rskip_146_activation` and +/// `callProcessFundsMigration_is_migrated_after_rskip_146_activation`. +#[test] +fn from_rskip146_a_migration_waits_with_its_transactions_hash_and_is_logged() { + migrated(Papyrus200); +} + +/// The active and the next federation's creation heights, as kept. +fn creation_heights(ctx: &mut Ctx) -> (Vec, Vec) { + ( + storage::bridge_load_bytes_named(ctx, storage::ACTIVE_FEDERATION_CREATION_BLOCK_HEIGHT_KEY), + storage::bridge_load_bytes_named(ctx, storage::NEXT_FEDERATION_CREATION_BLOCK_HEIGHT_KEY), + ) +} + +/// Keeps 1 as the next federation's creation height. +fn keep_next_creation_height(ctx: &mut Ctx) { + storage::bridge_store_bytes_named( + ctx, + storage::NEXT_FEDERATION_CREATION_BLOCK_HEIGHT_KEY, + &rlp_encode_u64(1), + ); +} + +/// `updateFederationCreationBlockHeights_before_rskip_186_activation`, at +/// Papyrus200: `updateCollections` leaves the heights as they are. +#[test] +fn before_rskip186_update_collections_leaves_the_creation_heights_alone() { + let (mut ctx, config, cfg) = migrating(Papyrus200); + keep_next_creation_height(&mut ctx); + assert!(update(&mut ctx, &config, &cfg).0); + assert_eq!(creation_heights(&mut ctx), (vec![], rlp_encode_u64(1))); +} + +/// `updateFederationCreationBlockHeights_after_rskip_186_activation`: with no +/// next height nothing changes, and a next height whose federation is active +/// becomes the active one, and is cleared. +#[test] +fn from_rskip186_update_collections_makes_the_next_creation_height_the_active_one() { + let (mut ctx, config, cfg) = migrating(Iris300); + assert!(update(&mut ctx, &config, &cfg).0); + assert_eq!(creation_heights(&mut ctx), (vec![], vec![])); + keep_next_creation_height(&mut ctx); + assert!(update(&mut ctx, &config, &cfg).0); + assert_eq!(creation_heights(&mut ctx), (rlp_encode_u64(1), vec![])); +} + +/// A peg-out, told apart by `seed`. +fn pegout(seed: u8) -> Transaction { + tx(vec![no_sender()], &[(COIN, p2pkh(&[seed; 20]))]) +} + +/// The set waiting for signatures: each peg-out by the RSK transaction hash +/// it is kept under. +fn waiting_for_signatures(ctx: &mut Ctx) -> BTreeMap<[u8; 32], Vec> { + peg::deserialize_rsk_txs_waiting_for_signatures(&storage::bridge_load_bytes_named( + ctx, + storage::PEGOUTS_WAITING_FOR_SIGNATURES_KEY, + )) +} + +/// A Bridge at RSK block `at` with `pegout`, created at block `created` in +/// the transaction `creation`, if known, waiting for confirmations, and +/// `signing` waiting for signatures. +fn confirming( + cfg: &RskHardforkConfig, + at: u64, + (pegout, created, creation): (&Transaction, u64, Option<[u8; 32]>), + signing: &BTreeMap<[u8; 32], Vec>, +) -> Ctx { + let mut ctx = ctx_at(at); + fund_bridge_but(&mut ctx, 0); + let entry = PegoutWaitingForConfirmations { + btc_tx_raw: bitcoin::consensus::serialize(pegout), + rsk_block_height: created, + rsk_tx_hash: creation, + }; + peg::store_pegout_confirmation_set(&mut ctx, &[entry], cfg.has_rskip146(at)); + storage::bridge_store_bytes_named( + &mut ctx, + storage::PEGOUTS_WAITING_FOR_SIGNATURES_KEY, + &peg::serialize_rsk_txs_waiting_for_signatures(signing), + ); + ctx +} + +/// rskj's regtest constants, a peg-out confirmed after one block, as rskj's +/// tests have it. +fn regtest_one_confirmation() -> BridgeConstants { + BridgeConstants { + rsk2btc_minimum_acceptable_confirmations: 1, + ..BridgeConstants::rskj_regtest() + } +} + +/// The networks rskj's parameterized tests run on, with their own +/// confirmations. +fn networks() -> [(BridgeConstants, u64); 3] { + [ + (BridgeConstants::rskj_regtest(), RSK_REGTEST_CHAIN_ID), + (BridgeConstants::testnet(), RSK_TESTNET_CHAIN_ID), + (BridgeConstants::mainnet(), RSK_MAINNET_CHAIN_ID), + ] +} + +/// The key a peg-out created at block 1 in `creation`, if known, confirmed by +/// `updateCollections` at block 2 of rskj regtest's ladder up to `last`, is +/// kept under, waiting for signatures. +fn kept_under(last: RskNetworkUpgrade, creation: Option<[u8; 32]>) -> [u8; 32] { + let config = regtest_one_confirmation(); + let cfg = up_to(last, RSK_REGTEST_CHAIN_ID); + let pegout = pegout(0x91); + let mut ctx = confirming(&cfg, 2, (&pegout, 1, creation), &BTreeMap::new()); + assert!(update(&mut ctx, &config, &cfg).0); + assert!( + waiting_pegouts(&mut ctx).is_empty(), + "no longer waiting for confirmations" + ); + let signing = waiting_for_signatures(&mut ctx); + assert_eq!(signing.len(), 1); + let (key, raw) = signing.into_iter().next().expect("one"); + assert_eq!(raw, bitcoin::consensus::serialize(&pegout)); + key +} + +/// `rskTxWaitingForSignature_uses_updateCollection_rskTxHash_before_rskip_146_activation`. +#[test] +fn before_rskip146_a_confirmed_pegout_is_kept_under_the_update_transaction() { + assert_eq!(kept_under(Wasabi100, None), RSK_TX_HASH); +} + +/// `rskTxWaitingForSignature_uses_updateCollection_rskTxHash_after_rskip_146_activation_if_release_transaction_doesnt_have_rstTxHash`. +#[test] +fn from_rskip146_a_confirmed_pegout_of_no_creation_hash_is_kept_under_the_update_transaction() { + assert_eq!(kept_under(Papyrus200, None), RSK_TX_HASH); +} + +/// `rskTxWaitingForSignature_uses_release_transaction_rstTxHash_after_rskip_146_activation`. +#[test] +fn from_rskip146_a_confirmed_pegout_is_kept_under_its_creation_transaction() { + assert_eq!(kept_under(Papyrus200, Some(CREATION)), CREATION); +} + +/// `rskTxWaitingForSignature_uses_updateCollection_rskTxHash_after_rskip_176_activation`. +#[test] +fn from_rskip176_a_confirmed_pegout_is_kept_under_the_update_transaction_again() { + assert_eq!(kept_under(Iris300, Some(CREATION)), RSK_TX_HASH); +} + +/// The events `updateCollections` at block `at` of rskj regtest's ladder up +/// to `last` logs, with a peg-out created at block `created` waiting for +/// confirmations, and whether the peg-out moved to the set waiting for +/// signatures. +fn confirmation_logged(last: RskNetworkUpgrade, created: u64, at: u64) -> (Vec, bool) { + let config = regtest_one_confirmation(); + let cfg = up_to(last, RSK_REGTEST_CHAIN_ID); + let pegout = pegout(0x91); + let mut ctx = confirming( + &cfg, + at, + (&pegout, created, Some(CREATION)), + &BTreeMap::new(), + ); + let (returned, logs) = update(&mut ctx, &config, &cfg); + assert!(returned); + (logs, !waiting_for_signatures(&mut ctx).is_empty()) +} + +/// `rskTxWaitingForSignature_postRSKIP326_emitNewPegoutConfirmedEvent`. +#[test] +fn from_rskip326_a_confirmed_pegout_is_logged() { + let (logs, moved) = confirmation_logged(Fingerroot500, 1, 2); + assert!(moved); + assert!( + logs.contains(&pegout_confirmed(&pegout(0x91), 1)), + "{logs:?}" + ); +} + +/// `rskTxWaitingForSignature_preRSKIP326_noPegoutConfirmedEventEmitted`. +#[test] +fn before_rskip326_a_confirmed_pegout_is_not_logged() { + let (logs, moved) = confirmation_logged(Hop400, 1, 2); + assert!(moved); + assert_eq!(count_of(&logs, PEGOUT_CONFIRMED), 0); +} + +/// `rskTxWaitingForSignature_postRSKIP326NoTxWithEnoughConfirmation_pegoutConfirmedEventNotEmitted`. +#[test] +fn from_rskip326_a_pegout_not_yet_confirmed_is_not_logged() { + let (logs, moved) = confirmation_logged(Fingerroot500, 2, 2); + assert!(!moved); + assert_eq!(count_of(&logs, PEGOUT_CONFIRMED), 0); +} + +/// `rskTxWaitingForSignature_uses_pegoutCreation_rskTxHash_after_rskip_375_activation`, +/// on each network. +#[test] +fn from_rskip375_a_confirmed_pegout_is_kept_under_its_creation_transaction() { + for (config, chain_id) in networks() { + let cfg = up_to(Fingerroot500, chain_id); + let at = 2 + u64::from(config.rsk2btc_minimum_acceptable_confirmations); + let pegout = pegout(0x91); + let mut ctx = confirming(&cfg, at, (&pegout, 1, Some(CREATION)), &BTreeMap::new()); + assert!(update(&mut ctx, &config, &cfg).0); + let kept = BTreeMap::from([(CREATION, bitcoin::consensus::serialize(&pegout))]); + assert_eq!(waiting_for_signatures(&mut ctx), kept, "{chain_id}"); + assert!(waiting_pegouts(&mut ctx).is_empty()); + } +} + +/// `rskTxWaitingForSignature_override_entry_is_allowed_before_rskip_375_activation`, +/// on each network, at Hop400: RSKIP146 and RSKIP176 on, RSKIP375 not yet. +/// The confirmed peg-out takes the key of the update transaction, which a +/// peg-out already had. +#[test] +fn before_rskip375_a_confirmed_pegout_takes_the_place_of_one_under_its_key() { + for (config, chain_id) in networks() { + let cfg = up_to(Hop400, chain_id); + let at = 2 + u64::from(config.rsk2btc_minimum_acceptable_confirmations); + let (confirmed, existing) = (pegout(0x91), pegout(0x92)); + let signing = BTreeMap::from([(RSK_TX_HASH, bitcoin::consensus::serialize(&existing))]); + let mut ctx = confirming(&cfg, at, (&confirmed, 1, Some(CREATION)), &signing); + assert!(update(&mut ctx, &config, &cfg).0); + let kept = BTreeMap::from([(RSK_TX_HASH, bitcoin::consensus::serialize(&confirmed))]); + assert_eq!(waiting_for_signatures(&mut ctx), kept, "{chain_id}"); + assert!(waiting_pegouts(&mut ctx).is_empty()); + } +} + +/// `rskTxWaitingForSignature_override_entry_attempt_after_rskip_375_activation`: +/// from RSKIP375 rskj refuses to keep a confirmed peg-out under a key the +/// set waiting for signatures already has +/// (`BridgeSupport.checkIfEntryExistsInPegoutsWaitingForSignatures`), and +/// `updateCollections` throws: both sets stay as they were. +#[test] +fn from_rskip375_a_confirmed_pegout_under_a_key_already_kept_fails_the_update() { + let config = regtest_one_confirmation(); + let cfg = up_to(Fingerroot500, RSK_REGTEST_CHAIN_ID); + let pegout = pegout(0x91); + let signing = BTreeMap::from([(CREATION, bitcoin::consensus::serialize(&pegout))]); + let mut ctx = confirming(&cfg, 2, (&pegout, 1, Some(CREATION)), &signing); + assert!( + !update(&mut ctx, &config, &cfg).0, + "updateCollections throws" + ); + assert_eq!(waiting_for_signatures(&mut ctx), signing); + assert_eq!(waiting_pegouts(&mut ctx), [pegout]); +} + +/// `rskTxWaitingForSignature_fail_adding_an_already_existing_key_after_rskip_375`: +/// two peg-outs made in one transaction, so of one creation hash, the first +/// waiting for signatures under it; the second, confirmed, fails +/// `updateCollections` and both sets stay as they were, until the first, +/// signed, leaves the set, when the second takes its key. +#[test] +fn from_rskip375_a_second_pegout_of_one_creation_waits_until_the_first_is_signed() { + let config = regtest_one_confirmation(); + let cfg = up_to(Fingerroot500, RSK_REGTEST_CHAIN_ID); + let (first, second) = (pegout(0x92), pegout(0x91)); + let signing = BTreeMap::from([(CREATION, bitcoin::consensus::serialize(&first))]); + let mut ctx = confirming(&cfg, 2, (&second, 1, Some(CREATION)), &signing); + assert!( + !update(&mut ctx, &config, &cfg).0, + "updateCollections throws" + ); + assert_eq!(waiting_for_signatures(&mut ctx), signing); + assert_eq!(waiting_pegouts(&mut ctx), std::slice::from_ref(&second)); + // The first is signed, and leaves the set. + storage::bridge_store_bytes_named( + &mut ctx, + storage::PEGOUTS_WAITING_FOR_SIGNATURES_KEY, + &peg::serialize_rsk_txs_waiting_for_signatures(&BTreeMap::new()), + ); + assert!(update(&mut ctx, &config, &cfg).0); + let kept = BTreeMap::from([(CREATION, bitcoin::consensus::serialize(&second))]); + assert_eq!(waiting_for_signatures(&mut ctx), kept); + assert!(waiting_pegouts(&mut ctx).is_empty()); +} + +/// From RSKIP375 rskj keys a confirmed peg-out by its creation hash, and a +/// peg-out of none, made before RSKIP146, is looked up under null in its +/// `TreeMap`, which throws: `updateCollections` fails, and both sets stay as +/// they were. Before RSKIP375 such a peg-out is kept under the update +/// transaction. No network reaches it: a peg-out waits for its confirmations +/// some thousands of blocks, and RSKIP146 came long before RSKIP375. +#[test] +fn from_rskip375_a_confirmed_pegout_of_no_creation_hash_fails_the_update() { + assert_eq!(kept_under(Hop400, None), RSK_TX_HASH); + + let config = regtest_one_confirmation(); + let cfg = up_to(Fingerroot500, RSK_REGTEST_CHAIN_ID); + let pegout = pegout(0x91); + let mut ctx = confirming(&cfg, 2, (&pegout, 1, None), &BTreeMap::new()); + assert!( + !update(&mut ctx, &config, &cfg).0, + "updateCollections throws" + ); + assert!(waiting_for_signatures(&mut ctx).is_empty()); + assert_eq!(waiting_pegouts(&mut ctx), [pegout]); +} + +/// `getNextPegoutCreationBlockNumber_after_RSKIP271_activation`: the height +/// kept is the one answered. +#[test] +fn the_next_pegout_creation_block_answered_is_the_one_kept() { + let mut ctx = ctx_at(10); + storage::bridge_store_u256(&mut ctx, storage::NEXT_PEGOUT_HEIGHT_KEY, U256::from(10u64)); + let answer = peg::get_next_pegout_creation_block_number(&mut ctx, 0).expect("an answer"); + assert_eq!(U256::from_be_slice(&answer.bytes), U256::from(10u64)); +} diff --git a/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_test/events.rs b/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_test/events.rs new file mode 100644 index 0000000..ceedfa0 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_test/events.rs @@ -0,0 +1,269 @@ +//! rskj's `BridgeSupportTest` tests of the event a credited legacy peg-in +//! logs (`eventLoggerLogLockBtc_*`, `eventLoggerLogPeginBtc_*`), of peg-ins +//! from a sender the whitelist does not name +//! (`registerBtcTransactionLockTxNotWhitelisted_*`), and of a segwit peg-in +//! registered twice (`registerBtcTransaction_sending_segwit_tx_twice_locks_just_once`). + +use alloy_primitives::U256; +use bitcoin::{OutPoint, Transaction, Witness}; + +use crate::bridge::constants::BridgeConstants; +use crate::bridge::{btc_chain, peg, storage, tx as bridge_tx}; +use crate::hardfork::{RskNetworkUpgrade, RSK_REGTEST_CHAIN_ID}; +use crate::precompiles::BRIDGE_ADDR; + +use super::super::support::*; +use super::waiting_in; + +use RskNetworkUpgrade::{Iris300, Papyrus200, Wasabi100}; + +/// The Bitcoin height a peg-in is registered at. +const HEIGHT: u32 = 100; + +const LOCK_BTC: &str = "lock_btc(address,bytes32,string,int256)"; +const PEGIN_BTC: &str = "pegin_btc(address,bytes32,int256,int256)"; +const RELEASE_REQUESTED: &str = "release_requested(bytes32,bytes32,uint256)"; + +/// rskj's regtest constants, with a locking cap no peg-in here reaches. +fn regtest() -> BridgeConstants { + BridgeConstants { + initial_locking_cap: 1_000 * COIN, + ..BridgeConstants::rskj_regtest() + } +} + +/// A bitcoin from the P2PKH key `sender`, whitelisted for it, to rskj +/// regtest's federation, registered at RSK block 10 of the regtest ladder up +/// to `last`: the peg-in, what its registration answered, and the Bridge. +fn whitelisted_pegin(sender: &[u8; 33], last: RskNetworkUpgrade) -> (Transaction, Registered, Ctx) { + let config = regtest(); + let cfg = up_to(last, RSK_REGTEST_CHAIN_ID); + let pegin = tx( + vec![p2pkh_spend(sender)], + &[(COIN, genesis_federation_output(&config))], + ); + let mut ctx = bridge_holding(&pegin, HEIGHT, 10, &config, &cfg); + whitelist_once(&mut ctx, hash160(sender), COIN); + let registered = register(&mut ctx, &pegin, i64::from(HEIGHT), &config, &cfg); + (pegin, registered, ctx) +} + +/// `eventLoggerLogLockBtc_before_rskip_146_activation`: before RSKIP146 a +/// credited peg-in logs no `lock_btc`. +#[test] +fn before_rskip146_a_credited_peg_in_logs_no_lock_btc() { + let sender = key_from_seed("seed"); + let (_, registered, mut ctx) = whitelisted_pegin(&sender, Wasabi100); + assert!(registered.processed); + assert_eq!(balance(&mut ctx, rsk_address_of(&sender)), wei(COIN)); + assert_eq!(count_of(®istered.logs, LOCK_BTC), 0); +} + +/// `eventLoggerLogLockBtc_after_rskip_146_activation` and +/// `eventLoggerLogPeginBtc_before_rskip_170_activation`: from RSKIP146, and +/// before RSKIP170, a credited peg-in logs `lock_btc` and not `pegin_btc`. +#[test] +fn from_rskip146_to_rskip170_a_credited_peg_in_logs_lock_btc() { + let sender = key_from_seed("seed"); + let (pegin, registered, _) = whitelisted_pegin(&sender, Papyrus200); + let sender_address = base58(111, &hash160(&sender)); + assert_eq!( + registered.logs, + [lock_btc( + rsk_address_of(&sender), + &pegin, + &sender_address, + COIN + )] + ); + assert_eq!(count_of(®istered.logs, PEGIN_BTC), 0); +} + +/// `eventLoggerLogPeginBtc_after_rskip_170_activation`: from RSKIP170 it +/// logs `pegin_btc`, of protocol version 0, and not `lock_btc`. +#[test] +fn from_rskip170_a_credited_peg_in_logs_pegin_btc() { + let sender = key_from_seed("seed"); + let (pegin, registered, _) = whitelisted_pegin(&sender, Iris300); + assert_eq!( + registered.logs, + [pegin_btc(rsk_address_of(&sender), &pegin, COIN, 0)] + ); + assert_eq!(count_of(®istered.logs, LOCK_BTC), 0); +} + +/// rskj's `registerBtcTransactionLockTxNotWhitelisted_*`: three peg-ins of a +/// P2PKH sender the whitelist does not name, one to the active federation, +/// one to the retiring one and one to both, each refunded from what it sent +/// to the sender. +fn not_whitelisted(last: RskNetworkUpgrade) { + let config = regtest(); + let cfg = up_to(last, RSK_REGTEST_CHAIN_ID); + // Past the activation age, so that the federation created at block 0 is + // active and the one before it retiring. + let block = config.federation_activation_age_legacy + 10; + let retiring = [ + public_key(&[0xfa, 0x03]), + public_key(&[0xfa, 0x04]), + public_key(&[0xfa, 0x05]), + ]; + let active = [ + public_key(&[0xfb, 0x03]), + public_key(&[0xfb, 0x04]), + public_key(&[0xfb, 0x05]), + ]; + let to_active = p2sh_of(&standard_redeem(&active)); + let to_retiring = p2sh_of(&standard_redeem(&retiring)); + let sender = key_from_seed("not whitelisted"); + let pegins = [ + tx(vec![p2pkh_spend(&sender)], &[(5 * COIN, to_active.clone())]), + tx( + vec![p2pkh_spend(&sender)], + &[(10 * COIN, to_retiring.clone())], + ), + tx( + vec![p2pkh_spend(&sender)], + &[(3 * COIN, to_active), (4 * COIN, to_retiring)], + ), + ]; + let roots: Vec<[u8; 32]> = pegins + .iter() + .map(|pegin| bridge_tx::calculate_btc_tx_hash(&bitcoin::consensus::serialize(pegin))) + .collect(); + let mut ctx = ctx_at(block); + let head = HEIGHT + 2 + config.btc2rsk_minimum_acceptable_confirmations - 1; + hold_roots(&mut ctx, &roots, HEIGHT, head, &cfg); + fund_bridge_but(&mut ctx, 0); + store_federation(&mut ctx, OLD_FEDERATION, &retiring, 0, 1000); + store_federation(&mut ctx, NEW_FEDERATION, &active, 0, 1000); + set_fee_per_kb(&mut ctx, MILLICOIN); + + let rskip146 = cfg.has_rskip146(block); + for (height, pegin) in (HEIGHT..).zip(&pegins) { + let registered = register(&mut ctx, pegin, i64::from(height), &config, &cfg); + assert!(registered.processed); + // rskj's `logReleaseBtcRequested`, from RSKIP146. + assert_eq!( + count_of(®istered.logs, RELEASE_REQUESTED), + usize::from(rskip146) + ); + } + assert_eq!(balance(&mut ctx, rsk_address_of(&sender)), U256::ZERO); + assert_eq!(balance(&mut ctx, BRIDGE_ADDR), wei(MAX_RBTC)); + // Before RSKIP146 a refund waits without the hash of the transaction that + // made it, and from RSKIP146 with it. + let without_hash = waiting_in( + &mut ctx, + storage::PEGOUTS_WAITING_FOR_CONFIRMATIONS_KEY, + false, + ); + let with_hash = waiting_in( + &mut ctx, + storage::PEGOUTS_WAITING_FOR_CONFIRMATIONS_WITH_TXHASH_KEY, + true, + ); + let (refunds, none) = if rskip146 { + (with_hash, without_hash) + } else { + (without_hash, with_hash) + }; + assert!(none.is_empty()); + assert_eq!(refunds.len(), 3); + for pegin in &pegins { + let txid = pegin.compute_txid(); + let refund = refunds + .iter() + .find(|refund| refund.input[0].previous_output.txid == txid) + .expect("its refund"); + assert_eq!(refund.output.len(), 1); + assert_eq!(refund.output[0].script_pubkey, p2pkh(&hash160(&sender))); + let sent: u64 = pegin + .output + .iter() + .map(|output| output.value.to_sat()) + .sum(); + assert!(sent - refund.output[0].value.to_sat() <= MILLICOIN); + let spent: Vec = refund + .input + .iter() + .map(|input| input.previous_output) + .collect(); + let outputs: Vec = (0..pegin.output.len() as u32) + .map(|vout| OutPoint { txid, vout }) + .collect(); + assert_eq!(spent, outputs); + } + let for_signatures = peg::deserialize_rsk_txs_waiting_for_signatures( + &storage::bridge_load_bytes_named(&mut ctx, storage::PEGOUTS_WAITING_FOR_SIGNATURES_KEY), + ); + assert!(for_signatures.is_empty()); +} + +/// `registerBtcTransactionLockTxNotWhitelisted_before_rskip_146_activation`. +#[test] +fn before_rskip146_peg_ins_of_a_sender_not_whitelisted_are_refunded() { + not_whitelisted(Wasabi100); +} + +/// `registerBtcTransactionLockTxNotWhitelisted_after_rskip_146_activation`, +/// at Papyrus200, which brings RSKIP146. +#[test] +fn from_rskip146_peg_ins_of_a_sender_not_whitelisted_are_refunded() { + not_whitelisted(Papyrus200); +} + +/// `registerBtcTransaction_sending_segwit_tx_twice_locks_just_once`, at +/// Papyrus200, which brings RSKIP143: a segwit peg-in registered with its +/// witness, proven by the witness root its block's coinbase registered, then +/// without it, proven by the block's merkle root. It is marked processed by +/// its id without the witness, so the second registration credits nothing. +#[test] +fn a_segwit_peg_in_registered_twice_is_credited_once() { + let config = regtest(); + let cfg = up_to(Papyrus200, RSK_REGTEST_CHAIN_ID); + let sender = key_from_seed("segwit twice"); + let pegin = tx( + vec![p2sh_p2wpkh_spend(&sender)], + &[(5 * COIN, genesis_federation_output(&config))], + ); + let mut stripped = pegin.clone(); + stripped.input[0].witness = Witness::new(); + let with_witness = bitcoin::consensus::serialize(&pegin); + let without_witness = bitcoin::consensus::serialize(&stripped); + + let mut ctx = ctx_at(10); + let head = HEIGHT + config.btc2rsk_minimum_acceptable_confirmations - 1; + let root = bridge_tx::calculate_btc_tx_hash(&without_witness); + let header = hold_with_root(&mut ctx, root, HEIGHT, head, &cfg); + // Kept as `registerBtcCoinbaseTransaction` keeps it, as it is displayed. + let mut witness_root = bridge_tx::calculate_btc_tx_hash(&with_witness); + witness_root.reverse(); + let block_hash = btc_chain::bitcoin_hash_to_b256(&header.block_hash()).0; + bridge_tx::set_coinbase_information(&mut ctx, &block_hash, &witness_root); + fund_bridge_but(&mut ctx, 0); + // rskj's whitelist here is off from height 0. + storage::store_one_off_whitelist(&mut ctx, &[], 0); + + let first = register_bytes( + &mut ctx, + &with_witness, + &pegin, + i64::from(HEIGHT), + &config, + &cfg, + ); + assert!(first.processed); + assert_eq!(balance(&mut ctx, rsk_address_of(&sender)), wei(5 * COIN)); + let second = register_bytes( + &mut ctx, + &without_witness, + &stripped, + i64::from(HEIGHT), + &config, + &cfg, + ); + assert!(second.processed); + assert!(second.logs.is_empty()); + assert_eq!(balance(&mut ctx, rsk_address_of(&sender)), wei(5 * COIN)); + assert_eq!(storage::load_federation_utxos(&mut ctx, &cfg, 10).len(), 1); +} diff --git a/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_test/locking_cap.rs b/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_test/locking_cap.rs new file mode 100644 index 0000000..0bfc474 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_test/locking_cap.rs @@ -0,0 +1,222 @@ +//! rskj's `registerBtcTransaction_{before,after}_RSKIP134_activation_sends_ +//! {above,exactly,below}_lockingcap` and the `assertLockingCap` they call: a +//! legacy peg-in of a whitelisted P2PKH sender to both federations, the +//! retiring one first, with what the federations already hold, against a +//! locking cap. RSKIP134 is off below Papyrus200 and on from it. +//! +//! rskj's `assertLockingCap` mocks a federation activation age of 0 and +//! registers at RSK block 10. Here mainnet's constants hold, so the new +//! federation, created at block 0, is active from block 18,500, and the +//! peg-ins are registered at block 20,000. What the federations hold is what +//! the Bridge has paid out, as rskj's cap reads it +//! (`BridgeSupport.verifyLockDoesNotSurpassLockingCap`); rskj also puts it in +//! the federations' UTXOs, which neither the cap nor the refund reads. rskj +//! asserts some of its rows twice or three times over; each row is here once. + +use bitcoin::OutPoint; + +use crate::bridge::constants::BridgeConstants; +use crate::hardfork::{RskNetworkUpgrade, RSK_MAINNET_CHAIN_ID}; +use crate::precompiles::BRIDGE_ADDR; + +use super::super::support::*; + +use RskNetworkUpgrade::{Papyrus200, Wasabi100}; + +/// One bitcoin. +const C: u64 = COIN; + +/// One satoshi (bitcoinj's `Coin.SATOSHI`). +const SAT: u64 = 1; + +/// The RSK block the peg-ins are registered at. +const BLOCK: u64 = 20_000; + +/// `assertLockingCap(shouldLock, isLockingCapEnabled, lockingCap, +/// amountSentToNewFed, amountSentToOldFed, amountInNewFed, amountInOldFed)`. +fn assert_locking_cap( + should_lock: bool, + enabled: bool, + cap: u64, + sent_to_new: u64, + sent_to_old: u64, + in_new: u64, + in_old: u64, +) { + let case = + format!("{should_lock} {enabled} {cap} {sent_to_new} {sent_to_old} {in_new} {in_old}"); + let config = BridgeConstants { + minimum_pegin_tx_value: SAT, + legacy_minimum_pegin_tx_value: SAT, + initial_locking_cap: cap, + btc2rsk_minimum_acceptable_confirmations: 1, + ..BridgeConstants::mainnet() + }; + let cfg = up_to( + if enabled { Papyrus200 } else { Wasabi100 }, + RSK_MAINNET_CHAIN_ID, + ); + // `PegTestUtils.createSimpleActiveFederation`, and the new federation of + // the keys fb01, fb02 and fb03. + let old_keys = [public_key(&[0xfa, 0x01]), public_key(&[0xfa, 0x02])]; + let new_keys = [ + public_key(&[0xfb, 0x01]), + public_key(&[0xfb, 0x02]), + public_key(&[0xfb, 0x03]), + ]; + let sender = key_from_seed("assertLockingCap"); + let pegin = tx( + vec![p2pkh_spend(&sender)], + &[ + (sent_to_old, p2sh_of(&standard_redeem(&old_keys))), + (sent_to_new, p2sh_of(&standard_redeem(&new_keys))), + ], + ); + let lock_value = sent_to_new + sent_to_old; + let funds = in_new + in_old; + + let mut ctx = bridge_holding(&pegin, 1, BLOCK, &config, &cfg); + fund_bridge_but(&mut ctx, funds); + store_federation(&mut ctx, OLD_FEDERATION, &old_keys, 0, 1000); + store_federation(&mut ctx, NEW_FEDERATION, &new_keys, 0, 1000); + whitelist_once(&mut ctx, hash160(&sender), lock_value); + set_fee_per_kb(&mut ctx, MILLICOIN); + + let registered = register(&mut ctx, &pegin, 1, &config, &cfg); + + assert!(registered.processed, "processed: {case}"); + // rskj's whitelist uses the entry up before the cap is looked at + // (`WhitelistSupportImpl.verifyLockSenderIsWhitelisted`), locked or not. + assert!( + !whitelisted_once(&mut ctx, &hash160(&sender)), + "the entry is used up: {case}" + ); + let locked = if should_lock { lock_value } else { 0 }; + assert_eq!( + balance(&mut ctx, rsk_address_of(&sender)), + wei(locked), + "credited: {case}" + ); + assert_eq!( + balance(&mut ctx, BRIDGE_ADDR), + wei(MAX_RBTC - funds - locked), + "the Bridge: {case}" + ); + let refunds = waiting_pegouts(&mut ctx); + if should_lock { + assert!(refunds.is_empty(), "no refund: {case}"); + return; + } + assert_eq!(refunds.len(), 1, "a refund: {case}"); + let refund = &refunds[0]; + assert_eq!(refund.output.len(), 1, "{case}"); + assert_eq!( + refund.output[0].script_pubkey, + p2pkh(&hash160(&sender)), + "{case}" + ); + // It spends the peg-in's outputs and nothing else, so what it pays and its + // fee are what the peg-in sent. + assert!(refund.output[0].value.to_sat() < lock_value, "{case}"); + let txid = pegin.compute_txid(); + let spent: Vec = refund + .input + .iter() + .map(|input| input.previous_output) + .collect(); + assert_eq!( + spent, + [OutPoint { txid, vout: 0 }, OutPoint { txid, vout: 1 }], + "{case}" + ); +} + +/// `registerBtcTransaction_before_RSKIP134_activation_sends_above_lockingcap`. +#[test] +fn before_rskip134_a_peg_in_above_the_locking_cap_is_credited() { + for (cap, to_new, to_old, in_new, in_old) in [ + (3 * C, C, C, C, C), + (3 * C, 0, 2 * C, C, C), + (3 * C, 2 * C, 0, C, C), + (3 * C, 2 * C, 0, 2 * C, 0), + (3 * C, 2 * C, 0, 0, 2 * C), + (5 * C, 5 * C, 0, 0, SAT), + (5 * C, 5 * C, 0, SAT, 0), + ] { + assert_locking_cap(true, false, cap, to_new, to_old, in_new, in_old); + } +} + +/// `registerBtcTransaction_before_RSKIP134_activation_sends_exactly_lockingcap`. +#[test] +fn before_rskip134_a_peg_in_up_to_the_locking_cap_is_credited() { + for (to_new, to_old, in_new, in_old) in [ + (0, C, C, C), + (C, 0, C, C), + (C, C, C, C), + (C, C, 0, C), + (C, C, C, 0), + ] { + assert_locking_cap(true, false, 5 * C, to_new, to_old, in_new, in_old); + } +} + +/// `registerBtcTransaction_before_RSKIP134_activation_sends_below_lockingcap`. +#[test] +fn before_rskip134_a_peg_in_below_the_locking_cap_is_credited() { + for (to_new, to_old, in_new, in_old) in [ + (0, C, C, C), + (C, 0, C, C), + (C, C, C, C), + (C, C, C, 0), + (C, C, 0, C), + ] { + assert_locking_cap(true, false, 6 * C, to_new, to_old, in_new, in_old); + } +} + +/// `registerBtcTransaction_after_RSKIP134_activation_sends_above_lockingcap`. +#[test] +fn from_rskip134_a_peg_in_above_the_locking_cap_is_refunded() { + for (cap, to_new, to_old, in_new, in_old) in [ + (4 * C, C, 2 * C, C, C), + (4 * C, 0, 3 * C, C, C), + (4 * C, 3 * C, 0, C, C), + (4 * C, 3 * C, 0, 2 * C, 0), + (4 * C, 3 * C, 0, 0, 2 * C), + (5 * C, 5 * C, 0, 0, SAT), + (5 * C, 5 * C, 0, SAT, 0), + (5 * C, 5 * C, SAT, 0, 0), + (5 * C, 5 * C + SAT, 0, 0, 0), + ] { + assert_locking_cap(false, true, cap, to_new, to_old, in_new, in_old); + } +} + +/// `registerBtcTransaction_after_RSKIP134_activation_sends_exactly_lockingcap`. +#[test] +fn from_rskip134_a_peg_in_up_to_the_locking_cap_is_credited() { + for (to_new, to_old, in_new, in_old) in [ + (0, C, C, C), + (C, 0, C, C), + (C, C, C, C), + (C, C, 0, C), + (C, C, C, 0), + ] { + assert_locking_cap(true, true, 5 * C, to_new, to_old, in_new, in_old); + } +} + +/// `registerBtcTransaction_after_RSKIP134_activation_sends_below_lockingcap`. +#[test] +fn from_rskip134_a_peg_in_below_the_locking_cap_is_credited() { + for (to_new, to_old, in_new, in_old) in [ + (0, C, C, C), + (C, 0, C, C), + (C, C, C, C), + (C, C, C, 0), + (C, C, 0, C), + ] { + assert_locking_cap(true, true, 6 * C, to_new, to_old, in_new, in_old); + } +} diff --git a/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_test/merkle_roots.rs b/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_test/merkle_roots.rs new file mode 100644 index 0000000..64b6549 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_test/merkle_roots.rs @@ -0,0 +1,310 @@ +//! rskj's `BridgeSupportTest` tests of what proves a Bitcoin transaction to +//! the Bridge (`isBlockMerkleRootValid_*`, `getBtcTransactionConfirmations_*`, +//! `registerBtcTransaction_*_rskip_143_*`): the root its merkle branch climbs +//! to must be its block's merkle root or, from RSKIP143, the witness merkle +//! root registered with the block's coinbase. +//! +//! rskj's `isBlockMerkleRootValid` is a method of its own, which its five +//! tests call; rustock checks the same inside `getBtcTransactionConfirmations` +//! and `registerBtcTransaction`, so those tests are held through the +//! confirmations `getBtcTransactionConfirmations` answers: their count where +//! the root is valid, -5 where it is not. rskj's tests mock RSKIP143 off or on +//! alone; here they run on Wasabi100 and on Papyrus200, which brings it. + +use bitcoin::block::Header; +use bitcoin::{Transaction, Witness}; + +use crate::abi::encode::{self, Arg}; +use crate::bridge::constants::BridgeConstants; +use crate::bridge::{btc_chain, storage, tx as bridge_tx}; +use crate::hardfork::{RskHardforkConfig, RskNetworkUpgrade, RSK_REGTEST_CHAIN_ID}; +use crate::precompiles::BRIDGE_ADDR; + +use super::super::support::*; + +use RskNetworkUpgrade::{Papyrus200, Wasabi100}; + +/// Where rskj's tests hold the transaction's block, and the chain's head. +const HEIGHT: u32 = 50; +const HEAD: u32 = 132; + +/// The confirmations of a transaction in the block at [`HEIGHT`]. +const CONFIRMED: i64 = (HEAD - HEIGHT + 1) as i64; + +/// `getBtcTransactionConfirmations`' answer for a branch that climbs to +/// neither root (`BTC_TRANSACTION_CONFIRMATION_INVALID_MERKLE_BRANCH_ERROR_CODE`). +const INVALID_MERKLE_BRANCH: i64 = -5; + +/// What the transactions here send. +const AMOUNT: u64 = 10 * COIN; + +/// A transaction from the P2PKH key `sender` to rskj regtest's federation, +/// its input's witness `witness`. +fn transaction(sender: &[u8; 33], witness: Witness) -> Transaction { + let (script_sig, _) = p2pkh_spend(sender); + tx( + vec![(script_sig, witness)], + &[( + AMOUNT, + genesis_federation_output(&BridgeConstants::rskj_regtest()), + )], + ) +} + +/// rskj's tests' witness: one empty push. +fn a_witness() -> Witness { + let mut witness = Witness::new(); + witness.push([0u8; 0]); + witness +} + +/// The hash of the transaction's bytes, with its witness if it has one +/// (rskj's `getHash(true)`). +fn hash_of(tx: &Transaction) -> [u8; 32] { + bridge_tx::calculate_btc_tx_hash(&bitcoin::consensus::serialize(tx)) +} + +/// The transaction's id, of its bytes without its witness (rskj's +/// `getHash()`). +fn id_of(tx: &Transaction) -> [u8; 32] { + let mut stripped = tx.clone(); + for input in &mut stripped.input { + input.witness = Witness::new(); + } + hash_of(&stripped) +} + +/// A block's hash, as the Bridge's methods take it. +fn block_hash(header: &Header) -> [u8; 32] { + btc_chain::bitcoin_hash_to_b256(&header.block_hash()).0 +} + +/// A Bridge at RSK block 10 of rskj regtest's ladder up to `last`, whose +/// Bitcoin store holds a block at [`HEIGHT`] with the merkle root `root` and +/// the chain on it up to [`HEAD`], and, if any, `witness_root` registered as +/// the witness root of the block's coinbase. +fn holding( + last: RskNetworkUpgrade, + root: [u8; 32], + witness_root: Option<[u8; 32]>, +) -> (Ctx, BridgeConstants, RskHardforkConfig, Header) { + let config = BridgeConstants { + initial_locking_cap: 1_000 * COIN, + ..BridgeConstants::rskj_regtest() + }; + let cfg = up_to(last, RSK_REGTEST_CHAIN_ID); + let mut ctx = ctx_at(10); + let header = hold_with_root(&mut ctx, root, HEIGHT, HEAD, &cfg); + if let Some(mut witness_root) = witness_root { + // Kept as `registerBtcCoinbaseTransaction` keeps it, as it is displayed. + witness_root.reverse(); + bridge_tx::set_coinbase_information(&mut ctx, &block_hash(&header), &witness_root); + } + fund_bridge_but(&mut ctx, 0); + (ctx, config, cfg, header) +} + +/// `getBtcTransactionConfirmations` of the transaction whose hash is `hash`, +/// in the block `header`, by a branch of no hashes. +fn confirmations( + ctx: &mut Ctx, + config: &BridgeConstants, + cfg: &RskHardforkConfig, + hash: [u8; 32], + header: &Header, +) -> i64 { + let mut tx_hash = hash; + tx_hash.reverse(); + let args = encode::args(&[ + Arg::Word(tx_hash), + Arg::Word(block_hash(header)), + Arg::Word(encode::int(0)), + Arg::Words(&[]), + ]); + let use_v2 = cfg.has_stored_block_v2(block_of(ctx)); + let answer = bridge_tx::get_btc_transaction_confirmations(ctx, &args, 0, config, use_v2, cfg) + .expect("an answer"); + i64::from_be_bytes(answer.bytes[24..32].try_into().expect("eight bytes")) +} + +/// `getBtcTransactionConfirmations_rejects_tx_with_witness_before_rskip_143`. +#[test] +fn before_rskip143_a_branch_from_the_witness_hash_proves_nothing() { + let tx = transaction(&key_from_seed("k1"), a_witness()); + let (mut ctx, config, cfg, header) = holding(Wasabi100, id_of(&tx), None); + let answer = confirmations(&mut ctx, &config, &cfg, hash_of(&tx), &header); + assert_eq!(answer, INVALID_MERKLE_BRANCH); +} + +/// rskj's `isBlockMerkleRootValid` reads the witness root registered for a +/// block only from RSKIP143: before it, a branch to the root kept with the +/// block's coinbase proves nothing, and from it the transaction is proven +/// (`from_rskip143_a_branch_to_the_registered_witness_root_proves_the_transaction`). +/// No chain shows it, since a coinbase can be registered only from RSKIP143; +/// the root is put in place as `registerBtcCoinbaseTransaction` keeps it. +#[test] +fn before_rskip143_a_registered_witness_root_proves_nothing() { + let tx = transaction(&key_from_seed("k1"), a_witness()); + let (mut ctx, config, cfg, header) = holding(Wasabi100, id_of(&tx), Some(hash_of(&tx))); + let answer = confirmations(&mut ctx, &config, &cfg, hash_of(&tx), &header); + assert_eq!(answer, INVALID_MERKLE_BRANCH); +} + +/// `getBtcTransactionConfirmations_accepts_tx_with_witness_after_rskip_143` +/// and `isBlockMerkleRootValid_coinbase_information_not_null_and_equal_mroots_after_rskip_143`. +#[test] +fn from_rskip143_a_branch_to_the_registered_witness_root_proves_the_transaction() { + let tx = transaction(&key_from_seed("k2"), a_witness()); + let (mut ctx, config, cfg, header) = holding(Papyrus200, id_of(&tx), Some(hash_of(&tx))); + assert_eq!( + confirmations(&mut ctx, &config, &cfg, hash_of(&tx), &header), + CONFIRMED + ); +} + +/// `getBtcTransactionConfirmations_unregistered_coinbase_after_rskip_143` and +/// `isBlockMerkleRootValid_coinbase_information_null_after_rskip_143`. +#[test] +fn from_rskip143_a_witness_root_no_coinbase_registered_proves_nothing() { + let tx = transaction(&key_from_seed("k3"), a_witness()); + let (mut ctx, config, cfg, header) = holding(Papyrus200, id_of(&tx), None); + let answer = confirmations(&mut ctx, &config, &cfg, hash_of(&tx), &header); + assert_eq!(answer, INVALID_MERKLE_BRANCH); +} + +/// `getBtcTransactionConfirmations_registered_coinbase_unequal_witnessroot_after_rskip_143` +/// and `isBlockMerkleRootValid_coinbase_information_not_null_and_unequal_mroots_after_rskip_143`. +#[test] +fn from_rskip143_a_witness_root_other_than_the_registered_one_proves_nothing() { + let tx = transaction(&key_from_seed("k4"), a_witness()); + let (mut ctx, config, cfg, header) = holding(Papyrus200, id_of(&tx), Some([0x3c; 32])); + let answer = confirmations(&mut ctx, &config, &cfg, hash_of(&tx), &header); + assert_eq!(answer, INVALID_MERKLE_BRANCH); +} + +/// `getBtcTransactionConfirmations_tx_without_witness_unequal_roots_after_rskip_143` +/// and `isBlockMerkleRootValid_unequal_merkle_roots_before_rskip_143`: a root +/// other than the block's proves nothing, before RSKIP143 or from it. +#[test] +fn a_root_other_than_the_blocks_proves_nothing() { + for last in [Wasabi100, Papyrus200] { + let tx = transaction(&key_from_seed("k5"), Witness::new()); + let (mut ctx, config, cfg, header) = holding(last, [0x22; 32], None); + let answer = confirmations(&mut ctx, &config, &cfg, hash_of(&tx), &header); + assert_eq!(answer, INVALID_MERKLE_BRANCH, "{last:?}"); + } +} + +/// `getBtcTransactionConfirmations_accepts_tx_without_witness_after_rskip_143` +/// and `isBlockMerkleRootValid_equal_merkle_roots`: the block's merkle root +/// proves the transaction, before RSKIP143 and from it. +#[test] +fn the_blocks_merkle_root_proves_the_transaction() { + for last in [Wasabi100, Papyrus200] { + let tx = transaction(&key_from_seed("k6"), Witness::new()); + let (mut ctx, config, cfg, header) = holding(last, hash_of(&tx), None); + let answer = confirmations(&mut ctx, &config, &cfg, hash_of(&tx), &header); + assert_eq!(answer, CONFIRMED, "{last:?}"); + } +} + +/// [`holding`] the transaction `tx` of the whitelisted P2PKH key `sender`, +/// registered at [`HEIGHT`] by a tree of it alone. +fn registered( + last: RskNetworkUpgrade, + sender: &[u8; 33], + tx: &Transaction, + root: [u8; 32], + witness_root: Option<[u8; 32]>, +) -> (Registered, Ctx, RskHardforkConfig) { + let (mut ctx, config, cfg, _) = holding(last, root, witness_root); + whitelist_once(&mut ctx, hash160(sender), AMOUNT); + let registered = register(&mut ctx, tx, i64::from(HEIGHT), &config, &cfg); + (registered, ctx, cfg) +} + +/// Proven, so credited to the sender, the federation holding it, and marked +/// processed by its id. +fn assert_credited( + registered: &Registered, + ctx: &mut Ctx, + cfg: &RskHardforkConfig, + sender: &[u8; 33], +) { + assert!(registered.processed, "processed"); + assert_eq!( + balance(ctx, rsk_address_of(sender)), + wei(AMOUNT), + "credited" + ); + let utxos = storage::load_federation_utxos(ctx, cfg, 10); + let values: Vec = utxos.iter().map(|utxo| utxo.value_satoshis).collect(); + assert_eq!(values, [AMOUNT], "the federation holds it"); +} + +/// Not proven: the registration returns, and nothing is credited or marked. +fn assert_not_proven(registered: &Registered, ctx: &mut Ctx) { + assert!(registered.returned, "it returns"); + assert!(!registered.processed, "not processed"); + assert_eq!(balance(ctx, BRIDGE_ADDR), wei(MAX_RBTC), "nothing credited"); +} + +/// `registerBtcTransaction_rejects_tx_with_witness_before_rskip_143_activation`: +/// before RSKIP143 rskj reads a segwit transaction's marker as a count of no +/// inputs and throws. +#[test] +fn before_rskip143_a_segwit_transaction_fails_its_registration() { + let sender = key_from_seed("h1"); + let tx = transaction(&sender, a_witness()); + let (registered, mut ctx, _) = registered(Wasabi100, &sender, &tx, hash_of(&tx), None); + assert!(!registered.returned, "it throws"); + assert!(!registered.processed); + assert_eq!(balance(&mut ctx, BRIDGE_ADDR), wei(MAX_RBTC)); +} + +/// `registerBtcTransaction_accepts_lock_tx_with_witness_after_rskip_143_activation`. +#[test] +fn from_rskip143_a_segwit_peg_in_proven_by_its_coinbase_is_credited() { + let sender = key_from_seed("h2"); + let tx = transaction(&sender, a_witness()); + let (registered, mut ctx, cfg) = + registered(Papyrus200, &sender, &tx, id_of(&tx), Some(hash_of(&tx))); + assert_credited(®istered, &mut ctx, &cfg, &sender); +} + +/// `registerBtcTransaction_rejects_tx_with_witness_and_unregistered_coinbase_after_rskip_143_activation`. +#[test] +fn from_rskip143_a_segwit_transaction_no_coinbase_proves_is_not_registered() { + let sender = key_from_seed("h3"); + let tx = transaction(&sender, a_witness()); + let (registered, mut ctx, _) = registered(Papyrus200, &sender, &tx, id_of(&tx), None); + assert_not_proven(®istered, &mut ctx); +} + +/// `registerBtcTransaction_rejects_tx_with_witness_and_unqual_witness_root_after_rskip_143_activation`. +#[test] +fn from_rskip143_a_segwit_transaction_of_another_witness_root_is_not_registered() { + let sender = key_from_seed("h4"); + let tx = transaction(&sender, a_witness()); + let (registered, mut ctx, _) = + registered(Papyrus200, &sender, &tx, id_of(&tx), Some([0x3c; 32])); + assert_not_proven(®istered, &mut ctx); +} + +/// `registerBtcTransaction_rejects_tx_without_witness_unequal_roots_after_rskip_143`. +#[test] +fn from_rskip143_a_transaction_of_another_root_is_not_registered() { + let sender = key_from_seed("h5"); + let tx = transaction(&sender, Witness::new()); + let (registered, mut ctx, _) = registered(Papyrus200, &sender, &tx, [0x22; 32], None); + assert_not_proven(®istered, &mut ctx); +} + +/// `registerBtcTransaction_accepts_lock_tx_without_witness_after_rskip_143_activation`. +#[test] +fn from_rskip143_a_peg_in_proven_by_its_blocks_root_is_credited() { + let sender = key_from_seed("h6"); + let tx = transaction(&sender, Witness::new()); + let (registered, mut ctx, cfg) = registered(Papyrus200, &sender, &tx, hash_of(&tx), None); + assert_credited(®istered, &mut ctx, &cfg, &sender); +} diff --git a/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_test/mod.rs b/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_test/mod.rs new file mode 100644 index 0000000..27b4717 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_test/mod.rs @@ -0,0 +1,40 @@ +//! rskj's `BridgeSupportTest` (`co/rsk/peg/BridgeSupportTest.java`): its +//! tests of the rules an upgrade turns on or off in `BridgeSupport`, of the +//! peg-ins it credits or refunds, of what `updateCollections` moves, of what +//! proves a Bitcoin transaction or coinbase to it, and of the chain work its +//! Bitcoin store takes. +//! +//! rskj's tests mock most of what `BridgeSupport` reads: the activations, a +//! rule at a time, the storage, the sender a peg-in is read to be from. Here +//! the Bridge reads its storage as it keeps it, the activations are an +//! upgrade ladder, and a sender is the input a peg-in spends. Where rskj +//! mocks on a rule that no ladder has on without others, the case runs on the +//! ladder up to the upgrade that brings it, and the test says so. + +use bitcoin::Transaction; + +use crate::bridge::{peg, storage}; + +use super::support::Ctx; + +mod chain_work; +mod coinbase; +mod collections; +mod events; +mod locking_cap; +mod merkle_roots; +mod release_info; +mod sender_types; + +/// The peg-outs one cell of the set waiting for confirmations holds: the +/// cell without the hash of the transaction that made them, or, from +/// RSKIP146, the one with it. +fn waiting_in(ctx: &mut Ctx, cell: &str, with_hash: bool) -> Vec { + peg::deserialize_pegouts_waiting_for_confirmations( + &storage::bridge_load_bytes_named(ctx, cell), + with_hash, + ) + .into_iter() + .map(|entry| bitcoin::consensus::deserialize(&entry.btc_tx_raw).expect("a transaction")) + .collect() +} diff --git a/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_test/release_info.rs b/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_test/release_info.rs new file mode 100644 index 0000000..db92577 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_test/release_info.rs @@ -0,0 +1,279 @@ +//! rskj's `BridgeSupportTest.ReleaseTransactionInfo` tests of the refund of a +//! legacy peg-in from a multisig sender +//! (`registerBtcTransaction_forRefundableLegacyPeginFromMultisig_*RSKIP305*`) +//! and of the peg-out batch `updateCollections` builds +//! (`updateCollections_whenReleasesInQueue*RSKIP305*`): from RSKIP428 the +//! Bridge logs `pegout_transaction_created` with the value of each output the +//! release spends, and from RSKIP305 it also keeps those values under the +//! release's hash (`BridgeSupport.processReleaseTransactionInfo`). Lovell700 +//! is before RSKIP305, and Reed800 brings it; rskj's tests of the latter have +//! every upgrade on, which Vetiver900 is. +//! +//! The refunded peg-in pays mainnet's genesis federation, at the height the +//! pegout index is read from. + +use alloy_primitives::{B256, U256}; +use bitcoin::Transaction; + +use crate::bridge::constants::BridgeConstants; +use crate::bridge::peg::{self, ReleaseRequest}; +use crate::bridge::storage::{self, BridgeUtxo}; +use crate::bridge::tx as bridge_tx; +use crate::hardfork::{RskNetworkUpgrade, RSK_MAINNET_CHAIN_ID}; + +use super::super::support::*; + +use RskNetworkUpgrade::{Lovell700, Reed800, Vetiver900}; + +/// One bitcoin as bitcoinj's `VarInt` writes it. +const ONE_BITCOIN_VARINT: [u8; 5] = [0xfe, 0x00, 0xe1, 0xf5, 0x05]; + +/// `pegout_transaction_created(bytes32 indexed btcTxHash, bytes +/// utxoOutpointValues)`, the values as bitcoinj's `VarInt`s. +fn pegout_transaction_created(release: &Transaction, values: &[u8]) -> Event { + let mut data = U256::from(32u64).to_be_bytes::<32>().to_vec(); + data.extend_from_slice(&U256::from(values.len()).to_be_bytes::<32>()); + data.extend_from_slice(values); + data.resize(data.len().div_ceil(32) * 32, 0); + ( + vec![ + topic("pegout_transaction_created(bytes32,bytes)"), + B256::from(txid_bytes(release)), + ], + data, + ) +} + +/// A bitcoin from a two-of-two multisig to mainnet's federation, registered +/// at RSK block 10 of mainnet's ladder up to `last`: its refund, the events +/// the registration logged, and what is kept under the refund's hash. +fn refunded(last: RskNetworkUpgrade) -> (Transaction, Vec, Option>) { + let config = BridgeConstants::mainnet(); + let cfg = up_to(last, RSK_MAINNET_CHAIN_ID); + let redeem = multisig( + &[ + key_from_seed("legacy_pegin_p2sh_multisig_key_1"), + key_from_seed("legacy_pegin_p2sh_multisig_key_2"), + ], + 2, + ); + let pegin = tx( + vec![p2sh_multisig_spend(&redeem, 2)], + &[(COIN, genesis_federation_output(&config))], + ); + let height = config.btc_height_when_pegout_tx_index_activates + + config.pegout_tx_index_grace_period_in_btc_blocks; + let mut ctx = bridge_holding(&pegin, height, 10, &config, &cfg); + let registered = register(&mut ctx, &pegin, i64::from(height), &config, &cfg); + assert!(registered.processed); + let refunds = waiting_pegouts(&mut ctx); + assert_eq!(refunds.len(), 1, "a refund"); + let refund = refunds[0].clone(); + let key = storage::compound_key( + storage::RELEASES_OUTPOINTS_VALUES_KEY, + "-", + &bridge_tx::to_hex(&txid_bytes(&refund)), + ); + let kept = storage::bridge_load_raw(&mut ctx, key); + (refund, registered.logs, kept) +} + +/// `registerBtcTransaction_forRefundableLegacyPeginFromMultisig_beforeRSKIP305_justEmitsPegoutTransactionCreatedEvent`. +#[test] +fn before_rskip305_a_refund_is_logged_with_what_it_spends_and_nothing_is_kept() { + let (refund, logs, kept) = refunded(Lovell700); + assert!(logs.contains(&release_requested(&refund, COIN)), "{logs:?}"); + assert!( + logs.contains(&pegout_transaction_created(&refund, &ONE_BITCOIN_VARINT)), + "{logs:?}" + ); + assert_eq!(kept, None); +} + +/// `registerBtcTransaction_forRefundableLegacyPeginFromMultisig_afterRSKIP305_processReleaseTransactionsInfo`. +#[test] +fn from_rskip305_what_a_refund_spends_is_kept_under_its_hash() { + for last in [Reed800, Vetiver900] { + let (refund, logs, kept) = refunded(last); + assert!( + logs.contains(&release_requested(&refund, COIN)), + "{last:?} {logs:?}" + ); + let created = pegout_transaction_created(&refund, &ONE_BITCOIN_VARINT); + assert!(logs.contains(&created), "{last:?} {logs:?}"); + assert_eq!(kept, Some(ONE_BITCOIN_VARINT.to_vec()), "{last:?}"); + } +} + +/// What the federation of the `updateCollections` tests holds, in satoshis, +/// one output each: a batch of the two requests [`REQUESTS`] needs all three, +/// and leaves change. +const OUTPOINT_VALUES: [u64; 3] = [400_000, 90_000, 50_000]; + +/// [`OUTPOINT_VALUES`] as bitcoinj's `VarInt`s. +const OUTPOINT_VALUES_VARINT: [u8; 13] = [ + 0xfe, 0x80, 0x1a, 0x06, 0x00, 0xfe, 0x90, 0x5f, 0x01, 0x00, 0xfd, 0x50, 0xc3, +]; + +/// The two requests queued, each with the hash of the transaction that made +/// it: 100,000 and 400,000 satoshis. +const REQUESTS: [([u8; 20], u64, [u8; 32]); 2] = [ + ([0x01; 20], 100_000, [0x01; 32]), + ([0x02; 20], 400_000, [0x02; 32]), +]; + +/// rskj `BitcoinUtils.getSigHashForPegoutIndex` of a peg-out whose inputs +/// carry no signatures: `hashForSignature(0, redeem, ALL, false)`. +fn index_sighash(tx: &Transaction, redeem: &[u8]) -> [u8; 32] { + use bitcoin::hashes::Hash; + let mut copy = tx.clone(); + for (index, input) in copy.input.iter_mut().enumerate() { + input.script_sig = if index == 0 { + bitcoin::ScriptBuf::from_bytes(redeem.to_vec()) + } else { + bitcoin::ScriptBuf::new() + }; + } + let mut preimage = bitcoin::consensus::serialize(©); + preimage.extend_from_slice(&1u32.to_le_bytes()); + bitcoin::hashes::sha256d::Hash::hash(&preimage).to_byte_array() +} + +/// What rskj's `ReleaseTransactionInfo` tests of `updateCollections` look at +/// once it has run. +struct Batched { + ctx: Ctx, + /// The peg-out it built. + pegout: Transaction, + /// The RSK block and the creation hash it waits for confirmations under. + waiting_under: (u64, Option<[u8; 32]>), + logs: Vec, + /// The active federation's redeem script. + redeem: Vec, +} + +/// rskj's `ReleaseTransactionInfo` set-up for `updateCollections`, at RSK +/// block 0 of mainnet's ladder up to `last`: `BridgeSupportTest`'s active +/// federation, a P2SH ERP federation, holding [`OUTPOINT_VALUES`], the fee +/// per kilobyte 10,000 satoshis, and [`REQUESTS`] queued; then +/// `updateCollections` in the transaction [`RSK_TX_HASH`]. +fn batched(last: RskNetworkUpgrade) -> Batched { + let config = BridgeConstants::mainnet(); + let cfg = up_to(last, RSK_MAINNET_CHAIN_ID); + let mut ctx = ctx_at(0); + fund_bridge_but(&mut ctx, 0); + set_fee_per_kb(&mut ctx, 10_000); + let members: Vec<[u8; 33]> = (1u8..=9).map(|seed| public_key(&[seed])).collect(); + store_federation(&mut ctx, NEW_FEDERATION, &members, 1, 3000); + let redeem = erp_redeem( + &members, + &keys_of(config.erp_fed_pubkeys), + config.erp_fed_activation_delay, + ); + let utxos: Vec = OUTPOINT_VALUES + .iter() + .zip(1u8..) + .map(|(&value_satoshis, tag)| BridgeUtxo { + tx_hash: [tag; 32], + vout: 0, + value_satoshis, + height: 1, + script: p2sh_of(&redeem).to_bytes(), + coinbase: false, + }) + .collect(); + storage::store_federation_utxos(&mut ctx, &cfg, 0, &utxos); + let queue: Vec = REQUESTS + .iter() + .map( + |&(btc_dest_hash160, amount_satoshis, hash)| ReleaseRequest { + btc_dest_hash160, + amount_satoshis, + rsk_tx_hash: Some(hash), + }, + ) + .collect(); + storage::bridge_store_bytes_named( + &mut ctx, + "releaseRequestQueueWithTxHash", + &peg::serialize_release_queue_with_hash(&queue), + ); + + in_a_call(&mut ctx, |ctx| { + peg::update_collections(ctx, 0, &config, &cfg, &tx_context()) + }) + .expect("updateCollections returns"); + let logs = take_logs(&mut ctx); + let waiting = peg::load_pegout_confirmation_set(&mut ctx, true); + assert_eq!(waiting.len(), 1, "one peg-out"); + let pegout = bitcoin::consensus::deserialize(&waiting[0].btc_tx_raw).expect("a transaction"); + Batched { + ctx, + pegout, + waiting_under: (waiting[0].rsk_block_height, waiting[0].rsk_tx_hash), + logs, + redeem, + } +} + +impl Batched { + /// What the Bridge keeps under the peg-out's hash. + fn kept(&mut self) -> Option> { + let hash = bridge_tx::to_hex(&txid_bytes(&self.pegout)); + let key = storage::compound_key(storage::RELEASES_OUTPOINTS_VALUES_KEY, "-", &hash); + storage::bridge_load_raw(&mut self.ctx, key) + } + + /// Whether the peg-out's first input's sighash is in the pegout index. + fn indexed(&mut self) -> bool { + let sighash = bridge_tx::to_hex(&index_sighash(&self.pegout, &self.redeem)); + let key = storage::compound_key("pegoutTxSigHash", "-", &sighash); + storage::bridge_load_raw(&mut self.ctx, key) == Some(vec![1]) + } + + /// rskj's assertions that the batch was settled, but for what is kept + /// under its hash: waiting for confirmations under this call's hash and + /// block, in the pegout index, logged as requested for all the queue + /// asked, and as created with what its inputs spend. + fn assert_settled(&mut self) { + assert_eq!(self.waiting_under, (0, Some(RSK_TX_HASH))); + assert!(self.indexed(), "in the pegout index"); + let total = REQUESTS.iter().map(|request| request.1).sum(); + let logs = &self.logs; + assert!( + logs.contains(&release_requested(&self.pegout, total)), + "{logs:?}" + ); + let created = pegout_transaction_created(&self.pegout, &OUTPOINT_VALUES_VARINT); + assert!(logs.contains(&created), "{logs:?}"); + } +} + +/// `updateCollections_whenReleasesInQueue_beforeRSKIP305_justEmitsPegoutTransactionCreatedEvent`. +#[test] +fn before_rskip305_a_batch_is_logged_with_what_it_spends_and_nothing_is_kept() { + let mut batched = batched(Lovell700); + batched.assert_settled(); + assert_eq!(batched.kept(), None); +} + +/// `updateCollections_whenReleasesInQueueAndLegacyFed_afterRSKIP305_shouldSetRedeemDataInScriptSigAndProcessReleaseTransactionInfo`: +/// the batch's first input pushes the federation's redeem script last in its +/// scriptSig, and what the batch spends is kept under its hash. +#[test] +fn from_rskip305_a_legacy_federations_batch_keeps_what_it_spends_under_its_hash() { + for last in [Reed800, Vetiver900] { + let mut batched = batched(last); + let pushed_last = match batched.pegout.input[0].script_sig.instructions().last() { + Some(Ok(bitcoin::script::Instruction::PushBytes(bytes))) => bytes.as_bytes().to_vec(), + other => panic!("no redeem script pushed last: {other:?}"), + }; + assert_eq!(pushed_last, batched.redeem, "{last:?}"); + batched.assert_settled(); + assert_eq!( + batched.kept(), + Some(OUTPOINT_VALUES_VARINT.to_vec()), + "{last:?}" + ); + } +} diff --git a/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_test/sender_types.rs b/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_test/sender_types.rs new file mode 100644 index 0000000..0d78916 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegins/bridge_support_test/sender_types.rs @@ -0,0 +1,399 @@ +//! rskj's `BridgeSupportTest` tests of who a peg-in is from, as rskj reads +//! its first input (`when_registerBtcTransaction_*`), of the instructions it +//! carries (`registerBtcTransaction_*_rskip_170_*`), and of the refunds +//! `registerPegIn` makes (`registerPegIn_*`). +//! +//! rskj's tests mock the sender a peg-in is read to be from; here it is the +//! input the peg-in spends: a P2PKH spend, a P2SH-P2WPKH one, a P2SH multisig +//! one, a P2SH-P2WSH multisig one, or one rskj reads no sender from. Before +//! RSKIP143 rskj takes a P2PKH sender only; from it, a P2SH-P2WPKH one too, +//! and refunds the others (`BridgeUtils.txIsProcessableInLegacyVersion`, +//! `BridgeSupport.isTxLockableForLegacyVersion`). rskj's "before fork" mocks +//! RSKIP143 off and its "after fork" RSKIP143 on alone; here they run on +//! Wasabi100 and on Papyrus200, which brings RSKIP143 with RSKIP134 and +//! RSKIP146. +//! +//! rskj's `registerPegIn_*` call `registerPegIn` on a mocked storage, which +//! keeps no processed mark, so they find none; here the peg-ins are +//! registered, and the mark rskj's `processPegInVersionLegacy` sets +//! (`BridgeSupport.java:682`) is held. + +use alloy_primitives::{Address as RskAddress, U256}; +use bitcoin::{OutPoint, ScriptBuf, Transaction, Witness}; + +use crate::bridge::constants::BridgeConstants; +use crate::bridge::storage; +use crate::hardfork::{RskNetworkUpgrade, RSK_REGTEST_CHAIN_ID}; +use crate::precompiles::BRIDGE_ADDR; + +use super::super::support::*; + +use RskNetworkUpgrade::{Iris300, Papyrus200, Wasabi100}; + +/// The Bitcoin height a peg-in is registered at. +const HEIGHT: u32 = 100; + +/// A locking cap no peg-in here reaches. +const NO_CAP: u64 = 1_000 * COIN; + +/// What a peg-in sends. +const AMOUNT: u64 = 5 * COIN; + +/// The P2PKH key the peg-ins are sent from. +fn sender() -> [u8; 33] { + key_from_seed("sender") +} + +/// The multisig redeem script the multisig peg-ins are sent from. +fn multisig_redeem() -> Vec { + standard_redeem(&[ + key_from_seed("m1"), + key_from_seed("m2"), + key_from_seed("m3"), + ]) +} + +/// The P2SH hash of the P2SH-P2WPKH output of `key`: rskj's address of the +/// sender (`P2shP2wpkhBtcLockSender`). +fn p2sh_p2wpkh_hash(key: &[u8; 33]) -> [u8; 20] { + let mut program = vec![0x00, 20]; + program.extend_from_slice(&hash160(key)); + hash160(&program) +} + +/// Instructions rskj cannot read: `RSKT`, version 1, and too few bytes for an +/// RSK destination. +fn unreadable_instructions() -> ScriptBuf { + op_return(&[b"RSKT".as_slice(), &[1], &[0xd7; 5]].concat()) +} + +/// [`AMOUNT`] from `input` to rskj regtest's federation, with the output +/// `instructions` beside it, if any. +fn pegin(input: (ScriptBuf, Witness), instructions: Option) -> Transaction { + let mut outputs = vec![( + AMOUNT, + genesis_federation_output(&BridgeConstants::rskj_regtest()), + )]; + outputs.extend(instructions.map(|script| (0, script))); + tx(vec![input], &outputs) +} + +/// `pegin` registered at RSK block 10 of rskj regtest's ladder up to `last`, +/// with a locking cap of `cap`, `whitelisted` on the whitelist once for +/// [`AMOUNT`] if any: what it answered, and the Bridge after it. +fn registered( + pegin: &Transaction, + last: RskNetworkUpgrade, + cap: u64, + whitelisted: Option<[u8; 20]>, +) -> (Registered, Ctx) { + let config = BridgeConstants { + initial_locking_cap: cap, + ..BridgeConstants::rskj_regtest() + }; + let cfg = up_to(last, RSK_REGTEST_CHAIN_ID); + let mut ctx = bridge_holding(pegin, HEIGHT, 10, &config, &cfg); + set_fee_per_kb(&mut ctx, MILLICOIN); + if let Some(hash) = whitelisted { + whitelist_once(&mut ctx, hash, AMOUNT); + } + let registered = register(&mut ctx, pegin, i64::from(HEIGHT), &config, &cfg); + (registered, ctx) +} + +/// Nothing credited, nothing refunded, and not processed. +fn assert_left_alone(registered: &Registered, ctx: &mut Ctx) { + assert!(!registered.processed, "not processed"); + assert_eq!(balance(ctx, BRIDGE_ADDR), wei(MAX_RBTC), "nothing credited"); + assert!(waiting_pegouts(ctx).is_empty(), "nothing refunded"); +} + +/// `to` credited [`AMOUNT`], the federation holding the peg-in's output, and +/// processed. +fn assert_credited( + registered: &Registered, + ctx: &mut Ctx, + to: RskAddress, + last: RskNetworkUpgrade, +) { + assert!(registered.processed, "processed"); + assert_eq!(balance(ctx, to), wei(AMOUNT), "credited"); + assert_eq!( + balance(ctx, BRIDGE_ADDR), + wei(MAX_RBTC - AMOUNT), + "paid by the Bridge" + ); + assert!(waiting_pegouts(ctx).is_empty(), "nothing refunded"); + let utxos = storage::load_federation_utxos(ctx, &up_to(last, RSK_REGTEST_CHAIN_ID), 10); + let values: Vec = utxos.iter().map(|utxo| utxo.value_satoshis).collect(); + assert_eq!(values, [AMOUNT], "the federation holds it"); +} + +/// One refund to `to`, of the peg-in's output to the federation less a fee +/// within a millicoin, nothing credited, and processed. +fn assert_refunded(registered: &Registered, ctx: &mut Ctx, pegin: &Transaction, to: &ScriptBuf) { + assert!(registered.processed, "processed"); + assert_eq!(balance(ctx, BRIDGE_ADDR), wei(MAX_RBTC), "nothing credited"); + let refunds = waiting_pegouts(ctx); + assert_eq!(refunds.len(), 1, "a refund"); + let refund = &refunds[0]; + assert_eq!(refund.output.len(), 1); + assert_eq!(&refund.output[0].script_pubkey, to, "where the refund goes"); + assert!( + AMOUNT - refund.output[0].value.to_sat() <= MILLICOIN, + "its fee" + ); + let spent: Vec = refund + .input + .iter() + .map(|input| input.previous_output) + .collect(); + assert_eq!( + spent, + [OutPoint { + txid: pegin.compute_txid(), + vout: 0 + }] + ); +} + +/// `when_registerBtcTransaction_sender_not_recognized_before_rskip170_no_lock_and_no_refund`, +/// at Papyrus200: RSKIP143 on, RSKIP170 not yet. +#[test] +fn before_rskip170_a_peg_in_without_a_sender_is_left_alone() { + let pegin = pegin(no_sender(), None); + let (registered, mut ctx) = registered(&pegin, Papyrus200, NO_CAP, None); + assert_left_alone(®istered, &mut ctx); +} + +/// `when_registerBtcTransaction_sender_not_recognized_after_rskip170_lock`. +#[test] +fn from_rskip170_a_peg_in_without_a_sender_is_credited_where_its_instructions_say() { + let destination = RskAddress::repeat_byte(0xd5); + let pegin = pegin(no_sender(), Some(rskt(1, destination, None))); + let (registered, mut ctx) = registered(&pegin, Iris300, NO_CAP, None); + assert_credited(®istered, &mut ctx, destination, Iris300); +} + +/// `when_registerBtcTransaction_usesLegacyType_beforeFork_lock_and_no_refund`. +#[test] +fn before_rskip143_a_whitelisted_p2pkh_sender_is_credited() { + let key = sender(); + let pegin = pegin(p2pkh_spend(&key), None); + let (registered, mut ctx) = registered(&pegin, Wasabi100, NO_CAP, Some(hash160(&key))); + assert_credited(®istered, &mut ctx, rsk_address_of(&key), Wasabi100); + assert!( + !whitelisted_once(&mut ctx, &hash160(&key)), + "the entry is used up" + ); +} + +/// `when_registerBtcTransaction_usesLegacyType_afterFork_notWhitelisted_no_lock_and_refund`. +#[test] +fn from_rskip143_a_p2pkh_sender_not_whitelisted_is_refunded() { + let key = sender(); + let pegin = pegin(p2pkh_spend(&key), None); + let (registered, mut ctx) = registered(&pegin, Papyrus200, NO_CAP, None); + assert_refunded(®istered, &mut ctx, &pegin, &p2pkh(&hash160(&key))); + assert_eq!(balance(&mut ctx, rsk_address_of(&key)), U256::ZERO); +} + +/// `when_registerBtcTransaction_usesLegacyType_afterFork_lock_and_no_refund`. +#[test] +fn from_rskip143_a_whitelisted_p2pkh_sender_is_credited() { + let key = sender(); + let pegin = pegin(p2pkh_spend(&key), None); + let (registered, mut ctx) = registered(&pegin, Papyrus200, NO_CAP, Some(hash160(&key))); + assert_credited(®istered, &mut ctx, rsk_address_of(&key), Papyrus200); + assert!( + !whitelisted_once(&mut ctx, &hash160(&key)), + "the entry is used up" + ); +} + +/// `when_registerBtcTransaction_usesSegCompatibilityType_beforeFork_no_lock_and_no_refund`. +#[test] +fn before_rskip143_a_p2sh_p2wpkh_sender_is_left_alone() { + let key = sender(); + let pegin = pegin(p2sh_p2wpkh_spend(&key), None); + let (registered, mut ctx) = registered(&pegin, Wasabi100, NO_CAP, Some(p2sh_p2wpkh_hash(&key))); + assert_left_alone(®istered, &mut ctx); +} + +/// `when_registerBtcTransaction_usesSegCompatibilityType_afterFork_lock_and_no_refund`. +#[test] +fn from_rskip143_a_whitelisted_p2sh_p2wpkh_sender_is_credited() { + let key = sender(); + let pegin = pegin(p2sh_p2wpkh_spend(&key), None); + let (registered, mut ctx) = + registered(&pegin, Papyrus200, NO_CAP, Some(p2sh_p2wpkh_hash(&key))); + assert_credited(®istered, &mut ctx, rsk_address_of(&key), Papyrus200); + assert!( + !whitelisted_once(&mut ctx, &p2sh_p2wpkh_hash(&key)), + "the entry is used up" + ); +} + +/// `when_registerBtcTransaction_usesSegCompatibilityType_afterFork_notWhitelisted_no_lock_and_refund`: +/// refunded to the sender's P2SH address. +#[test] +fn from_rskip143_a_p2sh_p2wpkh_sender_not_whitelisted_is_refunded() { + let key = sender(); + let pegin = pegin(p2sh_p2wpkh_spend(&key), None); + let (registered, mut ctx) = registered(&pegin, Papyrus200, NO_CAP, None); + assert_refunded( + ®istered, + &mut ctx, + &pegin, + &p2sh(&p2sh_p2wpkh_hash(&key)), + ); +} + +/// `when_registerBtcTransaction_usesMultisigType_beforeFork_no_lock_and_no_refund`. +#[test] +fn before_rskip143_a_multisig_sender_is_left_alone() { + let pegin = pegin(p2sh_multisig_spend(&multisig_redeem(), 2), None); + let (registered, mut ctx) = registered(&pegin, Wasabi100, NO_CAP, None); + assert_left_alone(®istered, &mut ctx); +} + +/// `when_registerBtcTransaction_usesMultisigType_afterFork_no_lock_and_refund` +/// and `registerPegIn_version0_tx_no_lockable_by_invalid_sender`: refunded to +/// the multisig's P2SH address. +#[test] +fn from_rskip143_a_multisig_sender_is_refunded() { + let redeem = multisig_redeem(); + let pegin = pegin(p2sh_multisig_spend(&redeem, 2), None); + let (registered, mut ctx) = registered(&pegin, Papyrus200, NO_CAP, None); + assert_refunded(®istered, &mut ctx, &pegin, &p2sh_of(&redeem)); +} + +/// `when_registerBtcTransaction_usesMultisigWithWitnessType_beforeFork_no_lock_and_no_refund`. +#[test] +fn before_rskip143_a_p2sh_p2wsh_multisig_sender_is_left_alone() { + let pegin = pegin(p2sh_p2wsh_spend(&multisig_redeem(), 2), None); + let (registered, mut ctx) = registered(&pegin, Wasabi100, NO_CAP, None); + assert_left_alone(®istered, &mut ctx); +} + +/// `when_registerBtcTransaction_usesMultisigWithWitnessType_afterFork_no_lock_and_refund`: +/// refunded to the multisig's P2SH-P2WSH address. +#[test] +fn from_rskip143_a_p2sh_p2wsh_multisig_sender_is_refunded() { + let redeem = multisig_redeem(); + let pegin = pegin(p2sh_p2wsh_spend(&redeem, 2), None); + let (registered, mut ctx) = registered(&pegin, Papyrus200, NO_CAP, None); + assert_refunded(®istered, &mut ctx, &pegin, &p2sh_p2wsh_of(&redeem)); +} + +/// `registerBtcTransaction_accepts_lock_tx_version1_after_rskip_170_activation`. +#[test] +fn from_rskip170_a_peg_in_is_credited_where_its_instructions_say() { + let key = sender(); + let destination = RskAddress::repeat_byte(0xd1); + let pegin = pegin(p2pkh_spend(&key), Some(rskt(1, destination, None))); + let (registered, mut ctx) = registered(&pegin, Iris300, NO_CAP, None); + assert_credited(®istered, &mut ctx, destination, Iris300); + assert_eq!(balance(&mut ctx, rsk_address_of(&key)), U256::ZERO); +} + +/// `registerBtcTransaction_ignores_pegin_instructions_before_rskip_170_activation`. +#[test] +fn before_rskip170_a_peg_in_is_credited_to_its_sender_whatever_its_instructions_say() { + let key = sender(); + let destination = RskAddress::repeat_byte(0xd2); + let pegin = pegin(p2pkh_spend(&key), Some(rskt(1, destination, None))); + let (registered, mut ctx) = registered(&pegin, Papyrus200, NO_CAP, Some(hash160(&key))); + assert_credited(®istered, &mut ctx, rsk_address_of(&key), Papyrus200); + assert_eq!(balance(&mut ctx, destination), U256::ZERO); +} + +/// `when_registerBtcTransaction_invalidPeginProtocolVersion_afterFork_no_lock_and_refund`: +/// instructions of a version rskj does not know, 2, are refunded to the +/// sender. +#[test] +fn from_rskip170_a_peg_in_of_an_unknown_version_is_refunded() { + let key = sender(); + let pegin = pegin( + p2pkh_spend(&key), + Some(rskt(2, RskAddress::repeat_byte(0xd3), None)), + ); + let (registered, mut ctx) = registered(&pegin, Iris300, NO_CAP, None); + assert_refunded(®istered, &mut ctx, &pegin, &p2pkh(&hash160(&key))); +} + +/// `registerPegIn_version0_tx_no_lockable_by_surpassing_locking_cap`, at +/// Papyrus200, which brings RSKIP134. +#[test] +fn from_rskip134_a_legacy_peg_in_past_the_locking_cap_is_refunded() { + let key = sender(); + let pegin = pegin(p2pkh_spend(&key), None); + let (registered, mut ctx) = registered(&pegin, Papyrus200, COIN, Some(hash160(&key))); + assert_refunded(®istered, &mut ctx, &pegin, &p2pkh(&hash160(&key))); +} + +/// `registerPegIn_version1_tx_no_lockable_by_surpassing_locking_cap`: with no +/// refund address in the instructions, refunded to the sender. +#[test] +fn from_rskip170_a_peg_in_past_the_locking_cap_is_refunded_to_its_sender() { + let key = sender(); + let pegin = pegin( + p2pkh_spend(&key), + Some(rskt(1, RskAddress::repeat_byte(0xd4), None)), + ); + let (registered, mut ctx) = registered(&pegin, Iris300, COIN, None); + assert_refunded(®istered, &mut ctx, &pegin, &p2pkh(&hash160(&key))); +} + +/// `registerPegIn_version1_tx_no_lockable_by_surpassing_locking_cap_unknown_sender_with_refund_address`. +#[test] +fn from_rskip170_a_peg_in_without_a_sender_past_the_locking_cap_is_refunded_where_it_says() { + let refund_to = [0x7a; 20]; + let pegin = pegin( + no_sender(), + Some(rskt( + 1, + RskAddress::repeat_byte(0xd6), + Some(Refund::P2pkh(refund_to)), + )), + ); + let (registered, mut ctx) = registered(&pegin, Iris300, COIN, None); + assert_refunded(®istered, &mut ctx, &pegin, &p2pkh(&refund_to)); +} + +/// `registerPegIn_version1_tx_no_lockable_by_surpassing_locking_cap_unknown_sender_without_refund_address`: +/// nowhere to refund to. +#[test] +fn from_rskip170_a_peg_in_without_a_sender_or_refund_address_past_the_locking_cap_is_not_refunded() +{ + let pegin = pegin( + no_sender(), + Some(rskt(1, RskAddress::repeat_byte(0xd6), None)), + ); + let (_, mut ctx) = registered(&pegin, Iris300, COIN, None); + assert!(waiting_pegouts(&mut ctx).is_empty(), "no refund"); + assert_eq!( + balance(&mut ctx, BRIDGE_ADDR), + wei(MAX_RBTC), + "nothing credited" + ); +} + +/// `registerPegIn_errorParsingPeginInstructions_beforeRskip170_dontRefundSender`: +/// before RSKIP170 the instructions are not read, and with no sender rskj +/// reads, the peg-in is left alone. +#[test] +fn before_rskip170_unreadable_instructions_are_not_refunded() { + let pegin = pegin(no_sender(), Some(unreadable_instructions())); + let (registered, mut ctx) = registered(&pegin, Papyrus200, NO_CAP, None); + assert_left_alone(®istered, &mut ctx); +} + +/// `registerPegIn_errorParsingPeginInstructions_afterRskip170_refundSender`. +#[test] +fn from_rskip170_unreadable_instructions_are_refunded_to_the_sender() { + let key = sender(); + let pegin = pegin(p2pkh_spend(&key), Some(unreadable_instructions())); + let (registered, mut ctx) = registered(&pegin, Iris300, NO_CAP, None); + assert_refunded(®istered, &mut ctx, &pegin, &p2pkh(&hash160(&key))); +} diff --git a/crates/execution/src/rskj_tests/bridge_pegins/mod.rs b/crates/execution/src/rskj_tests/bridge_pegins/mod.rs new file mode 100644 index 0000000..fc6d2e3 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegins/mod.rs @@ -0,0 +1,11 @@ +//! Peg-ins (rskj's `BridgeSupportTest`, the whitelist, peg-in information). + +mod bitcoin_utils_test; +mod bridge_constants_test; +mod bridge_costs_test; +mod bridge_support_rejected_pegin_test; +mod bridge_support_test; +mod pegin_information_test; +mod repository_btc_block_store_with_cache_test; +mod support; +mod whitelist_storage_provider_impl_test; diff --git a/crates/execution/src/rskj_tests/bridge_pegins/pegin_information_test.rs b/crates/execution/src/rskj_tests/bridge_pegins/pegin_information_test.rs new file mode 100644 index 0000000..a878aa3 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegins/pegin_information_test.rs @@ -0,0 +1,355 @@ +//! rskj's `PeginInformationTest` (`co/rsk/peg/PeginInformationTest.java`): +//! what rskj reads of a peg-in, its version, RSK destination and refund +//! address, from its sender and, from RSKIP170, from its instructions +//! (`PeginInformation.parse`). +//! +//! rustock reads the same inside `registerBtcTransaction`, so each port +//! registers a peg-in and holds what rskj's reading leads to: who is +//! credited, the version its event carries, and, where the locking cap turns +//! it down, where its refund goes. They run on rskj's regtest, as rskj's +//! test does. +//! +//! Read in full. Its other test, `parse_fromBtcLockSender`, is a legacy peg-in +//! credited to its sender, which every other port here and in +//! `bridge_support_test` holds. + +use alloy_primitives::U256; +use bitcoin::{ScriptBuf, Transaction, Witness}; + +use crate::bridge::constants::BridgeConstants; +use crate::hardfork::{RskNetworkUpgrade, RSK_REGTEST_CHAIN_ID}; + +use super::support::*; + +use RskNetworkUpgrade::{Arrowhead600, Fingerroot500, Iris300, Papyrus200}; + +/// The Bitcoin height a peg-in is registered at: below regtest's pegout +/// index. +const HEIGHT: u32 = 100; + +/// A locking cap no peg-in here reaches. +const NO_CAP: u64 = 1_000 * COIN; + +/// A peg-in of one bitcoin to rskj regtest's federation from `input`, with +/// the output `instructions` beside it, if any. +fn pegin(input: (ScriptBuf, Witness), instructions: Option) -> Transaction { + let mut outputs = vec![( + COIN, + genesis_federation_output(&BridgeConstants::rskj_regtest()), + )]; + outputs.extend(instructions.map(|script| (0, script))); + tx(vec![input], &outputs) +} + +/// `tx` registered at RSK block 10 of rskj's regtest ladder up to `last`, +/// at Bitcoin height `height`, with a locking cap of `cap` satoshis: what it +/// answered and left behind, and the Bridge after it. +fn registered_at( + tx: &Transaction, + last: RskNetworkUpgrade, + height: u32, + cap: u64, +) -> (Registered, Ctx) { + let config = BridgeConstants { + initial_locking_cap: cap, + ..BridgeConstants::rskj_regtest() + }; + let cfg = up_to(last, RSK_REGTEST_CHAIN_ID); + let mut ctx = bridge_holding(tx, height, 10, &config, &cfg); + let registered = register(&mut ctx, tx, i64::from(height), &config, &cfg); + (registered, ctx) +} + +/// [`registered_at`] at [`HEIGHT`]. +fn registered(tx: &Transaction, last: RskNetworkUpgrade, cap: u64) -> (Registered, Ctx) { + registered_at(tx, last, HEIGHT, cap) +} + +/// The one refund the Bridge waits to send, and where it pays. +fn refund(ctx: &mut Ctx) -> (Transaction, ScriptBuf) { + let refunds = waiting_pegouts(ctx); + assert_eq!(refunds.len(), 1, "one refund"); + assert_eq!(refunds[0].output.len(), 1, "one output"); + let to = refunds[0].output[0].script_pubkey.clone(); + (refunds[0].clone(), to) +} + +/// rskj `parse_fromPeginInstructions`: from RSKIP170 a peg-in's instructions +/// override what its sender gives, with version 1, their destination and +/// their refund address. Taken, it credits their destination, not the +/// sender's, and logs version 1; turned down by the locking cap, it is +/// refunded to their refund address. +#[test] +fn from_rskip170_the_instructions_give_the_destination_and_the_refund_address() { + let (sender, them) = (public_key(&[0x51]), public_key(&[0x52])); + let destination = rsk_address_of(&them); + let instructions = rskt(1, destination, Some(Refund::P2pkh(hash160(&them)))); + let pegin = pegin(p2pkh_spend(&sender), Some(instructions)); + + let (taken, mut ctx) = registered(&pegin, Iris300, NO_CAP); + assert_eq!( + taken, + Registered { + returned: true, + processed: true, + logs: vec![pegin_btc(destination, &pegin, COIN, 1)], + } + ); + assert_eq!(balance(&mut ctx, destination), wei(COIN)); + assert_eq!(balance(&mut ctx, rsk_address_of(&sender)), U256::ZERO); + + let (refused, mut ctx) = registered(&pegin, Iris300, COIN / 2); + let (refund, to) = refund(&mut ctx); + assert_eq!(to, p2pkh(&hash160(&them))); + assert_eq!( + refused, + Registered { + returned: true, + processed: true, + logs: vec![rejected_pegin(&pegin, 1), release_requested(&refund, COIN)], + } + ); + assert_eq!(balance(&mut ctx, destination), U256::ZERO); +} + +/// rskj `parse_fromPeginInstructions_withoutBtcLockSender`: with no sender +/// rskj can name, the instructions still give version 1, the destination and +/// the refund address. +#[test] +fn with_no_sender_the_instructions_give_the_destination_and_the_refund_address() { + let them = public_key(&[0x52]); + let destination = rsk_address_of(&them); + let instructions = rskt(1, destination, Some(Refund::P2sh(hash160(&them)))); + let pegin = pegin(p2wpkh_spend(&public_key(&[0x51])), Some(instructions)); + + let (taken, mut ctx) = registered(&pegin, Iris300, NO_CAP); + assert_eq!(taken.logs, vec![pegin_btc(destination, &pegin, COIN, 1)]); + assert!(taken.returned && taken.processed); + assert_eq!(balance(&mut ctx, destination), wei(COIN)); + + let (refused, mut ctx) = registered(&pegin, Iris300, COIN / 2); + let (refund, to) = refund(&mut ctx); + assert_eq!(to, p2sh(&hash160(&them))); + assert_eq!( + refused.logs, + vec![rejected_pegin(&pegin, 1), release_requested(&refund, COIN)] + ); + assert!(refused.processed); +} + +/// rskj `parse_fromPeginInstructions_withoutBtcRefundAddress`: instructions +/// without a refund address leave the sender's in place, so a peg-in the +/// locking cap turns down goes back to its sender, while one taken credits +/// the instructions' destination. +#[test] +fn instructions_without_a_refund_address_leave_the_senders() { + let (sender, them) = (public_key(&[0x51]), public_key(&[0x52])); + let destination = rsk_address_of(&them); + let pegin = pegin(p2pkh_spend(&sender), Some(rskt(1, destination, None))); + + let (taken, mut ctx) = registered(&pegin, Iris300, NO_CAP); + assert_eq!(taken.logs, vec![pegin_btc(destination, &pegin, COIN, 1)]); + assert_eq!(balance(&mut ctx, destination), wei(COIN)); + assert_eq!(balance(&mut ctx, rsk_address_of(&sender)), U256::ZERO); + + let (refused, mut ctx) = registered(&pegin, Iris300, COIN / 2); + let (refund, to) = refund(&mut ctx); + assert_eq!(to, p2pkh(&hash160(&sender))); + assert_eq!( + refused.logs, + vec![rejected_pegin(&pegin, 1), release_requested(&refund, COIN)] + ); +} + +/// rskj `parse_fromPeginInstructions_withoutBtcLockSender_withoutBtcRefundAddress`: +/// with no sender and no refund address in the instructions there is none: +/// a peg-in the locking cap turns down can't be refunded, which from RSKIP181 +/// is logged as PEGIN_V1_REFUND_ADDRESS_NOT_SET (2), and it is marked +/// processed. +#[test] +fn with_no_sender_and_no_refund_address_in_the_instructions_there_is_none() { + let them = public_key(&[0x52]); + let destination = rsk_address_of(&them); + let pegin = pegin( + p2wpkh_spend(&public_key(&[0x51])), + Some(rskt(1, destination, None)), + ); + + let (taken, mut ctx) = registered(&pegin, Iris300, NO_CAP); + assert_eq!(taken.logs, vec![pegin_btc(destination, &pegin, COIN, 1)]); + assert_eq!(balance(&mut ctx, destination), wei(COIN)); + + let (refused, mut ctx) = registered(&pegin, Iris300, COIN / 2); + assert_eq!( + refused, + Registered { + returned: true, + processed: true, + logs: vec![rejected_pegin(&pegin, 1), unrefundable_pegin(&pegin, 2)], + } + ); + assert!(waiting_pegouts(&mut ctx).is_empty()); + assert_eq!(balance(&mut ctx, destination), U256::ZERO); +} + +/// rskj `parse_fromPeginInstructions_invalidProtocolVersion`: instructions of +/// a version other than 1 throw (`PeginInformation.parseFromPeginInstructions`, +/// and before it `PeginInstructionsProvider.buildPeginInstructions`). A +/// peg-in of version 0 is then an invalid payload: from RSKIP181 logged as +/// PEGIN_V1_INVALID_PAYLOAD (4), refunded to its sender, whose address was +/// read before the throw, and marked processed, before RSKIP379 and after it. +#[test] +fn instructions_of_a_version_other_than_1_are_an_invalid_payload() { + let (sender, them) = (public_key(&[0x51]), public_key(&[0x52])); + let destination = rsk_address_of(&them); + let pegin = pegin( + p2pkh_spend(&sender), + Some(rskt(0, destination, Some(Refund::P2pkh(hash160(&them))))), + ); + for last in [Iris300, Arrowhead600] { + let (refused, mut ctx) = registered(&pegin, last, NO_CAP); + let (refund, to) = refund(&mut ctx); + assert_eq!(to, p2pkh(&hash160(&sender)), "{last:?}"); + assert_eq!( + refused, + Registered { + returned: true, + processed: true, + logs: vec![rejected_pegin(&pegin, 4), release_requested(&refund, COIN)], + }, + "{last:?}" + ); + assert_eq!(balance(&mut ctx, destination), U256::ZERO); + assert_eq!(balance(&mut ctx, rsk_address_of(&sender)), U256::ZERO); + } +} + +/// rskj `parse_withoutBtcLockSender_withoutPeginInstructions`: before RSKIP379 +/// a peg-in with neither a sender rskj can name nor instructions throws. +/// From RSKIP170 (with RSKIP181) that logs PEGIN_V1_INVALID_PAYLOAD (4), then, +/// with nowhere to refund to and the version still 0, +/// LEGACY_PEGIN_UNDETERMINED_SENDER (1), and marks it processed; before +/// RSKIP170 nothing is kept. +#[test] +fn before_rskip379_a_peg_in_with_neither_a_sender_nor_instructions_is_refused() { + let pegin = pegin(p2wpkh_spend(&public_key(&[0x51])), None); + let (refused, mut ctx) = registered(&pegin, Iris300, NO_CAP); + assert_eq!( + refused, + Registered { + returned: true, + processed: true, + logs: vec![rejected_pegin(&pegin, 4), unrefundable_pegin(&pegin, 1)], + } + ); + assert!(waiting_pegouts(&mut ctx).is_empty()); + + let (ignored, _) = registered(&pegin, Papyrus200, NO_CAP); + assert_eq!( + ignored, + Registered { + returned: true, + processed: false, + logs: vec![], + } + ); +} + +/// rskj `parse_withBtcLockSender_withPeginInstructions_preIris`: before +/// RSKIP170 the instructions aren't read: the peg-in is version 0, credited +/// to its sender's RSK address and, from RSKIP146, logged as `lock_btc` with +/// its sender's address; the instructions' destination gets nothing. +#[test] +fn before_rskip170_the_instructions_are_not_read() { + let (sender, them) = (public_key(&[0x51]), public_key(&[0x52])); + let destination = rsk_address_of(&them); + let instructions = rskt(1, destination, Some(Refund::P2pkh(hash160(&them)))); + let pegin = pegin(p2pkh_spend(&sender), Some(instructions)); + let config = BridgeConstants::rskj_regtest(); + let cfg = up_to(Papyrus200, RSK_REGTEST_CHAIN_ID); + assert!(!cfg.has_rskip170(10) && cfg.has_rskip146(10)); + let mut ctx = bridge_holding(&pegin, HEIGHT, 10, &config, &cfg); + whitelist_once(&mut ctx, hash160(&sender), COIN); + + let taken = register(&mut ctx, &pegin, i64::from(HEIGHT), &config, &cfg); + let to = rsk_address_of(&sender); + assert_eq!( + taken, + Registered { + returned: true, + processed: true, + logs: vec![lock_btc(to, &pegin, &base58(111, &hash160(&sender)), COIN)], + } + ); + assert_eq!(balance(&mut ctx, to), wei(COIN)); + assert_eq!(balance(&mut ctx, destination), U256::ZERO); +} + +/// rskj `parse_withoutBtcLockSender_withPeginInstructions_preIris`: before +/// RSKIP170 a peg-in from no sender rskj can name throws, its instructions +/// unread: nothing of it is kept, and it isn't marked processed. +#[test] +fn before_rskip170_instructions_do_not_stand_in_for_a_sender() { + let them = public_key(&[0x52]); + let destination = rsk_address_of(&them); + let instructions = rskt(1, destination, Some(Refund::P2pkh(hash160(&them)))); + let pegin = pegin(p2wpkh_spend(&public_key(&[0x51])), Some(instructions)); + + let (ignored, mut ctx) = registered(&pegin, Papyrus200, NO_CAP); + assert_eq!( + ignored, + Registered { + returned: true, + processed: false, + logs: vec![], + } + ); + assert_eq!(balance(&mut ctx, destination), U256::ZERO); +} + +/// rskj `parse_bech32_withPeginInstructions`, a testnet peg-in from a native +/// segwit (bech32) sender with no instructions: before RSKIP379 its reading +/// throws, an invalid payload with no one to refund (4, then 1), marked +/// processed; from RSKIP379 it doesn't, and the peg-in is a legacy one from +/// an undetermined sender (3, then 1), not marked at arrowhead600, below the +/// pegout index's height and at it. rskj's transaction pays testnet's +/// federation of the day; this one is as it is, paying regtest's. +#[test] +fn a_peg_in_from_a_bech32_sender_is_read_as_rskj_reads_it_on_each_side_of_rskip379() { + let config = BridgeConstants::rskj_regtest(); + let sender = public_key(&[0x53]); + let mut pegin = tx( + vec![p2wpkh_spend(&sender)], + &[ + (550_000, genesis_federation_output(&config)), + (97_803_500, p2wpkh(&hash160(&sender))), + ], + ); + pegin.version = bitcoin::transaction::Version::TWO; + + let (refused, _) = registered(&pegin, Fingerroot500, NO_CAP); + assert_eq!( + refused, + Registered { + returned: true, + processed: true, + logs: vec![rejected_pegin(&pegin, 4), unrefundable_pegin(&pegin, 1)], + } + ); + + let index = config.btc_height_when_pegout_tx_index_activates + + config.pegout_tx_index_grace_period_in_btc_blocks; + for height in [HEIGHT, index] { + let (refused, mut ctx) = registered_at(&pegin, Arrowhead600, height, NO_CAP); + assert_eq!( + refused, + Registered { + returned: true, + processed: false, + logs: vec![rejected_pegin(&pegin, 3), unrefundable_pegin(&pegin, 1)], + }, + "at {height}" + ); + assert_eq!(balance(&mut ctx, rsk_address_of(&sender)), U256::ZERO); + } +} diff --git a/crates/execution/src/rskj_tests/bridge_pegins/repository_btc_block_store_with_cache_test.rs b/crates/execution/src/rskj_tests/bridge_pegins/repository_btc_block_store_with_cache_test.rs new file mode 100644 index 0000000..b0d40ef --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegins/repository_btc_block_store_with_cache_test.rs @@ -0,0 +1,65 @@ +//! rskj's `RepositoryBtcBlockStoreWithCacheTest` +//! (`co/rsk/peg/RepositoryBtcBlockStoreWithCacheTest.java`): the Bridge's +//! Bitcoin block store, its listed tests of how deep below the head a block +//! is looked for from RSKIP199 (`getStoredBlockAtMainChainHeight`), on rskj's +//! regtest, where the block index activates at Bitcoin height 10 and blocks +//! below it are looked for at most 5 below the head. +//! +//! Read in full. Its first listed test, the limit at the index's height, is +//! held by `bridge/btc_chain.rs`'s +//! `below_the_index_activation_rskj_looks_only_so_deep` on mainnet and +//! testnet. Its other tests are of rskj's cache of the store, which +//! `bridge/btc_block_cache_readpath_tests.rs` holds to the store's answers, +//! and of lookups above the head and through the index, which +//! `bridge/btc_chain.rs`'s tests hold. + +use alloy_primitives::U256; +use bitcoin::hashes::Hash as _; + +use crate::bridge::btc_chain::{main_chain_block_at_height, MainChainLookup}; +use crate::bridge::btc_store::{self, StoredBlock}; +use crate::bridge::constants::BridgeConstants; + +use super::support::ctx_at; + +/// rskj `getStoredBlockAtMainChainHeight_postIris_heightLowerThanMaxDepth_limitInChainHeadMinusMaxDepthToSearch`: +/// with the head less than the search depth above the index's height (here +/// at 14, one below 10 + 5), the deepest block looked for is the search depth +/// below the head, 9: below it the lookup throws, and from it the lookup goes +/// on, here to a block the store doesn't hold. +#[test] +fn from_rskip199_below_the_search_depth_under_the_head_the_lookup_throws() { + let config = BridgeConstants::rskj_regtest(); + let (index, depth) = ( + config.btc_height_when_block_index_activates, + config.max_depth_to_search_blocks_below_index_activation, + ); + assert_eq!((index, depth), (10, 5)); + let head_height = index + depth - 1; + let genesis = bitcoin::constants::genesis_block(bitcoin::Network::Regtest).header; + let header = bitcoin::block::Header { + prev_blockhash: genesis.block_hash(), + merkle_root: bitcoin::TxMerkleNode::all_zeros(), + nonce: 1, + ..genesis + }; + let mut ctx = ctx_at(1); + let head = StoredBlock::new(header, head_height, U256::ZERO); + btc_store::put_stored_block(&mut ctx, &head, true); + btc_store::store_chain_head(&mut ctx, &head, true); + + let limit = head_height - depth; + assert!(matches!( + main_chain_block_at_height(&mut ctx, limit - 1, true, Some(&config)), + MainChainLookup::StoreError + )); + assert!(matches!( + main_chain_block_at_height(&mut ctx, limit, true, Some(&config)), + MainChainLookup::Absent + )); + // Before RSKIP199 nothing limits the search. + assert!(matches!( + main_chain_block_at_height(&mut ctx, limit - 1, false, Some(&config)), + MainChainLookup::Absent + )); +} diff --git a/crates/execution/src/rskj_tests/bridge_pegins/support.rs b/crates/execution/src/rskj_tests/bridge_pegins/support.rs new file mode 100644 index 0000000..4c2e6c1 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegins/support.rs @@ -0,0 +1,866 @@ +//! What this folder's ports share: a Bridge at an RSK block of an upgrade +//! ladder, a Bitcoin chain that holds a transaction, the transactions and +//! federations a peg-in is made of, and what a registration leaves behind. +//! +//! rskj's tests mock the Bitcoin block store +//! (`BridgeSupportTestUtil.mockChainOfStoredBlocks`). Here the blocks are +//! written into the Bridge's own store, as it keeps them ([`hold`]), so a +//! peg-in is registered at any Bitcoin height under any network's constants, +//! through `registerBtcTransaction` as a transaction calls it. What rskj +//! computes and rustock is held to (a script, an address, an event) is +//! computed here from rskj's definition, not by rustock's own code. + +use alloy_primitives::{Address as RskAddress, B256, U256}; +use bitcoin::hashes::Hash as _; +use revm::context_interface::journaled_state::account::JournaledAccountTr; +use revm::context_interface::{ContextTr, JournalTr}; +use sha2::Digest as _; + +use crate::abi::encode::{self, Arg}; +use crate::bridge::btc_store::{self, StoredBlock}; +use crate::bridge::constants::BridgeConstants; +use crate::bridge::federation::{serialize_federation_multikey, StoredMember}; +use crate::bridge::{btc_chain, peg, serialization, storage, tx}; +use crate::hardfork::{RskHardforkConfig, RskNetworkUpgrade}; +use crate::precompiles::{BridgeTxContext, BRIDGE_ADDR}; + +/// One bitcoin, in satoshis (bitcoinj's `Coin.COIN`). +pub(super) const COIN: u64 = 100_000_000; + +/// What one satoshi is worth in RBTC, in wei. +pub(super) const WEI_PER_SATOSHI: u64 = 10_000_000_000; + +/// All the RBTC there is, in satoshis (rskj's `BridgeConstants.maxRbtc`). +pub(super) const MAX_RBTC: u64 = 21_000_000 * COIN; + +/// The hash of the RSK transaction that calls the Bridge in these tests. +pub(super) const RSK_TX_HASH: [u8; 32] = [0x7e; 32]; + +/// The world a ported test calls the Bridge in: no trie and no database, only +/// what the test and the Bridge write. +pub(super) type Ctx = revm::Context< + revm::context::BlockEnv, + revm::context::TxEnv, + revm::context::CfgEnv, + revm::database::EmptyDB, + revm::Journal, + crate::raw_storage::RskChainExt, +>; + +/// The upgrades after genesis, in order. +const LADDER: [RskNetworkUpgrade; 13] = [ + RskNetworkUpgrade::Orchid, + RskNetworkUpgrade::Wasabi100, + RskNetworkUpgrade::TwoToThree, + RskNetworkUpgrade::Papyrus200, + RskNetworkUpgrade::Iris300, + RskNetworkUpgrade::Hop400, + RskNetworkUpgrade::Fingerroot500, + RskNetworkUpgrade::Arrowhead600, + RskNetworkUpgrade::Arrowhead631, + RskNetworkUpgrade::Lovell700, + RskNetworkUpgrade::Reed800, + RskNetworkUpgrade::Reed810, + RskNetworkUpgrade::Vetiver900, +]; + +/// rskj's `ActivationConfigsForTest.()`: every upgrade up to `last` +/// active from block 0 and none after it, on the network `chain_id` names. +/// `Genesis` has nothing active, and `Vetiver900` is rskj's `all()`. +pub(super) fn up_to(last: RskNetworkUpgrade, chain_id: u64) -> RskHardforkConfig { + LADDER + .into_iter() + .fold(RskHardforkConfig::all_active(chain_id), |cfg, upgrade| { + cfg.with_hardfork(upgrade, (upgrade <= last).then_some(0)) + }) +} + +/// A Bridge with nothing stored, at RSK block `number`. +pub(super) fn ctx_at(number: u64) -> Ctx { + use revm::MainContext; + let mut ctx: Ctx = + revm::Context::mainnet().with_chain(crate::raw_storage::RskChainExt::default()); + ctx.block.number = U256::from(number); + ctx.block.timestamp = U256::from(1_700_000_000u64); + ctx +} + +/// The RSK block a context is at. +pub(super) fn block_of(ctx: &Ctx) -> u64 { + ctx.block.number.to::() +} + +/// rskj's `mockChainOfStoredBlocks`: the Bridge's Bitcoin store holds a block +/// at `height` whose merkle root is the hash of `raw`, so that a tree of `raw` +/// alone proves it there, and a block at each height above it up to `head`, +/// each on the one below, the last the store's head. Each is written as the +/// store keeps it at the context's block, and from RSKIP199 indexed by its +/// height. +pub(super) fn hold(ctx: &mut Ctx, raw: &[u8], height: u32, head: u32, cfg: &RskHardforkConfig) { + hold_with_root(ctx, tx::calculate_btc_tx_hash(raw), height, head, cfg); +} + +/// [`hold`], the block at `height` having `root` for its merkle root. +pub(super) fn hold_with_root( + ctx: &mut Ctx, + root: [u8; 32], + height: u32, + head: u32, + cfg: &RskHardforkConfig, +) -> bitcoin::block::Header { + hold_roots(ctx, &[root], height, head, cfg)[0] +} + +/// [`hold`] of a block for each of `roots`, one on the other from `height`, +/// each having its root for its merkle root: their headers. +pub(super) fn hold_roots( + ctx: &mut Ctx, + roots: &[[u8; 32]], + height: u32, + head: u32, + cfg: &RskHardforkConfig, +) -> Vec { + let number = block_of(ctx); + let (v2, rskip199) = (cfg.has_stored_block_v2(number), cfg.has_rskip199(number)); + let mut prev = bitcoin::BlockHash::from_byte_array([0x42; 32]); + let mut held = Vec::new(); + for at in height..=head { + let root = roots.get((at - height) as usize); + let header = bitcoin::block::Header { + version: bitcoin::block::Version::from_consensus(1), + prev_blockhash: prev, + merkle_root: bitcoin::TxMerkleNode::from_byte_array( + root.copied().unwrap_or([0x11; 32]), + ), + time: 1_700_000_000, + bits: bitcoin::CompactTarget::from_consensus(0x1d00_ffff), + nonce: at, + }; + let block = StoredBlock::new(header, at, U256::from(at)); + btc_store::put_stored_block(ctx, &block, v2); + if rskip199 { + storage::bridge_store_btc_block_hash_by_height( + ctx, + at, + btc_chain::bitcoin_hash_to_b256(&header.block_hash()), + ); + } + if at == head { + btc_store::store_chain_head(ctx, &block, v2); + } + if root.is_some() { + held.push(header); + } + prev = header.block_hash(); + } + assert_eq!( + held.len(), + roots.len(), + "a block for each root, at or below the head" + ); + held +} + +/// A partial merkle tree of one transaction, `raw`, matched: its root is the +/// transaction's hash. +pub(super) fn one_leaf_tree(raw: &[u8]) -> Vec { + let mut tree = 1u32.to_le_bytes().to_vec(); + tree.push(1); + tree.extend_from_slice(&tx::calculate_btc_tx_hash(raw)); + tree.extend_from_slice(&[1, 0x01]); + tree +} + +/// An event as the Bridge logs it: its topics, then its data. +pub(super) type Event = (Vec, Vec); + +/// What `registerBtcTransaction` answered and left behind. +#[derive(Debug, PartialEq)] +pub(super) struct Registered { + /// The call returned, rather than threw. + pub(super) returned: bool, + /// The transaction is marked processed. + pub(super) processed: bool, + /// The events it logged. + pub(super) logs: Vec, +} + +/// `registerBtcTransaction` of `tx` at Bitcoin height `height`, in the +/// transaction [`RSK_TX_HASH`]. Its writes are kept when it returns and +/// dropped when it throws, as the executor keeps and drops them; what was +/// written before it is the state it starts from, and stays. +pub(super) fn register( + ctx: &mut Ctx, + tx: &bitcoin::Transaction, + height: i64, + config: &BridgeConstants, + cfg: &RskHardforkConfig, +) -> Registered { + register_bytes( + ctx, + &bitcoin::consensus::serialize(tx), + tx, + height, + config, + cfg, + ) +} + +/// [`register`], with `tx` given to the Bridge as the bytes `raw`. +pub(super) fn register_bytes( + ctx: &mut Ctx, + raw: &[u8], + tx: &bitcoin::Transaction, + height: i64, + config: &BridgeConstants, + cfg: &RskHardforkConfig, +) -> Registered { + let tree = one_leaf_tree(raw); + let returned = register_with_tree(ctx, raw, height, &tree, config, cfg).is_ok(); + let logs = take_logs(ctx); + let processed = tx::is_btc_tx_processed(ctx, &tx::legacy_btc_txid(tx)); + Registered { + returned, + processed, + logs, + } +} + +/// `registerBtcTransaction` of the bytes `raw` at `height` with the tree +/// `tree`, as [`register`] calls it: what it answered. +pub(super) fn register_with_tree( + ctx: &mut Ctx, + raw: &[u8], + height: i64, + tree: &[u8], + config: &BridgeConstants, + cfg: &RskHardforkConfig, +) -> Result { + let args = encode::args(&[ + Arg::Bytes(raw), + Arg::Word(encode::int(height)), + Arg::Bytes(tree), + ]); + in_a_call(ctx, |ctx| { + peg::register_btc_transaction(ctx, &args, 0, config, cfg, &tx_context()) + }) +} + +/// The transaction [`RSK_TX_HASH`], as the Bridge sees the call it makes. +pub(super) fn tx_context() -> BridgeTxContext { + BridgeTxContext { + rsk_tx_hash: RSK_TX_HASH, + origin_tx_hash: RSK_TX_HASH, + ..BridgeTxContext::default() + } +} + +/// `call`, run as a transaction runs a Bridge method: what it writes is kept +/// when it returns and dropped when it throws, as the executor keeps and +/// drops them. +pub(super) fn in_a_call( + ctx: &mut Ctx, + call: impl FnOnce(&mut Ctx) -> Result, +) -> Result { + ctx.chain_mut().raw_storage.commit_call(); + let checkpoint = ctx.journal_mut().checkpoint(); + let result = call(ctx); + if result.is_ok() { + ctx.chain_mut().raw_storage.commit_call(); + ctx.journal_mut().checkpoint_commit(); + } else { + ctx.chain_mut().raw_storage.discard_call(); + ctx.journal_mut().checkpoint_revert(checkpoint); + } + result +} + +/// The events logged since the last time they were taken. +pub(super) fn take_logs(ctx: &mut Ctx) -> Vec { + ctx.journal_mut() + .take_logs() + .into_iter() + .map(|log| (log.data.topics().to_vec(), log.data.data.to_vec())) + .collect() +} + +/// A Bridge at RSK block `number` whose Bitcoin store holds `tx` at `height` +/// with as many confirmations as `config` asks of a peg-in, and which holds +/// all the RBTC there is: the federations hold nothing yet, so no peg-in here +/// meets the locking cap unless a test sets it to. +pub(super) fn bridge_holding( + tx: &bitcoin::Transaction, + height: u32, + number: u64, + config: &BridgeConstants, + cfg: &RskHardforkConfig, +) -> Ctx { + let mut ctx = ctx_at(number); + let raw = bitcoin::consensus::serialize(tx); + let head = height + config.btc2rsk_minimum_acceptable_confirmations - 1; + hold(&mut ctx, &raw, height, head, cfg); + fund_bridge_but(&mut ctx, 0); + ctx +} + +/// The peg-outs waiting for confirmations: a refund a registration made is +/// one of them. +pub(super) fn waiting_pegouts(ctx: &mut Ctx) -> Vec { + peg::load_pegout_confirmation_set(ctx, true) + .into_iter() + .map(|entry| bitcoin::consensus::deserialize(&entry.btc_tx_raw).expect("a transaction")) + .collect() +} + +/// What `address` holds, in wei. +pub(super) fn balance(ctx: &mut Ctx, address: RskAddress) -> U256 { + *ctx.journal_mut() + .load_account_mut(address) + .expect("the account") + .balance() +} + +/// Gives the Bridge all the RBTC there is but `locked` satoshis, which the +/// federations hold for it: rskj's locking cap counts what the Bridge has +/// paid out, all the RBTC there is less what it holds. +pub(super) fn fund_bridge_but(ctx: &mut Ctx, locked: u64) { + let wei = U256::from(MAX_RBTC - locked) * U256::from(WEI_PER_SATOSHI); + ctx.journal_mut() + .load_account_mut(BRIDGE_ADDR) + .expect("the Bridge's account") + .set_balance(wei); +} + +/// `satoshis` in wei. +pub(super) fn wei(satoshis: u64) -> U256 { + U256::from(satoshis) * U256::from(WEI_PER_SATOSHI) +} + +/// A thousandth of a bitcoin (bitcoinj's `Coin.MILLICOIN`): the fee per +/// kilobyte rskj's tests have the Bridge pay. +pub(super) const MILLICOIN: u64 = 100_000; + +/// Sets the fee per kilobyte the Bridge pays its Bitcoin transactions with. +pub(super) fn set_fee_per_kb(ctx: &mut Ctx, satoshis: u64) { + storage::bridge_store_u256(ctx, storage::FEE_PER_KB_KEY, U256::from(satoshis)); +} + +// --------------------------------------------------------------------------- +// Keys, hashes and scripts +// --------------------------------------------------------------------------- + +/// The compressed public key of the private key `private`, read as rskj's +/// `BtcECKey.fromPrivate` reads it: a big-endian number. +pub(super) fn public_key(private: &[u8]) -> [u8; 33] { + use k256::elliptic_curve::sec1::ToEncodedPoint; + let mut scalar = [0u8; 32]; + scalar[32 - private.len()..].copy_from_slice(private); + let key = k256::SecretKey::from_slice(&scalar).expect("a private key in range"); + key.public_key() + .to_encoded_point(true) + .as_bytes() + .try_into() + .expect("33 bytes") +} + +/// rskj's `BitcoinTestUtils.getBtcEcKeyFromSeed`: the key whose private key +/// is the seed's Keccak-256 hash. +pub(super) fn key_from_seed(seed: &str) -> [u8; 33] { + use sha3::Digest as _; + public_key(&sha3::Keccak256::digest(seed.as_bytes())) +} + +/// rskj's `ECKey.getAddress`: the last 20 bytes of the Keccak-256 hash of a +/// key's uncompressed point. +pub(super) fn rsk_address_of(key: &[u8; 33]) -> RskAddress { + use k256::elliptic_curve::sec1::ToEncodedPoint; + use sha3::Digest as _; + let point = k256::PublicKey::from_sec1_bytes(key).expect("a public key"); + let uncompressed = point.to_encoded_point(false); + RskAddress::from_slice(&sha3::Keccak256::digest(&uncompressed.as_bytes()[1..])[12..]) +} + +/// RIPEMD-160 of SHA-256, Bitcoin's hash of a key or a script. +pub(super) fn hash160(data: &[u8]) -> [u8; 20] { + use ripemd::Digest as _; + ripemd::Ripemd160::digest(sha2::Sha256::digest(data)).into() +} + +/// `OP_DUP OP_HASH160 OP_EQUALVERIFY OP_CHECKSIG`. +pub(super) fn p2pkh(hash: &[u8; 20]) -> bitcoin::ScriptBuf { + let mut script = vec![0x76, 0xa9, 20]; + script.extend_from_slice(hash); + script.extend_from_slice(&[0x88, 0xac]); + bitcoin::ScriptBuf::from_bytes(script) +} + +/// `OP_HASH160 OP_EQUAL`. +pub(super) fn p2sh(hash: &[u8; 20]) -> bitcoin::ScriptBuf { + let mut script = vec![0xa9, 20]; + script.extend_from_slice(hash); + script.push(0x87); + bitcoin::ScriptBuf::from_bytes(script) +} + +/// `OP_0 `: a native segwit (bech32) output, P2WPKH. +pub(super) fn p2wpkh(hash: &[u8; 20]) -> bitcoin::ScriptBuf { + let mut script = vec![0x00, 20]; + script.extend_from_slice(hash); + bitcoin::ScriptBuf::from_bytes(script) +} + +/// The P2SH output of a redeem script. +pub(super) fn p2sh_of(redeem: &[u8]) -> bitcoin::ScriptBuf { + p2sh(&hash160(redeem)) +} + +/// The witness program of a redeem script, `OP_0 `. +pub(super) fn witness_program(redeem: &[u8]) -> Vec { + let mut program = vec![0x00, 32]; + program.extend_from_slice(&sha2::Sha256::digest(redeem)); + program +} + +/// The P2SH-P2WSH output of a redeem script: the P2SH of its witness +/// program. +pub(super) fn p2sh_p2wsh_of(redeem: &[u8]) -> bitcoin::ScriptBuf { + p2sh(&hash160(&witness_program(redeem))) +} + +/// bitcoinj-thin's `ScriptBuilder.createRedeemScript(threshold, keys)`: the +/// keys sorted, and the counts as `ScriptBuilder.number` writes them, OP_1 to +/// OP_15 below 16 and a one-byte push from 16 on. +pub(super) fn multisig(keys: &[[u8; 33]], threshold: usize) -> Vec { + let number = |n: usize| { + if n < 16 { + vec![0x50 + n as u8] + } else { + vec![1, n as u8] + } + }; + let mut sorted = keys.to_vec(); + sorted.sort(); + let mut script = number(threshold); + for key in &sorted { + script.push(33); + script.extend_from_slice(key); + } + script.extend(number(sorted.len())); + script.push(0xae); + script +} + +/// A standard multisig federation's redeem script: `size / 2 + 1` of its +/// members. +pub(super) fn standard_redeem(keys: &[[u8; 33]]) -> Vec { + multisig(keys, keys.len() / 2 + 1) +} + +/// rskj's `P2shErpRedeemScriptBuilder`, which a P2SH-P2WSH ERP federation's +/// script is built by too: `OP_NOTIF OP_ELSE +/// OP_CHECKSEQUENCEVERIFY OP_DROP OP_ENDIF`, the delay as +/// bitcoinj-thin's `Utils.signedLongToByteArrayLE` writes it. +pub(super) fn erp_redeem(members: &[[u8; 33]], emergency: &[[u8; 33]], delay: i64) -> Vec { + // `BigInteger.valueOf(delay).toByteArray()`: big-endian, as few bytes as + // hold it with a sign bit, then reversed. + let mut csv: Vec = delay + .to_be_bytes() + .into_iter() + .skip_while(|byte| *byte == 0) + .collect(); + if csv.first().is_none_or(|byte| byte & 0x80 != 0) { + csv.insert(0, 0); + } + csv.reverse(); + let mut script = vec![0x64]; + script.extend(standard_redeem(members)); + script.push(0x67); + script.push(csv.len() as u8); + script.extend(csv); + script.extend_from_slice(&[0xb2, 0x75]); + script.extend(standard_redeem(emergency)); + script.push(0x68); + script +} + +/// The keys of a network's Bridge constants, written in hex. +pub(super) fn keys_of(hexes: &[&str]) -> Vec<[u8; 33]> { + hexes + .iter() + .map(|key| { + hex::decode(key) + .expect("a key in hex") + .try_into() + .expect("33 bytes") + }) + .collect() +} + +/// The output a network's genesis federation is paid at, built as rskj's +/// `FederationSupportImpl.getGenesisFederation` builds it: a standard multisig +/// on mainnet and testnet, a P2SH-P2WSH ERP federation on rskj's regtest. +pub(super) fn genesis_federation_output(config: &BridgeConstants) -> bitcoin::ScriptBuf { + let members = keys_of(config.genesis_federation_public_keys); + match config.genesis_federation_format { + 4000 => p2sh_p2wsh_of(&erp_redeem( + &members, + &keys_of(config.erp_fed_pubkeys), + config.erp_fed_activation_delay, + )), + _ => p2sh_of(&standard_redeem(&members)), + } +} + +/// Where the federation that is, or is to be, active is stored. +pub(super) const NEW_FEDERATION: (&str, &str) = ( + storage::NEW_FEDERATION_KEY, + storage::NEW_FEDERATION_FORMAT_VERSION_KEY, +); + +/// Where the federation before it is stored. +pub(super) const OLD_FEDERATION: (&str, &str) = ( + storage::OLD_FEDERATION_KEY, + storage::OLD_FEDERATION_FORMAT_VERSION_KEY, +); + +/// Stores a federation of `keys`, one key each, in rskj's multikey format, +/// created at RSK block `creation_block`, with its format version. +pub(super) fn store_federation( + ctx: &mut Ctx, + (key_name, format_key): (&str, &str), + keys: &[[u8; 33]], + creation_block: u64, + format: u64, +) { + let members: Vec = keys.iter().copied().map(StoredMember::from_btc).collect(); + storage::bridge_store_bytes_named( + ctx, + key_name, + &serialize_federation_multikey(&members, 0, creation_block), + ); + storage::bridge_store_bytes_named(ctx, format_key, &serialization::rlp_encode_u64(format)); +} + +// --------------------------------------------------------------------------- +// Transactions +// --------------------------------------------------------------------------- + +/// A signature's worth of bytes: no node checks a peg-in's signatures. +const SIGNATURE: [u8; 71] = [0x30; 71]; + +/// The scriptSig of a P2PKH spend of `key`'s output: a signature, then the +/// key. +pub(super) fn p2pkh_spend(key: &[u8; 33]) -> (bitcoin::ScriptBuf, bitcoin::Witness) { + let mut script_sig = vec![SIGNATURE.len() as u8]; + script_sig.extend_from_slice(&SIGNATURE); + script_sig.push(33); + script_sig.extend_from_slice(key); + ( + bitcoin::ScriptBuf::from_bytes(script_sig), + bitcoin::Witness::new(), + ) +} + +/// A P2SH-P2WPKH spend of `key`'s output: the scriptSig pushes the witness +/// program `OP_0 `, and the witness holds a signature and the +/// key. +pub(super) fn p2sh_p2wpkh_spend(key: &[u8; 33]) -> (bitcoin::ScriptBuf, bitcoin::Witness) { + let mut program = vec![0x00, 20]; + program.extend_from_slice(&hash160(key)); + let mut script_sig = vec![program.len() as u8]; + script_sig.extend(program); + let mut witness = bitcoin::Witness::new(); + witness.push(SIGNATURE); + witness.push(key); + (bitcoin::ScriptBuf::from_bytes(script_sig), witness) +} + +/// A P2SH spend of a multisig redeem script: `OP_0`, `signatures` +/// signatures, then the redeem script. +pub(super) fn p2sh_multisig_spend( + redeem: &[u8], + signatures: usize, +) -> (bitcoin::ScriptBuf, bitcoin::Witness) { + let mut script_sig = vec![0x00]; + for _ in 0..signatures { + script_sig.push(SIGNATURE.len() as u8); + script_sig.extend_from_slice(&SIGNATURE); + } + script_sig.extend(push(redeem)); + ( + bitcoin::ScriptBuf::from_bytes(script_sig), + bitcoin::Witness::new(), + ) +} + +/// A P2SH-P2WSH spend of a multisig redeem script: the scriptSig pushes the +/// witness program, and the witness holds an empty item, `signatures` +/// signatures and the redeem script. +pub(super) fn p2sh_p2wsh_spend( + redeem: &[u8], + signatures: usize, +) -> (bitcoin::ScriptBuf, bitcoin::Witness) { + let program = witness_program(redeem); + let mut script_sig = vec![program.len() as u8]; + script_sig.extend(program); + let mut witness = bitcoin::Witness::new(); + witness.push([0u8; 0]); + for _ in 0..signatures { + witness.push(SIGNATURE); + } + witness.push(redeem); + (bitcoin::ScriptBuf::from_bytes(script_sig), witness) +} + +/// An input with nothing in its scriptSig and no witness, from which rskj +/// names no sender (rskj's tests' `new Script(new byte[]{})`). +pub(super) fn no_sender() -> (bitcoin::ScriptBuf, bitcoin::Witness) { + (bitcoin::ScriptBuf::new(), bitcoin::Witness::new()) +} + +/// A native P2WPKH spend of `key`'s output: nothing in the scriptSig, a +/// signature and the key in the witness. rskj names no sender for it. +pub(super) fn p2wpkh_spend(key: &[u8; 33]) -> (bitcoin::ScriptBuf, bitcoin::Witness) { + let mut witness = bitcoin::Witness::new(); + witness.push(SIGNATURE); + witness.push(key); + (bitcoin::ScriptBuf::new(), witness) +} + +/// A push of `data`, as bitcoinj-thin's `ScriptBuilder.data` writes one. +pub(super) fn push(data: &[u8]) -> Vec { + let mut out = match data.len() { + len @ 0..=75 => vec![len as u8], + len @ 76..=255 => vec![0x4c, len as u8], + len => { + let mut prefix = vec![0x4d]; + prefix.extend_from_slice(&(len as u16).to_le_bytes()); + prefix + } + }; + out.extend_from_slice(data); + out +} + +/// A version 1 transaction (bitcoinj's default) with `inputs`, each spending +/// an output of its own, paying `outputs`. +pub(super) fn tx( + inputs: Vec<(bitcoin::ScriptBuf, bitcoin::Witness)>, + outputs: &[(u64, bitcoin::ScriptBuf)], +) -> bitcoin::Transaction { + bitcoin::Transaction { + version: bitcoin::transaction::Version::ONE, + lock_time: bitcoin::absolute::LockTime::ZERO, + input: inputs + .into_iter() + .enumerate() + .map(|(index, (script_sig, witness))| bitcoin::TxIn { + previous_output: bitcoin::OutPoint { + txid: bitcoin::Txid::from_byte_array([0x5a; 32]), + vout: index as u32, + }, + 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 `OP_RETURN` output's script, pushing `data`. +pub(super) fn op_return(data: &[u8]) -> bitcoin::ScriptBuf { + let mut script = vec![0x6a]; + script.extend(push(data)); + bitcoin::ScriptBuf::from_bytes(script) +} + +/// Where a peg-in's instructions ask a refund to go. +pub(super) enum Refund { + P2pkh([u8; 20]), + P2sh([u8; 20]), +} + +/// rskj's `PegTestUtils.createOpReturnScriptForRsk`: `RSKT`, the version, +/// the RSK destination, and where a refund goes, its type (1 for P2PKH, 2 for +/// P2SH) then its hash. +pub(super) fn rskt( + version: u8, + destination: RskAddress, + refund: Option, +) -> bitcoin::ScriptBuf { + let mut payload = b"RSKT".to_vec(); + payload.push(version); + payload.extend_from_slice(destination.as_slice()); + match refund { + Some(Refund::P2pkh(hash)) => { + payload.push(1); + payload.extend_from_slice(&hash); + } + Some(Refund::P2sh(hash)) => { + payload.push(2); + payload.extend_from_slice(&hash); + } + None => {} + } + op_return(&payload) +} + +/// Whitelists `hash` once for up to `max` satoshis, as rskj's +/// `OneOffWhiteListEntry`, with the whitelist on for good. +pub(super) fn whitelist_once(ctx: &mut Ctx, hash: [u8; 20], max: u64) { + let (mut entries, disable_height) = storage::load_one_off_whitelist(ctx); + entries.push((hash, max)); + storage::store_one_off_whitelist(ctx, &entries, disable_height); +} + +/// Whether `hash` is on the one-off whitelist. +pub(super) fn whitelisted_once(ctx: &mut Ctx, hash: &[u8; 20]) -> bool { + storage::load_one_off_whitelist(ctx) + .0 + .iter() + .any(|(entry, _)| entry == hash) +} + +// --------------------------------------------------------------------------- +// Events, as rskj's `BridgeEvents` defines them +// --------------------------------------------------------------------------- + +/// The first topic of the event `signature`. +pub(super) fn topic(signature: &str) -> B256 { + alloy_primitives::keccak256(signature.as_bytes()) +} + +/// How many of `logs` are the event `signature`. +pub(super) fn count_of(logs: &[Event], signature: &str) -> usize { + logs.iter() + .filter(|(topics, _)| topics.first() == Some(&topic(signature))) + .count() +} + +fn word(n: u64) -> [u8; 32] { + U256::from(n).to_be_bytes() +} + +fn address_word(address: RskAddress) -> B256 { + let mut word = [0u8; 32]; + word[12..].copy_from_slice(address.as_slice()); + B256::from(word) +} + +/// `btcTx.getHash().getBytes()`: the transaction's id, without its witness, +/// in the order it is displayed. +pub(super) fn txid_bytes(tx: &bitcoin::Transaction) -> [u8; 32] { + let mut bytes = tx.compute_txid().to_byte_array(); + bytes.reverse(); + bytes +} + +/// `rejected_pegin(bytes32 indexed btcTxHash, int256 reason)`. rskj's +/// `RejectedPeginReason`: PEGIN_CAP_SURPASSED 1, LEGACY_PEGIN_MULTISIG_SENDER +/// 2, LEGACY_PEGIN_UNDETERMINED_SENDER 3, PEGIN_V1_INVALID_PAYLOAD 4, +/// INVALID_AMOUNT 5. +pub(super) fn rejected_pegin(tx: &bitcoin::Transaction, reason: u64) -> Event { + ( + vec![ + topic("rejected_pegin(bytes32,int256)"), + B256::from(txid_bytes(tx)), + ], + word(reason).to_vec(), + ) +} + +/// `unrefundable_pegin(bytes32 indexed btcTxHash, int256 reason)`. rskj's +/// `NonRefundablePeginReason`: LEGACY_PEGIN_UNDETERMINED_SENDER 1, +/// PEGIN_V1_REFUND_ADDRESS_NOT_SET 2, INVALID_AMOUNT 3, +/// OUTPUTS_SENT_TO_DIFFERENT_TYPES_OF_FEDS 4. +pub(super) fn unrefundable_pegin(tx: &bitcoin::Transaction, reason: u64) -> Event { + ( + vec![ + topic("unrefundable_pegin(bytes32,int256)"), + B256::from(txid_bytes(tx)), + ], + word(reason).to_vec(), + ) +} + +/// `pegin_btc(address indexed receiver, bytes32 indexed btcTxHash, int256 +/// amount, int256 protocolVersion)`, the amount in satoshis. +pub(super) fn pegin_btc( + receiver: RskAddress, + tx: &bitcoin::Transaction, + amount: u64, + version: u64, +) -> Event { + let mut data = word(amount).to_vec(); + data.extend_from_slice(&word(version)); + ( + vec![ + topic("pegin_btc(address,bytes32,int256,int256)"), + address_word(receiver), + B256::from(txid_bytes(tx)), + ], + data, + ) +} + +/// `lock_btc(address indexed receiver, bytes32 btcTxHash, string +/// senderBtcAddress, int256 amount)`, the amount in satoshis. +pub(super) fn lock_btc( + receiver: RskAddress, + tx: &bitcoin::Transaction, + sender: &str, + amount: u64, +) -> Event { + let mut data = txid_bytes(tx).to_vec(); + data.extend_from_slice(&word(96)); + data.extend_from_slice(&word(amount)); + data.extend_from_slice(&word(sender.len() as u64)); + data.extend_from_slice(sender.as_bytes()); + data.resize(data.len().div_ceil(32) * 32, 0); + ( + vec![ + topic("lock_btc(address,bytes32,string,int256)"), + address_word(receiver), + ], + data, + ) +} + +/// `release_requested(bytes32 indexed rskTxHash, bytes32 indexed btcTxHash, +/// uint256 amount)`, for the refund `refund` of [`RSK_TX_HASH`]'s call. +pub(super) fn release_requested(refund: &bitcoin::Transaction, amount: u64) -> Event { + ( + vec![ + topic("release_requested(bytes32,bytes32,uint256)"), + B256::from(RSK_TX_HASH), + B256::from(txid_bytes(refund)), + ], + word(amount).to_vec(), + ) +} + +/// `pegout_confirmed(bytes32 indexed btcTxHash, uint256 +/// pegoutCreationRskBlockNumber)`. +pub(super) fn pegout_confirmed(pegout: &bitcoin::Transaction, creation_block: u64) -> Event { + ( + vec![ + topic("pegout_confirmed(bytes32,uint256)"), + B256::from(txid_bytes(pegout)), + ], + word(creation_block).to_vec(), + ) +} + +/// The base58 address of a hash: rskj's `Address.toString()`. +pub(super) fn base58(version: u8, hash: &[u8; 20]) -> String { + let mut payload = vec![version]; + payload.extend_from_slice(hash); + bitcoin::base58::encode_check(&payload) +} diff --git a/crates/execution/src/rskj_tests/bridge_pegins/whitelist_storage_provider_impl_test.rs b/crates/execution/src/rskj_tests/bridge_pegins/whitelist_storage_provider_impl_test.rs new file mode 100644 index 0000000..e0f1607 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegins/whitelist_storage_provider_impl_test.rs @@ -0,0 +1,86 @@ +//! rskj's `WhitelistStorageProviderImplTest` +//! (`co/rsk/peg/whitelist/WhitelistStorageProviderImplTest.java`): the lock +//! whitelist as the Bridge stores it, its one-off entries in one cell and, +//! from RSKIP87, its unlimited entries in another. +//! +//! Read in full. Its other tests are of rskj's in-memory cache of the +//! whitelist and of a provider that was never read writing nothing, which +//! `storage::LockWhitelist` has no counterpart of: it is written back only by +//! the call that read it. + +use crate::bridge::storage::{self, LockWhitelist}; + +use super::support::{ctx_at, COIN}; + +/// The hash of rskj's `firstBtcAddress`. +const FIRST: [u8; 20] = [0x11; 20]; +/// The hash of rskj's `secondBtcAddress`. +const SECOND: [u8; 20] = [0x22; 20]; + +/// rskj `save_whenLockWhiteListIsNotNullAndRSKIP87IsNotActive_shouldReturnSavedValue`: +/// before RSKIP87 a one-off entry put in the whitelist read from an empty +/// store is saved in the one-off cell and read back, and the unlimited cell +/// is neither written nor read. +#[test] +fn before_rskip87_a_one_off_entry_is_saved_and_the_unlimited_cell_is_not() { + let mut ctx = ctx_at(1); + let mut whitelist = LockWhitelist::load(&mut ctx, false); + assert!(whitelist.one_off.is_empty() && whitelist.unlimited.is_empty()); + whitelist.one_off.push((FIRST, COIN)); + whitelist.save(&mut ctx); + + let read = LockWhitelist::load(&mut ctx, false); + assert_eq!(read.one_off, vec![(FIRST, COIN)]); + assert!(read.unlimited.is_empty()); + assert!(read.contains(&FIRST)); + assert_eq!( + storage::load_one_off_whitelist(&mut ctx).0, + vec![(FIRST, COIN)] + ); + assert!( + storage::bridge_load_bytes_named(&mut ctx, storage::UNLIMITED_LOCK_WHITELIST_KEY) + .is_empty() + ); +} + +/// rskj `save_whenIsActiveRSKIP87_shouldSavedUnlimitedLockWhitelist`: from +/// RSKIP87 a one-off and an unlimited entry are each saved in its own cell, +/// and both are read back. +#[test] +fn from_rskip87_a_one_off_and_an_unlimited_entry_are_saved_each_in_its_cell() { + let mut ctx = ctx_at(1); + let mut whitelist = LockWhitelist::load(&mut ctx, true); + assert!(whitelist.one_off.is_empty() && whitelist.unlimited.is_empty()); + whitelist.one_off.push((FIRST, COIN)); + whitelist.unlimited.push(SECOND); + whitelist.save(&mut ctx); + + let read = LockWhitelist::load(&mut ctx, true); + assert!(read.contains(&FIRST) && read.contains(&SECOND)); + assert_eq!(read.one_off.len() + read.unlimited.len(), 2); + assert_eq!( + storage::load_one_off_whitelist(&mut ctx).0, + vec![(FIRST, COIN)] + ); + assert_eq!(storage::load_unlimited_whitelist(&mut ctx), vec![SECOND]); +} + +/// rskj `getWhitelist_whenIsActiveRSKIP87_shouldReturnUnlimitedEntries`: from +/// RSKIP87 the whitelist read from the store holds both cells' entries, the +/// one-off one written with a disable height of 100. +#[test] +fn from_rskip87_the_whitelist_read_holds_both_cells_entries() { + let mut ctx = ctx_at(1); + storage::store_one_off_whitelist(&mut ctx, &[(FIRST, COIN)], 100); + storage::store_unlimited_whitelist(&mut ctx, &[SECOND]); + + let read = LockWhitelist::load(&mut ctx, true); + assert_eq!(read.one_off, vec![(FIRST, COIN)]); + assert_eq!(read.unlimited, vec![SECOND]); + assert_eq!(read.disable_height, 100); + assert!(read.contains(&FIRST) && read.contains(&SECOND)); + + // Before RSKIP87 the same store gives the one-off entry alone. + let before = LockWhitelist::load(&mut ctx, false); + assert!(before.contains(&FIRST) && !before.contains(&SECOND)); +} diff --git a/crates/execution/src/rskj_tests/bridge_pegouts/bitcoin_utils_legacy_test.rs b/crates/execution/src/rskj_tests/bridge_pegouts/bitcoin_utils_legacy_test.rs new file mode 100644 index 0000000..12daa05 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegouts/bitcoin_utils_legacy_test.rs @@ -0,0 +1,263 @@ +//! rskj's `BitcoinUtilsLegacyTest` (`co/rsk/peg/bitcoin/BitcoinUtilsLegacyTest.java`): +//! a multisig transaction's hash with its signatures taken out as rskj takes +//! them out before RSKIP305 +//! (`BitcoinUtilsLegacy.getMultiSigTransactionHashWithoutSignaturesBeforeRSKIP305`), +//! and the transaction with them taken out as it does from RSKIP305 +//! (`BitcoinUtils.getMultiSigTransactionWithoutSignatures`), over which a +//! peg-out's sighash is taken for the RSKIP379 index. rustock's +//! `peg::multisig_tx_hash_without_signatures` and +//! `peg::without_multisig_signatures` are the two; rskj's +//! `IllegalArgumentException` is their `Ok(None)`. +//! +//! rskj's `BtcTransaction.equals` compares two transactions' hashes, which +//! leave the witnesses out; the ports compare the whole transactions. Its two +//! tests of an input whose key reads as a script are held, through the +//! Bridge, by `bridge_support_register_btc_transaction_test.rs`. + +use bitcoin::hashes::Hash; + +use crate::bridge::{peg, release_tx, rskj_sender_compat}; + +use super::support::*; + +/// rskj `BitcoinTestUtils.getBtcEcKeyFromSeed`: the private key +/// `keccak256(seed)`. +fn key_of(seed: &str) -> k256::ecdsa::SigningKey { + use sha3::Digest; + k256::ecdsa::SigningKey::from_slice(&sha3::Keccak256::digest(seed.as_bytes())).expect("a key") +} + +/// A key's compressed public key. +fn public_of(key: &k256::ecdsa::SigningKey) -> [u8; 33] { + key.verifying_key() + .to_encoded_point(true) + .as_bytes() + .try_into() + .expect("33 bytes") +} + +/// rskj `getBtcEcKeysFromSeeds` of `member01` up to `count`, sorted: the +/// keys in the order of their public keys. +fn members(count: usize) -> Vec { + let mut keys: Vec<_> = (1..=count) + .map(|n| key_of(&format!("member{n:02}"))) + .collect(); + keys.sort_by_key(public_of); + keys +} + +/// The redeem script of rskj's `P2shErpFederationBuilder` (nine members) and +/// `P2shP2wshErpFederationBuilder` (twenty): the members' keys, with +/// mainnet's emergency keys and delay. Both formats spend by it. +fn erp_redeem(count: usize) -> Vec { + let constants = BridgeConstants::mainnet(); + let members: Vec<[u8; 33]> = members(count).iter().map(public_of).collect(); + let emergency: Vec<[u8; 33]> = constants + .erp_fed_pubkeys + .iter() + .map(|key| { + alloy_primitives::hex::decode(key) + .expect("hex") + .try_into() + .expect("33 bytes") + }) + .collect(); + peg::p2sh_erp_redeem_script(&members, &emergency, constants.erp_fed_activation_delay) +} + +/// bitcoinj `Script.getNumberOfSignaturesRequiredToSpend` of `redeem`. +fn required(redeem: &[u8]) -> usize { + let required = rskj_sender_compat::signatures_required_to_spend(redeem).expect("a multisig"); + usize::try_from(required).expect("a count") +} + +/// An input spending output 0 of rskj's `BitcoinTestUtils.createHash(n)`, +/// `n` in the hash's first byte, which bitcoinj writes last. +fn spending(n: u8, script_sig: Vec) -> bitcoin::TxIn { + let mut hash = [0u8; 32]; + hash[31] = n; + bitcoin::TxIn { + previous_output: bitcoin::OutPoint { + txid: bitcoin::Txid::from_byte_array(hash), + vout: 0, + }, + script_sig: bitcoin::ScriptBuf::from_bytes(script_sig), + sequence: bitcoin::Sequence::MAX, + witness: bitcoin::Witness::new(), + } +} + +/// rskj `ScriptBuilder.createInputScript(null, key)` for the key of the seed +/// `abc`: no signature, then the key, whose bytes don't read as a script. +fn spend_by_the_key_of_abc() -> Vec { + [vec![0x00], push(&public_of(&key_of("abc")))].concat() +} + +/// bitcoinj `getHash()`: a transaction's txid, which leaves its witnesses +/// out, in the order the two functions answer in. +fn hash_of(tx: &bitcoin::Transaction) -> [u8; 32] { + let mut hash = tx.compute_txid().to_byte_array(); + hash.reverse(); + hash +} + +/// `signer`'s signature of `sighash`, as bitcoinj writes it into an input. +fn signature(signer: &k256::ecdsa::SigningKey, sighash: &[u8; 32]) -> Vec { + use k256::ecdsa::signature::hazmat::PrehashSigner; + let signed: k256::ecdsa::Signature = signer.sign_prehash(sighash).expect("signed"); + release_tx::signature_to_insert(signed.to_der().as_bytes(), sighash, &public_of(signer)) + .expect("it verifies") +} + +/// rskj `BitcoinTestUtils.signLegacyTransactionInputFromP2shMultiSig` and +/// `signWitnessTransactionInputFromP2shMultiSig`: input `index` of `tx`, +/// spending `value` satoshis by `redeem`, signed by as many of `signers`, in +/// their order, as the script asks for, each signature where bitcoinj puts +/// it. +fn sign( + tx: &mut bitcoin::Transaction, + index: usize, + value: u64, + redeem: &[u8], + signers: &[k256::ecdsa::SigningKey], +) { + let witness = !tx.input[index].witness.is_empty(); + let sighash = if witness { + release_tx::segwit_sighash_all(tx, index, redeem, value) + } else { + release_tx::legacy_sighash_all(tx, index, redeem) + }; + for signer in signers.iter().take(required(redeem)) { + let public = public_of(signer); + let signature = signature(signer, &sighash); + if witness { + let pushes: Vec> = tx.input[index].witness.iter().map(<[u8]>::to_vec).collect(); + let at = release_tx::witness_sig_insertion_index(&pushes, &sighash, &public, redeem) + .expect("no point at infinity"); + let signed = release_tx::witness_update_with_signature(&pushes, &signature, at) + .expect("a slot left"); + tx.input[index].witness = bitcoin::Witness::from_slice(&signed); + } else { + let script_sig = tx.input[index].script_sig.as_bytes().to_vec(); + let at = release_tx::sig_insertion_index(&script_sig, &sighash, &public, redeem) + .expect("no point at infinity"); + let signed = release_tx::update_script_with_signature(&script_sig, &signature, at) + .expect("a slot left"); + tx.input[index].script_sig = bitcoin::ScriptBuf::from_bytes(signed); + } + } +} + +/// `getMultiSigTransactionHashWithoutSignatures_whenTransactionDoesNotHaveInputs_shouldReturnSameTxHash`: +/// a transaction of no inputs has nothing taken out, and keeps its hash. +#[test] +fn a_transaction_of_no_inputs_keeps_its_hash() { + let tx = btc_transaction(Vec::new(), &[]); + assert_eq!( + peg::multisig_tx_hash_without_signatures(&tx, false), + Ok(Some(hash_of(&tx))) + ); + assert_eq!( + peg::without_multisig_signatures(&tx, false), + Ok(Some(tx.clone())) + ); +} + +/// `getMultiSigTransactionHashWithoutSignatures_whenTransactionIsSegwit_shouldReturnExpectedTxHash`: +/// a P2SH-P2WSH ERP federation's input, as the Bridge leaves it +/// (`addSpendingFederationBaseScript`), signed by five of the eleven its +/// twenty members are asked for. A segwit transaction's hash is its own, +/// which its signatures don't change, and with them taken out it is the +/// transaction it was before them. +#[test] +fn a_segwit_transaction_keeps_its_hash_and_is_unsigned_as_it_was() { + let redeem = erp_redeem(20); + let mut tx = btc_transaction(vec![spending(1, Vec::new())], &[]); + let (script_sig, witness) = release_tx::segwit_base_input(&redeem, required(&redeem)); + tx.input[0].script_sig = script_sig; + tx.input[0].witness = witness; + let before = tx.clone(); + + sign(&mut tx, 0, BTC, &redeem, &members(5)); + assert_ne!(tx, before, "signed"); + + let hash = peg::multisig_tx_hash_without_signatures(&tx, false); + assert_eq!(hash, Ok(Some(hash_of(&tx)))); + assert_eq!(hash, Ok(Some(hash_of(&before)))); + let unsigned = peg::without_multisig_signatures(&tx, false).expect("no throw"); + assert_eq!(unsigned.as_ref(), Some(&before)); + assert_eq!(hash, Ok(unsigned.as_ref().map(hash_of))); +} + +/// `getMultiSigTransactionHashWithoutSignatures_whenTxIsNotSegwitAndTransactionInputsDoNotHaveP2shMultiSigInputScript_shouldThrowIAE`: +/// an input whose last push doesn't read as a script has no redeem script, +/// and both throw `IllegalArgumentException`. +#[test] +fn an_input_of_no_redeem_script_fails_both() { + let script_sig = spend_by_the_key_of_abc(); + assert_eq!( + rskj_sender_compat::redeem_script_of_input(&script_sig, None), + Ok(None) + ); + let tx = btc_transaction(vec![spending(1, script_sig)], &[]); + assert_eq!( + peg::multisig_tx_hash_without_signatures(&tx, false), + Ok(None) + ); + assert_eq!(peg::without_multisig_signatures(&tx, false), Ok(None)); +} + +/// `getMultiSigTransactionHashWithoutSignatures_whenTxIsNotSegwitButNotAllTransactionInputsHaveP2shMultiSigInputScript_shouldThrowIAE`: +/// a P2SH ERP federation's input with no signatures, its redeem script read, +/// and one whose last push doesn't read as a script: both throw +/// `IllegalArgumentException`. +#[test] +fn one_input_of_no_redeem_script_fails_both() { + let redeem = erp_redeem(9); + let multisig = release_tx::placeholder_scriptsig(&redeem, required(&redeem)); + assert_eq!( + rskj_sender_compat::redeem_script_of_input(&multisig, None), + Ok(Some(redeem.clone())) + ); + let by_a_key = spend_by_the_key_of_abc(); + assert_eq!( + rskj_sender_compat::redeem_script_of_input(&by_a_key, None), + Ok(None) + ); + let tx = btc_transaction(vec![spending(2, multisig), spending(1, by_a_key)], &[]); + assert_eq!( + peg::multisig_tx_hash_without_signatures(&tx, false), + Ok(None) + ); + assert_eq!(peg::without_multisig_signatures(&tx, false), Ok(None)); +} + +/// `getMultiSigTransactionHashWithoutSignatures_whenTransactionIsLegacyAndInputsHaveP2shMultiSigInputScript_shouldReturnExpectedTxHash`: +/// two inputs of a P2SH ERP federation of nine, paying a bitcoin to the +/// address of the seed `destinationAddress`, each signed by the five its +/// script asks for. With them taken out it is the transaction it was before +/// them, and its hash is that one's. +#[test] +fn a_legacy_transaction_is_unsigned_as_it_was() { + let redeem = erp_redeem(9); + let unsigned = release_tx::placeholder_scriptsig(&redeem, required(&redeem)); + let destination = p2pkh(&hash160(&public_of(&key_of("destinationAddress")))); + let mut tx = btc_transaction( + vec![spending(1, unsigned.clone()), spending(2, unsigned)], + &[(BTC, destination)], + ); + let before = tx.clone(); + + for index in 0..tx.input.len() { + sign(&mut tx, index, 0, &redeem, &members(5)); + } + assert_ne!(hash_of(&tx), hash_of(&before), "signed"); + + let unsigned = peg::without_multisig_signatures(&tx, false).expect("no throw"); + assert_eq!(unsigned.as_ref(), Some(&before)); + assert_eq!( + peg::multisig_tx_hash_without_signatures(&tx, false), + Ok(Some(hash_of(&before))) + ); + assert_eq!(unsigned.as_ref().map(hash_of), Some(hash_of(&before))); +} diff --git a/crates/execution/src/rskj_tests/bridge_pegouts/bridge_event_logger_impl_test.rs b/crates/execution/src/rskj_tests/bridge_pegouts/bridge_event_logger_impl_test.rs new file mode 100644 index 0000000..62ebd91 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegouts/bridge_event_logger_impl_test.rs @@ -0,0 +1,134 @@ +//! rskj's `BridgeEventLoggerImplTest`: the Bridge's events in the Solidity +//! format, here those whose data RSKIP326, RSKIP383 and RSKIP427 change. +//! rskj's tests give the logger an activation config; the ports call +//! mainnet's Bridge at a block before each rule and from it, and read what +//! it logs. + +use alloy_primitives::U256; + +use super::support::*; + +/// The one log among `logged` whose first topic is `topic`. +fn the(logged: &[revm::primitives::Log], topic: [u8; 32]) -> &revm::primitives::Log { + let matching: Vec<_> = logged + .iter() + .filter(|log| topics(log)[0] == topic) + .collect(); + assert_eq!(matching.len(), 1, "one log of the topic"); + matching[0] +} + +/// [`STRANGER`], who calls, as a topic. +fn sender_topic() -> [u8; 32] { + let mut word = [0u8; 32]; + word[12..].copy_from_slice(STRANGER.as_slice()); + word +} + +/// rskj's `logReleaseBtcRequestReceived_preRSKIP326_destinationAddressAsHash160`, +/// `logReleaseBtcRequestReceived_postRSKIP326_destinationAddressAsBase58` and +/// `logReleaseBtcRequestReceived_postRSKIP427_amountAsWeis`: a peg-out request +/// taken is logged as `release_request_received`, its sender a topic. Before +/// RSKIP326 (fingerroot500) its destination is the hash160 of its address, as +/// bytes; from it the address in Base58, and the event's signature changes +/// with it. Before RSKIP427 (lovell700) the amount is in satoshis, from it in +/// wei. +#[test] +fn a_pegout_request_taken_logs_its_destination_and_amount_as_its_era_has_them() { + let eras = [ + (MAINNET_HOP400, false, false), + (MAINNET_ARROWHEAD600, true, false), + (MAINNET_LOVELL700, true, true), + ]; + for (number, rskip326, rskip427) in eras { + let mut bridge = Bridge::mainnet_at(number); + let hash160 = bridge.tx.btc_sender_hash160; + bridge.release(BTC); + let logged = logs(&mut bridge.ctx); + let (signature, destination) = if rskip326 { + ( + "release_request_received(address,string,uint256)", + bitcoin::base58::encode_check(&[&[0x00][..], &hash160].concat()).into_bytes(), + ) + } else { + ( + "release_request_received(address,bytes,uint256)", + hash160.to_vec(), + ) + }; + let log = the(&logged, event_topic(signature)); + assert_eq!(topics(log)[1], sender_topic(), "{number}"); + let amount = if rskip427 { + U256::from(BTC) * U256::from(WEI_PER_SATOSHI) + } else { + U256::from(BTC) + }; + let data = log.data.data.to_vec(); + assert_eq!(word(&data, 0), U256::from(64), "{number}"); + assert_eq!(word(&data, 1), amount, "{number}"); + assert_eq!(word(&data, 2), U256::from(destination.len()), "{number}"); + assert_eq!( + &data[96..96 + destination.len()], + destination.as_slice(), + "{number}" + ); + } +} + +/// rskj's `logReleaseBtcRequestRejected_preRSKIP427_logAmountAsSatoshis` and +/// `logReleaseBtcRequestRejected_postRSKIP427_logAmountAsWeis`: a peg-out +/// request turned down, here for an amount below the minimum (LOW_AMOUNT, +/// 1), is logged as `release_request_rejected` with the amount in satoshis +/// before RSKIP427 and in wei from it. The fee per kilobyte stored, 10,000 +/// satoshis, keeps the fee rskj's `requestRelease` estimates below the +/// minimum, so that the minimum turns the request down; with the genesis +/// fee per kilobyte the estimate is the larger, and the reason +/// FEE_ABOVE_VALUE. +#[test] +fn a_pegout_request_turned_down_logs_its_amount_in_satoshis_before_rskip427_and_in_wei_from_it() { + let satoshis = BridgeConstants::mainnet().minimum_pegout_tx_value - 1; + for (number, rskip427) in [(MAINNET_ARROWHEAD600, false), (MAINNET_LOVELL700, true)] { + let mut bridge = Bridge::mainnet_at(number); + put_named( + &mut bridge.ctx, + "feePerKb", + &rlp_encode_element(&[0x27, 0x10]), + ); + bridge.release(satoshis); + let logged = logs(&mut bridge.ctx); + let log = the( + &logged, + event_topic("release_request_rejected(address,uint256,int256)"), + ); + assert_eq!(topics(log)[1], sender_topic(), "{number}"); + let amount = if rskip427 { + U256::from(satoshis) * U256::from(WEI_PER_SATOSHI) + } else { + U256::from(satoshis) + }; + assert_eq!(word(&log.data.data, 0), amount, "{number}"); + assert_eq!(word(&log.data.data, 1), U256::from(1), "{number}"); + } +} + +/// rskj's `logCommitFederation`, run without RSKIP383 and with it: a +/// federation committed is logged with its activation block, the commit's +/// block and mainnet's activation age, 18,500 blocks before RSKIP383 +/// (fingerroot500) and 40,320 from it. +#[test] +fn a_federation_committed_is_logged_with_the_activation_age_of_its_era() { + for (number, age) in [(MAINNET_HOP400, 18_500u64), (MAINNET_FINGERROOT500, 40_320)] { + let mut bridge = Bridge::mainnet_at(number); + assert_eq!(elect(&mut bridge, &keys(20, 3)), 1, "{number}"); + let logged = logs(&mut bridge.ctx); + let log = the( + &logged, + event_topic("commit_federation(bytes,string,bytes,string,int256)"), + ); + assert_eq!( + word(&log.data.data, 4), + U256::from(number + age), + "{number}" + ); + } +} diff --git a/crates/execution/src/rskj_tests/bridge_pegouts/bridge_event_logger_legacy_impl_test.rs b/crates/execution/src/rskj_tests/bridge_pegouts/bridge_event_logger_legacy_impl_test.rs new file mode 100644 index 0000000..f1fc203 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegouts/bridge_event_logger_legacy_impl_test.rs @@ -0,0 +1,172 @@ +//! rskj's `BridgeEventLoggerLegacyImplTest`: before RSKIP146 the Bridge logs +//! its events in rskj's legacy format, one topic, the event's name as +//! `DataWord.fromString` makes it, and RLP data. From RSKIP146 rskj's legacy +//! logger throws for each of them, and the Bridge logs them in the Solidity +//! format instead. rskj's tests call the logger; the ports call mainnet's +//! Bridge a block before papyrus200, which brings RSKIP146, and at it, and +//! read what it logs. + +use super::support::*; + +/// A block before RSKIP146. +const BEFORE_RSKIP146: u64 = MAINNET_PAPYRUS200 - 1; +/// The block that brings it. +const FROM_RSKIP146: u64 = MAINNET_PAPYRUS200; + +/// The logs among `logged` whose first topic is `topic`. +fn of(logged: &[revm::primitives::Log], topic: [u8; 32]) -> Vec<&revm::primitives::Log> { + logged + .iter() + .filter(|log| topics(log)[0] == topic) + .collect() +} + +/// rskj's `testLogUpdateCollectionsBeforeRskip146` and +/// `testLogUpdateCollectionsAfterRskip146`: before RSKIP146 +/// `updateCollections` logs `update_collections_topic`, from the Bridge, its +/// data the sender as an RLP element; from it, `update_collections(address)`. +#[test] +fn before_rskip146_update_collections_is_logged_in_the_legacy_format() { + let mut bridge = Bridge::mainnet_at(BEFORE_RSKIP146); + bridge.update_collections(); + let logged = logs(&mut bridge.ctx); + assert_eq!(logged.len(), 1); + assert_eq!(logged[0].address, crate::precompiles::BRIDGE_ADDR); + assert_eq!( + topics(&logged[0]), + vec![legacy_topic("update_collections_topic")] + ); + assert_eq!( + logged[0].data.data.to_vec(), + rlp_encode_element(STRANGER.as_slice()) + ); + + let mut bridge = Bridge::mainnet_at(FROM_RSKIP146); + bridge.update_collections(); + let logged = logs(&mut bridge.ctx); + assert_eq!(logged.len(), 1); + assert_eq!( + topics(&logged[0])[0], + event_topic("update_collections(address)") + ); +} + +/// rskj's `testLogAddSignatureBeforeRskip146` and +/// `testLogAddSignatureAfterRskip146`: before RSKIP146 a federator's +/// signature is logged as `add_signature_topic`, its data the RLP list of the +/// transaction's hash as a hex string, the hash160 of the federator's key and +/// the hash of the RSK transaction that made the peg-out; from it, as +/// `add_signature(bytes32,address,bytes)`. +#[test] +fn before_rskip146_a_signature_is_logged_in_the_legacy_format() { + let mut signing = Signing::mainnet_at(BEFORE_RSKIP146); + let hash = signing.unsigned.compute_txid().to_string(); + signing.sign(0); + let logged = logs(&mut signing.bridge.ctx); + assert_eq!(logged.len(), 1); + assert_eq!(logged[0].address, crate::precompiles::BRIDGE_ADDR); + assert_eq!( + topics(&logged[0]), + vec![legacy_topic("add_signature_topic")] + ); + assert_eq!( + logged[0].data.data.to_vec(), + rlp_encode_list(&[ + rlp_encode_element(hash.as_bytes()), + rlp_encode_element(&hash160(&signing.key(0))), + rlp_encode_element(&RELEASE_HASH), + ]) + ); + + let mut signing = Signing::mainnet_at(FROM_RSKIP146); + signing.sign(0); + let logged = logs(&mut signing.bridge.ctx); + assert_eq!(logged.len(), 1); + assert_eq!( + topics(&logged[0])[0], + event_topic("add_signature(bytes32,address,bytes)") + ); +} + +/// rskj's `testLogReleaseBtcBeforeRskip146` and +/// `testLogReleaseBtcAfterRskip146`: before RSKIP146 the signature that +/// completes a peg-out logs `release_btc_topic`, its data the RLP list of the +/// signed transaction's hash as a hex string and the transaction; from it, +/// `release_btc(bytes32,bytes)`. +#[test] +fn before_rskip146_a_pegout_signed_is_logged_in_the_legacy_format() { + let mut signing = Signing::mainnet_at(BEFORE_RSKIP146); + signing.sign(0); + logs(&mut signing.bridge.ctx); + signing.sign(1); + let logged = logs(&mut signing.bridge.ctx); + assert_eq!(logged.len(), 2); + assert_eq!( + topics(&logged[0]), + vec![legacy_topic("add_signature_topic")] + ); + assert_eq!(topics(&logged[1]), vec![legacy_topic("release_btc_topic")]); + let signed = signing.signed_by(&[0, 1]); + assert_eq!( + logged[1].data.data.to_vec(), + rlp_encode_list(&[ + rlp_encode_element(signed.compute_txid().to_string().as_bytes()), + rlp_encode_element(&bitcoin::consensus::serialize(&signed)), + ]) + ); + + let mut signing = Signing::mainnet_at(FROM_RSKIP146); + signing.sign(0); + signing.sign(1); + let logged = logs(&mut signing.bridge.ctx); + assert_eq!( + of(&logged, event_topic("release_btc(bytes32,bytes)")).len(), + 1 + ); + assert!(of(&logged, legacy_topic("release_btc_topic")).is_empty()); +} + +/// The data rskj's legacy logger gives a federation in +/// `commit_federation_topic`: the hash160 of its address, and its keys. +fn legacy_federation(keys: &[[u8; 33]]) -> Vec { + let mut sorted = keys.to_vec(); + sorted.sort(); + let keys: Vec> = sorted.iter().map(|key| rlp_encode_element(key)).collect(); + rlp_encode_list(&[ + rlp_encode_element(&hash160(&standard_redeem(&sorted))), + rlp_encode_list(&keys), + ]) +} + +/// rskj's `testLogCommitFederationBeforeRskip146` and +/// `testLogCommitFederationAfterRskip146`: before RSKIP146 a federation +/// committed is logged as `commit_federation_topic`, its data the RLP list of +/// the federation it follows (here mainnet's genesis federation), the new +/// one, and the new one's activation block as a decimal string, the commit's +/// block and mainnet's activation age before RSKIP383, 18,500 blocks; from +/// it, as `commit_federation(bytes,string,bytes,string,int256)`. +#[test] +fn before_rskip146_a_federation_committed_is_logged_in_the_legacy_format() { + let members = keys(20, 3); + let mut bridge = Bridge::mainnet_at(BEFORE_RSKIP146); + assert_eq!(elect(&mut bridge, &members), 1); + let logged = logs(&mut bridge.ctx); + let committed = of(&logged, legacy_topic("commit_federation_topic")); + assert_eq!(committed.len(), 1); + assert_eq!(topics(committed[0]).len(), 1); + assert_eq!( + committed[0].data.data.to_vec(), + rlp_encode_list(&[ + legacy_federation(&genesis_keys(&BridgeConstants::mainnet())), + legacy_federation(&members), + rlp_encode_element((BEFORE_RSKIP146 + 18_500).to_string().as_bytes()), + ]) + ); + + let mut bridge = Bridge::mainnet_at(FROM_RSKIP146); + assert_eq!(elect(&mut bridge, &members), 1); + let logged = logs(&mut bridge.ctx); + let signature = "commit_federation(bytes,string,bytes,string,int256)"; + assert_eq!(of(&logged, event_topic(signature)).len(), 1); + assert!(of(&logged, legacy_topic("commit_federation_topic")).is_empty()); +} diff --git a/crates/execution/src/rskj_tests/bridge_pegouts/bridge_storage_provider_test.rs b/crates/execution/src/rskj_tests/bridge_pegouts/bridge_storage_provider_test.rs new file mode 100644 index 0000000..218f296 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegouts/bridge_storage_provider_test.rs @@ -0,0 +1,905 @@ +//! rskj's `BridgeStorageProviderTest`: which of the Bridge's cells its +//! storage provider reads and writes, on each side of the upgrade that +//! brings each one. +//! +//! rskj's tests hand the provider a mocked repository and check which keys +//! it asks for and which it writes. rustock has no provider: the Bridge's +//! methods read and write the cells themselves, each with the switch of the +//! rule that brings the cell. So each port runs the method, or the function +//! the methods share, at a block of mainnet on either side of the rule's +//! upgrade, and reads the cells it leaves, written as rskj writes them. + +use super::support::*; +use crate::bridge::peg::{ + deserialize_rsk_txs_waiting_for_signatures, load_pegout_confirmation_set, + load_release_request_queue, store_pegout_confirmation_set, PegoutWaitingForConfirmations, +}; + +/// One bitcoin, in satoshis. +const BTC: u64 = 100_000_000; + +/// A Bitcoin transaction of one input, spending the output 0 of `tag`'s +/// transaction. +fn btc_tx(tag: u8) -> Vec { + use bitcoin::hashes::Hash; + bitcoin::consensus::serialize(&bitcoin::Transaction { + version: bitcoin::transaction::Version::ONE, + lock_time: bitcoin::absolute::LockTime::ZERO, + input: vec![bitcoin::TxIn { + previous_output: bitcoin::OutPoint { + txid: bitcoin::Txid::from_byte_array([tag; 32]), + vout: 0, + }, + script_sig: bitcoin::ScriptBuf::new(), + sequence: bitcoin::Sequence::MAX, + witness: bitcoin::Witness::new(), + }], + output: vec![], + }) +} + +/// rskj `serializePegoutsWaitingForConfirmations`: each transaction and the +/// block that made it, in the order of the transactions' bytes. +fn legacy_set(entries: &[(Vec, u64)]) -> Vec { + let mut entries = entries.to_vec(); + entries.sort(); + let items: Vec> = entries + .iter() + .flat_map(|(tx, height)| [rlp_encode_element(tx), serialize_long(*height)]) + .collect(); + rlp_encode_list(&items) +} + +/// rskj `serializePegoutsWaitingForConfirmationsWithTxHash`: each +/// transaction, the block that made it and the hash of the transaction that +/// did, in the order of the transactions' bytes. +fn hashed_set(entries: &[(Vec, u64, [u8; 32])]) -> Vec { + let mut entries = entries.to_vec(); + entries.sort(); + let items: Vec> = entries + .iter() + .flat_map(|(tx, height, hash)| { + [ + rlp_encode_element(tx), + serialize_long(*height), + rlp_encode_element(hash), + ] + }) + .collect(); + rlp_encode_list(&items) +} + +fn waiting(tx: Vec, height: u64, hash: Option<[u8; 32]>) -> PegoutWaitingForConfirmations { + PegoutWaitingForConfirmations { + btc_tx_raw: tx, + rsk_block_height: height, + rsk_tx_hash: hash, + } +} + +// --------------------------------------------------------------------------- +// RSKIP146: the peg-out requests and the peg-outs waiting for confirmations +// keep the hash of the transaction that made them, in cells of their own. +// --------------------------------------------------------------------------- + +/// rskj's `saveReleaseRequestQueue_before_rskip_146_activation`: before +/// RSKIP146 a release request carries no transaction hash, and the queue is +/// written to `releaseRequestQueue` alone; `releaseRequestQueueWithTxHash` is +/// never written. +#[test] +fn before_rskip146_a_peg_out_request_is_written_to_the_legacy_queue_alone() { + let mut bridge = Bridge::mainnet_at(MAINNET_PAPYRUS200 - 1); + assert!(!bridge.cfg.has_rskip146(bridge.number())); + bridge.release(BTC); + let destination = bridge.tx.btc_sender_hash160; + assert_eq!( + named(&mut bridge.ctx, "releaseRequestQueue"), + Some(legacy_queue(&[(destination, BTC)])) + ); + assert_eq!( + named(&mut bridge.ctx, "releaseRequestQueueWithTxHash"), + None + ); +} + +/// rskj's `getReleaseRequestQueue_after_rskip_146_activation` and +/// `saveReleaseRequestQueue_after_rskip_146_activation`: from RSKIP146 the +/// queue is the entries of `releaseRequestQueue` followed by those of +/// `releaseRequestQueueWithTxHash`. A new request carries its transaction's +/// hash and goes to the second; the first keeps its entries. +#[test] +fn from_rskip146_a_peg_out_request_is_queued_with_its_hash_after_the_legacy_ones() { + let mut bridge = Bridge::mainnet_at(MAINNET_PAPYRUS200); + assert!(bridge.cfg.has_rskip146(bridge.number())); + let old = ([0x11; 20], 2 * BTC); + put_named( + &mut bridge.ctx, + "releaseRequestQueue", + &legacy_queue(&[old]), + ); + bridge.release(BTC); + let new = (bridge.tx.btc_sender_hash160, BTC, bridge.tx.rsk_tx_hash); + assert_eq!( + named(&mut bridge.ctx, "releaseRequestQueue"), + Some(legacy_queue(&[old])) + ); + assert_eq!( + named(&mut bridge.ctx, "releaseRequestQueueWithTxHash"), + Some(hashed_queue(&[new])) + ); + let queue: Vec<_> = load_release_request_queue(&mut bridge.ctx, true) + .into_iter() + .map(|entry| { + ( + entry.btc_dest_hash160, + entry.amount_satoshis, + entry.rsk_tx_hash, + ) + }) + .collect(); + assert_eq!( + queue, + vec![(old.0, old.1, None), (new.0, new.1, Some(new.2))] + ); +} + +/// rskj's `saveReleaseRequestQueue_after_rskip_146_activation` asserts that +/// from RSKIP146 saving the queue writes `releaseRequestQueue` as well as +/// `releaseRequestQueueWithTxHash`. rskj saves the queue at the end of every +/// Bridge call that loaded it (`Bridge.execute`, `teardown`, +/// `BridgeSupport.save`, `BridgeStorageProvider.saveReleaseRequestQueue`), +/// and `releaseBtc` loads it to add the request. Where `releaseRequestQueue` +/// never held anything, it is written as the empty list. +#[test] +fn from_rskip146_a_peg_out_request_writes_the_legacy_queue_back_too() { + let mut bridge = Bridge::mainnet_at(MAINNET_PAPYRUS200); + bridge.release(BTC); + assert!(named(&mut bridge.ctx, "releaseRequestQueueWithTxHash").is_some()); + assert_eq!( + named(&mut bridge.ctx, "releaseRequestQueue"), + Some(vec![0xc0]), + "the empty list, RLP.encodeList()" + ); +} + +/// The getters that read the queue from RSKIP271, `getQueuedPegoutsCount` +/// and `getEstimatedFeesForNextPegOutEvent`, end as every Bridge method that +/// returns does in rskj: `teardown` saves what they read, both of the +/// queue's cells, the empty list where they held nothing. +#[test] +fn from_rskip271_the_getters_that_read_the_queue_write_it_back() { + for method in [ + "getQueuedPegoutsCount()", + "getEstimatedFeesForNextPegOutEvent()", + ] { + let mut bridge = Bridge::mainnet_at(MAINNET_HOP400); + bridge + .execute(&crate::abi::selector(method)) + .expect("the getter answers"); + for cell in ["releaseRequestQueue", "releaseRequestQueueWithTxHash"] { + assert_eq!( + named(&mut bridge.ctx, cell), + Some(vec![0xc0]), + "{method} writes {cell} back" + ); + } + } +} + +/// From RSKIP540 `getEstimatedFeesForPegOutAmount` simulates the peg-out +/// over the queue it reads, and when it answers, `teardown` writes the +/// queue back as the other getters do. +#[test] +fn from_rskip540_the_estimate_for_an_amount_writes_the_queue_back() { + let mut collections = Collections::mainnet_at(MAINNET_VETIVER900); + collections.active(1, 10); + let wei = alloy_primitives::U256::from(BTC) * alloy_primitives::U256::from(10_000_000_000u64); + let data = [ + crate::abi::selector("getEstimatedFeesForPegOutAmount(uint256)").to_vec(), + wei.to_be_bytes::<32>().to_vec(), + ] + .concat(); + collections + .bridge + .execute(&data) + .expect("the estimate answers"); + for cell in ["releaseRequestQueue", "releaseRequestQueueWithTxHash"] { + assert_eq!( + named(&mut collections.bridge.ctx, cell), + Some(vec![0xc0]), + "{cell} is written back" + ); + } +} + +/// rskj's `getPegoutsWaitingForConfirmations_before_rskip_146_activation` +/// and `savePegoutsWaitingForConfirmations_before_rskip_146_activations`: +/// before RSKIP146 the set is what `releaseTransactionSet` holds, and +/// `releaseTransactionSetWithTxHash` is neither read nor written. +#[test] +fn before_rskip146_the_pegouts_waiting_for_confirmations_are_the_legacy_cell_alone() { + let block = MAINNET_PAPYRUS200 - 1; + let rskip146 = RskHardforkConfig::mainnet().has_rskip146(block); + assert!(!rskip146); + + let mut ctx = ctx_at(block); + put_named( + &mut ctx, + "releaseTransactionSet", + &legacy_set(&[(btc_tx(1), 1)]), + ); + put_named( + &mut ctx, + "releaseTransactionSetWithTxHash", + &hashed_set(&[(btc_tx(2), 2, [0x22; 32])]), + ); + let set = load_pegout_confirmation_set(&mut ctx, rskip146); + assert_eq!(set.len(), 1); + assert_eq!( + (set[0].btc_tx_raw.clone(), set[0].rsk_block_height), + (btc_tx(1), 1) + ); + + let mut ctx = ctx_at(block); + store_pegout_confirmation_set( + &mut ctx, + &[waiting(btc_tx(1), 1, None), waiting(btc_tx(3), 3, None)], + rskip146, + ); + assert_eq!( + named(&mut ctx, "releaseTransactionSet"), + Some(legacy_set(&[(btc_tx(1), 1), (btc_tx(3), 3)])) + ); + assert_eq!(named(&mut ctx, "releaseTransactionSetWithTxHash"), None); +} + +/// rskj's `getPegoutsWaitingForConfirmations_after_rskip_146_activation` and +/// `savePegoutsWaitingForConfirmations_after_rskip_146_activations`: from +/// RSKIP146 the set is what both cells hold, and it is written back split by +/// whether an entry carries the hash of the transaction that made it. +#[test] +fn from_rskip146_the_pegouts_waiting_for_confirmations_span_both_cells() { + let rskip146 = RskHardforkConfig::mainnet().has_rskip146(MAINNET_PAPYRUS200); + assert!(rskip146); + + let mut ctx = ctx_at(MAINNET_PAPYRUS200); + put_named( + &mut ctx, + "releaseTransactionSet", + &legacy_set(&[(btc_tx(1), 1)]), + ); + put_named( + &mut ctx, + "releaseTransactionSetWithTxHash", + &hashed_set(&[(btc_tx(2), 2, [0x22; 32])]), + ); + let set = load_pegout_confirmation_set(&mut ctx, rskip146); + let read: Vec<_> = set + .iter() + .map(|entry| { + ( + entry.btc_tx_raw.clone(), + entry.rsk_block_height, + entry.rsk_tx_hash, + ) + }) + .collect(); + assert_eq!( + read, + vec![(btc_tx(1), 1, None), (btc_tx(2), 2, Some([0x22; 32]))] + ); + + let mut ctx = ctx_at(MAINNET_PAPYRUS200); + store_pegout_confirmation_set( + &mut ctx, + &[ + waiting(btc_tx(1), 1, None), + waiting(btc_tx(3), 1, Some([0x33; 32])), + ], + rskip146, + ); + assert_eq!( + named(&mut ctx, "releaseTransactionSet"), + Some(legacy_set(&[(btc_tx(1), 1)])) + ); + assert_eq!( + named(&mut ctx, "releaseTransactionSetWithTxHash"), + Some(hashed_set(&[(btc_tx(3), 1, [0x33; 32])])) + ); + assert_eq!(load_pegout_confirmation_set(&mut ctx, rskip146).len(), 2); +} + +/// rskj's `getReleaseTransaction_after_rskip_146_activations`: three +/// peg-outs saved with the hashes of the transactions that made them read +/// back as three, and none of them waits for signatures. +#[test] +fn from_rskip146_saved_pegouts_read_back_and_none_waits_for_signatures() { + let mut ctx = ctx_at(MAINNET_PAPYRUS200); + let pegouts: Vec<_> = (1..=3u8) + .map(|n| waiting(btc_tx(n), u64::from(n), Some([n; 32]))) + .collect(); + store_pegout_confirmation_set(&mut ctx, &pegouts, true); + assert_eq!(load_pegout_confirmation_set(&mut ctx, true).len(), 3); + let signing = named(&mut ctx, "rskTxsWaitingFS").unwrap_or_default(); + assert!(deserialize_rsk_txs_waiting_for_signatures(&signing).is_empty()); +} + +// --------------------------------------------------------------------------- +// RSKIP134: a processed peg-in's hash gets a cell of its own, where before it +// went into one map of them all. +// --------------------------------------------------------------------------- + +/// A Bitcoin transaction's hash in its internal byte order, and as bitcoinj's +/// `Sha256Hash` of it reads, which is the other way round. +fn btc_hash(tag: u8) -> ([u8; 32], [u8; 32]) { + let mut internal = [tag; 32]; + internal[0] = 0x01; + let mut display = internal; + display.reverse(); + (internal, display) +} + +/// rskj `serializeMapOfHashesToLong` of a map of one hash. +fn processed_map(display: &[u8; 32], height: u64) -> Vec { + rlp_encode_list(&[rlp_encode_element(display), serialize_long(height)]) +} + +/// rskj's `BTC_TX_HASH_AP.getCompoundKey("-", hash.toString())`. +fn processed_cell(display: &[u8; 32]) -> alloy_primitives::U256 { + compound("btcTxHashAP", &alloy_primitives::hex::encode(display)) +} + +/// rskj's `setHeightBtcTxhashAlreadyProcessed_before_RSKIP134_does_not_use_new_storage` +/// and `getHeightIfBtcTxhashIsAlreadyProcessed_before_RSKIP134_does_not_use_new_storage`: +/// before RSKIP134 a processed peg-in's hash goes into `btcTxHashesAP`, the +/// map of them all, and is read from it with its height. +#[test] +fn before_rskip134_a_processed_peg_in_is_kept_in_the_map_of_them_all() { + let block = MAINNET_PAPYRUS200 - 1; + let rskip134 = RskHardforkConfig::mainnet().has_rskip134(block); + assert!(!rskip134); + let (internal, display) = btc_hash(0xab); + let mut ctx = ctx_at(block); + crate::bridge::tx::set_btc_tx_processed(&mut ctx, &internal, 1, rskip134); + assert_eq!( + named(&mut ctx, "btcTxHashesAP"), + Some(processed_map(&display, 1)) + ); + assert_eq!(cell(&mut ctx, processed_cell(&display)), None); + assert_eq!( + crate::bridge::tx::get_btc_tx_processed_height(&mut ctx, &internal), + Some(1) + ); +} + +/// rskj's `setHeightBtcTxhashAlreadyProcessed_before_RSKIP134_uses_new_storage`, +/// whose activations are all of them: from RSKIP134 a processed peg-in's +/// hash gets a cell of its own, holding its height, and the map is not +/// touched. +#[test] +fn from_rskip134_a_processed_peg_in_is_kept_in_a_cell_of_its_own() { + let rskip134 = RskHardforkConfig::mainnet().has_rskip134(MAINNET_PAPYRUS200); + assert!(rskip134); + let (internal, display) = btc_hash(0xab); + let mut ctx = ctx_at(MAINNET_PAPYRUS200); + crate::bridge::tx::set_btc_tx_processed(&mut ctx, &internal, 1, rskip134); + assert_eq!( + cell(&mut ctx, processed_cell(&display)), + Some(serialize_long(1)) + ); + assert_eq!(named(&mut ctx, "btcTxHashesAP"), None); + assert_eq!( + crate::bridge::tx::get_btc_tx_processed_height(&mut ctx, &internal), + Some(1) + ); +} + +/// rskj's `getHeightIfBtcTxhashIsAlreadyProcessed_after_RSKIP134_uses_new_storage`: +/// from RSKIP134 a hash the map holds reads from the map, and one the map +/// doesn't hold from its own cell, every time it is asked. +#[test] +fn from_rskip134_a_processed_peg_in_is_read_from_the_map_or_its_own_cell() { + let (in_map, in_map_display) = btc_hash(0x01); + let (in_cell, in_cell_display) = btc_hash(0x02); + let mut ctx = ctx_at(MAINNET_PAPYRUS200); + put_named( + &mut ctx, + "btcTxHashesAP", + &processed_map(&in_map_display, 1), + ); + crate::bridge::storage::bridge_store_raw( + &mut ctx, + processed_cell(&in_cell_display), + Some(serialize_long(2)), + ); + let height = |ctx: &mut Ctx, hash| crate::bridge::tx::get_btc_tx_processed_height(ctx, hash); + assert_eq!(height(&mut ctx, &in_map), Some(1)); + assert_eq!(height(&mut ctx, &in_cell), Some(2)); + assert_eq!(height(&mut ctx, &in_cell), Some(2)); +} + +// --------------------------------------------------------------------------- +// RSKIP271: peg-outs are made in batches, the next batch not before the +// block `nextPegoutHeight` names. +// --------------------------------------------------------------------------- + +/// The Bridge at `block` of mainnet, its genesis federation holding ten +/// bitcoins, a peg-out request of one queued, and the next batch named for +/// a block far ahead. +fn a_request_and_a_batch_far_ahead(block: u64) -> Bridge { + let mut bridge = Bridge::mainnet_at(block); + let (constants, cfg) = (bridge.constants.clone(), bridge.cfg.clone()); + fund_genesis_federation(&mut bridge.ctx, &constants, &cfg, block, 10 * BTC); + put_named( + &mut bridge.ctx, + "releaseRequestQueueWithTxHash", + &hashed_queue(&[([0x44; 20], BTC, [0x45; 32])]), + ); + put_named( + &mut bridge.ctx, + "nextPegoutHeight", + &serialize_long(MAINNET_HOP400 + 10_000), + ); + bridge +} + +/// rskj's `getNextPegoutHeight_before_RSKIP271_activation` and +/// `saveNextPegoutHeight_before_RSKIP271`: before RSKIP271 the provider +/// answers no next height without reading the cell and never writes it, so +/// `updateCollections` makes a peg-out of every request whatever the cell +/// holds. From RSKIP271 the same request waits for the block it names. +#[test] +fn before_rskip271_the_next_pegout_height_is_neither_read_nor_written() { + let mut bridge = a_request_and_a_batch_far_ahead(MAINNET_HOP400 - 1); + assert!(!bridge.cfg.has_rskip271(bridge.number())); + bridge.update_collections(); + let queue = load_release_request_queue(&mut bridge.ctx, true); + assert!(queue.is_empty(), "the request was made a peg-out"); + assert_eq!(load_pegout_confirmation_set(&mut bridge.ctx, true).len(), 1); + assert_eq!( + named(&mut bridge.ctx, "nextPegoutHeight"), + Some(serialize_long(MAINNET_HOP400 + 10_000)) + ); + + let mut bridge = a_request_and_a_batch_far_ahead(MAINNET_HOP400); + assert!(bridge.cfg.has_rskip271(bridge.number())); + bridge.update_collections(); + assert_eq!(load_release_request_queue(&mut bridge.ctx, true).len(), 1); + assert!(load_pegout_confirmation_set(&mut bridge.ctx, true).is_empty()); +} + +/// rskj's `saveNextPegoutHeight_after_RSKIP271` and +/// `setNextPegoutHeightAndGetNextPegoutHeight_after_RSKIP271_activation`: +/// from RSKIP271 `updateCollections` with no request queued names the next +/// batch's block, `nextPegoutHeight` holds it as rskj's `serializeLong` +/// writes it, and `getNextPegoutCreationBlockNumber` reads it back. Before +/// it the cell is never written. +#[test] +fn from_rskip271_the_next_pegout_height_is_written_and_read_back() { + let mut bridge = Bridge::mainnet_at(MAINNET_HOP400 - 1); + bridge.update_collections(); + assert_eq!(named(&mut bridge.ctx, "nextPegoutHeight"), None); + + let mut bridge = Bridge::mainnet_at(MAINNET_HOP400); + bridge.update_collections(); + let next = MAINNET_HOP400 + bridge.constants.number_of_blocks_between_pegouts; + assert_eq!( + named(&mut bridge.ctx, "nextPegoutHeight"), + Some(serialize_long(next)) + ); + let answer = crate::bridge::peg::get_next_pegout_creation_block_number(&mut bridge.ctx, 0) + .expect("getNextPegoutCreationBlockNumber answers"); + assert_eq!( + alloy_primitives::U256::from_be_slice(&answer.bytes), + alloy_primitives::U256::from(next) + ); +} + +// --------------------------------------------------------------------------- +// RSKIP143: a Bitcoin block's coinbase information, the root of its +// transactions' witnesses, registered by `registerBtcCoinbaseTransaction`. +// --------------------------------------------------------------------------- + +/// rskj's `getCoinBaseInformation_before_RSKIP143` and +/// `getCoinBaseInformation_after_RSKIP143`: the coinbase information of a +/// Bitcoin block is read from RSKIP143, and before it the provider answers +/// none without reading it. rskj reads it where a peg-in's tree doesn't come +/// to the block's merkle root (`isBlockMerkleRootValid`): from RSKIP143 a +/// tree that comes to the block's witness root proves the peg-in, which is +/// taken; before it the peg-in is left alone. +#[test] +fn from_rskip143_a_blocks_coinbase_information_proves_a_peg_in() { + use RskNetworkUpgrade::{Papyrus200, TwoToThree}; + let federation = keys(1, 3); + let tx = tx_paying(&[(BTC, p2sh(&hash160(&standard_redeem(&federation))))]); + let raw = bitcoin::consensus::serialize(&tx); + let hash = sha256d(&raw); + for (last, taken) in [(TwoToThree, false), (Papyrus200, true)] { + let cfg = up_to(last, RSK_REGTEST_CHAIN_ID); + let mut ctx = ctx_at(1_000); + let headers = btc_chain(&mut ctx, &cfg, |_| [0x11; 32]); + store_federation(&mut ctx, NEW_FEDERATION, &federation, 1, 1000); + fund_bridge(&mut ctx); + // registerBtcCoinbaseTransaction's witness root, as it stores the + // argument: display order. + let block = block_hash(&headers[PEGIN_HEIGHT as usize - 1]); + crate::bridge::storage::bridge_store_raw( + &mut ctx, + compound("coinbaseInformation", &alloy_primitives::hex::encode(block)), + Some(rlp_encode_list(&[rlp_encode_element(&reversed(hash))])), + ); + let tree = one_leaf_tree(&hash); + let tx_ctx = BridgeTxContext::default(); + register(&mut ctx, &cfg, &tx_ctx, &raw, PEGIN_HEIGHT, &tree).expect("it answers"); + assert_eq!( + crate::bridge::tx::is_btc_tx_processed(&mut ctx, &hash), + taken, + "up to {last:?}" + ); + } +} + +/// rskj's `setCoinBaseInformation_after_RSKIP143` and +/// `saveCoinBaseInformation_after_RSKIP143`: the coinbase information of a +/// block is stored under `coinbaseInformation-` as rskj's +/// `serializeCoinbaseInformation` writes it, a list of the witness root, and +/// read back. +#[test] +fn a_blocks_coinbase_information_is_stored_as_rskj_serializes_it() { + let (block, root) = ([0x0b; 32], [0x5f; 32]); + let mut ctx = ctx_at(MAINNET_PAPYRUS200); + crate::bridge::tx::set_coinbase_information(&mut ctx, &block, &root); + assert_eq!( + cell( + &mut ctx, + compound("coinbaseInformation", &alloy_primitives::hex::encode(block)) + ), + Some(rlp_encode_list(&[rlp_encode_element(&root)])) + ); + assert_eq!( + crate::bridge::tx::get_coinbase_information(&mut ctx, &block), + Some(root) + ); +} + +// --------------------------------------------------------------------------- +// RSKIP199: the Bitcoin main chain's blocks indexed by their heights. +// --------------------------------------------------------------------------- + +/// rskj's `saveBtcBlocksIndex_beforeRskip199` and +/// `saveBtcBlocksIndex_afterRskip199`: from RSKIP199 each block that becomes +/// the head of the Bitcoin chain is indexed by its height, the cell +/// `btcBlockHeight-` holding its hash as rskj's `serializeSha256Hash` +/// writes it; before it nothing is indexed. +#[test] +fn from_rskip199_each_block_the_chain_takes_is_indexed_by_its_height() { + use RskNetworkUpgrade::{Iris300, Papyrus200}; + let headers = regtest_headers(3, |_| [0x11; 32]); + for (last, indexed) in [(Papyrus200, false), (Iris300, true)] { + let cfg = up_to(last, RSK_REGTEST_CHAIN_ID); + let mut ctx = ctx_at(1); + receive_headers(&mut ctx, &headers, &cfg); + for (height, header) in (1u32..).zip(&headers) { + assert_eq!( + cell(&mut ctx, compound("btcBlockHeight", &height.to_string())), + indexed.then(|| rlp_encode_element(&block_hash(header))), + "height {height} up to {last:?}" + ); + } + } +} + +/// rskj's `getBtcBestBlockHashByHeight_beforeRskip199`, +/// `getBtcBestBlockHashByHeight_afterRskip199` and +/// `getBtcBestBlockHashByHeight_afterRskip199_hashNotFound`: from RSKIP199 the +/// main chain's block at a height is the one its index names, and where the +/// index names none the chain is walked back from its head; before it the +/// index is never read and the chain is walked. +#[test] +fn from_rskip199_the_main_chain_block_at_a_height_is_the_one_its_index_names() { + use crate::bridge::btc_chain::{main_chain_block_at_height, MainChainLookup}; + let cfg = up_to(RskNetworkUpgrade::Iris300, RSK_REGTEST_CHAIN_ID); + let headers = regtest_headers(3, |_| [0x11; 32]); + let mut ctx = ctx_at(1); + receive_headers(&mut ctx, &headers, &cfg); + // A block beside the main chain's second, stored, with less work than + // the head: it doesn't become the head. + let beside = mine(headers[0].block_hash(), [0xf1; 32]); + receive_headers(&mut ctx, &[beside], &cfg); + let index = compound("btcBlockHeight", "2"); + crate::bridge::storage::bridge_store_raw( + &mut ctx, + index, + Some(rlp_encode_element(&block_hash(&beside))), + ); + let at_2 = |ctx: &mut Ctx, rskip199| match main_chain_block_at_height(ctx, 2, rskip199, None) { + MainChainLookup::Found(block) => Some(block.header.block_hash()), + _ => None, + }; + assert_eq!(at_2(&mut ctx, false), Some(headers[1].block_hash())); + assert_eq!(at_2(&mut ctx, true), Some(beside.block_hash())); + crate::bridge::storage::bridge_store_raw(&mut ctx, index, None); + assert_eq!(at_2(&mut ctx, true), Some(headers[1].block_hash())); +} + +// --------------------------------------------------------------------------- +// RSKIP176: Flyover's peg-ins, each marked by its transaction and its +// derivation hash, the federation's Flyover address described by its hash. +// --------------------------------------------------------------------------- + +/// The contract that registers Flyover payments. +const LBC: alloy_primitives::Address = alloy_primitives::Address::repeat_byte(0x42); + +/// The user's and the liquidity provider's address: a regtest P2PKH address, +/// its version and its hash. +const USER: [u8; 21] = { + let mut address = [0x51; 21]; + address[0] = 0x6f; + address +}; + +/// The hash of the payment's derivation arguments. +const ARGUMENTS: [u8; 32] = [0x11; 32]; + +/// rskj `PegUtils.getFlyoverDerivationHash` from RSKIP284: the Keccak-256 of +/// the arguments' hash, the user's refund address, the LBC and the liquidity +/// provider's address. +fn derivation_hash() -> [u8; 32] { + use sha3::Digest; + sha3::Keccak256::digest([&ARGUMENTS[..], &USER, LBC.as_slice(), &USER].concat()).into() +} + +/// rskj `FlyoverRedeemScriptBuilderImpl.of`: the derivation hash pushed and +/// dropped before the federation's redeem script. +fn flyover_redeem(federation: &[[u8; 33]]) -> Vec { + [ + &[0x20][..], + &derivation_hash(), + &[0x75], + &standard_redeem(federation), + ] + .concat() +} + +/// The output script of the Flyover address: the P2SH of its redeem script. +fn flyover_script(federation: &[[u8; 33]]) -> Vec { + p2sh(&hash160(&flyover_redeem(federation))) +} + +/// rskj's `fastBridgeHashUsedInBtcTx-`, the hashes +/// as bitcoinj's `Sha256Hash` and rskj's `Keccak256` print them. +fn used_mark(btc_tx_hash: &[u8; 32]) -> alloy_primitives::U256 { + compound( + "fastBridgeHashUsedInBtcTx", + &format!( + "{}{}", + alloy_primitives::hex::encode(reversed(*btc_tx_hash)), + alloy_primitives::hex::encode(derivation_hash()) + ), + ) +} + +/// What the LBC's registration of a Flyover payment of a bitcoin to the +/// federation of `keys(1, 3)` is answered with, at block 1,000 of rskj's +/// regtest, with the cell of its mark holding `mark`; the Bridge after it, +/// and the payment's hash. +fn register_flyover(mark: Option>) -> (i128, Ctx, [u8; 32]) { + use crate::abi::encode::{self, Arg}; + let cfg = RskHardforkConfig::rskj_regtest(); + let federation = keys(1, 3); + let tx = tx_paying(&[(BTC, flyover_script(&federation))]); + let raw = bitcoin::consensus::serialize(&tx); + let hash = sha256d(&raw); + let mut ctx = ctx_at(1_000); + btc_chain(&mut ctx, &cfg, |_| hash); + store_federation(&mut ctx, NEW_FEDERATION, &federation, 1, 1000); + fund_bridge(&mut ctx); + if let Some(mark) = mark { + crate::bridge::storage::bridge_store_raw(&mut ctx, used_mark(&hash), Some(mark)); + } + let mut lbc = [0u8; 32]; + lbc[12..].copy_from_slice(LBC.as_slice()); + let tree = one_leaf_tree(&hash); + let args = encode::args(&[ + Arg::Bytes(&raw), + Arg::Word(encode::int(i64::from(PEGIN_HEIGHT))), + Arg::Bytes(&tree), + Arg::Word(ARGUMENTS), + Arg::Bytes(&USER), + Arg::Word(lbc), + Arg::Bytes(&USER), + Arg::Word([0; 32]), + ]); + let answer = crate::bridge::peg::register_fast_bridge_btc_transaction( + &mut ctx, + &args, + 0, + &BridgeConstants::rskj_regtest(), + &cfg, + &BridgeTxContext::default(), + LBC, + 2, + ) + .expect("registerFastBridgeBtcTransaction answers"); + (int256(&answer.bytes), ctx, hash) +} + +/// rskj's `saveDerivationArgumentsScriptHash_afterRSKIP176_ok` and +/// `isFlyoverFederationDerivationHashUsed_afterRSKIP176_returnTrue`: a +/// registered payment's derivation hash is marked used in its cell with rskj's +/// one true byte, and with it marked the payment is answered +/// UNPROCESSABLE_TX_ALREADY_PROCESSED_ERROR, -302. +#[test] +fn a_registered_flyover_payment_is_marked_with_one_true_byte() { + let (answer, mut ctx, hash) = register_flyover(None); + assert_eq!(answer, i128::from(BTC) * i128::from(WEI_PER_SATOSHI)); + assert_eq!(cell(&mut ctx, used_mark(&hash)), Some(vec![1])); + let (answer, _, _) = register_flyover(Some(vec![1])); + assert_eq!(answer, -302); +} + +/// rskj's `isFlyoverFederationDerivationHashUsed_storageReturnsNull_returnFalse`, +/// `_storageReturnsEmpty_returnFalse` and `_storageReturnsWrongValue_returnFalse`: +/// a payment is marked used only by its cell holding the one byte 1. With the +/// cell holding nothing, or any other bytes, it is registered. (A cell can't +/// hold no bytes: writing none clears it.) +#[test] +fn a_flyover_mark_of_anything_but_one_true_byte_is_no_mark() { + let locked = i128::from(BTC) * i128::from(WEI_PER_SATOSHI); + for mark in [ + None, + Some(vec![]), + Some(vec![0]), + Some(vec![2]), + Some(vec![1, 1]), + ] { + let (answer, _, _) = register_flyover(mark.clone()); + assert_eq!(answer, locked, "marked {mark:?}"); + } +} + +// --------------------------------------------------------------------------- +// RSKIP200: `receiveHeader` keeps the time of the last header it took. +// --------------------------------------------------------------------------- + +/// rskj `receiveHeader` of `header` in a block of time `timestamp`: its answer. +fn receive_header( + ctx: &mut Ctx, + header: &bitcoin::block::Header, + cfg: &RskHardforkConfig, + timestamp: u64, +) -> i128 { + use crate::abi::encode::{self, Arg}; + ctx.block.timestamp = alloy_primitives::U256::from(timestamp); + let args = encode::args(&[Arg::Bytes(&bitcoin::consensus::serialize(header))]); + let answer = crate::bridge::btc_chain::receive_header( + ctx, + &args, + 0, + &BridgeConstants::rskj_regtest(), + false, + cfg, + ) + .expect("receiveHeader answers"); + int256(&answer.bytes) +} + +/// rskj's `getReceiveHeadersLastTimestamp_after_RSKIP200`, +/// `saveReceiveHeadersLastTimestamp_after_RSKIP200`, +/// `getReceiveHeadersLastTimestamp_not_in_repository` and +/// `saveReceiveHeadersLastTimestamp_not_set`: from RSKIP200 `receiveHeader` +/// keeps the time of the block that took a header, `receiveHeadersLastTimestamp` +/// holding it as `serializeLong` writes it, and reads it back: a header within +/// `minSecondsBetweenCallsToReceiveHeader` of it, 300 on rskj's regtest, is +/// answered RECEIVE_HEADER_CALLED_TOO_SOON, -1, and the time is left as it +/// was. With no time kept a header is taken; `receiveHeaders` keeps no time. +#[test] +fn from_rskip200_receive_header_keeps_and_reads_the_time_of_the_last_header() { + let cfg = up_to(RskNetworkUpgrade::Iris300, RSK_REGTEST_CHAIN_ID); + let headers = regtest_headers(5, |_| [0x11; 32]); + let mut ctx = ctx_at(1); + receive_headers(&mut ctx, &headers[..3], &cfg); + assert_eq!(named(&mut ctx, "receiveHeadersLastTimestamp"), None); + + let time = 1_700_000_000; + assert_eq!(receive_header(&mut ctx, &headers[3], &cfg, time), 0); + assert_eq!( + named(&mut ctx, "receiveHeadersLastTimestamp"), + Some(serialize_long(time)) + ); + assert_eq!(receive_header(&mut ctx, &headers[4], &cfg, time + 299), -1); + assert_eq!( + named(&mut ctx, "receiveHeadersLastTimestamp"), + Some(serialize_long(time)) + ); + assert_eq!(receive_header(&mut ctx, &headers[4], &cfg, time + 300), 0); + assert_eq!( + named(&mut ctx, "receiveHeadersLastTimestamp"), + Some(serialize_long(time + 300)) + ); +} + +/// The data the last push of `script` pushes. +fn last_push(script: &bitcoin::ScriptBuf) -> Vec { + script + .instructions() + .filter_map( + |instruction| match instruction.expect("a script that parses") { + bitcoin::script::Instruction::PushBytes(data) => Some(data.as_bytes().to_vec()), + bitcoin::script::Instruction::Op(_) => None, + }, + ) + .last() + .expect("a push") +} + +/// rskj's `getFlyoverFederationInformation_afterRSKIP176_ok` and +/// `getFlyoverFederationInformation_notFound`: the Flyover information stored +/// under the hash of a Flyover address's redeem script is read when a peg-out +/// spends a UTXO paid to that address (`FlyoverCompatibleBtcWalletWithStorage +/// .findRedeemDataFromScriptHash`), and the input is made to be signed with +/// the redeem script it names: the derivation hash, dropped, before the redeem +/// script of the federation it names. For a UTXO paid to the federation's own +/// address no information is found, and its input is made with the +/// federation's redeem script. +#[test] +fn a_pegout_spends_a_flyover_utxo_with_the_redeem_script_its_information_names() { + use bitcoin::hashes::Hash; + let cfg = RskHardforkConfig::rskj_regtest(); + let federation = keys(1, 3); + let redeem = standard_redeem(&federation); + let mut bridge = Bridge::new(cfg.clone(), BridgeConstants::rskj_regtest(), 1_000); + store_federation(&mut bridge.ctx, NEW_FEDERATION, &federation, 1, 1000); + let utxo = |tag: u8, script: Vec| crate::bridge::storage::BridgeUtxo { + tx_hash: [tag; 32], + vout: 0, + value_satoshis: BTC, + height: 1, + script, + coinbase: false, + }; + crate::bridge::storage::store_federation_utxos( + &mut bridge.ctx, + &cfg, + 1_000, + &[ + utxo(0xf1, flyover_script(&federation)), + utxo(0xfe, p2sh(&hash160(&redeem))), + ], + ); + crate::bridge::storage::bridge_store_raw( + &mut bridge.ctx, + compound( + "fastBridgeFederationInformation", + &alloy_primitives::hex::encode(hash160(&flyover_redeem(&federation))), + ), + Some(rlp_encode_list(&[ + rlp_encode_element(&derivation_hash()), + rlp_encode_element(&hash160(&redeem)), + ])), + ); + put_named( + &mut bridge.ctx, + "releaseRequestQueueWithTxHash", + &hashed_queue(&[([0x44; 20], 3 * BTC / 2, [0x45; 32])]), + ); + bridge.update_collections(); + + let set = load_pegout_confirmation_set(&mut bridge.ctx, true); + assert_eq!(set.len(), 1, "one peg-out"); + let pegout: bitcoin::Transaction = + bitcoin::consensus::deserialize(&set[0].btc_tx_raw).expect("a transaction"); + let mut spent: Vec<(u8, Vec)> = pegout + .input + .iter() + .map(|input| { + let tag = input.previous_output.txid.to_byte_array()[0]; + (tag, last_push(&input.script_sig)) + }) + .collect(); + spent.sort(); + assert_eq!( + spent, + vec![(0xf1, flyover_redeem(&federation)), (0xfe, redeem)] + ); +} diff --git a/crates/execution/src/rskj_tests/bridge_pegouts/bridge_support_add_signature_test.rs b/crates/execution/src/rskj_tests/bridge_pegouts/bridge_support_add_signature_test.rs new file mode 100644 index 0000000..a883674 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegouts/bridge_support_add_signature_test.rs @@ -0,0 +1,983 @@ +//! rskj's `BridgeSupportAddSignatureTest`: what `addSignature` signs in, and +//! when it logs `add_signature`, which RSKIP326 changes. Before it the event +//! is logged for each call of a federator's, before the signature is +//! checked; from it, only when a signature is added. rskj's tests mock +//! RSKIP146 on and RSKIP326 either way; the ports run mainnet's Bridge at +//! hop400, with RSKIP146 and before RSKIP326, and at fingerroot500, which +//! brings it. rskj's tests hand `BridgeSupport` what the Bridge would turn +//! down before it (no signatures, bytes that are not DER); the ports call +//! the Bridge, as a member's transaction does. Its nested `AddSignature` and +//! `MigrationTxSigning` tests, of P2SH ERP and P2SH-P2WSH ERP federations +//! with every upgrade on, run at vetiver900. Its parameterized `addSignature`, +//! of the address the event names a federator by, which RSKIP415 changes, +//! runs on each side of arrowhead600. + +use super::support::*; + +const BEFORE_RSKIP326: u64 = MAINNET_HOP400; +const FROM_RSKIP326: u64 = MAINNET_FINGERROOT500; + +/// The DER signature of r and s both 1: it reads, and signs nothing. +const WRONG: &[u8] = &[0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01]; + +/// How many `add_signature` events `calls` calls of the first member's log +/// at block `number`, each with its own signature, or with [`WRONG`]. +fn add_signature_events(number: u64, valid: bool, calls: usize) -> usize { + let mut signing = Signing::mainnet_at(number); + let signature = if valid { + signing.signature(0) + } else { + WRONG.to_vec() + }; + for _ in 0..calls { + signing + .add_signature(0, &signature) + .expect("addSignature runs"); + } + let topic = event_topic("add_signature(bytes32,address,bytes)"); + logs(&mut signing.bridge.ctx) + .iter() + .filter(|log| topics(log)[0] == topic) + .count() +} + +/// rskj's `addSignature_calledTwiceWithSameFederatorPreRSKIP326_emitEventTwice` +/// and `addSignature_calledTwiceWithSameFederatorPostRSKIP326_emitEventOnlyOnce`. +#[test] +fn a_federator_signing_twice_is_logged_twice_before_rskip326_and_once_from_it() { + assert_eq!(add_signature_events(BEFORE_RSKIP326, true, 2), 2); + assert_eq!(add_signature_events(FROM_RSKIP326, true, 2), 1); +} + +/// rskj's `addSignatureCreateEventLog_preRSKIP326` and +/// `addSignature_afterRSKIP326_eventEmitted`. +#[test] +fn a_federators_signature_is_logged() { + assert_eq!(add_signature_events(BEFORE_RSKIP326, true, 1), 1); + assert_eq!(add_signature_events(FROM_RSKIP326, true, 1), 1); +} + +/// The federator `addSignature`'s event names at block `number` when the +/// member of the private key 350 signs, in a federation of 150 and 250, each +/// stored with the RSK key of the private key after its own and the MST key +/// of the one after that. The signer is stored so too where `multikey`, and +/// with its BTC key for all three where not. One signature of the two needed +/// is kept, and logged alone, by the Bridge, with the peg-out's creation hash +/// and the federator's BTC key. +fn logged_federator(number: u64, multikey: bool) -> alloy_primitives::Address { + use crate::abi::encode::{args, Arg}; + use crate::bridge::federation::StoredMember; + let member = |btc: u16, rsk: u16, mst: u16| { + let mut private = [0u8; 32]; + private[30..].copy_from_slice(&btc.to_be_bytes()); + let stored = StoredMember { + btc: public_key(&btc.to_be_bytes()), + rsk: public_key(&rsk.to_be_bytes()), + mst: public_key(&mst.to_be_bytes()), + }; + (stored, private) + }; + let signer = if multikey { + member(350, 351, 352) + } else { + member(350, 350, 350) + }; + let members = vec![member(150, 151, 152), member(250, 251, 252), signer]; + let mut signing = Signing::of_members(number, members, 1); + let index = (0..3) + .find(|&i| signing.key(i) == signer.0.btc) + .expect("the signer is a member"); + let signature = signing.signature(index); + signing + .add_signature(index, &signature) + .expect("addSignature runs"); + let logs = logs(&mut signing.bridge.ctx); + assert_eq!(logs.len(), 1, "one signature of two, logged alone"); + assert_eq!(logs[0].address, crate::precompiles::BRIDGE_ADDR); + let topics = topics(&logs[0]); + assert_eq!(topics.len(), 3); + assert_eq!( + topics[0], + event_topic("add_signature(bytes32,address,bytes)") + ); + assert_eq!(topics[1], RELEASE_HASH); + assert_eq!( + logs[0].data.data.to_vec(), + args(&[Arg::Bytes(&signer.0.btc)]), + "the federator's BTC key" + ); + alloy_primitives::Address::from_slice(&topics[2][12..]) +} + +/// rskj's `addSignature`, `addSignatureArgProvider`'s four cases: from +/// RSKIP415 the `add_signature` event names a federator by the address of its +/// RSK key, and before it by its BTC key's, which is the same where a +/// member's keys are one. The two addresses are rskj's, of the private keys +/// 350 and 351. +#[test] +fn from_rskip415_a_federator_is_logged_at_its_rsk_keys_address() { + let of_btc_key = alloy_primitives::address!("dbc29273d4de3d5645e308c7e629d28d4499b3d3"); + let of_rsk_key = alloy_primitives::address!("74891a05ad4d7ec87c1cffe9bd00bb4e1382b586"); + let before = MAINNET_ARROWHEAD600 - 1; + assert_eq!(logged_federator(before, false), of_btc_key); + assert_eq!(logged_federator(before, true), of_btc_key); + assert_eq!(logged_federator(MAINNET_ARROWHEAD600, false), of_btc_key); + assert_eq!(logged_federator(MAINNET_ARROWHEAD600, true), of_rsk_key); +} + +/// rskj's `save()` writes back the set of peg-outs waiting for signatures +/// whenever a call read it (`BridgeStorageProvider +/// .savePegoutsWaitingForSignatures`), and `addSignature` reads it first +/// (`addReleaseSignatures`): where it was never written, a call for a +/// peg-out it does not hold leaves the empty list there. Where it is +/// written, a call that signs nothing, here by the key of no member, leaves +/// it as it was. +#[test] +fn add_signature_writes_back_the_set_it_read() { + let mut signing = Signing::mainnet_at(FROM_RSKIP326); + let kept = named(&mut signing.bridge.ctx, "rskTxsWaitingFS"); + assert!(kept.is_some()); + let signature = signing.signature(0); + signing + .add_signatures_as(0, &public_key(&[9]), std::slice::from_ref(&signature)) + .expect("addSignature runs"); + assert_eq!(named(&mut signing.bridge.ctx, "rskTxsWaitingFS"), kept); + + let mut signing = Signing::mainnet_at(FROM_RSKIP326); + crate::bridge::storage::bridge_store_raw( + &mut signing.bridge.ctx, + crate::bridge::storage::bridge_storage_key("rskTxsWaitingFS"), + None, + ); + assert_eq!(named(&mut signing.bridge.ctx, "rskTxsWaitingFS"), None); + signing + .add_signature(0, &signature) + .expect("addSignature runs"); + assert_eq!( + named(&mut signing.bridge.ctx, "rskTxsWaitingFS"), + Some(vec![0xc0]), + "the empty list" + ); + assert!(logs(&mut signing.bridge.ctx).is_empty()); +} + +/// rskj's `addSignature_invalidSignatureBeforeRSKIP326_eventStillEmitted` and +/// `addSignature_invalidSignatureAfterRSKIP326_noEventEmitted`. rskj's tests +/// hand `BridgeSupport` bytes that aren't DER; the Bridge turns those down +/// before `BridgeSupport` sees them, so here a DER signature that doesn't +/// verify takes their place. +#[test] +fn a_signature_that_does_not_verify_is_logged_before_rskip326_only() { + assert_eq!(add_signature_events(BEFORE_RSKIP326, false, 1), 1); + assert_eq!(add_signature_events(FROM_RSKIP326, false, 1), 0); +} + +/// The peg-out as it waits for signatures. +fn waiting(signing: &mut Signing) -> bitcoin::Transaction { + let kept = crate::bridge::peg::deserialize_rsk_txs_waiting_for_signatures( + &named(&mut signing.bridge.ctx, "rskTxsWaitingFS").unwrap_or_default(), + ); + bitcoin::consensus::deserialize(&kept[&RELEASE_HASH]).expect("a transaction") +} + +/// rskj's `addSignatureNonCanonicalSignature`: a member's signature with its +/// s in the upper half of the curve's order verifies, but rskj's +/// `getTransactionSignatures` wants it canonical (`isCanonical`) and signs +/// nothing. +#[test] +fn a_signature_whose_s_is_in_the_upper_half_of_the_order_signs_nothing() { + let mut signing = Signing::mainnet_at(FROM_RSKIP326); + let low = k256::ecdsa::Signature::from_der(&signing.signature(0)).expect("DER"); + let (r, s) = low.split_scalars(); + let high = + k256::ecdsa::Signature::from_scalars(r.to_bytes(), (-s).to_bytes()).expect("a signature"); + signing + .add_signature(0, high.to_der().as_bytes()) + .expect("addSignature runs"); + assert_eq!(waiting(&mut signing), signing.unsigned); +} + +/// A member's signature with its r's length in DER's long form, which +/// bitcoinj's `decodeFromDER` reads (Bouncy Castle's `ASN1InputStream`), or +/// with a byte after it, which bitcoinj reads past: each is signed in as +/// bitcoinj writes the signature, `TransactionSignature.encodeToBitcoin`, +/// not as it was sent. +#[test] +fn a_signature_bitcoinj_reads_is_signed_in_as_bitcoinj_writes_it() { + let der = Signing::mainnet_at(FROM_RSKIP326).signature(0); + // `30 L 02 lr r 02 ls s`, with r's length written `81 lr`. + let long_form = [&[0x30, der[1] + 1, 0x02, 0x81][..], &der[3..]].concat(); + let trailing = [der.clone(), vec![0x00]].concat(); + for sent in [long_form, trailing] { + let mut signing = Signing::mainnet_at(FROM_RSKIP326); + signing.add_signature(0, &sent).expect("addSignature runs"); + assert_eq!(waiting(&mut signing), signing.signed_by(&[0])); + } +} + +/// A signature with an integer whose first byte only repeats the sign of +/// the next: Bouncy Castle refuses the integer (`ASN1Integer.isMalformed`), +/// bitcoinj's `decodeFromDER` throws, and so, from RSKIP88, does the +/// Bridge's `addSignature`. A first byte the next one's sign needs is read, +/// and so is an integer with no bytes, as 0; neither signs anything. +#[test] +fn a_signature_with_an_integer_padded_past_its_sign_fails_the_call() { + let mut signing = Signing::mainnet_at(FROM_RSKIP326); + let padded: [&[u8]; 2] = [ + &[0x30, 0x07, 0x02, 0x02, 0x00, 0x7f, 0x02, 0x01, 0x01], + &[0x30, 0x07, 0x02, 0x01, 0x01, 0x02, 0x02, 0xff, 0x80], + ]; + for sent in padded { + assert!(signing.add_signature(0, sent).is_err(), "{sent:02x?}"); + } + let read: [&[u8]; 3] = [ + &[0x30, 0x07, 0x02, 0x02, 0x00, 0x80, 0x02, 0x01, 0x01], + &[0x30, 0x07, 0x02, 0x01, 0x01, 0x02, 0x02, 0xff, 0x7f], + &[0x30, 0x04, 0x02, 0x00, 0x02, 0x00], + ]; + for sent in read { + assert!(signing.add_signature(0, sent).is_ok(), "{sent:02x?}"); + } + assert_eq!(waiting(&mut signing), signing.unsigned); +} + +/// The peg-outs waiting for signatures, by the hash of the transaction that +/// made each. +fn waiting_for_signatures(signing: &mut Signing) -> std::collections::BTreeMap<[u8; 32], Vec> { + crate::bridge::peg::deserialize_rsk_txs_waiting_for_signatures( + &named(&mut signing.bridge.ctx, "rskTxsWaitingFS").unwrap_or_default(), + ) + .into_iter() + .collect() +} + +/// The private key of no member. +const STRANGER_KEY: [u8; 32] = [0x77; 32]; + +/// rskj's `addSignatureToMissingTransaction`: a member's signature for a +/// peg-out that is not waiting for signatures changes nothing, and logs +/// nothing. +#[test] +fn a_signature_for_a_pegout_not_waiting_changes_nothing() { + let mut signing = Signing::mainnet_at(BEFORE_RSKIP326); + let signature = signing.signature(0); + put_named( + &mut signing.bridge.ctx, + "rskTxsWaitingFS", + &rlp_encode_list(&[]), + ); + signing + .add_signature(0, &signature) + .expect("addSignature runs"); + assert!(waiting_for_signatures(&mut signing).is_empty()); + assert!(logs(&mut signing.bridge.ctx).is_empty()); +} + +/// rskj's `addSignatureFromInvalidFederator`: a member's call with the key +/// of no member, and that key's signature, signs nothing and logs nothing. +#[test] +fn the_key_of_no_member_signs_nothing() { + let mut signing = Signing::mainnet_at(BEFORE_RSKIP326); + let signature = signing.signature_by(&STRANGER_KEY, 0); + signing + .add_signatures_as(0, &public_key(&STRANGER_KEY), &[signature]) + .expect("addSignature runs"); + assert_eq!(waiting(&mut signing), signing.unsigned); + assert!(logs(&mut signing.bridge.ctx).is_empty()); +} + +/// rskj's `addSignatureWithInvalidSignature`: a member's call with the +/// signature of a key that is no member's signs nothing. +#[test] +fn a_signature_by_another_key_signs_nothing() { + let mut signing = Signing::mainnet_at(BEFORE_RSKIP326); + let signature = signing.signature_by(&STRANGER_KEY, 0); + signing + .add_signature(0, &signature) + .expect("addSignature runs"); + assert_eq!(waiting(&mut signing), signing.unsigned); +} + +/// rskj's `addSignatureWithLessSignaturesThanExpected`: no signatures for the +/// peg-out's one input. rskj's `BridgeSupport` returns; the Bridge turns an +/// empty list down before it, and from RSKIP88 fails the call. +#[test] +fn no_signatures_fail_the_call() { + let mut signing = Signing::mainnet_at(BEFORE_RSKIP326); + assert!(signing.add_signatures(0, &[]).is_err()); + assert_eq!(waiting(&mut signing), signing.unsigned); +} + +/// rskj's `addSignatureWithMoreSignaturesThanExpected`: two signatures for +/// the peg-out's one input sign nothing. +#[test] +fn more_signatures_than_inputs_sign_nothing() { + let mut signing = Signing::mainnet_at(BEFORE_RSKIP326); + let signature = signing.signature(0); + signing + .add_signatures(0, &[signature.clone(), signature]) + .expect("addSignature runs"); + assert_eq!(waiting(&mut signing), signing.unsigned); +} + +/// rskj's `addSignatureOneSignature`: one member's signature is kept, and +/// the peg-out waits for the rest. +#[test] +fn one_members_signature_is_kept() { + let mut signing = Signing::mainnet_at(BEFORE_RSKIP326); + signing.sign(0); + assert_eq!(waiting(&mut signing), signing.signed_by(&[0])); +} + +/// rskj's `addSignatureTwice`: a member that signed signs nothing more with +/// another signature of its own. +#[test] +fn a_member_that_signed_signs_nothing_more() { + let mut signing = Signing::mainnet_at(BEFORE_RSKIP326); + signing.sign(0); + let another = signing.another_signature_of(0, 0); + assert_ne!(another, signing.signature(0)); + signing + .add_signature(0, &another) + .expect("addSignature runs"); + assert_eq!(waiting(&mut signing), signing.signed_by(&[0])); +} + +/// The raw transaction a `release_btc` event logs. +fn released(log: &revm::primitives::Log) -> Vec { + let data = &log.data.data; + let length = word(data, 1).to::(); + data[64..64 + length].to_vec() +} + +/// rskj's `addSignatureTwoSignatures`: the second of the federation's two +/// needed signatures signs the peg-out. It no longer waits, and after each +/// member's `add_signature` the Bridge logs `release_btc` with it signed. +#[test] +fn two_members_sign_the_pegout_and_it_is_released() { + let mut signing = Signing::mainnet_at(BEFORE_RSKIP326); + signing.sign(0); + signing.sign(1); + assert!(waiting_for_signatures(&mut signing).is_empty()); + let logs = logs(&mut signing.bridge.ctx); + let firsts: Vec<[u8; 32]> = logs.iter().map(|log| topics(log)[0]).collect(); + let add = event_topic("add_signature(bytes32,address,bytes)"); + let release = event_topic("release_btc(bytes32,bytes)"); + assert_eq!(firsts, [add, add, release]); + assert_eq!(topics(&logs[2])[1], RELEASE_HASH); + assert_eq!( + released(&logs[2]), + bitcoin::consensus::serialize(&signing.signed_by(&[0, 1])) + ); +} + +/// rskj's `addSignatureMultipleInputsPartiallyValid`: a peg-out of three +/// inputs. A member's three signatures, the third by another key, sign none +/// of them; with a third that bitcoinj cannot read, the Bridge fails the +/// call, as rskj's test, which hands it to `BridgeSupport`, does not see; +/// with the member's own three, the three inputs are signed; and a second +/// member's three sign the peg-out, which is released. +#[test] +fn a_pegout_of_three_inputs_is_signed_only_by_signatures_for_all_three() { + let mut signing = Signing::spending(BEFORE_RSKIP326, 3); + let mut first: Vec> = (0..3).map(|index| signing.signature_of(0, index)).collect(); + let own = first[2].clone(); + first[2] = signing.signature_by(&STRANGER_KEY, 2); + signing + .add_signatures(0, &first) + .expect("addSignature runs"); + assert_eq!(waiting(&mut signing), signing.unsigned); + first[2] = (0..own.len() as u8).collect(); + assert!(signing.add_signatures(0, &first).is_err()); + assert_eq!(waiting(&mut signing), signing.unsigned); + first[2] = own; + signing + .add_signatures(0, &first) + .expect("addSignature runs"); + assert_eq!(waiting(&mut signing), signing.signed_by(&[0])); + let second: Vec> = (0..3).map(|index| signing.signature_of(1, index)).collect(); + signing + .add_signatures(1, &second) + .expect("addSignature runs"); + assert!(waiting_for_signatures(&mut signing).is_empty()); + let logs = logs(&mut signing.bridge.ctx); + let add = event_topic("add_signature(bytes32,address,bytes)"); + let release = event_topic("release_btc(bytes32,bytes)"); + let firsts: Vec<[u8; 32]> = logs.iter().map(|log| topics(log)[0]).collect(); + assert_eq!(firsts, [add, add, add, release]); + assert_eq!( + released(&logs[3]), + bitcoin::consensus::serialize(&signing.signed_by(&[0, 1])) + ); +} + +/// A key's compressed public key. +fn compressed(key: &k256::ecdsa::SigningKey) -> [u8; 33] { + key.verifying_key() + .to_encoded_point(true) + .as_bytes() + .try_into() + .expect("33 bytes") +} + +/// A federation as rskj's nested tests make one: the keys of the private keys +/// `keccak256(seed)` (`getBtcEcKeysFromSeeds(seeds, true)`), in the order of +/// their public keys, kept in `format`, P2SH ERP or P2SH-P2WSH ERP. +struct Seeded { + signers: Vec, + keys: Vec<[u8; 33]>, + format: u64, + redeem: Vec, +} + +impl Seeded { + fn of(seeds: &[String], format: u64, bridge: &Bridge) -> Self { + use sha3::Digest; + let mut signers: Vec = seeds + .iter() + .map(|seed| { + let private = sha3::Keccak256::digest(seed.as_bytes()); + k256::ecdsa::SigningKey::from_slice(&private).expect("a key") + }) + .collect(); + signers.sort_by_key(compressed); + let keys: Vec<[u8; 33]> = signers.iter().map(compressed).collect(); + let redeem = crate::bridge::peg::federation_redeem_for_format_pub( + &keys, + format, + &bridge.constants, + &bridge.cfg, + bridge.number(), + ); + Self { + signers, + keys, + format, + redeem, + } + } + + fn segwit(&self) -> bool { + self.format == P2SH_P2WSH_ERP + } + + fn threshold(&self) -> usize { + self.keys.len() / 2 + 1 + } + + /// The output script of its address. + fn output(&self) -> Vec { + p2sh(&crate::bridge::peg::federation_output_hash160( + &self.redeem, + self.format, + )) + } + + /// rskj `BitcoinUtils.addSpendingFederationBaseScript`: input `index` of + /// `tx` as the Bridge leaves it, spending this federation's output with + /// no signature yet. + fn leave_unsigned(&self, tx: &mut bitcoin::Transaction, index: usize) { + use crate::bridge::release_tx::{placeholder_scriptsig, segwit_base_input}; + if self.segwit() { + let (script_sig, witness) = segwit_base_input(&self.redeem, self.threshold()); + tx.input[index].script_sig = script_sig; + tx.input[index].witness = witness; + } else { + let script_sig = placeholder_scriptsig(&self.redeem, self.threshold()); + tx.input[index].script_sig = bitcoin::ScriptBuf::from_bytes(script_sig); + } + } + + /// What input `index` of `tx`, spending `value` satoshis of this + /// federation's, is signed over: its legacy sighash, or its BIP-143 one. + fn sighash(&self, tx: &bitcoin::Transaction, index: usize, value: u64) -> [u8; 32] { + use crate::bridge::release_tx::{legacy_sighash_all, segwit_sighash_all}; + if self.segwit() { + segwit_sighash_all(tx, index, &self.redeem, value) + } else { + legacy_sighash_all(tx, index, &self.redeem) + } + } +} + +/// `signer`'s DER signature of `sighash`. +fn der_signature(signer: &k256::ecdsa::SigningKey, sighash: &[u8; 32]) -> Vec { + use k256::ecdsa::signature::hazmat::PrehashSigner; + let signature: k256::ecdsa::Signature = signer.sign_prehash(sighash).expect("signed"); + signature.to_der().as_bytes().to_vec() +} + +/// Whether input `index` of `tx` holds `signer`'s signature of `sighash`, as +/// bitcoinj writes it, in its witness or its scriptSig. +fn holds_signature( + tx: &bitcoin::Transaction, + index: usize, + signer: &k256::ecdsa::SigningKey, + sighash: &[u8; 32], +) -> bool { + let written = [der_signature(signer, sighash), vec![0x01]].concat(); + let input = &tx.input[index]; + input.witness.iter().any(|push| push == written.as_slice()) + || input.script_sig.instructions().any(|instruction| { + matches!(instruction, Ok(bitcoin::script::Instruction::PushBytes(bytes)) + if bytes.as_bytes() == written.as_slice()) + }) +} + +/// A transaction waiting for signatures under [`RELEASE_HASH`], spending +/// `spent`'s output of a bitcoin, as rskj's nested tests keep one, and what +/// each of its inputs is signed over. +struct ToSign { + bridge: Bridge, + spent: Seeded, + sighashes: Vec<[u8; 32]>, +} + +impl ToSign { + /// The transaction `paying` the bitcoin of `spent`'s that it spends, of + /// `version`, waiting in `bridge`; with `keep_value`, the bitcoin its + /// input spends kept under its hash, as from RSKIP305 the Bridge keeps it + /// when it builds the transaction. + fn waiting_in( + mut bridge: Bridge, + spent: Seeded, + version: i32, + paying: &[(u64, Vec)], + keep_value: bool, + ) -> Self { + let funding = btc_transaction(vec![], &[(BTC, spent.output())]); + let mut tx = btc_transaction( + vec![bitcoin::TxIn { + previous_output: bitcoin::OutPoint::new(funding.compute_txid(), 0), + ..bitcoin::TxIn::default() + }], + paying, + ); + tx.version = bitcoin::transaction::Version(version); + spent.leave_unsigned(&mut tx, 0); + use crate::bridge::serialization::{rlp_encode_element, rlp_encode_list}; + put_named( + &mut bridge.ctx, + "rskTxsWaitingFS", + &rlp_encode_list(&[ + rlp_encode_element(&RELEASE_HASH), + rlp_encode_element(&bitcoin::consensus::serialize(&tx)), + ]), + ); + if keep_value { + use bitcoin::hashes::Hash; + let mut hash = tx.compute_txid().to_raw_hash().to_byte_array(); + hash.reverse(); + let hash = crate::bridge::tx::to_hex(&hash); + let key = crate::bridge::storage::compound_key("releasesOutpointsValues", "-", &hash); + // One bitcoin as bitcoinj's `VarInt` writes it. + let value = vec![0xfe, 0x00, 0xe1, 0xf5, 0x05]; + crate::bridge::storage::bridge_store_raw(&mut bridge.ctx, key, Some(value)); + } + let sighashes = vec![spent.sighash(&tx, 0, BTC)]; + Self { + bridge, + spent, + sighashes, + } + } + + /// rskj's nested `AddSignature` set-up, at block `number` of mainnet: a + /// federation of the signers `signer1` to `signer` active in + /// `format`, and waiting for its signatures a version 2 peg-out of its + /// bitcoin, the minimum to the receiver and the rest back to it. + fn pegout(number: u64, count: usize, format: u64, keep_value: bool) -> Self { + let mut bridge = Bridge::mainnet_at(number); + let seeds: Vec = (1..=count).map(|i| format!("signer{i}")).collect(); + let federation = Seeded::of(&seeds, format, &bridge); + store_federation(&mut bridge.ctx, NEW_FEDERATION, &federation.keys, 1, format); + let minimum = bridge.constants.minimum_pegout_tx_value; + let paying = [(minimum, user(9)), (BTC - minimum, federation.output())]; + Self::waiting_in(bridge, federation, 2, &paying, keep_value) + } + + /// At block `number` of mainnet, the SVP under way: a proposed + /// P2SH-P2WSH ERP federation of twenty, made the block before, and its + /// SVP spend transaction, a version 2 one of its bitcoin, waiting for its + /// signatures (`svpSpendTxWaitingForSignatures`). + fn svp_spend(number: u64, keep_value: bool) -> Self { + let mut bridge = Bridge::mainnet_at(number); + let seeds: Vec = (1..=20).map(|i| format!("signer{i}")).collect(); + let proposed = Seeded::of(&seeds, P2SH_P2WSH_ERP, &bridge); + let slot = ("proposedFederation", "proposedFederationFormatVersion"); + store_federation( + &mut bridge.ctx, + slot, + &proposed.keys, + number - 1, + P2SH_P2WSH_ERP, + ); + let mut to_sign = Self::waiting_in(bridge, proposed, 2, &[(BTC, user(9))], keep_value); + let entry = named(&mut to_sign.bridge.ctx, "rskTxsWaitingFS").expect("waiting"); + put_named( + &mut to_sign.bridge.ctx, + "svpSpendTxWaitingForSignatures", + &entry, + ); + to_sign + } + + /// `addSignature` by the member `signer` of `federation`, its signatures of + /// [`ToSign::sighashes`], from its transaction. + fn add_signature_by( + &mut self, + signer: &k256::ecdsa::SigningKey, + ) -> Result { + use crate::abi::encode::{args, Arg}; + let key = compressed(signer); + self.bridge.caller = + crate::bridge::federation::rsk_address_from_public_key(&key).expect("an address"); + let signatures: Vec> = self + .sighashes + .iter() + .map(|sighash| der_signature(signer, sighash)) + .collect(); + let sent: Vec<&[u8]> = signatures.iter().map(Vec::as_slice).collect(); + let data = [ + crate::abi::selector("addSignature(bytes,bytes[],bytes)").to_vec(), + args(&[ + Arg::Bytes(&key), + Arg::BytesArray(&sent), + Arg::Bytes(&RELEASE_HASH), + ]), + ] + .concat(); + self.bridge.execute(&data) + } + + /// The first `count` signers of the spent federation sign, each in a call + /// that returns. + fn signed_by_the_first(&mut self, count: usize) { + for at in 0..count { + let signer = self.spent.signers[at].clone(); + self.add_signature_by(&signer).expect("addSignature runs"); + } + } + + /// The transactions still waiting, by their creation hash. + fn still_waiting(&mut self) -> std::collections::BTreeMap<[u8; 32], Vec> { + crate::bridge::peg::deserialize_rsk_txs_waiting_for_signatures( + &named(&mut self.bridge.ctx, "rskTxsWaitingFS").unwrap_or_default(), + ) + .into_iter() + .collect() + } + + /// The transaction as it waits. + fn waiting(&mut self) -> bitcoin::Transaction { + let raw = self.still_waiting().remove(&RELEASE_HASH).expect("waiting"); + bitcoin::consensus::deserialize(&raw).expect("a transaction") + } + + /// rskj's `assertFederatorSigning` for the first `count` signers and + /// `assertLogReleaseBtc`: an `add_signature` event for each, by its + /// address, then `release_btc`, of the transaction holding each one's + /// signature, which no longer waits. The transaction released. + fn released_signed_by_the_first(&mut self, count: usize) -> bitcoin::Transaction { + assert!(self.still_waiting().is_empty(), "no longer waiting"); + let logs = logs(&mut self.bridge.ctx); + let add = event_topic("add_signature(bytes32,address,bytes)"); + let release = event_topic("release_btc(bytes32,bytes)"); + let firsts: Vec<[u8; 32]> = logs.iter().map(|log| topics(log)[0]).collect(); + assert_eq!(firsts, [vec![add; count], vec![release]].concat()); + let released: bitcoin::Transaction = + bitcoin::consensus::deserialize(&released(&logs[count])).expect("a transaction"); + for (signer, log) in self.spent.signers[..count].iter().zip(&logs) { + let address = + crate::bridge::federation::rsk_address_from_public_key(&compressed(signer)) + .expect("an address"); + assert_eq!(topics(log)[2][12..], address.0); + assert!(holds_signature(&released, 0, signer, &self.sighashes[0])); + } + released + } +} + +/// rskj's `AddSignature.addSignature_correctFederator_whenFedIsSegwit_shouldSign`: +/// a majority of a P2SH-P2WSH ERP federation of twenty signs its peg-out, each +/// signature over the input's BIP-143 sighash with the value kept for it, in +/// the input's witness; the peg-out is released. +#[test] +fn a_majority_of_a_segwit_federation_signs_its_pegout_and_it_is_released() { + let mut to_sign = ToSign::pegout(MAINNET_VETIVER900, 20, P2SH_P2WSH_ERP, true); + let majority = to_sign.spent.threshold(); + to_sign.signed_by_the_first(majority); + let released = to_sign.released_signed_by_the_first(majority); + assert!( + !released.input[0].script_sig.is_empty(), + "the witness program pushed" + ); +} + +/// rskj's `AddSignature.addSignature_correctFederator_whenFedIsSegwit_txFullySigned_shouldNotAddExtraSignature` +/// and `..._whenFedIsLegacy_txFullySigned_shouldNotAddExtraSignature`: once a +/// peg-out of a P2SH-P2WSH ERP federation of twenty, or of a P2SH ERP one of +/// nine, is released, another member's signatures change nothing. +#[test] +fn a_released_pegout_takes_no_more_signatures() { + for (count, format, keep_value) in [(20, P2SH_P2WSH_ERP, true), (9, P2SH_ERP, false)] { + let mut to_sign = ToSign::pegout(MAINNET_VETIVER900, count, format, keep_value); + let majority = to_sign.spent.threshold(); + to_sign.signed_by_the_first(majority); + let released = to_sign.released_signed_by_the_first(majority); + let extra = to_sign.spent.signers[majority + 1].clone(); + to_sign.add_signature_by(&extra).expect("addSignature runs"); + assert!(to_sign.still_waiting().is_empty(), "{format}"); + assert!(logs(&mut to_sign.bridge.ctx).is_empty(), "{format}"); + assert!( + !holds_signature(&released, 0, &extra, &to_sign.sighashes[0]), + "{format}" + ); + } +} + +/// rskj reads the value a segwit input spends, which its sighash covers, +/// with `getReleaseOutpointsValues`: before RSKIP305 it throws, and from it +/// it finds nothing where nothing is kept under the transaction's hash, and +/// the call throws. A member's signature of a peg-out with a witness fails +/// the call before RSKIP305 whatever is kept, and from it where nothing is, +/// and is signed in where the value is kept. +#[test] +fn a_segwit_input_is_signed_only_from_rskip305_and_with_its_value_kept() { + let first_signs = |to_sign: &mut ToSign| { + let first = to_sign.spent.signers[0].clone(); + to_sign.add_signature_by(&first).map(|_| ()) + }; + let mut before = ToSign::pegout(MAINNET_REED800 - 1, 20, P2SH_P2WSH_ERP, true); + assert!(first_signs(&mut before).is_err()); + + let mut nothing_kept = ToSign::pegout(MAINNET_REED800, 20, P2SH_P2WSH_ERP, false); + assert!(first_signs(&mut nothing_kept).is_err()); + + let mut kept = ToSign::pegout(MAINNET_REED800, 20, P2SH_P2WSH_ERP, true); + first_signs(&mut kept).expect("addSignature runs"); + let signer = kept.spent.signers[0].clone(); + let sighash = kept.sighashes[0]; + assert!(holds_signature(&kept.waiting(), 0, &signer, &sighash)); +} + +/// rskj signs the SVP spend transaction as a peg-out (`addSvpSpendTxSignatures` +/// runs `processSigning`): a proposed member's signature of its segwit input +/// fails the call before RSKIP305, and from it where nothing is kept under +/// its hash, and is signed in where the value is kept. +#[test] +fn the_svp_spend_transaction_is_signed_only_from_rskip305_and_with_its_value_kept() { + for (number, keep_value, signed) in [ + (MAINNET_REED800 - 1, true, false), + (MAINNET_REED800, false, false), + (MAINNET_REED800, true, true), + ] { + let mut to_sign = ToSign::svp_spend(number, keep_value); + let first = to_sign.spent.signers[0].clone(); + let answered = to_sign.add_signature_by(&first); + assert_eq!(answered.is_ok(), signed, "{number} {keep_value}"); + if signed { + use crate::bridge::serialization::rlp_decode_list; + let entry = named(&mut to_sign.bridge.ctx, "svpSpendTxWaitingForSignatures"); + let items = rlp_decode_list(&entry.expect("waiting")).expect("a list"); + let spend: bitcoin::Transaction = + bitcoin::consensus::deserialize(&items[1]).expect("a transaction"); + assert!(holds_signature(&spend, 0, &first, &to_sign.sighashes[0])); + } + } +} + +/// rskj's `addSvpSpendTxSignatures` looks the signer up among the proposed +/// federation's members and throws where it is none of them +/// (`getMemberByBtcPublicKey(..).orElseThrow`): a call to sign the SVP spend +/// transaction with another key fails, though a member sends it, and the +/// transaction waits as it was. +#[test] +fn a_key_of_no_proposed_member_fails_a_call_to_sign_the_svp_spend_transaction() { + use crate::abi::encode::{args, Arg}; + let mut to_sign = ToSign::svp_spend(MAINNET_REED800, true); + let waiting = named(&mut to_sign.bridge.ctx, "svpSpendTxWaitingForSignatures"); + let member = compressed(&to_sign.spent.signers[0]); + to_sign.bridge.caller = + crate::bridge::federation::rsk_address_from_public_key(&member).expect("an address"); + let stranger = k256::ecdsa::SigningKey::from_slice(&[7; 32]).expect("a key"); + let signature = der_signature(&stranger, &to_sign.sighashes[0]); + let data = [ + crate::abi::selector("addSignature(bytes,bytes[],bytes)").to_vec(), + args(&[ + Arg::Bytes(&compressed(&stranger)), + Arg::BytesArray(&[signature.as_slice()]), + Arg::Bytes(&RELEASE_HASH), + ]), + ] + .concat(); + assert!(to_sign.bridge.execute(&data).is_err(), "the call fails"); + assert_eq!( + named(&mut to_sign.bridge.ctx, "svpSpendTxWaitingForSignatures"), + waiting + ); + assert!(logs(&mut to_sign.bridge.ctx).is_empty()); +} + +/// A key in the two other forms Bouncy Castle reads: uncompressed, and +/// hybrid, whose first byte is 06 or 07 as its y is even or odd. +fn other_forms(key: &[u8; 33]) -> [Vec; 2] { + let uncompressed = bitcoin::secp256k1::PublicKey::from_slice(key) + .expect("a key") + .serialize_uncompressed(); + let mut hybrid = uncompressed; + hybrid[0] = 0x06 | (uncompressed[64] & 1); + [uncompressed.to_vec(), hybrid.to_vec()] +} + +/// rskj finds the federation a peg-out is signed for by +/// `Federation.hasBtcPublicKey`, which compares the key's bytes as the call +/// gave them with each member's compressed key: a member that gives its key +/// uncompressed or hybrid is no member's, and the call returns without +/// signing, and without the event rskj logs before signing until RSKIP326. +#[test] +fn a_peg_out_takes_no_signature_under_a_key_given_uncompressed_or_hybrid() { + for number in [BEFORE_RSKIP326, FROM_RSKIP326] { + for form in 0..2 { + let mut signing = Signing::mainnet_at(number); + let given = other_forms(&signing.key(0))[form].clone(); + let waiting = named(&mut signing.bridge.ctx, "rskTxsWaitingFS"); + let signature = signing.signature(0); + signing + .add_signatures_as(0, &given, &[signature]) + .expect("addSignature returns"); + let what = format!("block {number}, key {:02x}", given[0]); + assert_eq!( + named(&mut signing.bridge.ctx, "rskTxsWaitingFS"), + waiting, + "{what}" + ); + assert!(logs(&mut signing.bridge.ctx).is_empty(), "{what}"); + } + } +} + +/// To sign the SVP spend transaction rskj finds the proposed member by +/// `getMemberByBtcPublicKey`, which reads the key again and compares the +/// points: a member that gives its key uncompressed or hybrid is found, signs +/// in, and is logged with its key as the federation keeps it, compressed +/// (`BridgeEventLoggerImpl.logAddSignature`). +#[test] +fn the_svp_spend_transaction_takes_a_signature_under_a_key_given_uncompressed_or_hybrid() { + use crate::abi::encode::{args, Arg}; + for form in 0..2 { + let mut to_sign = ToSign::svp_spend(MAINNET_REED800, true); + let signer = to_sign.spent.signers[0].clone(); + let key = compressed(&signer); + let given = other_forms(&key)[form].clone(); + to_sign.bridge.caller = + crate::bridge::federation::rsk_address_from_public_key(&key).expect("an address"); + let signature = der_signature(&signer, &to_sign.sighashes[0]); + let data = [ + crate::abi::selector("addSignature(bytes,bytes[],bytes)").to_vec(), + args(&[ + Arg::Bytes(&given), + Arg::BytesArray(&[signature.as_slice()]), + Arg::Bytes(&RELEASE_HASH), + ]), + ] + .concat(); + to_sign.bridge.execute(&data).expect("addSignature runs"); + let logs = crate::rskj_tests::bridge_pegouts::support::logs(&mut to_sign.bridge.ctx); + assert_eq!(logs.len(), 1, "signed in, and logged: key {:02x}", given[0]); + assert_eq!(logs[0].data.data.to_vec(), args(&[Arg::Bytes(&key)])); + } +} + +/// rskj's nested `MigrationTxSigning` set-up, at vetiver900: a retiring +/// federation in `retiring_format` and an active one in `active_format`, of +/// nine members where P2SH ERP and twenty where P2SH-P2WSH ERP, sharing +/// `sharedFederator`, the retiring one with `retiringFederator` the active +/// one hasn't; and waiting for signatures the migration of the retiring +/// one's bitcoin to the active one, the bitcoin kept under its hash where +/// the retiring one is P2SH-P2WSH. The shared member's key and the other's. +fn migrating(retiring_format: u64, active_format: u64) -> (ToSign, [u8; 33], [u8; 33]) { + let number = MAINNET_VETIVER900; + let mut bridge = Bridge::mainnet_at(number); + let seeds = |second: &str, format: u64| -> Vec { + let count = if format == P2SH_P2WSH_ERP { 20 } else { 9 }; + ["sharedFederator".to_string(), second.to_string()] + .into_iter() + .chain((3..=count).map(|i| format!("signer{i}"))) + .collect() + }; + let retiring = Seeded::of( + &seeds("retiringFederator", retiring_format), + retiring_format, + &bridge, + ); + let active = Seeded::of( + &seeds("activeFederator", active_format), + active_format, + &bridge, + ); + store_federation( + &mut bridge.ctx, + OLD_FEDERATION, + &retiring.keys, + 1, + retiring_format, + ); + let creation = number - activation_age(number) - 1; + store_federation( + &mut bridge.ctx, + NEW_FEDERATION, + &active.keys, + creation, + active_format, + ); + let key_of = |seed: &str| { + use sha3::Digest; + let private = sha3::Keccak256::digest(seed.as_bytes()); + compressed(&k256::ecdsa::SigningKey::from_slice(&private).expect("a key")) + }; + let (shared, only_retiring) = (key_of("sharedFederator"), key_of("retiringFederator")); + assert!(retiring.keys.contains(&shared) && active.keys.contains(&shared)); + assert!(retiring.keys.contains(&only_retiring) && !active.keys.contains(&only_retiring)); + let paying = [(BTC, active.output())]; + let keep_value = retiring_format == P2SH_P2WSH_ERP; + let to_sign = ToSign::waiting_in(bridge, retiring, 1, &paying, keep_value); + (to_sign, shared, only_retiring) +} + +/// rskj's `MigrationTxSigning.addSignature_migrationTx_*_withOneSharedFederatorAndOneOnlyPartOfRetiringFed`, +/// for the retiring and active federations P2SH ERP and P2SH ERP, P2SH ERP +/// and P2SH-P2WSH ERP, and P2SH-P2WSH ERP and P2SH-P2WSH ERP: a member of +/// both federations signs the migration in, then a member of the retiring +/// one alone, each signature logged. +#[test] +fn members_of_the_retiring_federation_sign_its_migration() { + for (retiring, active) in [ + (P2SH_ERP, P2SH_ERP), + (P2SH_ERP, P2SH_P2WSH_ERP), + (P2SH_P2WSH_ERP, P2SH_P2WSH_ERP), + ] { + let (mut to_sign, shared, only_retiring) = migrating(retiring, active); + for key in [shared, only_retiring] { + let signer = to_sign + .spent + .signers + .iter() + .find(|signer| compressed(signer) == key) + .expect("a retiring member") + .clone(); + to_sign + .add_signature_by(&signer) + .expect("addSignature runs"); + let sighash = to_sign.sighashes[0]; + assert!( + holds_signature(&to_sign.waiting(), 0, &signer, &sighash), + "{retiring} {active}" + ); + let add = event_topic("add_signature(bytes32,address,bytes)"); + let logs = logs(&mut to_sign.bridge.ctx); + let firsts: Vec<[u8; 32]> = logs.iter().map(|log| topics(log)[0]).collect(); + assert_eq!(firsts, [add], "{retiring} {active}"); + } + } +} diff --git a/crates/execution/src/rskj_tests/bridge_pegouts/bridge_support_get_estimated_fees_test.rs b/crates/execution/src/rskj_tests/bridge_pegouts/bridge_support_get_estimated_fees_test.rs new file mode 100644 index 0000000..4bf3703 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegouts/bridge_support_get_estimated_fees_test.rs @@ -0,0 +1,328 @@ +//! rskj's `BridgeSupportGetEstimatedFeesTest`: what +//! `getEstimatedFeesForNextPegOutEvent` answers, in satoshis, under each +//! era's rules. Before RSKIP271 (hop400), and before RSKIP385 +//! (fingerroot500) with no request queued, nothing. Before RSKIP305 (reed800) +//! the fee of a peg-out of two inputs and an output for each request queued +//! and two more, sized by the active federation's format. From it the fee of +//! a batch built of the requests queued and one more, of a bitcoin before +//! RSKIP540 (vetiver900) and of the minimum peg-out value from it, or, where +//! that batch can't be built, the former. +//! +//! rskj's tests store the active federation, its UTXOs and a fee per +//! kilobyte of 8,000 satoshis under each era's activations; the ports store +//! the same in mainnet's Bridge at the block of that era's upgrade. The +//! federations are rskj's test builders': the nine members `member01` to +//! `member09`, standard multisig or P2SH ERP, or twenty P2SH-P2WSH ERP, the +//! ERP ones with mainnet's emergency keys and delay, made at block 1. + +use alloy_primitives::U256; + +use crate::bridge::peg; + +use super::support::*; + +/// rskj's `FEE_PER_KB`. +const FEE_PER_KB: u64 = 8_000; + +/// Eight and ten bitcoins. +const EIGHT: u64 = 8 * BTC; +const TEN: u64 = 10 * BTC; + +/// Mainnet's minimum peg-out value, rskj's `MIN_PEGOUT_TX_VALUE`. +const MIN: u64 = 400_000; + +/// rskj `BitcoinTestUtils.getBtcEcKeyFromSeed(seed)`'s public key. +fn key_of(seed: &str) -> [u8; 33] { + use sha3::Digest; + let private = sha3::Keccak256::digest(seed.as_bytes()); + k256::ecdsa::SigningKey::from_slice(&private) + .expect("a key") + .verifying_key() + .to_encoded_point(true) + .as_bytes() + .try_into() + .expect("33 bytes") +} + +/// The members of rskj's builder for `format`, sorted: nine, or twenty for +/// P2SH-P2WSH ERP. +fn members(format: u64) -> Vec<[u8; 33]> { + let count = if format == P2SH_P2WSH_ERP { 20 } else { 9 }; + let mut keys: Vec<[u8; 33]> = (1..=count) + .map(|n| key_of(&format!("member{n:02}"))) + .collect(); + keys.sort(); + keys +} + +/// `value` as rskj's `serializeCoin` writes it. +fn coin(value: u64) -> Vec { + let bytes = value.to_be_bytes(); + let first = bytes.iter().position(|&b| b != 0).unwrap_or(bytes.len()); + rlp_encode_element(&bytes[first..]) +} + +/// One of rskj's cases: its test, the era's upgrade block, the federation's +/// format, the values of its UTXOs, the requests queued, each group `count` +/// requests of `amount` (`addPegoutRequests`), and the fee per kilobyte. +struct Case { + test: &'static str, + number: u64, + format: u64, + utxos: &'static [u64], + requests: &'static [(usize, u64)], + fee_per_kb: u64, +} + +impl Case { + /// What `getEstimatedFeesForNextPegOutEvent` answers for this case. + fn estimated(&self) -> u64 { + let mut bridge = Bridge::mainnet_at(self.number); + put_named(&mut bridge.ctx, "feePerKb", &coin(self.fee_per_kb)); + let keys = members(self.format); + store_federation(&mut bridge.ctx, NEW_FEDERATION, &keys, 1, self.format); + let redeem = peg::federation_redeem_for_format_pub( + &keys, + self.format, + &bridge.constants, + &bridge.cfg, + 1, + ); + let script = p2sh(&peg::federation_output_hash160(&redeem, self.format)); + let utxos: Vec<_> = self + .utxos + .iter() + .zip(1u8..) + .map(|(&value, tag)| utxo(tag, value, script.clone())) + .collect(); + let number = bridge.number(); + crate::bridge::storage::store_federation_utxos( + &mut bridge.ctx, + &bridge.cfg, + number, + &utxos, + ); + let queued: Vec<([u8; 20], u64)> = self + .requests + .iter() + .flat_map(|&(count, amount)| { + (0..count).map(move |i| (hash160(&key_of(&format!("receiver{i}"))), amount)) + }) + .collect(); + put_named( + &mut bridge.ctx, + "releaseRequestQueue", + &legacy_queue(&queued), + ); + let out = peg::get_estimated_fees_for_next_pegout( + &mut bridge.ctx, + 0, + &bridge.constants, + &bridge.cfg, + ) + .expect("it answers"); + U256::from_be_slice(&out.bytes).to::() + } +} + +/// Runs each case and compares its answer with rskj's. +fn assert_estimates(cases: &[(Case, u64)]) { + for (case, expected) in cases { + assert_eq!(case.estimated(), *expected, "{}", case.test); + } +} + +/// A case at `number` with the fee per kilobyte of rskj's tests. +const fn case( + test: &'static str, + number: u64, + format: u64, + utxos: &'static [u64], + requests: &'static [(usize, u64)], +) -> Case { + Case { + test, + number, + format, + utxos, + requests, + fee_per_kb: FEE_PER_KB, + } +} + +/// The same case at twice the fee per kilobyte. +const fn doubled(case: Case) -> Case { + Case { + fee_per_kb: 2 * FEE_PER_KB, + ..case + } +} + +/// `PreHop400Activation.getEstimatedFeesForNextPegOutEvent_preHop_shouldReturnZeroFees`, +/// for none, one and 150 requests: before RSKIP271 nothing is estimated. +#[test] +fn before_rskip271_no_fee_is_estimated() { + let utxos = &[EIGHT, EIGHT, EIGHT]; + let at = MAINNET_IRIS300; + let test = "preHop_shouldReturnZeroFees"; + assert_estimates(&[ + (case(test, at, STANDARD_MULTISIG, utxos, &[]), 0), + (case(test, at, STANDARD_MULTISIG, utxos, &[(1, BTC)]), 0), + (case(test, at, STANDARD_MULTISIG, utxos, &[(150, BTC)]), 0), + ]); +} + +/// `PostHop400PreFingerrootActivations`: from RSKIP271 and before RSKIP385, +/// nothing with no request queued, and the fee of two inputs and an output +/// for each request and two more with some. +#[test] +fn from_rskip271_the_fee_is_estimated_only_with_a_request_queued() { + let at = MAINNET_HOP400; + let three = &[EIGHT, EIGHT, EIGHT]; + let one = &[EIGHT]; + assert_estimates(&[ + ( + case("withStandardMultisigFederation_withNoPegoutRequests_shouldEstimateZeroFees", at, STANDARD_MULTISIG, three, &[]), + 0, + ), + ( + case("withStandardMultisigFederation_withOnePegoutRequest_shouldEstimateFeesFromInputAndOutputCount", at, STANDARD_MULTISIG, three, &[(1, BTC)]), + 12_240, + ), + ( + case("withStandardMultisigFederation_withManyPegoutRequests_shouldEstimateFeesFromInputAndOutputCount", at, STANDARD_MULTISIG, three, &[(150, BTC)]), + 50_384, + ), + ( + doubled(case("withStandardMultisigFederation_withManyPegoutRequests_shouldEstimateFeesFromInputAndOutputCount, doubled", at, STANDARD_MULTISIG, three, &[(150, BTC)])), + 100_768, + ), + ( + case("withP2shErpFederation_withNoPegoutRequests_shouldEstimateZeroFees", at, P2SH_ERP, one, &[]), + 0, + ), + ( + case("withP2shErpFederation_withOnePegoutRequest_shouldEstimateFeesFromInputAndOutputCount", at, P2SH_ERP, one, &[(1, BTC)]), + 14_608, + ), + ( + case("withP2shErpFederation_withManyPegoutRequests_shouldEstimateFeesFromInputAndOutputCount", at, P2SH_ERP, one, &[(150, BTC)]), + 52_752, + ), + ( + doubled(case("withP2shErpFederation_withManyPegoutRequests_shouldEstimateFeesFromInputAndOutputCount, doubled", at, P2SH_ERP, one, &[(150, BTC)])), + 105_504, + ), + ]); +} + +/// `PostFingerrootPreReedActivations`: from RSKIP385, the fee of two inputs +/// and an output for each request and two more, with no request queued too. +#[test] +fn from_rskip385_the_fee_is_estimated_with_no_request_queued_too() { + let at = MAINNET_FINGERROOT500; + let one = &[EIGHT]; + assert_estimates(&[ + ( + case("withP2shErpFederation_withNoPegoutRequests_shouldEstimateFeesFromInputAndOutputCount", at, P2SH_ERP, one, &[]), + 14_352, + ), + ( + case("withP2shErpFederation_withOnePegoutRequest_shouldEstimateFeesFromInputAndOutputCount", at, P2SH_ERP, one, &[(1, BTC)]), + 14_608, + ), + ( + case("withP2shErpFederation_withManyPegoutRequests_shouldEstimateFeesFromInputAndOutputCount", at, P2SH_ERP, one, &[(150, BTC)]), + 52_752, + ), + ( + doubled(case("withP2shErpFederation_withManyPegoutRequests_shouldEstimateFeesFromInputAndOutputCount, doubled", at, P2SH_ERP, one, &[(150, BTC)])), + 105_504, + ), + ]); +} + +/// The cases rskj runs from RSKIP305 both before RSKIP540 +/// (`PostReedPreVetiverActivations`) and from it (`PostVetiverActivation`): +/// each a P2SH-P2WSH ERP federation's, and what it is estimated before +/// RSKIP540 and from it. +fn from_rskip305(at: u64, from_rskip540: bool) -> Vec<(Case, u64)> { + let f = P2SH_P2WSH_ERP; + let two_of_eight = &[EIGHT, EIGHT]; + let four_of_one = &[BTC, BTC, BTC, BTC]; + let two_of_one = &[BTC, BTC]; + let one_and_min = &[BTC, MIN]; + let either = |before: u64, from: u64| if from_rskip540 { from } else { before }; + let mut cases = vec![ + ( + case("withP2shP2wshErpFederation_withNoPegoutRequests_shouldEstimateFeesFromTransactionSimulation", at, f, two_of_eight, &[]), + 4_536, + ), + ( + case("withP2shP2wshErpFederation_withNoUtxos_withNoPegoutRequests_shouldFallBackToEstimateFeesFromInputAndOutputCount", at, f, &[], &[]), + 7_664, + ), + ( + case("withP2shP2wshErpFederation_withNoUtxos_withOnePegoutRequest_shouldFallBackToEstimateFeesFromInputAndOutputCount", at, f, &[], &[(1, BTC)]), + 7_920, + ), + ( + case("withP2shP2wshErpFederation_withMinPegoutValueRequests_shouldEstimateFeesFromTransactionSimulation, 1", at, f, four_of_one, &[(1, MIN)]), + either(8_736, 4_808), + ), + ( + case("withP2shP2wshErpFederation_withMinPegoutValueRequests_shouldEstimateFeesFromTransactionSimulation, 2", at, f, four_of_one, &[(2, MIN)]), + either(9_008, 5_080), + ), + ( + case("withP2shP2wshErpFederation_withMinPegoutValueRequests_shouldEstimateFeesFromTransactionSimulation, 3", at, f, four_of_one, &[(3, MIN)]), + either(9_280, 5_352), + ), + ( + case("withP2shP2wshErpFederation_withTwoBTCs_withPegoutRequestOfOneBtc", at, f, two_of_one, &[(1, BTC)]), + either(8_480, 8_736), + ), + ( + case("withP2shP2wshErpFederation_withTwoBTCs_withPegoutRequestGreaterThanOneBtc", at, f, two_of_one, &[(1, BTC + 1)]), + either(7_920, 8_736), + ), + ( + case("withP2shP2wshErpFederation_withTwoUtxos_withTwoPegoutRequests_shouldEstimateFeesFromTransactionSimulation", at, f, two_of_eight, &[(1, BTC + 1), (1, TEN)]), + 9_008, + ), + ( + case("withP2shP2wshErpFederation_withTwoUtxos_withThreePegoutRequests_shouldEstimateFeesFromTransactionSimulation", at, f, two_of_eight, &[(2, BTC), (1, TEN)]), + 9_280, + ), + ( + doubled(case("withP2shP2wshErpFederation_withTwoUtxos_withThreePegoutRequests_shouldEstimateFeesFromTransactionSimulation, doubled", at, f, two_of_eight, &[(2, BTC), (1, TEN)])), + 18_560, + ), + ]; + if from_rskip540 { + cases.push(( + case("withP2shP2wshErpFederationHavingOneBTCAndMinPegoutValue_withOneBTCPegoutRequest_shouldEstimateFeesFromTransactionSimulation", at, f, one_and_min, &[(1, BTC)]), + 8_480, + )); + cases.push(( + case("withP2shP2wshErpFederationHavingOneBTCAndMinPegoutValue_withPegoutRequestGreaterThanOneBtc_shouldFallBackToEstimateFeesFromInputAndOutputCount", at, f, one_and_min, &[(1, BTC + 1)]), + 7_920, + )); + } + cases +} + +/// `PostReedPreVetiverActivations`: from RSKIP305 the fee of a batch built of +/// the requests and one more of a bitcoin, or, where it can't be built, of +/// two inputs and an output for each request and two more, sized as segwit. +#[test] +fn from_rskip305_the_fee_is_that_of_a_batch_with_one_more_bitcoin() { + assert_estimates(&from_rskip305(MAINNET_REED800, false)); +} + +/// `PostVetiverActivation`: from RSKIP540 the request added is of the minimum +/// peg-out value. +#[test] +fn from_rskip540_the_batch_has_one_more_minimum_pegout() { + assert_estimates(&from_rskip305(MAINNET_VETIVER900, true)); +} diff --git a/crates/execution/src/rskj_tests/bridge_pegouts/bridge_support_pegout_transaction_created_event_test.rs b/crates/execution/src/rskj_tests/bridge_pegouts/bridge_support_pegout_transaction_created_event_test.rs new file mode 100644 index 0000000..344e7e9 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegouts/bridge_support_pegout_transaction_created_event_test.rs @@ -0,0 +1,118 @@ +//! rskj's `BridgeSupportPegoutTransactionCreatedEventTest`: from RSKIP428 +//! (lovell700) each peg-out `updateCollections` builds, a batch or a +//! migration, is logged as `pegout_transaction_created`, its hash a topic and +//! its data the values of the outputs it spends, each a Bitcoin compact size +//! (`UtxoUtils.encodeOutpointValues`). rskj's tests run under arrowhead600 +//! and lovell700; the ports run mainnet's Bridge at a block of each, its +//! UTXOs a bitcoin each. + +use super::support::*; + +const ERAS: [(u64, bool); 2] = [(MAINNET_ARROWHEAD600, false), (MAINNET_LOVELL700, true)]; + +/// A bitcoin as a Bitcoin compact size. +const A_BITCOIN: [u8; 5] = [0xfe, 0x00, 0xe1, 0xf5, 0x05]; + +/// The `pegout_transaction_created` events among `logged`: the hash topic, +/// and the bytes of the data. +fn created(logged: &[revm::primitives::Log]) -> Vec<([u8; 32], Vec)> { + let topic = event_topic("pegout_transaction_created(bytes32,bytes)"); + logged + .iter() + .filter(|log| topics(log)[0] == topic) + .map(|log| { + let data = &log.data.data; + let length = word(data, 1).to::(); + (topics(log)[1], data[64..64 + length].to_vec()) + }) + .collect() +} + +/// What rskj logs of `tx`: its hash as bitcoinj's `Sha256Hash` holds it, +/// and the value of each output it spends. +fn event_of(tx: &bitcoin::Transaction) -> ([u8; 32], Vec) { + (reversed(txid(tx)), A_BITCOIN.repeat(tx.input.len())) +} + +/// How many logs among `logged` are of the event `signature`. +fn count(logged: &[revm::primitives::Log], signature: &str) -> usize { + let topic = event_topic(signature); + logged.iter().filter(|log| topics(log)[0] == topic).count() +} + +/// rskj's `updateCollections_whenPegoutBatchIsCreated_shouldLogPegoutTransactionCreatedEvent`: +/// three requests make a batch, logged as `batch_pegout_created` and +/// `release_requested`, and from RSKIP428 as `pegout_transaction_created`. +#[test] +fn from_rskip428_a_pegout_batch_built_is_logged_with_the_values_it_spends() { + for (number, rskip428) in ERAS { + let mut collections = Collections::mainnet_at(number); + collections.active(1, 10); + collections.requests(3); + let logged = collections.update(); + let waiting = collections.waiting(); + assert_eq!(waiting.len(), 1, "{number}"); + assert_eq!( + count(&logged, "batch_pegout_created(bytes32,bytes)"), + 1, + "{number}" + ); + assert_eq!( + count(&logged, "release_requested(bytes32,bytes32,uint256)"), + 1, + "{number}" + ); + let expected = if rskip428 { + vec![event_of(&waiting[0])] + } else { + vec![] + }; + assert_eq!(created(&logged), expected, "{number}"); + } +} + +/// rskj's `updateCollections_whenPegoutMigrationIsCreated_shouldLogPegoutTransactionCreatedEvent`: +/// a migration, logged as `release_requested`, and from RSKIP428 as +/// `pegout_transaction_created`. +#[test] +fn from_rskip428_a_migration_built_is_logged_with_the_values_it_spends() { + for (number, rskip428) in ERAS { + let mut collections = Collections::migrating(number, STANDARD_MULTISIG, 10, false); + let logged = collections.update(); + let waiting = collections.waiting(); + assert_eq!(waiting.len(), 1, "{number}"); + assert_eq!( + count(&logged, "release_requested(bytes32,bytes32,uint256)"), + 1, + "{number}" + ); + let expected = if rskip428 { + vec![event_of(&waiting[0])] + } else { + vec![] + }; + assert_eq!(created(&logged), expected, "{number}"); + } +} + +/// rskj's `updateCollections_whenPegoutMigrationAndBatchAreCreated_shouldLogPegoutTransactionCreatedEvent`: +/// a migration and a batch built in one call, each logged from RSKIP428. +#[test] +fn from_rskip428_a_migration_and_a_pegout_batch_built_together_are_each_logged() { + for (number, rskip428) in ERAS { + let mut collections = Collections::migrating(number, STANDARD_MULTISIG, 10, false); + collections.requests(3); + let logged = collections.update(); + let waiting = collections.waiting(); + assert_eq!(waiting.len(), 2, "{number}"); + let mut expected: Vec<_> = if rskip428 { + waiting.iter().map(event_of).collect() + } else { + vec![] + }; + let mut logged = created(&logged); + expected.sort(); + logged.sort(); + assert_eq!(logged, expected, "{number}"); + } +} diff --git a/crates/execution/src/rskj_tests/bridge_pegouts/bridge_support_process_funds_migration_test.rs b/crates/execution/src/rskj_tests/bridge_pegouts/bridge_support_process_funds_migration_test.rs new file mode 100644 index 0000000..770ffa7 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegouts/bridge_support_process_funds_migration_test.rs @@ -0,0 +1,74 @@ +//! rskj's `BridgeSupportProcessFundsMigrationTest`: here its tests of +//! RSKIP294 (hop400). Before it a migration spends every UTXO of the +//! retiring federation, however many; from it, at most +//! `maxInputsPerPegoutTransaction`. rskj's listed tests run under iris300; +//! the ports run mainnet's Bridge at iris300 and, for the contrast rskj's +//! other tests draw, at hop400, with sixty retiring UTXOs, more than +//! mainnet's fifty inputs. + +use super::support::*; + +const MANY: u8 = 60; + +fn max_inputs() -> usize { + BridgeConstants::mainnet().max_inputs_per_pegout_transaction as usize +} + +/// What is left of the retiring federation's UTXOs. +fn retiring_utxos(collections: &mut Collections) -> usize { + crate::bridge::storage::load_old_federation_utxos(&mut collections.bridge.ctx).len() +} + +/// Whether the retiring federation is still stored. +fn retiring_federation(collections: &mut Collections) -> bool { + named(&mut collections.bridge.ctx, "oldFederation").is_some_and(|stored| !stored.is_empty()) +} + +/// rskj's `StandardMultisigFederationTest.updateCollections_duringMigration_preRSKIP294_withMoreUtxosThanMaxInputs_shouldUseAllRetiringUtxos`: +/// in the migration's window, before RSKIP294 the migration spends all +/// sixty UTXOs and the retiring federation stays; from it, fifty. +#[test] +fn before_rskip294_a_migration_in_its_window_spends_every_retiring_utxo() { + for (number, spent) in [ + (MAINNET_IRIS300, usize::from(MANY)), + (MAINNET_HOP400, max_inputs()), + ] { + let mut collections = Collections::migrating(number, STANDARD_MULTISIG, MANY, false); + collections.update(); + let waiting = collections.waiting(); + assert_eq!(waiting.len(), 1, "{number}"); + assert_eq!(waiting[0].input.len(), spent, "{number}"); + assert_eq!( + retiring_utxos(&mut collections), + usize::from(MANY) - spent, + "{number}" + ); + assert!(retiring_federation(&mut collections), "{number}"); + } +} + +/// rskj's `StandardMultisigFederationTest.updateCollections_pastMigrationAge_preRSKIP294_withMoreUtxosThanMaxInputs_shouldUseAllRetiringUtxos`, +/// and from the rest of the class +/// `updateCollections_pastMigrationAge_withMoreUtxosThanMaxInputs_shouldCreateLastMigrationTxWithMaxInputsAndClearRetiringFedEvenIfUtxosRemain`: +/// past the migration's age the last migration spends all sixty UTXOs +/// before RSKIP294, fifty from it, and the retiring federation is cleared +/// either way. +#[test] +fn before_rskip294_the_last_migration_spends_every_retiring_utxo() { + for (number, spent) in [ + (MAINNET_IRIS300, usize::from(MANY)), + (MAINNET_HOP400, max_inputs()), + ] { + let mut collections = Collections::migrating(number, STANDARD_MULTISIG, MANY, true); + collections.update(); + let waiting = collections.waiting(); + assert_eq!(waiting.len(), 1, "{number}"); + assert_eq!(waiting[0].input.len(), spent, "{number}"); + assert_eq!( + retiring_utxos(&mut collections), + usize::from(MANY) - spent, + "{number}" + ); + assert!(!retiring_federation(&mut collections), "{number}"); + } +} diff --git a/crates/execution/src/rskj_tests/bridge_pegouts/bridge_support_register_btc_transaction_test.rs b/crates/execution/src/rskj_tests/bridge_pegouts/bridge_support_register_btc_transaction_test.rs new file mode 100644 index 0000000..686d64a --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegouts/bridge_support_register_btc_transaction_test.rs @@ -0,0 +1,562 @@ +//! rskj's `BridgeSupportRegisterBtcTransactionTest`: how `registerBtcTransaction` +//! takes a peg-in, a peg-out and a migration, by the legacy rules before +//! RSKIP379 and below the pegout index's start, by the index from RSKIP379 for +//! a Bitcoin block at its start or above, and what it does with a peg-in it +//! turns down, which RSKIP459 marks processed and RSKIP551 no longer does. +//! +//! rskj's tests mock the block store, the storage provider and the event +//! logger, and run under `fingerroot500`, `arrowhead600` and `all` +//! activations. Here mainnet's own Bridge runs at a block of each of those +//! upgrades, and of lovell700, where a refused peg-in is marked processed. Its +//! Bitcoin store holds the transaction's block and the confirmations above it, +//! the pegout index holds what rskj's tests mock it to hold, and the ports read +//! what the call leaves: whether the transaction was marked processed, the +//! events, the UTXOs of each federation, and the refunds. +//! +//! The federations here are standard multisig ones, where rskj's tests use +//! P2SH ERP federations: these rules read a federation's address and its +//! standard redeem script, which for a standard federation are its own. + +use super::support::*; + +/// A peg-in taken: `pegin_btc` of `amount` and protocol 0, the transaction +/// processed, and `utxos` UTXOs to the active federation. +fn assert_taken(outcome: &Outcome, run: Run, amount: u64, utxos: usize) { + assert!(outcome.returned, "{run:?}"); + assert_eq!(outcome.events, vec![Event::PeginBtc(amount, 0)], "{run:?}"); + assert_eq!(outcome.processed_at, Some(run.block), "{run:?}"); + assert_eq!(outcome.active_utxos.len(), utxos, "{run:?}"); + assert!(outcome.retiring_utxos.is_empty(), "{run:?}"); +} + +/// rskj's `assertInvalidPeginIsRejectedWithInvalidAmountReason` by the index +/// and `assertInvalidPeginIsIgnored` by the legacy rules: by the index a +/// peg-in that sends the federation too little is turned down, +/// `rejected_pegin` INVALID_AMOUNT (5) and `unrefundable_pegin` +/// INVALID_AMOUNT (3), and marked processed between RSKIP459 and RSKIP551 +/// (`assertInvalidPeginMarkedAsProcessed`); by the legacy rules it is no +/// peg-in, and is left alone. +fn assert_too_little(outcome: &Outcome, run: Run) { + assert!(outcome.returned, "{run:?}"); + if run.indexed() { + assert_eq!( + outcome.events, + vec![Event::Rejected(5), Event::Unrefundable(3)], + "{run:?}" + ); + assert_eq!( + outcome.processed_at, + run.marks_refused().then_some(run.block), + "{run:?}" + ); + } else { + assert!(outcome.events.is_empty(), "{run:?}"); + assert_eq!(outcome.processed_at, None, "{run:?}"); + } + assert!(outcome.active_utxos.is_empty(), "{run:?}"); + assert!(outcome.retiring_utxos.is_empty(), "{run:?}"); + assert_eq!(outcome.refunds, 0, "{run:?}"); +} + +/// A peg-in rskj can't refund for want of a sender: before RSKIP379 +/// (`assertLegacyUndeterminedSenderPeginIsRejectedAsPeginV1InvalidPayloadBeforeRSKIP379`) +/// `rejected_pegin` PEGIN_V1_INVALID_PAYLOAD (4) whatever the reason, and +/// processed; from it `rejected_pegin` with `reason`, and processed between +/// RSKIP459 and RSKIP551. Either way `unrefundable_pegin` +/// LEGACY_PEGIN_UNDETERMINED_SENDER (1), no UTXO and no refund. +fn assert_not_refundable(outcome: &Outcome, run: Run, reason: u64) { + assert!(outcome.returned, "{run:?}"); + let (rejected, processed) = if run.rskip379() { + (reason, run.marks_refused().then_some(run.block)) + } else { + (4, Some(run.block)) + }; + assert_eq!( + outcome.events, + vec![Event::Rejected(rejected), Event::Unrefundable(1)], + "{run:?}" + ); + assert_eq!(outcome.processed_at, processed, "{run:?}"); + assert!(outcome.active_utxos.is_empty(), "{run:?}"); + assert!(outcome.retiring_utxos.is_empty(), "{run:?}"); + assert_eq!(outcome.refunds, 0, "{run:?}"); +} + +/// rskj's `assertPeginIsRejectedAndRefunded`: `rejected_pegin` with +/// `reason`, the refund of `amount` requested and, from RSKIP428, the +/// refund's `pegout_transaction_created`; the transaction processed, no UTXO +/// kept, and the refund waiting for confirmations. +fn assert_refunded(outcome: &Outcome, run: Run, reason: u64, amount: u64) { + assert!(outcome.returned, "{run:?}"); + let mut events = vec![Event::Rejected(reason), Event::ReleaseRequested(amount)]; + if RskHardforkConfig::mainnet().has_rskip428(run.block) { + events.push(Event::PegoutTransactionCreated); + } + assert_eq!(outcome.events, events, "{run:?}"); + assert_eq!(outcome.processed_at, Some(run.block), "{run:?}"); + assert!(outcome.active_utxos.is_empty(), "{run:?}"); + assert!(outcome.retiring_utxos.is_empty(), "{run:?}"); + assert_eq!(outcome.refunds, 1, "{run:?}"); +} + +/// rskj's `pegin_legacy_to_active_fed`: a bitcoin to the active federation +/// from a P2PKH sender is taken in every run, with a retiring federation or +/// without. +#[test] +fn a_legacy_peg_in_to_the_active_federation_is_taken() { + let tx = from_a_user(&[(BTC, address_of(&active()))]); + for run in RUNS { + for with_retiring in [false, true] { + let outcome = run.bridge(with_retiring).register(&tx, run.height); + assert_taken(&outcome, run, BTC, 1); + } + } +} + +/// rskj's `pegin_multiple_outputs_to_active_fed`: ten outputs of the minimum +/// to the active federation are one peg-in of ten minimums, and ten UTXOs. +#[test] +fn a_peg_in_of_many_outputs_to_the_active_federation_is_taken_whole() { + let outputs = vec![(minimum_pegin(), address_of(&active())); 10]; + let tx = from_a_user(&outputs); + for run in RUNS { + for with_retiring in [false, true] { + let outcome = run.bridge(with_retiring).register(&tx, run.height); + assert_taken(&outcome, run, 10 * minimum_pegin(), 10); + } + } +} + +/// rskj's `pegin_to_active_fed_with_bech32_output`: an output to a bech32 +/// address beside the one to the federation is not counted. +#[test] +fn a_peg_in_with_an_output_to_a_bech32_address_counts_the_federations_alone() { + let tx = from_a_user(&[(BTC, address_of(&active())), (BTC, p2wpkh(&[0xbe; 20]))]); + for run in RUNS { + for with_retiring in [false, true] { + let outcome = run.bridge(with_retiring).register(&tx, run.height); + assert_taken(&outcome, run, BTC, 1); + } + } +} + +/// rskj's `pegin_to_active_fed_equal_to_minimum_with_other_random_outputs_below_minimum`: +/// the minimum to the federation is a peg-in, whatever goes elsewhere below +/// the minimum. +#[test] +fn a_peg_in_of_the_minimum_is_taken_beside_smaller_outputs_elsewhere() { + let below = minimum_pegin() - 1; + let tx = from_a_user(&[ + (minimum_pegin(), address_of(&active())), + (below, p2wpkh(&[0xbe; 20])), + (below, user(0x0a)), + ]); + for run in RUNS { + for with_retiring in [false, true] { + let outcome = run.bridge(with_retiring).register(&tx, run.height); + assert_taken(&outcome, run, minimum_pegin(), 1); + } + } +} + +/// rskj's `pegin_to_active_fed_below_minimum`, +/// `pegin_to_active_fed_below_and_above_minimum` and +/// `pegin_multiple_outputs_to_active_fed_sum_amount_equal_to_minimum_pegin`: +/// an output to the federation below the minimum makes the transaction no +/// peg-in by the legacy rules, and a peg-in of an invalid amount by the index, +/// even where the outputs together come to the minimum. +#[test] +fn a_peg_in_with_an_output_below_the_minimum_is_refused() { + let below = minimum_pegin() - 1; + let to_the_federation = |value: u64| (value, address_of(&active())); + let txs = [ + from_a_user(&[to_the_federation(below)]), + from_a_user(&[to_the_federation(minimum_pegin()), to_the_federation(below)]), + from_a_user(&vec![to_the_federation(minimum_pegin() / 10); 10]), + ]; + for tx in &txs { + for run in RUNS { + for with_retiring in [false, true] { + let outcome = run.bridge(with_retiring).register(tx, run.height); + assert_too_little(&outcome, run); + } + } + } +} + +/// rskj's `pegin_to_active_fed_below_minimum_and_retiring_above_minimum`: +/// the minimum to the retiring federation doesn't make up for too little to +/// the active one. +#[test] +fn a_peg_in_below_the_minimum_to_the_active_federation_is_refused_whatever_the_retiring_one_gets() { + let tx = from_a_user(&[ + (minimum_pegin() - 1, address_of(&active())), + (minimum_pegin(), address_of(&retiring())), + ]); + for run in RUNS { + let outcome = run.bridge(true).register(&tx, run.height); + assert_too_little(&outcome, run); + } +} + +/// rskj's `pegin_v1_to_active_fed_with_invalid_payload_and_unknown_sender_cannot_be_processed`: +/// a peg-in v1 whose payload is empty, from a sender rskj can't name, can be +/// neither registered nor refunded: PEGIN_V1_INVALID_PAYLOAD. rskj's test +/// mocks the sender out of a multisig spend; here the input carries no +/// scriptSig, which no sender rskj knows reads. +#[test] +fn a_peg_in_v1_with_an_empty_payload_and_no_sender_is_not_refundable() { + let empty_payload = [&[0x6a, 5][..], b"RSKT", &[1]].concat(); + let tx = btc_transaction( + vec![input(1, vec![])], + &[(BTC, address_of(&active())), (0, empty_payload)], + ); + for run in RUNS { + let outcome = run.bridge(false).register(&tx, run.height); + assert_not_refundable(&outcome, run, 4); + } +} + +/// rskj's `pegin_to_retiring_fed_cannot_be_processed`: a peg-in to the +/// retiring federation from a sender rskj can't name is not refundable: +/// LEGACY_PEGIN_UNDETERMINED_SENDER (3) from RSKIP379. As above, the +/// sender is one with no scriptSig, where rskj's test mocks it. +#[test] +fn a_peg_in_to_the_retiring_federation_from_an_unknown_sender_is_not_refundable() { + let tx = btc_transaction(vec![input(1, vec![])], &[(BTC, address_of(&retiring()))]); + for run in RUNS { + let outcome = run.bridge(true).register(&tx, run.height); + assert_not_refundable(&outcome, run, 3); + } +} + +/// rskj's `pegin_legacy_from_segwit_to_active_fed_cannot_be_processed`: a +/// legacy peg-in from a segwit sender, here a P2WPKH spend, its signature and +/// key in the witness, is not refundable: rskj can't name the sender. +#[test] +fn a_legacy_peg_in_from_a_segwit_sender_is_not_refundable() { + let mut spend = input(1, vec![]); + spend.witness = bitcoin::Witness::from_slice(&[vec![0x30; 71], public_key(&[0x77]).to_vec()]); + let tx = btc_transaction(vec![spend], &[(BTC, address_of(&active()))]); + for run in RUNS { + let outcome = run.bridge(false).register(&tx, run.height); + assert_not_refundable(&outcome, run, 3); + } +} + +/// rskj's `pegout_no_change_output`: a peg-out paying a user alone is +/// processed, and nothing is registered. +#[test] +fn a_pegout_with_no_change_is_processed_and_registers_nothing() { + for run in RUNS { + for with_retiring in [false, true] { + let outcome = register_spend(run, with_retiring, &active(), 1, &[(BTC, user(1))]); + assert_moved(&outcome, run, 0); + } + } +} + +/// rskj's `pegout_many_outputs_and_inputs_with_change_output` and +/// `pegout_many_outputs_and_one_input`: a peg-out of fifty inputs, or one, to +/// forty users registers its change to the federation, a quarter of the +/// minimum peg-out, whatever its value. +#[test] +fn a_pegout_to_many_users_registers_its_change() { + let mut outputs = forty_outputs(|n| user(n + 1)); + outputs.push((minimum_pegout() / 4, address_of(&active()))); + for inputs in [50, 1] { + for run in RUNS { + for with_retiring in [false, true] { + let outcome = register_spend(run, with_retiring, &active(), inputs, &outputs); + assert_moved(&outcome, run, 1); + assert_eq!(outcome.active_utxos, vec![minimum_pegout() / 4]); + } + } + } +} + +/// rskj's `pegout_one_output_and_many_input`: fifty inputs to one user is a +/// peg-out, and nothing is registered. +#[test] +fn a_pegout_of_many_inputs_to_one_user_registers_nothing() { + for run in RUNS { + for with_retiring in [false, true] { + let outputs = [(minimum_pegout(), user(1))]; + let outcome = register_spend(run, with_retiring, &active(), 50, &outputs); + assert_moved(&outcome, run, 0); + } + } +} + +/// rskj's `migration_ok`, `migration_one_outputs_and_many_input`, +/// `migration_many_outputs_and_inputs` and +/// `migration_many_outputs_and_one_input`: a spend of the retiring federation +/// to the active one is processed, and each of its outputs registered to the +/// active federation, whatever their values. +#[test] +fn a_migration_registers_every_output_to_the_active_federation() { + let to_active = |n: u8| (BTC, address_of(&active()), n); + let cases: Vec<(u8, Vec<(u64, Vec)>)> = vec![ + (1, vec![(to_active(0).0, to_active(0).1)]), + (50, vec![(minimum_pegout(), address_of(&active()))]), + (50, forty_outputs(|_| address_of(&active()))), + (1, forty_outputs(|_| address_of(&active()))), + ]; + for (inputs, outputs) in &cases { + for run in RUNS { + let outcome = register_spend(run, true, &retiring(), *inputs, outputs); + assert_moved(&outcome, run, outputs.len()); + } + } +} + +/// rskj's `flyover_segwit_as_migration_utxo` and +/// `flyover_segwit_as_migration_utxo_with_many_outputs_and_inputs`, and +/// `PegUtilsGetTransactionTypeTest`'s `flyover_segwit_migration`, +/// `flyover_segwit_with_other_inputs_migration` and +/// `flyover_segwit_with_many_inputs_and_outputs_migration`: a migration whose +/// first input, spending a Flyover output, carries the retiring federation's +/// redeem script in its witness is a migration, read from the witness, and is +/// proven by its block's witness root. With nine more inputs spending the +/// retiring federation or none, to one output or to ten, its outputs to the +/// active federation are all registered. +#[test] +fn a_segwit_migration_spending_a_flyover_output_registers_its_outputs() { + use bitcoin::hashes::Hash; + let redeem = standard_redeem(&retiring()); + let flyover = [ + &[0x20][..], + &[0xd1; 32], + &[0x75], + &standard_redeem(&active()), + ] + .concat(); + let funding = from_a_user(&[ + (minimum_pegin(), p2sh(&hash160(&flyover))), + (minimum_pegin(), p2wpkh(&[0xbe; 20])), + ]); + // rskj `createBaseWitnessThatSpendsFromErpRedeemScript`: OP_0, a push for + // each signature, one for OP_NOTIF, and the redeem script. + let threshold = usize::from(redeem[0] - 0x50); + let mut witness: Vec> = vec![vec![]; threshold + 2]; + witness.push(redeem.clone()); + // Each case: the inputs beside the first, the first output's value, and + // the outputs of a bitcoin beside it. + for (others, first_output, more_outputs) in [ + (0u8, 10 * BTC, 0u8), + (9, 10 * BTC, 9), + (0, BTC, 0), + (9, 10 * BTC, 0), + (9, BTC, 9), + ] { + let mut first = input(0, multisig_spend(&redeem)); + first.previous_output = bitcoin::OutPoint { + txid: bitcoin::Txid::from_byte_array(txid(&funding)), + vout: 0, + }; + first.witness = bitcoin::Witness::from_slice(&witness); + let inputs = std::iter::once(first) + .chain((1..=others).map(|tag| input(tag, multisig_spend(&redeem)))) + .collect(); + let outputs: Vec<_> = std::iter::once((first_output, address_of(&active()))) + .chain((0..more_outputs).map(|_| (BTC, address_of(&active())))) + .collect(); + let tx = btc_transaction(inputs, &outputs); + for run in RUNS { + let mut bridge = run.bridge(true); + if run.rskip379() { + index_pegout(&mut bridge.ctx, &index_sighash(&tx, &redeem)); + } + let outcome = bridge.register(&tx, run.height); + assert_moved(&outcome, run, outputs.len()); + } + } +} + +/// A spend of the last retired federation to the active one, its sighash in +/// the index from RSKIP379, registered in `run` with the last retired +/// federation's P2SH script stored when `stored`. +fn register_retired_spend(run: Run, stored: bool) -> Outcome { + let redeem = standard_redeem(&retired()); + let tx = btc_transaction( + vec![input(1, multisig_spend(&redeem))], + &[(BTC, address_of(&active()))], + ); + let mut bridge = run.bridge(false); + if run.rskip379() { + index_pegout(&mut bridge.ctx, &index_sighash(&tx, &redeem)); + } + if stored { + put_named( + &mut bridge.ctx, + "lastRetiredFedP2SHScript", + &rlp_encode_list(&[rlp_encode_element(&address_of(&retired()))]), + ); + } + bridge.register(&tx, run.height) +} + +/// rskj's `last_retired_fed_to_active_fed`: a spend of the last retired +/// federation to the active one is a migration in every run. +#[test] +fn a_spend_of_the_last_retired_federation_is_a_migration() { + for run in RUNS { + assert_moved(®ister_retired_spend(run, true), run, 1); + } +} + +/// rskj's `no_last_retired_fed_in_storage_sending_funds_to_active_fed`: with +/// no last retired federation stored, the index still knows the spend for a +/// migration; the legacy rules take it for a peg-in from a multisig sender, +/// and refund it (`assertPeginIsRejectedAndRefunded`): +/// LEGACY_PEGIN_MULTISIG_SENDER (2) and the refund of the bitcoin. +#[test] +fn with_no_last_retired_federation_a_spend_of_it_is_refunded_by_the_legacy_rules() { + for run in RUNS { + let outcome = register_retired_spend(run, false); + if run.indexed() { + assert_moved(&outcome, run, 1); + } else { + assert_refunded(&outcome, run, 2, BTC); + } + } +} + +/// An unknown federation's members, a multisig sender. +fn unknown() -> Vec<[u8; 33]> { + keys(20, 3) +} + +/// rskj's `pegin_from_multisig_to_retiring_fed_can_be_refunded`: a peg-in to +/// the retiring federation from a multisig sender is refunded: +/// LEGACY_PEGIN_MULTISIG_SENDER (2). +#[test] +fn a_peg_in_from_a_multisig_sender_to_the_retiring_federation_is_refunded() { + let tx = btc_transaction( + vec![input(1, multisig_spend(&standard_redeem(&unknown())))], + &[(BTC, address_of(&retiring()))], + ); + for run in RUNS { + assert_refunded(&run.bridge(true).register(&tx, run.height), run, 2, BTC); + } +} + +/// rskj's `invalid_pegin_v1_from_multisig_to_active_fed_can_be_refunded`: a +/// peg-in v1 with an empty payload from a multisig sender, whose address +/// takes a refund, is refunded: PEGIN_V1_INVALID_PAYLOAD (4). +#[test] +fn a_peg_in_v1_with_an_empty_payload_from_a_multisig_sender_is_refunded() { + let empty_payload = [&[0x6a, 5][..], b"RSKT", &[1]].concat(); + let tx = btc_transaction( + vec![input(1, multisig_spend(&standard_redeem(&unknown())))], + &[(BTC, address_of(&active())), (0, empty_payload)], + ); + for run in RUNS { + assert_refunded(&run.bridge(false).register(&tx, run.height), run, 4, BTC); + } +} + +/// rskj's `pegout_sighash_no_exists_in_provider` and +/// `migration_sighash_no_exists_in_provider`: by the index, a spend of the +/// active or the retiring federation whose sighash the index doesn't hold is +/// no peg-out or migration but a peg-in from a multisig sender, and is +/// refunded what it sends the federation: LEGACY_PEGIN_MULTISIG_SENDER (2). +#[test] +fn by_the_index_a_federation_spend_it_doesnt_hold_is_refunded_as_a_peg_in() { + let pegout = btc_transaction( + vec![input(1, multisig_spend(&standard_redeem(&active())))], + &[(BTC, user(1)), (BTC, address_of(&active()))], + ); + let migration = btc_transaction( + vec![input(1, multisig_spend(&standard_redeem(&retiring())))], + &[(BTC, address_of(&active()))], + ); + for tx in [pegout, migration] { + for run in RUNS.into_iter().filter(|run| run.indexed()) { + assert_refunded(&run.bridge(true).register(&tx, run.height), run, 2, BTC); + } + } +} + +/// What a peg-in rskj's `PeginFromUserWithDifferentScriptPubKey` tests build +/// sends: from an input whose last push is the public key `03 77a6..2de9`, +/// which reads as a script, legacy or v1. +fn pegins_with_a_key_that_reads_as_a_script() -> [bitcoin::Transaction; 2] { + let key = alloy_primitives::hex::decode( + "0377a6c71c43d9fac4343f87538cd2880cf5ebefd3dd1d9aabdbbf454bca162de9", + ) + .expect("hex"); + let script_sig = [vec![0x00], push(&key)].concat(); + let v1 = [&[0x6a, 25][..], b"RSKT", &[1], &[0x5e; 20]].concat(); + [ + btc_transaction( + vec![input(1, script_sig.clone())], + &[(BTC, address_of(&active()))], + ), + btc_transaction( + vec![input(1, script_sig)], + &[(0, v1), (BTC, address_of(&active()))], + ), + ] +} + +/// rskj's `registerBtcTx_legacyPeginWithParseableScriptPubKey_beforeRSKIP305_withSVPOnGoing_shouldThrowISE` +/// and `registerBtcTx_peginV1WithParseableScriptPubKey_beforeRSKIP305_withSVPOnGoing_shouldThrowISE`: +/// while an SVP spend transaction is awaited, every transaction the index +/// reads has its signatures stripped to be compared with it. Before RSKIP305 +/// the stripping takes each input's last push for a redeem script, a key +/// that reads as a script too, and throws `IllegalStateException` on one that +/// is no multisig: the call fails, and the peg-in is not processed. From +/// RSKIP305 such an input is no multisig spend and the peg-in is taken +/// (`registerBtcTx_legacyPeginWithParseableScriptPubKey_withSVPOnGoing_shouldRegisterPegIn`). +#[test] +fn before_rskip305_an_awaited_svp_spend_fails_a_peg_in_whose_key_reads_as_a_script() { + for tx in pegins_with_a_key_that_reads_as_a_script() { + for (block, taken) in [(MAINNET_LOVELL700, false), (MAINNET_REED800, true)] { + let mut bridge = Registration::mainnet(block); + bridge.federation(NEW_FEDERATION, &active()); + put_named( + &mut bridge.ctx, + "svpSpendTxHashUnsigned", + &rlp_encode_element(&[0; 32]), + ); + let outcome = bridge.register(&tx, INDEX_START); + assert_eq!(outcome.returned, taken, "block {block}"); + assert_eq!( + outcome.processed_at, + taken.then_some(block), + "block {block}" + ); + assert_eq!( + outcome.active_utxos.len(), + usize::from(taken), + "block {block}" + ); + } + } +} + +/// Testnet's peg-in v1 77a135b5...3866a6 as rskj's test holds it. +const TESTNET_REAL_PEGIN: &str = "0200000002d5dfff21c0e1f0b02dcbcda77a56ffd6412b79d2081bc4bc0466cf3f0913b297010000006a47304402201dc13fabe4b29d0a596a84f6f15b3d2d636625a8aa02acb8a4635038322040e10220421d22271ca64b7c02b496dc916f092ea84a74e811f81a328f2f9d888aaee59b01210342e7b7961475e1fcb0e604ed34fc554f14ce7f931373646e98e463ae52a4b564fdffffffd5dfff21c0e1f0b02dcbcda77a56ffd6412b79d2081bc4bc0466cf3f0913b297020000006a47304402207e0add6292ac318db6657aeeb717818136f0f4d6e4efef35302ce72a79731fba0220297c3259eed72cd15fae7e0b9a63d2321e415eb6bd36c023bd179455f236147301210342e7b7961475e1fcb0e604ed34fc554f14ce7f931373646e98e463ae52a4b564fdffffff030000000000000000446a4252534b540147bc43b214c418c101b976f8bbb5101ed262a069011ae302de6607907116810e598b83897b00f764d5c0eff2d4911d78c411bf873de759e10d3b2eeaba04bc0300000000001976a914dfc505d84d81d346563fe9726a76c28e9ea8454588ac20a107000000000017a91405804450706addc3c6df3a400a22397ecaafe2d687cfba3d00"; + +/// rskj's `registerBtcTx_testnetRealPeginV1_withSVPOnGoing_beforeRSKIP305_shouldThrowISE`: +/// testnet's peg-in v1 77a135b5...3866a6, whose P2PKH inputs' key reads as a +/// script, fails at testnet's lovell700 while an SVP spend transaction is +/// awaited, and is not processed. +#[test] +fn before_rskip305_testnets_real_peg_in_fails_while_an_svp_spend_is_awaited() { + let raw = alloy_primitives::hex::decode(TESTNET_REAL_PEGIN).expect("hex"); + let tx: bitcoin::Transaction = bitcoin::consensus::deserialize(&raw).expect("a transaction"); + let constants = BridgeConstants::testnet(); + let height = constants.btc_height_when_pegout_tx_index_activates + + constants.pegout_tx_index_grace_period_in_btc_blocks; + let mut bridge = Registration::new(RskHardforkConfig::testnet(), constants, TESTNET_LOVELL700); + put_named( + &mut bridge.ctx, + "svpSpendTxHashUnsigned", + &rlp_encode_element(&[0; 32]), + ); + let outcome = bridge.register(&tx, height); + assert!(!outcome.returned); + assert_eq!(outcome.processed_at, None); +} diff --git a/crates/execution/src/rskj_tests/bridge_pegouts/bridge_support_release_btc_test.rs b/crates/execution/src/rskj_tests/bridge_pegouts/bridge_support_release_btc_test.rs new file mode 100644 index 0000000..d0fb189 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegouts/bridge_support_release_btc_test.rs @@ -0,0 +1,505 @@ +//! rskj's `BridgeSupportReleaseBtcTest`: here its tests of what RSKIP185 +//! changes in `releaseBtc`, of what RSKIP427 changes in a refund and in the +//! amount a turned-down request is logged with, and of the batches RSKIP271 +//! builds in `updateCollections`. rskj's tests run `BridgeSupport` under +//! papyrus200, arrowhead631 and every rule; the ports run mainnet's Bridge +//! at the block before iris300, which brings RSKIP185, at iris300, at +//! hop400, which brings RSKIP271, and at the block before lovell700, which +//! brings RSKIP427, and at lovell700. + +use super::support::*; +use alloy_primitives::{Address, U256}; +use revm::precompile::{PrecompileError, PrecompileOutput}; + +const BEFORE_RSKIP185: u64 = MAINNET_IRIS300 - 1; +const FROM_RSKIP185: u64 = MAINNET_IRIS300; + +/// A request below every minimum, in satoshis. +const LOW: u64 = 1_000; + +/// A contract that calls the Bridge. +const CONTRACT: Address = Address::repeat_byte(0xc0); + +/// `satoshis` in wei. +fn wei(satoshis: u64) -> U256 { + U256::from(satoshis) * U256::from(WEI_PER_SATOSHI) +} + +/// The requests queued. +fn queued(bridge: &mut Bridge) -> usize { + crate::bridge::peg::load_release_request_queue(&mut bridge.ctx, true).len() +} + +/// An address as an indexed topic holds it. +fn as_topic(address: Address) -> [u8; 32] { + let mut topic = [0u8; 32]; + topic[12..].copy_from_slice(address.as_slice()); + topic +} + +/// `releaseBtc` of `satoshis` reached from [`CONTRACT`], a call from its +/// code, the value already at the Bridge. +fn release_from_a_contract( + bridge: &mut Bridge, + satoshis: u64, +) -> Result { + bridge.caller = CONTRACT; + let bridge_address = crate::precompiles::BRIDGE_ADDR; + let held = bridge.balance(bridge_address); + bridge.set_balance(bridge_address, held + wei(satoshis)); + let tx = BridgeTxContext { + rsk_sender: CONTRACT, + ..bridge.tx.clone() + }; + crate::bridge::peg::release_btc( + &mut bridge.ctx, + 0, + &bridge.constants, + &bridge.cfg, + &tx, + CONTRACT, + 2, + wei(satoshis), + ) +} + +/// rskj's `release_after_papyrus_before_iris` and +/// `release_after_iris_before_fingerroot`: a request that is accepted is +/// queued on both sides, and from RSKIP185 logged, `release_request_received`. +#[test] +fn from_rskip185_an_accepted_request_is_logged() { + let received = event_topic("release_request_received(address,bytes,uint256)"); + for (number, expected) in [(BEFORE_RSKIP185, vec![]), (FROM_RSKIP185, vec![received])] { + let mut bridge = Bridge::mainnet_at(number); + bridge.release(BTC); + assert_eq!(queued(&mut bridge), 1, "at {number}"); + let logs = logs(&mut bridge.ctx); + let firsts: Vec<[u8; 32]> = logs.iter().map(|log| topics(log)[0]).collect(); + assert_eq!(firsts, expected, "at {number}"); + } +} + +/// rskj's `release_after_papyrus_before_iris_rejected_lowAmount` and +/// `release_after_iris_rejected_lowAmount`: a request below the minimum is +/// not queued. Before RSKIP185 the Bridge keeps its value and logs nothing; +/// from it the value goes back to the sender, and `release_request_rejected` +/// logs it with the reason LOW_AMOUNT. As in rskj's test, the fee per +/// kilobyte is 5,000 satoshis, which leaves the minimum the larger bound; +/// at mainnet's genesis fee per kilobyte the fee would be, and the reason +/// FEE_ABOVE_VALUE. +#[test] +fn from_rskip185_a_request_below_the_minimum_is_refunded_and_logged() { + let rejected = event_topic("release_request_rejected(address,uint256,int256)"); + let bridge_address = crate::precompiles::BRIDGE_ADDR; + let mut bridge = Bridge::mainnet_at(BEFORE_RSKIP185); + bridge.release(LOW); + assert_eq!(queued(&mut bridge), 0); + assert_eq!(bridge.balance(STRANGER), U256::ZERO); + assert_eq!(bridge.balance(bridge_address), wei(LOW)); + assert!(logs(&mut bridge.ctx).is_empty()); + + let mut bridge = Bridge::mainnet_at(FROM_RSKIP185); + put_named( + &mut bridge.ctx, + "feePerKb", + &rlp_encode_element(&[0x13, 0x88]), + ); + bridge.release(LOW); + assert_eq!(queued(&mut bridge), 0); + assert_eq!(bridge.balance(STRANGER), wei(LOW)); + assert_eq!(bridge.balance(bridge_address), U256::ZERO); + let logs = logs(&mut bridge.ctx); + assert_eq!(logs.len(), 1); + assert_eq!(topics(&logs[0]), [rejected, as_topic(STRANGER)]); + assert_eq!(word(&logs[0].data.data, 0), U256::from(LOW)); + assert_eq!(word(&logs[0].data.data, 1), U256::from(1), "LOW_AMOUNT"); +} + +/// From RSKIP271 rskj sizes the peg-out whose fee a request must cover by +/// the active federation's format (`BridgeUtils.calculatePegoutTxSize`): a +/// P2SH-P2WSH ERP federation's spend weighs its witness at a quarter, a P2SH +/// ERP federation's counts it whole. At 0.02 BTC a kilobyte, a request of +/// 0.03 BTC covers the first and not the second, which turns it down with +/// the reason FEE_ABOVE_VALUE. +#[test] +fn from_rskip271_a_segwit_federations_pegout_is_sized_by_its_weight() { + let rejected = event_topic("release_request_rejected(address,uint256,int256)"); + for (format, accepted) in [(P2SH_ERP, false), (P2SH_P2WSH_ERP, true)] { + let mut bridge = Bridge::mainnet_at(MAINNET_VETIVER900); + store_federation(&mut bridge.ctx, NEW_FEDERATION, &active(), 1, format); + put_named( + &mut bridge.ctx, + "feePerKb", + &rlp_encode_element(&[0x1e, 0x84, 0x80]), + ); + bridge.release(3 * CENT); + assert_eq!( + queued(&mut bridge), + usize::from(accepted), + "format {format}" + ); + let logs = logs(&mut bridge.ctx); + let turned_down: Vec<_> = logs + .iter() + .filter(|log| topics(log)[0] == rejected) + .collect(); + if accepted { + assert!(turned_down.is_empty()); + } else { + assert_eq!(turned_down.len(), 1); + assert_eq!( + word(&turned_down[0].data.data, 1), + U256::from(3), + "FEE_ABOVE_VALUE" + ); + } + } +} + +/// rskj's `release_after_papyrus_before_iris_rejected_contractCaller_throws_exception` +/// and `release_after_iris_rejected_contractCaller_emits_rejection_event`: a +/// contract's request throws before RSKIP185, rskj's `OutOfGasException`, +/// which `Bridge.execute` wraps as any other throw (not a want of gas); from +/// it the call returns, the value is not sent back, and +/// `release_request_rejected` logs the contract with the reason +/// CALLER_CONTRACT. +#[test] +fn from_rskip185_a_contracts_request_is_logged_and_not_refunded() { + let mut bridge = Bridge::mainnet_at(BEFORE_RSKIP185); + let thrown = release_from_a_contract(&mut bridge, BTC).expect_err("releaseBtc throws"); + assert!(!thrown.is_oog(), "a throw, not a want of gas: {thrown:?}"); + + let rejected = event_topic("release_request_rejected(address,uint256,int256)"); + let mut bridge = Bridge::mainnet_at(FROM_RSKIP185); + release_from_a_contract(&mut bridge, BTC).expect("releaseBtc answers"); + assert_eq!(queued(&mut bridge), 0); + assert_eq!(bridge.balance(CONTRACT), U256::ZERO); + assert_eq!(bridge.balance(crate::precompiles::BRIDGE_ADDR), wei(BTC)); + let logs = logs(&mut bridge.ctx); + assert_eq!(logs.len(), 1); + assert_eq!(topics(&logs[0]), [rejected, as_topic(CONTRACT)]); + assert_eq!( + word(&logs[0].data.data, 1), + U256::from(2), + "CALLER_CONTRACT" + ); +} + +/// The block before RSKIP427 and the block that brings it. +const AROUND_RSKIP427: [(u64, bool); 2] = + [(MAINNET_LOVELL700 - 1, false), (MAINNET_LOVELL700, true)]; + +/// What rskj's tests add to a request's satoshis: a satoshi less one wei. +fn under_a_satoshi() -> U256 { + U256::from(WEI_PER_SATOSHI - 1) +} + +/// `releaseBtc` of `value` wei asked for by `caller`, from the transaction +/// itself, or with `from_a_contract` from a contract's code; the value +/// already at the Bridge. +fn release_wei( + bridge: &mut Bridge, + caller: Address, + from_a_contract: bool, + value: U256, +) -> Result { + let bridge_address = crate::precompiles::BRIDGE_ADDR; + let held = bridge.balance(bridge_address); + bridge.set_balance(bridge_address, held + value); + let tx = BridgeTxContext { + rsk_sender: caller, + ..bridge.tx.clone() + }; + crate::bridge::peg::release_btc( + &mut bridge.ctx, + 0, + &bridge.constants, + &bridge.cfg, + &tx, + caller, + if from_a_contract { 2 } else { 1 }, + value, + ) +} + +/// A request of `satoshis` and a satoshi less one wei, turned down for +/// `reason`, with the fee per kilobyte `fee_per_kb`: from the sender it is +/// refunded, from a contract it is not. Before RSKIP427 the refund and the +/// amount `release_request_rejected` logs are its whole satoshis; from it +/// the wei it sent. +fn turned_down_around_rskip427( + satoshis: u64, + fee_per_kb: &[u8], + from_a_contract: bool, + reason: u64, +) { + let rejected = event_topic("release_request_rejected(address,uint256,int256)"); + let bridge_address = crate::precompiles::BRIDGE_ADDR; + let value = wei(satoshis) + under_a_satoshi(); + let caller = if from_a_contract { CONTRACT } else { STRANGER }; + for (number, rskip427) in AROUND_RSKIP427 { + let mut bridge = Bridge::mainnet_at(number); + put_named(&mut bridge.ctx, "feePerKb", &rlp_encode_element(fee_per_kb)); + release_wei(&mut bridge, caller, from_a_contract, value).expect("releaseBtc answers"); + assert_eq!(queued(&mut bridge), 0, "{number}"); + let refunded = match (from_a_contract, rskip427) { + (true, _) => U256::ZERO, + (false, true) => value, + (false, false) => wei(satoshis), + }; + assert_eq!(bridge.balance(caller), refunded, "{number}"); + assert_eq!(bridge.balance(bridge_address), value - refunded, "{number}"); + let logs = logs(&mut bridge.ctx); + assert_eq!(logs.len(), 1, "{number}"); + assert_eq!(topics(&logs[0]), [rejected, as_topic(caller)], "{number}"); + let amount = if rskip427 { + value + } else { + U256::from(satoshis) + }; + assert_eq!(word(&logs[0].data.data, 0), amount, "{number}"); + assert_eq!(word(&logs[0].data.data, 1), U256::from(reason), "{number}"); + } +} + +/// rskj's `low_amount_release_request_rejected_before_lovell` and +/// `low_amount_release_request_rejected_after_lovell`: a satoshi below the +/// minimum, LOW_AMOUNT. The fee per kilobyte is 5,000 satoshis, which +/// leaves the minimum the larger bound. +#[test] +fn a_request_below_the_minimum_is_refunded_its_satoshis_before_rskip427_and_its_wei_from_it() { + let minimum = BridgeConstants::mainnet().minimum_pegout_tx_value; + turned_down_around_rskip427(minimum - 1, &[0x13, 0x88], false, 1); +} + +/// rskj's `contract_caller_release_request_rejected_before_lovell` and +/// `contract_caller_release_request_rejected_after_lovell`: the minimum, +/// from a contract, CALLER_CONTRACT, nothing refunded. +#[test] +fn a_contracts_request_is_logged_in_satoshis_before_rskip427_and_in_wei_from_it() { + let minimum = BridgeConstants::mainnet().minimum_pegout_tx_value; + turned_down_around_rskip427(minimum, &[0x13, 0x88], true, 2); +} + +/// rskj's `fee_above_value_release_request_rejected_before_lovell` and +/// `fee_above_value_release_request_rejected_after_lovell`: a fee per +/// kilobyte of a bitcoin, which puts the fee a peg-out must cover above a +/// bitcoin, and a request of a bitcoin, FEE_ABOVE_VALUE. +#[test] +fn a_request_below_its_fee_is_refunded_its_satoshis_before_rskip427_and_its_wei_from_it() { + turned_down_around_rskip427(BTC, &[0x05, 0xf5, 0xe1, 0x00], false, 3); +} + +/// Where the Bridge burns what it holds and no one is owed. +const BURN: Address = Address::repeat_byte(0xff); + +/// `count` UTXOs of `satoshis` each, paid to the federation +/// [`Collections::active`] makes, each from a transaction of its own. +fn many_utxos(count: u16, satoshis: u64) -> Vec { + let script = address_of(&active()); + (1..=count) + .map(|i| { + let mut tx_hash = [0u8; 32]; + tx_hash[..2].copy_from_slice(&i.to_be_bytes()); + crate::bridge::storage::BridgeUtxo { + tx_hash, + vout: 0, + value_satoshis: satoshis, + height: 1, + script: script.clone(), + coinbase: false, + } + }) + .collect() +} + +/// Mainnet's Bridge at hop400, about to batch: the active federation holding +/// `utxos`, the requests of `amounts` queued, the request `i` (from 1) for +/// the user `i` with the hash `[i; 32]`, and the Bridge holding 100 BTC. +fn batching(utxos: &[crate::bridge::storage::BridgeUtxo], amounts: &[u64]) -> Collections { + let entries: Vec<([u8; 20], u64, [u8; 32])> = amounts + .iter() + .zip(1u8..) + .map(|(&amount, i)| ([i; 20], amount, [i; 32])) + .collect(); + batching_queue(utxos, &entries) +} + +/// [`batching`], with the queue's `(destination, amount, hash)` entries. +fn batching_queue( + utxos: &[crate::bridge::storage::BridgeUtxo], + entries: &[([u8; 20], u64, [u8; 32])], +) -> Collections { + let mut collections = Collections::mainnet_at(MAINNET_HOP400); + collections.active(1, 0); + let number = collections.bridge.number(); + crate::bridge::storage::store_federation_utxos( + &mut collections.bridge.ctx, + &collections.bridge.cfg, + number, + utxos, + ); + put_named( + &mut collections.bridge.ctx, + "releaseRequestQueueWithTxHash", + &hashed_queue(entries), + ); + collections + .bridge + .set_balance(crate::precompiles::BRIDGE_ADDR, wei(100 * BTC)); + collections +} + +/// The hashes of the requests still queued. +fn still_queued(collections: &mut Collections) -> Vec> { + crate::bridge::peg::load_release_request_queue(&mut collections.bridge.ctx, true) + .iter() + .map(|request| request.rsk_tx_hash) + .collect() +} + +/// rskj takes a batch's requests off the queue with `List.removeAll`, by +/// `ReleaseRequestQueue.Entry.equals`: every request equal to one the batch +/// pays goes, a request being equal to another of the same destination, +/// amount and creation hash. Here the queue, too large for one batch, repeats +/// its first request, whose copy goes with it though the halved batch pays +/// one, and holds a request of the same destination and amount with another +/// hash, which stays. +#[test] +fn from_rskip271_a_batch_takes_off_the_queue_every_request_equal_to_one_it_pays() { + let first = ([1; 20], 3 * BTC / 2, [1; 32]); + let second = ([2; 20], 3 * BTC / 2, [2; 32]); + let other_hash = ([1; 20], 3 * BTC / 2, [3; 32]); + let mut collections = batching_queue( + &many_utxos(700, BTC / 100), + &[first, second, first, other_hash], + ); + collections.update(); + assert_eq!(collections.waiting().len(), 1); + assert_eq!(still_queued(&mut collections), [Some([3; 32])]); +} + +/// rskj's `processPegoutsInBatch_after_hop_divide_transaction_when_max_size_exceeded`: +/// four requests of 1.5 BTC need 600 inputs of 0.01 BTC, over bitcoinj's +/// 100,000 bytes, and the batch is halved: the first two, 300 inputs, are +/// paid, the other two stay queued, and the next call pays them. rskj settles +/// the first batch with all four's 6 BTC: `release_requested` logs it, and +/// the federation having spent 3 BTC, `adjustBalancesIfChangeOutputWasDust` +/// burns the other 3. `batch_pegout_created` lists the two paid. +#[test] +fn from_rskip271_a_batch_over_the_size_is_halved() { + let mut collections = batching(&many_utxos(700, BTC / 100), &[3 * BTC / 2; 4]); + let logs = collections.update(); + assert_eq!( + still_queued(&mut collections), + [Some([3; 32]), Some([4; 32])] + ); + let waiting = collections.waiting(); + assert_eq!(waiting.len(), 1); + assert_eq!(waiting[0].input.len(), 300); + let paid: Vec> = waiting[0] + .output + .iter() + .map(|output| output.script_pubkey.to_bytes()) + .collect(); + assert_eq!(paid, [user(1), user(2)]); + + let requested = event_topic("release_requested(bytes32,bytes32,uint256)"); + let batch = event_topic("batch_pegout_created(bytes32,bytes)"); + let log_of = |topic: [u8; 32]| { + logs.iter() + .find(|log| topics(log)[0] == topic) + .expect("logged") + }; + assert_eq!(word(&log_of(requested).data.data, 0), U256::from(6 * BTC)); + let hashes = &log_of(batch).data.data; + assert_eq!(word(hashes, 1), U256::from(64)); + assert_eq!(&hashes[64..128], [[1u8; 32], [2u8; 32]].concat()); + assert_eq!(collections.bridge.balance(BURN), wei(3 * BTC)); + + collections.update(); + assert!(still_queued(&mut collections).is_empty()); + assert_eq!(collections.waiting().len(), 2); +} + +/// rskj's `processPegoutsInBatch_after_hop_when_max_size_exceeded_for_one_pegout`: +/// one request of 7 BTC needs 700 inputs of 0.01 BTC, over the size, and +/// cannot be halved: nothing is paid, and the request stays queued. +#[test] +fn from_rskip271_one_request_over_the_size_stays_queued() { + let mut collections = batching(&many_utxos(700, BTC / 100), &[7 * BTC]); + collections.update(); + assert_eq!(still_queued(&mut collections), [Some([1; 32])]); + assert!(collections.waiting().is_empty()); +} + +/// rskj's `processPegoutsInBatch_after_hop_when_max_size_exceeded_for_two_pegout`: +/// two requests of 7 BTC are over the size, and so is the first alone: +/// nothing is paid, and both stay queued. +#[test] +fn from_rskip271_two_requests_each_over_the_size_stay_queued() { + let mut collections = batching(&many_utxos(1400, BTC / 100), &[7 * BTC, 7 * BTC]); + collections.update(); + assert_eq!( + still_queued(&mut collections), + [Some([1; 32]), Some([2; 32])] + ); + assert!(collections.waiting().is_empty()); +} + +/// A request queued before RSKIP146 has no creation hash, and when it is +/// batched rskj's `logBatchPegoutCreated` cannot serialize it +/// (`serializeRskTxHashes`): the call throws. No network reaches it, as every +/// network queues requests with their hashes well before RSKIP271. +#[test] +fn from_rskip271_a_batched_request_with_no_hash_fails_the_call() { + let mut collections = batching(&many_utxos(10, BTC), &[BTC]); + put_named( + &mut collections.bridge.ctx, + "releaseRequestQueue", + &legacy_queue(&[([9; 20], BTC)]), + ); + let bridge = &mut collections.bridge; + let tx = BridgeTxContext { + rsk_sender: bridge.caller, + ..bridge.tx.clone() + }; + assert!(crate::bridge::peg::update_collections( + &mut bridge.ctx, + 0, + &bridge.constants, + &bridge.cfg, + &tx + ) + .is_err()); +} + +/// From RSKIP271 rskj reads the next peg-out height in every +/// `updateCollections` (`getNextPegoutHeight`, 0 where the cell holds +/// nothing) and in `getNextPegoutCreationBlockNumber`, and writes back what +/// it read where it set nothing new: a request that cannot be paid leaves +/// the height unset, and the cell holds 0, `RLP.encodeBigInteger(0)`. +/// Before RSKIP271 the cell is neither read nor written. +#[test] +fn from_rskip271_the_next_pegout_height_read_is_written_back() { + for (number, expected) in [ + (MAINNET_HOP400 - 1, None), + (MAINNET_HOP400, Some(vec![0x80])), + ] { + let mut collections = Collections::mainnet_at(number); + collections.active(1, 0); + collections.requests(1); + collections.update(); + assert_eq!( + named(&mut collections.bridge.ctx, "nextPegoutHeight"), + expected, + "at {number}" + ); + } + + let mut bridge = Bridge::mainnet_at(MAINNET_HOP400); + bridge + .execute(&crate::abi::selector("getNextPegoutCreationBlockNumber()")) + .expect("the getter answers"); + assert_eq!(named(&mut bridge.ctx, "nextPegoutHeight"), Some(vec![0x80])); +} diff --git a/crates/execution/src/rskj_tests/bridge_pegouts/bridge_support_sig_hash_test.rs b/crates/execution/src/rskj_tests/bridge_pegouts/bridge_support_sig_hash_test.rs new file mode 100644 index 0000000..ffea3a4 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegouts/bridge_support_sig_hash_test.rs @@ -0,0 +1,230 @@ +//! rskj's `BridgeSupportSigHashTest`: from RSKIP379 (arrowhead600) the +//! sighash of the first input of each peg-out `updateCollections` builds, a +//! batch or a migration, goes to the pegout index; before it, under +//! fingerroot500, none does. The ports run mainnet's Bridge at a block of +//! each and read the index. The SVP fund transaction, which +//! `updateCollections` builds from RSKIP419 (lovell700) and settles as it +//! settles a peg-out, goes there too; the SVP spend transaction, which it +//! builds once the fund transaction is registered, doesn't. Both are logged +//! as peg-outs are from RSKIP428, which lovell700 brings as well. + +use alloy_primitives::U256; + +use super::support::*; + +const ERAS: [(u64, bool); 2] = [(MAINNET_FINGERROOT500, false), (MAINNET_ARROWHEAD600, true)]; + +/// The redeem script the first input of a peg-out the Bridge built spends: +/// the last push of its scriptSig. +fn spent_redeem(tx: &bitcoin::Transaction) -> Vec { + match tx.input[0].script_sig.instructions().last() { + Some(Ok(bitcoin::script::Instruction::PushBytes(bytes))) => bytes.as_bytes().to_vec(), + other => panic!("no redeem script pushed last: {other:?}"), + } +} + +/// rskj's `test_pegoutTxIndex_when_pegout_batch_is_created`. +#[test] +fn from_rskip379_a_pegout_batch_built_goes_to_the_pegout_index() { + for (number, rskip379) in ERAS { + let mut collections = Collections::mainnet_at(number); + collections.active(1, 10); + collections.requests(3); + collections.update(); + let waiting = collections.waiting(); + assert_eq!(waiting.len(), 1, "{number}"); + let redeem = standard_redeem(&active()); + assert_eq!( + collections.indexed(&waiting[0], &redeem), + rskip379, + "{number}" + ); + } +} + +/// rskj's `test_pegoutTxIndex_when_migration_tx_is_created`. +#[test] +fn from_rskip379_a_migration_built_goes_to_the_pegout_index() { + for (number, rskip379) in ERAS { + let mut collections = Collections::migrating(number, STANDARD_MULTISIG, 10, false); + collections.update(); + let waiting = collections.waiting(); + assert_eq!(waiting.len(), 1, "{number}"); + let redeem = standard_redeem(&retiring()); + assert_eq!( + collections.indexed(&waiting[0], &redeem), + rskip379, + "{number}" + ); + } +} + +/// rskj's `test_pegoutTxIndex_when_migration_and_pegout_batch_tx_are_created`. +#[test] +fn from_rskip379_a_migration_and_a_pegout_batch_built_together_both_go_to_the_pegout_index() { + for (number, rskip379) in ERAS { + let mut collections = Collections::migrating(number, STANDARD_MULTISIG, 10, false); + collections.requests(3); + collections.update(); + let waiting = collections.waiting(); + assert_eq!(waiting.len(), 2, "{number}"); + let redeems: Vec> = waiting.iter().map(spent_redeem).collect(); + assert_ne!(redeems[0], redeems[1], "a migration and a batch"); + for (tx, redeem) in waiting.iter().zip(&redeems) { + assert_eq!(collections.indexed(tx, redeem), rskip379, "{number}"); + } + } +} + +/// Mainnet's Bridge at lovell700, which brings RSKIP419 and RSKIP428, with +/// the active federation of [`Collections::active`] and its ten UTXOs of a +/// bitcoin, and a federation of five proposed the block before. +fn proposing() -> Collections { + let number = MAINNET_LOVELL700; + let mut collections = Collections::mainnet_at(number); + collections.active(1, 10); + store_federation( + &mut collections.bridge.ctx, + ("proposedFederation", "proposedFederationFormatVersion"), + &keys(30, 5), + number - 1, + P2SH_ERP, + ); + collections +} + +/// A transaction's hash in the order the Bridge's events and cells hold it. +fn display_hash(tx: &bitcoin::Transaction) -> [u8; 32] { + let mut hash = txid(tx); + hash.reverse(); + hash +} + +/// The logs `updateCollections` leaves when it builds a peg-out `tx` from +/// RSKIP428, in the call whose transaction is `call`: `update_collections` +/// first, as every call of it does, then `release_requested` of `amount` +/// satoshis, then `pegout_transaction_created` with the values of the +/// outputs `tx` spends, `spent`, as bitcoinj's `VarInt`s. +fn assert_logged_as_a_pegout( + logs: &[revm::primitives::Log], + call: [u8; 32], + tx: &bitcoin::Transaction, + amount: u64, + spent: &[u8], +) { + let hash = display_hash(tx); + assert_eq!(logs.len(), 3, "{logs:?}"); + assert_eq!( + topics(&logs[0]), + [event_topic("update_collections(address)")] + ); + let requested = event_topic("release_requested(bytes32,bytes32,uint256)"); + assert_eq!(topics(&logs[1]), [requested, call, hash]); + assert_eq!(word(&logs[1].data.data, 0), U256::from(amount)); + let created = event_topic("pegout_transaction_created(bytes32,bytes)"); + assert_eq!(topics(&logs[2]), [created, hash]); + let data = &logs[2].data.data; + assert_eq!(word(data, 0), U256::from(32)); + assert_eq!(word(data, 1), U256::from(spent.len())); + assert_eq!(&data[64..64 + spent.len()], spent); +} + +/// From RSKIP419, with a federation proposed the block before and no SVP +/// value kept, `updateCollections` builds the SVP fund transaction +/// (`processSvpFundTransactionUnsigned`), keeps its hash as +/// `svpFundTxHashUnsigned`, and settles it as a peg-out +/// (`settleReleaseRequest`): it waits for its confirmations, its first +/// input's sighash goes to the pegout index (`savePegoutTxSigHash`), which +/// RSKIP379 has on wherever RSKIP419 is, and it is logged as a peg-out of +/// twice two minimum peg-outs, spending one of the active federation's +/// bitcoins. +#[test] +fn from_rskip419_the_svp_fund_transaction_built_goes_to_the_pegout_index() { + let mut collections = proposing(); + let logs = collections.update(); + let waiting = collections.waiting(); + assert_eq!(waiting.len(), 1); + let fund = &waiting[0]; + assert!(collections.indexed(fund, &standard_redeem(&active()))); + assert_eq!( + named(&mut collections.bridge.ctx, "svpFundTxHashUnsigned"), + Some(rlp_encode_element(&display_hash(fund))) + ); + let call = collections.bridge.tx.rsk_tx_hash; + let minimum = collections.bridge.constants.minimum_pegout_tx_value; + let one_bitcoin = [0xfe, 0x00, 0xe1, 0xf5, 0x05]; + assert_logged_as_a_pegout(&logs, call, fund, 4 * minimum, &one_bitcoin); +} + +/// Once the SVP fund transaction is registered (`registerSvpFundTx` keeps it +/// as `svpFundTxSigned` and clears `svpFundTxHashUnsigned`), the next +/// `updateCollections` builds the SVP spend transaction +/// (`processSvpSpendTransactionUnsigned`): version 2, spending the fund +/// transaction's outputs to the proposed federation and to its flyover +/// address, less its fees, to the active federation. It waits for +/// signatures under the call's transaction, its hash is kept as +/// `svpSpendTxHashUnsigned`, `svpFundTxSigned` is cleared, and it is logged +/// as a peg-out of what it pays, spending two minimum peg-outs twice. Unlike +/// the fund transaction it doesn't go to the pegout index. +#[test] +fn from_rskip419_the_svp_spend_transaction_is_logged_and_not_indexed() { + use crate::bridge::storage::{bridge_storage_key, bridge_store_raw}; + let mut collections = proposing(); + collections.update(); + let fund = collections.waiting().remove(0); + let ctx = &mut collections.bridge.ctx; + bridge_store_raw(ctx, bridge_storage_key("svpFundTxHashUnsigned"), None); + put_named( + ctx, + "svpFundTxSigned", + &rlp_encode_element(&bitcoin::consensus::serialize(&fund)), + ); + + let logs = collections.update(); + let ctx = &mut collections.bridge.ctx; + let entry = named(ctx, "svpSpendTxWaitingForSignatures").expect("waiting"); + let items = crate::bridge::serialization::rlp_decode_list(&entry).expect("a list"); + let call = collections.bridge.tx.rsk_tx_hash; + assert_eq!(items.len(), 2); + assert_eq!(items[0], call); + let spend: bitcoin::Transaction = + bitcoin::consensus::deserialize(&items[1]).expect("a transaction"); + assert_eq!( + named(ctx, "svpSpendTxHashUnsigned"), + Some(rlp_encode_element(&display_hash(&spend))) + ); + assert!(named(ctx, "svpFundTxSigned").unwrap_or_default().is_empty()); + + assert_eq!(spend.version, bitcoin::transaction::Version::TWO); + let spent: Vec = spend + .input + .iter() + .map(|input| input.previous_output) + .collect(); + let fund_txid = fund.compute_txid(); + assert_eq!( + spent, + [ + bitcoin::OutPoint::new(fund_txid, 0), + bitcoin::OutPoint::new(fund_txid, 1) + ] + ); + let minimum = collections.bridge.constants.minimum_pegout_tx_value; + assert_eq!(spend.output.len(), 1); + assert_eq!( + spend.output[0].script_pubkey.as_bytes(), + address_of(&active()).as_slice() + ); + let paid = spend.output[0].value.to_sat(); + assert!(0 < paid && paid < 4 * minimum, "{paid}"); + + assert!(!collections.indexed(&spend, &spent_redeem(&spend))); + let two_minimums = [0xfe, 0x00, 0x35, 0x0c, 0x00]; + assert_logged_as_a_pegout( + &logs, + call, + &spend, + paid, + &[two_minimums, two_minimums].concat(), + ); +} diff --git a/crates/execution/src/rskj_tests/bridge_pegouts/mod.rs b/crates/execution/src/rskj_tests/bridge_pegouts/mod.rs new file mode 100644 index 0000000..a63072b --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegouts/mod.rs @@ -0,0 +1,22 @@ +//! The Bridge's storage, peg-outs, signatures and events. + +mod bitcoin_utils_legacy_test; +mod bridge_event_logger_impl_test; +mod bridge_event_logger_legacy_impl_test; +mod bridge_storage_provider_test; +mod bridge_support_add_signature_test; +mod bridge_support_get_estimated_fees_test; +mod bridge_support_pegout_transaction_created_event_test; +mod bridge_support_process_funds_migration_test; +mod bridge_support_register_btc_transaction_test; +mod bridge_support_release_btc_test; +mod bridge_support_sig_hash_test; +mod peg_utils_all_utxos_to_fed_are_above_minimum_pegin_value_test; +mod peg_utils_get_transaction_type_test; +mod peg_utils_legacy_get_transaction_type_test; +mod pegouts_waiting_for_confirmations_test; +mod poc_sighash_test; +mod point_at_infinity_signer_test; +mod release_transaction_builder_test; +mod support; +mod what_a_call_writes_back_test; diff --git a/crates/execution/src/rskj_tests/bridge_pegouts/peg_utils_all_utxos_to_fed_are_above_minimum_pegin_value_test.rs b/crates/execution/src/rskj_tests/bridge_pegouts/peg_utils_all_utxos_to_fed_are_above_minimum_pegin_value_test.rs new file mode 100644 index 0000000..078cbf4 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegouts/peg_utils_all_utxos_to_fed_are_above_minimum_pegin_value_test.rs @@ -0,0 +1,236 @@ +//! rskj's `PegUtilsAllUTXOsToFedAreAboveMinimumPeginValueTest`: whether every +//! output a transaction sends the live federations is worth the minimum +//! peg-in. Before RSKIP379 rskj answers it as +//! `PegUtilsLegacy.isAnyUTXOAmountBelowMinimum` does, true where no output +//! goes to a federation; from RSKIP379 it answers false there. +//! +//! rskj's tests ask the function. Here its answer is read in what +//! `registerBtcTransaction` does with the transaction, sent by a P2PKH user. +//! Before RSKIP379 the answer is the legacy rules' minimum (`isValidPegInTx`, +//! from RSKIP293): with it the transaction is a peg-in, taken for what it +//! sends the federations, nothing where it sends them nothing; without it the +//! transaction is no peg-in and is left alone. From RSKIP379 `evaluatePegin` +//! asks it of every peg-in: with it the peg-in is taken, without it the peg-in +//! is turned down, INVALID_AMOUNT. A transaction that pays a federation is +//! asked by the pegout index; one that pays none only below the index's +//! start, where the legacy rules first take it for a peg-in. + +use super::support::*; + +/// Where an output goes. +#[derive(Clone, Copy, PartialEq, Eq)] +enum To { + Active, + Retiring, + /// A random user's address. + Elsewhere, +} + +fn script(to: To) -> Vec { + match to { + To::Active => address_of(&active()), + To::Retiring => address_of(&retiring()), + To::Elsewhere => user(0xad), + } +} + +/// What registering a user's transaction paying `outputs` leaves in `run`, +/// the retiring federation live where an output goes to it. +fn registered(run: Run, outputs: &[(u64, To)]) -> Outcome { + let paid: Vec<(u64, Vec)> = outputs + .iter() + .map(|(value, to)| (*value, script(*to))) + .collect(); + let with_retiring = outputs.iter().any(|(_, to)| *to == To::Retiring); + run.bridge(with_retiring) + .register(&from_a_user(&paid), run.height) +} + +/// The peg-in of `outputs` taken: `pegin_btc` of what they send the +/// federations, the transaction processed, each output to a federation its +/// UTXO. +fn assert_taken(outcome: &Outcome, run: Run, outputs: &[(u64, To)]) { + let sent: u64 = outputs + .iter() + .filter(|(_, to)| *to != To::Elsewhere) + .map(|(value, _)| value) + .sum(); + let count = |which: To| outputs.iter().filter(|(_, to)| *to == which).count(); + assert_eq!(outcome.events, vec![Event::PeginBtc(sent, 0)]); + assert_eq!(outcome.processed_at, Some(run.block)); + assert_eq!(outcome.active_utxos.len(), count(To::Active)); + assert_eq!(outcome.retiring_utxos.len(), count(To::Retiring)); +} + +/// rskj's answer before RSKIP379 for `outputs`, held: with it the +/// transaction is taken for a peg-in; without it it is left alone. +fn holds_before_rskip379(outputs: &[(u64, To)], answer: bool) { + let outcome = registered(BEFORE_RSKIP379, outputs); + assert!(outcome.returned); + if answer { + assert_taken(&outcome, BEFORE_RSKIP379, outputs); + } else { + assert!(outcome.events.is_empty()); + assert_eq!(outcome.processed_at, None); + assert!(outcome.active_utxos.is_empty() && outcome.retiring_utxos.is_empty()); + } +} + +/// rskj's answer from RSKIP379 for `outputs`, held: with it the peg-in is +/// taken; without it it is turned down, `rejected_pegin` INVALID_AMOUNT (5) +/// and `unrefundable_pegin` INVALID_AMOUNT (3), and at arrowhead600, before +/// RSKIP459, not marked processed. +fn holds_from_rskip379(outputs: &[(u64, To)], answer: bool) { + let pays_a_federation = outputs.iter().any(|(_, to)| *to != To::Elsewhere); + let run = if pays_a_federation { + RSKIP379_INDEXED + } else { + RSKIP379_BELOW_INDEX + }; + let outcome = registered(run, outputs); + assert!(outcome.returned); + if answer { + assert_taken(&outcome, run, outputs); + } else { + assert_eq!( + outcome.events, + vec![Event::Rejected(5), Event::Unrefundable(3)] + ); + assert_eq!(outcome.processed_at, None); + assert!(outcome.active_utxos.is_empty() && outcome.retiring_utxos.is_empty()); + } +} + +fn min() -> u64 { + minimum_pegin() +} + +/// rskj's `..._preRSKIP379WithManyOutputsBelowMinimumButNoneForFed_true` and +/// `..._postRSKIP379WithManyOutputsBelowMinimumButNoneForFed_false`. +#[test] +fn outputs_below_the_minimum_to_no_federation_pass_before_rskip379_only() { + let outputs = [(min() - 1, To::Elsewhere); 3]; + holds_before_rskip379(&outputs, true); + holds_from_rskip379(&outputs, false); +} + +/// rskj's `..._preRSKIP379WithManyOutputsEqualAndAboveMinimumButNoneForFed_true` +/// and `..._postRSKIP379WithManyOutputsEqualAndAboveMinimumButNoneForFed_false`. +#[test] +fn outputs_of_the_minimum_to_no_federation_pass_before_rskip379_only() { + let outputs = [ + (min(), To::Elsewhere), + (min() + 1, To::Elsewhere), + (min() + 1, To::Elsewhere), + ]; + holds_before_rskip379(&outputs, true); + holds_from_rskip379(&outputs, false); +} + +/// rskj's `..._preRSKIP379WithManyOutputsAboveMinimumForFedAndManyOutputsBelowMinimumForRandomAddresses_true` +/// and its `post` counterpart: outputs elsewhere below the minimum don't count. +#[test] +fn outputs_to_the_federation_of_the_minimum_pass_beside_smaller_ones_elsewhere() { + let elsewhere = [(min() - 1, To::Elsewhere); 3]; + let before = [elsewhere.as_slice(), &[(min() + 1, To::Active); 3]].concat(); + holds_before_rskip379(&before, true); + let after = [ + elsewhere.as_slice(), + &[ + (min(), To::Active), + (min() + 1, To::Active), + (min() + 1, To::Active), + ], + ] + .concat(); + holds_from_rskip379(&after, true); +} + +/// rskj's `..._preRSKIP379WithManyOutputsForFedButEachBelowMinimumAndSumAboveMinimum_false` +/// and its `post` counterpart: each output must be worth the minimum, not +/// their sum. +#[test] +fn outputs_to_the_federation_each_below_the_minimum_fail_whatever_their_sum() { + let outputs = [(min() - 1, To::Active); 3]; + holds_before_rskip379(&outputs, false); + holds_from_rskip379(&outputs, false); +} + +/// rskj's `..._preRSKIP379WithManyOutputsForFedAboveMinimumAndOneBelowMinimum_false` +/// and its `post` counterpart. +#[test] +fn one_output_to_the_federation_below_the_minimum_fails_them_all() { + let outputs = [ + (min(), To::Active), + (min() + 1, To::Active), + (min() - 1, To::Active), + ]; + holds_before_rskip379(&outputs, false); + holds_from_rskip379(&outputs, false); +} + +/// rskj's `..._preRSKIP379WithManyOutputsForFedAllEqualOrAboveMinimum_true` +/// and its `post` counterpart. +#[test] +fn outputs_to_the_federation_all_of_the_minimum_or_more_pass() { + let outputs = [ + (min(), To::Active), + (min() + 1, To::Active), + (min() + 1, To::Active), + ]; + holds_before_rskip379(&outputs, true); + holds_from_rskip379(&outputs, true); +} + +/// rskj's `..._preRSKIP379UtxosToRetiringFedAboveMinimumAndToActiveFedBelowMinimum_false` +/// and its `post` counterpart: the minimum to the retiring federation +/// doesn't make up for an output below it to the active one. +#[test] +fn an_output_to_the_active_federation_below_the_minimum_fails_those_to_the_retiring_one() { + let outputs = [ + (min() - 1, To::Active), + (min(), To::Active), + (min() + 1, To::Retiring), + (min() + 1, To::Retiring), + ]; + holds_before_rskip379(&outputs, false); + holds_from_rskip379(&outputs, false); +} + +/// rskj's `..._preRSKIP379UtxosToRetiringAndActiveFedEqualToAndAboveMinimum_true` +/// and its `post` counterpart. +#[test] +fn outputs_of_the_minimum_or_more_to_both_federations_pass() { + let outputs = [ + (min(), To::Active), + (min() + 1, To::Active), + (min(), To::Retiring), + (min() + 1, To::Retiring), + ]; + holds_before_rskip379(&outputs, true); + holds_from_rskip379(&outputs, true); +} + +/// rskj's `..._preRSKIP379Exactly1UtxoToFedWithMinimumValue_true` and +/// `..._postRSKIP379Exactly1UtxoToFedWithMinimumValue_true`, whose `post` +/// test runs under fingerroot500 again; here it runs from RSKIP379. +#[test] +fn one_output_of_exactly_the_minimum_passes() { + let outputs = [(min(), To::Active)]; + holds_before_rskip379(&outputs, true); + holds_from_rskip379(&outputs, true); +} + +/// rskj's `..._preRSKIP379UtxosBelowMinimumToBothRetiringAndActiveFed_false` +/// and its `post` counterpart. +#[test] +fn outputs_below_the_minimum_to_both_federations_fail() { + let outputs = [ + (min() - 1, To::Active), + (min() - 1, To::Active), + (min() - 1, To::Retiring), + (min() - 1, To::Retiring), + ]; + holds_before_rskip379(&outputs, false); + holds_from_rskip379(&outputs, false); +} diff --git a/crates/execution/src/rskj_tests/bridge_pegouts/peg_utils_get_transaction_type_test.rs b/crates/execution/src/rskj_tests/bridge_pegouts/peg_utils_get_transaction_type_test.rs new file mode 100644 index 0000000..bf5a198 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegouts/peg_utils_get_transaction_type_test.rs @@ -0,0 +1,58 @@ +//! rskj's `PegUtilsGetTransactionTypeTest`: how `PegUtils.getTransactionType` +//! classifies a transaction, by the legacy rules before RSKIP379 and below +//! the pegout index's start, by the index from it. +//! +//! rskj's tests ask the classifier; here the class is read in what +//! `registerBtcTransaction` does, in the runs of the ports of +//! `BridgeSupportRegisterBtcTransactionTest`: a peg-in is taken or turned +//! down, a peg-out or a migration is processed with no event and its outputs +//! to the active federation registered. Most of its listed peg-out and +//! migration cases are the transactions those ports register; the rest are +//! here. As there, the federations are standard multisig ones. + +use super::support::*; + +/// rskj's `many_outputs_and_inputs_pegout` and +/// `many_outputs_and_one_input_pegout`: a spend of the active federation, by +/// fifty inputs or by one, to forty users and no change, is a peg-out: +/// processed, and nothing registered. +#[test] +fn a_spend_of_the_active_federation_to_forty_users_is_a_peg_out() { + let outputs = forty_outputs(|n| user(n + 1)); + for inputs in [50, 1] { + for run in RUNS { + let outcome = register_spend(run, false, &active(), inputs, &outputs); + assert_moved(&outcome, run, 0); + } + } +} + +/// rskj's `anyAddressToAnyAddress_pegin_before_RSKIP379`: before RSKIP379 a +/// transaction that pays the minimum to an empty script, from an input with +/// none either, is a peg-in by the legacy rules, none of its outputs to a +/// federation being below the minimum. It is registered as one: its sender +/// can't be named, so it can't be refunded, and it is processed (the legacy +/// path's `rejected_pegin` PEGIN_V1_INVALID_PAYLOAD and `unrefundable_pegin` +/// LEGACY_PEGIN_UNDETERMINED_SENDER). By the index +/// (`unknown_anyAddressToAnyAddress_unknown_after_RSIP379_and_using_pegout_tx_index`) +/// it is no peg-in, and is left alone. +#[test] +fn before_rskip379_a_transaction_between_any_addresses_is_a_peg_in() { + let tx = btc_transaction(vec![input(1, vec![])], &[(minimum_pegin(), vec![])]); + let outcome = BEFORE_RSKIP379 + .bridge(false) + .register(&tx, BEFORE_RSKIP379.height); + assert!(outcome.returned); + assert_eq!( + outcome.events, + vec![Event::Rejected(4), Event::Unrefundable(1)] + ); + assert_eq!(outcome.processed_at, Some(BEFORE_RSKIP379.block)); + + let outcome = RSKIP379_INDEXED + .bridge(false) + .register(&tx, RSKIP379_INDEXED.height); + assert!(outcome.returned); + assert!(outcome.events.is_empty()); + assert_eq!(outcome.processed_at, None); +} diff --git a/crates/execution/src/rskj_tests/bridge_pegouts/peg_utils_legacy_get_transaction_type_test.rs b/crates/execution/src/rskj_tests/bridge_pegouts/peg_utils_legacy_get_transaction_type_test.rs new file mode 100644 index 0000000..e28bd09 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegouts/peg_utils_legacy_get_transaction_type_test.rs @@ -0,0 +1,123 @@ +//! rskj's `PegUtilsLegacyGetTransactionTypeTest`: how the legacy rules +//! classify a transaction, here a spend of the old federation, which +//! RSKIP186 (the last retired federation) and RSKIP199 (the old federation's +//! address) make a migration. + +use super::support::*; + +/// rskj regtest's `oldFederationAddress`. +const REGTEST_OLD_FEDERATION_ADDRESS: &str = "2N7ZgQyhFKm17RbaLqygYbS7KLrQfapyZzu"; + +/// rskj's `REGTEST_OLD_FEDERATION_PRIVATE_KEYS`. +const OLD_FEDERATION_PRIVATE_KEYS: [&str; 3] = [ + "47129ffed2c0273c75d21bb8ba020073bb9a1638df0e04853407461fdd9e8b83", + "9f72d27ba603cfab5a0201974a6783ca2476ec3d6b4e2625282c682e0e5f1c35", + "e1b17fcd0ef1942465eee61b20561b16750191143d365e71de08b33dd84a9788", +]; + +fn private(hex: &str) -> Vec { + alloy_primitives::hex::decode(hex).expect("a private key in hex") +} + +/// Their public keys. +fn old_federation() -> Vec<[u8; 33]> { + OLD_FEDERATION_PRIVATE_KEYS + .iter() + .map(|key| public_key(&private(key))) + .collect() +} + +/// `tx` with its first input a spend of `redeem`, signed as rskj's +/// `FederationTestUtils.addSignatures` signs it: OP_0, a SIGHASH_ALL +/// signature by each of the first two of the old federation's keys, in the +/// order their public keys stand in `redeem`, and `redeem` pushed last. The +/// transaction has no witness, so Bitcoin's legacy signature hash is +/// bitcoinj's. +fn signed_by_the_old_federation( + mut tx: bitcoin::Transaction, + redeem: &[u8], +) -> bitcoin::Transaction { + use bitcoin::hashes::Hash as _; + use k256::ecdsa::signature::hazmat::PrehashSigner as _; + let hash = bitcoin::sighash::SighashCache::new(&tx) + .legacy_signature_hash(0, bitcoin::Script::from_bytes(redeem), 1) + .expect("the first input") + .to_byte_array(); + let mut signers: Vec> = OLD_FEDERATION_PRIVATE_KEYS[..2] + .iter() + .map(|key| private(key)) + .collect(); + signers.sort_by_key(|signer| { + let key = public_key(signer); + redeem.windows(33).position(|window| window == key) + }); + let mut script_sig = vec![0x00]; + for signer in signers { + let key = k256::ecdsa::SigningKey::from_slice(&signer).expect("a private key in range"); + let signature: k256::ecdsa::Signature = key.sign_prehash(&hash).expect("a signature"); + script_sig.extend(push(&[signature.to_der().as_bytes(), &[0x01]].concat())); + } + script_sig.extend(push(redeem)); + tx.input[0].script_sig = bitcoin::ScriptBuf::from_bytes(script_sig); + tx +} + +/// rskj's `test_sentFromOldFed`: a spend of regtest's old federation, at +/// `2N7ZgQyhFKm17RbaLqygYbS7KLrQfapyZzu`, signed by two of its three members, +/// to the active federation. Under papyrus200, before RSKIP186 and RSKIP199, +/// it is a peg-in, from a multisig sender, and is refunded; under iris300 it +/// is a migration, its output registered to the active federation, whether +/// or not the old federation is also stored as the last retired one. +#[test] +fn from_rskip199_a_spend_of_the_old_federation_is_a_migration() { + use std::str::FromStr; + let redeem = standard_redeem(&old_federation()); + let address = bitcoin::Address::from_str(REGTEST_OLD_FEDERATION_ADDRESS) + .expect("an address") + .assume_checked(); + assert_eq!( + address.script_pubkey().into_bytes(), + p2sh(&hash160(&redeem)), + "the old federation's keys are those of its address" + ); + let tx = signed_by_the_old_federation( + btc_transaction( + vec![input(1, Vec::new())], + &[(100_000_000, address_of(&active()))], + ), + &redeem, + ); + let at = |last: RskNetworkUpgrade, last_retired: bool| { + let cfg = up_to(last, RSK_REGTEST_CHAIN_ID); + let mut bridge = Registration::new(cfg, BridgeConstants::rskj_regtest(), 1_000); + bridge.federation(NEW_FEDERATION, &active()); + if last_retired { + put_named( + &mut bridge.ctx, + "lastRetiredFedP2SHScript", + &rlp_encode_list(&[rlp_encode_element(&p2sh(&hash160(&redeem)))]), + ); + } + bridge.register(&tx, 1_000) + }; + + let outcome = at(RskNetworkUpgrade::Papyrus200, false); + assert!(outcome.returned); + assert_eq!(outcome.events, vec![Event::ReleaseRequested(100_000_000)]); + assert_eq!(outcome.processed_at, Some(1_000)); + assert!(outcome.active_utxos.is_empty()); + assert_eq!(outcome.refunds, 1); + + for last_retired in [true, false] { + let outcome = at(RskNetworkUpgrade::Iris300, last_retired); + assert!(outcome.returned); + assert!(outcome.events.is_empty(), "last retired {last_retired}"); + assert_eq!(outcome.processed_at, Some(1_000)); + assert_eq!( + outcome.active_utxos, + vec![100_000_000], + "last retired {last_retired}" + ); + assert_eq!(outcome.refunds, 0); + } +} diff --git a/crates/execution/src/rskj_tests/bridge_pegouts/pegouts_waiting_for_confirmations_test.rs b/crates/execution/src/rskj_tests/bridge_pegouts/pegouts_waiting_for_confirmations_test.rs new file mode 100644 index 0000000..0c63851 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegouts/pegouts_waiting_for_confirmations_test.rs @@ -0,0 +1,153 @@ +//! rskj's `PegoutsWaitingForConfirmationsTest`, and the set `updateCollections` +//! walks for the next peg-out with its confirmations. Before RSKIP559, which +//! is `tbd1000` in the release's `reference.conf` and so on no network, +//! `getNextPegoutWithEnoughConfirmations` takes the first confirmed peg-out in +//! the order a Java `HashSet` walks its table: bucket by bucket, in a table +//! sized when `BridgeStorageProvider` read the set and grown by what the same +//! call added to it. + +use super::support::*; +use crate::bridge::peg::{self, PegoutWaitingForConfirmations}; + +/// The transaction rskj's test builds: one spending output 0 of a +/// transaction that pays fifty bitcoins to the key of the private key +/// 123456, its input disconnected, and paying `value` to the key of the +/// private key `to`. +fn paying(to: u8, value: u64) -> bitcoin::Transaction { + let funding = btc_transaction( + vec![], + &[(50 * BTC, p2pkh(&hash160(&public_key(&[0x01, 0xe2, 0x40]))))], + ); + let spend = bitcoin::TxIn { + previous_output: bitcoin::OutPoint { + txid: funding.compute_txid(), + vout: 0, + }, + ..input(0, vec![]) + }; + btc_transaction(vec![spend], &[(value, p2pkh(&hash160(&public_key(&[to]))))]) +} + +/// `getNextPegoutWithEnoughConfirmation_multipleMatch_rskip559Off`: of the +/// eight peg-outs rskj's test keeps, the four made at block 5 have their five +/// confirmations at block 10, and the one taken is the first of them the +/// set's table of 16 walks to, `53efc6f7…`. +#[test] +fn before_rskip559_the_next_pegout_is_the_first_confirmed_one_the_set_walks_to() { + let entries: Vec = [ + (2, 150, 32), + (5, BTC, 100), + (4, 50 * BTC, 7), + (3, BTC / 1000, 10), + (8, 5 * (BTC / 100), 5), + (11, BTC / 1000, 5), + (12, BTC / 1000, 5), + (13, BTC / 1000, 5), + ] + .into_iter() + .map(|(to, value, height)| PegoutWaitingForConfirmations { + btc_tx_raw: bitcoin::consensus::serialize(&paying(to, value)), + rsk_block_height: height, + rsk_tx_hash: None, + }) + .collect(); + let next = peg::next_pegout_with_enough_confirmations(&entries, 10, 5, entries.len()) + .expect("a peg-out with its confirmations"); + let taken: bitcoin::Transaction = + bitcoin::consensus::deserialize(&entries[next].btc_tx_raw).expect("a transaction"); + assert_eq!( + taken.compute_txid().to_string(), + "53efc6f78eb9d159cfee76ec45bcffb08fd11f85c762e1eacf54e5c014da219d" + ); +} + +/// Mainnet's two peg-outs made at block #3,340,302, both confirmed at +/// #3,344,303. This one, `20806dad…`, is in bucket 15 of a table of 32 and +/// the other in bucket 21; rskj took this one at #3,344,303, from a set of +/// twelve it read. +const FIRST_OF_32_BUCKETS: &str = "010000000158fc2de209f41b6faa714856b621aec5b268369804046269e017fc7107ead5ca01000000fdc80100000000000000004dbd0157210231a395e332dde8688800a0025cccc5771ea1aa874a633b8ab6e5c89d300c7c3621026b472f7d59d201ff1f540f111b6eb329e071c30a9d23e3d2bcd128fe73dc254c21027319afb15481dbeb3c426bcc37f9a30e7f51ceff586936d85548d9395bcc2344210294c817150f78607566e961b3c71df53a22022a80acbb982f83c0c8baac040adc2103250c11be0561b1d7ae168b1f59e39cbc1fd1ba3cf4d2140c1a365b2723a2bf9321033ada6ef3b1d93a1978b595c7a9e2aa613860b26d4f5a7abb88576aa42b3432ad210357f7ed4c118e581f49cd3b4d9dd1edb4295f4def49d6dcf2faaaaac87a1a0a42210372cd46831f3b6afd4c044d160b7667e8ebf659d6cb51a825a3104df6ee0638c62103ae72827d25030818c4947a800187b1fbcc33ae751e248ae60094cc989fb880f62103b3a7aa25702000c5c1faa300600e8e2bd89cde2be7fb1ec898a39c50d9de90d12103b53899c390573471ba30e5054f78376c5f797fda26dde7a760789f02908cbad22103e05bf6002b62651378b1954820539c36ca405cbb778c225395dd9ebff67802992103ecd8af1e93c57a1b8c7f917bd9980af798adeb0205e9687865673353eb041e8d5daeffffffff02c2f2aa09000000001976a914fb53759d716de10362f5b57d28151a6481fd30e188ac132c42980000000017a914596cff92a275960df9cb2ab9df0ff69faa2b1d8a8700000000"; + +/// The other, `415c14…`: in bucket 5 of a table of 16, where the first is +/// in bucket 15. +const FIRST_OF_16_BUCKETS: &str = "0100000001961576fed5c343511f011b32e11681b90ab0df1e33fde103913698476fd8ddca01000000fdc80100000000000000004dbd0157210231a395e332dde8688800a0025cccc5771ea1aa874a633b8ab6e5c89d300c7c3621026b472f7d59d201ff1f540f111b6eb329e071c30a9d23e3d2bcd128fe73dc254c21027319afb15481dbeb3c426bcc37f9a30e7f51ceff586936d85548d9395bcc2344210294c817150f78607566e961b3c71df53a22022a80acbb982f83c0c8baac040adc2103250c11be0561b1d7ae168b1f59e39cbc1fd1ba3cf4d2140c1a365b2723a2bf9321033ada6ef3b1d93a1978b595c7a9e2aa613860b26d4f5a7abb88576aa42b3432ad210357f7ed4c118e581f49cd3b4d9dd1edb4295f4def49d6dcf2faaaaac87a1a0a42210372cd46831f3b6afd4c044d160b7667e8ebf659d6cb51a825a3104df6ee0638c62103ae72827d25030818c4947a800187b1fbcc33ae751e248ae60094cc989fb880f62103b3a7aa25702000c5c1faa300600e8e2bd89cde2be7fb1ec898a39c50d9de90d12103b53899c390573471ba30e5054f78376c5f797fda26dde7a760789f02908cbad22103e05bf6002b62651378b1954820539c36ca405cbb778c225395dd9ebff67802992103ecd8af1e93c57a1b8c7f917bd9980af798adeb0205e9687865673353eb041e8d5daeffffffff027e0c3012000000001976a9149fc530162154c490cb5ca484b8ad883d0bf18a1788ac48e38d410100000017a914596cff92a275960df9cb2ab9df0ff69faa2b1d8a8700000000"; + +/// The block the two were made at. +const MADE_AT: u64 = 3_340_302; + +fn raw(hex: &str) -> Vec { + alloy_primitives::hex::decode(hex).expect("hex") +} + +/// Waiting for confirmations, each under its creation transaction's hash: +/// the two peg-outs above, and nine made a block before the Bridge's, none +/// of them confirmed. Eleven, as `BridgeStorageProvider` reads them. +fn eleven_waiting(collections: &mut Collections) { + let number = collections.bridge.number(); + let mut entries: Vec = + [FIRST_OF_32_BUCKETS, FIRST_OF_16_BUCKETS] + .into_iter() + .zip(0x40u8..) + .map(|(hex, tag)| PegoutWaitingForConfirmations { + btc_tx_raw: raw(hex), + rsk_block_height: MADE_AT, + rsk_tx_hash: Some([tag; 32]), + }) + .collect(); + for tag in 0x60..0x69u8 { + entries.push(PegoutWaitingForConfirmations { + btc_tx_raw: bitcoin::consensus::serialize(&btc_transaction( + vec![input(tag, vec![])], + &[(BTC, user(tag))], + )), + rsk_block_height: number - 1, + rsk_tx_hash: Some([tag; 32]), + }); + } + peg::store_pegout_confirmation_set(&mut collections.bridge.ctx, &entries, true); +} + +/// The one peg-out waiting for signatures. +fn waiting_for_signatures(collections: &mut Collections) -> Vec { + let kept = peg::deserialize_rsk_txs_waiting_for_signatures( + &named(&mut collections.bridge.ctx, "rskTxsWaitingFS").unwrap_or_default(), + ); + assert_eq!(kept.len(), 1, "one peg-out waits for signatures"); + kept.into_values().next().expect("one") +} + +/// rskj reads eleven, and adds the batch the call builds to that same set: +/// the twelfth leaves its table of 16 three quarters full and not more, so +/// it is walked at 16, where a set of twelve read from storage would be +/// walked at 32. +#[test] +fn a_pegout_built_in_the_call_leaves_the_set_walked_at_the_size_it_was_read() { + let mut collections = Collections::mainnet_at(MAINNET_HOP400); + collections.active(1, 10); + collections.requests(3); + eleven_waiting(&mut collections); + collections.update(); + assert_eq!( + collections.waiting().len(), + 11, + "one taken, the batch added" + ); + assert_eq!( + waiting_for_signatures(&mut collections), + raw(FIRST_OF_16_BUCKETS) + ); +} + +/// The migration and the batch the call builds both go into the set it +/// read, and the thirteenth doubles its table to 32. +#[test] +fn a_migration_and_a_pegout_built_in_the_call_double_the_set_walked() { + let mut collections = Collections::migrating(MAINNET_HOP400, STANDARD_MULTISIG, 10, false); + collections.requests(3); + eleven_waiting(&mut collections); + collections.update(); + assert_eq!(collections.waiting().len(), 12, "one taken, two added"); + assert_eq!( + waiting_for_signatures(&mut collections), + raw(FIRST_OF_32_BUCKETS) + ); +} diff --git a/crates/execution/src/rskj_tests/bridge_pegouts/poc_sighash_test.rs b/crates/execution/src/rskj_tests/bridge_pegouts/poc_sighash_test.rs new file mode 100644 index 0000000..36881a5 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegouts/poc_sighash_test.rs @@ -0,0 +1,399 @@ +//! rskj's `PocSighashTest`: what the legacy signature hash of a peg-out's +//! input depends on, the hash a member signs and the pegout index keys by +//! (`BtcTransaction.hashForSignature(index, redeem, ALL, false)`). Each input +//! has its own; it covers no scriptSig, so an input signed and unsigned has +//! the same; it changes with the transaction; and over a peg-out testnet's +//! federators signed and broadcast, it is what rskj computes. The federation +//! is a P2SH ERP one of keys from seeds, with three emergency keys after 720 +//! blocks. rskj runs three of the tests on mainnet's and on testnet's +//! parameters, which name addresses alone: the hashes are the same on both, +//! and the ports run once. + +use std::collections::BTreeSet; + +use bitcoin::{ + absolute::LockTime, transaction::Version, Amount, Network, OutPoint, ScriptBuf, Sequence, + Transaction, TxIn, TxOut, Witness, +}; +use k256::ecdsa::{signature::hazmat::PrehashSigner, Signature, SigningKey}; + +use crate::bridge::peg::p2sh_erp_redeem_script; +use crate::bridge::release_tx::{legacy_sighash_all, placeholder_scriptsig, push_data}; +use crate::bridge::script_spend::script_correctly_spends_tx; + +use super::support::{p2pkh, p2sh}; + +/// The emergency keys' activation delay, in blocks. +const DELAY: i64 = 720; + +/// The emergency keys, by seed. +const EMERGENCY: [&str; 3] = ["erp-fed-01", "erp-fed-02", "erp-fed-03"]; + +/// A federation of seven, by seed. +const SEVEN: [&str; 7] = ["fed1", "fed2", "fed3", "fed4", "fed5", "fed6", "fed7"]; + +/// Testnet's federators of the peg-out below, by seed. +const TESTNET_FEDERATORS: [&str; 3] = ["federator1", "federator2", "federator6"]; + +/// Where testnet's federation of [`TESTNET_FEDERATORS`] is. +const TESTNET_ADDRESS: &str = "2NEfaGq4tGe6bJUxLEzGFoVyZrSrZtXRzJ7"; + +/// The transactions that paid that federation what the peg-out spends, with +/// the output of each it spends. +const FUNDING: [(&str, u32); 4] = [ + ("0200000000010134ed7734da14ecde305347153f70be45021e8e29137205a9899630f56c68d3890100000000fdffffff02891300000000000017a914eaf58ece160a383630667cfc1ccff519ab07c472873c3e020000000000160014f885b26136ad4d61247132271795cc29ae9cec0302473044022063986423838cdd2abc51b150f11a1c399dce53f68f1bd02e4ec793db2fa3485c022007d8909a83b01c57dcee7e55f3989af949756c6b5068948b888622d2e6673ed70121028f117bfbc90d934b73d0d55afe54ee33a288cdde572bd3d006cede67f4c797a2e7262500", 0), + ("020000000001013a43f0d972f5045b443c9c071bd2254f91d5b14d0b5ab5cdeab014c0ff2cc17f0200000000fdffffff027d6408000000000017a914eaf58ece160a383630667cfc1ccff519ab07c472870555dc0500000000220020617a81e635a62aa61d20fd4be369d285da2750c871016282c05a29181552d52b0400473044022048386d4e0270b68a17d3ea10d8c569c2150cb46416c82b3289f2e0ec1fe0272a02204f077bdc8f71d824a4a8a447708cb974e21c015021b94f8226a3da955024371e0147304402204fda66a5cc3503acd325671216f5dbfe4d329499321af508c4959e89072dc47802204531cb9a52c80ede1cab8ca40324ef18ddfb589a0b76f741764334f6f1e9570f014752210203307a637032952b21371f966838e03a768220f8bb663cbc2aa6d41dc71f6bbf2103fdadd0c267fc7a4890c37c6730ca124d53ad7001ee6d9ad00ed50b52a46d784552aee7262500", 0), + ("020000000001016a040fa1ec01bac865d4802083a435fef32052091d6d7bd02cc784d00d75e8400100000000fdffffff02916408000000000017a914eaf58ece160a383630667cfc1ccff519ab07c47287beefd305000000002200201ca17d3f37320ac4469faa1119824d98bbec3b1a586174901834d2b842715e7c0400473044022044033baafc0c15c8e2c3128d01f29a7c7c5c6ec790ead8c5be2610efb0e9d581022014b605cb59a5254a189a9fcd35ec94e6f4faae18f7011a41ac7663df33fbfdc50147304402206fc734a5db737ecb37056491881da15c2f8f2ca2bb9e47db7396029b9a0ad5e102206cae6f8766679c666cfa256e886e2816882623167e92fd9e272fd9712aa49d9a0147522102b9bb9ea5c5c0a56421bc7720e81e7f3e5fb9dfcbca946f01d868940efaa5da18210309eb318fe76a6b139d70e4037dd87733ced9e3ed6f851301a88938c7033d473b52ae05272500", 0), + ("0200000001a223ad7e9d4f5067a8fdddf9e4137af49d5a6dad7ea952533f701061fefd010f020000006a473044022003d999e14be1e2ea15cb16df81e14b30f26c684bcbf6fcbd07975d8648e5f04402207e99a414a0ebf1c3617944610de5ea002439a1ac56e667a60c095184c2419559012102c6bf1e099ec95510a8da3d1c67026cb65a019bad57dc2255dd56a426c629327bfdffffff0300000000000000001b6a1952534b540162db6c4b118d7259c23692b162829e6bd5e4d5b0891300000000000017a914eaf58ece160a383630667cfc1ccff519ab07c4728791200000000000001976a9145952b24450e80668e069b8152a3a38ea7f6ad44c88ace7262500", 1), +]; + +/// The peg-out the federators signed and testnet confirmed. +const BROADCAST: &str = "02000000044928841a8bcded3af365da1b57b98b96e4593893862599c504f883b0c956751600000000fd700100483045022100d05a33bd8d0032ed9ccd5f3ba1fb085166c9155f03d95a79e65ef9cf60b9e4b602201287eedce988adf75d8daadc978eca43f47bc33ec999652008e85a3d3dd47f8001483045022100eb5f917967f772328dfd82bd27a096cac453ca336a51d1782ca6684304dd34d5022055bd25e56ed2702974fb58224481e393fca12c4ff34771dab5f456c8fa76568401004cda64522103462ab7041341dadd996dc12ef0c118ca8ccb546498cbf304f7ffe0f1b12f9a9e210362634ab57dae9cb373a5d536e66a8c4f67468bbcfb063809bab643072d78a1242103c5946b3fbae03a654237da863c9ed534e0878657175b132b8ca630f245df04db53ae6702d002b2755221029cecea902067992d52c38b28bf0bb2345bda9b21eca76b16a17c477a64e433012103284178e5fbcc63c54c3b38e3ef88adf2da6c526313650041b0ef955763634ebd2103b9fc46657cf72a1afa007ecf431de1cd27ff5cc8829fa625b66ca47b967e6b2453ae68ffffffff6a040fa1ec01bac865d4802083a435fef32052091d6d7bd02cc784d00d75e84000000000fd6e010047304402206a00bb7a876ecbacd13c06a3e8ca3c55d022e9f35ad6211c65bc4ab3e2a12f1b022051a1d0861a5e812c6c2ffa1be0467fe953ceff6ecb9fc3eb68843441d5c3d7a501473044022044744cceb33a776928c61c8d01cf3acf58c7159ccd5a69914cb78be2a59d5b2d022020ff71156583ba563e9a4d6a8f0bd463d53a6612e831082e0ac9c1c2d1a6d79101004cda64522103462ab7041341dadd996dc12ef0c118ca8ccb546498cbf304f7ffe0f1b12f9a9e210362634ab57dae9cb373a5d536e66a8c4f67468bbcfb063809bab643072d78a1242103c5946b3fbae03a654237da863c9ed534e0878657175b132b8ca630f245df04db53ae6702d002b2755221029cecea902067992d52c38b28bf0bb2345bda9b21eca76b16a17c477a64e433012103284178e5fbcc63c54c3b38e3ef88adf2da6c526313650041b0ef955763634ebd2103b9fc46657cf72a1afa007ecf431de1cd27ff5cc8829fa625b66ca47b967e6b2453ae68ffffffff114c09191b5b1fe685640c23a2ab95bfec521d49ddde4bb96032471762b441e400000000fd6f0100483045022100979a3c231a8ca8c1aa081dffe6cbe0d7ad5987ce4329757cfd8e0c3bfbc5ba8c02202357deaf74162fefc1a05ddf4c2bb2b7054fa105ceddf6ed4e061dab88ae056f0147304402204ebb79cbc3fd9260e28b1f9c4564f3352ce3adba6dc390279808d42961152c9a022034c215c5e9fc88b1622d0636950b5dd1de7b1322e9857736f9a3a63e9bb2cdad01004cda64522103462ab7041341dadd996dc12ef0c118ca8ccb546498cbf304f7ffe0f1b12f9a9e210362634ab57dae9cb373a5d536e66a8c4f67468bbcfb063809bab643072d78a1242103c5946b3fbae03a654237da863c9ed534e0878657175b132b8ca630f245df04db53ae6702d002b2755221029cecea902067992d52c38b28bf0bb2345bda9b21eca76b16a17c477a64e433012103284178e5fbcc63c54c3b38e3ef88adf2da6c526313650041b0ef955763634ebd2103b9fc46657cf72a1afa007ecf431de1cd27ff5cc8829fa625b66ca47b967e6b2453ae68ffffffffefd39f04aa64aec104c68bbecef06dc8ffa916cec5c536b8e03a19e40b2f9af101000000fd700100483045022100a668d635ca8528be0b0affc22c64636f2128b1a1a0891a5f3d2c2579eb9ab4ea0220450088cb20042ce52ebd32c61f2d3b5085c6cd725c36ffae1a751878091a81b601483045022100e25e9ee15ac340063a27258ca905c273cd6fa907271c3d5ddcf1daea2969ddc6022059f190175e98636708bbc4c9cb9809216ee09a0f0d2794e9bf87220567e43c7401004cda64522103462ab7041341dadd996dc12ef0c118ca8ccb546498cbf304f7ffe0f1b12f9a9e210362634ab57dae9cb373a5d536e66a8c4f67468bbcfb063809bab643072d78a1242103c5946b3fbae03a654237da863c9ed534e0878657175b132b8ca630f245df04db53ae6702d002b2755221029cecea902067992d52c38b28bf0bb2345bda9b21eca76b16a17c477a64e433012103284178e5fbcc63c54c3b38e3ef88adf2da6c526313650041b0ef955763634ebd2103b9fc46657cf72a1afa007ecf431de1cd27ff5cc8829fa625b66ca47b967e6b2453ae68ffffffff0124e710000000000017a9140e39a099dabb03863ae5484943896a7c9eb9f9dd8700000000"; + +/// What the peg-out pays, and to whom. +const PAID: u64 = 1_107_748; +const PAID_TO: &str = "2MtYSUzFWEQV62r92bsGX8ewE5Mgpv4xn9M"; + +/// rskj's `FedSigner.listOf`: the keys of the private keys `keccak256(seed)`, +/// in the order of their public keys. +fn signers(seeds: &[&str]) -> Vec { + use sha3::Digest; + let mut signers: Vec = seeds + .iter() + .map(|seed| { + SigningKey::from_slice(&sha3::Keccak256::digest(seed.as_bytes())).expect("a key") + }) + .collect(); + signers.sort_by_key(public); + signers +} + +/// A key's compressed public key. +fn public(key: &SigningKey) -> [u8; 33] { + key.verifying_key() + .to_encoded_point(true) + .as_bytes() + .try_into() + .expect("33 bytes") +} + +/// rskj's `getSignaturesPermutations`: the majorities of `n` signers it signs +/// with, each a signer and a run of signers after it. +fn majorities(n: usize) -> Vec> { + let run = n / 2; + let mut all = Vec::new(); + for first in 0..n { + for start in first + 1..n { + if start + run > n { + break; + } + all.push(std::iter::once(first).chain(start..start + run).collect()); + } + } + all +} + +/// A P2SH ERP federation of members' keys, with [`EMERGENCY`]'s after +/// [`DELAY`] blocks. +struct Federation { + members: Vec, + emergency: Vec, + redeem: Vec, +} + +impl Federation { + fn of(members: &[&str]) -> Self { + let members = signers(members); + let emergency = signers(&EMERGENCY); + let keys = |signers: &[SigningKey]| signers.iter().map(public).collect::>(); + let redeem = p2sh_erp_redeem_script(&keys(&members), &keys(&emergency), DELAY); + Self { + members, + emergency, + redeem, + } + } + + /// Its P2SH output script. + fn script_pubkey(&self) -> ScriptBuf { + ScriptBuf::new_p2sh(&ScriptBuf::from_bytes(self.redeem.clone()).script_hash()) + } + + /// Its address on testnet. + fn testnet_address(&self) -> String { + bitcoin::Address::p2sh( + &ScriptBuf::from_bytes(self.redeem.clone()), + Network::Testnet, + ) + .expect("a redeem script of at most 520 bytes") + .to_string() + } + + /// The sighash of input `index` of `tx`. + fn sighash(&self, tx: &Transaction, index: usize) -> [u8; 32] { + legacy_sighash_all(tx, index, &self.redeem) + } + + /// rskj's `spendFromFed`: a version 2 transaction spending `outpoints`, + /// all the federation's, paying `amount` satoshis to `to`. Each input is + /// signed in turn by each of the [`majorities`] of the members, or with + /// `emergency` of the emergency keys, each input's sequence then the + /// delay; each time it spends the federation's output as bitcoinj runs + /// it, and the last majority's signatures stay. The sighash of each input + /// is taken before it is signed. + fn spend( + &self, + emergency: bool, + outpoints: &[OutPoint], + amount: u64, + to: &ScriptBuf, + ) -> (Transaction, Vec<[u8; 32]>) { + let sequence = if emergency { + Sequence(DELAY as u32) + } else { + Sequence::MAX + }; + let mut tx = Transaction { + version: Version::TWO, + lock_time: LockTime::ZERO, + input: outpoints + .iter() + .map(|&previous_output| TxIn { + previous_output, + script_sig: ScriptBuf::new(), + sequence, + witness: Witness::new(), + }) + .collect(), + output: vec![TxOut { + value: Amount::from_sat(amount), + script_pubkey: to.clone(), + }], + }; + let signers = if emergency { + &self.emergency + } else { + &self.members + }; + let output = self.script_pubkey(); + let mut sighashes = Vec::new(); + for index in 0..tx.input.len() { + let sighash = self.sighash(&tx, index); + sighashes.push(sighash); + for majority in majorities(signers.len()) { + let mut script_sig = vec![0x00]; + for &signer in &majority { + let signature: Signature = + signers[signer].sign_prehash(&sighash).expect("signed"); + let mut encoded = signature.to_der().as_bytes().to_vec(); + encoded.push(0x01); + push_data(&mut script_sig, &encoded); + } + // The branch of the redeem script: OP_0 the members', OP_1 the + // emergency keys'. + script_sig.push(if emergency { 0x51 } else { 0x00 }); + push_data(&mut script_sig, &self.redeem); + tx.input[index].script_sig = ScriptBuf::from_bytes(script_sig); + assert!( + script_correctly_spends_tx(&tx, index, output.as_bytes()), + "input {index} signed by {majority:?}" + ); + } + } + (tx, sighashes) + } +} + +/// A transaction of no inputs paying each of `outputs`, as rskj's tests make +/// a peg-in, and the output of it the federation holds. +fn paying(outputs: &[(u64, ScriptBuf)], held: u32) -> (Transaction, u32) { + let tx = Transaction { + version: Version::ONE, + lock_time: LockTime::ZERO, + input: vec![], + output: outputs + .iter() + .map(|(value, script_pubkey)| TxOut { + value: Amount::from_sat(*value), + script_pubkey: script_pubkey.clone(), + }) + .collect(), + }; + (tx, held) +} + +/// What the outputs `held` hold, and where they are. +fn outpoints_of(held: &[(Transaction, u32)]) -> (u64, Vec) { + let total = held + .iter() + .map(|(tx, index)| tx.output[*index as usize].value.to_sat()) + .sum(); + let outpoints = held + .iter() + .map(|(tx, index)| OutPoint::new(tx.compute_txid(), *index)) + .collect(); + (total, outpoints) +} + +/// Someone outside the federation: rskj's random P2PKH address. +fn someone() -> ScriptBuf { + ScriptBuf::from_bytes(p2pkh(&[0x55; 20])) +} + +/// The P2SH outputs of the [`FUNDING`] transactions. +fn funding() -> Vec { + FUNDING + .iter() + .map(|(raw, index)| { + let bytes = alloy_primitives::hex::decode(raw).expect("hex"); + let tx: Transaction = bitcoin::consensus::deserialize(&bytes).expect("a transaction"); + OutPoint::new(tx.compute_txid(), *index) + }) + .collect() +} + +/// [`PAID_TO`]'s output script. +fn paid_to() -> ScriptBuf { + PAID_TO + .parse::>() + .expect("an address") + .require_network(Network::Testnet) + .expect("testnet's") + .script_pubkey() +} + +/// The [`BROADCAST`] peg-out. +fn broadcast() -> Transaction { + let bytes = alloy_primitives::hex::decode(BROADCAST).expect("hex"); + bitcoin::consensus::deserialize(&bytes).expect("a transaction") +} + +/// rskj's `test_each_input_sighash_is_unique`: nine outputs the federation +/// holds, seven of peg-ins and two beside an output to another, a P2PKH and +/// a multisig one, are the inputs of a peg-out, each with a sighash of its +/// own, which the signed peg-out keeps. +#[test] +fn each_input_of_a_pegout_has_a_sighash_of_its_own() { + let fed = Federation::of(&SEVEN); + let mut held: Vec<(Transaction, u32)> = (1..=7) + .map(|i| paying(&[(100_000 * i, fed.script_pubkey())], 0)) + .collect(); + held.push(paying( + &[(500_001, someone()), (500_002, fed.script_pubkey())], + 1, + )); + let multisig = ScriptBuf::from_bytes(p2sh(&[0x66; 20])); + held.push(paying( + &[(700_001, multisig), (700_002, fed.script_pubkey())], + 1, + )); + let (total, outpoints) = outpoints_of(&held); + let (pegout, sighashes) = fed.spend(false, &outpoints, total - 15_000, &someone()); + let distinct: BTreeSet<[u8; 32]> = sighashes.iter().copied().collect(); + assert_eq!(distinct.len(), held.len()); + for index in 0..pegout.input.len() { + assert!( + distinct.contains(&fed.sighash(&pegout, index)), + "input {index}" + ); + } +} + +/// rskj's `test_sighash_is_different_when_tx_is_altered`: a peg-out of one +/// output the federation holds, then one of it and another: no input of the +/// second has a sighash of the first. +#[test] +fn a_pegout_of_one_more_output_has_none_of_the_sighashes() { + let fed = Federation::of(&SEVEN); + let mut held = vec![paying(&[(100_000, fed.script_pubkey())], 0)]; + let (total, outpoints) = outpoints_of(&held); + let (_, first) = fed.spend(false, &outpoints, total - 15_000, &someone()); + assert_eq!(first.len(), 1); + + held.push(paying(&[(100_000, fed.script_pubkey())], 0)); + let (total, outpoints) = outpoints_of(&held); + let (_, second) = fed.spend(false, &outpoints, total - 15_000, &someone()); + let distinct: BTreeSet<[u8; 32]> = second.iter().copied().collect(); + assert_eq!(distinct.len(), held.len()); + assert!(first.iter().all(|sighash| !distinct.contains(sighash))); +} + +/// rskj's `test_sighash_is_equal_for_signed_input_and_unsigned_input`: the +/// inputs of a signed peg-out given back the scriptSig the Bridge builds +/// them with, before any signature, have the sighashes they were signed over. +#[test] +fn an_input_has_its_sighash_signed_or_not() { + let fed = Federation::of(&["fed1", "fed2", "fed3"]); + let held: Vec<(Transaction, u32)> = (1..=7) + .map(|i| paying(&[(100_000 * i, fed.script_pubkey())], 0)) + .collect(); + let (total, outpoints) = outpoints_of(&held); + let (mut pegout, sighashes) = fed.spend(false, &outpoints, total - 15_000, &someone()); + assert_eq!(sighashes.iter().collect::>().len(), held.len()); + let threshold = fed.members.len() / 2 + 1; + for input in &mut pegout.input { + input.script_sig = ScriptBuf::from_bytes(placeholder_scriptsig(&fed.redeem, threshold)); + } + for (index, sighash) in sighashes.iter().enumerate() { + assert_eq!(&fed.sighash(&pegout, index), sighash, "input {index}"); + } +} + +/// rskj's `test_each_input_sighash_is_unique_using_real_tx_testnet`: the +/// peg-out testnet's federation of three built from four outputs it held, +/// signed again by its members, has the sighashes of the one they signed and +/// broadcast, input by input. +#[test] +fn a_pegout_signed_again_has_the_sighashes_of_the_one_testnet_confirmed() { + let fed = Federation::of(&TESTNET_FEDERATORS); + assert_eq!(fed.testnet_address(), TESTNET_ADDRESS); + let (pegout, sighashes) = fed.spend(false, &funding(), PAID, &paid_to()); + assert_eq!( + sighashes.iter().collect::>().len(), + FUNDING.len() + ); + let broadcast = broadcast(); + for (index, sighash) in sighashes.iter().enumerate() { + assert_eq!(&fed.sighash(&pegout, index), sighash, "input {index}"); + assert_eq!(&fed.sighash(&broadcast, index), sighash, "input {index}"); + } +} + +/// rskj's `test_each_input_sighash_is_unique_for_a_signed_erp_tx_testnet`: +/// the same peg-out signed by the emergency keys, its inputs' sequences the +/// delay, has a sighash of its own at each input, none the broadcast one's. +#[test] +fn the_emergency_keys_sign_other_sighashes_than_the_members() { + let fed = Federation::of(&TESTNET_FEDERATORS); + assert_eq!(fed.testnet_address(), TESTNET_ADDRESS); + let (pegout, sighashes) = fed.spend(true, &funding(), PAID, &paid_to()); + assert_eq!( + sighashes.iter().collect::>().len(), + FUNDING.len() + ); + let broadcast = broadcast(); + for index in 0..pegout.input.len() { + assert_ne!( + fed.sighash(&pegout, index), + fed.sighash(&broadcast, index), + "input {index}" + ); + } +} + +/// rskj's `test_redeemScript_can_be_obtained_from_input`: testnet's +/// federation of three is at its address. rskj's test then reads the redeem +/// script each input of a transaction pushes last, of a transaction it gives +/// no inputs; here an input as the Bridge builds it pushes the federation's. +#[test] +fn an_inputs_redeem_script_is_the_federations() { + let fed = Federation::of(&TESTNET_FEDERATORS); + assert_eq!(fed.testnet_address(), TESTNET_ADDRESS); + let threshold = fed.members.len() / 2 + 1; + let script_sig = ScriptBuf::from_bytes(placeholder_scriptsig(&fed.redeem, threshold)); + let pushed_last = match script_sig.instructions().last() { + Some(Ok(bitcoin::script::Instruction::PushBytes(bytes))) => bytes.as_bytes().to_vec(), + other => panic!("no redeem script pushed last: {other:?}"), + }; + assert_eq!(pushed_last, fed.redeem); +} diff --git a/crates/execution/src/rskj_tests/bridge_pegouts/point_at_infinity_signer_test.rs b/crates/execution/src/rskj_tests/bridge_pegouts/point_at_infinity_signer_test.rs new file mode 100644 index 0000000..224d633 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegouts/point_at_infinity_signer_test.rs @@ -0,0 +1,368 @@ +//! A federation member whose key is the point at infinity, the one byte +//! `00`, as a peg-out and the SVP spend transaction are signed. +//! +//! Not a port: no test of rskj's has such a member. rskj's `addSignature` +//! finds the member by that key (`Federation.hasBtcPublicKey`, +//! `getMemberByBtcPublicKey`), as any other by its own, and then verifies a +//! signature with it: `BtcECKey.verify` hands the point to Spongy Castle, +//! whose `ECPublicKeyParameters` refuses it with an +//! `IllegalArgumentException` ("point at infinity"), which nothing on the way +//! to the Bridge catches, so the call fails. Another member's first signature +//! of an input goes in; but once an input holds one, `getSigInsertionIndex` +//! looks it up with `findSigInRedeem`, which verifies it with the redeem +//! script's keys in order, the point at infinity first, and the call fails +//! alike. A member whose RSK key is that point is logged at its address. + +use alloy_primitives::Address; + +use super::support::*; +use crate::bridge::federation::rsk_address_from_public_key; + +/// The point at infinity's encoding. +const INFINITY: &[u8] = &[0x00]; + +/// The point at infinity's RSK address. +const INFINITY_ADDRESS: Address = + alloy_primitives::address!("dcc703c0e500b653ca82273b7bfad8045d85a470"); + +/// The hash of the RSK transaction that made the SVP spend transaction. +const SPEND_HASH: [u8; 32] = [0x5e; 32]; + +/// `serializeFederationMember` as an element of a federation's list: `btc` +/// its BTC and MST keys, `rsk` its RSK key, each as rskj encodes it. +fn member(btc: &[u8], rsk: &[u8]) -> Vec { + rlp_encode_element(&rlp_encode_list(&[ + rlp_encode_element(btc), + rlp_encode_element(rsk), + rlp_encode_element(btc), + ])) +} + +/// `serializeFederation` of `members`, given sorted, made at `block`. +fn federation(members: &[Vec], block: u64) -> Vec { + rlp_encode_list(&[ + serialize_long(0), + serialize_long(block), + rlp_encode_list(members), + ]) +} + +/// `ScriptBuilder.createRedeemScript`: `size / 2 + 1` of `keys`, given +/// sorted and as rskj encodes them, each pushed by `ScriptBuilder.data`, the +/// point at infinity's one byte as a one-byte push (`01 00`). +fn multisig_of(keys: &[&[u8]]) -> Vec { + let mut script = vec![0x50 + (keys.len() / 2 + 1) as u8]; + for key in keys { + script.push(key.len() as u8); + script.extend_from_slice(key); + } + script.push(0x50 + keys.len() as u8); + script.push(0xae); + script +} + +/// `P2shErpRedeemScriptBuilder` on mainnet: the members' multisig, and after +/// 52,560 blocks (`signedLongToByteArrayLE`: `50 cd 00`) the emergency +/// keys'. +fn p2sh_erp_of(keys: &[&[u8]], constants: &BridgeConstants) -> Vec { + assert_eq!(constants.erp_fed_activation_delay, 52_560); + let emergency: Vec<[u8; 33]> = constants + .erp_fed_pubkeys + .iter() + .map(|key| { + alloy_primitives::hex::decode(key) + .unwrap() + .try_into() + .unwrap() + }) + .collect(); + let mut script = vec![0x64]; + script.extend(multisig_of(keys)); + script.extend_from_slice(&[0x67, 0x03, 0x50, 0xcd, 0x00, 0xb2, 0x75]); + script.extend(standard_redeem(&emergency)); + script.push(0x68); + script +} + +/// The call data of `addSignature(key, signatures, hash)`. +fn add_signature(key: &[u8], signatures: &[Vec], hash: &[u8; 32]) -> Vec { + use crate::abi::encode::{args, Arg}; + let sent: Vec<&[u8]> = signatures.iter().map(Vec::as_slice).collect(); + [ + crate::abi::selector("addSignature(bytes,bytes[],bytes)").to_vec(), + args(&[Arg::Bytes(key), Arg::BytesArray(&sent), Arg::Bytes(hash)]), + ] + .concat() +} + +/// The DER signature of `sighash` by the private key `private`. +fn signature(private: u8, sighash: &[u8; 32]) -> Vec { + use k256::ecdsa::signature::hazmat::PrehashSigner; + let mut scalar = [0u8; 32]; + scalar[31] = private; + let key = k256::ecdsa::SigningKey::from_slice(&scalar).expect("a key"); + let signature: k256::ecdsa::Signature = key.sign_prehash(sighash).expect("signed"); + signature.to_der().as_bytes().to_vec() +} + +/// The RSK address of the private key `private`'s public key. +fn address_of_private(private: u8) -> Address { + rsk_address_from_public_key(&public_key(&[private])).expect("an address") +} + +/// Whether `tx`'s first input holds `signature` as bitcoinj writes it, in +/// its witness or its scriptSig. +fn holds(tx: &bitcoin::Transaction, signature: &[u8]) -> bool { + let written = [signature, &[0x01]].concat(); + let input = &tx.input[0]; + input.witness.iter().any(|push| push == written.as_slice()) + || input.script_sig.instructions().any(|instruction| { + matches!(instruction, Ok(bitcoin::script::Instruction::PushBytes(bytes)) + if bytes.as_bytes() == written.as_slice()) + }) +} + +/// A spending of a bitcoin of `redeem`'s, `threshold` of its keys, with no +/// signature yet (`addSpendingFederationBaseScript`): from its witness where +/// `segwit`, else from its scriptSig. +fn spending(redeem: &[u8], threshold: usize, segwit: bool) -> bitcoin::Transaction { + use crate::bridge::release_tx::{placeholder_scriptsig, segwit_base_input}; + let mut tx = btc_transaction(vec![input(1, Vec::new())], &[(BTC, user(9))]); + if segwit { + let (script_sig, witness) = segwit_base_input(redeem, threshold); + tx.input[0].script_sig = script_sig; + tx.input[0].witness = witness; + } else { + let script_sig = placeholder_scriptsig(redeem, threshold); + tx.input[0].script_sig = bitcoin::ScriptBuf::from_bytes(script_sig); + } + tx +} + +/// A Bridge at block `MAINNET_VETIVER900` of mainnet whose active federation +/// is of `members`, in format 4000 where `segwit` and 1000 otherwise, and +/// waiting for its signatures under [`RELEASE_HASH`] a peg-out of a bitcoin +/// of `redeem`'s, `threshold` of its keys, the value its input spends kept +/// where `segwit`, as the Bridge keeps it from RSKIP305. With what the input +/// is signed over. +fn waiting( + members: &[Vec], + redeem: &[u8], + threshold: usize, + segwit: bool, +) -> (Bridge, [u8; 32]) { + use crate::bridge::release_tx::{legacy_sighash_all, segwit_sighash_all}; + let mut bridge = Bridge::mainnet_at(MAINNET_VETIVER900); + put_named(&mut bridge.ctx, NEW_FEDERATION.0, &federation(members, 1)); + let format = if segwit { + P2SH_P2WSH_ERP + } else { + STANDARD_MULTISIG + }; + put_named(&mut bridge.ctx, NEW_FEDERATION.1, &serialize_long(format)); + let tx = spending(redeem, threshold, segwit); + put_named( + &mut bridge.ctx, + "rskTxsWaitingFS", + &rlp_encode_list(&[ + rlp_encode_element(&RELEASE_HASH), + rlp_encode_element(&bitcoin::consensus::serialize(&tx)), + ]), + ); + if !segwit { + return (bridge, legacy_sighash_all(&tx, 0, redeem)); + } + use bitcoin::hashes::Hash; + let mut hash = tx.compute_txid().to_raw_hash().to_byte_array(); + hash.reverse(); + let hash = crate::bridge::tx::to_hex(&hash); + let key = crate::bridge::storage::compound_key("releasesOutpointsValues", "-", &hash); + // One bitcoin as bitcoinj's `VarInt` writes it. + let value = vec![0xfe, 0x00, 0xe1, 0xf5, 0x05]; + crate::bridge::storage::bridge_store_raw(&mut bridge.ctx, key, Some(value)); + (bridge, segwit_sighash_all(&tx, 0, redeem, BTC)) +} + +/// A federation of the point at infinity and the keys of the private keys +/// 1 and 2, a member each, two of them to sign: its members, and its redeem +/// script, a P2SH ERP one where `segwit` and a standard multisig otherwise. +fn with_the_point_at_infinity(segwit: bool) -> (Vec>, Vec) { + let mut keys = [public_key(&[1]), public_key(&[2])]; + keys.sort(); + let members = vec![ + member(INFINITY, INFINITY), + member(&keys[0], &keys[0]), + member(&keys[1], &keys[1]), + ]; + let ordered: [&[u8]; 3] = [INFINITY, &keys[0], &keys[1]]; + let redeem = if segwit { + p2sh_erp_of(&ordered, &BridgeConstants::mainnet()) + } else { + multisig_of(&ordered) + }; + (members, redeem) +} + +/// The peg-out as it waits. +fn waiting_pegout(bridge: &mut Bridge) -> bitcoin::Transaction { + let entries = crate::bridge::peg::deserialize_rsk_txs_waiting_for_signatures( + &named(&mut bridge.ctx, "rskTxsWaitingFS").expect("waiting"), + ); + bitcoin::consensus::deserialize(&entries[&RELEASE_HASH]).expect("a transaction") +} + +/// A signature given for the point at infinity: rskj returns at the count +/// of signatures, one per input, before it looks the key up; with one, the +/// member is found, its signature is verified with the point at infinity, +/// and the call fails. Nothing is signed. +#[test] +fn a_signature_for_the_point_at_infinity_fails_the_call() { + for segwit in [false, true] { + let (members, redeem) = with_the_point_at_infinity(segwit); + let (mut bridge, sighash) = waiting(&members, &redeem, 2, segwit); + let before = named(&mut bridge.ctx, "rskTxsWaitingFS"); + bridge.caller = address_of_private(1); + let signed = signature(1, &sighash); + let two = add_signature(INFINITY, &[signed.clone(), signed.clone()], &RELEASE_HASH); + assert!( + bridge.execute(&two).is_ok(), + "two signatures, segwit: {segwit}" + ); + let one = add_signature(INFINITY, &[signed], &RELEASE_HASH); + assert!( + bridge.execute(&one).is_err(), + "one signature, segwit: {segwit}" + ); + assert_eq!( + named(&mut bridge.ctx, "rskTxsWaitingFS"), + before, + "segwit: {segwit}" + ); + } +} + +/// A member's first signature of an input goes in, its key found in the +/// redeem script by its bytes (`findKeyInRedeem`). Another member's then +/// fails the call: the signature held is looked up (`findSigInRedeem`) with +/// the point at infinity first. The peg-out waits as the first left it. +#[test] +fn once_an_input_holds_a_signature_another_members_fails_the_call() { + for segwit in [false, true] { + let (members, redeem) = with_the_point_at_infinity(segwit); + let (mut bridge, sighash) = waiting(&members, &redeem, 2, segwit); + bridge.caller = address_of_private(1); + let first = signature(1, &sighash); + let call = add_signature( + &public_key(&[1]), + std::slice::from_ref(&first), + &RELEASE_HASH, + ); + bridge.execute(&call).expect("the first signature goes in"); + assert!( + holds(&waiting_pegout(&mut bridge), &first), + "segwit: {segwit}" + ); + let signed_once = named(&mut bridge.ctx, "rskTxsWaitingFS"); + bridge.caller = address_of_private(2); + let second = signature(2, &sighash); + let call = add_signature(&public_key(&[2]), &[second], &RELEASE_HASH); + assert!(bridge.execute(&call).is_err(), "segwit: {segwit}"); + assert_eq!( + named(&mut bridge.ctx, "rskTxsWaitingFS"), + signed_once, + "segwit: {segwit}" + ); + } +} + +/// From RSKIP415 rskj logs a signing member at its RSK key's address +/// (`BridgeEventLoggerImpl.logAddSignature`): a member whose RSK key is the +/// point at infinity signs in with its BTC key, and is logged at that +/// point's address, the other member sending the transaction. +#[test] +fn a_member_whose_rsk_key_is_the_point_at_infinity_is_logged_at_its_address() { + let (one, two) = (public_key(&[1]), public_key(&[2])); + let mut keys = [one, two]; + keys.sort(); + let members: Vec> = keys + .iter() + .map(|key| member(key, if *key == two { INFINITY } else { key })) + .collect(); + let redeem = multisig_of(&[&keys[0], &keys[1]]); + let (mut bridge, sighash) = waiting(&members, &redeem, 2, false); + let _ = logs(&mut bridge.ctx); + bridge.caller = address_of_private(1); + let call = add_signature(&two, &[signature(2, &sighash)], &RELEASE_HASH); + bridge.execute(&call).expect("signed in"); + let add = event_topic("add_signature(bytes32,address,bytes)"); + let logged: Vec<_> = logs(&mut bridge.ctx) + .into_iter() + .filter(|log| topics(log)[0] == add) + .collect(); + assert_eq!(logged.len(), 1); + assert_eq!(topics(&logged[0])[2][12..], INFINITY_ADDRESS.0); +} + +/// The SVP spend transaction, signed by the proposed federation's members +/// (`addSvpSpendTxSignatures`): the member whose BTC key is the point at +/// infinity is found by it, so two signatures for one input return at their +/// count, and one fails the call at its verification; the member whose RSK +/// key is that point signs in, logged at its address. A third member, of +/// neither, sends the transactions. +#[test] +fn the_svp_spend_transaction_takes_the_point_at_infinity_as_a_proposed_members_key() { + let (one, three) = (public_key(&[1]), public_key(&[3])); + let members = [member(INFINITY, &three), member(&one, INFINITY)]; + let redeem = multisig_of(&[INFINITY, &one]); + let mut bridge = Bridge::mainnet_at(MAINNET_VETIVER900); + let creation = MAINNET_VETIVER900 - 1; + put_named( + &mut bridge.ctx, + "proposedFederation", + &federation(&members, creation), + ); + put_named( + &mut bridge.ctx, + "proposedFederationFormatVersion", + &serialize_long(P2SH_P2WSH_ERP), + ); + let spend = spending(&redeem, 2, false); + put_named( + &mut bridge.ctx, + "svpSpendTxWaitingForSignatures", + &rlp_encode_list(&[ + rlp_encode_element(&SPEND_HASH), + rlp_encode_element(&bitcoin::consensus::serialize(&spend)), + ]), + ); + let sighash = crate::bridge::release_tx::legacy_sighash_all(&spend, 0, &redeem); + let _ = logs(&mut bridge.ctx); + bridge.caller = address_of_private(3); + + let signed = signature(1, &sighash); + let two_signatures = add_signature(INFINITY, &[signed.clone(), signed.clone()], &SPEND_HASH); + assert!( + bridge.execute(&two_signatures).is_ok(), + "two signatures for one input" + ); + let one_signature = add_signature(INFINITY, std::slice::from_ref(&signed), &SPEND_HASH); + assert!( + bridge.execute(&one_signature).is_err(), + "verified with the point at infinity" + ); + + let call = add_signature(&one, std::slice::from_ref(&signed), &SPEND_HASH); + bridge.execute(&call).expect("signed in"); + let entry = named(&mut bridge.ctx, "svpSpendTxWaitingForSignatures").expect("waiting"); + let items = crate::bridge::serialization::rlp_decode_list(&entry).expect("a list"); + let waiting: bitcoin::Transaction = + bitcoin::consensus::deserialize(&items[1]).expect("a transaction"); + assert!(holds(&waiting, &signed)); + let add = event_topic("add_signature(bytes32,address,bytes)"); + let logged: Vec<_> = logs(&mut bridge.ctx) + .into_iter() + .filter(|log| topics(log)[0] == add) + .collect(); + assert_eq!(logged.len(), 1); + assert_eq!(topics(&logged[0])[2][12..], INFINITY_ADDRESS.0); +} diff --git a/crates/execution/src/rskj_tests/bridge_pegouts/release_transaction_builder_test.rs b/crates/execution/src/rskj_tests/bridge_pegouts/release_transaction_builder_test.rs new file mode 100644 index 0000000..850a89a --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegouts/release_transaction_builder_test.rs @@ -0,0 +1,78 @@ +//! rskj's `ReleaseTransactionBuilderTest`: here its tests of the Bitcoin +//! version of what it builds, 1 before RSKIP201 (iris300) for a peg-out +//! (`buildAmountTo`) and a refund (`buildEmptyWalletTo`), and 1 before +//! RSKIP376 (arrowhead600) for a migration (`buildMigrationTransaction`); 2 +//! from them. rskj's tests call the builder; the ports have the Bridge build +//! each, a block before the rule and at it, and read the version. + +use super::support::*; + +/// The version of each peg-out waiting for confirmations in `ctx`. +fn versions(ctx: &mut Ctx) -> Vec { + crate::bridge::peg::load_pegout_confirmation_set(ctx, true) + .iter() + .map(|entry| { + let tx: bitcoin::Transaction = + bitcoin::consensus::deserialize(&entry.btc_tx_raw).expect("a transaction"); + tx.version.0 + }) + .collect() +} + +/// rskj's `BuildAmountToTest.buildAmountTo_whenRSKIP201IsNotActive_shouldCreatePegoutTxWithBtcVersion1`. +#[test] +fn before_rskip201_a_pegout_is_built_as_version_1() { + for (number, version) in [(MAINNET_IRIS300 - 1, 1), (MAINNET_IRIS300, 2)] { + let mut collections = Collections::mainnet_at(number); + collections.active(1, 10); + collections.requests(1); + collections.update(); + assert_eq!( + versions(&mut collections.bridge.ctx), + vec![version], + "{number}" + ); + } +} + +/// rskj's `BuildEmptyWalletToTest.StandardMultiSigFederationTest.buildEmptyWalletTo_whenRSKIP201IsNotActive_shouldCreateRefundTxWithBtcVersion1`: +/// a peg-in from a multisig sender, refunded on rskj's regtest under +/// papyrus200 and under iris300. +#[test] +fn before_rskip201_a_refund_is_built_as_version_1() { + let sender = standard_redeem(&keys(20, 3)); + let tx = btc_transaction( + vec![input(1, multisig_spend(&sender))], + &[(BTC, address_of(&active()))], + ); + for (last, version) in [ + (RskNetworkUpgrade::Papyrus200, 1), + (RskNetworkUpgrade::Iris300, 2), + ] { + let cfg = up_to(last, RSK_REGTEST_CHAIN_ID); + let mut bridge = Registration::new(cfg, BridgeConstants::rskj_regtest(), 1_000); + bridge.federation(NEW_FEDERATION, &active()); + let outcome = bridge.register(&tx, 1_000); + assert_eq!(outcome.refunds, 1, "{last:?}"); + assert_eq!(versions(&mut bridge.ctx), vec![version], "{last:?}"); + } +} + +/// rskj's `BuildMigrationTransactionTest.StandardMultiSigFederationTests.buildMigrationTransaction_whenRSKIP376IsNotActive_shouldCreateMigrationTxWithBtcVersion1` +/// and `BuildMigrationTransactionTest.P2shErpFederationTests.buildMigrationTransaction_whenRSKIP376IsNotActive_shouldCreateMigrationTxWithBtcVersion1`: +/// a migration from a retiring federation stored as a standard multisig +/// one and as a P2SH ERP one. +#[test] +fn before_rskip376_a_migration_is_built_as_version_1() { + for format in [STANDARD_MULTISIG, P2SH_ERP] { + for (number, version) in [(MAINNET_ARROWHEAD600 - 1, 1), (MAINNET_ARROWHEAD600, 2)] { + let mut collections = Collections::migrating(number, format, 10, false); + collections.update(); + assert_eq!( + versions(&mut collections.bridge.ctx), + vec![version], + "{format} {number}" + ); + } + } +} diff --git a/crates/execution/src/rskj_tests/bridge_pegouts/support.rs b/crates/execution/src/rskj_tests/bridge_pegouts/support.rs new file mode 100644 index 0000000..3a9fadd --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegouts/support.rs @@ -0,0 +1,1540 @@ +//! What this folder's ports share: the Bridge at a block of a network, the +//! calls rskj's tests make to it, and the cells of its storage they read. +//! +//! rskj's tests build a `BridgeStorageProvider` or a `BridgeSupport` over a +//! mocked repository, with an `ActivationConfig` that has the rules a test +//! names. Here the Bridge runs in a context with no trie, at a block of +//! mainnet's own schedule or of a ladder that ends at an upgrade ([`up_to`]), +//! so that a rule is on or off as a network has it. What rskj writes and +//! rustock is held to (a serialized queue, a cell's bytes, a redeem script) is +//! written here from rskj's definition, not with rustock's own serializers. + +use alloy_primitives::{Address, U256}; +use revm::context_interface::{ContextTr, JournalTr}; +use revm::precompile::{PrecompileError, PrecompileOutput}; + +pub(super) use crate::bridge::constants::BridgeConstants; +pub(super) use crate::bridge::serialization::{rlp_encode_element, rlp_encode_list}; +pub(super) use crate::bridge::storage::rlp_encode_uint; +pub(super) use crate::hardfork::{RskHardforkConfig, RskNetworkUpgrade, RSK_REGTEST_CHAIN_ID}; +pub(super) use crate::precompiles::BridgeTxContext; + +/// The world a ported test runs the Bridge in: no trie and no database, only +/// what the test and the Bridge write. +pub(super) type Ctx = revm::Context< + revm::context::BlockEnv, + revm::context::TxEnv, + revm::context::CfgEnv, + revm::database::EmptyDB, + revm::Journal, + crate::raw_storage::RskChainExt, +>; + +/// Mainnet's papyrus200, which brings RSKIP134, RSKIP143 and RSKIP146. +pub(super) const MAINNET_PAPYRUS200: u64 = 2_392_700; +/// Mainnet's iris300, which brings RSKIP176, RSKIP185, RSKIP199 and RSKIP200. +pub(super) const MAINNET_IRIS300: u64 = 3_614_800; +/// Mainnet's hop400, which brings RSKIP271 and RSKIP293. +pub(super) const MAINNET_HOP400: u64 = 4_598_500; +/// Mainnet's fingerroot500, which brings RSKIP326. +pub(super) const MAINNET_FINGERROOT500: u64 = 5_468_000; +/// Mainnet's arrowhead600, which brings RSKIP379. +pub(super) const MAINNET_ARROWHEAD600: u64 = 6_223_700; +/// Mainnet's lovell700, which brings RSKIP419, RSKIP427, RSKIP428 and RSKIP459. +pub(super) const MAINNET_LOVELL700: u64 = 7_338_024; +/// Mainnet's reed800, which brings RSKIP305. +pub(super) const MAINNET_REED800: u64 = 8_052_200; +/// Mainnet's vetiver900, which brings RSKIP551. +pub(super) const MAINNET_VETIVER900: u64 = 8_804_200; +/// Testnet's lovell700. +pub(super) const TESTNET_LOVELL700: u64 = 6_110_487; + +/// One RBTC satoshi in wei. +pub(super) const WEI_PER_SATOSHI: u64 = 10_000_000_000; + +/// The upgrades after genesis, in order. +const LADDER: [RskNetworkUpgrade; 13] = [ + RskNetworkUpgrade::Orchid, + RskNetworkUpgrade::Wasabi100, + RskNetworkUpgrade::TwoToThree, + RskNetworkUpgrade::Papyrus200, + RskNetworkUpgrade::Iris300, + RskNetworkUpgrade::Hop400, + RskNetworkUpgrade::Fingerroot500, + RskNetworkUpgrade::Arrowhead600, + RskNetworkUpgrade::Arrowhead631, + RskNetworkUpgrade::Lovell700, + RskNetworkUpgrade::Reed800, + RskNetworkUpgrade::Reed810, + RskNetworkUpgrade::Vetiver900, +]; + +/// rskj's `ActivationConfigsForTest.()`: every upgrade up to `last` +/// active from block 0 and none after it, on the network `chain_id` names. +/// `Genesis` is rskj's `genesis()`, with nothing active. +pub(super) fn up_to(last: RskNetworkUpgrade, chain_id: u64) -> RskHardforkConfig { + LADDER + .into_iter() + .fold(RskHardforkConfig::all_active(chain_id), |cfg, upgrade| { + cfg.with_hardfork(upgrade, (upgrade <= last).then_some(0)) + }) +} + +/// A context at block `number`, with nothing stored. +pub(super) fn ctx_at(number: u64) -> Ctx { + use revm::MainContext; + let mut ctx: Ctx = + revm::Context::mainnet().with_chain(crate::raw_storage::RskChainExt::default()); + ctx.block.number = U256::from(number); + ctx.block.timestamp = U256::from(1_700_000_000u64); + ctx +} + +/// What the Bridge's cell `key` holds, `None` where it holds nothing. +pub(super) fn cell(ctx: &mut Ctx, key: U256) -> Option> { + crate::bridge::storage::bridge_load_raw(ctx, key) +} + +/// The cell rskj keys `DataWord.fromString(name)`. +pub(super) fn named(ctx: &mut Ctx, name: &str) -> Option> { + cell(ctx, crate::bridge::storage::bridge_storage_key(name)) +} + +/// Puts `value` in the cell rskj keys `DataWord.fromString(name)`. +pub(super) fn put_named(ctx: &mut Ctx, name: &str, value: &[u8]) { + crate::bridge::storage::bridge_store_raw( + ctx, + crate::bridge::storage::bridge_storage_key(name), + Some(value.to_vec()), + ); +} + +/// The cell rskj keys `-` (`getCompoundKey("-", ..)`), +/// which is `DataWord.fromLongString`: the Keccak-256 of the string. +pub(super) fn compound(base: &str, identifier: &str) -> U256 { + use sha3::Digest; + let hash: [u8; 32] = sha3::Keccak256::digest(format!("{base}-{identifier}")).into(); + U256::from_be_bytes(hash) +} + +/// rskj `serializeLong`: `RLP.encodeBigInteger(BigInteger.valueOf(value))`. +pub(super) fn serialize_long(value: u64) -> Vec { + rlp_encode_uint(U256::from(value)) +} + +/// RIPEMD-160 of SHA-256, Bitcoin's hash of a key or a script. +pub(super) fn hash160(data: &[u8]) -> [u8; 20] { + use ripemd::Digest as _; + ripemd::Ripemd160::digest(sha2::Sha256::digest(data)).into() +} + +/// bitcoinj-thin's `ScriptBuilder.createRedeemScript(threshold, keys)`: the +/// keys sorted, and the counts as its `ScriptBuilder.number` writes them, OP_1 +/// to OP_15 below 16 and a one-byte push from 16 on. +pub(super) fn multisig(keys: &[[u8; 33]], threshold: usize) -> Vec { + let number = |n: usize| { + if n < 16 { + vec![0x50 + n as u8] + } else { + vec![1, n as u8] + } + }; + let mut sorted = keys.to_vec(); + sorted.sort(); + let mut script = number(threshold); + for key in &sorted { + script.push(33); + script.extend_from_slice(key); + } + script.extend(number(sorted.len())); + script.push(0xae); + script +} + +/// A network's genesis federation's keys (`genesisFederationPublicKeys`). +pub(super) fn genesis_keys(constants: &BridgeConstants) -> Vec<[u8; 33]> { + constants + .genesis_federation_public_keys + .iter() + .map(|key| { + alloy_primitives::hex::decode(key) + .expect("a key in hex") + .try_into() + .expect("a compressed key") + }) + .collect() +} + +/// rskj `StandardMultiSigFederation.getRedeemScript`: `size / 2 + 1` of its +/// members. +pub(super) fn standard_redeem(keys: &[[u8; 33]]) -> Vec { + multisig(keys, keys.len() / 2 + 1) +} + +/// `OP_HASH160 OP_EQUAL`. +pub(super) fn p2sh(hash: &[u8; 20]) -> Vec { + let mut script = vec![0xa9, 20]; + script.extend_from_slice(hash); + script.push(0x87); + script +} + +/// `OP_DUP OP_HASH160 OP_EQUALVERIFY OP_CHECKSIG`. +pub(super) fn p2pkh(hash: &[u8; 20]) -> Vec { + let mut script = vec![0x76, 0xa9, 20]; + script.extend_from_slice(hash); + script.extend_from_slice(&[0x88, 0xac]); + script +} + +/// Gives the Bridge's active federation, the genesis federation of +/// `constants`, one UTXO of `satoshis` paid to its P2SH script, in the cell +/// `cfg` keeps them in at `block`. +pub(super) fn fund_genesis_federation( + ctx: &mut Ctx, + constants: &BridgeConstants, + cfg: &RskHardforkConfig, + block: u64, + satoshis: u64, +) { + let script = p2sh(&hash160(&standard_redeem(&genesis_keys(constants)))); + let utxo = crate::bridge::storage::BridgeUtxo { + tx_hash: [0x3c; 32], + vout: 0, + value_satoshis: satoshis, + height: 1, + script, + coinbase: false, + }; + crate::bridge::storage::store_federation_utxos(ctx, cfg, block, &[utxo]); +} + +/// A release request as rskj's `serializeReleaseRequestQueue` writes it: the +/// destination's hash160 and the amount, without the transaction's hash. +pub(super) fn legacy_queue(entries: &[([u8; 20], u64)]) -> Vec { + let items: Vec> = entries + .iter() + .flat_map(|(destination, satoshis)| { + [rlp_encode_element(destination), serialize_long(*satoshis)] + }) + .collect(); + rlp_encode_list(&items) +} + +/// Release requests as rskj's `serializeReleaseRequestQueueWithTxHash` writes +/// them: the destination's hash160, the amount and the transaction's hash. +pub(super) fn hashed_queue(entries: &[([u8; 20], u64, [u8; 32])]) -> Vec { + let items: Vec> = entries + .iter() + .flat_map(|(destination, satoshis, hash)| { + [ + rlp_encode_element(destination), + serialize_long(*satoshis), + rlp_encode_element(hash), + ] + }) + .collect(); + rlp_encode_list(&items) +} + +/// An `int256` answer, which here always fits an `i128`. +pub(super) fn int256(answer: &[u8]) -> i128 { + assert_eq!(answer.len(), 32, "an int256 answer is one word"); + let (high, low) = answer.split_at(16); + let low = i128::from_be_bytes(low.try_into().expect("16 bytes")); + let fill = if low < 0 { 0xff } else { 0x00 }; + assert!(high.iter().all(|&b| b == fill), "an int256 past an i128"); + low +} + +/// The compressed public key of the private key `private`, read as rskj's +/// `BtcECKey.fromPrivate` reads it: a big-endian number. +pub(super) fn public_key(private: &[u8]) -> [u8; 33] { + let mut scalar = [0u8; 32]; + scalar[32 - private.len()..].copy_from_slice(private); + let key = k256::ecdsa::SigningKey::from_slice(&scalar).expect("a private key in range"); + key.verifying_key() + .to_encoded_point(true) + .as_bytes() + .try_into() + .expect("33 bytes") +} + +/// `count` keys, from the private keys `first`, `first + 1`, ... +pub(super) fn keys(first: u8, count: u8) -> Vec<[u8; 33]> { + (first..first + count) + .map(|private| public_key(&[private])) + .collect() +} + +/// Where the active federation is stored, and its format. +pub(super) const NEW_FEDERATION: (&str, &str) = ("newFederation", "newFederationFormatVersion"); +/// Where the federation before it is stored, and its format. +pub(super) const OLD_FEDERATION: (&str, &str) = ("oldFederation", "oldFederationFormatVersion"); + +/// Stores a federation of `keys` where `slot` names, in rskj's multikey +/// format, made at RSK block `creation_block`, with its format version. +pub(super) fn store_federation( + ctx: &mut Ctx, + slot: (&str, &str), + keys: &[[u8; 33]], + creation_block: u64, + format: u64, +) { + use crate::bridge::federation::StoredMember; + let members: Vec = keys.iter().copied().map(StoredMember::from_btc).collect(); + store_members(ctx, slot, &members, creation_block, format); +} + +/// Stores a federation of `members`, each with its own RSK and MST keys, as +/// [`store_federation`] stores one of BTC keys alone. +pub(super) fn store_members( + ctx: &mut Ctx, + (key_name, format_key): (&str, &str), + members: &[crate::bridge::federation::StoredMember], + creation_block: u64, + format: u64, +) { + use crate::bridge::federation::serialize_federation_multikey; + put_named( + ctx, + key_name, + &serialize_federation_multikey(members, 0, creation_block), + ); + put_named(ctx, format_key, &serialize_long(format)); +} + +/// Gives the Bridge every RBTC there is, so that no peg-in passes the +/// locking cap for want of it. +pub(super) fn fund_bridge(ctx: &mut Ctx) { + use revm::context_interface::journaled_state::account::JournaledAccountTr; + let all_rbtc = U256::from(2_100_000_000_000_000u64) * U256::from(WEI_PER_SATOSHI); + ctx.journal_mut() + .load_account_mut(crate::precompiles::BRIDGE_ADDR) + .expect("the Bridge's account") + .set_balance(all_rbtc); +} + +/// bitcoinj `Sha256Hash.twiceOf`: SHA-256 of SHA-256, in the order computed, +/// which is a Bitcoin hash's internal order. +pub(super) fn sha256d(data: &[u8]) -> [u8; 32] { + use sha2::Digest; + sha2::Sha256::digest(sha2::Sha256::digest(data)).into() +} + +/// A hash the other way round: a Bitcoin hash's display order, which is +/// bitcoinj's `Sha256Hash` of a transaction or a block. +pub(super) fn reversed(mut hash: [u8; 32]) -> [u8; 32] { + hash.reverse(); + hash +} + +/// A partial merkle tree of one transaction, its hash `hash` matched: its +/// root is the hash. +pub(super) fn one_leaf_tree(hash: &[u8; 32]) -> Vec { + let mut tree = 1u32.to_le_bytes().to_vec(); + tree.push(1); + tree.extend_from_slice(hash); + tree.extend_from_slice(&[1, 0x01]); + tree +} + +/// A regtest header on `prev` whose merkle root is `merkle_root`, mined to +/// regtest's easiest target. +pub(super) fn mine(prev: bitcoin::BlockHash, merkle_root: [u8; 32]) -> bitcoin::block::Header { + use bitcoin::hashes::Hash; + (0..) + .map(|nonce| bitcoin::block::Header { + version: bitcoin::block::Version::from_consensus(1), + prev_blockhash: prev, + merkle_root: bitcoin::TxMerkleNode::from_byte_array(merkle_root), + time: 1_700_000_000, + bits: bitcoin::CompactTarget::from_consensus(0x207f_ffff), + nonce, + }) + .find(|header| { + crate::bridge::btc_rules::verify_header( + header, + crate::bridge::constants::BtcNetwork::Regtest, + ) + }) + .expect("a nonce under regtest's target") +} + +/// Bitcoin regtest headers 1 to `length`, each on the one before from the +/// genesis block, block `height`'s merkle root `root(height)`. +pub(super) fn regtest_headers( + length: u32, + root: impl Fn(u32) -> [u8; 32], +) -> Vec { + let mut prev = bitcoin::constants::genesis_block(bitcoin::Network::Regtest) + .header + .block_hash(); + (1..=length) + .map(|height| { + let header = mine(prev, root(height)); + prev = header.block_hash(); + header + }) + .collect() +} + +/// A header's hash as bitcoinj's `Sha256Hash` of it reads: display order. +pub(super) fn block_hash(header: &bitcoin::block::Header) -> [u8; 32] { + use bitcoin::hashes::Hash; + reversed(header.block_hash().to_byte_array()) +} + +/// rskj `receiveHeaders` of `headers`, on rskj regtest's constants. +pub(super) fn receive_headers( + ctx: &mut Ctx, + headers: &[bitcoin::block::Header], + cfg: &RskHardforkConfig, +) { + use crate::abi::encode::{self, Arg}; + let serialized: Vec> = headers.iter().map(bitcoin::consensus::serialize).collect(); + let slices: Vec<&[u8]> = serialized.iter().map(Vec::as_slice).collect(); + let number = revm::context_interface::Block::number(ctx.block()).to::(); + crate::bridge::btc_chain::receive_headers( + ctx, + &encode::args(&[Arg::BytesArray(&slices)]), + 0, + &BridgeConstants::rskj_regtest(), + cfg.has_stored_block_v2(number), + cfg, + ) + .expect("the headers are taken"); +} + +/// The Bitcoin height a peg-in is registered at: with blocks to +/// [`BTC_HEAD`] it has the three confirmations rskj's regtest asks of one. +pub(super) const PEGIN_HEIGHT: u32 = 350; +/// The head of the Bitcoin chain [`btc_chain`] stores. +pub(super) const BTC_HEAD: u32 = 352; + +/// Bitcoin regtest blocks 1 to [`BTC_HEAD`] in the Bridge's store, received +/// under `cfg`, block `height`'s merkle root `root(height)`. The headers. +pub(super) fn btc_chain( + ctx: &mut Ctx, + cfg: &RskHardforkConfig, + root: impl Fn(u32) -> [u8; 32], +) -> Vec { + let headers = regtest_headers(BTC_HEAD, root); + receive_headers(ctx, &headers, cfg); + headers +} + +/// A transaction of one input, a P2PKH spend by the key of the private key +/// `0x77`, paying `outputs`. +pub(super) fn tx_paying(outputs: &[(u64, Vec)]) -> bitcoin::Transaction { + use bitcoin::hashes::Hash; + let mut script_sig = vec![71]; + script_sig.extend_from_slice(&[0x30; 71]); + script_sig.push(33); + script_sig.extend_from_slice(&public_key(&[0x77])); + bitcoin::Transaction { + version: bitcoin::transaction::Version::TWO, + lock_time: bitcoin::absolute::LockTime::ZERO, + input: vec![bitcoin::TxIn { + previous_output: bitcoin::OutPoint { + txid: bitcoin::Txid::from_byte_array([0x5a; 32]), + vout: 0, + }, + script_sig: bitcoin::ScriptBuf::from_bytes(script_sig), + sequence: bitcoin::Sequence::MAX, + witness: bitcoin::Witness::new(), + }], + output: outputs + .iter() + .map(|(value, script)| bitcoin::TxOut { + value: bitcoin::Amount::from_sat(*value), + script_pubkey: bitcoin::ScriptBuf::from_bytes(script.clone()), + }) + .collect(), + } +} + +/// rskj `registerBtcTransaction(raw, height, tree)` on rskj regtest's +/// constants, by a transaction of `tx`'s. +pub(super) fn register( + ctx: &mut Ctx, + cfg: &RskHardforkConfig, + tx: &BridgeTxContext, + raw: &[u8], + height: u32, + tree: &[u8], +) -> Result { + use crate::abi::encode::{self, Arg}; + let args = encode::args(&[ + Arg::Bytes(raw), + Arg::Word(encode::int(i64::from(height))), + Arg::Bytes(tree), + ]); + crate::bridge::peg::register_btc_transaction( + ctx, + &args, + 0, + &BridgeConstants::rskj_regtest(), + cfg, + tx, + ) +} + +/// A transaction's txid, without its witnesses, in the order its bytes are +/// computed (bitcoinj's `getHash(false)` is the other way round). +pub(super) fn txid(tx: &bitcoin::Transaction) -> [u8; 32] { + use bitcoin::hashes::Hash; + tx.compute_txid().to_byte_array() +} + +/// `data` pushed as bitcoinj's `ScriptBuilder.data` pushes it: no bytes as +/// OP_0, up to 75 directly, then OP_PUSHDATA1 and OP_PUSHDATA2. +pub(super) fn push(data: &[u8]) -> Vec { + let mut out = match data.len() { + 0 => return vec![0x00], + n @ 1..=75 => vec![n as u8], + n @ 76..=255 => vec![0x4c, n as u8], + n => [&[0x4d][..], &(n as u16).to_le_bytes()].concat(), + }; + out.extend_from_slice(data); + out +} + +/// A P2PKH spend's scriptSig: a signature's worth of bytes and the key of +/// the private key `0x77`. +pub(super) fn p2pkh_spend() -> Vec { + [push(&[0x30; 71]), push(&public_key(&[0x77]))].concat() +} + +/// rskj `ScriptBuilder.createP2SHMultiSigInputScript` of `redeem` with its +/// signatures in: OP_0, a signature's worth of bytes for each the script asks +/// for, and the script pushed last. +pub(super) fn multisig_spend(redeem: &[u8]) -> Vec { + let threshold = usize::from(redeem[0] - 0x50); + let mut script = vec![0x00]; + for _ in 0..threshold { + script.extend(push(&[0x30; 71])); + } + script.extend(push(redeem)); + script +} + +/// The address a federation of `keys` is paid at: the P2SH of its redeem +/// script. +pub(super) fn address_of(keys: &[[u8; 33]]) -> Vec { + p2sh(&hash160(&standard_redeem(keys))) +} + +/// A P2WPKH output script, which rskj's tests write as a bech32 address. +pub(super) fn p2wpkh(hash: &[u8; 20]) -> Vec { + [&[0x00, 20][..], hash].concat() +} + +/// An input spending output 0 of the transaction `[tag; 32]`, with +/// `script_sig`. +pub(super) fn input(tag: u8, script_sig: Vec) -> bitcoin::TxIn { + use bitcoin::hashes::Hash; + bitcoin::TxIn { + previous_output: bitcoin::OutPoint { + txid: bitcoin::Txid::from_byte_array([tag; 32]), + vout: 0, + }, + script_sig: bitcoin::ScriptBuf::from_bytes(script_sig), + sequence: bitcoin::Sequence::MAX, + witness: bitcoin::Witness::new(), + } +} + +/// A transaction of version 1, as rskj's `new BtcTransaction(params)` makes +/// one, of `inputs`, paying `outputs`. +pub(super) fn btc_transaction( + inputs: Vec, + outputs: &[(u64, Vec)], +) -> bitcoin::Transaction { + bitcoin::Transaction { + version: bitcoin::transaction::Version::ONE, + lock_time: bitcoin::absolute::LockTime::ZERO, + input: inputs, + output: outputs + .iter() + .map(|(value, script)| bitcoin::TxOut { + value: bitcoin::Amount::from_sat(*value), + script_pubkey: bitcoin::ScriptBuf::from_bytes(script.clone()), + }) + .collect(), + } +} + +/// rskj `BitcoinUtils.getSigHashForPegoutIndex` of a transaction whose +/// inputs carry no signatures: `hashForSignature(0, redeem, ALL, false)`, +/// every scriptSig cleared and the first input's set to `redeem`, the +/// witnesses kept, SIGHASH_ALL in four bytes after it, hashed twice. +pub(super) fn index_sighash(tx: &bitcoin::Transaction, redeem: &[u8]) -> [u8; 32] { + let mut copy = tx.clone(); + for (index, input) in copy.input.iter_mut().enumerate() { + input.script_sig = if index == 0 { + bitcoin::ScriptBuf::from_bytes(redeem.to_vec()) + } else { + bitcoin::ScriptBuf::new() + }; + } + let mut preimage = bitcoin::consensus::serialize(©); + preimage.extend_from_slice(&1u32.to_le_bytes()); + sha256d(&preimage) +} + +/// rskj `setPegoutTxSigHash`: `sighash` in the pegout index, the one true +/// byte under `pegoutTxSigHash-`. +pub(super) fn index_pegout(ctx: &mut Ctx, sighash: &[u8; 32]) { + crate::bridge::storage::bridge_store_raw( + ctx, + compound("pegoutTxSigHash", &alloy_primitives::hex::encode(sighash)), + Some(vec![1]), + ); +} + +/// What the Bridge logged, as the ports read it. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(super) enum Event { + /// `pegin_btc`, its amount and its protocol version. + PeginBtc(u64, u64), + /// `rejected_pegin`, its `RejectedPeginReason`. + Rejected(u64), + /// `unrefundable_pegin`, its `NonRefundablePeginReason`. + Unrefundable(u64), + /// `release_requested`, its amount. + ReleaseRequested(u64), + /// `pegout_transaction_created`. + PegoutTransactionCreated, + /// Any other, by its first topic. + Other([u8; 32]), +} + +/// The Keccak-256 of a Solidity event's signature, its first topic. +fn topic(signature: &str) -> [u8; 32] { + use sha3::Digest; + sha3::Keccak256::digest(signature).into() +} + +/// The Bridge's events among `logs`. +pub(super) fn events_of(logs: &[revm::primitives::Log]) -> Vec { + let word = |data: &[u8]| U256::from_be_slice(&data[..32]).to::(); + logs.iter() + .map(|log| { + let first: [u8; 32] = log.data.topics()[0].0; + let data = &log.data.data; + if first == topic("pegin_btc(address,bytes32,int256,int256)") { + Event::PeginBtc(word(data), word(&data[32..])) + } else if first == topic("rejected_pegin(bytes32,int256)") { + Event::Rejected(word(data)) + } else if first == topic("unrefundable_pegin(bytes32,int256)") { + Event::Unrefundable(word(data)) + } else if first == topic("release_requested(bytes32,bytes32,uint256)") { + Event::ReleaseRequested(word(data)) + } else if first == topic("pegout_transaction_created(bytes32,bytes)") { + Event::PegoutTransactionCreated + } else { + Event::Other(first) + } + }) + .collect() +} + +/// What `registerBtcTransaction` left. +#[derive(Debug)] +pub(super) struct Outcome { + /// Whether the call returned; a call that throws leaves nothing. + pub(super) returned: bool, + /// The RSK block the transaction was marked processed at. + pub(super) processed_at: Option, + pub(super) events: Vec, + /// The values of the active federation's UTXOs. + pub(super) active_utxos: Vec, + /// The values of the retiring federation's UTXOs. + pub(super) retiring_utxos: Vec, + /// The peg-outs, refunds among them, waiting for confirmations. + pub(super) refunds: usize, +} + +/// A network's Bridge at an RSK block, for rskj's `registerBtcTransaction`: +/// the Bridge holding all the RBTC there is, and the lock whitelist off, as +/// mainnet's and testnet's have long been. +pub(super) struct Registration { + pub(super) ctx: Ctx, + pub(super) cfg: RskHardforkConfig, + pub(super) constants: BridgeConstants, + pub(super) block: u64, +} + +impl Registration { + pub(super) fn new(cfg: RskHardforkConfig, constants: BridgeConstants, block: u64) -> Self { + let mut ctx = ctx_at(block); + fund_bridge(&mut ctx); + crate::bridge::storage::store_one_off_whitelist(&mut ctx, &[], 0); + Self { + ctx, + cfg, + constants, + block, + } + } + + /// Mainnet's Bridge at `block`. + pub(super) fn mainnet(block: u64) -> Self { + Self::new( + RskHardforkConfig::mainnet(), + BridgeConstants::mainnet(), + block, + ) + } + + /// The federation of `keys` where `slot` names, made at RSK block 1. + pub(super) fn federation(&mut self, slot: (&str, &str), keys: &[[u8; 33]]) { + store_federation(&mut self.ctx, slot, keys, 1, 1000); + } + + /// rskj's `mockChainOfStoredBlocks`: Bitcoin blocks stored from `height` + /// up, the one at `height` with `merkle_root` and the network's + /// confirmations above it, the last the chain's head. The hash of the one + /// at `height`, as bitcoinj's `Sha256Hash` of it reads. + pub(super) fn store_chain(&mut self, height: u32, merkle_root: [u8; 32]) -> [u8; 32] { + use crate::bridge::btc_store::{put_stored_block, store_chain_head, StoredBlock}; + use bitcoin::hashes::Hash; + let use_v2 = self.cfg.has_stored_block_v2(self.block); + let head = height + self.constants.btc2rsk_minimum_acceptable_confirmations; + let mut prev = bitcoin::BlockHash::from_byte_array([2; 32]); + let mut at_height = [0; 32]; + for at in height..=head { + let root = if at == height { + merkle_root + } else { + [0x11; 32] + }; + let header = bitcoin::block::Header { + version: bitcoin::block::Version::from_consensus(1), + prev_blockhash: prev, + merkle_root: bitcoin::TxMerkleNode::from_byte_array(root), + time: 1, + bits: bitcoin::CompactTarget::from_consensus(1), + nonce: at, + }; + let stored = StoredBlock::new(header, at, U256::from(at)); + put_stored_block(&mut self.ctx, &stored, use_v2); + if at == head { + store_chain_head(&mut self.ctx, &stored, use_v2); + } + if at == height { + at_height = block_hash(&header); + } + prev = header.block_hash(); + } + at_height + } + + /// rskj `registerBtcTransaction` of `tx`, in the only block of its own at + /// Bitcoin height `height`, by an RSK transaction of its own. A transaction + /// with witnesses is proven, as rskj proves one, by the root of its block's + /// witnesses that `registerBtcCoinbaseTransaction` keeps. The call's + /// writes are kept when it returns and dropped when it fails. + pub(super) fn register(&mut self, tx: &bitcoin::Transaction, height: u32) -> Outcome { + use crate::abi::encode::{self, Arg}; + let raw = bitcoin::consensus::serialize(tx); + let hash = sha256d(&raw); + let txid = txid(tx); + let block = self.store_chain(height, txid); + if hash != txid { + crate::bridge::storage::bridge_store_raw( + &mut self.ctx, + compound("coinbaseInformation", &alloy_primitives::hex::encode(block)), + Some(rlp_encode_list(&[rlp_encode_element(&reversed(hash))])), + ); + } + let tree = one_leaf_tree(&hash); + let args = encode::args(&[ + Arg::Bytes(&raw), + Arg::Word(encode::int(i64::from(height))), + Arg::Bytes(&tree), + ]); + let tx_ctx = BridgeTxContext { + rsk_tx_hash: [0x7e; 32], + origin_tx_hash: [0x7e; 32], + ..Default::default() + }; + self.ctx.chain_mut().raw_storage.commit_call(); + let checkpoint = self.ctx.journal_mut().checkpoint(); + let returned = crate::bridge::peg::register_btc_transaction( + &mut self.ctx, + &args, + 0, + &self.constants, + &self.cfg, + &tx_ctx, + ) + .is_ok(); + if returned { + self.ctx.chain_mut().raw_storage.commit_call(); + self.ctx.journal_mut().checkpoint_commit(); + } else { + self.ctx.chain_mut().raw_storage.discard_call(); + self.ctx.journal_mut().checkpoint_revert(checkpoint); + } + let events = events_of(&self.ctx.journal_mut().take_logs()); + let values = |utxos: Vec| { + utxos + .iter() + .map(|utxo| utxo.value_satoshis) + .collect::>() + }; + let active = + crate::bridge::storage::load_federation_utxos(&mut self.ctx, &self.cfg, self.block); + let retiring = crate::bridge::storage::load_old_federation_utxos(&mut self.ctx); + let rskip146 = self.cfg.has_rskip146(self.block); + Outcome { + returned, + processed_at: crate::bridge::tx::get_btc_tx_processed_height(&mut self.ctx, &txid), + events, + active_utxos: values(active), + retiring_utxos: values(retiring), + refunds: crate::bridge::peg::load_pegout_confirmation_set(&mut self.ctx, rskip146) + .len(), + } + } +} + +/// Mainnet's `btcHeightWhenPegoutTxIndexActivates` and its +/// `pegoutTxIndexGracePeriodInBtcBlocks`: from RSKIP379 a transaction in a +/// Bitcoin block this high or higher is classified by the pegout index. +pub(super) const INDEX_START: u32 = 837_589 + 4_320; +/// The Bitcoin block below it, classified by the legacy rules. +pub(super) const BELOW_INDEX: u32 = INDEX_START - 1; + +/// One of the runs rskj's registration tests make: a block of mainnet, and +/// the Bitcoin height the transaction is registered at. +#[derive(Clone, Copy, Debug)] +pub(super) struct Run { + pub(super) block: u64, + pub(super) height: u32, +} + +/// rskj's `fingerrootActivations`, before RSKIP379. +pub(super) const BEFORE_RSKIP379: Run = Run { + block: MAINNET_FINGERROOT500, + height: BELOW_INDEX, +}; +/// rskj's `arrowhead600Activations` with `shouldUsePegoutTxIndex` false. +pub(super) const RSKIP379_BELOW_INDEX: Run = Run { + block: MAINNET_ARROWHEAD600, + height: BELOW_INDEX, +}; +/// rskj's `arrowhead600Activations` with `shouldUsePegoutTxIndex` true. +pub(super) const RSKIP379_INDEXED: Run = Run { + block: MAINNET_ARROWHEAD600, + height: INDEX_START, +}; +/// lovell700, which rskj's runs leave out, by the index: RSKIP459. +pub(super) const RSKIP459_INDEXED: Run = Run { + block: MAINNET_LOVELL700, + height: INDEX_START, +}; +/// rskj's `allActivations`, by the index: RSKIP551. +pub(super) const RSKIP551_INDEXED: Run = Run { + block: MAINNET_VETIVER900, + height: INDEX_START, +}; + +pub(super) const RUNS: [Run; 5] = [ + BEFORE_RSKIP379, + RSKIP379_BELOW_INDEX, + RSKIP379_INDEXED, + RSKIP459_INDEXED, + RSKIP551_INDEXED, +]; + +impl Run { + pub(super) fn rskip379(self) -> bool { + RskHardforkConfig::mainnet().has_rskip379(self.block) + } + + /// Whether the pegout index classifies the transaction. + pub(super) fn indexed(self) -> bool { + self.rskip379() && self.height >= INDEX_START + } + + /// rskj `shouldMarkRejectedPeginAsProcessed`: RSKIP459 and not RSKIP551. + pub(super) fn marks_refused(self) -> bool { + let cfg = RskHardforkConfig::mainnet(); + cfg.has_rskip459(self.block) && !cfg.has_rskip551(self.block) + } + + /// Mainnet's Bridge at the run's block, the active federation + /// [`active`] in it, and with `with_retiring` the retiring one + /// [`retiring`]. + pub(super) fn bridge(self, with_retiring: bool) -> Registration { + let mut bridge = Registration::mainnet(self.block); + bridge.federation(NEW_FEDERATION, &active()); + if with_retiring { + bridge.federation(OLD_FEDERATION, &retiring()); + } + bridge + } +} + +/// The active federation's members. +pub(super) fn active() -> Vec<[u8; 33]> { + keys(7, 5) +} + +/// The retiring federation's members. +pub(super) fn retiring() -> Vec<[u8; 33]> { + keys(4, 3) +} + +/// The last retired federation's members. +pub(super) fn retired() -> Vec<[u8; 33]> { + keys(1, 3) +} + +/// mainnet's `minimumPeginTxValue` from RSKIP219. +pub(super) fn minimum_pegin() -> u64 { + BridgeConstants::mainnet().minimum_pegin_tx_value +} + +/// mainnet's `minimumPegoutTxValue`. +pub(super) fn minimum_pegout() -> u64 { + BridgeConstants::mainnet().minimum_pegout_tx_value +} + +/// A transaction from a P2PKH sender paying `outputs`. +pub(super) fn from_a_user(outputs: &[(u64, Vec)]) -> bitcoin::Transaction { + btc_transaction(vec![input(1, p2pkh_spend())], outputs) +} + +/// A user's P2PKH address. +pub(super) fn user(tag: u8) -> Vec { + p2pkh(&[tag; 20]) +} + +/// One bitcoin, in satoshis. +pub(super) const BTC: u64 = 100_000_000; +/// bitcoinj's `Coin.CENT`. +pub(super) const CENT: u64 = 1_000_000; + +/// A peg-out or a migration: processed with no event, its outputs to the +/// active federation, `utxos` of them, registered. +pub(super) fn assert_moved(outcome: &Outcome, run: Run, utxos: usize) { + assert!(outcome.returned, "{run:?}"); + assert!(outcome.events.is_empty(), "{run:?}"); + assert_eq!(outcome.processed_at, Some(run.block), "{run:?}"); + assert_eq!(outcome.active_utxos.len(), utxos, "{run:?}"); + assert!(outcome.retiring_utxos.is_empty(), "{run:?}"); +} + +/// A transaction spending `inputs` outputs of the federation of `keys`, the +/// first input's sighash in the pegout index from RSKIP379 as rskj's tests +/// mock it, registered in `run`. +pub(super) fn register_spend( + run: Run, + with_retiring: bool, + keys: &[[u8; 33]], + inputs: u8, + outputs: &[(u64, Vec)], +) -> Outcome { + let redeem = standard_redeem(keys); + let tx = btc_transaction( + (1..=inputs) + .map(|tag| input(tag, multisig_spend(&redeem))) + .collect(), + outputs, + ); + let mut bridge = run.bridge(with_retiring); + if run.rskip379() { + index_pegout(&mut bridge.ctx, &index_sighash(&tx, &redeem)); + } + bridge.register(&tx, run.height) +} + +/// rskj's forty outputs of its many-output peg-outs and migrations: a quarter +/// of the minimum peg-out, half of it, and the minimum, each and a cent, and +/// the minimum and a bitcoin, ten of each. +pub(super) fn forty_outputs(to: impl Fn(u8) -> Vec) -> Vec<(u64, Vec)> { + let quarter = minimum_pegout() / 4; + let values = [ + quarter + CENT, + 2 * quarter + CENT, + minimum_pegout() + CENT, + minimum_pegout() + BTC, + ]; + (0..40u8) + .map(|n| (values[usize::from(n / 10)], to(n))) + .collect() +} + +/// The logs the calls so far left, taken. +pub(super) fn logs(ctx: &mut Ctx) -> Vec { + ctx.journal_mut().take_logs() +} + +/// A log's topics, as bytes. +pub(super) fn topics(log: &revm::primitives::Log) -> Vec<[u8; 32]> { + log.data.topics().iter().map(|topic| topic.0).collect() +} + +/// rskj's legacy event topic, `DataWord.fromString(name)`: the name's ASCII +/// right-aligned in a word. +pub(super) fn legacy_topic(name: &str) -> [u8; 32] { + let mut word = [0u8; 32]; + word[32 - name.len()..].copy_from_slice(name.as_bytes()); + word +} + +/// A Solidity event's topic: the Keccak-256 of its signature. +pub(super) fn event_topic(signature: &str) -> [u8; 32] { + use sha3::Digest; + sha3::Keccak256::digest(signature.as_bytes()).into() +} + +/// The `index`th word of an event's ABI data. +pub(super) fn word(data: &[u8], index: usize) -> U256 { + U256::from_be_slice(&data[32 * index..32 * (index + 1)]) +} + +/// The hash of the RSK transaction that made the peg-out the federators +/// sign. +pub(super) const RELEASE_HASH: [u8; 32] = [0x2b; 32]; + +/// A peg-out waiting for its federation's signatures, as rskj's +/// `addSignature` tests keep one: the federation of the private keys 1, 2 +/// and 3 active, and in `rskTxsWaitingFS`, under [`RELEASE_HASH`], a +/// transaction spending one of its outputs to a user, its input's scriptSig +/// the federation's with no signature yet +/// (`createBaseInputScriptThatSpendsFromTheFederation`). Members count in +/// the order of their keys in the redeem script. +pub(super) struct Signing { + pub(super) bridge: Bridge, + privates: Vec<[u8; 32]>, + /// Where each member's transaction comes from: the address of its RSK + /// key, which rskj's `addSignature` asks a sender to be a member's + /// (`BridgeUtils.isFromFederateMember`). + senders: Vec
, + pub(super) redeem: Vec, + pub(super) unsigned: bitcoin::Transaction, +} + +impl Signing { + /// At block `number` of mainnet. + pub(super) fn mainnet_at(number: u64) -> Self { + Self::spending(number, 1) + } + + /// At block `number` of mainnet, the peg-out spending `inputs` of the + /// federation's outputs, each input's scriptSig the federation's with no + /// signature yet. + pub(super) fn spending(number: u64, inputs: u8) -> Self { + use crate::bridge::federation::StoredMember; + let members = (1..=3u8) + .map(|i| { + let mut private = [0u8; 32]; + private[31] = i; + (StoredMember::from_btc(public_key(&[i])), private) + }) + .collect(); + Self::of_members(number, members, inputs) + } + + /// At block `number` of mainnet, the peg-out spending `inputs` of the + /// outputs of a federation of `members`, each stored with its own RSK and + /// MST keys and signing with the private key of its BTC key. + pub(super) fn of_members( + number: u64, + mut members: Vec<(crate::bridge::federation::StoredMember, [u8; 32])>, + inputs: u8, + ) -> Self { + members.sort(); + let publics: Vec<[u8; 33]> = members.iter().map(|(member, _)| member.btc).collect(); + let redeem = standard_redeem(&publics); + let mut bridge = Bridge::mainnet_at(number); + let stored: Vec<_> = members.iter().map(|(member, _)| *member).collect(); + store_members(&mut bridge.ctx, NEW_FEDERATION, &stored, 1, 1000); + let threshold = usize::from(redeem[0] - 0x50); + let script_sig = [vec![0x00; threshold + 1], push(&redeem)].concat(); + let spent = (1..=inputs) + .map(|tag| input(tag, script_sig.clone())) + .collect(); + let unsigned = btc_transaction(spent, &[(BTC, user(1))]); + put_named( + &mut bridge.ctx, + "rskTxsWaitingFS", + &rlp_encode_list(&[ + rlp_encode_element(&RELEASE_HASH), + rlp_encode_element(&bitcoin::consensus::serialize(&unsigned)), + ]), + ); + let privates = members.iter().map(|(_, private)| *private).collect(); + let senders = members + .iter() + .map(|(member, _)| { + crate::bridge::federation::rsk_address_from_public_key(&member.rsk) + .expect("an address") + }) + .collect(); + Self { + bridge, + privates, + senders, + redeem, + unsigned, + } + } + + /// A member's public key. + pub(super) fn key(&self, member: usize) -> [u8; 33] { + public_key(&self.privates[member]) + } + + /// What the peg-out's input `index` is signed over: its SIGHASH_ALL + /// hash over the redeem script. + pub(super) fn sighash(&self, index: usize) -> [u8; 32] { + use bitcoin::hashes::Hash; + bitcoin::sighash::SighashCache::new(&self.unsigned) + .legacy_signature_hash(index, bitcoin::Script::from_bytes(&self.redeem), 1) + .expect("a sighash") + .to_byte_array() + } + + /// The DER signature of the peg-out's input `index` by the private key + /// `private`. + pub(super) fn signature_by(&self, private: &[u8; 32], index: usize) -> Vec { + use k256::ecdsa::signature::hazmat::PrehashSigner; + let key = k256::ecdsa::SigningKey::from_slice(private).expect("a key"); + let signature: k256::ecdsa::Signature = + key.sign_prehash(&self.sighash(index)).expect("a signature"); + signature.to_der().as_bytes().to_vec() + } + + /// A member's DER signature of the peg-out's input `index`. + pub(super) fn signature_of(&self, member: usize, index: usize) -> Vec { + self.signature_by(&self.privates[member], index) + } + + /// A member's DER signature of the peg-out's input, SIGHASH_ALL over + /// the redeem script. + pub(super) fn signature(&self, member: usize) -> Vec { + self.signature_of(member, 0) + } + + /// Another of a member's signatures of input `index`: one that verifies + /// as [`Signing::signature_of`]'s does, made with another nonce. + pub(super) fn another_signature_of(&self, member: usize, index: usize) -> Vec { + use k256::ecdsa::signature::hazmat::RandomizedPrehashSigner; + use rand::SeedableRng; + let key = k256::ecdsa::SigningKey::from_slice(&self.privates[member]).expect("a key"); + let mut rng = rand::rngs::StdRng::seed_from_u64(7); + let signature: k256::ecdsa::Signature = key + .sign_prehash_with_rng(&mut rng, &self.sighash(index)) + .expect("a signature"); + signature.to_der().as_bytes().to_vec() + } + + /// `addSignature` with `key` and `signatures`, called by the member's + /// transaction. + pub(super) fn add_signatures_as( + &mut self, + member: usize, + key: &[u8], + signatures: &[Vec], + ) -> Result { + use crate::abi::encode::{args, Arg}; + self.bridge.caller = self.senders[member]; + let sent: Vec<&[u8]> = signatures.iter().map(Vec::as_slice).collect(); + let data = [ + crate::abi::selector("addSignature(bytes,bytes[],bytes)").to_vec(), + args(&[ + Arg::Bytes(key), + Arg::BytesArray(&sent), + Arg::Bytes(&RELEASE_HASH), + ]), + ] + .concat(); + self.bridge.execute(&data) + } + + /// `addSignature` with the member's key and `signatures`, called by the + /// member's transaction. + pub(super) fn add_signatures( + &mut self, + member: usize, + signatures: &[Vec], + ) -> Result { + let key = self.key(member); + self.add_signatures_as(member, &key, signatures) + } + + /// `addSignature` with `signature`, called by the member's transaction. + pub(super) fn add_signature( + &mut self, + member: usize, + signature: &[u8], + ) -> Result { + self.add_signatures(member, &[signature.to_vec()]) + } + + /// The member's own signature, as [`Signing::add_signature`] sends it. + pub(super) fn sign(&mut self, member: usize) { + let signature = self.signature(member); + self.add_signature(member, &signature) + .expect("addSignature runs"); + } + + /// The peg-out with the signatures of `members` in each input, as + /// bitcoinj fills them in: in the redeem script's order, before the slots + /// still empty. + pub(super) fn signed_by(&self, members: &[usize]) -> bitcoin::Transaction { + let threshold = usize::from(self.redeem[0] - 0x50); + let mut sorted = members.to_vec(); + sorted.sort(); + let mut tx = self.unsigned.clone(); + for index in 0..tx.input.len() { + let mut script_sig = vec![0x00]; + for &member in &sorted { + script_sig.extend(push( + &[self.signature_of(member, index), vec![0x01]].concat(), + )); + } + script_sig.extend(vec![0x00; threshold - sorted.len()]); + script_sig.extend(push(&self.redeem)); + tx.input[index].script_sig = bitcoin::ScriptBuf::from_bytes(script_sig); + } + tx + } +} + +/// The votes of a majority of the network's federation-change authorizers, +/// at the Bridge's block, to create a pending federation, add `members` to +/// it (each key as its member's BTC, RSK and MST key), and commit it; what +/// the deciding commit answers. +pub(super) fn elect(bridge: &mut Bridge, members: &[[u8; 33]]) -> i128 { + use crate::abi::encode::{args, Arg}; + use crate::bridge::governance; + let all: Vec
= bridge + .constants + .federation_change_authorizer_keys + .iter() + .map(|key| { + let key = alloy_primitives::hex::decode(key).expect("a key in hex"); + crate::bridge::federation::rsk_address_from_public_key(&key).expect("a key") + }) + .collect(); + let deciding = &all[..all.len() / 2 + 1]; + let sent_by = |sender: Address| BridgeTxContext { + rsk_sender: sender, + ..BridgeTxContext::default() + }; + let (ctx, constants, cfg) = (&mut bridge.ctx, &bridge.constants, &bridge.cfg); + for &sender in deciding { + governance::create_federation(ctx, 0, constants, cfg, &sent_by(sender)) + .expect("createFederation answers"); + } + for member in members { + let member = args(&[Arg::Bytes(member), Arg::Bytes(member), Arg::Bytes(member)]); + for &sender in deciding { + governance::add_federator_public_key_multikey( + ctx, + &member, + 0, + constants, + cfg, + &sent_by(sender), + ) + .expect("addFederatorPublicKeyMultikey answers"); + } + } + let hash = crate::bridge::getters::get_pending_federation_hash(ctx, 0) + .expect("getPendingFederationHash answers"); + let length = U256::from_be_slice(&hash.bytes[32..64]).to::(); + let hash = args(&[Arg::Bytes(&hash.bytes[64..64 + length])]); + let mut answer = 0; + for &sender in deciding { + let out = governance::commit_federation(ctx, &hash, 0, constants, cfg, &sent_by(sender)) + .expect("commitFederation answers"); + answer = int256(&out.bytes); + } + answer +} + +/// A UTXO of `satoshis` paid to `script`, its outpoint `[tag; 32]:0`. +pub(super) fn utxo(tag: u8, satoshis: u64, script: Vec) -> crate::bridge::storage::BridgeUtxo { + crate::bridge::storage::BridgeUtxo { + tx_hash: [tag; 32], + vout: 0, + value_satoshis: satoshis, + height: 1, + script, + coinbase: false, + } +} + +/// Mainnet's federation activation age at block `number` (rskj +/// `FederationMainNetConstants`): 18,500 blocks before RSKIP383, 40,320 from +/// it. +pub(super) fn activation_age(number: u64) -> u64 { + if RskHardforkConfig::mainnet().has_rskip383(number) { + 40_320 + } else { + 18_500 + } +} + +/// Mainnet's `fundsMigrationAgeSinceActivationEnd`. +pub(super) const MIGRATION_AGE_END: u64 = 10_585; +/// rskj's `FederationFormatVersion.STANDARD_MULTISIG_FEDERATION`. +pub(super) const STANDARD_MULTISIG: u64 = 1000; +/// rskj's `FederationFormatVersion.P2SH_ERP_FEDERATION`. +pub(super) const P2SH_ERP: u64 = 3000; + +/// rskj's `FederationFormatVersion.P2SH_P2WSH_ERP_FEDERATION`. +pub(super) const P2SH_P2WSH_ERP: u64 = 4000; + +/// Mainnet's Bridge at a block, about to run `updateCollections` as rskj's +/// `updateCollections` tests set it up: a fee per kilobyte of a millicoin, +/// and the federations, UTXOs and requests a test gives it. +pub(super) struct Collections { + pub(super) bridge: Bridge, +} + +impl Collections { + pub(super) fn mainnet_at(number: u64) -> Self { + let mut bridge = Bridge::mainnet_at(number); + put_named( + &mut bridge.ctx, + "feePerKb", + &rlp_encode_element(&[0x01, 0x86, 0xa0]), + ); + Self { bridge } + } + + /// The Bridge at `number` with the retiring federation's funds to move: + /// the active federation made, as rskj's tests make it, a block more + /// than its activation age before, so that the migration has begun, or + /// with `past` the migration's whole age before, so that it is over. The + /// active federation holds ten UTXOs, the retiring one, stored in + /// `format`, `utxos`. + pub(super) fn migrating(number: u64, format: u64, utxos: u8, past: bool) -> Self { + let age = activation_age(number) + if past { MIGRATION_AGE_END } else { 1 }; + let mut collections = Self::mainnet_at(number); + collections.active(number - age, 10); + collections.retiring(format, utxos); + collections + } + + /// The active federation, of [`active`]'s keys, made at block + /// `creation`, with `utxos` UTXOs of a bitcoin. + pub(super) fn active(&mut self, creation: u64, utxos: u8) { + let keys = active(); + store_federation( + &mut self.bridge.ctx, + NEW_FEDERATION, + &keys, + creation, + STANDARD_MULTISIG, + ); + let script = address_of(&keys); + let utxos: Vec<_> = (1..=utxos) + .map(|tag| utxo(tag, BTC, script.clone())) + .collect(); + let number = self.bridge.number(); + crate::bridge::storage::store_federation_utxos( + &mut self.bridge.ctx, + &self.bridge.cfg, + number, + &utxos, + ); + } + + /// The retiring federation, of [`retiring`]'s keys stored in `format`, + /// with `utxos` UTXOs of a bitcoin. + pub(super) fn retiring(&mut self, format: u64, utxos: u8) { + let keys = retiring(); + store_federation(&mut self.bridge.ctx, OLD_FEDERATION, &keys, 1, format); + let script = address_of(&keys); + let utxos: Vec<_> = (1..=utxos) + .map(|tag| utxo(tag.wrapping_add(0x80), BTC, script.clone())) + .collect(); + crate::bridge::storage::store_old_federation_utxos(&mut self.bridge.ctx, &utxos); + } + + /// `count` peg-out requests of half a bitcoin, each with its + /// transaction's hash. + pub(super) fn requests(&mut self, count: u8) { + let entries: Vec<([u8; 20], u64, [u8; 32])> = + (1..=count).map(|i| ([i; 20], BTC / 2, [i; 32])).collect(); + put_named( + &mut self.bridge.ctx, + "releaseRequestQueueWithTxHash", + &hashed_queue(&entries), + ); + } + + /// `updateCollections`, and what it logged. + pub(super) fn update(&mut self) -> Vec { + self.bridge.update_collections(); + logs(&mut self.bridge.ctx) + } + + /// The peg-outs waiting for confirmations. + pub(super) fn waiting(&mut self) -> Vec { + crate::bridge::peg::load_pegout_confirmation_set(&mut self.bridge.ctx, true) + .iter() + .map(|entry| bitcoin::consensus::deserialize(&entry.btc_tx_raw).expect("a transaction")) + .collect() + } + + /// Whether the pegout index holds `tx`, its first input spending + /// `redeem`. + pub(super) fn indexed(&mut self, tx: &bitcoin::Transaction, redeem: &[u8]) -> bool { + let sighash = alloy_primitives::hex::encode(index_sighash(tx, redeem)); + cell(&mut self.bridge.ctx, compound("pegoutTxSigHash", &sighash)) == Some(vec![1]) + } +} + +/// A Bridge to call, as rskj's `BridgeSupportBuilder` builds one: a block of +/// a network, the transaction that reaches the Bridge, and what it carries. +pub(super) struct Bridge { + pub(super) ctx: Ctx, + pub(super) cfg: RskHardforkConfig, + pub(super) constants: BridgeConstants, + /// The transaction that reaches the Bridge. + pub(super) tx: BridgeTxContext, + /// Who calls: the transaction's sender. + pub(super) caller: Address, + /// What the call carries. + pub(super) value: U256, +} + +/// Nobody in particular: no federator, no authorizer. +pub(super) const STRANGER: Address = Address::repeat_byte(0x99); + +impl Bridge { + /// The Bridge at block `number` of `cfg`, under `constants`, called by a + /// transaction of [`STRANGER`]'s. + pub(super) fn new(cfg: RskHardforkConfig, constants: BridgeConstants, number: u64) -> Self { + Self { + ctx: ctx_at(number), + cfg, + constants, + tx: BridgeTxContext { + rsk_sender: STRANGER, + rsk_tx_hash: [0x7e; 32], + origin_tx_hash: [0x7e; 32], + btc_sender_hash160: [0x5d; 20], + ..Default::default() + }, + caller: STRANGER, + value: U256::ZERO, + } + } + + /// At block `number` of mainnet. + pub(super) fn mainnet_at(number: u64) -> Self { + Self::new( + RskHardforkConfig::mainnet(), + BridgeConstants::mainnet(), + number, + ) + } + + /// The number of the block the Bridge runs in. + pub(super) fn number(&self) -> u64 { + revm::context_interface::Block::number(self.ctx.block()).to::() + } + + /// rskj's `bridge.execute(data)` for a transaction's own call, with all + /// the gas it asks for. The call's writes are kept when it returns and + /// dropped when it fails, as the executor keeps and drops them. + pub(super) fn execute(&mut self, data: &[u8]) -> Result { + self.ctx.chain_mut().raw_storage.commit_call(); + let number = self.number(); + let checkpoint = self.ctx.journal_mut().checkpoint(); + let tx = BridgeTxContext { + rsk_sender: self.caller, + ..self.tx.clone() + }; + let result = crate::bridge::execute_bridge( + &mut self.ctx, + data, + u64::MAX, + &self.constants, + None, + self.cfg.has_stored_block_v2(number), + &self.cfg, + &tx, + self.caller, + 1, + crate::bridge::BridgeCallKind::Call, + self.value, + ); + if result.is_ok() { + self.ctx.chain_mut().raw_storage.commit_call(); + self.ctx.journal_mut().checkpoint_commit(); + } else { + self.ctx.chain_mut().raw_storage.discard_call(); + self.ctx.journal_mut().checkpoint_revert(checkpoint); + } + result + } + + /// rskj `BridgeSupport.releaseBtc` for `satoshis`, asked for by the + /// Bridge's transaction, the value already at the Bridge as a call's value + /// reaches its callee. + pub(super) fn release(&mut self, satoshis: u64) { + let wei = U256::from(satoshis) * U256::from(WEI_PER_SATOSHI); + let held = self.balance(crate::precompiles::BRIDGE_ADDR); + self.set_balance(crate::precompiles::BRIDGE_ADDR, held + wei); + let tx = BridgeTxContext { + rsk_sender: self.caller, + ..self.tx.clone() + }; + crate::bridge::peg::release_btc( + &mut self.ctx, + 0, + &self.constants, + &self.cfg, + &tx, + self.caller, + 1, + wei, + ) + .expect("releaseBtc answers"); + } + + /// rskj `BridgeSupport.updateCollections`, called by the Bridge's + /// transaction. + pub(super) fn update_collections(&mut self) { + let tx = BridgeTxContext { + rsk_sender: self.caller, + ..self.tx.clone() + }; + crate::bridge::peg::update_collections(&mut self.ctx, 0, &self.constants, &self.cfg, &tx) + .expect("updateCollections answers"); + } + + /// What `address` holds. + pub(super) fn balance(&mut self, address: Address) -> U256 { + use revm::context_interface::journaled_state::account::JournaledAccountTr; + *self + .ctx + .journal_mut() + .load_account_mut(address) + .expect("an account") + .balance() + } + + /// Sets what `address` holds. + pub(super) fn set_balance(&mut self, address: Address, amount: U256) { + use revm::context_interface::journaled_state::account::JournaledAccountTr; + self.ctx + .journal_mut() + .load_account_mut(address) + .expect("an account") + .set_balance(amount); + } +} diff --git a/crates/execution/src/rskj_tests/bridge_pegouts/what_a_call_writes_back_test.rs b/crates/execution/src/rskj_tests/bridge_pegouts/what_a_call_writes_back_test.rs new file mode 100644 index 0000000..cda64a3 --- /dev/null +++ b/crates/execution/src/rskj_tests/bridge_pegouts/what_a_call_writes_back_test.rs @@ -0,0 +1,343 @@ +//! What a Bridge call that returns leaves in the cells it read. +//! +//! Not a port: rskj's tests mock the storage providers and check that `save` +//! is called, not what the cells hold after a call. rskj's Bridge saves after +//! every call that returns (`Bridge.execute` ends with `teardown`, which is +//! `BridgeSupport.save`), and its providers write back what the call read as +//! well as what it set: the federation stored as the new one, from RSKIP123 +//! with all three keys of each member and its format version +//! (`FederationStorageProviderImpl.saveNewFederation`), each federation's +//! UTXOs (`saveNewFederationBtcUTXOs`, `saveOldFederationBtcUTXOs`), and the +//! sets the state getters read. Before RSKIP88 a call that throws a +//! `BridgeIllegalArgumentException` is answered with null and saves nothing. + +use revm::context_interface::ContextTr; + +use super::support::*; +use crate::abi::encode::{self, Arg}; +use crate::bridge::federation::rsk_address_from_public_key; + +/// Where these tests schedule wasabi100 on rskj's regtest, and every upgrade +/// after it. +const WASABI: u64 = 10; + +/// A block of mainnet's before orchid's RSKIP88, at #729,000. +const BEFORE_RSKIP88: u64 = 700_000; + +/// rskj's regtest with wasabi100 and the upgrades after it at [`WASABI`], at +/// `block`: RSKIP123 off below it and on from it. Upgrades are scheduled in +/// their order, as this node takes an rskj schedule only so. +fn regtest_with_wasabi_at(block: u64) -> Bridge { + use RskNetworkUpgrade::*; + let cfg = [ + Wasabi100, + TwoToThree, + Papyrus200, + Iris300, + Hop400, + Fingerroot500, + Arrowhead600, + Lovell700, + Reed800, + Reed810, + Vetiver900, + ] + .into_iter() + .fold(RskHardforkConfig::rskj_regtest(), |cfg, upgrade| { + cfg.with_hardfork(upgrade, Some(WASABI)) + }); + assert!(!cfg.has_rskip123(WASABI - 1) && cfg.has_rskip123(WASABI)); + Bridge::new(cfg, BridgeConstants::rskj_regtest(), block) +} + +/// The federation's members' keys, in order. +fn members() -> Vec<[u8; 33]> { + let mut members = keys(1, 3); + members.sort(); + members +} + +/// `serializeFederationOnlyBtcKeys` of a federation of `keys`, given sorted, +/// made at block 1: how one committed before RSKIP123 is kept. +fn only_btc_keys(keys: &[[u8; 33]]) -> Vec { + let members: Vec> = keys.iter().map(|key| rlp_encode_element(key)).collect(); + rlp_encode_list(&[ + serialize_long(0), + serialize_long(1), + rlp_encode_list(&members), + ]) +} + +/// `serializeFederation` of the same federation: each member's BTC, RSK and +/// MST keys, its BTC key all three for a member that had only that one. +fn all_three_keys(keys: &[[u8; 33]]) -> Vec { + let members: Vec> = keys + .iter() + .map(|key| { + rlp_encode_element(&rlp_encode_list(&[ + rlp_encode_element(key), + rlp_encode_element(key), + rlp_encode_element(key), + ])) + }) + .collect(); + rlp_encode_list(&[ + serialize_long(0), + serialize_long(1), + rlp_encode_list(&members), + ]) +} + +/// That regtest at `block`, its new federation committed before RSKIP123, +/// called by the federation's first member. +fn with_a_federation_of_btc_keys(block: u64) -> Bridge { + let mut bridge = regtest_with_wasabi_at(block); + put_named( + &mut bridge.ctx, + NEW_FEDERATION.0, + &only_btc_keys(&members()), + ); + bridge.caller = rsk_address_from_public_key(&members()[0]).expect("an address"); + bridge +} + +/// `receiveHeaders` of no headers, a call only the federation may make from +/// RSKIP200: rskj reads the federation to know the caller. +fn receive_no_headers() -> Vec { + let mut data = selector("receiveHeaders(bytes[])"); + data.extend(encode::args(&[Arg::BytesArray(&[])])); + data +} + +/// A call of `signature`, a method of no arguments. +fn selector(signature: &str) -> Vec { + crate::abi::selector(signature).to_vec() +} + +/// The empty list, as rskj writes a set it read where its cell held nothing. +fn empty_list() -> Option> { + Some(rlp_encode_list(&[])) +} + +/// From RSKIP123 a federator's call, which reads the federation to know the +/// caller, saves it with all three keys of each member and the format +/// version of a standard multisig federation. +#[test] +fn from_rskip123_a_call_that_read_the_federation_saves_it_with_all_three_keys() { + let mut bridge = with_a_federation_of_btc_keys(WASABI); + bridge + .execute(&receive_no_headers()) + .expect("a federator's call"); + assert_eq!( + named(&mut bridge.ctx, NEW_FEDERATION.0), + Some(all_three_keys(&members())) + ); + assert_eq!( + named(&mut bridge.ctx, NEW_FEDERATION.1), + Some(serialize_long(1000)) + ); +} + +/// Before RSKIP123 the same call writes the federation back as it was kept. +#[test] +fn before_rskip123_the_call_leaves_the_federation_as_it_was() { + let mut bridge = with_a_federation_of_btc_keys(WASABI - 1); + bridge + .execute(&receive_no_headers()) + .expect("a federator's call"); + assert_eq!( + named(&mut bridge.ctx, NEW_FEDERATION.0), + Some(only_btc_keys(&members())) + ); + assert_eq!(named(&mut bridge.ctx, NEW_FEDERATION.1), None); +} + +/// A call that reads no federation saves none, though a call before it that +/// failed read it. +#[test] +fn a_call_that_read_no_federation_leaves_it_as_it_was() { + let mut bridge = with_a_federation_of_btc_keys(WASABI); + bridge.caller = STRANGER; + assert!( + bridge.execute(&receive_no_headers()).is_err(), + "a stranger's call fails" + ); + bridge + .execute(&selector("getQueuedPegoutsCount()")) + .expect("a count"); + assert_eq!( + named(&mut bridge.ctx, NEW_FEDERATION.0), + Some(only_btc_keys(&members())) + ); + assert_eq!(named(&mut bridge.ctx, NEW_FEDERATION.1), None); +} + +/// Nor does a call that reads another federation, the proposed one. +#[test] +fn a_call_that_read_another_federation_leaves_the_new_one_as_it_was() { + let mut bridge = with_a_federation_of_btc_keys(WASABI); + bridge.tx.local_call = true; + bridge + .execute(&selector("getProposedFederationSize()")) + .expect("a size"); + assert_eq!( + named(&mut bridge.ctx, NEW_FEDERATION.0), + Some(only_btc_keys(&members())) + ); +} + +/// `registerBtcTransaction` of a transaction in the Bitcoin block at +/// `height`, with `tree` for its tree, on mainnet before RSKIP88. Whether it +/// returned. +fn register_before_rskip88(bridge: &mut Registration, height: u32, tree: &[u8]) -> bool { + let raw = bitcoin::consensus::serialize(&tx_paying(&[(BTC, user(9))])); + let args = encode::args(&[ + Arg::Bytes(&raw), + Arg::Word(encode::int(i64::from(height))), + Arg::Bytes(tree), + ]); + let returned = crate::bridge::peg::register_btc_transaction( + &mut bridge.ctx, + &args, + 0, + &bridge.constants, + &bridge.cfg, + &BridgeTxContext::default(), + ) + .is_ok(); + if returned { + bridge.ctx.chain_mut().raw_storage.commit_call(); + } else { + bridge.ctx.chain_mut().raw_storage.discard_call(); + } + returned +} + +/// Before RSKIP88 a transaction whose tree is not of a tree's size, confirmed +/// enough, throws a `BridgeIllegalArgumentException`, which `Bridge.execute` +/// answers with null: the call returns and saves nothing, so the processed +/// transactions' map it read is left as it was. +#[test] +fn before_rskip88_a_call_answered_with_null_leaves_the_processed_map_unwritten() { + let mut bridge = Registration::mainnet(BEFORE_RSKIP88); + bridge.store_chain(1_000, [0x22; 32]); + assert!(register_before_rskip88(&mut bridge, 1_000, &[0x01]), "null"); + assert_eq!(named(&mut bridge.ctx, "btcTxHashesAP"), None); +} + +/// One turned down quietly, from a block above the chain's head, which no +/// block confirms, saves the map. +#[test] +fn before_rskip88_a_call_turned_down_quietly_writes_the_processed_map_back() { + let mut bridge = Registration::mainnet(BEFORE_RSKIP88); + assert!( + register_before_rskip88(&mut bridge, 2_000_000_000, &[0x01]), + "turned down" + ); + assert_eq!(named(&mut bridge.ctx, "btcTxHashesAP"), empty_list()); +} + +/// The fee estimate builds a peg-out of the active federation's UTXOs, and +/// writes them back as it returns: on a new regtest, where nothing wrote +/// them, the empty list. +#[test] +fn the_fee_estimate_writes_back_the_utxos_it_read() { + let mut bridge = Bridge::new( + RskHardforkConfig::rskj_regtest(), + BridgeConstants::rskj_regtest(), + 1, + ); + bridge + .execute(&selector("getEstimatedFeesForNextPegOutEvent()")) + .expect("an estimate"); + assert_eq!( + named(&mut bridge.ctx, "newFederationBtcUTXOs"), + empty_list() + ); +} + +/// A peg-in turned down for its amount: `registerPegIn` first computes what +/// the transaction sent, which reads the active federation's UTXOs and the +/// retiring one's, and the call writes both back. +#[test] +fn a_peg_in_turned_down_writes_back_the_utxos_it_read() { + let mut bridge = RSKIP379_INDEXED.bridge(true); + let tx = tx_paying(&[(minimum_pegin() - 1, address_of(&active()))]); + let outcome = bridge.register(&tx, RSKIP379_INDEXED.height); + assert!(outcome.returned); + assert!(outcome.active_utxos.is_empty()); + assert_eq!( + named(&mut bridge.ctx, "newFederationBtcUTXOs"), + empty_list() + ); + assert_eq!( + named(&mut bridge.ctx, "oldFederationBtcUTXOs"), + empty_list() + ); +} + +/// A transaction the legacy rules take for no peg-in, paying the federation +/// too little before RSKIP379, is left alone: nothing reads the UTXOs. +#[test] +fn a_transaction_that_is_no_peg_in_leaves_the_utxos_unwritten() { + let mut bridge = BEFORE_RSKIP379.bridge(true); + let tx = tx_paying(&[(minimum_pegin() - 1, address_of(&active()))]); + let outcome = bridge.register(&tx, BEFORE_RSKIP379.height); + assert!(outcome.returned); + assert_eq!(named(&mut bridge.ctx, "newFederationBtcUTXOs"), None); + assert_eq!(named(&mut bridge.ctx, "oldFederationBtcUTXOs"), None); +} + +/// A peg-out paying neither federation: `registerNewUtxos` reads both +/// federations' UTXOs whatever the transaction pays them, and the call +/// writes both back. +#[test] +fn a_peg_out_paying_neither_federation_writes_back_the_utxos_it_read() { + let redeem = standard_redeem(&active()); + let tx = btc_transaction(vec![input(1, multisig_spend(&redeem))], &[(BTC, user(9))]); + let mut bridge = RSKIP379_INDEXED.bridge(true); + index_pegout(&mut bridge.ctx, &index_sighash(&tx, &redeem)); + let outcome = bridge.register(&tx, RSKIP379_INDEXED.height); + assert!(outcome.returned); + assert_eq!( + named(&mut bridge.ctx, "newFederationBtcUTXOs"), + empty_list() + ); + assert_eq!( + named(&mut bridge.ctx, "oldFederationBtcUTXOs"), + empty_list() + ); +} + +/// Before RSKIP88 a transaction may ask for the state the federators sign +/// from, which reads the peg-outs waiting for signatures; the call writes +/// them back. +#[test] +fn before_rskip88_a_call_for_the_signing_state_writes_back_what_it_read() { + let mut bridge = Bridge::mainnet_at(BEFORE_RSKIP88); + bridge + .execute(&selector("getStateForBtcReleaseClient()")) + .expect("the state"); + assert_eq!(named(&mut bridge.ctx, "rskTxsWaitingFS"), empty_list()); +} + +/// And for the whole state, which reads the new federation's UTXOs, the +/// peg-outs waiting for signatures, the release request queue and the +/// peg-outs waiting for confirmations, each written back; the next peg-out +/// height is read only from RSKIP271. +#[test] +fn before_rskip88_a_call_for_the_whole_state_writes_back_what_it_read() { + let mut bridge = Bridge::mainnet_at(BEFORE_RSKIP88); + bridge + .execute(&selector("getStateForDebugging()")) + .expect("the state"); + for cell in [ + "newFederationBtcUTXOs", + "rskTxsWaitingFS", + "releaseRequestQueue", + "releaseTransactionSet", + ] { + assert_eq!(named(&mut bridge.ctx, cell), empty_list(), "{cell}"); + } + assert_eq!(named(&mut bridge.ctx, "nextPegoutHeight"), None); +} diff --git a/crates/execution/src/rskj_tests/mod.rs b/crates/execution/src/rskj_tests/mod.rs new file mode 100644 index 0000000..b3615a1 --- /dev/null +++ b/crates/execution/src/rskj_tests/mod.rs @@ -0,0 +1,24 @@ +//! Tests ported from rskj's own test suite. +//! +//! An rskj test names a behaviour rskj holds itself to, and many of them are of +//! cases no block on either network has reached: a refusal, a limit, an +//! overflow, the two sides of an activation height. Syncing the chain cannot +//! show whether rustock has such a case right. A test can. +//! +//! Each module ports one rskj test class from the release VETIVER-9.0.4 +//! (`rskj-core/src/test/java/`) and is named after it; each test asserts what +//! the rskj test asserts, in rustock's own terms and without rskj's mocks. +//! `docs/rskj-test-parity.md` keeps the table: each rskj test, and the rustock +//! test that covers it. +//! +//! The folders follow the code a test answers for. + +mod blocks; +mod bridge_federation; +mod bridge_federation_state; +mod bridge_methods; +mod bridge_pegins; +mod bridge_pegouts; +mod precompiles; +mod remasc; +mod vm; diff --git a/crates/execution/src/rskj_tests/precompiles/alt_bn128_test.rs b/crates/execution/src/rskj_tests/precompiles/alt_bn128_test.rs new file mode 100644 index 0000000..3ec9560 --- /dev/null +++ b/crates/execution/src/rskj_tests/precompiles/alt_bn128_test.rs @@ -0,0 +1,100 @@ +//! rskj's `co.rsk.pcc.AltBN128Test`: what the BN128 precompiled contracts +//! (RSKIP137) answer a contract's call with input they refuse, before +//! RSKIP197 (iris300) and from it, and with input they take. +//! +//! Each call is given exactly the contract's price, which rskj's tests hold +//! too: a gas less fails it. + +use alloy_primitives::{hex, Address}; + +use super::harness::call_precompile; + +/// iris300 on mainnet: RSKIP197. +const IRIS: u64 = 3_614_800; +/// BN128 addition. +const ADD: Address = Address::with_last_byte(6); +/// BN128 multiplication. +const MUL: Address = Address::with_last_byte(7); +/// BN128 pairing. +const PAIRING: Address = Address::with_last_byte(8); + +/// `words` words of 0x11 bytes: rskj's coordinates of no point on the curve. +fn elevens(words: usize) -> Vec { + vec![0x11; 32 * words] +} + +/// The bytes of `words`, each written in hex. +fn bytes(words: &[&str]) -> Vec { + hex::decode(words.concat()).expect("hex") +} + +/// What a contract's call of `precompile` on `input`, given `price`, comes +/// to before RSKIP197 and from it, holding first that a gas less fails it. +fn before_and_from(precompile: Address, input: &[u8], price: u32) -> [(bool, Vec); 2] { + let short = call_precompile(IRIS - 1, precompile, price - 1, input); + assert!(!short.0, "{price} less one"); + [IRIS - 1, IRIS].map(|number| call_precompile(number, precompile, price, input)) +} + +/// Input BN128 refuses: before RSKIP197 the call succeeds with no bytes +/// back, and from it the contract throws and the call fails. +#[test] +fn input_bn128_refuses_answers_empty_before_rskip197_and_fails_the_call_from_it() { + let refused = |test: &str, precompile: Address, input: &[u8], price: u32| { + let [before, from] = before_and_from(precompile, input, price); + assert_eq!(before, (true, Vec::new()), "{test}"); + assert_eq!(from, (false, Vec::new()), "{test}"); + }; + let test = "testALTBN128AddPointNotOnCurveShouldFail"; + refused(test, ADD, &elevens(4), 150); + refused("shouldFailForPointNotOnCurve", MUL, &elevens(3), 6_000); + refused("mulShouldFailForNotEnoughParams", MUL, &elevens(2), 6_000); + refused("invalidInputForPairing", PAIRING, &[0x11], 45_000); + // rskj's two pairs of a point of G1 and one of G2, not on the curve. + let g2 = [ + "1382cd45e5674247f9c900b5c6f6cabbc189c2fabe2df0bf5acd84c97818f508", + "1246178655ab8f2f26956b189894b7eb93cd4215b9937e7969e44305f80f521e", + "08331c0a261a74e7e75db1232956663cbc88110f726159c5cba1857ecd03fa64", + "1fbf8045ce3e79b5cde4112d38bcd0efbdb1295d2eefdf58151ae309d7ded7db", + ]; + let one = "0000000000000000000000000000000000000000000000000000000000000001"; + let two = "0000000000000000000000000000000000000000000000000000000000000002"; + let minus_two = "30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd45"; + let pairs = bytes(&[&[one, two][..], &g2[..], &[one, minus_two][..], &g2[..]].concat()); + let test = "shouldReturnEmptyForPairingPointsNotOnCurve"; + refused(test, PAIRING, &pairs, 113_000); +} + +/// Input BN128 takes gets the same answer before RSKIP197 and from it: a +/// multiplication of nothing is the point at infinity, 64 zero bytes; a +/// pairing of nothing holds, 1; and two valid pairs whose product is not one +/// don't, 0. +#[test] +fn input_bn128_takes_gets_the_same_answer_before_rskip197_and_from_it() { + let answers = |test: &str, precompile: Address, input: &[u8], price: u32, answer: &[u8]| { + let [before, from] = before_and_from(precompile, input, price); + assert_eq!(before, (true, answer.to_vec()), "{test}"); + assert_eq!(from, (true, answer.to_vec()), "{test}"); + }; + let mut one = [0u8; 32]; + one[31] = 1; + answers("mulShouldFailEmptyParams", MUL, &[], 6_000, &[0; 64]); + answers("shouldReturnTrueForNullInput", PAIRING, &[], 45_000, &one); + answers("emptyInputForPairing", PAIRING, &[], 45_000, &one); + let pairs = bytes(&[ + "1c76476f4def4bb94541d57ebba1193381ffa7aa76ada664dd31c16024c43f59", + "3034dd2920f673e204fee2811c678745fc819b55d3e9d294e45c9b03a76aef41", + "209dd15ebff5d46c4bd888e51a93cf99a7329636c63514396b4a452003a35bf7", + "04bf11ca01483bfa8b34b43561848d28905960114c8ac04049af4b6315a41678", + "2bb8324af6cfc93537a2ad1a445cfd0ca2a71acd7ac41fadbf933c2a51be344d", + "120a2a4cf30c1bf9845f20c6fe39e07ea2cce61f0c9bb048165fe5e4de877550", + "111e129f1cf1097710d41c4ac70fcdfa5ba2023c6ff1cbeac322de49d1b6df7c", + "103188585e2364128fe25c70558f1560f4f9350baf3959e603cc91486e110936", + "198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c2", + "1800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed", + "090689d0585ff075ec9e99ad690c3395bc4b313370b38ef355acdadcd122975b", + "12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa", + ]); + let test = "shouldReturnFalseForValidPointsPairing"; + answers(test, PAIRING, &pairs, 113_000, &[0; 32]); +} diff --git a/crates/execution/src/rskj_tests/precompiles/blake2f_null_data_test.rs b/crates/execution/src/rskj_tests/precompiles/blake2f_null_data_test.rs new file mode 100644 index 0000000..c0ee5dc --- /dev/null +++ b/crates/execution/src/rskj_tests/precompiles/blake2f_null_data_test.rs @@ -0,0 +1,55 @@ +//! rskj's `co.rsk.vm.precompiles.blake2b.Blake2fNullDataTest`: a +//! transaction's own call of BLAKE2F with no data, which rskj reads as null, +//! before RSKIP552 (vetiver900) and from it. + +use alloy_primitives::{Address, U256}; +use rustock_core::Transaction; + +use crate::rskj_tests::vm::harness::{call, Chain, ONE_RBTC, SENDER}; + +/// vetiver900 on mainnet: RSKIP552. +const VETIVER: u64 = 8_804_200; +/// BLAKE2F. +const BLAKE2F: Address = Address::with_last_byte(9); +/// The sender of the transaction that follows the call of BLAKE2F. +const OTHER_SENDER: Address = Address::repeat_byte(0xa2); + +/// rskj's two transactions, from funded senders on a chain whose next block +/// is `number`: a call of BLAKE2F with no data, and a transfer of 1 wei from +/// another sender. +fn calls(number: u64) -> (Chain, [(Transaction, Address); 2]) { + let mut chain = Chain::mainnet(number); + chain.put(SENDER, 0, ONE_RBTC, &[]); + chain.put(OTHER_SENDER, 0, ONE_RBTC, &[]); + let to = Address::with_last_byte(2); + let transactions = [ + (call(0, BLAKE2F, U256::ZERO, 30_000, &[]), SENDER), + (call(0, to, U256::from(1), 30_000, &[]), OTHER_SENDER), + ]; + (chain, transactions) +} + +/// Before RSKIP552 rskj's miner leaves such a call out of the block it +/// builds, as `BlockExecutor` drops a transaction whose execution throws +/// (here BLAKE2F's NullPointerException), and the block holds the rest. +#[test] +fn before_rskip552_the_miner_leaves_out_a_transactions_call_of_blake2f_with_no_data() { + let (chain, transactions) = calls(VETIVER - 1); + let mined = chain.mine(&transactions); + let (result, drops) = mined.expect("the miner builds the block"); + let test = "blake2fWithNullData_beforeRskip552_blockExecutorHandlesUnexpectedException"; + assert_eq!(drops.invalid, [0], "{test}"); + assert_eq!(result.tx_results.len(), 1, "{test}"); + assert!(result.tx_results[0].success, "{test}"); +} + +/// From RSKIP552 the call is refused for nothing, and the block holds it and +/// the transfer after it, which succeeds. +#[test] +fn from_rskip552_a_transactions_call_of_blake2f_with_no_data_leaves_its_block_valid() { + let (mut chain, transactions) = calls(VETIVER); + let block = chain.block(&transactions); + let test = "blake2fWithNullData_shouldNotInvalidateBlock"; + assert_eq!(block.tx_results.len(), 2, "{test}"); + assert!(block.tx_results[1].success, "{test}"); +} diff --git a/crates/execution/src/rskj_tests/precompiles/harness.rs b/crates/execution/src/rskj_tests/precompiles/harness.rs new file mode 100644 index 0000000..f48d1a2 --- /dev/null +++ b/crates/execution/src/rskj_tests/precompiles/harness.rs @@ -0,0 +1,35 @@ +//! What the ported tests of the precompiled contracts share: a contract that +//! calls a precompiled contract and returns what the call left it. + +use alloy_primitives::{Address, U256}; + +use crate::rskj_tests::vm::harness::{ + call, compile, push_address, Chain, CONTRACT, GAS, ONE_RBTC, SENDER, +}; + +/// What a contract's CALL of `precompile`, given `gas` and `input`, comes +/// to in block `number` of mainnet: whether the call succeeded, and the +/// return data it left. The input reaches the contract as its transaction's +/// data, which it copies to memory and passes on whole. +pub(super) fn call_precompile( + number: u64, + precompile: Address, + gas: u32, + input: &[u8], +) -> (bool, Vec) { + let code = compile(&format!( + "CALLDATASIZE PUSH1 0x00 PUSH2 0x0100 CALLDATACOPY + PUSH1 0x00 PUSH1 0x00 CALLDATASIZE PUSH2 0x0100 PUSH1 0x00 {} PUSH4 0x{gas:08x} CALL + PUSH1 0x00 MSTORE + RETURNDATASIZE PUSH1 0x00 PUSH1 0x20 RETURNDATACOPY + RETURNDATASIZE PUSH1 0x20 ADD PUSH1 0x00 RETURN", + push_address(precompile) + )); + let mut chain = Chain::mainnet(number); + chain.put(SENDER, 0, ONE_RBTC, &[]); + chain.put(CONTRACT, 1, U256::ZERO, &code); + let ran = chain.run(SENDER, call(0, CONTRACT, U256::ZERO, GAS, input)); + assert!(ran.success, "the calling contract runs"); + let (succeeded, data) = ran.output.split_at(32); + (succeeded[31] == 1, data.to_vec()) +} diff --git a/crates/execution/src/rskj_tests/precompiles/mod.rs b/crates/execution/src/rskj_tests/precompiles/mod.rs new file mode 100644 index 0000000..7eb5dba --- /dev/null +++ b/crates/execution/src/rskj_tests/precompiles/mod.rs @@ -0,0 +1,8 @@ +//! The precompiled contracts (rskj's `co.rsk.pcc` and `PrecompiledContracts`). + +mod alt_bn128_test; +mod blake2f_null_data_test; +mod harness; +mod precompiled_contract_address_tests; +mod precompiled_contract_test; +mod program_test; diff --git a/crates/execution/src/rskj_tests/precompiles/precompiled_contract_address_tests.rs b/crates/execution/src/rskj_tests/precompiles/precompiled_contract_address_tests.rs new file mode 100644 index 0000000..938a05f --- /dev/null +++ b/crates/execution/src/rskj_tests/precompiles/precompiled_contract_address_tests.rs @@ -0,0 +1,46 @@ +//! rskj's `co.rsk.vm.precompiles.PrecompiledContractAddressTests`: the +//! precompiled contract each address holds once RSKIP106, RSKIP119 and +//! RSKIP516 are on, as they are from reed800 on mainnet. + +use alloy_primitives::{hex, Address}; + +use super::harness::call_precompile; +use crate::hardfork::RskHardforkConfig; +use crate::precompiles::{ + rsk_precompiles, BLOCK_HEADER_ADDR, BRIDGE_ADDR, HD_WALLET_UTILS_ADDR, REMASC_ADDR, + SECP256K1_ADD_ADDR, SECP256K1_MUL_ADDR, +}; + +/// reed800 on mainnet: RSKIP516. +const REED: u64 = 8_052_200; + +/// Each of the eleven addresses rskj names holds a precompiled contract, and +/// those of sha256, ripemd160 and identity answer rskj's own vectors as +/// those contracts do. +#[test] +fn each_address_holds_its_precompiled_contract() { + let set = rsk_precompiles(&RskHardforkConfig::mainnet(), REED); + let ethereums = [1, 2, 3, 4, 5].map(Address::with_last_byte); + let rsks = [ + BRIDGE_ADDR, + REMASC_ADDR, + BLOCK_HEADER_ADDR, + HD_WALLET_UTILS_ADDR, + SECP256K1_ADD_ADDR, + SECP256K1_MUL_ADDR, + ]; + for address in ethereums.into_iter().chain(rsks) { + assert!(set.contains(&address), "testGetPrecompile: {address}"); + } + let sha256 = call_precompile(REED, Address::with_last_byte(2), 1_000, &[]); + let hash = hex!("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); + assert_eq!(sha256, (true, hash.to_vec()), "Sha256"); + let mut one = [0u8; 32]; + one[31] = 1; + let ripempd160 = call_precompile(REED, Address::with_last_byte(3), 1_000, &one); + let hash = hex!("000000000000000000000000ae387fcfeb723c3f5964509af111cf5a67f30661"); + assert_eq!(ripempd160, (true, hash.to_vec()), "Ripempd160"); + let input = hex!("112233445566"); + let identity = call_precompile(REED, Address::with_last_byte(4), 1_000, &input); + assert_eq!(identity, (true, input.to_vec()), "Identity"); +} diff --git a/crates/execution/src/rskj_tests/precompiles/precompiled_contract_test.rs b/crates/execution/src/rskj_tests/precompiles/precompiled_contract_test.rs new file mode 100644 index 0000000..f557e8c --- /dev/null +++ b/crates/execution/src/rskj_tests/precompiles/precompiled_contract_test.rs @@ -0,0 +1,89 @@ +//! rskj's `org.ethereum.vm.PrecompiledContractTest`, its tests of BLAKE2F +//! (RSKIP153, iris300): EIP-152's test vectors, run as a contract's call of +//! BLAKE2F in a block of mainnet. + +use alloy_primitives::{hex, Address}; + +use super::harness::call_precompile; + +/// iris300 on mainnet: RSKIP153. +const IRIS: u64 = 3_614_800; +/// BLAKE2F. +const BLAKE2F: Address = Address::with_last_byte(9); + +/// EIP-152's input of `rounds` rounds and the final-block flag `last`: +/// BLAKE2b-512's state h, the message "abc", and an offset of 3. +fn vector(rounds: u32, last: u8) -> Vec { + let mut input = rounds.to_be_bytes().to_vec(); + let state = [ + "48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5", + "d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b", + ]; + input.extend(hex::decode(state.concat()).expect("hex")); + let mut message = [0u8; 128]; + message[..3].copy_from_slice(b"abc"); + input.extend(message); + input.extend([3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); + input.push(last); + input +} + +/// An input BLAKE2F refuses fails the call: one of no bytes, of a byte short +/// of the 213 BLAKE2F reads, of a byte past them, and of a final-block flag +/// of 2. +#[test] +fn blake2f_refuses_an_input_of_the_wrong_length_or_flag() { + let twelve = vector(12, 1); + let cases = [ + ("blake2fTestVector0", Vec::new()), + ("blake2fTestVector1", twelve[1..].to_vec()), + ("blake2fTestVector2", [&[0u8][..], &twelve].concat()), + ("blake2fTestVector3", vector(12, 2)), + ]; + for (test, input) in cases { + let ran = call_precompile(IRIS, BLAKE2F, 1_000, &input); + assert_eq!(ran, (false, Vec::new()), "{test}"); + } +} + +/// BLAKE2F compresses EIP-152's vectors to their hashes, for a gas a round: +/// given one gas less than its rounds, the call fails. +#[test] +fn blake2f_compresses_eip152s_vectors_for_a_gas_a_round() { + let cases = [ + ( + "blake2fTestVector4", + vector(0, 1), + "08c9bcf367e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5\ + d282e6ad7f520e511f6c3e2b8c68059b9442be0454267ce079217e1319cde05b", + ), + ( + "blake2fTestVector5", + vector(12, 1), + "ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d1\ + 7d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923", + ), + ( + "blake2fTestVector6", + vector(12, 0), + "75ab69d3190a562c51aef8d88f1c2775876944407270c42c9844252c26d28752\ + 98743e7f6d5ea2f2d3e8d226039cd31b4e426ac4f2d3d666a610c2116fde4735", + ), + ( + "blake2fTestVector7", + vector(1, 1), + "b63a380cb2897d521994a85234ee2c181b5f844d2c624c002677e9703449d2fb\ + a551b3a8333bcdf5f2f7e08993d53923de3d64fcc68c034e717b9293fed7a421", + ), + ]; + for (test, input, hash) in cases { + let rounds = u32::from_be_bytes(input[..4].try_into().expect("four bytes")); + let hash = hex::decode(hash).expect("hex"); + let ran = call_precompile(IRIS, BLAKE2F, rounds, &input); + assert_eq!(ran, (true, hash), "{test}"); + if rounds > 0 { + let short = call_precompile(IRIS, BLAKE2F, rounds - 1, &input); + assert_eq!(short, (false, Vec::new()), "{test}: a gas short"); + } + } +} diff --git a/crates/execution/src/rskj_tests/precompiles/program_test.rs b/crates/execution/src/rskj_tests/precompiles/program_test.rs new file mode 100644 index 0000000..bd071c8 --- /dev/null +++ b/crates/execution/src/rskj_tests/precompiles/program_test.rs @@ -0,0 +1,26 @@ +//! rskj's `org.ethereum.vm.program.ProgramTest`, its tests of RSKIP516 +//! (reed800): from it the input a contract's call passes a precompiled +//! contract is cut to what that contract reads, and only where the contract +//! names a limit (`Program.getInputLength`). + +use alloy_primitives::Address; + +use super::harness::call_precompile; + +/// reed800 on mainnet: RSKIP516. +const REED: u64 = 8_052_200; +/// The identity precompiled contract, which names no limit. +const IDENTITY: Address = Address::with_last_byte(4); + +/// A contract's call of a precompiled contract that names no limit passes +/// it all of its input, before RSKIP516 and from it: identity, given 1,000 +/// bytes, gives all 1,000 back. +#[test] +fn a_precompile_that_names_no_limit_gets_all_of_a_contracts_input() { + let input: Vec = (0..=255u8).cycle().take(1_000).collect(); + for number in [REED - 1, REED] { + let (success, output) = call_precompile(number, IDENTITY, 100_000, &input); + assert!(success, "#{number}"); + assert_eq!(output, input, "#{number}"); + } +} diff --git a/crates/execution/src/rskj_tests/remasc/mod.rs b/crates/execution/src/rskj_tests/remasc/mod.rs new file mode 100644 index 0000000..e497e4d --- /dev/null +++ b/crates/execution/src/rskj_tests/remasc/mod.rs @@ -0,0 +1,8 @@ +//! REMASC (rskj's `co.rsk.remasc`). + +mod point_at_infinity_federator_test; +mod remasc_federation_provider_test; +mod remasc_process_miner_fees_test; +mod remasc_rsk_address_activation_test; +mod remasc_storage_provider_test; +mod runner; diff --git a/crates/execution/src/rskj_tests/remasc/point_at_infinity_federator_test.rs b/crates/execution/src/rskj_tests/remasc/point_at_infinity_federator_test.rs new file mode 100644 index 0000000..edfcdc3 --- /dev/null +++ b/crates/execution/src/rskj_tests/remasc/point_at_infinity_federator_test.rs @@ -0,0 +1,86 @@ +//! A federator whose key is the point at infinity, the one byte `00`. +//! +//! Not a port: no test of rskj's has one. rskj's +//! `RemascFederationProvider.getFederatorAddress` reads the federator's key +//! with `ECKey.fromPublicOnly`, for which Bouncy Castle decodes `00` as that +//! point, and pays `getAddress()` of it: the last 20 bytes of the Keccak-256 +//! hash of nothing, since the point's uncompressed encoding is the one byte +//! the address leaves out. The key is the BTC key before RSKIP415, the RSK +//! key from it. Here the Bridge holds a federation of two members, one with +//! that BTC key and one with that RSK key, and REMASC pays each half of the +//! federation's 168 at blocks 15 and 16. + +use alloy_primitives::{address, hex, Address, U256}; + +use super::runner::{run, without_rskip85}; +use crate::bridge::serialization::{rlp_encode_element, rlp_encode_list, rlp_encode_u64}; +use crate::bridge::storage::{bridge_storage_key, NEW_FEDERATION_KEY}; +use crate::hardfork::{RskHardforkConfig, RskNetworkUpgrade}; +use crate::precompiles::BRIDGE_ADDR; +use crate::rskj_tests::vm::harness::Chain; + +/// rskj's federator keys of `RemascFederationProviderTest`, and the +/// addresses each gives. +const BTC_PUBLIC_KEY: &str = "03a5e32151f974c35c5d08af36a8d6af0593248e8e4adca7ed0148192eb2f5d1bf"; +const RSK_PUBLIC_KEY: &str = "02f3b5fb3121932db9450121b0a37f3f051dc8b3fd5f1ea5e7cf726947d8f69c28"; +const FROM_BTC_KEY: Address = address!("131c7c821b0e4a1ab570feed952d9304e03fddd1"); +const FROM_RSK_KEY: Address = address!("54a948b3d76ce84e5c7b4b3cd01f2af1f18d41e0"); + +/// The point at infinity's address. +const INFINITY: Address = address!("dcc703c0e500b653ca82273b7bfad8045d85a470"); + +/// The chain, its Bridge holding a federation of two members, as rskj +/// serializes it: the first of BTC key `00` and RSK and MST key rskj's RSK +/// key, the second of BTC and MST key rskj's BTC key and RSK key `00`. +fn paid(hardforks: RskHardforkConfig) -> Chain { + let btc = hex::decode(BTC_PUBLIC_KEY).expect("hex"); + let rsk = hex::decode(RSK_PUBLIC_KEY).expect("hex"); + let member = |btc: &[u8], rsk: &[u8], mst: &[u8]| { + rlp_encode_element(&rlp_encode_list(&[ + rlp_encode_element(btc), + rlp_encode_element(rsk), + rlp_encode_element(mst), + ])) + }; + let federation = rlp_encode_list(&[ + rlp_encode_u64(0), + rlp_encode_u64(0), + rlp_encode_list(&[member(&[0x00], &rsk, &rsk), member(&btc, &[0x00], &btc)]), + ]); + run(hardforks, without_rskip85(), 1, &[], |chain| { + chain.put(BRIDGE_ADDR, 0, U256::ZERO, &[]); + let at = bridge_storage_key(NEW_FEDERATION_KEY); + chain.put_storage_bytes(BRIDGE_ADDR, at, &federation); + }) +} + +/// Before RSKIP415 each federator is paid at its BTC key's address: the +/// first at the point at infinity's. +#[test] +fn before_rskip415_a_federator_of_btc_key_00_is_paid_at_its_address() { + let mut hardforks = RskHardforkConfig::rskj_regtest(); + for upgrade in [ + RskNetworkUpgrade::Arrowhead600, + RskNetworkUpgrade::Arrowhead631, + RskNetworkUpgrade::Lovell700, + RskNetworkUpgrade::Reed800, + RskNetworkUpgrade::Reed810, + RskNetworkUpgrade::Vetiver900, + ] { + hardforks = hardforks.with_hardfork(upgrade, None); + } + let chain = paid(hardforks); + assert_eq!(chain.balance(INFINITY), U256::from(168)); + assert_eq!(chain.balance(FROM_BTC_KEY), U256::from(168)); + assert_eq!(chain.balance(FROM_RSK_KEY), U256::ZERO); +} + +/// From RSKIP415 each federator is paid at its RSK key's address: the +/// second at the point at infinity's. +#[test] +fn from_rskip415_a_federator_of_rsk_key_00_is_paid_at_its_address() { + let chain = paid(RskHardforkConfig::rskj_regtest()); + assert_eq!(chain.balance(FROM_RSK_KEY), U256::from(168)); + assert_eq!(chain.balance(INFINITY), U256::from(168)); + assert_eq!(chain.balance(FROM_BTC_KEY), U256::ZERO); +} diff --git a/crates/execution/src/rskj_tests/remasc/remasc_federation_provider_test.rs b/crates/execution/src/rskj_tests/remasc/remasc_federation_provider_test.rs new file mode 100644 index 0000000..f0e1ffe --- /dev/null +++ b/crates/execution/src/rskj_tests/remasc/remasc_federation_provider_test.rs @@ -0,0 +1,70 @@ +//! rskj's `co.rsk.remasc.RemascFederationProviderTest`, its tests of +//! RSKIP415 (arrowhead600): REMASC pays a federator at the address of its +//! BTC public key before it, and at the address of its RSK public key from +//! it (`RemascFederationProvider.getFederatorAddress`). Here the Bridge +//! holds a federation of one member with rskj's two keys, and REMASC pays it +//! the federation's 168 at blocks 15 and 16. + +use alloy_primitives::{address, hex, Address, U256}; + +use super::runner::{run, without_rskip85}; +use crate::bridge::federation::{serialize_federation_multikey, StoredMember}; +use crate::bridge::storage::{bridge_storage_key, NEW_FEDERATION_KEY}; +use crate::hardfork::{RskHardforkConfig, RskNetworkUpgrade}; +use crate::precompiles::BRIDGE_ADDR; +use crate::rskj_tests::vm::harness::Chain; + +/// rskj's federator keys, and the addresses each gives. +const BTC_PUBLIC_KEY: &str = "03a5e32151f974c35c5d08af36a8d6af0593248e8e4adca7ed0148192eb2f5d1bf"; +const RSK_PUBLIC_KEY: &str = "02f3b5fb3121932db9450121b0a37f3f051dc8b3fd5f1ea5e7cf726947d8f69c28"; +const FROM_BTC_KEY: Address = address!("131c7c821b0e4a1ab570feed952d9304e03fddd1"); +const FROM_RSK_KEY: Address = address!("54a948b3d76ce84e5c7b4b3cd01f2af1f18d41e0"); + +/// The chain, its Bridge holding a federation of one member whose BTC and +/// RSK keys are rskj's. +fn paid(hardforks: RskHardforkConfig) -> Chain { + let key = |key: &str| -> [u8; 33] { + let bytes = hex::decode(key).expect("hex"); + bytes.try_into().expect("a compressed key") + }; + let member = StoredMember { + btc: key(BTC_PUBLIC_KEY), + rsk: key(RSK_PUBLIC_KEY), + mst: key(RSK_PUBLIC_KEY), + }; + let federation = serialize_federation_multikey(&[member], 0, 0); + run(hardforks, without_rskip85(), 1, &[], |chain| { + chain.put(BRIDGE_ADDR, 0, U256::ZERO, &[]); + let at = bridge_storage_key(NEW_FEDERATION_KEY); + chain.put_storage_bytes(BRIDGE_ADDR, at, &federation); + }) +} + +/// Before RSKIP415 the federator is paid at the address of its BTC key. +#[test] +fn before_rskip415_remasc_pays_a_federator_at_its_btc_keys_address() { + let mut hardforks = RskHardforkConfig::rskj_regtest(); + for upgrade in [ + RskNetworkUpgrade::Arrowhead600, + RskNetworkUpgrade::Arrowhead631, + RskNetworkUpgrade::Lovell700, + RskNetworkUpgrade::Reed800, + RskNetworkUpgrade::Reed810, + RskNetworkUpgrade::Vetiver900, + ] { + hardforks = hardforks.with_hardfork(upgrade, None); + } + let chain = paid(hardforks); + let test = "getFederatorAddress_preRSKIP415_returnsRskAddressDerivedFromBtcPublicKey"; + assert_eq!(chain.balance(FROM_BTC_KEY), U256::from(2 * 168), "{test}"); + assert_eq!(chain.balance(FROM_RSK_KEY), U256::ZERO, "{test}"); +} + +/// From RSKIP415 the federator is paid at the address of its RSK key. +#[test] +fn from_rskip415_remasc_pays_a_federator_at_its_rsk_keys_address() { + let chain = paid(RskHardforkConfig::rskj_regtest()); + let test = "getFederatorAddress_postRSKIP415_returnsRskAddressDerivedFromRskPublicKey"; + assert_eq!(chain.balance(FROM_RSK_KEY), U256::from(2 * 168), "{test}"); + assert_eq!(chain.balance(FROM_BTC_KEY), U256::ZERO, "{test}"); +} diff --git a/crates/execution/src/rskj_tests/remasc/remasc_process_miner_fees_test.rs b/crates/execution/src/rskj_tests/remasc/remasc_process_miner_fees_test.rs new file mode 100644 index 0000000..12d6857 --- /dev/null +++ b/crates/execution/src/rskj_tests/remasc/remasc_process_miner_fees_test.rs @@ -0,0 +1,90 @@ +//! rskj's `co.rsk.remasc.RemascProcessMinerFeesTest`, its tests that turn +//! RSKIP85 (orchid) off: what REMASC pays at block 16 for block 6 and its +//! siblings. Block 16 takes 21,000 of the reward balance's 105,000: 4,200 +//! to RSK Labs, 168 to the federation, a tenth of the 16,632 left to the +//! miners that included the siblings, and the rest to block 6's miner and +//! the siblings' miners, a sibling losing a twentieth of its share for each +//! block it came in late (`Remasc.payWithSiblings`). + +use alloy_primitives::U256; + +use super::runner::{ + burned_balance, coinbase, reward_balance, run_without_rskip85, sibling_coinbase, +}; +use crate::precompiles::REMASC_ADDR; + +/// A sibling of block 6 that block 8 includes, a block later than it +/// could, loses a twentieth of its share. +#[test] +fn a_sibling_included_a_block_late_loses_a_twentieth_of_its_share() { + let test = "siblingIncludedOneBlockLater"; + let chain = run_without_rskip85(&[(6, 8)]); + assert_eq!(chain.balance(coinbase(8)), U256::from(1_663), "{test}"); + assert_eq!(chain.balance(coinbase(6)), U256::from(7_484), "{test}"); + let sibling = chain.balance(sibling_coinbase(0)); + assert_eq!(sibling, U256::from(7_110), "{test}"); + assert_eq!(reward_balance(&chain), U256::from(84_000), "{test}"); + assert_eq!(burned_balance(&chain), U256::from(375), "{test}"); +} + +/// Siblings of block 6 that blocks 7 and 8 include share the miners' +/// reward three ways, and the miners that included them the publishers'. +#[test] +fn siblings_included_by_two_blocks_share_the_reward_with_their_block() { + let test = "oneSiblingIncludedOneBlockLaterAndAnotherIncludedRightAfter"; + let chain = run_without_rskip85(&[(6, 7), (6, 8)]); + assert_eq!(chain.balance(coinbase(7)), U256::from(831), "{test}"); + assert_eq!(chain.balance(coinbase(8)), U256::from(831), "{test}"); + assert_eq!(chain.balance(coinbase(6)), U256::from(4_989), "{test}"); + let siblings = [0, 1].map(|index| chain.balance(sibling_coinbase(index))); + assert_eq!(siblings, [4_989u64, 4_740].map(U256::from), "{test}"); + assert_eq!(reward_balance(&chain), U256::from(84_000), "{test}"); + assert_eq!(burned_balance(&chain), U256::from(252), "{test}"); +} + +/// A sibling of block 6 that block 13 includes loses six twentieths of its +/// share, which REMASC keeps as burned. +#[test] +fn a_sibling_included_six_blocks_late_loses_six_twentieths_of_its_share() { + let test = "siblingIncludedSevenBlocksLater"; + let chain = run_without_rskip85(&[(6, 13)]); + assert_eq!(chain.balance(coinbase(13)), U256::from(1_663), "{test}"); + assert_eq!(chain.balance(coinbase(6)), U256::from(7_484), "{test}"); + let sibling = chain.balance(sibling_coinbase(0)); + assert_eq!(sibling, U256::from(5_239), "{test}"); + assert_eq!(chain.balance(REMASC_ADDR), U256::from(296_246), "{test}"); + assert_eq!(reward_balance(&chain), U256::from(84_000), "{test}"); + assert_eq!(burned_balance(&chain), U256::from(2_246), "{test}"); +} + +/// Nine siblings of block 6 that block 7 includes: each share is rounded +/// down, and what the rounding leaves is burned. Block 8's miner, paid +/// nothing yet, has no account. +#[test] +fn nine_siblings_shares_are_rounded_down_and_the_surplus_burned() { + let test = "siblingsFeeForMiningBlockMustBeRoundedAndTheRoundedSurplusBurned"; + let chain = run_without_rskip85(&[(6, 7); 9]); + assert_eq!(chain.balance(coinbase(7)), U256::from(1_656), "{test}"); + assert!(chain.account(coinbase(8)).is_none(), "{test}"); + assert_eq!(chain.balance(coinbase(6)), U256::from(1_496), "{test}"); + let siblings = [0, 1].map(|index| chain.balance(sibling_coinbase(index))); + assert_eq!(siblings, [U256::from(1_496); 2], "{test}"); + assert_eq!(reward_balance(&chain), U256::from(84_000), "{test}"); + assert_eq!(burned_balance(&chain), U256::from(16), "{test}"); +} + +/// Ten siblings of block 6 that block 7 includes: the publishers' shares +/// are rounded down too, and their surplus burned. +#[test] +fn ten_siblings_publishing_shares_are_rounded_down_and_the_surplus_burned() { + let test = "unclesPublishingFeeMustBeRoundedAndTheRoundedSurplusBurned"; + let chain = run_without_rskip85(&[(6, 7); 10]); + assert_eq!(chain.balance(coinbase(7)), U256::from(1_660), "{test}"); + assert_eq!(chain.balance(coinbase(6)), U256::from(1_360), "{test}"); + for index in 0..3 { + let sibling = chain.balance(sibling_coinbase(index)); + assert_eq!(sibling, U256::from(1_360), "{test}"); + } + assert_eq!(reward_balance(&chain), U256::from(84_000), "{test}"); + assert_eq!(burned_balance(&chain), U256::from(12), "{test}"); +} diff --git a/crates/execution/src/rskj_tests/remasc/remasc_rsk_address_activation_test.rs b/crates/execution/src/rskj_tests/remasc/remasc_rsk_address_activation_test.rs new file mode 100644 index 0000000..4067759 --- /dev/null +++ b/crates/execution/src/rskj_tests/remasc/remasc_rsk_address_activation_test.rs @@ -0,0 +1,25 @@ +//! rskj's `co.rsk.remasc.RemascRskAddressActivationTest`: RSKIP218 (iris300) +//! moves RSK Labs' cut of each reward to a new address +//! (`Remasc.getRskLabsAddress`). + +use alloy_primitives::{address, Address, U256}; + +use super::runner::{run, without_rskip85, LAST}; +use crate::hardfork::RskHardforkConfig; + +/// rskj's two addresses for RSK Labs, before RSKIP218 and from it. +const BEFORE: Address = address!("14d3065c8eb89895f4df12450ec6b130049f8034"); +const FROM: Address = address!("dcb12179ba4697350f66224c959bdd9c282818df"); + +/// With RSKIP218 at block 16, block 15 pays RSK Labs' 4,200 at the first +/// address and block 16 at the second. +#[test] +fn rsk_labs_cut_goes_to_its_rskip218_address_from_rskip218() { + let mut remasc = without_rskip85(); + remasc.rsk_labs_address = BEFORE; + remasc.rsk_labs_address_rskip218 = FROM; + remasc.rskip218_activation_height = Some(LAST); + let chain = run(RskHardforkConfig::rskj_regtest(), remasc, 1, &[], |_| {}); + assert_eq!(chain.balance(BEFORE), U256::from(4_200), "testActivation"); + assert_eq!(chain.balance(FROM), U256::from(4_200), "testActivation"); +} diff --git a/crates/execution/src/rskj_tests/remasc/remasc_storage_provider_test.rs b/crates/execution/src/rskj_tests/remasc/remasc_storage_provider_test.rs new file mode 100644 index 0000000..84a1eb7 --- /dev/null +++ b/crates/execution/src/rskj_tests/remasc/remasc_storage_provider_test.rs @@ -0,0 +1,171 @@ +//! rskj's `co.rsk.remasc.RemascStorageProviderTest`, its tests of RSKIP85 +//! (orchid): without it REMASC pays every block, the federation too; with +//! it a reward under the minimum gas price times the minimum payable gas +//! waits in the reward balance, and a federator's share under the minimum +//! gas price times the federator's minimum accrues in the federation's. + +use alloy_primitives::{hex, Address, B256, U256}; +use rustock_core::{Header, Transaction}; + +use super::runner::{ + burned_balance, federation_balance, reward_balance, run, run_without_rskip85, without_rskip85, +}; +use crate::bridge::constants::BridgeConstants; +use crate::bridge::federation::rsk_address_from_public_key; +use crate::hardfork::RskHardforkConfig; +use crate::precompiles::REMASC_ADDR; +use crate::remasc::RemascConfig; +use crate::rskj_tests::vm::harness::{Chain, ONE_RBTC}; + +/// The first federator of the federation REMASC pays, the Bridge's genesis +/// federation, and how many federators it has. +fn first_federator() -> (Address, u64) { + let constants = BridgeConstants::mainnet(); + let mut keys: Vec> = constants + .genesis_federation_public_keys + .iter() + .map(|key| hex::decode(key).expect("hex")) + .collect(); + keys.sort(); + let first = rsk_address_from_public_key(&keys[0]).expect("a key"); + (first, keys.len() as u64) +} + +/// The runner with RSKIP85 on, at `gas_price`, the minimum payable gas +/// `reward` and the federator's minimum payable gas `federator`. +fn with_rskip85(gas_price: u64, reward: u64, federator: u64) -> Chain { + let mut remasc = RemascConfig::regtest(); + remasc.minimum_payable_gas = reward; + remasc.federator_minimum_payable_gas = federator; + let hardforks = RskHardforkConfig::rskj_regtest(); + run(hardforks, remasc, gas_price, &[], |_| {}) +} + +/// Without RSKIP85 REMASC pays at blocks 15 and 16, and after block 16 its +/// reward balance holds 84,000 and nothing is burned. +#[test] +fn without_rskip85_remasc_pays_every_block() { + let chain = run_without_rskip85(&[]); + let test = "alwaysPaysBeforeRFS"; + assert_eq!(reward_balance(&chain), U256::from(84_000), "{test}"); + assert_eq!(burned_balance(&chain), U256::ZERO, "{test}"); +} + +/// Transfers at a gas price of 0 pay no fees: with siblings of blocks 6 and +/// 11, which blocks 7 and 12 include, REMASC's reward and burned balances +/// stay at 0. +#[test] +fn without_fees_remasc_keeps_nothing_for_blocks_with_siblings() { + let hardforks = RskHardforkConfig::rskj_regtest(); + let chain = run(hardforks, without_rskip85(), 0, &[(6, 7), (11, 12)], |_| {}); + let test = "setSaveRetrieveAndGetSiblingsBeforeRFS"; + assert_eq!(reward_balance(&chain), U256::ZERO, "{test}"); + assert_eq!(burned_balance(&chain), U256::ZERO, "{test}"); +} + +/// Without RSKIP85 the federation is paid its 168 at blocks 15 and 16, and +/// REMASC keeps none of it: the first federator gets its share of each. +#[test] +fn without_rskip85_the_federation_is_paid_every_block() { + let test = "alwaysPaysFedBeforeRFS"; + let chain = run_without_rskip85(&[]); + assert_eq!(federation_balance(&chain), U256::ZERO, "{test}"); + let (first, federators) = first_federator(); + let share = 168 / federators; + assert_eq!(chain.balance(first), U256::from(2 * share), "{test}"); +} + +/// With RSKIP85 a reward under the floor waits: at a floor of 100,000 (rskj's +/// 10,000 gas at its runner's minimum gas price of 10; here 100,000 at the +/// blocks' minimum of 1) block 16's 21,000 is not paid, and the reward +/// balance holds all six blocks' fees. +#[test] +fn from_rskip85_a_reward_under_its_floor_waits_in_the_reward_balance() { + let chain = with_rskip85(1, 100_000, 0); + let test = "doesntPayBelowMinimumRewardAfterRFS"; + assert_eq!(reward_balance(&chain), U256::from(126_000), "{test}"); + assert_eq!(burned_balance(&chain), U256::ZERO, "{test}"); +} + +/// With RSKIP85 a reward at its floor is paid: at a gas price of 10 each +/// block pays 210,000, and 21,000 gas at 10 is that floor. +#[test] +fn from_rskip85_a_reward_at_its_floor_is_paid() { + let chain = with_rskip85(10, 21_000, 10); + let test = "paysWhenHigherThanMinimumRewardAfterRFS"; + assert_eq!(reward_balance(&chain), U256::from(840_000), "{test}"); + assert_eq!(burned_balance(&chain), U256::ZERO, "{test}"); +} + +/// With RSKIP85 a federator's share under its floor accrues: the +/// federation's 168 of blocks 15 and 16, against a floor of 10,000, waits +/// as 336, and the first federator has no account. +#[test] +fn from_rskip85_a_federators_share_under_its_floor_accrues() { + let chain = with_rskip85(1, 0, 10_000); + let test = "doesntPayFedBelowMinimumRewardAfterRFS"; + assert_eq!(federation_balance(&chain), U256::from(336), "{test}"); + assert!(chain.account(first_federator().0).is_none(), "{test}"); +} + +/// With RSKIP85 a federator's share over its floor is paid: at a gas price +/// of 10 the federation gets 1,680 at blocks 15 and 16, against a floor of +/// 100. +#[test] +fn from_rskip85_a_federators_share_over_its_floor_is_paid() { + let chain = with_rskip85(10, 0, 10); + let test = "paysFedWhenHigherThanMinimumRewardAfterRFS"; + assert_eq!(federation_balance(&chain), U256::ZERO, "{test}"); + let (first, federators) = first_federator(); + let share = 1_680 / federators; + assert_eq!(chain.balance(first), U256::from(2 * share), "{test}"); +} + +/// rskj's runner to block 14 on one coinbase at a gas price of 10, then +/// blocks 15 to 18, whose transfers pay 210,000, 21,000, 210,000 and 21,000 +/// in fees, and ten more with none, each at a minimum gas price of 10: +/// RSKIP85's floor is 21,000 gas at that price, 210,000. The transfers of +/// blocks 16 and 18, at a gas price of 1, are under their blocks' minimum, +/// which consensus refuses and rustock's execution too; REMASC reads no more +/// of a block than its fees, so here each block carries its fees and +/// REMASC's transaction alone. By block 25 ten blocks are paid, 166,320 +/// each; block 26 holds back block 16's reward, 172,200 being under the +/// floor; block 27 pays 169,647 of 214,200; block 28 holds back 175,560. +#[test] +fn from_rskip85_only_rewards_that_reach_the_floor_are_paid() { + let test = "paysOnlyBlocksWithEnoughBalanceAccumulatedAfterRFS"; + let mut remasc = RemascConfig::regtest(); + remasc.minimum_payable_gas = 21_000; + let hardforks = RskHardforkConfig::rskj_regtest(); + let mut chain = + Chain::new(hardforks, 1).map_executor(|executor| executor.with_remasc_config(remasc)); + chain.put(REMASC_ADDR, 0, ONE_RBTC, &[]); + let coinbase = Address::repeat_byte(0xcb); + let mut parent = B256::ZERO; + for number in 1..=28 { + let fees = match number { + 16 | 18 => 21_000, + 19..=28 => 0, + _ => 210_000, + }; + let header = Header { + parent_hash: parent, + beneficiary: coinbase, + number, + paid_fees: U256::from(fees), + minimum_gas_price: U256::from(10), + ..chain.header() + }; + let remasc = [(Transaction::remasc(number), Address::ZERO)]; + chain.import(&header, &[], &remasc); + parent = header.hash(); + let paid = match number { + 24 => 1_663_200, + 25 | 26 => 1_829_520, + 27 | 28 => 1_999_167, + _ => continue, + }; + let balance = chain.balance(coinbase); + assert_eq!(balance, U256::from(paid), "{test} at block {number}"); + } +} diff --git a/crates/execution/src/rskj_tests/remasc/runner.rs b/crates/execution/src/rskj_tests/remasc/runner.rs new file mode 100644 index 0000000..56c20fd --- /dev/null +++ b/crates/execution/src/rskj_tests/remasc/runner.rs @@ -0,0 +1,140 @@ +//! rskj's `RemascTestRunner`, which the REMASC tests share: a regtest chain +//! of blocks 1 to 16, each mined by its own coinbase and holding a transfer +//! whose gas is its fees, then REMASC's transaction; siblings of its blocks, +//! mined on their parents, are included as uncles. On regtest's REMASC +//! constants (maturity 10, synthetic span 5) block 16 pays for block 6, out +//! of a reward balance the blocks before it filled. +//! +//! rskj's runner gives its blocks a minimum gas price of 10, over their +//! transfers' price of 1, with its block validation off; validation refuses +//! such a block (`TxsMinGasPriceRule`), and so does rustock's execution. +//! Here a block's minimum is its transfer's price: with RSKIP85 off, rskj's +//! figures don't depend on it. + +use alloy_primitives::{Address, B256, U256}; +use rustock_core::{Header, Transaction}; + +use crate::bridge::constants::BridgeConstants; +use crate::hardfork::RskHardforkConfig; +use crate::precompiles::REMASC_ADDR; +use crate::remasc::{ + remasc_storage_key, RemascConfig, BURNED_BALANCE_KEY, FEDERATION_BALANCE_KEY, + REWARD_BALANCE_KEY, +}; +use crate::rskj_tests::vm::harness::{call, Chain, ONE_RBTC, SENDER}; + +/// The chain's last block, which pays for block 6. +pub(super) const LAST: u64 = 16; +/// Where each block's transfer goes. +const PAYEE: Address = Address::repeat_byte(0x0e); + +/// The coinbase of block `number`. +pub(super) fn coinbase(number: u64) -> Address { + let mut bytes = [0xcb; 20]; + bytes[19] = number as u8; + Address::from(bytes) +} + +/// The coinbase of the `index`th sibling. +pub(super) fn sibling_coinbase(index: usize) -> Address { + let mut bytes = [0x5b; 20]; + bytes[19] = index as u8; + Address::from(bytes) +} + +/// Regtest's REMASC constants with RSKIP85 off, as +/// `ActivationConfigsForTest.allBut(RSKIP85)` leaves it: with it, a reward +/// under the minimum gas price times 200,000 is held back, as at rskj's +/// runner's minimum of 10 every reward would be. +pub(super) fn without_rskip85() -> RemascConfig { + let mut remasc = RemascConfig::regtest(); + remasc.rskip85_activation_height = Some(u64::MAX); + remasc +} + +/// Runs the chain on `hardforks` and `remasc`, with transfers at +/// `gas_price` and `siblings`, each a pair of the block it is a sibling of +/// and the block that includes it; `prepare` writes what the chain holds +/// before block 1. +pub(super) fn run( + hardforks: RskHardforkConfig, + remasc: RemascConfig, + gas_price: u64, + siblings: &[(u64, u64)], + prepare: impl FnOnce(&mut Chain), +) -> Chain { + let mut chain = Chain::new(hardforks, 1).map_executor(|executor| { + let executor = executor.with_remasc_config(remasc); + executor.with_bridge_constants(BridgeConstants::mainnet()) + }); + chain.put(SENDER, 0, ONE_RBTC, &[]); + prepare(&mut chain); + let mut hashes = vec![B256::ZERO]; + for number in 1..=LAST { + let ommers: Vec
= siblings + .iter() + .enumerate() + .filter(|(_, (_, included))| *included == number) + .map(|(index, (of, _))| { + let parent = hashes[*of as usize - 1]; + header(&chain, *of, parent, sibling_coinbase(index), gas_price, 0) + }) + .collect(); + let uncles = ommers.len() as u64; + let parent = hashes[number as usize - 1]; + let header = header(&chain, number, parent, coinbase(number), gas_price, uncles); + let transfer = Transaction { + gas_price: U256::from(gas_price), + ..call(number - 1, PAYEE, U256::from(10_000), 21_000, &[]) + }; + let remasc = Transaction::remasc(number); + let transactions = [(transfer, SENDER), (remasc, Address::ZERO)]; + chain.import(&header, &ommers, &transactions); + hashes.push(header.hash()); + } + chain +} + +/// rskj's runner with RSKIP85 off, transfers at a gas price of 1, and +/// `siblings`. +pub(super) fn run_without_rskip85(siblings: &[(u64, u64)]) -> Chain { + let hardforks = RskHardforkConfig::rskj_regtest(); + run(hardforks, without_rskip85(), 1, siblings, |_| {}) +} + +/// The header of block `number` on `parent`, mined by `coinbase` at a +/// minimum gas price of `gas_price`, with the fees of 21,000 gas at that +/// price paid and `uncles` uncles. +fn header( + chain: &Chain, + number: u64, + parent: B256, + coinbase: Address, + gas_price: u64, + uncles: u64, +) -> Header { + Header { + parent_hash: parent, + beneficiary: coinbase, + number, + paid_fees: U256::from(21_000 * gas_price), + minimum_gas_price: U256::from(gas_price), + uncle_count: uncles, + ..chain.header() + } +} + +/// REMASC's reward balance. +pub(super) fn reward_balance(chain: &Chain) -> U256 { + chain.storage_word(REMASC_ADDR, remasc_storage_key(REWARD_BALANCE_KEY)) +} + +/// REMASC's burned balance. +pub(super) fn burned_balance(chain: &Chain) -> U256 { + chain.storage_word(REMASC_ADDR, remasc_storage_key(BURNED_BALANCE_KEY)) +} + +/// What REMASC keeps for the federation. +pub(super) fn federation_balance(chain: &Chain) -> U256 { + chain.storage_word(REMASC_ADDR, remasc_storage_key(FEDERATION_BALANCE_KEY)) +} diff --git a/crates/execution/src/rskj_tests/vm/contract_creating_dsl_rollback_test.rs b/crates/execution/src/rskj_tests/vm/contract_creating_dsl_rollback_test.rs new file mode 100644 index 0000000..6d46eaa --- /dev/null +++ b/crates/execution/src/rskj_tests/vm/contract_creating_dsl_rollback_test.rs @@ -0,0 +1,50 @@ +//! rskj's `org.ethereum.core.ContractCreatingDslRollbackTest`, its tests of +//! RSKIP453 (lovell700): before it a contract's CREATE whose new code is +//! past 24,576 bytes leaves the new contract with no code and the creating +//! contract going on (`Program.exceedsMaxContractSize`, then +//! `finalizeContractCreation`); from it the creation is rolled back. + +use alloy_primitives::U256; + +use super::harness::{call, compile, return_top, Chain, CONTRACT, ONE_RBTC, SENDER}; + +/// lovell700 on mainnet: RSKIP453. +const LOVELL: u64 = 7_338_024; +/// The gas of the transaction: enough to pay for the new code's bytes. +const CREATION_GAS: u64 = 6_000_000; + +/// What a contract's CREATE of 24,577 zero bytes of code does in block +/// `number` of mainnet: whether its transaction succeeded, and the code and +/// account it left at the new address. +fn create_past_the_limit(number: u64) -> (bool, Vec, bool) { + // Init code: PUSH2 24,577, PUSH1 0, RETURN. + let init = "PUSH6 0x6160016000f3 PUSH1 0x00 MSTORE PUSH1 0x06 PUSH1 0x1a PUSH1 0x00 CREATE"; + let mut code = compile(init); + code.extend(return_top(1)); + let mut chain = Chain::mainnet(number); + chain.put(SENDER, 0, ONE_RBTC, &[]); + chain.put(CONTRACT, 1, U256::ZERO, &code); + let ran = chain.run(SENDER, call(0, CONTRACT, U256::ZERO, CREATION_GAS, &[])); + let created = CONTRACT.create(1); + let exists = chain.account(created).is_some(); + (ran.success, chain.code(created), exists) +} + +/// Before RSKIP453 the transaction succeeds, and the new contract is there +/// with no code. +#[test] +fn before_rskip453_code_past_the_limit_leaves_its_contract_empty() { + let (success, code, exists) = create_past_the_limit(LOVELL - 1); + let test = "createExecutionExceedingCodeSizeDoesNotRollbackBefore453"; + assert!(success, "{test}"); + assert!(code.is_empty() && exists, "{test}"); +} + +/// From RSKIP453 the creation is rolled back, and the transaction fails. +#[test] +fn from_rskip453_code_past_the_limit_fails_the_creation() { + let (success, _, exists) = create_past_the_limit(LOVELL); + let test = "createExecutionExceedingCodeSizeDoesRollbackAfter453"; + assert!(!success, "{test}"); + assert!(!exists, "{test}"); +} diff --git a/crates/execution/src/rskj_tests/vm/create2_test.rs b/crates/execution/src/rskj_tests/vm/create2_test.rs new file mode 100644 index 0000000..e8d3732 --- /dev/null +++ b/crates/execution/src/rskj_tests/vm/create2_test.rs @@ -0,0 +1,24 @@ +//! rskj's `co.rsk.vm.Create2Test`, its tests of RSKIP125 (wasabi100): CREATE2 +//! is an opcode from it, and an invalid one before it. + +use alloy_primitives::U256; + +use super::harness::{compile, return_top, run_code, GAS}; + +/// wasabi100 on mainnet: RSKIP125. +const WASABI: u64 = 1_591_000; + +/// Before RSKIP125 CREATE2 is an invalid opcode: the contract that runs it +/// fails with all its gas. From RSKIP125 it creates the contract. +#[test] +fn create2_is_an_invalid_opcode_before_rskip125() { + let mut code = compile("PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 CREATE2"); + code.extend(return_top(1)); + let before = run_code(WASABI - 1, &code); + let test = "testCREATE2ShouldFailInvalidOpcode"; + assert!(!before.success, "{test}"); + assert_eq!(before.gas_used, GAS, "{test}"); + let from = run_code(WASABI, &code); + assert!(from.success, "{test}"); + assert_ne!(U256::from_be_slice(&from.output), U256::ZERO, "{test}"); +} diff --git a/crates/execution/src/rskj_tests/vm/dsl_files_test.rs b/crates/execution/src/rskj_tests/vm/dsl_files_test.rs new file mode 100644 index 0000000..65f8949 --- /dev/null +++ b/crates/execution/src/rskj_tests/vm/dsl_files_test.rs @@ -0,0 +1,39 @@ +//! rskj's `co.rsk.test.DslFilesTest`, its test of the gas of a creation +//! before RSKIP438 (lovell700) and from it. + +use alloy_primitives::hex; + +use super::harness::{create, Chain, ONE_RBTC, SENDER}; + +/// lovell700 on mainnet: RSKIP438. +const LOVELL: u64 = 7_338_024; + +/// The init code of rskj's `create01.txt`, solc's empty contract: 80 bytes, +/// three words. +fn empty_contract() -> Vec { + let code = [ + "60606040523415600e57600080fd5b603580601b6000396000f3006060604052600080fd00", + "a165627a7a72305820b25edb28bec763685838b8044760e105b5385638276b4768c8045237b8fc", + "6bf10029", + ]; + hex::decode(code.concat()).expect("hex") +} + +/// The gas the creation of rskj's empty contract uses in block `number` of +/// mainnet. +fn creation_gas(number: u64) -> u64 { + let mut chain = Chain::mainnet(number); + chain.put(SENDER, 0, ONE_RBTC, &[]); + let ran = chain.run(SENDER, create(0, 500_000, &empty_contract())); + assert!(ran.success, "the creation runs"); + ran.gas_used +} + +/// The creation of rskj's empty contract uses 64,857 gas before RSKIP438, +/// and 64,863 from it: 2 more for each of its init code's three words. +#[test] +fn a_creation_of_rskjs_empty_contract_uses_6_gas_more_from_rskip438() { + let test = "runCreate01ResourceBeforeActivationOfRSKIP438"; + assert_eq!(creation_gas(LOVELL - 1), 64_857, "{test}"); + assert_eq!(creation_gas(LOVELL), 64_863, "runCreate01Resource"); +} diff --git a/crates/execution/src/rskj_tests/vm/evm_rules.rs b/crates/execution/src/rskj_tests/vm/evm_rules.rs new file mode 100644 index 0000000..3090d6b --- /dev/null +++ b/crates/execution/src/rskj_tests/vm/evm_rules.rs @@ -0,0 +1,520 @@ +//! The EVM's rules as rskj's `VM`, `Program` and `TransactionExecutor` hold +//! them, where no ported test holds a rule or one side of its activation: +//! each case runs as a contract's code in a block, on each side of the height +//! rskj's `reference.conf` gives the rule. + +use alloy_primitives::{keccak256, Address, B256, U256}; +use rustock_core::Transaction; + +use super::harness::{ + call, compile, create, hex_word, push_address, return_top, run_code, words, Chain, CONTRACT, + GAS, ONE_RBTC, SENDER, +}; + +/// orchid on mainnet: RSKIP91. +const ORCHID: u64 = 729_000; +/// wasabi100 on mainnet: RSKIP125. +const WASABI: u64 = 1_591_000; +/// twoToThree on mainnet: RSKIP150. +const TWO_TO_THREE: u64 = 2_018_000; +/// papyrus200 on mainnet: RSKIP140. +const PAPYRUS: u64 = 2_392_700; +/// iris300 on mainnet. +const IRIS: u64 = 3_614_800; +/// lovell700 on mainnet. +const LOVELL: u64 = 7_338_024; +/// The contract a case's contract calls. +const CALLEE: Address = Address::repeat_byte(0xce); + +/// RSKIP91 brings STATICCALL. Before it rskj's `VM` throws for the opcode as +/// for one it doesn't have (`OP_STATICCALL`), and the frame fails with all +/// its gas; from it the call runs, and pushes 1 for a callee with no code. +/// Before RSKIP103 (orchid060) STATICCALL takes a value, so the program gives +/// it seven words on both sides. +#[test] +fn staticcall_is_an_invalid_opcode_before_rskip91() { + let static_call = + compile("PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 PUSH1 0x2b GAS STATICCALL"); + let code = [static_call, return_top(1)].concat(); + + let before = run_code(ORCHID - 1, &code); + assert!(!before.success, "#{}", ORCHID - 1); + assert_eq!(before.gas_used, GAS, "#{}", ORCHID - 1); + + let from = run_code(ORCHID, &code); + assert!(from.success, "#{ORCHID}"); + assert_eq!(words(&from.output), [U256::from(1)], "#{ORCHID}"); +} + +/// In a static call, rskj's `VM.getMessageCall` refuses a CALL that moves a +/// value, and `doCREATE2` refuses CREATE2: the callee fails, and the STATICCALL +/// pushes 0. A CALLCODE that moves a value is no CALL, and runs: rskj checks +/// the opcode, not the value. The same callee run by a CALL does all three. +/// The callee holds 1,000 wei, so that only the static call can refuse it. +#[test] +fn in_a_static_call_a_call_with_a_value_and_create2_fail_and_callcode_with_one_runs() { + let to = push_address(Address::repeat_byte(0x2b)); + let callee_codes = [ + format!("PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 PUSH1 0x01 {to} PUSH2 0xffff CALL"), + format!( + "PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 PUSH1 0x01 {to} PUSH2 0xffff CALLCODE" + ), + "PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 CREATE2".to_string(), + ]; + let callee = push_address(CALLEE); + let zeros = "PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 PUSH1 0x00"; + let statically = compile(&format!("{zeros} {callee} PUSH3 0x030d40 STATICCALL")); + let plainly = compile(&format!("{zeros} PUSH1 0x00 {callee} PUSH3 0x030d40 CALL")); + let run = |caller: &[u8], callee_code: &str| { + let mut chain = Chain::mainnet(IRIS); + chain.put(SENDER, 0, ONE_RBTC, &[]); + chain.put(CONTRACT, 1, U256::ZERO, &[caller, &return_top(1)].concat()); + chain.put(CALLEE, 1, U256::from(1_000), &compile(callee_code)); + let ran = chain.run(SENDER, call(0, CONTRACT, U256::ZERO, GAS, &[])); + assert!(ran.success, "{callee_code}"); + words(&ran.output) + }; + let pushed: Vec<_> = callee_codes + .iter() + .map(|code| run(&statically, code)) + .collect(); + let (zero, one) = (vec![U256::ZERO], vec![U256::from(1)]); + assert_eq!(pushed, [zero.clone(), one.clone(), zero], "by a STATICCALL"); + let pushed: Vec<_> = callee_codes + .iter() + .map(|code| run(&plainly, code)) + .collect(); + assert_eq!(pushed, [one.clone(), one.clone(), one], "by a CALL"); +} + +/// The init code CREATE2 runs in the cases below: 0x60, a PUSH1 that the code +/// ends before, which pushes 0 and stops, leaving the new contract no code. +const PUSH1_AT_THE_END: &str = "PUSH1 0x60 PUSH1 0x00 MSTORE8"; + +/// CREATE2 of 10 wei, salt 0, and the byte at 0 of memory as its init code. +const CREATE2_OF_THE_BYTE: &str = "PUSH1 0x00 PUSH1 0x01 PUSH1 0x00 PUSH1 0x0a CREATE2"; + +/// rskj's `Create2Test.testCREATE2_DuplicateContractCreation`. From RSKIP125 +/// `Program.createContract` refuses a creation at an address that holds code +/// or a nonce, and the contract a first CREATE2 made holds a nonce (1) and no +/// code: a second CREATE2 of the same init code and salt makes nothing, moves +/// no value, and spends all the gas it was given, all the program had. The +/// STOP after it needs none, so the transaction succeeds. +#[test] +fn a_second_create2_at_the_address_of_one_it_made_fails_with_all_the_gas() { + let code = compile(&format!( + "{PUSH1_AT_THE_END} {CREATE2_OF_THE_BYTE} {CREATE2_OF_THE_BYTE} STOP" + )); + let mut chain = Chain::mainnet(IRIS); + chain.put(SENDER, 0, ONE_RBTC, &[]); + chain.put(CONTRACT, 1, U256::from(1_000), &code); + let ran = chain.run(SENDER, call(0, CONTRACT, U256::ZERO, GAS, &[])); + assert!(ran.success); + assert_eq!( + ran.gas_used, GAS, + "the second CREATE2 spent the gas it was given" + ); + let made = CONTRACT.create2_from_code(B256::ZERO, [0x60]); + let account = chain.account(made).expect("the first CREATE2's contract"); + assert_eq!( + (account.nonce, account.balance), + (U256::from(1), U256::from(10)) + ); + assert_eq!( + chain.balance(CONTRACT), + U256::from(990), + "the second moved no value" + ); +} + +/// rskj's `Create2Test.testCREATE2_PreserveContractAddressBalance`: CREATE2 at +/// an address that holds a balance keeps it, and adds the value it moves. +/// `Program.createContract` carries an existing account's balance over +/// (`createAccount(contractAddress, existingAccount)`) at every height; only +/// a creation transaction waits for RSKIP174 (iris300) to. +#[test] +fn create2_keeps_the_balance_already_at_the_address_at_every_height() { + let code = compile(&format!("{PUSH1_AT_THE_END} {CREATE2_OF_THE_BYTE}")); + let code = [code, return_top(1)].concat(); + let made = CONTRACT.create2_from_code(B256::ZERO, [0x60]); + for number in [WASABI, IRIS - 1, IRIS] { + let mut chain = Chain::mainnet(number); + chain.put(SENDER, 0, ONE_RBTC, &[]); + chain.put(CONTRACT, 1, U256::from(1_000), &code); + chain.put(made, 0, U256::from(100), &[]); + let ran = chain.run(SENDER, call(0, CONTRACT, U256::ZERO, GAS, &[])); + let pushed = U256::from_be_slice(made.as_slice()); + assert_eq!(words(&ran.output), [pushed], "#{number}"); + assert_eq!(chain.balance(made), U256::from(110), "#{number}"); + } +} + +/// The identity precompile (0x04) named by its address alone, and by a word +/// that holds a bit above the address's 20 bytes. +const IDENTITY: &str = "PUSH1 0x04"; +const IDENTITY_PAST_AN_ADDRESS: &str = + "PUSH32 0xff00000000000000000000000000000000000000000000000000000000000004"; + +/// rskj's `VM` looks for a precompile with the whole word that names an +/// address (`PrecompiledContracts.getContractForAddress`), where all else it +/// reads of an address takes the word's last 20 bytes (`RskAddress(DataWord)`). +/// A word with a bit set above them names no precompile: EXTCODESIZE reads the +/// code of the account at those 20 bytes, which has none (`doCODESIZE`), and +/// EXTCODEHASH reads that account, which doesn't exist (`doEXTCODEHASH`), +/// where the precompile's own address answers 2^256 - 1 and keccak256(""). +#[test] +fn extcodesize_and_extcodehash_of_a_word_past_a_precompiles_address_read_the_account() { + let code = compile(&format!( + "{IDENTITY} EXTCODESIZE {IDENTITY_PAST_AN_ADDRESS} EXTCODESIZE \ + {IDENTITY} EXTCODEHASH {IDENTITY_PAST_AN_ADDRESS} EXTCODEHASH" + )); + let ran = run_code(IRIS, &[code, return_top(4)].concat()); + assert!(ran.success); + let keccak_empty = U256::from_be_slice(keccak256([]).as_slice()); + assert_eq!( + words(&ran.output), + [U256::ZERO, keccak_empty, U256::ZERO, U256::MAX] + ); +} + +/// Nor does the word name a precompile to a call (`VM.doCALL`): rskj's +/// `Program.callToAddress` calls the account at its 20 bytes, which holds no +/// code, so nothing runs, all the gas given comes back, and from RSKIP171 no +/// return data is left. CALL and STATICCALL create the account, as +/// `addBalance` creates the account a call runs as. Through the precompile's +/// own address, identity returns the word it is given, for 18 gas. +#[test] +fn a_call_through_a_word_past_a_precompiles_address_runs_no_precompile() { + let identity = Address::with_last_byte(0x04); + for opcode in ["CALL", "CALLCODE", "DELEGATECALL", "STATICCALL"] { + let value = if matches!(opcode, "CALL" | "CALLCODE") { + "PUSH1 0x00" + } else { + "" + }; + let run = |callee: &str| { + let code = compile(&format!( + "PUSH1 0x2a PUSH1 0x00 MSTORE \ + PUSH1 0x00 PUSH1 0x00 PUSH1 0x20 PUSH1 0x00 {value} {callee} PUSH3 0x030d40 \ + {opcode} RETURNDATASIZE" + )); + let mut chain = Chain::mainnet(IRIS); + chain.put(SENDER, 0, ONE_RBTC, &[]); + chain.put(CONTRACT, 1, U256::ZERO, &[code, return_top(2)].concat()); + let ran = chain.run(SENDER, call(0, CONTRACT, U256::ZERO, GAS, &[])); + assert!(ran.success, "{opcode}"); + let created = chain.account(identity).is_some(); + (words(&ran.output), ran.gas_used, created) + }; + let (past, past_gas, created) = run(IDENTITY_PAST_AN_ADDRESS); + let (named, named_gas, _) = run(IDENTITY); + assert_eq!(past, [U256::ZERO, U256::from(1)], "{opcode}"); + assert_eq!(named, [U256::from(32), U256::from(1)], "{opcode}"); + assert_eq!(named_gas - past_gas, 18, "{opcode}"); + let creates = matches!(opcode, "CALL" | "STATICCALL"); + assert_eq!(created, creates, "{opcode}"); + } +} + +/// The call to the account cleans the return data only from RSKIP171 +/// (`Program.cleanReturnDataBuffer`): before it, the 64 bytes identity +/// returned to the call before it are still there, where a precompile's call +/// that returns sets the data to its output from RSKIP90 (orchid). +#[test] +fn before_rskip171_a_call_through_a_word_past_a_precompiles_address_keeps_the_return_data() { + let code = compile(&format!( + "PUSH1 0x2a PUSH1 0x00 MSTORE \ + PUSH1 0x00 PUSH1 0x00 PUSH1 0x40 PUSH1 0x00 PUSH1 0x00 {IDENTITY} PUSH3 0x030d40 CALL POP \ + PUSH1 0x00 PUSH1 0x00 PUSH1 0x20 PUSH1 0x00 PUSH1 0x00 {IDENTITY_PAST_AN_ADDRESS} \ + PUSH3 0x030d40 CALL RETURNDATASIZE" + )); + let code = [code, return_top(2)].concat(); + for (number, size) in [(WASABI, 64), (IRIS - 1, 64), (IRIS, 0)] { + let ran = run_code(number, &code); + assert!(ran.success, "#{number}"); + assert_eq!( + words(&ran.output), + [U256::from(size), U256::from(1)], + "#{number}" + ); + } +} + +/// The call to the account moves the value it carries to the account +/// (`Program.callToAddress`), and one its caller can't pay is refused: 0 is +/// pushed, and nothing moves. +#[test] +fn a_call_through_a_word_past_a_precompiles_address_moves_its_value_or_is_refused() { + let code = compile(&format!( + "PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 PUSH1 0x07 {IDENTITY_PAST_AN_ADDRESS} \ + PUSH3 0x030d40 CALL \ + PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 PUSH2 0x07d0 {IDENTITY_PAST_AN_ADDRESS} \ + PUSH3 0x030d40 CALL" + )); + let mut chain = Chain::mainnet(IRIS); + chain.put(SENDER, 0, ONE_RBTC, &[]); + chain.put( + CONTRACT, + 1, + U256::from(1_000), + &[code, return_top(2)].concat(), + ); + let ran = chain.run(SENDER, call(0, CONTRACT, U256::ZERO, GAS, &[])); + assert!(ran.success); + assert_eq!(words(&ran.output), [U256::ZERO, U256::from(1)]); + assert_eq!(chain.balance(Address::with_last_byte(0x04)), U256::from(7)); + assert_eq!(chain.balance(CONTRACT), U256::from(993)); +} + +/// Before RSKIP150 (twoToThree) a program 1,024 calls deep calls nothing +/// (`Program.getMaxDepth`): the call to the account is refused there as any +/// other call, 0 pushed. The contract calls itself until it is that deep, +/// then stores what its call through the word pushed, plus one. +#[test] +fn before_rskip150_a_call_through_a_word_past_a_precompiles_address_is_refused_1024_calls_deep() { + let code = compile(&format!( + "PUSH1 0x00 CALLDATALOAD DUP1 PUSH2 0x0400 EQ @deep JUMPI \ + PUSH1 0x01 ADD PUSH1 0x00 MSTORE \ + PUSH1 0x00 PUSH1 0x00 PUSH1 0x20 PUSH1 0x00 PUSH1 0x00 ADDRESS GAS CALL STOP \ + deep: POP \ + PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 {IDENTITY_PAST_AN_ADDRESS} \ + GAS CALL PUSH1 0x01 ADD PUSH1 0x00 SSTORE STOP" + )); + let mut chain = Chain::mainnet(WASABI); + chain.put(SENDER, 0, ONE_RBTC, &[]); + chain.put(CONTRACT, 1, U256::ZERO, &code); + let ran = chain.run(SENDER, call(0, CONTRACT, U256::ZERO, GAS, &[])); + assert!(ran.success); + assert_eq!(chain.storage(CONTRACT, 0), U256::from(1)); +} + +/// Before RSKIP169 rskj's EXTCODEHASH reads a contract's code key with +/// `getCodeHashNonStandard`, which answers 0 where the key holds nothing. The +/// block's repository keeps what its transactions write in a cache until the +/// block ends (`RepositoryLocator.startTrackingAt`, `MutableTrieCache`), so +/// the key of a contract made in the block with no code holds the empty code +/// its creation saved, whose hash is keccak256("") +/// (`MutableTrieCache.getValueHash`). But a creation before RSKIP453 whose +/// code is past the limit keeps its contract and saves no code +/// (`Program.finalizeContractCreation`), and its key holds nothing: 0. From +/// RSKIP169 both answer keccak256(""). +#[test] +fn before_rskip169_extcodehash_of_a_contract_made_in_the_block_reads_the_code_it_saved() { + let code = compile( + "PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 CREATE EXTCODEHASH \ + PUSH6 0x6160016000f3 PUSH1 0x00 MSTORE \ + PUSH1 0x06 PUSH1 0x1a PUSH1 0x00 CREATE EXTCODEHASH", + ); + let code = [code, return_top(2)].concat(); + let empty = U256::from_be_slice(keccak256([]).as_slice()); + for (number, past_the_limit) in [(PAPYRUS, U256::ZERO), (IRIS - 1, U256::ZERO), (IRIS, empty)] { + let ran = run_code(number, &code); + assert!(ran.success, "#{number}"); + assert_eq!(words(&ran.output), [past_the_limit, empty], "#{number}"); + } +} + +/// A creation transaction saves its code too, empty or not +/// (`TransactionExecutor.createContract`): before RSKIP169 a later +/// transaction of its block reads the empty value the cache holds, +/// keccak256(""), and one of the block after reads the trie, whose key holds +/// nothing: 0. +#[test] +fn before_rskip169_extcodehash_of_a_contract_a_transaction_made_without_code_reads_its_block() { + let probe = [ + compile("PUSH1 0x00 CALLDATALOAD EXTCODEHASH"), + return_top(1), + ] + .concat(); + let made = SENDER.create(0).into_word(); + let mut chain = Chain::mainnet(PAPYRUS); + chain.put(SENDER, 0, ONE_RBTC, &[]); + chain.put(CONTRACT, 1, U256::ZERO, &probe); + let ran = chain.block(&[ + (create(0, GAS, &[0x00]), SENDER), + (call(1, CONTRACT, U256::ZERO, GAS, made.as_slice()), SENDER), + ]); + assert!(ran.tx_results.iter().all(|tx| tx.success)); + let empty = U256::from_be_slice(keccak256([]).as_slice()); + assert_eq!(words(&ran.tx_results[1].output), [empty], "in its block"); + let later = chain.run(SENDER, call(2, CONTRACT, U256::ZERO, GAS, made.as_slice())); + assert_eq!(words(&later.output), [U256::ZERO], "in the block after"); +} + +/// While its constructor runs a contract has rskj's marker and no code saved: +/// before RSKIP169 EXTCODEHASH of it answers 0, from RSKIP169 keccak256(""). +/// So it does where a creation at the same address saved empty code earlier +/// in the transaction, in a call that then failed and took it back. The +/// contract calls itself to make that creation and revert, then makes the +/// second, whose constructor stores EXTCODEHASH of its own address, plus one. +#[test] +fn before_rskip169_extcodehash_of_a_contract_whose_constructor_runs_is_0() { + let code = compile( + "CALLDATASIZE @inner JUMPI \ + PUSH1 0x00 PUSH1 0x00 PUSH1 0x01 PUSH1 0x00 PUSH1 0x00 ADDRESS GAS CALL POP \ + PUSH9 0x303f60010160005500 PUSH1 0x00 MSTORE \ + PUSH1 0x09 PUSH1 0x17 PUSH1 0x00 CREATE STOP \ + inner: PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 CREATE POP PUSH1 0x00 PUSH1 0x00 REVERT", + ); + let made = CONTRACT.create(1); + let empty = U256::from_be_slice(keccak256([]).as_slice()); + for (number, while_it_runs) in [(PAPYRUS, U256::ZERO), (IRIS, empty)] { + let mut chain = Chain::mainnet(number); + chain.put(SENDER, 0, ONE_RBTC, &[]); + chain.put(CONTRACT, 1, U256::ZERO, &code); + let ran = chain.run(SENDER, call(0, CONTRACT, U256::ZERO, GAS, &[])); + assert!(ran.success, "#{number}"); + let stored = chain.storage(made, 0); + assert_eq!(stored, while_it_runs + U256::from(1), "#{number}"); + } +} + +/// Before RSKIP174 (iris300) a creation transaction writes a new account over +/// one already at its address (`TransactionExecutor.create`: +/// `createAccount(addr, false)`), and the balance the address held is lost: +/// the contract holds the 7 wei it is sent. But it writes it in the +/// transaction's `cacheTrack`, which a creation that fails rolls back +/// (`TransactionExecutor.go`), so there the balance stays, as it does at +/// every height from RSKIP174. The failed creations here revert, and throw; +/// the next transaction of the block reads the balance with BALANCE. +#[test] +fn before_rskip174_a_failed_deployment_leaves_the_balance_already_at_the_address() { + let made = SENDER.create(0); + let probe = [ + compile(&format!("{} BALANCE", push_address(made))), + return_top(1), + ] + .concat(); + let cases = [ + ("STOP", IRIS - 1, 7), + ("STOP", IRIS, 107), + ("PUSH1 0x00 PUSH1 0x00 REVERT", IRIS - 1, 100), + ("PUSH1 0x00 PUSH1 0x00 REVERT", IRIS, 100), + ("INVALID", IRIS - 1, 100), + ("INVALID", IRIS, 100), + ]; + for (init, number, left) in cases { + let mut chain = Chain::mainnet(number); + chain.put(SENDER, 0, ONE_RBTC, &[]); + chain.put(CONTRACT, 1, U256::ZERO, &probe); + chain.put(made, 0, U256::from(100), &[]); + let deploy = Transaction { + value: U256::from(7), + ..create(0, GAS, &compile(init)) + }; + let read = call(1, CONTRACT, U256::ZERO, GAS, &[]); + let ran = chain.block(&[(deploy, SENDER), (read, SENDER)]); + let case = format!("{init} #{number}"); + assert_eq!(ran.tx_results[0].success, init == "STOP", "{case}"); + let read = words(&ran.tx_results[1].output); + assert_eq!(read, [U256::from(left)], "{case}: what the block reads"); + assert_eq!( + chain.balance(made), + U256::from(left), + "{case}: what it leaves" + ); + } +} + +/// Code is the bytes it was saved as, and runs as they are. rskj has no +/// EIP-7702: code that starts 0xEF01 names no other account's code to run, +/// and a call to it fails at its first byte, which is no opcode. Code like +/// that could be deployed until RSKIP544 (vetiver900). One of the two here +/// has the shape of a designation, 0xEF0100 and an address, and names an +/// account whose code returns a word; the other is two bytes long. The +/// call to each pushes 0 and leaves no return data. +#[test] +fn code_that_starts_0xef01_runs_as_its_bytes() { + let odd = Address::repeat_byte(0xef); + let probe = compile(&format!( + "PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 {} PUSH3 0x030d40 CALL \ + RETURNDATASIZE", + push_address(odd) + )); + let designation = [&[0xef, 0x01, 0x00][..], CALLEE.as_slice()].concat(); + for number in [IRIS, LOVELL] { + for code in [designation.clone(), vec![0xef, 0x01]] { + let mut chain = Chain::mainnet(number); + chain.put(SENDER, 0, ONE_RBTC, &[]); + let word = [compile("PUSH1 0x01"), return_top(1)].concat(); + chain.put(CALLEE, 1, U256::ZERO, &word); + chain.put(odd, 1, U256::ZERO, &code); + chain.put( + CONTRACT, + 1, + U256::ZERO, + &[probe.clone(), return_top(2)].concat(), + ); + let ran = chain.run(SENDER, call(0, CONTRACT, U256::ZERO, GAS, &[])); + let case = format!("{} #{number}", alloy_primitives::hex::encode(&code)); + assert!(ran.success, "{case}"); + assert_eq!(words(&ran.output), [U256::ZERO, U256::ZERO], "{case}"); + } + } +} + +/// rskj refuses no transaction for code at its sender's address +/// (`TransactionExecutor.init` reads the sender's nonce and balance, and +/// nothing else of its account), where revm's EIP-3607 refuses one. The +/// sender here holds code; its transaction runs, moves its value, and takes +/// the sender's nonce on. +#[test] +fn a_transaction_from_an_address_with_code_runs() { + for number in [IRIS, LOVELL] { + let mut chain = Chain::mainnet(number); + chain.put(SENDER, 0, ONE_RBTC, &compile("STOP")); + let send = call(0, CALLEE, U256::from(5), GAS, &[]); + let ran = chain + .try_block(&[(send, SENDER)]) + .unwrap_or_else(|e| panic!("#{number}: {e:?}")); + assert!(ran.tx_results[0].success, "#{number}"); + assert_eq!(chain.balance(CALLEE), U256::from(5), "#{number}"); + let nonce = chain.account(SENDER).map(|a| a.nonce); + assert_eq!(nonce, Some(U256::from(1)), "#{number}"); + } +} + +/// Before RSKIP150 (twoToThree) a precompile's whole output is written at +/// the call's output offset, whatever size the call gave it, and the memory +/// that takes is charged for nothing (`Program.saveOutAfterExecution`). Here +/// the identity precompile's word lands past the 0 bytes asked for, and the +/// MLOAD that reads it then expands nothing. From RSKIP150 nothing is written +/// past what was asked for, and the MLOAD pays 6 gas for 2 more words. (Not +/// at wasabi100's own block, where rskj makes each precompile's account +/// before the block runs, and the call's new-account charge falls away.) +#[test] +fn before_rskip150_a_precompiles_whole_output_is_written_for_nothing() { + let word = U256::from_be_bytes([0x11; 32]); + let code = compile(&format!( + "PUSH32 {} PUSH1 0x00 MSTORE \ + PUSH1 0x00 PUSH1 0x40 PUSH1 0x20 PUSH1 0x00 PUSH1 0x00 {IDENTITY} PUSH3 0x030d40 \ + CALL POP MSIZE PUSH1 0x40 MLOAD", + hex_word(word) + )); + let run = |number| { + let mut chain = Chain::mainnet(number); + chain.put(SENDER, 0, ONE_RBTC, &[]); + chain.put( + CONTRACT, + 1, + U256::ZERO, + &[code.clone(), return_top(2)].concat(), + ); + let ran = chain.run(SENDER, call(0, CONTRACT, U256::ZERO, GAS, &[])); + assert!(ran.success, "#{number}"); + (words(&ran.output), ran.gas_used) + }; + let (before, before_gas) = run(TWO_TO_THREE - 1); + let (from, from_gas) = run(TWO_TO_THREE); + assert_eq!( + before, + [word, U256::from(96)], + "before RSKIP150: the word, and 3 words" + ); + assert_eq!( + from, + [U256::ZERO, U256::from(32)], + "from RSKIP150: nothing, and 1 word" + ); + assert_eq!(from_gas - before_gas, 6); +} diff --git a/crates/execution/src/rskj_tests/vm/ext_code_hash_test.rs b/crates/execution/src/rskj_tests/vm/ext_code_hash_test.rs new file mode 100644 index 0000000..880160e --- /dev/null +++ b/crates/execution/src/rskj_tests/vm/ext_code_hash_test.rs @@ -0,0 +1,98 @@ +//! rskj's `co.rsk.vm.ExtCodeHashTest`, its test of RSKIP140 (papyrus200): +//! EXTCODEHASH is an opcode from it, and an invalid one before it. It costs +//! 400 gas, and pushes the hash of the code at an address: of nothing for +//! an account without code and for a precompile, and 0 where no account is. + +use alloy_primitives::{address, keccak256, Address, B256, U256}; + +use super::harness::{ + call, compile, push_address, return_top, run_code, Chain, CONTRACT, GAS, ONE_RBTC, + RETURN_ONE_WORD_GAS, SENDER, +}; + +/// papyrus200 on mainnet: RSKIP140. +const PAPYRUS: u64 = 2_392_700; + +/// Before RSKIP140 EXTCODEHASH is an invalid opcode: the contract that runs +/// it fails with all its gas. From RSKIP140 it pushes the hash of the code +/// at the address, here the contract's own. +#[test] +fn extcodehash_is_an_invalid_opcode_before_rskip140() { + let mut code = compile("ADDRESS EXTCODEHASH"); + code.extend(return_top(1)); + let before = run_code(PAPYRUS - 1, &code); + let test = "testDoEXTCODEHASHWithHardForkDeactivated"; + assert!(!before.success, "{test}"); + assert_eq!(before.gas_used, GAS, "{test}"); + let from = run_code(PAPYRUS, &code); + assert!(from.success, "{test}"); + assert_eq!(B256::from_slice(&from.output), keccak256(&code), "{test}"); +} + +/// What `executeExtCodeHash` runs from RSKIP140, PUSH20 of `address` and +/// EXTCODEHASH, with the word it pushes returned, where `accounts` hold +/// what they're given and no code: the word, and the gas the two took. +fn extcodehash_of(address: Address, accounts: &[(Address, U256)]) -> (B256, u64) { + let mut chain = Chain::mainnet(PAPYRUS); + chain.put(SENDER, 0, ONE_RBTC, &[]); + for (account, balance) in accounts { + chain.put(*account, 0, *balance, &[]); + } + let code = compile(&format!("{} EXTCODEHASH", push_address(address))); + chain.put(CONTRACT, 1, U256::ZERO, &[code, return_top(1)].concat()); + let ran = chain.run(SENDER, call(0, CONTRACT, U256::ZERO, GAS, &[])); + assert!(ran.success); + let gas = ran.gas_used - 21_000 - RETURN_ONE_WORD_GAS; + (B256::from_slice(&ran.output), gas) +} + +#[test] +fn extcodehash_pushes_the_hash_of_a_contracts_code_for_403_gas() { + let test = "testDoEXTCODEHASHToContractAndGetTheCodeHash"; + let callee = address!("471fd3ad3e9eeadeec4608b92d16ce6b500704cc"); + let mut chain = Chain::mainnet(PAPYRUS); + chain.put(SENDER, 0, ONE_RBTC, &[]); + chain.put(callee, 1, U256::ZERO, &[0x60, 0x0b]); + let code = compile(&format!("{} EXTCODEHASH", push_address(callee))); + chain.put(CONTRACT, 1, U256::ZERO, &[code, return_top(1)].concat()); + let ran = chain.run(SENDER, call(0, CONTRACT, U256::ZERO, GAS, &[])); + assert!(ran.success, "{test}"); + assert_eq!( + B256::from_slice(&ran.output), + keccak256([0x60, 0x0b]), + "{test}" + ); + assert_eq!(ran.gas_used - 21_000 - RETURN_ONE_WORD_GAS, 403, "{test}"); +} + +#[test] +fn extcodehash_of_an_account_without_code_is_the_hash_of_nothing() { + let test = "testDoEXTCODEHASHToAccountAndGetEmptyHash"; + let account = address!("cd2a3d9f938e13cd947ec05abc7fe734df8dd826"); + let pushed = extcodehash_of(account, &[(account, U256::from(1))]); + assert_eq!(pushed, (keccak256([]), 403), "{test}"); +} + +#[test] +fn extcodehash_of_no_account_is_0() { + let test = "testDoEXTCODEHASHToNonExistentAccountAndGetZero"; + let pushed = extcodehash_of(address!("1111111111222222222233333333334444444444"), &[]); + assert_eq!(pushed, (B256::ZERO, 403), "{test}"); +} + +#[test] +fn extcodehash_of_a_precompile_is_the_hash_of_nothing() { + let test = "testDoEXTCODEHASHToPrecompiledContractAndGetEmptyHash"; + let bridge = address!("0000000000000000000000000000000001000006"); + assert_eq!(extcodehash_of(bridge, &[]), (keccak256([]), 403), "{test}"); +} + +/// With nothing on the stack EXTCODEHASH fails the program, which spends all +/// its gas. +#[test] +fn extcodehash_of_an_empty_stack_fails_with_all_the_gas() { + let test = "testDoEXTCODEHASHWithoutArguments"; + let ran = run_code(PAPYRUS, &compile("EXTCODEHASH")); + assert!(!ran.success, "{test}"); + assert_eq!(ran.gas_used, GAS, "{test}"); +} diff --git a/crates/execution/src/rskj_tests/vm/harness.rs b/crates/execution/src/rskj_tests/vm/harness.rs new file mode 100644 index 0000000..0508bac --- /dev/null +++ b/crates/execution/src/rskj_tests/vm/harness.rs @@ -0,0 +1,389 @@ +//! What the ported tests share: programs written as rskj's VM tests write +//! them, and a chain of blocks for them to run in. The tests of the +//! precompiled contracts and of REMASC use it too. +//! +//! rskj's VM tests write a program as opcode names, compiled by their +//! `BytecodeCompiler`, and step it on a mocked program or run it in blocks +//! of their DSL. Here a program is the code of a contract a transaction +//! calls, or the init code a transaction deploys, in a block rustock's +//! executor runs: the path every block takes. + +use std::sync::Arc; + +use alloy_primitives::{hex, Address, Bloom, Bytes, B256, U256}; +use rustock_core::{Header, Transaction}; +use rustock_storage::BlockStore; +use rustock_trie::{AccountState, MemoryTrieStore, TrieKeySlice, TrieNode}; + +use crate::executor::{ + BlockExecutionResult, ExecutionError, MiningDrops, RskExecutor, TxExecutionResult, +}; +use crate::hardfork::RskHardforkConfig; + +/// The account that sends the transactions of a test. +pub(crate) const SENDER: Address = Address::repeat_byte(0xa1); +/// The contract a test's program runs as. +pub(crate) const CONTRACT: Address = Address::repeat_byte(0xc0); +/// One RBTC, in wei. +pub(crate) const ONE_RBTC: U256 = U256::from_limbs([1_000_000_000_000_000_000, 0, 0, 0]); +/// The gas a test's transaction is given, unless it says otherwise. +pub(crate) const GAS: u64 = 1_000_000; +/// What [`return_top`] of one word costs: two PUSH2s, an MSTORE and the +/// word of memory it takes, and a PUSH1. RETURN costs nothing. +pub(crate) const RETURN_ONE_WORD_GAS: u64 = 15; +/// Code that logs the top word of the stack, and takes it off: an MSTORE of +/// it at 0, and a LOG0 of those 32 bytes. [`logged`] reads it back. +pub(crate) const LOG_TOP: &str = "PUSH1 0x00 MSTORE PUSH1 0x20 PUSH1 0x00 LOG0"; + +/// Compiles `code` as rskj's `BytecodeCompiler` does: each word is an +/// opcode's name, in any case, or `0x` and bytes in hex, which go in as +/// they are. rskj's names are read as rskj reads them: DUPN, SWAPN and +/// TXINDEX are its own opcodes (0xa8 to 0xaa), not EIP-8024's. +/// +/// Two more words let a program branch: `name:` is a JUMPDEST, and `@name` +/// pushes where that JUMPDEST is, with a PUSH2. +pub(crate) fn compile(code: &str) -> Vec { + let tokens: Vec = code + .split_whitespace() + .map(str::to_ascii_uppercase) + .collect(); + let mut labels = std::collections::HashMap::new(); + let mut at = 0; + for token in &tokens { + if let Some(label) = token.strip_suffix(':') { + labels.insert(label.to_string(), at); + } + at += if token.starts_with('@') { + 3 + } else if let Some(hex) = token.strip_prefix("0X") { + hex.len() / 2 + } else { + 1 + }; + } + let mut bytes = Vec::new(); + for token in &tokens { + if token.ends_with(':') { + bytes.push(0x5b); // JUMPDEST + } else if let Some(label) = token.strip_prefix('@') { + let Some(at) = labels.get(label) else { + panic!("no label {label}"); + }; + let at = u16::try_from(*at).expect("a label within 64 KB"); + bytes.push(0x61); // PUSH2 + bytes.extend(at.to_be_bytes()); + } else if let Some(hex) = token.strip_prefix("0X") { + bytes.extend(hex::decode(hex).expect("hex bytes")); + } else { + bytes.push(opcode(token).unwrap_or_else(|| panic!("no opcode {token}"))); + } + } + bytes +} + +fn opcode(name: &str) -> Option { + match name { + "DUPN" => Some(0xa8), + "SWAPN" => Some(0xa9), + "TXINDEX" => Some(0xaa), + "SHA3" => Some(0x20), + "SUICIDE" => Some(0xff), + _ => (0..=255u8) + .find(|byte| revm::bytecode::OpCode::new(*byte).is_some_and(|op| op.as_str() == name)), + } +} + +/// Code that ends a program by returning the top `words` words of its +/// stack, the top one first. +pub(crate) fn return_top(words: usize) -> Vec { + let mut code = Vec::new(); + for word in 0..words { + code.push(0x61); // PUSH2 + code.extend(((word * 32) as u16).to_be_bytes()); + code.push(0x52); // MSTORE + } + code.push(0x61); // PUSH2 + code.extend(((words * 32) as u16).to_be_bytes()); + code.extend([0x60, 0x00, 0xf3]); // PUSH1 0, RETURN + code +} + +/// The words of a program's output. +pub(crate) fn words(output: &[u8]) -> Vec { + output.chunks(32).map(U256::from_be_slice).collect() +} + +/// The word each log of a transaction holds, in order: what [`LOG_TOP`] +/// logged. +pub(crate) fn logged(result: &TxExecutionResult) -> Vec { + let data = result.logs.iter().map(|log| &log.data.data[..]); + data.map(U256::from_be_slice).collect() +} + +/// `value` as a word in hex, for a PUSH32: `0x` and 64 digits. +pub(crate) fn hex_word(value: U256) -> String { + format!("0x{}", hex::encode(value.to_be_bytes::<32>())) +} + +/// PUSH20 of `address`, for [`compile`]. +pub(crate) fn push_address(address: Address) -> String { + format!("PUSH20 0x{}", hex::encode(address)) +} + +/// A transaction calling `to` with `data`, at a gas price of 0. +pub(crate) fn call(nonce: u64, to: Address, value: U256, gas: u64, data: &[u8]) -> Transaction { + Transaction { + nonce, + gas_price: U256::ZERO, + gas_limit: U256::from(gas), + to: Bytes::copy_from_slice(to.as_slice()), + value, + input: Bytes::copy_from_slice(data), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + } +} + +/// A transaction creating a contract of the init code `data`, at a gas +/// price of 0. +pub(crate) fn create(nonce: u64, gas: u64, data: &[u8]) -> Transaction { + Transaction { + to: Bytes::new(), + ..call(nonce, Address::ZERO, U256::ZERO, gas, data) + } +} + +/// A chain on one network's rules: its state, and the number of the next +/// block, which each block run moves on by one. +pub(crate) struct Chain { + executor: RskExecutor, + store: Arc, + root: TrieNode, + /// The next block's number. + pub(crate) number: u64, + blocks: Arc, + _blocks: tempfile::TempDir, +} + +impl Chain { + pub(crate) fn new(config: RskHardforkConfig, number: u64) -> Self { + let folder = tempfile::tempdir().expect("a folder for the block store"); + let blocks = Arc::new(BlockStore::open(folder.path()).expect("a block store")); + Self { + executor: RskExecutor::new(config, blocks.clone()), + store: Arc::new(MemoryTrieStore::new()), + root: TrieNode::empty(), + number, + blocks, + _blocks: folder, + } + } + + /// The chain, its executor changed by `change`: given REMASC's constants + /// or the Bridge's, say. + pub(crate) fn map_executor(self, change: impl FnOnce(RskExecutor) -> RskExecutor) -> Self { + Self { + executor: change(self.executor), + ..self + } + } + + /// A chain on mainnet's rules whose next block is `number`. + pub(crate) fn mainnet(number: u64) -> Self { + Self::new(RskHardforkConfig::mainnet(), number) + } + + /// Writes an account with `nonce` and `balance`, and `code` where it + /// has any. + pub(crate) fn put(&mut self, address: Address, nonce: u64, balance: U256, code: &[u8]) { + let store = self.store.as_ref(); + let account = AccountState::new(U256::from(nonce), balance).encode(); + let key = TrieKeySlice::from_key(&rustock_trie::account_key(&address)); + self.root = self.root.put(&key, &account, store); + if !code.is_empty() { + let key = TrieKeySlice::from_key(&rustock_trie::code_key(&address)); + self.root = self.root.put(&key, code, store); + } + } + + /// The next block's header. + pub(crate) fn header(&self) -> Header { + Header { + parent_hash: B256::ZERO, + ommers_hash: B256::ZERO, + beneficiary: Address::repeat_byte(0x01), + state_root: B256::ZERO, + transactions_root: B256::ZERO, + receipts_root: B256::ZERO, + logs_bloom: Bloom::ZERO, + extension_data: None, + difficulty: U256::from(1_000_000), + number: self.number, + gas_limit: U256::from(6_800_000), + gas_used: 0, + timestamp: 1_700_000_000, + extra_data: Bytes::new(), + paid_fees: U256::ZERO, + minimum_gas_price: U256::ZERO, + uncle_count: 0, + umm_root: rustock_core::config::ActivationHeights::current() + .umm_active(self.number) + .then(Bytes::new), + 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, + kept: None, + } + } + + /// Runs `transactions` in the next block, or says what refuses the block. + /// A block that runs leaves its state for the next. + pub(crate) fn try_block( + &mut self, + transactions: &[(Transaction, Address)], + ) -> Result { + let result = self.executor.execute_block( + &self.header(), + transactions, + &self.root, + self.store.clone(), + )?; + self.apply(&result); + self.number += 1; + Ok(result) + } + + /// Runs `transactions` in the block `header` heads, whose uncles are + /// `ommers`, and keeps the block as the chain's at its height, as an + /// imported block is kept: REMASC reads the blocks before it there. + pub(crate) fn import( + &mut self, + header: &Header, + ommers: &[Header], + transactions: &[(Transaction, Address)], + ) -> BlockExecutionResult { + let store = self.store.clone(); + let result = self + .executor + .execute_block(header, transactions, &self.root, store); + let result = result.expect("the block runs"); + self.apply(&result); + let hash = header.hash(); + let blocks = &self.blocks; + blocks + .put_header_with_hash(hash, header) + .expect("the header is kept"); + blocks + .put_canonical_hash(header.number, hash) + .expect("the height is kept"); + blocks + .put_body(hash, &[], ommers) + .expect("the uncles are kept"); + self.number = header.number + 1; + result + } + + /// Leaves the state a block that ran left. + fn apply(&mut self, result: &BlockExecutionResult) { + self.root = match &result.applied_root { + Some(root) => root.clone(), + None => crate::state::apply_state_changes( + &self.root, + self.store.as_ref(), + &result.state_changes, + &result.markers, + ), + }; + } + + /// Runs `transactions` in the next block. + pub(crate) fn block( + &mut self, + transactions: &[(Transaction, Address)], + ) -> BlockExecutionResult { + self.try_block(transactions).expect("the block runs") + } + + /// Runs `transactions` as the miner builds the next block from them, + /// leaving the chain as it was: what ran, and what the miner left out. + pub(crate) fn mine( + &self, + transactions: &[(Transaction, Address)], + ) -> Result<(BlockExecutionResult, MiningDrops), ExecutionError> { + let (header, store) = (self.header(), self.store.clone()); + self.executor + .execute_block_for_mining(&header, transactions, &self.root, store) + } + + /// Runs `transaction` from `sender`, alone in the next block. + pub(crate) fn run(&mut self, sender: Address, transaction: Transaction) -> TxExecutionResult { + self.block(&[(transaction, sender)]).tx_results.remove(0) + } + + /// The account at `address`, where there is one. + pub(crate) fn account(&self, address: Address) -> Option { + let key = TrieKeySlice::from_key(&rustock_trie::account_key(&address)); + let encoded = self.root.get(&key, self.store.as_ref())?; + Some(AccountState::decode(&encoded).expect("an account")) + } + + /// The balance at `address`: zero where there is no account. + pub(crate) fn balance(&self, address: Address) -> U256 { + self.account(address) + .map_or(U256::ZERO, |account| account.balance) + } + + /// The code at `address`: none where there is none. + pub(crate) fn code(&self, address: Address) -> Vec { + let key = rustock_trie::code_key(&address); + let code = self + .root + .get(&TrieKeySlice::from_key(&key), self.store.as_ref()); + code.map(|code| code.to_vec()).unwrap_or_default() + } + + /// The word at `slot` of `address`'s storage: zero where none is. + pub(crate) fn storage(&self, address: Address, slot: u64) -> U256 { + self.storage_word(address, U256::from(slot)) + } + + /// The word at `key` of `address`'s storage: zero where none is. + pub(crate) fn storage_word(&self, address: Address, key: U256) -> U256 { + let key = rustock_trie::storage_key(&address, &B256::from(key)); + let value = self + .root + .get(&TrieKeySlice::from_key(&key), self.store.as_ref()); + value.map_or(U256::ZERO, |value| U256::from_be_slice(&value)) + } + + /// Writes `bytes` at `key` of `address`'s storage, as rskj's + /// `addStorageBytes` writes a value of any length. + pub(crate) fn put_storage_bytes(&mut self, address: Address, key: U256, bytes: &[u8]) { + let key = rustock_trie::storage_key(&address, &B256::from(key)); + let store = self.store.as_ref(); + self.root = self.root.put(&TrieKeySlice::from_key(&key), bytes, store); + } +} + +/// What a transaction of [`GAS`] gets from calling a contract whose code is +/// `code`, alone in block `number` of a chain on `config`'s rules. +pub(crate) fn run_code_on( + config: RskHardforkConfig, + number: u64, + code: &[u8], +) -> TxExecutionResult { + let mut chain = Chain::new(config, number); + chain.put(SENDER, 0, ONE_RBTC, &[]); + chain.put(CONTRACT, 1, U256::ZERO, code); + chain.run(SENDER, call(0, CONTRACT, U256::ZERO, GAS, &[])) +} + +/// [`run_code_on`] mainnet's rules. +pub(crate) fn run_code(number: u64, code: &[u8]) -> TxExecutionResult { + run_code_on(RskHardforkConfig::mainnet(), number, code) +} diff --git a/crates/execution/src/rskj_tests/vm/initcode_cost_calculator_test.rs b/crates/execution/src/rskj_tests/vm/initcode_cost_calculator_test.rs new file mode 100644 index 0000000..1150636 --- /dev/null +++ b/crates/execution/src/rskj_tests/vm/initcode_cost_calculator_test.rs @@ -0,0 +1,64 @@ +//! rskj's `org.ethereum.cost.InitcodeCostCalculatorTest`: RSKIP438's price +//! of init code, 2 gas for each 32-byte word or part of one +//! (`InitcodeCostCalculator.calculateCost`), seen in the gas a transaction +//! that creates a contract must carry from lovell700 on. + +use alloy_primitives::hex; + +use super::harness::{create, Chain, ONE_RBTC, SENDER}; + +/// lovell700 on mainnet: RSKIP438. +const LOVELL: u64 = 7_338_024; + +/// A creation's gas before RSKIP438: 53,000, 4 for each zero byte of its +/// init code, and 16 for each other (RSKIP400). +fn intrinsic(data: &[u8]) -> u64 { + let bytes = data.iter().map(|byte| if *byte == 0 { 4 } else { 16 }); + 53_000 + bytes.sum::() +} + +/// Whether the miner takes the creation of `data`, carrying `gas`, into +/// block `number` of mainnet. +fn taken(number: u64, data: &[u8], gas: u64) -> bool { + let mut chain = Chain::mainnet(number); + chain.put(SENDER, 0, ONE_RBTC, &[]); + let mined = chain.mine(&[(create(0, gas, data), SENDER)]); + let (_, drops) = mined.expect("the miner builds the block"); + drops.invalid.is_empty() +} + +/// rskj's three init codes, of 4, 40 and 71 bytes, cost 2, 4 and 6 gas from +/// RSKIP438: a creation of each needs that much more than before it. +#[test] +fn from_rskip438_a_creation_carries_2_gas_for_each_word_of_init_code() { + let cases = [ + (2, "00010203".to_string()), + ( + 4, + [ + "fd5fa123fd5fa123a31231000076890afd5fa123a31231000076890a", + "fd5fa123a31231000076890a", + ] + .concat(), + ), + ( + 6, + [ + "fd5fa123a31231000076890abcfff41239123912323123fd5fa123afd5fa12fd5fa123a3", + "1231000076890afd5fa123a31231000076890a3a31231000076890a31231000076890a", + ] + .concat(), + ), + ]; + let test = "testTransactionInitCodeCostCalculation"; + for (cost, data) in cases { + let data = hex::decode(data).expect("hex"); + let gas = intrinsic(&data); + assert!(taken(LOVELL - 1, &data, gas), "{test}: {cost}, before"); + assert!( + !taken(LOVELL, &data, gas + cost - 1), + "{test}: {cost}, a gas short" + ); + assert!(taken(LOVELL, &data, gas + cost), "{test}: {cost}"); + } +} diff --git a/crates/execution/src/rskj_tests/vm/mcopy_test.rs b/crates/execution/src/rskj_tests/vm/mcopy_test.rs new file mode 100644 index 0000000..d1a9128 --- /dev/null +++ b/crates/execution/src/rskj_tests/vm/mcopy_test.rs @@ -0,0 +1,98 @@ +//! rskj's `co.rsk.vm.opcode.MCopyGasTest` and `MCopyInputTest`, their tests +//! of RSKIP445 (lovell700): MCOPY costs 3 gas, 3 a word it copies and the +//! memory it expands to the greater of its two ranges, and a copy no gas can +//! pay for fails the program with all its gas. + +use alloy_primitives::U256; + +use super::harness::{compile, hex_word, run_code, GAS}; + +/// lovell700 on mainnet: RSKIP445. +const LOVELL: u64 = 7_338_024; + +/// The tests' words of memory: nothing, all ones, nine bytes, and eight +/// words that count from 0x00 to 0xff. +const ZERO: &str = "0000000000000000000000000000000000000000000000000000000000000000"; +const ONES: &str = "0101010101010101010101010101010101010101010101010101010101010101"; +const NINE: &str = "0001020304050607080000000000000000000000000000000000000000000000"; +const EIGHT_WORDS: [&str; 8] = [ + "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", + "202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f", + "404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f", + "606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f", + "808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f", + "a0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf", + "c0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedf", + "e0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", +]; + +/// The program a case runs: `memory` stored from 0, a word at a time, the +/// three words MCOPY takes, and MCOPY where it `copies`. +fn program(memory: &[&str], dst: U256, src: U256, length: U256, copies: bool) -> Vec { + let mut code = String::new(); + for (i, word) in memory.iter().enumerate() { + code += &format!("PUSH32 0x{word} PUSH2 0x{:04x} MSTORE ", i * 32); + } + let [length, src, dst] = [length, src, dst].map(hex_word); + code += &format!("PUSH32 {length} PUSH32 {src} PUSH32 {dst}"); + if copies { + code += " MCOPY"; + } + compile(&code) +} + +/// The gas MCOPY alone takes: the program's with it, less the same +/// program's without it. +#[test] +fn mcopy_costs_3_gas_3_a_word_and_the_memory_it_expands() { + let test = "testMCopy_ShouldConsumeTheCorrectAmountOfGas"; + let cases: [(&[&str], u64, u64, u64, u64); 6] = [ + (&[ZERO, EIGHT_WORDS[0]], 0, 32, 32, 6), + (&[ONES], 0, 0, 32, 6), + (&[NINE], 0, 1, 8, 6), + (&[NINE], 1, 0, 8, 6), + (&EIGHT_WORDS, 256, 256, 1, 9), + (&EIGHT_WORDS, 0, 256, 256, 51), + ]; + for (memory, dst, src, length, gas) in cases { + let case = format!("{test}: {dst} {src} {length}"); + let [dst, src, length] = [dst, src, length].map(U256::from); + let with = run_code(LOVELL, &program(memory, dst, src, length, true)); + let without = run_code(LOVELL, &program(memory, dst, src, length, false)); + assert!(with.success && without.success, "{case}"); + assert_eq!(with.gas_used - without.gas_used, gas, "{case}"); + } +} + +/// `MCopyGasTest.testMCopy_ShouldThrowOutOfGasException` and +/// `MCopyInputTest.testMCopy_ShouldThrowOOGException`: an offset or a length +/// past what any gas pays for. The latter's words are `DataWord.valueOf` of a +/// long, its 8 bytes, a negative one's as two's complement. +#[test] +fn an_mcopy_no_gas_can_pay_for_fails_with_all_the_gas() { + let (zero, one) = (U256::ZERO, U256::from(1)); + let mut cases: Vec<(&[&str], U256, U256, U256)> = Vec::new(); + for memory in [&[][..], &EIGHT_WORDS[..]] { + for big in [U256::MAX, U256::MAX - one, U256::MAX >> 1] { + cases.push((memory, big, zero, one)); + cases.push((memory, zero, big, one)); + cases.push((memory, zero, zero, big)); + } + } + let long = |n: i64| U256::from(n as u64); + let max_memory = 1i64 << 30; + cases.extend([ + (&[ZERO][..], zero, zero, long(-1)), + (&[][..], zero, zero, long(-(2 * (i64::MAX / 3)))), + (&[][..], zero, zero, long(i64::from(i32::MAX) + 1)), + (&[][..], long(max_memory), zero, one), + (&[][..], zero, long(max_memory), one), + (&[][..], zero, zero, long(max_memory + 1)), + (&[][..], zero, zero, long(-1)), + ]); + for (memory, dst, src, length) in cases { + let ran = run_code(LOVELL, &program(memory, dst, src, length, true)); + assert!(!ran.success, "{dst} {src} {length}"); + assert_eq!(ran.gas_used, GAS, "{dst} {src} {length}"); + } +} diff --git a/crates/execution/src/rskj_tests/vm/mod.rs b/crates/execution/src/rskj_tests/vm/mod.rs new file mode 100644 index 0000000..e626596 --- /dev/null +++ b/crates/execution/src/rskj_tests/vm/mod.rs @@ -0,0 +1,19 @@ +//! The VM: opcodes, programs, and a transaction's execution (rskj's `co.rsk.vm` +//! and `org.ethereum.vm`). + +mod contract_creating_dsl_rollback_test; +mod create2_test; +mod dsl_files_test; +mod evm_rules; +mod ext_code_hash_test; +pub(super) mod harness; +mod initcode_cost_calculator_test; +mod mcopy_test; +mod nested_calls_stack_depth_test; +mod rskip544_test; +mod transaction_executor_init_code_size_dsl_test; +mod transient_storage_dsl_test; +mod transient_storage_test; +mod vm_dsl_test; +mod vm_execution_test; +mod vm_test; diff --git a/crates/execution/src/rskj_tests/vm/nested_calls_stack_depth_test.rs b/crates/execution/src/rskj_tests/vm/nested_calls_stack_depth_test.rs new file mode 100644 index 0000000..660bae9 --- /dev/null +++ b/crates/execution/src/rskj_tests/vm/nested_calls_stack_depth_test.rs @@ -0,0 +1,87 @@ +//! rskj's `org.ethereum.core.NestedCallsStackDepthTest`: the Environment +//! contract (RSKIP203, arrowhead600) answers a contract's call of its +//! getCallStackDepth with how deep the call is, and before the upgrade no +//! contract is at its address. +//! +//! rskj's test of the second leaves arrowhead600 unscheduled on a regtest +//! whose later upgrades are at block 0, a schedule rustock refuses +//! (`RskHardforkConfig::with_rskj_overrides`). Here both run on mainnet, in +//! the upgrade's first block and the block before. + +use alloy_primitives::{hex, keccak256, Address, U256}; + +use super::harness::{ + call, compile, push_address, return_top, run_code, words, Chain, CONTRACT, GAS, ONE_RBTC, + SENDER, +}; +use crate::precompiles::ENVIRONMENT_ADDR; + +/// arrowhead600 on mainnet: RSKIP203. +const ARROWHEAD: u64 = 6_223_700; + +/// Code that calls the Environment contract's getCallStackDepth with +/// 100,000 gas, for an answer of one word written to memory at 0x20. +fn ask_depth() -> String { + let selector = hex::encode(&keccak256("getCallStackDepth()")[..4]); + format!( + "PUSH4 0x{selector} PUSH1 0x00 MSTORE PUSH1 0x20 PUSH1 0x20 PUSH1 0x04 PUSH1 0x1c \ + PUSH1 0x00 {} PUSH3 0x0186a0 CALL", + push_address(ENVIRONMENT_ADDR) + ) +} + +/// What a contract gets from calling the Environment contract's +/// getCallStackDepth in block `number` of mainnet: the word it was +/// answered, the size of the answer, and whether the call succeeded. +fn call_stack_depth_at(number: u64) -> [U256; 3] { + let mut code = compile(&format!( + "{} RETURNDATASIZE PUSH1 0x20 MLOAD", + ask_depth() + )); + code.extend(return_top(3)); + let ran = run_code(number, &code); + assert!(ran.success, "the contract runs at #{number}"); + let answer = words(&ran.output); + [answer[0], answer[1], answer[2]] +} + +/// Before RSKIP203 no contract is at the Environment contract's address: a +/// contract's call of it succeeds and gets nothing back, where rskj's +/// Solidity caller reverts for want of an answer. From RSKIP203 it answers +/// one word, the depth of the call: 1 from a contract a transaction calls. +#[test] +fn the_environment_contract_answers_a_contracts_call_from_rskip203() { + let (none, one, word) = (U256::ZERO, U256::from(1), U256::from(32)); + let test = "testContractConsensusInactive"; + assert_eq!(call_stack_depth_at(ARROWHEAD - 1), [none, none, one], "{test}"); + let test = "testContractCallsGetCallStackDepth"; + assert_eq!(call_stack_depth_at(ARROWHEAD), [one, word, one], "{test}"); +} + +/// A call three contracts deep is answered 3: the contract a transaction +/// calls calls a second, which calls a third, which asks. +#[test] +fn a_call_three_contracts_deep_is_answered_3() { + let (second, third) = (Address::repeat_byte(0xc2), Address::repeat_byte(0xc3)); + // Calls `next` with `gas`, and returns the word it answers. + let relay = |next: Address, gas: u32| { + compile(&format!( + "PUSH1 0x20 PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 {} PUSH4 0x{gas:08x} CALL \ + POP PUSH1 0x20 PUSH1 0x00 RETURN", + push_address(next) + )) + }; + let asks = compile(&format!( + "{} POP PUSH1 0x20 PUSH1 0x20 RETURN", + ask_depth() + )); + let mut chain = Chain::mainnet(ARROWHEAD); + chain.put(SENDER, 0, ONE_RBTC, &[]); + chain.put(CONTRACT, 1, U256::ZERO, &relay(second, 600_000)); + chain.put(second, 1, U256::ZERO, &relay(third, 300_000)); + chain.put(third, 1, U256::ZERO, &asks); + let ran = chain.run(SENDER, call(0, CONTRACT, U256::ZERO, GAS, &[])); + let test = "testNestedContractCallsGetCallStackDepth"; + assert!(ran.success, "{test}"); + assert_eq!(words(&ran.output), [U256::from(3)], "{test}"); +} diff --git a/crates/execution/src/rskj_tests/vm/rskip544_test.rs b/crates/execution/src/rskj_tests/vm/rskip544_test.rs new file mode 100644 index 0000000..f395779 --- /dev/null +++ b/crates/execution/src/rskj_tests/vm/rskip544_test.rs @@ -0,0 +1,59 @@ +//! rskj's `co.rsk.vm.RSKIP544Test`, its test of RSKIP544's activation +//! (vetiver900, EIP-3541): from it CREATE deploys no code that starts with +//! 0xEF (`Program.hasInvalidCodePrefix`); before it, and for code that +//! starts with another byte, it deploys. + +use alloy_primitives::U256; + +use super::harness::{call, compile, Chain, CONTRACT, GAS, ONE_RBTC, SENDER}; + +/// vetiver900 on mainnet: RSKIP544. +const VETIVER: u64 = 8_804_200; + +/// The code a contract's CREATE leaves at the new address, from rskj's +/// `initcode` of `size` bytes, in block `number` of mainnet. RSK gives a +/// CREATE all the gas left, so the contract stops right after it. +fn deployed(number: u64, initcode: &str, size: u8) -> Vec { + let code = compile(&format!( + "PUSH32 0x{initcode} PUSH1 0x00 MSTORE PUSH1 0x{size:02x} PUSH1 0x00 PUSH1 0x00 CREATE STOP" + )); + let mut chain = Chain::mainnet(number); + chain.put(SENDER, 0, ONE_RBTC, &[]); + chain.put(CONTRACT, 1, U256::ZERO, &code); + let ran = chain.run(SENDER, call(0, CONTRACT, U256::ZERO, GAS, &[])); + assert!(ran.success, "the creating contract runs"); + chain.code(CONTRACT.create(1)) +} + +/// rskj's scenarios: code of 0xEF and of 0xEF00 is deployed before RSKIP544 +/// and not from it; code of 0xFE is deployed on both sides. +#[test] +fn from_rskip544_create_deploys_no_code_that_starts_with_0xef() { + let cases = [ + ( + "60ef60005360016000f300000000000000000000000000000000000000000000", + 0x0a, + vec![0xef], + false, + ), + ( + "61ef006000526002601ef3000000000000000000000000000000000000000000", + 0x0b, + vec![0xef, 0x00], + false, + ), + ( + "60fe60005360016000f300000000000000000000000000000000000000000000", + 0x0a, + vec![0xfe], + true, + ), + ]; + let test = "testActivationBoundary_Parameterized"; + for (initcode, size, code, deployed_from) in cases { + let before = deployed(VETIVER - 1, initcode, size); + assert_eq!(before, code, "{test}: {initcode}"); + let from = deployed(VETIVER, initcode, size); + assert_eq!(from == code, deployed_from, "{test}: {initcode}"); + } +} diff --git a/crates/execution/src/rskj_tests/vm/transaction_executor_init_code_size_dsl_test.rs b/crates/execution/src/rskj_tests/vm/transaction_executor_init_code_size_dsl_test.rs new file mode 100644 index 0000000..065081f --- /dev/null +++ b/crates/execution/src/rskj_tests/vm/transaction_executor_init_code_size_dsl_test.rs @@ -0,0 +1,103 @@ +//! rskj's `org.ethereum.core.TransactionExecutorInitCodeSizeDslTest`: from +//! RSKIP438 (lovell700) a transaction that creates a contract carries 2 gas +//! for each word of its init code on top of its intrinsic gas +//! (`Transaction.transactionCost`), and one of more than 49,152 bytes of it +//! is invalid (`Transaction.isInitCodeSizeInvalidForTx`): the miner leaves +//! it out of its block, and a block that holds it is refused. + +use alloy_primitives::U256; +use rustock_core::Transaction; + +use super::harness::{create, Chain, GAS, ONE_RBTC, SENDER}; +use crate::hardfork::RskHardforkConfig; + +/// lovell700 on mainnet: RSKIP438. +const LOVELL: u64 = 7_338_024; +/// The most init code RSKIP438 lets a transaction carry. +const MAX_INITCODE: usize = 49_152; +/// The gas of a creation of `MAX_INITCODE` zero bytes before RSKIP438: 53,000 +/// and 4 for each byte. Its init code is a STOP, which runs for nothing. +const MAX_CREATION_GAS: u64 = 53_000 + 4 * MAX_INITCODE as u64; + +/// A chain on `config`'s rules, at block `number`, where the sender holds +/// `balance`. +fn chain(config: RskHardforkConfig, number: u64, balance: U256) -> Chain { + let mut chain = Chain::new(config, number); + chain.put(SENDER, 0, balance, &[]); + chain +} + +/// A creation of `size` zero bytes of init code, carrying `gas`. +fn creation(size: usize, gas: u64) -> [(Transaction, alloy_primitives::Address); 1] { + [(create(0, gas, &vec![0; size]), SENDER)] +} + +/// From RSKIP438 a creation pays 2 gas for each word of its init code: +/// 3,072 more for 49,152 bytes. +#[test] +fn from_rskip438_a_creation_pays_2_gas_a_word_of_its_init_code() { + let test = "testInitCodeSizeTransactionCreationValidationSuccessWithRSKIPActivated"; + for (number, gas) in [ + (LOVELL - 1, MAX_CREATION_GAS), + (LOVELL, MAX_CREATION_GAS + 3_072), + ] { + let mut chain = chain(RskHardforkConfig::mainnet(), number, ONE_RBTC); + let ran = chain + .block(&creation(MAX_INITCODE, GAS)) + .tx_results + .remove(0); + assert!(ran.success, "{test} #{number}"); + assert_eq!(ran.gas_used, gas, "{test} #{number}"); + } +} + +/// A creation whose sender cannot pay for the gas it carries, at its price, +/// is invalid: rskj's scenario gives the sender 4,322,017 for 4,323,189 gas +/// at a price of 1, and the miner leaves it out of the block. +#[test] +fn a_creation_whose_sender_cannot_pay_for_its_gas_is_left_out_of_the_block() { + let chain = chain(RskHardforkConfig::mainnet(), LOVELL, U256::from(4_322_017)); + let [(transaction, sender)] = creation(1, 4_323_189); + let priced = Transaction { + gas_price: U256::from(1), + ..transaction + }; + let mined = chain.mine(&[(priced, sender)]); + let (result, drops) = mined.expect("the miner builds the block"); + let test = "testInitCodeSizeTransactionCreationValidationFailsNotEnoughGas"; + assert_eq!(drops.invalid, [0], "{test}"); + assert!(result.tx_results.is_empty(), "{test}"); +} + +/// From RSKIP438 a creation of more than 49,152 bytes of init code is +/// invalid: the miner leaves it out, and a block that holds it is refused. +#[test] +fn from_rskip438_a_creation_of_more_than_49152_bytes_of_init_code_is_invalid() { + let test = "testInitCodeSizeTransactionCreationValidationFailsWithRSKIPActiveWithInitcodeSizeMaxReached"; + let mut chain = chain(RskHardforkConfig::mainnet(), LOVELL, ONE_RBTC); + let past = creation(MAX_INITCODE + 1, GAS); + let (_, drops) = chain.mine(&past).expect("the miner builds the block"); + assert_eq!(drops.invalid, [0], "{test}"); + assert!(chain.try_block(&past).is_err(), "{test}"); + assert!( + chain.block(&creation(MAX_INITCODE, GAS)).tx_results[0].success, + "{test}" + ); +} + +/// Before RSKIP438 a creation carries init code of any size, and no gas for +/// it: one of 49,152 bytes needs no more than its bytes' gas, and one byte +/// more is valid. +#[test] +fn before_rskip438_a_creation_carries_init_code_of_any_size_and_no_gas_for_it() { + let run = |size, gas| { + let mut chain = chain(RskHardforkConfig::mainnet(), LOVELL - 1, ONE_RBTC); + chain.block(&creation(size, gas)).tx_results.remove(0) + }; + let exact = run(MAX_INITCODE, MAX_CREATION_GAS); + assert!(exact.success); + assert_eq!(exact.gas_used, MAX_CREATION_GAS); + let past = run(MAX_INITCODE + 1, GAS); + assert!(past.success); + assert_eq!(past.gas_used, MAX_CREATION_GAS + 4); +} diff --git a/crates/execution/src/rskj_tests/vm/transient_storage_dsl_test.rs b/crates/execution/src/rskj_tests/vm/transient_storage_dsl_test.rs new file mode 100644 index 0000000..bafcfdd --- /dev/null +++ b/crates/execution/src/rskj_tests/vm/transient_storage_dsl_test.rs @@ -0,0 +1,793 @@ +//! rskj's `co.rsk.vm.opcode.TransientStorageDslTest`: transient storage +//! (RSKIP446, EIP-1153) across frames, contexts and transactions. +//! +//! rskj runs each case as Solidity contracts in blocks of its DSL and counts +//! the `OK` events its contracts emit, one for each check that holds. Here +//! each contract is written in opcodes and logs each word it checks (a LOG0 +//! of the word), and a test holds the logged words to the values rskj's +//! contracts check them against. A frame that fails drops its logs, as rskj +//! drops its events. Each case runs from lovell700 on mainnet. + +use alloy_primitives::{Address, U256}; +use rustock_core::Transaction; + +use super::harness::{ + call, compile, hex_word, logged, push_address, Chain, CONTRACT, LOG_TOP, ONE_RBTC, SENDER, +}; +use crate::executor::TxExecutionResult; + +/// lovell700 on mainnet: RSKIP446. +const LOVELL: u64 = 7_338_024; +/// A second contract with the code of the first, where a case needs one. +const OTHER: Address = Address::repeat_byte(0x0b); +/// The contract a case's contract calls. +const CALLEE: Address = Address::repeat_byte(0xce); +/// An account whose code is the init code a case's contract creates from. +const HOLDER: Address = Address::repeat_byte(0xd0); + +/// A chain whose next block is `number` on mainnet, with a funded sender +/// and `contracts`. +fn chain_at(number: u64, contracts: &[(Address, &[u8])]) -> Chain { + let mut chain = Chain::mainnet(number); + chain.put(SENDER, 0, ONE_RBTC, &[]); + for (address, code) in contracts { + chain.put(*address, 1, U256::ZERO, code); + } + chain +} + +/// [`chain_at`] lovell700. +fn chain_with(contracts: &[(Address, &[u8])]) -> Chain { + chain_at(LOVELL, contracts) +} + +/// A transaction of `gas` from the sender to [`CONTRACT`], whose input is +/// `words`. +fn sent(nonce: u64, gas: u64, words: &[U256]) -> (Transaction, Address) { + let input: Vec = words.iter().flat_map(U256::to_be_bytes::<32>).collect(); + (call(nonce, CONTRACT, U256::ZERO, gas, &input), SENDER) +} + +/// Runs [`sent`] alone in the chain's next block. +fn send(chain: &mut Chain, nonce: u64, gas: u64, words: &[U256]) -> TxExecutionResult { + let (transaction, sender) = sent(nonce, gas, words); + chain.run(sender, transaction) +} + +/// The words a test expects logged. +fn values(values: &[u64]) -> Vec { + values.iter().map(|value| U256::from(*value)).collect() +} + +/// A `bytes32` of rskj's cases: these bytes, then zeros. +fn bytes32(bytes: &[u8]) -> U256 { + let mut word = [0u8; 32]; + word[..bytes.len()].copy_from_slice(bytes); + U256::from_be_bytes(word) +} + +/// PUSH32 of `word`. +fn push(word: U256) -> String { + format!("PUSH32 {}", hex_word(word)) +} + +/// `op` (CALL, CALLCODE, DELEGATECALL or STATICCALL) of the contract the +/// code `to` pushes, with the gas the code `gas` pushes, passing the word at +/// 0x80 as its input and keeping a word of its output at 0xa0. Leaves the +/// call's success on the stack. +fn calling(op: &str, to: &str, gas: &str) -> String { + // CALL and CALLCODE take a value too: none. + let value = match op { + "CALL" | "CALLCODE" => "PUSH1 0x00", + _ => "", + }; + format!("PUSH1 0x20 PUSH1 0xa0 PUSH1 0x20 PUSH1 0x80 {value} {to} {gas} {op}") +} + +/// Init code that runs `constructor`, then returns `runtime` as the code it +/// deploys. +fn init(constructor: &str, runtime: &[u8]) -> Vec { + let constructor = compile(constructor); + // What copies and returns the runtime is 15 bytes. + let at = constructor.len() + 15; + let len = runtime.len(); + let returning = compile(&format!( + "PUSH2 0x{len:04x} PUSH2 0x{at:04x} PUSH1 0x00 CODECOPY PUSH2 0x{len:04x} PUSH1 0x00 RETURN" + )); + [constructor, returning, runtime.to_vec()].concat() +} + +/// rskj's `TestTransientStorage`, by the first word of its input. 1 TSTOREs +/// the third word at the second as key, and logs TLOAD of the key; 2 logs +/// TLOAD of the key; 3 and 4 TSTORE as 1 does, then 3 calls this contract +/// and 4 calls [`OTHER`] with 2 and the key. +fn basic() -> Vec { + compile(&format!( + "PUSH1 0x00 CALLDATALOAD + DUP1 PUSH1 0x02 EQ @load JUMPI + PUSH1 0x40 CALLDATALOAD PUSH1 0x20 CALLDATALOAD TSTORE + DUP1 PUSH1 0x01 EQ @load JUMPI + PUSH1 0x02 PUSH1 0x80 MSTORE PUSH1 0x20 CALLDATALOAD PUSH1 0xa0 MSTORE + PUSH1 0x03 EQ @itself JUMPI + {other} @calling JUMP + itself: ADDRESS + calling: PUSH1 0x00 DUP1 PUSH1 0x40 PUSH1 0x80 PUSH1 0x00 DUP6 GAS CALL STOP + load: PUSH1 0x20 CALLDATALOAD TLOAD {LOG_TOP} STOP", + other = push_address(OTHER), + )) +} + +/// RSKIP446 (lovell700): TLOAD of a key reads what TSTORE stored there in +/// the same frame. +#[test] +fn tload_reads_what_tstore_stored_at_the_key() { + let mut chain = chain_with(&[(CONTRACT, &basic())]); + let cases = [(b"1", &b"2"[..]), (b"2", &b"12345"[..])]; + for (nonce, (key, value)) in (0..).zip(cases) { + let (key, value) = (bytes32(key), bytes32(value)); + let ran = send(&mut chain, nonce, 200_000, &[U256::from(1), key, value]); + let test = "testTransientStorageOpcodesExecutionsWithRSKIPActivated"; + assert!(ran.success, "{test}"); + assert_eq!(logged(&ran), [value], "{test}"); + } +} + +/// A call a contract makes to itself reads what the contract stored before +/// the call, in the same transaction; rskj's case of it checks a TSTORE and +/// a TLOAD of its key and value in one frame. +#[test] +fn a_contracts_call_to_itself_reads_what_it_stored_before_the_call() { + let (key, value) = (bytes32(b"10"), bytes32(b"20")); + for selector in [1, 3] { + let mut chain = chain_with(&[(CONTRACT, &basic())]); + let input = [U256::from(selector), key, value]; + let ran = send(&mut chain, 0, 200_000, &input); + let test = "testTransientStorageOpcodesShareMemorySameTransaction"; + assert!(ran.success, "{test}: {selector}"); + assert_eq!(logged(&ran), [value], "{test}: {selector}"); + } +} + +/// Another contract reads none of what a contract stored: transient storage +/// is each address's own (rskj's `ERROR` event of the value loaded, zero). +#[test] +fn another_contract_reads_none_of_what_a_contract_stored() { + let mut chain = chain_with(&[(CONTRACT, &basic()), (OTHER, &basic())]); + let input = [U256::from(4), bytes32(b"10"), bytes32(b"20")]; + let ran = send(&mut chain, 0, 200_000, &input); + let test = "testTransientStorageOpcodesDoesntShareMemoryFromOtherContract"; + assert!(ran.success, "{test}"); + assert_eq!(logged(&ran), [U256::ZERO], "{test}"); +} + +/// Before RSKIP446 TLOAD and TSTORE are no opcodes, and the call fails with +/// all of its gas. +#[test] +fn tload_and_tstore_are_invalid_opcodes_before_rskip446() { + let input = [U256::from(1), bytes32(b"1"), bytes32(b"2")]; + let mut chain = chain_at(LOVELL - 1, &[(CONTRACT, &basic())]); + let ran = send(&mut chain, 0, 200_000, &input); + let test = "testTransientStorageOpcodesExecutionFailsWithRSKIPDeactivated"; + assert!(!ran.success, "{test}"); + assert_eq!(ran.gas_used, 200_000, "{test}"); +} + +/// The four keys of rskj's EIP-1153 cases, and four others. +fn eip1153_keys() -> [(U256, U256); 4] { + let one = U256::from(1); + [ + (one, U256::from(3)), + (U256::from(2), U256::from(4)), + (one << 128, (one << 128) + one), + (U256::MAX, U256::MAX - one), + ] +} + +/// rskj's `TestsTransientStorageEip1153BasicScenarios`, by the first word of +/// its input, for each of four keys: 1 SSTOREs 0xff at the key and logs +/// TLOAD of it; 2 TSTOREs the key at itself and logs TLOAD of it; 3 logs +/// TLOAD of it; anything else TSTOREs 0x4d2 at it and logs TLOAD of another +/// key. +fn eip1153_basic() -> Vec { + let each = |step: &dyn Fn(String, String) -> String| -> String { + let keys = eip1153_keys().map(|(key, other)| step(push(key), push(other))); + keys.join(" ") + }; + let after_sstore = each(&|k, _| format!("PUSH1 0xff {k} SSTORE {k} TLOAD {LOG_TOP}")); + let after_tstore = each(&|k, _| format!("{k} {k} TSTORE {k} TLOAD {LOG_TOP}")); + let unset = each(&|k, _| format!("{k} TLOAD {LOG_TOP}")); + let other_key = each(&|k, o| format!("PUSH2 0x04d2 {k} TSTORE {o} TLOAD {LOG_TOP}")); + compile(&format!( + "PUSH1 0x00 CALLDATALOAD + DUP1 PUSH1 0x01 EQ @after_sstore JUMPI + DUP1 PUSH1 0x02 EQ @after_tstore JUMPI + PUSH1 0x03 EQ @unset JUMPI + {other_key} STOP + after_sstore: {after_sstore} STOP + after_tstore: {after_tstore} STOP + unset: {unset} STOP" + )) +} + +/// EIP-1153's basic cases, a transaction each, in rskj's order: TLOAD after +/// an SSTORE of the key reads zero; TLOAD after a TSTORE reads its value; a +/// key in the next transaction reads zero again; and a TSTORE leaves other +/// keys at zero. For keys 1, 2, 2^128 and 2^256 - 1. +#[test] +fn tload_reads_only_what_tstore_stored_in_the_transaction() { + let mut chain = chain_with(&[(CONTRACT, &eip1153_basic())]); + let keys: Vec = eip1153_keys().iter().map(|(key, _)| *key).collect(); + let zeros = vec![U256::ZERO; 4]; + for (nonce, expected) in [(0, &zeros), (1, &keys), (2, &zeros), (3, &zeros)] { + let ran = send(&mut chain, nonce, 200_000, &[U256::from(nonce + 1)]); + let test = "testTransientStorageTestsEip1153BasicScenarios"; + assert!(ran.success, "{test}: {nonce}"); + assert_eq!(&logged(&ran), expected, "{test}: {nonce}"); + } +} + +/// rskj's `TestStorageCreateContexts`: TSTOREs 2, 3 and 4 at keys 0, 1 and +/// 2; creates a contract from the init code [`HOLDER`] holds as its code; +/// calls it, where `calls_it`; and logs TLOAD of keys 0, 1 and 2. +fn creating(init_len: usize, calls_it: bool) -> Vec { + // A CALL of the new contract, with all the gas left and no input. + let call_it = "PUSH1 0x00 DUP1 DUP1 DUP1 DUP1 DUP6 GAS CALL POP"; + let calls = if calls_it { call_it } else { "" }; + compile(&format!( + "PUSH1 0x02 PUSH1 0x00 TSTORE PUSH1 0x03 PUSH1 0x01 TSTORE PUSH1 0x04 PUSH1 0x02 TSTORE + PUSH2 0x{init_len:04x} PUSH1 0x00 PUSH2 0x0100 {holder} EXTCODECOPY + PUSH2 0x{init_len:04x} PUSH2 0x0100 PUSH1 0x00 CREATE + {calls} POP + PUSH1 0x00 TLOAD {LOG_TOP} PUSH1 0x01 TLOAD {LOG_TOP} PUSH1 0x02 TLOAD {LOG_TOP}", + holder = push_address(HOLDER), + )) +} + +/// What [`creating`] logs, creating from `init`. +fn created(init: &[u8], calls_it: bool) -> Vec { + let creator = creating(init.len(), calls_it); + let mut chain = chain_with(&[(CONTRACT, &creator), (HOLDER, init)]); + let ran = send(&mut chain, 0, 1_000_000, &[]); + assert!(ran.success); + logged(&ran) +} + +/// A constructor has the new contract's transient storage, none of its +/// creator's: it reads nothing at key 0, where its creator stored 2, and its +/// TSTORE at key 1 leaves its creator's 3 there. +#[test] +fn a_constructor_has_the_new_contracts_transient_storage() { + let constructor = format!( + "PUSH1 0x00 TLOAD {LOG_TOP} PUSH1 0x02 PUSH1 0x01 TSTORE PUSH1 0x01 TLOAD {LOG_TOP}" + ); + let logs = created(&init(&constructor, &[]), false); + assert_eq!(logs, values(&[0, 2, 2, 3, 4]), "testOnlyConstructorCode"); +} + +/// The new contract's code, called by its creator after the creation, has +/// the new contract's transient storage too. +#[test] +fn the_code_a_constructor_deploys_has_the_new_contracts_transient_storage() { + let stored = (U256::from(1) << 128) + U256::from(1); + let runtime = compile(&format!( + "PUSH1 0x00 TLOAD {LOG_TOP} {} PUSH1 0x01 TSTORE PUSH1 0x01 TLOAD {LOG_TOP} STOP", + push(stored) + )); + let constructor = format!("PUSH1 0x00 TLOAD {LOG_TOP}"); + let logs = created(&init(&constructor, &runtime), true); + let mut expected = values(&[0, 0]); + expected.extend([stored]); + expected.extend(values(&[2, 3, 4])); + assert_eq!(logs, expected, "testInConstructorAndCode"); +} + +/// What a constructor stores, the code it deploys reads later in the same +/// transaction: both are the new contract's (rskj's V0, and V1, whose +/// constructor also checks key 0 first). +#[test] +fn the_deployed_code_reads_what_its_constructor_stored_in_the_transaction() { + let runtime = |stored: u64| { + compile(&format!( + "PUSH1 0x00 TLOAD {LOG_TOP} PUSH1 0x01 TLOAD {LOG_TOP} + PUSH2 0x{stored:04x} PUSH1 0x02 TSTORE PUSH1 0x02 TLOAD {LOG_TOP} STOP" + )) + }; + let v0 = init("PUSH2 0x0111 PUSH1 0x01 TSTORE", &runtime(0x222)); + let expected = values(&[0, 0x111, 0x222, 2, 3, 4]); + let test = "testAccrossConstructorAndCodeV0CreateContext"; + assert_eq!(created(&v0, true), expected, "{test}"); + let constructor = format!("PUSH1 0x00 TLOAD {LOG_TOP} PUSH2 0x0333 PUSH1 0x01 TSTORE"); + let v1 = init(&constructor, &runtime(0x444)); + let expected = values(&[0, 0, 0x333, 0x444, 2, 3, 4]); + let test = "testAccrossConstructorAndCodeV1CreateContext"; + assert_eq!(created(&v1, true), expected, "{test}"); +} + +/// rskj's case of no constructor code: the deployed code alone, called by +/// its creator, reads nothing at key 0 and reads back its own TSTORE. +#[test] +fn code_deployed_by_an_empty_constructor_has_the_new_contracts_transient_storage() { + let runtime = compile(&format!( + "PUSH1 0x00 TLOAD {LOG_TOP} PUSH2 0x0555 PUSH1 0x01 TSTORE PUSH1 0x01 TLOAD {LOG_TOP} STOP" + )); + let logs = created(&init("", &runtime), true); + assert_eq!(logs, values(&[0, 0x555, 2, 3, 4]), "testNoConstructorCode"); +} + +/// rskj's caller in its cases of the dynamic contexts: TSTOREs 420 at key 0, +/// and at key 1 too where `both`; calls [`CALLEE`] with `op` and the gas the +/// code `gas` pushes; logs the call's success; then runs `then`. +fn context_caller(op: &str, gas: &str, both: bool, then: &str) -> Vec { + let second = if both { + "PUSH2 0x01a4 PUSH1 0x01 TSTORE" + } else { + "" + }; + let called = calling(op, &push_address(CALLEE), gas); + compile(&format!( + "PUSH2 0x01a4 PUSH1 0x00 TSTORE {second} {called} {LOG_TOP} {then}" + )) +} + +/// What `caller` logs, calling [`CALLEE`] whose code is `callee`, from a +/// transaction of `gas`; and the chain after it. +fn context_logs(caller: &[u8], callee: &str, gas: u64) -> (Chain, Vec) { + let callee = compile(callee); + let mut chain = chain_with(&[(CONTRACT, caller), (CALLEE, &callee)]); + let ran = send(&mut chain, 0, gas, &[]); + assert!(ran.success); + let logs = logged(&ran); + (chain, logs) +} + +/// CALLCODE and DELEGATECALL run the callee in its caller's transient +/// storage, as in its caller's storage: the callee reads the caller's 420 at +/// key 0 and stores it in slot 2, and its TSTORE of 69 at key 1 is the +/// caller's, which it reads back into slot 3. +#[test] +fn callcode_and_delegatecall_run_the_callee_in_the_callers_transient_storage() { + let callee = "PUSH1 0x00 TLOAD PUSH1 0x02 SSTORE PUSH1 0x45 PUSH1 0x01 TSTORE + PUSH1 0x01 TLOAD PUSH1 0x03 SSTORE STOP"; + let then = format!( + "PUSH1 0x00 TLOAD {LOG_TOP} PUSH1 0x02 SLOAD {LOG_TOP} + PUSH1 0x03 SLOAD {LOG_TOP} PUSH1 0x01 TLOAD {LOG_TOP}" + ); + for op in ["CALLCODE", "DELEGATECALL"] { + let caller = context_caller(op, "GAS", false, &then); + let (_, logs) = context_logs(&caller, callee, 300_000); + let test = "testDynamicExecutionContextSimpleScenario"; + assert_eq!(logs, values(&[1, 420, 420, 69, 69]), "{test}: {op}"); + } +} + +/// A callee that fails takes back its TSTORE of 69 at key 1, called with +/// CALLCODE, DELEGATECALL or CALL and the gas the code `gas` pushes: the +/// call reports failure, and the caller reads its own 420 at keys 0 and 1. +fn failing_callee_takes_back_its_tstore(test: &str, callee: &str, gas: &str) { + let then = format!("PUSH1 0x00 TLOAD {LOG_TOP} PUSH1 0x01 TLOAD {LOG_TOP}"); + for op in ["CALLCODE", "DELEGATECALL", "CALL"] { + let caller = context_caller(op, gas, true, &then); + let (_, logs) = context_logs(&caller, callee, 500_000); + assert_eq!(logs, values(&[0, 420, 420]), "{test}: {op}"); + } +} + +/// A callee that REVERTs takes back its TSTORE. +#[test] +fn a_callee_that_reverts_takes_back_its_tstore() { + let callee = "PUSH1 0x45 PUSH1 0x01 TSTORE PUSH1 0x00 DUP1 REVERT"; + let test = "testDynamicExecutionContextWithRevert"; + failing_callee_takes_back_its_tstore(test, callee, "GAS"); +} + +/// A callee that runs INVALID, given 255 gas, takes back its TSTORE. +#[test] +fn a_callee_that_runs_invalid_takes_back_its_tstore() { + let callee = "PUSH1 0x45 PUSH1 0x01 TSTORE INVALID"; + let test = "testDynamicExecutionContextWithInvalid"; + failing_callee_takes_back_its_tstore(test, callee, "PUSH1 0xff"); +} + +/// A callee given 0xffff gas, whose calls of itself, each a frame deeper, +/// fail at last for lack of gas, fails in turn and takes back its TSTORE. +#[test] +fn a_callee_failing_deep_in_its_own_calls_takes_back_its_tstore() { + let callee = format!( + "PUSH1 0x45 PUSH1 0x01 TSTORE + PUSH1 0x00 DUP1 DUP1 DUP1 DUP1 {itself} GAS CALL @fine JUMPI + PUSH1 0x00 DUP1 REVERT + fine: STOP", + itself = push_address(CALLEE) + ); + let test = "testDynamicExecutionContextWithStackOverflow"; + failing_callee_takes_back_its_tstore(test, &callee, "PUSH2 0xffff"); +} + +/// A CALL runs the callee in its own transient storage: it reads none of its +/// caller's, and its TSTORE is its own. The callee logs the 69 it stored; +/// the caller logs the call's success, its own 420 at key 0, nothing at key +/// 1, and nothing in its slots 0 and 1, where the callee wrote its own. +#[test] +fn a_call_runs_the_callee_in_its_own_transient_storage() { + let callee = format!( + "PUSH1 0x00 TLOAD PUSH1 0x00 SSTORE PUSH1 0x45 PUSH1 0x01 TSTORE + PUSH1 0x01 TLOAD PUSH1 0x01 SSTORE PUSH1 0x01 TLOAD {LOG_TOP} STOP" + ); + let then = format!( + "PUSH1 0x00 TLOAD {LOG_TOP} PUSH1 0x01 TLOAD {LOG_TOP} + PUSH1 0x00 SLOAD {LOG_TOP} PUSH1 0x01 SLOAD {LOG_TOP}" + ); + let caller = context_caller("CALL", "GAS", false, &then); + let (chain, logs) = context_logs(&caller, &callee, 1_000_000); + let test = "testDynamicExecutionCallContextSubcall"; + assert_eq!(logs, values(&[69, 1, 420, 0, 0, 0]), "{test}"); + assert_eq!(chain.storage(CALLEE, 0), U256::ZERO, "{test}"); + assert_eq!(chain.storage(CALLEE, 1), U256::from(69), "{test}"); +} + +/// A STATICCALL's callee cannot TSTORE: the call fails, and the caller's 420 +/// at key 0 is still there. +#[test] +fn a_static_calls_callee_cannot_tstore() { + let then = format!("PUSH1 0x00 TLOAD {LOG_TOP}"); + let caller = context_caller("STATICCALL", "PUSH2 0xffff", false, &then); + let (_, logs) = context_logs(&caller, "PUSH1 0x45 PUSH1 0x01 TSTORE STOP", 1_000_000); + let test = "testDynamicExecutionStaticCallSubcallCantUseTstore"; + assert_eq!(logs, values(&[0, 420]), "{test}"); +} + +/// rskj's contract for a TSTORE before a REVERT or an INVALID. The first call +/// sets a flag in slot 0, TSTOREs 0x100 at 0xff, logs TLOAD of it, calls +/// itself, and logs the call's success and TLOAD of 0xff after it. The call +/// it makes, finding the flag, TSTOREs 0x101 at 0xff, then REVERTs, or runs +/// INVALID where its input's first word is 1, as the first call then gives +/// it only 0xffff gas. +fn tstore_then_fail() -> Vec { + compile(&format!( + "PUSH1 0x00 SLOAD @inner JUMPI + PUSH1 0x01 PUSH1 0x00 SSTORE + PUSH2 0x0100 PUSH1 0xff TSTORE PUSH1 0xff TLOAD {LOG_TOP} + PUSH1 0x00 CALLDATALOAD PUSH1 0x80 MSTORE + PUSH1 0x00 CALLDATALOAD @limited JUMPI + {all} @called JUMP + limited: {limited} + called: {LOG_TOP} PUSH1 0xff TLOAD {LOG_TOP} STOP + inner: PUSH2 0x0101 PUSH1 0xff TSTORE + PUSH1 0x00 CALLDATALOAD @invalid JUMPI + PUSH1 0x00 DUP1 REVERT + invalid: INVALID", + all = calling("CALL", "ADDRESS", "GAS"), + limited = calling("CALL", "ADDRESS", "PUSH2 0xffff"), + )) +} + +/// A contract's call to itself that TSTOREs and then REVERTs leaves the +/// contract's value as it was. The next transaction, in the same block, +/// finds the flag the first left and runs the inner branch itself, whose +/// INVALID fails the transaction with all of its gas, as rskj's case has it. +#[test] +fn a_tstore_before_a_revert_has_no_effect() { + let mut chain = chain_with(&[(CONTRACT, &tstore_then_fail())]); + let reverting = sent(0, 300_000, &[U256::ZERO]); + let invalid = sent(1, 1_000_000, &[U256::from(1)]); + let block = chain.block(&[reverting, invalid]); + let (reverting, invalid) = (&block.tx_results[0], &block.tx_results[1]); + let test = "testDynamicReentrancyContextsTstoreBeforeRevertOrInvalidHasNoEffect"; + assert!(reverting.success, "{test}"); + assert_eq!(logged(reverting), values(&[0x100, 0, 0x100]), "{test}"); + assert!(!invalid.success, "{test}"); + assert_eq!(invalid.gas_used, 1_000_000, "{test}"); +} + +/// rskj's contract for a REVERT or an INVALID that undoes all. The first call +/// sets a flag, TSTOREs 0x100 at 0xfe and 0x101 at 0xff, logs TLOAD of both, +/// calls itself, and logs the call's success and TLOAD of both after it. The +/// call it makes, finding the flag, TSTOREs at both twice, then REVERTs, or +/// runs INVALID where its input's first word is 1. +fn undoing_all() -> Vec { + compile(&format!( + "PUSH1 0x00 SLOAD @inner JUMPI + PUSH1 0x01 PUSH1 0x00 SSTORE + PUSH2 0x0100 PUSH1 0xfe TSTORE PUSH2 0x0101 PUSH1 0xff TSTORE + PUSH1 0xfe TLOAD {LOG_TOP} PUSH1 0xff TLOAD {LOG_TOP} + PUSH1 0x00 CALLDATALOAD PUSH1 0x80 MSTORE + {all} {LOG_TOP} PUSH1 0xfe TLOAD {LOG_TOP} PUSH1 0xff TLOAD {LOG_TOP} STOP + inner: PUSH2 0x0201 PUSH1 0xfe TSTORE PUSH2 0x0202 PUSH1 0xfe TSTORE + PUSH2 0x0201 PUSH1 0xff TSTORE PUSH2 0x0202 PUSH1 0xff TSTORE + PUSH1 0x00 CALLDATALOAD @invalid JUMPI + PUSH1 0x00 DUP1 REVERT + invalid: INVALID", + all = calling("CALL", "ADDRESS", "GAS"), + )) +} + +/// A REVERT takes back every TSTORE of its frame: the contract reads its own +/// 0x100 and 0x101 after its call to itself stored others twice and +/// reverted. The next transaction finds the flag and fails with INVALID. +#[test] +fn a_revert_takes_back_every_tstore_of_its_frame() { + let mut chain = chain_with(&[(CONTRACT, &undoing_all())]); + let test = "testDynamicReentrancyContextsRevertOrInvalidUndoesAll"; + let ran = send(&mut chain, 0, 300_000, &[U256::ZERO]); + assert!(ran.success, "{test}"); + let expected = values(&[0x100, 0x101, 0, 0x100, 0x101]); + assert_eq!(logged(&ran), expected, "{test}"); + let ran = send(&mut chain, 1, 1_000_000, &[U256::from(1)]); + assert!(!ran.success, "{test}"); +} + +/// rskj's contract for a REVERT or an INVALID that undoes a TSTORE made in a +/// call that succeeded, by a counter in slot 0. At 0 it counts 1, TSTOREs +/// 0x100 at 0xff, keeps TLOAD of it in slot 1, calls itself, and keeps the +/// call's success in slot 3 and TLOAD of 0xff in slot 2. At 1 it counts 2, +/// calls itself, keeps that call's success in slot 4, then REVERTs, or runs +/// INVALID where its input's first word is 1. At 2 it TSTOREs 0x101 at 0xff. +fn undoing_a_successful_call() -> Vec { + compile(&format!( + "PUSH1 0x00 SLOAD + DUP1 PUSH1 0x01 EQ @second JUMPI + DUP1 PUSH1 0x02 EQ @third JUMPI + PUSH1 0x01 ADD PUSH1 0x00 SSTORE + PUSH2 0x0100 PUSH1 0xff TSTORE PUSH1 0xff TLOAD PUSH1 0x01 SSTORE + PUSH1 0x00 CALLDATALOAD PUSH1 0x80 MSTORE + {all} PUSH1 0x03 SSTORE PUSH1 0xff TLOAD PUSH1 0x02 SSTORE STOP + second: POP PUSH1 0x02 PUSH1 0x00 SSTORE + PUSH1 0x00 CALLDATALOAD PUSH1 0x80 MSTORE + PUSH1 0x00 CALLDATALOAD @invalid JUMPI + {all} PUSH1 0x04 SSTORE PUSH1 0x20 PUSH1 0x00 REVERT + invalid: {limited} PUSH1 0x04 SSTORE INVALID + third: PUSH2 0x0101 PUSH1 0xff TSTORE STOP", + all = calling("CALL", "ADDRESS", "GAS"), + limited = calling("CALL", "ADDRESS", "PUSH2 0xffff"), + )) +} + +/// A frame that REVERTs takes back the TSTORE of a call it made that +/// succeeded: the first frame reads its own 0x100 at 0xff after its call, +/// which failed, and the second call's success is taken back with the rest. +/// The next transaction finds the counter at 1 and fails with INVALID, +/// leaving the slots as the first left them. +#[test] +fn a_revert_takes_back_the_tstore_of_a_call_that_succeeded() { + let test = "testDynamicReentrancyContextsRevertOrInvalidUndoesTstorageAfterSuccessfullCall"; + let mut chain = chain_with(&[(CONTRACT, &undoing_a_successful_call())]); + // TLOAD before the call and after it, and each call's success. + let kept = |chain: &Chain| { + let slots = (1..=4).map(|slot| chain.storage(CONTRACT, slot)); + slots.collect::>() + }; + let ran = send(&mut chain, 0, 300_000, &[U256::ZERO]); + assert!(ran.success, "{test}"); + assert_eq!(kept(&chain), values(&[0x100, 0x100, 0, 0]), "{test}"); + let ran = send(&mut chain, 1, 300_000, &[U256::from(1)]); + assert!(!ran.success, "{test}"); + assert_eq!(kept(&chain), values(&[0x100, 0x100, 0, 0]), "{test}"); +} + +/// rskj's contracts for reentrancy: the first call sets a flag in slot 0, +/// TSTOREs 0x100 at the key `key` pushes and keeps TLOAD of it in slot 3, +/// calls itself, and keeps the call's success in slot 1 and TLOAD of the key +/// in slot 2. The call it makes, finding the flag, runs `inner`. +fn reentering(key: &str, inner: &str) -> Vec { + compile(&format!( + "PUSH1 0x00 SLOAD @inner JUMPI + PUSH1 0x01 PUSH1 0x00 SSTORE + PUSH2 0x0100 {key} TSTORE {key} TLOAD PUSH1 0x03 SSTORE + {all} PUSH1 0x01 SSTORE {key} TLOAD PUSH1 0x02 SSTORE STOP + inner: {inner} STOP", + all = calling("CALL", "ADDRESS", "GAS"), + )) +} + +/// Slots 1 to 4 of [`reentering`] after a transaction runs it. +fn reentered(key: &str, inner: &str) -> Vec { + let mut chain = chain_with(&[(CONTRACT, &reentering(key, inner))]); + let ran = send(&mut chain, 0, 300_000, &[]); + assert!(ran.success); + (1..=4).map(|slot| chain.storage(CONTRACT, slot)).collect() +} + +/// A contract's call to itself reads what the contract stored before it (in +/// slot 4): the call's success, then TLOAD after it, before it, and in it. +#[test] +fn a_reentrant_call_reads_what_the_contract_stored_before_it() { + let slots = reentered("PUSH1 0x00", "PUSH1 0x00 TLOAD PUSH1 0x04 SSTORE"); + let test = "testReentrancyContextsTstoreAfterReentrantCall"; + assert_eq!(slots, values(&[1, 0x100, 0x100, 0x100]), "{test}"); +} + +/// After its call to itself, a contract reads what the call stored. +#[test] +fn a_contract_reads_what_its_reentrant_call_stored() { + let slots = reentered("PUSH1 0xff", "PUSH2 0x0101 PUSH1 0xff TSTORE"); + let test = "testReentrancyContextsTloadAfterReentrantTstore"; + assert_eq!(slots, values(&[1, 0x101, 0x100, 0]), "{test}"); +} + +/// A contract's call to itself overwrites what the contract stored, reads +/// its own value back (in slot 4), and the contract reads it after. +#[test] +fn a_reentrant_call_overwrites_what_the_contract_stored() { + let inner = "PUSH2 0x0101 PUSH1 0xff TSTORE PUSH1 0xff TLOAD PUSH1 0x04 SSTORE"; + let slots = reentered("PUSH1 0xff", inner); + let test = "testReentrancyContextsManipulateInReentrantCall"; + assert_eq!(slots, values(&[1, 0x101, 0x100, 0x101]), "{test}"); +} + +/// rskj's contract for a TSTORE in a call and a TLOAD returned in a static +/// call, by a counter in slot 0. At 0 it counts 1, TSTOREs 0x100 at 0xff, +/// keeps TLOAD of it in slot 1, calls itself, and keeps the call's success +/// in slot 4 and TLOAD of 0xfe in slot 2. At 1 it counts 2, TSTOREs 0x101 at +/// 0xfe, STATICCALLs itself, and keeps that call's success in slot 5 and +/// the word it returned in slot 3. At 2 it returns TLOAD of 0xfe. +fn static_reader() -> Vec { + compile(&format!( + "PUSH1 0x00 SLOAD + DUP1 PUSH1 0x01 EQ @second JUMPI + DUP1 PUSH1 0x02 EQ @third JUMPI + PUSH1 0x01 ADD PUSH1 0x00 SSTORE + PUSH2 0x0100 PUSH1 0xff TSTORE PUSH1 0xff TLOAD PUSH1 0x01 SSTORE + {all} PUSH1 0x04 SSTORE PUSH1 0xfe TLOAD PUSH1 0x02 SSTORE STOP + second: POP PUSH1 0x02 PUSH1 0x00 SSTORE + PUSH2 0x0101 PUSH1 0xfe TSTORE + {statically} PUSH1 0x05 SSTORE PUSH1 0xa0 MLOAD PUSH1 0x03 SSTORE STOP + third: PUSH1 0xfe TLOAD PUSH1 0x00 MSTORE PUSH1 0x20 PUSH1 0x00 RETURN", + all = calling("CALL", "ADDRESS", "GAS"), + statically = calling("STATICCALL", "ADDRESS", "GAS"), + )) +} + +/// A static call of the contract reads, and returns, what a call of it +/// stored, and the contract reads that value after the call returns. +#[test] +fn a_static_call_returns_what_a_call_stored_and_the_caller_reads_it_after() { + let mut chain = chain_with(&[(CONTRACT, &static_reader())]); + let ran = send(&mut chain, 0, 300_000, &[]); + let test = "testReentrancyContextsTstoreInCallThenTloadReturnInStaticCall"; + assert!(ran.success, "{test}"); + let slots: Vec = (1..=5).map(|slot| chain.storage(CONTRACT, slot)).collect(); + // Before, 0xfe after the call, what the static call returned, and both + // calls' success. + assert_eq!(slots, values(&[0x100, 0x101, 0x101, 1, 1]), "{test}"); +} + +/// TLOAD and TSTORE cost 100 gas each, whether or not the key was written +/// before in the transaction. Each figure is the gas between two GASes. +#[test] +fn tload_and_tstore_cost_100_gas_whether_or_not_the_key_was_written() { + let measures = [ + "PUSH1 0x10 TLOAD POP", + "PUSH1 0x10 PUSH1 0x10 TSTORE PUSH1 0x10 TLOAD POP", + "PUSH1 0x10 PUSH1 0xff TSTORE", + "PUSH1 0x10 PUSH1 0xff TSTORE PUSH1 0x11 PUSH1 0xff TSTORE", + ]; + let code = measures.map(|code| format!("GAS {code} GAS SWAP1 SUB {LOG_TOP}")); + let mut chain = chain_with(&[(CONTRACT, &compile(&code.join(" ")))]); + let ran = send(&mut chain, 0, 350_000, &[]); + let test = "testTransientStorageGasMeasureTests"; + assert!(ran.success, "{test}"); + // The PUSH1s at 3, the POPs and the second GAS at 2, and each TLOAD and + // TSTORE at 100: the TSTORE of a key just written as the first. + let expected = [ + 3 + 100 + 2 + 2, + 3 + 3 + 100 + 3 + 100 + 2 + 2, + 3 + 3 + 100 + 2, + 3 + 3 + 100 + 3 + 3 + 100 + 2, + ]; + assert_eq!(logged(&ran), values(&expected), "{test}"); +} + +/// TSTOREs in a loop run out of gas, and the transaction fails with all of +/// it: at a new key each time, at a key 2^codesize past the gas left, and +/// with a TLOAD of each key after its TSTORE. +#[test] +fn a_loop_of_tstores_runs_out_of_gas_and_fails_with_all_of_it() { + for (test, body, gas) in [ + ("testTstoreLoopUntilOutOfGas", "GAS GAS TSTORE", 300_000), + ( + "testTstoreWideAddressSpaceLoopUntilOutOfGas", + "GAS GAS PUSH1 0x01 CODESIZE SHL ADD TSTORE", + 500_000, + ), + ( + "testTstoreAndTloadLoopUntilOutOfGas", + "GAS DUP1 GAS SWAP1 TSTORE TLOAD POP", + 700_000, + ), + ] { + let looping = compile(&format!("again: {body} @again JUMP")); + let mut chain = chain_with(&[(CONTRACT, &looping)]); + let ran = send(&mut chain, 0, gas, &[]); + assert!(!ran.success, "{test}"); + assert_eq!(ran.gas_used, gas, "{test}"); + } +} + +/// rskj's tester of SELFDESTRUCT: calls the callee with each of `options` as +/// the first word of its input, logging each call's success, then logs the +/// word the last call returned. The callee is [`CALLEE`], or, where +/// `created` is the length of the init code [`HOLDER`] holds, one this +/// contract creates from it first. +fn destruction_tester(options: &[u8], created: Option) -> Vec { + let (create, callee) = match created { + Some(len) => ( + format!( + "PUSH2 0x{len:04x} PUSH1 0x00 PUSH2 0x0100 {holder} EXTCODECOPY + PUSH2 0x{len:04x} PUSH2 0x0100 PUSH1 0x00 CREATE PUSH1 0x40 MSTORE", + holder = push_address(HOLDER) + ), + "PUSH1 0x40 MLOAD".to_string(), + ), + None => (String::new(), push_address(CALLEE)), + }; + let called = calling("CALL", &callee, "GAS"); + let calls: Vec = options + .iter() + .map(|option| format!("PUSH1 0x{option:02x} PUSH1 0x80 MSTORE {called} {LOG_TOP}")) + .collect(); + let calls = calls.join(" "); + compile(&format!("{create} {calls} PUSH1 0xa0 MLOAD {LOG_TOP}")) +} + +/// Transient storage outlives a SELFDESTRUCT in the same transaction, for a +/// contract that was there before the transaction and for one it created: +/// what the contract stored before it destroyed itself, or after, or what it +/// stored before a call of itself that destroyed it, it still reads. +#[test] +fn transient_storage_outlives_a_selfdestruct_in_the_same_transaction() { + let tload = "PUSH1 0x00 CALLDATALOAD PUSH1 0x01 EQ @destroy JUMPI + PUSH1 0xff TLOAD PUSH1 0x00 MSTORE PUSH1 0x20 PUSH1 0x00 RETURN + destroy: PUSH2 0x0100 PUSH1 0xff TSTORE PUSH1 0x00 SELFDESTRUCT" + .to_string(); + let tload_inner = format!( + "PUSH1 0x00 CALLDATALOAD PUSH1 0x01 EQ @first JUMPI + PUSH1 0x00 SELFDESTRUCT + first: PUSH2 0x0100 PUSH1 0xff TSTORE + PUSH1 0x02 PUSH1 0x80 MSTORE {itself} POP + PUSH1 0xff TLOAD PUSH1 0x00 MSTORE PUSH1 0x20 PUSH1 0x00 RETURN", + itself = calling("CALL", "ADDRESS", "GAS") + ); + let tstore = "PUSH1 0x00 CALLDATALOAD DUP1 PUSH1 0x01 EQ @destroy JUMPI + PUSH1 0x02 EQ @store JUMPI + PUSH1 0xff TLOAD PUSH1 0x00 MSTORE PUSH1 0x20 PUSH1 0x00 RETURN + destroy: PUSH1 0x00 SELFDESTRUCT + store: PUSH2 0x0100 PUSH1 0xff TSTORE STOP" + .to_string(); + let cases: [(&str, String, &[u8]); 3] = [ + ("tload_after_selfdestruct", tload, &[1, 2]), + ("tload_after_inner_selfdestruct", tload_inner, &[1]), + ("tstore_after_selfdestruct", tstore, &[1, 2, 3]), + ]; + for (case, callee, options) in cases { + // Each call succeeds, and the last returns the 0x100 stored. + let mut expected = vec![U256::from(1); options.len()]; + expected.push(U256::from(0x100)); + let callee = compile(&callee); + let tester = destruction_tester(options, None); + let mut chain = chain_with(&[(CONTRACT, &tester), (CALLEE, &callee)]); + let ran = send(&mut chain, 0, 300_000, &[]); + assert!(ran.success, "{case}_pre_existing_contract"); + assert_eq!(logged(&ran), expected, "{case}_pre_existing_contract"); + let init = init("", &callee); + let tester = destruction_tester(options, Some(init.len())); + let mut chain = chain_with(&[(CONTRACT, &tester), (HOLDER, &init)]); + let ran = send(&mut chain, 0, 300_000, &[]); + assert!(ran.success, "{case}_new_contract"); + assert_eq!(logged(&ran), expected, "{case}_new_contract"); + } +} + +/// Transient storage is empty again for the block's next transaction: the +/// first TSTOREs 0x64 at 0xff and reads it back, the second reads zero. +#[test] +fn transient_storage_is_empty_again_for_the_blocks_next_transaction() { + let mut chain = chain_with(&[(CONTRACT, &basic())]); + let (key, value) = (U256::from(0xff), U256::from(0x64)); + let store = sent(0, 200_000, &[U256::from(1), key, value]); + let load = sent(1, 200_000, &[U256::from(2), key]); + let block = chain.block(&[store, load]); + let test = "testTloadTstoreCheckingBetweenTransactions"; + assert!(block.tx_results.iter().all(|ran| ran.success), "{test}"); + assert_eq!(logged(&block.tx_results[0]), values(&[0x64]), "{test}"); + assert_eq!(logged(&block.tx_results[1]), values(&[0]), "{test}"); +} diff --git a/crates/execution/src/rskj_tests/vm/transient_storage_test.rs b/crates/execution/src/rskj_tests/vm/transient_storage_test.rs new file mode 100644 index 0000000..a6a74e6 --- /dev/null +++ b/crates/execution/src/rskj_tests/vm/transient_storage_test.rs @@ -0,0 +1,65 @@ +//! rskj's `co.rsk.vm.opcode.TransientStorageTest`: TLOAD and TSTORE (RSKIP446) +//! and the stack, run from lovell700 on mainnet. + +use alloy_primitives::U256; + +use super::harness::{compile, hex_word, return_top, run_code, words, GAS, RETURN_ONE_WORD_GAS}; + +/// lovell700 on mainnet: RSKIP446. +const LOVELL: u64 = 7_338_024; + +/// TLOAD with nothing on the stack fails, with all of the call's gas: rskj's +/// `StackTooSmallException`. +#[test] +fn tload_with_an_empty_stack_fails_with_all_the_gas() { + let ran = run_code(LOVELL, &compile("TLOAD")); + assert!(!ran.success, "testTLoadDynamicExecutionContextUnderflow"); + assert_eq!(ran.gas_used, GAS); +} + +/// TLOAD of a key nothing was stored at pushes zero, for 100 gas. +#[test] +fn tload_of_a_key_never_stored_pushes_zero() { + let one = hex_word(U256::from(1)); + let code = [compile(&format!("PUSH32 {one} TLOAD")), return_top(1)].concat(); + let ran = run_code(LOVELL, &code); + assert!(ran.success, "testTLoadDynamicExecutionContextWorksFine"); + assert_eq!(words(&ran.output), [U256::ZERO]); + assert_eq!(ran.gas_used, 21_000 + 3 + 100 + RETURN_ONE_WORD_GAS); +} + +/// TSTORE with fewer than its two words on the stack fails, with all of the +/// call's gas. +#[test] +fn tstore_short_of_its_two_words_fails_with_all_the_gas() { + for code in ["TSTORE", "PUSH1 0x01 TSTORE"] { + let ran = run_code(LOVELL, &compile(code)); + let test = "testTStoreDynamicExecutionContextUnderflow"; + assert!(!ran.success, "{test}: {code}"); + assert_eq!(ran.gas_used, GAS, "{code}"); + } +} + +/// TSTORE takes its key and its value off the stack and runs on, for 100 +/// gas: the word pushed before them is the top one after it. +#[test] +fn tstore_takes_its_key_and_its_value_off_the_stack() { + let (value, key) = (hex_word(U256::from(0x420)), hex_word(U256::from(1))); + let code = compile(&format!("PUSH1 0x2a PUSH32 {value} PUSH32 {key} TSTORE")); + let ran = run_code(LOVELL, &[code, return_top(1)].concat()); + assert!(ran.success, "testTStoreDynamicExecutionContextWorksFine"); + assert_eq!(words(&ran.output), [U256::from(0x2a)]); + assert_eq!(ran.gas_used, 21_000 + 9 + 100 + RETURN_ONE_WORD_GAS); +} + +/// TLOAD of a key pushes what TSTORE stored there. +#[test] +fn tload_pushes_what_tstore_stored_at_the_key() { + let (value, key) = (hex_word(U256::from(0x420)), hex_word(U256::from(1))); + let stored = format!("PUSH32 {value} PUSH32 {key} TSTORE"); + let code = compile(&format!("{stored} PUSH32 {key} TLOAD")); + let ran = run_code(LOVELL, &[code, return_top(1)].concat()); + let test = "testTStoreTloadDynamicExecutionContextWorksFine"; + assert!(ran.success, "{test}"); + assert_eq!(words(&ran.output), [U256::from(0x420)]); +} diff --git a/crates/execution/src/rskj_tests/vm/vm_dsl_test.rs b/crates/execution/src/rskj_tests/vm/vm_dsl_test.rs new file mode 100644 index 0000000..5750997 --- /dev/null +++ b/crates/execution/src/rskj_tests/vm/vm_dsl_test.rs @@ -0,0 +1,94 @@ +//! rskj's `co.rsk.vm.VmDslTest`, its tests of RSKIP438 (lovell700): from it +//! CREATE and CREATE2 charge 2 gas for each word of the init code they run +//! (`VM.doCREATE`, `VM.doCREATE2`), and init code past 49,152 bytes fails +//! the contract that runs them (`Program.validateInitcodeSize`). + +use alloy_primitives::U256; + +use super::harness::{compile, return_top, run_code, GAS}; + +/// lovell700 on mainnet: RSKIP438. +const LOVELL: u64 = 7_338_024; +/// The most init code RSKIP438 lets a creation run: twice the most code a +/// contract may hold. +const MAX_INITCODE: usize = 49_152; + +/// What a contract gets from creating, by `opcode`, a contract of `size` +/// zero bytes of init code (a STOP, which deploys no code), in block +/// `number` of mainnet: whether the transaction succeeded, the address the +/// contract returns, and the gas it used. +fn create_at(number: u64, opcode: &str, size: usize) -> (bool, U256, u64) { + let salt = if opcode == "CREATE2" { + "PUSH1 0x01" + } else { + "" + }; + let mut code = compile(&format!( + "{salt} PUSH3 0x{size:06x} PUSH1 0x00 PUSH1 0x00 {opcode}" + )); + code.extend(return_top(1)); + let ran = run_code(number, &code); + (ran.success, U256::from_be_slice(&ran.output), ran.gas_used) +} + +/// Before RSKIP438 CREATE and CREATE2 run init code of any size, and charge +/// for its memory and, CREATE2, its hashing, but nothing for the code: one +/// byte past 49,152 creates the contract too. +#[test] +fn before_rskip438_create_and_create2_run_init_code_of_any_size_for_no_gas() { + let cases = [ + ("CREATE", MAX_INITCODE, 62_237), + ("CREATE", MAX_INITCODE + 1, 62_246), + ("CREATE2", MAX_INITCODE, 71_456), + ("CREATE2", MAX_INITCODE + 1, 71_471), + ]; + for (opcode, size, gas) in cases { + let (success, created, used) = create_at(LOVELL - 1, opcode, size); + assert!(success && created != U256::ZERO, "{opcode} of {size} bytes"); + assert_eq!(used, gas, "{opcode} of {size} bytes"); + } +} + +/// From RSKIP438 CREATE and CREATE2 charge 2 gas for each word of init +/// code: 3,072 more for 49,152 bytes. +#[test] +fn from_rskip438_create_and_create2_charge_2_gas_a_word_of_init_code() { + let cases = [ + ( + "testInitCodeSizeCREATEOpcodeValidationSuccessWithInitcodeCost", + "CREATE", + 65_309, + ), + ( + "testInitCodeSizeCREATE2OpcodeValidationSuccessWithInitCodeCost", + "CREATE2", + 74_528, + ), + ]; + for (test, opcode, gas) in cases { + let (success, created, used) = create_at(LOVELL, opcode, MAX_INITCODE); + assert!(success && created != U256::ZERO, "{test}"); + assert_eq!(used, gas, "{test}"); + } +} + +/// From RSKIP438 init code past 49,152 bytes fails the contract that runs +/// CREATE or CREATE2, with all its gas. +#[test] +fn from_rskip438_init_code_past_49152_bytes_fails_the_contract_that_creates() { + let cases = [ + ( + "testInitCodeSizeCREATEOpcodeValidationFailWithRSKIP438Activated", + "CREATE", + ), + ( + "testInitCodeSizeCREATE2OpcodeValidationFailIfRSKIP438Activated", + "CREATE2", + ), + ]; + for (test, opcode) in cases { + let (success, _, used) = create_at(LOVELL, opcode, MAX_INITCODE + 1); + assert!(!success, "{test}"); + assert_eq!(used, GAS, "{test}"); + } +} diff --git a/crates/execution/src/rskj_tests/vm/vm_execution_test.rs b/crates/execution/src/rskj_tests/vm/vm_execution_test.rs new file mode 100644 index 0000000..afb8b48 --- /dev/null +++ b/crates/execution/src/rskj_tests/vm/vm_execution_test.rs @@ -0,0 +1,323 @@ +//! rskj's `co.rsk.vm.VMExecutionTest`: opcodes on either side of the upgrade +//! that brings them, each run as a contract's code in a block of mainnet. +//! +//! rskj steps each program on a mocked program with the one rule under test +//! switched on or off; here the block's height switches it, at the first +//! block of its upgrade on mainnet and the block before it. + +use alloy_primitives::{hex, U256}; + +use super::harness::{ + call, compile, return_top, run_code, run_code_on, words, Chain, CONTRACT, GAS, ONE_RBTC, + RETURN_ONE_WORD_GAS, SENDER, +}; +use crate::hardfork::RskHardforkConfig; + +/// wasabi100 on mainnet: RSKIP120, the shifts. +const WASABI: u64 = 1_591_000; +/// papyrus200 on mainnet: RSKIP151, SELFBALANCE, and RSKIP152, CHAINID. +const PAPYRUS: u64 = 2_392_700; +/// papyrus200 on testnet. +const TESTNET_PAPYRUS: u64 = 863_000; +/// iris300 on mainnet: RSKIP174. +const IRIS: u64 = 3_614_800; +/// arrowhead600 on mainnet: RSKIP398, PUSH0. +const ARROWHEAD: u64 = 6_223_700; +/// lovell700 on mainnet: RSKIP445, MCOPY. +const LOVELL: u64 = 7_338_024; + +/// rskj's `executeShift`: `number` shifted by `shift` with `op`, as +/// `PUSH32 number PUSH1 shift op` (PUSH2 for a shift of two bytes), and +/// the word it leaves. +fn shifted(op: &str, number: U256, shift: &str) -> U256 { + let push = (shift.len() - 2) / 2; + let number = hex::encode(number.to_be_bytes::<32>()); + let code = compile(&format!("PUSH32 0x{number} PUSH{push} {shift} {op}")); + let ran = run_code(WASABI, &[code, return_top(1)].concat()); + assert!(ran.success, "{op} {shift} of 0x{number}"); + words(&ran.output)[0] +} + +/// 0x00..01, 0x80..00, 0x7f..ff and 0xff..ff, rskj's numbers to shift. +fn numbers() -> (U256, U256, U256, U256) { + let one = U256::from(1); + (one, one << 255, U256::MAX >> 1, U256::MAX) +} + +/// RSKIP120 (wasabi100): SHL shifts left as EIP-145 has it, by up to 255 +/// bits and to zero past them. Each row is one of rskj's tests. +#[test] +fn shl_shifts_left_as_eip145_has_it_from_rskip120() { + let (one, top, low_255, all) = numbers(); + for (test, number, shift, expected) in [ + ("testSHL1", one, "0x00", one), + ("testSHL2", one, "0x01", U256::from(2)), + ("testSHL3", one, "0xff", top), + ("testSHL4", one, "0x0100", U256::ZERO), + ("testSHL5", all, "0x00", all), + ("testSHL6", all, "0xff", top), + ("testSHL7", low_255, "0x01", all - U256::from(1)), + ] { + assert_eq!(shifted("SHL", number, shift), expected, "{test}"); + } +} + +/// RSKIP120 (wasabi100): SHR shifts right, filling with zeros. +#[test] +fn shr_shifts_right_as_eip145_has_it_from_rskip120() { + let (one, top, _, all) = numbers(); + for (test, number, shift, expected) in [ + ("testSHR1", one, "0x00", one), + ("testSHR2", one, "0x01", U256::ZERO), + ("testSHR3", top, "0x01", top >> 1), + ("testSHR4", top, "0xff", one), + ("testSHR5", all, "0x00", all), + ("testSHR6", all, "0xff", one), + ("testSHR7", top, "0x0100", U256::ZERO), + ] { + assert_eq!(shifted("SHR", number, shift), expected, "{test}"); + } +} + +/// RSKIP120 (wasabi100): SAR shifts right, filling with the sign bit. +#[test] +fn sar_shifts_right_keeping_the_sign_as_eip145_has_it_from_rskip120() { + let (one, top, low_255, all) = numbers(); + for (test, number, shift, expected) in [ + ("testSAR1", one, "0x00", one), + ("testSAR2", one, "0x01", U256::ZERO), + ("testSAR3", top, "0x01", U256::from(3) << 254), + ("testSAR4", top, "0xff", all), + ("testSAR5", all, "0x00", all), + ("testSAR6", all, "0x01", all), + ("testSAR7", low_255, "0xf8", U256::from(0x7f)), + ("testSAR8", low_255, "0x0100", U256::ZERO), + ] { + assert_eq!(shifted("SAR", number, shift), expected, "{test}"); + } +} + +/// Before RSKIP120 the shifts are no opcodes: rskj throws +/// `IllegalOperationException`, and the call fails with all of its gas. +#[test] +fn the_shifts_are_invalid_opcodes_before_rskip120() { + let all = "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"; + let one = "0x0000000000000000000000000000000000000000000000000000000000000001"; + for (test, code) in [ + ( + "testSAR3ShouldFailOnOldVersion", + format!("PUSH32 {all} PUSH1 0xff SAR"), + ), + ( + "testSHL1ShouldFailOnOldVersion", + format!("PUSH32 {one} PUSH1 0x01 SHL"), + ), + ( + "testSHR1ShouldFailOnOldVersion", + format!("PUSH32 {one} PUSH1 0x01 SHR"), + ), + ] { + let before = run_code(WASABI - 1, &compile(&code)); + assert!(!before.success, "{test}"); + assert_eq!(before.gas_used, GAS, "{test}: all the gas"); + let from = run_code(WASABI, &compile(&code)); + assert!(from.success, "{test} from RSKIP120"); + } +} + +/// RSKIP152 (papyrus200): CHAINID pushes the chain's id, 30 on mainnet, 31 +/// on testnet and 33 on a regtest (rskj's `Constants`). +#[test] +fn chainid_pushes_each_networks_chain_id_from_rskip152() { + let code = [compile("CHAINID"), return_top(1)].concat(); + for (test, config, number, id) in [ + ( + "chainIDMainnet", + RskHardforkConfig::mainnet(), + PAPYRUS, + 30u64, + ), + ( + "chainIDTestnet", + RskHardforkConfig::testnet(), + TESTNET_PAPYRUS, + 31, + ), + ("chainIDRegtest", RskHardforkConfig::regtest(), 1, 33), + ] { + let ran = run_code_on(config, number, &code); + assert!(ran.success, "{test}"); + assert_eq!(words(&ran.output), [U256::from(id)], "{test}"); + // BASE tier. + assert_eq!(ran.gas_used, 21_000 + 2 + RETURN_ONE_WORD_GAS, "{test}"); + } +} + +/// Before RSKIP151 SELFBALANCE is no opcode, and the call fails with all of +/// its gas. +#[test] +fn selfbalance_is_an_invalid_opcode_before_rskip151() { + let code = [compile("SELFBALANCE"), return_top(1)].concat(); + let before = run_code(PAPYRUS - 1, &code); + assert!(!before.success, "selfBalanceFailsWithRSKIPNotActivated"); + assert_eq!(before.gas_used, GAS); + assert!(run_code(PAPYRUS, &code).success); +} + +/// The output and the gas of a call to a contract holding 100 wei whose code +/// is `code`, from RSKIP151. +fn run_with_100_wei(code: &[u8]) -> (Vec, u64) { + let mut chain = Chain::mainnet(PAPYRUS); + chain.put(SENDER, 0, ONE_RBTC, &[]); + chain.put(CONTRACT, 1, U256::from(100), code); + let ran = chain.run(SENDER, call(0, CONTRACT, U256::ZERO, GAS, &[])); + assert!(ran.success); + (words(&ran.output), ran.gas_used) +} + +/// RSKIP151 (papyrus200): SELFBALANCE pushes the balance of the contract +/// that runs it, for the LOW tier's 5 gas. +#[test] +fn selfbalance_pushes_the_contracts_own_balance_for_5_gas() { + let (pushed, gas) = run_with_100_wei(&[compile("SELFBALANCE"), return_top(1)].concat()); + assert_eq!(pushed, [U256::from(100)], "selfBalanceRunsCorrectly"); + assert_eq!( + gas, + 21_000 + 5 + RETURN_ONE_WORD_GAS, + "selfBalanceRunsCorrectly" + ); +} + +/// SELFBALANCE pushes what BALANCE of the contract's own address pushes. +#[test] +fn selfbalance_pushes_what_balance_of_the_contract_pushes() { + let (selfbalance, gas) = run_with_100_wei(&[compile("SELFBALANCE"), return_top(1)].concat()); + let push_own_address = format!("PUSH20 0x{}", hex::encode(CONTRACT)); + let (balance, _) = run_with_100_wei( + &[ + compile(&format!("{push_own_address} BALANCE")), + return_top(1), + ] + .concat(), + ); + assert_eq!(selfbalance, [U256::from(100)], "selfBalanceIsSameAsBalance"); + assert_eq!(balance, selfbalance, "selfBalanceIsSameAsBalance"); + assert_eq!( + gas, + 21_000 + 5 + RETURN_ONE_WORD_GAS, + "selfBalanceIsSameAsBalance" + ); +} + +/// RSKIP398 (arrowhead600): PUSH0 pushes one word, zero, for the BASE +/// tier's 2 gas: the word pushed before it is the next one down. +#[test] +fn push0_pushes_one_zero_word_from_rskip398() { + let ran = run_code( + ARROWHEAD, + &[compile("PUSH1 0x2a PUSH0"), return_top(2)].concat(), + ); + assert!(ran.success, "testPUSH0Activation"); + assert_eq!( + words(&ran.output), + [U256::ZERO, U256::from(0x2a)], + "testPUSH0Activation" + ); + // PUSH1, PUSH0, and three PUSH2s, two MSTOREs and two words of memory. + assert_eq!(ran.gas_used, 21_000 + 3 + 2 + 24); +} + +/// RSKIP445 (lovell700): MCOPY of nothing runs and takes its three words off +/// the stack: the word pushed before them is the top one after it. +#[test] +fn mcopy_takes_its_three_words_off_the_stack_from_rskip445() { + let code = compile("PUSH1 0x2a PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 MCOPY"); + let ran = run_code(LOVELL, &[code, return_top(1)].concat()); + assert!(ran.success, "testMCOPY_WhenActive_ExecutesAsExpected"); + assert_eq!( + words(&ran.output), + [U256::from(0x2a)], + "testMCOPY_WhenActive_ExecutesAsExpected" + ); + // Four PUSH1s, and MCOPY's VERY_LOW 3 for no words copied. + assert_eq!(ran.gas_used, 21_000 + 12 + 3 + RETURN_ONE_WORD_GAS); +} + +/// Before RSKIP445 MCOPY is no opcode, and the call fails with all of its +/// gas. +#[test] +fn mcopy_is_an_invalid_opcode_before_rskip445() { + let ran = run_code( + LOVELL - 1, + &compile("PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 MCOPY"), + ); + assert!(!ran.success, "testMCOPY_WhenInactive_ExecutesAsExpected"); + assert_eq!( + ran.gas_used, GAS, + "testMCOPY_WhenInactive_ExecutesAsExpected" + ); +} + +/// A JUMP to where no JUMPDEST is fails, with all of the call's gas: rskj's +/// `BadJumpDestinationException`, for a destination just past the code, past +/// it, 2^256 - 1 (rskj's PC[-1]) and 255. +#[test] +fn a_jump_to_where_no_jumpdest_is_fails_with_all_the_gas() { + let all_ones = format!("0x{}", "ff".repeat(32)); + for (test, code) in [ + ("invalidJustAfterEndOfCode", "PUSH1 0x03 JUMP".to_string()), + ("invalidJumpOutOfRange", "PUSH1 0x05 JUMP".to_string()), + ("invalidNegativeJump", format!("PUSH32 {all_ones} JUMP")), + ("invalidTooFarJump", "PUSH1 0xff JUMP".to_string()), + ] { + let ran = run_code(LOVELL, &compile(&code)); + assert!(!ran.success, "{test}"); + assert_eq!(ran.gas_used, GAS, "{test}"); + } + // The jump that lands on a JUMPDEST runs on (testJumpSkippingInvalidJump). + let ran = run_code( + LOVELL, + &[ + compile("PUSH1 0x05 JUMP PUSH1 0xa0 JUMPDEST PUSH1 0x01"), + return_top(1), + ] + .concat(), + ); + assert!(ran.success); + assert_eq!(words(&ran.output), [U256::from(1)]); +} + +/// A CREATE from a contract onto an address that already holds a balance +/// keeps that balance, and adds the endowment to it, at every height: rskj's +/// `Program.createContract` creates the account carrying over what it held +/// (`track.createAccount(contractAddress, existingAccount)`), whatever is +/// active. Only a transaction's own creation burned it before RSKIP174. +#[test] +fn a_create_from_a_contract_keeps_the_balance_already_at_the_address_at_every_height() { + // Store 0x60 (PUSH1) as the init code's one byte, and CREATE with 10 wei. + let creator = compile( + "PUSH32 0x6000000000000000000000000000000000000000000000000000000000000000 PUSH1 0x00 MSTORE \ + PUSH1 0x01 PUSH1 0x00 PUSH1 0x0a CREATE", + ); + let created = CONTRACT.create(1); + for number in [1, IRIS - 1, IRIS] { + let mut chain = Chain::mainnet(number); + chain.put(SENDER, 0, ONE_RBTC, &[]); + chain.put( + CONTRACT, + 1, + U256::from(1_010), + &[creator.clone(), return_top(1)].concat(), + ); + chain.put(created, 0, U256::from(100), &[]); + let ran = chain.run(SENDER, call(0, CONTRACT, U256::ZERO, GAS, &[])); + assert!(ran.success, "#{number}"); + assert_eq!( + words(&ran.output), + [U256::from_be_slice(created.as_slice())], + "#{number}" + ); + assert_eq!(chain.balance(created), U256::from(110), "#{number}"); + assert_eq!(chain.balance(CONTRACT), U256::from(1_000), "#{number}"); + } +} diff --git a/crates/execution/src/rskj_tests/vm/vm_test.rs b/crates/execution/src/rskj_tests/vm/vm_test.rs new file mode 100644 index 0000000..b3f570c --- /dev/null +++ b/crates/execution/src/rskj_tests/vm/vm_test.rs @@ -0,0 +1,299 @@ +//! rskj's `org.ethereum.vm.VMTest`: the return data a call that runs no code +//! leaves its caller (RSKIP171), and the refusals, limits and error paths of +//! the rest of the class. rskj steps each program on a mocked program; the +//! port runs it as a contract's code in a block of mainnet. + +use alloy_primitives::{hex, Address, U256}; + +use super::harness::{ + call, compile, push_address, return_top, run_code, words, Chain, CONTRACT, GAS, ONE_RBTC, + SENDER, +}; +use crate::executor::TxExecutionResult; +use crate::precompiles::BLOCK_HEADER_ADDR; + +/// orchid060 on mainnet: RSKIP103. +const ORCHID060: u64 = 1_052_700; +/// iris300 on mainnet: RSKIP171. +const IRIS: u64 = 3_614_800; +/// rskj's mock contract, which the cases of the return data call first. +const RETURNER: Address = Address::repeat_byte(0x2e); +/// The contract a case's contract calls. +const CALLEE: Address = Address::repeat_byte(0xce); +/// The identity precompiled contract. +const IDENTITY: Address = Address::with_last_byte(4); +/// The gas rskj's cases ask for a call: 6,000,000, more than is left. +const CALL_GAS: &str = "PUSH4 0x005b8d80"; + +/// What a transaction of [`GAS`] gets from calling [`CONTRACT`], whose code +/// is `code` and which holds 1,000 wei, in block `number` of mainnet, with +/// the contracts `others` there too. +fn run_with(number: u64, code: &[u8], others: &[(Address, &[u8])]) -> TxExecutionResult { + let mut chain = Chain::mainnet(number); + chain.put(SENDER, 0, ONE_RBTC, &[]); + chain.put(CONTRACT, 1, U256::from(1_000), code); + for (address, code) in others { + chain.put(*address, 1, U256::ZERO, code); + } + chain.run(SENDER, call(0, CONTRACT, U256::ZERO, GAS, &[])) +} + +/// [`run_with`], [`RETURNER`] there, a program that STATICCALLs it, which +/// leaves the word 0x15 (0x10 + 0x05) as the return data and at 0x40, then +/// runs `then` and ends with `end`. +fn after_a_word_returned(number: u64, then: &str, end: &[u8]) -> TxExecutionResult { + let returner = + compile("PUSH1 0x10 PUSH1 0x05 ADD PUSH1 0x40 MSTORE PUSH1 0x20 PUSH1 0x40 RETURN"); + let first = format!( + "PUSH1 0x20 PUSH1 0x40 PUSH1 0x00 PUSH1 0x00 {} {CALL_GAS} STATICCALL", + push_address(RETURNER) + ); + let code = [compile(&format!("{first} {then}")), end.to_vec()].concat(); + run_with(number, &code, &[(RETURNER, &returner)]) +} + +/// From RSKIP171 a STATICCALL of an account with no code (address 0) leaves +/// its caller no return data: RETURNDATASIZE reads 0, and RETURNDATACOPY of +/// a word fails, with all the gas. Before it the caller keeps the call +/// before's 32 bytes, the word 0x15. +#[test] +fn a_static_call_of_no_code_leaves_no_return_data_from_rskip171() { + let nothing = + format!("PUSH1 0x20 PUSH1 0x40 PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 {CALL_GAS} STATICCALL"); + let size = format!("{nothing} RETURNDATASIZE"); + let copy = format!("{nothing} PUSH1 0x20 PUSH1 0x00 PUSH1 0x40 RETURNDATACOPY"); + let copied = compile("PUSH1 0x20 PUSH1 0x40 RETURN"); + + let ran = after_a_word_returned(IRIS - 1, &size, &return_top(1)); + let test = "beforeIrisReturnDataSizeAfterCallToNonExistentContract"; + assert_eq!(words(&ran.output), [U256::from(32)], "{test}"); + let ran = after_a_word_returned(IRIS - 1, ©, &copied); + let test = "beforeIrisReturnDataBufferAfterCallToNonExistentContract"; + assert_eq!(words(&ran.output), [U256::from(0x15)], "{test}"); + + let ran = after_a_word_returned(IRIS, &size, &return_top(1)); + let test = "returnDataSizeAfterCallToNonExistentContract"; + assert_eq!(words(&ran.output), [U256::ZERO], "{test}"); + let ran = after_a_word_returned(IRIS, ©, &copied); + let test = "returnDataBufferAfterCallToNonExistentContract"; + assert!(!ran.success, "{test}"); + assert_eq!(ran.gas_used, GAS, "{test}"); +} + +/// A CALL whose value its caller cannot pay runs nothing. From RSKIP171 it +/// leaves the caller no return data, and before it the call before's 32 +/// bytes, whether the callee is a contract or a precompiled contract (the +/// block header contract, RSKIP119's). +#[test] +fn a_call_its_caller_cannot_pay_leaves_no_return_data_from_rskip171() { + for (callee, case) in [ + (RETURNER, "InsufficientFunds"), + (BLOCK_HEADER_ADDR, "InsufficientFundsAtPrecompiled"), + ] { + // 0xffffffff wei, of the 1,000 the caller holds. + let to = push_address(callee); + let unpaid = format!( + "PUSH1 0x20 PUSH1 0x40 PUSH1 0x00 PUSH1 0x00 PUSH4 0xffffffff {to} + {CALL_GAS} CALL RETURNDATASIZE" + ); + let ran = after_a_word_returned(IRIS - 1, &unpaid, &return_top(1)); + let test = format!("beforeIrisReturnDataBufferWith{case}"); + assert_eq!(words(&ran.output), [U256::from(32)], "{test}"); + let ran = after_a_word_returned(IRIS, &unpaid, &return_top(1)); + let test = format!("returnDataBufferAfter{case}"); + assert_eq!(words(&ran.output), [U256::ZERO], "{test}"); + } +} + +/// A STATICCALL's callee that would change state fails, and the STATICCALL +/// pushes 0: one that SSTOREs, LOGs with each count of topics, CREATEs or +/// SELFDESTRUCTs, each given the words it takes. The same callee run by a +/// CALL succeeds, and a STATICCALL of a callee that only computes pushes 1. +/// Each call is given 200,000 gas, which a callee that fails spends, so that +/// its caller has gas left to return what the call pushed. +#[test] +fn a_static_calls_callee_that_would_change_state_fails() { + let mut changing = vec!["PUSH1 0x01 PUSH1 0x02 SSTORE".to_string()]; + let topics = |n: usize| "PUSH1 0x01 ".repeat(n); + changing.extend((0..5).map(|n| format!("{}PUSH1 0x01 PUSH1 0x02 LOG{n}", topics(n)))); + changing.push("PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 CREATE".to_string()); + changing.push("PUSH1 0x2b SELFDESTRUCT".to_string()); + let callee = push_address(CALLEE); + let zeros = "PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 PUSH1 0x00"; + let statically = compile(&format!("{zeros} {callee} PUSH3 0x030d40 STATICCALL")); + let statically = [statically, return_top(1)].concat(); + let plainly = compile(&format!("{zeros} PUSH1 0x00 {callee} PUSH3 0x030d40 CALL")); + let plainly = [plainly, return_top(1)].concat(); + for code in &changing { + let others = [(CALLEE, &compile(code)[..])]; + let ran = run_with(IRIS, &statically, &others); + let test = "testSTATICCALLWithStatusZeroUsingOpCode"; + assert_eq!(words(&ran.output), [U256::ZERO], "{test}: {code}"); + let ran = run_with(IRIS, &plainly, &others); + assert_eq!(words(&ran.output), [U256::from(1)], "{code}, by a CALL"); + } + let computing = compile("PUSH1 0x01 PUSH1 0x02 SUB"); + let ran = run_with(IRIS, &statically, &[(CALLEE, &computing)]); + let test = "testSTATICCALLWithStatusOne"; + assert_eq!(words(&ran.output), [U256::from(1)], "{test}"); +} + +/// Before RSKIP103 (orchid060) STATICCALL takes seven words, a value among +/// them as CALL's: of six it fails for want of the seventh, with all the +/// gas, and of seven it leaves only its success. From RSKIP103 it takes six, +/// and of seven leaves the first under its success. +#[test] +fn staticcall_takes_seven_words_before_rskip103_and_six_from_it() { + let callee = compile("PUSH1 0x01 PUSH1 0x02 SUB"); + let others = [(CALLEE, &callee[..])]; + let six = format!( + "PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 {} {CALL_GAS} STATICCALL", + push_address(CALLEE) + ); + let seven = compile(&format!("PUSH1 0x2a {six}")); + + let ran = run_with(ORCHID060 - 1, &compile(&six), &others); + let test = "testSTATICCALLWithStatusOneFailsWithOldCode"; + assert!(!ran.success, "{test}"); + assert_eq!(ran.gas_used, GAS, "{test}"); + + let test = "testSTATICCALLWithStatusOneAndAdditionalValueInStackUsingPreFixStaticCall"; + let one = [seven.clone(), return_top(1)].concat(); + let ran = run_with(ORCHID060 - 1, &one, &others); + assert_eq!(words(&ran.output), [U256::from(1)], "{test}"); + // Nothing is left under the success to return. + let two = [seven, return_top(2)].concat(); + let ran = run_with(ORCHID060 - 1, &two, &others); + assert!(!ran.success, "{test}"); + + let ran = run_with(ORCHID060, &two, &others); + let test = "testSTATICCALLWithStatusOneAndAdditionalValueInStackUsingFixStaticCall"; + let left = [U256::from(1), U256::from(0x2a)]; + assert_eq!(words(&ran.output), left, "{test}"); +} + +/// A CALL that asks more gas than is left gives the callee all of it, as +/// rskj holds none back (there is no EIP-150 64th): a callee that fails, here +/// for want of the words ADD takes, leaves its caller none, and the +/// transaction uses all of its gas. +#[test] +fn a_call_asking_more_gas_than_is_left_gives_the_callee_all_of_it() { + let broken = compile("ADD"); + let code = compile(&format!( + "PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 PUSH1 0x01 {} PUSH8 0x7fffffffffffffff CALL", + push_address(CALLEE) + )); + let ran = run_with(IRIS, &code, &[(CALLEE, &broken)]); + assert!(ran.success, "testCALLWithBigUserSpecifiedGas"); + assert_eq!(ran.gas_used, GAS, "testCALLWithBigUserSpecifiedGas"); +} + +/// A LOG of 2^59 bytes, whose data cost is past the 2^62 - 1 rskj's gas +/// once stopped at, runs out of gas, with all of it. +#[test] +fn a_log_of_more_data_than_its_gas_pays_for_runs_out_of_gas() { + let ran = run_code(IRIS, &compile("PUSH8 0x0800000000000000 PUSH1 0x00 LOG0")); + let test = "testLOGWithDataCostBiggerThanPreviousGasSize"; + assert!(!ran.success, "{test}"); + assert_eq!(ran.gas_used, GAS, "{test}"); +} + +/// A copy of no bytes expands no memory and is charged none. RETURNDATACOPY +/// of none to 0x20 costs 3 gas less than of the word (its copy), the RETURN +/// of 0x20 to 0x40 after it paying the memory either way; CALLDATACOPY of +/// none to the memory's last byte leaves the memory at a word, where of a +/// word it grows to two. +#[test] +fn a_copy_of_no_bytes_expands_no_memory() { + let one = compile("PUSH1 0x01 PUSH1 0x02 SUB PUSH1 0x00 MSTORE PUSH1 0x20 PUSH1 0x00 RETURN"); + let others = [(CALLEE, &one[..])]; + let callee = push_address(CALLEE); + let return_data_copy = |len: u8| { + let code = format!( + "PUSH1 0x20 PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 {callee} PUSH1 0xff STATICCALL + PUSH1 0x{len:02x} PUSH1 0x00 PUSH1 0x20 RETURNDATACOPY PUSH1 0x20 PUSH1 0x20 RETURN" + ); + run_with(IRIS, &compile(&code), &others) + }; + let (word, none) = (return_data_copy(0x20), return_data_copy(0)); + let test = "testReturnDataCopyChargesCorrectGas"; + assert_eq!(words(&word.output), [U256::from(1)], "{test}"); + assert_eq!(words(&none.output), [U256::ZERO], "{test}"); + assert_eq!(word.gas_used, none.gas_used + 3, "{test}"); + + let call_data_copy = |len: u8| { + let code = format!( + "PUSH1 0x20 PUSH1 0x00 PUSH1 0x00 PUSH1 0x00 {callee} PUSH2 0xffff STATICCALL + PUSH1 0x{len:02x} PUSH1 0x00 PUSH1 0x01 MSIZE SUB CALLDATACOPY MSIZE" + ); + run_with(IRIS, &[compile(&code), return_top(1)].concat(), &others) + }; + let test = "testCallDataCopyDoesNotExpandMemoryForFree"; + let (none, word) = (call_data_copy(0), call_data_copy(0x20)); + assert_eq!(words(&none.output), [U256::from(32)], "{test}"); + assert_eq!(words(&word.output), [U256::from(64)], "{test}"); +} + +/// A call of a precompiled contract pays memory only for the room its +/// caller gave the output: identity's output, given no room at 0x20, takes a +/// word less memory than given a word's room there, for 3 gas less; given +/// no room at 0x01000000, it takes none, as given a byte's room at 0. +#[test] +fn a_precompiles_caller_pays_memory_only_for_the_room_it_gave_the_output() { + let identity = push_address(IDENTITY); + // A CALL of identity on `input` bytes at 0, with room for `out` bytes of + // its output where `at` pushes; then MSIZE. + let calling = |at: &str, out: u8, input: u8| { + let code = format!( + "PUSH1 0x00 PUSH1 0x00 PUSH1 0x{out:02x} {at} PUSH1 0x{input:02x} PUSH1 0x00 + PUSH1 0x00 {identity} {CALL_GAS} CALL MSIZE" + ); + run_with(IRIS, &[compile(&code), return_top(1)].concat(), &[]) + }; + let lying = calling("PUSH1 0x20", 0, 0x01); + let honest = calling("PUSH1 0x20", 0x20, 0x20); + let test = "getFreeMemoryUsingPrecompiledContractLyingAboutReturnSize"; + assert_eq!(words(&lying.output), [U256::from(32)], "{test}"); + assert_eq!(words(&honest.output), [U256::from(64)], "{test}"); + assert_eq!(honest.gas_used, lying.gas_used + 3, "{test}"); + let far = calling("PUSH4 0x01000000", 0, 0x01); + let near = calling("PUSH1 0x00", 0x01, 0x01); + let test = "getFreeMemoryUsingPrecompiledContractAndSettingFarOffOffset"; + assert_eq!(words(&far.output), [U256::from(32)], "{test}"); + assert_eq!(words(&near.output), [U256::from(32)], "{test}"); + assert_eq!(far.gas_used, near.gas_used, "{test}"); +} + +/// A program short of the words its opcode takes, jumping where no JUMPDEST +/// is, or running a byte that is no opcode fails, with all of its gas: +/// rskj's StackTooSmallException, BadJumpDestinationException and +/// IllegalOperationException. +#[test] +fn a_program_short_of_words_or_jumping_nowhere_fails_with_all_the_gas() { + for (test, code) in [ + ("testDUPN_2", "80"), + ("testSWAPN_2", "90"), + ("testMSTORE_5", "61123452"), + ("testMLOAD_6", "51"), + ("testJUMPI_3", "600157"), + ("testEXP_4", "621234560a"), + ( + "testCODECOPY_6", + "605e6007396000605f556014600054601e60205463abcddcba6040545b51602001600a5254516040\ + 016014525451606001601e5254516080016028525460a052546016604860003960166000f26000\ + 603f556103e756600054600053602002351234", + ), + ( + "testEXTCODECOPY_5", + "605e600773471fd3ad3e9eeadeec4608b92d16ce6b500704cc3c", + ), + ("testJUMP_1", "60aa60bb600e5660cc60dd60ee5b60ff"), + ("testJUMPI_4", "60016022909057"), + ("testJUMPDEST_1", "602360085660015b600255"), + ("testInvalidOpcodes_1", "a5"), + ] { + let ran = run_code(IRIS, &hex::decode(code).expect("hex")); + assert!(!ran.success, "{test}"); + assert_eq!(ran.gas_used, GAS, "{test}"); + } +} diff --git a/crates/execution/src/sender_cache.rs b/crates/execution/src/sender_cache.rs index 928e051..c108108 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/execution/src/silent.rs b/crates/execution/src/silent.rs index de24219..856cbc4 100644 --- a/crates/execution/src/silent.rs +++ b/crates/execution/src/silent.rs @@ -185,6 +185,22 @@ pub fn is_recording() -> bool { SINK.with(|s| s.borrow().is_some()) } +/// How many records this thread holds: where what is recorded next begins, +/// for [`truncate`]. +pub fn len() -> usize { + SINK.with(|s| s.borrow().as_ref().map_or(0, Vec::len)) +} + +/// Takes back what was recorded since [`len`] said `len`: the decisions made +/// under a frame that failed, which stand no more than its writes do. +pub fn truncate(len: usize) { + SINK.with(|s| { + if let Some(v) = s.borrow_mut().as_mut() { + v.truncate(len); + } + }); +} + /// Records one decision. A no-op unless a tool called [`start`]. pub fn record( kind: SilentKind, @@ -242,6 +258,23 @@ mod tests { assert!(!is_recording()); } + #[test] + fn what_was_recorded_since_a_length_can_be_taken_back() { + assert_eq!(len(), 0, "nothing is held while nobody records"); + start(); + record(SilentKind::PeginAccepted, 8, 0, vec![]); + let mark = len(); + record(SilentKind::PeginRefunded, 8, 1, vec![]); + record(SilentKind::PegoutRequested, 8, 1, vec![]); + assert_eq!(len(), 3); + truncate(mark); + let kept = finish(); + assert_eq!(kept.len(), 1); + assert_eq!(kept[0].kind, SilentKind::PeginAccepted); + truncate(0); + assert!(!is_recording(), "taking back records starts no recording"); + } + #[test] fn a_record_takes_the_position_set_for_it() { start(); diff --git a/crates/execution/src/state.rs b/crates/execution/src/state.rs index ad738d1..5b2aa7f 100644 --- a/crates/execution/src/state.rs +++ b/crates/execution/src/state.rs @@ -34,6 +34,21 @@ 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, + /// 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. @@ -86,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 { @@ -152,6 +171,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 +506,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/execution/src/testdata/rskj_precompiles_called.txt b/crates/execution/src/testdata/rskj_precompiles_called.txt new file mode 100644 index 0000000..59f2526 --- /dev/null +++ b/crates/execution/src/testdata/rskj_precompiles_called.txt @@ -0,0 +1,322 @@ +# Which precompiles RSKj's release VETIVER-9.0.4 counts a transaction as having called +# (TransactionExecutor.precompiledContractsCalled), the set RSKIP144 reads: a transaction +# that called REMASC or the Bridge may only sit in a block's sequential sublist +# (TransactionListExecutor, BlockExecutor). Each scenario runs one transaction through +# TransactionExecutor (rskj-core-9.0.4-VETIVER-all.jar, regtest, block 10) over the +# accounts it lists, the sender's first, as BlockExecutor runs a block's sequential sublist. +# The lines are those of bridge/testdata/rskj_refunded_pegins.txt, and after each result: +# called the precompiles counted as called, by address, or a hyphen for none +block 10 67c280 1700000000 0101010101010101010101010101010101010101 1 +scenario direct-bridge +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account 0000000000000000000000000000000001000008 0 0 - +account 0000000000000000000000000000000000000004 0 0 - +preroot 763e810364c1938f9fe44a108a5c8311bb07318f5a305370447748d6e03b43b8 +tx f8648000831e848094000000000000000000000000000000000100000680846923fa8565a01ee12f1ebf61703c05b8f27aefbfe7b52fda16593129a0c77444ef536a61bad4a06cf32eab8b50a5ea09fb6585fba220b4a766a434d161a5b340c8a5d1b40e56ee d06da76e0b65671fc4639b624338464ed7b52bbd 2cde3cde47c81936d4c5d338c671a9e4c44c1e3275f61e792ad8ed0c89382dfb +result 1 1 32072 +called 0000000000000000000000000000000001000006 +root cb1fbd2434e4607c9106ab3aa8a70677412f97c4dbd3b0c1f8f4c7c3b1f55ce8 +end +scenario direct-remasc +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account 0000000000000000000000000000000001000008 0 0 - +account 0000000000000000000000000000000000000004 0 0 - +preroot 763e810364c1938f9fe44a108a5c8311bb07318f5a305370447748d6e03b43b8 +tx f8608000831e8480940000000000000000000000000000000001000008808066a061c265aa672e941df4ddf59d582244911f1e8b49589d04fbfe01adc2cfc4cbb9a05f22776d93903eba1ae0784e29da20c13b0e5e7a1cbab2ab1784d2693a12d5d9 d06da76e0b65671fc4639b624338464ed7b52bbd 1117c181de1a5b824becae7814dcdb269e8b961fc7182626ef968102f5c28603 +result 1 1 21000 +called 0000000000000000000000000000000001000008 +root cb1fbd2434e4607c9106ab3aa8a70677412f97c4dbd3b0c1f8f4c7c3b1f55ce8 +end +scenario calls-bridge +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account 0000000000000000000000000000000001000008 0 0 - +account 0000000000000000000000000000000000000004 0 0 - +account 00000000000000000000000000000000000000a1 0 0 7f6923fa85000000000000000000000000000000000000000000000000000000006000526000600060046000600063010000065af15000 +preroot fa56c5dedefa4454d3fd7984eeb81cef161ed6d79d17860e3b0c63b2268e5268 +tx f8608000831e84809400000000000000000000000000000000000000a1808065a0b28505fcbdf2c68ae81943a3229e0dcff6a24b63cf137ad9c2d53336573b0701a07801cf62d9f6710c52954d4be17f5723f7ff417af026b30cbfaa9b28e5e1bc3f d06da76e0b65671fc4639b624338464ed7b52bbd c30fd37840c98439991f02ec2169ea1f0a2a3ac29c9ca9b82f6e020b0a4f333b +result 1 1 32742 +called 0000000000000000000000000000000001000006 +root bb0c1bfc17e147a88c22bc1e3079ec08c1fa467441128238d2526f1a0fab420a +end +scenario calls-bridge-unpaid +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account 0000000000000000000000000000000001000008 0 0 - +account 0000000000000000000000000000000000000004 0 0 - +account 00000000000000000000000000000000000000a1 0 0 7f6923fa85000000000000000000000000000000000000000000000000000000006000526000600060046000600163010000065af15000 +preroot 2f253323e27035cbf57dfb18c8d28fd0b9b5db9b4f99a30bd8687a222d943fe6 +tx f8608000831e84809400000000000000000000000000000000000000a1808065a0b28505fcbdf2c68ae81943a3229e0dcff6a24b63cf137ad9c2d53336573b0701a07801cf62d9f6710c52954d4be17f5723f7ff417af026b30cbfaa9b28e5e1bc3f d06da76e0b65671fc4639b624338464ed7b52bbd c30fd37840c98439991f02ec2169ea1f0a2a3ac29c9ca9b82f6e020b0a4f333b +result 1 1 30734 +called 0000000000000000000000000000000001000006 +root 988e83fd48e167d14aa4e851e554c84570fc0984f88ed7a5feb2b9e22bf049d2 +end +scenario calls-bridge-paid +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account 0000000000000000000000000000000001000008 0 0 - +account 0000000000000000000000000000000000000004 0 0 - +account 00000000000000000000000000000000000000a1 0 5 7f6923fa85000000000000000000000000000000000000000000000000000000006000526000600060046000600163010000065af15000 +preroot 3e9da4772a60e9ddab2df7b977f790d73066d1bb84bbe361c6608d681e89a929 +tx f8608000831e84809400000000000000000000000000000000000000a1808065a0b28505fcbdf2c68ae81943a3229e0dcff6a24b63cf137ad9c2d53336573b0701a07801cf62d9f6710c52954d4be17f5723f7ff417af026b30cbfaa9b28e5e1bc3f d06da76e0b65671fc4639b624338464ed7b52bbd c30fd37840c98439991f02ec2169ea1f0a2a3ac29c9ca9b82f6e020b0a4f333b +result 1 1 41742 +called 0000000000000000000000000000000001000006 +root 8676171be25927d46f76ceeb843690fd9acdedd7b3ce8df1360acb88804c2080 +end +scenario calls-remasc-unpaid +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account 0000000000000000000000000000000001000008 0 0 - +account 0000000000000000000000000000000000000004 0 0 - +account 00000000000000000000000000000000000000a1 0 0 6000600060006000600163010000085af15000 +preroot fef17447a1655a33991d70c70a3cc23a280822578bf20231530ebba50610f1e4 +tx f8608000831e84809400000000000000000000000000000000000000a1808065a0b28505fcbdf2c68ae81943a3229e0dcff6a24b63cf137ad9c2d53336573b0701a07801cf62d9f6710c52954d4be17f5723f7ff417af026b30cbfaa9b28e5e1bc3f d06da76e0b65671fc4639b624338464ed7b52bbd c30fd37840c98439991f02ec2169ea1f0a2a3ac29c9ca9b82f6e020b0a4f333b +result 1 1 30722 +called 0000000000000000000000000000000001000008 +root 88fbf745b49aa4d743aae8547e3f5e92cbc07dcce48686c7f92811f4bc447038 +end +scenario delegates-bridge +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account 0000000000000000000000000000000001000008 0 0 - +account 0000000000000000000000000000000000000004 0 0 - +account 00000000000000000000000000000000000000a1 0 0 7f6923fa8500000000000000000000000000000000000000000000000000000000600052600060006004600063010000065af45000 +preroot 3c429cd3adc90bbfbec5c05270ea0b98be231bfe387fe87402083ab509d2d8e0 +tx f8608000831e84809400000000000000000000000000000000000000a1808065a0b28505fcbdf2c68ae81943a3229e0dcff6a24b63cf137ad9c2d53336573b0701a07801cf62d9f6710c52954d4be17f5723f7ff417af026b30cbfaa9b28e5e1bc3f d06da76e0b65671fc4639b624338464ed7b52bbd c30fd37840c98439991f02ec2169ea1f0a2a3ac29c9ca9b82f6e020b0a4f333b +result 1 1 32739 +called 0000000000000000000000000000000001000006 +root b877b8a06b2accd1d6a37200ca29172583dc61a4cc1571dfbe2e383a29a72fd9 +end +scenario staticcalls-bridge +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account 0000000000000000000000000000000001000008 0 0 - +account 0000000000000000000000000000000000000004 0 0 - +account 00000000000000000000000000000000000000a1 0 0 7f6923fa8500000000000000000000000000000000000000000000000000000000600052600060006004600063010000065afa5000 +preroot 66b538d6fa650901f65d953c681b06048fae019d50160002b37b9489991d1c79 +tx f8608000831e84809400000000000000000000000000000000000000a1808065a0b28505fcbdf2c68ae81943a3229e0dcff6a24b63cf137ad9c2d53336573b0701a07801cf62d9f6710c52954d4be17f5723f7ff417af026b30cbfaa9b28e5e1bc3f d06da76e0b65671fc4639b624338464ed7b52bbd c30fd37840c98439991f02ec2169ea1f0a2a3ac29c9ca9b82f6e020b0a4f333b +result 1 1 32739 +called 0000000000000000000000000000000001000006 +root 0c3479c1dcb8f8d14193747a5b8273e2c6b7352bbdff8e3c81fd4b9dda23f1b7 +end +scenario callcodes-bridge-unpaid +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account 0000000000000000000000000000000001000008 0 0 - +account 0000000000000000000000000000000000000004 0 0 - +account 00000000000000000000000000000000000000a1 0 0 7f6923fa85000000000000000000000000000000000000000000000000000000006000526000600060046000600163010000065af25000 +preroot 30e45e9829350488ab2fa543bd02fe0a136188e72a44b9aee8c9bdfd28d96116 +tx f8608000831e84809400000000000000000000000000000000000000a1808065a0b28505fcbdf2c68ae81943a3229e0dcff6a24b63cf137ad9c2d53336573b0701a07801cf62d9f6710c52954d4be17f5723f7ff417af026b30cbfaa9b28e5e1bc3f d06da76e0b65671fc4639b624338464ed7b52bbd c30fd37840c98439991f02ec2169ea1f0a2a3ac29c9ca9b82f6e020b0a4f333b +result 1 1 30734 +called 0000000000000000000000000000000001000006 +root 67622cf9958d9167c5f9034f55c5b15aac7c0968b83176c34438fb6e6e0386e5 +end +scenario calls-bridge-via-callee +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account 0000000000000000000000000000000001000008 0 0 - +account 0000000000000000000000000000000000000004 0 0 - +account 00000000000000000000000000000000000000a1 0 0 6000600060006000600060d15af15000 +account 00000000000000000000000000000000000000d1 0 0 7f6923fa85000000000000000000000000000000000000000000000000000000006000526000600060046000600063010000065af15000 +preroot 4433a8d3e18550f20626bbb726c6c6b04b07318dd1b51995b18286bf99f3f3dd +tx f8608000831e84809400000000000000000000000000000000000000a1808065a0b28505fcbdf2c68ae81943a3229e0dcff6a24b63cf137ad9c2d53336573b0701a07801cf62d9f6710c52954d4be17f5723f7ff417af026b30cbfaa9b28e5e1bc3f d06da76e0b65671fc4639b624338464ed7b52bbd c30fd37840c98439991f02ec2169ea1f0a2a3ac29c9ca9b82f6e020b0a4f333b +result 1 1 33464 +called 0000000000000000000000000000000001000006 +root c2a9e04d3636fd4374efcce3228761a58e571296e8507c584f68785ec66c2c8a +end +scenario calls-reverting-caller-of-bridge +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account 0000000000000000000000000000000001000008 0 0 - +account 0000000000000000000000000000000000000004 0 0 - +account 00000000000000000000000000000000000000a1 0 0 6000600060006000600060b15af15000 +account 00000000000000000000000000000000000000b1 0 0 7f6923fa85000000000000000000000000000000000000000000000000000000006000526000600060046000600063010000065af15060006000fd +preroot 12809dc42d11c854a5734bbfbf789f537a99431aa481b73d0fa89c0838a0d6d2 +tx f8608000831e84809400000000000000000000000000000000000000a1808065a0b28505fcbdf2c68ae81943a3229e0dcff6a24b63cf137ad9c2d53336573b0701a07801cf62d9f6710c52954d4be17f5723f7ff417af026b30cbfaa9b28e5e1bc3f d06da76e0b65671fc4639b624338464ed7b52bbd c30fd37840c98439991f02ec2169ea1f0a2a3ac29c9ca9b82f6e020b0a4f333b +result 1 1 33470 +called 0000000000000000000000000000000001000006 +root b54e7b4e40f9fef77a189f64dfd68cb41fe47afebbd7f9056ad1c6f46963f07e +end +scenario static-value-to-bridge +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account 0000000000000000000000000000000001000008 0 0 - +account 0000000000000000000000000000000000000004 0 0 - +account 00000000000000000000000000000000000000a1 0 0 600060006000600060b15afa5000 +account 00000000000000000000000000000000000000b1 0 5 7f6923fa85000000000000000000000000000000000000000000000000000000006000526000600060046000600163010000065af15000 +preroot ab0c2e5cf48c5b91ae0062e81cec04b2287a2607960e4d538a22b5851d1685c6 +tx f8608000831e84809400000000000000000000000000000000000000a1808065a0b28505fcbdf2c68ae81943a3229e0dcff6a24b63cf137ad9c2d53336573b0701a07801cf62d9f6710c52954d4be17f5723f7ff417af026b30cbfaa9b28e5e1bc3f d06da76e0b65671fc4639b624338464ed7b52bbd c30fd37840c98439991f02ec2169ea1f0a2a3ac29c9ca9b82f6e020b0a4f333b +result 1 0 2000000 +called - +root e4c4662db6dae857d9c6713a1fa197598686fc36f859fe1327c1ed16be13487e +end +scenario calls-identity +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account 0000000000000000000000000000000001000008 0 0 - +account 0000000000000000000000000000000000000004 0 0 - +account 00000000000000000000000000000000000000a1 0 0 7f6923fa85000000000000000000000000000000000000000000000000000000006000526000600060046000600060045af15000 +preroot 3ac09a2757d4b1823d6a560ea73e30bde63ca0ceaa68264a58482c4b146712d1 +tx f8608000831e84809400000000000000000000000000000000000000a1808065a0b28505fcbdf2c68ae81943a3229e0dcff6a24b63cf137ad9c2d53336573b0701a07801cf62d9f6710c52954d4be17f5723f7ff417af026b30cbfaa9b28e5e1bc3f d06da76e0b65671fc4639b624338464ed7b52bbd c30fd37840c98439991f02ec2169ea1f0a2a3ac29c9ca9b82f6e020b0a4f333b +result 1 1 21752 +called 0000000000000000000000000000000000000004 +root 870fca3695a940b9e498d41302e03117e235ff4379b8a479f9b4066c3c60ddd8 +end +scenario creates-child-calling-bridge +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account 0000000000000000000000000000000001000008 0 0 - +account 0000000000000000000000000000000000000004 0 0 - +account 00000000000000000000000000000000000000a1 0 0 60376010600039603760006000f050007f6923fa85000000000000000000000000000000000000000000000000000000006000526000600060046000600063010000065af15000 +preroot c623800f6d834cc9a078514537f88180e5dacb5b27df9afdf2ce81b7334051e0 +tx f8608000831e84809400000000000000000000000000000000000000a1808065a0b28505fcbdf2c68ae81943a3229e0dcff6a24b63cf137ad9c2d53336573b0701a07801cf62d9f6710c52954d4be17f5723f7ff417af026b30cbfaa9b28e5e1bc3f d06da76e0b65671fc4639b624338464ed7b52bbd c30fd37840c98439991f02ec2169ea1f0a2a3ac29c9ca9b82f6e020b0a4f333b +result 1 1 64781 +called - +root c64268616399e92b88d6105d2708a38abd7b9e425e5ed877fbe2a3ce2de4c533 +end +scenario creates2-child-calling-bridge +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account 0000000000000000000000000000000001000008 0 0 - +account 0000000000000000000000000000000000000004 0 0 - +account 00000000000000000000000000000000000000a1 0 0 603760126000396000603760006000f550007f6923fa85000000000000000000000000000000000000000000000000000000006000526000600060046000600063010000065af15000 +preroot 87360683646abfc451899c2596b8e2ef2a8df0b22f04be36feef4a775d708372 +tx f8608000831e84809400000000000000000000000000000000000000a1808065a0b28505fcbdf2c68ae81943a3229e0dcff6a24b63cf137ad9c2d53336573b0701a07801cf62d9f6710c52954d4be17f5723f7ff417af026b30cbfaa9b28e5e1bc3f d06da76e0b65671fc4639b624338464ed7b52bbd c30fd37840c98439991f02ec2169ea1f0a2a3ac29c9ca9b82f6e020b0a4f333b +result 1 1 64796 +called - +root 18d9ac8923c66870b025a739f2b9b9b2ada52c7c5825c0072f2685960d5fbdcb +end +scenario creates-child-calling-caller-of-bridge +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account 0000000000000000000000000000000001000008 0 0 - +account 0000000000000000000000000000000000000004 0 0 - +account 00000000000000000000000000000000000000a1 0 0 60106010600039601060006000f050006000600060006000600060d15af15000 +account 00000000000000000000000000000000000000d1 0 0 7f6923fa85000000000000000000000000000000000000000000000000000000006000526000600060046000600063010000065af15000 +preroot b7ae02ddf04bcafad60dfee1f4cadccf24132b4a6e87b5fbd3f79d55bc7a1fba +tx f8608000831e84809400000000000000000000000000000000000000a1808065a0b28505fcbdf2c68ae81943a3229e0dcff6a24b63cf137ad9c2d53336573b0701a07801cf62d9f6710c52954d4be17f5723f7ff417af026b30cbfaa9b28e5e1bc3f d06da76e0b65671fc4639b624338464ed7b52bbd c30fd37840c98439991f02ec2169ea1f0a2a3ac29c9ca9b82f6e020b0a4f333b +result 1 1 65495 +called - +root ad838dda393e73b37391da2c6b3bff036952f1486db361e29fc272c763c29264 +end +scenario creates-child-calling-bridge-reverting +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account 0000000000000000000000000000000001000008 0 0 - +account 0000000000000000000000000000000000000004 0 0 - +account 00000000000000000000000000000000000000a1 0 0 603b6010600039603b60006000f050007f6923fa85000000000000000000000000000000000000000000000000000000006000526000600060046000600063010000065af15060006000fd +preroot 777210544f3b87d319e9b0a9a94273e75fe7f0570ae15dd3fbf4a620270760af +tx f8608000831e84809400000000000000000000000000000000000000a1808065a0b28505fcbdf2c68ae81943a3229e0dcff6a24b63cf137ad9c2d53336573b0701a07801cf62d9f6710c52954d4be17f5723f7ff417af026b30cbfaa9b28e5e1bc3f d06da76e0b65671fc4639b624338464ed7b52bbd c30fd37840c98439991f02ec2169ea1f0a2a3ac29c9ca9b82f6e020b0a4f333b +result 1 1 64787 +called - +root 9defdeda9f3fea9ece8b24367fb9da675012f50bf8a1ca729d33f5fe8cfefef8 +end +scenario creates-then-calls-created +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account 0000000000000000000000000000000001000008 0 0 - +account 0000000000000000000000000000000000000004 0 0 - +account 00000000000000000000000000000000000000a1 0 0 6043601e600039604360006000f060006000600060006000855af15050006037600c60003960376000f37f6923fa85000000000000000000000000000000000000000000000000000000006000526000600060046000600063010000065af15000 +preroot 7b229e3b910207a067fcfffcdeda66ab1488a76220b36cdcace69038c8cd692b +tx f8608000831e84809400000000000000000000000000000000000000a1808065a0b28505fcbdf2c68ae81943a3229e0dcff6a24b63cf137ad9c2d53336573b0701a07801cf62d9f6710c52954d4be17f5723f7ff417af026b30cbfaa9b28e5e1bc3f d06da76e0b65671fc4639b624338464ed7b52bbd c30fd37840c98439991f02ec2169ea1f0a2a3ac29c9ca9b82f6e020b0a4f333b +result 1 1 76541 +called 0000000000000000000000000000000001000006 +root 12be2c061031088ae78a52fabcad3a9d667318b8dd4400f2a0cdafacaf7b6c54 +end +scenario calls-creator-of-child-calling-bridge +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account 0000000000000000000000000000000001000008 0 0 - +account 0000000000000000000000000000000000000004 0 0 - +account 00000000000000000000000000000000000000a1 0 0 6000600060006000600060b15af15000 +account 00000000000000000000000000000000000000b1 0 0 60376010600039603760006000f050007f6923fa85000000000000000000000000000000000000000000000000000000006000526000600060046000600063010000065af15000 +preroot d8742a3c2d1b099c4c44f5b547cc8166b5822f56c0d59e2e16149fa3ebbb7ca7 +tx f8608000831e84809400000000000000000000000000000000000000a1808065a0b28505fcbdf2c68ae81943a3229e0dcff6a24b63cf137ad9c2d53336573b0701a07801cf62d9f6710c52954d4be17f5723f7ff417af026b30cbfaa9b28e5e1bc3f d06da76e0b65671fc4639b624338464ed7b52bbd c30fd37840c98439991f02ec2169ea1f0a2a3ac29c9ca9b82f6e020b0a4f333b +result 1 1 65503 +called - +root daf933648c9b28bf5f944e1e295b0a4793b8b467897609a04d76494ddc36d21c +end +scenario deploy-calling-bridge +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account 0000000000000000000000000000000001000008 0 0 - +account 0000000000000000000000000000000000000004 0 0 - +preroot 763e810364c1938f9fe44a108a5c8311bb07318f5a305370447748d6e03b43b8 +tx f8838000831e84808080b77f6923fa85000000000000000000000000000000000000000000000000000000006000526000600060046000600063010000065af1500066a0da6d1e82463c18ca9ad1f8be4cd7bdf27ea7c5413b08ad6725eaacf6cd662daea024ac93b5970e654384f6d744ec8d9f7c2f4f00ecac3d07f494dfc84c35aa7375 d06da76e0b65671fc4639b624338464ed7b52bbd 3ada3fdc93397ff90beb31350f28a4b8ca23bc626fb37860281ccf44938428da +result 1 1 65194 +called 0000000000000000000000000000000001000006 +root 6dd230430f93d490a843249c7f049dac7f3ae04b1981a47ba70e062ba2a346d5 +end +scenario deploy-creating-child-calling-bridge +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account 0000000000000000000000000000000001000008 0 0 - +account 0000000000000000000000000000000000000004 0 0 - +preroot 763e810364c1938f9fe44a108a5c8311bb07318f5a305370447748d6e03b43b8 +tx f8948000831e84808080b84760376010600039603760006000f050007f6923fa85000000000000000000000000000000000000000000000000000000006000526000600060046000600063010000065af1500065a0889a8431884a0402f92002fe789918e12829ccf591ed596f52eb53c4d2b85606a04d847a4ddc47c0604ffec0d62b2ea2d08430abc04d7d9b85aa893f97b2aded0f d06da76e0b65671fc4639b624338464ed7b52bbd 3e7622450b6c14f8918c7fb13835dc42672bd87ef1cc8054c7d9fa2c0ede158f +result 1 1 97443 +called - +root d15e1bb843768b10801871ee05441530b2089b4a2eadc8deea14b5ef20c035d7 +end +scenario deep-bridge +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account 0000000000000000000000000000000001000008 0 0 - +account 0000000000000000000000000000000000000004 0 0 - +account 00000000000000000000000000000000000000e1 0 0 60006000600060006000305af1610047577f6923fa85000000000000000000000000000000000000000000000000000000006000526000600060046000600063010000065af1505b00 +preroot db166282b3c252256c990bb98125056841432a989973c26911a119dc3975e2ab +tx f8608000831e84809400000000000000000000000000000000000000e1808065a0b0a7806f0a40da6a34deb5f1af94b0231d95b970a5e8530c1fdd4bdcdd1f8a37a00c94619a07274a13bd74033e7059b92a11d46cd736a61f06260269dc5d456554 d06da76e0b65671fc4639b624338464ed7b52bbd bd4a89ed5b5a3989062b73405ca5985ca4ed197ab7acd4e4f56f0d71c0d4fee1 +result 1 1 315667 +called 0000000000000000000000000000000001000006 +root 75fdb7fac0b5682150f75f5c61dd9d44b5f79ace2a06a9a8c2a636f50704280d +end +scenario creates-reverting-then-calls-bridge +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account 0000000000000000000000000000000001000008 0 0 - +account 0000000000000000000000000000000000000004 0 0 - +account 00000000000000000000000000000000000000a1 0 0 60056046600039600560006000f0507f6923fa85000000000000000000000000000000000000000000000000000000006000526000600060046000600063010000065af1500060006000fd +preroot 5dff3d25a8fd57f3e0a7a94602ac1ca241fcec6466e9e5784d4f74e7bd0ddf24 +tx f8608000831e84809400000000000000000000000000000000000000a1808065a0b28505fcbdf2c68ae81943a3229e0dcff6a24b63cf137ad9c2d53336573b0701a07801cf62d9f6710c52954d4be17f5723f7ff417af026b30cbfaa9b28e5e1bc3f d06da76e0b65671fc4639b624338464ed7b52bbd c30fd37840c98439991f02ec2169ea1f0a2a3ac29c9ca9b82f6e020b0a4f333b +result 1 1 64776 +called 0000000000000000000000000000000001000006 +root b5f723baded3c7d9070cb4bc9be9f0dca807be0d2aaf47e0641fc871c558a452 +end +scenario creates-empty-then-calls-bridge +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account 0000000000000000000000000000000001000008 0 0 - +account 0000000000000000000000000000000000000004 0 0 - +account 00000000000000000000000000000000000000a1 0 0 60006046600039600060006000f0507f6923fa85000000000000000000000000000000000000000000000000000000006000526000600060046000600063010000065af15000 +preroot 77dd6150b879b6b099ffcd84dbc4f60fa8ed6a396043649f8bea202eabd1b5c9 +tx f8608000831e84809400000000000000000000000000000000000000a1808065a0b28505fcbdf2c68ae81943a3229e0dcff6a24b63cf137ad9c2d53336573b0701a07801cf62d9f6710c52954d4be17f5723f7ff417af026b30cbfaa9b28e5e1bc3f d06da76e0b65671fc4639b624338464ed7b52bbd c30fd37840c98439991f02ec2169ea1f0a2a3ac29c9ca9b82f6e020b0a4f333b +result 1 1 64765 +called 0000000000000000000000000000000001000006 +root 13689424f9b45f43d74da58aaa42c53dec308dea08976b7110dacf39c6fdfd50 +end +scenario calls-bridge-then-reverts +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account 0000000000000000000000000000000001000008 0 0 - +account 0000000000000000000000000000000000000004 0 0 - +account 00000000000000000000000000000000000000a1 0 0 7f6923fa85000000000000000000000000000000000000000000000000000000006000526000600060046000600063010000065af15060006000fd +preroot cbe49b108eee609018957e09210693187d4f041eef6d3e38b572613f35b1395a +tx f8608000831e84809400000000000000000000000000000000000000a1808065a0b28505fcbdf2c68ae81943a3229e0dcff6a24b63cf137ad9c2d53336573b0701a07801cf62d9f6710c52954d4be17f5723f7ff417af026b30cbfaa9b28e5e1bc3f d06da76e0b65671fc4639b624338464ed7b52bbd c30fd37840c98439991f02ec2169ea1f0a2a3ac29c9ca9b82f6e020b0a4f333b +result 1 0 32748 +called 0000000000000000000000000000000001000006 +root 724ddaf9129ba571b569fc22123744d5ea2a5b1cfbf883ca6053d21f9ccfa6d1 +end +scenario deep-plain +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account 0000000000000000000000000000000001000008 0 0 - +account 0000000000000000000000000000000000000004 0 0 - +account 00000000000000000000000000000000000000e2 0 0 60006000600060006000305af1610011575b00 +preroot c6cb7be839690c1bbfb520014b94569037cb6a070f43e9fa50cbc2f0ed326002 +tx f8608000831e84809400000000000000000000000000000000000000e2808066a0701ea9840bf1f5cad7027411ca61fd214a8acfe01e472426904b780b065203f6a067ca6ff0c2f6bde8f45c09c19701b15a7f9fd5d7f0220e7f0b990e630a7a03c3 d06da76e0b65671fc4639b624338464ed7b52bbd 7bc988d2ca983d50adb0c8a7db9aa57066712f3a0cf7b0f9739d11372254ce24 +result 1 1 314933 +called - +root e1d4786a75043c6870c54f3715ad6ee321b3196a178b7daa42a090375d16cfbe +end diff --git a/crates/execution/src/tracer.rs b/crates/execution/src/tracer.rs index 688e262..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 @@ -195,6 +202,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, @@ -325,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; @@ -435,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/crates/execution/tests/live_peg_event_recording.rs b/crates/execution/tests/live_peg_event_recording.rs index 473fb4f..d1ebf46 100644 --- a/crates/execution/tests/live_peg_event_recording.rs +++ b/crates/execution/tests/live_peg_event_recording.rs @@ -3,16 +3,15 @@ //! The offline reconstruction is well covered: `verify_range //! --record-legacy-events` drives `events_for` directly and has unit tests //! around every branch. The **live** path does not go through any of that. -//! It runs inside `BlockProcessor::process_block_timed`, where the records -//! are drained from a thread-local sink that `execute_block` opens and -//! `commit_receipts` empties. +//! It runs inside `BlockProcessor::process_block_timed`, which opens a +//! thread-local sink for the block, closes it when the block is done and +//! hands what was recorded on with the block (`ProcessedBlock:: +//! unlogged_peg_events`) for `commit_receipts` to store. //! -//! Three things can go wrong there and in no unit test: +//! Two things can go wrong there and in no unit test: //! -//! - the sink is opened on one thread and drained on another, so nothing is +//! - the sink is never opened on the thread that executes, so nothing is //! ever recorded and the store looks like a chain with no Flyover peg-ins; -//! - the drain happens before the events are synthesised, or after something -//! else has already emptied it; //! - the gate suppresses what it should keep. //! //! Each failure is silent — an empty store is indistinguishable from a quiet @@ -82,16 +81,16 @@ fn a_flyover_pegin_is_recorded_through_the_live_commit_path() { .with_legacy_event_store(Arc::clone(&events)); // The import loop does these two in sequence (`sync/src/import.rs`): - // `process_block_timed` executes and fills the thread-local sink, and - // `commit_receipts` empties it. The recording half is called directly + // `process_block_timed` executes and records, and `commit_receipts` + // stores what the block carries. The recording half is called directly // here because the other half of `commit_receipts` writes receipts, and // this test runs against the node's real store, opened read-only. let mut timings = rustock_execution::ProcessTimings::default(); - processor + let processed = processor .process_block_timed(&block, &pre, trie.clone(), &mut timings) .expect("block executes"); processor - .record_unlogged_peg_events(&block) + .record_unlogged_peg_events(&block, &processed.unlogged_peg_events) .expect("records the events the Bridge did not emit"); let topic = rustock_execution::bridge::legacy_events::pegin_btc( diff --git a/crates/execution/tests/mainnet_block_rules.rs b/crates/execution/tests/mainnet_block_rules.rs index 427fddc..69979cf 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,15 @@ 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 +202,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 +216,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 +230,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/execution/tests/miner_clock.rs b/crates/execution/tests/miner_clock.rs new file mode 100644 index 0000000..6efdb16 --- /dev/null +++ b/crates/execution/tests/miner_clock.rs @@ -0,0 +1,44 @@ +//! rskj's `MinerClockTest`, for the clock a development chain's miner stamps +//! its blocks with: the wall clock plus what `evm_increaseTime` has added. +//! The offset is one value for the whole process, so these run in a test +//! binary of their own, where no other test's blocks read it, and in one test +//! function, so that none of them reads another's. + +use rustock_execution::mining::dev::{increase_time, now_with_offset, reset_time, time_offset}; + +fn wall_clock() -> u64 { + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_secs() +} + +/// rskj `MinerClockTest.adjustTimeIfNotRegtest` and `clearTimeIncrease`: an +/// increase of 5,392 seconds puts the clock a child block is stamped by that +/// far ahead of the wall clock, and clearing it puts it back. Between them, +/// rskj's `MinerClock.increaseTime`: a move of no seconds or fewer changes +/// nothing and answers the offset as it stands. +#[test] +fn the_miners_clock_runs_ahead_by_what_was_added() { + assert_eq!(time_offset(), 0); + assert_eq!(increase_time(5392), 5392); + let before = wall_clock(); + let clock = now_with_offset(); + let after = wall_clock(); + assert!( + (before + 5392..=after + 5392).contains(&clock), + "{clock} against the wall clock {before}..={after}" + ); + + assert_eq!(increase_time(0), 5392); + assert_eq!(increase_time(-5392), 5392); + assert_eq!(increase_time(i64::MIN), 5392); + assert_eq!(time_offset(), 5392); + + reset_time(); + assert_eq!(time_offset(), 0); + let before = wall_clock(); + let clock = now_with_offset(); + let after = wall_clock(); + assert!((before..=after).contains(&clock), "{clock} against the wall clock {before}..={after}"); +} 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), } } } 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/peers.rs b/crates/networking/src/peers.rs index c17954e..668b2a7 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,37 @@ 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/networking/src/protocol/rsk.rs b/crates/networking/src/protocol/rsk.rs index 73e0c28..8ea2c6c 100644 --- a/crates/networking/src/protocol/rsk.rs +++ b/crates/networking/src/protocol/rsk.rs @@ -1,6 +1,8 @@ use alloy_rlp::{Decodable, Encodable, RlpDecodable, RlpEncodable, Header as RlpHeader}; use alloy_primitives::{Address, 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}; use super::snap; @@ -276,6 +278,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). @@ -311,6 +319,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, @@ -407,6 +423,8 @@ impl Decodable for BlockRange { #[derive(Debug, Clone)] pub enum RskSubMessage { Status(RskStatus), + Block(Box), + GetBlock(B256), BlockHashRequest(BlockHashRequest), BlockHeadersRequest(BlockHeadersRequest), BlockHeadersResponse(BlockHeadersResponse), @@ -435,6 +453,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, @@ -569,7 +589,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() } @@ -587,10 +610,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(); @@ -700,7 +724,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 @@ -719,11 +744,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); @@ -752,8 +783,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. } } } @@ -843,6 +920,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]; @@ -871,7 +968,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 }) } @@ -968,35 +1070,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. - // - // 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. + // 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. // - // 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 @@ -1152,7 +1270,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 @@ -1209,22 +1331,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(); @@ -1232,6 +1353,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"); @@ -1510,6 +1632,7 @@ mod tests { id: 55, transactions: vec![], uncles: vec![], + extension: None, }; let msg = RskMessage::new(RskSubMessage::BodyResponse(resp)); @@ -1549,6 +1672,7 @@ mod tests { id: 77, transactions: vec![tx], uncles: vec![], + extension: None, }; let msg = RskMessage::new(RskSubMessage::BodyResponse(resp)); @@ -1614,6 +1738,7 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } @@ -1624,6 +1749,7 @@ mod tests { id: 100, transactions: transactions.clone(), uncles: uncles.clone(), + extension: None, }; let msg = RskMessage::new(RskSubMessage::BodyResponse(resp)); @@ -1683,12 +1809,14 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, }; let resp = BodyResponse { id: 88, transactions: vec![], uncles: vec![uncle], + extension: None, }; let msg = RskMessage::new(RskSubMessage::BodyResponse(resp)); @@ -1709,6 +1837,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![])); @@ -1913,7 +2154,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), @@ -1939,6 +2180,7 @@ mod headers_with_uncles_tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } @@ -1953,7 +2195,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 @@ -2044,6 +2286,261 @@ 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)])], + }), + 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 + .into_iter() + .map(|seed| { + let kind = format!("{:?}", seed.message_type()); + let mut bytes = Vec::new(); + RskMessage::new(seed).encode(&mut bytes); + (kind, bytes) + }) + .collect(); + // 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(); + 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)] @@ -2130,6 +2627,138 @@ mod difficulty_encoding_tests { } } +/// 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); + } +} + #[cfg(test)] mod trie_proof_message_tests { use super::*; diff --git a/crates/networking/src/protocol/snap.rs b/crates/networking/src/protocol/snap.rs index 6ce0365..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, } } @@ -649,20 +650,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 +673,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/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/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/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". diff --git a/crates/rpc/src/call.rs b/crates/rpc/src/call.rs index 2a5cd38..fd197c9 100644 --- a/crates/rpc/src/call.rs +++ b/crates/rpc/src/call.rs @@ -1,82 +1,143 @@ -use crate::dto::CallRequest; +use crate::dto::{CallArguments, CallRequest}; +use crate::params::{uncaught, Refusal}; use crate::server::RpcState; -use crate::state::resolve_state_root; + 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; +/// 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; + +/// 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(refusal) => return refuse(id, refusal), }; - let block_param = params.get(1).and_then(|v| v.as_str()).unwrap_or("latest"); 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 { - return JsonRpcResponse::error(id, INTERNAL_ERROR, "Cannot resolve block"); + // 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(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, &header, &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}")), } } 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(refusal) => return refuse(id, refusal), }; - let block_param = params.get(1).and_then(|v| v.as_str()).unwrap_or("latest"); 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 { - 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, + }; + // 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()); + let executor = state.executor(hardfork_cfg); - let gas_cap = call_req.gas.unwrap_or(header.gas_limit.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, 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}")), } } @@ -94,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, @@ -113,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, header, 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}")); @@ -128,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, header, 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, } @@ -140,28 +211,29 @@ 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. +/// 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, - header: &rustock_core::Header, 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(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/debug.rs b/crates/rpc/src/debug.rs index 5d3b5f4..8f7449a 100644 --- a/crates/rpc/src/debug.rs +++ b/crates/rpc/src/debug.rs @@ -96,6 +96,8 @@ pub fn debug_trace_transaction(id: Value, params: &Value, state: &RpcState) -> J else { return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid transaction hash"); }; + // 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)) = crate::helpers::canonical_tx_location(&state.store, tx_hash) else { @@ -201,7 +203,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/dto.rs b/crates/rpc/src/dto.rs index 8e8e592..a3356e6 100644 --- a/crates/rpc/src/dto.rs +++ b/crates/rpc/src/dto.rs @@ -180,61 +180,198 @@ impl LogDto { } } -/// Call request object for eth_call / eth_estimateGas. +/// 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`]). A transaction the node is to sign has them +/// read another way ([`CallArguments::transaction`]). #[derive(Debug)] -pub struct CallRequest { - pub from: Option
, - pub to: Option
, - pub gas: Option, - pub gas_price: Option, - pub value: Option, - pub data: Option>, +pub struct CallArguments { + from: Option
, + to: Option
, + gas: Option, + gas_price: Option, + gas_limit: Option, + nonce: Option, + chain_id: Option, + value: Option, + 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()); +/// 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)>, +} - let to = obj.get("to") - .and_then(|v| v.as_str()) - .and_then(|s| s.parse::
().ok()); +/// The gas a sent transaction names when the caller names none: rskj's +/// `GasCost.TRANSACTION_DEFAULT`. +const TRANSACTION_DEFAULT_GAS: u64 = 90_000; - let gas = obj.get("gas") - .and_then(|v| v.as_str()) - .and_then(parse_hex_u64); +/// 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}"), + )), + } +} - let gas_price = obj.get("gasPrice") - .and_then(|v| v.as_str()) - .and_then(parse_hex_u256); +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 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()?; + Ok(Self { + from, + to, + gas, + gas_price, + gas_limit, + nonce, + chain_id, + value, + // `CallArguments.setInput` writes over what `setData` wrote. + data: input.or(data), + }) + } - let value = obj.get("value") - .and_then(|v| v.as_str()) - .and_then(parse_hex_u256); + /// The sender the arguments name. + pub fn from(&self) -> Option
{ + self.from + } - 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() - }); + /// `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, + }) + } - Some(Self { from, to, gas, gas_price, value, 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, + }) } -} -fn parse_hex_u64(s: &str) -> Option { - let s = s.strip_prefix("0x").unwrap_or(s); - u64::from_str_radix(s, 16).ok() + /// [`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() + } } -fn parse_hex_u256(s: &str) -> Option { - let s = s.strip_prefix("0x").unwrap_or(s); - U256::from_str_radix(s, 16).ok() +/// Call request object for eth_call / eth_estimateGas. +#[derive(Debug)] +pub struct CallRequest { + pub from: Option
, + pub to: Option
, + pub gas: Option, + pub gas_price: Option, + pub value: Option, + pub data: Option>, } fn compute_contract_address(sender: &[u8], nonce: u64) -> Address { diff --git a/crates/rpc/src/eth.rs b/crates/rpc/src/eth.rs index 9a28bda..be10518 100644 --- a/crates/rpc/src/eth.rs +++ b/crates/rpc/src/eth.rs @@ -1,16 +1,16 @@ 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; 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). 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 { @@ -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)) @@ -482,19 +477,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); } @@ -520,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), } } @@ -789,7 +801,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 a7188a0..266bd2d 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() @@ -70,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 /// @@ -81,39 +93,364 @@ 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, } - if trimmed.eq_ignore_ascii_case("earliest") { - return Some(0); +} + +/// 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)) +} + +/// rskj's lower-case hex (`HexUtils.isHex`): digits and `a` to `f`, and no +/// characters at all. +pub(crate) 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(), } - // 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; +} + +/// The bytes rskj's `HexUtils.stringHexToByteArray` reads: a `0x` taken off, +/// 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`, +/// `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()), + }; + 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 { + let Some(text) = value.and_then(crate::params::text) else { + return Ok("latest".to_string()); + }; + execution_block(&text, true) +} + +/// 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 !(tag || is_decimal(s) || is_hex_number(s)) { + return Err((INVALID_PARAMS, format!("Invalid block identifier '{s}'"))); + } + 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}; + let text = crate::params::field_text(value).unwrap_or_else(|| "null".to_string()); + 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 @@ -124,41 +461,68 @@ 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 transactions: Vec, + pub uncles: Vec, 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, /// Total fees paid by this block's transactions, in wei. /// /// An RSK header field (number 14, where Ethereum has `mixHash`), so this /// is the chain's own figure rather than one recomputed here: it is /// committed to by the block hash. Summing `gasUsed * gasPrice` over the - /// block's receipts reproduces it exactly, which is what `block_fees - /// --verify` checks. + /// block's receipts reproduces it, which is what `block_fees --verify` + /// checks, except where a direct Bridge call ended in rskj's invisible + /// exception: its sender pays for the whole gas limit while its receipt + /// shows the gas spent (`RskHandler`'s `invisible_exception`; mainnet + /// #764,123). /// /// rskj's `BlockResultDTO` also carries it, so serving it is parity, not /// an extension. pub paid_fees: String, - pub transactions: Vec, - pub uncles: Vec, - pub size: 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 { @@ -202,31 +566,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), - miner: format!("{:#x}", header.beneficiary), - 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()), + 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}")), difficulty: to_hex_u256(&header.difficulty), total_difficulty: to_hex_u256(&total_difficulty), - gas_limit: to_hex_u256(&header.gas_limit), + 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_u256(&header.minimum_gas_price), - paid_fees: to_hex_u256(&header.paid_fees), 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 @@ -330,13 +791,15 @@ fn tx_to_json( }) } -/// Where a transaction is, **if the block holding it is still canonical**. +/// Where a transaction is on the main chain: of the blocks it was committed +/// in, **the one the chain still has at that block's height**, and its index +/// there (`BlockStore::tx_location_in_main_chain`). /// -/// `tx_location` answers from `CF_TX_INDEX`, which -/// `BlockProcessor::process_block` writes for *every* block it executes, -/// canonical or not — and which has no delete path at all. So after a reorg -/// the orphaned block is still in storage with its receipts, and the index -/// still points at it. +/// `CF_TX_INDEX` keeps every block a transaction was committed in, canonical +/// or not, and has no delete path. So after a reorg the orphaned block is +/// still in storage with its receipts, and the index still names it, perhaps +/// as the block it wrote last while a canonical block holds the transaction +/// too. /// /// Reading that index without this check is how `eth_getTransactionReceipt` /// came to report `status: 1` for a transaction whose effects are gone, @@ -345,7 +808,9 @@ fn tx_to_json( /// comment: a caller cannot defend against it, and the next method to read /// the index would have had to remember. /// -/// rskj does the same check, in `Web3Impl.eth_getTransactionByHash`: +/// rskj's `ReceiptStoreImpl.getInMainChain` looks through every block the +/// transaction is recorded in for the one the chain has at that height, and +/// `Web3Impl.eth_getTransactionByHash` then checks the answer again: /// /// ```java /// Block mainBlock = blockchain.getBlockByNumber(block.getNumber()); @@ -354,18 +819,15 @@ fn tx_to_json( /// } /// ``` /// -/// This is the read-side guard. It does not fix the cause — the index still -/// accumulates entries for abandoned blocks — but it is wanted regardless, -/// because a reorg can land between the lookup and the answer however the -/// index is maintained. See #343. +/// This is the read-side guard. It does not prune the index -- the entries +/// for abandoned blocks stay -- but it is wanted regardless, because a reorg +/// can land between the lookup and the answer however the index is +/// maintained. See #343. pub fn canonical_tx_location( store: &rustock_storage::BlockStore, tx_hash: B256, ) -> Option<(B256, u32)> { - let (block_hash, index) = store.tx_location(tx_hash).ok().flatten()?; - let number = store.header(block_hash).ok().flatten()?.number; - let canonical = store.canonical_hash(number).ok().flatten()?; - (canonical == block_hash).then_some((block_hash, index)) + store.tx_location_in_main_chain(tx_hash).ok().flatten() } #[cfg(test)] @@ -385,6 +847,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)); @@ -407,14 +885,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. @@ -424,6 +904,152 @@ 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 + /// (`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 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, 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'"), + ("finalized", "Invalid block identifier 'finalized'"), + ("0x", "Invalid block identifier '0x'"), + ("12", "invalid blocknumber 12"), + ] { + 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 + /// 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/java.rs b/crates/rpc/src/java.rs new file mode 100644 index 0000000..4419e29 --- /dev/null +++ b/crates/rpc/src/java.rs @@ -0,0 +1,616 @@ +//! 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`). +//! +//! 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}; + +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) +} + +/// 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)`. +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 `>`. + // + // 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 = 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 = Wide::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); + } + + /// `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] + 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 7e530fb..4d105d8 100644 --- a/crates/rpc/src/lib.rs +++ b/crates/rpc/src/lib.rs @@ -3,19 +3,24 @@ pub mod debug; pub mod dto; pub mod eth; pub mod helpers; +pub mod java; pub mod evm; pub mod logs; pub mod mnr; pub mod metrics_server; pub mod admin; pub mod net; +pub mod params; pub mod proof; +pub mod raw_tx; +pub mod revert; pub mod witness_search; pub mod rsk; 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/logs.rs b/crates/rpc/src/logs.rs index d42e9b9..3cebd74 100644 --- a/crates/rpc/src/logs.rs +++ b/crates/rpc/src/logs.rs @@ -1,12 +1,12 @@ 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}; 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() } } @@ -105,13 +126,18 @@ 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); 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, @@ -129,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), @@ -274,6 +303,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; @@ -382,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")); @@ -413,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/params.rs b/crates/rpc/src/params.rs new file mode 100644 index 0000000..7c24eb0 --- /dev/null +++ b/crates/rpc/src/params.rs @@ -0,0 +1,429 @@ +//! 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`, 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, 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, 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); + +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()) +} + +/// 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 +/// `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), + } +} + +/// 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(&java_length(text)) { + 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 java_length(text) > 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.")) +} + +/// `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 +/// 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) +} + +/// 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::*; + + /// `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/proof.rs b/crates/rpc/src/proof.rs index c09d66a..10b3bc1 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,17 +127,23 @@ pub async fn eth_get_proof(id: Value, params: &Value, state: &RpcState) -> JsonR slots.push((raw.to_string(), slot)); } - let block_param = params.get(2).and_then(|v| v.as_str()).unwrap_or("latest"); - // A node with no trie of its own -- a light client -- asks a peer and // checks the answer against a header it validated itself. The local trie - // wins whenever there is one: no round trip, nothing to verify. + // wins whenever there is one: no round trip, nothing to verify. Either + // way the block is read as the state queries read it: a tag, a number, + // or an EIP-1898 object. if state.trie_store.is_none() { - return light_proof(id, addr, &slots, block_param, state).await; + let named = crate::helpers::block_ref(params.get(2), &state.store) + .and_then(|(identifier, _)| crate::helpers::information_block(&identifier)); + return match named { + Ok(block) => light_proof(id, addr, &slots, &block, state).await, + Err((code, message)) => JsonRpcResponse::error(id, code, message), + }; } - 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"); @@ -210,6 +216,213 @@ pub async fn eth_get_proof(id: Value, params: &Value, state: &RpcState) -> JsonR ) } +/// `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. A value too long to sit in its node (over 32 bytes, as some of +/// the Bridge's cells are) is stored apart from it: one of those the store +/// has lost is answered as absent, though the slot's node still carries its +/// hash and length, so a caller that walks the nodes, as it must, finds it +/// held and the answer wrong. +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 (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"); + }; + 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, + }), + ) +} + +/// 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() +} + /// Answer `eth_getProof` on a node that holds no state. /// /// The peer's answer is checked before any of it is used: every path has to 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/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/rsk.rs b/crates/rpc/src/rsk.rs index 3d23a3f..ca64197 100644 --- a/crates/rpc/src/rsk.rs +++ b/crates/rpc/src/rsk.rs @@ -1,11 +1,21 @@ -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}; +/// 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( @@ -70,6 +80,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 @@ -122,9 +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"); - 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 { @@ -177,11 +332,10 @@ pub fn rsk_get_raw_transaction_receipt_by_hash( return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid transaction hash"); }; - // This method had the only canonicality check in the tree, and its - // comment said `tx_location` "indexes the canonical chain". It does not: - // it indexes every block ever executed and never forgets one. The check - // was what made this method correct while five others were wrong, and it - // now lives in `canonical_tx_location` so there is one of it. See #343. + // 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. `canonical_tx_location` is the one place + // every method reads the index through (#343). let Some((block_hash, index)) = crate::helpers::canonical_tx_location(store, tx_hash) else { return JsonRpcResponse::success(id, Value::Null); }; diff --git a/crates/rpc/src/server.rs b/crates/rpc/src/server.rs index 65f61c4..1c6cf73 100644 --- a/crates/rpc/src/server.rs +++ b/crates/rpc/src/server.rs @@ -106,6 +106,11 @@ pub struct RpcState { /// no network round trip and no verification. pub proof_fetcher: 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 @@ -176,6 +181,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, @@ -347,14 +375,54 @@ async fn dispatch_heavy(state: &RpcState, req: JsonRpcRequest) -> JsonRpcRespons } } +/// 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 { - if is_heavy(&req.method) { - return dispatch_heavy(state, req).await; + 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") + } } +} - let id = req.id.unwrap_or(Value::Null); +async fn route(state: &RpcState, req: JsonRpcRequest) -> JsonRpcResponse { + let id = req.id.clone().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; + } + + // The methods that run EVM code go off the runtime, once the arity + // check above has refused a call as jsonrpc4j would. + if is_heavy(&req.method) { + return dispatch_heavy(state, req).await; + } + match req.method.as_str() { // -- web3 -- "web3_clientVersion" => web3::web3_client_version(id), @@ -468,7 +536,7 @@ async fn dispatch(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, @@ -486,6 +554,10 @@ 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) } @@ -495,7 +567,7 @@ async fn dispatch(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), @@ -522,17 +594,26 @@ async fn dispatch(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), @@ -557,16 +638,7 @@ async fn dispatch(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), } } @@ -599,6 +671,36 @@ pub(crate) async fn dispatch_for_test(state: &RpcState, req: JsonRpcRequest) -> 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")); + } +} + #[cfg(test)] mod heavy_dispatch_tests { use super::*; diff --git a/crates/rpc/src/sign.rs b/crates/rpc/src/sign.rs index 7eb8b34..ea60dc6 100644 --- a/crates/rpc/src/sign.rs +++ b/crates/rpc/src/sign.rs @@ -1,11 +1,19 @@ //! `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. +//! +//! 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 @@ -13,20 +21,19 @@ //! 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; -/// 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"), - ) +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, @@ -41,51 +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 { - let Some(wallet) = &state.wallet else { return no_wallet(id, "eth_sign") }; - - 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"); + // 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 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"); + 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. + 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); }; 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() { - return no_wallet(id, "eth_signTransaction"); + 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() { - return no_wallet(id, "eth_sendTransaction"); - } + // 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 { @@ -94,149 +120,125 @@ 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), } } -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. -/// -/// 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. -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)), - }; + .and_then(|pool| pool.pending_nonce(&from)) + .unwrap_or_else(|| account_nonce(&from, state)); + U256::from(pending) + }); - 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), - }; - - // 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), - }; - - let chain_id = state.hardfork_cfg.as_ref().map(|c| c.chain_id).ok_or(SignError::NoChainId)?; + 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}"))); + } + } - 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, - }; + 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)?; - if !wallet.sign_transaction(&from, &mut tx, chain_id) { - return Err(SignError::Unknown(from)); - } - Ok((from, tx.rlp_for_trie())) + // 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 { @@ -252,16 +254,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) } - -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)) -} diff --git a/crates/rpc/src/state.rs b/crates/rpc/src/state.rs index 2c71744..6d9b792 100644 --- a/crates/rpc/src/state.rs +++ b/crates/rpc/src/state.rs @@ -11,10 +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"); - - 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); @@ -28,10 +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"); - // 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( @@ -42,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); @@ -57,10 +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"); - - 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); @@ -87,10 +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"); - - 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); @@ -119,9 +115,77 @@ 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 +/// ([`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| 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 +/// 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 @@ -133,8 +197,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 @@ -144,16 +213,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)) } pub fn resolve_header(block_param: &str, store: &BlockStore) -> Option
{ diff --git a/crates/rpc/src/stubs.rs b/crates/rpc/src/stubs.rs new file mode 100644 index 0000000..c7539b7 --- /dev/null +++ b/crates/rpc/src/stubs.rs @@ -0,0 +1,187 @@ +//! 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. +/// +/// 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 +/// 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") + } + "personal_newAccount" if params[0].is_null() => { + JsonRpcResponse::error(id, INVALID_PARAMS, "Passphrase is null") + } + _ => JsonRpcResponse::error(id, DISABLED_WALLET, "The local wallet feature is disabled"), + } +} diff --git a/crates/rpc/src/subscribe.rs b/crates/rpc/src/subscribe.rs index bf34233..3ccf56c 100644 --- a/crates/rpc/src/subscribe.rs +++ b/crates/rpc/src/subscribe.rs @@ -200,15 +200,22 @@ 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))); 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( @@ -222,13 +229,16 @@ 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()))); 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/testdata/rskj_local_calls.txt b/crates/rpc/src/testdata/rskj_local_calls.txt new file mode 100644 index 0000000..9b57b88 --- /dev/null +++ b/crates/rpc/src/testdata/rskj_local_calls.txt @@ -0,0 +1,679 @@ +# 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