make `VarInt::len` and `Transaction::get_weight` return a usize
This commit is contained in:
parent
7e6ad7c893
commit
abb9210c04
|
@ -7,12 +7,12 @@ fn do_test(data: &[u8]) {
|
|||
Ok(mut tx) => {
|
||||
let ser = bitcoin::consensus::encode::serialize(&tx);
|
||||
assert_eq!(&ser[..], data);
|
||||
let len = ser.len() as u64;
|
||||
let len = ser.len();
|
||||
let calculated_weight = tx.get_weight();
|
||||
for input in &mut tx.input {
|
||||
input.witness = vec![];
|
||||
}
|
||||
let no_witness_len = bitcoin::consensus::encode::serialize(&tx).len() as u64;
|
||||
let no_witness_len = bitcoin::consensus::encode::serialize(&tx).len();
|
||||
// For 0-input transactions, `no_witness_len` will be incorrect because
|
||||
// we serialize as segwit even after "stripping the witnesses". We need
|
||||
// to drop two bytes (i.e. eight weight)
|
||||
|
|
|
@ -368,40 +368,40 @@ impl Transaction {
|
|||
/// witness, this is the non-witness consensus-serialized size multiplied by 3 plus the
|
||||
/// with-witness consensus-serialized size.
|
||||
#[inline]
|
||||
pub fn get_weight(&self) -> u64 {
|
||||
pub fn get_weight(&self) -> usize {
|
||||
let mut input_weight = 0;
|
||||
let mut inputs_with_witnesses = 0;
|
||||
for input in &self.input {
|
||||
input_weight += 4*(32 + 4 + 4 + // outpoint (32+4) + nSequence
|
||||
VarInt(input.script_sig.len() as u64).encoded_length() +
|
||||
input.script_sig.len() as u64);
|
||||
VarInt(input.script_sig.len() as u64).len() +
|
||||
input.script_sig.len());
|
||||
if !input.witness.is_empty() {
|
||||
inputs_with_witnesses += 1;
|
||||
input_weight += VarInt(input.witness.len() as u64).encoded_length();
|
||||
input_weight += VarInt(input.witness.len() as u64).len();
|
||||
for elem in &input.witness {
|
||||
input_weight += VarInt(elem.len() as u64).encoded_length() + elem.len() as u64;
|
||||
input_weight += VarInt(elem.len() as u64).len() + elem.len();
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut output_size = 0;
|
||||
for output in &self.output {
|
||||
output_size += 8 + // value
|
||||
VarInt(output.script_pubkey.len() as u64).encoded_length() +
|
||||
output.script_pubkey.len() as u64;
|
||||
VarInt(output.script_pubkey.len() as u64).len() +
|
||||
output.script_pubkey.len();
|
||||
}
|
||||
let non_input_size =
|
||||
// version:
|
||||
4 +
|
||||
// count varints:
|
||||
VarInt(self.input.len() as u64).encoded_length() +
|
||||
VarInt(self.output.len() as u64).encoded_length() +
|
||||
VarInt(self.input.len() as u64).len() +
|
||||
VarInt(self.output.len() as u64).len() +
|
||||
output_size +
|
||||
// lock_time
|
||||
4;
|
||||
if inputs_with_witnesses == 0 {
|
||||
non_input_size * 4 + input_weight
|
||||
} else {
|
||||
non_input_size * 4 + input_weight + self.input.len() as u64 - inputs_with_witnesses + 2
|
||||
non_input_size * 4 + input_weight + self.input.len() - inputs_with_witnesses + 2
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -419,7 +419,7 @@ impl VarInt {
|
|||
/// Returns 1 for 0...0xFC, 3 for 0xFD...(2^16-1), 5 for 0x10000...(2^32-1),
|
||||
/// and 9 otherwise.
|
||||
#[inline]
|
||||
pub fn encoded_length(&self) -> u64 {
|
||||
pub fn len(&self) -> usize {
|
||||
match self.0 {
|
||||
0...0xFC => { 1 }
|
||||
0xFD...0xFFFF => { 3 }
|
||||
|
|
Loading…
Reference in New Issue