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

lightningnetwork / lnd / 13302325565

13 Feb 2025 07:09AM UTC coverage: 58.859% (+1.2%) from 57.696%
13302325565

Pull #9448

github

yyforyongyu
sweep: fix error logging
Pull Request #9448: sweep: properly handle failed sweeping txns

274 of 305 new or added lines in 4 files covered. (89.84%)

20 existing lines in 7 files now uncovered.

136446 of 231819 relevant lines covered (58.86%)

19230.81 hits per line

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

79.61
/watchtower/wtmock/peer.go
1
package wtmock
2

3
import (
4
        "fmt"
5
        "net"
6
        "time"
7

8
        "github.com/btcsuite/btcd/btcec/v2"
9
        "github.com/lightningnetwork/lnd/watchtower/wtserver"
10
)
11

12
// MockPeer emulates a single endpoint of brontide transport.
13
type MockPeer struct {
14
        remotePub  *btcec.PublicKey
15
        remoteAddr net.Addr
16
        localPub   *btcec.PublicKey
17
        localAddr  net.Addr
18

19
        IncomingMsgs chan []byte
20
        OutgoingMsgs chan []byte
21

22
        writeDeadline <-chan time.Time
23
        readDeadline  <-chan time.Time
24

25
        RemoteQuit chan struct{}
26
        Quit       chan struct{}
27
}
28

29
// NewMockPeer returns a fresh MockPeer.
30
func NewMockPeer(lpk, rpk *btcec.PublicKey, addr net.Addr,
31
        bufferSize int) *MockPeer {
37✔
32

37✔
33
        return &MockPeer{
37✔
34
                remotePub:  rpk,
37✔
35
                remoteAddr: addr,
37✔
36
                localAddr: &net.TCPAddr{
37✔
37
                        IP:   net.IP{0x32, 0x31, 0x30, 0x29},
37✔
38
                        Port: 36723,
37✔
39
                },
37✔
40
                localPub:     lpk,
37✔
41
                IncomingMsgs: make(chan []byte, bufferSize),
37✔
42
                OutgoingMsgs: make(chan []byte, bufferSize),
37✔
43
                Quit:         make(chan struct{}),
37✔
44
        }
37✔
45
}
37✔
46

47
// NewMockConn establishes a bidirectional connection between two MockPeers.
48
func NewMockConn(localPk, remotePk *btcec.PublicKey,
49
        localAddr, remoteAddr net.Addr,
50
        bufferSize int) (*MockPeer, *MockPeer) {
443✔
51

443✔
52
        localPeer := &MockPeer{
443✔
53
                remotePub:    remotePk,
443✔
54
                remoteAddr:   remoteAddr,
443✔
55
                localPub:     localPk,
443✔
56
                localAddr:    localAddr,
443✔
57
                IncomingMsgs: make(chan []byte, bufferSize),
443✔
58
                OutgoingMsgs: make(chan []byte, bufferSize),
443✔
59
                Quit:         make(chan struct{}),
443✔
60
        }
443✔
61

443✔
62
        remotePeer := &MockPeer{
443✔
63
                remotePub:    localPk,
443✔
64
                remoteAddr:   localAddr,
443✔
65
                localPub:     remotePk,
443✔
66
                localAddr:    remoteAddr,
443✔
67
                IncomingMsgs: localPeer.OutgoingMsgs,
443✔
68
                OutgoingMsgs: localPeer.IncomingMsgs,
443✔
69
                Quit:         make(chan struct{}),
443✔
70
        }
443✔
71

443✔
72
        localPeer.RemoteQuit = remotePeer.Quit
443✔
73
        remotePeer.RemoteQuit = localPeer.Quit
443✔
74

443✔
75
        return localPeer, remotePeer
443✔
76
}
443✔
77

78
// Write sends the raw bytes as the next full message read to the remote peer.
79
// The write will fail if either party closes the connection or the write
80
// deadline expires. The passed bytes slice is copied before sending, thus the
81
// bytes may be reused once the method returns.
82
func (p *MockPeer) Write(b []byte) (n int, err error) {
2,285✔
83
        bb := make([]byte, len(b))
2,285✔
84
        copy(bb, b)
2,285✔
85

2,285✔
86
        select {
2,285✔
87
        case p.OutgoingMsgs <- bb:
2,282✔
88
                return len(b), nil
2,282✔
89
        case <-p.writeDeadline:
3✔
90
                return 0, fmt.Errorf("write timeout expired")
3✔
UNCOV
91
        case <-p.RemoteQuit:
×
UNCOV
92
                return 0, fmt.Errorf("remote closed connected")
×
93
        case <-p.Quit:
×
94
                return 0, fmt.Errorf("connection closed")
×
95
        }
96
}
97

98
// Close tears down the connection, and fails any pending reads or writes.
99
func (p *MockPeer) Close() error {
791✔
100
        select {
791✔
101
        case <-p.Quit:
×
102
                return fmt.Errorf("connection already closed")
×
103
        default:
791✔
104
                close(p.Quit)
791✔
105
                return nil
791✔
106
        }
107
}
108

109
// ReadNextMessage returns the raw bytes of the next full message read from the
110
// remote peer. The read will fail if either party closes the connection or the
111
// read deadline expires.
112
func (p *MockPeer) ReadNextMessage() ([]byte, error) {
2,540✔
113
        select {
2,540✔
114
        case b := <-p.IncomingMsgs:
2,284✔
115
                return b, nil
2,284✔
116
        case <-p.readDeadline:
7✔
117
                return nil, fmt.Errorf("read timeout expired")
7✔
118
        case <-p.RemoteQuit:
249✔
119
                return nil, fmt.Errorf("remote closed connected")
249✔
120
        case <-p.Quit:
×
121
                return nil, fmt.Errorf("connection closed")
×
122
        }
123
}
124

125
// SetWriteDeadline initializes a timer that will cause any pending writes to
126
// fail at time t. If t is zero, the deadline is infinite.
127
func (p *MockPeer) SetWriteDeadline(t time.Time) error {
2,285✔
128
        if t.IsZero() {
2,285✔
129
                p.writeDeadline = nil
×
130
                return nil
×
131
        }
×
132

133
        duration := time.Until(t)
2,285✔
134
        p.writeDeadline = time.After(duration)
2,285✔
135

2,285✔
136
        return nil
2,285✔
137
}
138

139
// SetReadDeadline initializes a timer that will cause any pending reads to fail
140
// at time t. If t is zero, the deadline is infinite.
141
func (p *MockPeer) SetReadDeadline(t time.Time) error {
2,540✔
142
        if t.IsZero() {
2,540✔
143
                p.readDeadline = nil
×
144
                return nil
×
145
        }
×
146

147
        duration := time.Until(t)
2,540✔
148
        p.readDeadline = time.After(duration)
2,540✔
149

2,540✔
150
        return nil
2,540✔
151
}
152

153
// RemotePub returns the public key of the remote peer.
154
func (p *MockPeer) RemotePub() *btcec.PublicKey {
5,718✔
155
        return p.remotePub
5,718✔
156
}
5,718✔
157

158
// RemoteAddr returns the net address of the remote peer.
159
func (p *MockPeer) RemoteAddr() net.Addr {
6,185✔
160
        return p.remoteAddr
6,185✔
161
}
6,185✔
162

163
// LocalAddr returns the local net address of the peer.
164
func (p *MockPeer) LocalAddr() net.Addr {
×
165
        return p.localAddr
×
166
}
×
167

168
// Read is not implemented.
169
func (p *MockPeer) Read(dst []byte) (int, error) {
×
170
        panic("not implemented")
×
171
}
172

173
// SetDeadline is not implemented.
174
func (p *MockPeer) SetDeadline(t time.Time) error {
×
175
        panic("not implemented")
×
176
}
177

178
// Compile-time constraint ensuring the MockPeer implements the wserver.Peer
179
// interface.
180
var _ wtserver.Peer = (*MockPeer)(nil)
181

182
// Compile-time constraint ensuring the MockPeer implements the net.Conn
183
// interface.
184
var _ net.Conn = (*MockPeer)(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