github.com/decred/dcrlnd@v0.7.6/watchtower/lookout/punisher.go (about)

     1  package lookout
     2  
     3  import (
     4  	"github.com/decred/dcrd/wire"
     5  	"github.com/decred/dcrlnd/labels"
     6  )
     7  
     8  // PunisherConfig houses the resources required by the Punisher.
     9  type PunisherConfig struct {
    10  	// PublishTx provides the ability to send a signed transaction to the
    11  	// network.
    12  	PublishTx func(*wire.MsgTx, string) error
    13  
    14  	// TODO(conner) add DB tracking and spend ntfn registration to see if
    15  	// ours confirmed or not
    16  }
    17  
    18  // BreachPunisher handles the responsibility of constructing and broadcasting
    19  // justice transactions. Justice transactions are constructed from previously
    20  // accepted state updates uploaded by the watchtower's clients.
    21  type BreachPunisher struct {
    22  	cfg *PunisherConfig
    23  }
    24  
    25  // NewBreachPunisher constructs a new BreachPunisher given a PunisherConfig.
    26  func NewBreachPunisher(cfg *PunisherConfig) *BreachPunisher {
    27  	return &BreachPunisher{
    28  		cfg: cfg,
    29  	}
    30  }
    31  
    32  // Punish constructs a justice transaction given a JusticeDescriptor and
    33  // publishes is it to the network.
    34  func (p *BreachPunisher) Punish(desc *JusticeDescriptor, quit <-chan struct{}) error {
    35  	justiceTxn, err := desc.CreateJusticeTxn()
    36  	if err != nil {
    37  		log.Errorf("Unable to create justice txn for "+
    38  			"client=%s with breach-txid=%s: %v",
    39  			desc.SessionInfo.ID, desc.BreachedCommitTx.TxHash(), err)
    40  		return err
    41  	}
    42  
    43  	log.Infof("Publishing justice transaction for client=%s with txid=%s",
    44  		desc.SessionInfo.ID, justiceTxn.TxHash())
    45  
    46  	label := labels.MakeLabel(labels.LabelTypeJusticeTransaction, nil)
    47  	err = p.cfg.PublishTx(justiceTxn, label)
    48  	if err != nil {
    49  		log.Errorf("Unable to publish justice txn for client=%s"+
    50  			"with breach-txid=%s: %v",
    51  			desc.SessionInfo.ID, desc.BreachedCommitTx.TxHash(), err)
    52  		return err
    53  	}
    54  
    55  	// TODO(conner): register for spend and remove from db after
    56  	// confirmation
    57  
    58  	return nil
    59  }