modify the naming of variables to make code cleaner

address pr comments
pull/881/head
chao 6 years ago committed by chaosma
parent 69cfe9b633
commit 7cd24dae6c
  1. 140
      api/proto/message/message.pb.go
  2. 8
      api/proto/message/message.proto
  3. 2
      cmd/harmony/main.go
  4. 8
      consensus/README.md
  5. 9
      consensus/config.go
  6. 22
      consensus/consensus.go
  7. 6
      consensus/consensus_leader.go
  8. 42
      consensus/consensus_service.go
  9. 14
      consensus/consensus_service_test.go
  10. 4
      consensus/consensus_test.go
  11. 99
      consensus/consensus_v2.go
  12. 20
      consensus/consensus_validator.go
  13. 12
      consensus/consensus_viewchange_msg.go
  14. 4
      consensus/engine/errors.go
  15. 48
      consensus/pbft_log.go
  16. 4
      consensus/pbft_log_test.go
  17. 56
      consensus/view_change.go
  18. 20
      internal/attack/attack.go
  19. 4
      internal/attack/attack_test.go
  20. 2
      node/node.go
  21. 2
      node/node_handler.go
  22. 2
      node/node_syncing.go
  23. 6
      test/deploy.sh

@ -521,8 +521,8 @@ func (m *StakingRequest) GetNodeId() string {
}
type ConsensusRequest struct {
ConsensusId uint32 `protobuf:"varint,1,opt,name=consensus_id,json=consensusId,proto3" json:"consensus_id,omitempty"`
SeqNum uint64 `protobuf:"varint,2,opt,name=seq_num,json=seqNum,proto3" json:"seq_num,omitempty"`
ViewId uint32 `protobuf:"varint,1,opt,name=view_id,json=viewId,proto3" json:"view_id,omitempty"`
BlockNum uint64 `protobuf:"varint,2,opt,name=block_num,json=blockNum,proto3" json:"block_num,omitempty"`
BlockHash []byte `protobuf:"bytes,3,opt,name=block_hash,json=blockHash,proto3" json:"block_hash,omitempty"`
SenderPubkey []byte `protobuf:"bytes,4,opt,name=sender_pubkey,json=senderPubkey,proto3" json:"sender_pubkey,omitempty"`
Payload []byte `protobuf:"bytes,5,opt,name=payload,proto3" json:"payload,omitempty"`
@ -556,16 +556,16 @@ func (m *ConsensusRequest) XXX_DiscardUnknown() {
var xxx_messageInfo_ConsensusRequest proto.InternalMessageInfo
func (m *ConsensusRequest) GetConsensusId() uint32 {
func (m *ConsensusRequest) GetViewId() uint32 {
if m != nil {
return m.ConsensusId
return m.ViewId
}
return 0
}
func (m *ConsensusRequest) GetSeqNum() uint64 {
func (m *ConsensusRequest) GetBlockNum() uint64 {
if m != nil {
return m.SeqNum
return m.BlockNum
}
return 0
}
@ -647,8 +647,8 @@ func (m *DrandRequest) GetPayload() []byte {
}
type ViewChangeRequest struct {
ConsensusId uint32 `protobuf:"varint,1,opt,name=consensus_id,json=consensusId,proto3" json:"consensus_id,omitempty"`
SeqNum uint64 `protobuf:"varint,2,opt,name=seq_num,json=seqNum,proto3" json:"seq_num,omitempty"`
ViewId uint32 `protobuf:"varint,1,opt,name=view_id,json=viewId,proto3" json:"view_id,omitempty"`
BlockNum uint64 `protobuf:"varint,2,opt,name=block_num,json=blockNum,proto3" json:"block_num,omitempty"`
SenderPubkey []byte `protobuf:"bytes,3,opt,name=sender_pubkey,json=senderPubkey,proto3" json:"sender_pubkey,omitempty"`
LeaderPubkey []byte `protobuf:"bytes,4,opt,name=leader_pubkey,json=leaderPubkey,proto3" json:"leader_pubkey,omitempty"`
Payload []byte `protobuf:"bytes,5,opt,name=payload,proto3" json:"payload,omitempty"`
@ -688,16 +688,16 @@ func (m *ViewChangeRequest) XXX_DiscardUnknown() {
var xxx_messageInfo_ViewChangeRequest proto.InternalMessageInfo
func (m *ViewChangeRequest) GetConsensusId() uint32 {
func (m *ViewChangeRequest) GetViewId() uint32 {
if m != nil {
return m.ConsensusId
return m.ViewId
}
return 0
}
func (m *ViewChangeRequest) GetSeqNum() uint64 {
func (m *ViewChangeRequest) GetBlockNum() uint64 {
if m != nil {
return m.SeqNum
return m.BlockNum
}
return 0
}
@ -775,64 +775,64 @@ func init() {
func init() { proto.RegisterFile("message.proto", fileDescriptor_33c57e4bae7b9afd) }
var fileDescriptor_33c57e4bae7b9afd = []byte{
// 907 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00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0xf0, 0x05, 0xbc, 0xb4, 0xc8,
0xbd, 0x35, 0x1a, 0x10, 0xa7, 0x4f, 0x7a, 0xc6, 0xc8, 0x72, 0xf2, 0x54, 0x25, 0xac, 0x41, 0xbd,
0x67, 0x59, 0xa3, 0xa9, 0x65, 0x10, 0x24, 0x25, 0xa7, 0x8c, 0x29, 0x19, 0xf7, 0x28, 0x41, 0xe5,
0xc4, 0x95, 0x19, 0x03, 0x54, 0x49, 0x46, 0xcf, 0x18, 0xdd, 0xdd, 0x99, 0x36, 0xaa, 0xa6, 0xb5,
0x25, 0xbf, 0x6d, 0x32, 0x40, 0x67, 0xb8, 0x09, 0xf0, 0xc1, 0x24, 0xf7, 0xc6, 0xb0, 0x67, 0x5d,
0x13, 0x54, 0x4b, 0xb2, 0x58, 0xe4, 0x3e, 0x91, 0x90, 0x9c, 0x38, 0x45, 0xad, 0x8e, 0x69, 0x99,
0x36, 0x02, 0x8c, 0x40, 0x4b, 0xed, 0x2c, 0x9b, 0x8a, 0x3f, 0x83, 0xf3, 0xdb, 0x91, 0x6d, 0x13,
0xfa, 0x8b, 0x43, 0xc9, 0xcf, 0x53, 0x32, 0xb1, 0x91, 0xd6, 0xed, 0x41, 0xc3, 0xf0, 0x3d, 0x16,
0xc4, 0x59, 0x4f, 0xf0, 0x3b, 0x90, 0xc7, 0x51, 0x38, 0x67, 0x9c, 0x63, 0x74, 0xbc, 0x4e, 0x2f,
0x5e, 0xec, 0x94, 0x7c, 0xe3, 0xb5, 0x4b, 0xb3, 0x9a, 0xf8, 0x4b, 0x7e, 0xff, 0x77, 0x00, 0x00,
0x00, 0xff, 0xff, 0x18, 0x64, 0xa1, 0x49, 0xa3, 0x07, 0x00, 0x00,
// 903 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xbc, 0x95, 0xcd, 0x8e, 0xe2, 0x46,
0x10, 0xc7, 0x31, 0x30, 0x18, 0x97, 0x0d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61, 0x0d, 0xea, 0x7d, 0xcb, 0x1a, 0xcf,
0x2c, 0x83, 0x20, 0x29, 0x39, 0x65, 0x42, 0xc9, 0xa4, 0x4f, 0x09, 0x2a, 0x27, 0xae, 0xcc, 0x18,
0xa2, 0x4a, 0x32, 0x6e, 0xc6, 0xf8, 0xfe, 0xde, 0xb4, 0x51, 0x35, 0xad, 0x2d, 0xf9, 0x6d, 0x93,
0x21, 0xba, 0xc0, 0x4d, 0x80, 0x0f, 0x26, 0x79, 0x30, 0x46, 0x7d, 0xeb, 0x86, 0xa0, 0x5a, 0x92,
0xc5, 0x22, 0x0f, 0x89, 0x84, 0xe4, 0xc4, 0x29, 0x6a, 0x75, 0x4c, 0xcb, 0xb4, 0x11, 0x60, 0x04,
0x5a, 0x6a, 0x67, 0xd9, 0x54, 0xfc, 0x19, 0xb4, 0xee, 0xc6, 0xb6, 0x4d, 0xe8, 0xcf, 0x0e, 0x25,
0x3f, 0xcd, 0xc8, 0xd4, 0x46, 0x5a, 0xaf, 0x0f, 0x0d, 0xc3, 0xf7, 0x58, 0x10, 0x67, 0x3d, 0xc1,
0xef, 0x40, 0x9e, 0x44, 0xe1, 0x82, 0x71, 0x8e, 0xd1, 0xe9, 0x0a, 0xbd, 0x7c, 0xb1, 0x57, 0xf2,
0x2d, 0xd7, 0x29, 0xcd, 0x6b, 0xe2, 0x6f, 0xf8, 0xfd, 0xdf, 0x01, 0x00, 0x00, 0xff, 0xff, 0xc2,
0x7b, 0x2c, 0xea, 0x97, 0x07, 0x00, 0x00,
}
// Reference imports to suppress errors if they are not otherwise used.

@ -80,8 +80,8 @@ message StakingRequest {
}
message ConsensusRequest {
uint32 consensus_id = 1;
uint64 seq_num = 2;
uint32 view_id = 1;
uint64 block_num = 2;
bytes block_hash = 3;
bytes sender_pubkey = 4;
bytes payload = 5;
@ -94,8 +94,8 @@ message DrandRequest {
}
message ViewChangeRequest {
uint32 consensus_id = 1;
uint64 seq_num = 2;
uint32 view_id = 1;
uint64 block_num = 2;
bytes sender_pubkey = 3;
bytes leader_pubkey = 4;
bytes payload = 5; // message payload: either m1 type or m2 type

@ -360,7 +360,7 @@ func setUpConsensusAndNode(nodeConfig *nodeconfig.ConfigType) *node.Node {
// Set the consensus ID to be the current block number
height := currentNode.Blockchain().CurrentBlock().NumberU64()
currentConsensus.SetConsensusID(uint32(height))
currentConsensus.SetViewID(uint32(height))
utils.GetLogInstance().Info("Init Blockchain", "height", height)
// Assign closure functions to the consensus object

@ -58,17 +58,17 @@ func (consensus *Consensus) Start(stopChan chan struct{}, stoppedChan chan struc
msg := consensus.recvWithTimeout(receiveTimeout)
consensus.handleMessageUpdate(msg)
if consensus.idleTimeout.CheckExpire() {
consensus.startViewChange(consensus.consensusID + 1)
consensus.startViewChange(consensus.viewID + 1)
}
if consensus.commitTimeout.CheckExpire() {
consensus.startViewChange(consensus.consensusID + 1)
consensus.startViewChange(consensus.viewID + 1)
}
if consensus.viewChangeTimeout.CheckExpire() {
if consensus.mode.Mode() == Normal {
continue
}
consensusID := consensus.mode.ConsensusID()
consensus.startViewChange(consensusID + 1)
viewID := consensus.mode.ViewID()
consensus.startViewChange(viewID + 1)
}
case <-tick.C:
consensus.tryPublishBlock()

@ -17,5 +17,12 @@ const (
maxLogSize uint32 = 1000
)
// NIL is the m2 type message
const (
timeoutNormal = "normal"
timeoutViewChange = "viewchange"
)
// NIL is the m2 type message, which suppose to be nil/empty, however
// we cannot sign on empty message, instead we sign on some default "nil" message
// to indicate there is no prepared message received when we start view change
var NIL = []byte{0x01}

@ -43,8 +43,8 @@ type Consensus struct {
phase PbftPhase
// mode: indicate a node is in normal or viewchanging mode
mode PbftMode
// seqNum: the next blockNumber that PBFT is going to agree on, should be equal to the blockNumber of next block
seqNum uint64
// blockNum: the next blockNumber that PBFT is going to agree on, should be equal to the blockNumber of next block
blockNum uint64
// channel to receive consensus message
MsgChan chan []byte
@ -62,8 +62,8 @@ type Consensus struct {
commitBitmap *bls_cosi.Mask
// Commits collected from view change
bhpSigs map[common.Address]*bls.Sign // signature on m1 type message
nilSigs map[common.Address]*bls.Sign // signature on m2 type (i.e. nil) messages
bhpSigs map[common.Address]*bls.Sign // bhpSigs: blockHashPreparedSigs is the signature on m1 type message
nilSigs map[common.Address]*bls.Sign // nilSigs: there is no prepared message when view change, it's signature on m2 type (i.e. nil) messages
aggregatedBHPSig *bls.Sign
aggregatedNILSig *bls.Sign
bhpBitmap *bls_cosi.Mask
@ -99,7 +99,7 @@ type Consensus struct {
// Leader or validator address in hex
SelfAddress common.Address
// Consensus Id (View Id) - 4 byte
consensusID uint32 // TODO(chao): change it to uint64 or add overflow checking mechanism
viewID uint32 // TODO(chao): change it to uint64 or add overflow checking mechanism
// Blockhash - 32 byte
blockHash [32]byte
@ -109,8 +109,8 @@ type Consensus struct {
blockHashes [][32]byte
// Shard Id which this node belongs to
ShardID uint32
// whether to ignore consensusID check
ignoreConsensusIDCheck bool
// whether to ignore viewID check
ignoreViewIDCheck bool
// global consensus mutex
mutex sync.Mutex
@ -131,7 +131,7 @@ type Consensus struct {
VerifiedNewBlock chan *types.Block
// will trigger state syncing when consensus ID is low
ConsensusIDLowChan chan struct{}
ViewIDLowChan chan struct{}
// Channel for DRG protocol to send pRnd (preimage of randomness resulting from combined vrf randomnesses) to consensus. The first 32 bytes are randomness, the rest is for bitmap.
PRndChannel chan []byte
@ -189,7 +189,7 @@ type BlockConsensusStatus struct {
func New(host p2p.Host, ShardID uint32, leader p2p.Peer, blsPriKey *bls.SecretKey) (*Consensus, error) {
consensus := Consensus{}
consensus.host = host
consensus.ConsensusIDLowChan = make(chan struct{})
consensus.ViewIDLowChan = make(chan struct{})
consensus.ConsensusVersion = ConsensusVersion
// pbft related
@ -221,9 +221,9 @@ func New(host p2p.Host, ShardID uint32, leader p2p.Peer, blsPriKey *bls.SecretKe
consensus.PubKey = blsPriKey.GetPublicKey()
}
// consensusID has to be initialized as the height of the blockchain during initialization
// viewID has to be initialized as the height of the blockchain during initialization
// as it was displayed on explorer as Height right now
consensus.consensusID = 0
consensus.viewID = 0
consensus.ShardID = ShardID
consensus.MsgChan = make(chan []byte)

@ -316,14 +316,14 @@ func (consensus *Consensus) processCommitMessage(message *msg_pb.Message) {
consensus.reportMetrics(blockObj)
// Dump new block into level db.
explorer.GetStorageInstance(consensus.leader.IP, consensus.leader.Port, true).Dump(&blockObj, consensus.consensusID)
explorer.GetStorageInstance(consensus.leader.IP, consensus.leader.Port, true).Dump(&blockObj, consensus.viewID)
// Reset state to Finished, and clear other data.
consensus.ResetState()
consensus.consensusID++
consensus.viewID++
consensus.OnConsensusDone(&blockObj)
utils.GetLogInstance().Debug("HOORAY!!!!!!! CONSENSUS REACHED!!!!!!!", "consensusID", consensus.consensusID, "numOfSignatures", len(commitSigs))
utils.GetLogInstance().Debug("HOORAY!!!!!!! CONSENSUS REACHED!!!!!!!", "viewID", consensus.viewID, "numOfSignatures", len(commitSigs))
// TODO: remove this temporary delay
time.Sleep(500 * time.Millisecond)

@ -91,8 +91,8 @@ func (consensus *Consensus) GetSelfAddress() common.Address {
// Populates the common basic fields for all consensus message.
func (consensus *Consensus) populateMessageFields(request *msg_pb.ConsensusRequest) {
request.ConsensusId = consensus.consensusID
request.SeqNum = consensus.seqNum
request.ViewId = consensus.viewID
request.BlockNum = consensus.blockNum
// 32 byte block hash
request.BlockHash = consensus.blockHash[:]
@ -100,7 +100,7 @@ func (consensus *Consensus) populateMessageFields(request *msg_pb.ConsensusReque
// sender address
request.SenderPubkey = consensus.PubKey.Serialize()
utils.GetLogInstance().Debug("[populateMessageFields]", "myConsensusID", consensus.consensusID, "SenderAddress", consensus.SelfAddress, "seqNum", consensus.seqNum)
utils.GetLogInstance().Debug("[populateMessageFields]", "myViewID", consensus.viewID, "SenderAddress", consensus.SelfAddress, "blockNum", consensus.blockNum)
}
// Signs the consensus message and returns the marshaled message.
@ -142,9 +142,9 @@ func (consensus *Consensus) GetNodeIDs() []libp2p_peer.ID {
return nodes
}
// GetConsensusID returns the consensus ID
func (consensus *Consensus) GetConsensusID() uint32 {
return consensus.consensusID
// GetViewID returns the consensus ID
func (consensus *Consensus) GetViewID() uint32 {
return consensus.viewID
}
// DebugPrintPublicKeys print all the PublicKeys in string format in Consensus
@ -427,11 +427,11 @@ func (consensus *Consensus) RemovePeers(peers []p2p.Peer) int {
return count2
}
// ToggleConsensusCheck flip the flag of whether ignore consensusID check during consensus process
// ToggleConsensusCheck flip the flag of whether ignore viewID check during consensus process
func (consensus *Consensus) ToggleConsensusCheck() {
consensus.mutex.Lock()
defer consensus.mutex.Unlock()
consensus.ignoreConsensusIDCheck = !consensus.ignoreConsensusIDCheck
consensus.ignoreViewIDCheck = !consensus.ignoreViewIDCheck
}
// GetPeerByAddress the validator peer based on validator Address.
@ -509,9 +509,9 @@ func (consensus *Consensus) verifyViewChangeSenderKey(msg *msg_pb.Message) (*bls
return senderKey, senderAddr, nil
}
// SetConsensusID set the consensusID to the height of the blockchain
func (consensus *Consensus) SetConsensusID(height uint32) {
consensus.consensusID = height
// SetViewID set the viewID to the height of the blockchain
func (consensus *Consensus) SetViewID(height uint32) {
consensus.viewID = height
}
// RegisterPRndChannel registers the channel for receiving randomness preimage from DRG protocol
@ -527,7 +527,7 @@ func (consensus *Consensus) RegisterRndChannel(rndChannel chan [64]byte) {
// Checks the basic meta of a consensus message, including the signature.
func (consensus *Consensus) checkConsensusMessage(message *msg_pb.Message, publicKey *bls.PublicKey) error {
consensusMsg := message.GetConsensus()
consensusID := consensusMsg.ConsensusId
viewID := consensusMsg.ViewId
blockHash := consensusMsg.BlockHash
// Verify message signature
@ -542,26 +542,26 @@ func (consensus *Consensus) checkConsensusMessage(message *msg_pb.Message, publi
}
// just ignore consensus check for the first time when node join
if consensus.ignoreConsensusIDCheck {
consensus.consensusID = consensusID
if consensus.ignoreViewIDCheck {
consensus.viewID = viewID
consensus.ToggleConsensusCheck()
return nil
} else if consensusID != consensus.consensusID {
utils.GetLogInstance().Warn("Wrong consensus Id", "myConsensusId", consensus.consensusID, "theirConsensusId", consensusID, "consensus", consensus)
} else if viewID != consensus.viewID {
utils.GetLogInstance().Warn("Wrong consensus Id", "myViewId", consensus.viewID, "theirViewId", viewID, "consensus", consensus)
// notify state syncing to start
select {
case consensus.ConsensusIDLowChan <- struct{}{}:
case consensus.ViewIDLowChan <- struct{}{}:
default:
}
return consensus_engine.ErrConsensusIDNotMatch
return consensus_engine.ErrViewIDNotMatch
}
return nil
}
// SetSeqNum sets the seqNum in consensus object, called at node bootstrap
func (consensus *Consensus) SetSeqNum(seqNum uint64) {
// SetBlockNum sets the blockNum in consensus object, called at node bootstrap
func (consensus *Consensus) SetBlockNum(blockNum uint64) {
consensus.mutex.Lock()
defer consensus.mutex.Unlock()
consensus.seqNum = seqNum
consensus.blockNum = blockNum
}

@ -46,7 +46,7 @@ func TestPopulateMessageFields(t *testing.T) {
if err != nil {
t.Fatalf("Cannot craeate consensus: %v", err)
}
consensus.consensusID = 2
consensus.viewID = 2
consensus.blockHash = blockHash
msg := &msg_pb.Message{
@ -57,7 +57,7 @@ func TestPopulateMessageFields(t *testing.T) {
consensusMsg := msg.GetConsensus()
consensus.populateMessageFields(consensusMsg)
if consensusMsg.ConsensusId != 2 {
if consensusMsg.ViewId != 2 {
t.Errorf("Consensus ID is not populated correctly")
}
if !bytes.Equal(consensusMsg.BlockHash[:], blockHash[:]) {
@ -79,7 +79,7 @@ func TestSignAndMarshalConsensusMessage(t *testing.T) {
if err != nil {
t.Fatalf("Cannot craeate consensus: %v", err)
}
consensus.consensusID = 2
consensus.viewID = 2
consensus.blockHash = blockHash
consensus.SelfAddress = common.Address{}
@ -94,7 +94,7 @@ func TestSignAndMarshalConsensusMessage(t *testing.T) {
}
}
func TestSetConsensusID(t *testing.T) {
func TestSetViewID(t *testing.T) {
leader := p2p.Peer{IP: "127.0.0.1", Port: "9902"}
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902")
host, err := p2pimpl.NewHost(&leader, priKey)
@ -107,8 +107,8 @@ func TestSetConsensusID(t *testing.T) {
}
height := uint32(1000)
consensus.SetConsensusID(height)
if consensus.consensusID != height {
t.Errorf("Cannot set consensus ID. Got: %v, Expected: %v", consensus.consensusID, height)
consensus.SetViewID(height)
if consensus.viewID != height {
t.Errorf("Cannot set consensus ID. Got: %v, Expected: %v", consensus.viewID, height)
}
}

@ -21,8 +21,8 @@ func TestNew(test *testing.T) {
if err != nil {
test.Fatalf("Cannot craeate consensus: %v", err)
}
if consensus.consensusID != 0 {
test.Errorf("Consensus Id is initialized to the wrong value: %d", consensus.consensusID)
if consensus.viewID != 0 {
test.Errorf("Consensus Id is initialized to the wrong value: %d", consensus.viewID)
}
if !nodeconfig.GetDefaultConfig().IsLeader() {

@ -59,8 +59,8 @@ func (consensus *Consensus) handleMessageUpdate(payload []byte) {
// TODO: move to consensus_leader.go later
func (consensus *Consensus) tryAnnounce(block *types.Block) {
// here we assume the leader should always be update to date
if block.NumberU64() != consensus.seqNum {
utils.GetLogInstance().Debug("tryAnnounce seqNum not match", "blockNum", block.NumberU64(), "mySeqNum", consensus.seqNum)
if block.NumberU64() != consensus.blockNum {
utils.GetLogInstance().Debug("tryAnnounce blockNum not match", "blockNum", block.NumberU64(), "myBlockNum", consensus.blockNum)
return
}
if !consensus.PubKey.IsEqual(consensus.LeaderPubKey) {
@ -141,11 +141,11 @@ func (consensus *Consensus) onAnnounce(msg *msg_pb.Message) {
return
}
logMsgs := consensus.pbftLog.GetMessagesByTypeSeqView(msg_pb.MessageType_ANNOUNCE, recvMsg.SeqNum, recvMsg.ConsensusID)
logMsgs := consensus.pbftLog.GetMessagesByTypeSeqView(msg_pb.MessageType_ANNOUNCE, recvMsg.BlockNum, recvMsg.ViewID)
if len(logMsgs) > 0 {
if logMsgs[0].BlockHash != blockObj.Header().Hash() {
utils.GetLogInstance().Debug("onAnnounce leader is malicious", "leaderKey", utils.GetBlsAddress(consensus.LeaderPubKey))
consensus.startViewChange(consensus.consensusID + 1)
consensus.startViewChange(consensus.viewID + 1)
}
return
}
@ -175,7 +175,7 @@ func (consensus *Consensus) tryPrepare(blockHash common.Hash) {
return
}
if consensus.phase != Announce || consensus.seqNum != block.NumberU64() || !consensus.pbftLog.HasMatchingAnnounce(consensus.seqNum, consensus.consensusID, hash) {
if consensus.phase != Announce || consensus.blockNum != block.NumberU64() || !consensus.pbftLog.HasMatchingAnnounce(consensus.blockNum, consensus.viewID, hash) {
return
}
@ -211,14 +211,14 @@ func (consensus *Consensus) onPrepare(msg *msg_pb.Message) {
return
}
if recvMsg.ConsensusID != consensus.consensusID || recvMsg.SeqNum != consensus.seqNum || consensus.phase != Prepare {
utils.GetLogInstance().Debug("onPrepare message not match", "myPhase", consensus.phase, "myConsensusID", consensus.consensusID,
"msgConsensusID", recvMsg.ConsensusID, "mySeqNum", consensus.seqNum, "msgSeqNum", recvMsg.SeqNum)
if recvMsg.ViewID != consensus.viewID || recvMsg.BlockNum != consensus.blockNum || consensus.phase != Prepare {
utils.GetLogInstance().Debug("onPrepare message not match", "myPhase", consensus.phase, "myViewID", consensus.viewID,
"msgViewID", recvMsg.ViewID, "myBlockNum", consensus.blockNum, "msgBlockNum", recvMsg.BlockNum)
return
}
if !consensus.pbftLog.HasMatchingAnnounce(consensus.seqNum, consensus.consensusID, recvMsg.BlockHash) {
utils.GetLogInstance().Debug("onPrepare no matching announce message", "seqNum", consensus.seqNum, "consensusID", consensus.consensusID, "blockHash", recvMsg.BlockHash)
if !consensus.pbftLog.HasMatchingAnnounce(consensus.blockNum, consensus.viewID, recvMsg.BlockHash) {
utils.GetLogInstance().Debug("onPrepare no matching announce message", "blockNum", consensus.blockNum, "viewID", consensus.viewID, "blockHash", recvMsg.BlockHash)
return
}
@ -303,7 +303,7 @@ func (consensus *Consensus) onPrepared(msg *msg_pb.Message) {
utils.GetLogInstance().Debug("onPrepared Unparseable validator message", "error", err)
return
}
if recvMsg.SeqNum < consensus.seqNum {
if recvMsg.BlockNum < consensus.blockNum {
return
}
@ -349,7 +349,7 @@ func (consensus *Consensus) onPrepared(msg *msg_pb.Message) {
}
consensus.pbftLog.AddMessage(recvMsg)
if recvMsg.SeqNum > consensus.seqNum || consensus.phase != Prepare || recvMsg.ConsensusID != consensus.consensusID {
if recvMsg.BlockNum > consensus.blockNum || consensus.phase != Prepare || recvMsg.ViewID != consensus.viewID {
return
}
@ -393,11 +393,11 @@ func (consensus *Consensus) onCommit(msg *msg_pb.Message) {
return
}
if recvMsg.ConsensusID != consensus.consensusID || recvMsg.SeqNum != consensus.seqNum || consensus.phase != Commit {
if recvMsg.ViewID != consensus.viewID || recvMsg.BlockNum != consensus.blockNum || consensus.phase != Commit {
return
}
if !consensus.pbftLog.HasMatchingAnnounce(consensus.seqNum, consensus.consensusID, recvMsg.BlockHash) {
if !consensus.pbftLog.HasMatchingAnnounce(consensus.blockNum, consensus.viewID, recvMsg.BlockHash) {
return
}
@ -481,15 +481,15 @@ func (consensus *Consensus) onCommit(msg *msg_pb.Message) {
consensus.reportMetrics(blockObj)
// Dump new block into level db.
explorer.GetStorageInstance(consensus.leader.IP, consensus.leader.Port, true).Dump(&blockObj, consensus.consensusID)
explorer.GetStorageInstance(consensus.leader.IP, consensus.leader.Port, true).Dump(&blockObj, consensus.viewID)
// Reset state to Finished, and clear other data.
consensus.ResetState()
consensus.consensusID++
consensus.seqNum++
consensus.viewID++
consensus.blockNum++
consensus.OnConsensusDone(&blockObj)
utils.GetLogInstance().Debug("HOORAY!!!!!!! CONSENSUS REACHED!!!!!!!", "consensusID", consensus.consensusID, "numOfSignatures", len(commitSigs))
utils.GetLogInstance().Debug("HOORAY!!!!!!! CONSENSUS REACHED!!!!!!!", "viewID", consensus.viewID, "numOfSignatures", len(commitSigs))
// Send signal to Node so the new block can be added and new round of consensus can be triggered
consensus.ReadySignal <- struct{}{}
@ -523,7 +523,7 @@ func (consensus *Consensus) onCommitted(msg *msg_pb.Message) {
utils.GetLogInstance().Warn("onCommitted unable to parse msg", "error", err)
return
}
if recvMsg.SeqNum < consensus.seqNum {
if recvMsg.BlockNum < consensus.blockNum {
return
}
@ -564,7 +564,7 @@ func (consensus *Consensus) onCommitted(msg *msg_pb.Message) {
consensus.pbftLog.AddMessage(recvMsg)
if recvMsg.SeqNum > consensus.seqNum || consensus.phase != Commit || recvMsg.ConsensusID != consensus.consensusID {
if recvMsg.BlockNum > consensus.blockNum || consensus.phase != Commit || recvMsg.ViewID != consensus.viewID {
return
}
consensus.aggregatedCommitSig = &deserializedMultiSig
@ -580,18 +580,18 @@ func (consensus *Consensus) onCommitted(msg *msg_pb.Message) {
// try to catch up if fall behind
func (consensus *Consensus) tryCatchup() {
utils.GetLogInstance().Info("tryCatchup: commit new blocks", "seqNum", consensus.seqNum)
utils.GetLogInstance().Info("tryCatchup: commit new blocks", "blockNum", consensus.blockNum)
if consensus.phase != Commit {
return
}
consensus.switchPhase(Announce)
for {
msgs := consensus.pbftLog.GetMessagesByTypeSeq(msg_pb.MessageType_COMMITTED, consensus.seqNum)
msgs := consensus.pbftLog.GetMessagesByTypeSeq(msg_pb.MessageType_COMMITTED, consensus.blockNum)
if len(msgs) == 0 {
break
}
if len(msgs) > 1 {
utils.GetLogInstance().Info("[PBFT] we should only get one committed message for a given blockNum", "blockNum", consensus.seqNum, "numMsgs", len(msgs))
utils.GetLogInstance().Info("[PBFT] we should only get one committed message for a given blockNum", "blockNum", consensus.blockNum, "numMsgs", len(msgs))
}
block := consensus.pbftLog.GetBlockByHash(msgs[0].BlockHash)
@ -600,13 +600,13 @@ func (consensus *Consensus) tryCatchup() {
}
if block.ParentHash() != consensus.ChainReader.CurrentHeader().Hash() {
utils.GetLogInstance().Debug("[PBFT] parent block hash not match", "blockNum", consensus.seqNum)
utils.GetLogInstance().Debug("[PBFT] parent block hash not match", "blockNum", consensus.blockNum)
break
}
preparedMsgs := consensus.pbftLog.GetMessagesByTypeSeqHash(msg_pb.MessageType_PREPARED, msgs[0].SeqNum, msgs[0].BlockHash)
preparedMsgs := consensus.pbftLog.GetMessagesByTypeSeqHash(msg_pb.MessageType_PREPARED, msgs[0].BlockNum, msgs[0].BlockHash)
if len(preparedMsgs) > 1 {
utils.GetLogInstance().Info("[PBFT] we should only get one prepared message for a given blockNum", "blockNum", consensus.seqNum, "numMsgs", len(preparedMsgs))
utils.GetLogInstance().Info("[PBFT] we should only get one prepared message for a given blockNum", "blockNum", consensus.blockNum, "numMsgs", len(preparedMsgs))
}
if len(preparedMsgs) == 0 {
break
@ -617,8 +617,8 @@ func (consensus *Consensus) tryCatchup() {
if msg.BlockHash == msgs[0].BlockHash {
cnt++
consensus.blockHash = [32]byte{}
consensus.seqNum = consensus.seqNum + 1
consensus.consensusID = consensus.consensusID + 1
consensus.blockNum = consensus.blockNum + 1
consensus.viewID = consensus.viewID + 1
//#### Read payload data from committed msg
aggSig := make([]byte, 48)
@ -656,7 +656,7 @@ func (consensus *Consensus) tryCatchup() {
break
}
}
if cnt == 0 { // didn't find match block for current seqNum, return
if cnt == 0 { // didn't find match block for current blockNum, return
break
}
}
@ -674,6 +674,26 @@ func (consensus *Consensus) Start(blockChannel chan *types.Block, stopChan chan
consensus.consensusTimeout["bootstrap"].Start()
for {
select {
case <-ticker.C:
if !consensus.PubKey.IsEqual(consensus.LeaderPubKey) {
for k, v := range consensus.consensusTimeout {
if !v.CheckExpire() {
continue
}
if k != "viewchange" {
utils.GetLogInstance().Debug("ops", "phase", k, "mode", consensus.mode.Mode())
consensus.startViewChange(consensus.viewID + 1)
break
} else {
utils.GetLogInstance().Debug("ops", "phase", k, "mode", consensus.mode.Mode())
viewID := consensus.mode.ViewID()
consensus.startViewChange(viewID + 1)
break
}
}
}
case newBlock := <-blockChannel:
utils.GetLogInstance().Info("receive newBlock", "blockNum", newBlock.NumberU64())
if consensus.ShardID == 0 {
@ -709,27 +729,6 @@ func (consensus *Consensus) Start(blockChannel chan *types.Block, stopChan chan
case msg := <-consensus.MsgChan:
consensus.handleMessageUpdate(msg)
case <-ticker.C:
if !consensus.PubKey.IsEqual(consensus.LeaderPubKey) {
for k, v := range consensus.consensusTimeout {
if !v.CheckExpire() {
continue
}
if k != "viewchange" {
utils.GetLogInstance().Debug("ops", "phase", k, "mode", consensus.mode.Mode())
consensus.startViewChange(consensus.consensusID + 1)
break
} else {
utils.GetLogInstance().Debug("ops", "phase", k, "mode", consensus.mode.Mode())
consensusID := consensus.mode.ConsensusID()
consensus.startViewChange(consensusID + 1)
break
}
}
}
case <-stopChan:
return
}

@ -46,13 +46,13 @@ func (consensus *Consensus) processAnnounceMessage(message *msg_pb.Message) {
consensusMsg := message.GetConsensus()
consensusID := consensusMsg.ConsensusId
viewID := consensusMsg.ViewId
blockHash := consensusMsg.BlockHash
block := consensusMsg.Payload
// Add block to received block cache
consensus.mutex.Lock()
consensus.blocksReceived[consensusID] = &BlockConsensusStatus{block, consensus.state}
consensus.blocksReceived[viewID] = &BlockConsensusStatus{block, consensus.state}
consensus.mutex.Unlock()
copy(consensus.blockHash[:], blockHash[:])
@ -97,7 +97,7 @@ func (consensus *Consensus) processPreparedMessage(message *msg_pb.Message) {
consensusMsg := message.GetConsensus()
consensusID := consensusMsg.ConsensusId
viewID := consensusMsg.ViewId
blockHash := consensusMsg.BlockHash
pubKey, err := bls_cosi.BytesToBlsPublicKey(consensusMsg.SenderPubkey)
if err != nil {
@ -120,7 +120,7 @@ func (consensus *Consensus) processPreparedMessage(message *msg_pb.Message) {
//#### END Read payload data
// Update readyByConsensus for attack.
attack.GetInstance().UpdateConsensusReady(consensusID)
attack.GetInstance().UpdateConsensusReady(viewID)
if err := consensus.checkConsensusMessage(message, consensus.leader.ConsensusPubKey); err != nil {
utils.GetLogInstance().Debug("processPreparedMessage error", "error", err)
@ -166,7 +166,7 @@ func (consensus *Consensus) processCommittedMessage(message *msg_pb.Message) {
utils.GetLogInstance().Warn("Received Committed Message", "ValidatorAddress", consensus.SelfAddress)
consensusMsg := message.GetConsensus()
consensusID := consensusMsg.ConsensusId
viewID := consensusMsg.ViewId
pubKey, err := bls_cosi.BytesToBlsPublicKey(consensusMsg.SenderPubkey)
if err != nil {
utils.GetLogInstance().Debug("Failed to deserialize BLS public key", "error", err)
@ -187,7 +187,7 @@ func (consensus *Consensus) processCommittedMessage(message *msg_pb.Message) {
//#### END Read payload data
// Update readyByConsensus for attack.
attack.GetInstance().UpdateConsensusReady(consensusID)
attack.GetInstance().UpdateConsensusReady(viewID)
if err := consensus.checkConsensusMessage(message, consensus.leader.ConsensusPubKey); err != nil {
utils.GetLogInstance().Debug("processCommittedMessage error", "error", err)
@ -225,12 +225,12 @@ func (consensus *Consensus) processCommittedMessage(message *msg_pb.Message) {
// The logic is to roll up to the latest blocks one by one to try catching up with the leader.
// but because of checkConsensusMessage, the catchup logic will never be used here
for {
val, ok := consensus.blocksReceived[consensus.consensusID]
val, ok := consensus.blocksReceived[consensus.viewID]
if ok {
delete(consensus.blocksReceived, consensus.consensusID)
delete(consensus.blocksReceived, consensus.viewID)
consensus.blockHash = [32]byte{}
consensus.consensusID = consensusID + 1 // roll up one by one, until the next block is not received yet.
consensus.viewID = viewID + 1 // roll up one by one, until the next block is not received yet.
var blockObj types.Block
err := rlp.DecodeBytes(val.block, &blockObj)
@ -239,7 +239,7 @@ func (consensus *Consensus) processCommittedMessage(message *msg_pb.Message) {
}
// check block data (transactions
if err := consensus.VerifyHeader(consensus.ChainReader, blockObj.Header(), false); err != nil {
utils.GetLogInstance().Debug("[WARNING] Block content is not verified successfully", "consensusID", consensus.consensusID)
utils.GetLogInstance().Debug("[WARNING] Block content is not verified successfully", "viewID", consensus.viewID)
return
}

@ -18,18 +18,18 @@ func (consensus *Consensus) constructViewChangeMessage() []byte {
}
vcMsg := message.GetViewchange()
vcMsg.ConsensusId = consensus.mode.GetConsensusID()
vcMsg.SeqNum = consensus.seqNum
vcMsg.ViewId = consensus.mode.GetViewID()
vcMsg.BlockNum = consensus.blockNum
// sender address
vcMsg.SenderPubkey = consensus.PubKey.Serialize()
// next leader key already updated
vcMsg.LeaderPubkey = consensus.LeaderPubKey.Serialize()
preparedMsgs := consensus.pbftLog.GetMessagesByTypeSeqHash(msg_pb.MessageType_PREPARED, consensus.seqNum, consensus.blockHash)
preparedMsgs := consensus.pbftLog.GetMessagesByTypeSeqHash(msg_pb.MessageType_PREPARED, consensus.blockNum, consensus.blockHash)
if len(preparedMsgs) > 1 {
utils.GetLogInstance().Warn("constructViewChangeMessage got more than 1 prepared message", "seqNum", consensus.seqNum, "consensusID", consensus.consensusID)
utils.GetLogInstance().Warn("constructViewChangeMessage got more than 1 prepared message", "blockNum", consensus.blockNum, "viewID", consensus.viewID)
}
var msgToSign []byte
@ -65,8 +65,8 @@ func (consensus *Consensus) constructNewViewMessage() []byte {
}
vcMsg := message.GetViewchange()
vcMsg.ConsensusId = consensus.mode.GetConsensusID()
vcMsg.SeqNum = consensus.seqNum
vcMsg.ViewId = consensus.mode.GetViewID()
vcMsg.BlockNum = consensus.blockNum
// sender address
vcMsg.SenderPubkey = consensus.PubKey.Serialize()

@ -35,8 +35,8 @@ var (
// plus one.
ErrInvalidNumber = errors.New("invalid block number")
// ErrConsensusIDNotMatch is returned if the current consensusID is not equal message's consensusID
ErrConsensusIDNotMatch = errors.New("consensusID not match")
// ErrViewIDNotMatch is returned if the current viewID is not equal message's viewID
ErrViewIDNotMatch = errors.New("viewID not match")
// ErrInvalidConsensusMessage is returned is the consensus message received is invalid
ErrInvalidConsensusMessage = errors.New("invalid consensus message")

@ -24,8 +24,8 @@ type PbftLog struct {
// PbftMessage is the record of pbft messages received by a node during PBFT process
type PbftMessage struct {
MessageType msg_pb.MessageType
ConsensusID uint32
SeqNum uint64
ViewID uint32
BlockNum uint64
BlockHash common.Hash
SenderPubkey *bls.PublicKey
LeaderPubkey *bls.PublicKey
@ -103,7 +103,7 @@ func (log *PbftLog) DeleteMessagesByNumber(number uint64) {
found := mapset.NewSet()
it := log.Messages().Iterator()
for msg := range it.C {
if msg.(*PbftMessage).SeqNum == number {
if msg.(*PbftMessage).BlockNum == number {
found.Add(msg)
}
}
@ -115,54 +115,54 @@ func (log *PbftLog) AddMessage(msg *PbftMessage) {
log.messages.Add(msg)
}
// GetMessagesByTypeSeqViewHash returns pbft messages with matching type, seqNum, consensusID and blockHash
func (log *PbftLog) GetMessagesByTypeSeqViewHash(typ msg_pb.MessageType, seqNum uint64, consensusID uint32, blockHash common.Hash) []*PbftMessage {
// GetMessagesByTypeSeqViewHash returns pbft messages with matching type, blockNum, viewID and blockHash
func (log *PbftLog) GetMessagesByTypeSeqViewHash(typ msg_pb.MessageType, blockNum uint64, viewID uint32, blockHash common.Hash) []*PbftMessage {
found := []*PbftMessage{}
it := log.Messages().Iterator()
for msg := range it.C {
if msg.(*PbftMessage).MessageType == typ && msg.(*PbftMessage).SeqNum == seqNum && msg.(*PbftMessage).ConsensusID == consensusID && msg.(*PbftMessage).BlockHash == blockHash {
if msg.(*PbftMessage).MessageType == typ && msg.(*PbftMessage).BlockNum == blockNum && msg.(*PbftMessage).ViewID == viewID && msg.(*PbftMessage).BlockHash == blockHash {
found = append(found, msg.(*PbftMessage))
}
}
return found
}
// GetMessagesByTypeSeq returns pbft messages with matching type, seqNum
func (log *PbftLog) GetMessagesByTypeSeq(typ msg_pb.MessageType, seqNum uint64) []*PbftMessage {
// GetMessagesByTypeSeq returns pbft messages with matching type, blockNum
func (log *PbftLog) GetMessagesByTypeSeq(typ msg_pb.MessageType, blockNum uint64) []*PbftMessage {
found := []*PbftMessage{}
it := log.Messages().Iterator()
for msg := range it.C {
if msg.(*PbftMessage).MessageType == typ && msg.(*PbftMessage).SeqNum == seqNum {
if msg.(*PbftMessage).MessageType == typ && msg.(*PbftMessage).BlockNum == blockNum {
found = append(found, msg.(*PbftMessage))
}
}
return found
}
// GetMessagesByTypeSeqHash returns pbft messages with matching type, seqNum
func (log *PbftLog) GetMessagesByTypeSeqHash(typ msg_pb.MessageType, seqNum uint64, blockHash common.Hash) []*PbftMessage {
// GetMessagesByTypeSeqHash returns pbft messages with matching type, blockNum
func (log *PbftLog) GetMessagesByTypeSeqHash(typ msg_pb.MessageType, blockNum uint64, blockHash common.Hash) []*PbftMessage {
found := []*PbftMessage{}
it := log.Messages().Iterator()
for msg := range it.C {
if msg.(*PbftMessage).MessageType == typ && msg.(*PbftMessage).SeqNum == seqNum && msg.(*PbftMessage).BlockHash == blockHash {
if msg.(*PbftMessage).MessageType == typ && msg.(*PbftMessage).BlockNum == blockNum && msg.(*PbftMessage).BlockHash == blockHash {
found = append(found, msg.(*PbftMessage))
}
}
return found
}
// HasMatchingAnnounce returns whether the log contains announce type message with given seqNum, consensusID and blockHash
func (log *PbftLog) HasMatchingAnnounce(seqNum uint64, consensusID uint32, blockHash common.Hash) bool {
found := log.GetMessagesByTypeSeqViewHash(msg_pb.MessageType_ANNOUNCE, seqNum, consensusID, blockHash)
// HasMatchingAnnounce returns whether the log contains announce type message with given blockNum, viewID and blockHash
func (log *PbftLog) HasMatchingAnnounce(blockNum uint64, viewID uint32, blockHash common.Hash) bool {
found := log.GetMessagesByTypeSeqViewHash(msg_pb.MessageType_ANNOUNCE, blockNum, viewID, blockHash)
return len(found) == 1
}
// GetMessagesByTypeSeqView returns pbft messages with matching type, seqNum and consensusID
func (log *PbftLog) GetMessagesByTypeSeqView(typ msg_pb.MessageType, seqNum uint64, consensusID uint32) []*PbftMessage {
// GetMessagesByTypeSeqView returns pbft messages with matching type, blockNum and viewID
func (log *PbftLog) GetMessagesByTypeSeqView(typ msg_pb.MessageType, blockNum uint64, viewID uint32) []*PbftMessage {
found := []*PbftMessage{}
it := log.Messages().Iterator()
for msg := range it.C {
if msg.(*PbftMessage).MessageType != typ || msg.(*PbftMessage).SeqNum != seqNum || msg.(*PbftMessage).ConsensusID != consensusID {
if msg.(*PbftMessage).MessageType != typ || msg.(*PbftMessage).BlockNum != blockNum || msg.(*PbftMessage).ViewID != viewID {
continue
}
found = append(found, msg.(*PbftMessage))
@ -176,8 +176,8 @@ func ParsePbftMessage(msg *msg_pb.Message) (*PbftMessage, error) {
pbftMsg.MessageType = msg.GetType()
consensusMsg := msg.GetConsensus()
pbftMsg.ConsensusID = consensusMsg.ConsensusId
pbftMsg.SeqNum = consensusMsg.SeqNum
pbftMsg.ViewID = consensusMsg.ViewId
pbftMsg.BlockNum = consensusMsg.BlockNum
pbftMsg.BlockHash = common.Hash{}
copy(pbftMsg.BlockHash[:], consensusMsg.BlockHash[:])
pbftMsg.Payload = make([]byte, len(consensusMsg.Payload))
@ -199,8 +199,8 @@ func ParseViewChangeMessage(msg *msg_pb.Message) (*PbftMessage, error) {
}
vcMsg := msg.GetViewchange()
pbftMsg.ConsensusID = vcMsg.ConsensusId
pbftMsg.SeqNum = vcMsg.SeqNum
pbftMsg.ViewID = vcMsg.ViewId
pbftMsg.BlockNum = vcMsg.BlockNum
pbftMsg.Payload = make([]byte, len(vcMsg.Payload))
copy(pbftMsg.Payload[:], vcMsg.Payload[:])
@ -239,8 +239,8 @@ func (consensus *Consensus) ParseNewViewMessage(msg *msg_pb.Message) (*PbftMessa
}
vcMsg := msg.GetViewchange()
pbftMsg.ConsensusID = vcMsg.ConsensusId
pbftMsg.SeqNum = vcMsg.SeqNum
pbftMsg.ViewID = vcMsg.ViewId
pbftMsg.BlockNum = vcMsg.BlockNum
pbftMsg.Payload = make([]byte, len(vcMsg.Payload))
copy(pbftMsg.Payload[:], vcMsg.Payload[:])

@ -52,7 +52,7 @@ func TestParsePbftMessage(t *testing.T) {
}
func TestGetMessagesByTypeSeqViewHash(t *testing.T) {
pbftMsg := PbftMessage{MessageType: msg_pb.MessageType_ANNOUNCE, SeqNum: 2, ConsensusID: 3, BlockHash: [32]byte{01, 02}}
pbftMsg := PbftMessage{MessageType: msg_pb.MessageType_ANNOUNCE, BlockNum: 2, ViewID: 3, BlockHash: [32]byte{01, 02}}
log := NewPbftLog()
log.AddMessage(&pbftMsg)
@ -68,7 +68,7 @@ func TestGetMessagesByTypeSeqViewHash(t *testing.T) {
}
func TestHasMatchingAnnounce(t *testing.T) {
pbftMsg := PbftMessage{MessageType: msg_pb.MessageType_ANNOUNCE, SeqNum: 2, ConsensusID: 3, BlockHash: [32]byte{01, 02}}
pbftMsg := PbftMessage{MessageType: msg_pb.MessageType_ANNOUNCE, BlockNum: 2, ViewID: 3, BlockHash: [32]byte{01, 02}}
log := NewPbftLog()
log.AddMessage(&pbftMsg)
found := log.HasMatchingAnnounce(2, 3, [32]byte{01, 02})

@ -33,11 +33,11 @@ const (
ViewChanging
)
// PbftMode contains mode and consensusID of viewchanging
// PbftMode contains mode and viewID of viewchanging
type PbftMode struct {
mode Mode
consensusID uint32
mux sync.Mutex
mode Mode
viewID uint32
mux sync.Mutex
}
// Mode return the current node mode
@ -52,21 +52,21 @@ func (pm *PbftMode) SetMode(m Mode) {
pm.mode = m
}
// ConsensusID return the current viewchanging id
func (pm *PbftMode) ConsensusID() uint32 {
return pm.consensusID
// ViewID return the current viewchanging id
func (pm *PbftMode) ViewID() uint32 {
return pm.viewID
}
// SetConsensusID sets the viewchanging id accordingly
func (pm *PbftMode) SetConsensusID(consensusID uint32) {
// SetViewID sets the viewchanging id accordingly
func (pm *PbftMode) SetViewID(viewID uint32) {
pm.mux.Lock()
defer pm.mux.Unlock()
pm.consensusID = consensusID
pm.viewID = viewID
}
// GetConsensusID returns the current viewchange consensusID
func (pm *PbftMode) GetConsensusID() uint32 {
return pm.consensusID
// GetViewID returns the current viewchange viewID
func (pm *PbftMode) GetViewID() uint32 {
return pm.viewID
}
// switchPhase will switch PbftPhase to nextPhase if the desirePhase equals the nextPhase
@ -87,7 +87,7 @@ func (consensus *Consensus) switchPhase(desirePhase PbftPhase) {
}
}
// GetNextLeaderKey uniquely determine who is the leader for given consensusID
// GetNextLeaderKey uniquely determine who is the leader for given viewID
func (consensus *Consensus) GetNextLeaderKey() *bls.PublicKey {
idx := consensus.getIndexOfPubKey(consensus.LeaderPubKey)
if idx == -1 {
@ -132,23 +132,23 @@ func createTimeout() map[string]*utils.Timeout {
}
// startViewChange send a new view change
func (consensus *Consensus) startViewChange(consensusID uint32) {
func (consensus *Consensus) startViewChange(viewID uint32) {
for k := range consensus.consensusTimeout {
if k != "viewchange" {
consensus.consensusTimeout[k].Stop()
}
}
consensus.mode.SetMode(ViewChanging)
consensus.mode.SetConsensusID(consensusID)
consensus.mode.SetViewID(viewID)
nextLeaderKey := consensus.GetNextLeaderKey()
consensus.LeaderPubKey = consensus.GetNextLeaderKey()
if nextLeaderKey.IsEqual(consensus.PubKey) {
return
}
diff := consensusID - consensus.consensusID
diff := viewID - consensus.viewID
duration := time.Duration(int64(diff) * int64(viewChangeDuration))
utils.GetLogInstance().Info("startViewChange", "consensusID", consensusID, "timeoutDuration", duration, "nextLeader", consensus.LeaderPubKey.GetHexString()[:10])
utils.GetLogInstance().Info("startViewChange", "viewID", viewID, "timeoutDuration", duration, "nextLeader", consensus.LeaderPubKey.GetHexString()[:10])
msgToSend := consensus.constructViewChangeMessage()
consensus.host.SendMessageToGroups([]p2p.GroupID{p2p.NewGroupIDByShardID(p2p.ShardID(consensus.ShardID))}, host.ConstructP2pMessage(byte(17), msgToSend))
@ -160,7 +160,7 @@ func (consensus *Consensus) startViewChange(consensusID uint32) {
// new leader send new view message
func (consensus *Consensus) startNewView() {
utils.GetLogInstance().Info("startNewView", "consensusID", consensus.mode.GetConsensusID())
utils.GetLogInstance().Info("startNewView", "viewID", consensus.mode.GetViewID())
consensus.mode.SetMode(Normal)
consensus.switchPhase(Announce)
@ -185,12 +185,12 @@ func (consensus *Consensus) onViewChange(msg *msg_pb.Message) {
return
}
utils.GetLogInstance().Warn("onViewChange received", "viewChangeID", recvMsg.ConsensusID, "myCurrentID", consensus.consensusID, "ValidatorAddress", consensus.SelfAddress)
utils.GetLogInstance().Warn("onViewChange received", "viewChangeID", recvMsg.ViewID, "myCurrentID", consensus.viewID, "ValidatorAddress", consensus.SelfAddress)
if consensus.seqNum > recvMsg.SeqNum {
if consensus.blockNum > recvMsg.BlockNum {
return
}
if consensus.mode.Mode() == ViewChanging && consensus.mode.GetConsensusID() > recvMsg.ConsensusID {
if consensus.mode.Mode() == ViewChanging && consensus.mode.GetViewID() > recvMsg.ViewID {
return
}
if err = verifyMessageSig(senderKey, msg); err != nil {
@ -202,13 +202,13 @@ func (consensus *Consensus) onViewChange(msg *msg_pb.Message) {
defer consensus.vcLock.Unlock()
consensus.mode.SetMode(ViewChanging)
consensus.mode.SetConsensusID(recvMsg.ConsensusID)
consensus.mode.SetViewID(recvMsg.ViewID)
_, ok1 := consensus.nilSigs[consensus.SelfAddress]
_, ok2 := consensus.bhpSigs[consensus.SelfAddress]
if !(ok1 || ok2) {
// add own signature for newview message
preparedMsgs := consensus.pbftLog.GetMessagesByTypeSeq(msg_pb.MessageType_PREPARED, recvMsg.SeqNum)
preparedMsgs := consensus.pbftLog.GetMessagesByTypeSeq(msg_pb.MessageType_PREPARED, recvMsg.BlockNum)
if len(preparedMsgs) == 0 {
sign := consensus.priKey.SignHash(NIL)
consensus.nilSigs[consensus.SelfAddress] = sign
@ -323,7 +323,7 @@ func (consensus *Consensus) onViewChange(msg *msg_pb.Message) {
utils.GetLogInstance().Warn("onViewChange", "sent newview message", len(msgToSend))
consensus.host.SendMessageToGroups([]p2p.GroupID{p2p.NewGroupIDByShardID(p2p.ShardID(consensus.ShardID))}, host.ConstructP2pMessage(byte(17), msgToSend))
consensus.consensusID = consensus.mode.GetConsensusID()
consensus.viewID = consensus.mode.GetViewID()
consensus.ResetViewChangeState()
consensus.ResetState()
consensus.consensusTimeout["viewchange"].Stop()
@ -350,7 +350,7 @@ func (consensus *Consensus) onNewView(msg *msg_pb.Message) {
utils.GetLogInstance().Warn("onNewView key not match", "senderKey", senderKey.GetHexString()[:10], "newLeaderKey", consensus.LeaderPubKey.GetHexString()[:10])
return
}
if consensus.seqNum > recvMsg.SeqNum {
if consensus.blockNum > recvMsg.BlockNum {
return
}
if err = verifyMessageSig(senderKey, msg); err != nil {
@ -414,7 +414,7 @@ func (consensus *Consensus) onNewView(msg *msg_pb.Message) {
//create prepared message?: consensus.pbftLog.AddMessage(recvMsg)
if recvMsg.SeqNum > consensus.seqNum {
if recvMsg.BlockNum > consensus.blockNum {
return
}
@ -431,7 +431,7 @@ func (consensus *Consensus) onNewView(msg *msg_pb.Message) {
consensus.consensusTimeout["announce"].Start()
utils.GetLogInstance().Info("onNewView === announce")
}
consensus.consensusID = consensus.mode.GetConsensusID()
consensus.viewID = consensus.mode.GetViewID()
consensus.ResetViewChangeState()
consensus.ResetState()
consensus.consensusTimeout["viewchange"].Stop()

@ -11,11 +11,11 @@ import (
// Constants used for attack model.
const (
DroppingTickDuration = 2 * time.Second
HitRate = 10
DelayResponseDuration = 10 * time.Second
ConsensusIDThresholdMin = 10
ConsensusIDThresholdMax = 100
DroppingTickDuration = 2 * time.Second
HitRate = 10
DelayResponseDuration = 10 * time.Second
ViewIDThresholdMin = 10
ViewIDThresholdMax = 100
)
// Type is the type of attack model.
@ -32,7 +32,7 @@ const (
type Model struct {
AttackEnabled bool
attackType Type
ConsensusIDThreshold uint32
ViewIDThreshold uint32
readyByConsensusThreshold bool
}
@ -59,7 +59,7 @@ func (attack *Model) SetAttackEnabled(AttackEnabled bool) {
attack.AttackEnabled = AttackEnabled
if AttackEnabled {
attack.attackType = Type(rand.Intn(3))
attack.ConsensusIDThreshold = uint32(ConsensusIDThresholdMin + rand.Intn(ConsensusIDThresholdMax-ConsensusIDThresholdMin))
attack.ViewIDThreshold = uint32(ViewIDThresholdMin + rand.Intn(ViewIDThresholdMax-ViewIDThresholdMin))
}
}
@ -104,9 +104,9 @@ func (attack *Model) IncorrectResponse() bool {
return false
}
// UpdateConsensusReady enables an attack type given the current consensusID.
func (attack *Model) UpdateConsensusReady(consensusID uint32) {
if consensusID > attack.ConsensusIDThreshold {
// UpdateConsensusReady enables an attack type given the current viewID.
func (attack *Model) UpdateConsensusReady(viewID uint32) {
if viewID > attack.ViewIDThreshold {
attack.readyByConsensusThreshold = true
}
}

@ -18,9 +18,9 @@ func TestUpdateConsensusReady(t *testing.T) {
model := GetInstance()
model.NodeKilledByItSelf()
model.UpdateConsensusReady(model.ConsensusIDThreshold - 1)
model.UpdateConsensusReady(model.ViewIDThreshold - 1)
model.DelayResponse()
model.UpdateConsensusReady(model.ConsensusIDThreshold + 1)
model.UpdateConsensusReady(model.ViewIDThreshold + 1)
model.DelayResponse()
}

@ -292,7 +292,7 @@ func New(host p2p.Host, consensusObj *consensus.Consensus, db ethdb.Database, is
node.Consensus.VerifiedNewBlock = make(chan *types.Block)
// the sequence number is the next block number to be added in consensus protocol, which is always one more than current chain header block
node.Consensus.SetSeqNum(chain.CurrentBlock().NumberU64() + 1)
node.Consensus.SetBlockNum(chain.CurrentBlock().NumberU64() + 1)
// 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.

@ -272,7 +272,7 @@ func (node *Node) PostConsensusProcessing(newBlock *types.Block) {
if node.Consensus.PubKey.IsEqual(node.Consensus.LeaderPubKey) {
node.BroadcastNewBlock(newBlock)
} else {
utils.GetLogInstance().Info("BINGO !!! Reached Consensus", "ConsensusID", node.Consensus.GetConsensusID())
utils.GetLogInstance().Info("BINGO !!! Reached Consensus", "ViewID", node.Consensus.GetViewID())
}
node.AddNewBlock(newBlock)

@ -111,7 +111,7 @@ SyncingLoop:
node.State = NodeReadyForConsensus
node.stateMutex.Unlock()
if willJoinConsensus {
<-node.Consensus.ConsensusIDLowChan
<-node.Consensus.ViewIDLowChan
}
}
}

@ -72,8 +72,8 @@ EOU
DB=
TXGEN=true
DURATION=60
MIN=3
SHARDS=1
MIN=5
SHARDS=2
SYNC=true
DRYRUN=
@ -98,7 +98,7 @@ if [ -z "$config" ]; then
fi
if [ "$SYNC" == "true" ]; then
DURATION=600
DURATION=200
fi
# Kill nodes if any

Loading…
Cancel
Save