Skip to content
Closed
306 changes: 240 additions & 66 deletions integration-tests/tests/chain_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ mod chain_query_interface {

use super::*;

async fn create_test_manager_and_chain_index(
validator: &ValidatorKind,
async fn create_test_manager_and_chain_index_zebrad(
chain_cache: Option<std::path::PathBuf>,
enable_zaino: bool,
zaino_no_sync: bool,
Expand All @@ -77,7 +76,7 @@ mod chain_query_interface {
NodeBackedChainIndexSubscriber,
) {
let (test_manager, json_service) = create_test_manager_and_connector(
validator,
&ValidatorKind::Zebrad,
chain_cache.clone(),
enable_zaino,
zaino_no_sync,
Expand Down Expand Up @@ -149,7 +148,49 @@ mod chain_query_interface {
db_version: 1,
db_path,
db_size: None,
network: zebra_chain::parameters::Network::new_regtest(
network: network.clone(),
no_sync: false,
no_db: false,
};
let chain_index = NodeBackedChainIndex::new(
ValidatorConnector::State(chain_index::source::State {
read_state_service: state_service.read_state_service().clone(),
mempool_fetcher: json_service,
}),
config,
)
.await
.unwrap();
let index_reader = chain_index.subscriber();
tokio::time::sleep(Duration::from_secs(3)).await;

(test_manager, state_service, chain_index, index_reader)
}

async fn create_test_manager_and_chain_index_zcashd(
chain_cache: Option<std::path::PathBuf>,
enable_zaino: bool,
zaino_no_sync: bool,
zaino_no_db: bool,
enable_clients: bool,
) -> (
TestManager,
JsonRpSeeConnector,
NodeBackedChainIndex,
NodeBackedChainIndexSubscriber,
) {
let (test_manager, json_service) = create_test_manager_and_connector(
&ValidatorKind::Zcashd,
chain_cache.clone(),
enable_zaino,
zaino_no_sync,
zaino_no_db,
enable_clients,
)
.await;

let network = match test_manager.network.to_string().as_str() {
"Regtest" => zebra_chain::parameters::Network::new_regtest(
zebra_chain::parameters::testnet::ConfiguredActivationHeights {
before_overwinter: Some(1),
overwinter: Some(1),
Expand All @@ -159,41 +200,41 @@ mod chain_query_interface {
canopy: Some(1),
nu5: Some(1),
nu6: Some(1),
// see https://zips.z.cash/#nu6-1-candidate-zips for info on NU6.1
// TODO: What is network upgrade 6.1? What does a minor version NU mean?
nu6_1: None,
nu7: None,
},
),
"Testnet" => zebra_chain::parameters::Network::new_default_testnet(),
"Mainnet" => zebra_chain::parameters::Network::Mainnet,
_ => panic!("Incorrect newtork type found."),
};
let temp_dir: TempDir = tempfile::tempdir().unwrap();
let db_path: PathBuf = temp_dir.path().to_path_buf();
let config = BlockCacheConfig {
map_capacity: None,
map_shard_amount: None,
db_version: 1,
db_path,
db_size: None,
network: network.clone(),
no_sync: false,
no_db: false,
};
let chain_index = NodeBackedChainIndex::new(
ValidatorConnector::State(chain_index::source::State {
read_state_service: state_service.read_state_service().clone(),
mempool_fetcher: json_service,
}),
config,
)
.await
.unwrap();
let index_reader = chain_index.subscriber().await;
let chain_index =
NodeBackedChainIndex::new(ValidatorConnector::Fetch(json_service.clone()), config)
.await
.unwrap();
let index_reader = chain_index.subscriber();
tokio::time::sleep(Duration::from_secs(3)).await;

(test_manager, state_service, chain_index, index_reader)
(test_manager, json_service, chain_index, index_reader)
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn get_block_range() {
let (test_manager, _json_service, _chain_index, indexer) =
create_test_manager_and_chain_index(
&ValidatorKind::Zebrad,
None,
true,
false,
false,
true,
)
.await;
async fn get_block_range_zebrad() {
let (test_manager, _state_service, _chain_index, indexer) =
create_test_manager_and_chain_index_zebrad(None, true, false, false, true).await;

// this delay had to increase. Maybe we tweak sync loop rerun time?
test_manager.generate_blocks_with_delay(5).await;
Expand Down Expand Up @@ -224,17 +265,42 @@ mod chain_query_interface {
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn find_fork_point() {
async fn get_block_range_zcashd() {
let (test_manager, _json_service, _chain_index, indexer) =
create_test_manager_and_chain_index(
&ValidatorKind::Zebrad,
None,
true,
false,
false,
true,
)
.await;
create_test_manager_and_chain_index_zcashd(None, true, false, false, true).await;

// this delay had to increase. Maybe we tweak sync loop rerun time?
test_manager.generate_blocks_with_delay(5).await;
let snapshot = indexer.snapshot_nonfinalized_state();
assert_eq!(snapshot.as_ref().blocks.len(), 7);
let range = indexer
.get_block_range(&snapshot, Height::try_from(0).unwrap(), None)
.unwrap()
.try_collect::<Vec<_>>()
.await
.unwrap();
for block in range {
let block = block
.zcash_deserialize_into::<zebra_chain::block::Block>()
.unwrap();
assert_eq!(
block.hash().0,
snapshot
.heights_to_hashes
.get(
&chain_index::types::Height::try_from(block.coinbase_height().unwrap())
.unwrap()
)
.unwrap()
.0
);
}
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn find_fork_point_zebrad() {
let (test_manager, _state_service, _chain_index, indexer) =
create_test_manager_and_chain_index_zebrad(None, true, false, false, true).await;

// this delay had to increase. Maybe we tweak sync loop rerun time?
test_manager.generate_blocks_with_delay(5).await;
Expand All @@ -255,17 +321,32 @@ mod chain_query_interface {
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn get_raw_transaction() {
async fn find_fork_point_zchashd() {
let (test_manager, _json_service, _chain_index, indexer) =
create_test_manager_and_chain_index(
&ValidatorKind::Zebrad,
None,
true,
false,
false,
true,
create_test_manager_and_chain_index_zcashd(None, true, false, false, true).await;

// this delay had to increase. Maybe we tweak sync loop rerun time?
test_manager.generate_blocks_with_delay(5).await;
let snapshot = indexer.snapshot_nonfinalized_state();
assert_eq!(snapshot.as_ref().blocks.len(), 7);
for block_hash in snapshot.heights_to_hashes.values() {
// As all blocks are currently on the main chain,
// this should be the block provided
assert_eq!(
block_hash,
&indexer
.find_fork_point(&snapshot, block_hash)
.unwrap()
.unwrap()
.0
)
.await;
}
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn get_raw_transaction_zebrad() {
let (test_manager, _state_service, _chain_index, indexer) =
create_test_manager_and_chain_index_zebrad(None, true, false, false, true).await;

// this delay had to increase. Maybe we tweak sync loop rerun time?
test_manager.generate_blocks_with_delay(5).await;
Expand All @@ -292,17 +373,38 @@ mod chain_query_interface {
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn get_transaction_status() {
async fn get_raw_transaction_zcashd() {
let (test_manager, _json_service, _chain_index, indexer) =
create_test_manager_and_chain_index(
&ValidatorKind::Zebrad,
None,
true,
false,
false,
true,
)
.await;
create_test_manager_and_chain_index_zcashd(None, true, false, false, true).await;

// this delay had to increase. Maybe we tweak sync loop rerun time?
test_manager.generate_blocks_with_delay(5).await;
let snapshot = indexer.snapshot_nonfinalized_state();
assert_eq!(snapshot.as_ref().blocks.len(), 7);
for txid in snapshot
.blocks
.values()
.flat_map(|block| block.transactions().iter().map(|txdata| txdata.txid().0))
{
let raw_transaction = indexer
.get_raw_transaction(&snapshot, &TransactionHash(txid))
.await
.unwrap()
.unwrap();
let zebra_txn =
zebra_chain::transaction::Transaction::zcash_deserialize(&raw_transaction[..])
.unwrap();

let correct_txid = zebra_txn.hash().0;

assert_eq!(txid, correct_txid);
}
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn get_transaction_status_zebrad() {
let (test_manager, _state_service, _chain_index, indexer) =
create_test_manager_and_chain_index_zebrad(None, true, false, false, true).await;
let snapshot = indexer.snapshot_nonfinalized_state();
// I don't know where this second block is generated. Somewhere in the
// guts of create_test_manager_and_chain_index
Expand All @@ -329,17 +431,38 @@ mod chain_query_interface {
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn sync_large_chain() {
async fn get_transaction_status_zcashd() {
let (test_manager, _json_service, _chain_index, indexer) =
create_test_manager_and_chain_index_zcashd(None, true, false, false, true).await;
let snapshot = indexer.snapshot_nonfinalized_state();
// I don't know where this second block is generated. Somewhere in the
// guts of create_test_manager_and_chain_index
assert_eq!(snapshot.as_ref().blocks.len(), 2);

// this delay had to increase. Maybe we tweak sync loop rerun time?
test_manager.generate_blocks_with_delay(5).await;
let snapshot = indexer.snapshot_nonfinalized_state();
assert_eq!(snapshot.as_ref().blocks.len(), 7);
for (txid, height, block_hash) in snapshot.blocks.values().flat_map(|block| {
block
.transactions()
.iter()
.map(|txdata| (txdata.txid().0, block.height(), block.hash()))
}) {
let (transaction_status_blocks, _transaction_mempool_status) = indexer
.get_transaction_status(&snapshot, &TransactionHash(txid))
.await
.unwrap();
assert_eq!(1, transaction_status_blocks.len());
assert_eq!(transaction_status_blocks.keys().next().unwrap(), block_hash);
assert_eq!(transaction_status_blocks.values().next().unwrap(), &height)
}
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn sync_large_chain_zebrad() {
let (test_manager, state_service, _chain_index, indexer) =
create_test_manager_and_chain_index(
&ValidatorKind::Zebrad,
None,
true,
false,
false,
true,
)
.await;
create_test_manager_and_chain_index_zebrad(None, true, false, false, true).await;

// this delay had to increase. Maybe we tweak sync loop rerun time?
test_manager.generate_blocks_with_delay(5).await;
Expand Down Expand Up @@ -388,4 +511,55 @@ mod chain_query_interface {
.unwrap();
}
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn sync_large_chain_zcashd() {
let (test_manager, json_service, _chain_index, indexer) =
create_test_manager_and_chain_index_zcashd(None, true, false, false, true).await;

// this delay had to increase. Maybe we tweak sync loop rerun time?
test_manager.generate_blocks_with_delay(5).await;
{
let chain_height = json_service.get_blockchain_info().await.unwrap().blocks.0;
let indexer_height = indexer.snapshot_nonfinalized_state().best_tip.0;
assert_eq!(Height::try_from(chain_height).unwrap(), indexer_height);
}

test_manager.generate_blocks_with_delay(150).await;

tokio::time::sleep(std::time::Duration::from_millis(5000)).await;

let snapshot = indexer.snapshot_nonfinalized_state();
let chain_height = json_service.get_blockchain_info().await.unwrap().blocks.0;
let indexer_height = snapshot.best_tip.0;
assert_eq!(Height::try_from(chain_height).unwrap(), indexer_height);

let finalised_start = Height::try_from(chain_height - 150).unwrap();
let finalised_tip = Height::try_from(chain_height - 100).unwrap();
let end = Height::try_from(chain_height - 50).unwrap();

let finalized_blocks = indexer
.get_block_range(&snapshot, finalised_start, Some(finalised_tip))
.unwrap()
.try_collect::<Vec<_>>()
.await
.unwrap();
for block in finalized_blocks {
block
.zcash_deserialize_into::<zebra_chain::block::Block>()
.unwrap();
}

let non_finalised_blocks = indexer
.get_block_range(&snapshot, finalised_tip, Some(end))
.unwrap()
.try_collect::<Vec<_>>()
.await
.unwrap();
for block in non_finalised_blocks {
block
.zcash_deserialize_into::<zebra_chain::block::Block>()
.unwrap();
}
}
}
Loading