Skip to content

fix: sort cost model keys to determine index #1037

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2025
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
33 changes: 15 additions & 18 deletions ledger/alonzo/pparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ package alonzo

import (
"fmt"
"maps"
"math"
"strconv"
"slices"

"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/ledger/common"
Expand Down Expand Up @@ -193,25 +194,21 @@ func (p *AlonzoProtocolParameters) UpdateFromGenesis(
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)
}
}
// The sort order of the keys in map form corresponds to the index in list form
paramKeys := slices.Sorted(maps.Keys(model))
if len(paramKeys) != expectedCount {
return fmt.Errorf(
"incorrect param count for %s: %d",
versionStr,
len(paramKeys),
)
}

// 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,
)
}
// Copy values from map format into list format
values := make([]int64, expectedCount)
for index, paramName := range paramKeys {
val := model[paramName]
values[index] = int64(val)
}

p.CostModels[key] = values
Expand Down
4 changes: 2 additions & 2 deletions ledger/alonzo/pparams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func TestScientificNotationInCostModels(t *testing.T) {
}
// Fill remaining parameters
for i := 3; i < 166; i++ {
costModel[strconv.Itoa(i)] = i * 1000
costModel[`test`+strconv.Itoa(i)] = i * 1000
}

genesisJSON := fmt.Sprintf(`{
Expand Down Expand Up @@ -287,7 +287,7 @@ func TestInvalidCostModelFormats(t *testing.T) {
costModels: `"costModels": {
"PlutusV1": {"0":1, "1":2, "2":3}
}`,
expectError: "missing parameter at index 3 for PlutusV1",
expectError: "incorrect param count for PlutusV1: 3",
},
}

Expand Down
Loading