Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
101 changes: 100 additions & 1 deletion ledger/alonzo/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ package alonzo

import (
"encoding/json"
"fmt"
"io"
"math"
"math/big"
"os"
"strconv"
)

type AlonzoGenesis struct {
Expand All @@ -29,7 +32,100 @@ type AlonzoGenesis struct {
ExecutionPrices AlonzoGenesisExecutionPrices `json:"executionPrices"`
MaxTxExUnits AlonzoGenesisExUnits `json:"maxTxExUnits"`
MaxBlockExUnits AlonzoGenesisExUnits `json:"maxBlockExUnits"`
CostModels map[string]map[string]int `json:"costModels"`
CostModels map[string]CostModel `json:"costModels"`
}

type CostModel map[string]int

// NormalizeCostModels converts all cost model keys to consistent paramX format
func (g *AlonzoGenesis) NormalizeCostModels() error {
if g.CostModels == nil {
return nil
}

for version, model := range g.CostModels {
normalized := make(CostModel)
for k, v := range model {
// Check if key is already in paramX format
var index int
if _, err := fmt.Sscanf(k, "param%d", &index); err == nil {
normalized[k] = v // Keep existing paramX keys
continue
}

// Check if key is a numeric index (from array format)
if _, err := fmt.Sscanf(k, "%d", &index); err == nil {
normalized[fmt.Sprintf("param%d", index)] = v
continue
}
normalized[k] = v
}
g.CostModels[version] = normalized
}
return nil
}

func (c *CostModel) UnmarshalJSON(data []byte) error {
tmpMap := make(map[string]interface{})
if err := json.Unmarshal(data, &tmpMap); err != nil {
// Try to unmarshal as array first
var tmpArray []interface{}
if arrayErr := json.Unmarshal(data, &tmpArray); arrayErr == nil {
*c = make(CostModel)
for i, v := range tmpArray {
num, err := toInt(v)
if err != nil {
return fmt.Errorf("array index %d: %w", i, err)
}
(*c)[strconv.Itoa(i)] = num
}
return nil
}
return err
}

*c = make(CostModel)
for k, v := range tmpMap {
num, err := toInt(v)
if err != nil {
return fmt.Errorf("key %s: %w", k, err)
}
(*c)[k] = num
}
return nil
}

func toInt(v interface{}) (int, error) {
switch val := v.(type) {
case float64:
if val > float64(math.MaxInt) || val < float64(math.MinInt) {
return 0, fmt.Errorf("float64 value %v overflows int", val)
}
return int(val), nil
case int:
return val, nil
case json.Number:
intVal, err := val.Int64()
if err != nil {
return 0, err
}
if intVal > math.MaxInt || intVal < math.MinInt {
return 0, fmt.Errorf("json.Number value %v overflows int", val)
}
return int(intVal), nil
case int64:
if val > math.MaxInt || val < math.MinInt {
return 0, fmt.Errorf("int64 value %v overflows int", val)
}
return int(val), nil
case uint64:
if val > math.MaxInt {
return 0, fmt.Errorf("uint64 value %v overflows int", val)
}
return int(val), nil
default:
return 0, fmt.Errorf("unsupported numeric type: %T", v)
}
}

func NewAlonzoGenesisFromReader(r io.Reader) (AlonzoGenesis, error) {
Expand All @@ -39,6 +135,9 @@ func NewAlonzoGenesisFromReader(r io.Reader) (AlonzoGenesis, error) {
if err := dec.Decode(&ret); err != nil {
return ret, err
}
if err := ret.NormalizeCostModels(); err != nil {
return ret, err
}
return ret, nil
}

Expand Down
8 changes: 4 additions & 4 deletions ledger/alonzo/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ var expectedGenesisObj = alonzo.AlonzoGenesis{
Mem: 50000000,
Steps: 40000000000,
},
CostModels: map[string]map[string]int{
"PlutusV1": {
CostModels: map[string]alonzo.CostModel{
"PlutusV1": map[string]int{
"addInteger-cpu-arguments-intercept": 197209,
"addInteger-cpu-arguments-slope": 0,
"addInteger-memory-arguments-intercept": 1,
Expand Down Expand Up @@ -484,8 +484,8 @@ func TestNewAlonzoGenesisFromReader(t *testing.T) {
t.Logf("prMem is correct: %v", result.ExecutionPrices.Mem.Rat)
}

expectedCostModels := map[string]map[string]int{
"PlutusV1": {
expectedCostModels := map[string]alonzo.CostModel{
"PlutusV1": map[string]int{
"addInteger-cpu-arguments-intercept": 205665,
"addInteger-cpu-arguments-slope": 812,
},
Expand Down
76 changes: 71 additions & 5 deletions ledger/alonzo/pparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,30 @@
package alonzo

import (
"fmt"
"math"
"strconv"

"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/ledger/common"
"github.com/blinklabs-io/gouroboros/ledger/mary"
cardano "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
)

// Constants for Plutus version mapping
const (
PlutusV1Key uint = 0
PlutusV2Key uint = 1
PlutusV3Key uint = 2
)

// Expected parameter counts for validation
var plutusParamCounts = map[uint]int{
PlutusV1Key: 166,
PlutusV2Key: 175,
PlutusV3Key: 187,
}

type AlonzoProtocolParameters struct {
cbor.StructAsArray
MinFeeA uint
Expand Down Expand Up @@ -134,10 +150,12 @@ func (p *AlonzoProtocolParameters) Update(
}
}

func (p *AlonzoProtocolParameters) UpdateFromGenesis(genesis *AlonzoGenesis) {
func (p *AlonzoProtocolParameters) UpdateFromGenesis(genesis *AlonzoGenesis) error {
if genesis == nil {
return
return nil
}

// Common parameter updates
p.AdaPerUtxoByte = genesis.LovelacePerUtxoWord / 8
p.MaxValueSize = genesis.MaxValueSize
p.CollateralPercentage = genesis.CollateralPercentage
Expand All @@ -150,16 +168,64 @@ func (p *AlonzoProtocolParameters) UpdateFromGenesis(genesis *AlonzoGenesis) {
Memory: uint64(genesis.MaxBlockExUnits.Mem),
Steps: uint64(genesis.MaxBlockExUnits.Steps),
}

if genesis.ExecutionPrices.Mem != nil &&
genesis.ExecutionPrices.Steps != nil {
p.ExecutionCosts = common.ExUnitPrice{
MemPrice: &cbor.Rat{Rat: genesis.ExecutionPrices.Mem.Rat},
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

if genesis.CostModels != nil {
p.CostModels = make(map[uint][]int64)

for versionStr, model := range genesis.CostModels {
key, ok := plutusVersionToKey(versionStr)
if !ok {
continue
}

expectedCount, ok := plutusParamCounts[key]
if !ok {
continue
}

values := make([]int64, expectedCount)

for paramName, val := range model {
if index, err := strconv.Atoi(paramName); err == nil {
if index >= 0 && index < expectedCount {
values[index] = int64(val)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this only ever contain float64 values? It looks like anything else would be ignored

}
}

// Verify we have all expected parameters
for i, val := range values {
if val == 0 {
return fmt.Errorf("missing parameter at index %d for %s", i, versionStr)
}
}

p.CostModels[key] = values
}
}
return nil
}

// Helper to convert Plutus version string to key
func plutusVersionToKey(version string) (uint, bool) {
switch version {
case "PlutusV1":
return PlutusV1Key, true
case "PlutusV2":
return PlutusV2Key, true
case "PlutusV3":
return PlutusV3Key, true
default:
return 0, false
}
}

type AlonzoProtocolParameterUpdate struct {
Expand Down
Loading
Loading