2018-11-01 04:42:11 +01:00
|
|
|
package lookout
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
2020-07-29 09:27:22 +02:00
|
|
|
"github.com/lightningnetwork/lnd/labels"
|
2018-11-01 04:42:11 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// PunisherConfig houses the resources required by the Punisher.
|
|
|
|
type PunisherConfig struct {
|
|
|
|
// PublishTx provides the ability to send a signed transaction to the
|
|
|
|
// network.
|
2020-05-18 14:13:23 +02:00
|
|
|
PublishTx func(*wire.MsgTx, string) error
|
2018-11-01 04:42:11 +01:00
|
|
|
|
|
|
|
// TODO(conner) add DB tracking and spend ntfn registration to see if
|
|
|
|
// ours confirmed or not
|
|
|
|
}
|
|
|
|
|
|
|
|
// BreachPunisher handles the responsibility of constructing and broadcasting
|
|
|
|
// justice transactions. Justice transactions are constructed from previously
|
|
|
|
// accepted state updates uploaded by the watchtower's clients.
|
|
|
|
type BreachPunisher struct {
|
|
|
|
cfg *PunisherConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBreachPunisher constructs a new BreachPunisher given a PunisherConfig.
|
|
|
|
func NewBreachPunisher(cfg *PunisherConfig) *BreachPunisher {
|
|
|
|
return &BreachPunisher{
|
|
|
|
cfg: cfg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Punish constructs a justice transaction given a JusticeDescriptor and
|
|
|
|
// publishes is it to the network.
|
|
|
|
func (p *BreachPunisher) Punish(desc *JusticeDescriptor, quit <-chan struct{}) error {
|
|
|
|
justiceTxn, err := desc.CreateJusticeTxn()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Unable to create justice txn for "+
|
2018-12-20 06:50:42 +01:00
|
|
|
"client=%s with breach-txid=%s: %v",
|
2018-11-01 04:42:11 +01:00
|
|
|
desc.SessionInfo.ID, desc.BreachedCommitTx.TxHash(), err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-20 06:50:42 +01:00
|
|
|
log.Infof("Publishing justice transaction for client=%s with txid=%s",
|
|
|
|
desc.SessionInfo.ID, justiceTxn.TxHash())
|
2018-11-01 04:42:11 +01:00
|
|
|
|
2020-07-29 09:27:22 +02:00
|
|
|
label := labels.MakeLabel(labels.LabelTypeJusticeTransaction, nil)
|
|
|
|
err = p.cfg.PublishTx(justiceTxn, label)
|
2019-02-21 00:06:57 +01:00
|
|
|
if err != nil {
|
2018-11-30 06:25:53 +01:00
|
|
|
log.Errorf("Unable to publish justice txn for client=%s"+
|
2018-12-20 06:50:42 +01:00
|
|
|
"with breach-txid=%s: %v",
|
2018-11-01 04:42:11 +01:00
|
|
|
desc.SessionInfo.ID, desc.BreachedCommitTx.TxHash(), err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(conner): register for spend and remove from db after
|
|
|
|
// confirmation
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|