Skip to content

Commit

Permalink
db_exporter add output block execute (writeset and blockinfo)
Browse files Browse the repository at this point in the history
  • Loading branch information
nkysg committed Feb 20, 2024
1 parent 91950fc commit 2b6460f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,18 @@ use starcoin_vm_types::on_chain_resource::Epoch;
use std::cmp::min;
use std::iter::Extend;
use std::option::Option::{None, Some};
use std::sync::atomic::{AtomicBool, Ordering};
use std::{collections::HashMap, sync::Arc};

pub static MAIN_DIRECT_SAVE_BLOCK_HASH: Lazy<HashValue> = Lazy::new(|| {
static MAIN_DIRECT_SAVE_BLOCK_HASH: Lazy<HashValue> = Lazy::new(|| {
HashValue::from_hex_literal(
"0x6f36ea7df4bedb8e8aefebd822d493fb95c9434ae1d5095c0f5f2d7c33e7b866",
)
.expect("")
});

static OUTPUT_BLOCK: AtomicBool = AtomicBool::new(false);

pub struct ChainStatusWithBlock {
pub status: ChainStatus,
pub head: Block,
Expand Down Expand Up @@ -676,6 +679,10 @@ impl BlockChain {
Ok(ExecutedBlock { block, block_info })
}

pub fn set_output_block() {
OUTPUT_BLOCK.store(true, Ordering::Relaxed);
}

fn execute_block_without_save(
statedb: ChainStateDB,
txn_accumulator: MerkleAccumulator,
Expand All @@ -685,7 +692,6 @@ impl BlockChain {
block: Block,
vm_metrics: Option<VMMetrics>,
) -> Result<ExecutedBlock> {
let debug_info = block.id() == *MAIN_DIRECT_SAVE_BLOCK_HASH;
let header = block.header();
debug_assert!(header.is_genesis() || parent_status.is_some());
debug_assert!(!header.is_genesis() || parent_status.is_none());
Expand Down Expand Up @@ -772,7 +778,7 @@ impl BlockChain {
executed_data.txn_events.len() == executed_data.txn_infos.len(),
"events' length should be equal to txn infos' length"
);
if debug_info {
if OUTPUT_BLOCK.load(Ordering::Relaxed) {
println!(
"block {} executed_data {:?} ",
block.header().number(),
Expand Down
62 changes: 62 additions & 0 deletions cmd/db-exporter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ enum Cmd {
GenTurboSTMTransactions(GenTurboSTMTransactionsOptions),
ApplyTurboSTMBlock(ApplyTurboSTMBlockOptions),
VerifyBlock(VerifyBlockOptions),
BlockOutput(BlockOutputOptions),
}

#[derive(Debug, Clone, Parser)]
Expand Down Expand Up @@ -463,6 +464,19 @@ pub struct VerifyBlockOptions {
pub end: Option<BlockNumber>,
}

#[derive(Debug, Parser)]
#[clap(name = "block-output", about = "block output options")]
pub struct BlockOutputOptions {
#[clap(long, short = 'n')]
/// Chain Network
pub net: BuiltinNetworkID,
#[clap(long, short = 'i', parse(from_os_str))]
/// starcoin node db path. like ~/.starcoin/main
pub from_path: PathBuf,
#[clap(long, short = 's')]
pub num: BlockNumber,
}

#[tokio::main(flavor = "multi_thread")]
async fn main() -> anyhow::Result<()> {
let opt = Opt::parse();
Expand Down Expand Up @@ -638,6 +652,10 @@ async fn main() -> anyhow::Result<()> {
);
return result;
}
Cmd::BlockOutput(option) => {
let result = block_output(option.from_path, option.net, option.num);
return result;
}
}
Ok(())
}
Expand Down Expand Up @@ -2211,3 +2229,47 @@ fn verify_block_range(
file.flush()?;
Ok(())
}

pub fn block_output(
from_dir: PathBuf,
network: BuiltinNetworkID,
block_number: BlockNumber,
) -> anyhow::Result<()> {
::starcoin_logger::init();
let net = ChainNetwork::new_builtin(network);
let db_storage = DBStorage::open_with_cfs(
from_dir.join("starcoindb/db/starcoindb"),
StorageVersion::current_version()
.get_column_family_names()
.to_vec(),
true,
Default::default(),
None,
)?;
let storage = Arc::new(Storage::new(StorageInstance::new_cache_and_db_instance(
CacheStorage::new(None),
db_storage,
))?);
let (chain_info, _) =
Genesis::init_and_check_storage(&net, storage.clone(), from_dir.as_ref())?;
let chain = BlockChain::new(
net.time_service(),
chain_info.head().id(),
storage.clone(),
None,
)
.expect("create block chain should success.");
let block = chain
.get_block_by_number(block_number)?
.ok_or_else(|| format_err!("{} get block error", block_number))?;
BlockChain::set_output_block();
let mut chain = BlockChain::new(
net.time_service(),
block.header.parent_hash(),
storage.clone(),
None,
)
.expect("create block chain should success.");
chain.verify_without_save::<BasicVerifier>(block)?;
Ok(())
}

0 comments on commit 2b6460f

Please sign in to comment.