Skip to content

Commit

Permalink
feat(prt): complete rollups meta step
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenctw committed Dec 4, 2024
1 parent cca08b7 commit 901da71
Show file tree
Hide file tree
Showing 21 changed files with 418 additions and 210 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
107 changes: 76 additions & 31 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 @@ -200,18 +228,28 @@ local function ver(t, p, s)
return t
end

local function encode_access_log(logs)
local bint = require 'utils.bint' (256) -- use 256 bits integers

local function encode_access_logs(logs, input)
local encoded = {}

for _, a in ipairs(logs.accesses) do
if a.log2_size == 3 then
table.insert(encoded, a.read)
else
table.insert(encoded, a.read_hash)
end
if input then
-- TODO: check #input is encoded as uint256
table.insert(encoded, bint(#input))
table.insert(encoded, input)
end

for _, h in ipairs(a.sibling_hashes) do
table.insert(encoded, h)
for _, log in ipairs(logs) do
for _, a in ipairs(log.accesses) do
if a.log2_size == 3 then
table.insert(encoded, a.read)
else
table.insert(encoded, a.read_hash)
end

for _, h in ipairs(a.sibling_hashes) do
table.insert(encoded, h)
end
end
end

Expand All @@ -223,38 +261,45 @@ 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 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
table.insert(logs,
machine.machine:log_send_cmio_response(cartesi.machine.HTIF_YIELD_REASON_ADVANCE_STATE, input,
log_type
))
table.insert(logs, machine.machine:log_uarch_step(log_type))
return encode_access_logs(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)
if ucycle == consts.uarch_span then
logs = machine.machine:log_uarch_reset(log_type)
table.insert(logs, machine.machine:log_uarch_reset(log_type))
else
logs = machine.machine:log_uarch_step(log_type)
table.insert(logs, machine.machine:log_uarch_step(log_type))
end
return encode_access_log(logs)
return encode_access_logs(logs, nil)
end

return Machine
10 changes: 6 additions & 4 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 = 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 Down Expand Up @@ -281,7 +281,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 +300,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
Loading

0 comments on commit 901da71

Please sign in to comment.