Skip to content

Commit

Permalink
Merge pull request #766 from ElrondNetwork/rc-BoN-fix-probable-highes…
Browse files Browse the repository at this point in the history
…t-nonce

Rc bon fix probable highest nonce
  • Loading branch information
iulianpascalau authored Dec 8, 2019
2 parents 0683af4 + 24afc4a commit 8961a46
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 19 deletions.
3 changes: 3 additions & 0 deletions consensus/spos/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ var ErrMessageForPastRound = errors.New("message is for past round")
// ErrInvalidSignature is raised when signature is invalid
var ErrInvalidSignature = errors.New("signature is invalid")

// ErrInvalidHeader is raised when header is invalid
var ErrInvalidHeader = errors.New("header is invalid")

// ErrMessageFromItself is raised when a message from itself is received
var ErrMessageFromItself = errors.New("message is from itself")

Expand Down
29 changes: 16 additions & 13 deletions consensus/spos/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,21 +267,24 @@ func (wrk *Worker) ProcessReceivedMessage(message p2p.MessageP2P, _ func(buffToS

//TODO: Block validity should be checked here and also on interceptors side, taking into consideration the following:
//(previous random seed, round, shard id and current random seed to verify if the block has been sent by the right proposer)
isHeaderValid := !check.IfNil(header) && headerHash != nil
if isHeaderValid {
errNotCritical := wrk.forkDetector.AddHeader(header, headerHash, process.BHProposed, nil, nil, false)
if errNotCritical != nil {
log.Debug("add header in forkdetector", "error", errNotCritical.Error())
}
isHeaderInvalid := check.IfNil(header) || headerHash == nil
if isHeaderInvalid {
return ErrInvalidHeader
}

log.Debug("received proposed block",
"from", core.GetTrimmedPk(core.ToHex(cnsDta.PubKey)),
"header hash", cnsDta.BlockHeaderHash,
"round", header.GetRound(),
"nonce", header.GetNonce(),
"prev hash", header.GetPrevHash(),
)
err = wrk.forkDetector.AddHeader(header, headerHash, process.BHProposed, nil, nil, false)
if err != nil {
log.Trace("add header in forkdetector", "error", err.Error())
return err
}

log.Debug("received proposed block",
"from", core.GetTrimmedPk(core.ToHex(cnsDta.PubKey)),
"header hash", cnsDta.BlockHeaderHash,
"round", header.GetRound(),
"nonce", header.GetNonce(),
"prev hash", header.GetPrevHash(),
)
}

if wrk.consensusService.IsMessageWithSignature(msgType) {
Expand Down
34 changes: 33 additions & 1 deletion consensus/spos/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ func TestWorker_ProcessReceivedMessageWhenRoundIsCanceledShouldRetNilAndNotProce
assert.Nil(t, err)
}

func TestWorker_ProcessReceivedMessageOkValsShouldWork(t *testing.T) {
func TestWorker_ProcessReceivedMessageErrHeaderIsInvalid(t *testing.T) {
t.Parallel()
wrk := *initWorker()
blk := make(block.Body, 0)
Expand All @@ -840,6 +840,38 @@ func TestWorker_ProcessReceivedMessageOkValsShouldWork(t *testing.T) {
err := wrk.ProcessReceivedMessage(&mock.P2PMessageMock{DataField: buff}, nil)
time.Sleep(time.Second)

assert.Equal(t, 0, len(wrk.ReceivedMessages()[bn.MtBlockHeader]))
assert.Equal(t, spos.ErrInvalidHeader, err)
}

func TestWorker_ProcessReceivedMessageOkValsShouldWork(t *testing.T) {
t.Parallel()
wrk := *initWorker()
hdr := block.Header{Nonce: 1, Round: 1}
subRoundData, _ := mock.MarshalizerMock{}.Marshal(hdr)
blkHeaderHash := mock.HasherMock{}.Compute(string(subRoundData))

wrk.SetBlockProcessor(
&mock.BlockProcessorMock{
DecodeBlockHeaderCalled: func(dta []byte) data.HeaderHandler {
return &hdr
},
},
)

cnsMsg := consensus.NewConsensusMessage(
blkHeaderHash,
subRoundData,
[]byte(wrk.ConsensusState().ConsensusGroup()[0]),
[]byte("sig"),
int(bn.MtBlockHeader),
uint64(wrk.Rounder().TimeStamp().Unix()),
0,
)
buff, _ := wrk.Marshalizer().Marshal(cnsMsg)
err := wrk.ProcessReceivedMessage(&mock.P2PMessageMock{DataField: buff}, nil)
time.Sleep(time.Second)

assert.Equal(t, 1, len(wrk.ReceivedMessages()[bn.MtBlockHeader]))
assert.Nil(t, err)
}
Expand Down
5 changes: 0 additions & 5 deletions process/sync/baseForkDetector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"sync"

"github.com/ElrondNetwork/elrond-go/consensus"
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/data"
"github.com/ElrondNetwork/elrond-go/process"
)
Expand Down Expand Up @@ -165,10 +164,6 @@ func (bfd *baseForkDetector) removeCheckpointsBehindNonce(nonce uint64) {
// computeProbableHighestNonce computes the probable highest nonce from the valid received/processed headers
func (bfd *baseForkDetector) computeProbableHighestNonce() uint64 {
probableHighestNonce := bfd.finalCheckpoint().nonce
lastProposedBlockNonce := bfd.lastProposedBlockNonce()
if lastProposedBlockNonce > 0 {
probableHighestNonce = core.MaxUint64(bfd.finalCheckpoint().nonce, lastProposedBlockNonce-1)
}

bfd.mutHeaders.RLock()
for nonce := range bfd.headers {
Expand Down

0 comments on commit 8961a46

Please sign in to comment.