-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathrpc_fees_test.go
More file actions
602 lines (535 loc) · 15.8 KB
/
Copy pathrpc_fees_test.go
File metadata and controls
602 lines (535 loc) · 15.8 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
package waved
import (
"errors"
"math"
"testing"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil/v2"
"github.com/btcsuite/btcd/chainhash/v2"
"github.com/btcsuite/btclog/v2"
"github.com/lightninglabs/wavelength/arkrpc"
"github.com/lightninglabs/wavelength/db/sqlc"
"github.com/lightninglabs/wavelength/ledger"
"github.com/lightninglabs/wavelength/waverpc"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// TestEstimateFeeValidatesRequest covers the three pre-RPC
// rejection paths on EstimateFee: nil request, non-positive
// amount, and a nil upstream connection. Each path must return
// a gRPC status with a stable code so automated callers can
// retry or surface the right error without having to parse
// strings.
func TestEstimateFeeValidatesRequest(t *testing.T) {
t.Parallel()
cases := []struct {
name string
r *RPCServer
req *waverpc.EstimateFeeRequest
code codes.Code
}{
{
name: "nil request",
r: newTestRPCServer(),
req: nil,
code: codes.InvalidArgument,
},
{
name: "zero amount",
r: newTestRPCServer(),
req: &waverpc.EstimateFeeRequest{
AmountSat: 0,
},
code: codes.InvalidArgument,
},
{
name: "negative amount",
r: newTestRPCServer(),
req: &waverpc.EstimateFeeRequest{
AmountSat: -1,
},
code: codes.InvalidArgument,
},
{
name: "nil serverConn",
r: newTestRPCServer(),
req: &waverpc.EstimateFeeRequest{
AmountSat: 10_000,
},
code: codes.Unavailable,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_, err := tc.r.EstimateFee(t.Context(), tc.req)
require.Error(t, err)
require.Equal(
t, tc.code, status.Code(err),
"expected %s, got %s: %v", tc.code,
status.Code(err), err,
)
})
}
}
// TestGetFeeHistoryValidatesRequest covers GetFeeHistory's
// pre-read guards: nil request, an offset that would overflow
// int32, and a missing ledger store. Each returns a gRPC
// status; the offset overflow in particular is the fix the
// other agent landed in a fixup commit and this test pins that
// contract.
func TestGetFeeHistoryValidatesRequest(t *testing.T) {
t.Parallel()
cases := []struct {
name string
req *waverpc.GetFeeHistoryRequest
code codes.Code
}{
{
name: "nil request",
req: nil,
code: codes.InvalidArgument,
},
{
name: "offset overflow",
req: &waverpc.GetFeeHistoryRequest{
Offset: uint32(math.MaxInt32) + 1,
},
code: codes.InvalidArgument,
},
{
name: "nil ledger store",
req: &waverpc.GetFeeHistoryRequest{
Limit: 10,
},
code: codes.Unavailable,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
r := newTestRPCServer()
_, err := r.GetFeeHistory(t.Context(), tc.req)
require.Error(t, err)
require.Equal(
t, tc.code, status.Code(err),
"expected %s, got %s: %v", tc.code,
status.Code(err), err,
)
})
}
}
// TestListTransactionsValidatesRequest covers ListTransactions' pre-read
// guards so malformed pagination, date windows, and type filters fail before
// the DB layer is touched.
func TestListTransactionsValidatesRequest(t *testing.T) {
t.Parallel()
cases := []struct {
name string
req *waverpc.ListTransactionsRequest
code codes.Code
}{
{
name: "nil request",
req: nil,
code: codes.InvalidArgument,
},
{
name: "offset overflow",
req: &waverpc.ListTransactionsRequest{
Offset: uint32(math.MaxInt32) + 1,
},
code: codes.InvalidArgument,
},
{
name: "negative from",
req: &waverpc.ListTransactionsRequest{
FromUnixS: -1,
},
code: codes.InvalidArgument,
},
{
name: "negative to",
req: &waverpc.ListTransactionsRequest{
ToUnixS: -1,
},
code: codes.InvalidArgument,
},
{
name: "inverted window",
req: &waverpc.ListTransactionsRequest{
FromUnixS: 20,
ToUnixS: 10,
},
code: codes.InvalidArgument,
},
{
name: "unknown type",
req: &waverpc.ListTransactionsRequest{
Type: "bogus",
},
code: codes.InvalidArgument,
},
{
name: "nil ledger store",
req: &waverpc.ListTransactionsRequest{
Limit: 10,
},
code: codes.Unavailable,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
r := newTestRPCServer()
_, err := r.ListTransactions(t.Context(), tc.req)
require.Error(t, err)
require.Equal(
t, tc.code, status.Code(err),
"expected %s, got %s: %v", tc.code,
status.Code(err), err,
)
})
}
}
// TestProxyUpstreamErrorPreservesCode verifies that a gRPC
// status error from the operator keeps its code while the
// message is replaced with a generic string. This matters for
// retry logic (codes.Unavailable is retryable, codes.InvalidArgument
// is not) and for not leaking operator internals in error text.
func TestProxyUpstreamErrorPreservesCode(t *testing.T) {
t.Parallel()
cases := []struct {
name string
in error
code codes.Code
}{
{
name: "grpc unavailable",
in: status.Error(
codes.Unavailable,
"internal backend x failed at host foo:5432",
),
code: codes.Unavailable,
},
{
name: "grpc invalid argument",
in: status.Error(
codes.InvalidArgument, "amount too large",
),
code: codes.InvalidArgument,
},
{
name: "non-grpc network error",
in: errors.New("dial tcp: i/o timeout"),
code: codes.Unavailable,
},
{
name: "nil passthrough",
in: nil,
code: codes.OK,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
out := proxyUpstreamError(tc.in, "generic message")
if tc.in == nil {
require.Nil(t, out)
return
}
require.Error(t, out)
require.Equal(
t, tc.code, status.Code(out),
"status code must be preserved",
)
require.Equal(
t, "generic message",
status.Convert(out).Message(),
"upstream message must not leak",
)
})
}
}
// TestLedgerEntryToProtoOORSession verifies that a ledger row carrying
// a 32-byte session_id (OOR send leg) maps every column to the correct
// proto field, including the newly-added debit/credit account names
// and bytes-typed identifiers.
func TestLedgerEntryToProtoOORSession(t *testing.T) {
t.Parallel()
sessionID := make([]byte, 32)
for i := range sessionID {
sessionID[i] = byte(i + 1)
}
row := &sqlc.LedgerEntry{
EntryID: 42,
DebitAccount: ledger.AccountTransfersOut,
CreditAccount: ledger.AccountVTXOBalance,
AmountSat: 12_345,
RoundID: nil,
SessionID: sessionID,
EventType: ledger.EventVTXOSent,
Description: "oor send leg",
CreatedAt: 1_700_000_000,
}
got := ledgerEntryToProto(row)
require.Equal(t, int64(42), got.EntryId)
require.Equal(t, ledger.EventVTXOSent, got.EventType)
require.Equal(t, int64(12_345), got.AmountSat)
require.Equal(t, "oor send leg", got.Description)
require.Equal(t, int64(1_700_000_000), got.CreatedAtUnixS)
require.Equal(t, ledger.AccountTransfersOut, got.DebitAccount)
require.Equal(t, ledger.AccountVTXOBalance, got.CreditAccount)
require.Empty(t, got.RoundId)
require.Equal(t, sessionID, got.SessionId)
}
// TestLedgerEntryToProtoRoundFee verifies that an in-round boarding
// fee row carries round_id (16 bytes) and an empty session_id, so
// downstream consumers can route on length.
func TestLedgerEntryToProtoRoundFee(t *testing.T) {
t.Parallel()
roundID := make([]byte, 16)
for i := range roundID {
roundID[i] = byte(i + 100)
}
row := &sqlc.LedgerEntry{
EntryID: 7,
DebitAccount: ledger.AccountFeesPaid,
CreditAccount: ledger.AccountVTXOBalance,
AmountSat: 500,
RoundID: roundID,
SessionID: nil,
EventType: ledger.EventBoardingFeePaid,
Description: "boarding fee",
CreatedAt: 1_700_000_100,
}
got := ledgerEntryToProto(row)
require.Equal(t, ledger.AccountFeesPaid, got.DebitAccount)
require.Equal(t, ledger.AccountVTXOBalance, got.CreditAccount)
require.Equal(t, ledger.EventBoardingFeePaid, got.EventType)
require.Equal(t, roundID, got.RoundId)
require.Empty(t, got.SessionId)
}
// newFakeOperatorResponse builds a canned GetInfoResponse with a
// valid (parseable) pubkey and the given MinOperatorFee. Shared by
// the autoRefreshFeeQuoter degraded-mode tests below.
func newFakeOperatorResponse(t *testing.T,
minOperatorFee int64) *arkrpc.GetInfoResponse {
t.Helper()
priv, err := btcec.NewPrivateKey()
require.NoError(t, err)
return &arkrpc.GetInfoResponse{
Pubkey: priv.PubKey().SerializeCompressed(),
VtxoExitDelay: 144,
MinOperatorFee: minOperatorFee,
}
}
// TestAutoRefreshFeeQuoterQuoteFailsFallsBackToMinFee verifies that
// when the operator is reachable for GetInfo (so MinOperatorFee is
// known) but the EstimateFee RPC fails, the quoter returns the
// legacy MinOperatorFee rather than silently collapsing to zero.
// Silent zero would fail the server's #269 validateOperatorFee
// under a non-zero fee schedule, and the VTXO would eventually
// tip over into unilateral exit.
func TestAutoRefreshFeeQuoterQuoteFailsFallsBackToMinFee(t *testing.T) {
t.Parallel()
const wantMinFee = btcutil.Amount(500)
svc := &fakeArkService{
getInfoResponse: newFakeOperatorResponse(
t, int64(wantMinFee),
),
estimateFeeErr: errors.New("operator overload"),
}
s := &Server{
serverConn: newBufconnClient(t, svc),
log: btclog.Disabled,
}
got := s.autoRefreshFeeQuoter()(
t.Context(), 100_000, 200,
)
require.Equal(
t, wantMinFee, got, "quoter falls back to MinOperatorFee "+
"when EstimateFee fails, not to zero",
)
}
// TestAutoRefreshFeeQuoterTermsFailReturnsZeroFloor verifies that
// when fetchOperatorTerms fails (operator unreachable for GetInfo),
// minFee degrades to zero. The actor still calls EstimateFee, so
// if that succeeds the quoter returns the dynamic quote; if it
// also fails, the quoter returns zero. This test covers the
// both-fail branch: terms unreachable AND EstimateFee unreachable
// returns zero, matching pre-#269 behavior (safe only on zero-fee
// schedules).
func TestAutoRefreshFeeQuoterTermsAndQuoteFailReturnsZero(t *testing.T) {
t.Parallel()
// Leave getInfoResponse nil so GetInfo falls through to the
// embedded Unimplemented server (which returns an error).
svc := &fakeArkService{
estimateFeeErr: errors.New("operator overload"),
}
s := &Server{
serverConn: newBufconnClient(t, svc),
log: btclog.Disabled,
}
got := s.autoRefreshFeeQuoter()(
t.Context(), 100_000, 200,
)
require.Equal(
t, btcutil.Amount(0), got,
"both failures → zero, matching pre-#269 behavior",
)
}
// TestAutoRefreshFeeQuoterReturnsQuoteWhenAboveMinFee verifies that
// when both operator calls succeed and the dynamic quote exceeds
// the floor, the quoter returns the dynamic quote. This is the
// happy path: the server's validateOperatorFee expects the full
// ComputeForfeitFee amount, not the static floor.
func TestAutoRefreshFeeQuoterReturnsQuoteWhenAboveMinFee(t *testing.T) {
t.Parallel()
const (
wantMinFee = btcutil.Amount(100)
wantQuote = btcutil.Amount(750)
)
svc := &fakeArkService{
getInfoResponse: newFakeOperatorResponse(
t, int64(wantMinFee),
),
response: &arkrpc.EstimateFeeResponse{
TotalFeeSat: int64(wantQuote),
},
}
s := &Server{
serverConn: newBufconnClient(t, svc),
log: btclog.Disabled,
}
got := s.autoRefreshFeeQuoter()(
t.Context(), 100_000, 200,
)
require.Equal(t, wantQuote, got,
"quote > floor → dynamic quote wins")
}
// TestAutoRefreshFeeQuoterReturnsMinFeeWhenQuoteBelowFloor verifies
// that when the dynamic quote is at or below MinOperatorFee, the
// quoter returns the floor. Under a low-congestion schedule the
// round FSM's pre-flight check rejects any submission strictly
// below MinOperatorFee before the round is even sent to the
// server, so over-paying the floor is the safe default.
func TestAutoRefreshFeeQuoterReturnsMinFeeWhenQuoteBelowFloor(t *testing.T) {
t.Parallel()
const (
wantMinFee = btcutil.Amount(500)
smallQuote = btcutil.Amount(50)
)
svc := &fakeArkService{
getInfoResponse: newFakeOperatorResponse(
t, int64(wantMinFee),
),
response: &arkrpc.EstimateFeeResponse{
TotalFeeSat: int64(smallQuote),
},
}
s := &Server{
serverConn: newBufconnClient(t, svc),
log: btclog.Disabled,
}
got := s.autoRefreshFeeQuoter()(
t.Context(), 100_000, 200,
)
require.Equal(
t, wantMinFee, got, "quote <= floor → floor wins so the "+
"round FSM pre-flight check does not reject the "+
"submission",
)
}
// TestLedgerEntryToProtoExitCost verifies that an exit-cost row
// (onchain fee leg) is mapped with the on-chain fees account on the
// debit side and no round/session linkage (unilateral exits are not
// tied to round IDs).
func TestLedgerEntryToProtoExitCost(t *testing.T) {
t.Parallel()
row := &sqlc.LedgerEntry{
EntryID: 19,
DebitAccount: ledger.AccountOnchainFees,
CreditAccount: ledger.AccountVTXOBalance,
AmountSat: 2_000,
RoundID: nil,
SessionID: nil,
EventType: ledger.EventOnchainFeePaid,
Description: "unilateral exit miner fee",
CreatedAt: 1_700_000_200,
}
got := ledgerEntryToProto(row)
require.Equal(t, ledger.AccountOnchainFees, got.DebitAccount)
require.Equal(t, ledger.EventOnchainFeePaid, got.EventType)
require.Empty(t, got.RoundId)
require.Empty(t, got.SessionId)
}
// TestTransactionHistoryRowToProtoLedger verifies that a ledger-backed
// transaction row maps the common history fields and leaves txid empty.
func TestTransactionHistoryRowToProtoLedger(t *testing.T) {
t.Parallel()
row := &sqlc.ListTransactionHistoryRow{
Source: "ledger",
EntryID: 99,
TransactionType: "oor",
Subtype: ledger.EventVTXOSent,
AmountSat: 12_000,
FeeSat: 0,
CreatedAt: 1_700_000_300,
Status: "recorded",
Description: "oor send",
DebitAccount: ledger.AccountTransfersOut,
CreditAccount: ledger.AccountVTXOBalance,
SessionID: make([]byte, 32),
OutputIndex: 7,
}
got, err := transactionHistoryRowToProto(row)
require.NoError(t, err)
require.Equal(t, "ledger", got.Source)
require.Equal(t, "oor", got.Type)
require.Equal(t, ledger.EventVTXOSent, got.Subtype)
require.Equal(t, int64(12_000), got.AmountSat)
require.Equal(t, int64(1_700_000_300), got.CreatedAtUnixS)
require.Equal(t, "recorded", got.ConfirmationStatus)
require.Equal(t, int64(99), got.EntryId)
require.Empty(t, got.Txid)
require.Equal(t, int32(7), got.OutputIndex)
require.Equal(t, ledger.AccountTransfersOut, got.DebitAccount)
require.Equal(t, ledger.AccountVTXOBalance, got.CreditAccount)
}
// TestTransactionHistoryRowToProtoSweep verifies sweep-backed rows expose the
// transaction id, fee, and confirmation height fields.
func TestTransactionHistoryRowToProtoSweep(t *testing.T) {
t.Parallel()
txidBytes := make([]byte, chainhash.HashSize)
for i := range txidBytes {
txidBytes[i] = byte(i + 1)
}
wantTxid, err := chainhash.NewHash(txidBytes)
require.NoError(t, err)
row := &sqlc.ListTransactionHistoryRow{
Source: "boarding_sweep",
Txid: txidBytes,
TransactionType: "sweep",
Subtype: "confirmed",
AmountSat: 50_000,
FeeSat: 500,
CreatedAt: 1_700_000_400,
Status: "confirmed",
Description: "boarding timeout sweep",
ConfirmationHeight: 144,
}
got, err := transactionHistoryRowToProto(row)
require.NoError(t, err)
require.Equal(t, "sweep", got.Type)
require.Equal(t, wantTxid.String(), got.Txid)
require.Equal(t, int64(500), got.FeeSat)
require.Equal(t, int32(144), got.ConfirmationHeight)
}
// TestTransactionHistoryRowToProtoRejectsBadTxID pins corrupted sweep history
// handling: bad txid blobs fail conversion rather than returning misleading
// history.
func TestTransactionHistoryRowToProtoRejectsBadTxID(t *testing.T) {
t.Parallel()
_, err := transactionHistoryRowToProto(&sqlc.ListTransactionHistoryRow{
Txid: []byte{1, 2, 3},
})
require.ErrorContains(t, err, "decode txid")
}