Skip to content
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

feat: genesis config helper functions #725

Merged
merged 1 commit into from
Sep 27, 2024
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
25 changes: 25 additions & 0 deletions ledger/alonzo/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@

package alonzo

import (
"encoding/json"
"io"
"os"
)

type AlonzoGenesis struct {
LovelacePerUtxoWord uint64 `json:"lovelacePerUTxOWord"`
MaxValueSize int
Expand All @@ -24,3 +30,22 @@ type AlonzoGenesis struct {
MaxBlockExUnits map[string]int
CostModels map[string]map[string]int
}

func NewAlonzoGenesisFromReader(r io.Reader) (AlonzoGenesis, error) {
var ret AlonzoGenesis
dec := json.NewDecoder(r)
dec.DisallowUnknownFields()
if err := dec.Decode(&ret); err != nil {
return ret, err
}
return ret, nil
}

func NewAlonzoGenesisFromFile(path string) (AlonzoGenesis, error) {
f, err := os.Open(path)
if err != nil {
return AlonzoGenesis{}, err
}
defer f.Close()
return NewAlonzoGenesisFromReader(f)
}
6 changes: 3 additions & 3 deletions ledger/alonzo/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
package alonzo_test

import (
"encoding/json"
"reflect"
"strings"
"testing"

"github.com/blinklabs-io/gouroboros/ledger/alonzo"
Expand Down Expand Up @@ -411,8 +411,8 @@ var expectedGenesisObj = alonzo.AlonzoGenesis{
}

func TestGenesisFromJson(t *testing.T) {
var tmpGenesis alonzo.AlonzoGenesis
if err := json.Unmarshal([]byte(alonzoGenesisConfig), &tmpGenesis); err != nil {
tmpGenesis, err := alonzo.NewAlonzoGenesisFromReader(strings.NewReader(alonzoGenesisConfig))
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if !reflect.DeepEqual(tmpGenesis, expectedGenesisObj) {
Expand Down
25 changes: 25 additions & 0 deletions ledger/byron/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@

package byron

import (
"encoding/json"
"io"
"os"
)

type ByronGenesis struct {
AvvmDistr map[string]string
BlockVersionData ByronGenesisBlockVersionData
Expand Down Expand Up @@ -74,3 +80,22 @@ type ByronGenesisVssCert struct {
SigningKey string
VssKey string
}

func NewByronGenesisFromReader(r io.Reader) (ByronGenesis, error) {
var ret ByronGenesis
dec := json.NewDecoder(r)
dec.DisallowUnknownFields()
if err := dec.Decode(&ret); err != nil {
return ret, err
}
return ret, nil
}

func NewByronGenesisFromFile(path string) (ByronGenesis, error) {
f, err := os.Open(path)
if err != nil {
return ByronGenesis{}, err
}
defer f.Close()
return NewByronGenesisFromReader(f)
}
6 changes: 3 additions & 3 deletions ledger/byron/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
package byron_test

import (
"encoding/json"
"reflect"
"strings"
"testing"

"github.com/blinklabs-io/gouroboros/ledger/byron"
Expand Down Expand Up @@ -236,8 +236,8 @@ var expectedGenesisObj = byron.ByronGenesis{
}

func TestGenesisFromJson(t *testing.T) {
var tmpGenesis byron.ByronGenesis
if err := json.Unmarshal([]byte(byronGenesisConfig), &tmpGenesis); err != nil {
tmpGenesis, err := byron.NewByronGenesisFromReader(strings.NewReader(byronGenesisConfig))
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if !reflect.DeepEqual(tmpGenesis, expectedGenesisObj) {
Expand Down
25 changes: 25 additions & 0 deletions ledger/conway/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@

package conway

import (
"encoding/json"
"io"
"os"
)

type ConwayGenesis struct {
PoolVotingThresholds ConwayGenesisPoolVotingThresholds
DRepVotingThresholds ConwayGenesisDRepVotingThresholds
Expand Down Expand Up @@ -64,3 +70,22 @@ type ConwayGenesisCommittee struct {
Members map[string]int
Threshold map[string]int
}

func NewConwayGenesisFromReader(r io.Reader) (ConwayGenesis, error) {
var ret ConwayGenesis
dec := json.NewDecoder(r)
dec.DisallowUnknownFields()
if err := dec.Decode(&ret); err != nil {
return ret, err
}
return ret, nil
}

func NewConwayGenesisFromFile(path string) (ConwayGenesis, error) {
f, err := os.Open(path)
if err != nil {
return ConwayGenesis{}, err
}
defer f.Close()
return NewConwayGenesisFromReader(f)
}
6 changes: 3 additions & 3 deletions ledger/conway/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
package conway_test

import (
"encoding/json"
"reflect"
"strings"
"testing"

"github.com/blinklabs-io/gouroboros/ledger/conway"
Expand Down Expand Up @@ -383,8 +383,8 @@ var expectedGenesisObj = conway.ConwayGenesis{
}

func TestGenesisFromJson(t *testing.T) {
var tmpGenesis conway.ConwayGenesis
if err := json.Unmarshal([]byte(conwayGenesisConfig), &tmpGenesis); err != nil {
tmpGenesis, err := conway.NewConwayGenesisFromReader(strings.NewReader(conwayGenesisConfig))
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if !reflect.DeepEqual(tmpGenesis, expectedGenesisObj) {
Expand Down
26 changes: 25 additions & 1 deletion ledger/shelley/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@

package shelley

import "time"
import (
"encoding/json"
"io"
"os"
"time"
)

type ShelleyGenesis struct {
SystemStart time.Time `json:"systemStart"`
Expand Down Expand Up @@ -56,3 +61,22 @@ type ShelleyGenesisProtocolParams struct {
MinUtxoValue uint `json:"minUTxOValue"`
MinPoolCost uint
}

func NewShelleyGenesisFromReader(r io.Reader) (ShelleyGenesis, error) {
var ret ShelleyGenesis
dec := json.NewDecoder(r)
dec.DisallowUnknownFields()
if err := dec.Decode(&ret); err != nil {
return ret, err
}
return ret, nil
}

func NewShelleyGenesisFromFile(path string) (ShelleyGenesis, error) {
f, err := os.Open(path)
if err != nil {
return ShelleyGenesis{}, err
}
defer f.Close()
return NewShelleyGenesisFromReader(f)
}
6 changes: 3 additions & 3 deletions ledger/shelley/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
package shelley_test

import (
"encoding/json"
"reflect"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -167,8 +167,8 @@ var expectedGenesisObj = shelley.ShelleyGenesis{
}

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