lnwallet+contractcourt: publicly export DeriveCommitmentKeys use in chain watcher
In this commit, we consolidate the number of areas where we derive our commitment keys. Before this commit, the `isOurCommitment` function in the chain watcher used a custom routine to derive the expected scripts/keys for our commitment at that height. With the recent changes, we now have additional logic in `DeriveCommitmentKeys` that wasn't copied over to this area. As a result, the prior logic would erroneously detect if it was our commitment that had hit the chain or not. In this commit, we remove the old custom code, and use `DeriveCommitmentKeys` wihtin the chain watcher as well. This ensures that we only need to maintain the key derivation code in a single place, preventing future bugs of this nature.
This commit is contained in:
3 files changed
+36
-40
No files matched your search
@@ -331,7 +331,7 @@ func (c *chainWatcher) SubscribeChannelEvents() *ChainEventSubscription {
|
||||
// based off of only the set of outputs included.
|
||||
func isOurCommitment(localChanCfg, remoteChanCfg channeldb.ChannelConfig,
|
||||
commitSpend *chainntnfs.SpendDetail, broadcastStateNum uint64,
|
||||
revocationProducer shachain.Producer) (bool, error) {
|
||||
revocationProducer shachain.Producer, tweakless bool) (bool, error) {
|
||||
|
||||
// First, we'll re-derive our commitment point for this state since
|
||||
// this is what we use to randomize each of the keys for this state.
|
||||
@@ -344,14 +344,15 @@ func isOurCommitment(localChanCfg, remoteChanCfg channeldb.ChannelConfig,
|
||||
// Now that we have the commit point, we'll derive the tweaked local
|
||||
// and remote keys for this state. We use our point as only we can
|
||||
// revoke our own commitment.
|
||||
localDelayBasePoint := localChanCfg.DelayBasePoint.PubKey
|
||||
localDelayKey := input.TweakPubKey(localDelayBasePoint, commitPoint)
|
||||
remoteNonDelayPoint := remoteChanCfg.PaymentBasePoint.PubKey
|
||||
remotePayKey := input.TweakPubKey(remoteNonDelayPoint, commitPoint)
|
||||
commitKeyRing := lnwallet.DeriveCommitmentKeys(
|
||||
commitPoint, true, tweakless, &localChanCfg, &remoteChanCfg,
|
||||
)
|
||||
|
||||
// With the keys derived, we'll construct the remote script that'll be
|
||||
// present if they have a non-dust balance on the commitment.
|
||||
remotePkScript, err := input.CommitScriptUnencumbered(remotePayKey)
|
||||
remotePkScript, err := input.CommitScriptUnencumbered(
|
||||
commitKeyRing.NoDelayKey,
|
||||
)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
@@ -359,11 +360,9 @@ func isOurCommitment(localChanCfg, remoteChanCfg channeldb.ChannelConfig,
|
||||
// Next, we'll derive our script that includes the revocation base for
|
||||
// the remote party allowing them to claim this output before the CSV
|
||||
// delay if we breach.
|
||||
revocationKey := input.DeriveRevocationPubkey(
|
||||
remoteChanCfg.RevocationBasePoint.PubKey, commitPoint,
|
||||
)
|
||||
localScript, err := input.CommitScriptToSelf(
|
||||
uint32(localChanCfg.CsvDelay), localDelayKey, revocationKey,
|
||||
uint32(localChanCfg.CsvDelay), commitKeyRing.DelayKey,
|
||||
commitKeyRing.RevocationKey,
|
||||
)
|
||||
if err != nil {
|
||||
return false, err
|
||||
@@ -423,6 +422,11 @@ func (c *chainWatcher) closeObserver(spendNtfn *chainntnfs.SpendEvent) {
|
||||
// revoked state...!!!
|
||||
commitTxBroadcast := commitSpend.SpendingTx
|
||||
|
||||
// An additional piece of information we need to properly
|
||||
// dispatch a close event if is this channel was using the
|
||||
// tweakless remove key format or not.
|
||||
tweaklessCommit := c.cfg.chanState.ChanType.IsTweakless()
|
||||
|
||||
localCommit, remoteCommit, err := c.cfg.chanState.LatestCommitments()
|
||||
if err != nil {
|
||||
log.Errorf("Unable to fetch channel state for "+
|
||||
@@ -480,6 +484,7 @@ func (c *chainWatcher) closeObserver(spendNtfn *chainntnfs.SpendEvent) {
|
||||
c.cfg.chanState.LocalChanCfg,
|
||||
c.cfg.chanState.RemoteChanCfg, commitSpend,
|
||||
broadcastStateNum, c.cfg.chanState.RevocationProducer,
|
||||
tweaklessCommit,
|
||||
)
|
||||
if err != nil {
|
||||
log.Errorf("unable to determine self commit for "+
|
||||
@@ -584,9 +589,6 @@ func (c *chainWatcher) closeObserver(spendNtfn *chainntnfs.SpendEvent) {
|
||||
"state #%v!!! Attempting recovery...",
|
||||
broadcastStateNum, remoteStateNum)
|
||||
|
||||
tweaklessCommit := (c.cfg.chanState.ChanType ==
|
||||
channeldb.SingleFunderTweakless)
|
||||
|
||||
// If this isn't a tweakless commitment, then we'll
|
||||
// need to wait for the remote party's latest unrevoked
|
||||
// commitment point to be presented to us as we need
|
||||
|
||||
Reference in new issue
Block a user