forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ledger_change_reader_test.go
641 lines (595 loc) · 17.6 KB
/
ledger_change_reader_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
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
package ingest
import (
"context"
"encoding/hex"
"fmt"
"io"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stellar/go/ingest/ledgerbackend"
"github.com/stellar/go/network"
"github.com/stellar/go/xdr"
)
const (
feeAddress = "GAHK7EEG2WWHVKDNT4CEQFZGKF2LGDSW2IVM4S5DP42RBW3K6BTODB4A"
metaAddress = "GACMZD5VJXTRLKVET72CETCYKELPNCOTTBDC6DHFEUPLG5DHEK534JQX"
upgradeAddress = "GA5WBPYA5Y4WAEHXWR2UKO2UO4BUGHUQ74EUPKON2QHV4WRHOIRNKKH2"
)
func TestNewLedgerChangeReaderFails(t *testing.T) {
ctx := context.Background()
mock := &ledgerbackend.MockDatabaseBackend{}
seq := uint32(123)
mock.On("GetLedger", ctx, seq).Return(
xdr.LedgerCloseMeta{},
fmt.Errorf("ledger error"),
).Once()
_, err := NewLedgerChangeReader(ctx, mock, network.TestNetworkPassphrase, seq)
assert.EqualError(
t,
err,
"error getting ledger from the backend: ledger error",
)
}
func TestNewLedgerChangeReaderSucceeds(t *testing.T) {
ctx := context.Background()
mock := &ledgerbackend.MockDatabaseBackend{}
seq := uint32(123)
header := xdr.LedgerHeaderHistoryEntry{
Hash: xdr.Hash{1, 2, 3},
Header: xdr.LedgerHeader{
LedgerVersion: 7,
},
}
mock.On("GetLedger", ctx, seq).Return(
xdr.LedgerCloseMeta{
V0: &xdr.LedgerCloseMetaV0{
LedgerHeader: header,
},
},
nil,
).Once()
reader, err := NewLedgerChangeReader(ctx, mock, network.TestNetworkPassphrase, seq)
assert.NoError(t, err)
assert.Equal(t, reader.GetHeader(), header)
}
func buildChange(account string, balance int64) xdr.LedgerEntryChange {
return xdr.LedgerEntryChange{
Type: xdr.LedgerEntryChangeTypeLedgerEntryCreated,
Created: &xdr.LedgerEntry{
Data: xdr.LedgerEntryData{
Type: xdr.LedgerEntryTypeAccount,
Account: &xdr.AccountEntry{
AccountId: xdr.MustAddress(account),
Balance: xdr.Int64(balance),
},
},
},
}
}
type changePredicate func(*testing.T, int, Change)
func isBalance(address string, balance int64) changePredicate {
return func(t *testing.T, idx int, change Change) {
msg := fmt.Sprintf("change %d", idx)
require.NotNil(t, change.Post, msg)
assert.Equal(t, xdr.LedgerEntryTypeAccount.String(), change.Post.Data.Type.String(), msg)
assert.Equal(t, address, change.Post.Data.Account.AccountId.Address(), msg)
assert.EqualValues(t, balance, change.Post.Data.Account.Balance, msg)
}
}
func isContractDataExtension(contract xdr.ScAddress, key xdr.ScVal, extension uint32) changePredicate {
return func(t *testing.T, idx int, change Change) {
msg := fmt.Sprintf("change %d", idx)
require.NotNil(t, change.Post, msg)
require.NotNil(t, change.Pre, msg)
assert.Equal(t, xdr.LedgerEntryTypeContractData.String(), change.Post.Data.Type.String(), msg)
assert.Equal(t, contract, change.Post.Data.ContractData.Contract, msg)
assert.Equal(t, key, change.Post.Data.ContractData.Key, msg)
}
}
func isContractDataEviction(contract xdr.ScAddress, key xdr.ScVal) changePredicate {
return func(t *testing.T, idx int, change Change) {
msg := fmt.Sprintf("change %d", idx)
require.NotNil(t, change.Pre, msg)
assert.Nil(t, change.Post, msg)
assert.Equal(t, xdr.LedgerEntryTypeContractData.String(), change.Pre.Data.Type.String(), msg)
assert.Equal(t, contract, change.Pre.Data.ContractData.Contract, msg)
assert.Equal(t, key, change.Pre.Data.ContractData.Key, msg)
}
}
func assertChangesEqual(
t *testing.T,
ctx context.Context,
sequence uint32,
backend ledgerbackend.LedgerBackend,
expectations []changePredicate,
) {
reader, err := NewLedgerChangeReader(ctx, backend, network.TestNetworkPassphrase, sequence)
assert.NoError(t, err)
// Read all the changes
var changes []Change
for {
change, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
changes = append(changes, change)
}
assert.Len(t, changes, len(expectations), "unexpected number of changes")
// Check each change is what we expect
for i, change := range changes {
expectations[i](t, i+1, change)
}
}
func TestLedgerChangeReaderOrder(t *testing.T) {
ctx := context.Background()
mock := &ledgerbackend.MockDatabaseBackend{}
seq := uint32(123)
src := xdr.MustAddress("GBXGQJWVLWOYHFLVTKWV5FGHA3LNYY2JQKM7OAJAUEQFU6LPCSEFVXON")
firstTx := xdr.TransactionEnvelope{
Type: xdr.EnvelopeTypeEnvelopeTypeTx,
V1: &xdr.TransactionV1Envelope{
Tx: xdr.Transaction{
Fee: 1,
SourceAccount: src.ToMuxedAccount(),
},
},
}
firstTxHash, err := network.HashTransactionInEnvelope(firstTx, network.TestNetworkPassphrase)
assert.NoError(t, err)
src = xdr.MustAddress("GCXKG6RN4ONIEPCMNFB732A436Z5PNDSRLGWK7GBLCMQLIFO4S7EYWVU")
secondTx := xdr.TransactionEnvelope{
Type: xdr.EnvelopeTypeEnvelopeTypeTx,
V1: &xdr.TransactionV1Envelope{
Tx: xdr.Transaction{
Fee: 2,
SourceAccount: src.ToMuxedAccount(),
},
},
}
secondTxHash, err := network.HashTransactionInEnvelope(secondTx, network.TestNetworkPassphrase)
assert.NoError(t, err)
ledger := xdr.LedgerCloseMeta{
V0: &xdr.LedgerCloseMetaV0{
LedgerHeader: xdr.LedgerHeaderHistoryEntry{Header: xdr.LedgerHeader{LedgerVersion: 10}},
TxSet: xdr.TransactionSet{
Txs: []xdr.TransactionEnvelope{
secondTx,
firstTx,
},
},
TxProcessing: []xdr.TransactionResultMeta{
{
Result: xdr.TransactionResultPair{TransactionHash: firstTxHash},
FeeProcessing: xdr.LedgerEntryChanges{
buildChange(feeAddress, 100),
buildChange(feeAddress, 200),
},
TxApplyProcessing: xdr.TransactionMeta{
V: 1,
V1: &xdr.TransactionMetaV1{
Operations: []xdr.OperationMeta{
{
Changes: xdr.LedgerEntryChanges{
buildChange(
metaAddress,
300,
),
buildChange(
metaAddress,
400,
),
},
},
},
},
},
},
{
Result: xdr.TransactionResultPair{TransactionHash: secondTxHash},
FeeProcessing: xdr.LedgerEntryChanges{
buildChange(feeAddress, 300),
},
TxApplyProcessing: xdr.TransactionMeta{
V: 2,
V2: &xdr.TransactionMetaV2{
TxChangesBefore: xdr.LedgerEntryChanges{
buildChange(metaAddress, 600),
},
Operations: []xdr.OperationMeta{
{
Changes: xdr.LedgerEntryChanges{
buildChange(metaAddress, 700),
},
},
},
TxChangesAfter: xdr.LedgerEntryChanges{
buildChange(metaAddress, 800),
buildChange(metaAddress, 900),
},
},
},
},
},
UpgradesProcessing: []xdr.UpgradeEntryMeta{
{
Changes: xdr.LedgerEntryChanges{
buildChange(upgradeAddress, 2),
},
},
{
Changes: xdr.LedgerEntryChanges{
buildChange(upgradeAddress, 3),
},
},
},
},
}
mock.On("GetLedger", ctx, seq).Return(ledger, nil).Once()
assertChangesEqual(t, ctx, seq, mock, []changePredicate{
isBalance(feeAddress, 100),
isBalance(feeAddress, 200),
isBalance(feeAddress, 300),
isBalance(metaAddress, 300),
isBalance(metaAddress, 400),
isBalance(metaAddress, 600),
isBalance(metaAddress, 700),
isBalance(metaAddress, 800),
isBalance(metaAddress, 900),
isBalance(upgradeAddress, 2),
isBalance(upgradeAddress, 3),
})
mock.AssertExpectations(t)
ledger.V0.LedgerHeader.Header.LedgerVersion = 8
mock.On("GetLedger", ctx, seq).Return(ledger, nil).Once()
_, err = NewLedgerChangeReader(ctx, mock, network.TestNetworkPassphrase, seq)
assert.EqualError(
t,
err,
"error extracting transactions from ledger close meta: TransactionMeta.V=2 is required in protocol"+
" version older than version 10. Please process ledgers again using the latest stellar-core version.",
)
mock.AssertExpectations(t)
ledger.V0.LedgerHeader.Header.LedgerVersion = 9
ledger.V0.TxProcessing[0].FeeProcessing = xdr.LedgerEntryChanges{}
ledger.V0.TxProcessing[1].FeeProcessing = xdr.LedgerEntryChanges{}
mock.On("GetLedger", ctx, seq).Return(ledger, nil).Once()
assertChangesEqual(t, ctx, seq, mock, []changePredicate{
isBalance(metaAddress, 300),
isBalance(metaAddress, 400),
isBalance(metaAddress, 600),
isBalance(metaAddress, 700),
isBalance(metaAddress, 800),
isBalance(metaAddress, 900),
isBalance(upgradeAddress, 2),
isBalance(upgradeAddress, 3),
})
mock.AssertExpectations(t)
ledger.V0.LedgerHeader.Header.LedgerVersion = 10
ledger.V0.TxProcessing[0].FeeProcessing = xdr.LedgerEntryChanges{}
ledger.V0.TxProcessing[1].FeeProcessing = xdr.LedgerEntryChanges{}
mock.On("GetLedger", ctx, seq).Return(ledger, nil).Once()
assertChangesEqual(t, ctx, seq, mock, []changePredicate{
isBalance(metaAddress, 300),
isBalance(metaAddress, 400),
isBalance(metaAddress, 600),
isBalance(metaAddress, 700),
isBalance(metaAddress, 800),
isBalance(metaAddress, 900),
isBalance(upgradeAddress, 2),
isBalance(upgradeAddress, 3),
})
mock.AssertExpectations(t)
ledger.V0.UpgradesProcessing = []xdr.UpgradeEntryMeta{
{
Changes: xdr.LedgerEntryChanges{},
},
{
Changes: xdr.LedgerEntryChanges{},
},
}
mock.On("GetLedger", ctx, seq).Return(ledger, nil).Once()
assertChangesEqual(t, ctx, seq, mock, []changePredicate{
isBalance(metaAddress, 300),
isBalance(metaAddress, 400),
isBalance(metaAddress, 600),
isBalance(metaAddress, 700),
isBalance(metaAddress, 800),
isBalance(metaAddress, 900),
})
mock.AssertExpectations(t)
ledger.V0.TxProcessing[0].TxApplyProcessing = xdr.TransactionMeta{
V: 1,
V1: &xdr.TransactionMetaV1{
Operations: []xdr.OperationMeta{},
},
}
ledger.V0.TxProcessing[1].TxApplyProcessing = xdr.TransactionMeta{
V: 1,
V1: &xdr.TransactionMetaV1{
Operations: []xdr.OperationMeta{},
},
}
mock.On("GetLedger", ctx, seq).Return(ledger, nil).Once()
assertChangesEqual(t, ctx, seq, mock, []changePredicate{})
mock.AssertExpectations(t)
}
func TestLedgerChangeLedgerCloseMetaV2(t *testing.T) {
ctx := context.Background()
mock := &ledgerbackend.MockDatabaseBackend{}
seq := uint32(123)
src := xdr.MustAddress("GBXGQJWVLWOYHFLVTKWV5FGHA3LNYY2JQKM7OAJAUEQFU6LPCSEFVXON")
firstTx := xdr.TransactionEnvelope{
Type: xdr.EnvelopeTypeEnvelopeTypeTx,
V1: &xdr.TransactionV1Envelope{
Tx: xdr.Transaction{
Fee: 1,
SourceAccount: src.ToMuxedAccount(),
},
},
}
firstTxHash, err := network.HashTransactionInEnvelope(firstTx, network.TestNetworkPassphrase)
assert.NoError(t, err)
src = xdr.MustAddress("GCXKG6RN4ONIEPCMNFB732A436Z5PNDSRLGWK7GBLCMQLIFO4S7EYWVU")
secondTx := xdr.TransactionEnvelope{
Type: xdr.EnvelopeTypeEnvelopeTypeTx,
V1: &xdr.TransactionV1Envelope{
Tx: xdr.Transaction{
Fee: 2,
SourceAccount: src.ToMuxedAccount(),
},
},
}
secondTxHash, err := network.HashTransactionInEnvelope(secondTx, network.TestNetworkPassphrase)
assert.NoError(t, err)
baseFee := xdr.Int64(100)
tempKey := xdr.ScSymbol("TEMPKEY")
persistentKey := xdr.ScSymbol("TEMPVAL")
contractIDBytes, err := hex.DecodeString("df06d62447fd25da07c0135eed7557e5a5497ee7d15b7fe345bd47e191d8f577")
assert.NoError(t, err)
var contractID xdr.Hash
copy(contractID[:], contractIDBytes)
contractAddress := xdr.ScAddress{
Type: xdr.ScAddressTypeScAddressTypeContract,
ContractId: &contractID,
}
ledger := xdr.LedgerCloseMeta{
V: 1,
V1: &xdr.LedgerCloseMetaV1{
LedgerHeader: xdr.LedgerHeaderHistoryEntry{Header: xdr.LedgerHeader{LedgerVersion: 10}},
TxSet: xdr.GeneralizedTransactionSet{
V: 1,
V1TxSet: &xdr.TransactionSetV1{
PreviousLedgerHash: xdr.Hash{1, 2, 3},
Phases: []xdr.TransactionPhase{
{
V0Components: &[]xdr.TxSetComponent{
{
Type: xdr.TxSetComponentTypeTxsetCompTxsMaybeDiscountedFee,
TxsMaybeDiscountedFee: &xdr.TxSetComponentTxsMaybeDiscountedFee{
BaseFee: &baseFee,
Txs: []xdr.TransactionEnvelope{
secondTx,
firstTx,
},
},
},
},
},
},
},
},
TxProcessing: []xdr.TransactionResultMeta{
{
Result: xdr.TransactionResultPair{TransactionHash: firstTxHash},
FeeProcessing: xdr.LedgerEntryChanges{
buildChange(feeAddress, 100),
buildChange(feeAddress, 200),
},
TxApplyProcessing: xdr.TransactionMeta{
V: 3,
V3: &xdr.TransactionMetaV3{
Operations: []xdr.OperationMeta{
{
Changes: xdr.LedgerEntryChanges{
buildChange(
metaAddress,
300,
),
buildChange(
metaAddress,
400,
),
// Add a couple changes simulating a ledger entry extension
{
Type: xdr.LedgerEntryChangeTypeLedgerEntryState,
State: &xdr.LedgerEntry{
LastModifiedLedgerSeq: 1,
Data: xdr.LedgerEntryData{
Type: xdr.LedgerEntryTypeContractData,
ContractData: &xdr.ContractDataEntry{
Contract: contractAddress,
Key: xdr.ScVal{
Type: xdr.ScValTypeScvSymbol,
Sym: &persistentKey,
},
Durability: xdr.ContractDataDurabilityPersistent,
},
},
},
},
{
Type: xdr.LedgerEntryChangeTypeLedgerEntryUpdated,
Updated: &xdr.LedgerEntry{
LastModifiedLedgerSeq: 1,
Data: xdr.LedgerEntryData{
Type: xdr.LedgerEntryTypeContractData,
ContractData: &xdr.ContractDataEntry{
Contract: xdr.ScAddress{
Type: xdr.ScAddressTypeScAddressTypeContract,
ContractId: &contractID,
},
Key: xdr.ScVal{
Type: xdr.ScValTypeScvSymbol,
Sym: &persistentKey,
},
Durability: xdr.ContractDataDurabilityPersistent,
},
},
},
},
},
},
},
},
},
},
{
Result: xdr.TransactionResultPair{TransactionHash: secondTxHash},
FeeProcessing: xdr.LedgerEntryChanges{
buildChange(feeAddress, 300),
},
TxApplyProcessing: xdr.TransactionMeta{
V: 3,
V3: &xdr.TransactionMetaV3{
TxChangesBefore: xdr.LedgerEntryChanges{
buildChange(metaAddress, 600),
},
Operations: []xdr.OperationMeta{
{
Changes: xdr.LedgerEntryChanges{
buildChange(metaAddress, 700),
},
},
},
TxChangesAfter: xdr.LedgerEntryChanges{
buildChange(metaAddress, 800),
buildChange(metaAddress, 900),
},
},
},
},
},
UpgradesProcessing: []xdr.UpgradeEntryMeta{
{
Changes: xdr.LedgerEntryChanges{
buildChange(upgradeAddress, 2),
},
},
{
Changes: xdr.LedgerEntryChanges{
buildChange(upgradeAddress, 3),
},
},
},
EvictedTemporaryLedgerKeys: []xdr.LedgerKey{
{
Type: xdr.LedgerEntryTypeContractData,
ContractData: &xdr.LedgerKeyContractData{
Contract: contractAddress,
Key: xdr.ScVal{
Type: xdr.ScValTypeScvSymbol,
Sym: &tempKey,
},
Durability: xdr.ContractDataDurabilityTemporary,
},
},
},
EvictedPersistentLedgerEntries: []xdr.LedgerEntry{
{
LastModifiedLedgerSeq: 123,
Data: xdr.LedgerEntryData{
Type: xdr.LedgerEntryTypeContractData,
ContractData: &xdr.ContractDataEntry{
Contract: contractAddress,
Key: xdr.ScVal{
Type: xdr.ScValTypeScvSymbol,
Sym: &persistentKey,
},
Durability: xdr.ContractDataDurabilityTemporary,
},
},
},
},
},
}
mock.On("GetLedger", ctx, seq).Return(ledger, nil).Once()
// Check the changes are as expected
assertChangesEqual(t, ctx, seq, mock, []changePredicate{
// First the first txn balance xfers
isBalance(feeAddress, 100),
isBalance(feeAddress, 200),
isBalance(feeAddress, 300),
isBalance(metaAddress, 300),
isBalance(metaAddress, 400),
// Then the first txn data entry extension
isContractDataExtension(
contractAddress,
xdr.ScVal{
Type: xdr.ScValTypeScvSymbol,
Sym: &persistentKey,
},
5904,
),
// Second txn transfers
isBalance(metaAddress, 600),
isBalance(metaAddress, 700),
isBalance(metaAddress, 800),
isBalance(metaAddress, 900),
// Evictions
isContractDataEviction(
contractAddress,
xdr.ScVal{
Type: xdr.ScValTypeScvSymbol,
Sym: &persistentKey,
},
),
// Upgrades last
isBalance(upgradeAddress, 2),
isBalance(upgradeAddress, 3),
})
mock.AssertExpectations(t)
mock.AssertExpectations(t)
}
func TestLedgerChangeLedgerCloseMetaV2Empty(t *testing.T) {
ctx := context.Background()
mock := &ledgerbackend.MockDatabaseBackend{}
seq := uint32(123)
baseFee := xdr.Int64(100)
ledger := xdr.LedgerCloseMeta{
V: 1,
V1: &xdr.LedgerCloseMetaV1{
LedgerHeader: xdr.LedgerHeaderHistoryEntry{Header: xdr.LedgerHeader{LedgerVersion: 10}},
TxSet: xdr.GeneralizedTransactionSet{
V: 1,
V1TxSet: &xdr.TransactionSetV1{
PreviousLedgerHash: xdr.Hash{1, 2, 3},
Phases: []xdr.TransactionPhase{
{
V0Components: &[]xdr.TxSetComponent{
{
Type: xdr.TxSetComponentTypeTxsetCompTxsMaybeDiscountedFee,
TxsMaybeDiscountedFee: &xdr.TxSetComponentTxsMaybeDiscountedFee{
BaseFee: &baseFee,
Txs: []xdr.TransactionEnvelope{},
},
},
},
},
},
},
},
TxProcessing: []xdr.TransactionResultMeta{},
UpgradesProcessing: []xdr.UpgradeEntryMeta{},
EvictedTemporaryLedgerKeys: []xdr.LedgerKey{},
EvictedPersistentLedgerEntries: []xdr.LedgerEntry{},
},
}
mock.On("GetLedger", ctx, seq).Return(ledger, nil).Once()
// Check there are no changes
assertChangesEqual(t, ctx, seq, mock, []changePredicate{})
mock.AssertExpectations(t)
}