Skip to content

Commit

Permalink
feat(prt): refine rollups meta step
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenctw committed Nov 10, 2024
1 parent 082e242 commit 2b92765
Show file tree
Hide file tree
Showing 26 changed files with 407 additions and 186 deletions.
2 changes: 1 addition & 1 deletion cartesi-rollups/node/compute-runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ where
.state_manager
.machine_state_hashes(last_sealed_epoch.epoch_number)?;
let mut player = Player::new(
inputs.into_iter().map(|i| Input(i)).collect(),
Some(inputs.into_iter().map(|i| Input(i)).collect()),
leafs
.into_iter()
.map(|l| {
Expand Down
17 changes: 5 additions & 12 deletions cartesi-rollups/node/machine-runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,11 @@ where

fn process_input(&mut self, data: &[u8]) -> Result<(), SM> {
// TODO: review caclulations
let big_steps_in_stride = max_uint(LOG2_STRIDE - LOG2_UARCH_SPAN);
let stride_count_in_input = max_uint(LOG2_EMULATOR_SPAN + LOG2_UARCH_SPAN - LOG2_STRIDE);
let big_steps_in_stride = 1 << (LOG2_STRIDE - LOG2_UARCH_SPAN);
let stride_count_in_input = 1 << (LOG2_EMULATOR_SPAN + LOG2_UARCH_SPAN - LOG2_STRIDE);

// take snapshot and make it available to the compute client
// the snapshot taken before input insersion is for log/proof generation
self.snapshot(0)?;
self.feed_input(data)?;
self.run_machine(1)?;
// take snapshot and make it available to the compute client
// the snapshot taken after insersion and step is for commitment builder
self.snapshot(1)?;
self.run_machine(big_steps_in_stride - 1)?;
self.run_machine(big_steps_in_stride)?;

let mut i: u64 = 0;
while !self.machine.read_iflags_y()? {
Expand Down Expand Up @@ -228,12 +221,12 @@ where
Ok(())
}

fn snapshot(&self, offset: u64) -> Result<(), SM> {
fn take_snapshot(&self) -> Result<(), SM> {
// TODO: make sure "/rollups_data/{epoch_number}" exists
let snapshot_path = PathBuf::from(format!(
"/rollups_data/{}/{}",
self.epoch_number,
self.next_input_index_in_epoch << LOG2_EMULATOR_SPAN + offset
self.next_input_index_in_epoch << LOG2_EMULATOR_SPAN
));
if !snapshot_path.exists() {
self.machine.store(&snapshot_path)?;
Expand Down
21 changes: 14 additions & 7 deletions prt/client-lua/computation/commitment.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ local function run_uarch_span(machine)
end

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

Expand All @@ -60,10 +60,10 @@ local function build_small_machine_commitment(base_cycle, log2_stride_count, mac
end

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

Expand All @@ -88,9 +88,16 @@ 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, snapshot_dir)
local function build_commitment(base_cycle, log2_stride, log2_stride_count, machine_path, snapshot_dir, inputs)
local machine = Machine:new_from_path(machine_path)
machine:load_snapshot(snapshot_dir, base_cycle)
if inputs then
-- treat it as rollups
machine:run_with_inputs(base_cycle, inputs)
else
-- treat it as compute
machine:run(base_cycle)
end

if log2_stride >= consts.log2_uarch_span then
assert(
Expand Down Expand Up @@ -120,15 +127,15 @@ function CommitmentBuilder:new(machine_path, snapshot_dir, root_commitment)
return c
end

function CommitmentBuilder:build(base_cycle, level, log2_stride, log2_stride_count)
function CommitmentBuilder:build(base_cycle, level, log2_stride, log2_stride_count, inputs)
if not self.commitments[level] then
self.commitments[level] = {}
elseif self.commitments[level][base_cycle] then
return self.commitments[level][base_cycle]
end

local _, commitment = build_commitment(base_cycle, log2_stride, log2_stride_count, self.machine_path,
self.snapshot_dir)
self.snapshot_dir, inputs)
self.commitments[level][base_cycle] = commitment
return commitment
end
Expand Down
76 changes: 57 additions & 19 deletions prt/client-lua/computation/machine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ local function find_closest_snapshot(path, current_cycle, cycle)
return closest_dir
end

function Machine:snapshot(snapshot_dir, cycle)
function Machine:take_snapshot(snapshot_dir, cycle)
if helper.exists(snapshot_dir) then
local snapshot_path = snapshot_dir .. "/" .. tostring(cycle)

Expand Down Expand Up @@ -163,6 +163,34 @@ function Machine:run_uarch(ucycle)
self.ucycle = ucycle
end

function Machine:run_with_inputs(cycle, inputs)
local input_mask = arithmetic.max_uint(consts.log2_emulator_span)
local current_input_index = self.cycle >> consts.log2_emulator_span

local next_input_index

if self.cycle & input_mask == 0 then
next_input_index = current_input_index
else
next_input_index = current_input_index + 1
end
local next_input_cycle = next_input_index << consts.log2_emulator_span

while next_input_cycle < cycle do
self:run(next_input_cycle)
local input = inputs[next_input_index]
if input then
self.machine:send_cmio_response(cartesi.machine.HTIF_YIELD_REASON_ADVANCE_STATE, input);
end

next_input_index = next_input_index + 1
next_input_cycle = next_input_index << consts.log2_emulator_span
end
self:run(cycle)

return self:state()
end

function Machine:increment_uarch()
self.machine:run_uarch(self.ucycle + 1)
self.ucycle = self.ucycle + 1
Expand Down Expand Up @@ -223,29 +251,39 @@ local function encode_access_log(logs)
return '"' .. hex_data .. '"'
end

function Machine.get_logs(path, snapshot_dir, cycle, ucycle, input)
function Machine.get_logs(path, snapshot_dir, cycle, ucycle, inputs)
local machine = Machine:new_from_path(path)
machine:load_snapshot(snapshot_dir, cycle)
local logs
local log_type = { annotations = true, proofs = true }
machine:run(cycle)

local mask = 1 << consts.log2_emulator_span - 1;
if cycle & mask == 0 and input then
-- need to process input
if ucycle == 0 then
logs = machine.machine:log_send_cmio_response(cartesi.machine.HTIF_YIELD_REASON_ADVANCE_STATE, input,
log_type
)
local step_logs = machine.machine:log_uarch_step(log_type)
-- append step logs to cmio logs
for _, log in ipairs(step_logs) do
table.insert(logs, log)
local input = Hash.zero
if inputs then
-- treat it as rollups
machine:run_with_inputs(cycle, inputs)

local mask = arithmetic.max_uint(consts.log2_emulator_span);
local try_input = inputs[cycle >> consts.log2_emulator_span]
if cycle & mask == 0 and try_input then
input = try_input
-- need to process input
if ucycle == 0 then
-- need to log cmio
logs = machine.machine:log_send_cmio_response(cartesi.machine.HTIF_YIELD_REASON_ADVANCE_STATE, input,
log_type
)
local step_logs = machine.machine:log_uarch_step(log_type)
-- append step logs to cmio logs
for _, log in ipairs(step_logs) do
table.insert(logs, log)
end
return encode_access_log(logs), input
else
machine.machine:send_cmio_response(cartesi.machine.HTIF_YIELD_REASON_ADVANCE_STATE, input)
end
return encode_access_log(logs)
else
machine.machine:send_cmio_response(cartesi.machine.HTIF_YIELD_REASON_ADVANCE_STATE, input)
end
else
-- treat it as compute
machine:run(cycle)
end

machine:run_uarch(ucycle)
Expand All @@ -254,7 +292,7 @@ function Machine.get_logs(path, snapshot_dir, cycle, ucycle, input)
else
logs = machine.machine:log_uarch_step(log_type)
end
return encode_access_log(logs)
return encode_access_log(logs), input
end

return Machine
7 changes: 4 additions & 3 deletions prt/client-lua/player/sender.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ function Sender:_send_tx(tournament_address, sig, args)
local ret = handle:read "*a"
if ret:find "Error" then
handle:close()
error(string.format("Send transaction `%s` reverted:\n%s", cmd, ret))
error(string.format("Send transaction `%s` reverted:\n%s", sig, ret))
end

Expand Down Expand Up @@ -168,16 +169,16 @@ function Sender:tx_seal_leaf_match(
end

function Sender:tx_win_leaf_match(
tournament_address, commitment_one, commitment_two, left, right, proof
tournament_address, commitment_one, commitment_two, left, right, proof, input
)
local sig =
[[winLeafMatch((bytes32,bytes32),bytes32,bytes32,bytes)]]
[[winLeafMatch((bytes32,bytes32),bytes32,bytes32,bytes,bytes)]]
return pcall(
self._send_tx,
self,
tournament_address,
sig,
{ { commitment_one, commitment_two, _tag = "tuple" }, left, right, proof }
{ { commitment_one, commitment_two, _tag = "tuple" }, left, right, proof, input }
)
end

Expand Down
13 changes: 8 additions & 5 deletions prt/client-lua/player/strategy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ function HonestStrategy:_react_match(match, commitment, log)

local cycle = match.base_big_cycle
local ucycle = (match.leaf_cycle & constants.uarch_span):touinteger()
local input = self.inputs[cycle >> constants.log2_emulator_span]
local logs = Machine.get_logs(self.machine_path, self.commitment_builder.snapshot_dir, cycle, ucycle, input)
local logs, input = Machine.get_logs(self.machine_path, self.commitment_builder.snapshot_dir, cycle, ucycle,
self.inputs)

helper.log_full(self.sender.index, string.format(
"win leaf match in tournament %s of level %d for commitment %s",
Expand All @@ -142,7 +142,8 @@ function HonestStrategy:_react_match(match, commitment, log)
match.commitment_two,
left,
right,
logs
logs,
input
)
if not ok then
helper.log_full(self.sender.index, string.format(
Expand Down Expand Up @@ -281,7 +282,8 @@ function HonestStrategy:_react_tournament(tournament, log)
tournament.base_big_cycle,
tournament.level,
tournament.log2_stride,
tournament.log2_stride_count
tournament.log2_stride_count,
self.inputs
)

table.insert(log.tournaments, tournament)
Expand All @@ -299,7 +301,8 @@ function HonestStrategy:_react_tournament(tournament, log)
tournament.parent.base_big_cycle,
tournament.parent.level,
tournament.parent.log2_stride,
tournament.parent.log2_stride_count
tournament.parent.log2_stride_count,
self.inputs
)
if tournament_winner.commitment ~= old_commitment then
helper.log_full(self.sender.index, "player lost tournament")
Expand Down
4 changes: 4 additions & 0 deletions prt/client-rs/src/arena/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use alloy::{

use crate::{
arena::{arena::MatchID, config::BlockchainConfig},
db::compute_state_access::Input,
machine::MachineProof,
};
use cartesi_dave_merkle::{Digest, MerkleProof};
Expand Down Expand Up @@ -145,6 +146,7 @@ pub trait ArenaSender: Send + Sync {
left_node: Digest,
right_node: Digest,
proofs: MachineProof,
input: Input,
) -> Result<()>;

async fn eliminate_match(&self, tournament: Address, match_id: MatchID) -> Result<()>;
Expand Down Expand Up @@ -303,6 +305,7 @@ impl ArenaSender for EthArenaSender {
left_node: Digest,
right_node: Digest,
proofs: MachineProof,
input: Input,
) -> Result<()> {
let tournament = leaftournament::LeafTournament::new(tournament, &self.client);
tournament
Expand All @@ -311,6 +314,7 @@ impl ArenaSender for EthArenaSender {
left_node.into(),
right_node.into(),
Bytes::from(proofs),
Bytes::from(input.0),
)
.send()
.await?
Expand Down
Loading

0 comments on commit 2b92765

Please sign in to comment.