2022-07-29 10:35:07 +00:00
|
|
|
package backend
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2022-11-14 19:40:14 +00:00
|
|
|
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
|
2022-07-29 10:35:07 +00:00
|
|
|
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
|
2022-10-11 18:43:32 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2022-07-29 10:35:07 +00:00
|
|
|
"github.com/evmos/ethermint/rpc/backend/mocks"
|
|
|
|
rpc "github.com/evmos/ethermint/rpc/types"
|
|
|
|
"github.com/evmos/ethermint/tests"
|
|
|
|
evmtypes "github.com/evmos/ethermint/x/evm/types"
|
|
|
|
mock "github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
|
|
|
// QueryClient defines a mocked object that implements the ethermint GRPC
|
|
|
|
// QueryClient interface. It allows for performing QueryClient queries without having
|
|
|
|
// to run a ethermint GRPC server.
|
|
|
|
//
|
|
|
|
// To use a mock method it has to be registered in a given test.
|
2022-10-11 18:43:32 +00:00
|
|
|
var _ evmtypes.QueryClient = &mocks.EVMQueryClient{}
|
2022-07-29 10:35:07 +00:00
|
|
|
|
|
|
|
// Params
|
2022-10-11 18:43:32 +00:00
|
|
|
func RegisterParams(queryClient *mocks.EVMQueryClient, header *metadata.MD, height int64) {
|
2022-07-29 10:35:07 +00:00
|
|
|
queryClient.On("Params", rpc.ContextWithHeight(height), &evmtypes.QueryParamsRequest{}, grpc.Header(header)).
|
|
|
|
Return(&evmtypes.QueryParamsResponse{}, nil).
|
|
|
|
Run(func(args mock.Arguments) {
|
|
|
|
// If Params call is successful, also update the header height
|
|
|
|
arg := args.Get(2).(grpc.HeaderCallOption)
|
|
|
|
h := metadata.MD{}
|
|
|
|
h.Set(grpctypes.GRPCBlockHeightHeader, fmt.Sprint(height))
|
|
|
|
*arg.HeaderAddr = h
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-11 18:43:32 +00:00
|
|
|
func RegisterParamsInvalidHeader(queryClient *mocks.EVMQueryClient, header *metadata.MD, height int64) {
|
2022-07-29 10:35:07 +00:00
|
|
|
queryClient.On("Params", rpc.ContextWithHeight(height), &evmtypes.QueryParamsRequest{}, grpc.Header(header)).
|
|
|
|
Return(&evmtypes.QueryParamsResponse{}, nil).
|
|
|
|
Run(func(args mock.Arguments) {
|
|
|
|
// If Params call is successful, also update the header height
|
|
|
|
arg := args.Get(2).(grpc.HeaderCallOption)
|
|
|
|
h := metadata.MD{}
|
|
|
|
*arg.HeaderAddr = h
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-11 18:43:32 +00:00
|
|
|
func RegisterParamsInvalidHeight(queryClient *mocks.EVMQueryClient, header *metadata.MD, height int64) {
|
2022-07-29 10:35:07 +00:00
|
|
|
queryClient.On("Params", rpc.ContextWithHeight(height), &evmtypes.QueryParamsRequest{}, grpc.Header(header)).
|
|
|
|
Return(&evmtypes.QueryParamsResponse{}, nil).
|
|
|
|
Run(func(args mock.Arguments) {
|
|
|
|
// If Params call is successful, also update the header height
|
|
|
|
arg := args.Get(2).(grpc.HeaderCallOption)
|
|
|
|
h := metadata.MD{}
|
|
|
|
h.Set(grpctypes.GRPCBlockHeightHeader, "invalid")
|
|
|
|
*arg.HeaderAddr = h
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Params returns error
|
2022-10-11 18:43:32 +00:00
|
|
|
func RegisterParamsError(queryClient *mocks.EVMQueryClient, header *metadata.MD, height int64) {
|
2022-07-29 10:35:07 +00:00
|
|
|
queryClient.On("Params", rpc.ContextWithHeight(height), &evmtypes.QueryParamsRequest{}, grpc.Header(header)).
|
2022-11-14 19:40:14 +00:00
|
|
|
Return(nil, errortypes.ErrInvalidRequest)
|
2022-07-29 10:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRegisterParams(t *testing.T) {
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := mocks.NewEVMQueryClient(t)
|
2022-07-29 10:35:07 +00:00
|
|
|
var header metadata.MD
|
|
|
|
height := int64(1)
|
|
|
|
RegisterParams(queryClient, &header, height)
|
|
|
|
|
|
|
|
_, err := queryClient.Params(rpc.ContextWithHeight(height), &evmtypes.QueryParamsRequest{}, grpc.Header(&header))
|
|
|
|
require.NoError(t, err)
|
|
|
|
blockHeightHeader := header.Get(grpctypes.GRPCBlockHeightHeader)
|
|
|
|
headerHeight, err := strconv.ParseInt(blockHeightHeader[0], 10, 64)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, height, headerHeight)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRegisterParamsError(t *testing.T) {
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := mocks.NewEVMQueryClient(t)
|
2022-07-29 10:35:07 +00:00
|
|
|
RegisterBaseFeeError(queryClient)
|
|
|
|
_, err := queryClient.BaseFee(rpc.ContextWithHeight(1), &evmtypes.QueryBaseFeeRequest{})
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BaseFee
|
2022-10-11 18:43:32 +00:00
|
|
|
func RegisterBaseFee(queryClient *mocks.EVMQueryClient, baseFee sdk.Int) {
|
2022-07-29 10:35:07 +00:00
|
|
|
queryClient.On("BaseFee", rpc.ContextWithHeight(1), &evmtypes.QueryBaseFeeRequest{}).
|
|
|
|
Return(&evmtypes.QueryBaseFeeResponse{BaseFee: &baseFee}, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Base fee returns error
|
2022-10-11 18:43:32 +00:00
|
|
|
func RegisterBaseFeeError(queryClient *mocks.EVMQueryClient) {
|
2022-07-29 10:35:07 +00:00
|
|
|
queryClient.On("BaseFee", rpc.ContextWithHeight(1), &evmtypes.QueryBaseFeeRequest{}).
|
|
|
|
Return(&evmtypes.QueryBaseFeeResponse{}, evmtypes.ErrInvalidBaseFee)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Base fee not enabled
|
2022-10-11 18:43:32 +00:00
|
|
|
func RegisterBaseFeeDisabled(queryClient *mocks.EVMQueryClient) {
|
2022-07-29 10:35:07 +00:00
|
|
|
queryClient.On("BaseFee", rpc.ContextWithHeight(1), &evmtypes.QueryBaseFeeRequest{}).
|
|
|
|
Return(&evmtypes.QueryBaseFeeResponse{}, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRegisterBaseFee(t *testing.T) {
|
|
|
|
baseFee := sdk.NewInt(1)
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := mocks.NewEVMQueryClient(t)
|
2022-07-29 10:35:07 +00:00
|
|
|
RegisterBaseFee(queryClient, baseFee)
|
|
|
|
res, err := queryClient.BaseFee(rpc.ContextWithHeight(1), &evmtypes.QueryBaseFeeRequest{})
|
|
|
|
require.Equal(t, &evmtypes.QueryBaseFeeResponse{BaseFee: &baseFee}, res)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRegisterBaseFeeError(t *testing.T) {
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := mocks.NewEVMQueryClient(t)
|
2022-07-29 10:35:07 +00:00
|
|
|
RegisterBaseFeeError(queryClient)
|
|
|
|
res, err := queryClient.BaseFee(rpc.ContextWithHeight(1), &evmtypes.QueryBaseFeeRequest{})
|
|
|
|
require.Equal(t, &evmtypes.QueryBaseFeeResponse{}, res)
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRegisterBaseFeeDisabled(t *testing.T) {
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := mocks.NewEVMQueryClient(t)
|
2022-07-29 10:35:07 +00:00
|
|
|
RegisterBaseFeeDisabled(queryClient)
|
|
|
|
res, err := queryClient.BaseFee(rpc.ContextWithHeight(1), &evmtypes.QueryBaseFeeRequest{})
|
|
|
|
require.Equal(t, &evmtypes.QueryBaseFeeResponse{}, res)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidatorAccount
|
2022-10-11 18:43:32 +00:00
|
|
|
func RegisterValidatorAccount(queryClient *mocks.EVMQueryClient, validator sdk.AccAddress) {
|
2022-07-29 10:35:07 +00:00
|
|
|
queryClient.On("ValidatorAccount", rpc.ContextWithHeight(1), &evmtypes.QueryValidatorAccountRequest{}).
|
|
|
|
Return(
|
|
|
|
&evmtypes.QueryValidatorAccountResponse{
|
|
|
|
AccountAddress: validator.String(),
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-10-11 18:43:32 +00:00
|
|
|
func RegisterValidatorAccountError(queryClient *mocks.EVMQueryClient) {
|
2022-07-29 10:35:07 +00:00
|
|
|
queryClient.On("ValidatorAccount", rpc.ContextWithHeight(1), &evmtypes.QueryValidatorAccountRequest{}).
|
|
|
|
Return(nil, status.Error(codes.InvalidArgument, "empty request"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRegisterValidatorAccount(t *testing.T) {
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := mocks.NewEVMQueryClient(t)
|
2022-07-29 10:35:07 +00:00
|
|
|
|
|
|
|
validator := sdk.AccAddress(tests.GenerateAddress().Bytes())
|
|
|
|
RegisterValidatorAccount(queryClient, validator)
|
|
|
|
res, err := queryClient.ValidatorAccount(rpc.ContextWithHeight(1), &evmtypes.QueryValidatorAccountRequest{})
|
|
|
|
require.Equal(t, &evmtypes.QueryValidatorAccountResponse{AccountAddress: validator.String()}, res)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2022-10-11 18:43:32 +00:00
|
|
|
|
|
|
|
// Code
|
|
|
|
func RegisterCode(queryClient *mocks.EVMQueryClient, addr common.Address, code []byte) {
|
|
|
|
queryClient.On("Code", rpc.ContextWithHeight(1), &evmtypes.QueryCodeRequest{Address: addr.String()}).
|
|
|
|
Return(&evmtypes.QueryCodeResponse{Code: code}, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterCodeError(queryClient *mocks.EVMQueryClient, addr common.Address) {
|
|
|
|
queryClient.On("Code", rpc.ContextWithHeight(1), &evmtypes.QueryCodeRequest{Address: addr.String()}).
|
2022-11-14 19:40:14 +00:00
|
|
|
Return(nil, errortypes.ErrInvalidRequest)
|
2022-10-11 18:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Storage
|
|
|
|
func RegisterStorageAt(queryClient *mocks.EVMQueryClient, addr common.Address, key string, storage string) {
|
|
|
|
queryClient.On("Storage", rpc.ContextWithHeight(1), &evmtypes.QueryStorageRequest{Address: addr.String(), Key: key}).
|
|
|
|
Return(&evmtypes.QueryStorageResponse{Value: storage}, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterStorageAtError(queryClient *mocks.EVMQueryClient, addr common.Address, key string) {
|
|
|
|
queryClient.On("Storage", rpc.ContextWithHeight(1), &evmtypes.QueryStorageRequest{Address: addr.String(), Key: key}).
|
2022-11-14 19:40:14 +00:00
|
|
|
Return(nil, errortypes.ErrInvalidRequest)
|
2022-10-11 18:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterAccount(queryClient *mocks.EVMQueryClient, addr common.Address, height int64) {
|
|
|
|
queryClient.On("Account", rpc.ContextWithHeight(height), &evmtypes.QueryAccountRequest{Address: addr.String()}).
|
|
|
|
Return(&evmtypes.QueryAccountResponse{
|
|
|
|
Balance: "0",
|
|
|
|
CodeHash: "",
|
|
|
|
Nonce: 0,
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Balance
|
|
|
|
func RegisterBalance(queryClient *mocks.EVMQueryClient, addr common.Address, height int64) {
|
|
|
|
queryClient.On("Balance", rpc.ContextWithHeight(height), &evmtypes.QueryBalanceRequest{Address: addr.String()}).
|
|
|
|
Return(&evmtypes.QueryBalanceResponse{Balance: "1"}, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterBalanceInvalid(queryClient *mocks.EVMQueryClient, addr common.Address, height int64) {
|
|
|
|
queryClient.On("Balance", rpc.ContextWithHeight(height), &evmtypes.QueryBalanceRequest{Address: addr.String()}).
|
|
|
|
Return(&evmtypes.QueryBalanceResponse{Balance: "invalid"}, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterBalanceNegative(queryClient *mocks.EVMQueryClient, addr common.Address, height int64) {
|
|
|
|
queryClient.On("Balance", rpc.ContextWithHeight(height), &evmtypes.QueryBalanceRequest{Address: addr.String()}).
|
|
|
|
Return(&evmtypes.QueryBalanceResponse{Balance: "-1"}, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterBalanceError(queryClient *mocks.EVMQueryClient, addr common.Address, height int64) {
|
|
|
|
queryClient.On("Balance", rpc.ContextWithHeight(height), &evmtypes.QueryBalanceRequest{Address: addr.String()}).
|
2022-11-14 19:40:14 +00:00
|
|
|
Return(nil, errortypes.ErrInvalidRequest)
|
2022-10-11 18:43:32 +00:00
|
|
|
}
|