Skip to content

Commit

Permalink
♻ alphabet refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Jan 15, 2024
1 parent 56995f1 commit 98eeab3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 28 deletions.
43 changes: 22 additions & 21 deletions crates/fault/src/providers/alphabet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,48 @@ type AlphabetClaimConstruction = sol! { tuple(uint256, uint256) };

/// The [AlphabetTraceProvider] is a [TraceProvider] that provides the correct trace for the mock Alphabet VM.
pub struct AlphabetTraceProvider {
/// The absolute prestate of the alphabet VM is the setup state.
/// This will be the ascii representation of letter prior to the first
/// in the honest alphabet trace.
pub absolute_prestate: u8,
/// The absolute prestate of the alphabet VM is the setup state. This will be the ascii representation of letter
/// prior to the first in the honest alphabet trace.
pub absolute_prestate: [u8; 32],
/// The maximum depth of the dispute game position tree.
pub max_depth: u8,
}

impl AlphabetTraceProvider {
pub fn new(absolute_prestate: u8, max_depth: u8) -> Self {
pub fn new(absolute_prestate: u64, max_depth: u8) -> Self {
Self {
absolute_prestate,
absolute_prestate: U256::from(absolute_prestate).to_be_bytes(),
max_depth,
}
}
}

#[async_trait::async_trait]
impl TraceProvider<[u8; 1]> for AlphabetTraceProvider {
async fn absolute_prestate(&self) -> Result<Arc<[u8; 1]>> {
Ok(Arc::new([self.absolute_prestate]))
impl TraceProvider<[u8; 32]> for AlphabetTraceProvider {
async fn absolute_prestate(&self) -> Result<Arc<[u8; 32]>> {
Ok(Arc::new(self.absolute_prestate))
}

async fn absolute_prestate_hash(&self) -> Result<Claim> {
let prestate = U256::from(self.absolute_prestate);
let prestate = U256::from_be_bytes(self.absolute_prestate);
let mut prestate_hash = keccak256(<sol!(uint256)>::abi_encode(&prestate));
prestate_hash[0] = VMStatus::Unfinished as u8;
Ok(prestate_hash)
}

async fn state_at(&self, position: Position) -> Result<Arc<[u8; 1]>> {
let absolute_prestate = self.absolute_prestate as u64;
async fn state_at(&self, position: Position) -> Result<Arc<[u8; 32]>> {
let absolute_prestate = u64::from_be_bytes(self.absolute_prestate[24..32].try_into()?);
let trace_index = position.trace_index(self.max_depth);

let state = (absolute_prestate + trace_index + 1)
.try_into()
.unwrap_or(self.absolute_prestate + 2u8.pow(self.max_depth as u32));
Ok(Arc::new([state]))
// TODO: Handle wrapping.
let state = absolute_prestate + trace_index + 1;
Ok(Arc::new(U256::from(state).to_be_bytes()))
}

async fn state_hash(&self, position: Position) -> Result<Claim> {
let state_sol = (
U256::from(position.trace_index(self.max_depth)),
U256::from(self.state_at(position).await?[0]),
U256::from_be_bytes(*self.state_at(position).await?),
);
let mut state_hash = keccak256(AlphabetClaimConstruction::abi_encode(&state_sol));
state_hash[0] = VMStatus::Invalid as u8;
Expand All @@ -75,11 +73,11 @@ mod test {
#[tokio::test]
async fn alphabet_encoding() {
let provider = AlphabetTraceProvider {
absolute_prestate: b'a',
absolute_prestate: U256::from(b'a').to_be_bytes(),
max_depth: 4,
};

let prestate_sol = U256::from(provider.absolute_prestate().await.unwrap()[0]);
let prestate_sol = U256::from_be_bytes(*provider.absolute_prestate().await.unwrap());
let prestate = <sol!(uint256)>::abi_encode(&prestate_sol);
assert_eq!(
hex!("0000000000000000000000000000000000000000000000000000000000000061"),
Expand All @@ -97,7 +95,7 @@ mod test {
#[tokio::test]
async fn alphabet_trace_at() {
let provider = AlphabetTraceProvider {
absolute_prestate: b'a',
absolute_prestate: U256::from(b'a').to_be_bytes(),
max_depth: 4,
};

Expand All @@ -110,7 +108,10 @@ mod test {
keccak256(AlphabetClaimConstruction::abi_encode(&expected_encoded));
expected_hash[0] = VMStatus::Invalid as u8;

assert_eq!(provider.state_at(position).await.unwrap()[0], expected);
assert_eq!(
provider.state_at(position).await.unwrap(),
U256::from(expected).to_be_bytes().into()
);
assert_eq!(provider.state_hash(position).await.unwrap(), expected_hash);
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/fault/src/providers/cannon.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! This module contains the implementation of the [crate::TraceProvider] trait for calling out to `cannon` to fetch
//! This module contains the implementation of the [crate::TraceProvider] trait for calling out to `cannon` to fetch
//! state witnesses and proof values.

use crate::{Gindex, Position, TraceProvider};
Expand Down
22 changes: 16 additions & 6 deletions crates/fault/src/solvers/alpha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,19 +221,19 @@ pub mod rules {
mod test {
use super::*;
use crate::{providers::AlphabetTraceProvider, ClaimData, FaultDisputeSolver};
use alloy_primitives::{hex, Address, U128};
use alloy_primitives::{hex, Address, U128, U256};
use durin_primitives::{Claim, DisputeSolver, GameStatus};
use tokio::sync::Mutex;

fn mocks() -> (
FaultDisputeSolver<
[u8; 1],
[u8; 32],
AlphabetTraceProvider,
AlphaClaimSolver<[u8; 1], AlphabetTraceProvider>,
AlphaClaimSolver<[u8; 32], AlphabetTraceProvider>,
>,
Claim,
) {
let provider = AlphabetTraceProvider::new(b'a', 4);
let provider = AlphabetTraceProvider::new(b'a' as u64, 4);
let claim_solver = AlphaClaimSolver::new(provider);
let solver = FaultDisputeSolver::new(claim_solver);
let root_claim = Claim::from_slice(&hex!(
Expand Down Expand Up @@ -425,11 +425,21 @@ mod test {
let (solver, root_claim) = mocks();
let cases = [
(
FaultSolverResponse::Step(true, 4, Arc::new([b'a']), Arc::new([])),
FaultSolverResponse::Step(
true,
4,
Arc::new(U256::from(b'a').to_be_bytes()),
Arc::new([]),
),
true,
),
(
FaultSolverResponse::Step(false, 4, Arc::new([b'b']), Arc::new([])),
FaultSolverResponse::Step(
false,
4,
Arc::new(U256::from(b'b').to_be_bytes()),
Arc::new([]),
),
false,
),
];
Expand Down

0 comments on commit 98eeab3

Please sign in to comment.