Skip to content

Commit

Permalink
[storage] manually add epoch li at epoch 0 to db
Browse files Browse the repository at this point in the history
temp
  • Loading branch information
areshand committed Nov 2, 2023
1 parent 6433a31 commit 159edc9
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 17 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/test-context/src/test_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub fn new_test_context(
};
let ret =
db_bootstrapper::maybe_bootstrap::<AptosVM>(&db_rw, &genesis, genesis_waypoint).unwrap();
assert!(ret);
assert!(ret.is_some());

let mempool = MockSharedMempool::new_in_runtime(&db_rw, VMValidator::new(db.clone()));

Expand Down
29 changes: 24 additions & 5 deletions aptos-node/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,25 @@ use aptos_db::{fast_sync_storage_wrapper::FastSyncStorageWrapper, AptosDB};
use aptos_executor::db_bootstrapper::maybe_bootstrap;
use aptos_logger::{debug, info};
use aptos_storage_interface::{DbReader, DbReaderWriter};
use aptos_types::waypoint::Waypoint;
use aptos_types::{ledger_info::LedgerInfoWithSignatures, waypoint::Waypoint};
use aptos_vm::AptosVM;
use either::Either;
use std::{fs, path::Path, sync::Arc, time::Instant};
use tokio::runtime::Runtime;

pub(crate) fn maybe_apply_genesis(db_rw: &DbReaderWriter, node_config: &NodeConfig) -> Result<()> {
pub(crate) fn maybe_apply_genesis(
db_rw: &DbReaderWriter,
node_config: &NodeConfig,
) -> Result<Option<LedgerInfoWithSignatures>> {
let genesis_waypoint = node_config.base.waypoint.genesis_waypoint();
if let Some(genesis) = get_genesis_txn(node_config) {
maybe_bootstrap::<AptosVM>(db_rw, genesis, genesis_waypoint)
let ledger_info_opt = maybe_bootstrap::<AptosVM>(db_rw, genesis, genesis_waypoint)
.map_err(|err| anyhow!("DB failed to bootstrap {}", err))?;
Ok(ledger_info_opt)
} else {
info ! ("Genesis txn not provided! This is fine only if you don't expect to apply it. Otherwise, the config is incorrect!");
Ok(None)
}
Ok(())
}

#[cfg(not(feature = "consensus-only-perf-test"))]
Expand All @@ -43,9 +47,24 @@ pub(crate) fn bootstrap_db(
Either::Right(fast_sync_db_wrapper) => {
let temp_db = fast_sync_db_wrapper.get_temporary_db_with_genesis();
maybe_apply_genesis(&DbReaderWriter::from_arc(temp_db), node_config)?;

let (db_arc, db_rw) = DbReaderWriter::wrap(fast_sync_db_wrapper);
let fast_sync_db = db_arc.get_fast_sync_db();
// FastSyncDB requires ledger info at epoch 0 to establish provenance to genesis
let ledger_info = db_arc
.get_temporary_db_with_genesis()
.get_epoch_ending_ledger_info(0)
.expect("Genesis ledger info must exist");

if fast_sync_db
.get_latest_ledger_info_option()
.expect("should returns Ok results")
.is_none()
{
// it means the DB is empty and we need to
// commit the genesis ledger info to the DB.
fast_sync_db.commit_genesis_ledger_info(&ledger_info)?;
}

let db_backup_service =
start_backup_service(node_config.storage.backup_service_address, fast_sync_db);

Expand Down
7 changes: 4 additions & 3 deletions execution/executor/src/db_bootstrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ pub fn maybe_bootstrap<V: VMExecutor>(
db: &DbReaderWriter,
genesis_txn: &Transaction,
waypoint: Waypoint,
) -> Result<bool> {
) -> Result<Option<LedgerInfoWithSignatures>> {
let executed_trees = db.reader.get_latest_executed_trees()?;
// if the waypoint is not targeted with the genesis txn, it may be either already bootstrapped, or
// aiming for state sync to catch up.
if executed_trees.version().map_or(0, |v| v + 1) != waypoint.version() {
info!(waypoint = %waypoint, "Skip genesis txn.");
return Ok(false);
return Ok(None);
}

let committer = calculate_genesis::<V>(db, executed_trees, genesis_txn)?;
Expand All @@ -63,8 +63,9 @@ pub fn maybe_bootstrap<V: VMExecutor>(
waypoint,
committer.waypoint(),
);
let ledger_info = committer.output.ledger_info.clone();
committer.commit()?;
Ok(true)
Ok(ledger_info)
}

pub struct GenesisCommitter {
Expand Down
8 changes: 6 additions & 2 deletions execution/executor/tests/db_bootstrapper_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ fn test_empty_db() {
assert!(trusted_state_change.is_epoch_change());

// `maybe_bootstrap()` does nothing on non-empty DB.
assert!(!maybe_bootstrap::<AptosVM>(&db_rw, &genesis_txn, waypoint).unwrap());
assert!(maybe_bootstrap::<AptosVM>(&db_rw, &genesis_txn, waypoint)
.unwrap()
.is_none());
}

fn execute_and_commit(txns: Vec<Transaction>, db: &DbReaderWriter, signer: &ValidatorSigner) {
Expand Down Expand Up @@ -279,7 +281,9 @@ fn test_new_genesis() {

// Bootstrap DB into new genesis.
let waypoint = generate_waypoint::<AptosVM>(&db, &genesis_txn).unwrap();
assert!(maybe_bootstrap::<AptosVM>(&db, &genesis_txn, waypoint).unwrap());
assert!(maybe_bootstrap::<AptosVM>(&db, &genesis_txn, waypoint)
.unwrap()
.is_some());
assert_eq!(waypoint.version(), 6);

// Client bootable from waypoint.
Expand Down
13 changes: 13 additions & 0 deletions storage/aptosdb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,19 @@ impl AptosDB {
self.ledger_db.write_set_db().write_schemas(batch)
}

pub fn commit_genesis_ledger_info(&self, genesis_li: &LedgerInfoWithSignatures) -> Result<()> {
let ledger_batch = SchemaBatch::new();
let current_epoch = self
.ledger_store
.get_latest_ledger_info_option()
.map_or(0, |li| li.ledger_info().next_block_epoch());
ensure!(genesis_li.ledger_info().epoch() == current_epoch && current_epoch == 0);
self.ledger_store
.put_ledger_info(genesis_li, &ledger_batch)?;

self.ledger_db.metadata_db().write_schemas(ledger_batch)
}

fn commit_ledger_info(
&self,
last_version: Version,
Expand Down
2 changes: 1 addition & 1 deletion storage/db-tool/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ mod dbtool_tests {
use aptos_executor_test_helpers::integration_test_impl::{
test_execution_with_storage_impl, test_execution_with_storage_impl_inner,
};
use aptos_storage_interface::DbReader;
use aptos_temppath::TempPath;
use aptos_types::{
state_store::{state_key::StateKeyTag::AccessPath, state_key_prefix::StateKeyPrefix},
Expand Down Expand Up @@ -288,7 +289,6 @@ mod dbtool_tests {
force_sharding: bool,
) -> (Runtime, String) {
use aptos_db::utils::iterators::PrefixedStateValueIterator;
use aptos_storage_interface::DbReader;
use itertools::zip_eq;

let db = test_execution_with_storage_impl_inner(force_sharding, old_db_dir.as_path());
Expand Down
2 changes: 2 additions & 0 deletions testsuite/smoke-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ aptos-cached-packages = { workspace = true }
aptos-config = { workspace = true }
aptos-consensus = { workspace = true }
aptos-crypto = { workspace = true }
aptos-db = { workspace = true }
aptos-faucet-core = { workspace = true }
aptos-forge = { workspace = true }
aptos-framework = { workspace = true }
Expand All @@ -34,6 +35,7 @@ aptos-release-builder = { workspace = true }
aptos-rest-client = { workspace = true }
aptos-rosetta = { workspace = true }
aptos-sdk = { workspace = true }
aptos-storage-interface = { workspace = true }
aptos-temppath = { workspace = true }
aptos-types = { workspace = true }
aptos-vm = { workspace = true }
Expand Down
21 changes: 16 additions & 5 deletions testsuite/smoke-test/src/state_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ use crate::{
use aptos_config::config::{
BootstrappingMode, ContinuousSyncingMode, NodeConfig, OverrideNodeConfig,
};
use aptos_db::AptosDB;
use aptos_forge::{LocalSwarm, Node, NodeExt, Swarm, SwarmExt};
use aptos_inspection_service::inspection_client::InspectionClient;
use aptos_rest_client::Client as RestClient;
use aptos_sdk::types::LocalAccount;
use aptos_storage_interface::DbReader;
use aptos_types::{account_address::AccountAddress, PeerId};
use std::{
sync::Arc,
Expand Down Expand Up @@ -578,7 +580,7 @@ async fn perform_full_node_bootstrap_state_snapshot(epoch_changes: bool) {
for validator in swarm.validators() {
// Verify the oldest ledger info
let validator_rest_client = validator.rest_client();
verify_oldest_version_after_fast_sync(validator_rest_client, true).await;
verify_oldest_version_after_fast_sync(validator_rest_client.clone(), true).await;

// Verify the pruning metrics
let inspection_client = validator.inspection_client();
Expand All @@ -597,12 +599,21 @@ async fn perform_full_node_bootstrap_state_snapshot(epoch_changes: bool) {
// Verify the oldest ledger info and pruning metrics for the fullnode
if epoch_changes {
// Verify the oldest ledger info
let vfn_rest_client = swarm.fullnode_mut(vfn_peer_id).unwrap().rest_client();
verify_oldest_version_after_fast_sync(vfn_rest_client, false).await;

let fullnode = swarm.fullnode_mut(vfn_peer_id).unwrap();
let vfn_rest_client = fullnode.rest_client();
let db_path = fullnode.config().base.data_dir.as_path();
verify_oldest_version_after_fast_sync(vfn_rest_client.clone(), false).await;
// Verify the fullnode pruning metrics
let inspection_client = swarm.fullnode(vfn_peer_id).unwrap().inspection_client();
let inspection_client = fullnode.inspection_client();
verify_pruning_metrics_after_fast_sync(inspection_client, false).await;
// verify ledger info exists at version 0
let mut db_path_buf = db_path.to_path_buf();
db_path_buf.push("db");
fullnode.stop(); // stop the fullnode to avoid db contention
let aptos_db = AptosDB::new_for_test(db_path_buf.as_path());
aptos_db
.get_epoch_ending_ledger_info(0)
.expect("Ledgerinfo should exist at version 0");
}
}

Expand Down

0 comments on commit 159edc9

Please sign in to comment.