Skip to content

Commit

Permalink
node: Revert to state_hash of the previous block
Browse files Browse the repository at this point in the history
  • Loading branch information
goshawk-3 committed Jan 19, 2024
1 parent 7fd05b9 commit 5574529
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
28 changes: 22 additions & 6 deletions node/src/chain/acceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ use crate::chain::header_validation::Validator;

#[allow(dead_code)]
pub(crate) enum RevertTarget {
LastFinalizedState = 0,
LastEpoch = 1,
Commit([u8; 32]),
LastFinalizedState,
LastEpoch,
}

/// Implements block acceptance procedure. This includes block header,
Expand Down Expand Up @@ -407,17 +408,32 @@ impl<DB: database::DB, VM: vm::VMExecution, N: Network> Acceptor<N, DB, VM> {

let target_state_hash = match target {
RevertTarget::LastFinalizedState => {
info!(event = "vm_revert to last finalized state");
let state_hash = self.vm.read().await.revert()?;
let vm = self.vm.read().await;
let base_root = vm.get_base_state_root()?;
let state_hash = vm.revert(base_root)?;

info!(
event = "vm reverted",
state_root = hex::encode(state_hash)
state_root = hex::encode(state_hash),
is_final = "true",
);

anyhow::Ok(state_hash)
}
_ => unimplemented!(),
RevertTarget::Commit(state_hash) => {
let vm = self.vm.read().await;
let state_hash = vm.revert(state_hash)?;
let is_final = vm.get_base_state_root()? == state_hash;

info!(
event = "vm reverted",
state_root = hex::encode(state_hash),
is_final,
);

anyhow::Ok(state_hash)
}
RevertTarget::LastEpoch => unimplemented!(),
}?;

// Delete any block until we reach the target_state_hash, the
Expand Down
11 changes: 7 additions & 4 deletions node/src/chain/fsm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ impl<DB: database::DB, VM: vm::VMExecution, N: Network> InSyncImpl<DB, VM, N> {
// R_B.Iteration < L_B.Iteration
//
// Then we fallback to N_B.PrevBlock and accept N_B
let local_header = acc.db.read().await.view(|t| {
let result = acc.db.read().await.view(|t| {
if let Some((prev_header, _)) =
t.fetch_block_header(&remote_blk.header().prev_block_hash)?
{
Expand All @@ -308,20 +308,23 @@ impl<DB: database::DB, VM: vm::VMExecution, N: Network> InSyncImpl<DB, VM, N> {
if remote_blk.header().iteration
< l_b.header().iteration
{
return Ok(Some(l_b.header().clone()));
return Ok(Some((
l_b.header().clone(),
prev_header.state_hash,
)));
}
}
}

anyhow::Ok(None)
})?;

if let Some(local_header) = local_header {
if let Some((local_header, state_hash)) = result {
match fallback::WithContext::new(acc.deref())
.try_revert(
&local_header,
remote_blk.header(),
RevertTarget::LastFinalizedState,
RevertTarget::Commit(state_hash),
)
.await
{
Expand Down
3 changes: 2 additions & 1 deletion node/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub trait VMExecution: Send + Sync + 'static {
) -> anyhow::Result<Provisioners>;

fn get_state_root(&self) -> anyhow::Result<[u8; 32]>;
fn get_base_state_root(&self) -> anyhow::Result<[u8; 32]>;

fn revert(&self) -> anyhow::Result<[u8; 32]>;
fn revert(&self, state_hash: [u8; 32]) -> anyhow::Result<[u8; 32]>;
}
2 changes: 1 addition & 1 deletion node/testbed.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
RUSK_STATE_PATH=${RUSK_STATE_PATH} cargo r --release -p rusk -- recovery-state --init $GENESIS_PATH
echo "starting node $ID ..."
echo "${KEYS_PATH}/node_$ID.keys"
RUSK_STATE_PATH=${RUSK_STATE_PATH} ./target/release/rusk --kadcast-bootstrap "$BOOTSTRAP_ADDR" --kadcast-public-address "$PUBLIC_ADDR" --log-level="$LOG_LEVEL" --log-filter="dusk_consensus=debug" --consensus-keys-path="${KEYS_PATH}/node_$ID.keys" --db-path="$NODE_FOLDER" --http-listen-addr "$WS_LISTEN_ADDR" --delay-on-resp-msg=10 > "${TEMPD}/node_${ID}.log" &
RUSK_STATE_PATH=${RUSK_STATE_PATH} ./target/release/rusk --kadcast-bootstrap "$BOOTSTRAP_ADDR" --kadcast-public-address "$PUBLIC_ADDR" --log-type="json" --log-level="$LOG_LEVEL" --log-filter="dusk_consensus=debug" --consensus-keys-path="${KEYS_PATH}/node_$ID.keys" --db-path="$NODE_FOLDER" --http-listen-addr "$WS_LISTEN_ADDR" --delay-on-resp-msg=10 > "${TEMPD}/node_${ID}.log" &
}

## Use ~/.cargo/bin/tokio-console --retain-for 0s http://127.0.0.1:10000 to connect console to first node
Expand Down

0 comments on commit 5574529

Please sign in to comment.