Skip to content

Commit

Permalink
Merge pull request #315 from multiversx/MX-16238-tx-hash-extractor
Browse files Browse the repository at this point in the history
tx hashes extractor implementation
  • Loading branch information
axenteoctavian authored Dec 11, 2024
2 parents 417430d + a6f176c commit 4c6d164
Show file tree
Hide file tree
Showing 24 changed files with 286 additions and 50 deletions.
5 changes: 5 additions & 0 deletions factory/runType/interface.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package runType

import (
"github.com/multiversx/mx-chain-es-indexer-go/process/elasticproc/transactions"
)

// RunTypeComponentsCreator is the interface for creating run type components
type RunTypeComponentsCreator interface {
Create() *runTypeComponents
Expand All @@ -22,6 +26,7 @@ type RunTypeComponentsHandler interface {

// RunTypeComponentsHolder holds the run type components
type RunTypeComponentsHolder interface {
TxHashExtractorCreator() transactions.TxHashExtractor
Create() error
Close() error
CheckSubcomponents() error
Expand Down
8 changes: 7 additions & 1 deletion factory/runType/runTypeComponents.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package runType

type runTypeComponents struct{}
import (
"github.com/multiversx/mx-chain-es-indexer-go/process/elasticproc/transactions"
)

type runTypeComponents struct {
txHashExtractor transactions.TxHashExtractor
}

// Close does nothing
func (rtc *runTypeComponents) Close() error {
Expand Down
8 changes: 7 additions & 1 deletion factory/runType/runTypeComponentsFactory.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package runType

import (
"github.com/multiversx/mx-chain-es-indexer-go/process/elasticproc/transactions"
)

type runTypeComponentsFactory struct{}

// NewRunTypeComponentsFactory will return a new instance of run type components factory
Expand All @@ -9,7 +13,9 @@ func NewRunTypeComponentsFactory() *runTypeComponentsFactory {

// Create will create the run type components
func (rtcf *runTypeComponentsFactory) Create() *runTypeComponents {
return &runTypeComponents{}
return &runTypeComponents{
txHashExtractor: transactions.NewTxHashExtractor(),
}
}

// IsInterfaceNil returns true if there is no value under the interface
Expand Down
17 changes: 17 additions & 0 deletions factory/runType/runTypeComponentsHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"sync"

"github.com/multiversx/mx-chain-core-go/core/check"

"github.com/multiversx/mx-chain-es-indexer-go/process/elasticproc/transactions"
)

const runTypeComponentsName = "managedRunTypeComponents"
Expand Down Expand Up @@ -67,9 +69,24 @@ func (mrtc *managedRunTypeComponents) CheckSubcomponents() error {
if check.IfNil(mrtc.runTypeComponents) {
return errNilRunTypeComponents
}
if check.IfNil(mrtc.txHashExtractor) {
return transactions.ErrNilTxHashExtractor
}
return nil
}

// TxHashExtractorCreator returns tx hash extractor
func (mrtc *managedRunTypeComponents) TxHashExtractorCreator() transactions.TxHashExtractor {
mrtc.mutRunTypeCoreComponents.Lock()
defer mrtc.mutRunTypeCoreComponents.Unlock()

if check.IfNil(mrtc.runTypeComponents) {
return nil
}

return mrtc.runTypeComponents.txHashExtractor
}

// IsInterfaceNil returns true if the interface is nil
func (mrtc *managedRunTypeComponents) IsInterfaceNil() bool {
return mrtc == nil
Expand Down
4 changes: 4 additions & 0 deletions factory/runType/runTypeComponentsHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ func TestManagedRunTypeComponents_Create(t *testing.T) {
managedRunTypeComponents, err := createComponents()
require.NoError(t, err)

require.Nil(t, managedRunTypeComponents.TxHashExtractorCreator())

err = managedRunTypeComponents.Create()
require.NoError(t, err)

require.NotNil(t, managedRunTypeComponents.TxHashExtractorCreator())

require.Equal(t, runTypeComponentsName, managedRunTypeComponents.String())
require.NoError(t, managedRunTypeComponents.Close())
})
Expand Down
8 changes: 7 additions & 1 deletion factory/runType/sovereignRunTypeComponentsFactory.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package runType

import (
"github.com/multiversx/mx-chain-es-indexer-go/process/elasticproc/transactions"
)

type sovereignRunTypeComponentsFactory struct{}

// NewSovereignRunTypeComponentsFactory will return a new instance of sovereign run type components factory
Expand All @@ -9,7 +13,9 @@ func NewSovereignRunTypeComponentsFactory() *sovereignRunTypeComponentsFactory {

// Create will create the run type components
func (srtcf *sovereignRunTypeComponentsFactory) Create() *runTypeComponents {
return &runTypeComponents{}
return &runTypeComponents{
txHashExtractor: transactions.NewSovereignTxHashExtractor(),
}
}

// IsInterfaceNil returns true if there is no value under the interface
Expand Down
7 changes: 5 additions & 2 deletions integrationtests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import (

"github.com/elastic/go-elasticsearch/v7"
"github.com/multiversx/mx-chain-core-go/core/pubkeyConverter"
logger "github.com/multiversx/mx-chain-logger-go"

"github.com/multiversx/mx-chain-es-indexer-go/client"
"github.com/multiversx/mx-chain-es-indexer-go/client/logging"
"github.com/multiversx/mx-chain-es-indexer-go/mock"
"github.com/multiversx/mx-chain-es-indexer-go/process/dataindexer"
"github.com/multiversx/mx-chain-es-indexer-go/process/elasticproc"
"github.com/multiversx/mx-chain-es-indexer-go/process/elasticproc/factory"
logger "github.com/multiversx/mx-chain-logger-go"
"github.com/multiversx/mx-chain-es-indexer-go/process/elasticproc/transactions"
)

var (
Expand Down Expand Up @@ -60,7 +62,8 @@ func CreateElasticProcessor(
EnabledIndexes: []string{dataindexer.TransactionsIndex, dataindexer.LogsIndex, dataindexer.AccountsESDTIndex, dataindexer.ScResultsIndex,
dataindexer.ReceiptsIndex, dataindexer.BlockIndex, dataindexer.AccountsIndex, dataindexer.TokensIndex, dataindexer.TagsIndex, dataindexer.EventsIndex,
dataindexer.OperationsIndex, dataindexer.DelegatorsIndex, dataindexer.ESDTsIndex, dataindexer.SCDeploysIndex, dataindexer.MiniblocksIndex, dataindexer.ValuesIndex},
Denomination: 18,
Denomination: 18,
TxHashExtractor: transactions.NewTxHashExtractor(),
}

return factory.CreateElasticProcessor(args)
Expand Down
24 changes: 24 additions & 0 deletions mock/txHashExtractorMock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package mock

import (
coreData "github.com/multiversx/mx-chain-core-go/data"
)

// TxHashExtractorMock -
type TxHashExtractorMock struct {
ExtractExecutedTxHashesCalled func(mbIndex int, mbTxHashes [][]byte, header coreData.HeaderHandler) [][]byte
}

// ExtractExecutedTxHashes -
func (the *TxHashExtractorMock) ExtractExecutedTxHashes(mbIndex int, mbTxHashes [][]byte, header coreData.HeaderHandler) [][]byte {
if the.ExtractExecutedTxHashesCalled != nil {
return the.ExtractExecutedTxHashesCalled(mbIndex, mbTxHashes, header)
}

return make([][]byte, 0)
}

// IsInterfaceNil returns true if there is no value under the interface
func (the *TxHashExtractorMock) IsInterfaceNil() bool {
return the == nil
}
4 changes: 3 additions & 1 deletion process/elasticproc/elasticProcessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
dataBlock "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/transaction"
"github.com/stretchr/testify/require"

"github.com/multiversx/mx-chain-es-indexer-go/data"
"github.com/multiversx/mx-chain-es-indexer-go/mock"
"github.com/multiversx/mx-chain-es-indexer-go/process/dataindexer"
Expand All @@ -25,7 +27,6 @@ import (
"github.com/multiversx/mx-chain-es-indexer-go/process/elasticproc/tags"
"github.com/multiversx/mx-chain-es-indexer-go/process/elasticproc/transactions"
"github.com/multiversx/mx-chain-es-indexer-go/process/elasticproc/validators"
"github.com/stretchr/testify/require"
)

func newElasticsearchProcessor(elasticsearchWriter DatabaseClientHandler, arguments *ArgElasticProcessor) *elasticProcessor {
Expand Down Expand Up @@ -354,6 +355,7 @@ func TestElasticseachSaveTransactions(t *testing.T) {
Hasher: &mock.HasherMock{},
Marshalizer: &mock.MarshalizerMock{},
BalanceConverter: bc,
TxHashExtractor: transactions.NewTxHashExtractor(),
}
txDbProc, _ := transactions.NewTransactionsProcessor(args)
arguments.TransactionsProc = txDbProc
Expand Down
3 changes: 3 additions & 0 deletions process/elasticproc/factory/elasticProcessorFactory.go
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/hashing"
"github.com/multiversx/mx-chain-core-go/marshal"

"github.com/multiversx/mx-chain-es-indexer-go/process/dataindexer"
"github.com/multiversx/mx-chain-es-indexer-go/process/elasticproc"
"github.com/multiversx/mx-chain-es-indexer-go/process/elasticproc/accounts"
Expand Down Expand Up @@ -31,6 +32,7 @@ type ArgElasticProcessorFactory struct {
BulkRequestMaxSize int
UseKibana bool
ImportDB bool
TxHashExtractor transactions.TxHashExtractor
}

// CreateElasticProcessor will create a new instance of ElasticProcessor
Expand Down Expand Up @@ -87,6 +89,7 @@ func CreateElasticProcessor(arguments ArgElasticProcessorFactory) (dataindexer.E
Hasher: arguments.Hasher,
Marshalizer: arguments.Marshalizer,
BalanceConverter: balanceConverter,
TxHashExtractor: arguments.TxHashExtractor,
}
txsProc, err := transactions.NewTransactionsProcessor(argsTxsProc)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion process/elasticproc/factory/elasticProcessorFactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package factory
import (
"testing"

"github.com/multiversx/mx-chain-es-indexer-go/mock"
"github.com/stretchr/testify/require"

"github.com/multiversx/mx-chain-es-indexer-go/mock"
)

func TestCreateElasticProcessor(t *testing.T) {
Expand All @@ -18,6 +19,7 @@ func TestCreateElasticProcessor(t *testing.T) {
EnabledIndexes: []string{"blocks"},
Denomination: 1,
UseKibana: false,
TxHashExtractor: &mock.TxHashExtractorMock{},
}

ep, err := CreateElasticProcessor(args)
Expand Down
6 changes: 5 additions & 1 deletion process/elasticproc/transactions/checkers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (
"github.com/multiversx/mx-chain-core-go/core/check"
coreData "github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-core-go/data/outport"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"

"github.com/multiversx/mx-chain-es-indexer-go/data"
elasticIndexer "github.com/multiversx/mx-chain-es-indexer-go/process/dataindexer"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
)

const (
Expand All @@ -35,6 +36,9 @@ func checkTxsProcessorArg(args *ArgsTransactionProcessor) error {
if check.IfNil(args.BalanceConverter) {
return elasticIndexer.ErrNilBalanceConverter
}
if check.IfNil(args.TxHashExtractor) {
return ErrNilTxHashExtractor
}

return nil
}
Expand Down
27 changes: 25 additions & 2 deletions process/elasticproc/transactions/checkers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@ import (
coreData "github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-core-go/data/block"
"github.com/multiversx/mx-chain-core-go/data/outport"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
"github.com/stretchr/testify/require"

"github.com/multiversx/mx-chain-es-indexer-go/data"
"github.com/multiversx/mx-chain-es-indexer-go/mock"
elasticIndexer "github.com/multiversx/mx-chain-es-indexer-go/process/dataindexer"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
"github.com/stretchr/testify/require"
"github.com/multiversx/mx-chain-es-indexer-go/process/elasticproc/converters"
)

func createMockArgs() *ArgsTransactionProcessor {
bc, _ := converters.NewBalanceConverter(18)
return &ArgsTransactionProcessor{
AddressPubkeyConverter: &mock.PubkeyConverterMock{},
Hasher: &mock.HasherMock{},
Marshalizer: &mock.MarshalizerMock{},
BalanceConverter: bc,
TxHashExtractor: &mock.TxHashExtractorMock{},
}
}

Expand Down Expand Up @@ -59,6 +64,24 @@ func TestNewTransactionsProcessor(t *testing.T) {
},
exErr: elasticIndexer.ErrNilHasher,
},
{
name: "NilBalanceConverter",
args: func() *ArgsTransactionProcessor {
args := createMockArgs()
args.BalanceConverter = nil
return args
},
exErr: elasticIndexer.ErrNilBalanceConverter,
},
{
name: "NilTxHashExtractor",
args: func() *ArgsTransactionProcessor {
args := createMockArgs()
args.TxHashExtractor = nil
return args
},
exErr: ErrNilTxHashExtractor,
},
}

for _, tt := range tests {
Expand Down
8 changes: 8 additions & 0 deletions process/elasticproc/transactions/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package transactions

import (
"errors"
)

// ErrNilTxHashExtractor signals that a nil tx hash extractor has been provided
var ErrNilTxHashExtractor = errors.New("nil tx hash extractor")
7 changes: 7 additions & 0 deletions process/elasticproc/transactions/interface.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package transactions

import (
coreData "github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-core-go/data/outport"
datafield "github.com/multiversx/mx-chain-vm-common-go/parsers/dataField"
)
Expand All @@ -13,3 +14,9 @@ type DataFieldParser interface {
type feeInfoHandler interface {
GetFeeInfo() *outport.FeeInfo
}

// TxHashExtractor defines what tx hash extractor should be able to do
type TxHashExtractor interface {
ExtractExecutedTxHashes(mbIndex int, mbTxHashes [][]byte, header coreData.HeaderHandler) [][]byte
IsInterfaceNil() bool
}
22 changes: 22 additions & 0 deletions process/elasticproc/transactions/sovereignTxHashExtractor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package transactions

import (
coreData "github.com/multiversx/mx-chain-core-go/data"
)

type sovereignTxHashExtractor struct{}

// NewSovereignTxHashExtractor creates a new sovereign tx hash extractor
func NewSovereignTxHashExtractor() *sovereignTxHashExtractor {
return &sovereignTxHashExtractor{}
}

// ExtractExecutedTxHashes returns directly the provided mini block tx hashes
func (the *sovereignTxHashExtractor) ExtractExecutedTxHashes(_ int, mbTxHashes [][]byte, _ coreData.HeaderHandler) [][]byte {
return mbTxHashes
}

// IsInterfaceNil returns true if there is no value under the interface
func (the *sovereignTxHashExtractor) IsInterfaceNil() bool {
return the == nil
}
23 changes: 23 additions & 0 deletions process/elasticproc/transactions/sovereignTxHashExtractor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package transactions

import (
"testing"

"github.com/stretchr/testify/require"
)

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

sthe := NewSovereignTxHashExtractor()
require.False(t, sthe.IsInterfaceNil())
}

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

sthe := NewSovereignTxHashExtractor()
mbTxHashes := [][]byte{[]byte("hash1"), []byte("hash2")}
txHashes := sthe.ExtractExecutedTxHashes(0, mbTxHashes, nil)
require.Equal(t, mbTxHashes, txHashes)
}
Loading

0 comments on commit 4c6d164

Please sign in to comment.