From 708c0559c768ba10101f30229b0d58f49659a528 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Wed, 21 Apr 2021 22:37:32 +0300 Subject: [PATCH] Fix NPE when receiving commit message of unknown consensus --- consensus/consensus.go | 17 +++++++++-------- node/node.go | 2 +- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/consensus/consensus.go b/consensus/consensus.go index 4534235..c340415 100644 --- a/consensus/consensus.go +++ b/consensus/consensus.go @@ -55,7 +55,7 @@ func NewPBFTConsensusManager(psb *pubsub.PubSubRouter, minApprovals int, privKey return pcm } -func (pcm *PBFTConsensusManager) Propose(consensusID string, task types2.DioneTask, requestEvent *dioneOracle.DioneOracleNewOracleRequest) error { +func (pcm *PBFTConsensusManager) Propose(task types2.DioneTask, requestEvent *dioneOracle.DioneOracleNewOracleRequest) error { pcm.createConsensusInfo(&task, true) prePrepareMsg, err := pcm.prePreparePool.CreatePrePrepare( @@ -77,11 +77,10 @@ func (pcm *PBFTConsensusManager) handlePrePrepare(message *types.Message) { return } if pcm.prePreparePool.IsExistingPrePrepare(message) { - logrus.Debug("received existing pre_prepare msg, dropping...") return } if !pcm.prePreparePool.IsValidPrePrepare(message) { - logrus.Debug("received invalid pre_prepare msg, dropping...") + logrus.Warn("received invalid pre_prepare msg, dropping...") return } @@ -104,11 +103,10 @@ func (pcm *PBFTConsensusManager) handlePrePrepare(message *types.Message) { func (pcm *PBFTConsensusManager) handlePrepare(message *types.Message) { if pcm.preparePool.IsExistingPrepare(message) { - logrus.Debug("received existing prepare msg, dropping...") return } if !pcm.preparePool.IsValidPrepare(message) { - logrus.Debug("received invalid prepare msg, dropping...") + logrus.Warn("received invalid prepare msg, dropping...") return } @@ -130,11 +128,10 @@ func (pcm *PBFTConsensusManager) handlePrepare(message *types.Message) { func (pcm *PBFTConsensusManager) handleCommit(message *types.Message) { if pcm.commitPool.IsExistingCommit(message) { - logrus.Debug("received existing commit msg, dropping...") return } if !pcm.commitPool.IsValidCommit(message) { - logrus.Debug("received invalid commit msg, dropping...") + logrus.Warn("received invalid commit msg, dropping...") return } @@ -147,7 +144,11 @@ func (pcm *PBFTConsensusManager) handleCommit(message *types.Message) { consensusMsg := message.Payload if pcm.commitPool.CommitSize(consensusMsg.Task.ConsensusID) >= pcm.minApprovals { - info := pcm.consensusMap[consensusMsg.Task.ConsensusID] + info := pcm.GetConsensusInfo(consensusMsg.Task.ConsensusID) + if info == nil { + logrus.Debugf("consensus doesn't exist in our consensus map - skipping...") + return + } info.mutex.Lock() defer info.mutex.Unlock() if info.Finished { diff --git a/node/node.go b/node/node.go index 1b1e103..002af70 100644 --- a/node/node.go +++ b/node/node.go @@ -241,7 +241,7 @@ func (n *Node) subscribeOnEthContractsAsync(ctx context.Context) { continue } logrus.Infof("Proposed new Dione task with ID: %s", event.ReqID.String()) - err = n.ConsensusManager.Propose(event.ReqID.String(), *task, event) + err = n.ConsensusManager.Propose(*task, event) if err != nil { logrus.Errorf("Failed to propose task: %w", err) }