Cleanup mainnet genesis

pull/1121/head
Rongjian Lan 6 years ago
parent 4002603722
commit e874b299a9
  1. 13
      cmd/harmony/main.go
  2. 50
      node/node.go
  3. 27
      node/node_genesis.go
  4. 81
      node/node_handler.go
  5. 2
      node/node_syncing.go
  6. 2
      test/deploy.sh

@ -10,6 +10,8 @@ import (
"runtime"
"time"
"github.com/harmony-one/harmony/common/config"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
@ -98,6 +100,8 @@ var (
delayCommit = flag.String("delay_commit", "0ms", "how long to delay sending commit messages in consensus, ex: 500ms, 1s")
// isExplorer indicates this node is a node to serve explorer
isExplorer = flag.Bool("is_explorer", false, "true means this node is a node to serve explorer")
// networkType indicates the type of the network
networkType = flag.String("network_type", "mainnet", "type of the network: mainnet, testnet, devnet...")
// isNewNode indicates this node is a new node
isNewNode = flag.Bool("is_newnode", false, "true means this node is a new node")
shardID = flag.Int("shard_id", -1, "the shard ID of this node")
@ -135,6 +139,15 @@ var (
)
func initSetup() {
switch *networkType {
case "mainnet":
config.Network = config.Mainnet
case "testnet":
config.Network = config.Testnet
case "devnet":
config.Network = config.Devnet
}
// Set port and ip to global config.
nodeconfig.GetDefaultConfig().Port = *port
nodeconfig.GetDefaultConfig().IP = *ip

@ -8,9 +8,10 @@ import (
"sync"
"time"
"github.com/harmony-one/harmony/common/config"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"github.com/harmony-one/bls/ffi/go/bls"
"github.com/harmony-one/harmony/accounts"
"github.com/harmony-one/harmony/api/client"
@ -289,8 +290,6 @@ 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 {
var err error
node := Node{}
copy(node.syncID[:], GenerateRandomString(SyncIDLength))
if host != nil {
@ -298,13 +297,6 @@ func New(host p2p.Host, consensusObj *consensus.Consensus, chainDBFactory shardc
node.SelfPeer = host.GetSelfPeer()
}
// Create test keys. Genesis will later need this.
node.TestBankKeys, err = CreateTestBankKeys(TestAccountNumber)
if err != nil {
utils.GetLogInstance().Crit("Error while creating test keys",
"error", err)
}
collection := shardchain.NewCollection(
chainDBFactory, &genesisInitializer{&node}, consensusObj)
if isArchival {
@ -332,27 +324,37 @@ 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 node.isFirstTime {
// Setup one time smart contracts
//node.AddFaucetContractToPendingTransactions()
} else {
node.AddContractKeyAndAddress(scFaucet)
}
if node.Consensus.ShardID == 0 {
// Contracts only exist in beacon chain
if config.Network != config.Mainnet {
if node.isFirstTime {
// Setup one time smart contracts
node.CurrentStakes = make(map[common.Address]*structs.StakeInfo)
node.AddStakingContractToPendingTransactions() //This will save the latest information about staked nodes in current staked
//node.AddFaucetContractToPendingTransactions()
} else {
node.AddContractKeyAndAddress(scStaking)
node.AddContractKeyAndAddress(scFaucet)
}
if node.Consensus.ShardID == 0 {
// Contracts only exist in beacon chain
if node.isFirstTime {
// Setup one time smart contracts
node.CurrentStakes = make(map[common.Address]*structs.StakeInfo)
node.AddStakingContractToPendingTransactions() //This will save the latest information about staked nodes in current staked
} else {
node.AddContractKeyAndAddress(scStaking)
}
}
node.ContractCaller = contracts.NewContractCaller(node.Blockchain(), node.Blockchain().Config())
// Create test keys. Genesis will later need this.
var err error
node.TestBankKeys, err = CreateTestBankKeys(TestAccountNumber)
if err != nil {
utils.GetLogInstance().Crit("Error while creating test keys",
"error", err)
}
}
}
node.ContractCaller = contracts.NewContractCaller(node.Blockchain(), params.TestChainConfig)
if consensusObj != nil && nodeconfig.GetDefaultConfig().IsLeader() {
node.State = NodeLeader
} else {

@ -6,6 +6,8 @@ import (
"math/rand"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/harmony-one/harmony/common/config"
"github.com/ethereum/go-ethereum/crypto"
@ -17,7 +19,6 @@ import (
"github.com/harmony-one/harmony/core"
"github.com/harmony-one/harmony/core/types"
common2 "github.com/harmony-one/harmony/internal/common"
"github.com/harmony-one/harmony/internal/ctxerror"
"github.com/harmony-one/harmony/internal/genesis"
"github.com/harmony-one/harmony/internal/utils"
)
@ -25,8 +26,10 @@ import (
const (
// TestAccountNumber is the number of test accounts
TestAccountNumber = 100
// GenesisFund is the initial total fund in the genesis block.
GenesisFund = 12600000000
// TotalInitFund is the initial total fund for the contract deployer.
TotalInitFund = 12600000000
TotalInitFund = 100000000
// InitFreeFundInEther is the initial fund for permissioned accounts.
InitFreeFundInEther = 100
)
@ -47,14 +50,12 @@ func (gi *genesisInitializer) InitChainDB(db ethdb.Database, shardID uint32) err
}
shardState = types.ShardState{*c}
}
if err := gi.node.SetupGenesisBlock(db, shardID, shardState); err != nil {
return ctxerror.New("cannot setup genesis block").WithCause(err)
}
gi.node.SetupGenesisBlock(db, shardID, shardState)
return nil
}
// SetupGenesisBlock sets up a genesis blockchain.
func (node *Node) SetupGenesisBlock(db ethdb.Database, shardID uint32, myShardState types.ShardState) error {
func (node *Node) SetupGenesisBlock(db ethdb.Database, shardID uint32, myShardState types.ShardState) {
utils.GetLogger().Info("setting up a brand new chain database",
"shardID", shardID)
if shardID == node.Consensus.ShardID {
@ -69,6 +70,10 @@ func (node *Node) SetupGenesisBlock(db ethdb.Database, shardID uint32, myShardSt
switch config.Network {
case config.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:
chainConfig = *params.TestnetChainConfig
// Tests account for txgen to use
@ -83,14 +88,10 @@ func (node *Node) SetupGenesisBlock(db ethdb.Database, shardID uint32, myShardSt
node.ContractDeployerKey = contractDeployerKey
}
if shardID == 0 {
// Accounts used by validator/nodes to stake and participate in the network.
// AddNodeAddressesToGenesisAlloc(genesisAlloc)
}
// Initialize shard state
// TODO: add ShardID into chainconfig and change ChainID to NetworkID
chainConfig.ChainID = big.NewInt(int64(shardID)) // Use ChainID as piggybacked ShardID
gspec := core.Genesis{
Config: &chainConfig,
Alloc: genesisAlloc,
@ -100,9 +101,7 @@ func (node *Node) SetupGenesisBlock(db ethdb.Database, shardID uint32, myShardSt
}
// Store genesis block into db.
_, err := gspec.Commit(db)
return err
gspec.MustCommit(db)
}
// CreateTestBankKeys deterministically generates testing addresses.

@ -6,6 +6,7 @@ import (
"encoding/binary"
"encoding/hex"
"errors"
"github.com/harmony-one/harmony/common/config"
"math"
"math/big"
"os"
@ -403,56 +404,56 @@ func (node *Node) PostConsensusProcessing(newBlock *types.Block) {
node.AddNewBlock(newBlock)
// 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)
for _, tx := range newBlock.Transactions() {
msg, err := tx.AsMessage(types.HomesteadSigner{})
if err != nil {
utils.GetLogInstance().Error("Error when parsing tx into message")
}
if _, ok := node.AddressNonce.Load(msg.From()); ok {
nonce := node.GetNonceOfAddress(msg.From())
node.AddressNonce.Store(msg.From(), nonce)
}
}
if node.Consensus.ShardID == 0 {
if config.Network != config.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)
// TODO: enable drand only for beacon chain
// ConfirmedBlockChannel which is listened by drand leader who will initiate DRG if its a epoch block (first block of a epoch)
if node.DRand != nil {
go func() {
node.ConfirmedBlockChannel <- newBlock
}()
for _, tx := range newBlock.Transactions() {
msg, err := tx.AsMessage(types.HomesteadSigner{})
if err != nil {
utils.GetLogInstance().Error("Error when parsing tx into message")
}
if _, ok := node.AddressNonce.Load(msg.From()); ok {
nonce := node.GetNonceOfAddress(msg.From())
node.AddressNonce.Store(msg.From(), nonce)
}
}
// TODO: update staking information once per epoch.
node.UpdateStakingList(node.QueryStakeInfo())
node.printStakingList()
}
newBlockHeader := newBlock.Header()
if newBlockHeader.ShardStateHash != (common.Hash{}) {
// TODO: Enable the following after v0
if node.Consensus.ShardID == 0 {
// TODO ek – this is a temp hack until beacon chain sync is fixed
// End-of-epoch block on beacon chain; block's EpochState is the
// master resharding table. Broadcast it to the network.
if err := node.broadcastEpochShardState(newBlock); err != nil {
e := ctxerror.New("cannot broadcast shard state").WithCause(err)
// TODO: enable drand only for beacon chain
// ConfirmedBlockChannel which is listened by drand leader who will initiate DRG if its a epoch block (first block of a epoch)
if node.DRand != nil {
go func() {
node.ConfirmedBlockChannel <- newBlock
}()
}
// TODO: update staking information once per epoch.
node.UpdateStakingList(node.QueryStakeInfo())
node.printStakingList()
}
newBlockHeader := newBlock.Header()
if newBlockHeader.ShardStateHash != (common.Hash{}) {
if node.Consensus.ShardID == 0 {
// TODO ek – this is a temp hack until beacon chain sync is fixed
// End-of-epoch block on beacon chain; block's EpochState is the
// master resharding table. Broadcast it to the network.
if err := node.broadcastEpochShardState(newBlock); err != nil {
e := ctxerror.New("cannot broadcast shard state").WithCause(err)
ctxerror.Log15(utils.GetLogInstance().Error, e)
}
}
shardState, err := newBlockHeader.GetShardState()
if err != nil {
e := ctxerror.New("cannot get shard state from header").WithCause(err)
ctxerror.Log15(utils.GetLogInstance().Error, e)
} else {
node.transitionIntoNextEpoch(shardState)
}
}
shardState, err := newBlockHeader.GetShardState()
if err != nil {
e := ctxerror.New("cannot get shard state from header").WithCause(err)
ctxerror.Log15(utils.GetLogInstance().Error, e)
} else {
node.transitionIntoNextEpoch(shardState)
}
}
}

@ -237,7 +237,7 @@ func (node *Node) CalculateResponse(request *downloader_pb.DownloaderRequest) (*
copy(startHashHeader[:], request.BlockHash[:])
startBlock := node.Blockchain().GetBlockByHash(startHashHeader)
if startBlock == nil {
return response, fmt.Errorf("[SYNC] GetBlockHashes Request cannot find startHash %v", startHashHeader)
return response, fmt.Errorf("[SYNC] GetBlockHashes Request cannot find startHash %s", startHashHeader.Hex())
}
startHeight := startBlock.NumberU64()
endHeight := node.Blockchain().CurrentBlock().NumberU64()

@ -176,7 +176,7 @@ while IFS='' read -r line || [[ -n "$line" ]]; do
args=("${base_args[@]}" -ip "${ip}" -port "${port}" -key "/tmp/${ip}-${port}.key" -db_dir "db-${ip}-${port}" -accounts "${account}" -blspass file:blspass.txt -blskey_file "${blspub}.key")
fi
args=("${base_args[@]}" -ip "${ip}" -port "${port}" -key "/tmp/${ip}-${port}.key" -db_dir "db-${ip}-${port}" -blspass file:blspass.txt -blskey_file "${blspub}.key")
args=("${base_args[@]}" -ip "${ip}" -port "${port}" -key "/tmp/${ip}-${port}.key" -db_dir "db-${ip}-${port}" -blspass file:blspass.txt -blskey_file "${blspub}.key" -dns=false)
case "${mode}" in
leader*|validator*) args=("${args[@]}" -is_genesis);;
esac

Loading…
Cancel
Save