changed functions in zebra-state::service::finalized_state to accept &Network

This commit is contained in:
idky137 2024-03-15 16:39:37 +00:00
parent 69066346fc
commit 313598b4b7
No known key found for this signature in database
26 changed files with 72 additions and 66 deletions

View File

@ -10,7 +10,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let (config, network) = Default::default();
let (scan_service, _cmd_receiver) =
ScanService::new_with_mock_scanner(Storage::new(&config, network, false));
ScanService::new_with_mock_scanner(Storage::new(&config, &network, false));
let scan_service = ServiceBuilder::new().buffer(10).service(scan_service);
// Start the gRPC server.

View File

@ -21,7 +21,7 @@ pub const SCAN_SERVICE_TIMEOUT: Duration = Duration::from_secs(30);
pub async fn init_with_server(
listen_addr: SocketAddr,
config: Config,
network: Network,
network: &Network,
state: scan::State,
chain_tip_change: ChainTipChange,
) -> Result<(), Report> {
@ -43,7 +43,7 @@ pub async fn init_with_server(
/// Initialize the scanner and its gRPC server based on its config, and spawn a task for it.
pub fn spawn_init(
config: Config,
network: Network,
network: &Network,
state: scan::State,
chain_tip_change: ChainTipChange,
) -> JoinHandle<Result<(), Report>> {

View File

@ -41,7 +41,7 @@ impl ScanService {
/// Create a new [`ScanService`].
pub async fn new(
config: &Config,
network: Network,
network: &Network,
state: scan::State,
chain_tip_change: ChainTipChange,
) -> Self {

View File

@ -23,7 +23,7 @@ use crate::{
/// Tests that keys are deleted correctly
#[tokio::test]
pub async fn scan_service_deletes_keys_correctly() -> Result<()> {
let mut db = new_test_storage(Network::Mainnet);
let mut db = new_test_storage(&Network::Mainnet);
let zec_pages_sapling_efvk = ZECPAGES_SAPLING_VIEWING_KEY.to_string();
@ -86,7 +86,7 @@ pub async fn scan_service_deletes_keys_correctly() -> Result<()> {
/// Tests that keys are deleted correctly
#[tokio::test]
pub async fn scan_service_subscribes_to_results_correctly() -> Result<()> {
let db = new_test_storage(Network::Mainnet);
let db = new_test_storage(&Network::Mainnet);
let (mut scan_service, mut cmd_receiver) = ScanService::new_with_mock_scanner(db);
@ -133,7 +133,7 @@ pub async fn scan_service_subscribes_to_results_correctly() -> Result<()> {
/// Tests that results are cleared are deleted correctly
#[tokio::test]
pub async fn scan_service_clears_results_correctly() -> Result<()> {
let mut db = new_test_storage(Network::Mainnet);
let mut db = new_test_storage(&Network::Mainnet);
let zec_pages_sapling_efvk = ZECPAGES_SAPLING_VIEWING_KEY.to_string();
@ -188,7 +188,7 @@ pub async fn scan_service_clears_results_correctly() -> Result<()> {
/// Tests that results for key are returned correctly
#[tokio::test]
pub async fn scan_service_get_results_for_key_correctly() -> Result<()> {
let mut db = new_test_storage(Network::Mainnet);
let mut db = new_test_storage(&Network::Mainnet);
let zec_pages_sapling_efvk = ZECPAGES_SAPLING_VIEWING_KEY.to_string();
@ -265,13 +265,13 @@ pub async fn scan_service_get_results_for_key_correctly() -> Result<()> {
#[tokio::test]
pub async fn scan_service_registers_keys_correctly() -> Result<()> {
for network in Network::iter() {
scan_service_registers_keys_correctly_for(network).await?;
scan_service_registers_keys_correctly_for(&network).await?;
}
Ok(())
}
async fn scan_service_registers_keys_correctly_for(network: Network) -> Result<()> {
async fn scan_service_registers_keys_correctly_for(network: &Network) -> Result<()> {
// Mock the state.
let (state, _, _, chain_tip_change) = zebra_state::populated_state(vec![], network).await;
@ -357,7 +357,7 @@ async fn scan_service_registers_keys_correctly_for(network: Network) -> Result<(
/// Test that the scan service with a timeout layer returns timeout errors after expected timeout
#[tokio::test]
async fn scan_service_timeout() -> Result<()> {
let db = new_test_storage(Network::Mainnet);
let db = new_test_storage(&Network::Mainnet);
let (scan_service, _cmd_receiver) = ScanService::new_with_mock_scanner(db);
let mut scan_service = ServiceBuilder::new()

View File

@ -52,7 +52,7 @@ impl Storage {
///
/// This method can block while creating or reading database files, so it must be inside
/// spawn_blocking() in async code.
pub fn new(config: &Config, network: Network, read_only: bool) -> Self {
pub fn new(config: &Config, network: &Network, read_only: bool) -> Self {
let mut storage = Self::new_db(config, network, read_only);
for (sapling_key, birthday) in config.sapling_keys_to_scan.iter() {

View File

@ -50,7 +50,7 @@ impl Storage {
/// New keys in `config` are not inserted into the database.
pub(crate) fn new_db(config: &Config, network: Network, read_only: bool) -> Self {
Self::new_with_debug(
config, network,
config, &network,
// TODO: make format upgrades work with any database, then change debug_skip_format_upgrades to `false`
true, read_only,
)
@ -64,7 +64,7 @@ impl Storage {
/// This method is intended for use in tests.
pub(crate) fn new_with_debug(
config: &Config,
network: Network,
network: &Network,
debug_skip_format_upgrades: bool,
read_only: bool,
) -> Self {

View File

@ -23,7 +23,7 @@ mod snapshot;
mod vectors;
/// Returns an empty `Storage` suitable for testing.
pub fn new_test_storage(network: Network) -> Storage {
pub fn new_test_storage(network: &Network) -> Storage {
Storage::new(&Config::ephemeral(), network, false)
}

View File

@ -56,7 +56,7 @@ fn test_database_format_with_network(network: Network) {
let mut net_suffix = network.to_string();
net_suffix.make_ascii_lowercase();
let mut storage = super::new_test_storage(network);
let mut storage = super::new_test_storage(&network);
// Snapshot the column family names
let mut cf_names = storage.db.list_cf().expect("empty database is valid");

View File

@ -11,7 +11,7 @@ use crate::{
/// Tests that keys are deleted correctly
#[test]
pub fn deletes_keys_and_results_correctly() {
let mut db = new_test_storage(Network::Mainnet);
let mut db = new_test_storage(&Network::Mainnet);
let zec_pages_sapling_efvk = ZECPAGES_SAPLING_VIEWING_KEY.to_string();
@ -69,7 +69,7 @@ pub fn deletes_keys_and_results_correctly() {
/// Tests that keys are deleted correctly
#[test]
pub fn clears_results_correctly() {
let mut db = new_test_storage(Network::Mainnet);
let mut db = new_test_storage(&Network::Mainnet);
let zec_pages_sapling_efvk = ZECPAGES_SAPLING_VIEWING_KEY.to_string();

View File

@ -156,7 +156,7 @@ fn scanning_fake_blocks_store_key_and_results() -> Result<()> {
let key_to_be_stored = encode_extended_full_viewing_key("zxviews", &efvk);
// Create a database
let mut storage = new_test_storage(network);
let mut storage = new_test_storage(&network);
// Insert the generated key to the database
storage.add_sapling_key(&key_to_be_stored, None);

View File

@ -131,7 +131,7 @@ impl Config {
&self,
db_kind: impl AsRef<str>,
major_version: u64,
network: Network,
network: &Network,
) -> PathBuf {
let db_kind = db_kind.as_ref();
let major_version = format!("v{}", major_version);
@ -153,7 +153,7 @@ impl Config {
&self,
db_kind: impl AsRef<str>,
major_version: u64,
network: Network,
network: &Network,
) -> PathBuf {
let mut version_path = self.db_path(db_kind, major_version, network);
@ -200,7 +200,7 @@ impl Default for Config {
/// and deletes them from the filesystem.
///
/// See `check_and_delete_old_databases()` for details.
pub fn check_and_delete_old_state_databases(config: &Config, network: Network) -> JoinHandle<()> {
pub fn check_and_delete_old_state_databases(config: &Config, network: &Network) -> JoinHandle<()> {
check_and_delete_old_databases(
config,
STATE_DATABASE_KIND,
@ -229,7 +229,7 @@ pub fn check_and_delete_old_databases(
config: &Config,
db_kind: impl AsRef<str>,
major_version: u64,
network: Network,
network: &Network,
) -> JoinHandle<()> {
let current_span = Span::current();
let config = config.clone();
@ -246,7 +246,7 @@ pub fn check_and_delete_old_databases(
/// Check if there are old database folders and delete them from the filesystem.
///
/// See [`check_and_delete_old_databases`] for details.
fn delete_old_databases(config: Config, db_kind: String, major_version: u64, network: Network) {
fn delete_old_databases(config: Config, db_kind: String, major_version: u64, network: &Network) {
if config.ephemeral || !config.delete_old_database {
return;
}
@ -371,7 +371,7 @@ fn parse_major_version(dir_name: &str) -> Option<u64> {
/// Returns the full semantic version of the on-disk state database, based on its config and network.
pub fn state_database_format_version_on_disk(
config: &Config,
network: Network,
network: &Network,
) -> Result<Option<Version>, BoxError> {
database_format_version_on_disk(
config,
@ -398,7 +398,7 @@ pub fn database_format_version_on_disk(
config: &Config,
db_kind: impl AsRef<str>,
major_version: u64,
network: Network,
network: &Network,
) -> Result<Option<Version>, BoxError> {
let version_path = config.version_file_path(&db_kind, major_version, network);
let db_path = config.db_path(db_kind, major_version, network);
@ -470,7 +470,7 @@ pub(crate) mod hidden {
pub fn write_state_database_format_version_to_disk(
config: &Config,
changed_version: &Version,
network: Network,
network: &Network,
) -> Result<(), BoxError> {
write_database_format_version_to_disk(config, STATE_DATABASE_KIND, changed_version, network)
}
@ -496,7 +496,7 @@ pub(crate) mod hidden {
config: &Config,
db_kind: impl AsRef<str>,
changed_version: &Version,
network: Network,
network: &Network,
) -> Result<(), BoxError> {
let version_path = config.version_file_path(db_kind, changed_version.major, network);

View File

@ -310,7 +310,7 @@ impl StateService {
/// and read-only watch channels for its best chain tip.
pub fn new(
config: Config,
network: Network,
network: &Network,
max_checkpoint_height: block::Height,
checkpoint_verify_concurrency_limit: usize,
) -> (Self, ReadStateService, LatestChainTip, ChainTipChange) {
@ -1882,7 +1882,7 @@ impl Service<ReadRequest> for ReadStateService {
/// probably not what you want.
pub fn init(
config: Config,
network: Network,
network: &Network,
max_checkpoint_height: block::Height,
checkpoint_verify_concurrency_limit: usize,
) -> (
@ -1912,7 +1912,7 @@ pub fn init(
/// a read state service, and receivers for state chain tip updates.
pub fn spawn_init(
config: Config,
network: Network,
network: &Network,
max_checkpoint_height: block::Height,
checkpoint_verify_concurrency_limit: usize,
) -> tokio::task::JoinHandle<(

View File

@ -141,7 +141,7 @@ impl FinalizedState {
/// If there is no existing database, creates a new database on disk.
pub fn new(
config: &Config,
network: Network,
network: &Network,
#[cfg(feature = "elasticsearch")] elastic_db: Option<elasticsearch::Elasticsearch>,
) -> Self {
Self::new_with_debug(
@ -160,7 +160,7 @@ impl FinalizedState {
/// This method is intended for use in tests.
pub(crate) fn new_with_debug(
config: &Config,
network: Network,
network: &Network,
debug_skip_format_upgrades: bool,
#[cfg(feature = "elasticsearch")] elastic_db: Option<elasticsearch::Elasticsearch>,
read_only: bool,

View File

@ -727,7 +727,7 @@ impl DiskDb {
config: &Config,
db_kind: impl AsRef<str>,
format_version_in_code: &Version,
network: Network,
network: &Network,
column_families_in_code: impl IntoIterator<Item = String>,
read_only: bool,
) -> DiskDb {
@ -765,7 +765,7 @@ impl DiskDb {
let db = DiskDb {
db_kind: db_kind.to_string(),
format_version_in_code: format_version_in_code.clone(),
network,
network: network.clone(),
ephemeral: config.ephemeral,
db: Arc::new(db),
};

View File

@ -64,7 +64,7 @@ fn test_raw_rocksdb_column_families_with_network(network: Network) {
let mut state = FinalizedState::new(
&Config::ephemeral(),
network,
&network,
#[cfg(feature = "elasticsearch")]
None,
);

View File

@ -24,7 +24,7 @@ fn blocks_with_v5_transactions() -> Result<()> {
.and_then(|v| v.parse().ok())
.unwrap_or(DEFAULT_PARTIAL_CHAIN_PROPTEST_CASES)),
|((chain, count, network, _history_tree) in PreparedChain::default())| {
let mut state = FinalizedState::new(&Config::ephemeral(), network, #[cfg(feature = "elasticsearch")] None);
let mut state = FinalizedState::new(&Config::ephemeral(), &network, #[cfg(feature = "elasticsearch")] None);
let mut height = Height(0);
// use `count` to minimize test failures, so they are easier to diagnose
for block in chain.iter().take(count) {
@ -65,7 +65,7 @@ fn all_upgrades_and_wrong_commitments_with_fake_activation_heights() -> Result<(
.unwrap_or(DEFAULT_PARTIAL_CHAIN_PROPTEST_CASES)),
|((chain, _count, network, _history_tree) in PreparedChain::default().with_valid_commitments().no_shrink())| {
let mut state = FinalizedState::new(&Config::ephemeral(), network, #[cfg(feature = "elasticsearch")] None);
let mut state = FinalizedState::new(&Config::ephemeral(), &network, #[cfg(feature = "elasticsearch")] None);
let mut height = Height(0);
let heartwood_height = NetworkUpgrade::Heartwood.activation_height(&network).unwrap();
let heartwood_height_plus1 = (heartwood_height + 1).unwrap();

View File

@ -93,7 +93,7 @@ impl ZebraDb {
config: &Config,
db_kind: impl AsRef<str>,
format_version_in_code: &Version,
network: Network,
network: &Network,
debug_skip_format_upgrades: bool,
column_families_in_code: impl IntoIterator<Item = String>,
read_only: bool,
@ -194,7 +194,7 @@ impl ZebraDb {
self.config(),
self.db_kind(),
self.major_version(),
self.network(),
&self.network(),
)
}
@ -209,13 +209,13 @@ impl ZebraDb {
self.config(),
self.db_kind(),
new_version,
self.network(),
&self.network(),
)
}
/// Returns the configured network for this database.
pub fn network(&self) -> Network {
self.db.network()
self.db.network().clone()
}
/// Returns the `Path` where the files used by this database are located.
@ -271,7 +271,7 @@ impl ZebraDb {
&self.config,
self.db_kind(),
self.major_version(),
self.network(),
&self.network(),
)
.expect("unexpected invalid or unreadable database version file");

View File

@ -167,7 +167,7 @@ fn test_block_and_transaction_data_with_network(network: Network) {
let mut state = FinalizedState::new(
&Config::ephemeral(),
network,
&network,
#[cfg(feature = "elasticsearch")]
None,
);

View File

@ -42,14 +42,14 @@ fn test_block_db_round_trip() {
.iter()
.map(|(_height, block)| block.zcash_deserialize_into().unwrap());
test_block_db_round_trip_with(Mainnet, mainnet_test_cases);
test_block_db_round_trip_with(Testnet, testnet_test_cases);
test_block_db_round_trip_with(&Mainnet, mainnet_test_cases);
test_block_db_round_trip_with(&Testnet, testnet_test_cases);
// It doesn't matter if these blocks are mainnet or testnet,
// because there is no validation at this level of the database.
//
// These blocks have the same height and header hash, so they each need a new state.
test_block_db_round_trip_with(Mainnet, iter::once(large_multi_transaction_block()));
test_block_db_round_trip_with(&Mainnet, iter::once(large_multi_transaction_block()));
// These blocks are unstable under serialization, so we apply a round-trip first.
//
@ -61,7 +61,7 @@ fn test_block_db_round_trip() {
let block: Block = block_data
.zcash_deserialize_into()
.expect("deserialization of valid serialized block never fails");
test_block_db_round_trip_with(Mainnet, iter::once(block));
test_block_db_round_trip_with(&Mainnet, iter::once(block));
let block = large_single_transaction_block_many_outputs();
let block_data = block
@ -70,11 +70,11 @@ fn test_block_db_round_trip() {
let block: Block = block_data
.zcash_deserialize_into()
.expect("deserialization of valid serialized block never fails");
test_block_db_round_trip_with(Mainnet, iter::once(block));
test_block_db_round_trip_with(&Mainnet, iter::once(block));
}
fn test_block_db_round_trip_with(
network: Network,
network: &Network,
block_test_cases: impl IntoIterator<Item = Block>,
) {
let _init_guard = zebra_test::init();

View File

@ -373,7 +373,7 @@ impl Chain {
/// Returns the [`Network`] for this chain.
pub fn network(&self) -> Network {
self.network
self.network.clone()
}
/// Returns the [`ContextuallyVerifiedBlock`] with [`block::Hash`] or
@ -1270,7 +1270,6 @@ impl Chain {
) -> impl Iterator<Item = &TransparentTransfers> {
addresses
.iter()
.copied()
.flat_map(|address| self.partial_transparent_transfers.get(&address))
}

View File

@ -478,7 +478,7 @@ fn rejection_restores_internal_state_genesis() -> Result<()> {
}
))| {
let mut state = NonFinalizedState::new(network);
let finalized_state = FinalizedState::new(&Config::ephemeral(), network, #[cfg(feature = "elasticsearch")] None);
let finalized_state = FinalizedState::new(&Config::ephemeral(), &network, #[cfg(feature = "elasticsearch")] None);
let fake_value_pool = ValueBalance::<NonNegative>::fake_populated_pool();
finalized_state.set_finalized_value_pool(fake_value_pool);

View File

@ -372,7 +372,7 @@ fn new_ephemeral_db() -> ZebraDb {
&Config::ephemeral(),
STATE_DATABASE_KIND,
&state_database_format_version_in_code(),
Mainnet,
&Mainnet,
true,
STATE_COLUMN_FAMILIES_IN_CODE
.iter()

View File

@ -77,7 +77,10 @@ pub(crate) fn partial_nu5_chain_strategy(
false,
)
})
.prop_map(move |partial_chain| (network, nu_activation, partial_chain))
.prop_map(move |partial_chain| {
let network_clone = network.clone();
(network_clone, nu_activation, partial_chain)
})
})
}
@ -94,7 +97,7 @@ pub(crate) fn new_state_with_mainnet_genesis(
let mut finalized_state = FinalizedState::new_with_debug(
&config,
network,
&network,
// The tests that use this setup function also commit invalid blocks to the state.
true,
#[cfg(feature = "elasticsearch")]

View File

@ -263,7 +263,7 @@ impl Application for ZebradApp {
// reads state disk version file, doesn't open RocksDB database
let disk_db_version =
match state_database_format_version_on_disk(&config.state, config.network.network) {
match state_database_format_version_on_disk(&config.state, &config.network.network) {
Ok(Some(version)) => version.to_string(),
// This "version" is specially formatted to match a relaxed version regex in CI
Ok(None) => "creating.new.database".to_string(),

View File

@ -124,7 +124,7 @@ impl StartCmd {
let (state_service, read_only_state_service, latest_chain_tip, chain_tip_change) =
zebra_state::spawn_init(
config.state.clone(),
config.network.network,
&config.network.network,
max_checkpoint_height,
config.sync.checkpoint_verify_concurrency_limit
* (VERIFICATION_PIPELINE_SCALING_MULTIPLIER + 1),
@ -261,7 +261,7 @@ impl StartCmd {
info!("spawning delete old databases task");
let mut old_databases_task_handle = zebra_state::check_and_delete_old_state_databases(
&config.state,
config.network.network,
&config.network.network,
);
info!("spawning progress logging task");
@ -306,7 +306,7 @@ impl StartCmd {
info!("spawning shielded scanner with configured viewing keys");
zebra_scan::spawn_init(
config.shielded_scan.clone(),
config.network.network,
&config.network.network,
state,
chain_tip_change,
)

View File

@ -384,8 +384,12 @@ async fn db_init_outside_future_executor() -> Result<()> {
let start = Instant::now();
// This test doesn't need UTXOs to be verified efficiently, because it uses an empty state.
let db_init_handle =
zebra_state::spawn_init(config.state.clone(), config.network.network, Height::MAX, 0);
let db_init_handle = zebra_state::spawn_init(
config.state.clone(),
&config.network.network,
Height::MAX,
0,
);
// it's faster to panic if it takes longer than expected, since the executor
// will wait indefinitely for blocking operation to finish once started
@ -2530,7 +2534,7 @@ async fn generate_checkpoints_testnet() -> Result<()> {
#[tokio::test]
async fn new_state_format() -> Result<()> {
for network in [Mainnet, Testnet] {
state_format_test("new_state_format_test", network, 2, None).await?;
state_format_test("new_state_format_test", &network, 2, None).await?;
}
Ok(())
@ -2548,7 +2552,7 @@ async fn update_state_format() -> Result<()> {
fake_version.patch = 0;
for network in [Mainnet, Testnet] {
state_format_test("update_state_format_test", network, 3, Some(&fake_version)).await?;
state_format_test("update_state_format_test", &network, 3, Some(&fake_version)).await?;
}
Ok(())
@ -2567,7 +2571,7 @@ async fn downgrade_state_format() -> Result<()> {
for network in [Mainnet, Testnet] {
state_format_test(
"downgrade_state_format_test",
network,
&network,
3,
Some(&fake_version),
)
@ -2580,7 +2584,7 @@ async fn downgrade_state_format() -> Result<()> {
/// Test state format changes, see calling tests for details.
async fn state_format_test(
base_test_name: &str,
network: Network,
network: &Network,
reopen_count: usize,
fake_version: Option<&Version>,
) -> Result<()> {