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
23 changes: 4 additions & 19 deletions ledger/alonzo/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import (
"fmt"
"io"
"math"
"math/big"
"os"
"strconv"

"github.com/blinklabs-io/gouroboros/ledger/common"
)

type AlonzoGenesis struct {
Expand Down Expand Up @@ -156,22 +157,6 @@ type AlonzoGenesisExUnits struct {
}

type AlonzoGenesisExecutionPrices struct {
Steps *AlonzoGenesisExecutionPricesRat `json:"prSteps"`
Mem *AlonzoGenesisExecutionPricesRat `json:"prMem"`
}

type AlonzoGenesisExecutionPricesRat struct {
*big.Rat
}

func (r *AlonzoGenesisExecutionPricesRat) UnmarshalJSON(data []byte) error {
var tmpData struct {
Numerator int64 `json:"numerator"`
Denominator int64 `json:"denominator"`
}
if err := json.Unmarshal(data, &tmpData); err != nil {
return err
}
r.Rat = big.NewRat(tmpData.Numerator, tmpData.Denominator)
return nil
Steps *common.GenesisRat `json:"prSteps"`
Mem *common.GenesisRat `json:"prMem"`
}
5 changes: 3 additions & 2 deletions ledger/alonzo/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"github.com/blinklabs-io/gouroboros/ledger/alonzo"
"github.com/blinklabs-io/gouroboros/ledger/common"
)

const alonzoGenesisConfig = `
Expand Down Expand Up @@ -228,10 +229,10 @@ var expectedGenesisObj = alonzo.AlonzoGenesis{
CollateralPercentage: 150,
MaxCollateralInputs: 3,
ExecutionPrices: alonzo.AlonzoGenesisExecutionPrices{
Mem: &alonzo.AlonzoGenesisExecutionPricesRat{
Mem: &common.GenesisRat{
Rat: big.NewRat(577, 10000),
},
Steps: &alonzo.AlonzoGenesisExecutionPricesRat{
Steps: &common.GenesisRat{
Rat: big.NewRat(721, 10000000),
},
},
Expand Down
11 changes: 11 additions & 0 deletions ledger/common/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package common

import (
"encoding/json"
"fmt"
"math/big"
)
Expand All @@ -25,6 +26,16 @@ type GenesisRat struct {
}

func (r *GenesisRat) UnmarshalJSON(data []byte) error {
// Try as ratio
var tmpData struct {
Numerator int64 `json:"numerator"`
Denominator int64 `json:"denominator"`
}
if err := json.Unmarshal(data, &tmpData); err == nil {
r.Rat = big.NewRat(tmpData.Numerator, tmpData.Denominator)
return nil
}
// Try as decimal value
r.Rat = new(big.Rat)
if _, ok := r.SetString(string(data)); !ok {
return fmt.Errorf("math/big: cannot unmarshal %q into a *big.Rat", data)
Expand Down
51 changes: 51 additions & 0 deletions ledger/common/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2025 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package common_test

import (
"encoding/json"
"math/big"
"testing"

"github.com/blinklabs-io/gouroboros/ledger/common"
)

func TestGenesisRatNumDenom(t *testing.T) {
jsonData := `{"testRat": { "numerator": 721, "denominator": 10000 }}`
expectedRat := big.NewRat(721, 10000)
var testData struct {
TestRat common.GenesisRat `json:"testRat"`
}
if err := json.Unmarshal([]byte(jsonData), &testData); err != nil {
t.Fatalf("unexpected error: %s", err)
}
if testData.TestRat.Cmp(expectedRat) != 0 {
t.Errorf("did not get expected value: got %s, wanted %s", testData.TestRat.String(), expectedRat.String())
}
}

func TestGenesisRatFloat(t *testing.T) {
jsonData := `{"testRat": 0.0721}`
expectedRat := big.NewRat(721, 10000)
var testData struct {
TestRat common.GenesisRat `json:"testRat"`
}
if err := json.Unmarshal([]byte(jsonData), &testData); err != nil {
t.Fatalf("unexpected error: %s", err)
}
if testData.TestRat.Cmp(expectedRat) != 0 {
t.Errorf("did not get expected value: got %s, wanted %s", testData.TestRat.String(), expectedRat.String())
}
}