From 0b8302177e12d3d3778d1ef1c2380326a5f5c2a9 Mon Sep 17 00:00:00 2001 From: Akhil Repala Date: Sun, 8 Jun 2025 22:55:58 -0500 Subject: [PATCH] feat(ledger): Added unit tests for allegra, alonzo, babbage, byron, conway and mary transaction Utxorpc conversions Signed-off-by: Akhil Repala --- ledger/allegra/pparams_test.go | 85 ++++++++++++++++++++++ ledger/alonzo/pparams_test.go | 128 +++++++++++++++++++++++++++++++++ ledger/babbage/pparams_test.go | 120 +++++++++++++++++++++++++++++++ ledger/byron/byron_test.go | 69 ++++++++++++++++++ ledger/conway/pparams_test.go | 121 +++++++++++++++++++++++++++++++ ledger/mary/pparams_test.go | 114 +++++++++++++++++++++++++++++ 6 files changed, 637 insertions(+) create mode 100644 ledger/byron/byron_test.go diff --git a/ledger/allegra/pparams_test.go b/ledger/allegra/pparams_test.go index 6e9290b2..0ac58790 100644 --- a/ledger/allegra/pparams_test.go +++ b/ledger/allegra/pparams_test.go @@ -22,6 +22,8 @@ import ( "github.com/blinklabs-io/gouroboros/cbor" "github.com/blinklabs-io/gouroboros/ledger/allegra" + "github.com/blinklabs-io/gouroboros/ledger/common" + "github.com/blinklabs-io/gouroboros/ledger/shelley" "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano" ) @@ -128,3 +130,86 @@ func TestAllegraUtxorpc(t *testing.T) { ) } } + +// Unit test for AllegraTransactionBody.Utxorpc() +func TestAllegraTransactionBody_Utxorpc(t *testing.T) { + // mock input + input := shelley.NewShelleyTransactionInput("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1) + inputSet := shelley.NewShelleyTransactionInputSet([]shelley.ShelleyTransactionInput{input}) + + address := common.Address{} + + // Define a transaction output + output := shelley.ShelleyTransactionOutput{ + OutputAddress: address, + OutputAmount: 1000, + } + + // Create transaction body + txBody := &allegra.AllegraTransactionBody{ + TxInputs: inputSet, + TxOutputs: []shelley.ShelleyTransactionOutput{output}, + TxFee: 200, + Ttl: 5000, + TxValidityIntervalStart: 4500, + } + + // Run Utxorpc conversion + actual := txBody.Utxorpc() + + // Check that the fee matches + if actual.Fee != txBody.Fee() { + t.Errorf("AllegraTransactionBody.Utxorpc() fee mismatch\nGot: %d\nWant: %d", actual.Fee, txBody.Fee()) + } + // Check number of inputs + if len(actual.Inputs) != len(txBody.Inputs()) { + t.Errorf("AllegraTransactionBody.Utxorpc() input length mismatch\nGot: %d\nWant: %d", len(actual.Inputs), len(txBody.Inputs())) + } + // Check number of outputs + if len(actual.Outputs) != len(txBody.Outputs()) { + t.Errorf("AllegraTransactionBody.Utxorpc() output length mismatch\nGot: %d\nWant: %d", len(actual.Outputs), len(txBody.Outputs())) + } +} + +// Unit test for AllegraTransaction.Utxorpc() +func TestAllegraTransaction_Utxorpc(t *testing.T) { + // Prepare mock input + input := shelley.NewShelleyTransactionInput("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", 0) + inputSet := shelley.NewShelleyTransactionInputSet([]shelley.ShelleyTransactionInput{input}) + + // Prepare mock output + address := common.Address{} + output := shelley.ShelleyTransactionOutput{ + OutputAddress: address, + OutputAmount: 2000, + } + + // Create transaction body + tx := &allegra.AllegraTransaction{ + Body: allegra.AllegraTransactionBody{ + TxInputs: inputSet, + TxOutputs: []shelley.ShelleyTransactionOutput{output}, + TxFee: 150, + Ttl: 1000, + TxValidityIntervalStart: 950, + }, + WitnessSet: shelley.ShelleyTransactionWitnessSet{}, + TxMetadata: nil, + } + + // Run Utxorpc conversion + actual := tx.Utxorpc() + + // Assertion checks + if actual.Fee != tx.Fee() { + t.Errorf("AllegraTransaction.Utxorpc() fee mismatch\nGot: %d\nWant: %d", actual.Fee, tx.Fee()) + } + + if len(actual.Inputs) != len(tx.Inputs()) { + t.Errorf("AllegraTransaction.Utxorpc() input length mismatch\nGot: %d\nWant: %d", len(actual.Inputs), len(tx.Inputs())) + } + + if len(actual.Outputs) != len(tx.Outputs()) { + t.Errorf("AllegraTransaction.Utxorpc() output length mismatch\nGot: %d\nWant: %d", len(actual.Outputs), len(tx.Outputs())) + } +} diff --git a/ledger/alonzo/pparams_test.go b/ledger/alonzo/pparams_test.go index ade740fb..0726f72e 100644 --- a/ledger/alonzo/pparams_test.go +++ b/ledger/alonzo/pparams_test.go @@ -27,7 +27,10 @@ import ( "github.com/blinklabs-io/gouroboros/cbor" "github.com/blinklabs-io/gouroboros/ledger/alonzo" "github.com/blinklabs-io/gouroboros/ledger/common" + "github.com/blinklabs-io/gouroboros/ledger/mary" + "github.com/blinklabs-io/gouroboros/ledger/shelley" cardano "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano" + utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano" ) func newBaseProtocolParams() alonzo.AlonzoProtocolParameters { @@ -446,3 +449,128 @@ func toJSON(v interface{}) string { } return string(b) } + +// Unit test for AlonzoTransactionOutput.Utxorpc() +func TestAlonzoTransactionOutput_Utxorpc(t *testing.T) { + address := common.Address{} + amount := uint64(5000) + datumHash := common.Blake2b256{1, 2, 3, 4} + + // Mock output + output := alonzo.AlonzoTransactionOutput{ + OutputAddress: address, + OutputAmount: mary.MaryTransactionOutputValue{Amount: amount}, + TxOutputDatumHash: &datumHash, + } + + got := output.Utxorpc() + want := &utxorpc.TxOutput{ + Address: address.Bytes(), + Coin: amount, + Assets: nil, + Datum: &utxorpc.Datum{ + Hash: datumHash.Bytes(), + }, + } + + if !reflect.DeepEqual(got, want) { + t.Errorf("AlonzoTransactionOutput.Utxorpc() mismatch\nGot: %+v\nWant: %+v", got, want) + } +} + +// Unit test for AlonzoTransactionBody.Utxorpc() +func TestAlonzoTransactionBody_Utxorpc(t *testing.T) { + // Mock input + input := shelley.NewShelleyTransactionInput("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 0) + inputSet := shelley.NewShelleyTransactionInputSet([]shelley.ShelleyTransactionInput{input}) + + // Mock output + address := common.Address{} + datumHash := common.Blake2b256{1, 2, 3, 4} + output := alonzo.AlonzoTransactionOutput{ + OutputAddress: address, + OutputAmount: mary.MaryTransactionOutputValue{Amount: 1000}, + TxOutputDatumHash: &datumHash, + } + + body := alonzo.AlonzoTransactionBody{ + TxInputs: inputSet, + TxOutputs: []alonzo.AlonzoTransactionOutput{output}, + TxFee: 50, + } + + // Run Utxorpc conversion + got := body.Utxorpc() + + // Assertion checks + if got.Fee != 50 { + t.Errorf("Fee mismatch: got %d, want 50", got.Fee) + } + if len(got.Inputs) != 1 { + t.Errorf("Expected 1 input, got %d", len(got.Inputs)) + } + if got.Inputs[0].OutputIndex != input.Index() { + t.Errorf("Input index mismatch: got %d, want %d", got.Inputs[0].OutputIndex, input.Index()) + } + if len(got.Outputs) != 1 { + t.Errorf("Expected 1 output, got %d", len(got.Outputs)) + } + if got.Outputs[0].Coin != 1000 { + t.Errorf("Output coin mismatch: got %d, want 1000", got.Outputs[0].Coin) + } + if len(got.Hash) == 0 { + t.Error("Expected non-empty transaction hash") + } +} + +// Unit test for AlonzoTransaction.Utxorpc() +func TestAlonzoTransaction_Utxorpc(t *testing.T) { + // Mock input + input := shelley.NewShelleyTransactionInput("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", 1) + inputSet := shelley.NewShelleyTransactionInputSet([]shelley.ShelleyTransactionInput{input}) + + // Mock output + address := common.Address{} + datumHash := &common.Blake2b256{0x11, 0x22, 0x33} + output := alonzo.AlonzoTransactionOutput{ + OutputAddress: address, + OutputAmount: mary.MaryTransactionOutputValue{Amount: 2000}, + TxOutputDatumHash: datumHash, + } + + body := alonzo.AlonzoTransactionBody{ + TxInputs: inputSet, + TxOutputs: []alonzo.AlonzoTransactionOutput{output}, + TxFee: 75, + } + + // Wrap in transaction + tx := alonzo.AlonzoTransaction{ + Body: body, + TxIsValid: true, + } + + // Run Utxorpc conversion + got := tx.Utxorpc() + + // Assertion checks + if got.Fee != 75 { + t.Errorf("Transaction fee mismatch: got %d, want 75", got.Fee) + } + if len(got.Inputs) != 1 { + t.Errorf("Expected 1 input, got %d", len(got.Inputs)) + } + if len(got.Outputs) != 1 { + t.Errorf("Expected 1 output, got %d", len(got.Outputs)) + } + if got.Outputs[0].Coin != 2000 { + t.Errorf("Output coin mismatch: got %d, want 2000", got.Outputs[0].Coin) + } + expectedDatum := datumHash.Bytes() + if !reflect.DeepEqual(got.Outputs[0].Datum.Hash, expectedDatum) { + t.Errorf("Datum hash mismatch: got %x, want %x", got.Outputs[0].Datum.Hash, expectedDatum) + } + if len(got.Hash) == 0 { + t.Error("Expected non-empty transaction hash") + } +} diff --git a/ledger/babbage/pparams_test.go b/ledger/babbage/pparams_test.go index 6f4d5f20..c772a163 100644 --- a/ledger/babbage/pparams_test.go +++ b/ledger/babbage/pparams_test.go @@ -23,7 +23,10 @@ import ( "github.com/blinklabs-io/gouroboros/cbor" "github.com/blinklabs-io/gouroboros/ledger/babbage" "github.com/blinklabs-io/gouroboros/ledger/common" + "github.com/blinklabs-io/gouroboros/ledger/mary" + "github.com/blinklabs-io/gouroboros/ledger/shelley" "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano" + utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano" ) func TestBabbageProtocolParamsUpdate(t *testing.T) { @@ -552,3 +555,120 @@ func TestBabbageUtxorpc(t *testing.T) { } } } + +// Unit test for BabbageTransactionInput.Utxorpc() +func TestBabbageTransactionInput_Utxorpc(t *testing.T) { + input := shelley.NewShelleyTransactionInput("cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", 2) + + got := input.Utxorpc() + want := &cardano.TxInput{ + TxHash: input.Id().Bytes(), + OutputIndex: input.Index(), + } + + if !reflect.DeepEqual(got, want) { + t.Errorf("BabbageTransactionInput.Utxorpc() mismatch\\nGot: %+v\\nWant: %+v", got, want) + } +} + +// Unit test for BabbageTransactionOutput.Utxorpc() +func TestBabbageTransactionOutput_Utxorpc(t *testing.T) { + address := common.Address{} + amount := uint64(8400) + + output := babbage.BabbageTransactionOutput{ + OutputAddress: address, + OutputAmount: mary.MaryTransactionOutputValue{Amount: amount}, + } + + got := output.Utxorpc() + want := &utxorpc.TxOutput{ + Address: address.Bytes(), + Coin: amount, + Datum: &utxorpc.Datum{ + Hash: make([]byte, 32), + }, + } + + if !reflect.DeepEqual(got, want) { + t.Errorf("BabbageTransactionOutput.Utxorpc() mismatch\nGot: %+v\nWant: %+v", got, want) + } +} + +// Unit test for BabbageTransactionBody.Utxorpc() +func TestBabbageTransactionBody_Utxorpc(t *testing.T) { + input := shelley.NewShelleyTransactionInput( + "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", 1) + inputSet := shelley.NewShelleyTransactionInputSet([]shelley.ShelleyTransactionInput{input}) + + address := common.Address{} + output := babbage.BabbageTransactionOutput{ + OutputAddress: address, + OutputAmount: mary.MaryTransactionOutputValue{Amount: 5000}, + } + + body := babbage.BabbageTransactionBody{ + TxInputs: inputSet, + TxOutputs: []babbage.BabbageTransactionOutput{output}, + TxFee: 100, + } + + got := body.Utxorpc() + + if got.Fee != 100 { + t.Errorf("Fee mismatch: got %d, want 100", got.Fee) + } + if len(got.Inputs) != 1 { + t.Errorf("Expected 1 input, got %d", len(got.Inputs)) + } + if len(got.Outputs) != 1 { + t.Errorf("Expected 1 output, got %d", len(got.Outputs)) + } + if got.Outputs[0].Coin != 5000 { + t.Errorf("Output coin mismatch: got %d, want 5000", got.Outputs[0].Coin) + } + if len(got.Hash) == 0 { + t.Error("Expected non-empty transaction hash") + } +} + +// Unit test for BabbageTransaction.Utxorpc() +func TestBabbageTransaction_Utxorpc(t *testing.T) { + input := shelley.NewShelleyTransactionInput( + "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", 2) + inputSet := shelley.NewShelleyTransactionInputSet([]shelley.ShelleyTransactionInput{input}) + + address := common.Address{} + output := babbage.BabbageTransactionOutput{ + OutputAddress: address, + OutputAmount: mary.MaryTransactionOutputValue{Amount: 9000}, + } + + body := babbage.BabbageTransactionBody{ + TxInputs: inputSet, + TxOutputs: []babbage.BabbageTransactionOutput{output}, + TxFee: 150, + } + tx := babbage.BabbageTransaction{ + Body: body, + TxIsValid: true, + } + + got := tx.Utxorpc() + + if got.Fee != 150 { + t.Errorf("Fee mismatch: got %d, want 150", got.Fee) + } + if len(got.Inputs) != 1 { + t.Errorf("Expected 1 input, got %d", len(got.Inputs)) + } + if len(got.Outputs) != 1 { + t.Errorf("Expected 1 output, got %d", len(got.Outputs)) + } + if got.Outputs[0].Coin != 9000 { + t.Errorf("Output coin mismatch: got %d, want 9000", got.Outputs[0].Coin) + } + if len(got.Hash) == 0 { + t.Error("Expected non-empty transaction hash") + } +} diff --git a/ledger/byron/byron_test.go b/ledger/byron/byron_test.go new file mode 100644 index 00000000..51e0f967 --- /dev/null +++ b/ledger/byron/byron_test.go @@ -0,0 +1,69 @@ +package byron_test + +import ( + "reflect" + "testing" + + "github.com/blinklabs-io/gouroboros/ledger/byron" + "github.com/blinklabs-io/gouroboros/ledger/common" + utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano" +) + +// Unit test for ByronTransactionInput.Utxorpc() +func TestByronTransactionInput_Utxorpc(t *testing.T) { + input := byron.NewByronTransactionInput("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1) + + got := input.Utxorpc() + want := &utxorpc.TxInput{ + TxHash: input.Id().Bytes(), + OutputIndex: input.Index(), + } + + if !reflect.DeepEqual(got, want) { + t.Errorf("ByronTransactionInput.Utxorpc() mismatch\nGot: %+v\nWant: %+v", got, want) + } +} + +// Unit test for ByronTransactionOutput.Utxorpc() +func TestByronTransactionOutput_Utxorpc(t *testing.T) { + address := common.Address{} + output := byron.ByronTransactionOutput{ + OutputAddress: address, + OutputAmount: 5000, + } + + got := output.Utxorpc() + want := &utxorpc.TxOutput{ + Address: address.Bytes(), + Coin: output.OutputAmount, + } + + if !reflect.DeepEqual(got, want) { + t.Errorf("ByronTransactionOutput.Utxorpc() mismatch\nGot: %+v\nWant: %+v", got, want) + } +} + +// Unit test for ByronTransaction.Utxorpc() +func TestByronTransaction_Utxorpc_Empty(t *testing.T) { + // Create a dummy ByronTransaction + tx := &byron.ByronTransaction{} + + // Run Utxorpc conversion + result := tx.Utxorpc() + + // Validate it's not nil + if result == nil { + t.Fatal("ByronTransaction.Utxorpc() returned nil; expected empty utxorpc.Tx object") + } + + // Validate that it's the zero value + if len(result.Inputs) != 0 { + t.Errorf("Expected zero inputs, got %d", len(result.Inputs)) + } + if len(result.Outputs) != 0 { + t.Errorf("Expected zero outputs, got %d", len(result.Outputs)) + } + if result.Fee != 0 { + t.Errorf("Expected fee = 0, got %d", result.Fee) + } +} diff --git a/ledger/conway/pparams_test.go b/ledger/conway/pparams_test.go index 72fc2f7e..229de15b 100644 --- a/ledger/conway/pparams_test.go +++ b/ledger/conway/pparams_test.go @@ -22,8 +22,11 @@ import ( "testing" "github.com/blinklabs-io/gouroboros/cbor" + "github.com/blinklabs-io/gouroboros/ledger/babbage" "github.com/blinklabs-io/gouroboros/ledger/common" "github.com/blinklabs-io/gouroboros/ledger/conway" + "github.com/blinklabs-io/gouroboros/ledger/mary" + "github.com/blinklabs-io/gouroboros/ledger/shelley" "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano" ) @@ -596,3 +599,121 @@ func TestUtxorpc(t *testing.T) { } } } + +// Unit test for ConwayTransactionBody.Utxorpc() +func TestConwayTransactionBody_Utxorpc(t *testing.T) { + input := shelley.NewShelleyTransactionInput("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1) + var inputSet conway.ConwayTransactionInputSet + inputSet.SetItems([]shelley.ShelleyTransactionInput{input}) + + address := common.Address{} + output := babbage.BabbageTransactionOutput{ + OutputAddress: address, + OutputAmount: mary.MaryTransactionOutputValue{Amount: 5000}, + } + txCollateral := []shelley.ShelleyTransactionInput{input} + txTotalCollateral := uint64(200) + txReferenceInputs := []shelley.ShelleyTransactionInput{input} + txAuxDataHash := &common.Blake2b256{0xde, 0xad, 0xbe, 0xef} + txValidityIntervalStart := uint64(4000) + var signer common.Blake2b224 + copy(signer[:], []byte{0xab, 0xcd, 0xef}) + txRequiredSigners := []common.Blake2b224{signer} + txScriptDataHash := &common.Blake2b256{0xba, 0xad, 0xf0, 0x0d} + txMint := &common.MultiAsset[common.MultiAssetTypeMint]{} + + body := conway.ConwayTransactionBody{ + TxInputs: inputSet, + TxOutputs: []babbage.BabbageTransactionOutput{output}, + TxFee: 1000, + Ttl: 5000, + TxCollateral: txCollateral, + TxTotalCollateral: txTotalCollateral, + TxReferenceInputs: txReferenceInputs, + TxAuxDataHash: txAuxDataHash, + TxValidityIntervalStart: txValidityIntervalStart, + TxRequiredSigners: txRequiredSigners, + TxScriptDataHash: txScriptDataHash, + TxMint: txMint, + } + + got := body.Utxorpc() + + if got.Fee != 1000 { + t.Errorf("Fee mismatch: got %d, want 100", got.Fee) + } + if len(got.Inputs) != 1 { + t.Errorf("Expected 1 input, got %d", len(got.Inputs)) + } + if len(got.Outputs) != 1 { + t.Errorf("Expected 1 output, got %d", len(got.Outputs)) + } + if got.Outputs[0].Coin != 5000 { + t.Errorf("Output coin mismatch: got %d, want 5000", got.Outputs[0].Coin) + } + if len(got.Hash) == 0 { + t.Error("Expected non-empty transaction hash") + } +} + +// Unit test for ConwayTransaction.Utxorpc() +func TestConwayTransaction_Utxorpc(t *testing.T) { + input := shelley.NewShelleyTransactionInput("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 0) + var inputSet conway.ConwayTransactionInputSet + inputSet.SetItems([]shelley.ShelleyTransactionInput{input}) + + address := common.Address{} + output := babbage.BabbageTransactionOutput{ + OutputAddress: address, + OutputAmount: mary.MaryTransactionOutputValue{Amount: 5000}, + } + + txCollateral := []shelley.ShelleyTransactionInput{input} + txTotalCollateral := uint64(200) + txReferenceInputs := []shelley.ShelleyTransactionInput{input} + txAuxDataHash := &common.Blake2b256{0xde, 0xad, 0xbe, 0xef} + txValidityIntervalStart := uint64(4000) + var signer common.Blake2b224 + copy(signer[:], []byte{0xab, 0xcd, 0xef}) + txRequiredSigners := []common.Blake2b224{signer} + txScriptDataHash := &common.Blake2b256{0xba, 0xad, 0xf0, 0x0d} + txMint := &common.MultiAsset[common.MultiAssetTypeMint]{} + + body := conway.ConwayTransactionBody{ + TxInputs: inputSet, + TxOutputs: []babbage.BabbageTransactionOutput{output}, + TxFee: 1000, + Ttl: 5000, + TxCollateral: txCollateral, + TxTotalCollateral: txTotalCollateral, + TxReferenceInputs: txReferenceInputs, + TxAuxDataHash: txAuxDataHash, + TxValidityIntervalStart: txValidityIntervalStart, + TxRequiredSigners: txRequiredSigners, + TxScriptDataHash: txScriptDataHash, + TxMint: txMint, + } + + tx := &conway.ConwayTransaction{ + Body: body, + TxIsValid: true, + } + + got := tx.Utxorpc() + + if got.Fee != 1000 { + t.Errorf("Transaction fee mismatch: got %d, want 25", got.Fee) + } + if len(got.Inputs) != 1 { + t.Errorf("Expected 1 input, got %d", len(got.Inputs)) + } + if len(got.Outputs) != 1 { + t.Errorf("Expected 1 output, got %d", len(got.Outputs)) + } + if got.Outputs[0].Coin != 5000 { + t.Errorf("Output coin mismatch: got %d, want 8000", got.Outputs[0].Coin) + } + if len(got.Hash) == 0 { + t.Error("Expected non-empty transaction hash") + } +} diff --git a/ledger/mary/pparams_test.go b/ledger/mary/pparams_test.go index 17299d65..327c0b76 100644 --- a/ledger/mary/pparams_test.go +++ b/ledger/mary/pparams_test.go @@ -21,7 +21,9 @@ import ( "testing" "github.com/blinklabs-io/gouroboros/cbor" + "github.com/blinklabs-io/gouroboros/ledger/common" "github.com/blinklabs-io/gouroboros/ledger/mary" + "github.com/blinklabs-io/gouroboros/ledger/shelley" "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano" ) @@ -130,3 +132,115 @@ func TestMaryUtxorpc(t *testing.T) { ) } } + +// Unit test for MaryTransactionInput.Utxorpc() +func TestMaryTransactionInput_Utxorpc(t *testing.T) { + input := shelley.NewShelleyTransactionInput("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 0) + + got := input.Utxorpc() + want := &cardano.TxInput{ + TxHash: input.Id().Bytes(), + OutputIndex: input.Index(), + } + + if !reflect.DeepEqual(got, want) { + t.Errorf("MaryTransactionInput.Utxorpc() mismatch\\nGot: %+v\\nWant: %+v", got, want) + } +} + +// Unit test for MaryTransactionOutput.Utxorpc() +func TestMaryTransactionOutput_Utxorpc(t *testing.T) { + address := common.Address{} + amount := uint64(4200) + + output := mary.MaryTransactionOutput{ + OutputAddress: address, + OutputAmount: mary.MaryTransactionOutputValue{Amount: amount}, + } + + got := output.Utxorpc() + want := &cardano.TxOutput{ + Address: address.Bytes(), + Coin: amount, + } + + if !reflect.DeepEqual(got, want) { + t.Errorf("MaryTransactionOutput.Utxorpc() mismatch\\nGot: %+v\\nWant: %+v", got, want) + } +} + +// Unit test for MaryTransactionBody.Utxorpc() +func TestMaryTransactionBody_Utxorpc(t *testing.T) { + input := shelley.NewShelleyTransactionInput("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", 1) + inputSet := shelley.NewShelleyTransactionInputSet([]shelley.ShelleyTransactionInput{input}) + + address := common.Address{} + output := mary.MaryTransactionOutput{ + OutputAddress: address, + OutputAmount: mary.MaryTransactionOutputValue{Amount: 5000}, + } + + body := mary.MaryTransactionBody{ + TxInputs: inputSet, + TxOutputs: []mary.MaryTransactionOutput{output}, + TxFee: 100, + } + + got := body.Utxorpc() + + if got.Fee != 100 { + t.Errorf("Fee mismatch: got %d, want 100", got.Fee) + } + if len(got.Inputs) != 1 { + t.Errorf("Expected 1 input, got %d", len(got.Inputs)) + } + if len(got.Outputs) != 1 { + t.Errorf("Expected 1 output, got %d", len(got.Outputs)) + } + if got.Outputs[0].Coin != 5000 { + t.Errorf("Output coin mismatch: got %d, want 5000", got.Outputs[0].Coin) + } + if len(got.Hash) == 0 { + t.Error("Expected non-empty transaction hash") + } +} + +// Unit test for MaryTransaction.Utxorpc() +func TestMaryTransaction_Utxorpc(t *testing.T) { + input := shelley.NewShelleyTransactionInput("cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", 2) + inputSet := shelley.NewShelleyTransactionInputSet([]shelley.ShelleyTransactionInput{input}) + + address := common.Address{} + output := mary.MaryTransactionOutput{ + OutputAddress: address, + OutputAmount: mary.MaryTransactionOutputValue{Amount: 8000}, + } + + body := mary.MaryTransactionBody{ + TxInputs: inputSet, + TxOutputs: []mary.MaryTransactionOutput{output}, + TxFee: 25, + } + + tx := mary.MaryTransaction{ + Body: body, + } + + got := tx.Utxorpc() + + if got.Fee != 25 { + t.Errorf("Transaction fee mismatch: got %d, want 25", got.Fee) + } + if len(got.Inputs) != 1 { + t.Errorf("Expected 1 input, got %d", len(got.Inputs)) + } + if len(got.Outputs) != 1 { + t.Errorf("Expected 1 output, got %d", len(got.Outputs)) + } + if got.Outputs[0].Coin != 8000 { + t.Errorf("Output coin mismatch: got %d, want 8000", got.Outputs[0].Coin) + } + if len(got.Hash) == 0 { + t.Error("Expected non-empty transaction hash") + } +}