Skip to content

Commit

Permalink
Merge pull request #196 from cloudstruct/feat/networks
Browse files Browse the repository at this point in the history
feat: allow specifying pre-defined networks
  • Loading branch information
agaffney authored Feb 15, 2023
2 parents 11d2bf7 + 95aea93 commit 87808a1
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 66 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ $ make
Run the test program pointing to the UNIX socket (via `socat`) from the `cardano-node` instance started above.

```
$ ./go-ouroboros-network -address localhost:8082 -testnet ...
$ ./go-ouroboros-network -address localhost:8082 -network testnet ...
```

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

```
$ ./go-ouroboros-network -address localhost:8081 -ntn -testnet ...
$ ./go-ouroboros-network -address localhost:8081 -ntn -network testnet ...
```

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

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

### Stopping the local `cardano-node` instance
Expand Down
34 changes: 8 additions & 26 deletions cmd/common/cmdline.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@ import (
"flag"
"fmt"
"os"
)

const (
TESTNET_MAGIC = 1097911063
MAINNET_MAGIC = 764824073
PREPROD_MAGIC = 1
PREVIEW_MAGIC = 2
"github.com/cloudstruct/go-ouroboros-network"
)

type GlobalFlags struct {
Expand All @@ -19,11 +14,8 @@ type GlobalFlags struct {
Address string
UseTls bool
NtnProto bool
Network string
NetworkMagic int
Testnet bool
Mainnet bool
Preprod bool
Preview bool
}

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

Expand All @@ -48,18 +37,11 @@ func (f *GlobalFlags) Parse() {
os.Exit(1)
}
if f.NetworkMagic == 0 {
if f.Testnet {
f.NetworkMagic = TESTNET_MAGIC
} else if f.Mainnet {
f.NetworkMagic = MAINNET_MAGIC
} else if f.Preprod {
f.NetworkMagic = PREPROD_MAGIC
} else if f.Preview {
f.NetworkMagic = PREVIEW_MAGIC
} else {
fmt.Printf("You must specify one of -testnet, -mainnet, -preprod, -preview, or -network-magic\n\n")
f.Flagset.PrintDefaults()
network := ouroboros.NetworkByName(f.Network)
if network == ouroboros.NetworkInvalid {
fmt.Printf("Invalid network specified: %s\n", f.Network)
os.Exit(1)
}
f.NetworkMagic = int(network.NetworkMagic)
}
}
35 changes: 24 additions & 11 deletions cmd/go-ouroboros-network/chainsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ func newChainSyncFlags() *chainSyncFlags {
}

// Intersect points (last block of previous era) for each era on testnet/mainnet
var eraIntersect = map[int]map[string][]interface{}{
TESTNET_MAGIC: map[string][]interface{}{
var eraIntersect = map[string]map[string][]interface{}{
"unknown": map[string][]interface{}{
"genesis": []interface{}{},
},
"testnet": map[string][]interface{}{
"genesis": []interface{}{},
"byron": []interface{}{},
// Last block of epoch 73 (Byron era)
Expand All @@ -53,7 +56,7 @@ var eraIntersect = map[int]map[string][]interface{}{
// Last block of epoch 214 (Alonzo era)
"babbage": []interface{}{62510369, "d931221f9bc4cae34de422d9f4281a2b0344e86aac6b31eb54e2ee90f44a09b9"},
},
MAINNET_MAGIC: map[string][]interface{}{
"mainnet": map[string][]interface{}{
"genesis": []interface{}{},
"byron": []interface{}{},
// Last block of epoch 207 (Byron era)
Expand All @@ -66,11 +69,11 @@ var eraIntersect = map[int]map[string][]interface{}{
"alonzo": []interface{}{39916796, "e72579ff89dc9ed325b723a33624b596c08141c7bd573ecfff56a1f7229e4d09"},
// TODO: add Babbage starting point after mainnet hard fork
},
PREPROD_MAGIC: map[string][]interface{}{
"preprod": map[string][]interface{}{
"genesis": []interface{}{},
"alonzo": []interface{}{},
},
PREVIEW_MAGIC: map[string][]interface{}{
"preview": map[string][]interface{}{
"genesis": []interface{}{},
"alonzo": []interface{}{},
// Last block of epoch 3 (Alonzo era)
Expand Down Expand Up @@ -102,9 +105,19 @@ func testChainSync(f *globalFlags) {
os.Exit(1)
}

if _, ok := eraIntersect[f.networkMagic][chainSyncFlags.startEra]; !ok {
fmt.Printf("ERROR: unknown era '%s' specified as chain-sync start point\n", chainSyncFlags.startEra)
os.Exit(1)
var intersectPoint []interface{}
if _, ok := eraIntersect[f.network]; !ok {
if chainSyncFlags.startEra != "genesis" {
fmt.Printf("ERROR: only 'genesis' is supported for -start-era for unknown networks\n")
os.Exit(1)
}
intersectPoint = eraIntersect["unknown"]["genesis"]
} else {
if _, ok := eraIntersect[f.network][chainSyncFlags.startEra]; !ok {
fmt.Printf("ERROR: unknown era '%s' specified as chain-sync start point\n", chainSyncFlags.startEra)
os.Exit(1)
}
intersectPoint = eraIntersect[f.network][chainSyncFlags.startEra]
}

conn := createClientConnection(f)
Expand Down Expand Up @@ -144,11 +157,11 @@ func testChainSync(f *globalFlags) {
os.Exit(1)
}
point = tip.Point
} else if len(eraIntersect[f.networkMagic][chainSyncFlags.startEra]) > 0 {
} else if len(intersectPoint) > 0 {
// Slot
slot := uint64(eraIntersect[f.networkMagic][chainSyncFlags.startEra][0].(int))
slot := uint64(intersectPoint[0].(int))
// Block hash
hash, _ := hex.DecodeString(eraIntersect[f.networkMagic][chainSyncFlags.startEra][1].(string))
hash, _ := hex.DecodeString(intersectPoint[1].(string))
point = common.NewPoint(slot, hash)
} else {
point = common.NewPointOrigin()
Expand Down
34 changes: 8 additions & 26 deletions cmd/go-ouroboros-network/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@ import (
"fmt"
"net"
"os"
)

const (
TESTNET_MAGIC = 1097911063
MAINNET_MAGIC = 764824073
PREPROD_MAGIC = 1
PREVIEW_MAGIC = 2
"github.com/cloudstruct/go-ouroboros-network"
)

type globalFlags struct {
Expand All @@ -21,11 +16,8 @@ type globalFlags struct {
address string
useTls bool
ntnProto bool
network string
networkMagic int
testnet bool
mainnet bool
preprod bool
preview bool
}

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

Expand All @@ -53,19 +42,12 @@ func main() {
}

if f.networkMagic == 0 {
if f.testnet {
f.networkMagic = TESTNET_MAGIC
} else if f.mainnet {
f.networkMagic = MAINNET_MAGIC
} else if f.preprod {
f.networkMagic = PREPROD_MAGIC
} else if f.preview {
f.networkMagic = PREVIEW_MAGIC
} else {
fmt.Printf("You must specify one of -testnet, -mainnet, -preprod, -preview, or -network-magic\n\n")
flag.PrintDefaults()
network := ouroboros.NetworkByName(f.network)
if network == ouroboros.NetworkInvalid {
fmt.Printf("Invalid network specified: %s\n", f.network)
os.Exit(1)
}
f.networkMagic = int(network.NetworkMagic)
}

if len(f.flagset.Args()) > 0 {
Expand Down
55 changes: 55 additions & 0 deletions networks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package ouroboros

// Network definitions
var (
NetworkTestnet = Network{Id: 0, Name: "testnet", NetworkMagic: 1097911063}
NetworkMainnet = Network{Id: 1, Name: "mainnet", NetworkMagic: 764824073}
NetworkPreprod = Network{Id: 2, Name: "preprod", NetworkMagic: 1}
NetworkPreview = Network{Id: 3, Name: "preview", NetworkMagic: 2}

NetworkInvalid = Network{Id: 0, Name: "invalid", NetworkMagic: 0} // NetworkInvalid is used as a return value for lookup functions when a network isn't found
)

// List of valid networks for use in lookup functions
var networks = []Network{NetworkTestnet, NetworkMainnet, NetworkPreprod, NetworkPreview}

// NetworkByName returns a predefined network by name
func NetworkByName(name string) Network {
for _, network := range networks {
if network.Name == name {
return network
}
}
return NetworkInvalid
}

// NetworkById returns a predefined network by ID
func NetworkById(id uint) Network {
for _, network := range networks {
if network.Id == id {
return network
}
}
return NetworkInvalid
}

// NetworkByNetworkMagic returns a predefined network by network magic
func NetworkByNetworkMagic(networkMagic uint32) Network {
for _, network := range networks {
if network.NetworkMagic == networkMagic {
return network
}
}
return NetworkInvalid
}

// Network represents a Cardano network
type Network struct {
Id uint
Name string
NetworkMagic uint32
}

func (n Network) String() string {
return n.Name
}
7 changes: 7 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ func WithConnection(conn net.Conn) OuroborosOptionFunc {
}
}

// WithNetwork specifies the network
func WithNetwork(network Network) OuroborosOptionFunc {
return func(o *Ouroboros) {
o.networkMagic = network.NetworkMagic
}
}

// WithNetworkMagic specifies the network magic value
func WithNetworkMagic(networkMagic uint32) OuroborosOptionFunc {
return func(o *Ouroboros) {
Expand Down

0 comments on commit 87808a1

Please sign in to comment.