mirror of
https://github.com/Cuprate/cuprate.git
synced 2024-12-22 19:49:28 +00:00
add test
This commit is contained in:
parent
a9d8eee373
commit
123aedd6a9
7 changed files with 120 additions and 6 deletions
|
@ -171,7 +171,7 @@ pub fn get_alt_block_extended_header_from_height(
|
|||
let block_header = BlockHeader::read(&mut block_blob.as_slice())?;
|
||||
|
||||
Ok(ExtendedBlockHeader {
|
||||
version: HardFork::from_version(0).expect("Block in DB must have correct version"),
|
||||
version: HardFork::from_version(block_header.hardfork_version).expect("Block in DB must have correct version"),
|
||||
vote: block_header.hardfork_version,
|
||||
timestamp: block_header.timestamp,
|
||||
cumulative_difficulty: combine_low_high_bits_to_u128(
|
||||
|
@ -182,3 +182,101 @@ pub fn get_alt_block_extended_header_from_height(
|
|||
long_term_weight: block_info.long_term_weight,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::num::NonZero;
|
||||
use cuprate_database::{Env, EnvInner, TxRw};
|
||||
use cuprate_test_utils::data::{BLOCK_V1_TX2, BLOCK_V9_TX3, BLOCK_V16_TX0};
|
||||
use cuprate_types::ChainId;
|
||||
use crate::ops::alt_block::{add_alt_block, flush_alt_blocks, get_alt_block, get_alt_extended_headers_in_range};
|
||||
use crate::ops::block::{add_block, pop_block};
|
||||
use crate::tables::OpenTables;
|
||||
use crate::tests::{assert_all_tables_are_empty, map_verified_block_to_alt, tmp_concrete_env};
|
||||
use crate::types::AltBlockHeight;
|
||||
|
||||
#[test]
|
||||
fn all_alt_blocks() {
|
||||
let (env, _tmp) = tmp_concrete_env();
|
||||
let env_inner = env.env_inner();
|
||||
assert_all_tables_are_empty(&env);
|
||||
|
||||
let chain_id = ChainId(NonZero::new(1).unwrap()).into();
|
||||
|
||||
// Add initial block.
|
||||
{
|
||||
let tx_rw = env_inner.tx_rw().unwrap();
|
||||
let mut tables = env_inner.open_tables_mut(&tx_rw).unwrap();
|
||||
|
||||
let mut initial_block = BLOCK_V1_TX2.clone();
|
||||
initial_block.height = 0;
|
||||
|
||||
add_block(&initial_block, &mut tables).unwrap();
|
||||
|
||||
drop(tables);
|
||||
TxRw::commit(tx_rw).unwrap();
|
||||
}
|
||||
|
||||
let alt_blocks = [
|
||||
map_verified_block_to_alt(BLOCK_V9_TX3.clone(), chain_id),
|
||||
map_verified_block_to_alt(BLOCK_V16_TX0.clone(), chain_id),
|
||||
];
|
||||
|
||||
// Add alt-blocks
|
||||
{
|
||||
let tx_rw = env_inner.tx_rw().unwrap();
|
||||
let mut tables = env_inner.open_tables_mut(&tx_rw).unwrap();
|
||||
|
||||
let mut prev_hash = BLOCK_V1_TX2.block_hash;
|
||||
for (i, mut alt_block) in alt_blocks.into_iter().enumerate() {
|
||||
let height = i + 1;
|
||||
|
||||
alt_block.height = height;
|
||||
alt_block.block.header.previous = prev_hash;
|
||||
alt_block.block_blob = alt_block.block.serialize();
|
||||
|
||||
add_alt_block(&alt_block, &mut tables).unwrap();
|
||||
|
||||
let alt_height = AltBlockHeight {
|
||||
chain_id: chain_id.into(),
|
||||
height,
|
||||
};
|
||||
|
||||
let alt_block_2 = get_alt_block(&alt_height, &tables).unwrap();
|
||||
assert_eq!(alt_block.block, alt_block_2.block);
|
||||
|
||||
let headers = get_alt_extended_headers_in_range(0..(height + 1), chain_id, &tables).unwrap();
|
||||
assert_eq!(headers.len(), height);
|
||||
|
||||
let last_header = headers.last().unwrap();
|
||||
assert_eq!(last_header.timestamp, alt_block.block.header.timestamp);
|
||||
assert_eq!(last_header.block_weight, alt_block.weight);
|
||||
assert_eq!(last_header.long_term_weight, alt_block.long_term_weight);
|
||||
assert_eq!(last_header.cumulative_difficulty, alt_block.cumulative_difficulty);
|
||||
assert_eq!(last_header.version.as_u8(), alt_block.block.header.hardfork_version);
|
||||
assert_eq!(last_header.vote, alt_block.block.header.hardfork_signal);
|
||||
|
||||
prev_hash = alt_block.block_hash;
|
||||
}
|
||||
|
||||
drop(tables);
|
||||
TxRw::commit(tx_rw).unwrap();
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
let mut tx_rw = env_inner.tx_rw().unwrap();
|
||||
|
||||
flush_alt_blocks(&env_inner, &mut tx_rw).unwrap();
|
||||
|
||||
let mut tables = env_inner.open_tables_mut(&tx_rw).unwrap();
|
||||
pop_block(None, &mut tables).unwrap();
|
||||
|
||||
drop(tables);
|
||||
TxRw::commit(tx_rw).unwrap();
|
||||
}
|
||||
|
||||
assert_all_tables_are_empty(&env);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -45,7 +45,7 @@ pub fn get_alt_chain_history_ranges(
|
|||
|
||||
let start_height = max(range.start, chain_info.common_ancestor_height + 1);
|
||||
|
||||
ranges.push((chain_info.parent_chain.into(), start_height..i));
|
||||
ranges.push((Chain::Alt(current_chain_id.into()), start_height..i));
|
||||
i = chain_info.common_ancestor_height;
|
||||
|
||||
match chain_info.parent_chain.into() {
|
||||
|
|
|
@ -180,7 +180,7 @@ pub fn pop_block(
|
|||
block_blob,
|
||||
txs,
|
||||
block_hash: block_info.block_hash,
|
||||
pow_hash: [255; 32],
|
||||
pow_hash: [0; 32],
|
||||
height: block_height,
|
||||
weight: block_info.weight,
|
||||
long_term_weight: block_info.long_term_weight,
|
||||
|
|
|
@ -94,7 +94,7 @@
|
|||
//! // Read the data, assert it is correct.
|
||||
//! let tx_rw = env_inner.tx_rw()?;
|
||||
//! let mut tables = env_inner.open_tables_mut(&tx_rw)?;
|
||||
//! let (height, hash, serai_block) = pop_block(&mut tables)?;
|
||||
//! let (height, hash, serai_block) = pop_block(None, &mut tables)?;
|
||||
//!
|
||||
//! assert_eq!(height, 0);
|
||||
//! assert_eq!(serai_block, block.block);
|
||||
|
|
|
@ -98,7 +98,7 @@
|
|||
//!
|
||||
//! // Block write was OK.
|
||||
//! let response = response_channel.await?;
|
||||
//! assert_eq!(response, BlockchainResponse::WriteBlockOk);
|
||||
//! assert_eq!(response, BlockchainResponse::Ok);
|
||||
//!
|
||||
//! // Now, let's try getting the block hash
|
||||
//! // of the block we just wrote.
|
||||
|
|
|
@ -84,7 +84,7 @@ async fn test_template(
|
|||
let request = BlockchainWriteRequest::WriteBlock(block);
|
||||
let response_channel = writer.call(request);
|
||||
let response = response_channel.await.unwrap();
|
||||
assert_eq!(response, BlockchainResponse::WriteBlockOk);
|
||||
assert_eq!(response, BlockchainResponse::Ok);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------- Reset the transaction
|
||||
|
|
|
@ -10,6 +10,7 @@ use std::{borrow::Cow, fmt::Debug};
|
|||
use pretty_assertions::assert_eq;
|
||||
|
||||
use cuprate_database::{ConcreteEnv, DatabaseRo, Env, EnvInner};
|
||||
use cuprate_types::{AltBlockInformation, ChainId, VerifiedBlockInformation};
|
||||
|
||||
use crate::{
|
||||
config::ConfigBuilder,
|
||||
|
@ -88,3 +89,18 @@ pub(crate) fn assert_all_tables_are_empty(env: &ConcreteEnv) {
|
|||
assert!(tables.all_tables_empty().unwrap());
|
||||
assert_eq!(crate::ops::tx::get_num_tx(tables.tx_ids()).unwrap(), 0);
|
||||
}
|
||||
|
||||
pub(crate) fn map_verified_block_to_alt(verified_block: VerifiedBlockInformation, chain_id: ChainId) -> AltBlockInformation {
|
||||
AltBlockInformation {
|
||||
block: verified_block.block,
|
||||
block_blob: verified_block.block_blob,
|
||||
txs: verified_block.txs,
|
||||
block_hash: verified_block.block_hash,
|
||||
pow_hash: verified_block.pow_hash,
|
||||
height: verified_block.height,
|
||||
weight: verified_block.weight,
|
||||
long_term_weight: verified_block.long_term_weight,
|
||||
cumulative_difficulty: verified_block.cumulative_difficulty,
|
||||
chain_id,
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue