Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create partial chainstates from a full chainstate #127

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions crates/floresta-chain/src/pruned_utreexo/chain_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use alloc::fmt::format;
use alloc::string::ToString;
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::cell::UnsafeCell;
#[cfg(feature = "bitcoinconsensus")]
use core::ffi::c_uint;

Expand Down Expand Up @@ -41,6 +42,8 @@ use super::chainstore::KvChainStore;
use super::consensus::Consensus;
use super::error::BlockValidationErrors;
use super::error::BlockchainError;
use super::partial_chain::PartialChainState;
use super::partial_chain::PartialChainStateInner;
use super::BlockchainInterface;
use super::ChainStore;
use super::UpdatableChainstate;
Expand Down Expand Up @@ -107,6 +110,7 @@ impl<PersistedState: ChainStore> ChainState<PersistedState> {
_ => {}
}
}

/// Just adds headers to the chainstate, without validating them.
pub fn push_headers(
&self,
Expand Down Expand Up @@ -1033,6 +1037,32 @@ impl<PersistedState: ChainStore> UpdatableChainstate for ChainState<PersistedSta
let inner = read_lock!(self);
inner.acc.roots.clone()
}

fn get_partial_chain(
&self,
initial_height: u32,
final_height: u32,
acc: Stump,
) -> Result<super::partial_chain::PartialChainState, BlockchainError> {
let blocks = (initial_height..=final_height)
.map(|height| self.get_block_header_by_height(height))
.collect();

let inner = PartialChainStateInner {
error: None,
blocks,
consensus: Consensus {
parameters: self.chain_params(),
},
current_acc: acc,
final_height,
assume_valid: false,
initial_height,
current_height: initial_height,
};

Ok(PartialChainState(UnsafeCell::new(inner)))
}
}

impl<T: ChainStore> From<ChainStateBuilder<T>> for ChainState<T> {
Expand Down
5 changes: 5 additions & 0 deletions crates/floresta-chain/src/pruned_utreexo/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ pub enum BlockValidationErrors {
BlockExtendsAnOrphanChain,
BadBip34,
InvalidProof,
CoinbaseNotMatured,
}

impl Display for BlockValidationErrors {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Expand Down Expand Up @@ -67,6 +69,9 @@ impl Display for BlockValidationErrors {
}
BlockValidationErrors::BadBip34 => write!(f, "BIP34 commitment mismatch"),
BlockValidationErrors::InvalidProof => write!(f, "Invalid proof"),
BlockValidationErrors::CoinbaseNotMatured => {
write!(f, "Coinbase not matured yet")
}
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions crates/floresta-chain/src/pruned_utreexo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use bitcoin::Transaction;
use bitcoin::TxOut;
use rustreexo::accumulator::node_hash::NodeHash;
use rustreexo::accumulator::proof::Proof;
use rustreexo::accumulator::stump::Stump;

use self::partial_chain::PartialChainState;
use crate::prelude::*;
use crate::BestChain;
use crate::BlockConsumer;
Expand Down Expand Up @@ -102,6 +104,23 @@ pub trait UpdatableChainstate {
fn process_rescan_block(&self, block: &Block) -> Result<(), BlockchainError>;
/// Returns the root hashes of our utreexo forest
fn get_root_hashes(&self) -> Vec<NodeHash>;
/// Returns a partial chainstate from a range of blocks.
///
/// [PartialChainState] is a simplified version of `ChainState` that is used during IBD.
/// It doesn't suport reorgs, only hold headers for a subset of blocks and isn't [Sync].
/// The idea here is that you take a OS thread or some async task that will drive one
/// [PartialChainState] to completion by downloading blocks inside that chainstate's range.
/// If all goes right, it'll end without error, and you should mark blocks in this range as
/// valid.
///
/// Since this chainstate may start from a height with an existing UTXO set, you need to
/// provide a [Stump] for that block.
fn get_partial_chain(
&self,
initial_height: u32,
final_height: u32,
acc: Stump,
) -> Result<PartialChainState, BlockchainError>;
}

/// [ChainStore] is a trait defining how we interact with our chain database. This definitions
Expand Down
Loading
Loading