From 327bd04507c51115fef1778612eea9ff771cf183 Mon Sep 17 00:00:00 2001 From: miiu Date: Fri, 11 Oct 2024 18:42:50 +0300 Subject: [PATCH] id inner txs --- data/transaction.go | 1 + integrationtests/relayedTxV3_test.go | 2 +- .../testdata/relayedTxV3/innerTx1.json | 1 + .../testdata/relayedTxV3/innerTx2.json | 1 + process/elasticproc/elasticProcessor_test.go | 2 +- .../factory/elasticProcessorFactory.go | 7 +++++- .../elasticproc/innerTxs/innerTxsProcessor.go | 22 ++++++++++++++++--- .../innerTxs/innerTxsProcessor_test.go | 7 ++++-- process/elasticproc/innerTxs/serialize.go | 5 +---- .../elasticproc/innerTxs/serialize_test.go | 13 ++++++----- 10 files changed, 44 insertions(+), 17 deletions(-) diff --git a/data/transaction.go b/data/transaction.go index 726c7be7..64f6347a 100644 --- a/data/transaction.go +++ b/data/transaction.go @@ -97,6 +97,7 @@ type FeeData struct { // InnerTransaction is the structure that contains data about an inner transaction type InnerTransaction struct { + ID string `json:"-"` Hash string `json:"hash,omitempty"` Type string `json:"type,omitempty"` Nonce uint64 `json:"nonce"` diff --git a/integrationtests/relayedTxV3_test.go b/integrationtests/relayedTxV3_test.go index 6cf95a60..1828a408 100644 --- a/integrationtests/relayedTxV3_test.go +++ b/integrationtests/relayedTxV3_test.go @@ -100,7 +100,7 @@ func TestRelayedTxV3(t *testing.T) { string(genericResponse.Docs[0].Source), ) - ids = []string{"13b41efddcb2c01dbaba26ebc387a7f58a4ea0757a73420267818224f939e77a", "f8b8b93e42afb737a59dd622af054e34d970663b09b90138e7dc2712565ca8db"} + ids = []string{"59d001d2804b6b4f5d56223577b4469852743622521cd5081b9f49e5c791af44", "a37e88461df25c1989b912b6a451c4e7c703d3d9b02eb1b4fd964c6362e7cbd4"} err = esClient.DoMultiGet(context.Background(), ids, indexerdata.OperationsIndex, true, genericResponse) require.Nil(t, err) diff --git a/integrationtests/testdata/relayedTxV3/innerTx1.json b/integrationtests/testdata/relayedTxV3/innerTx1.json index 8a6a88ae..7ef8a5c9 100644 --- a/integrationtests/testdata/relayedTxV3/innerTx1.json +++ b/integrationtests/testdata/relayedTxV3/innerTx1.json @@ -1,4 +1,5 @@ { + "hash":"13b41efddcb2c01dbaba26ebc387a7f58a4ea0757a73420267818224f939e77a", "type": "innerTx", "nonce": 20, "value": "1000", diff --git a/integrationtests/testdata/relayedTxV3/innerTx2.json b/integrationtests/testdata/relayedTxV3/innerTx2.json index 93a6539a..bd446486 100644 --- a/integrationtests/testdata/relayedTxV3/innerTx2.json +++ b/integrationtests/testdata/relayedTxV3/innerTx2.json @@ -1,4 +1,5 @@ { + "hash":"f8b8b93e42afb737a59dd622af054e34d970663b09b90138e7dc2712565ca8db", "type": "innerTx", "nonce": 10, "value": "0", diff --git a/process/elasticproc/elasticProcessor_test.go b/process/elasticproc/elasticProcessor_test.go index 3bd41377..9ebe0d1c 100644 --- a/process/elasticproc/elasticProcessor_test.go +++ b/process/elasticproc/elasticProcessor_test.go @@ -74,7 +74,7 @@ func createMockElasticProcessorArgs() *ArgElasticProcessor { } lp, _ := logsevents.NewLogsAndEventsProcessor(args) op, _ := operations.NewOperationsProcessor() - ip := innerTxs.NewInnerTxsProcessor() + ip, _ := innerTxs.NewInnerTxsProcessor(args.Hasher) return &ArgElasticProcessor{ DBClient: &mock.DatabaseWriterStub{}, diff --git a/process/elasticproc/factory/elasticProcessorFactory.go b/process/elasticproc/factory/elasticProcessorFactory.go index c8ce0eae..d07ca16b 100644 --- a/process/elasticproc/factory/elasticProcessorFactory.go +++ b/process/elasticproc/factory/elasticProcessorFactory.go @@ -110,6 +110,11 @@ func CreateElasticProcessor(arguments ArgElasticProcessorFactory) (dataindexer.E return nil, err } + innerTxsProc, err := innerTxs.NewInnerTxsProcessor(arguments.Hasher) + if err != nil { + return nil, err + } + args := &elasticproc.ArgElasticProcessor{ BulkRequestMaxSize: arguments.BulkRequestMaxSize, TransactionsProc: txsProc, @@ -128,7 +133,7 @@ func CreateElasticProcessor(arguments ArgElasticProcessorFactory) (dataindexer.E OperationsProc: operationsProc, ImportDB: arguments.ImportDB, Version: arguments.Version, - InnerTxsHandler: innerTxs.NewInnerTxsProcessor(), + InnerTxsHandler: innerTxsProc, } return elasticproc.NewElasticProcessor(args) diff --git a/process/elasticproc/innerTxs/innerTxsProcessor.go b/process/elasticproc/innerTxs/innerTxsProcessor.go index 581c748d..5df631f9 100644 --- a/process/elasticproc/innerTxs/innerTxsProcessor.go +++ b/process/elasticproc/innerTxs/innerTxsProcessor.go @@ -1,16 +1,30 @@ package innerTxs -import "github.com/multiversx/mx-chain-es-indexer-go/data" +import ( + "encoding/hex" + + "github.com/multiversx/mx-chain-core-go/core" + "github.com/multiversx/mx-chain-core-go/core/check" + "github.com/multiversx/mx-chain-core-go/hashing" + "github.com/multiversx/mx-chain-es-indexer-go/data" +) // InnerTxType is the type for an inner transaction const InnerTxType = "innerTx" type innerTxsProcessor struct { + hasher hashing.Hasher } // NewInnerTxsProcessor will create a new instance of inner transactions processor -func NewInnerTxsProcessor() *innerTxsProcessor { - return &innerTxsProcessor{} +func NewInnerTxsProcessor(hasher hashing.Hasher) (*innerTxsProcessor, error) { + if check.IfNil(hasher) { + return nil, core.ErrNilHasher + } + + return &innerTxsProcessor{ + hasher: hasher, + }, nil } // ExtractInnerTxs will extract the inner transactions from the transaction array @@ -22,6 +36,8 @@ func (ip *innerTxsProcessor) ExtractInnerTxs( for _, innerTx := range tx.InnerTransactions { innerTxCopy := *innerTx + id := ip.hasher.Compute(innerTxCopy.Hash + innerTxCopy.RelayedTxHash) + innerTxCopy.ID = hex.EncodeToString(id) innerTxCopy.Type = InnerTxType innerTxCopy.RelayedTxHash = tx.Hash innerTxs = append(innerTxs, &innerTxCopy) diff --git a/process/elasticproc/innerTxs/innerTxsProcessor_test.go b/process/elasticproc/innerTxs/innerTxsProcessor_test.go index 3931e766..22d1db63 100644 --- a/process/elasticproc/innerTxs/innerTxsProcessor_test.go +++ b/process/elasticproc/innerTxs/innerTxsProcessor_test.go @@ -4,13 +4,14 @@ import ( "testing" "github.com/multiversx/mx-chain-es-indexer-go/data" + "github.com/multiversx/mx-chain-es-indexer-go/mock" "github.com/stretchr/testify/require" ) func TestInnerTxsProcessor_ExtractInnerTxsNoTransactions(t *testing.T) { t.Parallel() - innerTxsProc := NewInnerTxsProcessor() + innerTxsProc, _ := NewInnerTxsProcessor(&mock.HasherMock{}) res := innerTxsProc.ExtractInnerTxs(nil) require.Equal(t, 0, len(res)) @@ -22,7 +23,7 @@ func TestInnerTxsProcessor_ExtractInnerTxsNoTransactions(t *testing.T) { func TestInnerTxsProcessor_ExtractInnerTxs(t *testing.T) { t.Parallel() - innerTxsProc := NewInnerTxsProcessor() + innerTxsProc, _ := NewInnerTxsProcessor(&mock.HasherMock{}) res := innerTxsProc.ExtractInnerTxs([]*data.Transaction{{ Hash: "txHash", @@ -39,11 +40,13 @@ func TestInnerTxsProcessor_ExtractInnerTxs(t *testing.T) { require.Equal(t, 2, len(res)) require.Equal(t, []*data.InnerTransaction{ { + ID: "054efde0c8bb1ee713c9fe5981340d7efbf23b7aa72abeab9a63b64c21000188", Type: InnerTxType, Hash: "inner1", RelayedTxHash: "txHash", }, { + ID: "e50a3fd30c5c7bba673dd18a0b329760f1bff34342978821c5d341067da70fa1", Type: InnerTxType, Hash: "inner2", RelayedTxHash: "txHash", diff --git a/process/elasticproc/innerTxs/serialize.go b/process/elasticproc/innerTxs/serialize.go index 7a7e1f86..3149b4c8 100644 --- a/process/elasticproc/innerTxs/serialize.go +++ b/process/elasticproc/innerTxs/serialize.go @@ -29,15 +29,12 @@ func prepareSerializedDataForAInnerTx( innerTx *data.InnerTransaction, index string, ) ([]byte, []byte, error) { - innerTxHash := innerTx.Hash - innerTx.Hash = "" - marshaledSCR, err := json.Marshal(innerTx) if err != nil { return nil, nil, err } - meta := []byte(fmt.Sprintf(`{ "index" : { "_index":"%s","_id" : "%s" } }%s`, index, converters.JsonEscape(innerTxHash), "\n")) + meta := []byte(fmt.Sprintf(`{ "index" : { "_index":"%s","_id" : "%s" } }%s`, index, converters.JsonEscape(innerTx.ID), "\n")) return meta, marshaledSCR, nil } diff --git a/process/elasticproc/innerTxs/serialize_test.go b/process/elasticproc/innerTxs/serialize_test.go index 0d9aa77d..f49cc3c4 100644 --- a/process/elasticproc/innerTxs/serialize_test.go +++ b/process/elasticproc/innerTxs/serialize_test.go @@ -4,6 +4,7 @@ import ( "testing" "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" "github.com/stretchr/testify/require" ) @@ -13,22 +14,24 @@ func TestInnerTxsProcessor_SerializeInnerTxs(t *testing.T) { innerTxs := []*data.InnerTransaction{ { + ID: "id1", Hash: "h1", Type: InnerTxType, }, { + ID: "id2", Hash: "h2", Type: InnerTxType, }, } - innerTxsProc := NewInnerTxsProcessor() + innerTxsProc, _ := NewInnerTxsProcessor(&mock.HasherMock{}) buffSlice := data.NewBufferSlice(0) err := innerTxsProc.SerializeInnerTxs(innerTxs, buffSlice, dataindexer.OperationsIndex) require.Nil(t, err) - require.Equal(t, `{ "index" : { "_index":"operations","_id" : "h1" } } -{"type":"innerTx","nonce":0,"value":"","receiver":"","sender":"","gasPrice":0,"gasLimit":0,"chainID":"","version":0} -{ "index" : { "_index":"operations","_id" : "h2" } } -{"type":"innerTx","nonce":0,"value":"","receiver":"","sender":"","gasPrice":0,"gasLimit":0,"chainID":"","version":0} + require.Equal(t, `{ "index" : { "_index":"operations","_id" : "id1" } } +{"hash":"h1","type":"innerTx","nonce":0,"value":"","receiver":"","sender":"","gasPrice":0,"gasLimit":0,"chainID":"","version":0} +{ "index" : { "_index":"operations","_id" : "id2" } } +{"hash":"h2","type":"innerTx","nonce":0,"value":"","receiver":"","sender":"","gasPrice":0,"gasLimit":0,"chainID":"","version":0} `, buffSlice.Buffers()[0].String()) }