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

Fixes outport refactor #5135

Merged
merged 4 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ require (
github.com/google/gops v0.3.18
github.com/gorilla/websocket v1.5.0
github.com/mitchellh/mapstructure v1.5.0
github.com/multiversx/mx-chain-core-go v1.2.1-0.20230322093158-35195fa155c0
github.com/multiversx/mx-chain-core-go v1.2.1-0.20230329082847-b78e96c3ad5a
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not the latest commit hash
I think this should be 932a718276f65ff8f820720adf5e98c4cfdd3074.

Check here: multiversx/mx-chain-core-go#171

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

github.com/multiversx/mx-chain-crypto-go v1.2.5
github.com/multiversx/mx-chain-es-indexer-go v1.4.1-0.20230322095713-a82089993c0d
github.com/multiversx/mx-chain-es-indexer-go v1.4.1-0.20230331083741-0fd8a2156e96
github.com/multiversx/mx-chain-logger-go v1.0.11
github.com/multiversx/mx-chain-p2p-go v1.0.13
github.com/multiversx/mx-chain-storage-go v1.0.7
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -612,12 +612,12 @@ github.com/multiversx/concurrent-map v0.1.4/go.mod h1:8cWFRJDOrWHOTNSqgYCUvwT7c7
github.com/multiversx/mx-chain-core-go v1.1.30/go.mod h1:8gGEQv6BWuuJwhd25qqhCOZbBSv9mk+hLeKvinSaSMk=
github.com/multiversx/mx-chain-core-go v1.1.31/go.mod h1:8gGEQv6BWuuJwhd25qqhCOZbBSv9mk+hLeKvinSaSMk=
github.com/multiversx/mx-chain-core-go v1.2.0/go.mod h1:8gGEQv6BWuuJwhd25qqhCOZbBSv9mk+hLeKvinSaSMk=
github.com/multiversx/mx-chain-core-go v1.2.1-0.20230322093158-35195fa155c0 h1:Hr0bAMUJigh5xGFm2qoKKglEtpIXckxCLjCDBUYv1DM=
github.com/multiversx/mx-chain-core-go v1.2.1-0.20230322093158-35195fa155c0/go.mod h1:8gGEQv6BWuuJwhd25qqhCOZbBSv9mk+hLeKvinSaSMk=
github.com/multiversx/mx-chain-core-go v1.2.1-0.20230329082847-b78e96c3ad5a h1:cjPv/NIb4c3oBuBxxc2ggyaMvpmGlo1RO8mmzrkWARM=
github.com/multiversx/mx-chain-core-go v1.2.1-0.20230329082847-b78e96c3ad5a/go.mod h1:/lovncjwo+pXQ7IAERwNzwCifeH7SAWk0DGqjorX2bc=
github.com/multiversx/mx-chain-crypto-go v1.2.5 h1:tuq3BUNMhKud5DQbZi9DiVAAHUXypizy8zPH0NpTGZk=
github.com/multiversx/mx-chain-crypto-go v1.2.5/go.mod h1:teqhNyWEqfMPgNn8sgWXlgtJ1a36jGCnhs/tRpXW6r4=
github.com/multiversx/mx-chain-es-indexer-go v1.4.1-0.20230322095713-a82089993c0d h1:PDkG7SrzIsyRo5PLI68vlBM85PiQhWHhpafW7VCzhgM=
github.com/multiversx/mx-chain-es-indexer-go v1.4.1-0.20230322095713-a82089993c0d/go.mod h1:iDLxzwUi9CGzzEbFRi4TgYvmhsp21qNj5eqMgT32d2M=
github.com/multiversx/mx-chain-es-indexer-go v1.4.1-0.20230331083741-0fd8a2156e96 h1:okIfLr+NqX04eHNp9k97KuLhpYfLJOjmGZaOia9xcGg=
github.com/multiversx/mx-chain-es-indexer-go v1.4.1-0.20230331083741-0fd8a2156e96/go.mod h1:Y6jgeoMBpDCtm7lurtChhgPyhpQ0GF5OruW/tl/++JI=
github.com/multiversx/mx-chain-logger-go v1.0.11 h1:DFsHa+sc5fKwhDR50I8uBM99RTDTEW68ESyr5ALRDwE=
github.com/multiversx/mx-chain-logger-go v1.0.11/go.mod h1:1srDkP0DQucWQ+rYfaq0BX2qLnULsUdRPADpYUTM6dA=
github.com/multiversx/mx-chain-p2p-go v1.0.13 h1:woIlYkDFCKYyJQ5urDcOzz8HUFGsSEhTfUXDDxNI2zM=
Expand Down
46 changes: 40 additions & 6 deletions outport/notifier/eventNotifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-core-go/core/unmarshal"
"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"
"github.com/multiversx/mx-chain-core-go/marshal"
logger "github.com/multiversx/mx-chain-logger-go"
Expand All @@ -29,8 +30,9 @@ type RevertBlock struct {
}

type eventNotifier struct {
httpClient httpClientHandler
marshalizer marshal.Marshalizer
httpClient httpClientHandler
marshalizer marshal.Marshalizer
blockContainer blockContainerHandler
}

// ArgsEventNotifier defines the arguments needed for event notifier creation
Expand All @@ -46,10 +48,15 @@ func NewEventNotifier(args ArgsEventNotifier) (*eventNotifier, error) {
if err != nil {
return nil, err
}
blockContainer, err := createBlockCreatorsContainer()
Copy link
Contributor

Choose a reason for hiding this comment

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

Please move it at the same level as factory: CreateEventNotifier, just like you did on indexer

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

if err != nil {
return nil, err
}

return &eventNotifier{
httpClient: args.HttpClient,
marshalizer: args.Marshaller,
httpClient: args.HttpClient,
marshalizer: args.Marshaller,
blockContainer: blockContainer,
}, nil
}

Expand Down Expand Up @@ -78,7 +85,7 @@ func (en *eventNotifier) SaveBlock(args *outport.OutportBlock) error {

// RevertIndexedBlock converts revert data in order to be pushed to subscribers
func (en *eventNotifier) RevertIndexedBlock(blockData *outport.BlockData) error {
headerHandler, err := unmarshal.GetHeaderFromBytes(en.marshalizer, core.HeaderType(blockData.HeaderType), blockData.HeaderBytes)
headerHandler, err := en.getHeaderFromBytes(core.HeaderType(blockData.HeaderType), blockData.HeaderBytes)
if err != nil {
return err
}
Expand Down Expand Up @@ -142,3 +149,30 @@ func (en *eventNotifier) IsInterfaceNil() bool {
func (en *eventNotifier) Close() error {
return nil
}

func (en *eventNotifier) getHeaderFromBytes(headerType core.HeaderType, headerBytes []byte) (header data.HeaderHandler, err error) {
creator, err := en.blockContainer.Get(headerType)
if err != nil {
return nil, err
}

return block.GetHeaderFromBytes(en.marshalizer, creator, headerBytes)
}

func createBlockCreatorsContainer() (blockContainerHandler, error) {
container := block.NewEmptyBlockCreatorsContainer()
err := container.Add(core.ShardHeaderV1, block.NewEmptyHeaderCreator())
if err != nil {
return nil, err
}
err = container.Add(core.ShardHeaderV2, block.NewEmptyHeaderV2Creator())
if err != nil {
return nil, err
}
err = container.Add(core.MetaHeader, block.NewEmptyMetaBlockCreator())
if err != nil {
return nil, err
}

return container, nil
}
10 changes: 10 additions & 0 deletions outport/notifier/interface.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
package notifier

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

type httpClientHandler interface {
Post(route string, payload interface{}) error
IsInterfaceNil() bool
}

// BlockContainerHandler defines what a block container should be able to do
type blockContainerHandler interface {
Get(headerType core.HeaderType) (block.EmptyBlockCreator, error)
}
20 changes: 12 additions & 8 deletions outport/process/executionOrder/transactionsExecutionOrder.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (s *sorter) getInvalidTxsExecutedInCurrentBlock(scheduledMbsFromPreviousBlo
for _, hash := range mb.TxHashes {
_, found := allScheduledTxs[string(hash)]
if found {
scheduledExecutedInvalidTxsHashesPrevBlock = append(scheduledExecutedInvalidTxsHashesPrevBlock, string(hash))
scheduledExecutedInvalidTxsHashesPrevBlock = append(scheduledExecutedInvalidTxsHashesPrevBlock, hex.EncodeToString(hash))
Copy link
Contributor

Choose a reason for hiding this comment

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

is this still backward compatible?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes is not a problem. because this part is used only in indexer

Copy link
Contributor

Choose a reason for hiding this comment

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

agree, did not noticed that the package was actually outport...

continue
}
invalidTxHashes = append(invalidTxHashes, hash)
Expand Down Expand Up @@ -249,9 +249,10 @@ func getRewardsTxsFromMe(pool *outport.TransactionPool, blockBody *block.Body, h
func extractTxsFromMap(txsHashes [][]byte, txs map[string]*outport.TxInfo) ([]data.TxWithExecutionOrderHandler, error) {
result := make([]data.TxWithExecutionOrderHandler, 0, len(txsHashes))
for _, txHash := range txsHashes {
tx, found := txs[string(txHash)]
txHashHex := hex.EncodeToString(txHash)
tx, found := txs[txHashHex]
if !found {
return nil, fmt.Errorf("cannot find transaction in pool, txHash: %s", hex.EncodeToString(txHash))
return nil, fmt.Errorf("cannot find transaction in pool, txHash: %s", txHashHex)
}
result = append(result, tx)
}
Expand All @@ -262,9 +263,10 @@ func extractTxsFromMap(txsHashes [][]byte, txs map[string]*outport.TxInfo) ([]da
func extractSCRsFromMap(txsHashes [][]byte, scrs map[string]*outport.SCRInfo) ([]data.TxWithExecutionOrderHandler, error) {
result := make([]data.TxWithExecutionOrderHandler, 0, len(txsHashes))
for _, txHash := range txsHashes {
scr, found := scrs[string(txHash)]
txHashHex := hex.EncodeToString(txHash)
scr, found := scrs[txHashHex]
if !found {
return nil, fmt.Errorf("cannot find scr in pool, txHash: %s", hex.EncodeToString(txHash))
return nil, fmt.Errorf("cannot find scr in pool, txHash: %s", txHashHex)
}
result = append(result, scr)
}
Expand All @@ -275,9 +277,10 @@ func extractSCRsFromMap(txsHashes [][]byte, scrs map[string]*outport.SCRInfo) ([
func extractRewardsFromMap(txsHashes [][]byte, rewards map[string]*outport.RewardInfo) ([]data.TxWithExecutionOrderHandler, error) {
result := make([]data.TxWithExecutionOrderHandler, 0, len(txsHashes))
for _, txHash := range txsHashes {
reward, found := rewards[string(txHash)]
txHashHex := hex.EncodeToString(txHash)
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we keep the hash in hex format? This degrades the performance of the node as the hex.EncodeToString is more CPU intensive than the regular cast to string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we need the transaction hash hex-encoded in order can send it over to the WS.
If I will let it non hex I will have problems with the marshaller because the hash is used as a string key in map structures.

Copy link
Contributor

Choose a reason for hiding this comment

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

agree, this does not degrade the performance of the validators as the code impacted is in outport... package

reward, found := rewards[txHashHex]
if !found {
return nil, fmt.Errorf("cannot find reward in pool, txHash: %s", hex.EncodeToString(txHash))
return nil, fmt.Errorf("cannot find reward in pool, txHash: %s", txHashHex)
}
result = append(result, reward)
}
Expand All @@ -299,7 +302,8 @@ func extractExecutedTxHashes(mbIndex int, mbTxHashes [][]byte, header data.Heade

func extractAndPutScrsToDestinationMap(scrsHashes [][]byte, scrsMap map[string]*outport.SCRInfo, destinationMap map[string]data.TxWithExecutionOrderHandler) {
for _, scrHash := range scrsHashes {
scr, found := scrsMap[string(scrHash)]
scrHashHex := hex.EncodeToString(scrHash)
scr, found := scrsMap[scrHashHex]
if !found {
continue
}
Expand Down
53 changes: 27 additions & 26 deletions outport/process/executionOrder/transactionsExecutionOrder_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package executionOrder

import (
"encoding/hex"
"testing"

"github.com/multiversx/mx-chain-core-go/core"
Expand Down Expand Up @@ -130,25 +131,25 @@ func TestAddExecutionOrderInTransactionPool(t *testing.T) {

pool := &outport.TransactionPool{
Transactions: map[string]*outport.TxInfo{
string(txHashToMe): {Transaction: &transaction.Transaction{Nonce: 1}},
string(txHashFromMe): {Transaction: &transaction.Transaction{Nonce: 2}},
hex.EncodeToString(txHashToMe): {Transaction: &transaction.Transaction{Nonce: 1}},
hex.EncodeToString(txHashFromMe): {Transaction: &transaction.Transaction{Nonce: 2}},
},
SmartContractResults: map[string]*outport.SCRInfo{
string(scrHashToMe): {SmartContractResult: &smartContractResult.SmartContractResult{Nonce: 3}},
string(scrHashFromMe): {SmartContractResult: &smartContractResult.SmartContractResult{
hex.EncodeToString(scrHashToMe): {SmartContractResult: &smartContractResult.SmartContractResult{Nonce: 3}},
hex.EncodeToString(scrHashFromMe): {SmartContractResult: &smartContractResult.SmartContractResult{
Nonce: 4,
OriginalTxHash: txHashToMe,
}},
string(scrHashIntra): {SmartContractResult: &smartContractResult.SmartContractResult{
hex.EncodeToString(scrHashIntra): {SmartContractResult: &smartContractResult.SmartContractResult{
Nonce: 0,
OriginalTxHash: txHashToMe,
}},
},
Rewards: map[string]*outport.RewardInfo{
string(rewardTxHash): {Reward: &rewardTx.RewardTx{}},
hex.EncodeToString(rewardTxHash): {Reward: &rewardTx.RewardTx{}},
},
InvalidTxs: map[string]*outport.TxInfo{
string(invalidTxHash): {Transaction: &transaction.Transaction{Nonce: 5}},
hex.EncodeToString(invalidTxHash): {Transaction: &transaction.Transaction{Nonce: 5}},
},
Receipts: map[string]*receipt.Receipt{},
Logs: nil,
Expand All @@ -159,28 +160,28 @@ func TestAddExecutionOrderInTransactionPool(t *testing.T) {

require.Equal(t, &outport.TransactionPool{
Transactions: map[string]*outport.TxInfo{
string(txHashToMe): {
hex.EncodeToString(txHashToMe): {
Transaction: &transaction.Transaction{Nonce: 1},
ExecutionOrder: 0,
},
string(txHashFromMe): {
hex.EncodeToString(txHashFromMe): {
Transaction: &transaction.Transaction{Nonce: 2},
ExecutionOrder: 3,
},
},
SmartContractResults: map[string]*outport.SCRInfo{
string(scrHashToMe): {
hex.EncodeToString(scrHashToMe): {
SmartContractResult: &smartContractResult.SmartContractResult{Nonce: 3},
ExecutionOrder: 1,
},
string(scrHashFromMe): {
hex.EncodeToString(scrHashFromMe): {
SmartContractResult: &smartContractResult.SmartContractResult{
Nonce: 4,
OriginalTxHash: txHashToMe,
},
ExecutionOrder: 0,
},
string(scrHashIntra): {
hex.EncodeToString(scrHashIntra): {
SmartContractResult: &smartContractResult.SmartContractResult{
Nonce: 0,
OriginalTxHash: txHashToMe,
Expand All @@ -189,13 +190,13 @@ func TestAddExecutionOrderInTransactionPool(t *testing.T) {
},
},
Rewards: map[string]*outport.RewardInfo{
string(rewardTxHash): {
hex.EncodeToString(rewardTxHash): {
Reward: &rewardTx.RewardTx{},
ExecutionOrder: 2,
},
},
InvalidTxs: map[string]*outport.TxInfo{
string(invalidTxHash): {
hex.EncodeToString(invalidTxHash): {
Transaction: &transaction.Transaction{Nonce: 5},
ExecutionOrder: 4,
},
Expand Down Expand Up @@ -256,8 +257,8 @@ func TestAddExecutionOrderInTransactionPoolFromMeTransactionAndScheduled(t *test

pool := &outport.TransactionPool{
Transactions: map[string]*outport.TxInfo{
string(firstTxHash): {Transaction: &transaction.Transaction{Nonce: 1}},
string(secondTxHash): {Transaction: &transaction.Transaction{Nonce: 2}},
hex.EncodeToString(firstTxHash): {Transaction: &transaction.Transaction{Nonce: 1}},
hex.EncodeToString(secondTxHash): {Transaction: &transaction.Transaction{Nonce: 2}},
},
}

Expand All @@ -266,11 +267,11 @@ func TestAddExecutionOrderInTransactionPoolFromMeTransactionAndScheduled(t *test

require.Equal(t, &outport.TransactionPool{
Transactions: map[string]*outport.TxInfo{
string(firstTxHash): {
hex.EncodeToString(firstTxHash): {
Transaction: &transaction.Transaction{Nonce: 1},
ExecutionOrder: 0,
},
string(secondTxHash): {
hex.EncodeToString(secondTxHash): {
Transaction: &transaction.Transaction{Nonce: 2},
ExecutionOrder: 1,
},
Expand Down Expand Up @@ -369,13 +370,13 @@ func TestAddExecutionOrderInTransactionPoolFromMeTransactionAndScheduledInvalid(

pool := &outport.TransactionPool{
Transactions: map[string]*outport.TxInfo{
string(secondTxHash): {Transaction: &transaction.Transaction{Nonce: 2}},
hex.EncodeToString(secondTxHash): {Transaction: &transaction.Transaction{Nonce: 2}},
},
InvalidTxs: map[string]*outport.TxInfo{
string(firstTxHash): {Transaction: &transaction.Transaction{Nonce: 1}},
hex.EncodeToString(firstTxHash): {Transaction: &transaction.Transaction{Nonce: 1}},
},
SmartContractResults: map[string]*outport.SCRInfo{
string(scrHash): {SmartContractResult: &smartContractResult.SmartContractResult{
hex.EncodeToString(scrHash): {SmartContractResult: &smartContractResult.SmartContractResult{
Nonce: 3,
OriginalTxHash: scheduledTx,
}},
Expand All @@ -386,19 +387,19 @@ func TestAddExecutionOrderInTransactionPoolFromMeTransactionAndScheduledInvalid(
require.Nil(t, err)
require.Equal(t, &outport.TransactionPool{
Transactions: map[string]*outport.TxInfo{
string(secondTxHash): {
hex.EncodeToString(secondTxHash): {
Transaction: &transaction.Transaction{Nonce: 2},
ExecutionOrder: 1,
},
},
InvalidTxs: map[string]*outport.TxInfo{
string(firstTxHash): {
hex.EncodeToString(firstTxHash): {
Transaction: &transaction.Transaction{Nonce: 1},
ExecutionOrder: 0,
},
},
SmartContractResults: map[string]*outport.SCRInfo{
string(scrHash): {
hex.EncodeToString(scrHash): {
SmartContractResult: &smartContractResult.SmartContractResult{
Nonce: 3,
OriginalTxHash: scheduledTx,
Expand All @@ -408,6 +409,6 @@ func TestAddExecutionOrderInTransactionPoolFromMeTransactionAndScheduledInvalid(
},
}, pool)

require.Equal(t, []string{string(scrHash)}, scrsHashes)
require.Equal(t, []string{string(scheduledInvalidTxHash)}, invalidTxsHashes)
require.Equal(t, []string{hex.EncodeToString(scrHash)}, scrsHashes)
require.Equal(t, []string{hex.EncodeToString(scheduledInvalidTxHash)}, invalidTxsHashes)
}