Skip to content

test(ledger): UTxO RPC functions for Shelley Transactions #1026

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions ledger/shelley/pparams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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()))
}
}
Loading