Skip to content

Commit 3d70e4d

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

File tree

5 files changed

+576
-397
lines changed

5 files changed

+576
-397
lines changed

ledger/alonzo/genesis.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package alonzo
1616

1717
import (
1818
"encoding/json"
19+
"fmt"
1920
"io"
2021
"math/big"
2122
"os"
@@ -29,7 +30,7 @@ type AlonzoGenesis struct {
2930
ExecutionPrices AlonzoGenesisExecutionPrices `json:"executionPrices"`
3031
MaxTxExUnits AlonzoGenesisExUnits `json:"maxTxExUnits"`
3132
MaxBlockExUnits AlonzoGenesisExUnits `json:"maxBlockExUnits"`
32-
CostModels map[string]map[string]int `json:"costModels"`
33+
CostModels map[string]interface{} `json:"costModels"`
3334
}
3435

3536
func NewAlonzoGenesisFromReader(r io.Reader) (AlonzoGenesis, error) {
@@ -39,6 +40,9 @@ func NewAlonzoGenesisFromReader(r io.Reader) (AlonzoGenesis, error) {
3940
if err := dec.Decode(&ret); err != nil {
4041
return ret, err
4142
}
43+
if err := ret.NormalizeCostModels(); err != nil {
44+
return ret, err
45+
}
4246
return ret, nil
4347
}
4448

@@ -76,3 +80,42 @@ func (r *AlonzoGenesisExecutionPricesRat) UnmarshalJSON(data []byte) error {
7680
r.Rat = big.NewRat(tmpData.Numerator, tmpData.Denominator)
7781
return nil
7882
}
83+
84+
func (a *AlonzoGenesis) NormalizeCostModels() error {
85+
if a.CostModels == nil {
86+
return nil
87+
}
88+
89+
normalized := make(map[string]map[string]int)
90+
for version, model := range a.CostModels {
91+
if modelMap, ok := model.(map[string]interface{}); ok {
92+
versionMap := make(map[string]int)
93+
for k, v := range modelMap {
94+
switch val := v.(type) {
95+
case float64:
96+
versionMap[k] = int(val)
97+
case int:
98+
versionMap[k] = val
99+
case json.Number:
100+
intVal, err := val.Int64()
101+
if err != nil {
102+
floatVal, err := val.Float64()
103+
if err != nil {
104+
return fmt.Errorf("invalid number in cost model: %v", val)
105+
}
106+
intVal = int64(floatVal)
107+
}
108+
versionMap[k] = int(intVal)
109+
default:
110+
return fmt.Errorf("invalid cost model value type: %T", v)
111+
}
112+
}
113+
normalized[version] = versionMap
114+
}
115+
}
116+
a.CostModels = make(map[string]interface{})
117+
for k, v := range normalized {
118+
a.CostModels[k] = v
119+
}
120+
return nil
121+
}

ledger/alonzo/genesis_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ var expectedGenesisObj = alonzo.AlonzoGenesis{
243243
Mem: 50000000,
244244
Steps: 40000000000,
245245
},
246-
CostModels: map[string]map[string]int{
247-
"PlutusV1": {
246+
CostModels: map[string]interface{}{
247+
"PlutusV1": map[string]int{
248248
"addInteger-cpu-arguments-intercept": 197209,
249249
"addInteger-cpu-arguments-slope": 0,
250250
"addInteger-memory-arguments-intercept": 1,
@@ -484,8 +484,8 @@ func TestNewAlonzoGenesisFromReader(t *testing.T) {
484484
t.Logf("prMem is correct: %v", result.ExecutionPrices.Mem.Rat)
485485
}
486486

487-
expectedCostModels := map[string]map[string]int{
488-
"PlutusV1": {
487+
expectedCostModels := map[string]interface{}{
488+
"PlutusV1": map[string]int{
489489
"addInteger-cpu-arguments-intercept": 205665,
490490
"addInteger-cpu-arguments-slope": 812,
491491
},

0 commit comments

Comments
 (0)