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

Minor tests & production code fixes #314

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package multiversxtoeth

import (
"context"
"errors"

"github.com/multiversx/mx-bridge-eth-go/bridges/ethMultiversX/steps"
"github.com/multiversx/mx-bridge-eth-go/clients"
"github.com/multiversx/mx-bridge-eth-go/core"
logger "github.com/multiversx/mx-chain-logger-go"
)
Expand All @@ -22,12 +24,13 @@ func (step *resolveSetStatusStep) Execute(ctx context.Context) core.StepIdentifi
}

batch, err := step.bridge.GetBatchFromMultiversX(ctx)
if err != nil {
step.bridge.PrintInfo(logger.LogError, "error while fetching batch", "error", err)
isEmptyBatch := batch == nil || (err != nil && errors.Is(err, clients.ErrNoPendingBatchAvailable))
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 a great improvement on the logging side

if isEmptyBatch {
step.bridge.PrintInfo(logger.LogDebug, "nil/empty batch fetched")
return GettingPendingBatchFromMultiversX
}
if batch == nil {
step.bridge.PrintInfo(logger.LogDebug, "nil batch fetched")
if err != nil {
step.bridge.PrintInfo(logger.LogError, "error while fetching batch", "error", err)
return GettingPendingBatchFromMultiversX
}

Expand Down
5 changes: 5 additions & 0 deletions integrationTests/relayers/slowTests/framework/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,8 @@ func (address *MvxAddress) Bech32() string {
func (address *MvxAddress) Hex() string {
return address.hex
}

// String returns the address in bech32 format
func (address *MvxAddress) String() string {
return address.bech32
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const (
generateBlocksUntilEpochReachedEndpoint = "simulator/generate-blocks-until-epoch-reached/%d"
numProbeRetries = 10
networkConfigEndpointTemplate = "network/status/%d"
maxNumBlocksToBeProcessed = 15
minNumBlocksToBeProcessedForEachTx = 5
)

var (
Expand Down Expand Up @@ -119,7 +121,7 @@ func (instance *chainSimulatorWrapper) GetNetworkAddress() string {
}

// DeploySC will deploy the provided smart contract and return its address
func (instance *chainSimulatorWrapper) DeploySC(ctx context.Context, wasmFilePath string, ownerSK []byte, parameters []string) *MvxAddress {
func (instance *chainSimulatorWrapper) DeploySC(ctx context.Context, wasmFilePath string, ownerSK []byte, parameters []string) (*MvxAddress, string, *data.TransactionOnNetwork) {
networkConfig, err := instance.proxyInstance.GetNetworkConfig(ctx)
require.Nil(instance.TB, err)

Expand All @@ -145,19 +147,28 @@ func (instance *chainSimulatorWrapper) DeploySC(ctx context.Context, wasmFilePat
}

hash := instance.signAndSend(ctx, ownerSK, ftx)
log.Info("contract deployed", "hash", hash)
txResult := instance.getTransactionResult(ctx, hash)

txResult := instance.GetTransactionResult(ctx, hash)

return NewMvxAddressFromBech32(instance.TB, txResult.Logs.Events[0].Address)
return NewMvxAddressFromBech32(instance.TB, txResult.Logs.Events[0].Address), hash, txResult
}

// GetTransactionResult tries to get a transaction result. It may wait a few blocks
func (instance *chainSimulatorWrapper) GetTransactionResult(ctx context.Context, hash string) *data.TransactionOnNetwork {
// TODO: refactor here
instance.GenerateBlocks(ctx, 10)
func (instance *chainSimulatorWrapper) getTransactionResult(ctx context.Context, hash string) *data.TransactionOnNetwork {
instance.GenerateBlocks(ctx, minNumBlocksToBeProcessedForEachTx)

for i := 0; i < maxNumBlocksToBeProcessed; i++ {
instance.GenerateBlocks(ctx, 1)

status, txOnNetwork := instance.getTxInfoWithResultsIfTxProcessingFinished(ctx, hash)
if status == transaction.TxStatusPending {
continue
}
require.Equal(instance.TB, transaction.TxStatusSuccess, status, fmt.Sprintf("status not OK for transaction hash %s", hash))
return txOnNetwork
}

return instance.getTxInfoWithResultsIfTxProcessingFinished(ctx, hash)
require.Fail(instance.TB, fmt.Sprintf("status still pending for transaction hash %s", hash))
return nil
}

// GenerateBlocks calls the chain simulator generate block endpoint
Expand All @@ -178,27 +189,22 @@ func (instance *chainSimulatorWrapper) GenerateBlocksUntilEpochReached(ctx conte
}
}

func (instance *chainSimulatorWrapper) getTxInfoWithResultsIfTxProcessingFinished(ctx context.Context, hash string) *data.TransactionOnNetwork {
func (instance *chainSimulatorWrapper) getTxInfoWithResultsIfTxProcessingFinished(ctx context.Context, hash string) (transaction.TxStatus, *data.TransactionOnNetwork) {
txStatus, err := instance.proxyInstance.ProcessTransactionStatus(ctx, hash)
require.Nil(instance, err)

if txStatus == transaction.TxStatusPending {
return nil
}

if txStatus != transaction.TxStatusSuccess {
log.Warn("something went wrong with the transaction", "hash", hash, "status", txStatus)
return txStatus, nil
}

txResult, errGet := instance.proxyInstance.GetTransactionInfoWithResults(ctx, hash)
require.Nil(instance, errGet)

return &txResult.Data.Transaction

return txStatus, &txResult.Data.Transaction
}

// ScCall will make the provided sc call
func (instance *chainSimulatorWrapper) ScCall(ctx context.Context, senderSK []byte, contract *MvxAddress, value string, function string, parameters []string) string {
func (instance *chainSimulatorWrapper) ScCall(ctx context.Context, senderSK []byte, contract *MvxAddress, value string, function string, parameters []string) (string, *data.TransactionOnNetwork) {
params := []string{function}
params = append(params, parameters...)
txData := strings.Join(params, "@")
Expand All @@ -207,7 +213,7 @@ func (instance *chainSimulatorWrapper) ScCall(ctx context.Context, senderSK []by
}

// SendTx will build and send a transaction
func (instance *chainSimulatorWrapper) SendTx(ctx context.Context, senderSK []byte, receiver *MvxAddress, value string, dataField []byte) string {
func (instance *chainSimulatorWrapper) SendTx(ctx context.Context, senderSK []byte, receiver *MvxAddress, value string, dataField []byte) (string, *data.TransactionOnNetwork) {
networkConfig, err := instance.proxyInstance.GetNetworkConfig(ctx)
require.Nil(instance, err)

Expand All @@ -227,7 +233,10 @@ func (instance *chainSimulatorWrapper) SendTx(ctx context.Context, senderSK []by
Version: 1,
}

return instance.signAndSend(ctx, senderSK, ftx)
hash := instance.signAndSend(ctx, senderSK, ftx)
txResult := instance.getTransactionResult(ctx, hash)

return hash, txResult
}

// FundWallets sends funds to the provided addresses
Expand Down
7 changes: 3 additions & 4 deletions integrationTests/relayers/slowTests/framework/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ type Relayer interface {
type ChainSimulatorWrapper interface {
Proxy() multiversx.Proxy
GetNetworkAddress() string
DeploySC(ctx context.Context, path string, ownerSK []byte, extraParams []string) *MvxAddress
ScCall(ctx context.Context, senderSK []byte, contract *MvxAddress, value string, function string, parameters []string) string
SendTx(ctx context.Context, senderSK []byte, receiver *MvxAddress, value string, dataField []byte) string
GetTransactionResult(ctx context.Context, hash string) *data.TransactionOnNetwork
DeploySC(ctx context.Context, path string, ownerSK []byte, extraParams []string) (*MvxAddress, string, *data.TransactionOnNetwork)
ScCall(ctx context.Context, senderSK []byte, contract *MvxAddress, value string, function string, parameters []string) (string, *data.TransactionOnNetwork)
SendTx(ctx context.Context, senderSK []byte, receiver *MvxAddress, value string, dataField []byte) (string, *data.TransactionOnNetwork)
FundWallets(ctx context.Context, wallets []string)
GenerateBlocksUntilEpochReached(ctx context.Context, epoch uint32)
GenerateBlocks(ctx context.Context, numBlocks int)
Expand Down
Loading
Loading