use std::sync::{Arc, Mutex};
use darkfi_sdk::tx::TransactionHash;
use log::debug;
use sled_overlay::{sled, sled::Transactional};
use crate::{tx::Transaction, util::time::Timestamp, Error, Result};
pub mod block_store;
pub use block_store::{
Block, BlockDifficulty, BlockInfo, BlockStore, BlockStoreOverlay, SLED_BLOCK_DIFFICULTY_TREE,
SLED_BLOCK_ORDER_TREE, SLED_BLOCK_STATE_DIFF_TREE, SLED_BLOCK_TREE,
};
pub mod header_store;
pub use header_store::{
Header, HeaderHash, HeaderStore, HeaderStoreOverlay, SLED_HEADER_TREE, SLED_SYNC_HEADER_TREE,
};
pub mod tx_store;
pub use tx_store::{
TxStore, TxStoreOverlay, SLED_PENDING_TX_ORDER_TREE, SLED_PENDING_TX_TREE,
SLED_TX_LOCATION_TREE, SLED_TX_TREE,
};
pub mod contract_store;
pub use contract_store::{
ContractStore, ContractStoreOverlay, SLED_BINCODE_TREE, SLED_CONTRACTS_TREE,
};
#[derive(Clone)]
pub struct Blockchain {
pub sled_db: sled::Db,
pub headers: HeaderStore,
pub blocks: BlockStore,
pub transactions: TxStore,
pub contracts: ContractStore,
}
impl Blockchain {
pub fn new(db: &sled::Db) -> Result<Self> {
let headers = HeaderStore::new(db)?;
let blocks = BlockStore::new(db)?;
let transactions = TxStore::new(db)?;
let contracts = ContractStore::new(db)?;
Ok(Self { sled_db: db.clone(), headers, blocks, transactions, contracts })
}
pub fn add_block(&self, block: &BlockInfo) -> Result<HeaderHash> {
let mut trees = vec![];
let mut batches = vec![];
let (headers_batch, _) = self.headers.insert_batch(&[block.header.clone()]);
trees.push(self.headers.main.clone());
batches.push(headers_batch);
let blk: Block = Block::from_block_info(block);
let (bocks_batch, block_hashes) = self.blocks.insert_batch(&[blk]);
let block_hash = block_hashes[0];
let block_hash_vec = [block_hash];
trees.push(self.blocks.main.clone());
batches.push(bocks_batch);
let blocks_order_batch =
self.blocks.insert_batch_order(&[block.header.height], &block_hash_vec);
trees.push(self.blocks.order.clone());
batches.push(blocks_order_batch);
let (txs_batch, txs_hashes) = self.transactions.insert_batch(&block.txs);
trees.push(self.transactions.main.clone());
batches.push(txs_batch);
let txs_locations_batch =
self.transactions.insert_batch_location(&txs_hashes, block.header.height);
trees.push(self.transactions.location.clone());
batches.push(txs_locations_batch);
self.atomic_write(&trees, &batches)?;
Ok(block_hash)
}
pub fn has_block(&self, block: &BlockInfo) -> Result<bool> {
let blockhash = match self.blocks.get_order(&[block.header.height], true) {
Ok(v) => v[0].unwrap(),
Err(_) => return Ok(false),
};
let txs: Vec<TransactionHash> = block.txs.iter().map(|tx| tx.hash()).collect();
if self.transactions.get(&txs, true).is_err() {
return Ok(false)
}
Ok(blockhash == block.hash())
}
pub fn get_blocks_by_hash(&self, hashes: &[HeaderHash]) -> Result<Vec<BlockInfo>> {
let blocks = self.blocks.get(hashes, true)?;
let blocks: Vec<Block> = blocks.iter().map(|x| x.clone().unwrap()).collect();
let ret = self.get_blocks_infos(&blocks)?;
Ok(ret)
}
fn get_blocks_infos(&self, blocks: &[Block]) -> Result<Vec<BlockInfo>> {
let mut ret = Vec::with_capacity(blocks.len());
for block in blocks {
let headers = self.headers.get(&[block.header], true)?;
let header = headers[0].clone().unwrap();
let txs = self.transactions.get(&block.txs, true)?;
let txs = txs.iter().map(|x| x.clone().unwrap()).collect();
let info = BlockInfo::new(header, txs, block.signature);
ret.push(info);
}
Ok(ret)
}
pub fn get_blocks_by_heights(&self, heights: &[u32]) -> Result<Vec<BlockInfo>> {
debug!(target: "blockchain", "get_blocks_by_heights(): {:?}", heights);
let blockhashes = self.blocks.get_order(heights, false)?;
let mut hashes = vec![];
for i in blockhashes.into_iter().flatten() {
hashes.push(i);
}
self.get_blocks_by_hash(&hashes)
}
pub fn get_headers_before(&self, height: u32, n: usize) -> Result<Vec<Header>> {
debug!(target: "blockchain", "get_headers_before(): {} -> {}", height, n);
let hashes = self.blocks.get_before(height, n)?;
let headers = self.headers.get(&hashes, true)?;
Ok(headers.iter().map(|h| h.clone().unwrap()).collect())
}
pub fn len(&self) -> usize {
self.blocks.len()
}
pub fn txs_len(&self) -> usize {
self.transactions.len()
}
pub fn is_empty(&self) -> bool {
self.blocks.is_empty()
}
pub fn genesis(&self) -> Result<(u32, HeaderHash)> {
self.blocks.get_first()
}
pub fn genesis_block(&self) -> Result<BlockInfo> {
let (_, hash) = self.genesis()?;
Ok(self.get_blocks_by_hash(&[hash])?[0].clone())
}
pub fn last(&self) -> Result<(u32, HeaderHash)> {
self.blocks.get_last()
}
pub fn last_block(&self) -> Result<BlockInfo> {
let (_, hash) = self.last()?;
Ok(self.get_blocks_by_hash(&[hash])?[0].clone())
}
pub fn last_block_difficulty(&self) -> Result<BlockDifficulty> {
if let Some(found) = self.blocks.get_last_difficulty()? {
return Ok(found)
}
let genesis_block = self.genesis_block()?;
Ok(BlockDifficulty::genesis(genesis_block.header.timestamp))
}
pub fn has_height(&self, height: u32) -> Result<bool> {
let vec = match self.blocks.get_order(&[height], true) {
Ok(v) => v,
Err(_) => return Ok(false),
};
Ok(!vec.is_empty())
}
pub fn add_pending_txs(&self, txs: &[Transaction]) -> Result<Vec<TransactionHash>> {
let (txs_batch, txs_hashes) = self.transactions.insert_batch_pending(txs);
let txs_order_batch = self.transactions.insert_batch_pending_order(&txs_hashes)?;
let trees = [self.transactions.pending.clone(), self.transactions.pending_order.clone()];
let batches = [txs_batch, txs_order_batch];
self.atomic_write(&trees, &batches)?;
Ok(txs_hashes)
}
pub fn get_pending_txs(&self) -> Result<Vec<Transaction>> {
let txs = self.transactions.get_all_pending()?;
let indexes = self.transactions.get_all_pending_order()?;
if txs.len() != indexes.len() {
return Err(Error::InvalidInputLengths)
}
let mut ret = Vec::with_capacity(txs.len());
for index in indexes {
ret.push(txs.get(&index.1).unwrap().clone());
}
Ok(ret)
}
pub fn remove_pending_txs(&self, txs: &[Transaction]) -> Result<()> {
let txs_hashes: Vec<TransactionHash> = txs.iter().map(|tx| tx.hash()).collect();
self.remove_pending_txs_hashes(&txs_hashes)
}
pub fn remove_pending_txs_hashes(&self, txs: &[TransactionHash]) -> Result<()> {
let indexes = self.transactions.get_all_pending_order()?;
let mut removed_indexes = vec![];
for index in indexes {
if txs.contains(&index.1) {
removed_indexes.push(index.0);
}
}
let txs_batch = self.transactions.remove_batch_pending(txs);
let txs_order_batch = self.transactions.remove_batch_pending_order(&removed_indexes);
let trees = [self.transactions.pending.clone(), self.transactions.pending_order.clone()];
let batches = [txs_batch, txs_order_batch];
self.atomic_write(&trees, &batches)?;
Ok(())
}
fn atomic_write(&self, trees: &[sled::Tree], batches: &[sled::Batch]) -> Result<()> {
if trees.len() != batches.len() {
return Err(Error::InvalidInputLengths)
}
trees.transaction(|trees| {
for (index, tree) in trees.iter().enumerate() {
tree.apply_batch(&batches[index])?;
}
Ok::<(), sled::transaction::ConflictableTransactionError<sled::Error>>(())
})?;
Ok(())
}
pub fn get_all(&self) -> Result<Vec<BlockInfo>> {
let order = self.blocks.get_all_order()?;
let order: Vec<HeaderHash> = order.iter().map(|x| x.1).collect();
let blocks = self.get_blocks_by_hash(&order)?;
Ok(blocks)
}
pub fn get_by_range(&self, start: u32, end: u32) -> Result<Vec<BlockInfo>> {
let blockhashes = self.blocks.get_order_by_range(start, end)?;
let hashes: Vec<HeaderHash> = blockhashes.into_iter().map(|(_, hash)| hash).collect();
self.get_blocks_by_hash(&hashes)
}
pub fn get_last_n(&self, n: usize) -> Result<Vec<BlockInfo>> {
let records = self.blocks.get_last_n_orders(n)?;
let mut last_n = vec![];
for record in records {
let header_hash = record.1;
let blocks = self.get_blocks_by_hash(&[header_hash])?;
for block in blocks {
last_n.push(block.clone());
}
}
Ok(last_n)
}
pub fn reset_to_height(&self, height: u32) -> Result<()> {
let (last, _) = self.last()?;
if height >= last {
return Ok(())
}
let heights: Vec<u32> = (height + 1..=last).rev().collect();
let diffs = self.blocks.get_state_diff(&heights, true)?;
let overlay = BlockchainOverlay::new(self)?;
let overlay_lock = overlay.lock().unwrap();
let mut lock = overlay_lock.overlay.lock().unwrap();
for diff in diffs {
let inverse_diff = diff.unwrap().inverse();
lock.add_diff(&inverse_diff)?;
lock.apply_diff(&inverse_diff)?;
self.sled_db.flush()?;
}
drop(lock);
drop(overlay_lock);
Ok(())
}
}
pub type SledDbOverlayPtr = Arc<Mutex<sled_overlay::SledDbOverlay>>;
pub type BlockchainOverlayPtr = Arc<Mutex<BlockchainOverlay>>;
pub struct BlockchainOverlay {
pub overlay: SledDbOverlayPtr,
pub headers: HeaderStoreOverlay,
pub blocks: BlockStoreOverlay,
pub transactions: TxStoreOverlay,
pub contracts: ContractStoreOverlay,
}
impl BlockchainOverlay {
pub fn new(blockchain: &Blockchain) -> Result<BlockchainOverlayPtr> {
let protected_trees = vec![
SLED_BLOCK_TREE,
SLED_BLOCK_ORDER_TREE,
SLED_BLOCK_DIFFICULTY_TREE,
SLED_BLOCK_STATE_DIFF_TREE,
SLED_HEADER_TREE,
SLED_SYNC_HEADER_TREE,
SLED_TX_TREE,
SLED_TX_LOCATION_TREE,
SLED_PENDING_TX_TREE,
SLED_PENDING_TX_ORDER_TREE,
SLED_CONTRACTS_TREE,
SLED_BINCODE_TREE,
];
let overlay = Arc::new(Mutex::new(sled_overlay::SledDbOverlay::new(
&blockchain.sled_db,
protected_trees,
)));
let headers = HeaderStoreOverlay::new(&overlay)?;
let blocks = BlockStoreOverlay::new(&overlay)?;
let transactions = TxStoreOverlay::new(&overlay)?;
let contracts = ContractStoreOverlay::new(&overlay)?;
Ok(Arc::new(Mutex::new(Self { overlay, headers, blocks, transactions, contracts })))
}
pub fn is_empty(&self) -> Result<bool> {
self.blocks.is_empty()
}
pub fn last(&self) -> Result<(u32, HeaderHash)> {
self.blocks.get_last()
}
pub fn last_block(&self) -> Result<BlockInfo> {
let (_, hash) = self.last()?;
Ok(self.get_blocks_by_hash(&[hash])?[0].clone())
}
pub fn last_block_height(&self) -> Result<u32> {
Ok(self.last()?.0)
}
pub fn last_block_timestamp(&self) -> Result<Timestamp> {
let (_, hash) = self.last()?;
Ok(self.get_blocks_by_hash(&[hash])?[0].header.timestamp)
}
pub fn add_block(&self, block: &BlockInfo) -> Result<HeaderHash> {
self.headers.insert(&[block.header.clone()])?;
let blk: Block = Block::from_block_info(block);
let txs_hashes = blk.txs.clone();
let block_hash = self.blocks.insert(&[blk])?[0];
let block_hash_vec = [block_hash];
self.blocks.insert_order(&[block.header.height], &block_hash_vec)?;
self.transactions.insert(&block.txs)?;
self.transactions.insert_location(&txs_hashes, block.header.height)?;
Ok(block_hash)
}
pub fn has_block(&self, block: &BlockInfo) -> Result<bool> {
let blockhash = match self.blocks.get_order(&[block.header.height], true) {
Ok(v) => v[0].unwrap(),
Err(_) => return Ok(false),
};
let txs: Vec<TransactionHash> = block.txs.iter().map(|tx| tx.hash()).collect();
if self.transactions.get(&txs, true).is_err() {
return Ok(false)
}
Ok(blockhash == block.hash())
}
pub fn get_headers_by_hash(&self, hashes: &[HeaderHash]) -> Result<Vec<Header>> {
let headers = self.headers.get(hashes, true)?;
let ret: Vec<Header> = headers.iter().map(|x| x.clone().unwrap()).collect();
Ok(ret)
}
pub fn get_blocks_by_hash(&self, hashes: &[HeaderHash]) -> Result<Vec<BlockInfo>> {
let blocks = self.blocks.get(hashes, true)?;
let blocks: Vec<Block> = blocks.iter().map(|x| x.clone().unwrap()).collect();
let ret = self.get_blocks_infos(&blocks)?;
Ok(ret)
}
fn get_blocks_infos(&self, blocks: &[Block]) -> Result<Vec<BlockInfo>> {
let mut ret = Vec::with_capacity(blocks.len());
for block in blocks {
let headers = self.headers.get(&[block.header], true)?;
let header = headers[0].clone().unwrap();
let txs = self.transactions.get(&block.txs, true)?;
let txs = txs.iter().map(|x| x.clone().unwrap()).collect();
let info = BlockInfo::new(header, txs, block.signature);
ret.push(info);
}
Ok(ret)
}
pub fn get_blocks_txs_hashes(&self, hashes: &[HeaderHash]) -> Result<Vec<TransactionHash>> {
let blocks = self.blocks.get(hashes, true)?;
let mut ret = vec![];
for block in blocks {
ret.extend_from_slice(&block.unwrap().txs);
}
Ok(ret)
}
pub fn checkpoint(&self) {
self.overlay.lock().unwrap().checkpoint();
}
pub fn revert_to_checkpoint(&self) -> Result<()> {
self.overlay.lock().unwrap().revert_to_checkpoint()?;
Ok(())
}
pub fn full_clone(&self) -> Result<BlockchainOverlayPtr> {
let overlay = Arc::new(Mutex::new(self.overlay.lock().unwrap().clone()));
let headers = HeaderStoreOverlay::new(&overlay)?;
let blocks = BlockStoreOverlay::new(&overlay)?;
let transactions = TxStoreOverlay::new(&overlay)?;
let contracts = ContractStoreOverlay::new(&overlay)?;
Ok(Arc::new(Mutex::new(Self { overlay, headers, blocks, transactions, contracts })))
}
}