Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Update view-genesis command to view genesis in store #11809

Merged
merged 12 commits into from
Jul 18, 2024
10 changes: 5 additions & 5 deletions tools/state-viewer/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,14 +746,14 @@ pub struct ViewGenesisCmd {
/// If true, displays the genesis block built from nearcore code that combines the
/// contents of the genesis config (JSON) file with some hard-coded logic to set some
/// fields of the genesis block. At any given time, the block built this way should match
/// the genesis block recorded in the store (to be displayed with the --view-store option).
/// the genesis block recorded in the store (to be displayed with the --store option).
#[clap(long)]
view_config: bool,
config: bool,
/// If true, displays the genesis block saved in the store, when the genesis block is built
/// for the first time. At any given time, this saved block should match the genesis block
/// built by the code (to be displayed with the --view-config option).
/// built by the code (to be displayed with the --config option).
#[clap(long)]
view_store: bool,
store: bool,
/// If true, compares the contents of the genesis block saved in the store with
/// the genesis block built from the genesis config (JSON) file.
#[clap(long, default_value = "false")]
Expand All @@ -762,7 +762,7 @@ pub struct ViewGenesisCmd {

impl ViewGenesisCmd {
pub fn run(self, home_dir: &Path, near_config: NearConfig, store: Store) {
view_genesis(home_dir, near_config, store, self.view_config, self.view_store, self.compare);
view_genesis(home_dir, near_config, store, self.config, self.store, self.compare);
}
}

Expand Down
46 changes: 39 additions & 7 deletions tools/state-viewer/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use near_primitives::epoch_manager::epoch_info::EpochInfo;
use near_primitives::hash::CryptoHash;
use near_primitives::shard_layout::ShardLayout;
use near_primitives::shard_layout::ShardUId;
use near_primitives::sharding::ChunkHash;
use near_primitives::sharding::{ChunkHash, ShardChunk};
use near_primitives::state::FlatStateValue;
use near_primitives::state_record::state_record_to_account_id;
use near_primitives::state_record::StateRecord;
Expand Down Expand Up @@ -882,13 +882,12 @@ pub(crate) fn view_genesis(
epoch_manager.clone(),
)
.unwrap();
let chain_store = ChainStore::new(
store,
near_config.genesis.config.genesis_height,
near_config.client_config.save_trie_changes,
);
let genesis_height = near_config.genesis.config.genesis_height;
let chain_store =
ChainStore::new(store, genesis_height, near_config.client_config.save_trie_changes);

if view_config || compare {
tracing::info!(target: "state_viewer", "Computing genesis from config...");
let state_roots =
near_store::get_genesis_state_roots(chain_store.store()).unwrap().unwrap();
let (genesis_block, genesis_chunks) = Chain::make_genesis_block(
Expand Down Expand Up @@ -923,8 +922,41 @@ pub(crate) fn view_genesis(
}

if view_store {
unimplemented!("Viewing genesis from config is not yet implemented")
tracing::info!(target: "state_viewer", genesis_height, "Reading genesis from store...");
match read_genesis_from_store(&chain_store, genesis_height) {
Ok((genesis_block, genesis_chunks)) => {
println!("Genesis block from store: {:#?}", genesis_block);
for chunk in genesis_chunks {
println!(
"Genesis chunk from store at shard {}: {:#?}",
chunk.shard_id(),
chunk
);
}
}
Err(error) => {
println!("Failed to read genesis block from store. Error: {}", error);
if !near_config.config.archive {
println!("Hint: This is not an archival node. Try running this command from an archival node since genesis block may be garbage collected.");
}
}
}
}
}

fn read_genesis_from_store(
chain_store: &ChainStore,
genesis_height: u64,
) -> Result<(Block, Vec<Arc<ShardChunk>>), Error> {
let genesis_hash = chain_store.get_block_hash_by_height(genesis_height)?;
let genesis_block = chain_store.get_block(&genesis_hash)?;
let mut genesis_chunks = vec![];
for chunk_header in genesis_block.chunks().iter() {
if chunk_header.height_included() == genesis_height {
genesis_chunks.push(chain_store.get_chunk(&chunk_header.chunk_hash())?);
}
}
Ok((genesis_block, genesis_chunks))
}

pub(crate) fn check_block_chunk_existence(near_config: NearConfig, store: Store) {
Expand Down
Loading