From 85a3def64afeba65357ef01fb3a11857184057d4 Mon Sep 17 00:00:00 2001 From: Iulian Pascalau Date: Wed, 13 Dec 2023 12:26:27 +0200 Subject: [PATCH] - linter issues fixes - replaced `nonceHandlerV1` with `nonceHandlerV2` --- clients/batchValidator/batchValidator.go | 6 +++--- clients/batchValidator/batchValidator_test.go | 4 ++-- clients/gasManagement/gasStation.go | 4 ++-- clients/multiversx/client.go | 9 +++++++-- clients/multiversx/interface.go | 2 +- clients/multiversx/transactionHandler.go | 12 +++++------- clients/multiversx/transactionHandler_test.go | 13 ++++++++----- factory/ethMultiversXBridgeComponents.go | 4 ++-- factory/storageFactory_test.go | 17 +---------------- .../bridge/nonceTransactionsHandlerStub.go | 16 ++++++++-------- 10 files changed, 39 insertions(+), 48 deletions(-) diff --git a/clients/batchValidator/batchValidator.go b/clients/batchValidator/batchValidator.go index 38a0f931..4ecdf8ec 100644 --- a/clients/batchValidator/batchValidator.go +++ b/clients/batchValidator/batchValidator.go @@ -6,7 +6,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "net/http" "time" @@ -115,7 +115,7 @@ func (bv *batchValidator) doRequestReturningBytes(batch []byte, ctx context.Cont return nil, err } if response.StatusCode == http.StatusBadRequest && response.Body != http.NoBody { - data, _ := ioutil.ReadAll(response.Body) + data, _ := io.ReadAll(response.Body) badResponse := µserviceBadRequestBody{} err = json.Unmarshal(data, badResponse) if err != nil { @@ -131,7 +131,7 @@ func (bv *batchValidator) doRequestReturningBytes(batch []byte, ctx context.Cont _ = response.Body.Close() }() - body, err := ioutil.ReadAll(response.Body) + body, err := io.ReadAll(response.Body) if err != nil { return nil, err } diff --git a/clients/batchValidator/batchValidator_test.go b/clients/batchValidator/batchValidator_test.go index 7a785cf9..5d4aaad2 100644 --- a/clients/batchValidator/batchValidator_test.go +++ b/clients/batchValidator/batchValidator_test.go @@ -5,7 +5,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "math/big" "net/http" "net/http/httptest" @@ -261,7 +261,7 @@ func TestBatchValidator_ValidateBatch(t *testing.T) { _ = request.Body.Close() }() - body, err := ioutil.ReadAll(request.Body) + body, err := io.ReadAll(request.Body) require.Nil(t, err) require.Equal(t, expectedJsonString, string(body)) diff --git a/clients/gasManagement/gasStation.go b/clients/gasManagement/gasStation.go index a2b54762..659cf0fe 100644 --- a/clients/gasManagement/gasStation.go +++ b/clients/gasManagement/gasStation.go @@ -4,7 +4,7 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "io" "math/big" "net/http" "sync" @@ -196,7 +196,7 @@ func (gs *gasStation) doRequestReturningBytes(ctx context.Context) ([]byte, erro _ = response.Body.Close() }() - body, err := ioutil.ReadAll(response.Body) + body, err := io.ReadAll(response.Body) if err != nil { return nil, err } diff --git a/clients/multiversx/client.go b/clients/multiversx/client.go index ab1d1be8..ae940d80 100644 --- a/clients/multiversx/client.go +++ b/clients/multiversx/client.go @@ -20,7 +20,7 @@ import ( "github.com/multiversx/mx-sdk-go/builders" "github.com/multiversx/mx-sdk-go/core" "github.com/multiversx/mx-sdk-go/data" - "github.com/multiversx/mx-sdk-go/interactors/nonceHandlerV1" + "github.com/multiversx/mx-sdk-go/interactors/nonceHandlerV2" ) const ( @@ -73,7 +73,12 @@ func NewClient(args ClientArgs) (*client, error) { return nil, err } - nonceTxsHandler, err := nonceHandlerV1.NewNonceTransactionHandlerV1(args.Proxy, time.Second*time.Duration(args.IntervalToResendTxsInSeconds), true) + argNonceHandler := nonceHandlerV2.ArgsNonceTransactionsHandlerV2{ + Proxy: args.Proxy, + IntervalToResend: time.Second * time.Duration(args.IntervalToResendTxsInSeconds), + Creator: &nonceHandlerV2.AddressNonceHandlerCreator{}, + } + nonceTxsHandler, err := nonceHandlerV2.NewNonceTransactionHandlerV2(argNonceHandler) if err != nil { return nil, err } diff --git a/clients/multiversx/interface.go b/clients/multiversx/interface.go index 83d96374..cba066d7 100644 --- a/clients/multiversx/interface.go +++ b/clients/multiversx/interface.go @@ -23,7 +23,7 @@ type Proxy interface { // NonceTransactionsHandler represents the interface able to handle the current nonce and the transactions resend mechanism type NonceTransactionsHandler interface { - GetNonce(ctx context.Context, address core.AddressHandler) (uint64, error) + ApplyNonceAndGasPrice(ctx context.Context, address core.AddressHandler, tx *transaction.FrontendTransaction) error SendTransaction(ctx context.Context, tx *transaction.FrontendTransaction) (string, error) Close() error } diff --git a/clients/multiversx/transactionHandler.go b/clients/multiversx/transactionHandler.go index c6b43fa6..5fe62248 100644 --- a/clients/multiversx/transactionHandler.go +++ b/clients/multiversx/transactionHandler.go @@ -40,11 +40,6 @@ func (txHandler *transactionHandler) signTransaction(ctx context.Context, builde return nil, err } - nonce, err := txHandler.nonceTxHandler.GetNonce(context.Background(), txHandler.relayerAddress) - if err != nil { - return nil, err - } - dataBytes, err := builder.ToDataBytes() if err != nil { return nil, err @@ -54,14 +49,17 @@ func (txHandler *transactionHandler) signTransaction(ctx context.Context, builde ChainID: networkConfig.ChainID, Version: networkConfig.MinTransactionVersion, GasLimit: gasLimit, - GasPrice: networkConfig.MinGasPrice, - Nonce: nonce, Data: dataBytes, Sender: txHandler.relayerAddress.AddressAsBech32String(), Receiver: txHandler.multisigAddressAsBech32, Value: "0", } + err = txHandler.nonceTxHandler.ApplyNonceAndGasPrice(context.Background(), txHandler.relayerAddress, tx) + if err != nil { + return nil, err + } + err = txHandler.signTransactionWithPrivateKey(tx) if err != nil { return nil, err diff --git a/clients/multiversx/transactionHandler_test.go b/clients/multiversx/transactionHandler_test.go index 7aa9cef0..82999a9b 100644 --- a/clients/multiversx/transactionHandler_test.go +++ b/clients/multiversx/transactionHandler_test.go @@ -65,8 +65,8 @@ func TestTransactionHandler_SendTransactionReturnHash(t *testing.T) { expectedErr := errors.New("expected error in get nonce") txHandlerInstance := createTransactionHandlerWithMockComponents() txHandlerInstance.nonceTxHandler = &bridgeTests.NonceTransactionsHandlerStub{ - GetNonceCalled: func(ctx context.Context, address core.AddressHandler) (uint64, error) { - return 0, expectedErr + ApplyNonceAndGasPriceCalled: func(ctx context.Context, address core.AddressHandler, tx *transaction.FrontendTransaction) error { + return expectedErr }, } @@ -140,12 +140,15 @@ func TestTransactionHandler_SendTransactionReturnHash(t *testing.T) { } txHandlerInstance.nonceTxHandler = &bridgeTests.NonceTransactionsHandlerStub{ - GetNonceCalled: func(ctx context.Context, address core.AddressHandler) (uint64, error) { + ApplyNonceAndGasPriceCalled: func(ctx context.Context, address core.AddressHandler, tx *transaction.FrontendTransaction) error { if address.AddressAsBech32String() == relayerAddress { - return nonce, nil + tx.Nonce = nonce + tx.GasPrice = minGasPrice + + return nil } - return 0, errors.New("unexpected address to fetch the nonce") + return errors.New("unexpected address to fetch the nonce") }, SendTransactionCalled: func(ctx context.Context, tx *transaction.FrontendTransaction) (string, error) { sendWasCalled = true diff --git a/factory/ethMultiversXBridgeComponents.go b/factory/ethMultiversXBridgeComponents.go index 2a5aac7a..37970c74 100644 --- a/factory/ethMultiversXBridgeComponents.go +++ b/factory/ethMultiversXBridgeComponents.go @@ -5,7 +5,7 @@ import ( "crypto/ecdsa" "fmt" "io" - "io/ioutil" + "os" "sync" "time" @@ -357,7 +357,7 @@ func (components *ethMultiversXBridgeComponents) createEthereumClient(args ArgsE return err } - privateKeyBytes, err := ioutil.ReadFile(ethereumConfigs.PrivateKeyFile) + privateKeyBytes, err := os.ReadFile(ethereumConfigs.PrivateKeyFile) if err != nil { return err } diff --git a/factory/storageFactory_test.go b/factory/storageFactory_test.go index 1de89944..c15734f9 100644 --- a/factory/storageFactory_test.go +++ b/factory/storageFactory_test.go @@ -1,8 +1,6 @@ package factory import ( - "io/ioutil" - "os" "path" "testing" "time" @@ -11,7 +9,6 @@ import ( "github.com/multiversx/mx-chain-go/config" logger "github.com/multiversx/mx-chain-logger-go" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) var log = logger.GetOrCreate("factory_test") @@ -19,22 +16,10 @@ var log = logger.GetOrCreate("factory_test") func TestCreateUnitStorer(t *testing.T) { t.Parallel() - workingDir, err := ioutil.TempDir("", "") - require.Nil(t, err) + workingDir := t.TempDir() log.Info("created temporary directory", "directory", workingDir) - defer func() { - err = os.RemoveAll(workingDir) - require.Nil(t, err) - - if err == nil { - log.Info("removed temporary directory", "directory", workingDir) - } else { - log.Error("error while removing temporary directory", "directory", workingDir, "error", err) - } - }() - cfg := config.StorageConfig{ Cache: config.CacheConfig{ Name: "StatusMetricsStorage", diff --git a/testsCommon/bridge/nonceTransactionsHandlerStub.go b/testsCommon/bridge/nonceTransactionsHandlerStub.go index 1177a79b..213c5360 100644 --- a/testsCommon/bridge/nonceTransactionsHandlerStub.go +++ b/testsCommon/bridge/nonceTransactionsHandlerStub.go @@ -9,18 +9,18 @@ import ( // NonceTransactionsHandlerStub - type NonceTransactionsHandlerStub struct { - GetNonceCalled func(ctx context.Context, address core.AddressHandler) (uint64, error) - SendTransactionCalled func(ctx context.Context, tx *transaction.FrontendTransaction) (string, error) - CloseCalled func() error + ApplyNonceAndGasPriceCalled func(ctx context.Context, address core.AddressHandler, tx *transaction.FrontendTransaction) error + SendTransactionCalled func(ctx context.Context, tx *transaction.FrontendTransaction) (string, error) + CloseCalled func() error } -// GetNonce - -func (stub *NonceTransactionsHandlerStub) GetNonce(ctx context.Context, address core.AddressHandler) (uint64, error) { - if stub.GetNonceCalled != nil { - return stub.GetNonceCalled(ctx, address) +// ApplyNonceAndGasPrice - +func (stub *NonceTransactionsHandlerStub) ApplyNonceAndGasPrice(ctx context.Context, address core.AddressHandler, tx *transaction.FrontendTransaction) error { + if stub.ApplyNonceAndGasPriceCalled != nil { + return stub.ApplyNonceAndGasPriceCalled(ctx, address, tx) } - return 0, nil + return nil } // SendTransaction -