Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode
**/target/
**/snapshots/
Cargo.lock
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
target/
snapshots/
common-rs/Cargo.lock
prt/prt-rs/Cargo.lock
prt/client-rs/Cargo.lock
prt/lua_poc/outputs/
prt/lua_poc/pixels/
node_modules
Expand Down
3 changes: 1 addition & 2 deletions cartesi-rollups/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ cartesi-machine = { path = "../../machine/rust-bindings/cartesi-machine" }
cartesi-dave-arithmetic = { path = "../../common-rs/arithmetic" }
cartesi-dave-contracts = { path = "../contract-bindings" }
cartesi-dave-merkle = { path = "../../common-rs/merkle" }
cartesi-prt-core = { path = "../../prt/prt-rs/core" }
cartesi-prt-compute = { path = "../../prt/prt-rs/compute" }
cartesi-prt-core = { path = "../../prt/client-rs" }

alloy = { version = "0.3.1", features = ["sol-types", "contract", "network", "reqwest", "signers", "signer-local"] }
anyhow = "1.0"
Expand Down
2 changes: 2 additions & 0 deletions cartesi-rollups/node/compute-runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ repository.workspace = true
[dependencies]
cartesi-prt-core = { workspace = true }
rollups-state-manager = { workspace = true }

alloy = { workspace = true }
89 changes: 43 additions & 46 deletions cartesi-rollups/node/compute-runner/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use std::process::Command;
use alloy::sol_types::private::Address;
use std::result::Result;
use std::{sync::Arc, time::Duration};
use std::{str::FromStr, sync::Arc, time::Duration};

use cartesi_prt_core::arena::BlockchainConfig;
use rollups_state_manager::{Epoch, StateManager};
use cartesi_prt_core::{
arena::{BlockchainConfig, EthArenaSender},
db::dispute_state_access::{Input, Leaf},
strategy::player::Player,
};
use rollups_state_manager::StateManager;

pub struct ComputeRunner<SM: StateManager> {
config: BlockchainConfig,
last_processed_epoch: Epoch,
sender: EthArenaSender,
sleep_duration: Duration,
state_manager: Arc<SM>,
}
Expand All @@ -16,57 +20,50 @@ impl<SM: StateManager> ComputeRunner<SM>
where
<SM as StateManager>::Error: Send + Sync + 'static,
{
pub fn new(
config: &BlockchainConfig,
state_manager: Arc<SM>,
sleep_duration: u64,
) -> Result<Self, <SM as StateManager>::Error> {
let last_sealed_epoch = state_manager.last_epoch()?;
let last_processed_epoch = {
match last_sealed_epoch {
Some(e) => {
spawn_dave_process(config, &e);
e
}
None => Epoch {
epoch_number: 0,
epoch_boundary: 0,
root_tournament: String::new(),
},
}
};

Ok(Self {
pub fn new(config: &BlockchainConfig, state_manager: Arc<SM>, sleep_duration: u64) -> Self {
let sender = EthArenaSender::new(&config).expect("fail to initialize sender");
Self {
config: config.clone(),
last_processed_epoch,
sender,
sleep_duration: Duration::from_secs(sleep_duration),
state_manager,
})
}
}

pub fn start(&mut self) -> Result<(), <SM as StateManager>::Error> {
pub async fn start(&mut self) -> Result<(), <SM as StateManager>::Error> {
loop {
let last_sealed_epoch = self.state_manager.last_epoch()?;
if let Some(e) = last_sealed_epoch {
if e.epoch_number != self.last_processed_epoch.epoch_number
|| e.epoch_boundary != self.last_processed_epoch.epoch_boundary
|| e.root_tournament != self.last_processed_epoch.root_tournament
if let Some(last_sealed_epoch) = self.state_manager.last_epoch()? {
if let Some(snapshot) = self
.state_manager
.snapshot(last_sealed_epoch.epoch_number, 0)?
{
spawn_dave_process(&self.config, &e);
self.last_processed_epoch = e;
let inputs = self.state_manager.inputs(last_sealed_epoch.epoch_number)?;
let leafs = self
.state_manager
.machine_state_hashes(last_sealed_epoch.epoch_number)?;
let mut player = Player::new(
inputs.into_iter().map(|i| Input(i)).collect(),
leafs
.into_iter()
.map(|l| {
Leaf(
l.0.as_slice()
.try_into()
.expect("fail to convert leaf from machine state hash"),
l.1,
)
})
.collect(),
&self.config,
snapshot,
Address::from_str(&last_sealed_epoch.root_tournament)
.expect("fail to convert tournament address"),
)
.expect("fail to initialize compute player");
let _ = player.react_once(&self.sender).await;
}
}

std::thread::sleep(self.sleep_duration);
}
}
}

fn spawn_dave_process(config: &BlockchainConfig, epoch: &Epoch) {
// Create a command to run `dave-compute`
let cmd = Command::new("cartesi-prt-compute")
.env("key", "val") // TODO: set up config properly
.current_dir("path to the binary")
.spawn()
.expect("fail to spawn dave compute process");
}
1 change: 0 additions & 1 deletion cartesi-rollups/node/dave-rollups/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ rollups-state-manager = { workspace = true }

cartesi-rollups-contracts = { workspace = true }
cartesi-prt-core = { workspace = true }
cartesi-prt-compute = { workspace = true }

alloy = { workspace = true }
anyhow = { workspace = true }
Expand Down
7 changes: 3 additions & 4 deletions cartesi-rollups/node/dave-rollups/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,16 @@ pub fn create_compute_runner_task(
) -> JoinHandle<()> {
let params = parameters.clone();

spawn_blocking(move || {
spawn(async move {
let mut compute_runner = ComputeRunner::new(
&params.blockchain_config,
state_manager,
params.sleep_duration,
)
.inspect_err(|e| error!("{e}"))
.unwrap();
);

compute_runner
.start()
.await
.inspect_err(|e| error!("{e}"))
.unwrap();
})
Expand Down
12 changes: 12 additions & 0 deletions cartesi-rollups/node/machine-runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ mod tests {
}))
}

fn inputs(&self, _epoch_number: u64) -> Result<Vec<Vec<u8>>> {
panic!("inputs not implemented in mock version");
}

fn last_input(&self) -> Result<Option<InputId>> {
panic!("last_input not implemented in mock version");
}
Expand Down Expand Up @@ -375,6 +379,14 @@ mod tests {
fn latest_snapshot(&self) -> Result<Option<(String, u64, u64)>> {
Ok(None)
}

fn snapshot(
&self,
_epoch_number: u64,
_input_index_in_epoch: u64,
) -> Result<Option<String>> {
Ok(None)
}
}

fn hex_to_bytes(s: &str) -> Option<Vec<u8>> {
Expand Down
6 changes: 6 additions & 0 deletions cartesi-rollups/node/state-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ pub trait StateManager {
fn epoch_count(&self) -> Result<u64, Self::Error>;
fn last_epoch(&self) -> Result<Option<Epoch>, Self::Error>;
fn input(&self, id: &InputId) -> Result<Option<Input>, Self::Error>;
fn inputs(&self, epoch_number: u64) -> Result<Vec<Vec<u8>>, Self::Error>;
fn input_count(&self, epoch_number: u64) -> Result<u64, Self::Error>;
fn last_input(&self) -> Result<Option<InputId>, Self::Error>;
fn insert_consensus_data<'a>(
Expand Down Expand Up @@ -125,4 +126,9 @@ pub trait StateManager {
input_index_in_epoch: u64,
) -> Result<(), Self::Error>;
fn latest_snapshot(&self) -> Result<Option<(String, u64, u64)>, Self::Error>;
fn snapshot(
&self,
epoch_number: u64,
input_index_in_epoch: u64,
) -> Result<Option<String>, Self::Error>;
}
20 changes: 20 additions & 0 deletions cartesi-rollups/node/state-manager/src/persistent_state_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ impl StateManager for PersistentStateAccess {
consensus_data::input(&conn, id)
}

fn inputs(&self, epoch_number: u64) -> Result<Vec<Vec<u8>>> {
let conn = self.connection.lock().unwrap();
consensus_data::inputs(&conn, epoch_number)
}

fn input_count(&self, epoch_number: u64) -> Result<u64> {
let conn = self.connection.lock().unwrap();
consensus_data::input_count(&conn, epoch_number)
Expand Down Expand Up @@ -304,6 +309,21 @@ impl StateManager for PersistentStateAccess {
None => Ok(None),
}
}

fn snapshot(&self, epoch_number: u64, input_index_in_epoch: u64) -> Result<Option<String>> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is snapshot?

Copy link
Collaborator Author

@stephenctw stephenctw Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is to get the path of machine snapshot of given (epoch_number, input_index_in_epoch). The node would pass snapshot(epoch_number, 0)to be the initial machine path for the dispute client.

let conn = self.connection.lock().unwrap();
let mut sttm = conn.prepare(
"\
SELECT path FROM snapshots
WHERE epoch_number = ?1
AND input_index_in_epoch = ?2
",
)?;

Ok(sttm
.query_row([epoch_number, input_index_in_epoch], |row| Ok(row.get(0)?))
.optional()?)
}
}

#[cfg(test)]
Expand Down
21 changes: 20 additions & 1 deletion cartesi-rollups/node/state-manager/src/sql/consensus_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,25 @@ pub fn input(conn: &rusqlite::Connection, id: &InputId) -> Result<Option<Input>>
Ok(i)
}

pub fn inputs(conn: &rusqlite::Connection, epoch_number: u64) -> Result<Vec<Vec<u8>>> {
let mut stmt = conn.prepare(
"\
SELECT input FROM inputs
WHERE epoch_number = ?1
ORDER BY input_index_in_epoch ASC
",
)?;

let query = stmt.query_map([epoch_number], |r| Ok(r.get(0)?))?;

let mut res = vec![];
for row in query {
res.push(row?);
}

Ok(res)
}

pub fn input_count<'a>(conn: &'a rusqlite::Connection, epoch_number: u64) -> Result<u64> {
Ok(conn.query_row(
"\
Expand Down Expand Up @@ -620,7 +639,7 @@ mod epochs_tests {
Ok(Some(Epoch {
epoch_number: 130,
epoch_boundary: 99,
root_tournament: tournament_address,
..
}))
));
}
Expand Down
4 changes: 4 additions & 0 deletions common-rs/merkle/src/digest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ impl Digest {
Self::from_digest(&data)
}

pub fn data(&self) -> [u8; 32] {
self.data
}

pub fn slice(&self) -> &[u8] {
self.data.as_slice()
}
Expand Down
38 changes: 22 additions & 16 deletions prt/client-lua/computation/commitment.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ local function run_uarch_span(machine)
return builder:build()
end

local function build_small_machine_commitment(base_cycle, log2_stride_count, machine)
local function build_small_machine_commitment(base_cycle, log2_stride_count, machine, snapshot_dir)
local machine_state = machine:run(base_cycle)
if save_snapshot then
-- taking snapshot for leafs to save time in next level
machine:snapshot(snapshot_dir, base_cycle)
end
local initial_state = machine_state.root_hash

local builder = MerkleBuilder:new()
Expand All @@ -55,24 +59,21 @@ local function build_small_machine_commitment(base_cycle, log2_stride_count, mac
return initial_state, builder:build(initial_state)
end

local function build_big_machine_commitment(base_cycle, log2_stride, log2_stride_count, machine)
local function build_big_machine_commitment(base_cycle, log2_stride, log2_stride_count, machine, snapshot_dir)
local machine_state = machine:run(base_cycle)
if save_snapshot then
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If save_snapshot is a global variable, I think I prefer that snapshot_dir also be a global variable, unless it's changing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could change occasionally because it's /dispute_data/$root_tournament_address.

-- taking snapshot for leafs to save time in next level
machine:snapshot(snapshot_dir, base_cycle)
end
local initial_state = machine_state.root_hash

local builder = MerkleBuilder:new()
local instruction_count = arithmetic.max_uint(log2_stride_count)
local instruction = 0

local snapshot_frequency = 1024
while ulte(instruction, instruction_count) do
local cycle = ((instruction + 1) << (log2_stride - consts.log2_uarch_span))
machine_state = machine:run(base_cycle + cycle)
if save_snapshot then
-- taking snapshot for leafs to save time in next level
if ((instruction + 1) % snapshot_frequency) == 0 then
machine:snapshot(base_cycle + cycle)
end
end

if not machine_state.halted then
builder:add(machine_state.root_hash)
Expand All @@ -87,29 +88,33 @@ local function build_big_machine_commitment(base_cycle, log2_stride, log2_stride
return initial_state, builder:build(initial_state)
end

local function build_commitment(base_cycle, log2_stride, log2_stride_count, machine_path)
local function build_commitment(base_cycle, log2_stride, log2_stride_count, machine_path, snapshot_dir)
local machine = Machine:new_from_path(machine_path)
machine:load_snapshot(base_cycle)
machine:load_snapshot(snapshot_dir, base_cycle)

if log2_stride >= consts.log2_uarch_span then
assert(
log2_stride + log2_stride_count <=
consts.log2_emulator_span + consts.log2_uarch_span
)
return build_big_machine_commitment(base_cycle, log2_stride, log2_stride_count, machine)
return build_big_machine_commitment(base_cycle, log2_stride, log2_stride_count, machine, snapshot_dir)
else
assert(log2_stride == 0)
return build_small_machine_commitment(base_cycle, log2_stride_count, machine)
return build_small_machine_commitment(base_cycle, log2_stride_count, machine, snapshot_dir)
end
end

local CommitmentBuilder = {}
CommitmentBuilder.__index = CommitmentBuilder

function CommitmentBuilder:new(machine_path)
function CommitmentBuilder:new(machine_path, snapshot_dir, root_commitment)
-- receive honest root commitment from main process
local commitments = { [0] = { [0] = root_commitment } }

local c = {
commitments = commitments,
machine_path = machine_path,
commitments = {}
snapshot_dir = snapshot_dir
}
setmetatable(c, self)
return c
Expand All @@ -122,7 +127,8 @@ function CommitmentBuilder:build(base_cycle, level, log2_stride, log2_stride_cou
return self.commitments[level][base_cycle]
end

local _, commitment = build_commitment(base_cycle, log2_stride, log2_stride_count, self.machine_path)
local _, commitment = build_commitment(base_cycle, log2_stride, log2_stride_count, self.machine_path,
self.snapshot_dir)
self.commitments[level][base_cycle] = commitment
return commitment
end
Expand Down
Loading
Loading