1use std::sync::{Arc, Mutex};
20
21use darkfi_sdk::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
53#[derive(Clone)]
55pub struct Blockchain {
56 pub sled_db: sled::Db,
58 pub headers: HeaderStore,
60 pub blocks: BlockStore,
62 pub transactions: TxStore,
64 pub contracts: ContractStore,
66}
67
68impl Blockchain {
69 pub fn new(db: &sled::Db) -> Result<Self> {
71 let headers = HeaderStore::new(db)?;
72 let blocks = BlockStore::new(db)?;
73 let transactions = TxStore::new(db)?;
74 let contracts = ContractStore::new(db)?;
75
76 Ok(Self { sled_db: db.clone(), headers, blocks, transactions, contracts })
77 }
78
79 pub fn add_block(&self, block: &BlockInfo) -> Result<HeaderHash> {
85 let mut trees = vec![];
86 let mut batches = vec![];
87
88 let (headers_batch, _) = self.headers.insert_batch(&[block.header.clone()]);
90 trees.push(self.headers.main.clone());
91 batches.push(headers_batch);
92
93 let blk: Block = Block::from_block_info(block);
95 let (bocks_batch, block_hashes) = self.blocks.insert_batch(&[blk]);
96 let block_hash = block_hashes[0];
97 let block_hash_vec = [block_hash];
98 trees.push(self.blocks.main.clone());
99 batches.push(bocks_batch);
100
101 let blocks_order_batch =
103 self.blocks.insert_batch_order(&[block.header.height], &block_hash_vec);
104 trees.push(self.blocks.order.clone());
105 batches.push(blocks_order_batch);
106
107 let (txs_batch, txs_hashes) = self.transactions.insert_batch(&block.txs);
109 trees.push(self.transactions.main.clone());
110 batches.push(txs_batch);
111
112 let txs_locations_batch =
114 self.transactions.insert_batch_location(&txs_hashes, block.header.height);
115 trees.push(self.transactions.location.clone());
116 batches.push(txs_locations_batch);
117
118 self.atomic_write(&trees, &batches)?;
120
121 Ok(block_hash)
122 }
123
124 pub fn has_block(&self, block: &BlockInfo) -> Result<bool> {
126 let blockhash = match self.blocks.get_order(&[block.header.height], true) {
127 Ok(v) => v[0].unwrap(),
128 Err(_) => return Ok(false),
129 };
130
131 let txs: Vec<TransactionHash> = block.txs.iter().map(|tx| tx.hash()).collect();
133 if self.transactions.get(&txs, true).is_err() {
134 return Ok(false)
135 }
136
137 Ok(blockhash == block.hash())
139 }
140
141 pub fn get_blocks_by_hash(&self, hashes: &[HeaderHash]) -> Result<Vec<BlockInfo>> {
143 let blocks = self.blocks.get(hashes, true)?;
144 let blocks: Vec<Block> = blocks.iter().map(|x| x.clone().unwrap()).collect();
145 let ret = self.get_blocks_infos(&blocks)?;
146
147 Ok(ret)
148 }
149
150 fn get_blocks_infos(&self, blocks: &[Block]) -> Result<Vec<BlockInfo>> {
153 let mut ret = Vec::with_capacity(blocks.len());
154 for block in blocks {
155 let headers = self.headers.get(&[block.header], true)?;
156 let header = headers[0].clone().unwrap();
158
159 let txs = self.transactions.get(&block.txs, true)?;
160 let txs = txs.iter().map(|x| x.clone().unwrap()).collect();
161
162 let info = BlockInfo::new(header, txs, block.signature);
163 ret.push(info);
164 }
165
166 Ok(ret)
167 }
168
169 pub fn get_blocks_by_heights(&self, heights: &[u32]) -> Result<Vec<BlockInfo>> {
171 debug!(target: "blockchain", "get_blocks_by_heights(): {:?}", heights);
172 let blockhashes = self.blocks.get_order(heights, false)?;
173
174 let mut hashes = vec![];
175 for i in blockhashes.into_iter().flatten() {
176 hashes.push(i);
177 }
178
179 self.get_blocks_by_hash(&hashes)
180 }
181
182 pub fn get_headers_before(&self, height: u32, n: usize) -> Result<Vec<Header>> {
184 debug!(target: "blockchain", "get_headers_before(): {} -> {}", height, n);
185 let hashes = self.blocks.get_before(height, n)?;
186 let headers = self.headers.get(&hashes, true)?;
187 Ok(headers.iter().map(|h| h.clone().unwrap()).collect())
188 }
189
190 pub fn len(&self) -> usize {
192 self.blocks.len()
193 }
194
195 pub fn txs_len(&self) -> usize {
197 self.transactions.len()
198 }
199
200 pub fn is_empty(&self) -> bool {
202 self.blocks.is_empty()
203 }
204
205 pub fn genesis(&self) -> Result<(u32, HeaderHash)> {
207 self.blocks.get_first()
208 }
209
210 pub fn genesis_block(&self) -> Result<BlockInfo> {
212 let (_, hash) = self.genesis()?;
213 Ok(self.get_blocks_by_hash(&[hash])?[0].clone())
214 }
215
216 pub fn last(&self) -> Result<(u32, HeaderHash)> {
218 self.blocks.get_last()
219 }
220
221 pub fn last_block(&self) -> Result<BlockInfo> {
223 let (_, hash) = self.last()?;
224 Ok(self.get_blocks_by_hash(&[hash])?[0].clone())
225 }
226
227 pub fn last_block_difficulty(&self) -> Result<BlockDifficulty> {
230 if let Some(found) = self.blocks.get_last_difficulty()? {
231 return Ok(found)
232 }
233
234 let genesis_block = self.genesis_block()?;
235 Ok(BlockDifficulty::genesis(genesis_block.header.timestamp))
236 }
237
238 pub fn has_height(&self, height: u32) -> Result<bool> {
240 let vec = match self.blocks.get_order(&[height], true) {
241 Ok(v) => v,
242 Err(_) => return Ok(false),
243 };
244 Ok(!vec.is_empty())
245 }
246
247 pub fn add_pending_txs(&self, txs: &[Transaction]) -> Result<Vec<TransactionHash>> {
251 let (txs_batch, txs_hashes) = self.transactions.insert_batch_pending(txs);
252 let txs_order_batch = self.transactions.insert_batch_pending_order(&txs_hashes)?;
253
254 let trees = [self.transactions.pending.clone(), self.transactions.pending_order.clone()];
256 let batches = [txs_batch, txs_order_batch];
257 self.atomic_write(&trees, &batches)?;
258
259 Ok(txs_hashes)
260 }
261
262 pub fn get_pending_txs(&self) -> Result<Vec<Transaction>> {
265 let txs = self.transactions.get_all_pending()?;
266 let indexes = self.transactions.get_all_pending_order()?;
267 if txs.len() != indexes.len() {
268 return Err(Error::InvalidInputLengths)
269 }
270
271 let mut ret = Vec::with_capacity(txs.len());
272 for index in indexes {
273 ret.push(txs.get(&index.1).unwrap().clone());
274 }
275
276 Ok(ret)
277 }
278
279 pub fn remove_pending_txs(&self, txs: &[Transaction]) -> Result<()> {
281 let txs_hashes: Vec<TransactionHash> = txs.iter().map(|tx| tx.hash()).collect();
282 self.remove_pending_txs_hashes(&txs_hashes)
283 }
284
285 pub fn remove_pending_txs_hashes(&self, txs: &[TransactionHash]) -> Result<()> {
287 let indexes = self.transactions.get_all_pending_order()?;
288 let mut removed_indexes = vec![];
291 for index in indexes {
292 if txs.contains(&index.1) {
293 removed_indexes.push(index.0);
294 }
295 }
296
297 let txs_batch = self.transactions.remove_batch_pending(txs);
298 let txs_order_batch = self.transactions.remove_batch_pending_order(&removed_indexes);
299
300 let trees = [self.transactions.pending.clone(), self.transactions.pending_order.clone()];
302 let batches = [txs_batch, txs_order_batch];
303 self.atomic_write(&trees, &batches)?;
304
305 Ok(())
306 }
307
308 fn atomic_write(&self, trees: &[sled::Tree], batches: &[sled::Batch]) -> Result<()> {
310 if trees.len() != batches.len() {
311 return Err(Error::InvalidInputLengths)
312 }
313
314 trees.transaction(|trees| {
315 for (index, tree) in trees.iter().enumerate() {
316 tree.apply_batch(&batches[index])?;
317 }
318
319 Ok::<(), sled::transaction::ConflictableTransactionError<sled::Error>>(())
320 })?;
321
322 Ok(())
323 }
324
325 pub fn get_all(&self) -> Result<Vec<BlockInfo>> {
328 let order = self.blocks.get_all_order()?;
329 let order: Vec<HeaderHash> = order.iter().map(|x| x.1).collect();
330 let blocks = self.get_blocks_by_hash(&order)?;
331
332 Ok(blocks)
333 }
334
335 pub fn get_by_range(&self, start: u32, end: u32) -> Result<Vec<BlockInfo>> {
337 let blockhashes = self.blocks.get_order_by_range(start, end)?;
338 let hashes: Vec<HeaderHash> = blockhashes.into_iter().map(|(_, hash)| hash).collect();
339 self.get_blocks_by_hash(&hashes)
340 }
341
342 pub fn get_last_n(&self, n: usize) -> Result<Vec<BlockInfo>> {
344 let records = self.blocks.get_last_n_orders(n)?;
345
346 let mut last_n = vec![];
347 for record in records {
348 let header_hash = record.1;
349 let blocks = self.get_blocks_by_hash(&[header_hash])?;
350 for block in blocks {
351 last_n.push(block.clone());
352 }
353 }
354
355 Ok(last_n)
356 }
357
358 pub fn reset_to_height(&self, height: u32) -> Result<()> {
361 let (last, _) = self.last()?;
363
364 if height >= last {
366 return Ok(())
367 }
368
369 let heights: Vec<u32> = (height + 1..=last).rev().collect();
372 let inverse_diffs = self.blocks.get_state_inverse_diff(&heights, true)?;
373
374 let overlay = BlockchainOverlay::new(self)?;
376
377 let overlay_lock = overlay.lock().unwrap();
379 let mut lock = overlay_lock.overlay.lock().unwrap();
380 for inverse_diff in inverse_diffs {
381 let inverse_diff = inverse_diff.unwrap();
383 lock.add_diff(&inverse_diff)?;
384 lock.apply_diff(&inverse_diff)?;
385 self.sled_db.flush()?;
386 }
387 drop(lock);
388 drop(overlay_lock);
389
390 Ok(())
391 }
392}
393
394pub type SledDbOverlayPtr = Arc<Mutex<sled_overlay::SledDbOverlay>>;
396
397pub type BlockchainOverlayPtr = Arc<Mutex<BlockchainOverlay>>;
399
400pub struct BlockchainOverlay {
402 pub overlay: SledDbOverlayPtr,
404 pub headers: HeaderStoreOverlay,
406 pub blocks: BlockStoreOverlay,
408 pub transactions: TxStoreOverlay,
410 pub contracts: ContractStoreOverlay,
412}
413
414impl BlockchainOverlay {
415 pub fn new(blockchain: &Blockchain) -> Result<BlockchainOverlayPtr> {
417 let protected_trees = vec![
419 SLED_BLOCK_TREE,
420 SLED_BLOCK_ORDER_TREE,
421 SLED_BLOCK_DIFFICULTY_TREE,
422 SLED_BLOCK_STATE_INVERSE_DIFF_TREE,
423 SLED_HEADER_TREE,
424 SLED_SYNC_HEADER_TREE,
425 SLED_TX_TREE,
426 SLED_TX_LOCATION_TREE,
427 SLED_PENDING_TX_TREE,
428 SLED_PENDING_TX_ORDER_TREE,
429 SLED_CONTRACTS_TREE,
430 SLED_BINCODE_TREE,
431 ];
432 let overlay = Arc::new(Mutex::new(sled_overlay::SledDbOverlay::new(
433 &blockchain.sled_db,
434 protected_trees,
435 )));
436 let headers = HeaderStoreOverlay::new(&overlay)?;
437 let blocks = BlockStoreOverlay::new(&overlay)?;
438 let transactions = TxStoreOverlay::new(&overlay)?;
439 let contracts = ContractStoreOverlay::new(&overlay)?;
440
441 Ok(Arc::new(Mutex::new(Self { overlay, headers, blocks, transactions, contracts })))
442 }
443
444 pub fn is_empty(&self) -> Result<bool> {
446 self.blocks.is_empty()
447 }
448
449 pub fn last(&self) -> Result<(u32, HeaderHash)> {
451 self.blocks.get_last()
452 }
453
454 pub fn last_block(&self) -> Result<BlockInfo> {
456 let (_, hash) = self.last()?;
457 Ok(self.get_blocks_by_hash(&[hash])?[0].clone())
458 }
459
460 pub fn last_block_height(&self) -> Result<u32> {
462 Ok(self.last()?.0)
463 }
464
465 pub fn last_block_timestamp(&self) -> Result<Timestamp> {
467 let (_, hash) = self.last()?;
468 Ok(self.get_blocks_by_hash(&[hash])?[0].header.timestamp)
469 }
470
471 pub fn add_block(&self, block: &BlockInfo) -> Result<HeaderHash> {
479 self.headers.insert(&[block.header.clone()])?;
481
482 let blk: Block = Block::from_block_info(block);
484 let txs_hashes = blk.txs.clone();
485 let block_hash = self.blocks.insert(&[blk])?[0];
486 let block_hash_vec = [block_hash];
487
488 self.blocks.insert_order(&[block.header.height], &block_hash_vec)?;
490
491 self.transactions.insert(&block.txs)?;
493
494 self.transactions.insert_location(&txs_hashes, block.header.height)?;
496
497 Ok(block_hash)
498 }
499
500 pub fn has_block(&self, block: &BlockInfo) -> Result<bool> {
502 let blockhash = match self.blocks.get_order(&[block.header.height], true) {
503 Ok(v) => v[0].unwrap(),
504 Err(_) => return Ok(false),
505 };
506
507 let txs: Vec<TransactionHash> = block.txs.iter().map(|tx| tx.hash()).collect();
509 if self.transactions.get(&txs, true).is_err() {
510 return Ok(false)
511 }
512
513 Ok(blockhash == block.hash())
515 }
516
517 pub fn get_headers_by_hash(&self, hashes: &[HeaderHash]) -> Result<Vec<Header>> {
519 let headers = self.headers.get(hashes, true)?;
520 let ret: Vec<Header> = headers.iter().map(|x| x.clone().unwrap()).collect();
521
522 Ok(ret)
523 }
524
525 pub fn get_blocks_by_hash(&self, hashes: &[HeaderHash]) -> Result<Vec<BlockInfo>> {
527 let blocks = self.blocks.get(hashes, true)?;
528 let blocks: Vec<Block> = blocks.iter().map(|x| x.clone().unwrap()).collect();
529 let ret = self.get_blocks_infos(&blocks)?;
530
531 Ok(ret)
532 }
533
534 fn get_blocks_infos(&self, blocks: &[Block]) -> Result<Vec<BlockInfo>> {
537 let mut ret = Vec::with_capacity(blocks.len());
538 for block in blocks {
539 let headers = self.headers.get(&[block.header], true)?;
540 let header = headers[0].clone().unwrap();
542
543 let txs = self.transactions.get(&block.txs, true)?;
544 let txs = txs.iter().map(|x| x.clone().unwrap()).collect();
545
546 let info = BlockInfo::new(header, txs, block.signature);
547 ret.push(info);
548 }
549
550 Ok(ret)
551 }
552
553 pub fn get_blocks_txs_hashes(&self, hashes: &[HeaderHash]) -> Result<Vec<TransactionHash>> {
555 let blocks = self.blocks.get(hashes, true)?;
556 let mut ret = vec![];
557 for block in blocks {
558 ret.extend_from_slice(&block.unwrap().txs);
559 }
560
561 Ok(ret)
562 }
563
564 pub fn checkpoint(&self) {
566 self.overlay.lock().unwrap().checkpoint();
567 }
568
569 pub fn revert_to_checkpoint(&self) -> Result<()> {
571 self.overlay.lock().unwrap().revert_to_checkpoint()?;
572
573 Ok(())
574 }
575
576 pub fn full_clone(&self) -> Result<BlockchainOverlayPtr> {
579 let overlay = Arc::new(Mutex::new(self.overlay.lock().unwrap().clone()));
580 let headers = HeaderStoreOverlay::new(&overlay)?;
581 let blocks = BlockStoreOverlay::new(&overlay)?;
582 let transactions = TxStoreOverlay::new(&overlay)?;
583 let contracts = ContractStoreOverlay::new(&overlay)?;
584
585 Ok(Arc::new(Mutex::new(Self { overlay, headers, blocks, transactions, contracts })))
586 }
587}