|
| 1 | +use anyhow::Result; |
| 2 | +use aptos_config::config::{RocksdbConfigs, StorageDirPaths, NO_OP_STORAGE_PRUNER_CONFIG}; |
| 3 | +use aptos_db::AptosDB; |
| 4 | +use aptos_storage_interface::DbReader; |
| 5 | +use aptos_types::{transaction::Transaction, waypoint::Waypoint}; |
| 6 | +use std::path::Path; |
| 7 | +use std::fs; |
| 8 | + |
| 9 | +/// Extract genesis transaction and waypoint from an Aptos database |
| 10 | +pub fn extract_genesis_and_waypoint(db_path: &str, output_dir: &str) -> Result<()> { |
| 11 | + println!("Opening database at: {}", db_path); |
| 12 | + |
| 13 | + // Create storage directory paths |
| 14 | + let storage_dir_paths = StorageDirPaths::from_path(Path::new(db_path)); |
| 15 | + |
| 16 | + // Open the database with correct API |
| 17 | + let db = AptosDB::open( |
| 18 | + storage_dir_paths, |
| 19 | + true, // readonly |
| 20 | + NO_OP_STORAGE_PRUNER_CONFIG, // pruner_config |
| 21 | + RocksdbConfigs::default(), |
| 22 | + false, // enable_indexer |
| 23 | + 1, // buffered_state_target_items |
| 24 | + 10000, // max_num_nodes_per_lru_cache_shard |
| 25 | + None, // internal_indexer_db |
| 26 | + )?; |
| 27 | + |
| 28 | + println!("Database opened successfully"); |
| 29 | + |
| 30 | + // Get the latest version to understand the database state |
| 31 | + let latest_version = db.get_synced_version()?; |
| 32 | + println!("Latest synced version: {:?}", latest_version); |
| 33 | + |
| 34 | + if latest_version.is_none() { |
| 35 | + return Err(anyhow::anyhow!("Database has no synced version")); |
| 36 | + } |
| 37 | + |
| 38 | + let latest_ver = latest_version.unwrap(); |
| 39 | + |
| 40 | + // Extract genesis transaction |
| 41 | + extract_genesis_transaction(&db, latest_ver, output_dir)?; |
| 42 | + |
| 43 | + // Extract waypoint |
| 44 | + extract_waypoint(&db, output_dir)?; |
| 45 | + |
| 46 | + println!("✓ Genesis extraction completed successfully!"); |
| 47 | + println!(" - genesis.blob: Contains the BCS-serialized genesis transaction"); |
| 48 | + println!(" - waypoint.txt: Contains the initial waypoint for bootstrapping"); |
| 49 | + |
| 50 | + Ok(()) |
| 51 | +} |
| 52 | + |
| 53 | +/// Extract the genesis transaction from the database |
| 54 | +fn extract_genesis_transaction(db: &AptosDB, latest_ver: u64, output_dir: &str) -> Result<()> { |
| 55 | + println!("Extracting genesis transaction (version 0)..."); |
| 56 | + let genesis_txn_with_proof = db.get_transaction_by_version(0, latest_ver, false)?; |
| 57 | + let genesis_transaction = genesis_txn_with_proof.transaction; |
| 58 | + |
| 59 | + // Serialize the genesis transaction using BCS |
| 60 | + let genesis_bytes = bcs::to_bytes(&genesis_transaction)?; |
| 61 | + |
| 62 | + // Write genesis.blob |
| 63 | + let genesis_path = format!("{}/genesis.blob", output_dir); |
| 64 | + fs::write(&genesis_path, &genesis_bytes)?; |
| 65 | + println!("Genesis transaction written to: {}", genesis_path); |
| 66 | + println!("Genesis blob size: {} bytes", genesis_bytes.len()); |
| 67 | + |
| 68 | + // Print information about the genesis transaction |
| 69 | + print_genesis_transaction_info(&genesis_transaction); |
| 70 | + |
| 71 | + Ok(()) |
| 72 | +} |
| 73 | + |
| 74 | +/// Extract the waypoint from the database using proper waypoint conversion |
| 75 | +fn extract_waypoint(db: &AptosDB, output_dir: &str) -> Result<()> { |
| 76 | + // Get the ledger info to extract waypoint |
| 77 | + let ledger_info_with_sigs = db.get_latest_ledger_info()?; |
| 78 | + let ledger_info = ledger_info_with_sigs.ledger_info(); |
| 79 | + |
| 80 | + // Generate waypoint using the proper converter |
| 81 | + let waypoint = Waypoint::new_any(ledger_info); |
| 82 | + |
| 83 | + // Write waypoint.txt |
| 84 | + let waypoint_path = format!("{}/waypoint.txt", output_dir); |
| 85 | + fs::write(&waypoint_path, waypoint.to_string())?; |
| 86 | + println!("Waypoint written to: {}", waypoint_path); |
| 87 | + println!("Waypoint: {}", waypoint); |
| 88 | + |
| 89 | + Ok(()) |
| 90 | +} |
| 91 | + |
| 92 | +/// Print detailed information about the genesis transaction |
| 93 | +fn print_genesis_transaction_info(genesis_transaction: &Transaction) { |
| 94 | + match genesis_transaction { |
| 95 | + Transaction::GenesisTransaction(genesis_payload) => { |
| 96 | + println!("✓ Found GenesisTransaction (WriteSet transaction)"); |
| 97 | + // Access the payload correctly |
| 98 | + match genesis_payload { |
| 99 | + aptos_types::transaction::WriteSetPayload::Direct(change_set) => { |
| 100 | + println!(" Direct WriteSet payload"); |
| 101 | + println!(" Change set size: {} bytes", bcs::to_bytes(change_set).unwrap_or_default().len()); |
| 102 | + }, |
| 103 | + aptos_types::transaction::WriteSetPayload::Script { .. } => { |
| 104 | + println!(" Script-based WriteSet"); |
| 105 | + }, |
| 106 | + } |
| 107 | + }, |
| 108 | + Transaction::BlockMetadata(_) => { |
| 109 | + println!("⚠ Transaction 0 is BlockMetadata (unexpected for genesis)"); |
| 110 | + }, |
| 111 | + Transaction::BlockMetadataExt(_) => { |
| 112 | + println!("⚠ Transaction 0 is BlockMetadataExt (unexpected for genesis)"); |
| 113 | + }, |
| 114 | + Transaction::BlockEpilogue(_) => { |
| 115 | + println!("⚠ Transaction 0 is BlockEpilogue (unexpected for genesis)"); |
| 116 | + }, |
| 117 | + Transaction::UserTransaction(_) => { |
| 118 | + println!("⚠ Transaction 0 is UserTransaction (unexpected for genesis)"); |
| 119 | + }, |
| 120 | + Transaction::StateCheckpoint(_) => { |
| 121 | + println!("⚠ Transaction 0 is StateCheckpoint (unexpected for genesis)"); |
| 122 | + }, |
| 123 | + Transaction::ValidatorTransaction(_) => { |
| 124 | + println!("⚠ Transaction 0 is ValidatorTransaction (unexpected for genesis)"); |
| 125 | + }, |
| 126 | + } |
| 127 | +} |
0 commit comments