config: move NetMode into its own micropackage

It's going to be used a bit more and pulling whole config just for one type is
a bit wrong.
This commit is contained in:
Roman Khimov 2020-06-14 10:34:50 +03:00
parent 6eb600de5a
commit 26f11a52d9
12 changed files with 61 additions and 51 deletions

View file

@ -7,6 +7,7 @@ import (
"os/signal"
"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/core"
"github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/core/storage"
@ -107,12 +108,12 @@ func newGraceContext() context.Context {
// getConfigFromContext looks at path and mode flags in the given config and
// returns appropriate config.
func getConfigFromContext(ctx *cli.Context) (config.Config, error) {
var net = config.ModePrivNet
var net = netmode.PrivNet
if ctx.Bool("testnet") {
net = config.ModeTestNet
net = netmode.TestNet
}
if ctx.Bool("mainnet") {
net = config.ModeMainNet
net = netmode.MainNet
}
configPath := "./config"
if argCp := ctx.String("config-path"); argCp != "" {

View file

@ -5,6 +5,7 @@ import (
"testing"
"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/core"
"github.com/nspcc-dev/neo-go/pkg/core/storage"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
@ -20,7 +21,7 @@ import (
// Benchmark test to measure number of processed TX.
// Same benchmark made on reference C# node https://github.com/neo-project/neo/issues/1321.
func BenchmarkTXPerformanceTest(t *testing.B) {
net := config.ModeUnitTestNet
net := netmode.UnitTestNet
configPath := "../config"
cfg, err := config.Load(configPath, net)
require.NoError(t, err, "could not load config")

View file

@ -6,6 +6,7 @@ import (
"os"
"github.com/go-yaml/yaml"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/pkg/errors"
)
@ -28,7 +29,7 @@ func (c Config) GenerateUserAgent() string {
// Load attempts to load the config from the given
// path for the given netMode.
func Load(path string, netMode NetMode) (Config, error) {
func Load(path string, netMode netmode.Magic) (Config, error) {
configPath := fmt.Sprintf("%s/protocol.%s.yml", path, netMode)
if _, err := os.Stat(configPath); os.IsNotExist(err) {
return Config{}, errors.Wrap(err, "Unable to load config")

View file

@ -0,0 +1,31 @@
package netmode
const (
// MainNet contains magic code used in the NEO main official network.
MainNet Magic = 0x004f454e // 5195086
// TestNet contains magic code used in the NEO testing network.
TestNet Magic = 0x744f454e // 1951352142
// PrivNet contains magic code usually used for NEO private networks.
PrivNet Magic = 56753 // docker privnet
// UnitTestNet is a stub magic code used for testing purposes.
UnitTestNet Magic = 0
)
// Magic describes the network the blockchain will operate on.
type Magic uint32
// String implements the stringer interface.
func (n Magic) String() string {
switch n {
case PrivNet:
return "privnet"
case TestNet:
return "testnet"
case MainNet:
return "mainnet"
case UnitTestNet:
return "unit_testnet"
default:
return "net unknown"
}
}

View file

@ -1,20 +1,10 @@
package config
import (
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/util"
)
const (
// ModeMainNet contains magic code used in the NEO main official network.
ModeMainNet NetMode = 0x004f454e // 5195086
// ModeTestNet contains magic code used in the NEO testing network.
ModeTestNet NetMode = 0x744f454e // 1951352142
// ModePrivNet contains magic code usually used for NEO private networks.
ModePrivNet NetMode = 56753 // docker privnet
// ModeUnitTestNet is a stub magic code used for testing purposes.
ModeUnitTestNet NetMode = 0
)
// ProtocolConfiguration represents the protocol config.
type (
ProtocolConfiguration struct {
@ -24,7 +14,7 @@ type (
// FreeGasLimit is an amount of GAS which can be spent for free.
FreeGasLimit util.Fixed8 `yaml:"FreeGasLimit"`
LowPriorityThreshold float64 `yaml:"LowPriorityThreshold"`
Magic NetMode `yaml:"Magic"`
Magic netmode.Magic `yaml:"Magic"`
MaxTransactionsPerBlock int `yaml:"MaxTransactionsPerBlock"`
// Maximum size of low priority transaction in bytes.
MaxFreeTransactionSize int `yaml:"MaxFreeTransactionSize"`
@ -41,23 +31,4 @@ type (
// Whether to verify transactions in received blocks.
VerifyTransactions bool `yaml:"VerifyTransactions"`
}
// NetMode describes the mode the blockchain will operate on.
NetMode uint32
)
// String implements the stringer interface.
func (n NetMode) String() string {
switch n {
case ModePrivNet:
return "privnet"
case ModeTestNet:
return "testnet"
case ModeMainNet:
return "mainnet"
case ModeUnitTestNet:
return "unit_testnet"
default:
return "net unknown"
}
}

View file

@ -6,6 +6,7 @@ import (
"github.com/nspcc-dev/dbft/block"
"github.com/nspcc-dev/dbft/payload"
"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/core"
"github.com/nspcc-dev/neo-go/pkg/core/storage"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
@ -217,7 +218,7 @@ func getTestValidator(i int) (*privateKey, *publicKey) {
}
func newTestChain(t *testing.T) *core.Blockchain {
unitTestNetCfg, err := config.Load("../../config", config.ModeUnitTestNet)
unitTestNetCfg, err := config.Load("../../config", netmode.UnitTestNet)
require.NoError(t, err)
chain, err := core.NewBlockchain(storage.NewMemoryStore(), unitTestNetCfg.ProtocolConfiguration, zaptest.NewLogger(t))

View file

@ -10,6 +10,7 @@ import (
"time"
"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/core/storage"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
@ -34,7 +35,7 @@ var neoOwner = testchain.MultisigScriptHash()
// newTestChain should be called before newBlock invocation to properly setup
// global state.
func newTestChain(t *testing.T) *Blockchain {
unitTestNetCfg, err := config.Load("../../config", config.ModeUnitTestNet)
unitTestNetCfg, err := config.Load("../../config", netmode.UnitTestNet)
require.NoError(t, err)
chain, err := NewBlockchain(storage.NewMemoryStore(), unitTestNetCfg.ProtocolConfiguration, zaptest.NewLogger(t))
require.NoError(t, err)

View file

@ -4,13 +4,14 @@ import (
"testing"
"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGenesisBlockMainNet(t *testing.T) {
cfg, err := config.Load("../../config", config.ModeMainNet)
cfg, err := config.Load("../../config", netmode.MainNet)
require.NoError(t, err)
block, err := createGenesisBlock(cfg.ProtocolConfiguration)
@ -30,7 +31,7 @@ func TestGetConsensusAddressMainNet(t *testing.T) {
consensusScript = "72c3d9b3bbf776698694cd2c73fa597a10c31294"
)
cfg, err := config.Load("../../config", config.ModeMainNet)
cfg, err := config.Load("../../config", netmode.MainNet)
require.NoError(t, err)
validators, err := getValidators(cfg.ProtocolConfiguration)

View file

@ -3,7 +3,7 @@ package payload
import (
"time"
"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/network/capability"
)
@ -11,7 +11,7 @@ import (
// Version payload.
type Version struct {
// NetMode of the node
Magic config.NetMode
Magic netmode.Magic
// currently the version of the protocol is 0
Version uint32
// timestamp
@ -25,7 +25,7 @@ type Version struct {
}
// NewVersion returns a pointer to a Version payload.
func NewVersion(magic config.NetMode, id uint32, ua string, c []capability.Capability) *Version {
func NewVersion(magic netmode.Magic, id uint32, ua string, c []capability.Capability) *Version {
return &Version{
Magic: magic,
Version: 0,
@ -38,7 +38,7 @@ func NewVersion(magic config.NetMode, id uint32, ua string, c []capability.Capab
// DecodeBinary implements Serializable interface.
func (p *Version) DecodeBinary(br *io.BinReader) {
p.Magic = config.NetMode(br.ReadU32LE())
p.Magic = netmode.Magic(br.ReadU32LE())
p.Version = br.ReadU32LE()
p.Timestamp = br.ReadU32LE()
p.Nonce = br.ReadU32LE()

View file

@ -3,14 +3,14 @@ package payload
import (
"testing"
"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/network/capability"
"github.com/stretchr/testify/assert"
)
func TestVersionEncodeDecode(t *testing.T) {
var magic config.NetMode = 56753
var magic netmode.Magic = 56753
var tcpPort uint16 = 3000
var wsPort uint16 = 3001
var id uint32 = 13337

View file

@ -4,6 +4,7 @@ import (
"time"
"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/wallet"
"go.uber.org/zap/zapcore"
)
@ -38,7 +39,7 @@ type (
// ModePrivNet docker private network.
// ModeTestNet NEO test network.
// ModeMainNet NEO main network.
Net config.NetMode
Net netmode.Magic
// Relay determines whether the server is forwarding its inventory.
Relay bool

View file

@ -7,6 +7,7 @@ import (
"testing"
"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/core"
"github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/core/storage"
@ -19,7 +20,7 @@ import (
)
func getUnitTestChain(t *testing.T) (*core.Blockchain, config.Config, *zap.Logger) {
net := config.ModeUnitTestNet
net := netmode.UnitTestNet
configPath := "../../../config"
cfg, err := config.Load(configPath, net)
require.NoError(t, err, "could not load config")