forked from lightningnetwork/lnd
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhtlc_outgoing_contest_resolver_test.go
254 lines (209 loc) · 6.3 KB
/
htlc_outgoing_contest_resolver_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package contractcourt
import (
"fmt"
"testing"
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/graph/db/models"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lnmock"
"github.com/lightningnetwork/lnd/lntest/mock"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/stretchr/testify/require"
)
const (
outgoingContestHtlcExpiry = 110
)
// TestHtlcOutgoingResolverTimeout tests resolution of an offered htlc that
// timed out.
func TestHtlcOutgoingResolverTimeout(t *testing.T) {
t.Parallel()
defer timeout()()
// Setup the resolver with our test resolution.
ctx := newOutgoingResolverTestContext(t)
// Start the resolution process in a goroutine.
ctx.resolve()
// Notify arrival of the block after which the timeout path of the htlc
// unlocks.
ctx.notifyEpoch(outgoingContestHtlcExpiry)
// Assert that the resolver finishes without error and transforms in a
// timeout resolver.
ctx.waitForResult(true)
}
// TestHtlcOutgoingResolverRemoteClaim tests resolution of an offered htlc that
// is claimed by the remote party.
func TestHtlcOutgoingResolverRemoteClaim(t *testing.T) {
t.Parallel()
defer timeout()()
// Setup the resolver with our test resolution and start the resolution
// process.
ctx := newOutgoingResolverTestContext(t)
// Replace our mocked checkpoint function with one which will push
// reports into a channel for us to consume. We do so on the resolver
// level because our test context has already created the resolver.
reportChan := make(chan *channeldb.ResolverReport)
ctx.resolver.Checkpoint = func(_ ContractResolver,
reports ...*channeldb.ResolverReport) error {
// Send all of our reports into the channel.
for _, report := range reports {
reportChan <- report
}
return nil
}
ctx.resolve()
// The remote party sweeps the htlc. Notify our resolver of this event.
preimage := lntypes.Preimage{}
spendTx := &wire.MsgTx{
TxIn: []*wire.TxIn{
{
Witness: [][]byte{
{0}, {1}, {2}, preimage[:],
},
},
},
}
spendHash := spendTx.TxHash()
ctx.notifier.SpendChan <- &chainntnfs.SpendDetail{
SpendingTx: spendTx,
SpenderTxHash: &spendHash,
}
// We expect the extracted preimage to be added to the witness beacon.
<-ctx.preimageDB.newPreimages
// We also expect a resolution message to the incoming side of the
// circuit.
<-ctx.resolutionChan
// Finally, check that we have a report as expected.
expectedReport := &channeldb.ResolverReport{
OutPoint: wire.OutPoint{},
Amount: 0,
ResolverType: channeldb.ResolverTypeOutgoingHtlc,
ResolverOutcome: channeldb.ResolverOutcomeClaimed,
SpendTxID: &spendHash,
}
assertResolverReport(t, reportChan, expectedReport)
// Assert that the resolver finishes without error.
ctx.waitForResult(false)
}
type resolveResult struct {
err error
nextResolver ContractResolver
}
type outgoingResolverTestContext struct {
resolver *htlcOutgoingContestResolver
notifier *mock.ChainNotifier
preimageDB *mockWitnessBeacon
resolverResultChan chan resolveResult
resolutionChan chan ResolutionMsg
t *testing.T
}
func newOutgoingResolverTestContext(t *testing.T) *outgoingResolverTestContext {
notifier := &mock.ChainNotifier{
EpochChan: make(chan *chainntnfs.BlockEpoch),
SpendChan: make(chan *chainntnfs.SpendDetail),
ConfChan: make(chan *chainntnfs.TxConfirmation),
}
checkPointChan := make(chan struct{}, 1)
resolutionChan := make(chan ResolutionMsg, 1)
preimageDB := newMockWitnessBeacon()
onionProcessor := &mockOnionProcessor{}
chainCfg := ChannelArbitratorConfig{
ChainArbitratorConfig: ChainArbitratorConfig{
Notifier: notifier,
PreimageDB: preimageDB,
DeliverResolutionMsg: func(msgs ...ResolutionMsg) error {
if len(msgs) != 1 {
return fmt.Errorf("expected 1 "+
"resolution msg, instead got %v",
len(msgs))
}
resolutionChan <- msgs[0]
return nil
},
OnionProcessor: onionProcessor,
Budget: *DefaultBudgetConfig(),
QueryIncomingCircuit: func(
circuit models.CircuitKey) *models.CircuitKey {
return nil
},
ChainIO: &mock.ChainIO{},
},
PutResolverReport: func(_ kvdb.RwTx,
_ *channeldb.ResolverReport) error {
return nil
},
}
outgoingRes := lnwallet.OutgoingHtlcResolution{
Expiry: outgoingContestHtlcExpiry,
SweepSignDesc: input.SignDescriptor{
Output: &wire.TxOut{},
},
}
cfg := ResolverConfig{
ChannelArbitratorConfig: chainCfg,
Checkpoint: func(_ ContractResolver,
_ ...*channeldb.ResolverReport) error {
checkPointChan <- struct{}{}
return nil
},
}
resolver := &htlcOutgoingContestResolver{
htlcTimeoutResolver: &htlcTimeoutResolver{
contractResolverKit: *newContractResolverKit(cfg),
htlcResolution: outgoingRes,
htlc: channeldb.HTLC{
Amt: lnwire.MilliSatoshi(testHtlcAmount),
RHash: testResHash,
OnionBlob: lnmock.MockOnion(),
},
},
}
resolver.initLogger("htlcOutgoingContestResolver")
return &outgoingResolverTestContext{
resolver: resolver,
notifier: notifier,
preimageDB: preimageDB,
resolutionChan: resolutionChan,
t: t,
}
}
func (i *outgoingResolverTestContext) resolve() {
// Start resolver.
i.resolverResultChan = make(chan resolveResult, 1)
go func() {
err := i.resolver.Launch()
require.NoError(i.t, err)
nextResolver, err := i.resolver.Resolve()
i.resolverResultChan <- resolveResult{
nextResolver: nextResolver,
err: err,
}
}()
// Notify initial block height.
i.notifyEpoch(testInitialBlockHeight)
}
func (i *outgoingResolverTestContext) notifyEpoch(height int32) {
i.notifier.EpochChan <- &chainntnfs.BlockEpoch{
Height: height,
}
}
func (i *outgoingResolverTestContext) waitForResult(expectTimeoutRes bool) {
i.t.Helper()
result := <-i.resolverResultChan
if result.err != nil {
i.t.Fatal(result.err)
}
if !expectTimeoutRes {
if result.nextResolver != nil {
i.t.Fatal("expected no next resolver")
}
return
}
_, ok := result.nextResolver.(*htlcTimeoutResolver)
if !ok {
i.t.Fatal("expected htlcTimeoutResolver")
}
}