From dd65bf46732dabec648a60ebd7bac3b7d8573f88 Mon Sep 17 00:00:00 2001 From: ak Date: Tue, 12 Mar 2019 23:15:43 -0700 Subject: [PATCH 1/3] Archival node setup --- .../README.md => harmony/ArchivalNode.md} | 2 +- cmd/harmony/main.go | 70 ++++++++++++------- internal/configs/node/config.go | 33 ++++++--- node/node.go | 8 +-- node/node_handler.go | 6 +- node/node_syncing.go | 5 +- node/service_setup.go | 8 +-- test/configs/oneshard1.txt | 2 + test/deploy.sh | 8 ++- 9 files changed, 89 insertions(+), 53 deletions(-) rename cmd/{archival/README.md => harmony/ArchivalNode.md} (99%) diff --git a/cmd/archival/README.md b/cmd/harmony/ArchivalNode.md similarity index 99% rename from cmd/archival/README.md rename to cmd/harmony/ArchivalNode.md index 1822bd626..372bb4b90 100644 --- a/cmd/archival/README.md +++ b/cmd/harmony/ArchivalNode.md @@ -33,4 +33,4 @@ Followings are the set of functions required by archival node. #### Other functionality. -Though the archival nodes do not participate into consensus but the keep the blockchain data, they can function as a light node, answering any type of READ transactions about the blockchain like reading balance of an address, read data state of a deployed smart contract. +Though the archival nodes do not participate into consensus but the keep the blockchain data, they can function as a light node, answering any type of READ transactions about the blockchain like reading balance of an address, read data state of a deployed smart contract. \ No newline at end of file diff --git a/cmd/harmony/main.go b/cmd/harmony/main.go index b4f7609fa..fcea3a7f7 100644 --- a/cmd/harmony/main.go +++ b/cmd/harmony/main.go @@ -8,29 +8,30 @@ import ( "path" "runtime" "strconv" + "sync" "time" - "github.com/harmony-one/bls/ffi/go/bls" - - "github.com/harmony-one/harmony/internal/utils/contract" - "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/log" + + "github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/harmony/consensus" "github.com/harmony-one/harmony/drand" nodeconfig "github.com/harmony-one/harmony/internal/configs/node" "github.com/harmony-one/harmony/internal/profiler" "github.com/harmony-one/harmony/internal/utils" + "github.com/harmony-one/harmony/internal/utils/contract" "github.com/harmony-one/harmony/node" "github.com/harmony-one/harmony/p2p" "github.com/harmony-one/harmony/p2p/p2pimpl" ) var ( - version string - builtBy string - builtAt string - commit string + version string + builtBy string + builtAt string + commit string + stateMutex sync.Mutex ) // InitLDBDatabase initializes a LDBDatabase. isBeacon=true will return the beacon chain database for normal shard nodes @@ -85,6 +86,8 @@ var ( keyFile = flag.String("key", "./.hmykey", "the private key file of the harmony node") // isBeacon indicates this node is a beacon chain node isBeacon = flag.Bool("is_beacon", false, "true means this node is a beacon chain node") + // isArchival indicates this node is a archival node that will save and archive current blockchain + isArchival = flag.Bool("is_archival", false, "true means this node is a archival node") // isNewNode indicates this node is a new node isNewNode = flag.Bool("is_newnode", false, "true means this node is a new node") accountIndex = flag.Int("account_index", 0, "the index of the staking account to use") @@ -171,6 +174,8 @@ func createGlobalConfig() *nodeconfig.ConfigType { if *isLeader { nodeConfig.StringRole = "leader" nodeConfig.Leader = nodeConfig.SelfPeer + } else if *isArchival { + nodeConfig.StringRole = "archival" } else { nodeConfig.StringRole = "validator" } @@ -188,6 +193,13 @@ func createGlobalConfig() *nodeconfig.ConfigType { return nodeConfig } +func setupArchivalNode(nodeConfig *nodeconfig.ConfigType) (*node.Node, *nodeconfig.ConfigType) { + currentNode := node.New(nodeConfig.Host, &consensus.Consensus{ShardID: uint32(0)}, nil) + currentNode.NodeConfig.SetRole(nodeconfig.ArchivalNode) + currentNode.AddBeaconChainDatabase(nodeConfig.BeaconDB) + return currentNode, nodeConfig +} + func setUpConsensusAndNode(nodeConfig *nodeconfig.ConfigType) (*consensus.Consensus, *node.Node) { // Consensus object. // TODO: consensus object shouldn't start here @@ -245,28 +257,32 @@ func main() { flag.Parse() initSetup() + var currentNode *node.Node + var consensus *consensus.Consensus nodeConfig := createGlobalConfig() - - // Init logging. - loggingInit(*logFolder, nodeConfig.StringRole, *ip, *port, *onlyLogTps) - - // Start Profiler for leader if profile argument is on - if nodeConfig.StringRole == "leader" && (*profile || *metricsReportURL != "") { - prof := profiler.GetProfiler() - prof.Config(nodeConfig.ShardIDString, *metricsReportURL) - if *profile { - prof.Start() + if *isArchival { + currentNode, nodeConfig = setupArchivalNode(nodeConfig) + loggingInit(*logFolder, nodeConfig.StringRole, *ip, *port, *onlyLogTps) + log.Info("New Harmony Node ====", "Role", currentNode.NodeConfig.Role(), "multiaddress", fmt.Sprintf("/ip4/%s/tcp/%s/p2p/%s", *ip, *port, nodeConfig.Host.GetID().Pretty())) + go currentNode.DoBeaconSyncing() + } else { + // Start Profiler for leader if profile argument is on + if nodeConfig.StringRole == "leader" && (*profile || *metricsReportURL != "") { + prof := profiler.GetProfiler() + prof.Config(nodeConfig.ShardIDString, *metricsReportURL) + if *profile { + prof.Start() + } } + consensus, currentNode = setUpConsensusAndNode(nodeConfig) + if consensus.IsLeader { + go currentNode.SendPongMessage() + } + // Init logging. + loggingInit(*logFolder, nodeConfig.StringRole, *ip, *port, *onlyLogTps) + log.Info("New Harmony Node ====", "Role", currentNode.NodeConfig.Role(), "multiaddress", fmt.Sprintf("/ip4/%s/tcp/%s/p2p/%s", *ip, *port, nodeConfig.Host.GetID().Pretty())) + go currentNode.SupportSyncing() } - - consensus, currentNode := setUpConsensusAndNode(nodeConfig) - - if consensus.IsLeader { - go currentNode.SendPongMessage() - } - - log.Info("New Harmony Node ====", "Role", currentNode.NodeConfig.Role(), "multiaddress", fmt.Sprintf("/ip4/%s/tcp/%s/p2p/%s", *ip, *port, nodeConfig.Host.GetID().Pretty())) - go currentNode.SupportSyncing() currentNode.ServiceManagerSetup() currentNode.RunServices() currentNode.StartServer() diff --git a/internal/configs/node/config.go b/internal/configs/node/config.go index ad13eac6d..df8106671 100644 --- a/internal/configs/node/config.go +++ b/internal/configs/node/config.go @@ -27,7 +27,7 @@ const ( NewNode ClientNode WalletNode - BackupNode + ArchivalNode ) func (role Role) String() string { @@ -48,8 +48,8 @@ func (role Role) String() string { return "ClientNode" case WalletNode: return "WalletNode" - case BackupNode: - return "BackupNode" + case ArchivalNode: + return "ArchivalNode" } return "Unknown" } @@ -63,14 +63,15 @@ const ( // ConfigType is the structure of all node related configuration variables type ConfigType struct { // The three groupID design, please refer to https://github.com/harmony-one/harmony/blob/master/node/node.md#libp2p-integration - beacon p2p.GroupID // the beacon group ID - group p2p.GroupID // the group ID of the shard - client p2p.GroupID // the client group ID of the shard - isClient bool // whether this node is a client node, such as wallet/txgen - isBeacon bool // whether this node is a beacon node or not - isLeader bool // whether this node is a leader or not - shardID uint32 // shardID of this node - role Role // Role of the node + beacon p2p.GroupID // the beacon group ID + group p2p.GroupID // the group ID of the shard + client p2p.GroupID // the client group ID of the shard + isClient bool // whether this node is a client node, such as wallet/txgen + isBeacon bool // whether this node is a beacon node or not + isLeader bool // whether this node is a leader or not + isArchival bool // whether this node is a archival node. archival node backups all blockchain information. + shardID uint32 // shardID of this node + role Role // Role of the node ShardIDString string StringRole string @@ -142,6 +143,11 @@ func (conf *ConfigType) SetIsLeader(b bool) { conf.isLeader = b } +// SetIsArchival set the isLeader configuration +func (conf *ConfigType) SetIsArchival(b bool) { + conf.isArchival = b +} + // SetShardID set the shardID func (conf *ConfigType) SetShardID(s uint32) { conf.shardID = s @@ -182,6 +188,11 @@ func (conf *ConfigType) IsLeader() bool { return conf.isLeader } +// IsArchival returns the isArchival configuration +func (conf *ConfigType) IsArchival() bool { + return conf.isArchival +} + // ShardID returns the shardID func (conf *ConfigType) ShardID() uint32 { return conf.shardID diff --git a/node/node.go b/node/node.go index 03379084f..2d5fc4a62 100644 --- a/node/node.go +++ b/node/node.go @@ -247,7 +247,7 @@ func New(host p2p.Host, consensusObj *consensus.Consensus, db ethdb.Database) *N node.BlockChannel = make(chan *types.Block) node.ConfirmedBlockChannel = make(chan *types.Block) node.BeaconBlockChannel = make(chan *types.Block) - + utils.GetLogInstance().Debug("All Channels setup for ", "node", node.SelfPeer) node.TxPool = core.NewTxPool(core.DefaultTxPoolConfig, params.TestChainConfig, chain) node.Worker = worker.New(params.TestChainConfig, chain, node.Consensus, pki.GetAddressFromPublicKey(node.SelfPeer.ConsensusPubKey), node.Consensus.ShardID) @@ -431,12 +431,12 @@ func (node *Node) initNodeConfiguration(isBeacon bool, isClient bool) (service.N nodeConfig.Actions[p2p.GroupIDBeaconClient] = p2p.ActionStart var err error - if !isBeacon { - node.groupReceiver, err = node.host.GroupReceiver(p2p.GroupIDBeaconClient) - } else { + if isBeacon { node.groupReceiver, err = node.host.GroupReceiver(p2p.GroupIDBeacon) node.clientReceiver, err = node.host.GroupReceiver(p2p.GroupIDBeaconClient) node.NodeConfig.SetClientGroupID(p2p.GroupIDBeaconClient) + } else { + node.groupReceiver, err = node.host.GroupReceiver(p2p.GroupIDBeaconClient) } if err != nil { diff --git a/node/node_handler.go b/node/node_handler.go index d1fde694f..f9d458c92 100644 --- a/node/node_handler.go +++ b/node/node_handler.go @@ -142,12 +142,14 @@ func (node *Node) messageHandler(content []byte, sender string) { } else { // for non-beaconchain node, subscribe to beacon block broadcast role := node.NodeConfig.Role() - if proto_node.BlockMessageType(msgPayload[0]) == proto_node.Sync && (role == nodeconfig.ShardValidator || role == nodeconfig.ShardLeader || role == nodeconfig.NewNode) { + if proto_node.BlockMessageType(msgPayload[0]) == proto_node.Sync && (role == nodeconfig.ShardValidator || role == nodeconfig.ShardLeader || role == nodeconfig.NewNode || role == nodeconfig.ArchivalNode) { + utils.GetLogInstance().Info("Block being handled by block channel", "self peer", node.SelfPeer) for _, block := range blocks { node.BeaconBlockChannel <- block } } if node.Client != nil && node.Client.UpdateBlocks != nil && blocks != nil { + utils.GetLogInstance().Info("Block being handled by client by", "self peer", node.SelfPeer) node.Client.UpdateBlocks(blocks) } } @@ -340,7 +342,7 @@ func (node *Node) AddNewBlock(newBlock *types.Block) { if err != nil { utils.GetLogInstance().Debug("Error adding new block to blockchain", "blockNum", blockNum, "Error", err) } else { - utils.GetLogInstance().Info("adding new block to blockchain", "blockNum", blockNum) + utils.GetLogInstance().Info("adding new block to blockchain", "blockNum", blockNum, "by node", node.SelfPeer) } } diff --git a/node/node_syncing.go b/node/node_syncing.go index e0788c3a5..2146fb753 100644 --- a/node/node_syncing.go +++ b/node/node_syncing.go @@ -41,7 +41,6 @@ func (node *Node) getNeighborPeers(neighbor *sync.Map) []p2p.Peer { res = append(res, v.(p2p.Peer)) return true }) - removeID := -1 for i := range res { if res[i].Port == node.SelfPeer.Port { @@ -52,12 +51,12 @@ func (node *Node) getNeighborPeers(neighbor *sync.Map) []p2p.Peer { if removeID != -1 { res = append(res[:removeID], res[removeID+1:]...) } - utils.GetLogInstance().Debug("GetSyncingPeers: ", "res", res, "self", node.SelfPeer) return res } // GetBeaconSyncingPeers returns a list of peers for beaconchain syncing func (node *Node) GetBeaconSyncingPeers() []p2p.Peer { + return node.getNeighborPeers(&node.BeaconNeighbors) } @@ -71,6 +70,7 @@ func (node *Node) DoBeaconSyncing() { for { select { case beaconBlock := <-node.BeaconBlockChannel: + utils.GetLogInstance().Debug("[SYNC] Received BeaconBlockChannel Ping for Sync") if node.beaconSync == nil { node.beaconSync = syncing.CreateStateSync(node.SelfPeer.IP, node.SelfPeer.Port) node.beaconSync.CreateSyncConfig(node.GetBeaconSyncingPeers()) @@ -79,6 +79,7 @@ func (node *Node) DoBeaconSyncing() { startHash := node.beaconChain.CurrentBlock().Hash() node.beaconSync.AddLastMileBlock(beaconBlock) node.beaconSync.StartStateSync(startHash[:], node.beaconChain, node.BeaconWorker, true) + utils.GetLogInstance().Debug("[SYNC] STARTING BEACON SYNC") } } } diff --git a/node/service_setup.go b/node/service_setup.go index d9a148f4d..c33da5bac 100644 --- a/node/service_setup.go +++ b/node/service_setup.go @@ -104,8 +104,8 @@ func (node *Node) setupForClientNode() { node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, p2p.GroupIDBeacon, chanPeer, nil)) } -func (node *Node) setupForBackupNode(isBeacon bool) { - nodeConfig, chanPeer := node.initNodeConfiguration(isBeacon, false) +func (node *Node) setupForArchivalNode() { + nodeConfig, chanPeer := node.initNodeConfiguration(false, false) // Register peer discovery service. node.serviceManager.RegisterService(service.PeerDiscovery, discovery.New(node.host, nodeConfig, chanPeer, node.AddBeaconPeer)) // Register networkinfo service. "0" is the beacon shard ID @@ -130,8 +130,8 @@ func (node *Node) ServiceManagerSetup() { node.setupForNewNode() case nodeconfig.ClientNode: node.setupForClientNode() - case nodeconfig.BackupNode: - node.setupForBackupNode(true) + case nodeconfig.ArchivalNode: + node.setupForArchivalNode() } node.serviceManager.SetupServiceMessageChan(node.serviceMessageChan) } diff --git a/test/configs/oneshard1.txt b/test/configs/oneshard1.txt index 2e8297ff4..3d63a727d 100644 --- a/test/configs/oneshard1.txt +++ b/test/configs/oneshard1.txt @@ -9,4 +9,6 @@ 127.0.0.1 9008 validator 0 127.0.0.1 9009 validator 0 127.0.0.1 9010 validator 0 +127.0.0.1 9011 archival 0 127.0.0.1 19999 client 0 + diff --git a/test/deploy.sh b/test/deploy.sh index f3480ac66..a7f2504d0 100755 --- a/test/deploy.sh +++ b/test/deploy.sh @@ -148,6 +148,10 @@ while IFS='' read -r line || [[ -n "$line" ]]; do echo "launching validator ..." $DRYRUN $ROOT/bin/harmony -ip $ip -port $port -log_folder $log_folder $DB -min_peers $MIN $HMY_OPT2 $HMY_OPT3 -key /tmp/$ip-$port.key 2>&1 | tee -a $LOG_FILE & fi + if [ "$mode" == "archival" ]; then + echo "launching archival node ... wait" + $DRYRUN $ROOT/bin/harmony -ip $ip -port $port -log_folder $log_folder $DB $HMY_OPT2 -key /tmp/$ip-$port.key -is_archival 2>&1 | tee -a $LOG_FILE & + fi sleep 0.5 if [[ "$mode" == "newnode" && "$SYNC" == "true" ]]; then (( NUM_NN += 10 )) @@ -173,8 +177,8 @@ else sleep $DURATION fi -# save bc_config.json +#save bc_config.json [ -e bc_config.json ] && cp -f bc_config.json $log_folder cleanup -check_result +check_result \ No newline at end of file From d0832dbbb11350a2c0cb7c171765a4a784784455 Mon Sep 17 00:00:00 2001 From: ak Date: Tue, 19 Mar 2019 17:11:52 -0700 Subject: [PATCH 2/3] addressing leo and chao comments --- cmd/harmony/main.go | 17 +++++++---------- internal/configs/node/config.go | 2 +- node/node.go | 1 - node/node_syncing.go | 1 - test/deploy.sh | 4 ++-- 5 files changed, 10 insertions(+), 15 deletions(-) diff --git a/cmd/harmony/main.go b/cmd/harmony/main.go index fcea3a7f7..b42f8f4ba 100644 --- a/cmd/harmony/main.go +++ b/cmd/harmony/main.go @@ -8,7 +8,6 @@ import ( "path" "runtime" "strconv" - "sync" "time" "github.com/ethereum/go-ethereum/ethdb" @@ -27,11 +26,10 @@ import ( ) var ( - version string - builtBy string - builtAt string - commit string - stateMutex sync.Mutex + version string + builtBy string + builtAt string + commit string ) // InitLDBDatabase initializes a LDBDatabase. isBeacon=true will return the beacon chain database for normal shard nodes @@ -86,9 +84,9 @@ var ( keyFile = flag.String("key", "./.hmykey", "the private key file of the harmony node") // isBeacon indicates this node is a beacon chain node isBeacon = flag.Bool("is_beacon", false, "true means this node is a beacon chain node") - // isArchival indicates this node is a archival node that will save and archive current blockchain + // isArchival indicates this node is an archival node that will save and archive current blockchain isArchival = flag.Bool("is_archival", false, "true means this node is a archival node") - // isNewNode indicates this node is a new node + //isNewNode indicates this node is a new node isNewNode = flag.Bool("is_newnode", false, "true means this node is a new node") accountIndex = flag.Int("account_index", 0, "the index of the staking account to use") // isLeader indicates this node is a beacon chain leader node during the bootstrap process @@ -263,7 +261,6 @@ func main() { if *isArchival { currentNode, nodeConfig = setupArchivalNode(nodeConfig) loggingInit(*logFolder, nodeConfig.StringRole, *ip, *port, *onlyLogTps) - log.Info("New Harmony Node ====", "Role", currentNode.NodeConfig.Role(), "multiaddress", fmt.Sprintf("/ip4/%s/tcp/%s/p2p/%s", *ip, *port, nodeConfig.Host.GetID().Pretty())) go currentNode.DoBeaconSyncing() } else { // Start Profiler for leader if profile argument is on @@ -280,9 +277,9 @@ func main() { } // Init logging. loggingInit(*logFolder, nodeConfig.StringRole, *ip, *port, *onlyLogTps) - log.Info("New Harmony Node ====", "Role", currentNode.NodeConfig.Role(), "multiaddress", fmt.Sprintf("/ip4/%s/tcp/%s/p2p/%s", *ip, *port, nodeConfig.Host.GetID().Pretty())) go currentNode.SupportSyncing() } + log.Info("New Harmony Node ====", "Role", currentNode.NodeConfig.Role(), "multiaddress", fmt.Sprintf("/ip4/%s/tcp/%s/p2p/%s", *ip, *port, nodeConfig.Host.GetID().Pretty())) currentNode.ServiceManagerSetup() currentNode.RunServices() currentNode.StartServer() diff --git a/internal/configs/node/config.go b/internal/configs/node/config.go index df8106671..871f0cdd4 100644 --- a/internal/configs/node/config.go +++ b/internal/configs/node/config.go @@ -143,7 +143,7 @@ func (conf *ConfigType) SetIsLeader(b bool) { conf.isLeader = b } -// SetIsArchival set the isLeader configuration +// SetIsArchival set the isArchival configuration func (conf *ConfigType) SetIsArchival(b bool) { conf.isArchival = b } diff --git a/node/node.go b/node/node.go index 2d5fc4a62..b9cb89c0d 100644 --- a/node/node.go +++ b/node/node.go @@ -247,7 +247,6 @@ func New(host p2p.Host, consensusObj *consensus.Consensus, db ethdb.Database) *N node.BlockChannel = make(chan *types.Block) node.ConfirmedBlockChannel = make(chan *types.Block) node.BeaconBlockChannel = make(chan *types.Block) - utils.GetLogInstance().Debug("All Channels setup for ", "node", node.SelfPeer) node.TxPool = core.NewTxPool(core.DefaultTxPoolConfig, params.TestChainConfig, chain) node.Worker = worker.New(params.TestChainConfig, chain, node.Consensus, pki.GetAddressFromPublicKey(node.SelfPeer.ConsensusPubKey), node.Consensus.ShardID) diff --git a/node/node_syncing.go b/node/node_syncing.go index 2146fb753..1e0631884 100644 --- a/node/node_syncing.go +++ b/node/node_syncing.go @@ -70,7 +70,6 @@ func (node *Node) DoBeaconSyncing() { for { select { case beaconBlock := <-node.BeaconBlockChannel: - utils.GetLogInstance().Debug("[SYNC] Received BeaconBlockChannel Ping for Sync") if node.beaconSync == nil { node.beaconSync = syncing.CreateStateSync(node.SelfPeer.IP, node.SelfPeer.Port) node.beaconSync.CreateSyncConfig(node.GetBeaconSyncingPeers()) diff --git a/test/deploy.sh b/test/deploy.sh index a7f2504d0..e8777c346 100755 --- a/test/deploy.sh +++ b/test/deploy.sh @@ -177,8 +177,8 @@ else sleep $DURATION fi -#save bc_config.json +# save bc_config.json [ -e bc_config.json ] && cp -f bc_config.json $log_folder cleanup -check_result \ No newline at end of file +check_result From 5acf87ecf2b058697cee6548a80a967f52b440bf Mon Sep 17 00:00:00 2001 From: chao Date: Thu, 14 Mar 2019 16:33:43 -0700 Subject: [PATCH 3/3] add stop/restart support; enable archival config --- node/node.go | 50 ++++++++++++++++++++++++++++++++------------ node/node_genesis.go | 4 +++- test/deploy.sh | 4 ---- 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/node/node.go b/node/node.go index b9cb89c0d..9e0c8ea1c 100644 --- a/node/node.go +++ b/node/node.go @@ -5,6 +5,7 @@ import ( "crypto/ecdsa" "encoding/binary" "fmt" + "math/big" "os" "sync" "time" @@ -27,6 +28,7 @@ import ( "github.com/harmony-one/harmony/consensus" "github.com/harmony-one/harmony/core" "github.com/harmony-one/harmony/core/types" + "github.com/harmony-one/harmony/core/vm" "github.com/harmony-one/harmony/crypto/pki" "github.com/harmony-one/harmony/drand" nodeconfig "github.com/harmony-one/harmony/internal/configs/node" @@ -218,8 +220,11 @@ func (node *Node) countNumTransactionsInBlockchain() int { // New creates a new node. func New(host p2p.Host, consensusObj *consensus.Consensus, db ethdb.Database) *Node { - node := Node{} + var chain *core.BlockChain + var err error + var isFirstTime bool // if cannot get blockchain from database, then isFirstTime = true + node := Node{} if host != nil { node.host = host node.SelfPeer = host.GetSelfPeer() @@ -229,15 +234,22 @@ func New(host p2p.Host, consensusObj *consensus.Consensus, db ethdb.Database) *N // Consensus and associated channel to communicate blocks node.Consensus = consensusObj - // Init db. + // Init db database := db if database == nil { database = ethdb.NewMemDatabase() + chain, err = node.GenesisBlockSetup(database) + isFirstTime = true + } else { + chain, err = node.InitBlockChainFromDB(db, node.Consensus) + isFirstTime = false + if err != nil || chain == nil || chain.CurrentBlock().NumberU64() <= 0 { + chain, err = node.GenesisBlockSetup(database) + isFirstTime = true + } } - - chain, err := node.GenesisBlockSetup(database) if err != nil { - utils.GetLogInstance().Error("Error when doing genesis setup") + utils.GetLogInstance().Error("Error when setup blockchain", "err", err) os.Exit(1) } node.blockchain = chain @@ -250,18 +262,19 @@ func New(host p2p.Host, consensusObj *consensus.Consensus, db ethdb.Database) *N node.TxPool = core.NewTxPool(core.DefaultTxPoolConfig, params.TestChainConfig, chain) node.Worker = worker.New(params.TestChainConfig, chain, node.Consensus, pki.GetAddressFromPublicKey(node.SelfPeer.ConsensusPubKey), node.Consensus.ShardID) - utils.GetLogInstance().Debug("Created Genesis Block", "blockHash", chain.GetBlockByNumber(0).Hash().Hex()) node.Consensus.ConsensusBlock = make(chan *consensus.BFTBlockInfo) node.Consensus.VerifiedNewBlock = make(chan *types.Block) - // Setup one time smart contracts - node.AddFaucetContractToPendingTransactions() - node.CurrentStakes = make(map[common.Address]*structs.StakeInfo) - node.AddStakingContractToPendingTransactions() //This will save the latest information about staked nodes in current staked + if isFirstTime { + // Setup one time smart contracts + node.AddFaucetContractToPendingTransactions() + node.CurrentStakes = make(map[common.Address]*structs.StakeInfo) + node.AddStakingContractToPendingTransactions() //This will save the latest information about staked nodes in current staked + // TODO(minhdoan): Think of a better approach to deploy smart contract. + // This is temporary for demo purpose. + node.AddLotteryContract() - // TODO(minhdoan): Think of a better approach to deploy smart contract. - // This is temporary for demo purpose. - node.AddLotteryContract() + } } node.ContractCaller = contracts.NewContractCaller(&db, node.blockchain, params.TestChainConfig) @@ -460,3 +473,14 @@ func (node *Node) AddBeaconChainDatabase(db ethdb.Database) { node.beaconChain = chain node.BeaconWorker = worker.New(params.TestChainConfig, chain, node.Consensus, pki.GetAddressFromPublicKey(node.SelfPeer.ConsensusPubKey), node.Consensus.ShardID) } + +// InitBlockChainFromDB retrieves the latest blockchain and state available from the local database +func (node *Node) InitBlockChainFromDB(db ethdb.Database, consensus *consensus.Consensus) (*core.BlockChain, error) { + chainConfig := params.TestChainConfig + if consensus != nil { + chainConfig.ChainID = big.NewInt(int64(consensus.ShardID)) // Use ChainID as piggybacked ShardID + } + cacheConfig := core.CacheConfig{Disabled: false, TrieNodeLimit: 256 * 1024 * 1024, TrieTimeLimit: 5 * time.Minute} + chain, err := core.NewBlockChain(db, &cacheConfig, chainConfig, consensus, vm.Config{}, nil) + return chain, err +} diff --git a/node/node_genesis.go b/node/node_genesis.go index c5ffcd854..f14268e7f 100644 --- a/node/node_genesis.go +++ b/node/node_genesis.go @@ -5,6 +5,7 @@ import ( "math/big" "math/rand" "strings" + "time" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" @@ -52,7 +53,8 @@ func (node *Node) GenesisBlockSetup(db ethdb.Database) (*core.BlockChain, error) // Store genesis block into db. gspec.MustCommit(db) - return core.NewBlockChain(db, nil, gspec.Config, node.Consensus, vm.Config{}, nil) + cacheConfig := core.CacheConfig{Disabled: false, TrieNodeLimit: 256 * 1024 * 1024, TrieTimeLimit: 5 * time.Minute} + return core.NewBlockChain(db, &cacheConfig, gspec.Config, node.Consensus, vm.Config{}, nil) } // CreateGenesisAllocWithTestingAddresses create the genesis block allocation that contains deterministically diff --git a/test/deploy.sh b/test/deploy.sh index e8777c346..53a198d62 100755 --- a/test/deploy.sh +++ b/test/deploy.sh @@ -24,8 +24,6 @@ function cleanup() { echo 'Killed process: '$pid $DRYRUN kill -9 $pid 2> /dev/null done - # Remove bc_config.json before starting experiment. - rm -f bc_config.json rm -rf ./db/harmony_* } @@ -177,8 +175,6 @@ else sleep $DURATION fi -# save bc_config.json -[ -e bc_config.json ] && cp -f bc_config.json $log_folder cleanup check_result