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

lightningnetwork / lnd / 12807768804

16 Jan 2025 11:16AM UTC coverage: 58.746% (+0.04%) from 58.709%
12807768804

Pull #9232

github

Abdulkbk
docs: add release note
Pull Request #9232: chanbackup: archive old channel backup files

55 of 70 new or added lines in 3 files covered. (78.57%)

27 existing lines in 10 files now uncovered.

135505 of 230662 relevant lines covered (58.75%)

19223.88 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) {
436✔
51

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

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

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

436✔
75
        return localPeer, remotePeer
436✔
76
}
436✔
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,272✔
83
        bb := make([]byte, len(b))
2,272✔
84
        copy(bb, b)
2,272✔
85

2,272✔
86
        select {
2,272✔
87
        case p.OutgoingMsgs <- bb:
2,269✔
88
                return len(b), nil
2,269✔
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 {
776✔
100
        select {
776✔
101
        case <-p.Quit:
×
102
                return fmt.Errorf("connection already closed")
×
103
        default:
776✔
104
                close(p.Quit)
776✔
105
                return nil
776✔
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,528✔
113
        select {
2,528✔
114
        case b := <-p.IncomingMsgs:
2,271✔
115
                return b, nil
2,271✔
116
        case <-p.readDeadline:
7✔
117
                return nil, fmt.Errorf("read timeout expired")
7✔
118
        case <-p.RemoteQuit:
250✔
119
                return nil, fmt.Errorf("remote closed connected")
250✔
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,272✔
128
        if t.IsZero() {
2,272✔
129
                p.writeDeadline = nil
×
130
                return nil
×
131
        }
×
132

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

2,272✔
136
        return nil
2,272✔
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,528✔
142
        if t.IsZero() {
2,528✔
143
                p.readDeadline = nil
×
144
                return nil
×
145
        }
×
146

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

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

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

158
// RemoteAddr returns the net address of the remote peer.
159
func (p *MockPeer) RemoteAddr() net.Addr {
6,145✔
160
        return p.remoteAddr
6,145✔
161
}
6,145✔
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