You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
344 lines
11 KiB
344 lines
11 KiB
6 years ago
|
package consensus
|
||
|
|
||
|
import (
|
||
6 years ago
|
"fmt"
|
||
6 years ago
|
"sync"
|
||
|
|
||
6 years ago
|
mapset "github.com/deckarep/golang-set"
|
||
|
"github.com/ethereum/go-ethereum/common"
|
||
6 years ago
|
"github.com/harmony-one/bls/ffi/go/bls"
|
||
6 years ago
|
msg_pb "github.com/harmony-one/harmony/api/proto/message"
|
||
6 years ago
|
"github.com/harmony-one/harmony/core/types"
|
||
6 years ago
|
bls_cosi "github.com/harmony-one/harmony/crypto/bls"
|
||
|
"github.com/harmony-one/harmony/internal/utils"
|
||
6 years ago
|
)
|
||
|
|
||
5 years ago
|
// FBFTLog represents the log stored by a node during FBFT process
|
||
|
type FBFTLog struct {
|
||
|
blocks mapset.Set //store blocks received in FBFT
|
||
|
messages mapset.Set // store messages received in FBFT
|
||
6 years ago
|
maxLogSize uint32
|
||
6 years ago
|
mutex sync.Mutex
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
// FBFTMessage is the record of pbft messages received by a node during FBFT process
|
||
|
type FBFTMessage struct {
|
||
6 years ago
|
MessageType msg_pb.MessageType
|
||
6 years ago
|
ViewID uint64
|
||
6 years ago
|
BlockNum uint64
|
||
6 years ago
|
BlockHash common.Hash
|
||
6 years ago
|
Block []byte
|
||
6 years ago
|
SenderPubkey *bls.PublicKey
|
||
|
LeaderPubkey *bls.PublicKey
|
||
|
Payload []byte
|
||
|
ViewchangeSig *bls.Sign
|
||
6 years ago
|
ViewidSig *bls.Sign
|
||
6 years ago
|
M2AggSig *bls.Sign
|
||
|
M2Bitmap *bls_cosi.Mask
|
||
6 years ago
|
M3AggSig *bls.Sign
|
||
|
M3Bitmap *bls_cosi.Mask
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
// NewFBFTLog returns new instance of FBFTLog
|
||
|
func NewFBFTLog() *FBFTLog {
|
||
6 years ago
|
blocks := mapset.NewSet()
|
||
|
messages := mapset.NewSet()
|
||
|
logSize := maxLogSize
|
||
5 years ago
|
pbftLog := FBFTLog{blocks: blocks, messages: messages, maxLogSize: logSize}
|
||
6 years ago
|
return &pbftLog
|
||
|
}
|
||
|
|
||
|
// Blocks return the blocks stored in the log
|
||
5 years ago
|
func (log *FBFTLog) Blocks() mapset.Set {
|
||
6 years ago
|
return log.blocks
|
||
|
}
|
||
|
|
||
|
// Messages return the messages stored in the log
|
||
5 years ago
|
func (log *FBFTLog) Messages() mapset.Set {
|
||
6 years ago
|
return log.messages
|
||
|
}
|
||
|
|
||
|
// AddBlock add a new block into the log
|
||
5 years ago
|
func (log *FBFTLog) AddBlock(block *types.Block) {
|
||
6 years ago
|
log.blocks.Add(block)
|
||
|
}
|
||
|
|
||
|
// GetBlockByHash returns the block matches the given block hash
|
||
5 years ago
|
func (log *FBFTLog) GetBlockByHash(hash common.Hash) *types.Block {
|
||
6 years ago
|
var found *types.Block
|
||
|
it := log.Blocks().Iterator()
|
||
|
for block := range it.C {
|
||
6 years ago
|
if block.(*types.Block).Header().Hash() == hash {
|
||
6 years ago
|
found = block.(*types.Block)
|
||
|
it.Stop()
|
||
|
}
|
||
|
}
|
||
|
return found
|
||
|
}
|
||
|
|
||
6 years ago
|
// GetBlocksByNumber returns the blocks match the given block number
|
||
5 years ago
|
func (log *FBFTLog) GetBlocksByNumber(number uint64) []*types.Block {
|
||
6 years ago
|
found := []*types.Block{}
|
||
6 years ago
|
it := log.Blocks().Iterator()
|
||
|
for block := range it.C {
|
||
6 years ago
|
if block.(*types.Block).NumberU64() == number {
|
||
|
found = append(found, block.(*types.Block))
|
||
6 years ago
|
}
|
||
|
}
|
||
|
return found
|
||
|
}
|
||
|
|
||
6 years ago
|
// DeleteBlocksLessThan deletes blocks less than given block number
|
||
5 years ago
|
func (log *FBFTLog) DeleteBlocksLessThan(number uint64) {
|
||
6 years ago
|
found := mapset.NewSet()
|
||
|
it := log.Blocks().Iterator()
|
||
|
for block := range it.C {
|
||
6 years ago
|
if block.(*types.Block).NumberU64() < number {
|
||
6 years ago
|
found.Add(block)
|
||
|
}
|
||
|
}
|
||
|
log.blocks = log.blocks.Difference(found)
|
||
|
}
|
||
|
|
||
6 years ago
|
// DeleteBlockByNumber deletes block of specific number
|
||
5 years ago
|
func (log *FBFTLog) DeleteBlockByNumber(number uint64) {
|
||
6 years ago
|
found := mapset.NewSet()
|
||
|
it := log.Blocks().Iterator()
|
||
|
for block := range it.C {
|
||
|
if block.(*types.Block).NumberU64() == number {
|
||
|
found.Add(block)
|
||
|
}
|
||
|
}
|
||
|
log.blocks = log.blocks.Difference(found)
|
||
|
}
|
||
|
|
||
6 years ago
|
// DeleteMessagesLessThan deletes messages less than given block number
|
||
5 years ago
|
func (log *FBFTLog) DeleteMessagesLessThan(number uint64) {
|
||
6 years ago
|
found := mapset.NewSet()
|
||
|
it := log.Messages().Iterator()
|
||
|
for msg := range it.C {
|
||
5 years ago
|
if msg.(*FBFTMessage).BlockNum < number {
|
||
6 years ago
|
found.Add(msg)
|
||
|
}
|
||
|
}
|
||
|
log.messages = log.messages.Difference(found)
|
||
|
}
|
||
|
|
||
6 years ago
|
// AddMessage adds a pbft message into the log
|
||
5 years ago
|
func (log *FBFTLog) AddMessage(msg *FBFTMessage) {
|
||
6 years ago
|
log.messages.Add(msg)
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
// GetMessagesByTypeSeqViewHash returns pbft messages with matching type, blockNum, viewID and blockHash
|
||
5 years ago
|
func (log *FBFTLog) GetMessagesByTypeSeqViewHash(typ msg_pb.MessageType, blockNum uint64, viewID uint64, blockHash common.Hash) []*FBFTMessage {
|
||
|
found := []*FBFTMessage{}
|
||
6 years ago
|
it := log.Messages().Iterator()
|
||
|
for msg := range it.C {
|
||
5 years ago
|
if msg.(*FBFTMessage).MessageType == typ && msg.(*FBFTMessage).BlockNum == blockNum && msg.(*FBFTMessage).ViewID == viewID && msg.(*FBFTMessage).BlockHash == blockHash {
|
||
|
found = append(found, msg.(*FBFTMessage))
|
||
6 years ago
|
}
|
||
|
}
|
||
|
return found
|
||
|
}
|
||
|
|
||
6 years ago
|
// GetMessagesByTypeSeq returns pbft messages with matching type, blockNum
|
||
5 years ago
|
func (log *FBFTLog) GetMessagesByTypeSeq(typ msg_pb.MessageType, blockNum uint64) []*FBFTMessage {
|
||
|
found := []*FBFTMessage{}
|
||
6 years ago
|
it := log.Messages().Iterator()
|
||
|
for msg := range it.C {
|
||
5 years ago
|
if msg.(*FBFTMessage).MessageType == typ && msg.(*FBFTMessage).BlockNum == blockNum {
|
||
|
found = append(found, msg.(*FBFTMessage))
|
||
6 years ago
|
}
|
||
|
}
|
||
|
return found
|
||
|
}
|
||
|
|
||
6 years ago
|
// GetMessagesByTypeSeqHash returns pbft messages with matching type, blockNum
|
||
5 years ago
|
func (log *FBFTLog) GetMessagesByTypeSeqHash(typ msg_pb.MessageType, blockNum uint64, blockHash common.Hash) []*FBFTMessage {
|
||
|
found := []*FBFTMessage{}
|
||
6 years ago
|
it := log.Messages().Iterator()
|
||
|
for msg := range it.C {
|
||
5 years ago
|
if msg.(*FBFTMessage).MessageType == typ && msg.(*FBFTMessage).BlockNum == blockNum && msg.(*FBFTMessage).BlockHash == blockHash {
|
||
|
found = append(found, msg.(*FBFTMessage))
|
||
6 years ago
|
}
|
||
|
}
|
||
|
return found
|
||
|
}
|
||
|
|
||
6 years ago
|
// HasMatchingAnnounce returns whether the log contains announce type message with given blockNum, blockHash
|
||
5 years ago
|
func (log *FBFTLog) HasMatchingAnnounce(blockNum uint64, blockHash common.Hash) bool {
|
||
6 years ago
|
found := log.GetMessagesByTypeSeqHash(msg_pb.MessageType_ANNOUNCE, blockNum, blockHash)
|
||
6 years ago
|
return len(found) >= 1
|
||
6 years ago
|
}
|
||
|
|
||
|
// HasMatchingViewAnnounce returns whether the log contains announce type message with given blockNum, viewID and blockHash
|
||
5 years ago
|
func (log *FBFTLog) HasMatchingViewAnnounce(blockNum uint64, viewID uint64, blockHash common.Hash) bool {
|
||
6 years ago
|
found := log.GetMessagesByTypeSeqViewHash(msg_pb.MessageType_ANNOUNCE, blockNum, viewID, blockHash)
|
||
6 years ago
|
return len(found) >= 1
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
// HasMatchingPrepared returns whether the log contains prepared message with given blockNum, viewID and blockHash
|
||
5 years ago
|
func (log *FBFTLog) HasMatchingPrepared(blockNum uint64, blockHash common.Hash) bool {
|
||
6 years ago
|
found := log.GetMessagesByTypeSeqHash(msg_pb.MessageType_PREPARED, blockNum, blockHash)
|
||
|
return len(found) >= 1
|
||
|
}
|
||
|
|
||
6 years ago
|
// HasMatchingViewPrepared returns whether the log contains prepared message with given blockNum, viewID and blockHash
|
||
5 years ago
|
func (log *FBFTLog) HasMatchingViewPrepared(blockNum uint64, viewID uint64, blockHash common.Hash) bool {
|
||
6 years ago
|
found := log.GetMessagesByTypeSeqViewHash(msg_pb.MessageType_PREPARED, blockNum, viewID, blockHash)
|
||
6 years ago
|
return len(found) >= 1
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
// GetMessagesByTypeSeqView returns pbft messages with matching type, blockNum and viewID
|
||
5 years ago
|
func (log *FBFTLog) GetMessagesByTypeSeqView(typ msg_pb.MessageType, blockNum uint64, viewID uint64) []*FBFTMessage {
|
||
|
found := []*FBFTMessage{}
|
||
6 years ago
|
it := log.Messages().Iterator()
|
||
|
for msg := range it.C {
|
||
5 years ago
|
if msg.(*FBFTMessage).MessageType != typ || msg.(*FBFTMessage).BlockNum != blockNum || msg.(*FBFTMessage).ViewID != viewID {
|
||
6 years ago
|
continue
|
||
|
}
|
||
5 years ago
|
found = append(found, msg.(*FBFTMessage))
|
||
6 years ago
|
}
|
||
|
return found
|
||
|
}
|
||
|
|
||
6 years ago
|
// FindMessageByMaxViewID returns the message that has maximum ViewID
|
||
5 years ago
|
func (log *FBFTLog) FindMessageByMaxViewID(msgs []*FBFTMessage) *FBFTMessage {
|
||
6 years ago
|
if len(msgs) == 0 {
|
||
|
return nil
|
||
|
}
|
||
|
maxIdx := -1
|
||
6 years ago
|
maxViewID := uint64(0)
|
||
6 years ago
|
for k, v := range msgs {
|
||
|
if v.ViewID >= maxViewID {
|
||
|
maxIdx = k
|
||
|
maxViewID = v.ViewID
|
||
|
}
|
||
|
}
|
||
|
return msgs[maxIdx]
|
||
|
}
|
||
|
|
||
5 years ago
|
// ParseFBFTMessage parses FBFT message into FBFTMessage structure
|
||
|
func ParseFBFTMessage(msg *msg_pb.Message) (*FBFTMessage, error) {
|
||
|
pbftMsg := FBFTMessage{}
|
||
6 years ago
|
pbftMsg.MessageType = msg.GetType()
|
||
|
consensusMsg := msg.GetConsensus()
|
||
6 years ago
|
pbftMsg.ViewID = consensusMsg.ViewId
|
||
|
pbftMsg.BlockNum = consensusMsg.BlockNum
|
||
6 years ago
|
copy(pbftMsg.BlockHash[:], consensusMsg.BlockHash[:])
|
||
|
pbftMsg.Payload = make([]byte, len(consensusMsg.Payload))
|
||
|
copy(pbftMsg.Payload[:], consensusMsg.Payload[:])
|
||
6 years ago
|
pbftMsg.Block = make([]byte, len(consensusMsg.Block))
|
||
|
copy(pbftMsg.Block[:], consensusMsg.Block[:])
|
||
6 years ago
|
pubKey, err := bls_cosi.BytesToBlsPublicKey(consensusMsg.SenderPubkey)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
pbftMsg.SenderPubkey = pubKey
|
||
|
return &pbftMsg, nil
|
||
|
}
|
||
|
|
||
5 years ago
|
// ParseViewChangeMessage parses view change message into FBFTMessage structure
|
||
|
func ParseViewChangeMessage(msg *msg_pb.Message) (*FBFTMessage, error) {
|
||
|
pbftMsg := FBFTMessage{}
|
||
6 years ago
|
pbftMsg.MessageType = msg.GetType()
|
||
|
if pbftMsg.MessageType != msg_pb.MessageType_VIEWCHANGE {
|
||
|
return nil, fmt.Errorf("ParseViewChangeMessage: incorrect message type %s", pbftMsg.MessageType)
|
||
|
}
|
||
|
|
||
|
vcMsg := msg.GetViewchange()
|
||
6 years ago
|
pbftMsg.ViewID = vcMsg.ViewId
|
||
|
pbftMsg.BlockNum = vcMsg.BlockNum
|
||
6 years ago
|
pbftMsg.Payload = make([]byte, len(vcMsg.Payload))
|
||
|
copy(pbftMsg.Payload[:], vcMsg.Payload[:])
|
||
|
|
||
|
pubKey, err := bls_cosi.BytesToBlsPublicKey(vcMsg.SenderPubkey)
|
||
|
if err != nil {
|
||
6 years ago
|
utils.Logger().Warn().Err(err).Msg("ParseViewChangeMessage failed to parse senderpubkey")
|
||
6 years ago
|
return nil, err
|
||
|
}
|
||
|
leaderKey, err := bls_cosi.BytesToBlsPublicKey(vcMsg.LeaderPubkey)
|
||
|
if err != nil {
|
||
6 years ago
|
utils.Logger().Warn().Err(err).Msg("ParseViewChangeMessage failed to parse leaderpubkey")
|
||
6 years ago
|
return nil, err
|
||
|
}
|
||
|
|
||
|
vcSig := bls.Sign{}
|
||
|
err = vcSig.Deserialize(vcMsg.ViewchangeSig)
|
||
|
if err != nil {
|
||
6 years ago
|
utils.Logger().Warn().Err(err).Msg("ParseViewChangeMessage failed to deserialize the viewchange signature")
|
||
6 years ago
|
return nil, err
|
||
|
}
|
||
6 years ago
|
|
||
|
vcSig1 := bls.Sign{}
|
||
|
err = vcSig1.Deserialize(vcMsg.ViewidSig)
|
||
|
if err != nil {
|
||
6 years ago
|
utils.Logger().Warn().Err(err).Msg("ParseViewChangeMessage failed to deserialize the viewid signature")
|
||
6 years ago
|
return nil, err
|
||
|
}
|
||
6 years ago
|
pbftMsg.SenderPubkey = pubKey
|
||
|
pbftMsg.LeaderPubkey = leaderKey
|
||
|
pbftMsg.ViewchangeSig = &vcSig
|
||
6 years ago
|
pbftMsg.ViewidSig = &vcSig1
|
||
6 years ago
|
return &pbftMsg, nil
|
||
|
}
|
||
|
|
||
5 years ago
|
// ParseNewViewMessage parses new view message into FBFTMessage structure
|
||
|
func (consensus *Consensus) ParseNewViewMessage(msg *msg_pb.Message) (*FBFTMessage, error) {
|
||
|
FBFTMsg := FBFTMessage{}
|
||
|
FBFTMsg.MessageType = msg.GetType()
|
||
6 years ago
|
|
||
5 years ago
|
if FBFTMsg.MessageType != msg_pb.MessageType_NEWVIEW {
|
||
|
return nil, fmt.Errorf("ParseNewViewMessage: incorrect message type %s", FBFTMsg.MessageType)
|
||
6 years ago
|
}
|
||
|
|
||
|
vcMsg := msg.GetViewchange()
|
||
5 years ago
|
FBFTMsg.ViewID = vcMsg.ViewId
|
||
|
FBFTMsg.BlockNum = vcMsg.BlockNum
|
||
|
FBFTMsg.Payload = make([]byte, len(vcMsg.Payload))
|
||
|
copy(FBFTMsg.Payload[:], vcMsg.Payload[:])
|
||
6 years ago
|
|
||
|
pubKey, err := bls_cosi.BytesToBlsPublicKey(vcMsg.SenderPubkey)
|
||
|
if err != nil {
|
||
6 years ago
|
utils.Logger().Warn().Err(err).Msg("ParseViewChangeMessage failed to parse senderpubkey")
|
||
6 years ago
|
return nil, err
|
||
|
}
|
||
5 years ago
|
FBFTMsg.SenderPubkey = pubKey
|
||
6 years ago
|
|
||
6 years ago
|
if len(vcMsg.M3Aggsigs) > 0 {
|
||
|
m3Sig := bls.Sign{}
|
||
|
err = m3Sig.Deserialize(vcMsg.M3Aggsigs)
|
||
6 years ago
|
if err != nil {
|
||
6 years ago
|
utils.Logger().Warn().Err(err).Msg("ParseViewChangeMessage failed to deserialize the multi signature for M3 viewID signature")
|
||
6 years ago
|
return nil, err
|
||
|
}
|
||
5 years ago
|
m3mask, err := bls_cosi.NewMask(consensus.Decider.Participants(), nil)
|
||
6 years ago
|
if err != nil {
|
||
6 years ago
|
utils.Logger().Warn().Err(err).Msg("ParseViewChangeMessage failed to create mask for multi signature")
|
||
6 years ago
|
return nil, err
|
||
|
}
|
||
6 years ago
|
m3mask.SetMask(vcMsg.M3Bitmap)
|
||
5 years ago
|
FBFTMsg.M3AggSig = &m3Sig
|
||
|
FBFTMsg.M3Bitmap = m3mask
|
||
6 years ago
|
}
|
||
|
|
||
|
if len(vcMsg.M2Aggsigs) > 0 {
|
||
|
m2Sig := bls.Sign{}
|
||
|
err = m2Sig.Deserialize(vcMsg.M2Aggsigs)
|
||
|
if err != nil {
|
||
6 years ago
|
utils.Logger().Warn().Err(err).Msg("ParseViewChangeMessage failed to deserialize the multi signature for M2 aggregated signature")
|
||
6 years ago
|
return nil, err
|
||
|
}
|
||
5 years ago
|
m2mask, err := bls_cosi.NewMask(consensus.Decider.Participants(), nil)
|
||
6 years ago
|
if err != nil {
|
||
6 years ago
|
utils.Logger().Warn().Err(err).Msg("ParseViewChangeMessage failed to create mask for multi signature")
|
||
6 years ago
|
return nil, err
|
||
|
}
|
||
|
m2mask.SetMask(vcMsg.M2Bitmap)
|
||
5 years ago
|
FBFTMsg.M2AggSig = &m2Sig
|
||
|
FBFTMsg.M2Bitmap = m2mask
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
5 years ago
|
return &FBFTMsg, nil
|
||
6 years ago
|
}
|