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: support for parsing a cardano-node topology config #418

Merged
merged 1 commit into from
Nov 7, 2023
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
74 changes: 74 additions & 0 deletions topology.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2023 Blink Labs, LLC.
//
// 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 ouroboros

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

// TopologyConfig represents a Cardano node topology config
type TopologyConfig struct {
Producers []TopologyConfigLegacyProducer `json:"Producers"`
LocalRoots []TopologyConfigP2PLocalRoot `json:"localRoots"`
PublicRoots []TopologyConfigP2PPublicRoot `json:"publicRoots"`
UseLedgerAfterSlot uint64 `json:"useLedgerAfterSlot"`
}

type TopologyConfigLegacyProducer struct {
Address string `json:"addr"`
Port uint16 `json:"port"`
Valency uint `json:"valency"`
Continent string `json:"continent"`
State string `json:"state"`
}

type TopologyConfigP2PAccessPoint struct {
Address string `json:"address"`
Port uint16 `json:"port"`
}

type TopologyConfigP2PLocalRoot struct {
AccessPoints []TopologyConfigP2PAccessPoint `json:"accessPoints"`
Advertise bool `json:"advertise"`
Valency uint `json:"valency"`
}

type TopologyConfigP2PPublicRoot struct {
AccessPoints []TopologyConfigP2PAccessPoint `json:"accessPoints"`
Advertise bool `json:"advertise"`
Valency uint `json:"valency"`
}

func NewTopologyConfigFromFile(path string) (*TopologyConfig, error) {
dataFile, err := os.Open(path)
if err != nil {
return nil, err
}
return NewTopologyConfigFromReader(dataFile)
}

func NewTopologyConfigFromReader(r io.Reader) (*TopologyConfig, error) {
t := &TopologyConfig{}
data, err := io.ReadAll(r)
if err != nil {
return nil, err
}
if err := json.Unmarshal(data, t); err != nil {
return nil, err
}
return t, nil
}
135 changes: 135 additions & 0 deletions topology_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Copyright 2023 Blink Labs, LLC.
//
// 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 ouroboros_test

import (
"reflect"
"strings"
"testing"

ouroboros "github.com/blinklabs-io/gouroboros"
)

type topologyTestDefinition struct {
jsonData string
expectedObject *ouroboros.TopologyConfig
}

var topologyTests = []topologyTestDefinition{
{
jsonData: `
{
"Producers": [
{
"addr": "relays-new.cardano-mainnet.iohk.io",
"port": 3001,
"valency": 2
}
]
}
`,
expectedObject: &ouroboros.TopologyConfig{
Producers: []ouroboros.TopologyConfigLegacyProducer{
{
Address: "relays-new.cardano-mainnet.iohk.io",
Port: 3001,
Valency: 2,
},
},
},
},
{
jsonData: `
{
"localRoots": [
{
"accessPoints": [],
"advertise": false,
"valency": 1
}
],
"publicRoots": [
{
"accessPoints": [
{
"address": "backbone.cardano-mainnet.iohk.io",
"port": 3001
}
],
"advertise": false
},
{
"accessPoints": [
{
"address": "backbone.mainnet.emurgornd.com",
"port": 3001
}
],
"advertise": false
}
],
"useLedgerAfterSlot": 99532743
}
`,
expectedObject: &ouroboros.TopologyConfig{
LocalRoots: []ouroboros.TopologyConfigP2PLocalRoot{
{
AccessPoints: []ouroboros.TopologyConfigP2PAccessPoint{},
Advertise: false,
Valency: 1,
},
},
PublicRoots: []ouroboros.TopologyConfigP2PPublicRoot{
{
AccessPoints: []ouroboros.TopologyConfigP2PAccessPoint{
{
Address: "backbone.cardano-mainnet.iohk.io",
Port: 3001,
},
},
Advertise: false,
},
{
AccessPoints: []ouroboros.TopologyConfigP2PAccessPoint{
{
Address: "backbone.mainnet.emurgornd.com",
Port: 3001,
},
},
Advertise: false,
},
},
UseLedgerAfterSlot: 99532743,
},
},
}

func TestParseTopologyConfig(t *testing.T) {
for _, test := range topologyTests {
topology, err := ouroboros.NewTopologyConfigFromReader(
strings.NewReader(test.jsonData),
)
if err != nil {
t.Fatalf("failed to load TopologyConfig from JSON data: %s", err)
}
if !reflect.DeepEqual(topology, test.expectedObject) {
t.Fatalf(
"did not get expected object\n got:\n %#v\n wanted:\n %#v",
topology,
test.expectedObject,
)
}
}
}
Loading