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: ChainParamsThe parameters of the chain we are validating, it is usually hardcoded constants. See ChainParams for more information.
Implementations§
Source§impl Consensus
impl Consensus
Sourcepub fn get_subsidy(&self, height: u32) -> u64
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.
Sourcepub fn verify_block_transactions(
height: u32,
utxos: HashMap<OutPoint, UtxoData>,
transactions: &[Transaction],
subsidy: u64,
verify_script: bool,
flags: c_uint,
) -> Result<(), BlockchainError>
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
Sourcepub fn verify_transaction(
transaction: &Transaction,
utxos: &mut HashMap<OutPoint, UtxoData>,
height: u32,
_verify_script: bool,
_flags: c_uint,
) -> Result<(u64, u64), BlockchainError>
pub fn verify_transaction( transaction: &Transaction, utxos: &mut HashMap<OutPoint, UtxoData>, height: u32, _verify_script: bool, _flags: c_uint, ) -> Result<(u64, u64), 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
Sourcepub fn verify_coinbase(tx: &Transaction) -> Result<(), TransactionError>
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.
Sourcepub fn check_bip94_time(
block: &BlockHeader,
prev_block: &BlockHeader,
) -> Result<(), BlockValidationErrors>
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
Sourcepub fn calc_next_work_required(
last_block: &BlockHeader,
first_block: &BlockHeader,
params: ChainParams,
) -> Target
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.
Sourcepub fn update_acc(
acc: &Stump,
block: &Block,
height: u32,
proof: Proof,
del_hashes: Vec<Hash>,
) -> Result<Stump, BlockchainError>
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.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Consensus
impl RefUnwindSafe for Consensus
impl Send for Consensus
impl Sync for Consensus
impl Unpin for Consensus
impl UnwindSafe for Consensus
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more