Skip to content

Commit aaad66e

Browse files
authored
chore: add localnet gen block (#6176)
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.
1 parent 2ccde17 commit aaad66e

File tree

7 files changed

+58
-72
lines changed

7 files changed

+58
-72
lines changed

base_layer/core/src/blocks/genesis_block.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub fn get_genesis_block(network: Network) -> ChainBlock {
4848
NextNet => get_nextnet_genesis_block(),
4949
Igor => get_igor_genesis_block(),
5050
Esmeralda => get_esmeralda_genesis_block(),
51-
LocalNet => get_esmeralda_genesis_block(),
51+
LocalNet => get_localnet_genesis_block(),
5252
}
5353
}
5454

@@ -321,6 +321,40 @@ fn get_esmeralda_genesis_block_raw() -> Block {
321321
get_raw_block(&genesis_timestamp, &not_before_proof.to_vec())
322322
}
323323

324+
pub fn get_localnet_genesis_block() -> ChainBlock {
325+
// lets get the block
326+
let block = crate::blocks::genesis_block::get_localnet_genesis_block_raw();
327+
let accumulated_data = BlockHeaderAccumulatedData {
328+
hash: block.hash(),
329+
total_kernel_offset: block.header.total_kernel_offset.clone(),
330+
achieved_difficulty: Difficulty::min(),
331+
total_accumulated_difficulty: 1.into(),
332+
accumulated_randomx_difficulty: AccumulatedDifficulty::min(),
333+
accumulated_sha3x_difficulty: AccumulatedDifficulty::min(),
334+
target_difficulty: Difficulty::min(),
335+
};
336+
ChainBlock::try_construct(Arc::new(block), accumulated_data).unwrap()
337+
}
338+
339+
fn get_localnet_genesis_block_raw() -> Block {
340+
// Set genesis timestamp
341+
let genesis_timestamp = DateTime::parse_from_rfc2822("20 Feb 2024 08:01:00 +0200").expect("parse may not fail");
342+
// Let us add a "not before" proof to the genesis block
343+
let not_before_proof =
344+
b"as I sip my drink, thoughts of esmeralda consume my mind, like a refreshing nourishing draught \
345+
\
346+
The New York Times , 2000/01/01 \
347+
\
348+
Lorem Ipsum \
349+
\
350+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore \
351+
magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo \
352+
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla \
353+
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id \
354+
est laborum.";
355+
get_raw_block(&genesis_timestamp, &not_before_proof.to_vec())
356+
}
357+
324358
fn get_raw_block(genesis_timestamp: &DateTime<FixedOffset>, not_before_proof: &[u8]) -> Block {
325359
// Note: Use 'print_new_genesis_block_values' in core/tests/helpers/block_builders.rs to generate the required
326360
// fields below
@@ -418,6 +452,13 @@ mod test {
418452
check_block(Network::Igor, &block, 0, 0);
419453
}
420454

455+
#[test]
456+
fn localnet_genesis_sanity_check() {
457+
// Note: If outputs and kernels are added, this test will fail unless you explicitly check that network == Igor
458+
let block = get_localnet_genesis_block();
459+
check_block(Network::LocalNet, &block, 0, 0);
460+
}
461+
421462
fn check_block(network: Network, block: &ChainBlock, expected_outputs: usize, expected_kernels: usize) {
422463
assert!(block.block().body.inputs().is_empty());
423464
assert_eq!(block.block().body.kernels().len(), expected_kernels);

base_layer/core/src/chain_storage/blockchain_database.rs

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2522,7 +2522,6 @@ mod test {
25222522
create_new_blockchain,
25232523
create_orphan_chain,
25242524
create_test_blockchain_db,
2525-
rewind_smt,
25262525
update_block_and_smt,
25272526
TempDatabase,
25282527
},
@@ -2602,11 +2601,9 @@ mod test {
26022601
])
26032602
.await;
26042603
// Create reorg chain
2604+
// we only need a smt, this one will not be technically correct, but due to the use of mockvalidators(true),
2605+
// they will pass all mr tests
26052606
let mut smt = db.fetch_tip_smt().unwrap();
2606-
let d_block = mainchain.get("D").unwrap().clone();
2607-
rewind_smt(d_block, &mut smt);
2608-
let c_block = mainchain.get("C").unwrap().clone();
2609-
rewind_smt(c_block, &mut smt);
26102607
let fork_root = mainchain.get("B").unwrap().clone();
26112608
let (_, reorg_chain) = create_orphan_chain(
26122609
&db,
@@ -2722,7 +2719,6 @@ mod test {
27222719
#[tokio::test]
27232720
async fn it_correctly_detects_strongest_orphan_tips() {
27242721
let db = create_new_blockchain();
2725-
let mut gen_smt = db.fetch_tip_smt().unwrap();
27262722
let validator = MockValidator::new(true);
27272723
let (_, main_chain) = create_main_chain(&db, &[
27282724
("A->GB", 1, 120),
@@ -2737,20 +2733,9 @@ mod test {
27372733

27382734
// Fork 1 (with 3 blocks)
27392735
let fork_root_1 = main_chain.get("A").unwrap().clone();
2736+
// we only need a smt, this one will not be technically correct, but due to the use of mockvalidators(true),
2737+
// they will pass all mr tests
27402738
let mut smt = db.fetch_tip_smt().unwrap();
2741-
let g_block = main_chain.get("G").unwrap().clone();
2742-
rewind_smt(g_block, &mut smt);
2743-
let f_block = main_chain.get("F").unwrap().clone();
2744-
rewind_smt(f_block, &mut smt);
2745-
let e_block = main_chain.get("E").unwrap().clone();
2746-
rewind_smt(e_block, &mut smt);
2747-
let d_block = main_chain.get("D").unwrap().clone();
2748-
rewind_smt(d_block, &mut smt);
2749-
let c_block = main_chain.get("C").unwrap().clone();
2750-
rewind_smt(c_block, &mut smt);
2751-
let mut c_smt = smt.clone();
2752-
let b_block = main_chain.get("B").unwrap().clone();
2753-
rewind_smt(b_block, &mut smt);
27542739

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

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

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

27702755
// Add blocks to db
27712756
let mut access = db.db_write_access().unwrap();
@@ -2852,13 +2837,6 @@ mod test {
28522837
)
28532838
.await;
28542839

2855-
let b6_block = main_chain.get("6b").unwrap().clone();
2856-
rewind_smt(b6_block, &mut smt);
2857-
let b5_block = main_chain.get("5b").unwrap().clone();
2858-
rewind_smt(b5_block, &mut smt);
2859-
let b4_block = main_chain.get("4b").unwrap().clone();
2860-
rewind_smt(b4_block, &mut smt);
2861-
28622840
// Add orphans out of height order
28632841
for name in ["5b", "3b", "4b", "6b"] {
28642842
let block = orphan_chain_b.get(name).unwrap();
@@ -2875,8 +2853,6 @@ mod test {
28752853
)
28762854
.await;
28772855

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

32713247
let mock_validator = MockValidator::new(true);
32723248
let chain_strength_comparer = strongest_chain().by_sha3x_difficulty().build();
3249+
// we only need a smt, this one will not be technically correct, but due to the use of mockvalidators(true),
3250+
// they will pass all mr tests
32733251
let mut smt = db.fetch_tip_smt().unwrap();
3274-
let d_block = mainchain.get("D").unwrap().clone();
3275-
rewind_smt(d_block, &mut smt);
32763252
let fork_block = mainchain.get("C").unwrap().clone();
32773253
let (_, reorg_chain) =
32783254
create_chained_blocks(&[("D2->GB", 1, 120), ("E2->D2", 2, 120)], fork_block, &mut smt).await;

base_layer/core/src/chain_storage/tests/blockchain_database.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -477,22 +477,6 @@ mod prepare_new_block {
477477
mod fetch_header_containing_kernel_mmr {
478478
use super::*;
479479
use crate::transactions::key_manager::create_memory_db_key_manager;
480-
481-
#[test]
482-
fn it_returns_genesis() {
483-
let db = setup();
484-
let genesis = db.fetch_block(0, true).unwrap();
485-
assert_eq!(genesis.block().body.kernels().len(), 1);
486-
let mut mmr_position = 0;
487-
genesis.block().body.kernels().iter().for_each(|_| {
488-
let header = db.fetch_header_containing_kernel_mmr(mmr_position).unwrap();
489-
assert_eq!(header.height(), 0);
490-
mmr_position += 1;
491-
});
492-
let err = db.fetch_header_containing_kernel_mmr(mmr_position).unwrap_err();
493-
matches!(err, ChainStorageError::ValueNotFound { .. });
494-
}
495-
496480
#[tokio::test]
497481
async fn it_returns_corresponding_header() {
498482
let db = setup();

base_layer/core/src/consensus/consensus_constants.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ impl ConsensusConstants {
394394
max_randomx_seed_height: u64::MAX,
395395
max_extra_field_size: 200,
396396
proof_of_work: algos,
397-
faucet_value: ESMERALDA_FAUCET_VALUE.into(), // The esmeralda genesis block is re-used for localnet
397+
faucet_value: 0.into(),
398398
transaction_weight: TransactionWeight::latest(),
399399
max_script_byte_size: 2048,
400400
input_version_range,

base_layer/core/src/test_helpers/blockchain.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use tari_common_types::{
3434
tari_address::TariAddress,
3535
types::{Commitment, FixedHash, HashOutput, PublicKey, Signature},
3636
};
37-
use tari_mmr::sparse_merkle_tree::{DeleteResult, NodeKey, ValueHash};
37+
use tari_mmr::sparse_merkle_tree::{NodeKey, ValueHash};
3838
use tari_storage::lmdb_store::LMDBConfig;
3939
use tari_test_utils::paths::create_temporary_data_path;
4040
use tari_utilities::ByteArray;
@@ -493,21 +493,6 @@ pub async fn create_main_chain<T: Into<BlockSpecs>>(
493493
(names, chain)
494494
}
495495

496-
pub fn rewind_smt(block: Arc<ChainBlock>, smt: &mut OutputSmt) {
497-
for input in block.block().body.inputs() {
498-
let smt_key = NodeKey::try_from(input.commitment().unwrap().as_bytes()).unwrap();
499-
let smt_node = ValueHash::try_from(input.smt_hash(block.header().height).as_slice()).unwrap();
500-
smt.insert(smt_key, smt_node).unwrap();
501-
}
502-
for output in block.block().body.outputs() {
503-
let smt_key = NodeKey::try_from(output.commitment.as_bytes()).unwrap();
504-
match smt.delete(&smt_key).unwrap() {
505-
DeleteResult::Deleted(_value_hash) => {},
506-
DeleteResult::KeyNotFound => panic!("key should be found"),
507-
};
508-
}
509-
}
510-
511496
pub async fn create_orphan_chain<T: Into<BlockSpecs>>(
512497
db: &BlockchainDatabase<TempDatabase>,
513498
blocks: T,

base_layer/tari_mining_helper_ffi/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,14 +388,14 @@ mod tests {
388388
fn detect_change_in_consensus_encoding() {
389389
#[cfg(tari_target_network_mainnet)]
390390
let (nonce, difficulty) = match Network::get_current_or_user_setting_or_default() {
391-
Network::MainNet => (9205754023158580549, Difficulty::from_u64(1015).unwrap()),
392-
Network::StageNet => (12022341430563186162, Difficulty::from_u64(1011).unwrap()),
391+
Network::MainNet => (3145418102407526886, Difficulty::from_u64(1505).unwrap()),
392+
Network::StageNet => (135043993867732261, Difficulty::from_u64(1059).unwrap()),
393393
_ => panic!("Invalid network for mainnet target"),
394394
};
395395
#[cfg(tari_target_network_nextnet)]
396-
let (nonce, difficulty) = (8721374869059089110, Difficulty::from_u64(3037).unwrap());
396+
let (nonce, difficulty) = (5154919981564263219, Difficulty::from_u64(2950).unwrap());
397397
#[cfg(not(any(tari_target_network_mainnet, tari_target_network_nextnet)))]
398-
let (nonce, difficulty) = (9860518124890236943, Difficulty::from_u64(2724).unwrap());
398+
let (nonce, difficulty) = (8520885611996410570, Difficulty::from_u64(3143).unwrap());
399399
unsafe {
400400
let mut error = -1;
401401
let error_ptr = &mut error as *mut c_int;

integration_tests/tests/steps/node_steps.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ async fn no_meddling_with_data(world: &mut TariWorld, node: String) {
687687
Ok(_) => panic!("The block should not have been valid"),
688688
Err(e) => assert_eq!(
689689
"Chain storage error: Validation error: Block validation error: MMR size for Kernel does not match. \
690-
Expected: 3, received: 4"
690+
Expected: 2, received: 3"
691691
.to_string(),
692692
e.message()
693693
),
@@ -712,7 +712,7 @@ async fn no_meddling_with_data(world: &mut TariWorld, node: String) {
712712
Ok(_) => panic!("The block should not have been valid"),
713713
Err(e) => assert_eq!(
714714
"Chain storage error: Validation error: Block validation error: MMR size for UTXO does not match. \
715-
Expected: 102, received: 103"
715+
Expected: 2, received: 3"
716716
.to_string(),
717717
e.message()
718718
),

0 commit comments

Comments
 (0)