Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions ledger/alonzo/pparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package alonzo

import (
"math"
"sort"

"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/ledger/common"
Expand Down Expand Up @@ -94,9 +95,38 @@ func (p *AlonzoProtocolParameters) UpdateFromGenesis(genesis *AlonzoGenesis) {
StepPrice: &cbor.Rat{Rat: genesis.ExecutionPrices.Steps.Rat},
}
}
// TODO: cost models (#852)
// We have 150+ string values to map to array indexes
// CostModels map[string]map[string]int

// Process cost models
if genesis.CostModels != nil {
p.CostModels = make(map[uint][]int64)
for lang, model := range genesis.CostModels {
var langKey uint
switch lang {
case "plutus:v1":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is PlutusV1 in the JSON

langKey = 0
case "plutus:v2":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is PlutusV2 in the JSON

langKey = 1
default:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also possible to have a PlutusV3

// Skip unknown language
continue
}

// Get sorted keys to determine indexes
keys := make([]string, 0, len(model))
for k := range model {
keys = append(keys, k)
}
sort.Strings(keys)

// Create slice with values in alphabetical order
costs := make([]int64, len(keys))
for i, key := range keys {
costs[i] = int64(model[key])
}

p.CostModels[langKey] = costs
}
}
}

type AlonzoProtocolParameterUpdate struct {
Expand Down
58 changes: 58 additions & 0 deletions ledger/alonzo/pparams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,64 @@ func TestAlonzoProtocolParamsUpdateFromGenesis(t *testing.T) {
AdaPerUtxoByte: 34482 / 8,
},
},
{
startParams: alonzo.AlonzoProtocolParameters{},
genesisJson: `{
"lovelacePerUTxOWord": 34482,
"costModels": {
"plutus:v1": {
"addInteger-cpu-arguments-intercept": 10,
"subtractInteger-cpu-arguments-intercept": 30,
"multiplyInteger-cpu-arguments-intercept": 20
},
"plutus:v2": {
"appendByteString-cpu-arguments-slope": 5,
"consByteString-cpu-arguments-intercept": 2
},
"plutus:v3": {
"unknown-op": 999
}
}
}`,
expectedParams: alonzo.AlonzoProtocolParameters{
AdaPerUtxoByte: 34482 / 8,
CostModels: map[uint][]int64{
0: {
10, // addInteger-cpu-arguments-intercept
20, // multiplyInteger-cpu-arguments-intercept
30, // subtractInteger-cpu-arguments-intercept
},
1: {
5, // appendByteString-cpu-arguments-slope
2, // consByteString-cpu-arguments-intercept
},
},
},
},
{
//out of order keys
startParams: alonzo.AlonzoProtocolParameters{},
genesisJson: `{
"lovelacePerUTxOWord": 34482,
"costModels": {
"plutus:v1": {
"zzz": 3,
"aaa": 1,
"mmm": 2
}
}
}`,
expectedParams: alonzo.AlonzoProtocolParameters{
AdaPerUtxoByte: 34482 / 8,
CostModels: map[uint][]int64{
0: {
1, // "aaa"
2, // "mmm"
3, // "zzz"
},
},
},
},
}
for _, testDef := range testDefs {
tmpGenesis, err := alonzo.NewAlonzoGenesisFromReader(
Expand Down
Loading