Use Weight type for stripped_size
This commit is contained in:
parent
cb76f3ec43
commit
142dde64c3
|
@ -291,12 +291,12 @@ impl Block {
|
||||||
since = "0.31.0",
|
since = "0.31.0",
|
||||||
note = "Truncates on 32-bit machines, Use Block::stripped_size() instead"
|
note = "Truncates on 32-bit machines, Use Block::stripped_size() instead"
|
||||||
)]
|
)]
|
||||||
pub fn strippedsize(&self) -> usize { Self::stripped_size(self) }
|
pub fn strippedsize(&self) -> usize { Self::stripped_size(self).to_wu() as usize }
|
||||||
|
|
||||||
/// Returns the stripped size of the block.
|
/// Returns the stripped size of the block.
|
||||||
pub fn stripped_size(&self) -> usize {
|
pub fn stripped_size(&self) -> Weight {
|
||||||
let txs_size: usize = self.txdata.iter().map(Transaction::stripped_size).sum();
|
let txs_size: Weight = self.txdata.iter().map(Transaction::stripped_size).sum();
|
||||||
self.base_size() + txs_size
|
Weight::from_wu_usize(self.base_size()) + txs_size
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the weight of the block.
|
/// Returns the weight of the block.
|
||||||
|
@ -488,7 +488,7 @@ mod tests {
|
||||||
// [test] TODO: check the transaction data
|
// [test] TODO: check the transaction data
|
||||||
|
|
||||||
assert_eq!(real_decode.size(), some_block.len());
|
assert_eq!(real_decode.size(), some_block.len());
|
||||||
assert_eq!(real_decode.stripped_size(), some_block.len());
|
assert_eq!(real_decode.stripped_size(), Weight::from_wu_usize(some_block.len()));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
real_decode.weight(),
|
real_decode.weight(),
|
||||||
Weight::from_non_witness_data_size(some_block.len() as u64)
|
Weight::from_non_witness_data_size(some_block.len() as u64)
|
||||||
|
@ -530,7 +530,7 @@ mod tests {
|
||||||
// [test] TODO: check the transaction data
|
// [test] TODO: check the transaction data
|
||||||
|
|
||||||
assert_eq!(real_decode.size(), segwit_block.len());
|
assert_eq!(real_decode.size(), segwit_block.len());
|
||||||
assert_eq!(real_decode.stripped_size(), 4283);
|
assert_eq!(real_decode.stripped_size(), Weight::from_wu(4283));
|
||||||
assert_eq!(real_decode.weight(), Weight::from_wu(17168));
|
assert_eq!(real_decode.weight(), Weight::from_wu(17168));
|
||||||
|
|
||||||
assert!(real_decode.check_witness_commitment());
|
assert!(real_decode.check_witness_commitment());
|
||||||
|
|
|
@ -714,31 +714,31 @@ impl Transaction {
|
||||||
|
|
||||||
/// Returns the size of this transaction excluding the witness data.
|
/// Returns the size of this transaction excluding the witness data.
|
||||||
#[deprecated(since = "0.31.0", note = "Use Transaction::stripped_size() instead")]
|
#[deprecated(since = "0.31.0", note = "Use Transaction::stripped_size() instead")]
|
||||||
pub fn strippedsize(&self) -> usize { Self::stripped_size(self) }
|
pub fn strippedsize(&self) -> usize { Self::stripped_size(self).to_wu() as usize }
|
||||||
|
|
||||||
/// Returns the size of this transaction excluding the witness data.
|
/// Returns the size of this transaction excluding the witness data.
|
||||||
pub fn stripped_size(&self) -> usize {
|
pub fn stripped_size(&self) -> Weight {
|
||||||
let mut input_size = 0;
|
let mut input_size: Weight = Weight::ZERO;
|
||||||
for input in &self.input {
|
for input in &self.input {
|
||||||
input_size += TxIn::BASE_WEIGHT.to_wu() as usize
|
input_size += TxIn::BASE_WEIGHT
|
||||||
+ VarInt(input.script_sig.len() as u64).len()
|
+ Weight::from_wu_usize(VarInt(input.script_sig.len() as u64).len())
|
||||||
+ input.script_sig.len();
|
+ Weight::from_wu_usize(input.script_sig.len());
|
||||||
}
|
}
|
||||||
let mut output_size = 0;
|
let mut output_size = Weight::ZERO;
|
||||||
for output in &self.output {
|
for output in &self.output {
|
||||||
output_size += 8 + // value
|
output_size += Weight::from_wu(8)+ // value
|
||||||
VarInt(output.script_pubkey.len() as u64).len() +
|
Weight::from_wu_usize(VarInt(output.script_pubkey.len() as u64).len()) +
|
||||||
output.script_pubkey.len();
|
Weight::from_wu_usize(output.script_pubkey.len());
|
||||||
}
|
}
|
||||||
let non_input_size =
|
let non_input_size: Weight =
|
||||||
// version:
|
// version:
|
||||||
4 +
|
Weight::from_wu(4)+
|
||||||
// count varints:
|
// count varints:
|
||||||
VarInt(self.input.len() as u64).len() +
|
Weight::from_wu_usize(VarInt(self.input.len() as u64).len()) +
|
||||||
VarInt(self.output.len() as u64).len() +
|
Weight::from_wu_usize(VarInt(self.output.len() as u64).len()) +
|
||||||
output_size +
|
output_size +
|
||||||
// lock_time
|
// lock_time
|
||||||
4;
|
Weight::from_wu(4);
|
||||||
non_input_size + input_size
|
non_input_size + input_size
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1398,7 +1398,7 @@ mod tests {
|
||||||
assert_eq!(realtx.weight().to_wu() as usize, tx_bytes.len() * WITNESS_SCALE_FACTOR);
|
assert_eq!(realtx.weight().to_wu() as usize, tx_bytes.len() * WITNESS_SCALE_FACTOR);
|
||||||
assert_eq!(realtx.size(), tx_bytes.len());
|
assert_eq!(realtx.size(), tx_bytes.len());
|
||||||
assert_eq!(realtx.vsize(), tx_bytes.len());
|
assert_eq!(realtx.vsize(), tx_bytes.len());
|
||||||
assert_eq!(realtx.stripped_size(), tx_bytes.len());
|
assert_eq!(realtx.stripped_size(), Weight::from_wu_usize(tx_bytes.len()));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -1444,18 +1444,20 @@ mod tests {
|
||||||
// weight = WITNESS_SCALE_FACTOR * stripped_size + witness_size
|
// weight = WITNESS_SCALE_FACTOR * stripped_size + witness_size
|
||||||
// then,
|
// then,
|
||||||
// stripped_size = (weight - size) / (WITNESS_SCALE_FACTOR - 1)
|
// stripped_size = (weight - size) / (WITNESS_SCALE_FACTOR - 1)
|
||||||
let expected_strippedsize =
|
let expected_strippedsize: Weight = Weight::from_wu(
|
||||||
(EXPECTED_WEIGHT.to_wu() as usize - tx_bytes.len()) / (WITNESS_SCALE_FACTOR - 1);
|
(EXPECTED_WEIGHT - Weight::from_wu_usize(tx_bytes.len()))
|
||||||
|
/ (Weight::from_wu_usize(WITNESS_SCALE_FACTOR - 1)),
|
||||||
|
);
|
||||||
assert_eq!(realtx.stripped_size(), expected_strippedsize);
|
assert_eq!(realtx.stripped_size(), expected_strippedsize);
|
||||||
// Construct a transaction without the witness data.
|
// Construct a transaction without the witness data.
|
||||||
let mut tx_without_witness = realtx;
|
let mut tx_without_witness = realtx;
|
||||||
tx_without_witness.input.iter_mut().for_each(|input| input.witness.clear());
|
tx_without_witness.input.iter_mut().for_each(|input| input.witness.clear());
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
tx_without_witness.weight().to_wu() as usize,
|
tx_without_witness.weight(),
|
||||||
expected_strippedsize * WITNESS_SCALE_FACTOR
|
expected_strippedsize.scale_by_witness_factor().unwrap()
|
||||||
);
|
);
|
||||||
assert_eq!(tx_without_witness.size(), expected_strippedsize);
|
assert_eq!(Weight::from_wu_usize(tx_without_witness.size()), expected_strippedsize);
|
||||||
assert_eq!(tx_without_witness.vsize(), expected_strippedsize);
|
assert_eq!(Weight::from_wu_usize(tx_without_witness.vsize()), expected_strippedsize);
|
||||||
assert_eq!(tx_without_witness.stripped_size(), expected_strippedsize);
|
assert_eq!(tx_without_witness.stripped_size(), expected_strippedsize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue