rebase code

pull/3814/head
Lutty 3 years ago committed by Leo Chen
parent 1c363c73f4
commit faacc7c091
  1. 9
      cmd/harmony/flags.go
  2. 26
      cmd/harmony/main.go
  3. 6
      consensus/consensus.go
  4. 15
      consensus/consensus_service.go
  5. 11
      consensus/consensus_v2.go
  6. 3
      consensus/enums.go
  7. 11
      consensus/validator.go
  8. 13
      consensus/view_change.go
  9. 2
      hmy/hmy.go
  10. 1
      internal/configs/harmony/harmony.go
  11. 16
      node/api.go
  12. 8
      rpc/blockchain.go

@ -236,6 +236,11 @@ var (
Usage: "run node in offline mode",
DefValue: defaultConfig.General.IsOffline,
}
isBackupFlag = cli.BoolFlag{
Name: "run.backup",
Usage: "run node in backup mode",
DefValue: defaultConfig.General.IsBackup,
}
dataDirFlag = cli.StringFlag{
Name: "datadir",
Usage: "directory of chain database",
@ -342,6 +347,10 @@ func applyGeneralFlags(cmd *cobra.Command, config *harmonyconfig.HarmonyConfig)
if cli.IsFlagChanged(cmd, isOfflineFlag) {
config.General.IsOffline = cli.GetBoolFlagValue(cmd, isOfflineFlag)
}
if cli.IsFlagChanged(cmd, isBackupFlag) {
config.General.IsBackup = cli.GetBoolFlagValue(cmd, isBackupFlag)
}
}
// network flags

@ -686,15 +686,7 @@ func setupConsensusAndNode(hc harmonyconfig.HarmonyConfig, nodeConfig *nodeconfi
)
nodeconfig.GetDefaultConfig().DBDir = nodeConfig.DBDir
switch hc.General.NodeType {
case nodeTypeExplorer:
nodeconfig.SetDefaultRole(nodeconfig.ExplorerNode)
currentNode.NodeConfig.SetRole(nodeconfig.ExplorerNode)
case nodeTypeValidator:
nodeconfig.SetDefaultRole(nodeconfig.Validator)
currentNode.NodeConfig.SetRole(nodeconfig.Validator)
}
processNodeType(hc, currentNode, currentConsensus)
currentNode.NodeConfig.SetShardGroupID(nodeconfig.NewGroupIDByShardID(nodeconfig.ShardID(nodeConfig.ShardID)))
currentNode.NodeConfig.SetClientGroupID(nodeconfig.NewClientGroupIDByShardID(shard.BeaconChainShardID))
currentNode.NodeConfig.ConsensusPriKey = nodeConfig.ConsensusPriKey
@ -723,6 +715,22 @@ func setupConsensusAndNode(hc harmonyconfig.HarmonyConfig, nodeConfig *nodeconfi
return currentNode
}
func processNodeType(hc harmonyconfig.HarmonyConfig, currentNode *node.Node, currentConsensus *consensus.Consensus) {
switch hc.General.NodeType {
case nodeTypeExplorer:
nodeconfig.SetDefaultRole(nodeconfig.ExplorerNode)
currentNode.NodeConfig.SetRole(nodeconfig.ExplorerNode)
case nodeTypeValidator:
nodeconfig.SetDefaultRole(nodeconfig.Validator)
currentNode.NodeConfig.SetRole(nodeconfig.Validator)
if hc.General.IsBackup {
currentConsensus.SetIsBackup(true)
}
}
}
func setupPrometheusService(node *node.Node, hc harmonyconfig.HarmonyConfig, sid uint32) {
prometheusConfig := prometheus.Config{
Enabled: hc.Prometheus.Enabled,

@ -48,6 +48,8 @@ type Consensus struct {
phase FBFTPhase
// current indicates what state a node is in
current State
// isBackup declarative the node is in backup mode
isBackup bool
// 2 types of timeouts: normal and viewchange
consensusTimeout map[TimeoutType]*utils.Timeout
// Commits collected from validators.
@ -185,6 +187,10 @@ func (consensus *Consensus) SetBlockVerifier(verifier VerifyBlockFunc) {
consensus.vc.SetVerifyBlock(consensus.VerifyBlock)
}
func (consensus *Consensus) IsBackup() bool {
return consensus.isBackup
}
// New create a new Consensus record
func New(
host p2p.Host, shard uint32, leader p2p.Peer, multiBLSPriKey multibls.PrivateKeys,

@ -173,12 +173,25 @@ func (consensus *Consensus) IsValidatorInCommittee(pubKey bls.SerializedPublicKe
// SetMode sets the mode of consensus
func (consensus *Consensus) SetMode(m Mode) {
if m == Normal && consensus.isBackup {
m = NormalBackup
}
consensus.getLogger().Debug().
Str("Mode", m.String()).
Msg("[SetMode]")
consensus.current.SetMode(m)
}
// SetIsBackup sets the mode of consensus
func (consensus *Consensus) SetIsBackup(isBackup bool) {
consensus.getLogger().Debug().
Bool("IsBackup", isBackup).
Msg("[SetIsBackup]")
consensus.isBackup = isBackup
consensus.current.SetIsBackup(isBackup)
}
// Mode returns the mode of consensus
func (consensus *Consensus) Mode() Mode {
return consensus.current.Mode()
@ -200,7 +213,7 @@ func (consensus *Consensus) checkViewID(msg *FBFTMessage) error {
if consensus.IgnoreViewIDCheck.IsSet() {
//in syncing mode, node accepts incoming messages without viewID/leaderKey checking
//so only set mode to normal when new node enters consensus and need checking viewID
consensus.current.SetMode(Normal)
consensus.SetMode(Normal)
consensus.SetViewIDs(msg.ViewID)
if !msg.HasSingleSender() {
return errors.New("Leader message can not have multiple sender keys")

@ -92,10 +92,17 @@ func (consensus *Consensus) HandleMessageUpdate(ctx context.Context, msg *msg_pb
return errors.Wrapf(err, "unable to parse consensus msg with type: %s", msg.Type)
}
canHandleViewChange := true
intendedForValidator, intendedForLeader :=
!consensus.IsLeader(),
consensus.IsLeader()
// if in backup normal mode, force ignore view change event and leader event.
if consensus.current.Mode() == NormalBackup {
canHandleViewChange = false
intendedForLeader = false
}
// Route message to handler
switch t := msg.Type; true {
// Handle validator intended messages first
@ -113,9 +120,9 @@ func (consensus *Consensus) HandleMessageUpdate(ctx context.Context, msg *msg_pb
consensus.onCommit(fbftMsg)
// Handle view change messages
case t == msg_pb.MessageType_VIEWCHANGE:
case t == msg_pb.MessageType_VIEWCHANGE && canHandleViewChange:
consensus.onViewChange(fbftMsg)
case t == msg_pb.MessageType_NEWVIEW:
case t == msg_pb.MessageType_NEWVIEW && canHandleViewChange:
consensus.onNewView(fbftMsg)
}

@ -14,6 +14,8 @@ const (
Syncing
// Listening ..
Listening
// NormalBackup Backup Node ..
NormalBackup
)
// FBFTPhase : different phases of consensus
@ -32,6 +34,7 @@ var (
ViewChanging: "ViewChanging",
Syncing: "Syncing",
Listening: "Listening",
NormalBackup: "NormalBackup",
}
phaseNames = map[FBFTPhase]string{
FBFTAnnounce: "Announce",

@ -57,9 +57,14 @@ func (consensus *Consensus) onAnnounce(msg *msg_pb.Message) {
return
}
consensus.prepare()
consensus.switchPhase("Announce", FBFTPrepare)
}
func (consensus *Consensus) prepare() {
if consensus.IsBackup() {
return
}
priKeys := consensus.getPriKeysInCommittee()
p2pMsgs := consensus.constructP2pMessages(msg_pb.MessageType_PREPARE, nil, priKeys)
@ -71,12 +76,14 @@ func (consensus *Consensus) prepare() {
Str("blockHash", hex.EncodeToString(consensus.blockHash[:])).
Msg("[OnAnnounce] Sent Prepare Message!!")
}
consensus.switchPhase("Announce", FBFTPrepare)
}
// sendCommitMessages send out commit messages to leader
func (consensus *Consensus) sendCommitMessages(blockObj *types.Block) {
if consensus.IsBackup() {
return
}
priKeys := consensus.getPriKeysInCommittee()
// Sign commit signature on the received block and construct the p2p messages

@ -37,7 +37,8 @@ type State struct {
// it is the next view id
viewChangingID uint64
viewMux sync.RWMutex
viewMux sync.RWMutex
isBackup bool
}
// Mode return the current node mode
@ -49,6 +50,10 @@ func (pm *State) Mode() Mode {
// SetMode set the node mode as required
func (pm *State) SetMode(s Mode) {
if s == Normal && pm.isBackup {
s = NormalBackup
}
pm.modeMux.Lock()
defer pm.modeMux.Unlock()
pm.mode = s
@ -95,6 +100,10 @@ func (pm *State) GetViewChangeDuraion() time.Duration {
return time.Duration(diff * diff * int64(viewChangeDuration))
}
func (pm *State) SetIsBackup(isBackup bool) {
pm.isBackup = isBackup
}
// fallbackNextViewID return the next view ID and duration when there is an exception
// to calculate the time-based viewId
func (consensus *Consensus) fallbackNextViewID() (uint64, time.Duration) {
@ -229,7 +238,7 @@ func createTimeout() map[TimeoutType]*utils.Timeout {
// startViewChange start the view change process
func (consensus *Consensus) startViewChange() {
if consensus.disableViewChange {
if consensus.disableViewChange || consensus.IsBackup() {
return
}
consensus.mutex.Lock()

@ -100,6 +100,8 @@ type NodeAPI interface {
ListBlockedPeer() []peer.ID
GetConsensusInternal() commonRPC.ConsensusInternal
IsBackup() bool
SetNodeBackupMode(isBackup bool) bool
// debug API
GetConsensusMode() string

@ -56,6 +56,7 @@ type GeneralConfig struct {
NoStaking bool
ShardID int
IsArchival bool
IsBackup bool
IsBeaconArchival bool
IsOffline bool
DataDir string

@ -143,6 +143,22 @@ func (node *Node) GetConsensusInternal() rpc_common.ConsensusInternal {
}
}
// IsBackup returns the node is in backup mode
func (node *Node) IsBackup() bool {
return node.Consensus.IsBackup()
}
// SetNodeBackupMode change node backup mode
func (node *Node) SetNodeBackupMode(isBackup bool) bool {
if node.Consensus.IsBackup() == isBackup {
return false
}
node.Consensus.SetIsBackup(isBackup)
node.Consensus.ResetViewChangeState()
return true
}
func (node *Node) GetConfig() rpc_common.Config {
return rpc_common.Config{
HarmonyConfig: *node.HarmonyConfig,

@ -838,6 +838,14 @@ func isBlockGreaterThanLatest(hmy *hmy.Harmony, blockNum rpc.BlockNumber) bool {
return uint64(blockNum) > hmy.CurrentBlock().NumberU64()
}
func (s *PublicBlockchainService) GetCurrentNodeBackupState(ctx context.Context) (bool, error) {
return s.hmy.NodeAPI.IsBackup(), nil
}
func (s *PublicBlockchainService) SetNodeToBackupMode(ctx context.Context, isBackup bool) (bool, error) {
return s.hmy.NodeAPI.SetNodeBackupMode(isBackup), nil
}
func combineCacheKey(number uint64, version Version, blockArgs *rpc_common.BlockArgs) string {
// no need format blockArgs.Signers[] as a part of cache key
// because it's not input from rpc caller, it's caculate with blockArgs.WithSigners

Loading…
Cancel
Save