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

View file

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

View file

@ -6,6 +6,7 @@ import (
"os" "os"
"github.com/go-yaml/yaml" "github.com/go-yaml/yaml"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -28,7 +29,7 @@ func (c Config) GenerateUserAgent() string {
// Load attempts to load the config from the given // Load attempts to load the config from the given
// path for the given netMode. // 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) configPath := fmt.Sprintf("%s/protocol.%s.yml", path, netMode)
if _, err := os.Stat(configPath); os.IsNotExist(err) { if _, err := os.Stat(configPath); os.IsNotExist(err) {
return Config{}, errors.Wrap(err, "Unable to load config") 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 package config
import ( import (
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/util" "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. // ProtocolConfiguration represents the protocol config.
type ( type (
ProtocolConfiguration struct { ProtocolConfiguration struct {
@ -22,10 +12,10 @@ type (
// transactions exceeding the MaxFreeTransactionSize. // transactions exceeding the MaxFreeTransactionSize.
FeePerExtraByte float64 `yaml:"FeePerExtraByte"` FeePerExtraByte float64 `yaml:"FeePerExtraByte"`
// FreeGasLimit is an amount of GAS which can be spent for free. // FreeGasLimit is an amount of GAS which can be spent for free.
FreeGasLimit util.Fixed8 `yaml:"FreeGasLimit"` FreeGasLimit util.Fixed8 `yaml:"FreeGasLimit"`
LowPriorityThreshold float64 `yaml:"LowPriorityThreshold"` LowPriorityThreshold float64 `yaml:"LowPriorityThreshold"`
Magic NetMode `yaml:"Magic"` Magic netmode.Magic `yaml:"Magic"`
MaxTransactionsPerBlock int `yaml:"MaxTransactionsPerBlock"` MaxTransactionsPerBlock int `yaml:"MaxTransactionsPerBlock"`
// Maximum size of low priority transaction in bytes. // Maximum size of low priority transaction in bytes.
MaxFreeTransactionSize int `yaml:"MaxFreeTransactionSize"` MaxFreeTransactionSize int `yaml:"MaxFreeTransactionSize"`
// Maximum number of low priority transactions accepted into block. // Maximum number of low priority transactions accepted into block.
@ -41,23 +31,4 @@ type (
// Whether to verify transactions in received blocks. // Whether to verify transactions in received blocks.
VerifyTransactions bool `yaml:"VerifyTransactions"` 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/block"
"github.com/nspcc-dev/dbft/payload" "github.com/nspcc-dev/dbft/payload"
"github.com/nspcc-dev/neo-go/pkg/config" "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"
"github.com/nspcc-dev/neo-go/pkg/core/storage" "github.com/nspcc-dev/neo-go/pkg/core/storage"
"github.com/nspcc-dev/neo-go/pkg/core/transaction" "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 { 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) require.NoError(t, err)
chain, err := core.NewBlockchain(storage.NewMemoryStore(), unitTestNetCfg.ProtocolConfiguration, zaptest.NewLogger(t)) chain, err := core.NewBlockchain(storage.NewMemoryStore(), unitTestNetCfg.ProtocolConfiguration, zaptest.NewLogger(t))

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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