Skip to content

Commit

Permalink
Mock out FaultDisputeState
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Sep 9, 2023
1 parent c15b203 commit 32dbc68
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 38 deletions.
4 changes: 3 additions & 1 deletion crates/fault/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
extern crate durin_primitives;

mod position;
mod response;
mod state;
mod traits;

pub mod prelude {
pub use super::{position::*, traits::*};
pub use super::{position::*, response::*, traits::*};
}
12 changes: 12 additions & 0 deletions crates/fault/src/response.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//! Holds the response type for a [crate::prelude::FaultDisputeGame]

/// 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,
}
57 changes: 57 additions & 0 deletions crates/fault/src/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//! This module contains the in-memory represtentation of a
//! [crate::prelude::FaultDisputeGame]'s state

#![allow(dead_code, unused_variables)]

use crate::prelude::{FaultDisputeGame, Position};
use durin_primitives::{Claim, DisputeGame, GameStatus};

/// The [ClaimData] struct holds the data associated with a claim within a
/// [crate::prelude::FaultDisputeGame]'s state on-chain.
pub struct ClaimData<P: Position> {
parent_index: u32,
countered: bool,
value: Claim,
position: P,
clock: u64,
}

/// the [FaultDisputeState] struct holds the in-memory representation of a
/// [crate::prelude::FaultDisputeGame]'s state as well as its root claim and
/// local status.
pub struct FaultDisputeState<P: Position> {
/// The [FaultDisputeState] is modeled as a directed acyclical graph (DAG) of
/// [ClaimData] structs pointing to their parents, all the way up to the root
/// claim of the dispute game.
pub state: Vec<ClaimData<P>>,
/// The root claim is the claim that commits to the entirety of the backend
/// VM's trace. The outcome of the game determines if this claim is true or
/// false.
root_claim: Claim,
/// The status of the dispute game.
status: GameStatus,
}

impl DisputeGame for FaultDisputeState<u128> {
fn root_claim(&self) -> Claim {
self.root_claim
}

fn status(&self) -> &GameStatus {
&self.status
}

fn resolve(&mut self) -> &GameStatus {
&self.status
}
}

impl FaultDisputeGame<u128> for FaultDisputeState<u128> {
fn step(&mut self, claim: ClaimData<u128>, is_attack: bool) -> anyhow::Result<()> {
todo!()
}

fn do_move(&mut self, claim: ClaimData<u128>, is_attack: bool) -> anyhow::Result<()> {
todo!()
}
}
22 changes: 19 additions & 3 deletions crates/fault/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
//! This module holds traits related to the [FaultDisputeGame]

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

/// 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
/// fault [crate::dispute_game::GameType] variants.
pub trait FaultDisputeGame: DisputeGame {
/* todo */
pub trait FaultDisputeGame<P: Position>: DisputeGame {
/// Performs an move against the given [ClaimData].
fn do_move(&mut self, claim: ClaimData<P>, is_attack: bool) -> anyhow::Result<()>;

/// Step against the given [ClaimData].
fn step(&mut self, claim: ClaimData<P>, is_attack: bool) -> anyhow::Result<()>;
}

pub trait FaultDisputeSolver: DisputeSolver<FaultSolverResponse, FaultDisputeState<u128>> {
/// Returns the raw state (in bytes) at the given position.
fn trace_at(&self, position: u128) -> anyhow::Result<Vec<u8>>;

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

/// The [Position] trait defines the interface of a generalized index within a binary tree.
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod dispute_game;
pub use dispute_game::{Claim, GameStatus, GameType};

mod traits;
pub use traits::{DisputeAgent, DisputeGame};
pub use traits::{DisputeGame, DisputeSolver};

// Re-export alloy primitives
pub use alloy_primitives::*;
50 changes: 17 additions & 33 deletions crates/primitives/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
//! The traits module contains traits used throughout the library.

use crate::{
dispute_game::{Claim, GameType},
GameStatus,
};
use alloy_primitives::Bytes;
use crate::{dispute_game::Claim, GameStatus};

/// The [DisputeGame] trait is the highest level trait in the library, describing
/// a simple primitive dispute. It has several key properties:
/// the state of a simple primitive dispute. It has several key properties:
///
/// - It houses a root [Claim], a 32 byte commitment, which is the claim being
/// disputed.
/// - It has a [GameType], which indicates the type of dispute game being played.
/// - It has a [GameStatus], which indicates the current status of the dispute.
/// - It can exist in one of three states, as indicated by the [GameStatus] enum.
/// 1. [GameStatus::InProgress] - The dispute game is still in progress.
/// 2. [GameStatus::ChallengerWins] - The challenger of the root claim has won
/// the dispute game.
/// 3. [GameStatus::DefenderWins] - The defender of the root claim has won the
/// dispute game.
/// - It has a method to resolve the dispute, which returns the [GameStatus]
/// after resolution. The resolution mechanism can be anything - a fault proof,
/// a validity proof, a multisig, etc. It is up to the implementation of the
/// dispute game to determine the resolution mechanism.
///
/// TODO: This trait should be generic over the backend that the game is being played on,
/// i.e. onchain vs. local vs. arbiter, etc. We'll need another trait that describes
/// the generic interaction with the backend for a [DisputeGame], and another for
/// a [crate::FaultDisputeGame].
pub trait DisputeGame {
/// Returns the root claim of the dispute game. The root claim is a 32 byte
/// commitment to what is being disputed.
Expand All @@ -30,29 +25,18 @@ pub trait DisputeGame {
/// a 32 byte commitment.
fn root_claim(&self) -> Claim;

/// Returns the type of the dispute game being played.
fn game_type(&self) -> GameType;

/// Returns the current status of the dispute game.
fn status(&self) -> GameStatus;

/// Returns the UNIX timestamp of the creation of the dispute game on-chain.
fn created_at(&self) -> u64;

/// Returns the extra data passed to the [DisputeGame] by its creator. This
/// data is generic and it is up to the implementation of the game to
/// determine its decoding.
fn extra_data(&self) -> Bytes;
fn status(&self) -> &GameStatus;

/// Resolves the dispute game, returning the [GameStatus] after resolution.
fn resolve(&self) -> GameStatus;
fn resolve(&mut self) -> &GameStatus;
}

/// The [DisputeAgent] trait describes the base functionality of a dispute agent
/// for any given [DisputeGame]. It serves as the highest level agent trait, and
/// only enforces functionality that is common to all dispute agents.
///
/// All other agent traits should be subtraits of the [DisputeAgent].
pub trait DisputeAgent<DG: DisputeGame> {
/* todo */
/// The [DisputeSolver] trait describes the base functionality of a solver for
/// a [DisputeGame].
pub trait DisputeSolver<R, DG: DisputeGame> {
/// 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: Claim) -> R;
}

0 comments on commit 32dbc68

Please sign in to comment.