Skip to content

Commit

Permalink
id inner txs
Browse files Browse the repository at this point in the history
  • Loading branch information
miiu96 committed Oct 11, 2024
1 parent 9e25a5f commit 327bd04
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 17 deletions.
1 change: 1 addition & 0 deletions data/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
2 changes: 1 addition & 1 deletion integrationtests/relayedTxV3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions integrationtests/testdata/relayedTxV3/innerTx1.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"hash":"13b41efddcb2c01dbaba26ebc387a7f58a4ea0757a73420267818224f939e77a",
"type": "innerTx",
"nonce": 20,
"value": "1000",
Expand Down
1 change: 1 addition & 0 deletions integrationtests/testdata/relayedTxV3/innerTx2.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"hash":"f8b8b93e42afb737a59dd622af054e34d970663b09b90138e7dc2712565ca8db",
"type": "innerTx",
"nonce": 10,
"value": "0",
Expand Down
2 changes: 1 addition & 1 deletion process/elasticproc/elasticProcessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down
7 changes: 6 additions & 1 deletion process/elasticproc/factory/elasticProcessorFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
22 changes: 19 additions & 3 deletions process/elasticproc/innerTxs/innerTxsProcessor.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions process/elasticproc/innerTxs/innerTxsProcessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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",
Expand All @@ -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",
Expand Down
5 changes: 1 addition & 4 deletions process/elasticproc/innerTxs/serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
13 changes: 8 additions & 5 deletions process/elasticproc/innerTxs/serialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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())
}

0 comments on commit 327bd04

Please sign in to comment.