Skip to content

Commit

Permalink
feat: get rid of Box<dyn Error>
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenctw committed Apr 17, 2024
1 parent 686c614 commit 754c0c4
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 86 deletions.
9 changes: 9 additions & 0 deletions offchain/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions offchain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ readme = "README.md"
repository = "https://github.com/cartesi/dave"

[workspace.dependencies]
anyhow = "1.0.63"
async-recursion = "1"
async-trait = "0.1.74"
ethers = "2.0.11"
Expand Down
1 change: 1 addition & 0 deletions offchain/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ build = "build.rs"
[dependencies]
cartesi-machine = { path = "../../machine-bindings/cartesi-machine" }
cartesi-machine-sys = { path = "../../machine-bindings/cartesi-machine-sys" }
anyhow = { workspace = true }
async-recursion = { workspace = true }
async-trait = { workspace = true }
ethers = { workspace = true }
Expand Down
32 changes: 12 additions & 20 deletions offchain/core/src/arena/reader.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! This module defines the struct [StateReader] that is responsible for the reading the states
//! of tournaments
use std::{collections::HashMap, error::Error, str::FromStr, sync::Arc, time::Duration};
use std::{collections::HashMap, str::FromStr, sync::Arc, time::Duration};

use anyhow::Result;
use async_recursion::async_recursion;

use ethers::{
Expand Down Expand Up @@ -31,7 +32,7 @@ pub struct StateReader {
}

impl StateReader {
pub fn new(config: ArenaConfig) -> Result<Self, Box<dyn Error>> {
pub fn new(config: ArenaConfig) -> Result<Self> {
let provider = Provider::<Http>::try_from(config.web3_rpc_url.clone())?
.interval(Duration::from_millis(10u64));
let wallet = LocalWallet::from_str(config.web3_private_key.as_str())?;
Expand All @@ -47,7 +48,7 @@ impl StateReader {
&self,
tournament_address: Address,
match_id: MatchID,
) -> Result<Option<TournamentCreatedEvent>, Box<dyn Error>> {
) -> Result<Option<TournamentCreatedEvent>> {
let tournament =
non_leaf_tournament::NonLeafTournament::new(tournament_address, self.client.clone());
let events = tournament
Expand All @@ -68,10 +69,7 @@ impl StateReader {
}
}

async fn created_matches(
&self,
tournament_address: Address,
) -> Result<Vec<MatchCreatedEvent>, Box<dyn Error>> {
async fn created_matches(&self, tournament_address: Address) -> Result<Vec<MatchCreatedEvent>> {
let tournament = tournament::Tournament::new(tournament_address, self.client.clone());
let events: Vec<MatchCreatedEvent> = tournament
.match_created_filter()
Expand All @@ -95,7 +93,7 @@ impl StateReader {
async fn joined_commitments(
&self,
tournament_address: Address,
) -> Result<Vec<CommitmentJoinedEvent>, Box<dyn Error>> {
) -> Result<Vec<CommitmentJoinedEvent>> {
let tournament = tournament::Tournament::new(tournament_address, self.client.clone());
let events = tournament
.commitment_joined_filter()
Expand All @@ -116,7 +114,7 @@ impl StateReader {
&self,
tournament: Address,
commitment_hash: Digest,
) -> Result<CommitmentState, Box<dyn Error>> {
) -> Result<CommitmentState> {
let tournament = tournament::Tournament::new(tournament, self.client.clone());
let (clock_state, hash) = tournament
.get_commitment(commitment_hash.into())
Expand All @@ -140,10 +138,7 @@ impl StateReader {
})
}

pub async fn fetch_from_root(
&self,
root_tournament: Address,
) -> Result<TournamentStateMap, Box<dyn Error>> {
pub async fn fetch_from_root(&self, root_tournament: Address) -> Result<TournamentStateMap> {
self.fetch_tournament(TournamentState::new_root(root_tournament), HashMap::new())
.await
}
Expand All @@ -153,7 +148,7 @@ impl StateReader {
&self,
tournament_state: TournamentState,
states: TournamentStateMap,
) -> Result<TournamentStateMap, Box<dyn Error>> {
) -> Result<TournamentStateMap> {
let tournament = tournament::Tournament::new(tournament_state.address, self.client.clone());
let mut state = tournament_state.clone();

Expand Down Expand Up @@ -247,7 +242,7 @@ impl StateReader {
match_state: MatchState,
states: TournamentStateMap,
tournament_level: u64,
) -> Result<(MatchState, TournamentStateMap), Box<dyn Error>> {
) -> Result<(MatchState, TournamentStateMap)> {
let mut state = match_state.clone();
let created_tournament = self
.created_tournament(match_state.tournament_address, match_state.id)
Expand All @@ -271,7 +266,7 @@ impl StateReader {
async fn root_tournament_winner(
&self,
root_tournament: Address,
) -> Result<Option<TournamentWinner>, Box<dyn Error>> {
) -> Result<Option<TournamentWinner>> {
let root_tournament =
root_tournament::RootTournament::new(root_tournament, self.client.clone());
let (finished, commitment, state) = root_tournament.arbitration_result().call().await?;
Expand All @@ -285,10 +280,7 @@ impl StateReader {
}
}

async fn tournament_winner(
&self,
tournament: Address,
) -> Result<Option<TournamentWinner>, Box<dyn Error>> {
async fn tournament_winner(&self, tournament: Address) -> Result<Option<TournamentWinner>> {
let tournament =
non_root_tournament::NonRootTournament::new(tournament, self.client.clone());
let (finished, parent_commitment, dangling_commitment) =
Expand Down
41 changes: 17 additions & 24 deletions offchain/core/src/arena/sender.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! This module defines the struct [EthArenaSender] that is responsible for the sending transactions
//! to tournaments
use std::{error::Error, str::FromStr, sync::Arc, time::Duration};
use std::{ str::FromStr, sync::Arc, time::Duration};

use anyhow::Result;
use async_trait::async_trait;

use ethers::{
Expand All @@ -25,7 +26,7 @@ pub struct EthArenaSender {
}

impl EthArenaSender {
pub fn new(config: ArenaConfig) -> Result<Self, Box<dyn Error>> {
pub fn new(config: ArenaConfig) -> Result<Self> {
let provider = Provider::<Http>::try_from(config.web3_rpc_url.clone())?
.interval(Duration::from_millis(10u64));
let wallet = LocalWallet::from_str(config.web3_private_key.as_str())?;
Expand All @@ -48,7 +49,7 @@ pub trait ArenaSender: Send + Sync {
proof: MerkleProof,
left_child: Digest,
right_child: Digest,
) -> Result<(), Box<dyn Error>>;
) -> Result<()>;

async fn advance_match(
&self,
Expand All @@ -58,7 +59,7 @@ pub trait ArenaSender: Send + Sync {
right_node: Digest,
new_left_node: Digest,
new_right_node: Digest,
) -> Result<(), Box<dyn Error>>;
) -> Result<()>;

async fn seal_inner_match(
&self,
Expand All @@ -68,15 +69,15 @@ pub trait ArenaSender: Send + Sync {
right_leaf: Digest,
initial_hash: Digest,
initial_hash_proof: MerkleProof,
) -> Result<(), Box<dyn Error>>;
) -> Result<()>;

async fn win_inner_match(
&self,
tournament: Address,
child_tournament: Address,
left_node: Digest,
right_node: Digest,
) -> Result<(), Box<dyn Error>>;
) -> Result<()>;

async fn seal_leaf_match(
&self,
Expand All @@ -86,7 +87,7 @@ pub trait ArenaSender: Send + Sync {
right_leaf: Digest,
initial_hash: Digest,
initial_hash_proof: MerkleProof,
) -> Result<(), Box<dyn Error>>;
) -> Result<()>;

async fn win_leaf_match(
&self,
Expand All @@ -95,13 +96,9 @@ pub trait ArenaSender: Send + Sync {
left_node: Digest,
right_node: Digest,
proofs: MachineProof,
) -> Result<(), Box<dyn Error>>;
) -> Result<()>;

async fn eliminate_match(
&self,
tournament: Address,
match_id: MatchID,
) -> Result<(), Box<dyn Error>>;
async fn eliminate_match(&self, tournament: Address, match_id: MatchID) -> Result<()>;
}

#[async_trait]
Expand All @@ -113,7 +110,7 @@ impl ArenaSender for EthArenaSender {
proof: MerkleProof,
left_child: Digest,
right_child: Digest,
) -> Result<(), Box<dyn Error>> {
) -> Result<()> {
let tournament = tournament::Tournament::new(tournament, self.client.clone());
let proof = proof.iter().map(|h| -> [u8; 32] { (*h).into() }).collect();
tournament
Expand All @@ -137,7 +134,7 @@ impl ArenaSender for EthArenaSender {
right_node: Digest,
new_left_node: Digest,
new_right_node: Digest,
) -> Result<(), Box<dyn Error>> {
) -> Result<()> {
let tournament = tournament::Tournament::new(tournament, self.client.clone());
let match_id = tournament::Id {
commitment_one: match_id.commitment_one.into(),
Expand Down Expand Up @@ -165,7 +162,7 @@ impl ArenaSender for EthArenaSender {
right_leaf: Digest,
initial_hash: Digest,
initial_hash_proof: MerkleProof,
) -> Result<(), Box<dyn Error>> {
) -> Result<()> {
let tournament =
non_leaf_tournament::NonLeafTournament::new(tournament, self.client.clone());
let match_id = non_leaf_tournament::Id {
Expand Down Expand Up @@ -196,7 +193,7 @@ impl ArenaSender for EthArenaSender {
child_tournament: Address,
left_node: Digest,
right_node: Digest,
) -> Result<(), Box<dyn Error>> {
) -> Result<()> {
let tournament =
non_leaf_tournament::NonLeafTournament::new(tournament, self.client.clone());
tournament
Expand All @@ -215,7 +212,7 @@ impl ArenaSender for EthArenaSender {
right_leaf: Digest,
initial_hash: Digest,
initial_hash_proof: MerkleProof,
) -> Result<(), Box<dyn Error>> {
) -> Result<()> {
let tournament = leaf_tournament::LeafTournament::new(tournament, self.client.clone());
let match_id = leaf_tournament::Id {
commitment_one: match_id.commitment_one.into(),
Expand Down Expand Up @@ -246,7 +243,7 @@ impl ArenaSender for EthArenaSender {
left_node: Digest,
right_node: Digest,
proofs: MachineProof,
) -> Result<(), Box<dyn Error>> {
) -> Result<()> {
let tournament = leaf_tournament::LeafTournament::new(tournament, self.client.clone());
let match_id = leaf_tournament::Id {
commitment_one: match_id.commitment_one.into(),
Expand All @@ -265,11 +262,7 @@ impl ArenaSender for EthArenaSender {
Ok(())
}

async fn eliminate_match(
&self,
tournament: Address,
match_id: MatchID,
) -> Result<(), Box<dyn Error>> {
async fn eliminate_match(&self, tournament: Address, match_id: MatchID) -> Result<()> {
let tournament = tournament::Tournament::new(tournament, self.client.clone());
let match_id = tournament::Id {
commitment_one: match_id.commitment_one.into(),
Expand Down
13 changes: 7 additions & 6 deletions offchain/core/src/machine/commitment.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! This module defines a struct [MachineCommitment] that is used to represent a `computation hash`
//! described on the paper https://arxiv.org/pdf/2212.12439.pdf.
use std::{error::Error, ops::ControlFlow, sync::Arc};
use anyhow::Result;
use std::{ops::ControlFlow, sync::Arc};

use crate::{
machine::{constants, MachineInstance},
Expand All @@ -23,7 +24,7 @@ pub fn build_machine_commitment(
base_cycle: u64,
log2_stride: u64,
log2_stride_count: u64,
) -> Result<MachineCommitment, Box<dyn Error>> {
) -> Result<MachineCommitment> {
if log2_stride >= constants::LOG2_UARCH_SPAN {
assert!(
log2_stride + log2_stride_count
Expand All @@ -42,7 +43,7 @@ pub fn build_big_machine_commitment(
base_cycle: u64,
log2_stride: u64,
log2_stride_count: u64,
) -> Result<MachineCommitment, Box<dyn Error>> {
) -> Result<MachineCommitment> {
machine.run(base_cycle)?;
let initial_state = machine.machine_state()?;

Expand Down Expand Up @@ -78,7 +79,7 @@ fn advance_instruction(
base_cycle: u64,
builder: &mut MerkleBuilder,
instruction_count: u64,
) -> Result<ControlFlow<()>, Box<dyn Error>> {
) -> Result<ControlFlow<()>> {
let cycle = (instruction + 1) << (log2_stride - constants::LOG2_UARCH_SPAN);
machine.run(base_cycle + cycle)?;
let state = machine.machine_state()?;
Expand All @@ -99,7 +100,7 @@ pub fn build_small_machine_commitment(
machine: &mut MachineInstance,
base_cycle: u64,
log2_stride_count: u64,
) -> Result<MachineCommitment, Box<dyn Error>> {
) -> Result<MachineCommitment> {
machine.run(base_cycle)?;
let initial_state = machine.machine_state()?;

Expand Down Expand Up @@ -131,7 +132,7 @@ pub fn build_small_machine_commitment(
})
}

fn run_uarch_span(machine: &mut MachineInstance) -> Result<MerkleTree, Box<dyn Error>> {
fn run_uarch_span(machine: &mut MachineInstance) -> Result<MerkleTree> {
let (_, ucycle) = machine.position();
assert!(ucycle == 0);

Expand Down
8 changes: 3 additions & 5 deletions offchain/core/src/machine/commitment_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
//! [MachineCommitment]. It is used by the [Arena] to build the commitments of the tournaments.
use crate::machine::{build_machine_commitment, MachineCommitment, MachineInstance};
use std::{
collections::{hash_map::Entry, HashMap},
error::Error,
};
use anyhow::Result;
use std::collections::{hash_map::Entry, HashMap};

pub struct CachingMachineCommitmentBuilder {
machine_path: String,
Expand All @@ -26,7 +24,7 @@ impl CachingMachineCommitmentBuilder {
level: u64,
log2_stride: u64,
log2_stride_count: u64,
) -> Result<MachineCommitment, Box<dyn Error>> {
) -> Result<MachineCommitment> {
if let Entry::Vacant(e) = self.commitments.entry(level) {
e.insert(HashMap::new());
} else if self.commitments[&level].contains_key(&base_cycle) {
Expand Down
Loading

0 comments on commit 754c0c4

Please sign in to comment.