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

Pre-processing improvements #92

Merged
merged 9 commits into from
Feb 16, 2024
3 changes: 1 addition & 2 deletions api/groups/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (

// EventsFacadeHandler defines the behavior of a facade handler needed for events group
type EventsFacadeHandler interface {
HandlePushEventsV2(events data.ArgsSaveBlockData) error
HandlePushEventsV1(events data.SaveBlockData) error
HandlePushEvents(events data.ArgsSaveBlockData) error
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

HandleRevertEvents(revertBlock data.RevertBlock)
HandleFinalizedEvents(finalizedBlock data.FinalizedBlock)
GetConnectorUserAndPass() (string, string)
Expand Down
3 changes: 1 addition & 2 deletions api/shared/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ type GroupHandler interface {

// FacadeHandler defines the behavior of a notifier base facade handler
type FacadeHandler interface {
HandlePushEventsV2(events data.ArgsSaveBlockData) error
HandlePushEventsV1(events data.SaveBlockData) error
HandlePushEvents(events data.ArgsSaveBlockData) error
HandleRevertEvents(revertBlock data.RevertBlock)
HandleFinalizedEvents(finalizedBlock data.FinalizedBlock)
GetConnectorUserAndPass() (string, string)
Expand Down
3 changes: 0 additions & 3 deletions facade/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,3 @@ var ErrNilEventsHandler = errors.New("nil events handler")

// ErrNilWSHandler signals that a nil websocket handler was provided
var ErrNilWSHandler = errors.New("nil websocket handler")

// ErrNilEventsInterceptor signals that a nil events interceptor was provided
var ErrNilEventsInterceptor = errors.New("nil events interceptor")
5 changes: 1 addition & 4 deletions facade/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@ import (
// EventsHandler defines the behavior of an events handler component.
// This will handle push events from observer node.
type EventsHandler interface {
HandlePushEvents(events data.BlockEvents) error
HandleSaveBlockEvents(allEvents data.ArgsSaveBlockData) error
HandleRevertEvents(revertBlock data.RevertBlock)
HandleFinalizedEvents(finalizedBlock data.FinalizedBlock)
HandleBlockTxs(blockTxs data.BlockTxs)
HandleBlockScrs(blockScrs data.BlockScrs)
HandleBlockEventsWithOrder(blockTxs data.BlockEventsWithOrder)
IsInterfaceNil() bool
}

Expand Down
94 changes: 11 additions & 83 deletions facade/notifierFacade.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ type ArgsNotifierFacade struct {
APIConfig config.ConnectorApiConfig
EventsHandler EventsHandler
WSHandler dispatcher.WSHandler
EventsInterceptor EventsInterceptor
StatusMetricsHandler common.StatusMetricsHandler
}

type notifierFacade struct {
config config.ConnectorApiConfig
eventsHandler EventsHandler
wsHandler dispatcher.WSHandler
eventsInterceptor EventsInterceptor
statusMetrics common.StatusMetricsHandler
config config.ConnectorApiConfig
eventsHandler EventsHandler
wsHandler dispatcher.WSHandler
statusMetrics common.StatusMetricsHandler
}

// NewNotifierFacade creates a new notifier facade instance
Expand All @@ -38,11 +36,10 @@ func NewNotifierFacade(args ArgsNotifierFacade) (*notifierFacade, error) {
}

return &notifierFacade{
eventsHandler: args.EventsHandler,
config: args.APIConfig,
wsHandler: args.WSHandler,
eventsInterceptor: args.EventsInterceptor,
statusMetrics: args.StatusMetricsHandler,
eventsHandler: args.EventsHandler,
config: args.APIConfig,
wsHandler: args.WSHandler,
statusMetrics: args.StatusMetricsHandler,
}, nil
}

Expand All @@ -53,86 +50,17 @@ func checkArgs(args ArgsNotifierFacade) error {
if check.IfNil(args.WSHandler) {
return ErrNilWSHandler
}
if check.IfNil(args.EventsInterceptor) {
return ErrNilEventsInterceptor
}
if check.IfNil(args.StatusMetricsHandler) {
return common.ErrNilStatusMetricsHandler
}

return nil
}

// HandlePushEventsV2 will handle push events received from observer
// It splits block data and handles log, txs and srcs events separately
func (nf *notifierFacade) HandlePushEventsV2(allEvents data.ArgsSaveBlockData) error {
eventsData, err := nf.eventsInterceptor.ProcessBlockEvents(&allEvents)
if err != nil {
return err
}

pushEvents := data.BlockEvents{
Hash: eventsData.Hash,
ShardID: eventsData.Header.GetShardID(),
TimeStamp: eventsData.Header.GetTimeStamp(),
Events: eventsData.LogEvents,
}
err = nf.eventsHandler.HandlePushEvents(pushEvents)
if err != nil {
return err
}

txs := data.BlockTxs{
Hash: eventsData.Hash,
Txs: eventsData.Txs,
}
nf.eventsHandler.HandleBlockTxs(txs)

scrs := data.BlockScrs{
Hash: eventsData.Hash,
Scrs: eventsData.Scrs,
}
nf.eventsHandler.HandleBlockScrs(scrs)

txsWithOrder := data.BlockEventsWithOrder{
Hash: eventsData.Hash,
ShardID: eventsData.Header.GetShardID(),
TimeStamp: eventsData.Header.GetTimeStamp(),
Txs: eventsData.TxsWithOrder,
Scrs: eventsData.ScrsWithOrder,
Events: eventsData.LogEvents,
}
nf.eventsHandler.HandleBlockEventsWithOrder(txsWithOrder)

return nil
}

// HandlePushEventsV1 will handle push events received from observer
// HandlePushEvents will handle push events received from observer
// It splits block data and handles log, txs and srcs events separately
// TODO: remove this implementation
func (nf *notifierFacade) HandlePushEventsV1(eventsData data.SaveBlockData) error {
pushEvents := data.BlockEvents{
Hash: eventsData.Hash,
Events: eventsData.LogEvents,
}
err := nf.eventsHandler.HandlePushEvents(pushEvents)
if err != nil {
return err
}

txs := data.BlockTxs{
Hash: eventsData.Hash,
Txs: eventsData.Txs,
}
nf.eventsHandler.HandleBlockTxs(txs)

scrs := data.BlockScrs{
Hash: eventsData.Hash,
Scrs: eventsData.Scrs,
}
nf.eventsHandler.HandleBlockScrs(scrs)

return nil
func (nf *notifierFacade) HandlePushEvents(allEvents data.ArgsSaveBlockData) error {
return nf.eventsHandler.HandleSaveBlockEvents(allEvents)
}

// HandleRevertEvents will handle revents events received from observer
Expand Down
160 changes: 7 additions & 153 deletions facade/notifierFacade_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package facade_test

import (
"errors"
"net/http"
"net/http/httptest"
"testing"

"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-core-go/data/block"
"github.com/multiversx/mx-chain-core-go/data/outport"
"github.com/multiversx/mx-chain-core-go/data/smartContractResult"
"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/multiversx/mx-chain-notifier-go/common"
"github.com/multiversx/mx-chain-notifier-go/config"
Expand All @@ -25,7 +23,6 @@ func createMockFacadeArgs() facade.ArgsNotifierFacade {
EventsHandler: &mocks.EventsHandlerStub{},
APIConfig: config.ConnectorApiConfig{},
WSHandler: &mocks.WSHandlerStub{},
EventsInterceptor: &mocks.EventsInterceptorStub{},
StatusMetricsHandler: &mocks.StatusMetricsStub{},
}
}
Expand Down Expand Up @@ -55,17 +52,6 @@ func TestNewNotifierFacade(t *testing.T) {
require.Equal(t, facade.ErrNilWSHandler, err)
})

t.Run("nil events interceptor", func(t *testing.T) {
t.Parallel()

args := createMockFacadeArgs()
args.EventsInterceptor = nil

f, err := facade.NewNotifierFacade(args)
require.True(t, check.IfNil(f))
require.Equal(t, facade.ErrNilEventsInterceptor, err)
})

t.Run("nil status metrics handler", func(t *testing.T) {
t.Parallel()

Expand All @@ -90,29 +76,6 @@ func TestNewNotifierFacade(t *testing.T) {
func TestHandlePushEvents(t *testing.T) {
t.Parallel()

t.Run("process block events error, should fail", func(t *testing.T) {
t.Parallel()

args := createMockFacadeArgs()

expectedErr := errors.New("expected error")
args.EventsInterceptor = &mocks.EventsInterceptorStub{
ProcessBlockEventsCalled: func(eventsData *data.ArgsSaveBlockData) (*data.InterceptorBlockData, error) {
return nil, expectedErr
},
}

facade, err := facade.NewNotifierFacade(args)
require.Nil(t, err)

blockData := data.ArgsSaveBlockData{
HeaderHash: []byte("blockHash"),
Header: &block.HeaderV2{},
}
err = facade.HandlePushEventsV2(blockData)
require.Equal(t, expectedErr, err)
})

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

Expand All @@ -127,138 +90,29 @@ func TestHandlePushEvents(t *testing.T) {
ExecutionOrder: 1,
},
}
scrs := map[string]*outport.SCRInfo{
"hash2": {
SmartContractResult: &smartContractResult.SmartContractResult{
Nonce: 2,
},
},
}
logData := []*outport.LogData{
{
Log: &transaction.Log{
Address: []byte("logaddr1"),
Events: []*transaction.Event{},
},
TxHash: "logHash1",
},
}

logEvents := []data.Event{
{
Address: "addr1",
},
}

header := &block.HeaderV2{
Header: &block.Header{
ShardID: 2,
},
}
blockData := data.ArgsSaveBlockData{
HeaderHash: []byte(blockHash),
TransactionsPool: &outport.TransactionPool{
Transactions: txs,
SmartContractResults: scrs,
Logs: logData,
Transactions: txs,
},
Header: &block.HeaderV2{},
}

expTxs := map[string]*transaction.Transaction{
"hash1": {
Nonce: 1,
},
}
expScrs := map[string]*smartContractResult.SmartContractResult{
"hash2": {
Nonce: 2,
},
}

expTxsData := data.BlockTxs{
Hash: blockHash,
Txs: expTxs,
}
expScrsData := data.BlockScrs{
Hash: blockHash,
Scrs: expScrs,
}
expLogEvents := data.BlockEvents{
Hash: blockHash,
Events: logEvents,
ShardID: 2,
}

expTxsWithOrder := map[string]*outport.TxInfo{
"hash1": {
Transaction: &transaction.Transaction{
Nonce: 1,
},
ExecutionOrder: 1,
},
}
expScrsWithOrder := map[string]*outport.SCRInfo{
"hash2": {
SmartContractResult: &smartContractResult.SmartContractResult{
Nonce: 2,
},
},
}
expTxsWithOrderData := data.BlockEventsWithOrder{
Hash: blockHash,
ShardID: 2,
Txs: expTxsWithOrder,
Scrs: expScrsWithOrder,
Events: logEvents,
}

pushWasCalled := false
txsWasCalled := false
scrsWasCalled := false
blockEventsWithOrderWasCalled := false
saveBlockWasCalled := false
args.EventsHandler = &mocks.EventsHandlerStub{
HandlePushEventsCalled: func(events data.BlockEvents) error {
pushWasCalled = true
assert.Equal(t, expLogEvents, events)
HandleSaveBlockEventsCalled: func(allEvents data.ArgsSaveBlockData) error {
saveBlockWasCalled = true
return nil
},
HandleBlockTxsCalled: func(blockTxs data.BlockTxs) {
txsWasCalled = true
assert.Equal(t, expTxsData, blockTxs)
},
HandleBlockScrsCalled: func(blockScrs data.BlockScrs) {
scrsWasCalled = true
assert.Equal(t, expScrsData, blockScrs)
},
HandleBlockEventsWithOrderCalled: func(blockTxs data.BlockEventsWithOrder) {
blockEventsWithOrderWasCalled = true
assert.Equal(t, expTxsWithOrderData, blockTxs)
},
}
args.EventsInterceptor = &mocks.EventsInterceptorStub{
ProcessBlockEventsCalled: func(eventsData *data.ArgsSaveBlockData) (*data.InterceptorBlockData, error) {
return &data.InterceptorBlockData{
Hash: blockHash,
Header: header,
Txs: expTxs,
Scrs: expScrs,
LogEvents: logEvents,
TxsWithOrder: expTxsWithOrder,
ScrsWithOrder: expScrsWithOrder,
}, nil
},
}

facade, err := facade.NewNotifierFacade(args)
require.Nil(t, err)

facade.HandlePushEventsV2(blockData)
err = facade.HandlePushEvents(blockData)
require.Nil(t, err)

assert.True(t, pushWasCalled)
assert.True(t, txsWasCalled)
assert.True(t, scrsWasCalled)
assert.True(t, blockEventsWithOrderWasCalled)
assert.True(t, saveBlockWasCalled)
})
}

Expand Down
Loading
Loading