Skip to content

Commit

Permalink
🧹 Consolidate + VMStatus byte
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Sep 9, 2023
1 parent 4318d15 commit cc02867
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 59 deletions.
27 changes: 0 additions & 27 deletions crates/fault/src/clock.rs

This file was deleted.

10 changes: 2 additions & 8 deletions crates/fault/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,11 @@ extern crate alloy_primitives;
extern crate alloy_sol_types;
extern crate durin_primitives;

mod clock;
pub use clock::Clock;

mod position;
pub use position::{compute_gindex, Position};
mod types;
pub use types::*;

mod providers;

mod response;
pub use response::FaultSolverResponse;

mod state;
pub use state::{ClaimData, FaultDisputeState};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! This modules contains trace providers for the variants of the [crate::FaultDisputeGame].
//! This module contains the implementation of the [crate::TraceProvider] trait for the
//! mock Alphabet VM.

use crate::{Gindex, Position, TraceProvider};
use crate::{Gindex, Position, TraceProvider, VMStatus};
use alloy_primitives::{keccak256, U256};
use alloy_sol_types::{sol, SolType};
use durin_primitives::Claim;
Expand All @@ -18,7 +19,7 @@ sol! {
}

/// The [AlphabetTraceProvider] is a [TraceProvider] that provides the
struct AlphabetTraceProvider {
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.
Expand All @@ -36,7 +37,9 @@ impl TraceProvider<u8> for AlphabetTraceProvider {
let prestate_sol = AlphabetPrestate {
letter: self.absolute_prestate,
};
keccak256(AlphabetPrestate::encode(&prestate_sol))
let mut prestate_hash = keccak256(AlphabetPrestate::encode(&prestate_sol));
prestate_hash[0] = VMStatus::Unfinished as u8;
prestate_hash
}

fn trace_at(&self, position: Position) -> anyhow::Result<u8> {
Expand All @@ -53,7 +56,9 @@ impl TraceProvider<u8> for AlphabetTraceProvider {
instruction: U256::from(position.trace_index(self.max_depth)),
claim: U256::from(self.trace_at(position)?),
};
Ok(keccak256(AlphabetEncoding::encode(&state_sol)))
let mut state_hash = keccak256(AlphabetEncoding::encode(&state_sol));
state_hash[0] = VMStatus::Invalid as u8;
Ok(state_hash)
}
}

Expand All @@ -79,9 +84,10 @@ mod test {
prestate.as_slice()
);

let prestate_hash = provider.absolute_prestate_hash();
let mut prestate_hash = provider.absolute_prestate_hash();
prestate_hash[0] = VMStatus::Unfinished as u8;
assert_eq!(
hex!("f0ecb75dd1820844c57b6762233d4e26853b3a7b8157bbd9f41f280a0f1cee9b"),
hex!("03ecb75dd1820844c57b6762233d4e26853b3a7b8157bbd9f41f280a0f1cee9b"),
prestate_hash.as_slice()
);
}
Expand All @@ -101,7 +107,8 @@ mod test {
instruction: U256::from(i),
claim: U256::from(expected),
};
let expected_hash = keccak256(AlphabetEncoding::encode(expected_encoded));
let mut expected_hash = keccak256(AlphabetEncoding::encode(expected_encoded));
expected_hash[0] = VMStatus::Invalid as u8;

assert_eq!(provider.trace_at(position).unwrap(), expected);
assert_eq!(provider.state_hash(position).unwrap(), expected_hash);
Expand Down
4 changes: 4 additions & 0 deletions crates/fault/src/providers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//! This modules contains trace providers for the variants of the [crate::FaultDisputeGame].

mod alphabet;
pub use self::alphabet::AlphabetTraceProvider;
12 changes: 0 additions & 12 deletions crates/fault/src/response.rs

This file was deleted.

3 changes: 1 addition & 2 deletions crates/fault/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//! This module holds traits related to the [FaultDisputeGame]

use crate::{
position::Position,
state::{ClaimData, FaultDisputeState},
FaultSolverResponse,
FaultSolverResponse, Position,
};
use durin_primitives::{Claim, DisputeGame, DisputeSolver};

Expand Down
46 changes: 44 additions & 2 deletions crates/fault/src/position.rs → crates/fault/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
//! The position module holds the [Position] type and the implementation of the [Gindex]
//! trait on it.
//! The position module holds the types specific to the [crate::FaultDisputeGame] solver.

use crate::ChessClock;
use crate::Gindex;

pub type Position = u128;
pub type Clock = u128;

/// The [FaultSolverResponse] enum describes the response that a solver should
/// return when asked to make a move.
pub enum FaultSolverResponse {
/// A response indicating that the proper move is to attack the given claim.
Attack,
/// A response indicating that the proper move is to defend the given claim.
Defend,
/// A response indicating that the proper move is to skip the given claim.
Skip,
}

/// The [VMStatus] enum describes the status of a VM at a given position.
/// - [VMStatus::Valid]: The VM is exited with a valid status.
/// - [VMStatus::Invalid]: The VM is exited with an invalid status.
/// - [VMStatus::Panic]: The VM is exited with a panic status.
/// - [VMStatus::Unfinished]: The VM is not yet exited.
pub enum VMStatus {
Valid = 0,
Invalid = 1,
Panic = 2,
Unfinished = 3,
}

/// Computes a generalized index from a depth and index at depth.
///
Expand Down Expand Up @@ -53,10 +77,28 @@ impl Gindex for Position {
}
}

impl ChessClock for Clock {
fn duration(&self) -> u64 {
(self >> 64) as u64
}

fn timestamp(&self) -> u64 {
(self & u64::MAX as u128) as u64
}
}

#[cfg(test)]
mod test {
use super::ChessClock;
use super::{Gindex, Position};

#[test]
fn chess_clock_correctness() {
let clock = 0xa5000000000000001;
assert_eq!(clock.duration(), 10);
assert_eq!(clock.timestamp(), 5764607523034234881);
}

/// A helper struct for testing the [Position] trait implementation for [std::u128].
/// 0. `u64` - `depth`
/// 1. `u64` - `index_at_depth`
Expand Down

0 comments on commit cc02867

Please sign in to comment.