From c41017610b45ce81784a000f21a8e06ea8ae05fb Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Thu, 13 Jun 2019 17:28:14 -0700 Subject: [PATCH] htlcswitch/link: backup revoked states to watchtower --- htlcswitch/link.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/htlcswitch/link.go b/htlcswitch/link.go index 6f3a83163..8a17ad331 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -240,6 +240,11 @@ type ChannelLinkConfig struct { // the outgoing broadcast delta, because in any case we don't want to // risk offering an htlc that triggers channel closure. OutgoingCltvRejectDelta uint32 + + // TowerClient is an optional engine that manages the signing, + // encrypting, and uploading of justice transactions to the daemon's + // configured set of watchtowers. + TowerClient TowerClient } // channelLink is the service which drives a channel's commitment update @@ -396,6 +401,15 @@ func (l *channelLink) Start() error { log.Infof("ChannelLink(%v) is starting", l) + // If the config supplied watchtower client, ensure the channel is + // registered before trying to use it during operation. + if l.cfg.TowerClient != nil { + err := l.cfg.TowerClient.RegisterChannel(l.ChanID()) + if err != nil { + return err + } + } + l.mailBox.ResetMessages() l.overflowQueue.Start() l.hodlQueue.Start() @@ -1786,6 +1800,28 @@ func (l *channelLink) handleUpstreamMsg(msg lnwire.Message) { return } + // If we have a tower client, we'll proceed in backing up the + // state that was just revoked. + if l.cfg.TowerClient != nil { + state := l.channel.State() + breachInfo, err := lnwallet.NewBreachRetribution( + state, state.RemoteCommitment.CommitHeight-1, 0, + ) + if err != nil { + l.fail(LinkFailureError{code: ErrInternalError}, + "failed to load breach info: %v", err) + return + } + + chanID := l.ChanID() + err = l.cfg.TowerClient.BackupState(&chanID, breachInfo) + if err != nil { + l.fail(LinkFailureError{code: ErrInternalError}, + "unable to queue breach backup: %v", err) + return + } + } + l.processRemoteSettleFails(fwdPkg, settleFails) needUpdate := l.processRemoteAdds(fwdPkg, adds)