diff --git a/ledger/shelley/pparams_test.go b/ledger/shelley/pparams_test.go index 58cdba2a..2bba89f0 100644 --- a/ledger/shelley/pparams_test.go +++ b/ledger/shelley/pparams_test.go @@ -22,6 +22,7 @@ import ( "testing" "github.com/blinklabs-io/gouroboros/cbor" + "github.com/blinklabs-io/gouroboros/ledger/common" "github.com/blinklabs-io/gouroboros/ledger/shelley" "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano" ) @@ -161,3 +162,142 @@ func TestShelleyUtxorpc(t *testing.T) { ) } } + +// Tests conversion of a ShelleyTransactionInput to its utxorpc-compatible representation. +func TestShelleyTransactionInput_Utxorpc(t *testing.T) { + // Create a mock transaction input with dummy transaction hash and index + input := shelley.NewShelleyTransactionInput("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1) + + // Convert it to utxorpc TxInput + got := input.Utxorpc() + + // Expected value with same hash and index + want := &cardano.TxInput{ + TxHash: input.Id().Bytes(), + OutputIndex: input.Index(), + } + + // Compare actual and expected results + if !reflect.DeepEqual(got, want) { + t.Errorf("TxInput.Utxorpc() mismatch\nGot: %+v\nWant: %+v", got, want) + } +} + +// Test the conversion of a ShelleyTransactionOutput to its utxorpc-compatible representation. +func TestShelleyTransactionOutput_Utxorpc(t *testing.T) { + // Use a zero-value common.Address as a placeholder + address := common.Address{} + + // Create a transaction output + output := shelley.ShelleyTransactionOutput{ + OutputAddress: address, + OutputAmount: 1000, + } + + // Convert it to utxorpc format + actual := output.Utxorpc() + + // expected output in utxorpc format + expected := &cardano.TxOutput{ + Address: address.Bytes(), + Coin: 1000, + } + + // Debug prints + t.Logf("DEBUG got.Address=%#v want.Address=%#v\n", actual.Address, expected.Address) + + // Compare actual and expected results + if !reflect.DeepEqual(actual, expected) { + t.Errorf("TxOutput.Utxorpc() mismatch\nGot: %+v\nWant: %+v", actual, expected) + } +} + +// Test the conversion of a full ShelleyTransactionBody to utxorpc format, verifying fee, input count, and output count. +func TestShelleyTransactionBody_Utxorpc(t *testing.T) { + // Create input set with one mock input + input := shelley.NewShelleyTransactionInput("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", 0) + inputSet := shelley.NewShelleyTransactionInputSet([]shelley.ShelleyTransactionInput{input}) + + // Use mock address + address := common.Address{} + + // Define a transaction output + output := shelley.ShelleyTransactionOutput{ + OutputAddress: address, + OutputAmount: 2000, + } + + // Create the transaction body + txBody := &shelley.ShelleyTransactionBody{ + TxInputs: inputSet, + TxOutputs: []shelley.ShelleyTransactionOutput{output}, + TxFee: 150, + Ttl: 1000, + } + + // Convert the transaction body to utxorpc format + actual := txBody.Utxorpc() + + // Check that the fee matches + if actual.Fee != txBody.Fee() { + t.Errorf("TxBody.Utxorpc() fee mismatch\nGot: %d\nWant: %d", actual.Fee, txBody.Fee()) + } + + // Check number of inputs + if len(actual.Inputs) != len(txBody.Inputs()) { + t.Errorf("TxBody.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("TxBody.Utxorpc() output length mismatch\nGot: %d\nWant: %d", len(actual.Outputs), len(txBody.Outputs())) + } +} + +// Test the conversion of a full ShelleyTransaction to utxorpc format, verifying fee, input count, and output count. +func TestShelleyTransaction_Utxorpc(t *testing.T) { + // Create input set with one mock input + input := shelley.NewShelleyTransactionInput("cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", 0) + inputSet := shelley.NewShelleyTransactionInputSet([]shelley.ShelleyTransactionInput{input}) + + // Use mock address + address := common.Address{} + + // Define a transaction output + output := shelley.ShelleyTransactionOutput{ + OutputAddress: address, + OutputAmount: 5000, + } + + // Build the transaction body + txBody := shelley.ShelleyTransactionBody{ + TxInputs: inputSet, + TxOutputs: []shelley.ShelleyTransactionOutput{output}, + TxFee: 250, + Ttl: 800, + } + + // Create full transaction with body and empty witness set + tx := shelley.ShelleyTransaction{ + Body: txBody, + WitnessSet: shelley.ShelleyTransactionWitnessSet{}, + } + + // Invoke Utxorpc conversion + got := tx.Utxorpc() + + // Verify the fee + if got.Fee != tx.Body.Fee() { + t.Errorf("ShelleyTransaction.Utxorpc() fee mismatch\nGot: %d\nWant: %d", got.Fee, tx.Body.Fee()) + } + + // Verify input count + if len(got.Inputs) != len(tx.Body.Inputs()) { + t.Errorf("ShelleyTransaction.Utxorpc() input count mismatch\nGot: %d\nWant: %d", len(got.Inputs), len(tx.Body.Inputs())) + } + + // Verify output count + if len(got.Outputs) != len(tx.Body.Outputs()) { + t.Errorf("ShelleyTransaction.Utxorpc() output count mismatch\nGot: %d\nWant: %d", len(got.Outputs), len(tx.Body.Outputs())) + } +}