@@ -17,40 +17,16 @@ package allegra_test
17
17
import (
18
18
"crypto/rand"
19
19
"encoding/hex"
20
- "errors"
21
20
"testing"
22
21
22
+ test "github.com/blinklabs-io/gouroboros/internal/test/ledger"
23
23
"github.com/blinklabs-io/gouroboros/ledger/allegra"
24
24
"github.com/blinklabs-io/gouroboros/ledger/common"
25
25
"github.com/blinklabs-io/gouroboros/ledger/shelley"
26
26
27
27
"github.com/stretchr/testify/assert"
28
28
)
29
29
30
- type testLedgerState struct {
31
- networkId uint
32
- utxos []common.Utxo
33
- }
34
-
35
- func (ls testLedgerState ) NetworkId () uint {
36
- return ls .networkId
37
- }
38
-
39
- func (ls testLedgerState ) UtxoById (
40
- id common.TransactionInput ,
41
- ) (common.Utxo , error ) {
42
- for _ , tmpUtxo := range ls .utxos {
43
- if id .Index () != tmpUtxo .Id .Index () {
44
- continue
45
- }
46
- if string (id .Id ().Bytes ()) != string (tmpUtxo .Id .Id ().Bytes ()) {
47
- continue
48
- }
49
- return tmpUtxo , nil
50
- }
51
- return common.Utxo {}, errors .New ("not found" )
52
- }
53
-
54
30
func TestUtxoValidateOutsideValidityIntervalUtxo (t * testing.T ) {
55
31
var testSlot uint64 = 555666777
56
32
var testZeroSlot uint64 = 0
@@ -59,7 +35,7 @@ func TestUtxoValidateOutsideValidityIntervalUtxo(t *testing.T) {
59
35
TxValidityIntervalStart : testSlot ,
60
36
},
61
37
}
62
- testLedgerState := testLedgerState {}
38
+ testLedgerState := test. MockLedgerState {}
63
39
testProtocolParams := & allegra.AllegraProtocolParameters {}
64
40
var testBeforeSlot uint64 = 555666700
65
41
var testAfterSlot uint64 = 555666799
@@ -165,7 +141,7 @@ func TestUtxoValidateInputSetEmptyUtxo(t *testing.T) {
165
141
},
166
142
},
167
143
}
168
- testLedgerState := testLedgerState {}
144
+ testLedgerState := test. MockLedgerState {}
169
145
testSlot := uint64 (0 )
170
146
testProtocolParams := & allegra.AllegraProtocolParameters {}
171
147
// Non-empty
@@ -236,7 +212,7 @@ func TestUtxoValidateFeeTooSmallUtxo(t *testing.T) {
236
212
MinFeeB : 53 ,
237
213
},
238
214
}
239
- testLedgerState := testLedgerState {}
215
+ testLedgerState := test. MockLedgerState {}
240
216
testSlot := uint64 (0 )
241
217
// Test helper function
242
218
testRun := func (t * testing.T , name string , testFee uint64 , validateFunc func (* testing.T , error )) {
@@ -322,8 +298,8 @@ func TestUtxoValidateBadInputsUtxo(t *testing.T) {
322
298
testTx := & allegra.AllegraTransaction {
323
299
Body : allegra.AllegraTransactionBody {},
324
300
}
325
- testLedgerState := testLedgerState {
326
- utxos : []common.Utxo {
301
+ testLedgerState := test. MockLedgerState {
302
+ MockUtxos : []common.Utxo {
327
303
{
328
304
Id : testGoodInput ,
329
305
},
@@ -402,8 +378,8 @@ func TestUtxoValidateWrongNetwork(t *testing.T) {
402
378
},
403
379
},
404
380
}
405
- testLedgerState := testLedgerState {
406
- networkId : common .AddressNetworkMainnet ,
381
+ testLedgerState := test. MockLedgerState {
382
+ MockNetworkId : common .AddressNetworkMainnet ,
407
383
}
408
384
testSlot := uint64 (0 )
409
385
testProtocolParams := & allegra.AllegraProtocolParameters {}
@@ -470,8 +446,8 @@ func TestUtxoValidateWrongNetworkWithdrawal(t *testing.T) {
470
446
},
471
447
},
472
448
}
473
- testLedgerState := testLedgerState {
474
- networkId : common .AddressNetworkMainnet ,
449
+ testLedgerState := test. MockLedgerState {
450
+ MockNetworkId : common .AddressNetworkMainnet ,
475
451
}
476
452
testSlot := uint64 (0 )
477
453
testProtocolParams := & allegra.AllegraProtocolParameters {}
@@ -529,6 +505,8 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
529
505
var testInputAmount uint64 = 555666777
530
506
var testFee uint64 = 123456
531
507
var testStakeDeposit uint64 = 2_000_000
508
+ var testStakeCred1 = []byte {0x01 , 0x23 , 0x45 }
509
+ var testStakeCred2 = []byte {0xab , 0xcd , 0xef }
532
510
testOutputExactAmount := testInputAmount - testFee
533
511
testOutputUnderAmount := testOutputExactAmount - 999
534
512
testOutputOverAmount := testOutputExactAmount + 999
@@ -548,15 +526,22 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
548
526
},
549
527
},
550
528
}
551
- testLedgerState := testLedgerState {
552
- utxos : []common.Utxo {
529
+ testLedgerState := test. MockLedgerState {
530
+ MockUtxos : []common.Utxo {
553
531
{
554
532
Id : shelley .NewShelleyTransactionInput (testInputTxId , 0 ),
555
533
Output : shelley.ShelleyTransactionOutput {
556
534
OutputAmount : testInputAmount ,
557
535
},
558
536
},
559
537
},
538
+ MockStakeRegistration : []common.StakeRegistrationCertificate {
539
+ {
540
+ StakeRegistration : common.StakeCredential {
541
+ Credential : testStakeCred2 ,
542
+ },
543
+ },
544
+ },
560
545
}
561
546
testSlot := uint64 (0 )
562
547
testProtocolParams := & allegra.AllegraProtocolParameters {
@@ -583,15 +568,48 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
583
568
}
584
569
},
585
570
)
586
- // Stake registration
571
+ // First stake registration
587
572
t .Run (
588
- "stake registration" ,
573
+ "first stake registration" ,
589
574
func (t * testing.T ) {
590
575
testTx .Body .TxOutputs [0 ].OutputAmount = testOutputExactAmount - testStakeDeposit
591
576
testTx .Body .TxCertificates = []common.CertificateWrapper {
592
577
{
593
- Type : common .CertificateTypeStakeRegistration ,
594
- Certificate : & common.StakeRegistrationCertificate {},
578
+ Type : common .CertificateTypeStakeRegistration ,
579
+ Certificate : & common.StakeRegistrationCertificate {
580
+ StakeRegistration : common.StakeCredential {
581
+ Credential : testStakeCred1 ,
582
+ },
583
+ },
584
+ },
585
+ }
586
+ err := allegra .UtxoValidateValueNotConservedUtxo (
587
+ testTx ,
588
+ testSlot ,
589
+ testLedgerState ,
590
+ testProtocolParams ,
591
+ )
592
+ if err != nil {
593
+ t .Errorf (
594
+ "UtxoValidateValueNotConservedUtxo should succeed when inputs and outputs are balanced\n got error: %v" ,
595
+ err ,
596
+ )
597
+ }
598
+ },
599
+ )
600
+ // Second stake registration
601
+ t .Run (
602
+ "second stake registration" ,
603
+ func (t * testing.T ) {
604
+ testTx .Body .TxOutputs [0 ].OutputAmount = testOutputExactAmount
605
+ testTx .Body .TxCertificates = []common.CertificateWrapper {
606
+ {
607
+ Type : common .CertificateTypeStakeRegistration ,
608
+ Certificate : & common.StakeRegistrationCertificate {
609
+ StakeRegistration : common.StakeCredential {
610
+ Credential : testStakeCred2 ,
611
+ },
612
+ },
595
613
},
596
614
}
597
615
err := allegra .UtxoValidateValueNotConservedUtxo (
@@ -679,7 +697,7 @@ func TestUtxoValidateOutputTooSmallUtxo(t *testing.T) {
679
697
},
680
698
},
681
699
}
682
- testLedgerState := testLedgerState {}
700
+ testLedgerState := test. MockLedgerState {}
683
701
testSlot := uint64 (0 )
684
702
testProtocolParams := & allegra.AllegraProtocolParameters {
685
703
ShelleyProtocolParameters : shelley.ShelleyProtocolParameters {
@@ -766,7 +784,7 @@ func TestUtxoValidateOutputBootAddrAttrsTooBig(t *testing.T) {
766
784
},
767
785
},
768
786
}
769
- testLedgerState := testLedgerState {}
787
+ testLedgerState := test. MockLedgerState {}
770
788
testSlot := uint64 (0 )
771
789
testProtocolParams := & allegra.AllegraProtocolParameters {}
772
790
// Good
@@ -822,7 +840,7 @@ func TestUtxoValidateMaxTxSizeUtxo(t *testing.T) {
822
840
var testMaxTxSizeSmall uint = 2
823
841
var testMaxTxSizeLarge uint = 64 * 1024
824
842
testTx := & allegra.AllegraTransaction {}
825
- testLedgerState := testLedgerState {}
843
+ testLedgerState := test. MockLedgerState {}
826
844
testSlot := uint64 (0 )
827
845
testProtocolParams := & allegra.AllegraProtocolParameters {}
828
846
// Transaction under limit
0 commit comments