Skip to content

Commit

Permalink
check for already existing equivalent proof
Browse files Browse the repository at this point in the history
  • Loading branch information
ssd04 committed Dec 17, 2024
1 parent ebb3fc3 commit 39af8f1
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 13 deletions.
3 changes: 3 additions & 0 deletions dataRetriever/dataPool/proofsCache/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ 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")
2 changes: 1 addition & 1 deletion dataRetriever/dataPool/proofsCache/proofsPool.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (pp *proofsPool) AddProof(

hasProof := pp.HasProof(shardID, headerHash)
if hasProof {
return fmt.Errorf("there was already a valid proof for header, headerHash: %s", hex.EncodeToString(headerHash))
return fmt.Errorf("%w, headerHash: %s", ErrAlreadyExistingEquivalentProof, hex.EncodeToString(headerHash))
}

pp.mutCache.Lock()
Expand Down
3 changes: 3 additions & 0 deletions dataRetriever/dataPool/proofsCache/proofsPool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ func TestProofsPool_ShouldWork(t *testing.T) {
_ = pp.AddProof(proof3)
_ = pp.AddProof(proof4)

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

proof, err := pp.GetProof(shardID, []byte("hash3"))
require.Nil(t, err)
require.Equal(t, proof3, proof)
Expand Down
13 changes: 13 additions & 0 deletions process/block/interceptedBlocks/interceptedEquivalentProof.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/multiversx/mx-chain-core-go/data/block"
"github.com/multiversx/mx-chain-core-go/marshal"
"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"
logger "github.com/multiversx/mx-chain-logger-go"
Expand All @@ -22,12 +24,14 @@ type ArgInterceptedEquivalentProof struct {
Marshaller marshal.Marshalizer
ShardCoordinator sharding.Coordinator
HeaderSigVerifier consensus.HeaderSigVerifier
Proofs dataRetriever.ProofsPool
}

type interceptedEquivalentProof struct {
proof *block.HeaderProof
isForCurrentShard bool
headerSigVerifier consensus.HeaderSigVerifier
proofsPool dataRetriever.ProofsPool
}

// NewInterceptedEquivalentProof returns a new instance of interceptedEquivalentProof
Expand All @@ -46,6 +50,7 @@ func NewInterceptedEquivalentProof(args ArgInterceptedEquivalentProof) (*interce
proof: equivalentProof,
isForCurrentShard: extractIsForCurrentShard(args.ShardCoordinator, equivalentProof),
headerSigVerifier: args.HeaderSigVerifier,
proofsPool: args.Proofs,
}, nil
}

Expand All @@ -62,6 +67,9 @@ func checkArgInterceptedEquivalentProof(args ArgInterceptedEquivalentProof) erro
if check.IfNil(args.HeaderSigVerifier) {
return process.ErrNilHeaderSigVerifier
}
if check.IfNil(args.Proofs) {
return process.ErrNilProofsPool
}

return nil
}
Expand Down Expand Up @@ -101,6 +109,11 @@ func (iep *interceptedEquivalentProof) CheckValidity() error {
return err
}

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

return iep.headerSigVerifier.VerifyHeaderProof(iep.proof)
}

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 @@ -9,8 +9,10 @@ import (
"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/data/block"
"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/consensus"
"github.com/multiversx/mx-chain-go/testscommon/dataRetriever"
"github.com/multiversx/mx-chain-go/testscommon/marshallerMock"
logger "github.com/multiversx/mx-chain-logger-go"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -41,6 +43,7 @@ func createMockArgInterceptedEquivalentProof() ArgInterceptedEquivalentProof {
Marshaller: testMarshaller,
ShardCoordinator: &mock.ShardCoordinatorMock{},
HeaderSigVerifier: &consensus.HeaderSigVerifierMock{},
Proofs: &dataRetriever.ProofsPoolMock{},
}
}

Expand Down Expand Up @@ -93,6 +96,15 @@ func TestNewInterceptedEquivalentProof(t *testing.T) {
require.Equal(t, process.ErrNilHeaderSigVerifier, err)
require.Nil(t, iep)
})
t.Run("nil proofs pool should error", func(t *testing.T) {
t.Parallel()

args := createMockArgInterceptedEquivalentProof()
args.Proofs = nil
iep, err := NewInterceptedEquivalentProof(args)
require.Equal(t, process.ErrNilProofsPool, err)
require.Nil(t, iep)
})
t.Run("unmarshal error should error", func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -134,6 +146,24 @@ func TestInterceptedEquivalentProof_CheckValidity(t *testing.T) {
err = iep.CheckValidity()
require.Equal(t, ErrInvalidProof, err)
})

t.Run("already exiting proof should error", func(t *testing.T) {
t.Parallel()

args := createMockArgInterceptedEquivalentProof()
args.Proofs = &dataRetriever.ProofsPoolMock{
HasProofCalled: func(shardID uint32, headerHash []byte) bool {
return true
},
}

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

err = iep.CheckValidity()
require.Equal(t, proofscache.ErrAlreadyExistingEquivalentProof, err)
})

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

Expand Down
3 changes: 0 additions & 3 deletions process/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -1254,9 +1254,6 @@ var ErrNoMatchingConfigForProvidedEpoch = errors.New("no matching configuration"
// ErrInvalidHeader is raised when header is invalid
var ErrInvalidHeader = errors.New("header is invalid")

// ErrNilEquivalentProofsPool signals that a nil equivalent proofs pool has been provided
var ErrNilEquivalentProofsPool = errors.New("nil equivalent proofs pool")

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ func (bicf *baseInterceptorsContainerFactory) generateValidatorInfoInterceptor()
}

func (bicf *baseInterceptorsContainerFactory) createOneShardEquivalentProofsInterceptor(topic string) (process.Interceptor, error) {
equivalentProofsFactory := interceptorFactory.NewInterceptedEquivalentProofsFactory(*bicf.argInterceptorFactory)
equivalentProofsFactory := interceptorFactory.NewInterceptedEquivalentProofsFactory(*bicf.argInterceptorFactory, bicf.dataPool.Proofs())

marshaller := bicf.argInterceptorFactory.CoreComponents.InternalMarshalizer()
argProcessor := processor.ArgEquivalentProofsInterceptorProcessor{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package factory
import (
"github.com/multiversx/mx-chain-core-go/marshal"
"github.com/multiversx/mx-chain-go/consensus"
"github.com/multiversx/mx-chain-go/dataRetriever"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/process/block/interceptedBlocks"
"github.com/multiversx/mx-chain-go/sharding"
Expand All @@ -12,14 +13,16 @@ type interceptedEquivalentProofsFactory struct {
marshaller marshal.Marshalizer
shardCoordinator sharding.Coordinator
headerSigVerifier consensus.HeaderSigVerifier
proofsPool dataRetriever.ProofsPool
}

// NewInterceptedEquivalentProofsFactory creates a new instance of interceptedEquivalentProofsFactory
func NewInterceptedEquivalentProofsFactory(args ArgInterceptedDataFactory) *interceptedEquivalentProofsFactory {
func NewInterceptedEquivalentProofsFactory(args ArgInterceptedDataFactory, proofsPool dataRetriever.ProofsPool) *interceptedEquivalentProofsFactory {
return &interceptedEquivalentProofsFactory{
marshaller: args.CoreComponents.InternalMarshalizer(),
shardCoordinator: args.ShardCoordinator,
headerSigVerifier: args.HeaderSigVerifier,
proofsPool: proofsPool,
}
}

Expand All @@ -30,6 +33,7 @@ func (factory *interceptedEquivalentProofsFactory) Create(buff []byte) (process.
Marshaller: factory.marshaller,
ShardCoordinator: factory.shardCoordinator,
HeaderSigVerifier: factory.headerSigVerifier,
Proofs: factory.proofsPool,
}
return interceptedBlocks.NewInterceptedEquivalentProof(args)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/multiversx/mx-chain-go/consensus/mock"
processMock "github.com/multiversx/mx-chain-go/process/mock"
"github.com/multiversx/mx-chain-go/testscommon/consensus"
"github.com/multiversx/mx-chain-go/testscommon/dataRetriever"
"github.com/stretchr/testify/require"
)

Expand All @@ -27,22 +28,22 @@ func TestInterceptedEquivalentProofsFactory_IsInterfaceNil(t *testing.T) {
var factory *interceptedEquivalentProofsFactory
require.True(t, factory.IsInterfaceNil())

factory = NewInterceptedEquivalentProofsFactory(createMockArgInterceptedDataFactory())
factory = NewInterceptedEquivalentProofsFactory(createMockArgInterceptedDataFactory(), &dataRetriever.ProofsPoolMock{})
require.False(t, factory.IsInterfaceNil())
}

func TestNewInterceptedEquivalentProofsFactory(t *testing.T) {
t.Parallel()

factory := NewInterceptedEquivalentProofsFactory(createMockArgInterceptedDataFactory())
factory := NewInterceptedEquivalentProofsFactory(createMockArgInterceptedDataFactory(), &dataRetriever.ProofsPoolMock{})
require.NotNil(t, factory)
}

func TestInterceptedEquivalentProofsFactory_Create(t *testing.T) {
t.Parallel()

args := createMockArgInterceptedDataFactory()
factory := NewInterceptedEquivalentProofsFactory(args)
factory := NewInterceptedEquivalentProofsFactory(args, &dataRetriever.ProofsPoolMock{})
require.NotNil(t, factory)

providedProof := &block.HeaderProof{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func NewEquivalentProofsInterceptorProcessor(args ArgEquivalentProofsInterceptor

func checkArgsEquivalentProofs(args ArgEquivalentProofsInterceptorProcessor) error {
if check.IfNil(args.EquivalentProofsPool) {
return process.ErrNilEquivalentProofsPool
return process.ErrNilProofsPool
}
if check.IfNil(args.Marshaller) {
return process.ErrNilMarshalizer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestNewEquivalentProofsInterceptorProcessor(t *testing.T) {
args.EquivalentProofsPool = nil

epip, err := NewEquivalentProofsInterceptorProcessor(args)
require.Equal(t, process.ErrNilEquivalentProofsPool, err)
require.Equal(t, process.ErrNilProofsPool, err)
require.Nil(t, epip)
})
t.Run("nil Marshaller should error", func(t *testing.T) {
Expand Down Expand Up @@ -104,6 +104,7 @@ func TestEquivalentProofsInterceptorProcessor_Save(t *testing.T) {
Marshaller: args.Marshaller,
ShardCoordinator: &mock.ShardCoordinatorMock{},
HeaderSigVerifier: &consensus.HeaderSigVerifierMock{},
Proofs: &dataRetriever.ProofsPoolMock{},
}
argInterceptedEquivalentProof.DataBuff, _ = argInterceptedEquivalentProof.Marshaller.Marshal(&block.HeaderProof{
PubKeysBitmap: []byte("bitmap"),
Expand Down
2 changes: 1 addition & 1 deletion process/interceptors/processor/hdrInterceptorProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func NewHdrInterceptorProcessor(argument *ArgHdrInterceptorProcessor) (*HdrInter
return nil, process.ErrNilCacher
}
if check.IfNil(argument.Proofs) {
return nil, process.ErrNilEquivalentProofsPool
return nil, process.ErrNilProofsPool
}
if check.IfNil(argument.BlockBlackList) {
return nil, process.ErrNilBlackListCacher
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestNewHdrInterceptorProcessor_NilProofsPoolShouldErr(t *testing.T) {
hip, err := processor.NewHdrInterceptorProcessor(arg)

assert.Nil(t, hip)
assert.Equal(t, process.ErrNilEquivalentProofsPool, err)
assert.Equal(t, process.ErrNilProofsPool, err)
}

func TestNewHdrInterceptorProcessor_NilEnableEpochsHandlerShouldErr(t *testing.T) {
Expand Down

0 comments on commit 39af8f1

Please sign in to comment.