Skip to content
Merged
Show file tree
Hide file tree
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
85 changes: 85 additions & 0 deletions ledger/allegra/pparams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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()))
}
}
128 changes: 128 additions & 0 deletions ledger/alonzo/pparams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}
}
120 changes: 120 additions & 0 deletions ledger/babbage/pparams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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")
}
}
Loading
Loading