mod.rs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 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 std::sync::{Arc, Mutex};
  19. use log::debug;
  20. use sled::Transactional;
  21. use darkfi_sdk::blockchain::Slot;
  22. use darkfi_serial::{deserialize, serialize, Decodable};
  23. use crate::{tx::Transaction, Error, Result};
  24. /// Block related definitions and storage implementations
  25. pub mod block_store;
  26. pub use block_store::{
  27. Block, BlockInfo, BlockOrderStore, BlockOrderStoreOverlay, BlockProducer, BlockStore,
  28. BlockStoreOverlay,
  29. };
  30. /// Header definition and storage implementation
  31. pub mod header_store;
  32. pub use header_store::{Header, HeaderStore, HeaderStoreOverlay};
  33. /// Slots storage implementation
  34. pub mod slot_store;
  35. pub use slot_store::{validate_slot, SlotStore, SlotStoreOverlay};
  36. /// Transactions related storage implementations
  37. pub mod tx_store;
  38. pub use tx_store::{PendingTxOrderStore, PendingTxStore, TxStore, TxStoreOverlay};
  39. /// Contracts and Wasm storage implementations
  40. pub mod contract_store;
  41. pub use contract_store::{
  42. ContractStateStore, ContractStateStoreOverlay, WasmStore, WasmStoreOverlay,
  43. };
  44. /// Structure holding all sled trees that define the concept of Blockchain.
  45. #[derive(Clone)]
  46. pub struct Blockchain {
  47. /// Main pointer to the sled db connection
  48. pub sled_db: sled::Db,
  49. /// Headers sled tree
  50. pub headers: HeaderStore,
  51. /// Blocks sled tree
  52. pub blocks: BlockStore,
  53. /// Block order sled tree
  54. pub order: BlockOrderStore,
  55. /// Slot sled tree
  56. pub slots: SlotStore,
  57. /// Transactions sled tree
  58. pub transactions: TxStore,
  59. /// Pending transactions sled tree
  60. pub pending_txs: PendingTxStore,
  61. /// Pending transactions order sled tree
  62. pub pending_txs_order: PendingTxOrderStore,
  63. /// Contract states
  64. pub contracts: ContractStateStore,
  65. /// Wasm bincodes
  66. pub wasm_bincode: WasmStore,
  67. }
  68. impl Blockchain {
  69. /// Instantiate a new `Blockchain` with the given `sled` database.
  70. pub fn new(db: &sled::Db) -> Result<Self> {
  71. let headers = HeaderStore::new(db)?;
  72. let blocks = BlockStore::new(db)?;
  73. let order = BlockOrderStore::new(db)?;
  74. let slots = SlotStore::new(db)?;
  75. let transactions = TxStore::new(db)?;
  76. let pending_txs = PendingTxStore::new(db)?;
  77. let pending_txs_order = PendingTxOrderStore::new(db)?;
  78. let contracts = ContractStateStore::new(db)?;
  79. let wasm_bincode = WasmStore::new(db)?;
  80. Ok(Self {
  81. sled_db: db.clone(),
  82. headers,
  83. blocks,
  84. order,
  85. slots,
  86. transactions,
  87. pending_txs,
  88. pending_txs_order,
  89. contracts,
  90. wasm_bincode,
  91. })
  92. }
  93. /* TODO: FIXME: This should not be part of `Blockchain`
  94. /// A blockchain is considered valid, when every block is valid,
  95. /// based on validate_block checks.
  96. /// Be careful as this will try to load everything in memory.
  97. pub fn validate(&self) -> Result<()> {
  98. // We use block order store here so we have all blocks in order
  99. let blocks = self.order.get_all()?;
  100. for (index, block) in blocks[1..].iter().enumerate() {
  101. let full_blocks = self.get_blocks_by_hash(&[blocks[index].1, block.1])?;
  102. let expected_reward = next_block_reward();
  103. full_blocks[1].validate(&full_blocks[0], expected_reward)?;
  104. }
  105. Ok(())
  106. }
  107. */
  108. /// Insert a given [`BlockInfo`] into the blockchain database.
  109. /// This functions wraps all the logic of separating the block into specific
  110. /// data that can be fed into the different trees of the database.
  111. /// Upon success, the functions returns the block hash that
  112. /// were given and appended to the ledger.
  113. pub fn add_block(&self, block: &BlockInfo) -> Result<blake3::Hash> {
  114. let mut trees = vec![];
  115. let mut batches = vec![];
  116. // Store transactions
  117. let (txs_batch, _) = self.transactions.insert_batch(&block.txs)?;
  118. trees.push(self.transactions.0.clone());
  119. batches.push(txs_batch);
  120. // Store header
  121. let (headers_batch, _) = self.headers.insert_batch(&[block.header.clone()])?;
  122. trees.push(self.headers.0.clone());
  123. batches.push(headers_batch);
  124. // Store block
  125. let blk: Block = Block::from(block.clone());
  126. let (bocks_batch, block_hashes) = self.blocks.insert_batch(&[blk])?;
  127. let block_hash = block_hashes[0];
  128. trees.push(self.blocks.0.clone());
  129. batches.push(bocks_batch);
  130. // Store block order
  131. let blocks_order_batch = self.order.insert_batch(&[block.header.slot], &[block_hash])?;
  132. trees.push(self.order.0.clone());
  133. batches.push(blocks_order_batch);
  134. // Store slot checkpoints
  135. let slots_batch = self.slots.insert_batch(&block.slots)?;
  136. trees.push(self.slots.0.clone());
  137. batches.push(slots_batch);
  138. // Perform an atomic transaction over the trees and apply the batches.
  139. self.atomic_write(&trees, &batches)?;
  140. Ok(block_hash)
  141. }
  142. /// Check if the given [`BlockInfo`] is in the database and all trees.
  143. pub fn has_block(&self, block: &BlockInfo) -> Result<bool> {
  144. let blockhash = match self.order.get(&[block.header.slot], true) {
  145. Ok(v) => v[0].unwrap(),
  146. Err(_) => return Ok(false),
  147. };
  148. // Check if we have all transactions
  149. let txs: Vec<blake3::Hash> =
  150. block.txs.iter().map(|x| blake3::hash(&serialize(x))).collect();
  151. if self.transactions.get(&txs, true).is_err() {
  152. return Ok(false)
  153. }
  154. // Check if we have all slots
  155. let slots: Vec<u64> = block.slots.iter().map(|x| x.id).collect();
  156. if self.slots.get(&slots, true).is_err() {
  157. return Ok(false)
  158. }
  159. // Check provided info produces the same hash
  160. Ok(blockhash == block.blockhash())
  161. }
  162. /// Retrieve [`BlockInfo`]s by given hashes. Fails if any of them is not found.
  163. pub fn get_blocks_by_hash(&self, hashes: &[blake3::Hash]) -> Result<Vec<BlockInfo>> {
  164. let blocks = self.blocks.get(hashes, true)?;
  165. let blocks: Vec<Block> = blocks.iter().map(|x| x.clone().unwrap()).collect();
  166. let ret = self.get_blocks_infos(&blocks)?;
  167. Ok(ret)
  168. }
  169. /// Retrieve all [`BlockInfo`] for given slice of [`Block`].
  170. /// Fails if any of them is not found
  171. fn get_blocks_infos(&self, blocks: &[Block]) -> Result<Vec<BlockInfo>> {
  172. let mut ret = Vec::with_capacity(blocks.len());
  173. for block in blocks {
  174. let headers = self.headers.get(&[block.header], true)?;
  175. // Since we used strict get, its safe to unwrap here
  176. let header = headers[0].clone().unwrap();
  177. let txs = self.transactions.get(&block.txs, true)?;
  178. let txs = txs.iter().map(|x| x.clone().unwrap()).collect();
  179. let slots = self.slots.get(&block.slots, true)?;
  180. let slots = slots.iter().map(|x| x.clone().unwrap()).collect();
  181. let info = BlockInfo::new(header, txs, block.producer.clone(), slots);
  182. ret.push(info);
  183. }
  184. Ok(ret)
  185. }
  186. /// Retrieve [`BlockInfo`]s by given slots. Does not fail if any of them are not found.
  187. pub fn get_blocks_by_slot(&self, slots: &[u64]) -> Result<Vec<BlockInfo>> {
  188. debug!(target: "blockchain", "get_blocks_by_slot(): {:?}", slots);
  189. let blockhashes = self.order.get(slots, false)?;
  190. let mut hashes = vec![];
  191. for i in blockhashes.into_iter().flatten() {
  192. hashes.push(i);
  193. }
  194. self.get_blocks_by_hash(&hashes)
  195. }
  196. /// Retrieve n blocks after given start slot.
  197. pub fn get_blocks_after(&self, slot: u64, n: u64) -> Result<Vec<BlockInfo>> {
  198. debug!(target: "blockchain", "get_blocks_after(): {} -> {}", slot, n);
  199. let hashes = self.order.get_after(slot, n)?;
  200. self.get_blocks_by_hash(&hashes)
  201. }
  202. /// Retrieve stored blocks count
  203. pub fn len(&self) -> usize {
  204. self.order.len()
  205. }
  206. /// Retrieve stored txs count
  207. pub fn txs_len(&self) -> usize {
  208. self.transactions.len()
  209. }
  210. /// Check if blockchain contains any blocks
  211. pub fn is_empty(&self) -> bool {
  212. self.order.is_empty()
  213. }
  214. /// Retrieve genesis (first) block slot and hash.
  215. pub fn genesis(&self) -> Result<(u64, blake3::Hash)> {
  216. self.order.get_first()
  217. }
  218. /// Retrieve the last block slot and hash.
  219. pub fn last(&self) -> Result<(u64, blake3::Hash)> {
  220. self.order.get_last()
  221. }
  222. /// Retrieve the last block info.
  223. pub fn last_block(&self) -> Result<BlockInfo> {
  224. let (_, hash) = self.last()?;
  225. Ok(self.get_blocks_by_hash(&[hash])?[0].clone())
  226. }
  227. /// Retrieve the last slot.
  228. pub fn last_slot(&self) -> Result<Slot> {
  229. self.slots.get_last()
  230. }
  231. /// Retrieve n slots after given start slot.
  232. pub fn get_slots_after(&self, slot: u64, n: u64) -> Result<Vec<Slot>> {
  233. debug!(target: "blockchain", "get_slots_after(): {} -> {}", slot, n);
  234. self.slots.get_after(slot, n)
  235. }
  236. /// Retrieve [`Slot`]s by given ids. Does not fail if any of them are not found.
  237. pub fn get_slots_by_id(&self, ids: &[u64]) -> Result<Vec<Option<Slot>>> {
  238. debug!(target: "blockchain", "get_slots_by_id(): {:?}", ids);
  239. self.slots.get(ids, true)
  240. }
  241. /// Check if the given [`Slot`] is in the database and all trees.
  242. pub fn has_slot(&self, slot: &Slot) -> Result<bool> {
  243. Ok(self.slots.get(&[slot.id], true).is_ok())
  244. }
  245. /// Check if block order for the given slot is in the database.
  246. pub fn has_slot_order(&self, slot: u64) -> Result<bool> {
  247. let vec = match self.order.get(&[slot], true) {
  248. Ok(v) => v,
  249. Err(_) => return Ok(false),
  250. };
  251. Ok(!vec.is_empty())
  252. }
  253. /// Insert a given slice of pending transactions into the blockchain database.
  254. /// On success, the function returns the transaction hashes in the same order
  255. /// as the input transactions.
  256. pub fn add_pending_txs(&self, txs: &[Transaction]) -> Result<Vec<blake3::Hash>> {
  257. let (txs_batch, txs_hashes) = self.pending_txs.insert_batch(txs)?;
  258. let txs_order_batch = self.pending_txs_order.insert_batch(&txs_hashes)?;
  259. // Perform an atomic transaction over the trees and apply the batches.
  260. let trees = [self.pending_txs.0.clone(), self.pending_txs_order.0.clone()];
  261. let batches = [txs_batch, txs_order_batch];
  262. self.atomic_write(&trees, &batches)?;
  263. Ok(txs_hashes)
  264. }
  265. /// Retrieve all transactions from the pending tx store.
  266. /// Be careful as this will try to load everything in memory.
  267. pub fn get_pending_txs(&self) -> Result<Vec<Transaction>> {
  268. let txs = self.pending_txs.get_all()?;
  269. let indexes = self.pending_txs_order.get_all()?;
  270. if txs.len() != indexes.len() {
  271. return Err(Error::InvalidInputLengths)
  272. }
  273. let mut ret = Vec::with_capacity(txs.len());
  274. for index in indexes {
  275. ret.push(txs.get(&index.1).unwrap().clone());
  276. }
  277. Ok(ret)
  278. }
  279. /// Remove a given slice of pending transactions from the blockchain database.
  280. pub fn remove_pending_txs(&self, txs: &[Transaction]) -> Result<()> {
  281. let txs_hashes: Vec<blake3::Hash> =
  282. txs.iter().map(|x| blake3::hash(&serialize(x))).collect();
  283. let indexes = self.pending_txs_order.get_all()?;
  284. // We could do indexes.iter().map(|x| txs_hashes.contains(x.1)).collect.map(|x| x.0).collect but this is faster
  285. // since we don't do the second iteration
  286. let mut removed_indexes = vec![];
  287. for index in indexes {
  288. if txs_hashes.contains(&index.1) {
  289. removed_indexes.push(index.0);
  290. }
  291. }
  292. let txs_batch = self.pending_txs.remove_batch(&txs_hashes);
  293. let txs_order_batch = self.pending_txs_order.remove_batch(&removed_indexes);
  294. // Perform an atomic transaction over the trees and apply the batches.
  295. let trees = [self.pending_txs.0.clone(), self.pending_txs_order.0.clone()];
  296. let batches = [txs_batch, txs_order_batch];
  297. self.atomic_write(&trees, &batches)?;
  298. Ok(())
  299. }
  300. /// Auxiliary function to write to multiple trees completely atomic.
  301. fn atomic_write(&self, trees: &[sled::Tree], batches: &[sled::Batch]) -> Result<()> {
  302. if trees.len() != batches.len() {
  303. return Err(Error::InvalidInputLengths)
  304. }
  305. trees.transaction(|trees| {
  306. for (index, tree) in trees.iter().enumerate() {
  307. tree.apply_batch(&batches[index])?;
  308. }
  309. Ok::<(), sled::transaction::ConflictableTransactionError<sled::Error>>(())
  310. })?;
  311. Ok(())
  312. }
  313. /// Retrieve all blocks contained in the blockchain in order.
  314. /// Be careful as this will try to load everything in memory.
  315. pub fn get_all(&self) -> Result<Vec<BlockInfo>> {
  316. let order = self.order.get_all()?;
  317. let order: Vec<blake3::Hash> = order.iter().map(|x| x.1).collect();
  318. let blocks = self.get_blocks_by_hash(&order)?;
  319. Ok(blocks)
  320. }
  321. }
  322. /// Atomic pointer to sled db overlay.
  323. pub type SledDbOverlayPtr = Arc<Mutex<sled_overlay::SledDbOverlay>>;
  324. /// Atomic pointer to blockchain overlay.
  325. pub type BlockchainOverlayPtr = Arc<Mutex<BlockchainOverlay>>;
  326. /// Overlay structure over a [`Blockchain`] instance.
  327. pub struct BlockchainOverlay {
  328. /// Main [`sled_overlay::SledDbOverlay`] to the sled db connection
  329. pub overlay: SledDbOverlayPtr,
  330. /// Headers overlay
  331. pub headers: HeaderStoreOverlay,
  332. /// Blocks overlay
  333. pub blocks: BlockStoreOverlay,
  334. /// Block order overlay
  335. pub order: BlockOrderStoreOverlay,
  336. /// Slots overlay
  337. pub slots: SlotStoreOverlay,
  338. /// Transactions overlay
  339. pub transactions: TxStoreOverlay,
  340. /// Contract states overlay
  341. pub contracts: ContractStateStoreOverlay,
  342. /// Wasm bincodes overlay
  343. pub wasm_bincode: WasmStoreOverlay,
  344. }
  345. impl BlockchainOverlay {
  346. /// Instantiate a new `BlockchainOverlay` over the given [`Blockchain`] instance.
  347. pub fn new(blockchain: &Blockchain) -> Result<BlockchainOverlayPtr> {
  348. let overlay = Arc::new(Mutex::new(sled_overlay::SledDbOverlay::new(&blockchain.sled_db)));
  349. let headers = HeaderStoreOverlay::new(&overlay)?;
  350. let blocks = BlockStoreOverlay::new(&overlay)?;
  351. let order = BlockOrderStoreOverlay::new(&overlay)?;
  352. let slots = SlotStoreOverlay::new(&overlay)?;
  353. let transactions = TxStoreOverlay::new(&overlay)?;
  354. let contracts = ContractStateStoreOverlay::new(&overlay)?;
  355. let wasm_bincode = WasmStoreOverlay::new(&overlay)?;
  356. Ok(Arc::new(Mutex::new(Self {
  357. overlay,
  358. headers,
  359. blocks,
  360. order,
  361. slots,
  362. transactions,
  363. contracts,
  364. wasm_bincode,
  365. })))
  366. }
  367. /// Check if blockchain contains any blocks
  368. pub fn is_empty(&self) -> Result<bool> {
  369. self.order.is_empty()
  370. }
  371. /// Retrieve the last block slot and hash.
  372. pub fn last(&self) -> Result<(u64, blake3::Hash)> {
  373. self.order.get_last()
  374. }
  375. /// Retrieve the last block info.
  376. pub fn last_block(&self) -> Result<BlockInfo> {
  377. let (_, hash) = self.last()?;
  378. Ok(self.get_blocks_by_hash(&[hash])?[0].clone())
  379. }
  380. /// Insert a given [`BlockInfo`] into the overlay.
  381. /// This functions wraps all the logic of separating the block into specific
  382. /// data that can be fed into the different trees of the overlay.
  383. /// Upon success, the functions returns the block hash that
  384. /// were given and appended to the overlay.
  385. /// Since we are adding to the overlay, we don't need to exeucte
  386. /// the writes atomically.
  387. pub fn add_block(&self, block: &BlockInfo) -> Result<blake3::Hash> {
  388. // Store transactions
  389. self.transactions.insert(&block.txs)?;
  390. // Store header
  391. self.headers.insert(&[block.header.clone()])?;
  392. // Store block
  393. let blk: Block = Block::from(block.clone());
  394. let block_hash = self.blocks.insert(&[blk])?[0];
  395. // Store block order
  396. self.order.insert(&[block.header.slot], &[block_hash])?;
  397. // Store slot checkpoints
  398. self.slots.insert(&block.slots)?;
  399. Ok(block_hash)
  400. }
  401. /// Check if the given [`BlockInfo`] is in the database and all trees.
  402. pub fn has_block(&self, block: &BlockInfo) -> Result<bool> {
  403. let blockhash = match self.order.get(&[block.header.slot], true) {
  404. Ok(v) => v[0].unwrap(),
  405. Err(_) => return Ok(false),
  406. };
  407. // Check if we have all transactions
  408. let txs: Vec<blake3::Hash> =
  409. block.txs.iter().map(|x| blake3::hash(&serialize(x))).collect();
  410. if self.transactions.get(&txs, true).is_err() {
  411. return Ok(false)
  412. }
  413. // Check if we have all slots
  414. let slots: Vec<u64> = block.slots.iter().map(|x| x.id).collect();
  415. if self.slots.get(&slots, true).is_err() {
  416. return Ok(false)
  417. }
  418. // Check provided info produces the same hash
  419. Ok(blockhash == block.blockhash())
  420. }
  421. /// Retrieve [`BlockInfo`]s by given hashes. Fails if any of them is not found.
  422. pub fn get_blocks_by_hash(&self, hashes: &[blake3::Hash]) -> Result<Vec<BlockInfo>> {
  423. let blocks = self.blocks.get(hashes, true)?;
  424. let blocks: Vec<Block> = blocks.iter().map(|x| x.clone().unwrap()).collect();
  425. let ret = self.get_blocks_infos(&blocks)?;
  426. Ok(ret)
  427. }
  428. /// Retrieve all [`BlockInfo`] for given slice of [`Block`].
  429. /// Fails if any of them is not found
  430. fn get_blocks_infos(&self, blocks: &[Block]) -> Result<Vec<BlockInfo>> {
  431. let mut ret = Vec::with_capacity(blocks.len());
  432. for block in blocks {
  433. let headers = self.headers.get(&[block.header], true)?;
  434. // Since we used strict get, its safe to unwrap here
  435. let header = headers[0].clone().unwrap();
  436. let txs = self.transactions.get(&block.txs, true)?;
  437. let txs = txs.iter().map(|x| x.clone().unwrap()).collect();
  438. let slots = self.slots.get(&block.slots, true)?;
  439. let slots = slots.iter().map(|x| x.clone().unwrap()).collect();
  440. let info = BlockInfo::new(header, txs, block.producer.clone(), slots);
  441. ret.push(info);
  442. }
  443. Ok(ret)
  444. }
  445. /// Checkpoint overlay so we can revert to it, if needed.
  446. pub fn checkpoint(&self) {
  447. self.overlay.lock().unwrap().checkpoint();
  448. }
  449. /// Revert to current overlay checkpoint.
  450. pub fn revert_to_checkpoint(&self) -> Result<()> {
  451. self.overlay.lock().unwrap().revert_to_checkpoint()?;
  452. Ok(())
  453. }
  454. /// Auxiliary function to create a full clone using SledDbOverlay::clone,
  455. /// generating new pointers for the underlying overlays.
  456. pub fn full_clone(&self) -> Result<BlockchainOverlayPtr> {
  457. let overlay = Arc::new(Mutex::new(self.overlay.lock().unwrap().clone()));
  458. let headers = HeaderStoreOverlay::new(&overlay)?;
  459. let blocks = BlockStoreOverlay::new(&overlay)?;
  460. let order = BlockOrderStoreOverlay::new(&overlay)?;
  461. let slots = SlotStoreOverlay::new(&overlay)?;
  462. let transactions = TxStoreOverlay::new(&overlay)?;
  463. let contracts = ContractStateStoreOverlay::new(&overlay)?;
  464. let wasm_bincode = WasmStoreOverlay::new(&overlay)?;
  465. Ok(Arc::new(Mutex::new(Self {
  466. overlay,
  467. headers,
  468. blocks,
  469. order,
  470. slots,
  471. transactions,
  472. contracts,
  473. wasm_bincode,
  474. })))
  475. }
  476. }
  477. /// Parse a sled record with a u64 keyin the form of a tuple (`key`, `value`).
  478. pub fn parse_u64_key_record<T: Decodable>(record: (sled::IVec, sled::IVec)) -> Result<(u64, T)> {
  479. let key_bytes: [u8; 8] = record.0.as_ref().try_into().unwrap();
  480. let key = u64::from_be_bytes(key_bytes);
  481. let value = deserialize(&record.1)?;
  482. Ok((key, value))
  483. }
  484. /// Parse a sled record in the form of a tuple (`key`, `value`).
  485. pub fn parse_record<T1: Decodable, T2: Decodable>(
  486. record: (sled::IVec, sled::IVec),
  487. ) -> Result<(T1, T2)> {
  488. let key = deserialize(&record.0)?;
  489. let value = deserialize(&record.1)?;
  490. Ok((key, value))
  491. }