Skip to content

Commit 348f4ef

Browse files
authored
test(ledger): unit tests for allegra, alonzo, babbage, byron, conway and mary transaction Utxorpc conversions (#1029)
Signed-off-by: Akhil Repala <[email protected]>
1 parent b4351b9 commit 348f4ef

File tree

6 files changed

+637
-0
lines changed

6 files changed

+637
-0
lines changed

ledger/allegra/pparams_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222

2323
"github.com/blinklabs-io/gouroboros/cbor"
2424
"github.com/blinklabs-io/gouroboros/ledger/allegra"
25+
"github.com/blinklabs-io/gouroboros/ledger/common"
26+
"github.com/blinklabs-io/gouroboros/ledger/shelley"
2527
"github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
2628
)
2729

@@ -128,3 +130,86 @@ func TestAllegraUtxorpc(t *testing.T) {
128130
)
129131
}
130132
}
133+
134+
// Unit test for AllegraTransactionBody.Utxorpc()
135+
func TestAllegraTransactionBody_Utxorpc(t *testing.T) {
136+
// mock input
137+
input := shelley.NewShelleyTransactionInput("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1)
138+
inputSet := shelley.NewShelleyTransactionInputSet([]shelley.ShelleyTransactionInput{input})
139+
140+
address := common.Address{}
141+
142+
// Define a transaction output
143+
output := shelley.ShelleyTransactionOutput{
144+
OutputAddress: address,
145+
OutputAmount: 1000,
146+
}
147+
148+
// Create transaction body
149+
txBody := &allegra.AllegraTransactionBody{
150+
TxInputs: inputSet,
151+
TxOutputs: []shelley.ShelleyTransactionOutput{output},
152+
TxFee: 200,
153+
Ttl: 5000,
154+
TxValidityIntervalStart: 4500,
155+
}
156+
157+
// Run Utxorpc conversion
158+
actual := txBody.Utxorpc()
159+
160+
// Check that the fee matches
161+
if actual.Fee != txBody.Fee() {
162+
t.Errorf("AllegraTransactionBody.Utxorpc() fee mismatch\nGot: %d\nWant: %d", actual.Fee, txBody.Fee())
163+
}
164+
// Check number of inputs
165+
if len(actual.Inputs) != len(txBody.Inputs()) {
166+
t.Errorf("AllegraTransactionBody.Utxorpc() input length mismatch\nGot: %d\nWant: %d", len(actual.Inputs), len(txBody.Inputs()))
167+
}
168+
// Check number of outputs
169+
if len(actual.Outputs) != len(txBody.Outputs()) {
170+
t.Errorf("AllegraTransactionBody.Utxorpc() output length mismatch\nGot: %d\nWant: %d", len(actual.Outputs), len(txBody.Outputs()))
171+
}
172+
}
173+
174+
// Unit test for AllegraTransaction.Utxorpc()
175+
func TestAllegraTransaction_Utxorpc(t *testing.T) {
176+
// Prepare mock input
177+
input := shelley.NewShelleyTransactionInput("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", 0)
178+
inputSet := shelley.NewShelleyTransactionInputSet([]shelley.ShelleyTransactionInput{input})
179+
180+
// Prepare mock output
181+
address := common.Address{}
182+
output := shelley.ShelleyTransactionOutput{
183+
OutputAddress: address,
184+
OutputAmount: 2000,
185+
}
186+
187+
// Create transaction body
188+
tx := &allegra.AllegraTransaction{
189+
Body: allegra.AllegraTransactionBody{
190+
TxInputs: inputSet,
191+
TxOutputs: []shelley.ShelleyTransactionOutput{output},
192+
TxFee: 150,
193+
Ttl: 1000,
194+
TxValidityIntervalStart: 950,
195+
},
196+
WitnessSet: shelley.ShelleyTransactionWitnessSet{},
197+
TxMetadata: nil,
198+
}
199+
200+
// Run Utxorpc conversion
201+
actual := tx.Utxorpc()
202+
203+
// Assertion checks
204+
if actual.Fee != tx.Fee() {
205+
t.Errorf("AllegraTransaction.Utxorpc() fee mismatch\nGot: %d\nWant: %d", actual.Fee, tx.Fee())
206+
}
207+
208+
if len(actual.Inputs) != len(tx.Inputs()) {
209+
t.Errorf("AllegraTransaction.Utxorpc() input length mismatch\nGot: %d\nWant: %d", len(actual.Inputs), len(tx.Inputs()))
210+
}
211+
212+
if len(actual.Outputs) != len(tx.Outputs()) {
213+
t.Errorf("AllegraTransaction.Utxorpc() output length mismatch\nGot: %d\nWant: %d", len(actual.Outputs), len(tx.Outputs()))
214+
}
215+
}

ledger/alonzo/pparams_test.go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ import (
2727
"github.com/blinklabs-io/gouroboros/cbor"
2828
"github.com/blinklabs-io/gouroboros/ledger/alonzo"
2929
"github.com/blinklabs-io/gouroboros/ledger/common"
30+
"github.com/blinklabs-io/gouroboros/ledger/mary"
31+
"github.com/blinklabs-io/gouroboros/ledger/shelley"
3032
cardano "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
33+
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
3134
)
3235

3336
func newBaseProtocolParams() alonzo.AlonzoProtocolParameters {
@@ -446,3 +449,128 @@ func toJSON(v interface{}) string {
446449
}
447450
return string(b)
448451
}
452+
453+
// Unit test for AlonzoTransactionOutput.Utxorpc()
454+
func TestAlonzoTransactionOutput_Utxorpc(t *testing.T) {
455+
address := common.Address{}
456+
amount := uint64(5000)
457+
datumHash := common.Blake2b256{1, 2, 3, 4}
458+
459+
// Mock output
460+
output := alonzo.AlonzoTransactionOutput{
461+
OutputAddress: address,
462+
OutputAmount: mary.MaryTransactionOutputValue{Amount: amount},
463+
TxOutputDatumHash: &datumHash,
464+
}
465+
466+
got := output.Utxorpc()
467+
want := &utxorpc.TxOutput{
468+
Address: address.Bytes(),
469+
Coin: amount,
470+
Assets: nil,
471+
Datum: &utxorpc.Datum{
472+
Hash: datumHash.Bytes(),
473+
},
474+
}
475+
476+
if !reflect.DeepEqual(got, want) {
477+
t.Errorf("AlonzoTransactionOutput.Utxorpc() mismatch\nGot: %+v\nWant: %+v", got, want)
478+
}
479+
}
480+
481+
// Unit test for AlonzoTransactionBody.Utxorpc()
482+
func TestAlonzoTransactionBody_Utxorpc(t *testing.T) {
483+
// Mock input
484+
input := shelley.NewShelleyTransactionInput("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 0)
485+
inputSet := shelley.NewShelleyTransactionInputSet([]shelley.ShelleyTransactionInput{input})
486+
487+
// Mock output
488+
address := common.Address{}
489+
datumHash := common.Blake2b256{1, 2, 3, 4}
490+
output := alonzo.AlonzoTransactionOutput{
491+
OutputAddress: address,
492+
OutputAmount: mary.MaryTransactionOutputValue{Amount: 1000},
493+
TxOutputDatumHash: &datumHash,
494+
}
495+
496+
body := alonzo.AlonzoTransactionBody{
497+
TxInputs: inputSet,
498+
TxOutputs: []alonzo.AlonzoTransactionOutput{output},
499+
TxFee: 50,
500+
}
501+
502+
// Run Utxorpc conversion
503+
got := body.Utxorpc()
504+
505+
// Assertion checks
506+
if got.Fee != 50 {
507+
t.Errorf("Fee mismatch: got %d, want 50", got.Fee)
508+
}
509+
if len(got.Inputs) != 1 {
510+
t.Errorf("Expected 1 input, got %d", len(got.Inputs))
511+
}
512+
if got.Inputs[0].OutputIndex != input.Index() {
513+
t.Errorf("Input index mismatch: got %d, want %d", got.Inputs[0].OutputIndex, input.Index())
514+
}
515+
if len(got.Outputs) != 1 {
516+
t.Errorf("Expected 1 output, got %d", len(got.Outputs))
517+
}
518+
if got.Outputs[0].Coin != 1000 {
519+
t.Errorf("Output coin mismatch: got %d, want 1000", got.Outputs[0].Coin)
520+
}
521+
if len(got.Hash) == 0 {
522+
t.Error("Expected non-empty transaction hash")
523+
}
524+
}
525+
526+
// Unit test for AlonzoTransaction.Utxorpc()
527+
func TestAlonzoTransaction_Utxorpc(t *testing.T) {
528+
// Mock input
529+
input := shelley.NewShelleyTransactionInput("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", 1)
530+
inputSet := shelley.NewShelleyTransactionInputSet([]shelley.ShelleyTransactionInput{input})
531+
532+
// Mock output
533+
address := common.Address{}
534+
datumHash := &common.Blake2b256{0x11, 0x22, 0x33}
535+
output := alonzo.AlonzoTransactionOutput{
536+
OutputAddress: address,
537+
OutputAmount: mary.MaryTransactionOutputValue{Amount: 2000},
538+
TxOutputDatumHash: datumHash,
539+
}
540+
541+
body := alonzo.AlonzoTransactionBody{
542+
TxInputs: inputSet,
543+
TxOutputs: []alonzo.AlonzoTransactionOutput{output},
544+
TxFee: 75,
545+
}
546+
547+
// Wrap in transaction
548+
tx := alonzo.AlonzoTransaction{
549+
Body: body,
550+
TxIsValid: true,
551+
}
552+
553+
// Run Utxorpc conversion
554+
got := tx.Utxorpc()
555+
556+
// Assertion checks
557+
if got.Fee != 75 {
558+
t.Errorf("Transaction fee mismatch: got %d, want 75", got.Fee)
559+
}
560+
if len(got.Inputs) != 1 {
561+
t.Errorf("Expected 1 input, got %d", len(got.Inputs))
562+
}
563+
if len(got.Outputs) != 1 {
564+
t.Errorf("Expected 1 output, got %d", len(got.Outputs))
565+
}
566+
if got.Outputs[0].Coin != 2000 {
567+
t.Errorf("Output coin mismatch: got %d, want 2000", got.Outputs[0].Coin)
568+
}
569+
expectedDatum := datumHash.Bytes()
570+
if !reflect.DeepEqual(got.Outputs[0].Datum.Hash, expectedDatum) {
571+
t.Errorf("Datum hash mismatch: got %x, want %x", got.Outputs[0].Datum.Hash, expectedDatum)
572+
}
573+
if len(got.Hash) == 0 {
574+
t.Error("Expected non-empty transaction hash")
575+
}
576+
}

ledger/babbage/pparams_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ import (
2323
"github.com/blinklabs-io/gouroboros/cbor"
2424
"github.com/blinklabs-io/gouroboros/ledger/babbage"
2525
"github.com/blinklabs-io/gouroboros/ledger/common"
26+
"github.com/blinklabs-io/gouroboros/ledger/mary"
27+
"github.com/blinklabs-io/gouroboros/ledger/shelley"
2628
"github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
29+
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
2730
)
2831

2932
func TestBabbageProtocolParamsUpdate(t *testing.T) {
@@ -552,3 +555,120 @@ func TestBabbageUtxorpc(t *testing.T) {
552555
}
553556
}
554557
}
558+
559+
// Unit test for BabbageTransactionInput.Utxorpc()
560+
func TestBabbageTransactionInput_Utxorpc(t *testing.T) {
561+
input := shelley.NewShelleyTransactionInput("cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", 2)
562+
563+
got := input.Utxorpc()
564+
want := &cardano.TxInput{
565+
TxHash: input.Id().Bytes(),
566+
OutputIndex: input.Index(),
567+
}
568+
569+
if !reflect.DeepEqual(got, want) {
570+
t.Errorf("BabbageTransactionInput.Utxorpc() mismatch\\nGot: %+v\\nWant: %+v", got, want)
571+
}
572+
}
573+
574+
// Unit test for BabbageTransactionOutput.Utxorpc()
575+
func TestBabbageTransactionOutput_Utxorpc(t *testing.T) {
576+
address := common.Address{}
577+
amount := uint64(8400)
578+
579+
output := babbage.BabbageTransactionOutput{
580+
OutputAddress: address,
581+
OutputAmount: mary.MaryTransactionOutputValue{Amount: amount},
582+
}
583+
584+
got := output.Utxorpc()
585+
want := &utxorpc.TxOutput{
586+
Address: address.Bytes(),
587+
Coin: amount,
588+
Datum: &utxorpc.Datum{
589+
Hash: make([]byte, 32),
590+
},
591+
}
592+
593+
if !reflect.DeepEqual(got, want) {
594+
t.Errorf("BabbageTransactionOutput.Utxorpc() mismatch\nGot: %+v\nWant: %+v", got, want)
595+
}
596+
}
597+
598+
// Unit test for BabbageTransactionBody.Utxorpc()
599+
func TestBabbageTransactionBody_Utxorpc(t *testing.T) {
600+
input := shelley.NewShelleyTransactionInput(
601+
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", 1)
602+
inputSet := shelley.NewShelleyTransactionInputSet([]shelley.ShelleyTransactionInput{input})
603+
604+
address := common.Address{}
605+
output := babbage.BabbageTransactionOutput{
606+
OutputAddress: address,
607+
OutputAmount: mary.MaryTransactionOutputValue{Amount: 5000},
608+
}
609+
610+
body := babbage.BabbageTransactionBody{
611+
TxInputs: inputSet,
612+
TxOutputs: []babbage.BabbageTransactionOutput{output},
613+
TxFee: 100,
614+
}
615+
616+
got := body.Utxorpc()
617+
618+
if got.Fee != 100 {
619+
t.Errorf("Fee mismatch: got %d, want 100", got.Fee)
620+
}
621+
if len(got.Inputs) != 1 {
622+
t.Errorf("Expected 1 input, got %d", len(got.Inputs))
623+
}
624+
if len(got.Outputs) != 1 {
625+
t.Errorf("Expected 1 output, got %d", len(got.Outputs))
626+
}
627+
if got.Outputs[0].Coin != 5000 {
628+
t.Errorf("Output coin mismatch: got %d, want 5000", got.Outputs[0].Coin)
629+
}
630+
if len(got.Hash) == 0 {
631+
t.Error("Expected non-empty transaction hash")
632+
}
633+
}
634+
635+
// Unit test for BabbageTransaction.Utxorpc()
636+
func TestBabbageTransaction_Utxorpc(t *testing.T) {
637+
input := shelley.NewShelleyTransactionInput(
638+
"cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", 2)
639+
inputSet := shelley.NewShelleyTransactionInputSet([]shelley.ShelleyTransactionInput{input})
640+
641+
address := common.Address{}
642+
output := babbage.BabbageTransactionOutput{
643+
OutputAddress: address,
644+
OutputAmount: mary.MaryTransactionOutputValue{Amount: 9000},
645+
}
646+
647+
body := babbage.BabbageTransactionBody{
648+
TxInputs: inputSet,
649+
TxOutputs: []babbage.BabbageTransactionOutput{output},
650+
TxFee: 150,
651+
}
652+
tx := babbage.BabbageTransaction{
653+
Body: body,
654+
TxIsValid: true,
655+
}
656+
657+
got := tx.Utxorpc()
658+
659+
if got.Fee != 150 {
660+
t.Errorf("Fee mismatch: got %d, want 150", got.Fee)
661+
}
662+
if len(got.Inputs) != 1 {
663+
t.Errorf("Expected 1 input, got %d", len(got.Inputs))
664+
}
665+
if len(got.Outputs) != 1 {
666+
t.Errorf("Expected 1 output, got %d", len(got.Outputs))
667+
}
668+
if got.Outputs[0].Coin != 9000 {
669+
t.Errorf("Output coin mismatch: got %d, want 9000", got.Outputs[0].Coin)
670+
}
671+
if len(got.Hash) == 0 {
672+
t.Error("Expected non-empty transaction hash")
673+
}
674+
}

0 commit comments

Comments
 (0)