Skip to content

Commit aa8c86e

Browse files
authored
feat: genesis config helper functions (#725)
1 parent 5308b44 commit aa8c86e

File tree

8 files changed

+112
-13
lines changed

8 files changed

+112
-13
lines changed

ledger/alonzo/genesis.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414

1515
package alonzo
1616

17+
import (
18+
"encoding/json"
19+
"io"
20+
"os"
21+
)
22+
1723
type AlonzoGenesis struct {
1824
LovelacePerUtxoWord uint64 `json:"lovelacePerUTxOWord"`
1925
MaxValueSize int
@@ -24,3 +30,22 @@ type AlonzoGenesis struct {
2430
MaxBlockExUnits map[string]int
2531
CostModels map[string]map[string]int
2632
}
33+
34+
func NewAlonzoGenesisFromReader(r io.Reader) (AlonzoGenesis, error) {
35+
var ret AlonzoGenesis
36+
dec := json.NewDecoder(r)
37+
dec.DisallowUnknownFields()
38+
if err := dec.Decode(&ret); err != nil {
39+
return ret, err
40+
}
41+
return ret, nil
42+
}
43+
44+
func NewAlonzoGenesisFromFile(path string) (AlonzoGenesis, error) {
45+
f, err := os.Open(path)
46+
if err != nil {
47+
return AlonzoGenesis{}, err
48+
}
49+
defer f.Close()
50+
return NewAlonzoGenesisFromReader(f)
51+
}

ledger/alonzo/genesis_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
package alonzo_test
1616

1717
import (
18-
"encoding/json"
1918
"reflect"
19+
"strings"
2020
"testing"
2121

2222
"github.com/blinklabs-io/gouroboros/ledger/alonzo"
@@ -411,8 +411,8 @@ var expectedGenesisObj = alonzo.AlonzoGenesis{
411411
}
412412

413413
func TestGenesisFromJson(t *testing.T) {
414-
var tmpGenesis alonzo.AlonzoGenesis
415-
if err := json.Unmarshal([]byte(alonzoGenesisConfig), &tmpGenesis); err != nil {
414+
tmpGenesis, err := alonzo.NewAlonzoGenesisFromReader(strings.NewReader(alonzoGenesisConfig))
415+
if err != nil {
416416
t.Fatalf("unexpected error: %s", err)
417417
}
418418
if !reflect.DeepEqual(tmpGenesis, expectedGenesisObj) {

ledger/byron/genesis.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414

1515
package byron
1616

17+
import (
18+
"encoding/json"
19+
"io"
20+
"os"
21+
)
22+
1723
type ByronGenesis struct {
1824
AvvmDistr map[string]string
1925
BlockVersionData ByronGenesisBlockVersionData
@@ -74,3 +80,22 @@ type ByronGenesisVssCert struct {
7480
SigningKey string
7581
VssKey string
7682
}
83+
84+
func NewByronGenesisFromReader(r io.Reader) (ByronGenesis, error) {
85+
var ret ByronGenesis
86+
dec := json.NewDecoder(r)
87+
dec.DisallowUnknownFields()
88+
if err := dec.Decode(&ret); err != nil {
89+
return ret, err
90+
}
91+
return ret, nil
92+
}
93+
94+
func NewByronGenesisFromFile(path string) (ByronGenesis, error) {
95+
f, err := os.Open(path)
96+
if err != nil {
97+
return ByronGenesis{}, err
98+
}
99+
defer f.Close()
100+
return NewByronGenesisFromReader(f)
101+
}

ledger/byron/genesis_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
package byron_test
1616

1717
import (
18-
"encoding/json"
1918
"reflect"
19+
"strings"
2020
"testing"
2121

2222
"github.com/blinklabs-io/gouroboros/ledger/byron"
@@ -236,8 +236,8 @@ var expectedGenesisObj = byron.ByronGenesis{
236236
}
237237

238238
func TestGenesisFromJson(t *testing.T) {
239-
var tmpGenesis byron.ByronGenesis
240-
if err := json.Unmarshal([]byte(byronGenesisConfig), &tmpGenesis); err != nil {
239+
tmpGenesis, err := byron.NewByronGenesisFromReader(strings.NewReader(byronGenesisConfig))
240+
if err != nil {
241241
t.Fatalf("unexpected error: %s", err)
242242
}
243243
if !reflect.DeepEqual(tmpGenesis, expectedGenesisObj) {

ledger/conway/genesis.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414

1515
package conway
1616

17+
import (
18+
"encoding/json"
19+
"io"
20+
"os"
21+
)
22+
1723
type ConwayGenesis struct {
1824
PoolVotingThresholds ConwayGenesisPoolVotingThresholds
1925
DRepVotingThresholds ConwayGenesisDRepVotingThresholds
@@ -64,3 +70,22 @@ type ConwayGenesisCommittee struct {
6470
Members map[string]int
6571
Threshold map[string]int
6672
}
73+
74+
func NewConwayGenesisFromReader(r io.Reader) (ConwayGenesis, error) {
75+
var ret ConwayGenesis
76+
dec := json.NewDecoder(r)
77+
dec.DisallowUnknownFields()
78+
if err := dec.Decode(&ret); err != nil {
79+
return ret, err
80+
}
81+
return ret, nil
82+
}
83+
84+
func NewConwayGenesisFromFile(path string) (ConwayGenesis, error) {
85+
f, err := os.Open(path)
86+
if err != nil {
87+
return ConwayGenesis{}, err
88+
}
89+
defer f.Close()
90+
return NewConwayGenesisFromReader(f)
91+
}

ledger/conway/genesis_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
package conway_test
1616

1717
import (
18-
"encoding/json"
1918
"reflect"
19+
"strings"
2020
"testing"
2121

2222
"github.com/blinklabs-io/gouroboros/ledger/conway"
@@ -383,8 +383,8 @@ var expectedGenesisObj = conway.ConwayGenesis{
383383
}
384384

385385
func TestGenesisFromJson(t *testing.T) {
386-
var tmpGenesis conway.ConwayGenesis
387-
if err := json.Unmarshal([]byte(conwayGenesisConfig), &tmpGenesis); err != nil {
386+
tmpGenesis, err := conway.NewConwayGenesisFromReader(strings.NewReader(conwayGenesisConfig))
387+
if err != nil {
388388
t.Fatalf("unexpected error: %s", err)
389389
}
390390
if !reflect.DeepEqual(tmpGenesis, expectedGenesisObj) {

ledger/shelley/genesis.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414

1515
package shelley
1616

17-
import "time"
17+
import (
18+
"encoding/json"
19+
"io"
20+
"os"
21+
"time"
22+
)
1823

1924
type ShelleyGenesis struct {
2025
SystemStart time.Time `json:"systemStart"`
@@ -56,3 +61,22 @@ type ShelleyGenesisProtocolParams struct {
5661
MinUtxoValue uint `json:"minUTxOValue"`
5762
MinPoolCost uint
5863
}
64+
65+
func NewShelleyGenesisFromReader(r io.Reader) (ShelleyGenesis, error) {
66+
var ret ShelleyGenesis
67+
dec := json.NewDecoder(r)
68+
dec.DisallowUnknownFields()
69+
if err := dec.Decode(&ret); err != nil {
70+
return ret, err
71+
}
72+
return ret, nil
73+
}
74+
75+
func NewShelleyGenesisFromFile(path string) (ShelleyGenesis, error) {
76+
f, err := os.Open(path)
77+
if err != nil {
78+
return ShelleyGenesis{}, err
79+
}
80+
defer f.Close()
81+
return NewShelleyGenesisFromReader(f)
82+
}

ledger/shelley/genesis_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
package shelley_test
1616

1717
import (
18-
"encoding/json"
1918
"reflect"
19+
"strings"
2020
"testing"
2121
"time"
2222

@@ -167,8 +167,8 @@ var expectedGenesisObj = shelley.ShelleyGenesis{
167167
}
168168

169169
func TestGenesisFromJson(t *testing.T) {
170-
var tmpGenesis shelley.ShelleyGenesis
171-
if err := json.Unmarshal([]byte(shelleyGenesisConfig), &tmpGenesis); err != nil {
170+
tmpGenesis, err := shelley.NewShelleyGenesisFromReader(strings.NewReader(shelleyGenesisConfig))
171+
if err != nil {
172172
t.Fatalf("unexpected error: %s", err)
173173
}
174174
if !reflect.DeepEqual(tmpGenesis, expectedGenesisObj) {

0 commit comments

Comments
 (0)