iuna

iuna

iuna - experimental mainnet-candidate protocol
git clone https://getiuna.org/git/iuna.git
Log | Files | Refs | README | LICENSE

ledger_view.rs (3500B)


      1 use anyhow::Result;
      2 
      3 use std::collections::BTreeSet;
      4 
      5 use crate::compact::CompactBlockSizeBreakdown;
      6 use crate::domain::{
      7     Block, BurnLeaderRank, Ledger, LegacyTransactionId, OutPoint, Transaction, TransactionV2,
      8     hex_encode,
      9 };
     10 use std::collections::BTreeMap;
     11 
     12 use super::{NodeCore, helpers::transaction_input_outpoints};
     13 
     14 impl NodeCore {
     15     pub fn ledger(&self) -> &Ledger {
     16         &self.ledger
     17     }
     18 
     19     pub(crate) fn clone_ledger(&self) -> Ledger {
     20         self.ledger.clone()
     21     }
     22 
     23     pub fn wallet_view_ledger(&self) -> Result<Ledger> {
     24         let mut ledger = self.ledger.clone();
     25         self.queue_local_block_anchor(&mut ledger)?;
     26         Ok(ledger)
     27     }
     28 
     29     pub fn wallet_pending_spent_outpoints(&self) -> BTreeSet<OutPoint> {
     30         let mut spent = self
     31             .ledger
     32             .pending()
     33             .iter()
     34             .flat_map(transaction_input_outpoints)
     35             .collect::<BTreeSet<_>>();
     36         if let Some((height, burn)) = &self.local_block_anchor_burn {
     37             if *height == self.ledger.height() && !self.ledger.has_transaction(burn.signature()) {
     38                 spent.extend(transaction_input_outpoints(burn));
     39             }
     40         }
     41         for transaction in self.ledger.pending_v2() {
     42             match transaction {
     43                 TransactionV2::Migration { inputs, .. } => {
     44                     spent.extend(inputs.iter().map(|input| OutPoint {
     45                         txid: match &input.outpoint_id {
     46                             LegacyTransactionId::Hash(value) => hex_encode(value),
     47                             LegacyTransactionId::Signature(value) => hex_encode(value),
     48                         },
     49                         index: input.outpoint_index,
     50                     }));
     51                 }
     52                 TransactionV2::Transfer { inputs, .. } | TransactionV2::Burn { inputs, .. } => {
     53                     spent.extend(inputs.iter().map(|input| OutPoint {
     54                         txid: hex_encode(input.outpoint_txid),
     55                         index: input.outpoint_index,
     56                     }));
     57                 }
     58                 TransactionV2::Mine { .. } => {}
     59             }
     60         }
     61         spent
     62     }
     63 
     64     pub fn chain(&self) -> &[Block] {
     65         self.ledger.chain()
     66     }
     67 
     68     pub fn chain_height(&self) -> u64 {
     69         self.ledger.height()
     70     }
     71 
     72     pub fn chain_tip_hash(&self) -> String {
     73         self.ledger.tip_hash().to_string()
     74     }
     75 
     76     pub fn has_real_chain(&self) -> bool {
     77         !self.ledger.is_setup_placeholder()
     78     }
     79 
     80     pub fn recent_blocks(&self, limit: usize) -> Vec<Block> {
     81         self.ledger.recent_blocks(limit)
     82     }
     83 
     84     pub fn blocks_before(&self, before_height: u64, limit: usize) -> Vec<Block> {
     85         self.ledger.blocks_before(before_height, limit)
     86     }
     87 
     88     pub(crate) fn block_storage_size_breakdowns(
     89         &self,
     90         blocks: &[Block],
     91     ) -> BTreeMap<String, CompactBlockSizeBreakdown> {
     92         self.ledger.storage_size_breakdowns(blocks)
     93     }
     94 
     95     pub(crate) fn chain_storage_bytes_by_hash(&self) -> Result<BTreeMap<String, u64>> {
     96         self.ledger.chain_storage_bytes_by_hash()
     97     }
     98 
     99     pub fn burn_leader_ranks_for_block(&self, height: u64) -> Result<Vec<BurnLeaderRank>> {
    100         self.ledger.burn_leader_ranks_for_block(height)
    101     }
    102 
    103     pub fn pending_transactions(&self) -> Vec<Transaction> {
    104         self.ledger.pending().to_vec()
    105     }
    106 
    107     pub fn pending_transactions_v2(&self) -> Vec<TransactionV2> {
    108         self.ledger.pending_v2().to_vec()
    109     }
    110 }