Skip to content

Commit

Permalink
node: Load block label from DB on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
goshawk committed Dec 6, 2023
1 parent 7e6a39e commit 84cb658
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
28 changes: 18 additions & 10 deletions node/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use tracing::{error, info, warn};

use async_trait::async_trait;

use node_data::ledger::{to_str, Block, Label};
use node_data::ledger::{to_str, Block, BlockWithLabel, Label};

use node_data::message::AsyncQueue;
use node_data::message::{Payload, Topics};
Expand Down Expand Up @@ -97,7 +97,9 @@ impl<N: Network, DB: database::DB, VM: vm::VMExecution>
);

// Detect a consistency issue between VM and Ledger states.
if mrb.header().height > 0 && mrb.header().state_hash != state_root {
if mrb.inner().header().height > 0
&& mrb.inner().header().state_hash != state_root
{
info!("revert to last finalized state");
// Revert to last known finalized state.
acc.read()
Expand Down Expand Up @@ -212,11 +214,11 @@ impl ChainSrv {
/// If register entry is read but block is not found.
async fn load_most_recent_block<DB: database::DB>(
db: Arc<RwLock<DB>>,
) -> Result<Block> {
let mut mrb = Block::default();
) -> Result<BlockWithLabel> {
let mut blk = Block::default();

db.read().await.update(|t| {
mrb = match t.get_register()? {
blk = match t.get_register()? {
Some(r) => t.fetch_block(&r.mrb_hash)?.unwrap(),
None => {
// Lack of register record means the loaded database is
Expand All @@ -229,18 +231,24 @@ impl ChainSrv {
genesis_blk
}
};

Ok(())
})?;

let label = db
.read()
.await
.view(|t| t.fetch_block_label_by_height(blk.header().height))?
.unwrap();

tracing::info!(
event = "Ledger block loaded",
height = mrb.header().height,
hash = hex::encode(mrb.header().hash),
state_root = hex::encode(mrb.header().state_hash)
height = blk.header().height,
hash = hex::encode(blk.header().hash),
state_root = hex::encode(blk.header().state_hash),
label = format!("{:?}", label),
);

Ok(mrb)
Ok(BlockWithLabel::new_with_label(blk, label))
}

fn next_timeout() -> Instant {
Expand Down
10 changes: 4 additions & 6 deletions node/src/chain/acceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,14 @@ impl<DB: database::DB, VM: vm::VMExecution, N: Network> Drop
impl<DB: database::DB, VM: vm::VMExecution, N: Network> Acceptor<N, DB, VM> {
pub async fn new_with_run(
keys_path: &str,
mrb: &Block,
mrb: &BlockWithLabel,
provisioners_list: &Provisioners,
db: Arc<RwLock<DB>>,
network: Arc<RwLock<N>>,
vm: Arc<RwLock<VM>>,
) -> Self {
let acc = Self {
mrb: RwLock::new(BlockWithLabel::new_with_label(
mrb.clone(),
Label::Accepted, // TODO: Load this from DB
)),
mrb: RwLock::new(mrb.clone()),
provisioners_list: RwLock::new(provisioners_list.clone()),
db: db.clone(),
vm: vm.clone(),
Expand All @@ -82,7 +79,7 @@ impl<DB: database::DB, VM: vm::VMExecution, N: Network> Acceptor<N, DB, VM> {
};

acc.task.write().await.spawn(
mrb,
mrb.inner(),
provisioners_list,
&db,
&vm,
Expand Down Expand Up @@ -319,6 +316,7 @@ impl<DB: database::DB, VM: vm::VMExecution, N: Network> Acceptor<N, DB, VM> {
block_time,
generator = blk.header().generator_bls_pubkey.to_bs58(),
dur_ms = duration.as_millis(),
label = format!("{:?}", label),
);

// Restart Consensus.
Expand Down

0 comments on commit 84cb658

Please sign in to comment.