|
| 1 | +//! Integration test for the SP1 STF prover's `execute` path. |
| 2 | +//! |
| 3 | +//! `execute` runs the guest in the SP1 RISC-V executor without proving, so it |
| 4 | +//! is cheap enough for regular runs while still exercising the real guest |
| 5 | +//! and the `StfInput`/`StfPublicValues` serialization/deserialization. |
| 6 | +
|
| 7 | + |
| 8 | +use ethlambda_prover_core::{StfInput, StfProver, StfPublicValues}; |
| 9 | +use ethlambda_prover_sp1::Sp1Prover; |
| 10 | +use ethlambda_state_transition::{process_block, process_slots}; |
| 11 | +use ethlambda_types::{ |
| 12 | + block::{Block, BlockBody}, |
| 13 | + primitives::{H256, HashTreeRoot as _}, |
| 14 | + state::{State, Validator}, |
| 15 | +}; |
| 16 | + |
| 17 | +const GENESIS_TIME: u64 = 0; |
| 18 | +const NUM_VALIDATORS: u64 = 4; |
| 19 | + |
| 20 | + |
| 21 | +fn valid_transition() -> (StfInput, StfPublicValues) { |
| 22 | + let validators = (0..NUM_VALIDATORS) |
| 23 | + .map(|index| Validator { |
| 24 | + attestation_pubkey: [0u8; 52], |
| 25 | + proposal_pubkey: [0u8; 52], |
| 26 | + index, |
| 27 | + }) |
| 28 | + .collect(); |
| 29 | + let pre_state = State::from_genesis(GENESIS_TIME, validators); |
| 30 | + |
| 31 | + // Parent root as `process_slots` will leave it: the genesis header with its |
| 32 | + // `state_root` filled in (mirrors block_builder.rs). |
| 33 | + let mut parent_header = pre_state.latest_block_header.clone(); |
| 34 | + parent_header.state_root = pre_state.hash_tree_root(); |
| 35 | + let parent_root = parent_header.hash_tree_root(); |
| 36 | + |
| 37 | + let mut block = Block { |
| 38 | + slot: 1, |
| 39 | + proposer_index: 1 % NUM_VALIDATORS, |
| 40 | + parent_root, |
| 41 | + state_root: H256::ZERO, |
| 42 | + body: BlockBody::default(), |
| 43 | + }; |
| 44 | + |
| 45 | + let mut scratch = pre_state.clone(); |
| 46 | + process_slots(&mut scratch, block.slot).expect("process_slots"); |
| 47 | + process_block(&mut scratch, &block).expect("process_block"); |
| 48 | + block.state_root = scratch.hash_tree_root(); |
| 49 | + |
| 50 | + let expected = StfPublicValues { |
| 51 | + pre_state_root: pre_state.hash_tree_root(), |
| 52 | + block_root: block.hash_tree_root(), |
| 53 | + post_state_root: block.state_root, |
| 54 | + }; |
| 55 | + (StfInput::new(pre_state, block), expected) |
| 56 | +} |
| 57 | + |
| 58 | +#[tokio::test] |
| 59 | +async fn execute_commits_expected_roots() { |
| 60 | + // generate the inputs and the output expected state. |
| 61 | + let (input, expected) = valid_transition(); |
| 62 | + |
| 63 | + let prover = Sp1Prover::new().await; |
| 64 | + let execution_result = prover.execute(&input).await.expect("execute failed"); |
| 65 | + |
| 66 | + assert_eq!(execution_result.pre_state_root, expected.pre_state_root, "pre_state_root"); |
| 67 | + assert_eq!(execution_result.block_root, expected.block_root, "block_root"); |
| 68 | + assert_eq!( |
| 69 | + execution_result.post_state_root, expected.post_state_root, |
| 70 | + "post_state_root" |
| 71 | + ); |
| 72 | +} |
0 commit comments