Skip to content

Commit e131992

Browse files
committed
feat: handle revert in machine-runner
1 parent d35a0c5 commit e131992

4 files changed

Lines changed: 57 additions & 15 deletions

File tree

cartesi-rollups/node/machine-runner/src/lib.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ pub mod error;
66
use error::Result;
77
use std::{ops::ControlFlow, time::Duration};
88

9+
use cartesi_machine::types::cmio::ManualReason;
910
use rollups_state_manager::{InputId, StateManager, rollups_machine::RollupsMachine, sync::Watch};
10-
1111
pub struct MachineRunner<SM: StateManager> {
1212
state_manager: SM,
1313
sleep_duration: Duration,
@@ -66,9 +66,18 @@ impl<SM: StateManager + std::fmt::Debug> MachineRunner<SM> {
6666
match next {
6767
Some(input) => {
6868
log::info!("processing input {}", input.id.input_index_in_epoch);
69-
let state_hashes = rollups_machine.process_input(&input.data)?;
70-
self.state_manager
71-
.advance_accepted(rollups_machine, &state_hashes)?;
69+
let (state_hashes, reason) = rollups_machine.process_input(&input.data)?;
70+
71+
match reason {
72+
ManualReason::RxAccepted { .. } => {
73+
self.state_manager
74+
.advance_accepted(rollups_machine, &state_hashes)?;
75+
}
76+
_ => {
77+
self.state_manager
78+
.advance_reverted(rollups_machine, &state_hashes)?;
79+
}
80+
}
7281
}
7382
None => break Ok(rollups_machine),
7483
}

cartesi-rollups/node/state-manager/src/persistent_state_access.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,34 @@ impl StateManager for PersistentStateAccess {
134134
Ok(machine)
135135
}
136136

137-
fn finish_reverted_advance(
137+
fn advance_reverted(
138138
&mut self,
139-
_machine: RollupsMachine,
140-
_leafs: &[CommitmentLeaf],
139+
mut machine: RollupsMachine,
140+
leafs: &[CommitmentLeaf],
141141
) -> Result<RollupsMachine> {
142-
todo!()
142+
assert!(!leafs.is_empty());
143+
let epoch = machine.epoch();
144+
let input = machine.input_index_in_epoch();
145+
146+
rollup_data::insert_state_hashes_for_input(&self.connection, epoch, input, &leafs)?;
147+
148+
let (snapshot_path, snapshot_epoch, snapshot_input) =
149+
rollup_data::latest_snapshot_path(&self.connection)?;
150+
assert_eq!(snapshot_input + 1, input);
151+
assert_eq!(snapshot_epoch, epoch);
152+
153+
// load rollups machine from previous successful (ACCEPT) snapshot
154+
machine = RollupsMachine::new(&snapshot_path, epoch, input)?;
155+
156+
rollup_data::insert_snapshot(
157+
&self.connection,
158+
epoch,
159+
input,
160+
&machine.state_hash()?,
161+
&snapshot_path,
162+
)?;
163+
164+
Ok(machine)
143165
}
144166

145167
fn epoch_state_hashes(&mut self, epoch_number: u64) -> Result<Vec<CommitmentLeaf>> {
@@ -423,7 +445,7 @@ mod tests {
423445
};
424446

425447
initial_snapshot =
426-
access.finish_accepted_advance(initial_snapshot, &[commitment_leaf_1.clone()])?;
448+
access.advance_accepted(initial_snapshot, &[commitment_leaf_1.clone()])?;
427449

428450
assert_eq!(
429451
access.epoch_state_hashes(0)?[0],
@@ -441,7 +463,7 @@ mod tests {
441463

442464
initial_snapshot.increment_input();
443465
initial_snapshot =
444-
access.finish_accepted_advance(initial_snapshot, &[commitment_leaf_2.clone()])?;
466+
access.advance_reverted(initial_snapshot, &[commitment_leaf_2.clone()])?;
445467

446468
assert_eq!(
447469
access.epoch_state_hashes(0)?.len(),

cartesi-rollups/node/state-manager/src/rollups_machine.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ use cartesi_machine::{
1313
constants::{break_reason, pma::TX_START},
1414
error::{MachineError, MachineResult},
1515
machine::Machine,
16-
types::{Hash, cmio::CmioResponseReason},
16+
types::{
17+
Hash,
18+
cmio::{CmioRequest, CmioResponseReason, ManualReason},
19+
},
1720
};
1821

1922
use thiserror::Error;
@@ -91,7 +94,10 @@ impl RollupsMachine {
9194
self.machine.root_hash()
9295
}
9396

94-
pub fn process_input(&mut self, data: &[u8]) -> MachineResult<Vec<CommitmentLeaf>> {
97+
pub fn process_input(
98+
&mut self,
99+
data: &[u8],
100+
) -> MachineResult<(Vec<CommitmentLeaf>, ManualReason)> {
95101
let mut state_hashes = Vec::with_capacity(1 << 20);
96102

97103
self.feed_input(data)?;
@@ -114,9 +120,14 @@ impl RollupsMachine {
114120
hash,
115121
repetitions: STRIDE_COUNT_IN_INPUT - i,
116122
});
117-
118123
self.input_index_in_epoch += 1;
119-
Ok(state_hashes)
124+
125+
match self.machine.receive_cmio_request()? {
126+
CmioRequest::Manual(reason) => Ok((state_hashes, reason)),
127+
_ => {
128+
panic!("This branch should not be reached");
129+
}
130+
}
120131
}
121132

122133
fn feed_input(&mut self, input: &[u8]) -> MachineResult<()> {

cartesi-rollups/node/state-manager/src/state_manager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub trait StateManager {
3636
leafs: &[CommitmentLeaf],
3737
) -> Result<RollupsMachine>;
3838

39-
fn finish_reverted_advance(
39+
fn advance_reverted(
4040
&mut self,
4141
machine: RollupsMachine,
4242
leafs: &[CommitmentLeaf],

0 commit comments

Comments
 (0)