Skip to content

feat: added error return value to all Utxorpc() functions #1050

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 2 commits into from
Jun 21, 2025
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
21 changes: 14 additions & 7 deletions ledger/allegra/allegra.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,13 @@ func (b *AllegraBlock) Transactions() []common.Transaction {
return ret
}

func (b *AllegraBlock) Utxorpc() *utxorpc.Block {
func (b *AllegraBlock) Utxorpc() (*utxorpc.Block, error) {
txs := []*utxorpc.Tx{}
for _, t := range b.Transactions() {
tx := t.Utxorpc()
tx, err := t.Utxorpc()
if err != nil {
return nil, err
}
txs = append(txs, tx)
}
body := &utxorpc.BlockBody{
Expand All @@ -130,7 +133,7 @@ func (b *AllegraBlock) Utxorpc() *utxorpc.Block {
Body: body,
Header: header,
}
return block
return block, nil
}

type AllegraBlockHeader struct {
Expand Down Expand Up @@ -221,8 +224,8 @@ func (b *AllegraTransactionBody) AuxDataHash() *common.Blake2b256 {
return b.TxAuxDataHash
}

func (b *AllegraTransactionBody) Utxorpc() *utxorpc.Tx {
return common.TransactionBodyToUtxorpc(b)
func (b *AllegraTransactionBody) Utxorpc() (*utxorpc.Tx, error) {
return common.TransactionBodyToUtxorpc(b), nil
}

type AllegraTransaction struct {
Expand Down Expand Up @@ -332,8 +335,12 @@ func (t AllegraTransaction) Metadata() *cbor.LazyValue {
return t.TxMetadata
}

func (t AllegraTransaction) Utxorpc() *utxorpc.Tx {
return t.Body.Utxorpc()
func (t AllegraTransaction) Utxorpc() (*utxorpc.Tx, error) {
tx, err := t.Body.Utxorpc()
if err != nil {
return nil, fmt.Errorf("failed to convert transaction body: %w", err)
}
return tx, nil
}

func (t AllegraTransaction) IsValid() bool {
Expand Down
17 changes: 12 additions & 5 deletions ledger/allegra/pparams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@ func TestAllegraUtxorpc(t *testing.T) {
},
}

result := inputParams.Utxorpc()

result, err := inputParams.Utxorpc()
if err != nil {
t.Fatalf("Utxorpc() conversion failed: %v", err)
}
if !reflect.DeepEqual(result, expectedUtxorpc) {
t.Fatalf(
"Utxorpc() test failed for Allegra:\nExpected: %#v\nGot: %#v",
Expand Down Expand Up @@ -160,7 +162,10 @@ func TestAllegraTransactionBody_Utxorpc(t *testing.T) {
}

// Run Utxorpc conversion
actual := txBody.Utxorpc()
actual, err := txBody.Utxorpc()
if err != nil {
t.Errorf("Failed to convert the transaction")
}

// Check that the fee matches
if actual.Fee != txBody.Fee() {
Expand Down Expand Up @@ -220,8 +225,10 @@ func TestAllegraTransaction_Utxorpc(t *testing.T) {
}

// Run Utxorpc conversion
actual := tx.Utxorpc()

actual, err := tx.Utxorpc()
if err != nil {
t.Errorf("Failed to convert the transaction")
}
// Assertion checks
if actual.Fee != tx.Fee() {
t.Errorf(
Expand Down
21 changes: 14 additions & 7 deletions ledger/alonzo/alonzo.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,13 @@ func (b *AlonzoBlock) Transactions() []common.Transaction {
return ret
}

func (b *AlonzoBlock) Utxorpc() *utxorpc.Block {
func (b *AlonzoBlock) Utxorpc() (*utxorpc.Block, error) {
txs := []*utxorpc.Tx{}
for _, t := range b.Transactions() {
tx := t.Utxorpc()
tx, err := t.Utxorpc()
if err != nil {
return nil, err
}
txs = append(txs, tx)
}
body := &utxorpc.BlockBody{
Expand All @@ -139,7 +142,7 @@ func (b *AlonzoBlock) Utxorpc() *utxorpc.Block {
Body: body,
Header: header,
}
return block
return block, nil
}

type AlonzoBlockHeader struct {
Expand Down Expand Up @@ -255,8 +258,8 @@ func (b *AlonzoTransactionBody) ScriptDataHash() *common.Blake2b256 {
return b.TxScriptDataHash
}

func (b *AlonzoTransactionBody) Utxorpc() *utxorpc.Tx {
return common.TransactionBodyToUtxorpc(b)
func (b *AlonzoTransactionBody) Utxorpc() (*utxorpc.Tx, error) {
return common.TransactionBodyToUtxorpc(b), nil
}

type AlonzoTransactionOutput struct {
Expand Down Expand Up @@ -640,8 +643,12 @@ func (t *AlonzoTransaction) Cbor() []byte {
return cborData
}

func (t *AlonzoTransaction) Utxorpc() *utxorpc.Tx {
return t.Body.Utxorpc()
func (t *AlonzoTransaction) Utxorpc() (*utxorpc.Tx, error) {
tx, err := t.Body.Utxorpc()
if err != nil {
return nil, fmt.Errorf("failed to convert Alonzo transaction: %w", err)
}
return tx, nil
}

func NewAlonzoBlockFromCbor(data []byte) (*AlonzoBlock, error) {
Expand Down
15 changes: 8 additions & 7 deletions ledger/alonzo/pparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package alonzo

import (
"errors"
"fmt"
"maps"
"math"
Expand Down Expand Up @@ -273,32 +274,32 @@ func (u *AlonzoProtocolParameterUpdate) UnmarshalCBOR(cborData []byte) error {
return nil
}

func (p *AlonzoProtocolParameters) Utxorpc() *cardano.PParams {
func (p *AlonzoProtocolParameters) Utxorpc() (*cardano.PParams, error) {
// sanity check
if p.A0.Num().Int64() > math.MaxInt32 ||
p.A0.Denom().Int64() < 0 ||
p.A0.Denom().Int64() > math.MaxUint32 {
return nil
return nil, errors.New("invalid A0 rational number values")
}
if p.Rho.Num().Int64() > math.MaxInt32 ||
p.Rho.Denom().Int64() < 0 ||
p.Rho.Denom().Int64() > math.MaxUint32 {
return nil
return nil, errors.New("invalid Rho rational number values")
}
if p.Tau.Num().Int64() > math.MaxInt32 ||
p.Tau.Denom().Int64() < 0 ||
p.Tau.Denom().Int64() > math.MaxUint32 {
return nil
return nil, errors.New("invalid Tau rational number values")
}
if p.ExecutionCosts.MemPrice.Num().Int64() > math.MaxInt32 ||
p.ExecutionCosts.MemPrice.Denom().Int64() < 0 ||
p.ExecutionCosts.MemPrice.Denom().Int64() > math.MaxUint32 {
return nil
return nil, errors.New("invalid memory price rational number values")
}
if p.ExecutionCosts.StepPrice.Num().Int64() > math.MaxInt32 ||
p.ExecutionCosts.StepPrice.Denom().Int64() < 0 ||
p.ExecutionCosts.StepPrice.Denom().Int64() > math.MaxUint32 {
return nil
return nil, errors.New("invalid step price rational number values")
}
// #nosec G115
return &cardano.PParams{
Expand Down Expand Up @@ -353,7 +354,7 @@ func (p *AlonzoProtocolParameters) Utxorpc() *cardano.PParams {
Memory: p.MaxBlockExUnits.Memory,
Steps: p.MaxBlockExUnits.Steps,
},
}
}, nil
}

func UpgradePParams(
Expand Down
15 changes: 12 additions & 3 deletions ledger/alonzo/pparams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,10 @@ func TestAlonzoUtxorpc(t *testing.T) {
},
}

result := inputParams.Utxorpc()
result, err := inputParams.Utxorpc()
if err != nil {
t.Fatalf("Utxorpc() conversion failed")
}

if !reflect.DeepEqual(result, expectedUtxorpc) {
t.Fatalf(
Expand Down Expand Up @@ -512,7 +515,10 @@ func TestAlonzoTransactionBody_Utxorpc(t *testing.T) {
}

// Run Utxorpc conversion
got := body.Utxorpc()
got, err := body.Utxorpc()
if err != nil {
t.Errorf("Failed to convert the transaction")
}

// Assertion checks
if got.Fee != 50 {
Expand Down Expand Up @@ -572,7 +578,10 @@ func TestAlonzoTransaction_Utxorpc(t *testing.T) {
}

// Run Utxorpc conversion
got := tx.Utxorpc()
got, err := tx.Utxorpc()
if err != nil {
t.Errorf("Failed to convert the transaction")
}

// Assertion checks
if got.Fee != 75 {
Expand Down
21 changes: 14 additions & 7 deletions ledger/babbage/babbage.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,13 @@ func (b *BabbageBlock) Transactions() []common.Transaction {
return ret
}

func (b *BabbageBlock) Utxorpc() *utxorpc.Block {
func (b *BabbageBlock) Utxorpc() (*utxorpc.Block, error) {
txs := []*utxorpc.Tx{}
for _, t := range b.Transactions() {
tx := t.Utxorpc()
tx, err := t.Utxorpc()
if err != nil {
return nil, err
}
txs = append(txs, tx)
}
body := &utxorpc.BlockBody{
Expand All @@ -141,7 +144,7 @@ func (b *BabbageBlock) Utxorpc() *utxorpc.Block {
Body: body,
Header: header,
}
return block
return block, nil
}

type BabbageBlockHeader struct {
Expand Down Expand Up @@ -353,8 +356,8 @@ func (b *BabbageTransactionBody) TotalCollateral() uint64 {
return b.TxTotalCollateral
}

func (b *BabbageTransactionBody) Utxorpc() *utxorpc.Tx {
return common.TransactionBodyToUtxorpc(b)
func (b *BabbageTransactionBody) Utxorpc() (*utxorpc.Tx, error) {
return common.TransactionBodyToUtxorpc(b), nil
}

const (
Expand Down Expand Up @@ -799,8 +802,12 @@ func (t *BabbageTransaction) Cbor() []byte {
return cborData
}

func (t *BabbageTransaction) Utxorpc() *utxorpc.Tx {
return t.Body.Utxorpc()
func (t *BabbageTransaction) Utxorpc() (*utxorpc.Tx, error) {
tx, err := t.Body.Utxorpc()
if err != nil {
return nil, fmt.Errorf("failed to convert Babbage transaction: %w", err)
}
return tx, nil
}

func NewBabbageBlockFromCbor(data []byte) (*BabbageBlock, error) {
Expand Down
5 changes: 4 additions & 1 deletion ledger/babbage/babbage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2731,7 +2731,10 @@ func TestBabbageBlock_Utxorpc(t *testing.T) {
assert.NotNil(t, babbageBlock.TransactionBodies)
assert.NotNil(t, babbageBlock.TransactionWitnessSets)

utxoBlock := babbageBlock.Utxorpc()
utxoBlock, err := babbageBlock.Utxorpc()
if err != nil {
t.Fatalf("Utxorpc() failed: %v", err)
}

// Validate the resulting utxorpc.Block
assert.NotNil(t, utxoBlock)
Expand Down
15 changes: 8 additions & 7 deletions ledger/babbage/pparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package babbage

import (
"errors"
"math"

"github.com/blinklabs-io/gouroboros/cbor"
Expand Down Expand Up @@ -162,32 +163,32 @@ func (u *BabbageProtocolParameterUpdate) UnmarshalCBOR(cborData []byte) error {
return nil
}

func (p *BabbageProtocolParameters) Utxorpc() *cardano.PParams {
func (p *BabbageProtocolParameters) Utxorpc() (*cardano.PParams, error) {
// sanity check
if p.A0.Num().Int64() > math.MaxInt32 ||
p.A0.Denom().Int64() < 0 ||
p.A0.Denom().Int64() > math.MaxUint32 {
return nil
return nil, errors.New("invalid A0 rational number values")
}
if p.Rho.Num().Int64() > math.MaxInt32 ||
p.Rho.Denom().Int64() < 0 ||
p.Rho.Denom().Int64() > math.MaxUint32 {
return nil
return nil, errors.New("invalid Rho rational number values")
}
if p.Tau.Num().Int64() > math.MaxInt32 ||
p.Tau.Denom().Int64() < 0 ||
p.Tau.Denom().Int64() > math.MaxUint32 {
return nil
return nil, errors.New("invalid Tau rational number values")
}
if p.ExecutionCosts.MemPrice.Num().Int64() > math.MaxInt32 ||
p.ExecutionCosts.MemPrice.Denom().Int64() < 0 ||
p.ExecutionCosts.MemPrice.Denom().Int64() > math.MaxUint32 {
return nil
return nil, errors.New("invalid memory price rational number values")
}
if p.ExecutionCosts.StepPrice.Num().Int64() > math.MaxInt32 ||
p.ExecutionCosts.StepPrice.Denom().Int64() < 0 ||
p.ExecutionCosts.StepPrice.Denom().Int64() > math.MaxUint32 {
return nil
return nil, errors.New("invalid step price rational number values")
}
// #nosec G115
return &cardano.PParams{
Expand Down Expand Up @@ -242,7 +243,7 @@ func (p *BabbageProtocolParameters) Utxorpc() *cardano.PParams {
Memory: p.MaxBlockExUnits.Memory,
Steps: p.MaxBlockExUnits.Steps,
},
}
}, nil
}

func UpgradePParams(
Expand Down
Loading
Loading