Skip to content

Commit 7621ce1

Browse files
committed
add witness struct
1 parent c9dc9b4 commit 7621ce1

File tree

11 files changed

+8193
-29
lines changed

11 files changed

+8193
-29
lines changed

Cargo.lock

+16-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ alloy = "0.5"
2323
alloy-eips = "0.5"
2424
alloy-consensus = "0.5"
2525
alloy-rpc-types-eth = "0.5"
26+
alloy-rpc-types-debug = "0.5"
2627
alloy-serde = "0.5"
2728

2829
alloy-eip2930 = "0.1"
@@ -99,12 +100,8 @@ ff = { git = "https://github.com/scroll-tech/ff", branch = "feat/sp1" }
99100
# patched add rkyv support & MSRV 1.77
100101
ruint = { git = "https://github.com/scroll-tech/uint.git", branch = "v1.12.3" }
101102

102-
alloy-eip2930 = { git = "https://github.com/scroll-tech/alloy-eips", branch = "v0.4.2" }
103-
104103
alloy-primitives = { git = "https://github.com/scroll-tech/alloy-core", branch = "v0.8.14-transparent" }
105104

106-
alloy-trie = { git = "https://github.com/scroll-tech/alloy-trie", branch = "v0.7.4" }
107-
108105
revm = { git = "https://github.com/scroll-tech/revm", branch = "scroll-evm-executor/v50" }
109106
revm-interpreter = { git = "https://github.com/scroll-tech/revm", branch = "scroll-evm-executor/v50" }
110107
revm-precompile = { git = "https://github.com/scroll-tech/revm", branch = "scroll-evm-executor/v50" }

crates/primitives/src/lib.rs

+17
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub mod types;
1212

1313
pub use alloy_consensus;
1414

15+
use crate::types::TypedTransaction;
1516
pub use alloy_primitives;
1617
pub use alloy_primitives::{Address, BlockHash, B256, U256};
1718

@@ -49,6 +50,22 @@ pub trait BlockHeader: fmt::Debug {
4950
fn withdraw_root(&self) -> B256;
5051
}
5152

53+
/// BlockWitness trait
54+
pub trait BlockWitness: fmt::Debug {
55+
/// Header
56+
fn header(&self) -> &impl BlockHeader;
57+
/// Pre-state root
58+
fn pre_state_root(&self) -> B256;
59+
/// Transactions
60+
fn build_typed_transactions(
61+
&self,
62+
) -> impl Iterator<Item = Result<TypedTransaction, alloy_primitives::SignatureError>>;
63+
/// States
64+
fn states_iter(&self) -> impl Iterator<Item = impl AsRef<[u8]>>;
65+
/// Codes
66+
fn codes_iter(&self) -> impl Iterator<Item = impl AsRef<[u8]>>;
67+
}
68+
5269
// #[cfg(feature = "scroll")]
5370
// pub trait BlockScrollExt: Block {
5471
// /// start l1 queue index

crates/primitives/src/types/mod.rs

+41
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,49 @@ mod access_list;
77
mod block_header;
88
mod signature;
99
mod transaction;
10+
mod witness;
1011

1112
pub use access_list::{AccessList, AccessListItem, ArchivedAccessList, ArchivedAccessListItem};
1213
pub use block_header::{ArchivedBlockHeader, BlockHeader};
1314
pub use signature::{ArchivedSignature, Signature};
1415
pub use transaction::{ArchivedTransaction, Transaction, TypedTransaction};
16+
pub use witness::{ArchivedBlockWitness, BlockWitness};
17+
18+
#[cfg(test)]
19+
mod test {
20+
use super::*;
21+
use alloy_rpc_types_debug::ExecutionWitness;
22+
use alloy_rpc_types_eth::Block;
23+
24+
const BLOCK_2BA60C: &str =
25+
include_str!("../../../../testdata/holesky_witness/0x2ba60c/block.json");
26+
const BLOCK_2BA60D: &str =
27+
include_str!("../../../../testdata/holesky_witness/0x2ba60d/block.json");
28+
const WITNESS_2BA60C: &str =
29+
include_str!("../../../../testdata/holesky_witness/0x2ba60c/witness.json");
30+
const WITNESS_2BA60D: &str =
31+
include_str!("../../../../testdata/holesky_witness/0x2ba60d/witness.json");
32+
33+
#[test]
34+
fn test_deserialize_block() {
35+
serde_json::from_str::<Block>(BLOCK_2BA60C).unwrap();
36+
serde_json::from_str::<Block>(BLOCK_2BA60D).unwrap();
37+
serde_json::from_str::<ExecutionWitness>(WITNESS_2BA60C).unwrap();
38+
serde_json::from_str::<ExecutionWitness>(WITNESS_2BA60D).unwrap();
39+
}
40+
41+
#[test]
42+
fn test_build() {
43+
let block = serde_json::from_str::<Block>(BLOCK_2BA60D).unwrap();
44+
let prev_state_root = serde_json::from_str::<Block>(BLOCK_2BA60C)
45+
.unwrap()
46+
.header
47+
.state_root;
48+
let witness = serde_json::from_str::<ExecutionWitness>(WITNESS_2BA60D).unwrap();
49+
50+
let block_witness = BlockWitness::new_from_block(block, prev_state_root, witness);
51+
test_block_witness(block_witness);
52+
}
53+
54+
fn test_block_witness<B: crate::BlockWitness>(_block_witness: B) {}
55+
}
+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
use crate::types::{BlockHeader, Transaction, TypedTransaction};
2+
use alloy_primitives::{Bytes, B256};
3+
use alloy_rpc_types_debug::ExecutionWitness;
4+
use alloy_rpc_types_eth::Block;
5+
6+
/// Witness for a block.
7+
#[derive(Clone, Debug, PartialEq, Eq, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
8+
#[rkyv(derive(Debug, PartialEq, Eq))]
9+
pub struct BlockWitness {
10+
/// Block header representation.
11+
#[rkyv(attr(doc = "Block header representation"))]
12+
pub header: BlockHeader,
13+
/// State trie root before the block.
14+
#[rkyv(attr(doc = "State trie root before the block"))]
15+
pub pre_state_root: B256,
16+
/// Transactions in the block.
17+
#[rkyv(attr(doc = "Transactions in the block"))]
18+
pub transaction: Vec<Transaction>,
19+
/// Rlp encoded state trie nodes.
20+
#[rkyv(attr(doc = "Rlp encoded state trie nodes"))]
21+
pub states: Vec<Bytes>,
22+
/// Code bytecodes
23+
#[rkyv(attr(doc = "Code bytecodes"))]
24+
pub codes: Vec<Bytes>,
25+
}
26+
27+
impl BlockWitness {
28+
/// Creates a new block witness from a block, pre-state root, execution witness.
29+
pub fn new_from_block(block: Block, pre_state_root: B256, witness: ExecutionWitness) -> Self {
30+
let header = BlockHeader::from(block.header);
31+
let transaction = block
32+
.transactions
33+
.into_transactions()
34+
.map(|tx| Transaction::from_alloy(tx))
35+
.collect();
36+
let states = witness.state.into_values().collect();
37+
let codes = witness.codes.into_values().collect();
38+
Self {
39+
header,
40+
transaction,
41+
pre_state_root,
42+
states,
43+
codes,
44+
}
45+
}
46+
}
47+
48+
impl crate::BlockWitness for BlockWitness {
49+
fn header(&self) -> &impl crate::BlockHeader {
50+
&self.header
51+
}
52+
fn pre_state_root(&self) -> B256 {
53+
self.pre_state_root
54+
}
55+
fn build_typed_transactions(
56+
&self,
57+
) -> impl Iterator<Item = Result<TypedTransaction, alloy_primitives::SignatureError>> {
58+
self.transaction.iter().map(|tx| tx.try_into())
59+
}
60+
fn states_iter(&self) -> impl Iterator<Item = impl AsRef<[u8]>> {
61+
self.states.iter().map(|s| s.as_ref())
62+
}
63+
fn codes_iter(&self) -> impl Iterator<Item = impl AsRef<[u8]>> {
64+
self.codes.iter().map(|c| c.as_ref())
65+
}
66+
}
67+
68+
impl crate::BlockWitness for ArchivedBlockWitness {
69+
fn header(&self) -> &impl crate::BlockHeader {
70+
&self.header
71+
}
72+
fn pre_state_root(&self) -> B256 {
73+
self.pre_state_root.into()
74+
}
75+
fn build_typed_transactions(
76+
&self,
77+
) -> impl Iterator<Item = Result<TypedTransaction, alloy_primitives::SignatureError>> {
78+
self.transaction.iter().map(|tx| tx.try_into())
79+
}
80+
fn states_iter(&self) -> impl Iterator<Item = impl AsRef<[u8]>> {
81+
self.states.iter().map(|s| s.as_ref())
82+
}
83+
fn codes_iter(&self) -> impl Iterator<Item = impl AsRef<[u8]>> {
84+
self.codes.iter().map(|c| c.as_ref())
85+
}
86+
}

scripts/download.sh

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
set -ex
4+
5+
if [ $# -eq 0 ]; then
6+
echo "Usage: $0 <block_number> [url] [output_dir]"
7+
exit 0
8+
fi
9+
10+
BLOCK=$1
11+
URL=${2:-http://localhost:8545}
12+
OUT_DIR=${3:-testdata/holesky_witness}
13+
14+
HEX_BLOCK=$(printf '0x%x' $BLOCK)
15+
16+
mkdir -p ${OUT_DIR}/${HEX_BLOCK}
17+
18+
cast rpc -r $URL eth_getBlockByNumber "$HEX_BLOCK" true | jq > ${OUT_DIR}/${HEX_BLOCK}/block.json
19+
#PAYLOAD='{"jsonrpc":"2.0","method":"debug_executionWitness", "params": ["'$HEX_BLOCK'"], "id": 1}'
20+
#curl -H "Content-Type: application/json" -X POST --data "$PAYLOAD" $URL | jq .result > ${OUT_DIR}/${HEX_BLOCK}/witness.json
21+
#
22+
#PAYLOAD='{"jsonrpc":"2.0","method":"eth_getBlockByNumber", "params": ["'$HEX_BLOCK'", true], "id": 1}'
23+
#curl -H "Content-Type: application/json" -X POST --data "$PAYLOAD" $URL | jq .result > ${OUT_DIR}/${HEX_BLOCK}/block.json
24+
##cast rpc -r $URL debug_executionWitness "$HEX_BLOCK" > ${OUT_DIR}/${HEX_BLOCK}/witness.json
25+
##

scripts/download_trace.sh

-20
This file was deleted.

0 commit comments

Comments
 (0)