Skip to content

Commit

Permalink
Merge branch 'feat/v3' into add-docker-image-for-sc-calls-executor
Browse files Browse the repository at this point in the history
  • Loading branch information
iulianpascalau authored Aug 12, 2024
2 parents e38eafd + 46546ad commit 4fa6039
Show file tree
Hide file tree
Showing 30 changed files with 395 additions and 196 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test-coverage:

slow-tests: clean-test
@docker compose -f docker/docker-compose.yml build
@docker compose -f docker/docker-compose.yml up & go test ./integrationTests/... -v -tags slow
@docker compose -f docker/docker-compose.yml up & go test ./integrationTests/... -v -timeout 40m -tags slow
@docker compose -f docker/docker-compose.yml down -v

lint-install:
Expand Down
14 changes: 13 additions & 1 deletion bridges/ethMultiversX/bridgeExecutor.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ type ArgsBridgeExecutor struct {
MaxRestriesOnWasProposed uint64
}

var emtpyCallData = parsers.CallData{
Type: parsers.DataPresentProtocolMarker,
Function: "",
GasLimit: 0,
Arguments: nil,
}

type bridgeExecutor struct {
log logger.Logger
topologyProvider TopologyProvider
Expand Down Expand Up @@ -486,7 +493,12 @@ func (executor *bridgeExecutor) addMetadataToTransfer(transfer *bridgeCommon.Dep
var err error
transfer.DisplayableData, err = ConvertToDisplayableData(transfer.Data)
if err != nil {
executor.log.Warn("failed to convert call data to displayable data", "error", err)
executor.log.Warn("failed to convert call data to displayable data, will alter and call with an empty call data struct",
"error", err,
"call data", fmt.Sprintf("%+v", emtpyCallData))

codec := parsers.MultiversxCodec{}
transfer.Data = codec.EncodeCallData(emtpyCallData)
}

return transfer
Expand Down
105 changes: 76 additions & 29 deletions bridges/ethMultiversX/bridgeExecutor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
bridgeCommon "github.com/multiversx/mx-bridge-eth-go/common"
"github.com/multiversx/mx-bridge-eth-go/core"
"github.com/multiversx/mx-bridge-eth-go/core/batchProcessor"
"github.com/multiversx/mx-bridge-eth-go/parsers"
"github.com/multiversx/mx-bridge-eth-go/testsCommon"
bridgeTests "github.com/multiversx/mx-bridge-eth-go/testsCommon/bridge"
"github.com/multiversx/mx-chain-core-go/core/check"
Expand All @@ -27,6 +28,7 @@ import (
var expectedErr = errors.New("expected error")
var providedBatch = &bridgeCommon.TransferBatch{}
var expectedMaxRetries = uint64(3)
var codec = parsers.MultiversxCodec{}

func createMockExecutorArgs() ArgsBridgeExecutor {
return ArgsBridgeExecutor{
Expand Down Expand Up @@ -399,36 +401,80 @@ func TestEthToMultiversXBridgeExecutor_GetAndStoreBatchFromEthereum(t *testing.T
assert.True(t, expectedBatch == executor.batch)
})
t.Run("should add deposits metadata for sc calls", func(t *testing.T) {
args := createMockExecutorArgs()
providedNonce := uint64(8346)
depositNonce := uint64(100)
depositData := "testData"
expectedBatch := &bridgeCommon.TransferBatch{
ID: providedNonce,
Deposits: []*bridgeCommon.DepositTransfer{
{
Nonce: depositNonce,
t.Run("invalid data", func(t *testing.T) {
t.Parallel()

args := createMockExecutorArgs()
providedNonce := uint64(8346)
depositNonce := uint64(100)
depositData := "testData"
expectedBatch := &bridgeCommon.TransferBatch{
ID: providedNonce,
Deposits: []*bridgeCommon.DepositTransfer{
{
Nonce: depositNonce,
},
},
},
}
args.EthereumClient = &bridgeTests.EthereumClientStub{
GetBatchCalled: func(ctx context.Context, nonce uint64) (*bridgeCommon.TransferBatch, bool, error) {
assert.Equal(t, providedNonce, nonce)
return expectedBatch, true, nil
},
GetBatchSCMetadataCalled: func(ctx context.Context, nonce uint64) ([]*contract.ERC20SafeERC20SCDeposit, error) {
return []*contract.ERC20SafeERC20SCDeposit{{
DepositNonce: big.NewInt(0).SetUint64(depositNonce),
CallData: depositData,
}}, nil
},
}
executor, _ := NewBridgeExecutor(args)
err := executor.GetAndStoreBatchFromEthereum(context.Background(), providedNonce)
}
args.EthereumClient = &bridgeTests.EthereumClientStub{
GetBatchCalled: func(ctx context.Context, nonce uint64) (*bridgeCommon.TransferBatch, bool, error) {
assert.Equal(t, providedNonce, nonce)
return expectedBatch, true, nil
},
GetBatchSCMetadataCalled: func(ctx context.Context, nonce uint64) ([]*contract.ERC20SafeERC20SCDeposit, error) {
return []*contract.ERC20SafeERC20SCDeposit{{
DepositNonce: big.NewInt(0).SetUint64(depositNonce),
CallData: depositData,
}}, nil
},
}
executor, _ := NewBridgeExecutor(args)
err := executor.GetAndStoreBatchFromEthereum(context.Background(), providedNonce)

assert.Nil(t, err)
assert.True(t, expectedBatch == executor.GetStoredBatch()) // pointer testing
expectedDepositData := codec.EncodeCallData(emtpyCallData)
assert.Equal(t, string(expectedDepositData), string(executor.batch.Deposits[0].Data))
})
t.Run("correct data", func(t *testing.T) {
t.Parallel()

args := createMockExecutorArgs()
providedNonce := uint64(8346)
depositNonce := uint64(100)
depositData := string(codec.EncodeCallData(parsers.CallData{
Type: parsers.DataPresentProtocolMarker,
Function: "function",
GasLimit: 37,
Arguments: []string{"arg1", "arg2"},
}))
expectedBatch := &bridgeCommon.TransferBatch{
ID: providedNonce,
Deposits: []*bridgeCommon.DepositTransfer{
{
Nonce: depositNonce,
},
},
}
args.EthereumClient = &bridgeTests.EthereumClientStub{
GetBatchCalled: func(ctx context.Context, nonce uint64) (*bridgeCommon.TransferBatch, bool, error) {
assert.Equal(t, providedNonce, nonce)
return expectedBatch, true, nil
},
GetBatchSCMetadataCalled: func(ctx context.Context, nonce uint64) ([]*contract.ERC20SafeERC20SCDeposit, error) {
return []*contract.ERC20SafeERC20SCDeposit{{
DepositNonce: big.NewInt(0).SetUint64(depositNonce),
CallData: depositData,
}}, nil
},
}
executor, _ := NewBridgeExecutor(args)
err := executor.GetAndStoreBatchFromEthereum(context.Background(), providedNonce)

assert.Nil(t, err)
assert.True(t, expectedBatch == executor.GetStoredBatch()) // pointer testing
assert.Equal(t, depositData, string(executor.batch.Deposits[0].Data))
assert.Nil(t, err)
assert.True(t, expectedBatch == executor.GetStoredBatch()) // pointer testing
assert.Equal(t, depositData, string(executor.batch.Deposits[0].Data))
})
})
t.Run("should add deposits metadata for sc calls even if with no data", func(t *testing.T) {
args := createMockExecutorArgs()
Expand Down Expand Up @@ -460,7 +506,8 @@ func TestEthToMultiversXBridgeExecutor_GetAndStoreBatchFromEthereum(t *testing.T

assert.Nil(t, err)
assert.True(t, expectedBatch == executor.GetStoredBatch()) // pointer testing
assert.Equal(t, "", string(executor.batch.Deposits[0].Data))
expectedDepositData := codec.EncodeCallData(emtpyCallData)
assert.Equal(t, string(expectedDepositData), string(executor.batch.Deposits[0].Data))
})
}

Expand Down
2 changes: 1 addition & 1 deletion clients/multiversx/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func (c *client) ProposeSetStatus(ctx context.Context, batch *common.TransferBat
gasLimit := c.gasMapConfig.ProposeStatusBase + uint64(len(batch.Deposits))*c.gasMapConfig.ProposeStatusForEach
hash, err := c.txHandler.SendTransactionReturnHash(ctx, txBuilder, gasLimit)
if err == nil {
c.log.Info("proposed set statuses"+batch.String(), "transaction hash", hash)
c.log.Info("proposed set statuses "+batch.String(), "transaction hash", hash)
}

return hash, err
Expand Down
2 changes: 1 addition & 1 deletion cmd/scCallsExecutor/config/config.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ScProxyBech32Address = "erd1qqqqqqqqqqqqqpgqnef5f5aq32d63kljld8w5vnvz4gk5sy9hrrq2ld08s"
ExtraGasToExecute = 50000000
ExtraGasToExecute = 60000000 #this value allow the SC calls without provided gas limit to be refunded
NetworkAddress = "127.0.0.1:8085"
ProxyMaxNoncesDelta = 7
ProxyFinalityCheck = true
Expand Down
4 changes: 2 additions & 2 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
version: "3.9"

services:
multiversx:
image: multiversx/chainsimulator:v1.7.13-fix3
chain-simulator:
image: multiversx/chainsimulator:v1.7.13-patch1
ports:
- 8085:8085
volumes:
Expand Down
6 changes: 6 additions & 0 deletions executors/multiversx/module/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ type pollingHandler interface {
Close() error
IsInterfaceNil() bool
}

type executor interface {
Execute(ctx context.Context) error
GetNumSentTransaction() uint32
IsInterfaceNil() bool
}
14 changes: 10 additions & 4 deletions executors/multiversx/module/scCallsModule.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ var keyGen = signing.NewKeyGenerator(suite)
var singleSigner = &singlesig.Ed25519Signer{}

type scCallsModule struct {
nonceTxsHandler nonceTransactionsHandler
pollingHandler pollingHandler
nonceTxsHandler nonceTransactionsHandler
pollingHandler pollingHandler
executorInstance executor
}

// NewScCallsModule creates a starts a new scCallsModule instance
Expand Down Expand Up @@ -82,7 +83,7 @@ func NewScCallsModule(cfg config.ScCallsModuleConfig, log logger.Logger) (*scCal
PrivateKey: privateKey,
SingleSigner: singleSigner,
}
executor, err := multiversx.NewScCallExecutor(argsExecutor)
module.executorInstance, err = multiversx.NewScCallExecutor(argsExecutor)
if err != nil {
return nil, err
}
Expand All @@ -92,7 +93,7 @@ func NewScCallsModule(cfg config.ScCallsModuleConfig, log logger.Logger) (*scCal
Name: "MultiversX SC calls",
PollingInterval: time.Duration(cfg.PollingIntervalInMillis) * time.Millisecond,
PollingWhenError: time.Duration(cfg.PollingIntervalInMillis) * time.Millisecond,
Executor: executor,
Executor: module.executorInstance,
}

module.pollingHandler, err = polling.NewPollingHandler(argsPollingHandler)
Expand All @@ -108,6 +109,11 @@ func NewScCallsModule(cfg config.ScCallsModuleConfig, log logger.Logger) (*scCal
return module, nil
}

// GetNumSentTransaction returns the total sent transactions
func (module *scCallsModule) GetNumSentTransaction() uint32 {
return module.executorInstance.GetNumSentTransaction()
}

// Close closes any components started
func (module *scCallsModule) Close() error {
errPollingHandler := module.pollingHandler.Close()
Expand Down
9 changes: 9 additions & 0 deletions executors/multiversx/scCallsExecutor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"math/big"
"sync/atomic"

"github.com/ethereum/go-ethereum/log"
"github.com/multiversx/mx-bridge-eth-go/errors"
Expand Down Expand Up @@ -49,6 +50,7 @@ type scCallExecutor struct {
privateKey crypto.PrivateKey
singleSigner crypto.SingleSigner
senderAddress core.AddressHandler
numSentTransactions uint32
}

// NewScCallExecutor creates a new instance of type scCallExecutor
Expand Down Expand Up @@ -244,6 +246,8 @@ func (executor *scCallExecutor) executeOperation(
"extra gas", executor.extraGasToExecute,
"sender", bech32Address)

atomic.AddUint32(&executor.numSentTransactions, 1)

return nil
}

Expand All @@ -265,6 +269,11 @@ func (executor *scCallExecutor) signTransactionWithPrivateKey(tx *transaction.Fr
return nil
}

// GetNumSentTransaction returns the total sent transactions
func (executor *scCallExecutor) GetNumSentTransaction() uint32 {
return atomic.LoadUint32(&executor.numSentTransactions)
}

// IsInterfaceNil returns true if there is no value under the interface
func (executor *scCallExecutor) IsInterfaceNil() bool {
return executor == nil
Expand Down
17 changes: 16 additions & 1 deletion executors/multiversx/scCallsExecutor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func TestScCallExecutor_Execute(t *testing.T) {
executor, _ := NewScCallExecutor(args)
err := executor.Execute(context.Background())
assert.Equal(t, expectedError, err)
assert.Zero(t, executor.GetNumSentTransaction())
})
t.Run("get pending returns a not ok status, should error", func(t *testing.T) {
t.Parallel()
Expand All @@ -206,6 +207,7 @@ func TestScCallExecutor_Execute(t *testing.T) {
err := executor.Execute(context.Background())
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "got response code 'NOT OK'")
assert.Zero(t, executor.GetNumSentTransaction())
})
t.Run("get pending returns an odd number of lines, should error", func(t *testing.T) {
t.Parallel()
Expand All @@ -228,6 +230,7 @@ func TestScCallExecutor_Execute(t *testing.T) {
err := executor.Execute(context.Background())
assert.ErrorIs(t, err, errInvalidNumberOfResponseLines)
assert.Contains(t, err.Error(), "expected an even number, got 1")
assert.Zero(t, executor.GetNumSentTransaction())
})
t.Run("decoder errors, should error", func(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -257,6 +260,7 @@ func TestScCallExecutor_Execute(t *testing.T) {
executor, _ := NewScCallExecutor(args)
err := executor.Execute(context.Background())
assert.ErrorIs(t, err, expectedError)
assert.Zero(t, executor.GetNumSentTransaction())
})
t.Run("get network configs errors, should error", func(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -289,6 +293,7 @@ func TestScCallExecutor_Execute(t *testing.T) {
executor, _ := NewScCallExecutor(args)
err := executor.Execute(context.Background())
assert.ErrorIs(t, err, expectedError)
assert.Zero(t, executor.GetNumSentTransaction())
})
t.Run("ApplyNonceAndGasPrice errors, should error", func(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -330,6 +335,7 @@ func TestScCallExecutor_Execute(t *testing.T) {
executor, _ := NewScCallExecutor(args)
err := executor.Execute(context.Background())
assert.ErrorIs(t, err, expectedError)
assert.Zero(t, executor.GetNumSentTransaction())
})
t.Run("Sign errors, should error", func(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -376,6 +382,7 @@ func TestScCallExecutor_Execute(t *testing.T) {
executor, _ := NewScCallExecutor(args)
err := executor.Execute(context.Background())
assert.ErrorIs(t, err, expectedError)
assert.Zero(t, executor.GetNumSentTransaction())
})
t.Run("SendTransaction errors, should error", func(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -409,13 +416,20 @@ func TestScCallExecutor_Execute(t *testing.T) {
DecodeProxySCCompleteCallDataCalled: func(buff []byte) (parsers.ProxySCCompleteCallData, error) {
assert.Equal(t, []byte{0x03, 0x04}, buff)

return parsers.ProxySCCompleteCallData{}, nil
return parsers.ProxySCCompleteCallData{
CallData: parsers.CallData{
Type: parsers.DataPresentProtocolMarker,
Function: "func",
GasLimit: 1000000,
},
}, nil
},
}

executor, _ := NewScCallExecutor(args)
err := executor.Execute(context.Background())
assert.ErrorIs(t, err, expectedError)
assert.Equal(t, uint32(0), executor.GetNumSentTransaction())
})
t.Run("should work", func(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -505,5 +519,6 @@ func TestScCallExecutor_Execute(t *testing.T) {
err := executor.Execute(context.Background())
assert.Nil(t, err)
assert.True(t, sendWasCalled)
assert.Equal(t, uint32(1), executor.GetNumSentTransaction())
})
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/multiversx/mx-chain-communication-go v1.0.14
github.com/multiversx/mx-chain-core-go v1.2.20
github.com/multiversx/mx-chain-crypto-go v1.2.11
github.com/multiversx/mx-chain-go v1.7.12
github.com/multiversx/mx-chain-go v1.7.13-patch1
github.com/multiversx/mx-chain-logger-go v1.0.14
github.com/multiversx/mx-sdk-go v1.4.1
github.com/pelletier/go-toml v1.9.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -532,8 +532,8 @@ github.com/multiversx/mx-chain-crypto-go v1.2.11 h1:MNPJoiTJA5/tedYrI0N22OorbsKD
github.com/multiversx/mx-chain-crypto-go v1.2.11/go.mod h1:pcZutPdfLiAFytzCU3LxU3s8cXkvpNqquyitFSfoF3o=
github.com/multiversx/mx-chain-es-indexer-go v1.4.21 h1:rzxXCkgOsqj67GRYtqzKuf9XgHwnZLTZhU90Ck3VbrE=
github.com/multiversx/mx-chain-es-indexer-go v1.4.21/go.mod h1:V9xxOBkfV7GjN4K5SODaOetoGVpQm4snibMVPCjL0Kk=
github.com/multiversx/mx-chain-go v1.7.12 h1:cQ3g5sFZEcQmIRwi/wt+K/3d5nIwCMQRC1ZnJDRuRY4=
github.com/multiversx/mx-chain-go v1.7.12/go.mod h1:HwklJGQfMpv/yyF4oLpxjwdKCawspv1JjdgezlWBpRQ=
github.com/multiversx/mx-chain-go v1.7.13-patch1 h1:x3U0BFpQrwXcXXa81KMgM25hNRGgjsquzq6ot+f9Ot4=
github.com/multiversx/mx-chain-go v1.7.13-patch1/go.mod h1:qyW6EBgsqgZwA+SrAhLVi6cCM6kFqlm6Lh28Re1LrIA=
github.com/multiversx/mx-chain-logger-go v1.0.14 h1:PRMpAvXE7Nec2d//QNmbYfKVHMomOKmcN4UXurQWX9o=
github.com/multiversx/mx-chain-logger-go v1.0.14/go.mod h1:bDfHSdwqIimn7Gp8w+SH5KlDuGzJ//nlyEANAaTSc3o=
github.com/multiversx/mx-chain-scenario-go v1.4.3 h1:9xeVB8TOsolXS4YEr1CZ/VZr5Qk0X+nde8nRGnxJICo=
Expand Down
Loading

0 comments on commit 4fa6039

Please sign in to comment.