1 //! Utilities for computing witnesses weight and feerate computation for onchain operation
3 use ln::msgs::DecodeError;
4 use util::ser::{Readable, Writer, Writeable};
6 #[derive(PartialEq, Clone, Copy)]
7 pub(crate) enum InputDescriptors {
12 RevokedOutput, // either a revoked to_holder output on commitment tx, a revoked HTLC-Timeout output or a revoked HTLC-Success output
15 impl Writeable for InputDescriptors {
16 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
18 &InputDescriptors::RevokedOfferedHTLC => {
19 writer.write_all(&[0; 1])?;
21 &InputDescriptors::RevokedReceivedHTLC => {
22 writer.write_all(&[1; 1])?;
24 &InputDescriptors::OfferedHTLC => {
25 writer.write_all(&[2; 1])?;
27 &InputDescriptors::ReceivedHTLC => {
28 writer.write_all(&[3; 1])?;
30 &InputDescriptors::RevokedOutput => {
31 writer.write_all(&[4; 1])?;
38 impl Readable for InputDescriptors {
39 fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
40 let input_descriptor = match <u8 as Readable>::read(reader)? {
42 InputDescriptors::RevokedOfferedHTLC
45 InputDescriptors::RevokedReceivedHTLC
48 InputDescriptors::OfferedHTLC
51 InputDescriptors::ReceivedHTLC
54 InputDescriptors::RevokedOutput
56 _ => return Err(DecodeError::InvalidValue),
62 pub(crate) fn get_witnesses_weight(inputs: &[InputDescriptors]) -> usize {
63 let mut tx_weight = 2; // count segwit flags
65 // We use expected weight (and not actual) as signatures and time lock delays may vary
66 tx_weight += match inp {
67 // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
68 &InputDescriptors::RevokedOfferedHTLC => {
69 1 + 1 + 73 + 1 + 33 + 1 + 133
71 // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
72 &InputDescriptors::RevokedReceivedHTLC => {
73 1 + 1 + 73 + 1 + 33 + 1 + 139
75 // number_of_witness_elements + sig_length + counterpartyhtlc_sig + preimage_length + preimage + witness_script_length + witness_script
76 &InputDescriptors::OfferedHTLC => {
77 1 + 1 + 73 + 1 + 32 + 1 + 133
79 // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
80 &InputDescriptors::ReceivedHTLC => {
81 1 + 1 + 73 + 1 + 1 + 1 + 139
83 // number_of_witness_elements + sig_length + revocation_sig + true_length + op_true + witness_script_length + witness_script
84 &InputDescriptors::RevokedOutput => {
85 1 + 1 + 73 + 1 + 1 + 1 + 77