|
| 1 | +use std::path::Path; |
| 2 | + |
| 3 | +use anyhow::{anyhow, Context, Result}; |
| 4 | + |
| 5 | +use diem_backup_cli::backup_types::transaction::manifest::TransactionBackup; |
| 6 | +use diem_backup_cli::backup_types::transaction::manifest::TransactionChunk; |
| 7 | +use diem_backup_cli::utils::read_record_bytes::ReadRecordBytes; |
| 8 | +use diem_types::contract_event::ContractEvent; |
| 9 | +use diem_types::transaction::Transaction; |
| 10 | +use diem_types::transaction::TransactionInfo; |
| 11 | +use diem_types::write_set::WriteSet; |
| 12 | +use libra_backwards_compatibility::version_five::state_snapshot_v5::open_for_read; |
| 13 | + |
| 14 | +/// read snapshot manifest file into object |
| 15 | +pub fn load_tx_chunk_manifest(path: &Path) -> anyhow::Result<TransactionBackup> { |
| 16 | + let s = |
| 17 | + std::fs::read_to_string(path).context(format!("Error: cannot read file at {:?}", path))?; |
| 18 | + |
| 19 | + let map: TransactionBackup = serde_json::from_str(&s)?; |
| 20 | + |
| 21 | + Ok(map) |
| 22 | +} |
| 23 | + |
| 24 | +// similar to Loaded Chunk |
| 25 | +// diem/storage/backup/backup-cli/src/backup_types/transaction/restore.rs |
| 26 | +// The vectors below are OF THE SAME LENGTH |
| 27 | +// It is a table where for example, a tx without events will be an empty slot in the vector. |
| 28 | +pub struct TransactionArchiveChunk { |
| 29 | + pub manifest: TransactionChunk, |
| 30 | + pub txns: Vec<Transaction>, |
| 31 | + pub txn_infos: Vec<TransactionInfo>, |
| 32 | + pub event_vecs: Vec<Vec<ContractEvent>>, |
| 33 | + pub write_sets: Vec<WriteSet>, |
| 34 | +} |
| 35 | + |
| 36 | +pub async fn load_chunk( |
| 37 | + archive_path: &Path, |
| 38 | + manifest: TransactionChunk, |
| 39 | +) -> Result<TransactionArchiveChunk> { |
| 40 | + let full_handle = archive_path |
| 41 | + .parent() |
| 42 | + .expect("could not read archive path") |
| 43 | + .join(&manifest.transactions); |
| 44 | + let handle_str = full_handle.to_str().unwrap(); |
| 45 | + assert!(full_handle.exists(), "file does not exist"); |
| 46 | + |
| 47 | + let mut file = open_for_read(handle_str) |
| 48 | + .await |
| 49 | + .map_err(|e| anyhow!("snapshot chunk {:?}, {:?}", &handle_str, e))?; |
| 50 | + |
| 51 | + let mut txns = Vec::new(); |
| 52 | + let mut txn_infos = Vec::new(); |
| 53 | + let mut event_vecs = Vec::new(); |
| 54 | + let mut write_sets = Vec::new(); |
| 55 | + |
| 56 | + while let Some(record_bytes) = file.read_record_bytes().await? { |
| 57 | + let (txn, txn_info, events, write_set): (_, _, _, WriteSet) = |
| 58 | + bcs::from_bytes(&record_bytes)?; |
| 59 | + txns.push(txn); |
| 60 | + txn_infos.push(txn_info); |
| 61 | + event_vecs.push(events); |
| 62 | + write_sets.push(write_set); |
| 63 | + } |
| 64 | + |
| 65 | + // the chunk is a table implements with vectors, |
| 66 | + // they should have the same length |
| 67 | + assert!( |
| 68 | + txns.len() == txn_infos.len() |
| 69 | + && txn_infos.len() == event_vecs.len() |
| 70 | + && event_vecs.len() == write_sets.len(), |
| 71 | + "transactions chunk have different vector length for txs, events, and writesets" |
| 72 | + ); |
| 73 | + |
| 74 | + // TODO: for purposes of explorer/warehouse do we want to do the full tx restore controller verifications |
| 75 | + |
| 76 | + Ok(TransactionArchiveChunk { |
| 77 | + manifest, |
| 78 | + txns, |
| 79 | + txn_infos, |
| 80 | + event_vecs, |
| 81 | + write_sets, |
| 82 | + }) |
| 83 | +} |
0 commit comments