Skip to content

Commit 519c705

Browse files
authored
refactor: merge genesis big.Rat types (#1125)
This adds the functionality from alonzo.AlonzoGenesisExecutionPricesRat to common.Rat and removes the former type Fixes #1124 Signed-off-by: Aurora Gaffney <[email protected]>
1 parent ed6f6f9 commit 519c705

File tree

4 files changed

+69
-21
lines changed

4 files changed

+69
-21
lines changed

ledger/alonzo/genesis.go

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ import (
1919
"fmt"
2020
"io"
2121
"math"
22-
"math/big"
2322
"os"
2423
"strconv"
24+
25+
"github.com/blinklabs-io/gouroboros/ledger/common"
2526
)
2627

2728
type AlonzoGenesis struct {
@@ -156,22 +157,6 @@ type AlonzoGenesisExUnits struct {
156157
}
157158

158159
type AlonzoGenesisExecutionPrices struct {
159-
Steps *AlonzoGenesisExecutionPricesRat `json:"prSteps"`
160-
Mem *AlonzoGenesisExecutionPricesRat `json:"prMem"`
161-
}
162-
163-
type AlonzoGenesisExecutionPricesRat struct {
164-
*big.Rat
165-
}
166-
167-
func (r *AlonzoGenesisExecutionPricesRat) UnmarshalJSON(data []byte) error {
168-
var tmpData struct {
169-
Numerator int64 `json:"numerator"`
170-
Denominator int64 `json:"denominator"`
171-
}
172-
if err := json.Unmarshal(data, &tmpData); err != nil {
173-
return err
174-
}
175-
r.Rat = big.NewRat(tmpData.Numerator, tmpData.Denominator)
176-
return nil
160+
Steps *common.GenesisRat `json:"prSteps"`
161+
Mem *common.GenesisRat `json:"prMem"`
177162
}

ledger/alonzo/genesis_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"testing"
2222

2323
"github.com/blinklabs-io/gouroboros/ledger/alonzo"
24+
"github.com/blinklabs-io/gouroboros/ledger/common"
2425
)
2526

2627
const alonzoGenesisConfig = `
@@ -228,10 +229,10 @@ var expectedGenesisObj = alonzo.AlonzoGenesis{
228229
CollateralPercentage: 150,
229230
MaxCollateralInputs: 3,
230231
ExecutionPrices: alonzo.AlonzoGenesisExecutionPrices{
231-
Mem: &alonzo.AlonzoGenesisExecutionPricesRat{
232+
Mem: &common.GenesisRat{
232233
Rat: big.NewRat(577, 10000),
233234
},
234-
Steps: &alonzo.AlonzoGenesisExecutionPricesRat{
235+
Steps: &common.GenesisRat{
235236
Rat: big.NewRat(721, 10000000),
236237
},
237238
},

ledger/common/genesis.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package common
1616

1717
import (
18+
"encoding/json"
1819
"fmt"
1920
"math/big"
2021
)
@@ -25,6 +26,16 @@ type GenesisRat struct {
2526
}
2627

2728
func (r *GenesisRat) UnmarshalJSON(data []byte) error {
29+
// Try as ratio
30+
var tmpData struct {
31+
Numerator int64 `json:"numerator"`
32+
Denominator int64 `json:"denominator"`
33+
}
34+
if err := json.Unmarshal(data, &tmpData); err == nil {
35+
r.Rat = big.NewRat(tmpData.Numerator, tmpData.Denominator)
36+
return nil
37+
}
38+
// Try as decimal value
2839
r.Rat = new(big.Rat)
2940
if _, ok := r.SetString(string(data)); !ok {
3041
return fmt.Errorf("math/big: cannot unmarshal %q into a *big.Rat", data)

ledger/common/genesis_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2025 Blink Labs Software
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package common_test
16+
17+
import (
18+
"encoding/json"
19+
"math/big"
20+
"testing"
21+
22+
"github.com/blinklabs-io/gouroboros/ledger/common"
23+
)
24+
25+
func TestGenesisRatNumDenom(t *testing.T) {
26+
jsonData := `{"testRat": { "numerator": 721, "denominator": 10000 }}`
27+
expectedRat := big.NewRat(721, 10000)
28+
var testData struct {
29+
TestRat common.GenesisRat `json:"testRat"`
30+
}
31+
if err := json.Unmarshal([]byte(jsonData), &testData); err != nil {
32+
t.Fatalf("unexpected error: %s", err)
33+
}
34+
if testData.TestRat.Cmp(expectedRat) != 0 {
35+
t.Errorf("did not get expected value: got %s, wanted %s", testData.TestRat.String(), expectedRat.String())
36+
}
37+
}
38+
39+
func TestGenesisRatFloat(t *testing.T) {
40+
jsonData := `{"testRat": 0.0721}`
41+
expectedRat := big.NewRat(721, 10000)
42+
var testData struct {
43+
TestRat common.GenesisRat `json:"testRat"`
44+
}
45+
if err := json.Unmarshal([]byte(jsonData), &testData); err != nil {
46+
t.Fatalf("unexpected error: %s", err)
47+
}
48+
if testData.TestRat.Cmp(expectedRat) != 0 {
49+
t.Errorf("did not get expected value: got %s, wanted %s", testData.TestRat.String(), expectedRat.String())
50+
}
51+
}

0 commit comments

Comments
 (0)