-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support for loading cardano-node and genesis configs
This commit also includes the upstream cardano-node and genesis config files for the preview network
- Loading branch information
Showing
14 changed files
with
1,030 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright 2024 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 cardano | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"path" | ||
|
||
"github.com/blinklabs-io/gouroboros/ledger/alonzo" | ||
"github.com/blinklabs-io/gouroboros/ledger/byron" | ||
"github.com/blinklabs-io/gouroboros/ledger/conway" | ||
"github.com/blinklabs-io/gouroboros/ledger/shelley" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// CardanoNodeConfig represents the config.json/yaml file used by cardano-node | ||
type CardanoNodeConfig struct { | ||
path string | ||
AlonzoGenesisFile string `yaml:"AlonzoGenesisFile"` | ||
AlonzoGenesisHash string `yaml:"AlonzoGenesisHash"` | ||
ByronGenesisFile string `yaml:"ByronGenesisFile"` | ||
ByronGenesisHash string `yaml:"ByronGenesisHash"` | ||
ConwayGenesisFile string `yaml:"ConwayGenesisFile"` | ||
ConwayGenesisHash string `yaml:"ConwayGenesisHash"` | ||
ShelleyGenesisFile string `yaml:"ShelleyGenesisFile"` | ||
ShelleyGenesisHash string `yaml:"ShelleyGenesisHash"` | ||
// TODO: add more fields from cardano-node config as we need them | ||
} | ||
|
||
func NewCardanoNodeConfigFromReader(r io.Reader) (*CardanoNodeConfig, error) { | ||
var ret CardanoNodeConfig | ||
dec := yaml.NewDecoder(r) | ||
if err := dec.Decode(&ret); err != nil { | ||
return nil, err | ||
} | ||
return &ret, nil | ||
} | ||
|
||
func NewCardanoNodeConfigFromFile(file string) (*CardanoNodeConfig, error) { | ||
f, err := os.Open(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
c, err := NewCardanoNodeConfigFromReader(f) | ||
if err != nil { | ||
return nil, err | ||
} | ||
c.path = path.Dir(file) | ||
return c, nil | ||
} | ||
|
||
// ByronGenesis loads and returns the Byron genesis config specified in the node config | ||
func (c *CardanoNodeConfig) ByronGenesis() (*byron.ByronGenesis, error) { | ||
tmpPath := path.Join(c.path, c.ByronGenesisFile) | ||
// TODO: check genesis file hash | ||
ret, err := byron.NewByronGenesisFromFile(tmpPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &ret, err | ||
} | ||
|
||
// ShelleyGenesis loads and returns the Shelley genesis config specified in the node config | ||
func (c *CardanoNodeConfig) ShelleyGenesis() (*shelley.ShelleyGenesis, error) { | ||
tmpPath := path.Join(c.path, c.ShelleyGenesisFile) | ||
// TODO: check genesis file hash | ||
ret, err := shelley.NewShelleyGenesisFromFile(tmpPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &ret, err | ||
} | ||
|
||
// AlonzoGenesis loads and returns the Alonzo genesis config specified in the node config | ||
func (c *CardanoNodeConfig) AlonzoGenesis() (*alonzo.AlonzoGenesis, error) { | ||
tmpPath := path.Join(c.path, c.AlonzoGenesisFile) | ||
// TODO: check genesis file hash | ||
ret, err := alonzo.NewAlonzoGenesisFromFile(tmpPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &ret, err | ||
} | ||
|
||
// ConwayGenesis loads and returns the Conway genesis config specified in the node config | ||
func (c *CardanoNodeConfig) ConwayGenesis() (*conway.ConwayGenesis, error) { | ||
tmpPath := path.Join(c.path, c.ConwayGenesisFile) | ||
// TODO: check genesis file hash | ||
ret, err := conway.NewConwayGenesisFromFile(tmpPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &ret, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright 2024 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 cardano | ||
|
||
import ( | ||
"path" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
const ( | ||
testDataDir = "testdata" | ||
) | ||
|
||
var expectedCardanoNodeConfig = &CardanoNodeConfig{ | ||
path: testDataDir, | ||
AlonzoGenesisFile: "alonzo-genesis.json", | ||
AlonzoGenesisHash: "7e94a15f55d1e82d10f09203fa1d40f8eede58fd8066542cf6566008068ed874", | ||
ByronGenesisFile: "byron-genesis.json", | ||
ByronGenesisHash: "83de1d7302569ad56cf9139a41e2e11346d4cb4a31c00142557b6ab3fa550761", | ||
ConwayGenesisFile: "conway-genesis.json", | ||
ConwayGenesisHash: "9cc5084f02e27210eacba47af0872e3dba8946ad9460b6072d793e1d2f3987ef", | ||
ShelleyGenesisFile: "shelley-genesis.json", | ||
ShelleyGenesisHash: "363498d1024f84bb39d3fa9593ce391483cb40d479b87233f868d6e57c3a400d", | ||
} | ||
|
||
func TestCardanoNodeConfig(t *testing.T) { | ||
tmpPath := path.Join( | ||
testDataDir, | ||
"config.json", | ||
) | ||
cfg, err := NewCardanoNodeConfigFromFile(tmpPath) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %s", err) | ||
} | ||
if !reflect.DeepEqual(cfg, expectedCardanoNodeConfig) { | ||
t.Fatalf("did not get expected object\n got: %#v\n wanted: %#v\n", cfg, expectedCardanoNodeConfig) | ||
} | ||
t.Run("Byron genesis", func(t *testing.T) { | ||
g, err := cfg.ByronGenesis() | ||
if err != nil { | ||
t.Fatalf("unexpected error: %s", err) | ||
} | ||
if g == nil { | ||
t.Fatalf("got nil instead of ByronGenesis") | ||
} | ||
}) | ||
t.Run("Shelley genesis", func(t *testing.T) { | ||
g, err := cfg.ShelleyGenesis() | ||
if err != nil { | ||
t.Fatalf("unexpected error: %s", err) | ||
} | ||
if g == nil { | ||
t.Fatalf("got nil instead of ShelleyGenesis") | ||
} | ||
}) | ||
t.Run("Alonzo genesis", func(t *testing.T) { | ||
g, err := cfg.AlonzoGenesis() | ||
if err != nil { | ||
t.Fatalf("unexpected error: %s", err) | ||
} | ||
if g == nil { | ||
t.Fatalf("got nil instead of AlonzoGenesis") | ||
} | ||
}) | ||
t.Run("Conway genesis", func(t *testing.T) { | ||
g, err := cfg.ConwayGenesis() | ||
if err != nil { | ||
t.Fatalf("unexpected error: %s", err) | ||
} | ||
if g == nil { | ||
t.Fatalf("got nil instead of ConwayGenesis") | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../configs/cardano/preview |
Oops, something went wrong.