Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚧 Sweep Resolution #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions crates/fault/src/solvers/alpha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ mod test {
value: claim,
position: 1,
clock: 0,
countered: false,
}],
claim,
GameStatus::InProgress,
Expand Down Expand Up @@ -290,20 +291,23 @@ mod test {
value: root_claim,
position: 1,
clock: 0,
countered: false,
},
ClaimData {
parent_index: 0,
visited: true,
value: solver.provider().state_hash(2).unwrap(),
position: 2,
clock: 0,
countered: false,
},
ClaimData {
parent_index: 1,
visited: false,
value: claim,
position: 4,
clock: 0,
countered: false,
},
],
root_claim,
Expand All @@ -328,6 +332,7 @@ mod test {
value: root_claim,
position: 1,
clock: 0,
countered: false,
},
// Right level; Wrong claim - SKIP
ClaimData {
Expand All @@ -336,6 +341,7 @@ mod test {
value: root_claim,
position: 2,
clock: 0,
countered: false,
},
// Wrong level; Right claim - DEFEND
ClaimData {
Expand All @@ -344,6 +350,7 @@ mod test {
value: solver.provider().state_hash(4).unwrap(),
position: 4,
clock: 0,
countered: false,
},
// Right level; Wrong claim - SKIP
ClaimData {
Expand All @@ -352,6 +359,7 @@ mod test {
value: root_claim,
position: 8,
clock: 0,
countered: false,
},
],
root_claim,
Expand Down Expand Up @@ -395,6 +403,7 @@ mod test {
value: root_claim,
position: 1,
clock: 0,
countered: false,
},
// Honest Attack
ClaimData {
Expand All @@ -403,6 +412,7 @@ mod test {
value: solver.provider().state_hash(2).unwrap(),
position: 2,
clock: 0,
countered: false,
},
// Wrong level; Wrong claim - ATTACK
ClaimData {
Expand All @@ -411,6 +421,7 @@ mod test {
value: root_claim,
position: 4,
clock: 0,
countered: false,
},
// Honest Attack
ClaimData {
Expand All @@ -419,6 +430,7 @@ mod test {
value: solver.provider().state_hash(8).unwrap(),
position: 8,
clock: 0,
countered: false,
},
// Wrong level; Wrong claim - ATTACK STEP
ClaimData {
Expand All @@ -431,6 +443,7 @@ mod test {
},
position: 16,
clock: 0,
countered: false,
},
],
root_claim,
Expand Down
66 changes: 62 additions & 4 deletions crates/fault/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

#![allow(dead_code, unused_variables)]

use crate::{Clock, FaultDisputeGame, Position};
use crate::{ChessClock, Clock, FaultDisputeGame, Gindex, Position};
use durin_primitives::{Claim, DisputeGame, GameStatus};
use std::time::{SystemTime, UNIX_EPOCH};

/// The [ClaimData] struct holds the data associated with a claim within a
/// [crate::FaultDisputeGame]'s state on-chain.
#[derive(Debug, Clone, Copy)]
pub struct ClaimData {
pub parent_index: u32,
pub visited: bool,
pub countered: bool,
pub value: Claim,
pub position: Position,
pub clock: Clock,
pub visited: bool,
}

/// the [FaultDisputeState] struct holds the in-memory representation of a
Expand Down Expand Up @@ -60,8 +62,64 @@ impl DisputeGame for FaultDisputeState {
&self.status
}

fn resolve(&mut self) -> &GameStatus {
&self.status
// TODO(clabby): Generic resolution mechanisms
fn resolve(&mut self, sim: bool) -> anyhow::Result<GameStatus> {
if self.status != GameStatus::InProgress {
return Ok(self.status);
}

let mut left_most_index = self.state.len() - 1;
let mut left_most_trace_index = u64::MAX;
for i in (0..=left_most_index).rev() {
let claim = &self
.state()
.get(i)
.ok_or(anyhow::anyhow!("Could not fetch claim from state"))?;

if claim.countered {
continue;
}

let trace_index = claim.position.trace_index(self.max_depth);
if trace_index < left_most_trace_index {
left_most_trace_index = trace_index;
left_most_index = i + 1;
}
}

let left_most_uncontested = self
.state()
.get(left_most_index)
.ok_or(anyhow::anyhow!("Could not fetch claim from state"))?;

let status = if left_most_uncontested.position.depth() % 2 == 0
&& left_most_trace_index != u64::MAX
{
GameStatus::DefenderWins
} else {
GameStatus::ChallengerWins
};

if !sim {
let parent_index = left_most_uncontested.parent_index;
let opposing_clock = if parent_index == u32::MAX {
left_most_uncontested.clock
} else {
self.state()
.get(parent_index as usize)
.ok_or(anyhow::anyhow!("Could not fetch parent claim from state"))?
.clock
};

let now = SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs();
if opposing_clock.duration() + (now - opposing_clock.timestamp()) <= 604800 >> 1 {
anyhow::bail!("Clocks have not expired")
}

self.status = status;
}

Ok(status)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/src/dispute_game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl TryFrom<u8> for GameType {
}

/// The [GameStatus] enum is used to indicate the status of a dispute game.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Eq, PartialEq, Copy)]
pub enum GameStatus {
/// The [GameStatus::InProgress] variant is used to indicate that the dispute game is
/// still in progress.
Expand Down
6 changes: 5 additions & 1 deletion crates/primitives/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ pub trait DisputeGame {
fn status(&self) -> &GameStatus;

/// Resolves the dispute game, returning the [GameStatus] after resolution.
fn resolve(&mut self) -> &GameStatus;
///
/// ### Takes
/// - `sim`: A boolean indicating whether or not the resolution modifies
/// the state and updates the status.
fn resolve(&mut self, sim: bool) -> anyhow::Result<GameStatus>;
}

/// The [DisputeSolver] trait describes the base functionality of a solver for
Expand Down
Loading