Skip to content

Commit b275c23

Browse files
authored
feat: added error return value to all Utxorpc() functions (#1050)
Signed-off-by: Jenita <[email protected]>
1 parent 71fe314 commit b275c23

24 files changed

+418
-196
lines changed

ledger/allegra/allegra.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,13 @@ func (b *AllegraBlock) Transactions() []common.Transaction {
112112
return ret
113113
}
114114

115-
func (b *AllegraBlock) Utxorpc() *utxorpc.Block {
115+
func (b *AllegraBlock) Utxorpc() (*utxorpc.Block, error) {
116116
txs := []*utxorpc.Tx{}
117117
for _, t := range b.Transactions() {
118-
tx := t.Utxorpc()
118+
tx, err := t.Utxorpc()
119+
if err != nil {
120+
return nil, err
121+
}
119122
txs = append(txs, tx)
120123
}
121124
body := &utxorpc.BlockBody{
@@ -130,7 +133,7 @@ func (b *AllegraBlock) Utxorpc() *utxorpc.Block {
130133
Body: body,
131134
Header: header,
132135
}
133-
return block
136+
return block, nil
134137
}
135138

136139
type AllegraBlockHeader struct {
@@ -221,8 +224,8 @@ func (b *AllegraTransactionBody) AuxDataHash() *common.Blake2b256 {
221224
return b.TxAuxDataHash
222225
}
223226

224-
func (b *AllegraTransactionBody) Utxorpc() *utxorpc.Tx {
225-
return common.TransactionBodyToUtxorpc(b)
227+
func (b *AllegraTransactionBody) Utxorpc() (*utxorpc.Tx, error) {
228+
return common.TransactionBodyToUtxorpc(b), nil
226229
}
227230

228231
type AllegraTransaction struct {
@@ -332,8 +335,12 @@ func (t AllegraTransaction) Metadata() *cbor.LazyValue {
332335
return t.TxMetadata
333336
}
334337

335-
func (t AllegraTransaction) Utxorpc() *utxorpc.Tx {
336-
return t.Body.Utxorpc()
338+
func (t AllegraTransaction) Utxorpc() (*utxorpc.Tx, error) {
339+
tx, err := t.Body.Utxorpc()
340+
if err != nil {
341+
return nil, fmt.Errorf("failed to convert transaction body: %w", err)
342+
}
343+
return tx, nil
337344
}
338345

339346
func (t AllegraTransaction) IsValid() bool {

ledger/allegra/pparams_test.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,10 @@ func TestAllegraUtxorpc(t *testing.T) {
120120
},
121121
}
122122

123-
result := inputParams.Utxorpc()
124-
123+
result, err := inputParams.Utxorpc()
124+
if err != nil {
125+
t.Fatalf("Utxorpc() conversion failed: %v", err)
126+
}
125127
if !reflect.DeepEqual(result, expectedUtxorpc) {
126128
t.Fatalf(
127129
"Utxorpc() test failed for Allegra:\nExpected: %#v\nGot: %#v",
@@ -160,7 +162,10 @@ func TestAllegraTransactionBody_Utxorpc(t *testing.T) {
160162
}
161163

162164
// Run Utxorpc conversion
163-
actual := txBody.Utxorpc()
165+
actual, err := txBody.Utxorpc()
166+
if err != nil {
167+
t.Errorf("Failed to convert the transaction")
168+
}
164169

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

222227
// Run Utxorpc conversion
223-
actual := tx.Utxorpc()
224-
228+
actual, err := tx.Utxorpc()
229+
if err != nil {
230+
t.Errorf("Failed to convert the transaction")
231+
}
225232
// Assertion checks
226233
if actual.Fee != tx.Fee() {
227234
t.Errorf(

ledger/alonzo/alonzo.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,13 @@ func (b *AlonzoBlock) Transactions() []common.Transaction {
121121
return ret
122122
}
123123

124-
func (b *AlonzoBlock) Utxorpc() *utxorpc.Block {
124+
func (b *AlonzoBlock) Utxorpc() (*utxorpc.Block, error) {
125125
txs := []*utxorpc.Tx{}
126126
for _, t := range b.Transactions() {
127-
tx := t.Utxorpc()
127+
tx, err := t.Utxorpc()
128+
if err != nil {
129+
return nil, err
130+
}
128131
txs = append(txs, tx)
129132
}
130133
body := &utxorpc.BlockBody{
@@ -139,7 +142,7 @@ func (b *AlonzoBlock) Utxorpc() *utxorpc.Block {
139142
Body: body,
140143
Header: header,
141144
}
142-
return block
145+
return block, nil
143146
}
144147

145148
type AlonzoBlockHeader struct {
@@ -255,8 +258,8 @@ func (b *AlonzoTransactionBody) ScriptDataHash() *common.Blake2b256 {
255258
return b.TxScriptDataHash
256259
}
257260

258-
func (b *AlonzoTransactionBody) Utxorpc() *utxorpc.Tx {
259-
return common.TransactionBodyToUtxorpc(b)
261+
func (b *AlonzoTransactionBody) Utxorpc() (*utxorpc.Tx, error) {
262+
return common.TransactionBodyToUtxorpc(b), nil
260263
}
261264

262265
type AlonzoTransactionOutput struct {
@@ -640,8 +643,12 @@ func (t *AlonzoTransaction) Cbor() []byte {
640643
return cborData
641644
}
642645

643-
func (t *AlonzoTransaction) Utxorpc() *utxorpc.Tx {
644-
return t.Body.Utxorpc()
646+
func (t *AlonzoTransaction) Utxorpc() (*utxorpc.Tx, error) {
647+
tx, err := t.Body.Utxorpc()
648+
if err != nil {
649+
return nil, fmt.Errorf("failed to convert Alonzo transaction: %w", err)
650+
}
651+
return tx, nil
645652
}
646653

647654
func NewAlonzoBlockFromCbor(data []byte) (*AlonzoBlock, error) {

ledger/alonzo/pparams.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package alonzo
1616

1717
import (
18+
"errors"
1819
"fmt"
1920
"maps"
2021
"math"
@@ -273,32 +274,32 @@ func (u *AlonzoProtocolParameterUpdate) UnmarshalCBOR(cborData []byte) error {
273274
return nil
274275
}
275276

276-
func (p *AlonzoProtocolParameters) Utxorpc() *cardano.PParams {
277+
func (p *AlonzoProtocolParameters) Utxorpc() (*cardano.PParams, error) {
277278
// sanity check
278279
if p.A0.Num().Int64() > math.MaxInt32 ||
279280
p.A0.Denom().Int64() < 0 ||
280281
p.A0.Denom().Int64() > math.MaxUint32 {
281-
return nil
282+
return nil, errors.New("invalid A0 rational number values")
282283
}
283284
if p.Rho.Num().Int64() > math.MaxInt32 ||
284285
p.Rho.Denom().Int64() < 0 ||
285286
p.Rho.Denom().Int64() > math.MaxUint32 {
286-
return nil
287+
return nil, errors.New("invalid Rho rational number values")
287288
}
288289
if p.Tau.Num().Int64() > math.MaxInt32 ||
289290
p.Tau.Denom().Int64() < 0 ||
290291
p.Tau.Denom().Int64() > math.MaxUint32 {
291-
return nil
292+
return nil, errors.New("invalid Tau rational number values")
292293
}
293294
if p.ExecutionCosts.MemPrice.Num().Int64() > math.MaxInt32 ||
294295
p.ExecutionCosts.MemPrice.Denom().Int64() < 0 ||
295296
p.ExecutionCosts.MemPrice.Denom().Int64() > math.MaxUint32 {
296-
return nil
297+
return nil, errors.New("invalid memory price rational number values")
297298
}
298299
if p.ExecutionCosts.StepPrice.Num().Int64() > math.MaxInt32 ||
299300
p.ExecutionCosts.StepPrice.Denom().Int64() < 0 ||
300301
p.ExecutionCosts.StepPrice.Denom().Int64() > math.MaxUint32 {
301-
return nil
302+
return nil, errors.New("invalid step price rational number values")
302303
}
303304
// #nosec G115
304305
return &cardano.PParams{
@@ -353,7 +354,7 @@ func (p *AlonzoProtocolParameters) Utxorpc() *cardano.PParams {
353354
Memory: p.MaxBlockExUnits.Memory,
354355
Steps: p.MaxBlockExUnits.Steps,
355356
},
356-
}
357+
}, nil
357358
}
358359

359360
func UpgradePParams(

ledger/alonzo/pparams_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,10 @@ func TestAlonzoUtxorpc(t *testing.T) {
431431
},
432432
}
433433

434-
result := inputParams.Utxorpc()
434+
result, err := inputParams.Utxorpc()
435+
if err != nil {
436+
t.Fatalf("Utxorpc() conversion failed")
437+
}
435438

436439
if !reflect.DeepEqual(result, expectedUtxorpc) {
437440
t.Fatalf(
@@ -512,7 +515,10 @@ func TestAlonzoTransactionBody_Utxorpc(t *testing.T) {
512515
}
513516

514517
// Run Utxorpc conversion
515-
got := body.Utxorpc()
518+
got, err := body.Utxorpc()
519+
if err != nil {
520+
t.Errorf("Failed to convert the transaction")
521+
}
516522

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

574580
// Run Utxorpc conversion
575-
got := tx.Utxorpc()
581+
got, err := tx.Utxorpc()
582+
if err != nil {
583+
t.Errorf("Failed to convert the transaction")
584+
}
576585

577586
// Assertion checks
578587
if got.Fee != 75 {

ledger/babbage/babbage.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,13 @@ func (b *BabbageBlock) Transactions() []common.Transaction {
123123
return ret
124124
}
125125

126-
func (b *BabbageBlock) Utxorpc() *utxorpc.Block {
126+
func (b *BabbageBlock) Utxorpc() (*utxorpc.Block, error) {
127127
txs := []*utxorpc.Tx{}
128128
for _, t := range b.Transactions() {
129-
tx := t.Utxorpc()
129+
tx, err := t.Utxorpc()
130+
if err != nil {
131+
return nil, err
132+
}
130133
txs = append(txs, tx)
131134
}
132135
body := &utxorpc.BlockBody{
@@ -141,7 +144,7 @@ func (b *BabbageBlock) Utxorpc() *utxorpc.Block {
141144
Body: body,
142145
Header: header,
143146
}
144-
return block
147+
return block, nil
145148
}
146149

147150
type BabbageBlockHeader struct {
@@ -353,8 +356,8 @@ func (b *BabbageTransactionBody) TotalCollateral() uint64 {
353356
return b.TxTotalCollateral
354357
}
355358

356-
func (b *BabbageTransactionBody) Utxorpc() *utxorpc.Tx {
357-
return common.TransactionBodyToUtxorpc(b)
359+
func (b *BabbageTransactionBody) Utxorpc() (*utxorpc.Tx, error) {
360+
return common.TransactionBodyToUtxorpc(b), nil
358361
}
359362

360363
const (
@@ -799,8 +802,12 @@ func (t *BabbageTransaction) Cbor() []byte {
799802
return cborData
800803
}
801804

802-
func (t *BabbageTransaction) Utxorpc() *utxorpc.Tx {
803-
return t.Body.Utxorpc()
805+
func (t *BabbageTransaction) Utxorpc() (*utxorpc.Tx, error) {
806+
tx, err := t.Body.Utxorpc()
807+
if err != nil {
808+
return nil, fmt.Errorf("failed to convert Babbage transaction: %w", err)
809+
}
810+
return tx, nil
804811
}
805812

806813
func NewBabbageBlockFromCbor(data []byte) (*BabbageBlock, error) {

ledger/babbage/babbage_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2731,7 +2731,10 @@ func TestBabbageBlock_Utxorpc(t *testing.T) {
27312731
assert.NotNil(t, babbageBlock.TransactionBodies)
27322732
assert.NotNil(t, babbageBlock.TransactionWitnessSets)
27332733

2734-
utxoBlock := babbageBlock.Utxorpc()
2734+
utxoBlock, err := babbageBlock.Utxorpc()
2735+
if err != nil {
2736+
t.Fatalf("Utxorpc() failed: %v", err)
2737+
}
27352738

27362739
// Validate the resulting utxorpc.Block
27372740
assert.NotNil(t, utxoBlock)

ledger/babbage/pparams.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package babbage
1616

1717
import (
18+
"errors"
1819
"math"
1920

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

165-
func (p *BabbageProtocolParameters) Utxorpc() *cardano.PParams {
166+
func (p *BabbageProtocolParameters) Utxorpc() (*cardano.PParams, error) {
166167
// sanity check
167168
if p.A0.Num().Int64() > math.MaxInt32 ||
168169
p.A0.Denom().Int64() < 0 ||
169170
p.A0.Denom().Int64() > math.MaxUint32 {
170-
return nil
171+
return nil, errors.New("invalid A0 rational number values")
171172
}
172173
if p.Rho.Num().Int64() > math.MaxInt32 ||
173174
p.Rho.Denom().Int64() < 0 ||
174175
p.Rho.Denom().Int64() > math.MaxUint32 {
175-
return nil
176+
return nil, errors.New("invalid Rho rational number values")
176177
}
177178
if p.Tau.Num().Int64() > math.MaxInt32 ||
178179
p.Tau.Denom().Int64() < 0 ||
179180
p.Tau.Denom().Int64() > math.MaxUint32 {
180-
return nil
181+
return nil, errors.New("invalid Tau rational number values")
181182
}
182183
if p.ExecutionCosts.MemPrice.Num().Int64() > math.MaxInt32 ||
183184
p.ExecutionCosts.MemPrice.Denom().Int64() < 0 ||
184185
p.ExecutionCosts.MemPrice.Denom().Int64() > math.MaxUint32 {
185-
return nil
186+
return nil, errors.New("invalid memory price rational number values")
186187
}
187188
if p.ExecutionCosts.StepPrice.Num().Int64() > math.MaxInt32 ||
188189
p.ExecutionCosts.StepPrice.Denom().Int64() < 0 ||
189190
p.ExecutionCosts.StepPrice.Denom().Int64() > math.MaxUint32 {
190-
return nil
191+
return nil, errors.New("invalid step price rational number values")
191192
}
192193
// #nosec G115
193194
return &cardano.PParams{
@@ -242,7 +243,7 @@ func (p *BabbageProtocolParameters) Utxorpc() *cardano.PParams {
242243
Memory: p.MaxBlockExUnits.Memory,
243244
Steps: p.MaxBlockExUnits.Steps,
244245
},
245-
}
246+
}, nil
246247
}
247248

248249
func UpgradePParams(

0 commit comments

Comments
 (0)