Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added checks on proofs parameters like epoch, nonce and shard #6698

Open
wants to merge 3 commits into
base: equivalent-proofs-feat-stabilization
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions process/block/baseProcess.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,34 +222,35 @@ func (bp *baseProcessor) checkBlockValidity(
return process.ErrEpochDoesNotMatch
}

return bp.checkPrevProofValidity(headerHandler)
return bp.checkPrevProofValidity(currentBlockHeader, headerHandler)
}

func (bp *baseProcessor) checkPrevProofValidity(headerHandler data.HeaderHandler) error {
if !bp.enableEpochsHandler.IsFlagEnabledInEpoch(common.EquivalentMessagesFlag, headerHandler.GetEpoch()) {
func (bp *baseProcessor) checkPrevProofValidity(prevHeader, headerHandler data.HeaderHandler) error {
if !common.ShouldBlockHavePrevProof(headerHandler, bp.enableEpochsHandler, common.EquivalentMessagesFlag) {
return nil
}

prevProof := headerHandler.GetPreviousProof()
if check.IfNilReflect(prevProof) {
return process.ErrMissingHeaderProof
}
return bp.verifyProofAgainstHeader(prevProof, prevHeader)
}

headersPool := bp.dataPool.Headers()
prevHeader, err := headersPool.GetHeaderByHash(prevProof.GetHeaderHash())
if err != nil {
return fmt.Errorf("%w while getting header for proof hash %s", err, hex.EncodeToString(prevProof.GetHeaderHash()))
func (bp *baseProcessor) verifyProofAgainstHeader(proof data.HeaderProofHandler, header data.HeaderHandler) error {
if check.IfNilReflect(proof) {
return process.ErrMissingHeaderProof
}

if prevProof.GetHeaderNonce() != prevHeader.GetNonce() {
if proof.GetHeaderNonce() != header.GetNonce() {
return fmt.Errorf("%w, nonce mismatch", process.ErrInvalidHeaderProof)
}
if prevProof.GetHeaderShardId() != prevHeader.GetShardID() {
if proof.GetHeaderShardId() != header.GetShardID() {
return fmt.Errorf("%w, shard id mismatch", process.ErrInvalidHeaderProof)
}
if prevProof.GetHeaderEpoch() != prevHeader.GetEpoch() {
if proof.GetHeaderEpoch() != header.GetEpoch() {
return fmt.Errorf("%w, epoch mismatch", process.ErrInvalidHeaderProof)
}
if proof.GetHeaderRound() != header.GetRound() {
return fmt.Errorf("%w, round mismatch", process.ErrInvalidHeaderProof)
}

return nil
}
Expand Down
3 changes: 3 additions & 0 deletions process/block/interceptedBlocks/interceptedEquivalentProof.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ func (iep *interceptedEquivalentProof) checkHeaderParamsFromProof() error {
if iep.proof.GetHeaderEpoch() != header.GetEpoch() {
return fmt.Errorf("%w, epoch mismatch", ErrInvalidProof)
}
if iep.proof.GetHeaderRound() != header.GetRound() {
return fmt.Errorf("%w, round mismatch", ErrInvalidProof)
}

return nil
}
Expand Down
30 changes: 30 additions & 0 deletions process/block/interceptedBlocks/interceptedEquivalentProof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var (
providedEpoch = uint32(123)
providedNonce = uint64(345)
providedShard = uint32(0)
providedRound = uint64(123456)
)

func createMockDataBuff() []byte {
Expand All @@ -38,6 +39,7 @@ func createMockDataBuff() []byte {
HeaderEpoch: providedEpoch,
HeaderNonce: providedNonce,
HeaderShardId: providedShard,
HeaderRound: providedRound,
}

dataBuff, _ := testMarshaller.Marshal(proof)
Expand All @@ -55,6 +57,7 @@ func createMockArgInterceptedEquivalentProof() ArgInterceptedEquivalentProof {
GetHeaderByHashCalled: func(hash []byte) (data.HeaderHandler, error) {
return &testscommon.HeaderHandlerStub{
EpochField: providedEpoch,
RoundField: providedRound,
GetNonceCalled: func() uint64 {
return providedNonce
},
Expand Down Expand Up @@ -284,6 +287,33 @@ func TestInterceptedEquivalentProof_CheckValidity(t *testing.T) {
require.True(t, strings.Contains(err.Error(), "epoch mismatch"))
})

t.Run("round mismatch should error", func(t *testing.T) {
t.Parallel()

args := createMockArgInterceptedEquivalentProof()
args.Headers = &pool.HeadersPoolStub{
GetHeaderByHashCalled: func(hash []byte) (data.HeaderHandler, error) {
return &testscommon.HeaderHandlerStub{
GetNonceCalled: func() uint64 {
return providedNonce
},
GetShardIDCalled: func() uint32 {
return providedShard
},
EpochField: providedEpoch,
RoundField: providedRound + 1,
}, nil
},
}

iep, err := NewInterceptedEquivalentProof(args)
require.NoError(t, err)

err = iep.CheckValidity()
require.True(t, errors.Is(err, ErrInvalidProof))
require.True(t, strings.Contains(err.Error(), "round mismatch"))
})

t.Run("should work", func(t *testing.T) {
t.Parallel()

Expand Down
12 changes: 12 additions & 0 deletions process/block/metablock.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,18 @@ func (mp *metaProcessor) checkProofsForShardData(header *block.MetaBlock) error
if !mp.proofsPool.HasProof(shardData.ShardID, shardData.HeaderHash) {
return fmt.Errorf("%w for header hash %s", process.ErrMissingHeaderProof, hex.EncodeToString(shardData.HeaderHash))
}

prevProof := shardData.GetPreviousProof()
headersPool := mp.dataPool.Headers()
prevHeader, err := headersPool.GetHeaderByHash(prevProof.GetHeaderHash())
Comment on lines 430 to +436
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here we check if there is shardData.HeaderHash proof in pool, and then we check proof fields on previousProof, is this ok?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, the proof from pool should have already been validated

if err != nil {
return err
}

err = mp.verifyProofAgainstHeader(prevProof, prevHeader)
if err != nil {
return err
}
}

return nil
Expand Down
Loading