Skip to content

Commit c1adc83

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

File tree

2 files changed

+91
-3
lines changed

2 files changed

+91
-3
lines changed

ledger/alonzo/pparams.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package alonzo
1616

1717
import (
1818
"math"
19+
"sort"
1920

2021
"github.com/blinklabs-io/gouroboros/cbor"
2122
"github.com/blinklabs-io/gouroboros/ledger/common"
@@ -94,9 +95,38 @@ func (p *AlonzoProtocolParameters) UpdateFromGenesis(genesis *AlonzoGenesis) {
9495
StepPrice: &cbor.Rat{Rat: genesis.ExecutionPrices.Steps.Rat},
9596
}
9697
}
97-
// TODO: cost models (#852)
98-
// We have 150+ string values to map to array indexes
99-
// CostModels map[string]map[string]int
98+
99+
// Process cost models
100+
if genesis.CostModels != nil {
101+
p.CostModels = make(map[uint][]int64)
102+
for lang, model := range genesis.CostModels {
103+
var langKey uint
104+
switch lang {
105+
case "plutus:v1":
106+
langKey = 0
107+
case "plutus:v2":
108+
langKey = 1
109+
default:
110+
// Skip unknown language
111+
continue
112+
}
113+
114+
// Get sorted keys to determine indexes
115+
keys := make([]string, 0, len(model))
116+
for k := range model {
117+
keys = append(keys, k)
118+
}
119+
sort.Strings(keys)
120+
121+
// Create slice with values in alphabetical order
122+
costs := make([]int64, len(keys))
123+
for i, key := range keys {
124+
costs[i] = int64(model[key])
125+
}
126+
127+
p.CostModels[langKey] = costs
128+
}
129+
}
100130
}
101131

102132
type AlonzoProtocolParameterUpdate struct {

ledger/alonzo/pparams_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,64 @@ func TestAlonzoProtocolParamsUpdateFromGenesis(t *testing.T) {
163163
AdaPerUtxoByte: 34482 / 8,
164164
},
165165
},
166+
{
167+
startParams: alonzo.AlonzoProtocolParameters{},
168+
genesisJson: `{
169+
"lovelacePerUTxOWord": 34482,
170+
"costModels": {
171+
"plutus:v1": {
172+
"addInteger-cpu-arguments-intercept": 10,
173+
"subtractInteger-cpu-arguments-intercept": 30,
174+
"multiplyInteger-cpu-arguments-intercept": 20
175+
},
176+
"plutus:v2": {
177+
"appendByteString-cpu-arguments-slope": 5,
178+
"consByteString-cpu-arguments-intercept": 2
179+
},
180+
"plutus:v3": {
181+
"unknown-op": 999
182+
}
183+
}
184+
}`,
185+
expectedParams: alonzo.AlonzoProtocolParameters{
186+
AdaPerUtxoByte: 34482 / 8,
187+
CostModels: map[uint][]int64{
188+
0: {
189+
10, // addInteger-cpu-arguments-intercept
190+
20, // multiplyInteger-cpu-arguments-intercept
191+
30, // subtractInteger-cpu-arguments-intercept
192+
},
193+
1: {
194+
5, // appendByteString-cpu-arguments-slope
195+
2, // consByteString-cpu-arguments-intercept
196+
},
197+
},
198+
},
199+
},
200+
{
201+
//out of order keys
202+
startParams: alonzo.AlonzoProtocolParameters{},
203+
genesisJson: `{
204+
"lovelacePerUTxOWord": 34482,
205+
"costModels": {
206+
"plutus:v1": {
207+
"zzz": 3,
208+
"aaa": 1,
209+
"mmm": 2
210+
}
211+
}
212+
}`,
213+
expectedParams: alonzo.AlonzoProtocolParameters{
214+
AdaPerUtxoByte: 34482 / 8,
215+
CostModels: map[uint][]int64{
216+
0: {
217+
1, // "aaa"
218+
2, // "mmm"
219+
3, // "zzz"
220+
},
221+
},
222+
},
223+
},
166224
}
167225
for _, testDef := range testDefs {
168226
tmpGenesis, err := alonzo.NewAlonzoGenesisFromReader(

0 commit comments

Comments
 (0)