Skip to content

Commit

Permalink
Merge pull request #6712 from multiversx/add-proof-update
Browse files Browse the repository at this point in the history
Change AddProof
  • Loading branch information
ssd04 authored Jan 20, 2025
2 parents fd92b5a + e509e47 commit 2a1a875
Show file tree
Hide file tree
Showing 18 changed files with 44 additions and 42 deletions.
3 changes: 3 additions & 0 deletions common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ var ErrNilStateSyncNotifierSubscriber = errors.New("nil state sync notifier subs

// ErrInvalidHeaderProof signals that an invalid equivalent proof has been provided
var ErrInvalidHeaderProof = errors.New("invalid equivalent proof")

// ErrAlreadyExistingEquivalentProof signals that the provided proof was already exiting in the pool
var ErrAlreadyExistingEquivalentProof = errors.New("already existing equivalent proof")
2 changes: 1 addition & 1 deletion consensus/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ type KeysHandler interface {

// EquivalentProofsPool defines the behaviour of a proofs pool components
type EquivalentProofsPool interface {
AddProof(headerProof data.HeaderProofHandler) error
AddProof(headerProof data.HeaderProofHandler) bool
GetProof(shardID uint32, headerHash []byte) (data.HeaderProofHandler, error)
HasProof(shardID uint32, headerHash []byte) bool
IsInterfaceNil() bool
Expand Down
7 changes: 4 additions & 3 deletions consensus/spos/bls/v2/subroundBlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v2
import (
"bytes"
"context"
"encoding/hex"
"time"

"github.com/multiversx/mx-chain-core-go/core"
Expand Down Expand Up @@ -367,9 +368,9 @@ func (sr *subroundBlock) saveProofForPreviousHeaderIfNeeded(header data.HeaderHa
return
}

err = sr.EquivalentProofsPool().AddProof(proof)
if err != nil {
log.Debug("saveProofForPreviousHeaderIfNeeded: failed to add proof, %w", err)
ok := sr.EquivalentProofsPool().AddProof(proof)
if !ok {
log.Debug("saveProofForPreviousHeaderIfNeeded: proof not added", "headerHash", hex.EncodeToString(proof.GetHeaderHash()))
}
}

Expand Down
6 changes: 3 additions & 3 deletions consensus/spos/bls/v2/subroundEndRound.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,9 @@ func (sr *subroundEndRound) doEndRoundJobByNode() bool {

// if proof not nil, it was created and broadcasted so it has to be added to the pool
if proof != nil {
err = sr.EquivalentProofsPool().AddProof(proof)
if err != nil {
log.Debug("doEndRoundJobByNode.AddProof", "error", err)
ok := sr.EquivalentProofsPool().AddProof(proof)
if !ok {
log.Trace("doEndRoundJobByNode.AddProof", "added", ok)
}
}

Expand Down
4 changes: 2 additions & 2 deletions consensus/spos/bls/v2/subroundEndRound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1244,11 +1244,11 @@ func TestSubroundEndRound_DoEndRoundJobByNode(t *testing.T) {

wasSetCurrentHeaderProofCalled := false
container.SetEquivalentProofsPool(&dataRetriever.ProofsPoolMock{
AddProofCalled: func(headerProof data.HeaderProofHandler) error {
AddProofCalled: func(headerProof data.HeaderProofHandler) bool {
wasSetCurrentHeaderProofCalled = true
require.NotEqual(t, providedPrevSig, headerProof.GetAggregatedSignature())
require.NotEqual(t, providedPrevBitmap, headerProof.GetPubKeysBitmap())
return nil
return true
},
})

Expand Down
3 changes: 0 additions & 3 deletions dataRetriever/dataPool/proofsCache/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,3 @@ var ErrMissingProof = errors.New("missing proof")

// ErrNilProof signals that a nil proof has been provided
var ErrNilProof = errors.New("nil proof provided")

// ErrAlreadyExistingEquivalentProof signals that the provided proof was already exiting in the pool
var ErrAlreadyExistingEquivalentProof = errors.New("already existing equivalent proof")
9 changes: 4 additions & 5 deletions dataRetriever/dataPool/proofsCache/proofsPool.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proofscache

import (
"encoding/hex"
"fmt"
"sync"

Expand Down Expand Up @@ -31,17 +30,17 @@ func NewProofsPool() *proofsPool {
// AddProof will add the provided proof to the pool
func (pp *proofsPool) AddProof(
headerProof data.HeaderProofHandler,
) error {
) bool {
if check.IfNilReflect(headerProof) {
return ErrNilProof
return false
}

shardID := headerProof.GetHeaderShardId()
headerHash := headerProof.GetHeaderHash()

hasProof := pp.HasProof(shardID, headerHash)
if hasProof {
return fmt.Errorf("%w, headerHash: %s", ErrAlreadyExistingEquivalentProof, hex.EncodeToString(headerHash))
return false
}

pp.mutCache.Lock()
Expand All @@ -65,7 +64,7 @@ func (pp *proofsPool) AddProof(

pp.callAddedProofSubscribers(headerProof)

return nil
return true
}

func (pp *proofsPool) callAddedProofSubscribers(headerProof data.HeaderProofHandler) {
Expand Down
4 changes: 2 additions & 2 deletions dataRetriever/dataPool/proofsCache/proofsPool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func TestProofsPool_ShouldWork(t *testing.T) {
_ = pp.AddProof(proof3)
_ = pp.AddProof(proof4)

err := pp.AddProof(proof4)
require.True(t, errors.Is(err, proofscache.ErrAlreadyExistingEquivalentProof))
ok := pp.AddProof(proof4)
require.False(t, ok)

proof, err := pp.GetProof(shardID, []byte("hash3"))
require.Nil(t, err)
Expand Down
2 changes: 1 addition & 1 deletion dataRetriever/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ type PeerAuthenticationPayloadValidator interface {

// ProofsPool defines the behaviour of a proofs pool components
type ProofsPool interface {
AddProof(headerProof data.HeaderProofHandler) error
AddProof(headerProof data.HeaderProofHandler) bool
RegisterHandler(handler func(headerProof data.HeaderProofHandler))
CleanupProofsBehindNonce(shardID uint32, nonce uint64) error
GetProof(shardID uint32, headerHash []byte) (data.HeaderProofHandler, error)
Expand Down
7 changes: 3 additions & 4 deletions integrationTests/testProcessorNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -2833,11 +2833,10 @@ func (tpn *TestProcessorNode) setBlockSignatures(blockHeader data.HeaderHandler)
}
blockHeader.SetPreviousProof(previousProof)

err = tpn.ProofsPool.AddProof(previousProof)
if err != nil {
log.Warn("ProofsPool.AddProof", "currHdrHash", currHdrHash, "node", tpn.OwnAccount.Address, "err", err.Error())
wasAdded := tpn.ProofsPool.AddProof(previousProof)
if !wasAdded {
log.Warn("ProofsPool.AddProof not added", "currHdrHash", currHdrHash, "node", tpn.OwnAccount.Address)
}
return err
}

err = blockHeader.SetPubKeysBitmap(pubKeysBitmap)
Expand Down
4 changes: 2 additions & 2 deletions process/block/interceptedBlocks/interceptedEquivalentProof.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
logger "github.com/multiversx/mx-chain-logger-go"
"github.com/multiversx/mx-chain-vm-v1_2-go/ipc/marshaling"

"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/consensus"
"github.com/multiversx/mx-chain-go/dataRetriever"
proofscache "github.com/multiversx/mx-chain-go/dataRetriever/dataPool/proofsCache"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/sharding"
)
Expand Down Expand Up @@ -137,7 +137,7 @@ func (iep *interceptedEquivalentProof) CheckValidity() error {

ok := iep.proofsPool.HasProof(iep.proof.GetHeaderShardId(), iep.proof.GetHeaderHash())
if ok {
return proofscache.ErrAlreadyExistingEquivalentProof
return common.ErrAlreadyExistingEquivalentProof
}

// TODO: make sure proof fields (besides ones used to verify signature) should be checked on processing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
logger "github.com/multiversx/mx-chain-logger-go"
"github.com/stretchr/testify/require"

"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/consensus/mock"
proofscache "github.com/multiversx/mx-chain-go/dataRetriever/dataPool/proofsCache"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/testscommon"
"github.com/multiversx/mx-chain-go/testscommon/consensus"
Expand Down Expand Up @@ -202,7 +202,7 @@ func TestInterceptedEquivalentProof_CheckValidity(t *testing.T) {
require.NoError(t, err)

err = iep.CheckValidity()
require.Equal(t, proofscache.ErrAlreadyExistingEquivalentProof, err)
require.Equal(t, common.ErrAlreadyExistingEquivalentProof, err)
})
t.Run("should work", func(t *testing.T) {
t.Parallel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-core-go/marshal"
"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/process"
)

Expand Down Expand Up @@ -56,7 +57,12 @@ func (epip *equivalentProofsInterceptorProcessor) Save(data process.InterceptedD
return process.ErrWrongTypeAssertion
}

return epip.equivalentProofsPool.AddProof(interceptedProof.GetProof())
wasAdded := epip.equivalentProofsPool.AddProof(interceptedProof.GetProof())
if !wasAdded {
return common.ErrAlreadyExistingEquivalentProof
}

return nil
}

// RegisterHandler registers a callback function to be notified of incoming equivalent proofs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ func TestEquivalentProofsInterceptorProcessor_Save(t *testing.T) {
wasCalled := false
args := createMockArgEquivalentProofsInterceptorProcessor()
args.EquivalentProofsPool = &dataRetriever.ProofsPoolMock{
AddProofCalled: func(notarizedProof data.HeaderProofHandler) error {
AddProofCalled: func(notarizedProof data.HeaderProofHandler) bool {
wasCalled = true
return nil
return true
},
}
epip, err := NewEquivalentProofsInterceptorProcessor(args)
Expand Down
7 changes: 2 additions & 5 deletions process/interceptors/processor/hdrInterceptorProcessor.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package processor

import (
"reflect"
"sync"

"github.com/multiversx/mx-chain-core-go/core"
Expand Down Expand Up @@ -82,10 +81,8 @@ func (hip *HdrInterceptorProcessor) Save(data process.InterceptedData, _ core.Pe
hip.headers.AddHeader(interceptedHdr.Hash(), interceptedHdr.HeaderHandler())

if common.ShouldBlockHavePrevProof(interceptedHdr.HeaderHandler(), hip.enableEpochsHandler, common.EquivalentMessagesFlag) {
err := hip.proofs.AddProof(interceptedHdr.HeaderHandler().GetPreviousProof())
if err != nil {
log.Error("failed to add proof", "error", err, "intercepted header hash", interceptedHdr.Hash(), "header type", reflect.TypeOf(interceptedHdr.HeaderHandler()))
}
ok = hip.proofs.AddProof(interceptedHdr.HeaderHandler().GetPreviousProof())
log.Trace("HdrInterceptorProcessor.AddProof: add previous proof", "intercepted header hash", interceptedHdr.Hash(), "added", ok)
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@ func TestHdrInterceptorProcessor_SaveShouldWork(t *testing.T) {

wasAddedProofs := false
arg.Proofs = &dataRetriever.ProofsPoolMock{
AddProofCalled: func(headerProof data.HeaderProofHandler) error {
AddProofCalled: func(headerProof data.HeaderProofHandler) bool {
wasAddedProofs = true
return nil
return true
},
}

Expand Down
2 changes: 1 addition & 1 deletion process/interceptors/processor/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type interceptedEquivalentProof interface {

// EquivalentProofsPool defines the behaviour of a proofs pool components
type EquivalentProofsPool interface {
AddProof(headerProof data.HeaderProofHandler) error
AddProof(headerProof data.HeaderProofHandler) bool
CleanupProofsBehindNonce(shardID uint32, nonce uint64) error
GetProof(shardID uint32, headerHash []byte) (data.HeaderProofHandler, error)
HasProof(shardID uint32, headerHash []byte) bool
Expand Down
6 changes: 3 additions & 3 deletions testscommon/dataRetriever/proofsPoolMock.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ import (

// ProofsPoolMock -
type ProofsPoolMock struct {
AddProofCalled func(headerProof data.HeaderProofHandler) error
AddProofCalled func(headerProof data.HeaderProofHandler) bool
CleanupProofsBehindNonceCalled func(shardID uint32, nonce uint64) error
GetProofCalled func(shardID uint32, headerHash []byte) (data.HeaderProofHandler, error)
HasProofCalled func(shardID uint32, headerHash []byte) bool
RegisterHandlerCalled func(handler func(headerProof data.HeaderProofHandler))
}

// AddProof -
func (p *ProofsPoolMock) AddProof(headerProof data.HeaderProofHandler) error {
func (p *ProofsPoolMock) AddProof(headerProof data.HeaderProofHandler) bool {
if p.AddProofCalled != nil {
return p.AddProofCalled(headerProof)
}

return nil
return true
}

// CleanupProofsBehindNonce -
Expand Down

0 comments on commit 2a1a875

Please sign in to comment.