Skip to content

Commit abd12a4

Browse files
authored
Merge pull request #418 from blinklabs-io/feat/topology-config
feat: support for parsing a cardano-node topology config
2 parents 673cbf6 + 8d652a1 commit abd12a4

File tree

2 files changed

+209
-0
lines changed

2 files changed

+209
-0
lines changed

topology.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2023 Blink Labs, LLC.
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 ouroboros
16+
17+
import (
18+
"encoding/json"
19+
"io"
20+
"os"
21+
)
22+
23+
// TopologyConfig represents a Cardano node topology config
24+
type TopologyConfig struct {
25+
Producers []TopologyConfigLegacyProducer `json:"Producers"`
26+
LocalRoots []TopologyConfigP2PLocalRoot `json:"localRoots"`
27+
PublicRoots []TopologyConfigP2PPublicRoot `json:"publicRoots"`
28+
UseLedgerAfterSlot uint64 `json:"useLedgerAfterSlot"`
29+
}
30+
31+
type TopologyConfigLegacyProducer struct {
32+
Address string `json:"addr"`
33+
Port uint16 `json:"port"`
34+
Valency uint `json:"valency"`
35+
Continent string `json:"continent"`
36+
State string `json:"state"`
37+
}
38+
39+
type TopologyConfigP2PAccessPoint struct {
40+
Address string `json:"address"`
41+
Port uint16 `json:"port"`
42+
}
43+
44+
type TopologyConfigP2PLocalRoot struct {
45+
AccessPoints []TopologyConfigP2PAccessPoint `json:"accessPoints"`
46+
Advertise bool `json:"advertise"`
47+
Valency uint `json:"valency"`
48+
}
49+
50+
type TopologyConfigP2PPublicRoot struct {
51+
AccessPoints []TopologyConfigP2PAccessPoint `json:"accessPoints"`
52+
Advertise bool `json:"advertise"`
53+
Valency uint `json:"valency"`
54+
}
55+
56+
func NewTopologyConfigFromFile(path string) (*TopologyConfig, error) {
57+
dataFile, err := os.Open(path)
58+
if err != nil {
59+
return nil, err
60+
}
61+
return NewTopologyConfigFromReader(dataFile)
62+
}
63+
64+
func NewTopologyConfigFromReader(r io.Reader) (*TopologyConfig, error) {
65+
t := &TopologyConfig{}
66+
data, err := io.ReadAll(r)
67+
if err != nil {
68+
return nil, err
69+
}
70+
if err := json.Unmarshal(data, t); err != nil {
71+
return nil, err
72+
}
73+
return t, nil
74+
}

topology_test.go

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// Copyright 2023 Blink Labs, LLC.
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 ouroboros_test
16+
17+
import (
18+
"reflect"
19+
"strings"
20+
"testing"
21+
22+
ouroboros "github.com/blinklabs-io/gouroboros"
23+
)
24+
25+
type topologyTestDefinition struct {
26+
jsonData string
27+
expectedObject *ouroboros.TopologyConfig
28+
}
29+
30+
var topologyTests = []topologyTestDefinition{
31+
{
32+
jsonData: `
33+
{
34+
"Producers": [
35+
{
36+
"addr": "relays-new.cardano-mainnet.iohk.io",
37+
"port": 3001,
38+
"valency": 2
39+
}
40+
]
41+
}
42+
`,
43+
expectedObject: &ouroboros.TopologyConfig{
44+
Producers: []ouroboros.TopologyConfigLegacyProducer{
45+
{
46+
Address: "relays-new.cardano-mainnet.iohk.io",
47+
Port: 3001,
48+
Valency: 2,
49+
},
50+
},
51+
},
52+
},
53+
{
54+
jsonData: `
55+
{
56+
"localRoots": [
57+
{
58+
"accessPoints": [],
59+
"advertise": false,
60+
"valency": 1
61+
}
62+
],
63+
"publicRoots": [
64+
{
65+
"accessPoints": [
66+
{
67+
"address": "backbone.cardano-mainnet.iohk.io",
68+
"port": 3001
69+
}
70+
],
71+
"advertise": false
72+
},
73+
{
74+
"accessPoints": [
75+
{
76+
"address": "backbone.mainnet.emurgornd.com",
77+
"port": 3001
78+
}
79+
],
80+
"advertise": false
81+
}
82+
],
83+
"useLedgerAfterSlot": 99532743
84+
}
85+
`,
86+
expectedObject: &ouroboros.TopologyConfig{
87+
LocalRoots: []ouroboros.TopologyConfigP2PLocalRoot{
88+
{
89+
AccessPoints: []ouroboros.TopologyConfigP2PAccessPoint{},
90+
Advertise: false,
91+
Valency: 1,
92+
},
93+
},
94+
PublicRoots: []ouroboros.TopologyConfigP2PPublicRoot{
95+
{
96+
AccessPoints: []ouroboros.TopologyConfigP2PAccessPoint{
97+
{
98+
Address: "backbone.cardano-mainnet.iohk.io",
99+
Port: 3001,
100+
},
101+
},
102+
Advertise: false,
103+
},
104+
{
105+
AccessPoints: []ouroboros.TopologyConfigP2PAccessPoint{
106+
{
107+
Address: "backbone.mainnet.emurgornd.com",
108+
Port: 3001,
109+
},
110+
},
111+
Advertise: false,
112+
},
113+
},
114+
UseLedgerAfterSlot: 99532743,
115+
},
116+
},
117+
}
118+
119+
func TestParseTopologyConfig(t *testing.T) {
120+
for _, test := range topologyTests {
121+
topology, err := ouroboros.NewTopologyConfigFromReader(
122+
strings.NewReader(test.jsonData),
123+
)
124+
if err != nil {
125+
t.Fatalf("failed to load TopologyConfig from JSON data: %s", err)
126+
}
127+
if !reflect.DeepEqual(topology, test.expectedObject) {
128+
t.Fatalf(
129+
"did not get expected object\n got:\n %#v\n wanted:\n %#v",
130+
topology,
131+
test.expectedObject,
132+
)
133+
}
134+
}
135+
}

0 commit comments

Comments
 (0)