Skip to content

Commit

Permalink
🚧 Mock out blanket fault solver impl
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Sep 9, 2023
1 parent 940d757 commit 91bfb09
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 34 deletions.
21 changes: 11 additions & 10 deletions crates/fault/src/providers/alphabet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ pub struct AlphabetTraceProvider {
pub max_depth: u8,
}

impl TraceProvider<u8> for AlphabetTraceProvider {
fn absolute_prestate(&self) -> &u8 {
&self.absolute_prestate
impl TraceProvider<[u8; 1]> for AlphabetTraceProvider {
fn absolute_prestate(&self) -> [u8; 1] {
[self.absolute_prestate]
}

fn absolute_prestate_hash(&self) -> Claim {
Expand All @@ -32,19 +32,20 @@ impl TraceProvider<u8> for AlphabetTraceProvider {
prestate_hash
}

fn trace_at(&self, position: Position) -> anyhow::Result<u8> {
let absolute_prestate = *self.absolute_prestate() as u64;
fn state_at(&self, position: Position) -> anyhow::Result<[u8; 1]> {
let absolute_prestate = self.absolute_prestate as u64;
let trace_index = position.trace_index(self.max_depth);

Ok((absolute_prestate + trace_index + 1)
let state = (absolute_prestate + trace_index + 1)
.try_into()
.unwrap_or(self.absolute_prestate + 2u8.pow(self.max_depth as u32)))
.unwrap_or(self.absolute_prestate + 2u8.pow(self.max_depth as u32));
Ok([state])
}

fn state_hash(&self, position: Position) -> anyhow::Result<Claim> {
let state_sol = (
U256::from(position.trace_index(self.max_depth)),
U256::from(self.trace_at(position)?),
U256::from(self.state_at(position)?[0]),
);
let mut state_hash = keccak256(AlphabetClaimConstruction::encode(&state_sol));
state_hash[0] = VMStatus::Invalid as u8;
Expand All @@ -65,7 +66,7 @@ mod test {
max_depth: 4,
};

let prestate_sol = U256::from(*provider.absolute_prestate());
let prestate_sol = U256::from(provider.absolute_prestate()[0]);
let prestate = <sol!(uint256)>::encode_single(&prestate_sol);
assert_eq!(
hex!("0000000000000000000000000000000000000000000000000000000000000061"),
Expand Down Expand Up @@ -95,7 +96,7 @@ mod test {
let mut expected_hash = keccak256(AlphabetClaimConstruction::encode(&expected_encoded));
expected_hash[0] = VMStatus::Invalid as u8;

assert_eq!(provider.trace_at(position).unwrap(), expected);
assert_eq!(provider.state_at(position).unwrap()[0], expected);
assert_eq!(provider.state_hash(position).unwrap(), expected_hash);
}
}
Expand Down
31 changes: 29 additions & 2 deletions crates/fault/src/solver.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
//! This module contains the various implementations of the [crate::FaultDisputeSolver] trait.
//!
//! TODO

#![allow(dead_code, unused_variables)]

use crate::{FaultDisputeState, FaultSolverResponse, TraceProvider};
use durin_primitives::DisputeSolver;
use std::marker::PhantomData;

/// A [FaultDisputeSolver] is a [DisputeSolver] that is played over a fault proof VM backend. The
/// solver is responsible for honestly responding to any given [ClaimData] in a given
/// [FaultDisputeState]. It uses a [TraceProvider] to fetch the absolute prestate of the VM as
/// well as the state at any given [Position] within the tree.
pub struct FaultDisputeSolver<T, P>
where
T: AsRef<[u8]>,
P: TraceProvider<T>,
{
pub provider: P,
_phantom: PhantomData<T>,
}

impl<T, P> DisputeSolver<FaultDisputeState, FaultSolverResponse> for FaultDisputeSolver<T, P>
where
T: AsRef<[u8]>,
P: TraceProvider<T>,
{
fn available_moves(&self, game: &FaultDisputeState) -> &[FaultSolverResponse] {
todo!()
}
}
22 changes: 5 additions & 17 deletions crates/fault/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
//! This module holds traits related to the [FaultDisputeGame]

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

/// A [FaultDisputeGame] is a [DisputeGame] that is played over a FaultVM backend. This
/// trait extends the [DisputeGame] trait with functionality that is specific to the
Expand All @@ -19,29 +16,20 @@ pub trait FaultDisputeGame: DisputeGame {

/// A [TraceProvider] is a type that can provide the raw state (in bytes) at a given
/// [Position] within a [FaultDisputeGame].
pub trait TraceProvider<P> {
pub trait TraceProvider<P: AsRef<[u8]>> {
/// Returns the raw absolute prestate (in bytes).
fn absolute_prestate(&self) -> &P;
fn absolute_prestate(&self) -> P;

/// Returns the absolute prestate hash.
fn absolute_prestate_hash(&self) -> Claim;

/// Returns the raw state (in bytes) at the given position.
fn trace_at(&self, position: Position) -> anyhow::Result<P>;
fn state_at(&self, position: Position) -> anyhow::Result<P>;

/// Returns the state hash at the given position.
fn state_hash(&self, position: Position) -> anyhow::Result<Claim>;
}

/// A [FaultDisputeSolver] is a [DisputeSolver] that is played over a fault proof VM backend. The
/// solver is responsible for honestly responding to any given [ClaimData] in a given
/// [FaultDisputeState]. It uses a [TraceProvider] to fetch the absolute prestate of the VM as
/// well as the state at any given [Position] within the tree.
pub trait FaultDisputeSolver<AP, P: TraceProvider<AP>>:
DisputeSolver<FaultDisputeState, ClaimData, FaultSolverResponse>
{
}

/// The [Gindex] trait defines the interface of a generalized index within a binary tree.
/// A "Generalized Index" is calculated as `2^{depth} + index_at_depth`.
pub trait Gindex {
Expand Down
10 changes: 5 additions & 5 deletions crates/primitives/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ pub trait DisputeGame {

/// The [DisputeSolver] trait describes the base functionality of a solver for
/// a [DisputeGame].
pub trait DisputeSolver<DG: DisputeGame, C, R> {
/// Returns the response of the solver provided a [DisputeGame] and a
/// [Claim] within it. The consumer of the response is responsible for
/// dispatching the action associated with it.
fn respond(&self, game: &DG, claim: C) -> R;
pub trait DisputeSolver<DG: DisputeGame, R> {
/// Returns any available responses computed by the solver provided a [DisputeGame].
/// The consumer of the response is responsible for dispatching the action associated
/// with the responses.
fn available_moves(&self, game: &DG) -> &[R];
}

0 comments on commit 91bfb09

Please sign in to comment.