Skip to content

Commit

Permalink
- added extra protection and removed duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
iulianpascalau committed Dec 27, 2024
1 parent 132992f commit 7b0500c
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 36 deletions.
35 changes: 35 additions & 0 deletions executors/multiversx/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

bridgeCore "github.com/multiversx/mx-bridge-eth-go/core"
"github.com/multiversx/mx-bridge-eth-go/errors"
"github.com/multiversx/mx-chain-core-go/core/check"
logger "github.com/multiversx/mx-chain-logger-go"
"github.com/multiversx/mx-sdk-go/data"
Expand Down Expand Up @@ -79,3 +80,37 @@ func (executor *baseExecutor) filterOperations(component string, pendingOperatio

return result
}

func (executor *baseExecutor) executeVmQuery(ctx context.Context, scProxyAddress string, function string) (*data.VmValuesResponseData, error) {
request := &data.VmValueRequest{
Address: scProxyAddress,
FuncName: function,
}

response, err := executor.proxy.ExecuteVMQuery(ctx, request)
if err != nil {
executor.log.Error("got error on VMQuery", "FuncName", request.FuncName,
"Args", request.Args, "SC address", request.Address, "Caller", request.CallerAddr, "error", err)
return nil, err
}
if response == nil || response.Data == nil {
return nil, errors.NewQueryResponseError(
emptyErrorCode,
nilResponseData,
request.FuncName,
request.Address,
request.Args...,
)
}
if response.Data.ReturnCode != okCodeAfterExecution {
return nil, errors.NewQueryResponseError(
response.Data.ReturnCode,
response.Data.ReturnMessage,
request.FuncName,
request.Address,
request.Args...,
)
}

return response, nil
}
21 changes: 3 additions & 18 deletions executors/multiversx/refundExecutor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"math/big"

bridgeCore "github.com/multiversx/mx-bridge-eth-go/core"
"github.com/multiversx/mx-bridge-eth-go/errors"
logger "github.com/multiversx/mx-chain-logger-go"
"github.com/multiversx/mx-sdk-go/builders"
"github.com/multiversx/mx-sdk-go/data"
Expand All @@ -16,6 +15,8 @@ const (
getRefundTransactionsFunction = "getRefundTransactions"
executeRefundTransactionFunction = "executeRefundTransaction"
refundTxType = "refund"
nilResponseData = "nil response data"
emptyErrorCode = ""
)

// ArgsRefundExecutor represents the DTO struct for creating a new instance of type refundExecutor
Expand Down Expand Up @@ -79,26 +80,10 @@ func (executor *refundExecutor) executeRefundForScProxyAddress(ctx context.Conte
}

func (executor *refundExecutor) getPendingRefunds(ctx context.Context, scProxyAddress string) (map[uint64]bridgeCore.ProxySCCompleteCallData, error) {
request := &data.VmValueRequest{
Address: scProxyAddress,
FuncName: getRefundTransactionsFunction,
}

response, err := executor.proxy.ExecuteVMQuery(ctx, request)
response, err := executor.executeVmQuery(ctx, scProxyAddress, getRefundTransactionsFunction)
if err != nil {
executor.log.Error("got error on VMQuery", "FuncName", request.FuncName,
"Args", request.Args, "SC address", request.Address, "Caller", request.CallerAddr, "error", err)
return nil, err
}
if response.Data.ReturnCode != okCodeAfterExecution {
return nil, errors.NewQueryResponseError(
response.Data.ReturnCode,
response.Data.ReturnMessage,
request.FuncName,
request.Address,
request.Args...,
)
}

return executor.parseResponse(response)
}
Expand Down
30 changes: 30 additions & 0 deletions executors/multiversx/refundExecutor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,36 @@ func TestRefundExecutor_Execute(t *testing.T) {
assert.Contains(t, err.Error(), expectedError.Error())
assert.Contains(t, err.Error(), "errors found during execution")
})
t.Run("get pending returns an invalid vm values response (nil and nil), should error", func(t *testing.T) {
t.Parallel()

args := argsForErrors // value copy
args.Proxy = &interactors.ProxyStub{
ExecuteVMQueryCalled: func(ctx context.Context, vmRequest *data.VmValueRequest) (*data.VmValuesResponseData, error) {
return nil, nil
},
}

executor, _ := NewRefundExecutor(args)
err := executor.Execute(context.Background())
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "nil response data")
})
t.Run("get pending returns an invalid vm values response (nil data), should error", func(t *testing.T) {
t.Parallel()

args := argsForErrors // value copy
args.Proxy = &interactors.ProxyStub{
ExecuteVMQueryCalled: func(ctx context.Context, vmRequest *data.VmValueRequest) (*data.VmValuesResponseData, error) {
return &data.VmValuesResponseData{}, nil
},
}

executor, _ := NewRefundExecutor(args)
err := executor.Execute(context.Background())
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "nil response data")
})
t.Run("get pending returns a not ok status, should error", func(t *testing.T) {
t.Parallel()

Expand Down
19 changes: 1 addition & 18 deletions executors/multiversx/scCallsExecutor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/multiversx/mx-bridge-eth-go/config"
bridgeCore "github.com/multiversx/mx-bridge-eth-go/core"
"github.com/multiversx/mx-bridge-eth-go/errors"
logger "github.com/multiversx/mx-chain-logger-go"
"github.com/multiversx/mx-sdk-go/builders"
"github.com/multiversx/mx-sdk-go/data"
Expand Down Expand Up @@ -98,26 +97,10 @@ func (executor *scCallExecutor) executeScCallForScProxyAddress(ctx context.Conte
}

func (executor *scCallExecutor) getPendingOperations(ctx context.Context, scProxyAddress string) (map[uint64]bridgeCore.ProxySCCompleteCallData, error) {
request := &data.VmValueRequest{
Address: scProxyAddress,
FuncName: getPendingTransactionsFunction,
}

response, err := executor.proxy.ExecuteVMQuery(ctx, request)
response, err := executor.executeVmQuery(ctx, scProxyAddress, getPendingTransactionsFunction)
if err != nil {
executor.log.Error("got error on VMQuery", "FuncName", request.FuncName,
"Args", request.Args, "SC address", request.Address, "Caller", request.CallerAddr, "error", err)
return nil, err
}
if response.Data.ReturnCode != okCodeAfterExecution {
return nil, errors.NewQueryResponseError(
response.Data.ReturnCode,
response.Data.ReturnMessage,
request.FuncName,
request.Address,
request.Args...,
)
}

return executor.parseResponse(response)
}
Expand Down
30 changes: 30 additions & 0 deletions executors/multiversx/scCallsExecutor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,36 @@ func TestScCallExecutor_Execute(t *testing.T) {
assert.Contains(t, err.Error(), expectedError.Error())
assert.Contains(t, err.Error(), "errors found during execution")
})
t.Run("get pending returns an invalid vm values response (nil and nil), should error", func(t *testing.T) {
t.Parallel()

args := argsForErrors // value copy
args.Proxy = &interactors.ProxyStub{
ExecuteVMQueryCalled: func(ctx context.Context, vmRequest *data.VmValueRequest) (*data.VmValuesResponseData, error) {
return nil, nil
},
}

executor, _ := NewScCallExecutor(args)
err := executor.Execute(context.Background())
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "nil response data")
})
t.Run("get pending returns an invalid vm values response (nil data), should error", func(t *testing.T) {
t.Parallel()

args := argsForErrors // value copy
args.Proxy = &interactors.ProxyStub{
ExecuteVMQueryCalled: func(ctx context.Context, vmRequest *data.VmValueRequest) (*data.VmValuesResponseData, error) {
return &data.VmValuesResponseData{}, nil
},
}

executor, _ := NewScCallExecutor(args)
err := executor.Execute(context.Background())
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "nil response data")
})
t.Run("get pending returns a not ok status, should error", func(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 7b0500c

Please sign in to comment.