• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 13930092738

18 Mar 2025 05:51PM UTC coverage: 68.791% (+0.2%) from 68.624%
13930092738

Pull #9610

github

web-flow
Merge 42fa83700 into c292acfe7
Pull Request #9610: multi: integrate rbf changes from staging branch

1132 of 1435 new or added lines in 22 files covered. (78.89%)

84 existing lines in 17 files now uncovered.

131486 of 191139 relevant lines covered (68.79%)

23420.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.0
/peer/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 {
3✔
45

3✔
46
        return &peerMsgSender{
3✔
47
                sender:  msgSender,
3✔
48
                peerPub: peerPub,
3✔
49
        }
3✔
50
}
3✔
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 {
3✔
58

3✔
59
        if !p.peerPub.IsEqual(&pub) {
3✔
NEW
60
                return fmt.Errorf("wrong peer pubkey: got %x, can only send "+
×
NEW
61
                        "to %x", pub.SerializeCompressed(),
×
NEW
62
                        p.peerPub.SerializeCompressed())
×
NEW
63
        }
×
64

65
        return p.sender.SendMessage(true, msgs...)
3✔
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 {
3✔
98
        return &LndDaemonAdapters{
3✔
99
                cfg: cfg,
3✔
100
        }
3✔
101
}
3✔
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 {
3✔
106

3✔
107
        return l.cfg.MsgSender.SendMessages(pub, msgs)
3✔
108
}
3✔
109

110
// BroadcastTransaction broadcasts a transaction with the target label.
111
func (l *LndDaemonAdapters) BroadcastTransaction(tx *wire.MsgTx,
112
        label string) error {
3✔
113

3✔
114
        return l.cfg.TxBroadcaster.PublishTransaction(tx, label)
3✔
115
}
3✔
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,
NEW
122
) (*chainntnfs.ConfirmationEvent, error) {
×
NEW
123

×
NEW
124
        return l.cfg.ChainNotifier.RegisterConfirmationsNtfn(
×
NEW
125
                txid, pkScript, numConfs, heightHint, opts...,
×
NEW
126
        )
×
NEW
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) {
3✔
133

3✔
134
        return l.cfg.ChainNotifier.RegisterSpendNtfn(
3✔
135
                outpoint, pkScript, heightHint,
3✔
136
        )
3✔
137
}
3✔
138

139
// A compile time check to ensure that lndDaemonAdapters fully implements the
140
// DaemonAdapters interface.
141
var _ protofsm.DaemonAdapters = (*LndDaemonAdapters)(nil)
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc