Skip to content

Commit 65a3bf1

Browse files
authored
feat: multiple bootstrap peers per network (#762)
Fixes #761 BREAKING CHANGE: the prototype for functions for getting a network changed to add a boolean return parameter to signify whether the network is known. The public root address/port fields have also been replaced with a list of bootstrap peers
1 parent ee4c98b commit 65a3bf1

File tree

3 files changed

+69
-53
lines changed

3 files changed

+69
-53
lines changed

cmd/common/cmdline.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"fmt"
2020
"os"
2121

22-
"github.com/blinklabs-io/gouroboros"
22+
ouroboros "github.com/blinklabs-io/gouroboros"
2323
)
2424

2525
type GlobalFlags struct {
@@ -86,9 +86,9 @@ func (f *GlobalFlags) Parse() {
8686
os.Exit(1)
8787
}
8888
if f.NetworkMagic == 0 {
89-
network := ouroboros.NetworkByName(f.Network)
90-
if network == ouroboros.NetworkInvalid {
91-
fmt.Printf("Invalid network specified: %s\n", f.Network)
89+
network, ok := ouroboros.NetworkByName(f.Network)
90+
if !ok {
91+
fmt.Printf("Unknown network specified: %s\n", f.Network)
9292
os.Exit(1)
9393
}
9494
f.NetworkMagic = int(network.NetworkMagic)

cmd/gouroboros/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ func main() {
9191
}
9292

9393
if f.networkMagic == 0 {
94-
network := ouroboros.NetworkByName(f.network)
95-
if network == ouroboros.NetworkInvalid {
96-
fmt.Printf("Invalid network specified: %s\n", f.network)
94+
network, ok := ouroboros.NetworkByName(f.network)
95+
if !ok {
96+
fmt.Printf("Unknown network specified: %s\n", f.network)
9797
os.Exit(1)
9898
}
9999
f.networkMagic = int(network.NetworkMagic)

networks.go

Lines changed: 62 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,93 +18,109 @@ import "github.com/blinklabs-io/gouroboros/ledger/common"
1818

1919
// Network definitions
2020
var (
21-
NetworkTestnet = Network{
22-
Id: common.AddressNetworkTestnet,
23-
Name: "testnet",
24-
NetworkMagic: 1097911063,
25-
}
2621
NetworkMainnet = Network{
27-
Id: common.AddressNetworkMainnet,
28-
Name: "mainnet",
29-
NetworkMagic: 764824073,
30-
PublicRootAddress: "backbone.cardano.iog.io",
31-
PublicRootPort: 3001,
22+
Id: common.AddressNetworkMainnet,
23+
Name: "mainnet",
24+
NetworkMagic: 764824073,
25+
BootstrapPeers: []NetworkBootstrapPeer{
26+
{
27+
Address: "backbone.cardano.iog.io",
28+
Port: 3001,
29+
},
30+
{
31+
Address: "backbone.mainnet.emurgornd.com",
32+
Port: 3001,
33+
},
34+
{
35+
Address: "backbone.mainnet.cardanofoundation.org",
36+
Port: 3001,
37+
},
38+
},
3239
}
3340
NetworkPreprod = Network{
34-
Id: common.AddressNetworkTestnet,
35-
Name: "preprod",
36-
NetworkMagic: 1,
37-
PublicRootAddress: "preprod-node.world.dev.cardano.org",
38-
PublicRootPort: 30000,
41+
Id: common.AddressNetworkTestnet,
42+
Name: "preprod",
43+
NetworkMagic: 1,
44+
BootstrapPeers: []NetworkBootstrapPeer{
45+
{
46+
Address: "preprod-node.play.dev.cardano.org",
47+
Port: 3001,
48+
},
49+
},
3950
}
4051
NetworkPreview = Network{
41-
Id: common.AddressNetworkTestnet,
42-
Name: "preview",
43-
NetworkMagic: 2,
44-
PublicRootAddress: "preview-node.play.dev.cardano.org",
45-
PublicRootPort: 3001,
52+
Id: common.AddressNetworkTestnet,
53+
Name: "preview",
54+
NetworkMagic: 2,
55+
BootstrapPeers: []NetworkBootstrapPeer{
56+
{
57+
Address: "preview-node.play.dev.cardano.org",
58+
Port: 3001,
59+
},
60+
},
4661
}
4762
NetworkSancho = Network{
48-
Id: common.AddressNetworkTestnet,
49-
Name: "sanchonet",
50-
NetworkMagic: 4,
51-
PublicRootAddress: "sanchonet-node.play.dev.cardano.org",
52-
PublicRootPort: 3001,
63+
Id: common.AddressNetworkTestnet,
64+
Name: "sanchonet",
65+
NetworkMagic: 4,
66+
BootstrapPeers: []NetworkBootstrapPeer{
67+
{
68+
Address: "sanchonet-node.play.dev.cardano.org",
69+
Port: 3001,
70+
},
71+
},
5372
}
54-
55-
NetworkInvalid = Network{
56-
Id: 0,
57-
Name: "invalid",
58-
NetworkMagic: 0,
59-
} // NetworkInvalid is used as a return value for lookup functions when a network isn't found
6073
)
6174

6275
// List of valid networks for use in lookup functions
6376
var networks = []Network{
64-
NetworkTestnet,
6577
NetworkMainnet,
6678
NetworkPreprod,
6779
NetworkPreview,
6880
NetworkSancho,
6981
}
7082

7183
// NetworkByName returns a predefined network by name
72-
func NetworkByName(name string) Network {
84+
func NetworkByName(name string) (Network, bool) {
7385
for _, network := range networks {
7486
if network.Name == name {
75-
return network
87+
return network, true
7688
}
7789
}
78-
return NetworkInvalid
90+
return Network{}, false
7991
}
8092

8193
// NetworkById returns a predefined network by ID
82-
func NetworkById(id uint8) Network {
94+
func NetworkById(id uint8) (Network, bool) {
8395
for _, network := range networks {
8496
if network.Id == id {
85-
return network
97+
return network, true
8698
}
8799
}
88-
return NetworkInvalid
100+
return Network{}, false
89101
}
90102

91103
// NetworkByNetworkMagic returns a predefined network by network magic
92-
func NetworkByNetworkMagic(networkMagic uint32) Network {
104+
func NetworkByNetworkMagic(networkMagic uint32) (Network, bool) {
93105
for _, network := range networks {
94106
if network.NetworkMagic == networkMagic {
95-
return network
107+
return network, true
96108
}
97109
}
98-
return NetworkInvalid
110+
return Network{}, false
99111
}
100112

101113
// Network represents a Cardano network
102114
type Network struct {
103-
Id uint8 // network ID used for addresses
104-
Name string
105-
NetworkMagic uint32
106-
PublicRootAddress string
107-
PublicRootPort uint
115+
Id uint8 // network ID used for addresses
116+
Name string
117+
NetworkMagic uint32
118+
BootstrapPeers []NetworkBootstrapPeer
119+
}
120+
121+
type NetworkBootstrapPeer struct {
122+
Address string
123+
Port uint
108124
}
109125

110126
func (n Network) String() string {

0 commit comments

Comments
 (0)