Move networkType into internal config

pull/1121/head
Rongjian Lan 6 years ago
parent 973001f376
commit 2f1223e84b
  1. 25
      cmd/harmony/main.go
  2. 27
      common/config/global_config.go
  3. 37
      internal/configs/node/config.go
  4. 19
      node/node.go
  5. 11
      node/node_genesis.go
  6. 4
      node/node_handler.go

@ -10,8 +10,6 @@ import (
"runtime" "runtime"
"time" "time"
"github.com/harmony-one/harmony/common/config"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
@ -139,17 +137,6 @@ var (
) )
func initSetup() { 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. // Set port and ip to global config.
nodeconfig.GetDefaultConfig().Port = *port nodeconfig.GetDefaultConfig().Port = *port
nodeconfig.GetDefaultConfig().IP = *ip nodeconfig.GetDefaultConfig().IP = *ip
@ -264,6 +251,18 @@ func createGlobalConfig() *nodeconfig.ConfigType {
nodeConfig = nodeconfig.GetShardConfig(uint32(*shardID)) 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} 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 if accountIndex < core.GenesisShardNum && !*isExplorer { // The first node in a shard is the leader at genesis

@ -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

@ -55,6 +55,31 @@ func (role Role) String() string {
return "Unknown" 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 // Global is the index of the global node configuration
const ( const (
Global = 0 Global = 0
@ -87,6 +112,8 @@ type ConfigType struct {
SelfPeer p2p.Peer SelfPeer p2p.Peer
Leader p2p.Peer Leader p2p.Peer
networkType NetworkType
} }
// configs is a list of node configuration. // configs is a list of node configuration.
@ -202,3 +229,13 @@ func (conf *ConfigType) IsLeader() bool {
func (conf *ConfigType) Role() Role { func (conf *ConfigType) Role() Role {
return conf.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
}

@ -8,8 +8,6 @@ import (
"sync" "sync"
"time" "time"
"github.com/harmony-one/harmony/common/config"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/bls/ffi/go/bls"
@ -291,6 +289,14 @@ func (node *Node) GetSyncID() [SyncIDLength]byte {
// New creates a new node. // New creates a new node.
func New(host p2p.Host, consensusObj *consensus.Consensus, chainDBFactory shardchain.DBFactory, isArchival bool) *Node { func New(host p2p.Host, consensusObj *consensus.Consensus, chainDBFactory shardchain.DBFactory, isArchival bool) *Node {
node := 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)) copy(node.syncID[:], GenerateRandomString(SyncIDLength))
if host != nil { if host != nil {
node.host = host 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 // 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. // 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 { if node.isFirstTime {
// Setup one time smart contracts // Setup one time smart contracts
node.AddFaucetContractToPendingTransactions() node.AddFaucetContractToPendingTransactions()
@ -377,13 +383,6 @@ func New(host p2p.Host, consensusObj *consensus.Consensus, chainDBFactory shardc
node.startConsensus = make(chan struct{}) 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 return &node
} }

@ -6,9 +6,9 @@ import (
"math/rand" "math/rand"
"strings" "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/crypto"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
@ -67,14 +67,15 @@ func (node *Node) SetupGenesisBlock(db ethdb.Database, shardID uint32, myShardSt
genesisAlloc := make(core.GenesisAlloc) genesisAlloc := make(core.GenesisAlloc)
chainConfig := params.ChainConfig{} chainConfig := params.ChainConfig{}
switch config.Network { switch node.NodeConfig.GetNetworkType() {
case config.Mainnet: case nodeconfig.Mainnet:
chainConfig = *params.MainnetChainConfig chainConfig = *params.MainnetChainConfig
foundationAddress := common.HexToAddress("0xE25ABC3f7C3d5fB7FB81EAFd421FF1621A61107c") foundationAddress := common.HexToAddress("0xE25ABC3f7C3d5fB7FB81EAFd421FF1621A61107c")
genesisFunds := big.NewInt(GenesisFund) genesisFunds := big.NewInt(GenesisFund)
genesisFunds = genesisFunds.Mul(genesisFunds, big.NewInt(denominations.One)) genesisFunds = genesisFunds.Mul(genesisFunds, big.NewInt(denominations.One))
genesisAlloc[foundationAddress] = core.GenesisAccount{Balance: genesisFunds} genesisAlloc[foundationAddress] = core.GenesisAccount{Balance: genesisFunds}
case config.Testnet: case nodeconfig.Testnet:
case nodeconfig.Devnet:
chainConfig = *params.TestnetChainConfig chainConfig = *params.TestnetChainConfig
// Tests account for txgen to use // Tests account for txgen to use
node.AddTestingAddresses(genesisAlloc, TestAccountNumber) node.AddTestingAddresses(genesisAlloc, TestAccountNumber)

@ -16,8 +16,6 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/harmony-one/harmony/common/config"
"github.com/harmony-one/harmony/api/service/explorer" "github.com/harmony-one/harmony/api/service/explorer"
"github.com/harmony-one/harmony/consensus" "github.com/harmony-one/harmony/consensus"
@ -405,7 +403,7 @@ func (node *Node) PostConsensusProcessing(newBlock *types.Block) {
node.AddNewBlock(newBlock) 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 // Update contract deployer's nonce so default contract like faucet can issue transaction with current nonce
nonce := node.GetNonceOfAddress(crypto.PubkeyToAddress(node.ContractDeployerKey.PublicKey)) nonce := node.GetNonceOfAddress(crypto.PubkeyToAddress(node.ContractDeployerKey.PublicKey))
atomic.StoreUint64(&node.ContractDeployerCurrentNonce, nonce) atomic.StoreUint64(&node.ContractDeployerCurrentNonce, nonce)

Loading…
Cancel
Save