Add package.rs file
[rust-lightning] / lightning / src / ln / package.rs
1 //! Utilities for computing witnesses weight and feerate computation for onchain operation
2
3 use ln::msgs::DecodeError;
4 use util::ser::{Readable, Writer, Writeable};
5
6 #[derive(PartialEq, Clone, Copy)]
7 pub(crate) enum InputDescriptors {
8         RevokedOfferedHTLC,
9         RevokedReceivedHTLC,
10         OfferedHTLC,
11         ReceivedHTLC,
12         RevokedOutput, // either a revoked to_holder output on commitment tx, a revoked HTLC-Timeout output or a revoked HTLC-Success output
13 }
14
15 impl Writeable for InputDescriptors {
16         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
17                 match self {
18                         &InputDescriptors::RevokedOfferedHTLC => {
19                                 writer.write_all(&[0; 1])?;
20                         },
21                         &InputDescriptors::RevokedReceivedHTLC => {
22                                 writer.write_all(&[1; 1])?;
23                         },
24                         &InputDescriptors::OfferedHTLC => {
25                                 writer.write_all(&[2; 1])?;
26                         },
27                         &InputDescriptors::ReceivedHTLC => {
28                                 writer.write_all(&[3; 1])?;
29                         }
30                         &InputDescriptors::RevokedOutput => {
31                                 writer.write_all(&[4; 1])?;
32                         }
33                 }
34                 Ok(())
35         }
36 }
37
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)? {
41                         0 => {
42                                 InputDescriptors::RevokedOfferedHTLC
43                         },
44                         1 => {
45                                 InputDescriptors::RevokedReceivedHTLC
46                         },
47                         2 => {
48                                 InputDescriptors::OfferedHTLC
49                         },
50                         3 => {
51                                 InputDescriptors::ReceivedHTLC
52                         },
53                         4 => {
54                                 InputDescriptors::RevokedOutput
55                         }
56                         _ => return Err(DecodeError::InvalidValue),
57                 };
58                 Ok(input_descriptor)
59         }
60 }
61
62 pub(crate) fn get_witnesses_weight(inputs: &[InputDescriptors]) -> usize {
63         let mut tx_weight = 2; // count segwit flags
64         for inp in inputs {
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
70                         },
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
74                         },
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
78                         },
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
82                         },
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
86                         },
87                 };
88         }
89         tx_weight
90 }