From f0f856b01ff247728afc102b974eaf9c8dfbc4fd Mon Sep 17 00:00:00 2001 From: Stephen Chen <20940639+stephenctw@users.noreply.github.com> Date: Thu, 24 Oct 2024 15:16:45 +0800 Subject: [PATCH] chore: rename `dispute` to `compute` --- .../node/compute-runner/src/lib.rs | 3 +- ...tate_access.rs => compute_state_access.rs} | 34 +++++++++---------- prt/client-rs/src/db/mod.rs | 2 +- .../sql/{dispute_data.rs => compute_data.rs} | 4 +-- prt/client-rs/src/db/sql/error.rs | 4 +-- prt/client-rs/src/db/sql/mod.rs | 2 +- prt/client-rs/src/machine/commitment.rs | 12 +++---- .../src/machine/commitment_builder.rs | 4 +-- prt/client-rs/src/strategy/player.rs | 6 ++-- prt/tests/compute-rs/Dockerfile | 2 +- prt/tests/compute/Dockerfile | 2 +- prt/tests/compute/prt_compute.lua | 4 +-- prt/tests/compute/runners/hero_runner.lua | 2 +- prt/tests/compute/runners/sybil_runner.lua | 2 +- 14 files changed, 42 insertions(+), 41 deletions(-) rename prt/client-rs/src/db/{dispute_state_access.rs => compute_state_access.rs} (92%) rename prt/client-rs/src/db/sql/{dispute_data.rs => compute_data.rs} (98%) diff --git a/cartesi-rollups/node/compute-runner/src/lib.rs b/cartesi-rollups/node/compute-runner/src/lib.rs index fad17c81..5f037ffe 100644 --- a/cartesi-rollups/node/compute-runner/src/lib.rs +++ b/cartesi-rollups/node/compute-runner/src/lib.rs @@ -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; @@ -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 diff --git a/prt/client-rs/src/db/dispute_state_access.rs b/prt/client-rs/src/db/compute_state_access.rs similarity index 92% rename from prt/client-rs/src/db/dispute_state_access.rs rename to prt/client-rs/src/db/compute_state_access.rs index 27923249..3cfb81dc 100644 --- a/prt/client-rs/src/db/dispute_state_access.rs +++ b/prt/client-rs/src/db/compute_state_access.rs @@ -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; @@ -28,7 +28,7 @@ pub struct Input(#[serde(with = "alloy_hex::serde")] pub Vec); pub struct Leaf(#[serde(with = "alloy_hex::serde")] pub [u8; 32], pub u64); #[derive(Debug)] -pub struct DisputeStateAccess { +pub struct ComputeStateAccess { connection: Mutex, pub work_path: PathBuf, } @@ -44,18 +44,18 @@ fn read_json_file(file_path: &Path) -> Result { Ok(data) } -impl DisputeStateAccess { +impl ComputeStateAccess { pub fn new( inputs: Vec, leafs: Vec, root_tournament: String, - dispute_data_path: &str, + compute_data_path: &str, ) -> Result { // 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; @@ -77,7 +77,7 @@ 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(), @@ -85,7 +85,7 @@ impl DisputeStateAccess { } Err(_) => { info!("load inputs and leafs from parameters"); - dispute_data::insert_dispute_data( + compute_data::insert_compute_data( &connection, inputs.iter(), leafs.iter(), @@ -103,7 +103,7 @@ impl DisputeStateAccess { pub fn input(&self, id: u64) -> Result>> { let conn = self.connection.lock().unwrap(); - dispute_data::input(&conn, id) + compute_data::input(&conn, id) } pub fn insert_compute_leafs<'a>( @@ -113,7 +113,7 @@ impl DisputeStateAccess { leafs: impl Iterator, ) -> 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( @@ -122,11 +122,11 @@ impl DisputeStateAccess { base_cycle: u64, ) -> Result, 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(); @@ -148,7 +148,7 @@ impl DisputeStateAccess { tree_leafs: impl Iterator, ) -> 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> { @@ -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<()> { @@ -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); @@ -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 = [ diff --git a/prt/client-rs/src/db/mod.rs b/prt/client-rs/src/db/mod.rs index 050effad..93a5489e 100644 --- a/prt/client-rs/src/db/mod.rs +++ b/prt/client-rs/src/db/mod.rs @@ -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; diff --git a/prt/client-rs/src/db/sql/dispute_data.rs b/prt/client-rs/src/db/sql/compute_data.rs similarity index 98% rename from prt/client-rs/src/db/sql/dispute_data.rs rename to prt/client-rs/src/db/sql/compute_data.rs index facde827..57097438 100644 --- a/prt/client-rs/src/db/sql/dispute_data.rs +++ b/prt/client-rs/src/db/sql/compute_data.rs @@ -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}; @@ -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, leafs: impl Iterator, diff --git a/prt/client-rs/src/db/sql/error.rs b/prt/client-rs/src/db/sql/error.rs index 492234db..abf8462d 100644 --- a/prt/client-rs/src/db/sql/error.rs +++ b/prt/client-rs/src/db/sql/error.rs @@ -4,7 +4,7 @@ use thiserror::Error; #[derive(Error, Debug)] -pub enum DisputeStateAccessError { +pub enum ComputeStateAccessError { #[error(transparent)] Digest { #[from] @@ -39,4 +39,4 @@ pub enum DisputeStateAccessError { DataNotFound { description: String }, } -pub type Result = std::result::Result; +pub type Result = std::result::Result; diff --git a/prt/client-rs/src/db/sql/mod.rs b/prt/client-rs/src/db/sql/mod.rs index b072fed3..4e7d4a2c 100644 --- a/prt/client-rs/src/db/sql/mod.rs +++ b/prt/client-rs/src/db/sql/mod.rs @@ -1,3 +1,3 @@ -pub mod dispute_data; +pub mod compute_data; pub mod error; pub mod migrations; diff --git a/prt/client-rs/src/machine/commitment.rs b/prt/client-rs/src/machine/commitment.rs index 10072687..c94df7ed 100644 --- a/prt/client-rs/src/machine/commitment.rs +++ b/prt/client-rs/src/machine/commitment.rs @@ -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; @@ -49,7 +49,7 @@ pub fn build_machine_commitment( level: u64, log2_stride: u64, log2_stride_count: u64, - db: &DisputeStateAccess, + db: &ComputeStateAccess, ) -> Result { if log2_stride >= constants::LOG2_UARCH_SPAN { assert!( @@ -79,7 +79,7 @@ pub fn build_big_machine_commitment( level: u64, log2_stride: u64, log2_stride_count: u64, - db: &DisputeStateAccess, + db: &ComputeStateAccess, ) -> Result { machine.run(base_cycle)?; snapshot_base_cycle(machine, base_cycle, db)?; @@ -143,7 +143,7 @@ pub fn build_small_machine_commitment( base_cycle: u64, level: u64, log2_stride_count: u64, - db: &DisputeStateAccess, + db: &ComputeStateAccess, ) -> Result { machine.run(base_cycle)?; snapshot_base_cycle(machine, base_cycle, db)?; @@ -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)?; @@ -193,7 +193,7 @@ fn snapshot_base_cycle( fn run_uarch_span( machine: &mut MachineInstance, - db: &DisputeStateAccess, + db: &ComputeStateAccess, ) -> Result> { let (_, ucycle) = machine.position(); assert!(ucycle == 0); diff --git a/prt/client-rs/src/machine/commitment_builder.rs b/prt/client-rs/src/machine/commitment_builder.rs index 6f114f68..a9f190d1 100644 --- a/prt/client-rs/src/machine/commitment_builder.rs +++ b/prt/client-rs/src/machine/commitment_builder.rs @@ -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, @@ -34,7 +34,7 @@ impl CachingMachineCommitmentBuilder { level: u64, log2_stride: u64, log2_stride_count: u64, - db: &DisputeStateAccess, + db: &ComputeStateAccess, ) -> Result { if let Entry::Vacant(e) = self.commitments.entry(level) { e.insert(HashMap::new()); diff --git a/prt/client-rs/src/strategy/player.rs b/prt/client-rs/src/strategy/player.rs index cc99db12..3639857c 100644 --- a/prt/client-rs/src/strategy/player.rs +++ b/prt/client-rs/src/strategy/player.rs @@ -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, }; @@ -25,7 +25,7 @@ pub enum PlayerTournamentResult { } pub struct Player { - db: DisputeStateAccess, + db: ComputeStateAccess, machine_path: String, commitment_builder: CachingMachineCommitmentBuilder, root_tournament: Address, @@ -42,7 +42,7 @@ impl Player { root_tournament: Address, ) -> Result { 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()); diff --git a/prt/tests/compute-rs/Dockerfile b/prt/tests/compute-rs/Dockerfile index 08ababec..f3a0bd43 100644 --- a/prt/tests/compute-rs/Dockerfile +++ b/prt/tests/compute-rs/Dockerfile @@ -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"] diff --git a/prt/tests/compute/Dockerfile b/prt/tests/compute/Dockerfile index 4a7ea964..c612d3a6 100644 --- a/prt/tests/compute/Dockerfile +++ b/prt/tests/compute/Dockerfile @@ -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"] diff --git a/prt/tests/compute/prt_compute.lua b/prt/tests/compute/prt_compute.lua index 94bd9b61..41a9e369 100755 --- a/prt/tests/compute/prt_compute.lua +++ b/prt/tests/compute/prt_compute.lua @@ -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()) @@ -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) diff --git a/prt/tests/compute/runners/hero_runner.lua b/prt/tests/compute/runners/hero_runner.lua index 1c773207..ae38eeb7 100755 --- a/prt/tests/compute/runners/hero_runner.lua +++ b/prt/tests/compute/runners/hero_runner.lua @@ -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), {}, diff --git a/prt/tests/compute/runners/sybil_runner.lua b/prt/tests/compute/runners/sybil_runner.lua index ae5f4d5f..69fd4680 100755 --- a/prt/tests/compute/runners/sybil_runner.lua +++ b/prt/tests/compute/runners/sybil_runner.lua @@ -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), {},