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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

139 changes: 139 additions & 0 deletions crates/cli/examples/account_at.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
//! An account's nonce and balance, as the trie holds them at a chosen block.
//!
//! One lookup, no walk: a unitrie account key is derived from the address, so
//! the state root plus the address is enough. Nothing here executes anything.
//!
//! ```text
//! account_at <data-dir> <trie-dir> <0xaddress>…
//! [--block N] [--engine flatdb|rocksdb]
//! ```
//!
//! `--block` defaults to the node's **executed** head, which is the state a
//! balance ordinarily means; pass a height for a historical one, which needs a
//! store that still holds that state (`archive_bound` says how high an archive
//! reaches, and a garbage-collected store answers only above its watermark --
//! `segbuild_status` prints that).
//!
//! `--engine` defaults to `flatdb`, which is what the mainnet node runs; the
//! archive is `rocksdb`. Passing the wrong one fails at the open rather than
//! quietly answering from nothing.
//!
//! An absent account is reported as absent rather than as a zero balance. The
//! two are different facts, and on this chain the distinction carries weight:
//! a precompile address exists as an account from an activation onward whatever
//! its balance, and an empty account is kept rather than pruned (`BR-057`).
//!
//! Read-only, and safe to run against a live node.

use alloy_primitives::U256;
use anyhow::{Context, Result};
use rustock_storage::epoch_store::{EpochConfig, EpochTrieStore};
use rustock_storage::BlockStore;
use rustock_trie::{account_key_from_bytes, AccountState, TrieKeySlice, TrieNode, TrieStore};
use std::sync::Arc;

fn flag(name: &str) -> Option<String> {
let args: Vec<String> = std::env::args().collect();
args.windows(2).find(|w| w[0] == format!("--{name}")).map(|w| w[1].clone())
}

/// Ten-to-the-eighteenth, so a balance reads as RBTC as well as in weis.
fn as_rbtc(wei: U256) -> String {
let unit = U256::from(10u64).pow(U256::from(18));
let whole = wei / unit;
let frac = wei % unit;
// Eighteen places, trimmed, so a dust balance is not rounded to nothing.
let frac = format!("{:018}", frac).trim_end_matches('0').to_string();
if frac.is_empty() {
format!("{whole}")
} else {
format!("{whole}.{frac}")
}
}

fn main() -> Result<()> {
let positional: Vec<String> =
std::env::args().skip(1).take_while(|a| !a.starts_with("--")).collect();
if positional.len() < 3 {
eprintln!(
"usage: account_at <data-dir> <trie-dir> <0xaddress>... \
[--block N] [--engine flatdb|rocksdb]"
);
std::process::exit(64);
}
let (data_dir, trie_dir) = (positional[0].clone(), positional[1].clone());
let engine = flag("engine").unwrap_or_else(|| "flatdb".into());

let blocks = BlockStore::open_read_only(&data_dir)
.with_context(|| format!("opening {data_dir} read-only"))?;

// The executed head, not the best header: only executed blocks have state.
let (at, root) = match flag("block").and_then(|v| v.parse::<u64>().ok()) {
Some(number) => {
let hash = blocks
.canonical_hash(number)?
.with_context(|| format!("no canonical block at #{number}"))?;
let header =
blocks.header(hash)?.with_context(|| format!("no header at #{number}"))?;
(number, header.state_root)
}
None => {
let (hash, root) = blocks.exec_head()?.context("this node has executed nothing")?;
let number = blocks.header(hash)?.map(|h| h.number).unwrap_or(0);
(number, root)
}
};

let engine_parsed = rustock_storage::TrieEngine::parse(&engine)?;
let config = EpochConfig::default();
let expected = rustock_storage::epoch_backend::keys_for_rotation(config.rotate_bytes);
let store: Arc<dyn TrieStore> = Arc::new(
EpochTrieStore::open_read_only_with(
std::path::Path::new(&trie_dir),
config,
engine_parsed.factory(expected),
)
.with_context(|| format!("opening {trie_dir} read-only as {engine}"))?,
);

let root_bytes = store.get(root.as_slice()).with_context(|| {
format!(
"the state root {root:?} of #{at} is not in {trie_dir}. A garbage-collected \
store answers only above its collection watermark, and an archive only up to \
the height it was built to."
)
})?;
let node = TrieNode::from_message(&root_bytes, store.as_ref());

println!("state at #{at} root {root:?}");
println!("store {trie_dir} ({engine})");
println!();

for raw in &positional[2..] {
let hex = raw.trim().trim_start_matches("0x");
let bytes = alloy_primitives::hex::decode(hex)
.with_context(|| format!("{raw} is not a hex address"))?;
anyhow::ensure!(
bytes.len() == 20 || bytes.len() == 1,
"{raw} is {} octets; an address is 20 (or 1, for REMASC)",
bytes.len()
);
let key = account_key_from_bytes(&bytes);
match node.get(&TrieKeySlice::from_key(&key), store.as_ref()) {
Some(encoded) => {
let account = AccountState::decode(&encoded)
.with_context(|| format!("decoding the account state of 0x{hex}"))?;
println!("0x{hex}");
println!(" nonce {}", account.nonce);
println!(" balance {} wei", account.balance);
println!(" {} RBTC", as_rbtc(account.balance));
if account.state_flags != 0 {
println!(" flags 0x{:08x}", account.state_flags);
}
}
// Absent is not zero. See the module note.
None => println!("0x{hex}\n ABSENT -- no account at this address in this state"),
}
}
Ok(())
}
178 changes: 178 additions & 0 deletions crates/cli/examples/contract_code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
//! A contract's bytecode, and the function selectors it dispatches on.
//!
//! The state tools answer whether an account exists and what it holds;
//! none of them answer *what it is*. When a measurement points at an address --
//! a high-water mark, a census hit, a divergence -- the next question is always
//! what the code does, and that needs the code.
//!
//! ```text
//! contract_code <data-dir> <trie-dir> <0xaddress>...
//! [--block N] [--engine flatdb|rocksdb] [--plain] [--hex]
//! ```
//!
//! `--plain` opens the trie directory as a single rustock-format store rather
//! than an epoch store, which is what an archival trie and a segbuild segment
//! are. Without it the directory is opened as an epoch store, which is what the
//! node keeps.
//!
//! # What the selector list is, and is not
//!
//! Solidity dispatches by comparing the first four octets of calldata against
//! `PUSH4` constants, so the `PUSH4` immediates in a contract's code are its
//! likely entry points. The list is a strong hint and not a proof: a `PUSH4`
//! can be any constant, a dispatcher can be written without one, and a proxy
//! has none of the selectors it actually serves.
//!
//! The decode is linear and honours `PUSH` immediates, so an octet *inside* a
//! push is never read as an opcode -- the same discipline `opcode_census` uses,
//! and the reason a naive byte scan over-reports. It also skips RSK's `0xFC`
//! code header, which moves the offset every instruction decodes from.
//!
//! Read-only, and safe to run beside a live node.

use anyhow::{Context, Result};
use rustock_trie::{code_key_from_bytes, TrieKeySlice, TrieNode, TrieStore};
use std::sync::Arc;

fn flag(name: &str) -> Option<String> {
let a: Vec<String> = std::env::args().collect();
a.windows(2).find(|w| w[0] == format!("--{name}")).map(|w| w[1].clone())
}
fn has(name: &str) -> bool {
std::env::args().any(|a| a == format!("--{name}"))
}

/// `(selectors, instructions)` from a linear decode that honours `PUSH`.
fn decode(code: &[u8]) -> (Vec<[u8; 4]>, usize) {
// RSK's code header: `0xFC`, then three octets, the third giving how many
// more follow. Execution and jumpdest analysis both start after it.
let mut i = if code.first() == Some(&0xfc) && code.len() >= 4 {
4 + code[3] as usize
} else {
0
};
let (mut sels, mut n) = (Vec::new(), 0usize);
while i < code.len() {
let op = code[i];
n += 1;
if (0x60..=0x7f).contains(&op) {
let width = (op - 0x5f) as usize;
if op == 0x63 && i + 4 < code.len() {
sels.push([code[i + 1], code[i + 2], code[i + 3], code[i + 4]]);
}
i += 1 + width;
} else {
i += 1;
}
}
sels.sort_unstable();
sels.dedup();
(sels, n)
}

/// `--sig "token0()"` prints that signature's selector and exits.
///
/// The point is to turn a guess into a check. A selector list identifies a
/// contract only if the names behind it are verified, and recalling that
/// `0x0dfe1681` is `token0()` is exactly the kind of claim that should be
/// computed rather than remembered.
fn selectors_from_signatures() -> bool {
let args: Vec<String> = std::env::args().collect();
let sigs: Vec<&String> = args
.windows(2)
.filter(|w| w[0] == "--sig")
.map(|w| &w[1])
.collect();
if sigs.is_empty() {
return false;
}
for sig in sigs {
let h = alloy_primitives::keccak256(sig.as_bytes());
println!("0x{} {sig}", alloy_primitives::hex::encode(&h[0..4]));
}
true
}

fn main() -> Result<()> {
if selectors_from_signatures() {
return Ok(());
}
let pos: Vec<String> =
std::env::args().skip(1).take_while(|a| !a.starts_with("--")).collect();
if pos.len() < 3 {
eprintln!(
"usage: contract_code <data-dir> <trie-dir> <0xaddress>... \
[--block N] [--engine flatdb|rocksdb] [--plain] [--hex]"
);
std::process::exit(64);
}
let (data_dir, trie_dir) = (pos[0].clone(), pos[1].clone());

let blocks = rustock_storage::BlockStore::open_read_only(&data_dir)
.with_context(|| format!("opening {data_dir} read-only"))?;
let (at, root) = match flag("block").and_then(|v| v.parse::<u64>().ok()) {
Some(n) => {
let h = blocks.canonical_hash(n)?.with_context(|| format!("no block #{n}"))?;
(n, blocks.header(h)?.with_context(|| format!("no header #{n}"))?.state_root)
}
None => {
let (h, r) = blocks.exec_head()?.context("this node has executed nothing")?;
(blocks.header(h)?.map(|x| x.number).unwrap_or(0), r)
}
};

let store: Arc<dyn TrieStore> = if has("plain") {
Arc::new(
rustock_storage::RocksDbTrieStore::open_read_only(&trie_dir)
.with_context(|| format!("opening {trie_dir} as a plain rustock store"))?,
)
} else {
let engine = flag("engine").unwrap_or_else(|| "flatdb".into());
let cfg = rustock_storage::epoch_store::EpochConfig::default();
let expected = rustock_storage::epoch_backend::keys_for_rotation(cfg.rotate_bytes);
Arc::new(
rustock_storage::epoch_store::EpochTrieStore::open_read_only_with(
std::path::Path::new(&trie_dir),
cfg,
rustock_storage::TrieEngine::parse(&engine)?.factory(expected),
)
.with_context(|| format!("opening {trie_dir} as an epoch store ({engine})"))?,
)
};

let root_bytes = store.get(root.as_slice()).with_context(|| {
format!("the state root of #{at} is not in {trie_dir}; a collected store answers only \
above its watermark, and an archive only to the height it was built to")
})?;
let node = TrieNode::from_message(&root_bytes, store.as_ref());
println!("state at #{at}\n");

for raw in &pos[2..] {
let hex = raw.trim().trim_start_matches("0x");
let bytes = alloy_primitives::hex::decode(hex)
.with_context(|| format!("{raw} is not a hex address"))?;
let key = code_key_from_bytes(&bytes);
match node.get(&TrieKeySlice::from_key(&key), store.as_ref()) {
None => println!("0x{hex}\n NO CODE -- an EOA, or a contract with none\n"),
Some(code) => {
let (sels, insns) = decode(&code);
println!("0x{hex}");
println!(" code {} octets, {insns} instructions", code.len());
if code.first() == Some(&0xfc) {
println!(" header RSK 0xFC code header present");
}
println!(" PUSH4 constants ({}), likely selectors:", sels.len());
for chunk in sels.chunks(8) {
let line: Vec<String> =
chunk.iter().map(|s| format!("0x{}", alloy_primitives::hex::encode(s))).collect();
println!(" {}", line.join(" "));
}
if has("hex") {
println!(" {}", alloy_primitives::hex::encode(&code));
}
println!();
}
}
}
Ok(())
}
Loading
Loading