Merge rust-bitcoin/rust-bitcoin#1826: Move weight constants in the `Weight` type

fc7c251502 Move weight constants in the `Weight` type (Riccardo Casatta)

Pull request description:

  deprecate constants::MAX_BLOCK_WEIGHT and constants::MIN_TRANSACTION_WEIGHT to nicely redirect users to the constants in the Weight type

ACKs for top commit:
  Kixunil:
    ACK fc7c251502
  apoelstra:
    ACK fc7c251502

Tree-SHA512: 4072688671a1471a87845afa842351db96c321a48cb33ab67bf1ff92ec3914bbb910bfb43be562ea3920416fa038967f81f180d51fc1ade6801cce0c1977a2a7
This commit is contained in:
Andrew Poelstra 2023-05-01 17:25:43 +00:00
commit 5160a0ab25
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
3 changed files with 19 additions and 2 deletions

View File

@ -32,10 +32,15 @@ pub const TARGET_BLOCK_SPACING: u32 = 600;
pub const DIFFCHANGE_INTERVAL: u32 = 2016; pub const DIFFCHANGE_INTERVAL: u32 = 2016;
/// How much time on average should occur between diffchanges. /// How much time on average should occur between diffchanges.
pub const DIFFCHANGE_TIMESPAN: u32 = 14 * 24 * 3600; pub const DIFFCHANGE_TIMESPAN: u32 = 14 * 24 * 3600;
#[deprecated(since = "0.31.0", note = "Use Weight::MAX_BLOCK instead")]
/// The maximum allowed weight for a block, see BIP 141 (network rule). /// The maximum allowed weight for a block, see BIP 141 (network rule).
pub const MAX_BLOCK_WEIGHT: u32 = 4_000_000; pub const MAX_BLOCK_WEIGHT: u32 = 4_000_000;
#[deprecated(since = "0.31.0", note = "Use Weight::MIN_TRANSACTION instead")]
/// The minimum transaction weight for a valid serialized transaction. /// The minimum transaction weight for a valid serialized transaction.
pub const MIN_TRANSACTION_WEIGHT: u32 = 4 * 60; pub const MIN_TRANSACTION_WEIGHT: u32 = 4 * 60;
/// The factor that non-witness serialization data is multiplied by during weight calculation. /// The factor that non-witness serialization data is multiplied by during weight calculation.
pub const WITNESS_SCALE_FACTOR: usize = 4; pub const WITNESS_SCALE_FACTOR: usize = 4;
/// The maximum allowed number of signature check operations in a block. /// The maximum allowed number of signature check operations in a block.

View File

@ -29,6 +29,12 @@ impl Weight {
/// Maximum possible value. /// Maximum possible value.
pub const MAX: Weight = Weight(u64::max_value()); pub const MAX: Weight = Weight(u64::max_value());
/// The maximum allowed weight for a block, see BIP 141 (network rule).
pub const MAX_BLOCK: Weight = Weight(4_000_000);
/// The minimum transaction weight for a valid serialized transaction.
pub const MIN_TRANSACTION: Weight = Weight(4 * 60);
/// Directly constructs `Weight` from weight units. /// Directly constructs `Weight` from weight units.
pub const fn from_wu(wu: u64) -> Self { Weight(wu) } pub const fn from_wu(wu: u64) -> Self { Weight(wu) }
@ -249,6 +255,12 @@ impl Div<u64> for Weight {
fn div(self, rhs: u64) -> Self::Output { Weight(self.0 / rhs) } fn div(self, rhs: u64) -> Self::Output { Weight(self.0 / rhs) }
} }
impl Div<Weight> for Weight {
type Output = u64;
fn div(self, rhs: Weight) -> Self::Output { self.to_wu() / rhs.to_wu() }
}
impl DivAssign<u64> for Weight { impl DivAssign<u64> for Weight {
fn div_assign(&mut self, rhs: u64) { self.0 /= rhs } fn div_assign(&mut self, rhs: u64) { self.0 /= rhs }
} }

View File

@ -45,8 +45,8 @@ use hashes::Hash;
use self::MerkleBlockError::*; use self::MerkleBlockError::*;
use crate::blockdata::block::{self, Block}; use crate::blockdata::block::{self, Block};
use crate::blockdata::constants::{MAX_BLOCK_WEIGHT, MIN_TRANSACTION_WEIGHT};
use crate::blockdata::transaction::Transaction; use crate::blockdata::transaction::Transaction;
use crate::blockdata::weight::Weight;
use crate::consensus::encode::{self, Decodable, Encodable}; use crate::consensus::encode::{self, Decodable, Encodable};
use crate::hash_types::{TxMerkleNode, Txid}; use crate::hash_types::{TxMerkleNode, Txid};
use crate::io; use crate::io;
@ -276,7 +276,7 @@ impl PartialMerkleTree {
return Err(NoTransactions); return Err(NoTransactions);
}; };
// check for excessively high numbers of transactions // check for excessively high numbers of transactions
if self.num_transactions > MAX_BLOCK_WEIGHT / MIN_TRANSACTION_WEIGHT { if self.num_transactions as u64 > Weight::MAX_BLOCK / Weight::MIN_TRANSACTION {
return Err(TooManyTransactions); return Err(TooManyTransactions);
} }
// there can never be more hashes provided than one for every txid // there can never be more hashes provided than one for every txid