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

Mx 16271 indexing incoming tokens #325

Merged
merged 20 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions client/disabled/elasticClient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package disabled

import (
"bytes"
"context"
)

type elasticClient struct{}

// NewDisabledElasticClient -
func NewDisabledElasticClient() *elasticClient {
return &elasticClient{}
}

// DoBulkRequest -
func (ec *elasticClient) DoBulkRequest(_ context.Context, _ *bytes.Buffer, _ string) error {
return nil
}

// DoQueryRemove -
func (ec *elasticClient) DoQueryRemove(_ context.Context, _ string, _ *bytes.Buffer) error {
return nil
}

// DoMultiGet -
func (ec *elasticClient) DoMultiGet(_ context.Context, _ []string, _ string, _ bool, _ interface{}) error {
return nil
}

// DoScrollRequest -
func (ec *elasticClient) DoScrollRequest(_ context.Context, _ string, _ []byte, _ bool, _ func(responseBytes []byte) error) error {
return nil
}

// DoCountRequest -
func (ec *elasticClient) DoCountRequest(_ context.Context, _ string, _ []byte) (uint64, error) {
return 0, nil
}

// UpdateByQuery -
func (ec *elasticClient) UpdateByQuery(_ context.Context, _ string, _ *bytes.Buffer) error {
return nil
}

// PutMappings -
func (ec *elasticClient) PutMappings(_ string, _ *bytes.Buffer) error {
return nil
}

// CheckAndCreateIndex -
func (ec *elasticClient) CheckAndCreateIndex(_ string) error {
return nil
}

// CheckAndCreateAlias -
func (ec *elasticClient) CheckAndCreateAlias(_ string, _ string) error {
return nil
}

// CheckAndCreateTemplate -
func (ec *elasticClient) CheckAndCreateTemplate(_ string, _ *bytes.Buffer) error {
return nil
}

// CheckAndCreatePolicy -
func (ec *elasticClient) CheckAndCreatePolicy(_ string, _ *bytes.Buffer) error {
return nil
}

// IsInterfaceNil - returns true if there is no value under the interface
func (ec *elasticClient) IsInterfaceNil() bool {
return ec == nil
}
31 changes: 31 additions & 0 deletions client/disabled/elasticClient_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package disabled

import (
"bytes"
"context"
"testing"

"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/stretchr/testify/require"
)

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

ec := NewDisabledElasticClient()
require.False(t, check.IfNil(ec))

require.NotPanics(t, func() {
_ = ec.DoBulkRequest(context.Background(), new(bytes.Buffer), "")
_ = ec.DoQueryRemove(context.Background(), "", new(bytes.Buffer))
_ = ec.DoMultiGet(context.Background(), make([]string, 0), "", true, nil)
_ = ec.DoScrollRequest(context.Background(), "", []byte(""), true, nil)
_, _ = ec.DoCountRequest(context.Background(), "", []byte(""))
_ = ec.UpdateByQuery(context.Background(), "", new(bytes.Buffer))
_ = ec.PutMappings("", new(bytes.Buffer))
_ = ec.CheckAndCreateIndex("")
_ = ec.CheckAndCreateAlias("", "")
_ = ec.CheckAndCreateTemplate("", new(bytes.Buffer))
_ = ec.CheckAndCreatePolicy("", new(bytes.Buffer))
})
}
2 changes: 1 addition & 1 deletion data/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type SourceToken struct {
CurrentOwner string `json:"currentOwner"`
}

// ResponseTokenInfo is the structure for the token info response
// ResponseTokenInfo is the structure for the tokens info response
type ResponseTokenInfo struct {
Docs []ResponseTokenInfoDB `json:"docs"`
}
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ services:
ports:
- "9200:9200"
- "9300:9300"
elasticsearch2:
main-elasticsearch:
mariusmihaic marked this conversation as resolved.
Show resolved Hide resolved
container_name: es-container2
image: docker.elastic.co/elasticsearch/elasticsearch:7.16.1
environment:
Expand Down
2 changes: 1 addition & 1 deletion factory/runType/runTypeComponentsFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (rtcf *runTypeComponentsFactory) Create() (*runTypeComponents, error) {
return &runTypeComponents{
txHashExtractor: transactions.NewTxHashExtractor(),
rewardTxData: transactions.NewRewardTxData(),
indexTokensHandler: tokens.NewIndexTokensHandler(),
indexTokensHandler: tokens.NewDisabledIndexTokensHandler(),
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion factory/runType/runTypeComponentsHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (mrtc *managedRunTypeComponents) RewardTxDataCreator() transactions.RewardT
return mrtc.runTypeComponents.rewardTxData
}

// IndexTokensHandlerCreator return index tokens handler
// IndexTokensHandlerCreator returns the index tokens handler
func (mrtc *managedRunTypeComponents) IndexTokensHandlerCreator() elasticproc.IndexTokensHandler {
mrtc.mutRunTypeCoreComponents.Lock()
defer mrtc.mutRunTypeCoreComponents.Unlock()
Expand Down
37 changes: 36 additions & 1 deletion factory/runType/sovereignRunTypeComponentsFactory.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
package runType

import (
"math"
"net/http"
"time"

"github.com/elastic/go-elasticsearch/v7"

"github.com/multiversx/mx-chain-es-indexer-go/client"
"github.com/multiversx/mx-chain-es-indexer-go/client/disabled"
"github.com/multiversx/mx-chain-es-indexer-go/client/logging"
"github.com/multiversx/mx-chain-es-indexer-go/process/elasticproc"
"github.com/multiversx/mx-chain-es-indexer-go/process/elasticproc/factory"
"github.com/multiversx/mx-chain-es-indexer-go/process/elasticproc/tokens"
"github.com/multiversx/mx-chain-es-indexer-go/process/elasticproc/transactions"
Expand All @@ -21,7 +31,12 @@ func NewSovereignRunTypeComponentsFactory(mainChainElastic factory.ElasticConfig

// Create will create the run type components
func (srtcf *sovereignRunTypeComponentsFactory) Create() (*runTypeComponents, error) {
sovIndexTokensHandler, err := tokens.NewSovereignIndexTokensHandler(srtcf.mainChainElastic, srtcf.esdtPrefix)
mainChainElasticClient, err := createMainChainElasticClient(srtcf.mainChainElastic)
if err != nil {
return nil, err
}

sovIndexTokensHandler, err := tokens.NewSovereignIndexTokensHandler(srtcf.mainChainElastic.Enabled, mainChainElasticClient, srtcf.esdtPrefix)
if err != nil {
return nil, err
}
Expand All @@ -33,6 +48,26 @@ func (srtcf *sovereignRunTypeComponentsFactory) Create() (*runTypeComponents, er
}, nil
}

func createMainChainElasticClient(mainChainElastic factory.ElasticConfig) (elasticproc.DatabaseClientHandler, error) {
if mainChainElastic.Enabled {
argsEsClient := elasticsearch.Config{
Addresses: []string{mainChainElastic.Url},
Username: mainChainElastic.UserName,
Password: mainChainElastic.Password,
Logger: &logging.CustomLogger{},
RetryOnStatus: []int{http.StatusConflict},
RetryBackoff: retryBackOff,
}
return client.NewElasticClient(argsEsClient)
} else {
return disabled.NewDisabledElasticClient(), nil
}
}

func retryBackOff(attempt int) time.Duration {
mariusmihaic marked this conversation as resolved.
Show resolved Hide resolved
return time.Duration(math.Exp2(float64(attempt))) * time.Second
}

// IsInterfaceNil returns true if there is no value under the interface
func (srtcf *sovereignRunTypeComponentsFactory) IsInterfaceNil() bool {
return srtcf == nil
Expand Down
2 changes: 1 addition & 1 deletion integrationtests/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const (
//nolint
esURL = "http://localhost:9200"
//nolint
es2URL = "http://localhost:9201"
esMainURL = "http://localhost:9201"
mariusmihaic marked this conversation as resolved.
Show resolved Hide resolved
//nolint
addressPrefix = "erd"
)
24 changes: 12 additions & 12 deletions integrationtests/incomingSCR_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/stretchr/testify/require"

indexerData "github.com/multiversx/mx-chain-es-indexer-go/process/dataindexer"
"github.com/multiversx/mx-chain-es-indexer-go/process/elasticproc/factory"
"github.com/multiversx/mx-chain-es-indexer-go/process/elasticproc"
)

type esToken struct {
Expand Down Expand Up @@ -72,18 +72,16 @@ func createTokens() ([]esToken, []esNft) {
func TestCrossChainTokensIndexingFromMainChain(t *testing.T) {
mariusmihaic marked this conversation as resolved.
Show resolved Hide resolved
setLogLevelDebug()

mainChainEs := factory.ElasticConfig{
Enabled: true,
Url: es2URL,
}
mainEsClient, err := createESClient(esMainURL)
mariusmihaic marked this conversation as resolved.
Show resolved Hide resolved
require.Nil(t, err)

tokens, nfts := createTokens()
createTokensInSourceEs(t, mainChainEs, tokens, nfts)
createTokensInSourceEs(t, mainEsClient, tokens, nfts)

esClient, err := createESClient(esURL)
require.Nil(t, err)

esProc, err := CreateSovereignElasticProcessor(esClient, mainChainEs)
esProc, err := CreateSovereignElasticProcessor(esClient, mainEsClient)
require.Nil(t, err)

allTokens := getAllTokensIDs(tokens, nfts)
Expand Down Expand Up @@ -143,10 +141,7 @@ func TestCrossChainTokensIndexingFromMainChain(t *testing.T) {
}
}

func createTokensInSourceEs(t *testing.T, es factory.ElasticConfig, tokens []esToken, nfts []esNft) {
esClient, err := createESClient(es.Url)
require.Nil(t, err)

func createTokensInSourceEs(t *testing.T, esClient elasticproc.DatabaseClientHandler, tokens []esToken, nfts []esNft) {
esProc, err := CreateElasticProcessor(esClient)
require.Nil(t, err)

Expand Down Expand Up @@ -254,7 +249,12 @@ func getAllTokensIDs(tokens []esToken, nfts []esNft) []string {
func getAllNftIDs(nfts []esNft) []string {
allNfts := make([]string, 0)
for _, nft := range nfts {
allNfts = append(allNfts, nft.Collection+"-"+hex.EncodeToString(big.NewInt(0).SetUint64(nft.Nonce).Bytes()))
nonceBytes := big.NewInt(0).SetUint64(nft.Nonce).Bytes()
nonceHex := hex.EncodeToString(nonceBytes)
nftIdentifier := nft.Collection + "-" + nonceHex

allNfts = append(allNfts, nftIdentifier)

}
return allNfts
}
Expand Down
6 changes: 3 additions & 3 deletions integrationtests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func CreateElasticProcessor(
Denomination: 18,
TxHashExtractor: transactions.NewTxHashExtractor(),
RewardTxData: transactions.NewRewardTxData(),
IndexTokensHandler: tokens.NewIndexTokensHandler(),
IndexTokensHandler: tokens.NewDisabledIndexTokensHandler(),
}

return factory.CreateElasticProcessor(args)
Expand All @@ -76,9 +76,9 @@ func CreateElasticProcessor(
// CreateSovereignElasticProcessor -
func CreateSovereignElasticProcessor(
esClient elasticproc.DatabaseClientHandler,
mainChainEs factory.ElasticConfig,
mainEsClient elasticproc.DatabaseClientHandler,
) (dataindexer.ElasticProcessor, error) {
sovIndexTokens, _ := tokens.NewSovereignIndexTokensHandler(mainChainEs, sovEsdtPrefix)
sovIndexTokens, _ := tokens.NewSovereignIndexTokensHandler(true, mainEsClient, sovEsdtPrefix)

args := factory.ArgElasticProcessorFactory{
Marshalizer: &mock.MarshalizerMock{},
Expand Down
4 changes: 2 additions & 2 deletions process/elasticproc/tokens/indexTokensHandler.go
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe you should rename the file in disalbedIndexTokensHandler or similar

Copy link
Author

Choose a reason for hiding this comment

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

renamed

Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

type indexTokensHandler struct{}

// NewIndexTokensHandler creates a new index tokens handler
func NewIndexTokensHandler() *indexTokensHandler {
// NewDisabledIndexTokensHandler creates a new disabled index tokens handler
func NewDisabledIndexTokensHandler() *indexTokensHandler {
return &indexTokensHandler{}
}

Expand Down
4 changes: 2 additions & 2 deletions process/elasticproc/tokens/indexTokensHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
func TestNewIndexTokensHandler(t *testing.T) {
t.Parallel()

ith := NewIndexTokensHandler()
ith := NewDisabledIndexTokensHandler()
require.False(t, ith.IsInterfaceNil())
}

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

ith := NewIndexTokensHandler()
ith := NewDisabledIndexTokensHandler()
err := ith.IndexCrossChainTokens(nil, nil, nil)
require.NoError(t, err)
}
Loading