Skip to content

Commit 87808a1

Browse files
authored
Merge pull request #196 from cloudstruct/feat/networks
feat: allow specifying pre-defined networks
2 parents 11d2bf7 + 95aea93 commit 87808a1

File tree

6 files changed

+105
-66
lines changed

6 files changed

+105
-66
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ $ make
6969
Run the test program pointing to the UNIX socket (via `socat`) from the `cardano-node` instance started above.
7070

7171
```
72-
$ ./go-ouroboros-network -address localhost:8082 -testnet ...
72+
$ ./go-ouroboros-network -address localhost:8082 -network testnet ...
7373
```
7474

7575
Run it against the public port in node-to-node mode.
7676

7777
```
78-
$ ./go-ouroboros-network -address localhost:8081 -ntn -testnet ...
78+
$ ./go-ouroboros-network -address localhost:8081 -ntn -network testnet ...
7979
```
8080

8181
Test chain-sync (works in node-to-node and node-to-client modes).
@@ -93,7 +93,7 @@ $ ./go-ouroboros-network ... local-tx-submission ...
9393
Test following the chain tip in the `preview` network.
9494

9595
```
96-
$ ./go-ouroboros-network -preview -address preview-node.world.dev.cardano.org:30002 -ntn chain-sync -tip
96+
$ ./go-ouroboros-network -network preview -address preview-node.world.dev.cardano.org:30002 -ntn chain-sync -tip
9797
```
9898

9999
### Stopping the local `cardano-node` instance

cmd/common/cmdline.go

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,8 @@ import (
44
"flag"
55
"fmt"
66
"os"
7-
)
87

9-
const (
10-
TESTNET_MAGIC = 1097911063
11-
MAINNET_MAGIC = 764824073
12-
PREPROD_MAGIC = 1
13-
PREVIEW_MAGIC = 2
8+
"github.com/cloudstruct/go-ouroboros-network"
149
)
1510

1611
type GlobalFlags struct {
@@ -19,11 +14,8 @@ type GlobalFlags struct {
1914
Address string
2015
UseTls bool
2116
NtnProto bool
17+
Network string
2218
NetworkMagic int
23-
Testnet bool
24-
Mainnet bool
25-
Preprod bool
26-
Preview bool
2719
}
2820

2921
func NewGlobalFlags() *GlobalFlags {
@@ -34,11 +26,8 @@ func NewGlobalFlags() *GlobalFlags {
3426
f.Flagset.StringVar(&f.Address, "address", "", "TCP address to connect to in address:port format")
3527
f.Flagset.BoolVar(&f.UseTls, "tls", false, "enable TLS")
3628
f.Flagset.BoolVar(&f.NtnProto, "ntn", false, "use node-to-node protocol (defaults to node-to-client)")
37-
f.Flagset.IntVar(&f.NetworkMagic, "network-magic", 0, "network magic value")
38-
f.Flagset.BoolVar(&f.Testnet, "testnet", false, fmt.Sprintf("alias for -network-magic=%d", TESTNET_MAGIC))
39-
f.Flagset.BoolVar(&f.Mainnet, "mainnet", false, fmt.Sprintf("alias for -network-magic=%d", MAINNET_MAGIC))
40-
f.Flagset.BoolVar(&f.Preprod, "preprod", false, fmt.Sprintf("alias for -network-magic=%d", PREPROD_MAGIC))
41-
f.Flagset.BoolVar(&f.Preview, "preview", false, fmt.Sprintf("alias for -network-magic=%d", PREVIEW_MAGIC))
29+
f.Flagset.StringVar(&f.Network, "network", "preview", "specifies network that node is participating in")
30+
f.Flagset.IntVar(&f.NetworkMagic, "network-magic", 0, "specifies network magic value. this overrides the -network option")
4231
return f
4332
}
4433

@@ -48,18 +37,11 @@ func (f *GlobalFlags) Parse() {
4837
os.Exit(1)
4938
}
5039
if f.NetworkMagic == 0 {
51-
if f.Testnet {
52-
f.NetworkMagic = TESTNET_MAGIC
53-
} else if f.Mainnet {
54-
f.NetworkMagic = MAINNET_MAGIC
55-
} else if f.Preprod {
56-
f.NetworkMagic = PREPROD_MAGIC
57-
} else if f.Preview {
58-
f.NetworkMagic = PREVIEW_MAGIC
59-
} else {
60-
fmt.Printf("You must specify one of -testnet, -mainnet, -preprod, -preview, or -network-magic\n\n")
61-
f.Flagset.PrintDefaults()
40+
network := ouroboros.NetworkByName(f.Network)
41+
if network == ouroboros.NetworkInvalid {
42+
fmt.Printf("Invalid network specified: %s\n", f.Network)
6243
os.Exit(1)
6344
}
45+
f.NetworkMagic = int(network.NetworkMagic)
6446
}
6547
}

cmd/go-ouroboros-network/chainsync.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ func newChainSyncFlags() *chainSyncFlags {
3838
}
3939

4040
// Intersect points (last block of previous era) for each era on testnet/mainnet
41-
var eraIntersect = map[int]map[string][]interface{}{
42-
TESTNET_MAGIC: map[string][]interface{}{
41+
var eraIntersect = map[string]map[string][]interface{}{
42+
"unknown": map[string][]interface{}{
43+
"genesis": []interface{}{},
44+
},
45+
"testnet": map[string][]interface{}{
4346
"genesis": []interface{}{},
4447
"byron": []interface{}{},
4548
// Last block of epoch 73 (Byron era)
@@ -53,7 +56,7 @@ var eraIntersect = map[int]map[string][]interface{}{
5356
// Last block of epoch 214 (Alonzo era)
5457
"babbage": []interface{}{62510369, "d931221f9bc4cae34de422d9f4281a2b0344e86aac6b31eb54e2ee90f44a09b9"},
5558
},
56-
MAINNET_MAGIC: map[string][]interface{}{
59+
"mainnet": map[string][]interface{}{
5760
"genesis": []interface{}{},
5861
"byron": []interface{}{},
5962
// Last block of epoch 207 (Byron era)
@@ -66,11 +69,11 @@ var eraIntersect = map[int]map[string][]interface{}{
6669
"alonzo": []interface{}{39916796, "e72579ff89dc9ed325b723a33624b596c08141c7bd573ecfff56a1f7229e4d09"},
6770
// TODO: add Babbage starting point after mainnet hard fork
6871
},
69-
PREPROD_MAGIC: map[string][]interface{}{
72+
"preprod": map[string][]interface{}{
7073
"genesis": []interface{}{},
7174
"alonzo": []interface{}{},
7275
},
73-
PREVIEW_MAGIC: map[string][]interface{}{
76+
"preview": map[string][]interface{}{
7477
"genesis": []interface{}{},
7578
"alonzo": []interface{}{},
7679
// Last block of epoch 3 (Alonzo era)
@@ -102,9 +105,19 @@ func testChainSync(f *globalFlags) {
102105
os.Exit(1)
103106
}
104107

105-
if _, ok := eraIntersect[f.networkMagic][chainSyncFlags.startEra]; !ok {
106-
fmt.Printf("ERROR: unknown era '%s' specified as chain-sync start point\n", chainSyncFlags.startEra)
107-
os.Exit(1)
108+
var intersectPoint []interface{}
109+
if _, ok := eraIntersect[f.network]; !ok {
110+
if chainSyncFlags.startEra != "genesis" {
111+
fmt.Printf("ERROR: only 'genesis' is supported for -start-era for unknown networks\n")
112+
os.Exit(1)
113+
}
114+
intersectPoint = eraIntersect["unknown"]["genesis"]
115+
} else {
116+
if _, ok := eraIntersect[f.network][chainSyncFlags.startEra]; !ok {
117+
fmt.Printf("ERROR: unknown era '%s' specified as chain-sync start point\n", chainSyncFlags.startEra)
118+
os.Exit(1)
119+
}
120+
intersectPoint = eraIntersect[f.network][chainSyncFlags.startEra]
108121
}
109122

110123
conn := createClientConnection(f)
@@ -144,11 +157,11 @@ func testChainSync(f *globalFlags) {
144157
os.Exit(1)
145158
}
146159
point = tip.Point
147-
} else if len(eraIntersect[f.networkMagic][chainSyncFlags.startEra]) > 0 {
160+
} else if len(intersectPoint) > 0 {
148161
// Slot
149-
slot := uint64(eraIntersect[f.networkMagic][chainSyncFlags.startEra][0].(int))
162+
slot := uint64(intersectPoint[0].(int))
150163
// Block hash
151-
hash, _ := hex.DecodeString(eraIntersect[f.networkMagic][chainSyncFlags.startEra][1].(string))
164+
hash, _ := hex.DecodeString(intersectPoint[1].(string))
152165
point = common.NewPoint(slot, hash)
153166
} else {
154167
point = common.NewPointOrigin()

cmd/go-ouroboros-network/main.go

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,8 @@ import (
66
"fmt"
77
"net"
88
"os"
9-
)
109

11-
const (
12-
TESTNET_MAGIC = 1097911063
13-
MAINNET_MAGIC = 764824073
14-
PREPROD_MAGIC = 1
15-
PREVIEW_MAGIC = 2
10+
"github.com/cloudstruct/go-ouroboros-network"
1611
)
1712

1813
type globalFlags struct {
@@ -21,11 +16,8 @@ type globalFlags struct {
2116
address string
2217
useTls bool
2318
ntnProto bool
19+
network string
2420
networkMagic int
25-
testnet bool
26-
mainnet bool
27-
preprod bool
28-
preview bool
2921
}
3022

3123
func newGlobalFlags() *globalFlags {
@@ -36,11 +28,8 @@ func newGlobalFlags() *globalFlags {
3628
f.flagset.StringVar(&f.address, "address", "", "TCP address to connect to in address:port format")
3729
f.flagset.BoolVar(&f.useTls, "tls", false, "enable TLS")
3830
f.flagset.BoolVar(&f.ntnProto, "ntn", false, "use node-to-node protocol (defaults to node-to-client)")
39-
f.flagset.IntVar(&f.networkMagic, "network-magic", 0, "network magic value")
40-
f.flagset.BoolVar(&f.testnet, "testnet", false, fmt.Sprintf("alias for -network-magic=%d", TESTNET_MAGIC))
41-
f.flagset.BoolVar(&f.mainnet, "mainnet", false, fmt.Sprintf("alias for -network-magic=%d", MAINNET_MAGIC))
42-
f.flagset.BoolVar(&f.preprod, "preprod", false, fmt.Sprintf("alias for -network-magic=%d", PREPROD_MAGIC))
43-
f.flagset.BoolVar(&f.preview, "preview", false, fmt.Sprintf("alias for -network-magic=%d", PREVIEW_MAGIC))
31+
f.flagset.StringVar(&f.network, "network", "preview", "specifies network that node is participating in")
32+
f.flagset.IntVar(&f.networkMagic, "network-magic", 0, "specifies network magic value. this overrides the -network option")
4433
return f
4534
}
4635

@@ -53,19 +42,12 @@ func main() {
5342
}
5443

5544
if f.networkMagic == 0 {
56-
if f.testnet {
57-
f.networkMagic = TESTNET_MAGIC
58-
} else if f.mainnet {
59-
f.networkMagic = MAINNET_MAGIC
60-
} else if f.preprod {
61-
f.networkMagic = PREPROD_MAGIC
62-
} else if f.preview {
63-
f.networkMagic = PREVIEW_MAGIC
64-
} else {
65-
fmt.Printf("You must specify one of -testnet, -mainnet, -preprod, -preview, or -network-magic\n\n")
66-
flag.PrintDefaults()
45+
network := ouroboros.NetworkByName(f.network)
46+
if network == ouroboros.NetworkInvalid {
47+
fmt.Printf("Invalid network specified: %s\n", f.network)
6748
os.Exit(1)
6849
}
50+
f.networkMagic = int(network.NetworkMagic)
6951
}
7052

7153
if len(f.flagset.Args()) > 0 {

networks.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package ouroboros
2+
3+
// Network definitions
4+
var (
5+
NetworkTestnet = Network{Id: 0, Name: "testnet", NetworkMagic: 1097911063}
6+
NetworkMainnet = Network{Id: 1, Name: "mainnet", NetworkMagic: 764824073}
7+
NetworkPreprod = Network{Id: 2, Name: "preprod", NetworkMagic: 1}
8+
NetworkPreview = Network{Id: 3, Name: "preview", NetworkMagic: 2}
9+
10+
NetworkInvalid = Network{Id: 0, Name: "invalid", NetworkMagic: 0} // NetworkInvalid is used as a return value for lookup functions when a network isn't found
11+
)
12+
13+
// List of valid networks for use in lookup functions
14+
var networks = []Network{NetworkTestnet, NetworkMainnet, NetworkPreprod, NetworkPreview}
15+
16+
// NetworkByName returns a predefined network by name
17+
func NetworkByName(name string) Network {
18+
for _, network := range networks {
19+
if network.Name == name {
20+
return network
21+
}
22+
}
23+
return NetworkInvalid
24+
}
25+
26+
// NetworkById returns a predefined network by ID
27+
func NetworkById(id uint) Network {
28+
for _, network := range networks {
29+
if network.Id == id {
30+
return network
31+
}
32+
}
33+
return NetworkInvalid
34+
}
35+
36+
// NetworkByNetworkMagic returns a predefined network by network magic
37+
func NetworkByNetworkMagic(networkMagic uint32) Network {
38+
for _, network := range networks {
39+
if network.NetworkMagic == networkMagic {
40+
return network
41+
}
42+
}
43+
return NetworkInvalid
44+
}
45+
46+
// Network represents a Cardano network
47+
type Network struct {
48+
Id uint
49+
Name string
50+
NetworkMagic uint32
51+
}
52+
53+
func (n Network) String() string {
54+
return n.Name
55+
}

options.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ func WithConnection(conn net.Conn) OuroborosOptionFunc {
2121
}
2222
}
2323

24+
// WithNetwork specifies the network
25+
func WithNetwork(network Network) OuroborosOptionFunc {
26+
return func(o *Ouroboros) {
27+
o.networkMagic = network.NetworkMagic
28+
}
29+
}
30+
2431
// WithNetworkMagic specifies the network magic value
2532
func WithNetworkMagic(networkMagic uint32) OuroborosOptionFunc {
2633
return func(o *Ouroboros) {

0 commit comments

Comments
 (0)