mod.rs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2025 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 darkfi_sdk::tx::TransactionHash;
  20. use log::debug;
  21. use sled_overlay::{sled, sled::Transactional};
  22. use crate::{tx::Transaction, util::time::Timestamp, Error, Result};
  23. /// Block related definitions and storage implementations
  24. pub mod block_store;
  25. pub use block_store::{
  26. Block, BlockDifficulty, BlockInfo, BlockStore, BlockStoreOverlay, SLED_BLOCK_DIFFICULTY_TREE,
  27. SLED_BLOCK_ORDER_TREE, SLED_BLOCK_STATE_DIFF_TREE, SLED_BLOCK_TREE,
  28. };
  29. /// Header definition and storage implementation
  30. pub mod header_store;
  31. pub use header_store::{
  32. Header, HeaderHash, HeaderStore, HeaderStoreOverlay, SLED_HEADER_TREE, SLED_SYNC_HEADER_TREE,
  33. };
  34. /// Transactions related storage implementations
  35. pub mod tx_store;
  36. pub use tx_store::{
  37. TxStore, TxStoreOverlay, SLED_PENDING_TX_ORDER_TREE, SLED_PENDING_TX_TREE,
  38. SLED_TX_LOCATION_TREE, SLED_TX_TREE,
  39. };
  40. /// Contracts and Wasm storage implementations
  41. pub mod contract_store;
  42. pub use contract_store::{
  43. ContractStore, ContractStoreOverlay, SLED_BINCODE_TREE, SLED_CONTRACTS_TREE,
  44. };
  45. /// Structure holding all sled trees that define the concept of Blockchain.
  46. #[derive(Clone)]
  47. pub struct Blockchain {
  48. /// Main pointer to the sled db connection
  49. pub sled_db: sled::Db,
  50. /// Headers sled tree
  51. pub headers: HeaderStore,
  52. /// Blocks sled tree
  53. pub blocks: BlockStore,
  54. /// Transactions related sled trees
  55. pub transactions: TxStore,
  56. /// Contracts related sled trees
  57. pub contracts: ContractStore,
  58. }
  59. impl Blockchain {
  60. /// Instantiate a new `Blockchain` with the given `sled` database.
  61. pub fn new(db: &sled::Db) -> Result<Self> {
  62. let headers = HeaderStore::new(db)?;
  63. let blocks = BlockStore::new(db)?;
  64. let transactions = TxStore::new(db)?;
  65. let contracts = ContractStore::new(db)?;
  66. Ok(Self { sled_db: db.clone(), headers, blocks, transactions, contracts })
  67. }
  68. /// Insert a given [`BlockInfo`] into the blockchain database.
  69. /// This functions wraps all the logic of separating the block into specific
  70. /// data that can be fed into the different trees of the database.
  71. /// Upon success, the functions returns the block hash that
  72. /// were given and appended to the ledger.
  73. pub fn add_block(&self, block: &BlockInfo) -> Result<HeaderHash> {
  74. let mut trees = vec![];
  75. let mut batches = vec![];
  76. // Store header
  77. let (headers_batch, _) = self.headers.insert_batch(&[block.header.clone()]);
  78. trees.push(self.headers.main.clone());
  79. batches.push(headers_batch);
  80. // Store block
  81. let blk: Block = Block::from_block_info(block);
  82. let (bocks_batch, block_hashes) = self.blocks.insert_batch(&[blk]);
  83. let block_hash = block_hashes[0];
  84. let block_hash_vec = [block_hash];
  85. trees.push(self.blocks.main.clone());
  86. batches.push(bocks_batch);
  87. // Store block order
  88. let blocks_order_batch =
  89. self.blocks.insert_batch_order(&[block.header.height], &block_hash_vec);
  90. trees.push(self.blocks.order.clone());
  91. batches.push(blocks_order_batch);
  92. // Store transactions
  93. let (txs_batch, txs_hashes) = self.transactions.insert_batch(&block.txs);
  94. trees.push(self.transactions.main.clone());
  95. batches.push(txs_batch);
  96. // Store transactions_locations
  97. let txs_locations_batch =
  98. self.transactions.insert_batch_location(&txs_hashes, block.header.height);
  99. trees.push(self.transactions.location.clone());
  100. batches.push(txs_locations_batch);
  101. // Perform an atomic transaction over the trees and apply the batches.
  102. self.atomic_write(&trees, &batches)?;
  103. Ok(block_hash)
  104. }
  105. /// Check if the given [`BlockInfo`] is in the database and all trees.
  106. pub fn has_block(&self, block: &BlockInfo) -> Result<bool> {
  107. let blockhash = match self.blocks.get_order(&[block.header.height], true) {
  108. Ok(v) => v[0].unwrap(),
  109. Err(_) => return Ok(false),
  110. };
  111. // Check if we have all transactions
  112. let txs: Vec<TransactionHash> = block.txs.iter().map(|tx| tx.hash()).collect();
  113. if self.transactions.get(&txs, true).is_err() {
  114. return Ok(false)
  115. }
  116. // Check provided info produces the same hash
  117. Ok(blockhash == block.hash())
  118. }
  119. /// Retrieve [`BlockInfo`]s by given hashes. Fails if any of them is not found.
  120. pub fn get_blocks_by_hash(&self, hashes: &[HeaderHash]) -> Result<Vec<BlockInfo>> {
  121. let blocks = self.blocks.get(hashes, true)?;
  122. let blocks: Vec<Block> = blocks.iter().map(|x| x.clone().unwrap()).collect();
  123. let ret = self.get_blocks_infos(&blocks)?;
  124. Ok(ret)
  125. }
  126. /// Retrieve all [`BlockInfo`] for given slice of [`Block`].
  127. /// Fails if any of them is not found
  128. fn get_blocks_infos(&self, blocks: &[Block]) -> Result<Vec<BlockInfo>> {
  129. let mut ret = Vec::with_capacity(blocks.len());
  130. for block in blocks {
  131. let headers = self.headers.get(&[block.header], true)?;
  132. // Since we used strict get, its safe to unwrap here
  133. let header = headers[0].clone().unwrap();
  134. let txs = self.transactions.get(&block.txs, true)?;
  135. let txs = txs.iter().map(|x| x.clone().unwrap()).collect();
  136. let info = BlockInfo::new(header, txs, block.signature);
  137. ret.push(info);
  138. }
  139. Ok(ret)
  140. }
  141. /// Retrieve [`BlockInfo`]s by given heights. Does not fail if any of them are not found.
  142. pub fn get_blocks_by_heights(&self, heights: &[u32]) -> Result<Vec<BlockInfo>> {
  143. debug!(target: "blockchain", "get_blocks_by_heights(): {:?}", heights);
  144. let blockhashes = self.blocks.get_order(heights, false)?;
  145. let mut hashes = vec![];
  146. for i in blockhashes.into_iter().flatten() {
  147. hashes.push(i);
  148. }
  149. self.get_blocks_by_hash(&hashes)
  150. }
  151. /// Retrieve n headers before given block height.
  152. pub fn get_headers_before(&self, height: u32, n: usize) -> Result<Vec<Header>> {
  153. debug!(target: "blockchain", "get_headers_before(): {} -> {}", height, n);
  154. let hashes = self.blocks.get_before(height, n)?;
  155. let headers = self.headers.get(&hashes, true)?;
  156. Ok(headers.iter().map(|h| h.clone().unwrap()).collect())
  157. }
  158. /// Retrieve stored blocks count
  159. pub fn len(&self) -> usize {
  160. self.blocks.len()
  161. }
  162. /// Retrieve stored txs count
  163. pub fn txs_len(&self) -> usize {
  164. self.transactions.len()
  165. }
  166. /// Check if blockchain contains any blocks
  167. pub fn is_empty(&self) -> bool {
  168. self.blocks.is_empty()
  169. }
  170. /// Retrieve genesis (first) block height and hash.
  171. pub fn genesis(&self) -> Result<(u32, HeaderHash)> {
  172. self.blocks.get_first()
  173. }
  174. /// Retrieve genesis (first) block info.
  175. pub fn genesis_block(&self) -> Result<BlockInfo> {
  176. let (_, hash) = self.genesis()?;
  177. Ok(self.get_blocks_by_hash(&[hash])?[0].clone())
  178. }
  179. /// Retrieve the last block height and hash.
  180. pub fn last(&self) -> Result<(u32, HeaderHash)> {
  181. self.blocks.get_last()
  182. }
  183. /// Retrieve the last block info.
  184. pub fn last_block(&self) -> Result<BlockInfo> {
  185. let (_, hash) = self.last()?;
  186. Ok(self.get_blocks_by_hash(&[hash])?[0].clone())
  187. }
  188. /// Retrieve the last block difficulty. If the tree is empty,
  189. /// returns `BlockDifficulty::genesis` difficulty.
  190. pub fn last_block_difficulty(&self) -> Result<BlockDifficulty> {
  191. if let Some(found) = self.blocks.get_last_difficulty()? {
  192. return Ok(found)
  193. }
  194. let genesis_block = self.genesis_block()?;
  195. Ok(BlockDifficulty::genesis(genesis_block.header.timestamp))
  196. }
  197. /// Check if block order for the given height is in the database.
  198. pub fn has_height(&self, height: u32) -> Result<bool> {
  199. let vec = match self.blocks.get_order(&[height], true) {
  200. Ok(v) => v,
  201. Err(_) => return Ok(false),
  202. };
  203. Ok(!vec.is_empty())
  204. }
  205. /// Insert a given slice of pending transactions into the blockchain database.
  206. /// On success, the function returns the transaction hashes in the same order
  207. /// as the input transactions.
  208. pub fn add_pending_txs(&self, txs: &[Transaction]) -> Result<Vec<TransactionHash>> {
  209. let (txs_batch, txs_hashes) = self.transactions.insert_batch_pending(txs);
  210. let txs_order_batch = self.transactions.insert_batch_pending_order(&txs_hashes)?;
  211. // Perform an atomic transaction over the trees and apply the batches.
  212. let trees = [self.transactions.pending.clone(), self.transactions.pending_order.clone()];
  213. let batches = [txs_batch, txs_order_batch];
  214. self.atomic_write(&trees, &batches)?;
  215. Ok(txs_hashes)
  216. }
  217. /// Retrieve all transactions from the pending tx store.
  218. /// Be careful as this will try to load everything in memory.
  219. pub fn get_pending_txs(&self) -> Result<Vec<Transaction>> {
  220. let txs = self.transactions.get_all_pending()?;
  221. let indexes = self.transactions.get_all_pending_order()?;
  222. if txs.len() != indexes.len() {
  223. return Err(Error::InvalidInputLengths)
  224. }
  225. let mut ret = Vec::with_capacity(txs.len());
  226. for index in indexes {
  227. ret.push(txs.get(&index.1).unwrap().clone());
  228. }
  229. Ok(ret)
  230. }
  231. /// Remove a given slice of pending transactions from the blockchain database.
  232. pub fn remove_pending_txs(&self, txs: &[Transaction]) -> Result<()> {
  233. let txs_hashes: Vec<TransactionHash> = txs.iter().map(|tx| tx.hash()).collect();
  234. self.remove_pending_txs_hashes(&txs_hashes)
  235. }
  236. /// Remove a given slice of pending transactions hashes from the blockchain database.
  237. pub fn remove_pending_txs_hashes(&self, txs: &[TransactionHash]) -> Result<()> {
  238. let indexes = self.transactions.get_all_pending_order()?;
  239. // We could do indexes.iter().map(|x| txs.contains(x.1)).collect.map(|x| x.0).collect
  240. // but this is faster since we don't do the second iteration
  241. let mut removed_indexes = vec![];
  242. for index in indexes {
  243. if txs.contains(&index.1) {
  244. removed_indexes.push(index.0);
  245. }
  246. }
  247. let txs_batch = self.transactions.remove_batch_pending(txs);
  248. let txs_order_batch = self.transactions.remove_batch_pending_order(&removed_indexes);
  249. // Perform an atomic transaction over the trees and apply the batches.
  250. let trees = [self.transactions.pending.clone(), self.transactions.pending_order.clone()];
  251. let batches = [txs_batch, txs_order_batch];
  252. self.atomic_write(&trees, &batches)?;
  253. Ok(())
  254. }
  255. /// Auxiliary function to write to multiple trees completely atomic.
  256. fn atomic_write(&self, trees: &[sled::Tree], batches: &[sled::Batch]) -> Result<()> {
  257. if trees.len() != batches.len() {
  258. return Err(Error::InvalidInputLengths)
  259. }
  260. trees.transaction(|trees| {
  261. for (index, tree) in trees.iter().enumerate() {
  262. tree.apply_batch(&batches[index])?;
  263. }
  264. Ok::<(), sled::transaction::ConflictableTransactionError<sled::Error>>(())
  265. })?;
  266. Ok(())
  267. }
  268. /// Retrieve all blocks contained in the blockchain in order.
  269. /// Be careful as this will try to load everything in memory.
  270. pub fn get_all(&self) -> Result<Vec<BlockInfo>> {
  271. let order = self.blocks.get_all_order()?;
  272. let order: Vec<HeaderHash> = order.iter().map(|x| x.1).collect();
  273. let blocks = self.get_blocks_by_hash(&order)?;
  274. Ok(blocks)
  275. }
  276. /// Retrieve [`BlockInfo`]s by given heights range.
  277. pub fn get_by_range(&self, start: u32, end: u32) -> Result<Vec<BlockInfo>> {
  278. let blockhashes = self.blocks.get_order_by_range(start, end)?;
  279. let hashes: Vec<HeaderHash> = blockhashes.into_iter().map(|(_, hash)| hash).collect();
  280. self.get_blocks_by_hash(&hashes)
  281. }
  282. /// Retrieve last 'N' [`BlockInfo`]s from the blockchain.
  283. pub fn get_last_n(&self, n: usize) -> Result<Vec<BlockInfo>> {
  284. let records = self.blocks.get_last_n_orders(n)?;
  285. let mut last_n = vec![];
  286. for record in records {
  287. let header_hash = record.1;
  288. let blocks = self.get_blocks_by_hash(&[header_hash])?;
  289. for block in blocks {
  290. last_n.push(block.clone());
  291. }
  292. }
  293. Ok(last_n)
  294. }
  295. /// Auxiliary function to reset the blockchain and consensus state
  296. /// to the provided block height.
  297. pub fn reset_to_height(&self, height: u32) -> Result<()> {
  298. // First we grab the last block height
  299. let (last, _) = self.last()?;
  300. // Check if request height is after our last height
  301. if height >= last {
  302. return Ok(())
  303. }
  304. // Grab all state diffs until requested height going backwards
  305. let heights: Vec<u32> = (height + 1..=last).rev().collect();
  306. let diffs = self.blocks.get_state_diff(&heights, true)?;
  307. // Create an overlay to apply the reverse diffs
  308. let overlay = BlockchainOverlay::new(self)?;
  309. // Apply the inverse diffs sequence
  310. let overlay_lock = overlay.lock().unwrap();
  311. let mut lock = overlay_lock.overlay.lock().unwrap();
  312. for diff in diffs {
  313. // Since we used strict retrieval it's safe to unwrap here
  314. let inverse_diff = diff.unwrap().inverse();
  315. lock.add_diff(&inverse_diff)?;
  316. lock.apply_diff(&inverse_diff)?;
  317. self.sled_db.flush()?;
  318. }
  319. drop(lock);
  320. drop(overlay_lock);
  321. Ok(())
  322. }
  323. }
  324. /// Atomic pointer to sled db overlay.
  325. pub type SledDbOverlayPtr = Arc<Mutex<sled_overlay::SledDbOverlay>>;
  326. /// Atomic pointer to blockchain overlay.
  327. pub type BlockchainOverlayPtr = Arc<Mutex<BlockchainOverlay>>;
  328. /// Overlay structure over a [`Blockchain`] instance.
  329. pub struct BlockchainOverlay {
  330. /// Main [`sled_overlay::SledDbOverlay`] to the sled db connection
  331. pub overlay: SledDbOverlayPtr,
  332. /// Headers overlay
  333. pub headers: HeaderStoreOverlay,
  334. /// Blocks overlay
  335. pub blocks: BlockStoreOverlay,
  336. /// Transactions overlay
  337. pub transactions: TxStoreOverlay,
  338. /// Contract overlay
  339. pub contracts: ContractStoreOverlay,
  340. }
  341. impl BlockchainOverlay {
  342. /// Instantiate a new `BlockchainOverlay` over the given [`Blockchain`] instance.
  343. pub fn new(blockchain: &Blockchain) -> Result<BlockchainOverlayPtr> {
  344. // Here we configure all our blockchain sled trees to be protected in the overlay
  345. let protected_trees = vec![
  346. SLED_BLOCK_TREE,
  347. SLED_BLOCK_ORDER_TREE,
  348. SLED_BLOCK_DIFFICULTY_TREE,
  349. SLED_BLOCK_STATE_DIFF_TREE,
  350. SLED_HEADER_TREE,
  351. SLED_SYNC_HEADER_TREE,
  352. SLED_TX_TREE,
  353. SLED_TX_LOCATION_TREE,
  354. SLED_PENDING_TX_TREE,
  355. SLED_PENDING_TX_ORDER_TREE,
  356. SLED_CONTRACTS_TREE,
  357. SLED_BINCODE_TREE,
  358. ];
  359. let overlay = Arc::new(Mutex::new(sled_overlay::SledDbOverlay::new(
  360. &blockchain.sled_db,
  361. protected_trees,
  362. )));
  363. let headers = HeaderStoreOverlay::new(&overlay)?;
  364. let blocks = BlockStoreOverlay::new(&overlay)?;
  365. let transactions = TxStoreOverlay::new(&overlay)?;
  366. let contracts = ContractStoreOverlay::new(&overlay)?;
  367. Ok(Arc::new(Mutex::new(Self { overlay, headers, blocks, transactions, contracts })))
  368. }
  369. /// Check if blockchain contains any blocks
  370. pub fn is_empty(&self) -> Result<bool> {
  371. self.blocks.is_empty()
  372. }
  373. /// Retrieve the last block height and hash.
  374. pub fn last(&self) -> Result<(u32, HeaderHash)> {
  375. self.blocks.get_last()
  376. }
  377. /// Retrieve the last block info.
  378. pub fn last_block(&self) -> Result<BlockInfo> {
  379. let (_, hash) = self.last()?;
  380. Ok(self.get_blocks_by_hash(&[hash])?[0].clone())
  381. }
  382. /// Retrieve the last block height.
  383. pub fn last_block_height(&self) -> Result<u32> {
  384. Ok(self.last()?.0)
  385. }
  386. /// Retrieve the last block timestamp.
  387. pub fn last_block_timestamp(&self) -> Result<Timestamp> {
  388. let (_, hash) = self.last()?;
  389. Ok(self.get_blocks_by_hash(&[hash])?[0].header.timestamp)
  390. }
  391. /// Insert a given [`BlockInfo`] into the overlay.
  392. /// This functions wraps all the logic of separating the block into specific
  393. /// data that can be fed into the different trees of the overlay.
  394. /// Upon success, the functions returns the block hash that
  395. /// were given and appended to the overlay.
  396. /// Since we are adding to the overlay, we don't need to exeucte
  397. /// the writes atomically.
  398. pub fn add_block(&self, block: &BlockInfo) -> Result<HeaderHash> {
  399. // Store header
  400. self.headers.insert(&[block.header.clone()])?;
  401. // Store block
  402. let blk: Block = Block::from_block_info(block);
  403. let txs_hashes = blk.txs.clone();
  404. let block_hash = self.blocks.insert(&[blk])?[0];
  405. let block_hash_vec = [block_hash];
  406. // Store block order
  407. self.blocks.insert_order(&[block.header.height], &block_hash_vec)?;
  408. // Store transactions
  409. self.transactions.insert(&block.txs)?;
  410. // Store transactions locations
  411. self.transactions.insert_location(&txs_hashes, block.header.height)?;
  412. Ok(block_hash)
  413. }
  414. /// Check if the given [`BlockInfo`] is in the database and all trees.
  415. pub fn has_block(&self, block: &BlockInfo) -> Result<bool> {
  416. let blockhash = match self.blocks.get_order(&[block.header.height], true) {
  417. Ok(v) => v[0].unwrap(),
  418. Err(_) => return Ok(false),
  419. };
  420. // Check if we have all transactions
  421. let txs: Vec<TransactionHash> = block.txs.iter().map(|tx| tx.hash()).collect();
  422. if self.transactions.get(&txs, true).is_err() {
  423. return Ok(false)
  424. }
  425. // Check provided info produces the same hash
  426. Ok(blockhash == block.hash())
  427. }
  428. /// Retrieve [`Header`]s by given hashes. Fails if any of them is not found.
  429. pub fn get_headers_by_hash(&self, hashes: &[HeaderHash]) -> Result<Vec<Header>> {
  430. let headers = self.headers.get(hashes, true)?;
  431. let ret: Vec<Header> = headers.iter().map(|x| x.clone().unwrap()).collect();
  432. Ok(ret)
  433. }
  434. /// Retrieve [`BlockInfo`]s by given hashes. Fails if any of them is not found.
  435. pub fn get_blocks_by_hash(&self, hashes: &[HeaderHash]) -> Result<Vec<BlockInfo>> {
  436. let blocks = self.blocks.get(hashes, true)?;
  437. let blocks: Vec<Block> = blocks.iter().map(|x| x.clone().unwrap()).collect();
  438. let ret = self.get_blocks_infos(&blocks)?;
  439. Ok(ret)
  440. }
  441. /// Retrieve all [`BlockInfo`] for given slice of [`Block`].
  442. /// Fails if any of them is not found
  443. fn get_blocks_infos(&self, blocks: &[Block]) -> Result<Vec<BlockInfo>> {
  444. let mut ret = Vec::with_capacity(blocks.len());
  445. for block in blocks {
  446. let headers = self.headers.get(&[block.header], true)?;
  447. // Since we used strict get, its safe to unwrap here
  448. let header = headers[0].clone().unwrap();
  449. let txs = self.transactions.get(&block.txs, true)?;
  450. let txs = txs.iter().map(|x| x.clone().unwrap()).collect();
  451. let info = BlockInfo::new(header, txs, block.signature);
  452. ret.push(info);
  453. }
  454. Ok(ret)
  455. }
  456. /// Retrieve [`Block`]s by given hashes and return their transactions hashes.
  457. pub fn get_blocks_txs_hashes(&self, hashes: &[HeaderHash]) -> Result<Vec<TransactionHash>> {
  458. let blocks = self.blocks.get(hashes, true)?;
  459. let mut ret = vec![];
  460. for block in blocks {
  461. ret.extend_from_slice(&block.unwrap().txs);
  462. }
  463. Ok(ret)
  464. }
  465. /// Checkpoint overlay so we can revert to it, if needed.
  466. pub fn checkpoint(&self) {
  467. self.overlay.lock().unwrap().checkpoint();
  468. }
  469. /// Revert to current overlay checkpoint.
  470. pub fn revert_to_checkpoint(&self) -> Result<()> {
  471. self.overlay.lock().unwrap().revert_to_checkpoint()?;
  472. Ok(())
  473. }
  474. /// Auxiliary function to create a full clone using SledDbOverlay::clone,
  475. /// generating new pointers for the underlying overlays.
  476. pub fn full_clone(&self) -> Result<BlockchainOverlayPtr> {
  477. let overlay = Arc::new(Mutex::new(self.overlay.lock().unwrap().clone()));
  478. let headers = HeaderStoreOverlay::new(&overlay)?;
  479. let blocks = BlockStoreOverlay::new(&overlay)?;
  480. let transactions = TxStoreOverlay::new(&overlay)?;
  481. let contracts = ContractStoreOverlay::new(&overlay)?;
  482. Ok(Arc::new(Mutex::new(Self { overlay, headers, blocks, transactions, contracts })))
  483. }
  484. }