From 26f11a52d95e839ac1f7b4ba44216f10f5bc9f34 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Sun, 14 Jun 2020 10:34:50 +0300 Subject: [PATCH] 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. --- cli/server/server.go | 7 ++--- integration/performance_test.go | 3 ++- pkg/config/config.go | 3 ++- pkg/config/netmode/netmode.go | 31 ++++++++++++++++++++++ pkg/config/protocol_config.go | 39 ++++------------------------ pkg/consensus/consensus_test.go | 3 ++- pkg/core/helper_test.go | 3 ++- pkg/core/util_test.go | 5 ++-- pkg/network/payload/version.go | 8 +++--- pkg/network/payload/version_test.go | 4 +-- pkg/network/server_config.go | 3 ++- pkg/rpc/server/server_helper_test.go | 3 ++- 12 files changed, 61 insertions(+), 51 deletions(-) create mode 100644 pkg/config/netmode/netmode.go diff --git a/cli/server/server.go b/cli/server/server.go index 2e6aca218..e73c0c239 100644 --- a/cli/server/server.go +++ b/cli/server/server.go @@ -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 != "" { diff --git a/integration/performance_test.go b/integration/performance_test.go index 68592cc87..f139cd83a 100644 --- a/integration/performance_test.go +++ b/integration/performance_test.go @@ -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") diff --git a/pkg/config/config.go b/pkg/config/config.go index f434191bd..68b0bc8c9 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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") diff --git a/pkg/config/netmode/netmode.go b/pkg/config/netmode/netmode.go new file mode 100644 index 000000000..8c3642d96 --- /dev/null +++ b/pkg/config/netmode/netmode.go @@ -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" + } +} diff --git a/pkg/config/protocol_config.go b/pkg/config/protocol_config.go index a0342c3a2..151accf24 100644 --- a/pkg/config/protocol_config.go +++ b/pkg/config/protocol_config.go @@ -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 { @@ -22,10 +12,10 @@ type ( // transactions exceeding the MaxFreeTransactionSize. FeePerExtraByte float64 `yaml:"FeePerExtraByte"` // 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"` - MaxTransactionsPerBlock int `yaml:"MaxTransactionsPerBlock"` + FreeGasLimit util.Fixed8 `yaml:"FreeGasLimit"` + LowPriorityThreshold float64 `yaml:"LowPriorityThreshold"` + Magic netmode.Magic `yaml:"Magic"` + MaxTransactionsPerBlock int `yaml:"MaxTransactionsPerBlock"` // Maximum size of low priority transaction in bytes. MaxFreeTransactionSize int `yaml:"MaxFreeTransactionSize"` // Maximum number of low priority transactions accepted into block. @@ -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" - } -} diff --git a/pkg/consensus/consensus_test.go b/pkg/consensus/consensus_test.go index 44ef14532..e03a60c55 100644 --- a/pkg/consensus/consensus_test.go +++ b/pkg/consensus/consensus_test.go @@ -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)) diff --git a/pkg/core/helper_test.go b/pkg/core/helper_test.go index 3c04e798e..414c480dd 100644 --- a/pkg/core/helper_test.go +++ b/pkg/core/helper_test.go @@ -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) diff --git a/pkg/core/util_test.go b/pkg/core/util_test.go index c28752e55..c27d0c10e 100644 --- a/pkg/core/util_test.go +++ b/pkg/core/util_test.go @@ -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) diff --git a/pkg/network/payload/version.go b/pkg/network/payload/version.go index b0747284c..6bb3bcdc1 100644 --- a/pkg/network/payload/version.go +++ b/pkg/network/payload/version.go @@ -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() diff --git a/pkg/network/payload/version_test.go b/pkg/network/payload/version_test.go index 09cd9430b..405b4cead 100644 --- a/pkg/network/payload/version_test.go +++ b/pkg/network/payload/version_test.go @@ -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 diff --git a/pkg/network/server_config.go b/pkg/network/server_config.go index edd6731b7..a84458e3a 100644 --- a/pkg/network/server_config.go +++ b/pkg/network/server_config.go @@ -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 diff --git a/pkg/rpc/server/server_helper_test.go b/pkg/rpc/server/server_helper_test.go index 5e31c4ae7..3f16f12cf 100644 --- a/pkg/rpc/server/server_helper_test.go +++ b/pkg/rpc/server/server_helper_test.go @@ -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")