@@ -16,6 +16,7 @@ package alonzo
16
16
17
17
import (
18
18
"encoding/json"
19
+ "fmt"
19
20
"io"
20
21
"math/big"
21
22
"os"
@@ -29,7 +30,7 @@ type AlonzoGenesis struct {
29
30
ExecutionPrices AlonzoGenesisExecutionPrices `json:"executionPrices"`
30
31
MaxTxExUnits AlonzoGenesisExUnits `json:"maxTxExUnits"`
31
32
MaxBlockExUnits AlonzoGenesisExUnits `json:"maxBlockExUnits"`
32
- CostModels map [string ]map [ string ] int `json:"costModels"`
33
+ CostModels map [string ]interface {} `json:"costModels"`
33
34
}
34
35
35
36
func NewAlonzoGenesisFromReader (r io.Reader ) (AlonzoGenesis , error ) {
@@ -39,6 +40,9 @@ func NewAlonzoGenesisFromReader(r io.Reader) (AlonzoGenesis, error) {
39
40
if err := dec .Decode (& ret ); err != nil {
40
41
return ret , err
41
42
}
43
+ if err := ret .NormalizeCostModels (); err != nil {
44
+ return ret , err
45
+ }
42
46
return ret , nil
43
47
}
44
48
@@ -76,3 +80,42 @@ func (r *AlonzoGenesisExecutionPricesRat) UnmarshalJSON(data []byte) error {
76
80
r .Rat = big .NewRat (tmpData .Numerator , tmpData .Denominator )
77
81
return nil
78
82
}
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
+ }
0 commit comments