From 894ef34fc25e308c2bfe12fc5467bfa5e19ad5f9 Mon Sep 17 00:00:00 2001 From: Vladimir Motylenko Date: Mon, 13 Dec 2021 13:29:50 +0200 Subject: [PATCH] Feat(evm-archive): Add possibility to save current state into archive at startup. --- core/src/validator.rs | 3 ++- core/tests/snapshots.rs | 1 + ledger-tool/src/main.rs | 1 + ledger/src/bank_forks_utils.rs | 2 ++ runtime/src/serde_snapshot.rs | 29 ++++++++++++++++++++++++++++- runtime/src/serde_snapshot/tests.rs | 1 + runtime/src/snapshot_utils.rs | 4 ++++ 7 files changed, 39 insertions(+), 2 deletions(-) diff --git a/core/src/validator.rs b/core/src/validator.rs index 893a036d6b..f971df8873 100644 --- a/core/src/validator.rs +++ b/core/src/validator.rs @@ -1162,7 +1162,7 @@ fn new_banks_from_ledger( blockstore.clone(), exit, config.rpc_config.enable_cpi_and_log_storage, - evm_archive, + evm_archive.clone(), ) } else { TransactionHistoryServices::default() @@ -1188,6 +1188,7 @@ fn new_banks_from_ledger( .cache_block_meta_sender .as_ref(), config.verify_evm_state, + evm_archive, ) .unwrap_or_else(|err| { error!("Failed to load ledger: {:?}", err); diff --git a/core/tests/snapshots.rs b/core/tests/snapshots.rs index 7350087ae5..f6e7f44bc7 100644 --- a/core/tests/snapshots.rs +++ b/core/tests/snapshots.rs @@ -178,6 +178,7 @@ mod tests { AccountSecondaryIndexes::default(), false, false, + None, ) .unwrap(); diff --git a/ledger-tool/src/main.rs b/ledger-tool/src/main.rs index be0e43d6ec..0ce7676224 100644 --- a/ledger-tool/src/main.rs +++ b/ledger-tool/src/main.rs @@ -733,6 +733,7 @@ fn load_bank_forks( None, None, verify_evm_state, + None, ) } diff --git a/ledger/src/bank_forks_utils.rs b/ledger/src/bank_forks_utils.rs index 57fff591d2..a8ad52fe54 100644 --- a/ledger/src/bank_forks_utils.rs +++ b/ledger/src/bank_forks_utils.rs @@ -42,6 +42,7 @@ pub fn load( transaction_status_sender: Option<&TransactionStatusSender>, cache_block_meta_sender: Option<&CacheBlockMetaSender>, verify_evm_state: bool, + evm_archive: Option, ) -> LoadResult { if let Some(snapshot_config) = snapshot_config.as_ref() { info!( @@ -77,6 +78,7 @@ pub fn load( process_options.account_indexes.clone(), process_options.accounts_db_caching_enabled, verify_evm_state, + evm_archive, ) .expect("Load from snapshot failed"); if let Some(shrink_paths) = shrink_paths { diff --git a/runtime/src/serde_snapshot.rs b/runtime/src/serde_snapshot.rs index be394d7353..db54c16904 100644 --- a/runtime/src/serde_snapshot.rs +++ b/runtime/src/serde_snapshot.rs @@ -146,6 +146,7 @@ pub(crate) fn bank_from_stream( caching_enabled: bool, evm_state_backup_path: &Path, skip_purge_verify: bool, + evm_archive: Option, ) -> std::result::Result where R: Read, @@ -167,6 +168,7 @@ where evm_state_version.support_gc(), skip_purge_verify, true, // enable gc + evm_archive, ) .map_err(|err| { warn!("bankrc_from_stream error: {:?}", err); @@ -257,6 +259,7 @@ fn reconstruct_bank_from_fields( skip_purge_verify: bool, // true if gc should be enabled enable_gc: bool, + evm_archive: Option, ) -> Result where E: SerializableStorage, @@ -309,9 +312,20 @@ where .map_err(|e| Error::custom(format!("Unable to open destination evm-state {}", e)))?; let mut measure = Measure::start("EVM snapshot purging"); + + // vars to save temp arrays for copy_and_purge + let (archive_dest, regular_dest); + let destination: &[_] = if let Some(evm_archive) = evm_archive { + info!("Copying current evm state to archive during evm purging."); + archive_dest = [destination, evm_archive]; + &archive_dest + } else { + regular_dest = [destination]; + ®ular_dest + }; evm_state::storage::copy_and_purge( src, - &[destination], // TODO: Add archive storage + &destination, bank_fields.evm_persist_feilds.last_root(), ) .map_err(|e| Error::custom(format!("Unable to copy_and_purge storage {}", e)))?; @@ -323,6 +337,19 @@ where .map_err(|e| Error::custom(format!("Unable to restore evm backup storage {}", e)))?; measure.stop(); info!("{}", measure); + if let Some(evm_archive) = evm_archive { + info!("Copying current evm state to archive."); + let src = + evm_state::Storage::open_persistent(evm_state_path, enable_gc).map_err(|e| { + Error::custom(format!("Unable to restore tmp evm backup storage {}", e)) + })?; + evm_state::storage::copy_and_purge( + src, + &[evm_archive], + bank_fields.evm_persist_feilds.last_root(), + ) + .map_err(|e| Error::custom(format!("Unable to copy_and_purge storage {}", e)))?; + }; } let evm_state = evm_state::EvmState::load_from( diff --git a/runtime/src/serde_snapshot/tests.rs b/runtime/src/serde_snapshot/tests.rs index 254631c5e1..a2039d79f1 100644 --- a/runtime/src/serde_snapshot/tests.rs +++ b/runtime/src/serde_snapshot/tests.rs @@ -225,6 +225,7 @@ fn test_bank_serialize_style(evm_version: EvmStateVersion) { false, evm_backup_state_path.path(), true, + None, ) .unwrap(); dbank.src = ref_sc; diff --git a/runtime/src/snapshot_utils.rs b/runtime/src/snapshot_utils.rs index f971c0b2ab..4a580326c8 100644 --- a/runtime/src/snapshot_utils.rs +++ b/runtime/src/snapshot_utils.rs @@ -639,6 +639,7 @@ pub fn bank_from_archive>( account_indexes: AccountSecondaryIndexes, accounts_db_caching_enabled: bool, verify_evm_state: bool, + evm_archive: Option, ) -> Result { // Untar the snapshot into a temporary directory let unpack_dir = tempfile::Builder::new() @@ -672,6 +673,7 @@ pub fn bank_from_archive>( account_indexes, accounts_db_caching_enabled, verify_evm_state, + evm_archive, )?; if !bank.verify_snapshot_bank() { @@ -823,6 +825,7 @@ fn rebuild_bank_from_snapshots( account_indexes: AccountSecondaryIndexes, accounts_db_caching_enabled: bool, verify_evm_state: bool, + evm_archive: Option, ) -> Result { info!("snapshot version: {}", snapshot_version); @@ -864,6 +867,7 @@ fn rebuild_bank_from_snapshots( accounts_db_caching_enabled, &root_paths.evm_state_backup_path, !verify_evm_state, + evm_archive, )?) })?;