Add base weight const to TxIn

This commit is contained in:
yancy 2023-08-17 00:17:31 +02:00
parent 02890d3fba
commit 5496feb5c1
1 changed files with 9 additions and 6 deletions

View File

@ -199,6 +199,9 @@ pub struct TxIn {
} }
impl TxIn { impl TxIn {
/// The weight of a `TxIn` excluding the `script_sig` and `witness`.
pub const BASE_WEIGHT: Weight = Weight::from_wu(32 + 4 + 4);
/// Returns true if this input enables the [`absolute::LockTime`] (aka `nLockTime`) of its /// Returns true if this input enables the [`absolute::LockTime`] (aka `nLockTime`) of its
/// [`Transaction`]. /// [`Transaction`].
/// ///
@ -713,9 +716,9 @@ impl Transaction {
pub fn strippedsize(&self) -> usize { pub fn strippedsize(&self) -> usize {
let mut input_size = 0; let mut input_size = 0;
for input in &self.input { for input in &self.input {
input_size += 32 + 4 + 4 + // outpoint (32+4) + nSequence input_size += TxIn::BASE_WEIGHT.to_wu() as usize
VarInt(input.script_sig.len() as u64).len() + + VarInt(input.script_sig.len() as u64).len()
input.script_sig.len(); + input.script_sig.len();
} }
let mut output_size = 0; let mut output_size = 0;
for output in &self.output { for output in &self.output {
@ -741,9 +744,9 @@ impl Transaction {
let mut inputs_with_witnesses = 0; let mut inputs_with_witnesses = 0;
for input in &self.input { for input in &self.input {
input_weight += scale_factor input_weight += scale_factor
* (32 + 4 + 4 + // outpoint (32+4) + nSequence * (TxIn::BASE_WEIGHT.to_wu() as usize
VarInt(input.script_sig.len() as u64).len() + + VarInt(input.script_sig.len() as u64).len()
input.script_sig.len()); + input.script_sig.len());
if !input.witness.is_empty() { if !input.witness.is_empty() {
inputs_with_witnesses += 1; inputs_with_witnesses += 1;
input_weight += input.witness.serialized_len(); input_weight += input.witness.serialized_len();