Skip to content

Commit

Permalink
chore: rename dispute to compute
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenctw committed Nov 2, 2024
1 parent 0669f10 commit f0f856b
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 41 deletions.
3 changes: 2 additions & 1 deletion cartesi-rollups/node/compute-runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{str::FromStr, sync::Arc, time::Duration};

use cartesi_prt_core::{
arena::{BlockchainConfig, EthArenaSender},
db::dispute_state_access::{Input, Leaf},
db::compute_state_access::{Input, Leaf},
strategy::player::Player,
};
use rollups_state_manager::StateManager;
Expand Down Expand Up @@ -37,6 +37,7 @@ where
.state_manager
.snapshot(last_sealed_epoch.epoch_number, 0)?
{
// TODO: make sure all snapshots are available to compute
let inputs = self.state_manager.inputs(last_sealed_epoch.epoch_number)?;
let leafs = self
.state_manager
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

use crate::db::sql::{dispute_data, error::*, migrations};
use crate::db::sql::{compute_data, error::*, migrations};
use cartesi_dave_merkle::{Digest, MerkleBuilder, MerkleTree};

use alloy::hex as alloy_hex;
Expand All @@ -28,7 +28,7 @@ pub struct Input(#[serde(with = "alloy_hex::serde")] pub Vec<u8>);
pub struct Leaf(#[serde(with = "alloy_hex::serde")] pub [u8; 32], pub u64);

#[derive(Debug)]
pub struct DisputeStateAccess {
pub struct ComputeStateAccess {
connection: Mutex<Connection>,
pub work_path: PathBuf,
}
Expand All @@ -44,18 +44,18 @@ fn read_json_file(file_path: &Path) -> Result<InputsAndLeafs> {
Ok(data)
}

impl DisputeStateAccess {
impl ComputeStateAccess {
pub fn new(
inputs: Vec<Input>,
leafs: Vec<Leaf>,
root_tournament: String,
dispute_data_path: &str,
compute_data_path: &str,
) -> Result<Self> {
// initialize the database if it doesn't exist
// fill the database from a json-format file, or the parameters
// the database should be "/dispute_data/0x_root_tournament_address/db"
// the json file should be "/dispute_data/0x_root_tournament_address/inputs_and_leafs.json"
let work_dir = format!("{dispute_data_path}/{root_tournament}");
// the database should be "/compute_data/0x_root_tournament_address/db"
// the json file should be "/compute_data/0x_root_tournament_address/inputs_and_leafs.json"
let work_dir = format!("{compute_data_path}/{root_tournament}");
let work_path = PathBuf::from(work_dir);
let db_path = work_path.join("db");
let no_create_flags = OpenFlags::default() & !OpenFlags::SQLITE_OPEN_CREATE;
Expand All @@ -77,15 +77,15 @@ impl DisputeStateAccess {
// prioritize json file over parameters
match read_json_file(&json_path) {
Ok(inputs_and_leafs) => {
dispute_data::insert_dispute_data(
compute_data::insert_compute_data(
&connection,
inputs_and_leafs.inputs.iter(),
inputs_and_leafs.leafs.iter(),
)?;
}
Err(_) => {
info!("load inputs and leafs from parameters");
dispute_data::insert_dispute_data(
compute_data::insert_compute_data(
&connection,
inputs.iter(),
leafs.iter(),
Expand All @@ -103,7 +103,7 @@ impl DisputeStateAccess {

pub fn input(&self, id: u64) -> Result<Option<Vec<u8>>> {
let conn = self.connection.lock().unwrap();
dispute_data::input(&conn, id)
compute_data::input(&conn, id)
}

pub fn insert_compute_leafs<'a>(
Expand All @@ -113,7 +113,7 @@ impl DisputeStateAccess {
leafs: impl Iterator<Item = &'a Leaf>,
) -> Result<()> {
let conn = self.connection.lock().unwrap();
dispute_data::insert_compute_leafs(&conn, level, base_cycle, leafs)
compute_data::insert_compute_leafs(&conn, level, base_cycle, leafs)
}

pub fn compute_leafs(
Expand All @@ -122,11 +122,11 @@ impl DisputeStateAccess {
base_cycle: u64,
) -> Result<Vec<(Arc<MerkleTree>, u64)>> {
let conn = self.connection.lock().unwrap();
let leafs = dispute_data::compute_leafs(&conn, level, base_cycle)?;
let leafs = compute_data::compute_leafs(&conn, level, base_cycle)?;

let mut tree = Vec::new();
for leaf in leafs {
let tree_leafs = dispute_data::compute_tree(&conn, &leaf.0)?;
let tree_leafs = compute_data::compute_tree(&conn, &leaf.0)?;
if tree_leafs.len() > 0 {
// if leaf is also tree, rebuild it from nested leafs
let mut builder = MerkleBuilder::default();
Expand All @@ -148,7 +148,7 @@ impl DisputeStateAccess {
tree_leafs: impl Iterator<Item = &'a Leaf>,
) -> Result<()> {
let conn = self.connection.lock().unwrap();
dispute_data::insert_compute_tree(&conn, tree_root, tree_leafs)
compute_data::insert_compute_tree(&conn, tree_root, tree_leafs)
}

pub fn closest_snapshot(&self, base_cycle: u64) -> Result<Option<PathBuf>> {
Expand Down Expand Up @@ -180,7 +180,7 @@ impl DisputeStateAccess {
}

#[cfg(test)]
mod dispute_state_access_tests {
mod compute_state_access_tests {
use super::*;

fn create_directory(path: &Path) -> std::io::Result<()> {
Expand All @@ -205,7 +205,7 @@ mod dispute_state_access_tests {
create_directory(&work_dir).unwrap();
{
let access =
DisputeStateAccess::new(Vec::new(), Vec::new(), String::from("0x12345678"), "/tmp")
ComputeStateAccess::new(Vec::new(), Vec::new(), String::from("0x12345678"), "/tmp")
.unwrap();

assert_eq!(access.closest_snapshot(0).unwrap(), None);
Expand Down Expand Up @@ -264,7 +264,7 @@ mod dispute_state_access_tests {
remove_directory(&work_dir).unwrap();
create_directory(&work_dir).unwrap();
let access =
DisputeStateAccess::new(Vec::new(), Vec::new(), String::from("0x12345678"), "/tmp")
ComputeStateAccess::new(Vec::new(), Vec::new(), String::from("0x12345678"), "/tmp")
.unwrap();

let root = [
Expand Down
2 changes: 1 addition & 1 deletion prt/client-rs/src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

pub mod dispute_state_access;
pub mod compute_state_access;

pub(crate) mod sql;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

use super::error::*;
use crate::db::dispute_state_access::{Input, Leaf};
use crate::db::compute_state_access::{Input, Leaf};

use rusqlite::{params, OptionalExtension};

Expand Down Expand Up @@ -161,7 +161,7 @@ pub fn compute_tree_count(conn: &rusqlite::Connection, tree_root: &[u8]) -> Resu
)?)
}

pub fn insert_dispute_data<'a>(
pub fn insert_compute_data<'a>(
conn: &rusqlite::Connection,
inputs: impl Iterator<Item = &'a Input>,
leafs: impl Iterator<Item = &'a Leaf>,
Expand Down
4 changes: 2 additions & 2 deletions prt/client-rs/src/db/sql/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use thiserror::Error;

#[derive(Error, Debug)]
pub enum DisputeStateAccessError {
pub enum ComputeStateAccessError {
#[error(transparent)]
Digest {
#[from]
Expand Down Expand Up @@ -39,4 +39,4 @@ pub enum DisputeStateAccessError {
DataNotFound { description: String },
}

pub type Result<T> = std::result::Result<T, DisputeStateAccessError>;
pub type Result<T> = std::result::Result<T, ComputeStateAccessError>;
2 changes: 1 addition & 1 deletion prt/client-rs/src/db/sql/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod dispute_data;
pub mod compute_data;
pub mod error;
pub mod migrations;
12 changes: 6 additions & 6 deletions prt/client-rs/src/machine/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use anyhow::Result;
use std::{ops::ControlFlow, sync::Arc};

use crate::{
db::dispute_state_access::{DisputeStateAccess, Leaf},
db::compute_state_access::{ComputeStateAccess, Leaf},
machine::{constants, MachineInstance},
};
use cartesi_dave_arithmetic as arithmetic;
Expand Down Expand Up @@ -49,7 +49,7 @@ pub fn build_machine_commitment(
level: u64,
log2_stride: u64,
log2_stride_count: u64,
db: &DisputeStateAccess,
db: &ComputeStateAccess,
) -> Result<MachineCommitment> {
if log2_stride >= constants::LOG2_UARCH_SPAN {
assert!(
Expand Down Expand Up @@ -79,7 +79,7 @@ pub fn build_big_machine_commitment(
level: u64,
log2_stride: u64,
log2_stride_count: u64,
db: &DisputeStateAccess,
db: &ComputeStateAccess,
) -> Result<MachineCommitment> {
machine.run(base_cycle)?;
snapshot_base_cycle(machine, base_cycle, db)?;
Expand Down Expand Up @@ -143,7 +143,7 @@ pub fn build_small_machine_commitment(
base_cycle: u64,
level: u64,
log2_stride_count: u64,
db: &DisputeStateAccess,
db: &ComputeStateAccess,
) -> Result<MachineCommitment> {
machine.run(base_cycle)?;
snapshot_base_cycle(machine, base_cycle, db)?;
Expand Down Expand Up @@ -184,7 +184,7 @@ pub fn build_small_machine_commitment(
fn snapshot_base_cycle(
machine: &mut MachineInstance,
base_cycle: u64,
db: &DisputeStateAccess,
db: &ComputeStateAccess,
) -> Result<()> {
let snapshot_path = db.work_path.join(format!("{}", base_cycle));
machine.snapshot(&snapshot_path)?;
Expand All @@ -193,7 +193,7 @@ fn snapshot_base_cycle(

fn run_uarch_span(
machine: &mut MachineInstance,
db: &DisputeStateAccess,
db: &ComputeStateAccess,
) -> Result<Arc<MerkleTree>> {
let (_, ucycle) = machine.position();
assert!(ucycle == 0);
Expand Down
4 changes: 2 additions & 2 deletions prt/client-rs/src/machine/commitment_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! [MachineCommitment]. It is used by the [Arena] to build the commitments of the tournaments.
use crate::{
db::dispute_state_access::DisputeStateAccess,
db::compute_state_access::ComputeStateAccess,
machine::{
build_machine_commitment, build_machine_commitment_from_leafs, MachineCommitment,
MachineInstance,
Expand Down Expand Up @@ -34,7 +34,7 @@ impl CachingMachineCommitmentBuilder {
level: u64,
log2_stride: u64,
log2_stride_count: u64,
db: &DisputeStateAccess,
db: &ComputeStateAccess,
) -> Result<MachineCommitment> {
if let Entry::Vacant(e) = self.commitments.entry(level) {
e.insert(HashMap::new());
Expand Down
6 changes: 3 additions & 3 deletions prt/client-rs/src/strategy/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
ArenaSender, BlockchainConfig, CommitmentMap, CommitmentState, MatchState, StateReader,
TournamentState, TournamentStateMap, TournamentWinner,
},
db::dispute_state_access::{DisputeStateAccess, Input, Leaf},
db::compute_state_access::{ComputeStateAccess, Input, Leaf},
machine::{constants, CachingMachineCommitmentBuilder, MachineCommitment, MachineInstance},
strategy::gc::GarbageCollector,
};
Expand All @@ -25,7 +25,7 @@ pub enum PlayerTournamentResult {
}

pub struct Player {
db: DisputeStateAccess,
db: ComputeStateAccess,
machine_path: String,
commitment_builder: CachingMachineCommitmentBuilder,
root_tournament: Address,
Expand All @@ -42,7 +42,7 @@ impl Player {
root_tournament: Address,
) -> Result<Self> {
let db =
DisputeStateAccess::new(inputs, leafs, root_tournament.to_string(), "/dispute_data")?;
ComputeStateAccess::new(inputs, leafs, root_tournament.to_string(), "/compute_data")?;
let reader = StateReader::new(&blockchain_config)?;
let gc = GarbageCollector::new(root_tournament);
let commitment_builder = CachingMachineCommitmentBuilder::new(machine_path.clone());
Expand Down
2 changes: 1 addition & 1 deletion prt/tests/compute-rs/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ WORKDIR /root/prt/contracts
RUN forge --version
RUN forge build

RUN mkdir -p /dispute_data/0xa16E02E87b7454126E5E10d957A927A7F5B5d2be
RUN mkdir -p /compute_data/0xa16E02E87b7454126E5E10d957A927A7F5B5d2be

WORKDIR /root/prt/tests/compute
ENTRYPOINT ["./compute-test-entrypoint.sh"]
2 changes: 1 addition & 1 deletion prt/tests/compute/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ RUN chmod +x doom_showcase/process_doom_graphics.lua
WORKDIR "/app"
RUN mkdir -p pixels
RUN mkdir -p outputs
RUN mkdir -p /dispute_data/0xa16E02E87b7454126E5E10d957A927A7F5B5d2be
RUN mkdir -p /compute_data/0xa16E02E87b7454126E5E10d957A927A7F5B5d2be

WORKDIR "/app/tests/compute"
ENTRYPOINT ["./compute-test-entrypoint.sh"]
4 changes: 2 additions & 2 deletions prt/tests/compute/prt_compute.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ local function write_json_file(leafs, root_tournament)

local flat = require "utils.flat"
local json = require "utils.json"
local file_path = string.format("/dispute_data/%s/inputs_and_leafs.json", root_tournament)
local file_path = string.format("/compute_data/%s/inputs_and_leafs.json", root_tournament)
local file = assert(io.open(file_path, "w"))
file:write(json.encode(flat.flatten(inputs_and_leafs).flat_object))
assert(file:close())
Expand All @@ -47,7 +47,7 @@ local function setup_players(use_lua_node, extra_data, root_constants, root_tour
local player_coroutines = {}
local player_index = 1
print("Calculating root commitment...")
local snapshot_dir = string.format("/dispute_data/%s", root_tournament)
local snapshot_dir = string.format("/compute_data/%s", root_tournament)
local builder = CommitmentBuilder:new(machine_path, snapshot_dir)
local root_commitment = builder:build(0, 0, root_constants.log2_step, root_constants.height)

Expand Down
2 changes: 1 addition & 1 deletion prt/tests/compute/runners/hero_runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ local function hero_runner(player_id, machine_path, root_commitment, root_tourna
hook = false
end

local snapshot_dir = string.format("/dispute_data/%s", root_tournament)
local snapshot_dir = string.format("/compute_data/%s", root_tournament)
local strategy = HonestStrategy:new(
CommitmentBuilder:new(machine_path, snapshot_dir, root_commitment),
{},
Expand Down
2 changes: 1 addition & 1 deletion prt/tests/compute/runners/sybil_runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ end


local function sybil_runner(player_id, machine_path, root_commitment, root_tournament, fake_commitment_count)
local snapshot_dir = string.format("/dispute_data/%s", root_tournament)
local snapshot_dir = string.format("/compute_data/%s", root_tournament)
local strategy = HonestStrategy:new(
FakeCommitmentBuilder:new(machine_path, root_commitment, snapshot_dir),
{},
Expand Down

0 comments on commit f0f856b

Please sign in to comment.