Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add FaultProofFixture definition #70

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ exclude = ["**/target", "benches/"]
# General
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde_repr = "0.1"
thiserror = "1"
color-eyre = "0.6"
lazy_static = "1"
Expand Down
5 changes: 4 additions & 1 deletion crates/op-test-vectors/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ edition.workspace = true
[dependencies]
# Core
serde.workspace = true
serde_repr.workspace = true
color-eyre.workspace = true
hashbrown.workspace = true

Expand All @@ -24,6 +25,8 @@ alloy-consensus.workspace = true
op-alloy-rpc-types.workspace = true
op-alloy-consensus.workspace = true

# Kona
kona-primitives.workspace = true

[dev-dependencies]
serde_json.workspace = true
kona-primitives.workspace = true
144 changes: 144 additions & 0 deletions crates/op-test-vectors/src/faultproof.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
//! Module containing the fault proof test fixture.

use alloy_primitives::{BlockHash, BlockNumber, Bytes, B256};
use hashbrown::HashMap;
use kona_primitives::RollupConfig;
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};

/// The fault proof fixture is the top-level object that contains
/// everything needed to run a fault proof test.
#[derive(Serialize, Deserialize, Debug, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct FaultProofFixture {
/// The inputs to the fault proof test.
pub inputs: FaultProofInputs,
/// The expected status of the fault proof test.
pub expected_status: FaultProofStatus,
/// The witness data for the fault proof test.
pub witness_data: HashMap<B256, Bytes>,
}

/// The fault proof inputs are the inputs to the fault proof test.
#[derive(Serialize, Deserialize, Debug, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct FaultProofInputs {
/// The L1 head block hash.
pub l1_head: BlockHash,
/// The L2 head block hash.
pub l2_head: BlockHash,
/// The claimed L2 output root to validate.
pub l2_claim: B256,
/// The agreed L2 output root to start derivation from.
pub l2_output_root: B256,
/// The L2 block number that the claim is from.
pub l2_block_number: BlockNumber,
/// The configuration of the l2 chain.
pub rollup_config: RollupConfig,
}

/// The fault proof status is the result of executing the fault proof program.
#[derive(Serialize_repr, Deserialize_repr, Debug, Default, PartialEq, Eq)]
#[repr(u8)]
pub enum FaultProofStatus {
/// The claim is valid.
#[default]
Valid = 0,
/// The claim is invalid.
Invalid = 1,
/// Executing the program resulted in a panic.
Panic = 2,
/// The program has not exited.
Unfinished = 3,
/// The status is unknown.
Unknown,
}

impl TryFrom<u8> for FaultProofStatus {
type Error = String;

fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Ok(FaultProofStatus::Valid),
1 => Ok(FaultProofStatus::Invalid),
2 => Ok(FaultProofStatus::Panic),
3 => Ok(FaultProofStatus::Unfinished),
_ => Ok(FaultProofStatus::Unknown),
}
}
}

impl From<FaultProofStatus> for u8 {
fn from(status: FaultProofStatus) -> u8 {
status as u8
}
}

#[cfg(test)]
mod tests {
use kona_primitives::BASE_MAINNET_CONFIG;

use super::*;

#[test]
fn test_serialize_fault_proof_status() {
let statuses = vec![
FaultProofStatus::Valid,
FaultProofStatus::Invalid,
FaultProofStatus::Panic,
FaultProofStatus::Unfinished,
FaultProofStatus::Unknown,
];

for status in statuses {
let serialized_status =
serde_json::to_string(&status).expect("failed to serialize status");
let deserialized_status = serde_json::from_str::<FaultProofStatus>(&serialized_status)
.expect("failed to deserialize status");
assert_eq!(status, deserialized_status);
}
}

#[test]
fn test_serialize_fault_proof_inputs() {
let inputs = FaultProofInputs {
l1_head: B256::from([1; 32]),
l2_head: B256::from([2; 32]),
l2_claim: B256::from([3; 32]),
l2_output_root: B256::from([4; 32]),
l2_block_number: 1337,
rollup_config: BASE_MAINNET_CONFIG,
};

let serialized_inputs = serde_json::to_string(&inputs).expect("failed to serialize inputs");
let deserialized_inputs = serde_json::from_str::<FaultProofInputs>(&serialized_inputs)
.expect("failed to deserialize inputs");
assert_eq!(inputs, deserialized_inputs);
}

#[test]
fn test_serialize_fault_proof_fixture() {
let mut witness_data = HashMap::new();
witness_data.insert(B256::random(), Bytes::from([1; 32]));
witness_data.insert(B256::random(), Bytes::from([2; 32]));

let fixture = FaultProofFixture {
inputs: FaultProofInputs {
l1_head: B256::from([1; 32]),
l2_head: B256::from([2; 32]),
l2_claim: B256::from([3; 32]),
l2_output_root: B256::from([4; 32]),
l2_block_number: 1337,
rollup_config: BASE_MAINNET_CONFIG,
},
expected_status: FaultProofStatus::Valid,
witness_data,
};

let serialized_fixture =
serde_json::to_string(&fixture).expect("failed to serialize fixture");
let deserialized_fixture = serde_json::from_str::<FaultProofFixture>(&serialized_fixture)
.expect("failed to deserialize fixture");
assert_eq!(fixture, deserialized_fixture);
}
}
2 changes: 2 additions & 0 deletions crates/op-test-vectors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@

pub mod derivation;

pub mod faultproof;

pub mod execution;