block_store.rs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use darkfi_sdk::{
  19. crypto::{
  20. schnorr::{SchnorrSecret, Signature},
  21. MerkleTree, SecretKey,
  22. },
  23. pasta::{group::ff::FromUniformBytes, pallas},
  24. tx::TransactionHash,
  25. };
  26. #[cfg(feature = "async-serial")]
  27. use darkfi_serial::async_trait;
  28. use darkfi_serial::{deserialize, serialize, SerialDecodable, SerialEncodable};
  29. use num_bigint::BigUint;
  30. use crate::{tx::Transaction, util::time::Timestamp, Error, Result};
  31. use super::{parse_record, parse_u32_key_record, Header, HeaderHash, SledDbOverlayPtr};
  32. /// This struct represents a tuple of the form (`header`, `txs`, `signature`).
  33. /// The header and transactions are stored as hashes, serving as pointers to the actual data
  34. /// in the sled database.
  35. /// NOTE: This struct fields are considered final, as it represents a blockchain block.
  36. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  37. pub struct Block {
  38. /// Block header
  39. pub header: HeaderHash,
  40. /// Trasaction hashes
  41. pub txs: Vec<TransactionHash>,
  42. /// Block producer signature
  43. pub signature: Signature,
  44. }
  45. impl Block {
  46. pub fn new(header: HeaderHash, txs: Vec<TransactionHash>, signature: Signature) -> Self {
  47. Self { header, txs, signature }
  48. }
  49. /// A block's hash is the same as the hash of its header
  50. pub fn hash(&self) -> HeaderHash {
  51. self.header
  52. }
  53. /// Generate a `Block` from a `BlockInfo`
  54. pub fn from_block_info(block_info: &BlockInfo) -> Self {
  55. let header = block_info.header.hash();
  56. let txs = block_info.txs.iter().map(|tx| tx.hash()).collect();
  57. let signature = block_info.signature;
  58. Self { header, txs, signature }
  59. }
  60. }
  61. /// Structure representing full block data, acting as
  62. /// a wrapper struct over `Block`, enabling us to include
  63. /// more information that might be used in different block
  64. /// version, without affecting the original struct.
  65. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  66. pub struct BlockInfo {
  67. /// Block header data
  68. pub header: Header,
  69. /// Transactions payload
  70. pub txs: Vec<Transaction>,
  71. /// Block producer signature
  72. pub signature: Signature,
  73. }
  74. impl Default for BlockInfo {
  75. /// Represents the genesis block on current timestamp
  76. fn default() -> Self {
  77. Self {
  78. header: Header::default(),
  79. txs: vec![Transaction::default()],
  80. signature: Signature::dummy(),
  81. }
  82. }
  83. }
  84. impl BlockInfo {
  85. pub fn new(header: Header, txs: Vec<Transaction>, signature: Signature) -> Self {
  86. Self { header, txs, signature }
  87. }
  88. /// Generate an empty block for provided Header.
  89. /// Transactions and the producer signature must be added after.
  90. pub fn new_empty(header: Header) -> Self {
  91. let txs = vec![];
  92. let signature = Signature::dummy();
  93. Self { header, txs, signature }
  94. }
  95. /// A block's hash is the same as the hash of its header
  96. pub fn hash(&self) -> HeaderHash {
  97. self.header.hash()
  98. }
  99. /// Append a transaction to the block. Also adds it to the Merkle tree.
  100. pub fn append_tx(&mut self, tx: Transaction) {
  101. append_tx_to_merkle_tree(&mut self.header.tree, &tx);
  102. self.txs.push(tx);
  103. }
  104. /// Append a vector of transactions to the block. Also adds them to the
  105. /// Merkle tree.
  106. pub fn append_txs(&mut self, txs: Vec<Transaction>) {
  107. for tx in txs {
  108. self.append_tx(tx);
  109. }
  110. }
  111. /// Sign block header using provided secret key
  112. // TODO: sign more stuff?
  113. pub fn sign(&mut self, secret_key: &SecretKey) {
  114. self.signature = secret_key.sign(self.hash().inner());
  115. }
  116. }
  117. /// Auxiliary structure used to keep track of blocks order.
  118. #[derive(Debug, SerialEncodable, SerialDecodable)]
  119. pub struct BlockOrder {
  120. /// Block height
  121. pub height: u32,
  122. /// Block header hash of that height
  123. pub block: HeaderHash,
  124. }
  125. /// Auxiliary structure used to keep track of block ranking information.
  126. /// Note: we only need height cummulative ranks, but we also keep its actual
  127. /// ranks, so we can verify the sequence and/or know specific block height
  128. /// ranks, if ever needed.
  129. #[derive(Debug)]
  130. pub struct BlockRanks {
  131. /// Block target rank
  132. pub target_rank: BigUint,
  133. /// Height cummulative targets rank
  134. pub targets_rank: BigUint,
  135. /// Block hash rank
  136. pub hash_rank: BigUint,
  137. /// Height cummulative hashes rank
  138. pub hashes_rank: BigUint,
  139. }
  140. impl BlockRanks {
  141. pub fn new(
  142. target_rank: BigUint,
  143. targets_rank: BigUint,
  144. hash_rank: BigUint,
  145. hashes_rank: BigUint,
  146. ) -> Self {
  147. Self { target_rank, targets_rank, hash_rank, hashes_rank }
  148. }
  149. }
  150. // Note: Doing all the imports here as this might get obselete if
  151. // we implemented Encodable/Decodable for num_bigint::BigUint.
  152. impl darkfi_serial::Encodable for BlockRanks {
  153. fn encode<S: std::io::Write>(&self, mut s: S) -> std::io::Result<usize> {
  154. let mut len = 0;
  155. len += self.target_rank.to_bytes_be().encode(&mut s)?;
  156. len += self.targets_rank.to_bytes_be().encode(&mut s)?;
  157. len += self.hash_rank.to_bytes_be().encode(&mut s)?;
  158. len += self.hashes_rank.to_bytes_be().encode(&mut s)?;
  159. Ok(len)
  160. }
  161. }
  162. impl darkfi_serial::Decodable for BlockRanks {
  163. fn decode<D: std::io::Read>(mut d: D) -> std::io::Result<Self> {
  164. let bytes: Vec<u8> = darkfi_serial::Decodable::decode(&mut d)?;
  165. let target_rank: BigUint = BigUint::from_bytes_be(&bytes);
  166. let bytes: Vec<u8> = darkfi_serial::Decodable::decode(&mut d)?;
  167. let targets_rank: BigUint = BigUint::from_bytes_be(&bytes);
  168. let bytes: Vec<u8> = darkfi_serial::Decodable::decode(&mut d)?;
  169. let hash_rank: BigUint = BigUint::from_bytes_be(&bytes);
  170. let bytes: Vec<u8> = darkfi_serial::Decodable::decode(&mut d)?;
  171. let hashes_rank: BigUint = BigUint::from_bytes_be(&bytes);
  172. let ret = Self { target_rank, targets_rank, hash_rank, hashes_rank };
  173. Ok(ret)
  174. }
  175. }
  176. /// Auxiliary structure used to keep track of block PoW difficulty information.
  177. /// Note: we only need height cummulative difficulty, but we also keep its actual
  178. /// difficulty, so we can verify the sequence and/or know specific block height
  179. /// difficulty, if ever needed.
  180. #[derive(Debug)]
  181. pub struct BlockDifficulty {
  182. /// Block height number
  183. pub height: u32,
  184. /// Block creation timestamp
  185. pub timestamp: Timestamp,
  186. /// Height difficulty
  187. pub difficulty: BigUint,
  188. /// Height cummulative difficulty (total + height difficulty)
  189. pub cummulative_difficulty: BigUint,
  190. /// Block ranks
  191. pub ranks: BlockRanks,
  192. }
  193. impl BlockDifficulty {
  194. pub fn new(
  195. height: u32,
  196. timestamp: Timestamp,
  197. difficulty: BigUint,
  198. cummulative_difficulty: BigUint,
  199. ranks: BlockRanks,
  200. ) -> Self {
  201. Self { height, timestamp, difficulty, cummulative_difficulty, ranks }
  202. }
  203. /// Represents the genesis block difficulty
  204. pub fn genesis(timestamp: Timestamp) -> Self {
  205. let ranks = BlockRanks::new(
  206. BigUint::from(0u64),
  207. BigUint::from(0u64),
  208. BigUint::from(0u64),
  209. BigUint::from(0u64),
  210. );
  211. BlockDifficulty::new(0u32, timestamp, BigUint::from(0u64), BigUint::from(0u64), ranks)
  212. }
  213. }
  214. // Note: Doing all the imports here as this might get obselete if
  215. // we implemented Encodable/Decodable for num_bigint::BigUint.
  216. impl darkfi_serial::Encodable for BlockDifficulty {
  217. fn encode<S: std::io::Write>(&self, mut s: S) -> std::io::Result<usize> {
  218. let mut len = 0;
  219. len += self.height.encode(&mut s)?;
  220. len += self.timestamp.encode(&mut s)?;
  221. len += self.difficulty.to_bytes_be().encode(&mut s)?;
  222. len += self.cummulative_difficulty.to_bytes_be().encode(&mut s)?;
  223. len += self.ranks.encode(&mut s)?;
  224. Ok(len)
  225. }
  226. }
  227. impl darkfi_serial::Decodable for BlockDifficulty {
  228. fn decode<D: std::io::Read>(mut d: D) -> std::io::Result<Self> {
  229. let height: u32 = darkfi_serial::Decodable::decode(&mut d)?;
  230. let timestamp: Timestamp = darkfi_serial::Decodable::decode(&mut d)?;
  231. let bytes: Vec<u8> = darkfi_serial::Decodable::decode(&mut d)?;
  232. let difficulty: BigUint = BigUint::from_bytes_be(&bytes);
  233. let bytes: Vec<u8> = darkfi_serial::Decodable::decode(&mut d)?;
  234. let cummulative_difficulty: BigUint = BigUint::from_bytes_be(&bytes);
  235. let ranks: BlockRanks = darkfi_serial::Decodable::decode(&mut d)?;
  236. let ret = Self { height, timestamp, difficulty, cummulative_difficulty, ranks };
  237. Ok(ret)
  238. }
  239. }
  240. const SLED_BLOCK_TREE: &[u8] = b"_blocks";
  241. const SLED_BLOCK_ORDER_TREE: &[u8] = b"_block_order";
  242. const SLED_BLOCK_DIFFICULTY_TREE: &[u8] = b"_block_difficulty";
  243. /// The `BlockStore` is a structure representing all `sled` trees related
  244. /// to storing the blockchain's blocks information.
  245. #[derive(Clone)]
  246. pub struct BlockStore {
  247. /// Main `sled` tree, storing all the blockchain's blocks, where the
  248. /// key is the blocks' hash, and value is the serialized block.
  249. pub main: sled::Tree,
  250. /// The `sled` tree storing the order of the blockchain's blocks,
  251. /// where the key is the height number, and the value is the blocks'
  252. /// hash.
  253. pub order: sled::Tree,
  254. /// The `sled` tree storing the the difficulty information of the
  255. /// blockchain's blocks, where the key is the block height number,
  256. /// and the value is the blocks' hash.
  257. pub difficulty: sled::Tree,
  258. }
  259. impl BlockStore {
  260. /// Opens a new or existing `BlockStore` on the given sled database.
  261. pub fn new(db: &sled::Db) -> Result<Self> {
  262. let main = db.open_tree(SLED_BLOCK_TREE)?;
  263. let order = db.open_tree(SLED_BLOCK_ORDER_TREE)?;
  264. let difficulty = db.open_tree(SLED_BLOCK_DIFFICULTY_TREE)?;
  265. Ok(Self { main, order, difficulty })
  266. }
  267. /// Insert a slice of [`Block`] into the store's main tree.
  268. pub fn insert(&self, blocks: &[Block]) -> Result<Vec<HeaderHash>> {
  269. let (batch, ret) = self.insert_batch(blocks);
  270. self.main.apply_batch(batch)?;
  271. Ok(ret)
  272. }
  273. /// Insert a slice of `u32` and block hashes into the store's
  274. /// order tree.
  275. pub fn insert_order(&self, heights: &[u32], hashes: &[HeaderHash]) -> Result<()> {
  276. let batch = self.insert_batch_order(heights, hashes);
  277. self.order.apply_batch(batch)?;
  278. Ok(())
  279. }
  280. /// Insert a slice of [`BlockDifficulty`] into the store's
  281. /// difficulty tree.
  282. pub fn insert_difficulty(&self, block_difficulties: &[BlockDifficulty]) -> Result<()> {
  283. let batch = self.insert_batch_difficulty(block_difficulties);
  284. self.difficulty.apply_batch(batch)?;
  285. Ok(())
  286. }
  287. /// Generate the sled batch corresponding to an insert to the main
  288. /// tree, so caller can handle the write operation.
  289. /// The block's hash() function output is used as the key,
  290. /// while value is the serialized [`Block`] itself.
  291. /// On success, the function returns the block hashes in the same order.
  292. pub fn insert_batch(&self, blocks: &[Block]) -> (sled::Batch, Vec<HeaderHash>) {
  293. let mut ret = Vec::with_capacity(blocks.len());
  294. let mut batch = sled::Batch::default();
  295. for block in blocks {
  296. let blockhash = block.hash();
  297. batch.insert(blockhash.inner(), serialize(block));
  298. ret.push(blockhash);
  299. }
  300. (batch, ret)
  301. }
  302. /// Generate the sled batch corresponding to an insert to the order
  303. /// tree, so caller can handle the write operation.
  304. /// The block height is used as the key, and the block hash is used as value.
  305. pub fn insert_batch_order(&self, heights: &[u32], hashes: &[HeaderHash]) -> sled::Batch {
  306. let mut batch = sled::Batch::default();
  307. for (i, height) in heights.iter().enumerate() {
  308. batch.insert(&height.to_be_bytes(), hashes[i].inner());
  309. }
  310. batch
  311. }
  312. /// Generate the sled batch corresponding to an insert to the difficulty
  313. /// tree, so caller can handle the write operation.
  314. /// The block's height number is used as the key, while value is
  315. // the serialized [`BlockDifficulty`] itself.
  316. pub fn insert_batch_difficulty(&self, block_difficulties: &[BlockDifficulty]) -> sled::Batch {
  317. let mut batch = sled::Batch::default();
  318. for block_difficulty in block_difficulties {
  319. batch.insert(&block_difficulty.height.to_be_bytes(), serialize(block_difficulty));
  320. }
  321. batch
  322. }
  323. /// Check if the store's main tree contains a given block hash.
  324. pub fn contains(&self, blockhash: &HeaderHash) -> Result<bool> {
  325. Ok(self.main.contains_key(blockhash.inner())?)
  326. }
  327. /// Check if the store's order tree contains a given height.
  328. pub fn contains_order(&self, height: u32) -> Result<bool> {
  329. Ok(self.order.contains_key(height.to_be_bytes())?)
  330. }
  331. /// Fetch given block hashes from the store's main tree.
  332. /// The resulting vector contains `Option`, which is `Some` if the block
  333. /// was found in the block store, and otherwise it is `None`, if it has not.
  334. /// The second parameter is a boolean which tells the function to fail in
  335. /// case at least one block was not found.
  336. pub fn get(&self, block_hashes: &[HeaderHash], strict: bool) -> Result<Vec<Option<Block>>> {
  337. let mut ret = Vec::with_capacity(block_hashes.len());
  338. for hash in block_hashes {
  339. if let Some(found) = self.main.get(hash.inner())? {
  340. let block = deserialize(&found)?;
  341. ret.push(Some(block));
  342. continue
  343. }
  344. if strict {
  345. return Err(Error::BlockNotFound(hash.as_string()))
  346. }
  347. ret.push(None);
  348. }
  349. Ok(ret)
  350. }
  351. /// Fetch given heights from the store's order tree.
  352. /// The resulting vector contains `Option`, which is `Some` if the height
  353. /// was found in the block order store, and otherwise it is `None`, if it has not.
  354. /// The second parameter is a boolean which tells the function to fail in
  355. /// case at least one height was not found.
  356. pub fn get_order(&self, heights: &[u32], strict: bool) -> Result<Vec<Option<HeaderHash>>> {
  357. let mut ret = Vec::with_capacity(heights.len());
  358. for height in heights {
  359. if let Some(found) = self.order.get(height.to_be_bytes())? {
  360. let block_hash = deserialize(&found)?;
  361. ret.push(Some(block_hash));
  362. continue
  363. }
  364. if strict {
  365. return Err(Error::BlockHeightNotFound(*height))
  366. }
  367. ret.push(None);
  368. }
  369. Ok(ret)
  370. }
  371. /// Fetch given block height numbers from the store's difficulty tree.
  372. /// The resulting vector contains `Option`, which is `Some` if the block
  373. /// height number was found in the block difficulties store, and otherwise
  374. /// it is `None`, if it has not.
  375. /// The second parameter is a boolean which tells the function to fail in
  376. /// case at least one block height number was not found.
  377. pub fn get_difficulty(
  378. &self,
  379. heights: &[u32],
  380. strict: bool,
  381. ) -> Result<Vec<Option<BlockDifficulty>>> {
  382. let mut ret = Vec::with_capacity(heights.len());
  383. for height in heights {
  384. if let Some(found) = self.difficulty.get(height.to_be_bytes())? {
  385. let block_difficulty = deserialize(&found)?;
  386. ret.push(Some(block_difficulty));
  387. continue
  388. }
  389. if strict {
  390. return Err(Error::BlockDifficultyNotFound(*height))
  391. }
  392. ret.push(None);
  393. }
  394. Ok(ret)
  395. }
  396. /// Retrieve all blocks from the store's main tree in the form of a
  397. /// tuple (`hash`, `block`).
  398. /// Be careful as this will try to load everything in memory.
  399. pub fn get_all(&self) -> Result<Vec<(HeaderHash, Block)>> {
  400. let mut blocks = vec![];
  401. for block in self.main.iter() {
  402. blocks.push(parse_record(block.unwrap())?);
  403. }
  404. Ok(blocks)
  405. }
  406. /// Retrieve complete order from the store's order tree in the form
  407. /// of a vector containing (`height`, `hash`) tuples.
  408. /// Be careful as this will try to load everything in memory.
  409. pub fn get_all_order(&self) -> Result<Vec<(u32, HeaderHash)>> {
  410. let mut order = vec![];
  411. for record in self.order.iter() {
  412. order.push(parse_u32_key_record(record.unwrap())?);
  413. }
  414. Ok(order)
  415. }
  416. /// Retrieve all block difficulties from the store's difficulty tree in
  417. /// the form of a vector containing (`height`, `difficulty`) tuples.
  418. /// Be careful as this will try to load everything in memory.
  419. pub fn get_all_difficulty(&self) -> Result<Vec<(u32, BlockDifficulty)>> {
  420. let mut block_difficulties = vec![];
  421. for record in self.difficulty.iter() {
  422. block_difficulties.push(parse_u32_key_record(record.unwrap())?);
  423. }
  424. Ok(block_difficulties)
  425. }
  426. /// Fetch n hashes after given height. In the iteration, if an order
  427. /// height is not found, the iteration stops and the function returns what
  428. /// it has found so far in the `BlockOrderStore`.
  429. pub fn get_after(&self, height: u32, n: usize) -> Result<Vec<HeaderHash>> {
  430. let mut ret = vec![];
  431. let mut key = height;
  432. let mut counter = 0;
  433. while counter <= n {
  434. if let Some(found) = self.order.get_gt(key.to_be_bytes())? {
  435. let (height, hash) = parse_u32_key_record(found)?;
  436. key = height;
  437. ret.push(hash);
  438. counter += 1;
  439. continue
  440. }
  441. break
  442. }
  443. Ok(ret)
  444. }
  445. /// Fetch the first block hash in the order tree, based on the `Ord`
  446. /// implementation for `Vec<u8>`.
  447. pub fn get_first(&self) -> Result<(u32, HeaderHash)> {
  448. let found = match self.order.first()? {
  449. Some(s) => s,
  450. None => return Err(Error::BlockHeightNotFound(0u32)),
  451. };
  452. let (height, hash) = parse_u32_key_record(found)?;
  453. Ok((height, hash))
  454. }
  455. /// Fetch the last block hash in the order tree, based on the `Ord`
  456. /// implementation for `Vec<u8>`.
  457. pub fn get_last(&self) -> Result<(u32, HeaderHash)> {
  458. let found = self.order.last()?.unwrap();
  459. let (height, hash) = parse_u32_key_record(found)?;
  460. Ok((height, hash))
  461. }
  462. /// Fetch the last record in the difficulty tree, based on the `Ord`
  463. /// implementation for `Vec<u8>`. If the tree is empty,
  464. /// returns `None`.
  465. pub fn get_last_difficulty(&self) -> Result<Option<BlockDifficulty>> {
  466. let Some(found) = self.difficulty.last()? else { return Ok(None) };
  467. let block_difficulty = deserialize(&found.1)?;
  468. Ok(Some(block_difficulty))
  469. }
  470. /// Fetch the last N records from the difficulty store, in order.
  471. pub fn get_last_n_difficulties(&self, n: usize) -> Result<Vec<BlockDifficulty>> {
  472. // Build an iterator to retrieve last N records
  473. let records = self.difficulty.iter().rev().take(n);
  474. // Since the iterator grabs in right -> left order,
  475. // we deserialize found records, and push them in reverse order
  476. let mut last_n = vec![];
  477. for record in records {
  478. last_n.insert(0, deserialize(&record?.1)?);
  479. }
  480. Ok(last_n)
  481. }
  482. /// Retrieve store's order tree records count.
  483. pub fn len(&self) -> usize {
  484. self.order.len()
  485. }
  486. /// Check if store's order tree contains any records.
  487. pub fn is_empty(&self) -> bool {
  488. self.order.is_empty()
  489. }
  490. }
  491. /// Overlay structure over a [`BlockStore`] instance.
  492. pub struct BlockStoreOverlay(SledDbOverlayPtr);
  493. impl BlockStoreOverlay {
  494. pub fn new(overlay: &SledDbOverlayPtr) -> Result<Self> {
  495. overlay.lock().unwrap().open_tree(SLED_BLOCK_TREE)?;
  496. overlay.lock().unwrap().open_tree(SLED_BLOCK_ORDER_TREE)?;
  497. overlay.lock().unwrap().open_tree(SLED_BLOCK_DIFFICULTY_TREE)?;
  498. Ok(Self(overlay.clone()))
  499. }
  500. /// Insert a slice of [`Block`] into the overlay's main tree.
  501. /// The block's hash() function output is used as the key,
  502. /// while value is the serialized [`Block`] itself.
  503. /// On success, the function returns the block hashes in the same order.
  504. pub fn insert(&self, blocks: &[Block]) -> Result<Vec<HeaderHash>> {
  505. let mut ret = Vec::with_capacity(blocks.len());
  506. let mut lock = self.0.lock().unwrap();
  507. for block in blocks {
  508. let blockhash = block.hash();
  509. lock.insert(SLED_BLOCK_TREE, blockhash.inner(), &serialize(block))?;
  510. ret.push(blockhash);
  511. }
  512. Ok(ret)
  513. }
  514. /// Insert a slice of `u32` and block hashes into overlay's order tree.
  515. /// The block height is used as the key, and the blockhash is used as value.
  516. pub fn insert_order(&self, heights: &[u32], hashes: &[HeaderHash]) -> Result<()> {
  517. if heights.len() != hashes.len() {
  518. return Err(Error::InvalidInputLengths)
  519. }
  520. let mut lock = self.0.lock().unwrap();
  521. for (i, height) in heights.iter().enumerate() {
  522. lock.insert(SLED_BLOCK_ORDER_TREE, &height.to_be_bytes(), hashes[i].inner())?;
  523. }
  524. Ok(())
  525. }
  526. /// Insert a slice of [`BlockDifficulty`] into the overlay's difficulty tree.
  527. pub fn insert_difficulty(&self, block_difficulties: &[BlockDifficulty]) -> Result<()> {
  528. let mut lock = self.0.lock().unwrap();
  529. for block_difficulty in block_difficulties {
  530. lock.insert(
  531. SLED_BLOCK_DIFFICULTY_TREE,
  532. &block_difficulty.height.to_be_bytes(),
  533. &serialize(block_difficulty),
  534. )?;
  535. }
  536. Ok(())
  537. }
  538. /// Fetch given block hashes from the overlay's main tree.
  539. /// The resulting vector contains `Option`, which is `Some` if the block
  540. /// was found in the overlay, and otherwise it is `None`, if it has not.
  541. /// The second parameter is a boolean which tells the function to fail in
  542. /// case at least one block was not found.
  543. pub fn get(&self, block_hashes: &[HeaderHash], strict: bool) -> Result<Vec<Option<Block>>> {
  544. let mut ret = Vec::with_capacity(block_hashes.len());
  545. let lock = self.0.lock().unwrap();
  546. for hash in block_hashes {
  547. if let Some(found) = lock.get(SLED_BLOCK_TREE, hash.inner())? {
  548. let block = deserialize(&found)?;
  549. ret.push(Some(block));
  550. continue
  551. }
  552. if strict {
  553. return Err(Error::BlockNotFound(hash.as_string()))
  554. }
  555. ret.push(None);
  556. }
  557. Ok(ret)
  558. }
  559. /// Fetch given heights from the overlay's order tree.
  560. /// The resulting vector contains `Option`, which is `Some` if the height
  561. /// was found in the overlay, and otherwise it is `None`, if it has not.
  562. /// The second parameter is a boolean which tells the function to fail in
  563. /// case at least one height was not found.
  564. pub fn get_order(&self, heights: &[u32], strict: bool) -> Result<Vec<Option<HeaderHash>>> {
  565. let mut ret = Vec::with_capacity(heights.len());
  566. let lock = self.0.lock().unwrap();
  567. for height in heights {
  568. if let Some(found) = lock.get(SLED_BLOCK_ORDER_TREE, &height.to_be_bytes())? {
  569. let block_hash = deserialize(&found)?;
  570. ret.push(Some(block_hash));
  571. continue
  572. }
  573. if strict {
  574. return Err(Error::BlockHeightNotFound(*height))
  575. }
  576. ret.push(None);
  577. }
  578. Ok(ret)
  579. }
  580. /// Fetch the last block hash in the overlay's order tree, based on the `Ord`
  581. /// implementation for `Vec<u8>`.
  582. pub fn get_last(&self) -> Result<(u32, HeaderHash)> {
  583. let found = match self.0.lock().unwrap().last(SLED_BLOCK_ORDER_TREE)? {
  584. Some(b) => b,
  585. None => return Err(Error::BlockHeightNotFound(0u32)),
  586. };
  587. let (height, hash) = parse_u32_key_record(found)?;
  588. Ok((height, hash))
  589. }
  590. /// Check if overlay's order tree contains any records.
  591. pub fn is_empty(&self) -> Result<bool> {
  592. Ok(self.0.lock().unwrap().is_empty(SLED_BLOCK_ORDER_TREE)?)
  593. }
  594. }
  595. /// Auxiliary function to append a transaction to a Merkle tree.
  596. pub fn append_tx_to_merkle_tree(tree: &mut MerkleTree, tx: &Transaction) {
  597. let mut buf = [0u8; 64];
  598. buf[..32].copy_from_slice(tx.hash().inner());
  599. let leaf = pallas::Base::from_uniform_bytes(&buf);
  600. tree.append(leaf.into());
  601. }