Skip to content

Commit

Permalink
Merge pull request #4775 from halseth/input-tx-size-constants
Browse files Browse the repository at this point in the history
[input/size] Check tx size constants
  • Loading branch information
cfromknecht committed Mar 10, 2021
2 parents 65b0bbc + d30aae4 commit 7e33548
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 48 deletions.
74 changes: 49 additions & 25 deletions input/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ const (
// - Sequence: 4 bytes
InputSize = 32 + 4 + 1 + 4

// FundingInputSize 41 bytes
// FundingInputSize represents the size of an input to a funding
// transaction, and is equivalent to the size of a standard segwit input
// as calculated above.
Expand Down Expand Up @@ -204,31 +205,37 @@ const (
// HTLCWeight 172 weight
HTLCWeight = witnessScaleFactor * HTLCSize

// HtlcTimeoutWeight 663 weight
// HtlcTimeoutWeight is the weight of the HTLC timeout transaction
// which will transition an outgoing HTLC to the delay-and-claim state.
HtlcTimeoutWeight = 663

// HtlcSuccessWeight 703 weight
// HtlcSuccessWeight is the weight of the HTLC success transaction
// which will transition an incoming HTLC to the delay-and-claim state.
HtlcSuccessWeight = 703

// HtlcConfirmedScriptOverhead 3 bytes
// HtlcConfirmedScriptOverhead is the extra length of an HTLC script
// that requires confirmation before it can be spent. These extra bytes
// is a result of the extra CSV check.
HtlcConfirmedScriptOverhead = 3

// HtlcTimeoutWeightConfirmed 666 weight
// HtlcTimeoutWeightConfirmed is the weight of the HTLC timeout
// transaction which will transition an outgoing HTLC to the
// delay-and-claim state, for the confirmed HTLC outputs. It is 3 bytes
// larger because of the additional CSV check in the input script.
HtlcTimeoutWeightConfirmed = HtlcTimeoutWeight + HtlcConfirmedScriptOverhead

// HtlcSuccessWeightCOnfirmed is the weight of the HTLC success
// HtlcSuccessWeightConfirmed 706 weight
// HtlcSuccessWeightConfirmed is the weight of the HTLC success
// transaction which will transition an incoming HTLC to the
// delay-and-claim state, for the confirmed HTLC outputs. It is 3 bytes
// larger because of the cdditional CSV check in the input script.
HtlcSuccessWeightConfirmed = HtlcSuccessWeight + HtlcConfirmedScriptOverhead

// MaxHTLCNumber 966
// MaxHTLCNumber is the maximum number HTLCs which can be included in a
// commitment transaction. This limit was chosen such that, in the case
// of a contract breach, the punishment transaction is able to sweep
Expand Down Expand Up @@ -286,7 +293,7 @@ const (
// - witness_script (to_remote_delayed_script)
ToRemoteConfirmedWitnessSize = 1 + 1 + 73 + 1 + ToRemoteConfirmedScriptSize

// AcceptedHtlcScriptSize 143 bytes
// AcceptedHtlcScriptSize 140 bytes
// - OP_DUP: 1 byte
// - OP_HASH160: 1 byte
// - OP_DATA: 1 byte (RIPEMD160(SHA256(revocationkey)) length)
Expand Down Expand Up @@ -321,20 +328,18 @@ const (
// - OP_DROP: 1 byte
// - OP_CHECKSIG: 1 byte
// - OP_ENDIF: 1 byte
// - OP_1: 1 byte // These 3 extra bytes are used for both confirmed and regular
// - OP_CSV: 1 byte // HTLC script types. The size won't be correct in all cases,
// - OP_DROP: 1 byte // but it is just an upper bound used for fee estimation in any case.
// - OP_1: 1 byte // These 3 extra bytes are only
// - OP_CSV: 1 byte // present for the confirmed
// - OP_DROP: 1 byte // HTLC script types.
// - OP_ENDIF: 1 byte
AcceptedHtlcScriptSize = 3*1 + 20 + 5*1 + 33 + 8*1 + 20 + 4*1 +
33 + 5*1 + 4 + 8*1
33 + 5*1 + 4 + 5*1

// AcceptedHtlcScriptSizeConfirmed 143 bytes
//
// TODO(halseth): the non-confirmed version currently includes the
// overhead.
AcceptedHtlcScriptSizeConfirmed = AcceptedHtlcScriptSize // + HtlcConfirmedScriptOverhead
AcceptedHtlcScriptSizeConfirmed = AcceptedHtlcScriptSize +
HtlcConfirmedScriptOverhead

// AcceptedHtlcTimeoutWitnessSize 219
// AcceptedHtlcTimeoutWitnessSize 217 bytes
// - number_of_witness_elements: 1 byte
// - sender_sig_length: 1 byte
// - sender_sig: 73 bytes
Expand All @@ -343,7 +348,11 @@ const (
// - witness_script: (accepted_htlc_script)
AcceptedHtlcTimeoutWitnessSize = 1 + 1 + 73 + 1 + 1 + AcceptedHtlcScriptSize

// AcceptedHtlcPenaltyWitnessSize 252 bytes
// AcceptedHtlcTimeoutWitnessSizeConfirmed 220 bytes
AcceptedHtlcTimeoutWitnessSizeConfirmed = 1 + 1 + 73 + 1 + 1 +
AcceptedHtlcScriptSizeConfirmed

// AcceptedHtlcPenaltyWitnessSize 250 bytes
// - number_of_witness_elements: 1 byte
// - revocation_sig_length: 1 byte
// - revocation_sig: 73 bytes
Expand All @@ -353,7 +362,11 @@ const (
// - witness_script (accepted_htlc_script)
AcceptedHtlcPenaltyWitnessSize = 1 + 1 + 73 + 1 + 33 + 1 + AcceptedHtlcScriptSize

// AcceptedHtlcSuccessWitnessSize 322 bytes
// AcceptedHtlcPenaltyWitnessSizeConfirmed 253 bytes
AcceptedHtlcPenaltyWitnessSizeConfirmed = 1 + 1 + 73 + 1 + 33 + 1 +
AcceptedHtlcScriptSizeConfirmed

// AcceptedHtlcSuccessWitnessSize 324 bytes
// - number_of_witness_elements: 1 byte
// - nil_length: 1 byte
// - sig_alice_length: 1 byte
Expand All @@ -364,6 +377,8 @@ const (
// - preimage: 32 bytes
// - witness_script_length: 1 byte
// - witness_script (accepted_htlc_script)
//
// Input to second level success tx, spending non-delayed HTLC output.
AcceptedHtlcSuccessWitnessSize = 1 + 1 + 1 + 73 + 1 + 73 + 1 + 32 + 1 +
AcceptedHtlcScriptSize

Expand All @@ -373,7 +388,7 @@ const (
AcceptedHtlcSuccessWitnessSizeConfirmed = 1 + 1 + 1 + 73 + 1 + 73 + 1 + 32 + 1 +
AcceptedHtlcScriptSizeConfirmed

// OfferedHtlcScriptSize 136 bytes
// OfferedHtlcScriptSize 133 bytes
// - OP_DUP: 1 byte
// - OP_HASH160: 1 byte
// - OP_DATA: 1 byte (RIPEMD160(SHA256(revocationkey)) length)
Expand Down Expand Up @@ -404,19 +419,17 @@ const (
// - OP_EQUALVERIFY: 1 byte
// - OP_CHECKSIG: 1 byte
// - OP_ENDIF: 1 byte
// - OP_1: 1 byte
// - OP_CSV: 1 byte
// - OP_DROP: 1 byte
// - OP_1: 1 byte // These 3 extra bytes are only
// - OP_CSV: 1 byte // present for the confirmed
// - OP_DROP: 1 byte // HTLC script types.
// - OP_ENDIF: 1 byte
OfferedHtlcScriptSize = 3*1 + 20 + 5*1 + 33 + 10*1 + 33 + 5*1 + 20 + 7*1
OfferedHtlcScriptSize = 3*1 + 20 + 5*1 + 33 + 10*1 + 33 + 5*1 + 20 + 4*1

// OfferedHtlcScriptSizeConfirmed 136 bytes
//
// TODO(halseth): the non-confirmed version currently includes the
// overhead.
OfferedHtlcScriptSizeConfirmed = OfferedHtlcScriptSize // + HtlcConfirmedScriptOverhead
OfferedHtlcScriptSizeConfirmed = OfferedHtlcScriptSize +
HtlcConfirmedScriptOverhead

// OfferedHtlcSuccessWitnessSize 245 bytes
// OfferedHtlcSuccessWitnessSize 242 bytes
// - number_of_witness_elements: 1 byte
// - receiver_sig_length: 1 byte
// - receiver_sig: 73 bytes
Expand All @@ -426,6 +439,10 @@ const (
// - witness_script (offered_htlc_script)
OfferedHtlcSuccessWitnessSize = 1 + 1 + 73 + 1 + 32 + 1 + OfferedHtlcScriptSize

// OfferedHtlcSuccessWitnessSizeConfirmed 245 bytes
OfferedHtlcSuccessWitnessSizeConfirmed = 1 + 1 + 73 + 1 + 32 + 1 +
OfferedHtlcScriptSizeConfirmed

// OfferedHtlcTimeoutWitnessSize 285 bytes
// - number_of_witness_elements: 1 byte
// - nil_length: 1 byte
Expand All @@ -436,15 +453,18 @@ const (
// - nil_length: 1 byte
// - witness_script_length: 1 byte
// - witness_script (offered_htlc_script)
OfferedHtlcTimeoutWitnessSize = 1 + 1 + 1 + 73 + 1 + 73 + 1 + 1 + OfferedHtlcScriptSize
//
// Input to second level timeout tx, spending non-delayed HTLC output.
OfferedHtlcTimeoutWitnessSize = 1 + 1 + 1 + 73 + 1 + 73 + 1 + 1 +
OfferedHtlcScriptSize

// OfferedHtlcTimeoutWitnessSizeConfirmed 288 bytes
//
// Input to second level timeout tx, spending 1 CSV delayed HTLC output.
OfferedHtlcTimeoutWitnessSizeConfirmed = 1 + 1 + 1 + 73 + 1 + 73 + 1 + 1 +
OfferedHtlcScriptSizeConfirmed

// OfferedHtlcPenaltyWitnessSize 246 bytes
// OfferedHtlcPenaltyWitnessSize 243 bytes
// - number_of_witness_elements: 1 byte
// - revocation_sig_length: 1 byte
// - revocation_sig: 73 bytes
Expand All @@ -454,6 +474,10 @@ const (
// - witness_script (offered_htlc_script)
OfferedHtlcPenaltyWitnessSize = 1 + 1 + 73 + 1 + 33 + 1 + OfferedHtlcScriptSize

// OfferedHtlcPenaltyWitnessSizeConfirmed 246 bytes
OfferedHtlcPenaltyWitnessSizeConfirmed = 1 + 1 + 73 + 1 + 33 + 1 +
OfferedHtlcScriptSizeConfirmed

// AnchorScriptSize 40 bytes
// - pubkey_length: 1 byte
// - pubkey: 33 bytes
Expand Down
Loading

0 comments on commit 7e33548

Please sign in to comment.