watchtower: uses P2TR for sweep, delivery, and reward addresses
In this commit, we modify the watch tower to use P2TR addrs for just about anything sweep related. One eye sore in this diff are the changes to `backup_task_internal_test.go`. All the values are hard coded, and now either differ by a value of 48, or needed to be modified to account for the new assumptions propagated to rewards values and fees.
This commit is contained in:
7 files changed
+107
-46
No files matched your search
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/btcsuite/btcd/btcec/v2"
|
||||
"github.com/btcsuite/btcd/btcutil"
|
||||
"github.com/btcsuite/btcd/btcutil/txsort"
|
||||
"github.com/btcsuite/btcd/chaincfg"
|
||||
"github.com/btcsuite/btcd/txscript"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/lightningnetwork/lnd/channeldb"
|
||||
@@ -151,6 +152,42 @@ func (t *backupTask) inputs() map[wire.OutPoint]input.Input {
|
||||
return inputs
|
||||
}
|
||||
|
||||
// addrType returns the type of an address after parsing it and matching it to
|
||||
// the set of known script templates.
|
||||
func addrType(pkScript []byte) txscript.ScriptClass {
|
||||
// We pass in a set of dummy chain params here as they're only needed
|
||||
// to make the address struct, which we're ignoring anyway (scripts are
|
||||
// always the same, it's addresses that change across chains).
|
||||
scriptClass, _, _, _ := txscript.ExtractPkScriptAddrs(
|
||||
pkScript, &chaincfg.MainNetParams,
|
||||
)
|
||||
|
||||
return scriptClass
|
||||
}
|
||||
|
||||
// addScriptWeight parses the passed pkScript and adds the computed weight cost
|
||||
// were the script to be added to the justice transaction.
|
||||
func addScriptWeight(weightEstimate *input.TxWeightEstimator,
|
||||
pkScript []byte) error {
|
||||
|
||||
switch addrType(pkScript) { //nolint: whitespace
|
||||
|
||||
case txscript.WitnessV0PubKeyHashTy:
|
||||
weightEstimate.AddP2WKHOutput()
|
||||
|
||||
case txscript.WitnessV0ScriptHashTy:
|
||||
weightEstimate.AddP2WSHOutput()
|
||||
|
||||
case txscript.WitnessV1TaprootTy:
|
||||
weightEstimate.AddP2TROutput()
|
||||
|
||||
default:
|
||||
return fmt.Errorf("invalid addr type: %v", addrType(pkScript))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// bindSession determines if the backupTask is compatible with the passed
|
||||
// SessionInfo's policy. If no error is returned, the task has been bound to the
|
||||
// session and can be queued to upload to the tower. Otherwise, the bind failed
|
||||
@@ -192,13 +229,19 @@ func (t *backupTask) bindSession(session *wtdb.ClientSessionBody) error {
|
||||
}
|
||||
}
|
||||
|
||||
// All justice transactions have a p2wkh output paying to the victim.
|
||||
weightEstimate.AddP2WKHOutput()
|
||||
// All justice transactions will either use segwit v0 (p2wkh + p2wsh)
|
||||
// or segwit v1 (p2tr).
|
||||
if err := addScriptWeight(&weightEstimate, t.sweepPkScript); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// If the justice transaction has a reward output, add the output's
|
||||
// contribution to the weight estimate.
|
||||
if session.Policy.BlobType.Has(blob.FlagReward) {
|
||||
weightEstimate.AddP2WKHOutput()
|
||||
err := addScriptWeight(&weightEstimate, session.RewardPkScript)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if t.chanType.HasAnchors() != session.Policy.IsAnchorChannel() {
|
||||
|
||||
@@ -247,7 +247,7 @@ func genTaskTest(
|
||||
RewardPkScript: rewardScript,
|
||||
},
|
||||
bindErr: bindErr,
|
||||
expSweepScript: makeAddrSlice(22),
|
||||
expSweepScript: sweepAddr,
|
||||
signer: signer,
|
||||
chanType: chanType,
|
||||
}
|
||||
@@ -259,10 +259,18 @@ var (
|
||||
blobTypeCommitReward = (blob.FlagCommitOutputs | blob.FlagReward).Type()
|
||||
|
||||
addr, _ = btcutil.DecodeAddress(
|
||||
"mrX9vMRYLfVy1BnZbc5gZjuyaqH3ZW2ZHz", &chaincfg.TestNet3Params,
|
||||
"tb1pw8gzj8clt3v5lxykpgacpju5n8xteskt7gxhmudu6pa70nwfhe6s3unsyk",
|
||||
&chaincfg.TestNet3Params,
|
||||
)
|
||||
|
||||
addrScript, _ = txscript.PayToAddrScript(addr)
|
||||
|
||||
sweepAddrScript, _ = btcutil.DecodeAddress(
|
||||
"tb1qs3jyc9sf5kak3x0w99cav9u605aeu3t600xxx0",
|
||||
&chaincfg.TestNet3Params,
|
||||
)
|
||||
|
||||
sweepAddr, _ = txscript.PayToAddrScript(sweepAddrScript)
|
||||
)
|
||||
|
||||
// TestBackupTaskBind tests the initialization and binding of a backupTask to a
|
||||
@@ -297,9 +305,9 @@ func TestBackupTask(t *testing.T) {
|
||||
expSweepCommitNoRewardBoth int64 = 299241
|
||||
expSweepCommitNoRewardLocal int64 = 199514
|
||||
expSweepCommitNoRewardRemote int64 = 99561
|
||||
expSweepCommitRewardBoth int64 = 296117
|
||||
expSweepCommitRewardLocal int64 = 197390
|
||||
expSweepCommitRewardRemote int64 = 98437
|
||||
expSweepCommitRewardBoth int64 = 296069
|
||||
expSweepCommitRewardLocal int64 = 197342
|
||||
expSweepCommitRewardRemote int64 = 98389
|
||||
sweepFeeRateNoRewardRemoteDust chainfee.SatPerKWeight = 227500
|
||||
sweepFeeRateRewardRemoteDust chainfee.SatPerKWeight = 175350
|
||||
)
|
||||
@@ -307,9 +315,9 @@ func TestBackupTask(t *testing.T) {
|
||||
expSweepCommitNoRewardBoth = 299236
|
||||
expSweepCommitNoRewardLocal = 199513
|
||||
expSweepCommitNoRewardRemote = 99557
|
||||
expSweepCommitRewardBoth = 296112
|
||||
expSweepCommitRewardLocal = 197389
|
||||
expSweepCommitRewardRemote = 98433
|
||||
expSweepCommitRewardBoth = 296064
|
||||
expSweepCommitRewardLocal = 197341
|
||||
expSweepCommitRewardRemote = 98385
|
||||
sweepFeeRateNoRewardRemoteDust = 225400
|
||||
sweepFeeRateRewardRemoteDust = 174100
|
||||
}
|
||||
@@ -436,7 +444,7 @@ func TestBackupTask(t *testing.T) {
|
||||
"commit reward, to-remote output only, creates dust",
|
||||
1, // stateNum
|
||||
0, // toLocalAmt
|
||||
100000, // toRemoteAmt
|
||||
108221, // toRemoteAmt
|
||||
blobTypeCommitReward, // blobType
|
||||
sweepFeeRateRewardRemoteDust, // sweepFeeRate
|
||||
addrScript, // rewardScript
|
||||
|
||||
@@ -58,7 +58,8 @@ var (
|
||||
|
||||
// addr is the server's reward address given to watchtower clients.
|
||||
addr, _ = btcutil.DecodeAddress(
|
||||
"mrX9vMRYLfVy1BnZbc5gZjuyaqH3ZW2ZHz", &chaincfg.TestNet3Params,
|
||||
"tb1pw8gzj8clt3v5lxykpgacpju5n8xteskt7gxhmudu6pa70nwfhe6s3unsyk",
|
||||
&chaincfg.TestNet3Params,
|
||||
)
|
||||
|
||||
addrScript, _ = txscript.PayToAddrScript(addr)
|
||||
|
||||
Reference in new issue
Block a user