Skip to content

Commit

Permalink
chore: add localnet gen block (#6176)
Browse files Browse the repository at this point in the history
Description
---
added localnet gen block so that cucumbers can use that block. Local net
used esmeralda block before, but when rewinding back to 0, the ouput_mr
will be different between esmeralda and localnet, so we need a unique
block for local net as well.
  • Loading branch information
SWvheerden committed Mar 1, 2024
1 parent 2ccde17 commit aaad66e
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 72 deletions.
43 changes: 42 additions & 1 deletion base_layer/core/src/blocks/genesis_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn get_genesis_block(network: Network) -> ChainBlock {
NextNet => get_nextnet_genesis_block(),
Igor => get_igor_genesis_block(),
Esmeralda => get_esmeralda_genesis_block(),
LocalNet => get_esmeralda_genesis_block(),
LocalNet => get_localnet_genesis_block(),
}
}

Expand Down Expand Up @@ -321,6 +321,40 @@ fn get_esmeralda_genesis_block_raw() -> Block {
get_raw_block(&genesis_timestamp, &not_before_proof.to_vec())
}

pub fn get_localnet_genesis_block() -> ChainBlock {
// lets get the block
let block = crate::blocks::genesis_block::get_localnet_genesis_block_raw();
let accumulated_data = BlockHeaderAccumulatedData {
hash: block.hash(),
total_kernel_offset: block.header.total_kernel_offset.clone(),
achieved_difficulty: Difficulty::min(),
total_accumulated_difficulty: 1.into(),
accumulated_randomx_difficulty: AccumulatedDifficulty::min(),
accumulated_sha3x_difficulty: AccumulatedDifficulty::min(),
target_difficulty: Difficulty::min(),
};
ChainBlock::try_construct(Arc::new(block), accumulated_data).unwrap()
}

fn get_localnet_genesis_block_raw() -> Block {
// Set genesis timestamp
let genesis_timestamp = DateTime::parse_from_rfc2822("20 Feb 2024 08:01:00 +0200").expect("parse may not fail");
// Let us add a "not before" proof to the genesis block
let not_before_proof =
b"as I sip my drink, thoughts of esmeralda consume my mind, like a refreshing nourishing draught \
\
The New York Times , 2000/01/01 \
\
Lorem Ipsum \
\
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore \
magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo \
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla \
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id \
est laborum.";
get_raw_block(&genesis_timestamp, &not_before_proof.to_vec())
}

fn get_raw_block(genesis_timestamp: &DateTime<FixedOffset>, not_before_proof: &[u8]) -> Block {
// Note: Use 'print_new_genesis_block_values' in core/tests/helpers/block_builders.rs to generate the required
// fields below
Expand Down Expand Up @@ -418,6 +452,13 @@ mod test {
check_block(Network::Igor, &block, 0, 0);
}

#[test]
fn localnet_genesis_sanity_check() {
// Note: If outputs and kernels are added, this test will fail unless you explicitly check that network == Igor
let block = get_localnet_genesis_block();
check_block(Network::LocalNet, &block, 0, 0);
}

fn check_block(network: Network, block: &ChainBlock, expected_outputs: usize, expected_kernels: usize) {
assert!(block.block().body.inputs().is_empty());
assert_eq!(block.block().body.kernels().len(), expected_kernels);
Expand Down
40 changes: 8 additions & 32 deletions base_layer/core/src/chain_storage/blockchain_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2522,7 +2522,6 @@ mod test {
create_new_blockchain,
create_orphan_chain,
create_test_blockchain_db,
rewind_smt,
update_block_and_smt,
TempDatabase,
},
Expand Down Expand Up @@ -2602,11 +2601,9 @@ mod test {
])
.await;
// Create reorg chain
// we only need a smt, this one will not be technically correct, but due to the use of mockvalidators(true),
// they will pass all mr tests
let mut smt = db.fetch_tip_smt().unwrap();
let d_block = mainchain.get("D").unwrap().clone();
rewind_smt(d_block, &mut smt);
let c_block = mainchain.get("C").unwrap().clone();
rewind_smt(c_block, &mut smt);
let fork_root = mainchain.get("B").unwrap().clone();
let (_, reorg_chain) = create_orphan_chain(
&db,
Expand Down Expand Up @@ -2722,7 +2719,6 @@ mod test {
#[tokio::test]
async fn it_correctly_detects_strongest_orphan_tips() {
let db = create_new_blockchain();
let mut gen_smt = db.fetch_tip_smt().unwrap();
let validator = MockValidator::new(true);
let (_, main_chain) = create_main_chain(&db, &[
("A->GB", 1, 120),
Expand All @@ -2737,20 +2733,9 @@ mod test {

// Fork 1 (with 3 blocks)
let fork_root_1 = main_chain.get("A").unwrap().clone();
// we only need a smt, this one will not be technically correct, but due to the use of mockvalidators(true),
// they will pass all mr tests
let mut smt = db.fetch_tip_smt().unwrap();
let g_block = main_chain.get("G").unwrap().clone();
rewind_smt(g_block, &mut smt);
let f_block = main_chain.get("F").unwrap().clone();
rewind_smt(f_block, &mut smt);
let e_block = main_chain.get("E").unwrap().clone();
rewind_smt(e_block, &mut smt);
let d_block = main_chain.get("D").unwrap().clone();
rewind_smt(d_block, &mut smt);
let c_block = main_chain.get("C").unwrap().clone();
rewind_smt(c_block, &mut smt);
let mut c_smt = smt.clone();
let b_block = main_chain.get("B").unwrap().clone();
rewind_smt(b_block, &mut smt);

let (_, orphan_chain_1) = create_chained_blocks(
&[("B2->GB", 1, 120), ("C2->B2", 1, 120), ("D2->C2", 1, 120)],
Expand All @@ -2761,11 +2746,11 @@ mod test {

// Fork 2 (with 1 block)
let fork_root_2 = main_chain.get("GB").unwrap().clone();
let (_, orphan_chain_2) = create_chained_blocks(&[("B3->GB", 1, 120)], fork_root_2, &mut gen_smt).await;
let (_, orphan_chain_2) = create_chained_blocks(&[("B3->GB", 1, 120)], fork_root_2, &mut smt).await;

// Fork 3 (with 1 block)
let fork_root_3 = main_chain.get("B").unwrap().clone();
let (_, orphan_chain_3) = create_chained_blocks(&[("B4->GB", 1, 120)], fork_root_3, &mut c_smt).await;
let (_, orphan_chain_3) = create_chained_blocks(&[("B4->GB", 1, 120)], fork_root_3, &mut smt).await;

// Add blocks to db
let mut access = db.db_write_access().unwrap();
Expand Down Expand Up @@ -2852,13 +2837,6 @@ mod test {
)
.await;

let b6_block = main_chain.get("6b").unwrap().clone();
rewind_smt(b6_block, &mut smt);
let b5_block = main_chain.get("5b").unwrap().clone();
rewind_smt(b5_block, &mut smt);
let b4_block = main_chain.get("4b").unwrap().clone();
rewind_smt(b4_block, &mut smt);

// Add orphans out of height order
for name in ["5b", "3b", "4b", "6b"] {
let block = orphan_chain_b.get(name).unwrap();
Expand All @@ -2875,8 +2853,6 @@ mod test {
)
.await;

let c7_block = main_chain.get("7c").unwrap().clone();
rewind_smt(c7_block, &mut smt);
for name in ["7c", "5c", "6c", "4c"] {
let block = orphan_chain_c.get(name).unwrap();
let result = test.handle_possible_reorg(block.to_arc_block()).unwrap();
Expand Down Expand Up @@ -3270,9 +3246,9 @@ mod test {

let mock_validator = MockValidator::new(true);
let chain_strength_comparer = strongest_chain().by_sha3x_difficulty().build();
// we only need a smt, this one will not be technically correct, but due to the use of mockvalidators(true),
// they will pass all mr tests
let mut smt = db.fetch_tip_smt().unwrap();
let d_block = mainchain.get("D").unwrap().clone();
rewind_smt(d_block, &mut smt);
let fork_block = mainchain.get("C").unwrap().clone();
let (_, reorg_chain) =
create_chained_blocks(&[("D2->GB", 1, 120), ("E2->D2", 2, 120)], fork_block, &mut smt).await;
Expand Down
16 changes: 0 additions & 16 deletions base_layer/core/src/chain_storage/tests/blockchain_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,22 +477,6 @@ mod prepare_new_block {
mod fetch_header_containing_kernel_mmr {
use super::*;
use crate::transactions::key_manager::create_memory_db_key_manager;

#[test]
fn it_returns_genesis() {
let db = setup();
let genesis = db.fetch_block(0, true).unwrap();
assert_eq!(genesis.block().body.kernels().len(), 1);
let mut mmr_position = 0;
genesis.block().body.kernels().iter().for_each(|_| {
let header = db.fetch_header_containing_kernel_mmr(mmr_position).unwrap();
assert_eq!(header.height(), 0);
mmr_position += 1;
});
let err = db.fetch_header_containing_kernel_mmr(mmr_position).unwrap_err();
matches!(err, ChainStorageError::ValueNotFound { .. });
}

#[tokio::test]
async fn it_returns_corresponding_header() {
let db = setup();
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/consensus/consensus_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ impl ConsensusConstants {
max_randomx_seed_height: u64::MAX,
max_extra_field_size: 200,
proof_of_work: algos,
faucet_value: ESMERALDA_FAUCET_VALUE.into(), // The esmeralda genesis block is re-used for localnet
faucet_value: 0.into(),
transaction_weight: TransactionWeight::latest(),
max_script_byte_size: 2048,
input_version_range,
Expand Down
17 changes: 1 addition & 16 deletions base_layer/core/src/test_helpers/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use tari_common_types::{
tari_address::TariAddress,
types::{Commitment, FixedHash, HashOutput, PublicKey, Signature},
};
use tari_mmr::sparse_merkle_tree::{DeleteResult, NodeKey, ValueHash};
use tari_mmr::sparse_merkle_tree::{NodeKey, ValueHash};
use tari_storage::lmdb_store::LMDBConfig;
use tari_test_utils::paths::create_temporary_data_path;
use tari_utilities::ByteArray;
Expand Down Expand Up @@ -493,21 +493,6 @@ pub async fn create_main_chain<T: Into<BlockSpecs>>(
(names, chain)
}

pub fn rewind_smt(block: Arc<ChainBlock>, smt: &mut OutputSmt) {
for input in block.block().body.inputs() {
let smt_key = NodeKey::try_from(input.commitment().unwrap().as_bytes()).unwrap();
let smt_node = ValueHash::try_from(input.smt_hash(block.header().height).as_slice()).unwrap();
smt.insert(smt_key, smt_node).unwrap();
}
for output in block.block().body.outputs() {
let smt_key = NodeKey::try_from(output.commitment.as_bytes()).unwrap();
match smt.delete(&smt_key).unwrap() {
DeleteResult::Deleted(_value_hash) => {},
DeleteResult::KeyNotFound => panic!("key should be found"),
};
}
}

pub async fn create_orphan_chain<T: Into<BlockSpecs>>(
db: &BlockchainDatabase<TempDatabase>,
blocks: T,
Expand Down
8 changes: 4 additions & 4 deletions base_layer/tari_mining_helper_ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,14 +388,14 @@ mod tests {
fn detect_change_in_consensus_encoding() {
#[cfg(tari_target_network_mainnet)]
let (nonce, difficulty) = match Network::get_current_or_user_setting_or_default() {
Network::MainNet => (9205754023158580549, Difficulty::from_u64(1015).unwrap()),
Network::StageNet => (12022341430563186162, Difficulty::from_u64(1011).unwrap()),
Network::MainNet => (3145418102407526886, Difficulty::from_u64(1505).unwrap()),
Network::StageNet => (135043993867732261, Difficulty::from_u64(1059).unwrap()),
_ => panic!("Invalid network for mainnet target"),
};
#[cfg(tari_target_network_nextnet)]
let (nonce, difficulty) = (8721374869059089110, Difficulty::from_u64(3037).unwrap());
let (nonce, difficulty) = (5154919981564263219, Difficulty::from_u64(2950).unwrap());
#[cfg(not(any(tari_target_network_mainnet, tari_target_network_nextnet)))]
let (nonce, difficulty) = (9860518124890236943, Difficulty::from_u64(2724).unwrap());
let (nonce, difficulty) = (8520885611996410570, Difficulty::from_u64(3143).unwrap());
unsafe {
let mut error = -1;
let error_ptr = &mut error as *mut c_int;
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/tests/steps/node_steps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ async fn no_meddling_with_data(world: &mut TariWorld, node: String) {
Ok(_) => panic!("The block should not have been valid"),
Err(e) => assert_eq!(
"Chain storage error: Validation error: Block validation error: MMR size for Kernel does not match. \
Expected: 3, received: 4"
Expected: 2, received: 3"
.to_string(),
e.message()
),
Expand All @@ -712,7 +712,7 @@ async fn no_meddling_with_data(world: &mut TariWorld, node: String) {
Ok(_) => panic!("The block should not have been valid"),
Err(e) => assert_eq!(
"Chain storage error: Validation error: Block validation error: MMR size for UTXO does not match. \
Expected: 102, received: 103"
Expected: 2, received: 3"
.to_string(),
e.message()
),
Expand Down

0 comments on commit aaad66e

Please sign in to comment.