Merge pull request #1016 from rlan35/rj_fork

Add network type so we can separate out mainnet and testnet
pull/1022/head
Rongjian Lan 6 years ago committed by GitHub
commit 008e98e4a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      common/config/global_config.go
  2. 2
      node/node.go
  3. 51
      node/node_genesis.go

@ -0,0 +1,14 @@
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
)
// Network is the type of Harmony network
var Network = Testnet

@ -298,7 +298,7 @@ func New(host p2p.Host, consensusObj *consensus.Consensus, chainDBFactory shardc
} }
// Create test keys. Genesis will later need this. // Create test keys. Genesis will later need this.
node.TestBankKeys, err = CreateTestBankKeys(FakeAddressNumber) node.TestBankKeys, err = CreateTestBankKeys(TestAccountNumber)
if err != nil { if err != nil {
utils.GetLogInstance().Crit("Error while creating test keys", utils.GetLogInstance().Crit("Error while creating test keys",
"error", err) "error", err)

@ -6,6 +6,8 @@ import (
"math/rand" "math/rand"
"strings" "strings"
"github.com/harmony-one/harmony/common/config"
"github.com/ethereum/go-ethereum/common" "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"
@ -23,11 +25,11 @@ import (
) )
const ( const (
// FakeAddressNumber is the number of fake address. // TestAccountNumber is the number of test accounts
FakeAddressNumber = 100 TestAccountNumber = 100
// TotalInitFund is the initial total fund for the contract deployer. // TotalInitFund is the initial total fund for the contract deployer.
TotalInitFund = 1000000100 TotalInitFund = 12600000000
// InitFreeFundInEther is the initial fund for sample accounts. // InitFreeFundInEther is the initial fund for permissioned accounts.
InitFreeFundInEther = 100 InitFreeFundInEther = 100
) )
@ -65,25 +67,33 @@ func (node *Node) SetupGenesisBlock(db ethdb.Database, shardID uint32) error {
} }
// Initialize genesis block and blockchain // Initialize genesis block and blockchain
// Tests account for txgen to use
genesisAlloc := node.CreateGenesisAllocWithTestingAddresses(FakeAddressNumber)
// Smart contract deployer account used to deploy protocol-level smart contract genesisAlloc := make(core.GenesisAlloc)
contractDeployerKey, _ := ecdsa.GenerateKey(crypto.S256(), strings.NewReader("Test contract key string stream that is fixed so that generated test key are deterministic every time")) chainConfig := params.ChainConfig{}
contractDeployerAddress := crypto.PubkeyToAddress(contractDeployerKey.PublicKey)
contractDeployerFunds := big.NewInt(TotalInitFund) switch config.Network {
contractDeployerFunds = contractDeployerFunds.Mul(contractDeployerFunds, big.NewInt(denominations.One)) case config.Mainnet:
genesisAlloc[contractDeployerAddress] = core.GenesisAccount{Balance: contractDeployerFunds} chainConfig = *params.MainnetChainConfig
node.ContractDeployerKey = contractDeployerKey case config.Testnet:
chainConfig = *params.TestnetChainConfig
// Tests account for txgen to use
node.AddTestingAddresses(genesisAlloc, TestAccountNumber)
// Smart contract deployer account used to deploy initial smart contract
contractDeployerKey, _ := ecdsa.GenerateKey(crypto.S256(), strings.NewReader("Test contract key string stream that is fixed so that generated test key are deterministic every time"))
contractDeployerAddress := crypto.PubkeyToAddress(contractDeployerKey.PublicKey)
contractDeployerFunds := big.NewInt(TotalInitFund)
contractDeployerFunds = contractDeployerFunds.Mul(contractDeployerFunds, big.NewInt(denominations.One))
genesisAlloc[contractDeployerAddress] = core.GenesisAccount{Balance: contractDeployerFunds}
node.ContractDeployerKey = contractDeployerKey
}
if shardID == 0 { if shardID == 0 {
// Accounts used by validator/nodes to stake and participate in the network. // Accounts used by validator/nodes to stake and participate in the network.
AddNodeAddressesToGenesisAlloc(genesisAlloc) AddNodeAddressesToGenesisAlloc(genesisAlloc)
} }
// TODO: create separate chain config instead of using the same pointer reference // TODO: add ShardID into chainconfig and change ChainID to NetworkID
chainConfig := *params.TestChainConfig
chainConfig.ChainID = big.NewInt(int64(shardID)) // Use ChainID as piggybacked ShardID chainConfig.ChainID = big.NewInt(int64(shardID)) // Use ChainID as piggybacked ShardID
gspec := core.Genesis{ gspec := core.Genesis{
Config: &chainConfig, Config: &chainConfig,
@ -115,18 +125,15 @@ func CreateTestBankKeys(numAddresses int) (keys []*ecdsa.PrivateKey, err error)
return keys, nil return keys, nil
} }
// CreateGenesisAllocWithTestingAddresses create the genesis block allocation that contains deterministically // AddTestingAddresses create the genesis block allocation that contains deterministically
// generated testing addresses with tokens. This is mostly used for generated simulated transactions in txgen. // generated testing addresses with tokens. This is mostly used for generated simulated transactions in txgen.
// TODO: Remove it later when moving to production. func (node *Node) AddTestingAddresses(gAlloc core.GenesisAlloc, numAddress int) {
func (node *Node) CreateGenesisAllocWithTestingAddresses(numAddress int) core.GenesisAlloc {
genesisAloc := make(core.GenesisAlloc)
for _, testBankKey := range node.TestBankKeys { for _, testBankKey := range node.TestBankKeys {
testBankAddress := crypto.PubkeyToAddress(testBankKey.PublicKey) testBankAddress := crypto.PubkeyToAddress(testBankKey.PublicKey)
testBankFunds := big.NewInt(InitFreeFundInEther) testBankFunds := big.NewInt(InitFreeFundInEther)
testBankFunds = testBankFunds.Mul(testBankFunds, big.NewInt(denominations.One)) testBankFunds = testBankFunds.Mul(testBankFunds, big.NewInt(denominations.One))
genesisAloc[testBankAddress] = core.GenesisAccount{Balance: testBankFunds} gAlloc[testBankAddress] = core.GenesisAccount{Balance: testBankFunds}
} }
return genesisAloc
} }
// AddNodeAddressesToGenesisAlloc adds to the genesis block allocation the accounts used for network validators/nodes, // AddNodeAddressesToGenesisAlloc adds to the genesis block allocation the accounts used for network validators/nodes,

Loading…
Cancel
Save