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