Skip to content

Commit b417ab5

Browse files
author
Jenita
committed
feat: changes to support cost models while updating Alonzon protocols
Signed-off-by: Jenita <[email protected]>
1 parent 32afc58 commit b417ab5

File tree

1 file changed

+44
-36
lines changed

1 file changed

+44
-36
lines changed

ledger/alonzo/pparams.go

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -328,69 +328,77 @@ func (p *AlonzoProtocolParameters) Utxorpc() *cardano.PParams {
328328
return nil
329329
}
330330

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 {
333355
if rat == nil || rat.Rat == nil {
334356
return nil
335357
}
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 {
341361
return nil
342362
}
343-
if denom <= 0 || denom > math.MaxUint32 {
363+
denom := uint32(denom64)
364+
if !numOk {
344365
return nil
345366
}
346-
347367
return &cardano.RationalNumber{
348-
Numerator: int32(num),
349-
Denominator: uint32(denom),
368+
Numerator: num,
369+
Denominator: denom,
350370
}
351371
}
352372

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)
359378

360-
// Return nil if any conversion failed
361379
if a0 == nil || rho == nil || tau == nil || memPrice == nil || stepPrice == nil {
362380
return nil
363381
}
364382

365-
// Convert cost models with proper version handling
383+
// Convert cost models
366384
costModels := &cardano.CostModels{}
367385
if p.CostModels != nil {
368-
// Initialize all Plutus versions to empty models first
369386
costModels.PlutusV1 = &cardano.CostModel{Values: []int64{}}
370387
costModels.PlutusV2 = &cardano.CostModel{Values: []int64{}}
371388
costModels.PlutusV3 = &cardano.CostModel{Values: []int64{}}
372389

373-
// Convert each version that exists in our parameters
374390
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+
376396
switch version {
377397
case PlutusV1:
378-
values = make([]int64, len(model.Order))
379-
for i, name := range model.Order {
380-
values[i] = model.Parameters[name]
381-
}
382398
costModels.PlutusV1.Values = values
383399
case PlutusV2:
384-
values = make([]int64, len(model.Order))
385-
for i, name := range model.Order {
386-
values[i] = model.Parameters[name]
387-
}
388400
costModels.PlutusV2.Values = values
389401
case PlutusV3:
390-
values = make([]int64, len(model.Order))
391-
for i, name := range model.Order {
392-
values[i] = model.Parameters[name]
393-
}
394402
costModels.PlutusV3.Values = values
395403
}
396404
}
@@ -412,8 +420,8 @@ func (p *AlonzoProtocolParameters) Utxorpc() *cardano.PParams {
412420
TreasuryExpansion: tau,
413421
MinPoolCost: p.MinPoolCost,
414422
ProtocolVersion: &cardano.ProtocolVersion{
415-
Major: uint32(p.ProtocolMajor),
416-
Minor: uint32(p.ProtocolMinor),
423+
Major: protocolMajor,
424+
Minor: protocolMinor,
417425
},
418426
MaxValueSize: uint64(p.MaxValueSize),
419427
CollateralPercentage: uint64(p.CollateralPercentage),

0 commit comments

Comments
 (0)