mod.rs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. use log::debug;
  2. use crate::{
  3. consensus::{Block, BlockInfo},
  4. util::time::Timestamp,
  5. Result,
  6. };
  7. pub mod blockstore;
  8. pub use blockstore::{BlockOrderStore, BlockStore, HeaderStore};
  9. pub mod metadatastore;
  10. pub use metadatastore::MetadataStore;
  11. pub mod nfstore;
  12. pub use nfstore::NullifierStore;
  13. pub mod rootstore;
  14. pub use rootstore::RootStore;
  15. pub mod statestore;
  16. pub use statestore::StateStore;
  17. pub mod txstore;
  18. pub use txstore::TxStore;
  19. /// Structure holding all sled trees that comprise the concept of Blockchain.
  20. pub struct Blockchain {
  21. /// Headers sled tree
  22. pub headers: HeaderStore,
  23. /// Blocks sled tree
  24. pub blocks: BlockStore,
  25. /// Block order sled tree
  26. pub order: BlockOrderStore,
  27. /// Transactions sled tree
  28. pub transactions: TxStore,
  29. /// Metadata sled tree
  30. pub metadata: MetadataStore,
  31. /// Nullifiers sled tree
  32. pub nullifiers: NullifierStore,
  33. /// Merkle roots sled tree
  34. pub merkle_roots: RootStore,
  35. }
  36. impl Blockchain {
  37. //FIXME why the blockchain taking genesis_data on the constructor as a hash?
  38. //genesis data are supposed to be a a hash?
  39. /// Instantiate a new `Blockchain` with the given `sled` database.
  40. pub fn new(db: &sled::Db, genesis_ts: Timestamp, genesis_data: blake3::Hash) -> Result<Self> {
  41. let headers = HeaderStore::new(db, genesis_ts, genesis_data)?;
  42. let blocks = BlockStore::new(db, genesis_ts, genesis_data)?;
  43. let order = BlockOrderStore::new(db, genesis_ts, genesis_data)?;
  44. let metadata = MetadataStore::new(db, genesis_ts, genesis_data)?;
  45. let transactions = TxStore::new(db)?;
  46. let nullifiers = NullifierStore::new(db)?;
  47. let merkle_roots = RootStore::new(db)?;
  48. Ok(Self { headers, blocks, order, transactions, metadata, nullifiers, merkle_roots })
  49. }
  50. /// Insert a given slice of [`BlockInfo`] into the blockchain database.
  51. /// This functions wraps all the logic of separating the block into specific
  52. /// data that can be fed into the different trees of the database.
  53. /// Upon success, the functions returns a vector of the block hashes that
  54. /// were given and appended to the ledger.
  55. pub fn add(&self, blocks: &[BlockInfo]) -> Result<Vec<blake3::Hash>> {
  56. let mut ret = Vec::with_capacity(blocks.len());
  57. for block in blocks {
  58. // Store transactions
  59. let _tx_hashes = self.transactions.insert(&block.txs)?;
  60. // Store header
  61. let headerhash = self.headers.insert(&[block.header.clone()])?;
  62. ret.push(headerhash[0]);
  63. // Store block
  64. //let _block = Block::new(headerhash[0], tx_hashes, block.m.clone());
  65. //self.blocks.insert(&[_block])?;
  66. let blk: Block = Block::from(block.clone());
  67. self.blocks.insert(&[blk])?;
  68. // Store block order
  69. self.order.insert(&[block.header.slot], &[headerhash[0]])?;
  70. // Store ouroboros metadata
  71. self.metadata.insert(&[headerhash[0]], &[block.metadata.clone()])?;
  72. // NOTE: The nullifiers and Merkle roots are applied in the state
  73. // transition apply function.
  74. }
  75. Ok(ret)
  76. }
  77. /// Check if the given [`BlockInfo`] is in the database and all trees.
  78. pub fn has_block(&self, block: &BlockInfo) -> Result<bool> {
  79. let blockhash = match self.order.get(&[block.header.slot], true) {
  80. Ok(v) => v[0].unwrap(),
  81. Err(_) => return Ok(false),
  82. };
  83. // TODO: Check if we have all transactions
  84. // Check provided info produces the same hash
  85. Ok(blockhash == block.header.headerhash())
  86. }
  87. /// Retrieve [`BlockInfo`]s by given hashes. Fails if any of them are not found.
  88. pub fn get_blocks_by_hash(&self, hashes: &[blake3::Hash]) -> Result<Vec<BlockInfo>> {
  89. let mut ret = Vec::with_capacity(hashes.len());
  90. let headers = self.headers.get(hashes, true)?;
  91. let blocks = self.blocks.get(hashes, true)?;
  92. for (i, header) in headers.iter().enumerate() {
  93. let header = header.clone().unwrap();
  94. let block = blocks[i].clone().unwrap();
  95. let txs = self.transactions.get(&block.txs, true)?;
  96. let txs = txs.iter().map(|x| x.clone().unwrap()).collect();
  97. let info = BlockInfo::new(header, txs, block.metadata.clone());
  98. ret.push(info);
  99. }
  100. Ok(ret)
  101. }
  102. /// Retrieve [`BlockInfo`]s by given slots. Does not fail if any of them are not found.
  103. pub fn get_blocks_by_slot(&self, slots: &[u64]) -> Result<Vec<BlockInfo>> {
  104. debug!("get_blocks_by_slot(): {:?}", slots);
  105. let blockhashes = self.order.get(slots, false)?;
  106. let mut hashes = vec![];
  107. for i in blockhashes.into_iter().flatten() {
  108. hashes.push(i);
  109. }
  110. self.get_blocks_by_hash(&hashes)
  111. }
  112. /// Retrieve n blocks after given start slot.
  113. pub fn get_blocks_after(&self, slot: u64, n: u64) -> Result<Vec<BlockInfo>> {
  114. debug!("get_blocks_after(): {} -> {}", slot, n);
  115. let hashes = self.order.get_after(slot, n)?;
  116. self.get_blocks_by_hash(&hashes)
  117. }
  118. /// Retrieve the last block slot and hash.
  119. pub fn last(&self) -> Result<(u64, blake3::Hash)> {
  120. self.order.get_last()
  121. }
  122. pub fn get_last_proof_hash(&self) -> Result<blake3::Hash> {
  123. let (hash, _) = self.metadata.get_last().unwrap();
  124. Ok(hash)
  125. }
  126. }