|
| 1 | +use bytemuck::TransparentWrapper; |
| 2 | + |
| 3 | +use cuprate_database::{DatabaseRw, RuntimeError, StorableVec, DatabaseRo}; |
| 4 | +use cuprate_helper::map::split_u128_into_low_high_bits; |
| 5 | +use cuprate_types::{AltBlockInformation, Chain, VerifiedTransactionInformation}; |
| 6 | + |
| 7 | +use crate::{ |
| 8 | + tables::TablesMut, |
| 9 | + types::{AltBlockHeight, AltChainInfo, AltTransactionInfo, BlockHash, CompactAltBlockInfo}, |
| 10 | +}; |
| 11 | + |
| 12 | +pub fn add_alt_block( |
| 13 | + alt_block: &AltBlockInformation, |
| 14 | + tables: &mut impl TablesMut, |
| 15 | +) -> Result<(), RuntimeError> { |
| 16 | + let alt_block_height = AltBlockHeight { |
| 17 | + chain_id: alt_block.chain_id.into(), |
| 18 | + height: alt_block.height, |
| 19 | + }; |
| 20 | + |
| 21 | + tables |
| 22 | + .alt_block_heights_mut() |
| 23 | + .put(&alt_block.block_hash, &alt_block_height)?; |
| 24 | + |
| 25 | + check_add_alt_chain_info(&alt_block_height, &alt_block.block.header.previous, tables)?; |
| 26 | + |
| 27 | + let (cumulative_difficulty_low, cumulative_difficulty_high) = |
| 28 | + split_u128_into_low_high_bits(alt_block.cumulative_difficulty); |
| 29 | + |
| 30 | + let alt_block_info = CompactAltBlockInfo { |
| 31 | + block_hash: alt_block.block_hash, |
| 32 | + pow_hash: alt_block.pow_hash, |
| 33 | + height: alt_block.height, |
| 34 | + weight: alt_block.weight, |
| 35 | + long_term_weight: alt_block.long_term_weight, |
| 36 | + cumulative_difficulty_low, |
| 37 | + cumulative_difficulty_high, |
| 38 | + }; |
| 39 | + |
| 40 | + tables |
| 41 | + .alt_blocks_info_mut() |
| 42 | + .put(&alt_block_height, &alt_block_info)?; |
| 43 | + |
| 44 | + tables.alt_block_blobs_mut().put( |
| 45 | + &alt_block_height, |
| 46 | + StorableVec::wrap_ref(&alt_block.block_blob), |
| 47 | + )?; |
| 48 | + |
| 49 | + for tx in &alt_block.txs { |
| 50 | + add_alt_transaction(&tx, tables)?; |
| 51 | + } |
| 52 | + |
| 53 | + Ok(()) |
| 54 | +} |
| 55 | + |
| 56 | +pub fn add_alt_transaction( |
| 57 | + tx: &VerifiedTransactionInformation, |
| 58 | + tables: &mut impl TablesMut, |
| 59 | +) -> Result<(), RuntimeError> { |
| 60 | + if tables.tx_ids().get(&tx.tx_hash).is_ok() |
| 61 | + || tables.alt_transaction_infos().get(&tx.tx_hash).is_ok() |
| 62 | + { |
| 63 | + return Ok(()); |
| 64 | + } |
| 65 | + |
| 66 | + tables.alt_transaction_infos_mut().put( |
| 67 | + &tx.tx_hash, |
| 68 | + &AltTransactionInfo { |
| 69 | + tx_weight: tx.tx_weight, |
| 70 | + fee: tx.fee, |
| 71 | + tx_hash: tx.tx_hash, |
| 72 | + }, |
| 73 | + )?; |
| 74 | + |
| 75 | + tables |
| 76 | + .alt_transaction_blobs_mut() |
| 77 | + .put(&tx.tx_hash, StorableVec::wrap_ref(&tx.tx_blob)) |
| 78 | +} |
| 79 | + |
| 80 | +pub fn check_add_alt_chain_info( |
| 81 | + alt_block_height: &AltBlockHeight, |
| 82 | + prev_hash: &BlockHash, |
| 83 | + tables: &mut impl TablesMut, |
| 84 | +) -> Result<(), RuntimeError> { |
| 85 | + match tables.alt_chain_infos().get(&alt_block_height.chain_id) { |
| 86 | + Ok(_) => return Ok(()), |
| 87 | + Err(RuntimeError::KeyNotFound) => (), |
| 88 | + Err(e) => return Err(e), |
| 89 | + } |
| 90 | + |
| 91 | + let parent_chain = match tables.alt_block_heights().get(prev_hash) { |
| 92 | + Ok(alt_parent_height) => Chain::Alt(alt_parent_height.chain_id.into()), |
| 93 | + Err(RuntimeError::KeyNotFound) => Chain::Main, |
| 94 | + Err(e) => return Err(e), |
| 95 | + }; |
| 96 | + |
| 97 | + tables.alt_chain_infos_mut().put( |
| 98 | + &alt_block_height.chain_id, |
| 99 | + &AltChainInfo { |
| 100 | + parent_chain: parent_chain.into(), |
| 101 | + common_ancestor_height: alt_block_height.height - 1, |
| 102 | + }, |
| 103 | + ) |
| 104 | +} |
0 commit comments