Skip to main content

StockHeader

Struct StockHeader 

Source
pub struct StockHeader {
    pub version: u32,
    pub prev_block_hash: BlockHash,
    pub merkle_root: [u8; 32],
    pub time: u32,
    pub bits: u32,
    pub nonce: u32,
}
Expand description

The 80-byte Bitcoin block header (btc:BlockHeader in the kernel’s schema/core.jsonld).

Layout, all little-endian: version (4) ‖ prev_block_hash (32) ‖ merkle_root (32) ‖ time (4) ‖ bits (4) ‖ nonce (4). The block hash is SHA-256d of these bytes, printed byte-reversed.

Beside a stock parent siding builds every header with version = 0x2000_0000 and bit 31 clear (siding/lib/block.mjs buildBlock); the height lives only in the coinbase’s BIP 34 push, so this struct has no height field. version is held as u32 rather than Bitcoin’s i32: the two agree for every header this crate accepts, because a set bit 31 is rejected by StockHeader::decode.

use sidestr_header::{StockHeader, Target};
// sidestr:dreamlab block 0 (agentbox ADR-2103), beside tbtc4.
let bytes = hex::decode(
    "0000002000000000000000000000000000000000000000000000000000000000000000003a87d59ecf60ab58ee75948cc39d1bb44ac4285747e64b5a1e7a960d37764cb40e67b26affff7f2002000000",
).unwrap();
let h = StockHeader::decode(&bytes).unwrap();
assert_eq!(h.version, 0x2000_0000);
assert_eq!(h.time, 1_790_076_686);
assert_eq!(h.nonce, 2);
assert_eq!(h.hash().to_string(),
           "4db37517728bd509c0cb96ee5a2e3e2a77f9e965a092e9f67948b413d453dbc0");
let pow_limit = Target::from_hex("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap();
assert!(h.check_pow(&pow_limit).is_ok());
assert_eq!(h.encode().as_slice(), bytes.as_slice());

Fields§

§version: u32

Block version. Bit 31 must be clear (see the struct docs).

§prev_block_hash: BlockHash

The previous block’s hash; all zeros for a genesis.

§merkle_root: [u8; 32]

The transaction merkle root in wire (internal) order — the bytes as serialised, not as an explorer prints them.

§time: u32

Block time, Unix seconds.

§bits: u32

Compact proof-of-work target; on a sidestr chain always the compact form of powLimit.

§nonce: u32

The proof-of-work nonce.

Implementations§

Source§

impl StockHeader

Source

pub const WIRE_SIZE: usize = 80

Wire size in bytes.

Source

pub fn decode(bytes: &[u8]) -> Result<Self, Error>

Decodes exactly 80 bytes. Rejects any other length (Error::WrongLength) and a version with bit 31 set (Error::VersionBit31Set).

Source

pub fn encode(&self) -> [u8; 80]

The 80 wire bytes.

Source

pub fn hash(&self) -> BlockHash

The block hash: SHA-256d of the 80 bytes, in display order. It is also the proof-of-work hash (the kernel’s sha256d powHash).

Source

pub fn target(&self) -> Result<Target, Error>

The target bits encodes.

Source

pub fn meets_target(&self) -> bool

hash ≤ target(bits): the kernel’s checkProofOfWork. False when bits does not decode.

Source

pub fn check_pow(&self, pow_limit: &Target) -> Result<(), Error>

SPEC 4 step 1 as siding applies it: bits must be the compact form of pow_limit and the hash must meet it.

Source

pub fn signet_preimage(&self, stripped_merkle_root: [u8; 32]) -> [u8; 72]

The first 72 header bytes — version ‖ prev ‖ merkle_root ‖ time — with merkle_root replaced by stripped_merkle_root, the root over the coinbase stripped of its solution. This is what BIP-325’s block data hashes (siding/lib/block.mjs blockData).

Source

pub fn block_data(&self, stripped_merkle_root: [u8; 32]) -> [u8; 32]

The BIP-325 block data, SHA256(signet_preimage), which the block’s signature commits to (SPEC 4 step 2).

Trait Implementations§

Source§

impl Clone for StockHeader

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for StockHeader

Source§

impl Debug for StockHeader

Source§

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

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

impl Eq for StockHeader

Source§

impl PartialEq for StockHeader

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for StockHeader

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, dest: *mut u8)

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

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.