Fix approve count for PREPARE state in consensus

This commit is contained in:
ChronosX88 2021-07-20 01:40:49 +03:00
parent cbce24fdce
commit 550d69fb26
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
4 changed files with 27 additions and 4 deletions

View File

@ -3,6 +3,7 @@ package pool
import (
"bytes"
"encoding/hex"
"fmt"
"time"
"github.com/asaskevich/EventBus"
@ -37,6 +38,7 @@ func (bp *BlockPool) AddBlock(block *types.Block) error {
if err != nil {
return err
}
logrus.WithField("hash", fmt.Sprintf("%x", block.Header.Hash)).Debug("New block discovered")
bp.bus.Publish("blockpool:knownBlockAdded", block)
return nil
}

View File

@ -271,7 +271,10 @@ func (sm *syncManager) onNewBlock(message *pubsub.PubSubMessage) {
err = sm.blockpool.StoreBlock(&block)
if err != nil {
logrus.WithField("err", err.Error()).Error("failed to store block from NewBlock message")
logrus.WithFields(logrus.Fields{
"err": err.Error(),
"blockHash": fmt.Sprintf("%x", block.Header.Hash),
}).Error("failed to store block from NewBlock message")
return
}
}

View File

@ -2,4 +2,4 @@ package config
import "math/big"
var ExpectedLeadersPerEpoch = big.NewInt(5)
var ExpectedLeadersPerEpoch = big.NewInt(1)

View File

@ -3,6 +3,7 @@ package consensus
import (
"encoding/hex"
"errors"
"fmt"
"math/big"
"sync"
@ -122,6 +123,7 @@ func (pcm *PBFTConsensusManager) propose(blk *types3.Block) error {
}
pcm.psb.BroadcastToServiceTopic(prePrepareMsg)
pcm.blockPool.AddBlock(blk)
logrus.WithField("blockHash", fmt.Sprintf("%x", blk.Header.Hash)).Debugf("Entered into PREPREPARED state")
pcm.state.status = StateStatusPrePrepared
return nil
}
@ -157,6 +159,10 @@ func (pcm *PBFTConsensusManager) handlePrePrepare(message *pubsub.PubSubMessage)
}
pcm.msgLog.AddMessage(cmsg)
logrus.WithFields(logrus.Fields{
"blockHash": fmt.Sprintf("%x", cmsg.Block.Header.Hash),
"from": message.From.String(),
}).Debug("Received PREPREPARE message")
pcm.blockPool.AddBlock(cmsg.Block)
prepareMsg, err := NewMessage(cmsg, types.ConsensusMessageTypePrepare, pcm.privKey)
@ -165,6 +171,7 @@ func (pcm *PBFTConsensusManager) handlePrePrepare(message *pubsub.PubSubMessage)
return
}
logrus.WithField("blockHash", fmt.Sprintf("%x", prePrepare.Block.Header.Hash)).Debugf("Entered into PREPREPARED state")
pcm.psb.BroadcastToServiceTopic(prepareMsg)
pcm.state.status = StateStatusPrePrepared
}
@ -187,7 +194,7 @@ func (pcm *PBFTConsensusManager) handlePrepare(message *pubsub.PubSubMessage) {
}
if _, err := pcm.blockPool.GetBlock(cmsg.Blockhash); errors.Is(err, cache.ErrNotFound) {
logrus.Warnf("received unknown block %x", cmsg.Blockhash)
logrus.WithField("blockHash", cmsg.Blockhash).Warnf("received unknown block %x", cmsg.Blockhash)
return
}
@ -202,14 +209,19 @@ func (pcm *PBFTConsensusManager) handlePrepare(message *pubsub.PubSubMessage) {
}
pcm.msgLog.AddMessage(cmsg)
logrus.WithFields(logrus.Fields{
"blockHash": fmt.Sprintf("%x", cmsg.Blockhash),
"from": message.From.String(),
}).Debug("Received PREPARE message")
if len(pcm.msgLog.Get(types.ConsensusMessageTypePrepare, cmsg.Blockhash)) >= pcm.minApprovals {
if len(pcm.msgLog.Get(types.ConsensusMessageTypePrepare, cmsg.Blockhash)) >= pcm.minApprovals-1 {
commitMsg, err := NewMessage(cmsg, types.ConsensusMessageTypeCommit, pcm.privKey)
if err != nil {
logrus.Errorf("failed to create commit message: %v", err)
return
}
pcm.psb.BroadcastToServiceTopic(commitMsg)
logrus.WithField("blockHash", fmt.Sprintf("%x", cmsg.Blockhash)).Debugf("Entered into PREPARED state")
pcm.state.status = StateStatusPrepared
}
}
@ -247,6 +259,11 @@ func (pcm *PBFTConsensusManager) handleCommit(message *pubsub.PubSubMessage) {
pcm.msgLog.AddMessage(cmsg)
logrus.WithFields(logrus.Fields{
"blockHash": fmt.Sprintf("%x", cmsg.Blockhash),
"from": message.From.String(),
}).Debug("Received COMMIT message")
if len(pcm.msgLog.Get(types.ConsensusMessageTypeCommit, cmsg.Blockhash)) >= pcm.minApprovals {
block, err := pcm.blockPool.GetBlock(cmsg.Blockhash)
if err != nil {
@ -254,6 +271,7 @@ func (pcm *PBFTConsensusManager) handleCommit(message *pubsub.PubSubMessage) {
return
}
pcm.blockPool.AddAcceptedBlock(block)
logrus.WithField("blockHash", fmt.Sprintf("%x", cmsg.Blockhash)).Debugf("Entered into COMMIT state")
pcm.state.status = StateStatusCommited
}
}