Struct Consensus

Source
pub struct Consensus {
    pub parameters: ChainParams,
}
Expand description

This struct contains all the information and methods needed to validate a block, it is used by the ChainState to validate blocks and transactions.

Fields§

§parameters: ChainParams

The parameters of the chain we are validating, it is usually hardcoded constants. See ChainParams for more information.

Implementations§

Source§

impl Consensus

Source

pub fn get_subsidy(&self, height: u32) -> u64

Returns the amount of block subsidy to be paid in a block, given it’s height.

The Bitcoin Core source can be found here.

Source

pub fn verify_block_transactions( height: u32, utxos: HashMap<OutPoint, UtxoData>, transactions: &[Transaction], subsidy: u64, verify_script: bool, flags: c_uint, ) -> Result<(), BlockchainError>

Verify if all transactions in a block are valid. Here we check the following:

  • The block must contain at least one transaction, and this transaction must be coinbase
  • The first transaction in the block must be coinbase
  • The coinbase transaction must have the correct value (subsidy + fees)
  • The block must not create more coins than allowed
  • All transactions must be valid, as verified by Consensus::verify_transaction
Source

pub fn verify_transaction( transaction: &Transaction, utxos: &mut HashMap<OutPoint, UtxoData>, height: u32, _verify_script: bool, _flags: c_uint, ) -> Result<(Amount, Amount), BlockchainError>

Verifies a single, non-coinbase transaction. To verify (the structure of) a coinbase transaction, use Consensus::verify_coinbase.

This function checks that the transaction:

  • Has at least one input and one output
  • Doesn’t have null PrevOuts (reserved only for coinbase transactions)
  • Doesn’t spend more coins than it claims in the inputs
  • Doesn’t “move” more coins than allowed (at most 21 million)
  • Spends mature coins, in case any input refers to a coinbase transaction
  • Has valid scripts (if we don’t assume them), and within the allowed size
Source

fn verify_input_scripts( transaction: &Transaction, utxos: &mut HashMap<OutPoint, UtxoData>, flags: c_uint, ) -> Result<(), BlockchainError>

Source

pub fn check_transaction_context_free( transaction: &Transaction, ) -> Result<Amount, BlockchainError>

Performs consensus checks that are independent of the spent outputs (non-coinbase only). Returns the total output value as an [Amount].

Source

pub fn check_block( &self, block: &Block, height: u32, ) -> Result<Vec<Txid>, BlockchainError>

Runs inexpensive, consensus-critical block checks that don’t require script execution. If successful, returns the list of [Txid]s computed for the merkle root check.

This verifies:

  • the header merkle root matches the block’s txids
  • BIP34 coinbase-encoded height once activated (at bip34_height)
  • if there are SegWit transactions, the witness commitment is present and correct
  • total block weight is within the 4,000,000 WU limit
Source

pub fn check_merkle_root(block: &Block) -> Option<Vec<Txid>>

Checks if the merkle root of the header matches the merkle root of the transaction list.

Unlike [Block::check_merkle_root], this function returns the list of computed [Txid]s if the merkle roots matched, or None otherwise.

The merkle root is computed in the same way as [Block::compute_merkle_root].

Source

fn get_utxo<'a, F: Fn() -> Txid>( input: &TxIn, utxos: &'a HashMap<OutPoint, UtxoData>, txid: F, ) -> Result<&'a UtxoData, TransactionError>

Returns the TxOut being spent by the given input.

Fails if the UTXO is not present in the given hashmap.

Source

fn validate_locktime( input: &TxIn, transaction: &Transaction, height: u32, ) -> Result<(), BlockValidationErrors>

Source

fn validate_script_size<F: Fn() -> Txid>( script: &ScriptBuf, txid: F, ) -> Result<(), TransactionError>

Validates the script size and the number of sigops in a scriptpubkey or scriptsig.

Source

pub fn verify_coinbase(tx: &Transaction) -> Result<(), TransactionError>

Validates the coinbase transaction’s input. The checks on the outputs require context about the block and are performed by Consensus::verify_block_transactions.

Source

pub fn get_bip34_height(block: &Block) -> Option<u32>

Source

pub fn check_bip94_time( block: &BlockHeader, prev_block: &BlockHeader, ) -> Result<(), BlockValidationErrors>

Checks if a testnet4 block is compliant with the anti-timewarp rules of BIP94.

a. The block’s nTime field MUST be greater than or equal to the nTime field of the immediately prior block minus 600 seconds

Source

pub fn calc_next_work_required( last_block: &BlockHeader, first_block: &BlockHeader, params: ChainParams, ) -> Target

Calculates the next target for the proof of work algorithm, given the first and last block headers inside a difficulty adjustment period.

Source

pub fn update_acc( acc: &Stump, block: &Block, height: u32, proof: Proof, del_hashes: Vec<Hash>, ) -> Result<Stump, BlockchainError>

Updates our accumulator with the new block. This is done by calculating the new root hash of the accumulator, and then verifying the proof of inclusion of the deleted nodes. If the proof is valid, we return the new accumulator. Otherwise, we return an error. This function is pure, it doesn’t modify the accumulator, but returns a new one.

Source

fn contains_unspendable_utxo(del_hashes: &[Hash]) -> bool

Trait Implementations§

Source§

impl Clone for Consensus

Source§

fn clone(&self) -> Consensus

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Consensus

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<Network> for Consensus

Source§

fn from(network: Network) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromRef<T> for T
where T: Clone,

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more