From 2f1223e84bad58bf4515cec5a1bfcd566ee52752 Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Thu, 20 Jun 2019 21:36:49 -0700 Subject: [PATCH] Move networkType into internal config --- cmd/harmony/main.go | 25 +++++++++++----------- common/config/global_config.go | 27 ------------------------ internal/configs/node/config.go | 37 +++++++++++++++++++++++++++++++++ node/node.go | 19 ++++++++--------- node/node_genesis.go | 11 +++++----- node/node_handler.go | 4 +--- 6 files changed, 65 insertions(+), 58 deletions(-) delete mode 100644 common/config/global_config.go diff --git a/cmd/harmony/main.go b/cmd/harmony/main.go index fcb11c201..77b1c56d1 100644 --- a/cmd/harmony/main.go +++ b/cmd/harmony/main.go @@ -10,8 +10,6 @@ import ( "runtime" "time" - "github.com/harmony-one/harmony/common/config" - "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/log" @@ -139,17 +137,6 @@ var ( ) func initSetup() { - switch *networkType { - case "mainnet": - config.Network = config.Mainnet - case "testnet": - config.Network = config.Testnet - case "devnet": - config.Network = config.Devnet - default: - panic(fmt.Sprintf("invalid network type: %s", *networkType)) - } - // Set port and ip to global config. nodeconfig.GetDefaultConfig().Port = *port nodeconfig.GetDefaultConfig().IP = *ip @@ -264,6 +251,18 @@ func createGlobalConfig() *nodeconfig.ConfigType { nodeConfig = nodeconfig.GetShardConfig(uint32(*shardID)) } + // Set network type + switch *networkType { + case "mainnet": + nodeConfig.SetNetworkType(nodeconfig.Mainnet) + case "testnet": + nodeConfig.SetNetworkType(nodeconfig.Testnet) + case "devnet": + nodeConfig.SetNetworkType(nodeconfig.Devnet) + default: + panic(fmt.Sprintf("invalid network type: %s", *networkType)) + } + nodeConfig.SelfPeer = p2p.Peer{IP: *ip, Port: *port, ConsensusPubKey: nodeConfig.ConsensusPubKey} if accountIndex < core.GenesisShardNum && !*isExplorer { // The first node in a shard is the leader at genesis diff --git a/common/config/global_config.go b/common/config/global_config.go deleted file mode 100644 index fed48ced6..000000000 --- a/common/config/global_config.go +++ /dev/null @@ -1,27 +0,0 @@ -package config - -// NetworkType describes the type of Harmony network -type NetworkType int - -// Constants for NetworkType -const ( - Mainnet NetworkType = 0 - Testnet NetworkType = 1 - Devnet NetworkType = 2 -) - -func (network NetworkType) String() string { - switch network { - case 0: - return "Mainnet" - case 1: - return "Testnet" - case 2: - return "Devnet" - default: - return "Unknown" - } -} - -// Network is the type of Harmony network -var Network = Testnet diff --git a/internal/configs/node/config.go b/internal/configs/node/config.go index 946a6f195..e1ff1d52c 100644 --- a/internal/configs/node/config.go +++ b/internal/configs/node/config.go @@ -55,6 +55,31 @@ func (role Role) String() string { return "Unknown" } +// NetworkType describes the type of Harmony network +type NetworkType int + +// Constants for NetworkType +const ( + Mainnet NetworkType = iota + Testnet + Devnet +) + +func (network NetworkType) String() string { + switch network { + case Mainnet: + return "Mainnet" + case Testnet: + return "Testnet" + case Devnet: + return "Devnet" + } + return "Unknown" +} + +// Network is the type of Harmony network +var Network = Testnet + // Global is the index of the global node configuration const ( Global = 0 @@ -87,6 +112,8 @@ type ConfigType struct { SelfPeer p2p.Peer Leader p2p.Peer + + networkType NetworkType } // configs is a list of node configuration. @@ -202,3 +229,13 @@ func (conf *ConfigType) IsLeader() bool { func (conf *ConfigType) Role() Role { return conf.role } + +// SetNetworkType set the networkType +func (conf *ConfigType) SetNetworkType(networkType NetworkType) { + conf.networkType = networkType +} + +// GetNetworkType gets the networkType +func (conf *ConfigType) GetNetworkType() NetworkType { + return conf.networkType +} diff --git a/node/node.go b/node/node.go index 92b2e0de3..20e8404b5 100644 --- a/node/node.go +++ b/node/node.go @@ -8,8 +8,6 @@ import ( "sync" "time" - "github.com/harmony-one/harmony/common/config" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/log" "github.com/harmony-one/bls/ffi/go/bls" @@ -291,6 +289,14 @@ func (node *Node) GetSyncID() [SyncIDLength]byte { // New creates a new node. func New(host p2p.Host, consensusObj *consensus.Consensus, chainDBFactory shardchain.DBFactory, isArchival bool) *Node { node := Node{} + + // Get the node config that's created in the harmony.go program. + if consensusObj != nil { + node.NodeConfig = nodeconfig.GetShardConfig(consensusObj.ShardID) + } else { + node.NodeConfig = nodeconfig.GetDefaultConfig() + } + copy(node.syncID[:], GenerateRandomString(SyncIDLength)) if host != nil { node.host = host @@ -324,7 +330,7 @@ func New(host p2p.Host, consensusObj *consensus.Consensus, chainDBFactory shardc // Add Faucet contract to all shards, so that on testnet, we can demo wallet in explorer // TODO (leo): we need to have support of cross-shard tx later so that the token can be transferred from beacon chain shard to other tx shards. - if config.Network != config.Mainnet { + if node.NodeConfig.GetNetworkType() != nodeconfig.Mainnet { if node.isFirstTime { // Setup one time smart contracts node.AddFaucetContractToPendingTransactions() @@ -377,13 +383,6 @@ func New(host p2p.Host, consensusObj *consensus.Consensus, chainDBFactory shardc node.startConsensus = make(chan struct{}) - // Get the node config that's created in the harmony.go program. - if consensusObj != nil { - node.NodeConfig = nodeconfig.GetShardConfig(consensusObj.ShardID) - } else { - node.NodeConfig = nodeconfig.GetDefaultConfig() - } - return &node } diff --git a/node/node_genesis.go b/node/node_genesis.go index 93ed1a703..7c788cf67 100644 --- a/node/node_genesis.go +++ b/node/node_genesis.go @@ -6,9 +6,9 @@ import ( "math/rand" "strings" - "github.com/ethereum/go-ethereum/common" + nodeconfig "github.com/harmony-one/harmony/internal/configs/node" - "github.com/harmony-one/harmony/common/config" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" @@ -67,14 +67,15 @@ func (node *Node) SetupGenesisBlock(db ethdb.Database, shardID uint32, myShardSt genesisAlloc := make(core.GenesisAlloc) chainConfig := params.ChainConfig{} - switch config.Network { - case config.Mainnet: + switch node.NodeConfig.GetNetworkType() { + case nodeconfig.Mainnet: chainConfig = *params.MainnetChainConfig foundationAddress := common.HexToAddress("0xE25ABC3f7C3d5fB7FB81EAFd421FF1621A61107c") genesisFunds := big.NewInt(GenesisFund) genesisFunds = genesisFunds.Mul(genesisFunds, big.NewInt(denominations.One)) genesisAlloc[foundationAddress] = core.GenesisAccount{Balance: genesisFunds} - case config.Testnet: + case nodeconfig.Testnet: + case nodeconfig.Devnet: chainConfig = *params.TestnetChainConfig // Tests account for txgen to use node.AddTestingAddresses(genesisAlloc, TestAccountNumber) diff --git a/node/node_handler.go b/node/node_handler.go index aa9e8dfbc..c7ab6c6a0 100644 --- a/node/node_handler.go +++ b/node/node_handler.go @@ -16,8 +16,6 @@ import ( "syscall" "time" - "github.com/harmony-one/harmony/common/config" - "github.com/harmony-one/harmony/api/service/explorer" "github.com/harmony-one/harmony/consensus" @@ -405,7 +403,7 @@ func (node *Node) PostConsensusProcessing(newBlock *types.Block) { node.AddNewBlock(newBlock) - if config.Network != config.Mainnet { + if node.NodeConfig.GetNetworkType() != nodeconfig.Mainnet { // Update contract deployer's nonce so default contract like faucet can issue transaction with current nonce nonce := node.GetNonceOfAddress(crypto.PubkeyToAddress(node.ContractDeployerKey.PublicKey)) atomic.StoreUint64(&node.ContractDeployerCurrentNonce, nonce)