diff --git a/crates/fault/src/lib.rs b/crates/fault/src/lib.rs index caa3489..20f7535 100644 --- a/crates/fault/src/lib.rs +++ b/crates/fault/src/lib.rs @@ -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::*}; } diff --git a/crates/fault/src/response.rs b/crates/fault/src/response.rs new file mode 100644 index 0000000..f5337b7 --- /dev/null +++ b/crates/fault/src/response.rs @@ -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, +} diff --git a/crates/fault/src/state.rs b/crates/fault/src/state.rs new file mode 100644 index 0000000..0f64ff5 --- /dev/null +++ b/crates/fault/src/state.rs @@ -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 { + 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 { + /// 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>, + /// 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 { + fn root_claim(&self) -> Claim { + self.root_claim + } + + fn status(&self) -> &GameStatus { + &self.status + } + + fn resolve(&mut self) -> &GameStatus { + &self.status + } +} + +impl FaultDisputeGame for FaultDisputeState { + fn step(&mut self, claim: ClaimData, is_attack: bool) -> anyhow::Result<()> { + todo!() + } + + fn do_move(&mut self, claim: ClaimData, is_attack: bool) -> anyhow::Result<()> { + todo!() + } +} diff --git a/crates/fault/src/traits.rs b/crates/fault/src/traits.rs index 223690b..dc0d729 100644 --- a/crates/fault/src/traits.rs +++ b/crates/fault/src/traits.rs @@ -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: DisputeGame { + /// Performs an move against the given [ClaimData]. + fn do_move(&mut self, claim: ClaimData

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

, is_attack: bool) -> anyhow::Result<()>; +} + +pub trait FaultDisputeSolver: DisputeSolver> { + /// Returns the raw state (in bytes) at the given position. + fn trace_at(&self, position: u128) -> anyhow::Result>; + + /// Returns the state hash (in bytes) at the given position. + fn state_hash(&self, position: u128) -> anyhow::Result; } /// The [Position] trait defines the interface of a generalized index within a binary tree. diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index 8d6eeca..952aac0 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -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::*; diff --git a/crates/primitives/src/traits.rs b/crates/primitives/src/traits.rs index e6a7bfd..e2aedc8 100644 --- a/crates/primitives/src/traits.rs +++ b/crates/primitives/src/traits.rs @@ -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. @@ -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 { - /* todo */ +/// The [DisputeSolver] trait describes the base functionality of a solver for +/// a [DisputeGame]. +pub trait DisputeSolver { + /// 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; }