1use std::sync::{Arc, Mutex};
20
21use darkfi_sdk::{monotree::Monotree, tx::TransactionHash};
22use log::debug;
23use sled_overlay::{sled, sled::Transactional};
24
25use crate::{tx::Transaction, util::time::Timestamp, Error, Result};
26
27pub mod block_store;
29pub use block_store::{
30 Block, BlockDifficulty, BlockInfo, BlockStore, BlockStoreOverlay, SLED_BLOCK_DIFFICULTY_TREE,
31 SLED_BLOCK_ORDER_TREE, SLED_BLOCK_STATE_INVERSE_DIFF_TREE, SLED_BLOCK_TREE,
32};
33
34pub mod header_store;
36pub use header_store::{
37 Header, HeaderHash, HeaderStore, HeaderStoreOverlay, SLED_HEADER_TREE, SLED_SYNC_HEADER_TREE,
38};
39
40pub mod tx_store;
42pub use tx_store::{
43 TxStore, TxStoreOverlay, SLED_PENDING_TX_ORDER_TREE, SLED_PENDING_TX_TREE,
44 SLED_TX_LOCATION_TREE, SLED_TX_TREE,
45};
46
47pub mod contract_store;
49pub use contract_store::{
50 ContractStore, ContractStoreOverlay, SLED_BINCODE_TREE, SLED_CONTRACTS_TREE,
51};
52
53pub mod monero;
55
56#[derive(Clone)]
58pub struct Blockchain {
59 pub sled_db: sled::Db,
61 pub headers: HeaderStore,
63 pub blocks: BlockStore,
65 pub transactions: TxStore,
67 pub contracts: ContractStore,
69}
70
71impl Blockchain {
72 pub fn new(db: &sled::Db) -> Result<Self> {
74 let headers = HeaderStore::new(db)?;
75 let blocks = BlockStore::new(db)?;
76 let transactions = TxStore::new(db)?;
77 let contracts = ContractStore::new(db)?;
78
79 Ok(Self { sled_db: db.clone(), headers, blocks, transactions, contracts })
80 }
81
82 pub fn add_block(&self, block: &BlockInfo) -> Result<HeaderHash> {
88 let mut trees = vec![];
89 let mut batches = vec![];
90
91 let (headers_batch, _) = self.headers.insert_batch(&[block.header.clone()]);
93 trees.push(self.headers.main.clone());
94 batches.push(headers_batch);
95
96 let blk: Block = Block::from_block_info(block);
98 let (bocks_batch, block_hashes) = self.blocks.insert_batch(&[blk]);
99 let block_hash = block_hashes[0];
100 let block_hash_vec = [block_hash];
101 trees.push(self.blocks.main.clone());
102 batches.push(bocks_batch);
103
104 let blocks_order_batch =
106 self.blocks.insert_batch_order(&[block.header.height], &block_hash_vec);
107 trees.push(self.blocks.order.clone());
108 batches.push(blocks_order_batch);
109
110 let (txs_batch, txs_hashes) = self.transactions.insert_batch(&block.txs);
112 trees.push(self.transactions.main.clone());
113 batches.push(txs_batch);
114
115 let txs_locations_batch =
117 self.transactions.insert_batch_location(&txs_hashes, block.header.height);
118 trees.push(self.transactions.location.clone());
119 batches.push(txs_locations_batch);
120
121 self.atomic_write(&trees, &batches)?;
123
124 Ok(block_hash)
125 }
126
127 pub fn has_block(&self, block: &BlockInfo) -> Result<bool> {
129 let blockhash = match self.blocks.get_order(&[block.header.height], true) {
130 Ok(v) => v[0].unwrap(),
131 Err(_) => return Ok(false),
132 };
133
134 let txs: Vec<TransactionHash> = block.txs.iter().map(|tx| tx.hash()).collect();
136 if self.transactions.get(&txs, true).is_err() {
137 return Ok(false)
138 }
139
140 Ok(blockhash == block.hash())
142 }
143
144 pub fn get_blocks_by_hash(&self, hashes: &[HeaderHash]) -> Result<Vec<BlockInfo>> {
146 let blocks = self.blocks.get(hashes, true)?;
147 let blocks: Vec<Block> = blocks.iter().map(|x| x.clone().unwrap()).collect();
148 let ret = self.get_blocks_infos(&blocks)?;
149
150 Ok(ret)
151 }
152
153 fn get_blocks_infos(&self, blocks: &[Block]) -> Result<Vec<BlockInfo>> {
156 let mut ret = Vec::with_capacity(blocks.len());
157 for block in blocks {
158 let headers = self.headers.get(&[block.header], true)?;
159 let header = headers[0].clone().unwrap();
161
162 let txs = self.transactions.get(&block.txs, true)?;
163 let txs = txs.iter().map(|x| x.clone().unwrap()).collect();
164
165 let info = BlockInfo::new(header, txs, block.signature);
166 ret.push(info);
167 }
168
169 Ok(ret)
170 }
171
172 pub fn get_blocks_by_heights(&self, heights: &[u32]) -> Result<Vec<BlockInfo>> {
174 debug!(target: "blockchain", "get_blocks_by_heights(): {heights:?}");
175 let blockhashes = self.blocks.get_order(heights, false)?;
176
177 let mut hashes = vec![];
178 for i in blockhashes.into_iter().flatten() {
179 hashes.push(i);
180 }
181
182 self.get_blocks_by_hash(&hashes)
183 }
184
185 pub fn get_headers_before(&self, height: u32, n: usize) -> Result<Vec<Header>> {
187 debug!(target: "blockchain", "get_headers_before(): {height} -> {n}");
188 let hashes = self.blocks.get_before(height, n)?;
189 let headers = self.headers.get(&hashes, true)?;
190 Ok(headers.iter().map(|h| h.clone().unwrap()).collect())
191 }
192
193 pub fn len(&self) -> usize {
195 self.blocks.len()
196 }
197
198 pub fn txs_len(&self) -> usize {
200 self.transactions.len()
201 }
202
203 pub fn is_empty(&self) -> bool {
205 self.blocks.is_empty()
206 }
207
208 pub fn genesis(&self) -> Result<(u32, HeaderHash)> {
210 self.blocks.get_first()
211 }
212
213 pub fn genesis_block(&self) -> Result<BlockInfo> {
215 let (_, hash) = self.genesis()?;
216 Ok(self.get_blocks_by_hash(&[hash])?[0].clone())
217 }
218
219 pub fn last(&self) -> Result<(u32, HeaderHash)> {
221 self.blocks.get_last()
222 }
223
224 pub fn last_header(&self) -> Result<Header> {
226 let (_, hash) = self.last()?;
227 Ok(self.headers.get(&[hash], true)?[0].clone().unwrap())
228 }
229
230 pub fn last_block(&self) -> Result<BlockInfo> {
232 let (_, hash) = self.last()?;
233 Ok(self.get_blocks_by_hash(&[hash])?[0].clone())
234 }
235
236 pub fn last_block_difficulty(&self) -> Result<BlockDifficulty> {
239 if let Some(found) = self.blocks.get_last_difficulty()? {
240 return Ok(found)
241 }
242
243 let genesis_block = self.genesis_block()?;
244 Ok(BlockDifficulty::genesis(genesis_block.header.timestamp))
245 }
246
247 pub fn has_height(&self, height: u32) -> Result<bool> {
249 let vec = match self.blocks.get_order(&[height], true) {
250 Ok(v) => v,
251 Err(_) => return Ok(false),
252 };
253 Ok(!vec.is_empty())
254 }
255
256 pub fn add_pending_txs(&self, txs: &[Transaction]) -> Result<Vec<TransactionHash>> {
260 let (txs_batch, txs_hashes) = self.transactions.insert_batch_pending(txs);
261 let txs_order_batch = self.transactions.insert_batch_pending_order(&txs_hashes)?;
262
263 let trees = [self.transactions.pending.clone(), self.transactions.pending_order.clone()];
265 let batches = [txs_batch, txs_order_batch];
266 self.atomic_write(&trees, &batches)?;
267
268 Ok(txs_hashes)
269 }
270
271 pub fn get_pending_txs(&self) -> Result<Vec<Transaction>> {
274 let txs = self.transactions.get_all_pending()?;
275 let indexes = self.transactions.get_all_pending_order()?;
276 if txs.len() != indexes.len() {
277 return Err(Error::InvalidInputLengths)
278 }
279
280 let mut ret = Vec::with_capacity(txs.len());
281 for index in indexes {
282 ret.push(txs.get(&index.1).unwrap().clone());
283 }
284
285 Ok(ret)
286 }
287
288 pub fn remove_pending_txs(&self, txs: &[Transaction]) -> Result<()> {
290 let txs_hashes: Vec<TransactionHash> = txs.iter().map(|tx| tx.hash()).collect();
291 self.remove_pending_txs_hashes(&txs_hashes)
292 }
293
294 pub fn remove_pending_txs_hashes(&self, txs: &[TransactionHash]) -> Result<()> {
296 let indexes = self.transactions.get_all_pending_order()?;
297 let mut removed_indexes = vec![];
300 for index in indexes {
301 if txs.contains(&index.1) {
302 removed_indexes.push(index.0);
303 }
304 }
305
306 let txs_batch = self.transactions.remove_batch_pending(txs);
307 let txs_order_batch = self.transactions.remove_batch_pending_order(&removed_indexes);
308
309 let trees = [self.transactions.pending.clone(), self.transactions.pending_order.clone()];
311 let batches = [txs_batch, txs_order_batch];
312 self.atomic_write(&trees, &batches)?;
313
314 Ok(())
315 }
316
317 fn atomic_write(&self, trees: &[sled::Tree], batches: &[sled::Batch]) -> Result<()> {
319 if trees.len() != batches.len() {
320 return Err(Error::InvalidInputLengths)
321 }
322
323 trees.transaction(|trees| {
324 for (index, tree) in trees.iter().enumerate() {
325 tree.apply_batch(&batches[index])?;
326 }
327
328 Ok::<(), sled::transaction::ConflictableTransactionError<sled::Error>>(())
329 })?;
330
331 Ok(())
332 }
333
334 pub fn get_all(&self) -> Result<Vec<BlockInfo>> {
337 let order = self.blocks.get_all_order()?;
338 let order: Vec<HeaderHash> = order.iter().map(|x| x.1).collect();
339 let blocks = self.get_blocks_by_hash(&order)?;
340
341 Ok(blocks)
342 }
343
344 pub fn get_by_range(&self, start: u32, end: u32) -> Result<Vec<BlockInfo>> {
346 let blockhashes = self.blocks.get_order_by_range(start, end)?;
347 let hashes: Vec<HeaderHash> = blockhashes.into_iter().map(|(_, hash)| hash).collect();
348 self.get_blocks_by_hash(&hashes)
349 }
350
351 pub fn get_last_n(&self, n: usize) -> Result<Vec<BlockInfo>> {
353 let records = self.blocks.get_last_n_orders(n)?;
354
355 let mut last_n = vec![];
356 for record in records {
357 let header_hash = record.1;
358 let blocks = self.get_blocks_by_hash(&[header_hash])?;
359 for block in blocks {
360 last_n.push(block.clone());
361 }
362 }
363
364 Ok(last_n)
365 }
366
367 pub fn reset_to_height(&self, height: u32) -> Result<()> {
370 let (last, _) = self.last()?;
372
373 if height >= last {
375 return Ok(())
376 }
377
378 let heights: Vec<u32> = (height + 1..=last).rev().collect();
381 let inverse_diffs = self.blocks.get_state_inverse_diff(&heights, true)?;
382
383 let overlay = BlockchainOverlay::new(self)?;
385
386 let overlay_lock = overlay.lock().unwrap();
388 let mut lock = overlay_lock.overlay.lock().unwrap();
389 for inverse_diff in inverse_diffs {
390 let inverse_diff = inverse_diff.unwrap();
392 lock.add_diff(&inverse_diff)?;
393 lock.apply_diff(&inverse_diff)?;
394 self.sled_db.flush()?;
395 }
396 drop(lock);
397 drop(overlay_lock);
398
399 Ok(())
400 }
401
402 pub fn get_state_monotree(&self) -> Result<Monotree> {
407 self.contracts.get_state_monotree(&self.sled_db)
408 }
409}
410
411pub type SledDbOverlayPtr = Arc<Mutex<sled_overlay::SledDbOverlay>>;
413
414pub type BlockchainOverlayPtr = Arc<Mutex<BlockchainOverlay>>;
416
417pub struct BlockchainOverlay {
419 pub overlay: SledDbOverlayPtr,
421 pub headers: HeaderStoreOverlay,
423 pub blocks: BlockStoreOverlay,
425 pub transactions: TxStoreOverlay,
427 pub contracts: ContractStoreOverlay,
429}
430
431impl BlockchainOverlay {
432 pub fn new(blockchain: &Blockchain) -> Result<BlockchainOverlayPtr> {
434 let protected_trees = vec![
436 SLED_BLOCK_TREE,
437 SLED_BLOCK_ORDER_TREE,
438 SLED_BLOCK_DIFFICULTY_TREE,
439 SLED_BLOCK_STATE_INVERSE_DIFF_TREE,
440 SLED_HEADER_TREE,
441 SLED_SYNC_HEADER_TREE,
442 SLED_TX_TREE,
443 SLED_TX_LOCATION_TREE,
444 SLED_PENDING_TX_TREE,
445 SLED_PENDING_TX_ORDER_TREE,
446 SLED_CONTRACTS_TREE,
447 SLED_BINCODE_TREE,
448 ];
449 let overlay = Arc::new(Mutex::new(sled_overlay::SledDbOverlay::new(
450 &blockchain.sled_db,
451 protected_trees,
452 )));
453 let headers = HeaderStoreOverlay::new(&overlay)?;
454 let blocks = BlockStoreOverlay::new(&overlay)?;
455 let transactions = TxStoreOverlay::new(&overlay)?;
456 let contracts = ContractStoreOverlay::new(&overlay)?;
457
458 Ok(Arc::new(Mutex::new(Self { overlay, headers, blocks, transactions, contracts })))
459 }
460
461 pub fn is_empty(&self) -> Result<bool> {
463 self.blocks.is_empty()
464 }
465
466 pub fn last(&self) -> Result<(u32, HeaderHash)> {
468 self.blocks.get_last()
469 }
470
471 pub fn last_block(&self) -> Result<BlockInfo> {
473 let (_, hash) = self.last()?;
474 Ok(self.get_blocks_by_hash(&[hash])?[0].clone())
475 }
476
477 pub fn last_block_height(&self) -> Result<u32> {
479 Ok(self.last()?.0)
480 }
481
482 pub fn last_block_timestamp(&self) -> Result<Timestamp> {
484 let (_, hash) = self.last()?;
485 Ok(self.get_blocks_by_hash(&[hash])?[0].header.timestamp)
486 }
487
488 pub fn add_block(&self, block: &BlockInfo) -> Result<HeaderHash> {
496 self.headers.insert(&[block.header.clone()])?;
498
499 let blk: Block = Block::from_block_info(block);
501 let txs_hashes = blk.txs.clone();
502 let block_hash = self.blocks.insert(&[blk])?[0];
503 let block_hash_vec = [block_hash];
504
505 self.blocks.insert_order(&[block.header.height], &block_hash_vec)?;
507
508 self.transactions.insert(&block.txs)?;
510
511 self.transactions.insert_location(&txs_hashes, block.header.height)?;
513
514 Ok(block_hash)
515 }
516
517 pub fn has_block(&self, block: &BlockInfo) -> Result<bool> {
519 let blockhash = match self.blocks.get_order(&[block.header.height], true) {
520 Ok(v) => v[0].unwrap(),
521 Err(_) => return Ok(false),
522 };
523
524 let txs: Vec<TransactionHash> = block.txs.iter().map(|tx| tx.hash()).collect();
526 if self.transactions.get(&txs, true).is_err() {
527 return Ok(false)
528 }
529
530 Ok(blockhash == block.hash())
532 }
533
534 pub fn get_headers_by_hash(&self, hashes: &[HeaderHash]) -> Result<Vec<Header>> {
536 let headers = self.headers.get(hashes, true)?;
537 let ret: Vec<Header> = headers.iter().map(|x| x.clone().unwrap()).collect();
538
539 Ok(ret)
540 }
541
542 pub fn get_blocks_by_hash(&self, hashes: &[HeaderHash]) -> Result<Vec<BlockInfo>> {
544 let blocks = self.blocks.get(hashes, true)?;
545 let blocks: Vec<Block> = blocks.iter().map(|x| x.clone().unwrap()).collect();
546 let ret = self.get_blocks_infos(&blocks)?;
547
548 Ok(ret)
549 }
550
551 fn get_blocks_infos(&self, blocks: &[Block]) -> Result<Vec<BlockInfo>> {
554 let mut ret = Vec::with_capacity(blocks.len());
555 for block in blocks {
556 let headers = self.headers.get(&[block.header], true)?;
557 let header = headers[0].clone().unwrap();
559
560 let txs = self.transactions.get(&block.txs, true)?;
561 let txs = txs.iter().map(|x| x.clone().unwrap()).collect();
562
563 let info = BlockInfo::new(header, txs, block.signature);
564 ret.push(info);
565 }
566
567 Ok(ret)
568 }
569
570 pub fn get_blocks_txs_hashes(&self, hashes: &[HeaderHash]) -> Result<Vec<TransactionHash>> {
572 let blocks = self.blocks.get(hashes, true)?;
573 let mut ret = vec![];
574 for block in blocks {
575 ret.extend_from_slice(&block.unwrap().txs);
576 }
577
578 Ok(ret)
579 }
580
581 pub fn checkpoint(&self) {
583 self.overlay.lock().unwrap().checkpoint();
584 }
585
586 pub fn revert_to_checkpoint(&self) -> Result<()> {
588 self.overlay.lock().unwrap().revert_to_checkpoint()?;
589
590 Ok(())
591 }
592
593 pub fn full_clone(&self) -> Result<BlockchainOverlayPtr> {
596 let overlay = Arc::new(Mutex::new(self.overlay.lock().unwrap().clone()));
597 let headers = HeaderStoreOverlay::new(&overlay)?;
598 let blocks = BlockStoreOverlay::new(&overlay)?;
599 let transactions = TxStoreOverlay::new(&overlay)?;
600 let contracts = ContractStoreOverlay::new(&overlay)?;
601
602 Ok(Arc::new(Mutex::new(Self { overlay, headers, blocks, transactions, contracts })))
603 }
604
605 pub fn get_state_monotree(&self) -> Result<Monotree> {
612 self.full_clone()?.lock().unwrap().contracts.get_state_monotree()
613 }
614}