whole network can restart and resume from previous state

pull/637/head
chao 6 years ago committed by chaosma
parent 1bedd7dc85
commit 2c6292c40e
  1. 6
      cmd/harmony/main.go
  2. 30
      internal/configs/node/config.go
  3. 18
      node/contract.go
  4. 3
      node/node.go
  5. 2
      node/node_handler.go
  6. 14
      node/service_setup.go
  7. 7
      test/configs/oneshard3.txt
  8. 10
      test/deploy.sh

@ -169,8 +169,6 @@ func createGlobalConfig() *nodeconfig.ConfigType {
if *isLeader {
nodeConfig.StringRole = "leader"
nodeConfig.Leader = nodeConfig.SelfPeer
} else if *isArchival {
nodeConfig.StringRole = "archival"
} else {
nodeConfig.StringRole = "validator"
}
@ -218,9 +216,7 @@ func setUpConsensusAndNode(nodeConfig *nodeconfig.ConfigType) (*consensus.Consen
} else {
currentNode.AddBeaconChainDatabase(nodeConfig.BeaconDB)
if *isArchival {
currentNode.NodeConfig.SetRole(nodeconfig.ArchivalNode)
} else if *isNewNode {
if *isNewNode {
currentNode.NodeConfig.SetRole(nodeconfig.NewNode)
} else if nodeConfig.StringRole == "leader" {
currentNode.NodeConfig.SetRole(nodeconfig.ShardLeader)

@ -27,7 +27,6 @@ const (
NewNode
ClientNode
WalletNode
ArchivalNode
)
func (role Role) String() string {
@ -48,8 +47,6 @@ func (role Role) String() string {
return "ClientNode"
case WalletNode:
return "WalletNode"
case ArchivalNode:
return "ArchivalNode"
}
return "Unknown"
}
@ -63,15 +60,14 @@ 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
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
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
StringRole string
Host p2p.Host
@ -142,11 +138,6 @@ func (conf *ConfigType) SetIsLeader(b bool) {
conf.isLeader = b
}
// SetIsArchival set the isArchival configuration
func (conf *ConfigType) SetIsArchival(b bool) {
conf.isArchival = b
}
// SetShardID set the ShardID
func (conf *ConfigType) SetShardID(s uint32) {
conf.ShardID = s
@ -187,11 +178,6 @@ func (conf *ConfigType) IsLeader() bool {
return conf.isLeader
}
// IsArchival returns the isArchival configuration
func (conf *ConfigType) IsArchival() bool {
return conf.isArchival
}
// Role returns the role
func (conf *ConfigType) Role() Role {
return conf.role

@ -1,6 +1,7 @@
package node
import (
"crypto/ecdsa"
"math/big"
"strings"
@ -171,3 +172,20 @@ func (node *Node) callGetFreeTokenWithNonce(address common.Address, nonce uint64
node.addPendingTransactions(types.Transactions{tx})
return tx.Hash()
}
// AddContractKeyAndAddress is used to add smart contract related information when node restart and resume with previous state
func (node *Node) AddContractKeyAndAddress() {
// faucet 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"))
node.ContractDeployerKey = contractDeployerKey
node.ContractAddresses = append(node.ContractAddresses, crypto.CreateAddress(crypto.PubkeyToAddress(contractDeployerKey.PublicKey), uint64(0)))
// staking contract
node.CurrentStakes = make(map[common.Address]*structs.StakeInfo)
stakingPrivKey := contract_constants.GenesisBeaconAccountPriKey
node.StakingContractAddress = crypto.CreateAddress(crypto.PubkeyToAddress(stakingPrivKey.PublicKey), uint64(0))
// lottery
lotteryPriKey, _ := crypto.HexToECDSA(contract_constants.DemoAccounts[0].Private)
node.DemoContractAddress = crypto.CreateAddress(crypto.PubkeyToAddress(lotteryPriKey.PublicKey), uint64(0))
}

@ -272,7 +272,8 @@ func New(host p2p.Host, consensusObj *consensus.Consensus, db ethdb.Database, is
// TODO(minhdoan): Think of a better approach to deploy smart contract.
// This is temporary for demo purpose.
node.AddLotteryContract()
} else {
node.AddContractKeyAndAddress()
}
}

@ -143,7 +143,7 @@ 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 || role == nodeconfig.ArchivalNode) {
if proto_node.BlockMessageType(msgPayload[0]) == proto_node.Sync && (role == nodeconfig.ShardValidator || role == nodeconfig.ShardLeader || role == nodeconfig.NewNode) {
utils.GetLogInstance().Info("Block being handled by block channel", "self peer", node.SelfPeer)
for _, block := range blocks {
node.BeaconBlockChannel <- block

@ -106,18 +106,6 @@ func (node *Node) setupForClientNode() {
node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, p2p.GroupIDBeacon, chanPeer, nil))
}
// archival node act as normal validator and will backup data for its own shard; currently, just we just have beacon shard archival
func (node *Node) setupForArchivalNode() {
nodeConfig, chanPeer := node.initNodeConfiguration(true, false) // (false,_) will backup for normal shard
// Register client support service.
node.serviceManager.RegisterService(service.ClientSupport, clientsupport.New(node.blockchain.State, node.CallFaucetContract, node.getDeployedStakingContract, node.SelfPeer.IP, node.SelfPeer.Port))
// Register peer discovery service. No need to do staking for beacon chain node.
node.serviceManager.RegisterService(service.PeerDiscovery, discovery.New(node.host, nodeConfig, chanPeer, nil))
// Register networkinfo service.
node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, p2p.GroupIDBeacon, chanPeer, nil))
}
// ServiceManagerSetup setups service store.
func (node *Node) ServiceManagerSetup() {
node.serviceManager = &service.Manager{}
@ -135,8 +123,6 @@ func (node *Node) ServiceManagerSetup() {
node.setupForNewNode()
case nodeconfig.ClientNode:
node.setupForClientNode()
case nodeconfig.ArchivalNode:
node.setupForArchivalNode()
}
node.serviceManager.SetupServiceMessageChan(node.serviceMessageChan)
}

@ -0,0 +1,7 @@
127.0.0.1 9000 leader_archival 0
127.0.0.1 9001 archival 0
127.0.0.1 9002 archival 0
127.0.0.1 9003 archival 0
127.0.0.1 9004 archival 0
127.0.0.1 9009 archival 0
127.0.0.1 19999 client 0

@ -70,7 +70,7 @@ EOU
DB=
TXGEN=true
DURATION=90
DURATION=9000
MIN=5
SHARDS=2
KILLPORT=9004
@ -99,7 +99,7 @@ if [ -z "$config" ]; then
fi
if [ "$SYNC" == "true" ]; then
DURATION=300
DURATION=9000
fi
# Kill nodes if any
@ -144,13 +144,17 @@ while IFS='' read -r line || [[ -n "$line" ]]; do
echo "launching leader ..."
$DRYRUN $ROOT/bin/harmony -ip $ip -port $port -log_folder $log_folder $DB -account_index $i -min_peers $MIN $HMY_OPT2 $HMY_OPT3 -key /tmp/$ip-$port.key -is_leader 2>&1 | tee -a $LOG_FILE &
fi
if [ "$mode" == "leader_archival" ]; then
echo "launching leader ..."
$DRYRUN $ROOT/bin/harmony -ip $ip -port $port -log_folder $log_folder $DB -account_index $i -min_peers $MIN $HMY_OPT2 $HMY_OPT3 -key /tmp/$ip-$port.key -is_leader -is_archival 2>&1 | tee -a $LOG_FILE &
fi
if [ "$mode" == "validator" ]; then
echo "launching validator ..."
$DRYRUN $ROOT/bin/harmony -ip $ip -port $port -log_folder $log_folder $DB -account_index $i -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 -account_index $i $HMY_OPT2 -key /tmp/$ip-$port.key -is_archival 2>&1 | tee -a $LOG_FILE &
$DRYRUN $ROOT/bin/harmony -ip $ip -port $port -log_folder $log_folder $DB -account_index $i -min_peers $MIN $HMY_OPT2 $HMY_OPT3 -key /tmp/$ip-$port.key -is_archival 2>&1 | tee -a $LOG_FILE &
fi
sleep 0.5
if [[ "$mode" == "newnode" && "$SYNC" == "true" ]]; then

Loading…
Cancel
Save