@ -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
FakeAddress Number = 100
TestAccount Number = 100
// TotalInitFund is the initial total fund for the contract deployer.
// TotalInitFund is the initial total fund for the contract deployer.
TotalInitFund = 10000001 00
TotalInitFund = 126 00000000
// InitFreeFundInEther is the initial fund for sam pl e 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
}
}
// CreateGenesisAllocWith TestingAddresses create the genesis block allocation that contains deterministically
// Add TestingAddresses 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 ) )
genesis Aloc [ testBankAddress ] = core . GenesisAccount { Balance : testBankFunds }
gAl loc [ 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,