Skip to content

Commit

Permalink
added flag for proofs check in header intercetor
Browse files Browse the repository at this point in the history
  • Loading branch information
ssd04 committed Dec 17, 2024
1 parent 8a95e05 commit 89a246e
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type baseInterceptorsContainerFactory struct {
hardforkTrigger heartbeat.HardforkTrigger
nodeOperationMode common.NodeOperation
interceptedDataVerifierFactory process.InterceptedDataVerifierFactory
enableEpochsHandler common.EnableEpochsHandler
}

func checkBaseParams(
Expand Down Expand Up @@ -423,9 +424,10 @@ func (bicf *baseInterceptorsContainerFactory) generateHeaderInterceptors() error
}

argProcessor := &processor.ArgHdrInterceptorProcessor{
Headers: bicf.dataPool.Headers(),
BlockBlackList: bicf.blockBlackList,
Proofs: bicf.dataPool.Proofs(),
Headers: bicf.dataPool.Headers(),
BlockBlackList: bicf.blockBlackList,
Proofs: bicf.dataPool.Proofs(),
EnableEpochsHandler: bicf.enableEpochsHandler,
}
hdrProcessor, err := processor.NewHdrInterceptorProcessor(argProcessor)
if err != nil {
Expand Down Expand Up @@ -566,9 +568,10 @@ func (bicf *baseInterceptorsContainerFactory) generateMetachainHeaderInterceptor
}

argProcessor := &processor.ArgHdrInterceptorProcessor{
Headers: bicf.dataPool.Headers(),
BlockBlackList: bicf.blockBlackList,
Proofs: bicf.dataPool.Proofs(),
Headers: bicf.dataPool.Headers(),
BlockBlackList: bicf.blockBlackList,
Proofs: bicf.dataPool.Proofs(),
EnableEpochsHandler: bicf.enableEpochsHandler,
}
hdrProcessor, err := processor.NewHdrInterceptorProcessor(argProcessor)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func NewMetaInterceptorsContainerFactory(
hardforkTrigger: args.HardforkTrigger,
nodeOperationMode: args.NodeOperationMode,
interceptedDataVerifierFactory: args.InterceptedDataVerifierFactory,
enableEpochsHandler: args.CoreComponents.EnableEpochsHandler(),
}

icf := &metaInterceptorsContainerFactory{
Expand Down Expand Up @@ -264,9 +265,10 @@ func (micf *metaInterceptorsContainerFactory) createOneShardHeaderInterceptor(to
}

argProcessor := &processor.ArgHdrInterceptorProcessor{
Headers: micf.dataPool.Headers(),
BlockBlackList: micf.blockBlackList,
Proofs: micf.dataPool.Proofs(),
Headers: micf.dataPool.Headers(),
BlockBlackList: micf.blockBlackList,
Proofs: micf.dataPool.Proofs(),
EnableEpochsHandler: micf.enableEpochsHandler,
}
hdrProcessor, err := processor.NewHdrInterceptorProcessor(argProcessor)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func NewShardInterceptorsContainerFactory(
hardforkTrigger: args.HardforkTrigger,
nodeOperationMode: args.NodeOperationMode,
interceptedDataVerifierFactory: args.InterceptedDataVerifierFactory,
enableEpochsHandler: args.CoreComponents.EnableEpochsHandler(),
}

icf := &shardInterceptorsContainerFactory{
Expand Down
8 changes: 5 additions & 3 deletions process/interceptors/processor/argHdrInterceptorProcessor.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package processor

import (
"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/dataRetriever"
"github.com/multiversx/mx-chain-go/process"
)

// ArgHdrInterceptorProcessor is the argument for the interceptor processor used for headers (shard, meta and so on)
type ArgHdrInterceptorProcessor struct {
Headers dataRetriever.HeadersPool
Proofs dataRetriever.ProofsPool
BlockBlackList process.TimeCacher
Headers dataRetriever.HeadersPool
Proofs dataRetriever.ProofsPool
BlockBlackList process.TimeCacher
EnableEpochsHandler common.EnableEpochsHandler
}
33 changes: 20 additions & 13 deletions process/interceptors/processor/hdrInterceptorProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,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/data"
"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/dataRetriever"
"github.com/multiversx/mx-chain-go/process"
)
Expand All @@ -16,11 +17,12 @@ var _ process.InterceptorProcessor = (*HdrInterceptorProcessor)(nil)
// HdrInterceptorProcessor is the processor used when intercepting headers
// (shard headers, meta headers) structs which satisfy HeaderHandler interface.
type HdrInterceptorProcessor struct {
headers dataRetriever.HeadersPool
proofs dataRetriever.ProofsPool
blackList process.TimeCacher
registeredHandlers []func(topic string, hash []byte, data interface{})
mutHandlers sync.RWMutex
headers dataRetriever.HeadersPool
proofs dataRetriever.ProofsPool
blackList process.TimeCacher
enableEpochsHandler common.EnableEpochsHandler
registeredHandlers []func(topic string, hash []byte, data interface{})
mutHandlers sync.RWMutex
}

// NewHdrInterceptorProcessor creates a new TxInterceptorProcessor instance
Expand All @@ -37,12 +39,16 @@ func NewHdrInterceptorProcessor(argument *ArgHdrInterceptorProcessor) (*HdrInter
if check.IfNil(argument.BlockBlackList) {
return nil, process.ErrNilBlackListCacher
}
if check.IfNil(argument.EnableEpochsHandler) {
return nil, process.ErrNilEnableEpochsHandler
}

return &HdrInterceptorProcessor{
headers: argument.Headers,
proofs: argument.Proofs,
blackList: argument.BlockBlackList,
registeredHandlers: make([]func(topic string, hash []byte, data interface{}), 0),
headers: argument.Headers,
proofs: argument.Proofs,
blackList: argument.BlockBlackList,
enableEpochsHandler: argument.EnableEpochsHandler,
registeredHandlers: make([]func(topic string, hash []byte, data interface{}), 0),
}, nil
}

Expand Down Expand Up @@ -74,10 +80,11 @@ func (hip *HdrInterceptorProcessor) Save(data process.InterceptedData, _ core.Pe

hip.headers.AddHeader(interceptedHdr.Hash(), interceptedHdr.HeaderHandler())

// TODO: check for equivalent flag
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()))
if hip.enableEpochsHandler.IsFlagEnabledInEpoch(common.EquivalentMessagesFlag, interceptedHdr.HeaderHandler().GetEpoch()) {
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()))
}
}

return nil
Expand Down
26 changes: 23 additions & 3 deletions process/interceptors/processor/hdrInterceptorProcessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@ import (
"testing"
"time"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/process/interceptors/processor"
"github.com/multiversx/mx-chain-go/process/mock"
"github.com/multiversx/mx-chain-go/testscommon"
"github.com/multiversx/mx-chain-go/testscommon/dataRetriever"
"github.com/multiversx/mx-chain-go/testscommon/enableEpochsHandlerMock"
"github.com/stretchr/testify/assert"
)

func createMockHdrArgument() *processor.ArgHdrInterceptorProcessor {
arg := &processor.ArgHdrInterceptorProcessor{
Headers: &mock.HeadersCacherStub{},
Proofs: &dataRetriever.ProofsPoolMock{},
BlockBlackList: &testscommon.TimeCacheStub{},
Headers: &mock.HeadersCacherStub{},
Proofs: &dataRetriever.ProofsPoolMock{},
BlockBlackList: &testscommon.TimeCacheStub{},
EnableEpochsHandler: &enableEpochsHandlerMock.EnableEpochsHandlerStub{},
}

return arg
Expand Down Expand Up @@ -68,6 +72,17 @@ func TestNewHdrInterceptorProcessor_NilProofsPoolShouldErr(t *testing.T) {
assert.Equal(t, process.ErrNilEquivalentProofsPool, err)
}

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

arg := createMockHdrArgument()
arg.EnableEpochsHandler = nil
hip, err := processor.NewHdrInterceptorProcessor(arg)

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

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

Expand Down Expand Up @@ -178,6 +193,11 @@ func TestHdrInterceptorProcessor_SaveShouldWork(t *testing.T) {
wasAddedHeaders = true
},
}
arg.EnableEpochsHandler = &enableEpochsHandlerMock.EnableEpochsHandlerStub{
IsFlagEnabledInEpochCalled: func(flag core.EnableEpochFlag, epoch uint32) bool {
return flag == common.EquivalentMessagesFlag
},
}

wasAddedProofs := false
arg.Proofs = &dataRetriever.ProofsPoolMock{
Expand Down

0 comments on commit 89a246e

Please sign in to comment.