input: add anchor witness type
This commit is contained in:
6 files changed
+155
-67
No files matched your search
@@ -1,8 +1,12 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
|
||||
"github.com/btcsuite/btcd/blockchain"
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/lightningnetwork/lnd/keychain"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -406,6 +410,47 @@ const (
|
||||
OfferedHtlcPenaltyWitnessSize = 1 + 1 + 73 + 1 + 33 + 1 + OfferedHtlcScriptSize
|
||||
)
|
||||
|
||||
// dummySigner is a fake signer used for size (upper bound) calculations.
|
||||
type dummySigner struct {
|
||||
Signer
|
||||
}
|
||||
|
||||
// SignOutputRaw generates a signature for the passed transaction according to
|
||||
// the data within the passed SignDescriptor.
|
||||
func (s *dummySigner) SignOutputRaw(tx *wire.MsgTx, signDesc *SignDescriptor) (
|
||||
[]byte, error) {
|
||||
|
||||
// Always return worst-case signature length, excluding the one byte
|
||||
// sighash flag.
|
||||
return make([]byte, 73-1), nil
|
||||
}
|
||||
|
||||
var (
|
||||
// dummyPubKey is a pubkey used in script size calculation.
|
||||
dummyPubKey = btcec.PublicKey{
|
||||
X: &big.Int{},
|
||||
Y: &big.Int{},
|
||||
}
|
||||
|
||||
// dummyAnchorScript is a script used for size calculation.
|
||||
dummyAnchorScript, _ = CommitScriptAnchor(&dummyPubKey)
|
||||
|
||||
// dummyAnchorWitness is a witness used for size calculation.
|
||||
dummyAnchorWitness, _ = CommitSpendAnchor(
|
||||
&dummySigner{},
|
||||
&SignDescriptor{
|
||||
KeyDesc: keychain.KeyDescriptor{
|
||||
PubKey: &dummyPubKey,
|
||||
},
|
||||
WitnessScript: dummyAnchorScript,
|
||||
},
|
||||
nil,
|
||||
)
|
||||
|
||||
// AnchorWitnessSize 116 bytes
|
||||
AnchorWitnessSize = dummyAnchorWitness.SerializeSize()
|
||||
)
|
||||
|
||||
// EstimateCommitTxWeight estimate commitment transaction weight depending on
|
||||
// the precalculated weight of base transaction, witness data, which is needed
|
||||
// for paying for funding tx, and htlc weight multiplied by their count.
|
||||
|
||||
@@ -280,3 +280,11 @@ func TestTxWeightEstimator(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestSizes guards calculated constants to make sure their values remain
|
||||
// unchanged.
|
||||
func TestSizes(t *testing.T) {
|
||||
if input.AnchorWitnessSize != 116 {
|
||||
t.Fatal("unexpected anchor witness size")
|
||||
}
|
||||
}
|
||||
@@ -125,6 +125,10 @@ const (
|
||||
// output on the counterparty's commitment transaction after a
|
||||
// confirmation.
|
||||
CommitmentToRemoteConfirmed StandardWitnessType = 13
|
||||
|
||||
// CommitmentAnchor is a witness that allows us to spend our anchor on
|
||||
// the commitment transaction.
|
||||
CommitmentAnchor StandardWitnessType = 14
|
||||
)
|
||||
|
||||
// String returns a human readable version of the target WitnessType.
|
||||
@@ -138,6 +142,9 @@ func (wt StandardWitnessType) String() string {
|
||||
case CommitmentToRemoteConfirmed:
|
||||
return "CommitmentToRemoteConfirmed"
|
||||
|
||||
case CommitmentAnchor:
|
||||
return "CommitmentAnchor"
|
||||
|
||||
case CommitmentNoDelay:
|
||||
return "CommitmentNoDelay"
|
||||
|
||||
@@ -218,6 +225,16 @@ func (wt StandardWitnessType) WitnessGenerator(signer Signer,
|
||||
Witness: witness,
|
||||
}, nil
|
||||
|
||||
case CommitmentAnchor:
|
||||
witness, err := CommitSpendAnchor(signer, desc, tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Script{
|
||||
Witness: witness,
|
||||
}, nil
|
||||
|
||||
case CommitmentNoDelay:
|
||||
witness, err := CommitSpendNoDelay(signer, desc, tx, false)
|
||||
if err != nil {
|
||||
@@ -348,6 +365,10 @@ func (wt StandardWitnessType) SizeUpperBound() (int, bool, error) {
|
||||
case CommitmentToRemoteConfirmed:
|
||||
return ToRemoteConfirmedWitnessSize, false, nil
|
||||
|
||||
// Anchor output on the commitment transaction.
|
||||
case CommitmentAnchor:
|
||||
return AnchorWitnessSize, false, nil
|
||||
|
||||
// Outgoing second layer HTLC's that have confirmed within the
|
||||
// chain, and the output they produced is now mature enough to
|
||||
// sweep.
|
||||
|
||||
Reference in new issue
Block a user