From 65e4c06f39b11020e6078eb45c6ef6b3792fb33d Mon Sep 17 00:00:00 2001 From: anton Date: Mon, 3 Aug 2026 08:12:46 +0300 Subject: [PATCH] Add a version field to block header --- app/app.rs | 7 +++++-- lib/state/block.rs | 24 +++++++++++++++++------- lib/state/error.rs | 4 +++- lib/state/two_way_peg_data.rs | 6 ++++-- lib/types/mod.rs | 2 ++ 5 files changed, 31 insertions(+), 12 deletions(-) diff --git a/app/app.rs b/app/app.rs index 5b4b6998..8b2f003c 100644 --- a/app/app.rs +++ b/app/app.rs @@ -7,8 +7,9 @@ use plain_bitassets::{ miner::{self, Miner}, node::{self, Node}, types::{ - self, Address, AmountOverflowError, BitcoinOutputContent, Body, - FilledOutput, OutPoint, Output, Transaction, + self, Address, AmountOverflowError, BLOCK_HEADER_VERSION, + BitcoinOutputContent, Body, FilledOutput, OutPoint, Output, + Transaction, proto::mainchain::{ self, generated::{validator_service_server, wallet_service_server}, @@ -439,6 +440,7 @@ impl App { coinbase, ); let header = types::Header { + version: BLOCK_HEADER_VERSION, merkle_root, prev_side_hash, prev_main_hash, @@ -456,6 +458,7 @@ impl App { let merkle_root = Body::compute_merkle_root(&coinbase, &[]); let body = Body::new(Vec::new(), coinbase); let header = types::Header { + version: BLOCK_HEADER_VERSION, merkle_root, prev_side_hash, prev_main_hash, diff --git a/lib/state/block.rs b/lib/state/block.rs index 2c378591..59de1b0e 100644 --- a/lib/state/block.rs +++ b/lib/state/block.rs @@ -6,10 +6,10 @@ use sneed::{RoTxn, RwTxn}; use crate::{ state::{Error, PrevalidatedBlock, State, amm, dutch_auction, error}, types::{ - AmountOverflowError, Authorization, BitAssetId, Body, FilledOutput, - FilledOutputContent, GetAddress as _, GetBitcoinValue as _, Header, - InPoint, OutPoint, OutPointKey, OutputContent, SpentOutput, TxData, - Verify as _, + AmountOverflowError, Authorization, BLOCK_HEADER_VERSION, BitAssetId, + Body, FilledOutput, FilledOutputContent, GetAddress as _, + GetBitcoinValue as _, Header, InPoint, OutPoint, OutPointKey, + OutputContent, SpentOutput, TxData, Verify as _, }, }; @@ -45,6 +45,8 @@ pub fn validate( let err = Error::InvalidBody { expected: header.merkle_root, computed: merkle_root, + local_header_version: BLOCK_HEADER_VERSION, + block_header_version: header.version, }; return Err(err); } @@ -124,6 +126,8 @@ pub fn prevalidate( let err = Error::InvalidBody { expected: header.merkle_root, computed: computed_merkle_root, + local_header_version: BLOCK_HEADER_VERSION, + block_header_version: header.version, }; return Err(err); } @@ -412,6 +416,8 @@ pub fn connect( let err = Error::InvalidBody { expected: merkle_root, computed: header.merkle_root, + local_header_version: BLOCK_HEADER_VERSION, + block_header_version: header.version, }; return Err(err); } @@ -574,6 +580,8 @@ pub fn disconnect_tip( let err = Error::InvalidBody { expected: header.merkle_root, computed: merkle_root, + local_header_version: BLOCK_HEADER_VERSION, + block_header_version: header.version, }; return Err(err); } @@ -726,14 +734,16 @@ mod test { test::fresh_state, }, types::{ - BitAssetData, BitAssetDataUpdates, BitAssetId, BlockHash, Body, - FilledOutput, FilledOutputContent, Hash, Header, OutPoint, - OutPointKey, Output, OutputContent, Transaction, TxData, Update, + BLOCK_HEADER_VERSION, BitAssetData, BitAssetDataUpdates, + BitAssetId, BlockHash, Body, FilledOutput, FilledOutputContent, + Hash, Header, OutPoint, OutPointKey, Output, OutputContent, + Transaction, TxData, Update, }, }; fn header(prev_side_hash: Option, body: &Body) -> Header { Header { + version: BLOCK_HEADER_VERSION, merkle_root: Body::compute_merkle_root( &body.coinbase, &body.transactions, diff --git a/lib/state/error.rs b/lib/state/error.rs index 02f4b64c..0e053384 100644 --- a/lib/state/error.rs +++ b/lib/state/error.rs @@ -136,7 +136,7 @@ pub mod dutch_auction { #[error("Invalid tx; Final price cannot be greater than initial price")] FinalPrice, #[error( - "Invalid tx; For a single-block auction, + "Invalid tx; For a single-block auction, final price must be exactly equal to initial price" )] PriceMismatch, @@ -374,6 +374,8 @@ pub enum Error { InvalidBody { expected: MerkleRoot, computed: MerkleRoot, + local_header_version: u8, + block_header_version: u8, }, #[error("invalid header: {0}")] InvalidHeader(InvalidHeader), diff --git a/lib/state/two_way_peg_data.rs b/lib/state/two_way_peg_data.rs index 72cdc5ab..9cf7a54d 100644 --- a/lib/state/two_way_peg_data.rs +++ b/lib/state/two_way_peg_data.rs @@ -1015,8 +1015,8 @@ mod test { }, }, types::{ - Address, FilledOutput, FilledOutputContent, InPoint, M6id, - OutPoint, OutPointKey, Txid, WithdrawalBundle, + Address, BLOCK_HEADER_VERSION, FilledOutput, FilledOutputContent, + InPoint, M6id, OutPoint, OutPointKey, Txid, WithdrawalBundle, WithdrawalBundleEvent, WithdrawalBundleEventStatus, WithdrawalBundleStatus, WithdrawalOutputContent, proto::mainchain::{BlockEvent, BlockInfo, Deposit, TwoWayPegData}, @@ -1269,6 +1269,7 @@ mod test { let main1 = bitcoin::BlockHash::from_byte_array([11; 32]); let genesis = Header { + version: BLOCK_HEADER_VERSION, merkle_root, prev_side_hash: None, prev_main_hash: main0, @@ -1284,6 +1285,7 @@ mod test { } let block1 = Header { + version: BLOCK_HEADER_VERSION, merkle_root, prev_side_hash: Some(genesis.hash()), prev_main_hash: main1, diff --git a/lib/types/mod.rs b/lib/types/mod.rs index d53e024b..9c60272d 100644 --- a/lib/types/mod.rs +++ b/lib/types/mod.rs @@ -38,6 +38,7 @@ pub use transaction::{ }; pub const THIS_SIDECHAIN: u8 = 4; +pub const BLOCK_HEADER_VERSION: u8 = 0; #[derive(Debug, Error)] #[error("Bitcoin amount overflow")] @@ -158,6 +159,7 @@ where ToSchema, )] pub struct Header { + pub version: u8, pub merkle_root: MerkleRoot, pub prev_side_hash: Option, #[borsh(serialize_with = "borsh_serialize_bitcoin_block_hash")]