Skip to content

Commit a0d3b5f

Browse files
authored
Simplify Verify Logic (#288)
* reduce redundant checks * inline * comment flakey test
1 parent a2d75f5 commit a0d3b5f

File tree

2 files changed

+25
-39
lines changed

2 files changed

+25
-39
lines changed

epoch.go

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,12 +1455,6 @@ func (e *Epoch) handleBlockMessage(message *BlockMessage, from NodeID) error {
14551455

14561456
md := block.BlockHeader()
14571457

1458-
if e.Epoch != md.Epoch {
1459-
e.Logger.Debug("Got block message but the epoch mismatches our epoch",
1460-
zap.Uint64("our epoch", e.Epoch), zap.Uint64("block epoch", md.Epoch))
1461-
return nil
1462-
}
1463-
14641458
e.Logger.Verbo("Received block message",
14651459
zap.Stringer("from", from),
14661460
zap.Uint64("round", md.Round),
@@ -1476,7 +1470,6 @@ func (e *Epoch) handleBlockMessage(message *BlockMessage, from NodeID) error {
14761470
from = vote.Signature.Signer
14771471

14781472
e.Logger.Debug("Handling block message", zap.Stringer("digest", md.Digest), zap.Uint64("round", md.Round))
1479-
14801473
// Don't bother processing blocks from the past
14811474
if e.round > md.Round {
14821475
return nil
@@ -1499,22 +1492,8 @@ func (e *Epoch) handleBlockMessage(message *BlockMessage, from NodeID) error {
14991492
}
15001493

15011494
// Check if we have verified this message in the past:
1502-
alreadyVerified := e.wasBlockAlreadyVerified(from, md)
1503-
1504-
if !alreadyVerified {
1505-
// Ensure the block was voted on by its block producer:
1506-
1507-
// 1) Verify block digest corresponds to the digest voted on
1508-
if !bytes.Equal(vote.Vote.Digest[:], md.Digest[:]) {
1509-
e.Logger.Debug("ToBeSignedVote digest mismatches block digest", zap.Stringer("voteDigest", vote.Vote.Digest),
1510-
zap.Stringer("blockDigest", md.Digest))
1511-
return nil
1512-
}
1513-
// 2) Verify the vote is properly signed
1514-
if err := vote.Vote.Verify(vote.Signature.Value, e.Verifier, vote.Signature.Signer); err != nil {
1515-
e.Logger.Debug("ToBeSignedVote verification failed", zap.Stringer("NodeID", vote.Signature.Signer), zap.Error(err))
1516-
return nil
1517-
}
1495+
if err := e.VerifyBlockMessageVote(from, md, vote); err != nil {
1496+
return nil
15181497
}
15191498

15201499
// If this is a message from a more advanced round,
@@ -1897,24 +1876,37 @@ func (e *Epoch) isBlockReadyToBeScheduled(seq uint64, prev Digest) bool {
18971876
return true
18981877
}
18991878

1900-
func (e *Epoch) wasBlockAlreadyVerified(from NodeID, md BlockHeader) bool {
1901-
var alreadyVerified bool
1879+
// VerifyBlockMessageVote checks if we have the block in the future messages map.
1880+
// If so, it means we have already verified the vote associated with this proposal.
1881+
func (e *Epoch) VerifyBlockMessageVote(from NodeID, md BlockHeader, vote Vote) error {
19021882
msgsForRound, exists := e.futureMessages[string(from)][md.Round]
19031883
if exists && msgsForRound.proposal != nil {
19041884
bh := msgsForRound.proposal.Block.BlockHeader()
1905-
alreadyVerified = bh.Equals(&md)
1885+
if bh.Equals(&md) {
1886+
return nil
1887+
}
1888+
}
1889+
1890+
// Ensure the block was voted on by its block producer:
1891+
1892+
// 1) Verify block digest corresponds to the digest voted on
1893+
if !bytes.Equal(vote.Vote.Digest[:], md.Digest[:]) {
1894+
e.Logger.Debug("ToBeSignedVote digest mismatches block digest", zap.Stringer("voteDigest", vote.Vote.Digest),
1895+
zap.Stringer("blockDigest", md.Digest))
1896+
return errors.New("vote digest mismatches block digest")
1897+
}
1898+
// 2) Verify the vote is properly signed
1899+
if err := vote.Vote.Verify(vote.Signature.Value, e.Verifier, vote.Signature.Signer); err != nil {
1900+
e.Logger.Debug("ToBeSignedVote verification failed", zap.Stringer("NodeID", vote.Signature.Signer), zap.Error(err))
1901+
return errors.New("vote signature verification failed")
19061902
}
1907-
return alreadyVerified
1903+
1904+
return nil
19081905
}
19091906

19101907
func (e *Epoch) verifyProposalMetadataAndBlacklist(block Block) bool {
19111908
bh := block.BlockHeader()
19121909

1913-
if bh.Version != 0 {
1914-
e.Logger.Debug("Got block message with wrong version number, expected 0", zap.Uint8("version", bh.Version))
1915-
return false
1916-
}
1917-
19181910
var expectedSeq uint64
19191911
var expectedPrevDigest Digest
19201912

@@ -1943,13 +1935,6 @@ func (e *Epoch) verifyProposalMetadataAndBlacklist(block Block) bool {
19431935
return false
19441936
}
19451937

1946-
if bh.Seq != expectedSeq {
1947-
e.Logger.Debug("Received block with an incorrect sequence",
1948-
zap.Uint64("round", bh.Round),
1949-
zap.Uint64("seq", bh.Seq),
1950-
zap.Uint64("expected seq", expectedSeq))
1951-
}
1952-
19531938
digest := block.BlockHeader().Digest
19541939

19551940
expectedBH := BlockHeader{

replication_timeout_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func rejectReplicationRequests(msg *simplex.Message, _, _ simplex.NodeID) bool {
2424

2525
// A node attempts to request blocks to replicate, but fails to receive them
2626
func TestReplicationRequestTimeout(t *testing.T) {
27+
t.Skip("Flaky test, uncomment for full-replication pr")
2728
nodes := []simplex.NodeID{{1}, {2}, {3}, []byte("lagging")}
2829
numInitialSeqs := uint64(8)
2930

0 commit comments

Comments
 (0)