-
Notifications
You must be signed in to change notification settings - Fork 0
/
punisher.go
58 lines (48 loc) · 1.72 KB
/
punisher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package lookout
import (
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/lnwallet"
)
// PunisherConfig houses the resources required by the Punisher.
type PunisherConfig struct {
// PublishTx provides the ability to send a signed transaction to the
// network.
PublishTx func(*wire.MsgTx) error
// 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 "+
"client=%s with breach-txid=%x: %v",
desc.SessionInfo.ID, desc.BreachedCommitTx.TxHash(), err)
return err
}
log.Infof("Publishing justice transaction txid=%x for client=%s",
justiceTxn.TxHash(), desc.SessionInfo.ID)
err = p.cfg.PublishTx(justiceTxn)
if err != nil && err != lnwallet.ErrDoubleSpend {
log.Errorf("Unable to publish justice txn for client=%s",
"with breach-txid=%x: %v",
desc.SessionInfo.ID, desc.BreachedCommitTx.TxHash(), err)
return err
}
// TODO(conner): register for spend and remove from db after
// confirmation
return nil
}