@@ -328,69 +328,77 @@ func (p *AlonzoProtocolParameters) Utxorpc() *cardano.PParams {
328
328
return nil
329
329
}
330
330
331
- // Helper function to safely convert rational numbers with bounds checking
332
- safeRatConvert := func (rat * cbor.Rat ) * cardano.RationalNumber {
331
+ // Safe conversion helper functions
332
+ safeInt64ToInt32 := func (val int64 ) (int32 , bool ) {
333
+ if val < math .MinInt32 || val > math .MaxInt32 {
334
+ return 0 , false
335
+ }
336
+ return int32 (val ), true
337
+ }
338
+
339
+ safeUintToUint32 := func (val uint ) (uint32 , bool ) {
340
+ if val > math .MaxUint32 {
341
+ return 0 , false
342
+ }
343
+ return uint32 (val ), true
344
+ }
345
+
346
+ // Convert protocol version
347
+ protocolMajor , ok1 := safeUintToUint32 (p .ProtocolMajor )
348
+ protocolMinor , ok2 := safeUintToUint32 (p .ProtocolMinor )
349
+ if ! ok1 || ! ok2 {
350
+ return nil
351
+ }
352
+
353
+ // Convert rational numbers
354
+ convertRat := func (rat * cbor.Rat ) * cardano.RationalNumber {
333
355
if rat == nil || rat .Rat == nil {
334
356
return nil
335
357
}
336
- num := rat .Num ().Int64 ()
337
- denom := rat .Denom ().Int64 ()
338
-
339
- // Check bounds for int32 numerator and uint32 denominator
340
- if num < math .MinInt32 || num > math .MaxInt32 {
358
+ num , numOk := safeInt64ToInt32 (rat .Num ().Int64 ())
359
+ denom64 := rat .Denom ().Int64 ()
360
+ if denom64 <= 0 || denom64 > math .MaxUint32 {
341
361
return nil
342
362
}
343
- if denom <= 0 || denom > math .MaxUint32 {
363
+ denom := uint32 (denom64 )
364
+ if ! numOk {
344
365
return nil
345
366
}
346
-
347
367
return & cardano.RationalNumber {
348
- Numerator : int32 ( num ) ,
349
- Denominator : uint32 ( denom ) ,
368
+ Numerator : num ,
369
+ Denominator : denom ,
350
370
}
351
371
}
352
372
353
- // Convert all rational numbers with safety checks
354
- a0 := safeRatConvert (p .A0 )
355
- rho := safeRatConvert (p .Rho )
356
- tau := safeRatConvert (p .Tau )
357
- memPrice := safeRatConvert (p .ExecutionCosts .MemPrice )
358
- stepPrice := safeRatConvert (p .ExecutionCosts .StepPrice )
373
+ a0 := convertRat (p .A0 )
374
+ rho := convertRat (p .Rho )
375
+ tau := convertRat (p .Tau )
376
+ memPrice := convertRat (p .ExecutionCosts .MemPrice )
377
+ stepPrice := convertRat (p .ExecutionCosts .StepPrice )
359
378
360
- // Return nil if any conversion failed
361
379
if a0 == nil || rho == nil || tau == nil || memPrice == nil || stepPrice == nil {
362
380
return nil
363
381
}
364
382
365
- // Convert cost models with proper version handling
383
+ // Convert cost models
366
384
costModels := & cardano.CostModels {}
367
385
if p .CostModels != nil {
368
- // Initialize all Plutus versions to empty models first
369
386
costModels .PlutusV1 = & cardano.CostModel {Values : []int64 {}}
370
387
costModels .PlutusV2 = & cardano.CostModel {Values : []int64 {}}
371
388
costModels .PlutusV3 = & cardano.CostModel {Values : []int64 {}}
372
389
373
- // Convert each version that exists in our parameters
374
390
for version , model := range p .CostModels {
375
- var values []int64
391
+ values := make ([]int64 , len (model .Order ))
392
+ for i , name := range model .Order {
393
+ values [i ] = model .Parameters [name ]
394
+ }
395
+
376
396
switch version {
377
397
case PlutusV1 :
378
- values = make ([]int64 , len (model .Order ))
379
- for i , name := range model .Order {
380
- values [i ] = model .Parameters [name ]
381
- }
382
398
costModels .PlutusV1 .Values = values
383
399
case PlutusV2 :
384
- values = make ([]int64 , len (model .Order ))
385
- for i , name := range model .Order {
386
- values [i ] = model .Parameters [name ]
387
- }
388
400
costModels .PlutusV2 .Values = values
389
401
case PlutusV3 :
390
- values = make ([]int64 , len (model .Order ))
391
- for i , name := range model .Order {
392
- values [i ] = model .Parameters [name ]
393
- }
394
402
costModels .PlutusV3 .Values = values
395
403
}
396
404
}
@@ -412,8 +420,8 @@ func (p *AlonzoProtocolParameters) Utxorpc() *cardano.PParams {
412
420
TreasuryExpansion : tau ,
413
421
MinPoolCost : p .MinPoolCost ,
414
422
ProtocolVersion : & cardano.ProtocolVersion {
415
- Major : uint32 ( p . ProtocolMajor ) ,
416
- Minor : uint32 ( p . ProtocolMinor ) ,
423
+ Major : protocolMajor ,
424
+ Minor : protocolMinor ,
417
425
},
418
426
MaxValueSize : uint64 (p .MaxValueSize ),
419
427
CollateralPercentage : uint64 (p .CollateralPercentage ),
0 commit comments