Skip to content

Commit

Permalink
Add genesis block checks for dev clients
Browse files Browse the repository at this point in the history
  • Loading branch information
raychu86 committed Aug 24, 2022
1 parent 87ec022 commit 4d49f81
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion snarkos/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl Clean {
}

/// Removes the specified ledger from storage.
fn remove_ledger(network: u16, dev: Option<u16>) -> Result<String> {
pub(super) fn remove_ledger(network: u16, dev: Option<u16>) -> Result<String> {
// Construct the path to the ledger in storage.
let path = aleo_std::aleo_ledger_dir(network, dev);
// Check if the path to the ledger exists in storage.
Expand Down
22 changes: 19 additions & 3 deletions snarkos/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use crate::CLI;

use crate::{connect_to_leader, handle_listener, handle_peer, request_genesis_block, send_pings, Account, Ledger};
use crate::{connect_to_leader, handle_listener, handle_peer, request_genesis_block, send_pings, Account, Clean, Ledger};
use snarkos_environment::{helpers::Status, Environment};
use snarkvm::prelude::Network;

Expand Down Expand Up @@ -51,8 +51,24 @@ impl<N: Network, E: Environment> Node<N, E> {
// Request genesis block from the beacon leader.
let genesis_block = request_genesis_block::<N>(&cli.beacon_addr).await?;

// Initialize the ledger from the provided genesis block.
Ledger::<N>::new_with_genesis(account.private_key(), genesis_block).await?
// Initialize the development ledger. This will clear the existing ledger if the
// genesis block does not match the one in the existing ledger.
// Initialize the ledger.
let existing_ledger = Ledger::<N>::load(account.private_key()).await?;

// Check if the existing ledger shares the same genesis block as the beacon node.
let genesis_block_exists = existing_ledger.ledger().read().contains_block_hash(&genesis_block.hash())?;
match genesis_block_exists {
true => existing_ledger.clone(),
false => {
// Remove the existing ledger if the genesis block is not found.
drop(existing_ledger);
Clean::remove_ledger(N::ID, cli.dev)?;

// Initialize a new ledger from the provided genesis block.
Ledger::<N>::new_with_genesis(account.private_key(), genesis_block).await?
}
}
}
};

Expand Down

0 comments on commit 4d49f81

Please sign in to comment.