/ peer / daemon_adapters.go
daemon_adapters.go
  1  package peer
  2  
  3  import (
  4  	"fmt"
  5  
  6  	"github.com/btcsuite/btcd/btcec/v2"
  7  	"github.com/btcsuite/btcd/chaincfg/chainhash"
  8  	"github.com/btcsuite/btcd/wire"
  9  	"github.com/lightningnetwork/lnd/chainntnfs"
 10  	"github.com/lightningnetwork/lnd/lnwire"
 11  	"github.com/lightningnetwork/lnd/protofsm"
 12  )
 13  
 14  // MessageSender is an interface that represents an object capable of sending
 15  // p2p messages to a destination.
 16  type MessageSender interface {
 17  	// SendMessages sends the target set of messages to the target peer.
 18  	//
 19  	// TODO(roasbeef): current impl bound to single peer, need server
 20  	// pointer otherwise
 21  	SendMessages(btcec.PublicKey, []lnwire.Message) error
 22  }
 23  
 24  // flexMessageSender is a message sender-like interface that is aware of
 25  // sync/async semantics, and is bound to a single peer.
 26  type flexMessageSender interface {
 27  	// SendMessage sends a variadic number of high-priority messages to the
 28  	// remote peer. The first argument denotes if the method should block
 29  	// until the messages have been sent to the remote peer or an error is
 30  	// returned, otherwise it returns immediately after queuing.
 31  	SendMessage(sync bool, msgs ...lnwire.Message) error
 32  }
 33  
 34  // peerMsgSender implements the MessageSender interface for a single peer.
 35  // It'll return an error if the target public isn't equal to public key of the
 36  // backing peer.
 37  type peerMsgSender struct {
 38  	sender  flexMessageSender
 39  	peerPub btcec.PublicKey
 40  }
 41  
 42  // newPeerMsgSender creates a new instance of a peerMsgSender.
 43  func newPeerMsgSender(peerPub btcec.PublicKey,
 44  	msgSender flexMessageSender) *peerMsgSender {
 45  
 46  	return &peerMsgSender{
 47  		sender:  msgSender,
 48  		peerPub: peerPub,
 49  	}
 50  }
 51  
 52  // SendMessages sends the target set of messages to the target peer.
 53  //
 54  // TODO(roasbeef): current impl bound to single peer, need server pointer
 55  // otherwise?
 56  func (p *peerMsgSender) SendMessages(pub btcec.PublicKey,
 57  	msgs []lnwire.Message) error {
 58  
 59  	if !p.peerPub.IsEqual(&pub) {
 60  		return fmt.Errorf("wrong peer pubkey: got %x, can only send "+
 61  			"to %x", pub.SerializeCompressed(),
 62  			p.peerPub.SerializeCompressed())
 63  	}
 64  
 65  	return p.sender.SendMessage(true, msgs...)
 66  }
 67  
 68  // TxBroadcaster is an interface that represents an object capable of
 69  // broadcasting transactions to the network.
 70  type TxBroadcaster interface {
 71  	// PublishTransaction broadcasts a transaction to the network.
 72  	PublishTransaction(*wire.MsgTx, string) error
 73  }
 74  
 75  // LndAdapterCfg is a struct that holds the configuration for the
 76  // LndDaemonAdapters instance.
 77  type LndAdapterCfg struct {
 78  	// MsgSender is capable of sending messages to an arbitrary peer.
 79  	MsgSender MessageSender
 80  
 81  	// TxBroadcaster is capable of broadcasting a transaction to the
 82  	// network.
 83  	TxBroadcaster TxBroadcaster
 84  
 85  	// ChainNotifier is capable of receiving notifications for on-chain
 86  	// events.
 87  	ChainNotifier chainntnfs.ChainNotifier
 88  }
 89  
 90  // LndDaemonAdapters is a struct that implements the protofsm.DaemonAdapters
 91  // interface using common lnd abstractions.
 92  type LndDaemonAdapters struct {
 93  	cfg LndAdapterCfg
 94  }
 95  
 96  // NewLndDaemonAdapters creates a new instance of the lndDaemonAdapters struct.
 97  func NewLndDaemonAdapters(cfg LndAdapterCfg) *LndDaemonAdapters {
 98  	return &LndDaemonAdapters{
 99  		cfg: cfg,
100  	}
101  }
102  
103  // SendMessages sends the target set of messages to the target peer.
104  func (l *LndDaemonAdapters) SendMessages(pub btcec.PublicKey,
105  	msgs []lnwire.Message) error {
106  
107  	return l.cfg.MsgSender.SendMessages(pub, msgs)
108  }
109  
110  // BroadcastTransaction broadcasts a transaction with the target label.
111  func (l *LndDaemonAdapters) BroadcastTransaction(tx *wire.MsgTx,
112  	label string) error {
113  
114  	return l.cfg.TxBroadcaster.PublishTransaction(tx, label)
115  }
116  
117  // RegisterConfirmationsNtfn registers an intent to be notified once txid
118  // reaches numConfs confirmations.
119  func (l *LndDaemonAdapters) RegisterConfirmationsNtfn(txid *chainhash.Hash,
120  	pkScript []byte, numConfs, heightHint uint32,
121  	opts ...chainntnfs.NotifierOption,
122  ) (*chainntnfs.ConfirmationEvent, error) {
123  
124  	return l.cfg.ChainNotifier.RegisterConfirmationsNtfn(
125  		txid, pkScript, numConfs, heightHint, opts...,
126  	)
127  }
128  
129  // RegisterSpendNtfn registers an intent to be notified once the target
130  // outpoint is successfully spent within a transaction.
131  func (l *LndDaemonAdapters) RegisterSpendNtfn(outpoint *wire.OutPoint,
132  	pkScript []byte, heightHint uint32) (*chainntnfs.SpendEvent, error) {
133  
134  	return l.cfg.ChainNotifier.RegisterSpendNtfn(
135  		outpoint, pkScript, heightHint,
136  	)
137  }
138  
139  // A compile time check to ensure that lndDaemonAdapters fully implements the
140  // DaemonAdapters interface.
141  var _ protofsm.DaemonAdapters = (*LndDaemonAdapters)(nil)