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 & updates for v3 #272

Merged
merged 1 commit into from
Dec 13, 2023
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
6 changes: 3 additions & 3 deletions clients/batchValidator/batchValidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
Copy link
Contributor

Choose a reason for hiding this comment

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

Hell yeah, no more "util". Uncle Bob would shed a happy tear

"io"
"net/http"
"time"

Expand Down Expand Up @@ -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 := &microserviceBadRequestBody{}
err = json.Unmarshal(data, badResponse)
if err != nil {
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions clients/batchValidator/batchValidator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"math/big"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -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))

Expand Down
4 changes: 2 additions & 2 deletions clients/gasManagement/gasStation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"math/big"
"net/http"
"sync"
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 7 additions & 2 deletions clients/multiversx/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion clients/multiversx/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 5 additions & 7 deletions clients/multiversx/transactionHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
13 changes: 8 additions & 5 deletions clients/multiversx/transactionHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions factory/ethMultiversXBridgeComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"crypto/ecdsa"
"fmt"
"io"
"io/ioutil"
"os"
"sync"
"time"

Expand Down Expand Up @@ -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
}
Expand Down
17 changes: 1 addition & 16 deletions factory/storageFactory_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package factory

import (
"io/ioutil"
"os"
"path"
"testing"
"time"
Expand All @@ -11,30 +9,17 @@ 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")

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",
Expand Down
16 changes: 8 additions & 8 deletions testsCommon/bridge/nonceTransactionsHandlerStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 -
Expand Down