blockstore.rs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. use crate::{
  2. consensus::Block,
  3. util::{
  4. serial::{deserialize, serialize},
  5. time::Timestamp,
  6. },
  7. Error, Result,
  8. };
  9. const SLED_BLOCK_TREE: &[u8] = b"_blocks";
  10. const SLED_BLOCK_ORDER_TREE: &[u8] = b"_block_order";
  11. /// The `BlockStore` is a `sled` tree storing all the blockchain's blocks
  12. /// where the key is the block's hash, and value is the serialized block.
  13. #[derive(Clone)]
  14. pub struct BlockStore(sled::Tree);
  15. impl BlockStore {
  16. /// Opens a new or existing `BlockStore` on the given sled database.
  17. pub fn new(db: &sled::Db, genesis_ts: Timestamp, genesis_data: blake3::Hash) -> Result<Self> {
  18. let tree = db.open_tree(SLED_BLOCK_TREE)?;
  19. let store = Self(tree);
  20. // In case the store is empty, initialize it with the genesis block.
  21. if store.0.is_empty() {
  22. let genesis_block = Block::genesis_block(genesis_ts, genesis_data);
  23. store.insert(&[genesis_block])?;
  24. }
  25. Ok(store)
  26. }
  27. /// Insert a slice of [`Block`] into the blockstore. With sled, the
  28. /// operation is done as a batch.
  29. /// The blocks are hashed with BLAKE3 and this blockhash is used as
  30. /// the key, while value is the serialized [`Block`] itself.
  31. /// On success, the function returns the block hashes in the same order.
  32. pub fn insert(&self, blocks: &[Block]) -> Result<Vec<blake3::Hash>> {
  33. let mut ret = Vec::with_capacity(blocks.len());
  34. let mut batch = sled::Batch::default();
  35. for block in blocks {
  36. let serialized = serialize(block);
  37. let blockhash = blake3::hash(&serialized);
  38. batch.insert(blockhash.as_bytes(), serialized);
  39. ret.push(blockhash);
  40. }
  41. self.0.apply_batch(batch)?;
  42. Ok(ret)
  43. }
  44. /// Check if the blockstore contains a given blockhash.
  45. pub fn contains(&self, blockhash: &blake3::Hash) -> Result<bool> {
  46. Ok(self.0.contains_key(blockhash.as_bytes())?)
  47. }
  48. /// Fetch given blockhashes from the blockstore.
  49. /// The resulting vector contains `Option`, which is `Some` if the block
  50. /// was found in the blockstore, and otherwise it is `None`, if it has not.
  51. /// The second parameter is a boolean which tells the function to fail in
  52. /// case at least one block was not found.
  53. pub fn get(&self, blockhashes: &[blake3::Hash], strict: bool) -> Result<Vec<Option<Block>>> {
  54. let mut ret = Vec::with_capacity(blockhashes.len());
  55. for hash in blockhashes {
  56. if let Some(found) = self.0.get(hash.as_bytes())? {
  57. let block = deserialize(&found)?;
  58. ret.push(Some(block));
  59. } else {
  60. if strict {
  61. let s = hash.to_hex().as_str().to_string();
  62. return Err(Error::BlockNotFound(s))
  63. }
  64. ret.push(None);
  65. }
  66. }
  67. Ok(ret)
  68. }
  69. /// Retrieve all blocks from the blockstore in the form of a tuple
  70. /// (`blockhash`, `block`).
  71. /// Be careful as this will try to load everything in memory.
  72. pub fn get_all(&self) -> Result<Vec<(blake3::Hash, Block)>> {
  73. let mut blocks = vec![];
  74. for block in self.0.iter() {
  75. let (key, value) = block.unwrap();
  76. let hash_bytes: [u8; 32] = key.as_ref().try_into().unwrap();
  77. let block = deserialize(&value)?;
  78. blocks.push((hash_bytes.into(), block));
  79. }
  80. Ok(blocks)
  81. }
  82. }
  83. /// The `BlockOrderStore` is a `sled` tree storing the order of the
  84. /// blockchain's slots, where the key is the slot uid, and the value is
  85. /// the block's hash. [`BlockStore`] can be queried with this hash.
  86. pub struct BlockOrderStore(sled::Tree);
  87. impl BlockOrderStore {
  88. /// Opens a new or existing `BlockOrderStore` on the given sled database.
  89. pub fn new(db: &sled::Db, genesis_ts: Timestamp, genesis_data: blake3::Hash) -> Result<Self> {
  90. let tree = db.open_tree(SLED_BLOCK_ORDER_TREE)?;
  91. let store = Self(tree);
  92. // In case the store is empty, initialize it with the genesis block.
  93. if store.0.is_empty() {
  94. let genesis_block = Block::genesis_block(genesis_ts, genesis_data);
  95. let blockhash = blake3::hash(&serialize(&genesis_block));
  96. store.insert(&[genesis_block.sl], &[blockhash])?;
  97. }
  98. Ok(store)
  99. }
  100. /// Insert a slice of slots and blockhashes into the store. With sled, the
  101. /// operation is done as a batch.
  102. /// The block slot is used as the key, and the blockhash is used as value.
  103. pub fn insert(&self, slots: &[u64], hashes: &[blake3::Hash]) -> Result<()> {
  104. assert_eq!(slots.len(), hashes.len());
  105. let mut batch = sled::Batch::default();
  106. for (i, sl) in slots.iter().enumerate() {
  107. batch.insert(&sl.to_be_bytes(), hashes[i].as_bytes());
  108. }
  109. self.0.apply_batch(batch)?;
  110. Ok(())
  111. }
  112. /// Check if the blockorderstore contains a given slot.
  113. pub fn contains(&self, slot: u64) -> Result<bool> {
  114. Ok(self.0.contains_key(slot.to_be_bytes())?)
  115. }
  116. /// Fetch given slots from the blockorderstore.
  117. /// The resulting vector contains `Option`, which is `Some` if the slot
  118. /// was found in the blockstore, and otherwise it is `None`, if it has not.
  119. /// The second parameter is a boolean which tells the function to fail in
  120. /// case at least one slot was not found.
  121. pub fn get(&self, slots: &[u64], strict: bool) -> Result<Vec<Option<blake3::Hash>>> {
  122. let mut ret = Vec::with_capacity(slots.len());
  123. for slot in slots {
  124. if let Some(found) = self.0.get(slot.to_be_bytes())? {
  125. let hash_bytes: [u8; 32] = found.as_ref().try_into().unwrap();
  126. let hash = blake3::Hash::from(hash_bytes);
  127. ret.push(Some(hash));
  128. } else {
  129. if strict {
  130. return Err(Error::SlotNotFound(*slot))
  131. }
  132. ret.push(None);
  133. }
  134. }
  135. Ok(ret)
  136. }
  137. /// Retrieve all slots from the blockorderstore in the form of a tuple
  138. /// (`slot`, `blockhash`).
  139. /// Be careful as this will try to load everything in memory.
  140. pub fn get_all(&self) -> Result<Vec<(u64, blake3::Hash)>> {
  141. let mut slots = vec![];
  142. for slot in self.0.iter() {
  143. let (key, value) = slot.unwrap();
  144. let slot_bytes: [u8; 8] = key.as_ref().try_into().unwrap();
  145. let hash_bytes: [u8; 32] = value.as_ref().try_into().unwrap();
  146. let slot = u64::from_be_bytes(slot_bytes);
  147. let hash = blake3::Hash::from(hash_bytes);
  148. slots.push((slot, hash));
  149. }
  150. Ok(slots)
  151. }
  152. /// Fetch n hashes after given slot. In the iteration, if a slot is not
  153. /// found, the iteration stops and the function returns what it has found
  154. /// so far in the `BlockOrderStore`.
  155. pub fn get_after(&self, slot: u64, n: u64) -> Result<Vec<blake3::Hash>> {
  156. let mut ret = vec![];
  157. let mut key = slot;
  158. let mut counter = 0;
  159. while counter <= n {
  160. if let Some(found) = self.0.get_gt(key.to_be_bytes())? {
  161. let key_bytes: [u8; 8] = found.0.as_ref().try_into().unwrap();
  162. key = u64::from_be_bytes(key_bytes);
  163. let block_hash = deserialize(&found.1)?;
  164. ret.push(block_hash);
  165. counter += 1;
  166. continue
  167. }
  168. break
  169. }
  170. Ok(ret)
  171. }
  172. /// Fetch the last block hash in the tree, based on the `Ord`
  173. /// implementation for `Vec<u8>`. This should not be able to
  174. /// fail because we initialize the store with the genesis block.
  175. pub fn get_last(&self) -> Result<(u64, blake3::Hash)> {
  176. let found = self.0.last()?.unwrap();
  177. let slot_bytes: [u8; 8] = found.0.as_ref().try_into().unwrap();
  178. let hash_bytes: [u8; 32] = found.1.as_ref().try_into().unwrap();
  179. let slot = u64::from_be_bytes(slot_bytes);
  180. let hash = blake3::Hash::from(hash_bytes);
  181. Ok((slot, hash))
  182. }
  183. }