lnwallet: pass chan cfgs to CreateCommitTx
Instead of passing delays and dustlimits separately, we pass the correct channel config to CreateCommitTx from the POV of the local party that owns the commit tx. To make it more clear which commitment we are actually creating, we rename variables to denote local and remote, to prepare for the case when both outputs might be delayed.
This commit is contained in:
parent
1a4f81ed90
commit
fff9dbe6f3
@ -2428,23 +2428,27 @@ func (lc *LightningChannel) createCommitmentTx(c *commitment,
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
delay uint32
|
commitTx *wire.MsgTx
|
||||||
delayBalance, p2wkhBalance btcutil.Amount
|
err error
|
||||||
)
|
)
|
||||||
if c.isOurs {
|
|
||||||
delay = uint32(lc.channelState.LocalChanCfg.CsvDelay)
|
|
||||||
delayBalance = ourBalance.ToSatoshis()
|
|
||||||
p2wkhBalance = theirBalance.ToSatoshis()
|
|
||||||
} else {
|
|
||||||
delay = uint32(lc.channelState.RemoteChanCfg.CsvDelay)
|
|
||||||
delayBalance = theirBalance.ToSatoshis()
|
|
||||||
p2wkhBalance = ourBalance.ToSatoshis()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate a new commitment transaction with all the latest
|
// Depending on whether the transaction is ours or not, we call
|
||||||
// unsettled/un-timed out HTLCs.
|
// CreateCommitTx with parameters mathcing the perspective, to generate
|
||||||
commitTx, err := CreateCommitTx(lc.fundingTxIn(), keyRing, delay,
|
// a new commitment transaction with all the latest unsettled/un-timed
|
||||||
delayBalance, p2wkhBalance, c.dustLimit)
|
// out HTLCs.
|
||||||
|
if c.isOurs {
|
||||||
|
commitTx, err = CreateCommitTx(
|
||||||
|
lc.fundingTxIn(), keyRing, &lc.channelState.LocalChanCfg,
|
||||||
|
&lc.channelState.RemoteChanCfg, ourBalance.ToSatoshis(),
|
||||||
|
theirBalance.ToSatoshis(),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
commitTx, err = CreateCommitTx(
|
||||||
|
lc.fundingTxIn(), keyRing, &lc.channelState.RemoteChanCfg,
|
||||||
|
&lc.channelState.LocalChanCfg, theirBalance.ToSatoshis(),
|
||||||
|
ourBalance.ToSatoshis(),
|
||||||
|
)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -6216,32 +6220,39 @@ func (lc *LightningChannel) generateRevocation(height uint64) (*lnwire.RevokeAnd
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateCommitTx creates a commitment transaction, spending from specified
|
// CreateCommitTx creates a commitment transaction, spending from specified
|
||||||
// funding output. The commitment transaction contains two outputs: one paying
|
// funding output. The commitment transaction contains two outputs: one local
|
||||||
// to the "owner" of the commitment transaction which can be spent after a
|
// output paying to the "owner" of the commitment transaction which can be
|
||||||
// relative block delay or revocation event, and the other paying the
|
// spent after a relative block delay or revocation event, and a remote output
|
||||||
// counterparty within the channel, which can be spent immediately.
|
// paying the counterparty within the channel, which can be spent immediately
|
||||||
func CreateCommitTx(fundingOutput wire.TxIn,
|
// or after a delay depending on the commitment type..
|
||||||
keyRing *CommitmentKeyRing, csvTimeout uint32,
|
func CreateCommitTx(fundingOutput wire.TxIn, keyRing *CommitmentKeyRing,
|
||||||
amountToSelf, amountToThem, dustLimit btcutil.Amount) (*wire.MsgTx, error) {
|
localChanCfg, remoteChanCfg *channeldb.ChannelConfig,
|
||||||
|
amountToLocal, amountToRemote btcutil.Amount) (*wire.MsgTx, error) {
|
||||||
|
|
||||||
// First, we create the script for the delayed "pay-to-self" output.
|
// First, we create the script for the delayed "pay-to-self" output.
|
||||||
// This output has 2 main redemption clauses: either we can redeem the
|
// This output has 2 main redemption clauses: either we can redeem the
|
||||||
// output after a relative block delay, or the remote node can claim
|
// output after a relative block delay, or the remote node can claim
|
||||||
// the funds with the revocation key if we broadcast a revoked
|
// the funds with the revocation key if we broadcast a revoked
|
||||||
// commitment transaction.
|
// commitment transaction.
|
||||||
ourRedeemScript, err := input.CommitScriptToSelf(csvTimeout, keyRing.DelayKey,
|
toLocalRedeemScript, err := input.CommitScriptToSelf(
|
||||||
keyRing.RevocationKey)
|
uint32(localChanCfg.CsvDelay), keyRing.DelayKey,
|
||||||
|
keyRing.RevocationKey,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
payToUsScriptHash, err := input.WitnessScriptHash(ourRedeemScript)
|
toLocalScriptHash, err := input.WitnessScriptHash(
|
||||||
|
toLocalRedeemScript,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Next, we create the script paying to them. This is just a regular
|
// Next, we create the script paying to the remote. This is just a
|
||||||
// P2WPKH output, without any added CSV delay.
|
// regular P2WPKH output, without any added CSV delay.
|
||||||
theirWitnessKeyHash, err := input.CommitScriptUnencumbered(keyRing.NoDelayKey)
|
toRemoteWitnessKeyHash, err := input.CommitScriptUnencumbered(
|
||||||
|
keyRing.NoDelayKey,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -6253,16 +6264,16 @@ func CreateCommitTx(fundingOutput wire.TxIn,
|
|||||||
commitTx.AddTxIn(&fundingOutput)
|
commitTx.AddTxIn(&fundingOutput)
|
||||||
|
|
||||||
// Avoid creating dust outputs within the commitment transaction.
|
// Avoid creating dust outputs within the commitment transaction.
|
||||||
if amountToSelf >= dustLimit {
|
if amountToLocal >= localChanCfg.DustLimit {
|
||||||
commitTx.AddTxOut(&wire.TxOut{
|
commitTx.AddTxOut(&wire.TxOut{
|
||||||
PkScript: payToUsScriptHash,
|
PkScript: toLocalScriptHash,
|
||||||
Value: int64(amountToSelf),
|
Value: int64(amountToLocal),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if amountToThem >= dustLimit {
|
if amountToRemote >= localChanCfg.DustLimit {
|
||||||
commitTx.AddTxOut(&wire.TxOut{
|
commitTx.AddTxOut(&wire.TxOut{
|
||||||
PkScript: theirWitnessKeyHash,
|
PkScript: toRemoteWitnessKeyHash,
|
||||||
Value: int64(amountToThem),
|
Value: int64(amountToRemote),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1015,7 +1015,7 @@ func testSpendValidation(t *testing.T, tweakless bool) {
|
|||||||
fakeFundingTxIn := wire.NewTxIn(fundingOut, nil, nil)
|
fakeFundingTxIn := wire.NewTxIn(fundingOut, nil, nil)
|
||||||
|
|
||||||
const channelBalance = btcutil.Amount(1 * 10e8)
|
const channelBalance = btcutil.Amount(1 * 10e8)
|
||||||
const csvTimeout = uint32(5)
|
const csvTimeout = 5
|
||||||
|
|
||||||
// We also set up set some resources for the commitment transaction.
|
// We also set up set some resources for the commitment transaction.
|
||||||
// Each side currently has 1 BTC within the channel, with a total
|
// Each side currently has 1 BTC within the channel, with a total
|
||||||
@ -1050,6 +1050,20 @@ func testSpendValidation(t *testing.T, tweakless bool) {
|
|||||||
Privkeys: []*btcec.PrivateKey{aliceKeyPriv},
|
Privkeys: []*btcec.PrivateKey{aliceKeyPriv},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
aliceChanCfg := &channeldb.ChannelConfig{
|
||||||
|
ChannelConstraints: channeldb.ChannelConstraints{
|
||||||
|
DustLimit: DefaultDustLimit(),
|
||||||
|
CsvDelay: csvTimeout,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
bobChanCfg := &channeldb.ChannelConfig{
|
||||||
|
ChannelConstraints: channeldb.ChannelConstraints{
|
||||||
|
DustLimit: DefaultDustLimit(),
|
||||||
|
CsvDelay: csvTimeout,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
// With all the test data set up, we create the commitment transaction.
|
// With all the test data set up, we create the commitment transaction.
|
||||||
// We only focus on a single party's transactions, as the scripts are
|
// We only focus on a single party's transactions, as the scripts are
|
||||||
// identical with the roles reversed.
|
// identical with the roles reversed.
|
||||||
@ -1063,8 +1077,8 @@ func testSpendValidation(t *testing.T, tweakless bool) {
|
|||||||
NoDelayKey: bobPayKey,
|
NoDelayKey: bobPayKey,
|
||||||
}
|
}
|
||||||
commitmentTx, err := CreateCommitTx(
|
commitmentTx, err := CreateCommitTx(
|
||||||
*fakeFundingTxIn, keyRing, csvTimeout, channelBalance,
|
*fakeFundingTxIn, keyRing, aliceChanCfg, bobChanCfg,
|
||||||
channelBalance, DefaultDustLimit(),
|
channelBalance, channelBalance,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to create commitment transaction: %v", nil)
|
t.Fatalf("unable to create commitment transaction: %v", nil)
|
||||||
|
@ -784,9 +784,10 @@ func CreateCommitmentTxns(localBalance, remoteBalance btcutil.Amount,
|
|||||||
theirChanCfg,
|
theirChanCfg,
|
||||||
)
|
)
|
||||||
|
|
||||||
ourCommitTx, err := CreateCommitTx(fundingTxIn, localCommitmentKeys,
|
ourCommitTx, err := CreateCommitTx(
|
||||||
uint32(ourChanCfg.CsvDelay), localBalance, remoteBalance,
|
fundingTxIn, localCommitmentKeys, ourChanCfg, theirChanCfg,
|
||||||
ourChanCfg.DustLimit)
|
localBalance, remoteBalance,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
@ -796,9 +797,10 @@ func CreateCommitmentTxns(localBalance, remoteBalance btcutil.Amount,
|
|||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
theirCommitTx, err := CreateCommitTx(fundingTxIn, remoteCommitmentKeys,
|
theirCommitTx, err := CreateCommitTx(
|
||||||
uint32(theirChanCfg.CsvDelay), remoteBalance, localBalance,
|
fundingTxIn, remoteCommitmentKeys, theirChanCfg, ourChanCfg,
|
||||||
theirChanCfg.DustLimit)
|
remoteBalance, localBalance,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user