2021-05-17 10:13:08 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
2021-07-23 14:24:36 +00:00
|
|
|
_ "embed"
|
|
|
|
"encoding/json"
|
2021-05-17 10:13:08 +00:00
|
|
|
"fmt"
|
2021-07-23 14:24:36 +00:00
|
|
|
"math/big"
|
2021-05-17 10:13:08 +00:00
|
|
|
|
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
|
2021-07-23 14:24:36 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
2021-06-08 17:10:29 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2021-05-17 10:13:08 +00:00
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
2021-07-23 14:24:36 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2021-05-17 10:13:08 +00:00
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
2021-07-23 14:24:36 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2021-05-17 10:13:08 +00:00
|
|
|
ethcrypto "github.com/ethereum/go-ethereum/crypto"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
2021-06-22 10:49:18 +00:00
|
|
|
ethermint "github.com/tharsis/ethermint/types"
|
|
|
|
"github.com/tharsis/ethermint/x/evm/types"
|
2021-05-17 10:13:08 +00:00
|
|
|
|
|
|
|
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
|
|
|
|
)
|
|
|
|
|
2021-06-29 15:08:07 +00:00
|
|
|
//Not valid Ethereum address
|
|
|
|
const invalidAddress = "0x0000"
|
|
|
|
|
2021-07-23 14:24:36 +00:00
|
|
|
var (
|
|
|
|
//go:embed ERC20Contract.json
|
|
|
|
compiledContractJSON []byte
|
|
|
|
contractBin []byte
|
|
|
|
contractABI abi.ABI
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
var tmp struct {
|
|
|
|
Abi string
|
|
|
|
Bin string
|
|
|
|
}
|
|
|
|
err := json.Unmarshal(compiledContractJSON, &tmp)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
contractBin = common.FromHex(tmp.Bin)
|
|
|
|
err = json.Unmarshal([]byte(tmp.Abi), &contractABI)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-17 10:13:08 +00:00
|
|
|
func (suite *KeeperTestSuite) TestQueryAccount() {
|
|
|
|
var (
|
|
|
|
req *types.QueryAccountRequest
|
|
|
|
expAccount *types.QueryAccountResponse
|
|
|
|
)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
msg string
|
|
|
|
malleate func()
|
|
|
|
expPass bool
|
|
|
|
}{
|
2021-06-30 15:28:38 +00:00
|
|
|
{
|
|
|
|
"invalid address",
|
2021-05-17 10:13:08 +00:00
|
|
|
func() {
|
|
|
|
expAccount = &types.QueryAccountResponse{
|
2021-07-02 09:29:47 +00:00
|
|
|
Balance: "0",
|
2021-06-08 17:10:29 +00:00
|
|
|
CodeHash: common.BytesToHash(ethcrypto.Keccak256(nil)).Hex(),
|
2021-05-17 10:13:08 +00:00
|
|
|
Nonce: 0,
|
|
|
|
}
|
|
|
|
req = &types.QueryAccountRequest{
|
2021-06-29 15:08:07 +00:00
|
|
|
Address: invalidAddress,
|
2021-05-17 10:13:08 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"success",
|
|
|
|
func() {
|
2021-06-29 17:02:21 +00:00
|
|
|
amt := sdk.Coins{ethermint.NewPhotonCoinInt64(100)}
|
2021-06-30 15:28:38 +00:00
|
|
|
err := suite.app.BankKeeper.MintCoins(suite.ctx, types.ModuleName, amt)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
err = suite.app.BankKeeper.SendCoinsFromModuleToAccount(suite.ctx, types.ModuleName, suite.address.Bytes(), amt)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
2021-05-17 10:13:08 +00:00
|
|
|
expAccount = &types.QueryAccountResponse{
|
2021-07-02 09:29:47 +00:00
|
|
|
Balance: "100",
|
2021-06-08 17:10:29 +00:00
|
|
|
CodeHash: common.BytesToHash(ethcrypto.Keccak256(nil)).Hex(),
|
2021-05-17 10:13:08 +00:00
|
|
|
Nonce: 0,
|
|
|
|
}
|
|
|
|
req = &types.QueryAccountRequest{
|
|
|
|
Address: suite.address.String(),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
|
|
|
|
suite.SetupTest() // reset
|
|
|
|
|
|
|
|
tc.malleate()
|
|
|
|
ctx := sdk.WrapSDKContext(suite.ctx)
|
|
|
|
res, err := suite.queryClient.Account(ctx, req)
|
|
|
|
|
|
|
|
if tc.expPass {
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().NotNil(res)
|
|
|
|
|
|
|
|
suite.Require().Equal(expAccount, res)
|
|
|
|
} else {
|
|
|
|
suite.Require().Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) TestQueryCosmosAccount() {
|
|
|
|
var (
|
|
|
|
req *types.QueryCosmosAccountRequest
|
|
|
|
expAccount *types.QueryCosmosAccountResponse
|
|
|
|
)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
msg string
|
|
|
|
malleate func()
|
|
|
|
expPass bool
|
|
|
|
}{
|
2021-06-29 15:08:07 +00:00
|
|
|
{"invalid address",
|
2021-05-17 10:13:08 +00:00
|
|
|
func() {
|
|
|
|
expAccount = &types.QueryCosmosAccountResponse{
|
|
|
|
CosmosAddress: sdk.AccAddress(ethcmn.Address{}.Bytes()).String(),
|
|
|
|
}
|
|
|
|
req = &types.QueryCosmosAccountRequest{
|
2021-06-29 15:08:07 +00:00
|
|
|
Address: invalidAddress,
|
2021-05-17 10:13:08 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"success",
|
|
|
|
func() {
|
|
|
|
expAccount = &types.QueryCosmosAccountResponse{
|
|
|
|
CosmosAddress: sdk.AccAddress(suite.address.Bytes()).String(),
|
|
|
|
Sequence: 0,
|
|
|
|
AccountNumber: 0,
|
|
|
|
}
|
|
|
|
req = &types.QueryCosmosAccountRequest{
|
|
|
|
Address: suite.address.String(),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"success with seq and account number",
|
|
|
|
func() {
|
|
|
|
acc := suite.app.AccountKeeper.GetAccount(suite.ctx, suite.address.Bytes())
|
|
|
|
suite.Require().NoError(acc.SetSequence(10))
|
|
|
|
suite.Require().NoError(acc.SetAccountNumber(1))
|
|
|
|
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
|
|
|
|
|
|
|
|
expAccount = &types.QueryCosmosAccountResponse{
|
|
|
|
CosmosAddress: sdk.AccAddress(suite.address.Bytes()).String(),
|
|
|
|
Sequence: 10,
|
|
|
|
AccountNumber: 1,
|
|
|
|
}
|
|
|
|
req = &types.QueryCosmosAccountRequest{
|
|
|
|
Address: suite.address.String(),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
|
|
|
|
suite.SetupTest() // reset
|
|
|
|
|
|
|
|
tc.malleate()
|
|
|
|
ctx := sdk.WrapSDKContext(suite.ctx)
|
|
|
|
res, err := suite.queryClient.CosmosAccount(ctx, req)
|
|
|
|
|
|
|
|
if tc.expPass {
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().NotNil(res)
|
|
|
|
|
|
|
|
suite.Require().Equal(expAccount, res)
|
|
|
|
} else {
|
|
|
|
suite.Require().Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) TestQueryBalance() {
|
|
|
|
var (
|
|
|
|
req *types.QueryBalanceRequest
|
2021-07-02 09:29:47 +00:00
|
|
|
expBalance string
|
2021-05-17 10:13:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
msg string
|
|
|
|
malleate func()
|
|
|
|
expPass bool
|
|
|
|
}{
|
2021-06-29 15:08:07 +00:00
|
|
|
{"invalid address",
|
2021-05-17 10:13:08 +00:00
|
|
|
func() {
|
2021-07-02 09:29:47 +00:00
|
|
|
expBalance = "0"
|
2021-05-17 10:13:08 +00:00
|
|
|
req = &types.QueryBalanceRequest{
|
2021-06-29 15:08:07 +00:00
|
|
|
Address: invalidAddress,
|
2021-05-17 10:13:08 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"success",
|
|
|
|
func() {
|
2021-06-29 17:02:21 +00:00
|
|
|
amt := sdk.Coins{ethermint.NewPhotonCoinInt64(100)}
|
2021-06-30 15:28:38 +00:00
|
|
|
err := suite.app.BankKeeper.MintCoins(suite.ctx, types.ModuleName, amt)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
err = suite.app.BankKeeper.SendCoinsFromModuleToAccount(suite.ctx, types.ModuleName, suite.address.Bytes(), amt)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
2021-07-02 09:29:47 +00:00
|
|
|
expBalance = "100"
|
2021-05-17 10:13:08 +00:00
|
|
|
req = &types.QueryBalanceRequest{
|
|
|
|
Address: suite.address.String(),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
|
|
|
|
suite.SetupTest() // reset
|
|
|
|
|
|
|
|
tc.malleate()
|
|
|
|
ctx := sdk.WrapSDKContext(suite.ctx)
|
|
|
|
res, err := suite.queryClient.Balance(ctx, req)
|
|
|
|
|
|
|
|
if tc.expPass {
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().NotNil(res)
|
|
|
|
|
|
|
|
suite.Require().Equal(expBalance, res.Balance)
|
|
|
|
} else {
|
|
|
|
suite.Require().Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) TestQueryStorage() {
|
|
|
|
var (
|
|
|
|
req *types.QueryStorageRequest
|
|
|
|
expValue string
|
|
|
|
)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
msg string
|
|
|
|
malleate func()
|
|
|
|
expPass bool
|
|
|
|
}{
|
2021-06-29 15:08:07 +00:00
|
|
|
{"invalid address",
|
2021-05-17 10:13:08 +00:00
|
|
|
func() {
|
|
|
|
req = &types.QueryStorageRequest{
|
2021-06-29 15:08:07 +00:00
|
|
|
Address: invalidAddress,
|
2021-05-17 10:13:08 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"success",
|
|
|
|
func() {
|
|
|
|
key := ethcmn.BytesToHash([]byte("key"))
|
|
|
|
value := ethcmn.BytesToHash([]byte("value"))
|
|
|
|
expValue = value.String()
|
2021-06-08 17:10:29 +00:00
|
|
|
suite.app.EvmKeeper.SetState(suite.address, key, value)
|
2021-05-17 10:13:08 +00:00
|
|
|
req = &types.QueryStorageRequest{
|
|
|
|
Address: suite.address.String(),
|
|
|
|
Key: key.String(),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
|
|
|
|
suite.SetupTest() // reset
|
|
|
|
|
|
|
|
tc.malleate()
|
|
|
|
ctx := sdk.WrapSDKContext(suite.ctx)
|
|
|
|
res, err := suite.queryClient.Storage(ctx, req)
|
|
|
|
|
|
|
|
if tc.expPass {
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().NotNil(res)
|
|
|
|
|
|
|
|
suite.Require().Equal(expValue, res.Value)
|
|
|
|
} else {
|
|
|
|
suite.Require().Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) TestQueryCode() {
|
|
|
|
var (
|
|
|
|
req *types.QueryCodeRequest
|
|
|
|
expCode []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
msg string
|
|
|
|
malleate func()
|
|
|
|
expPass bool
|
|
|
|
}{
|
2021-06-29 15:08:07 +00:00
|
|
|
{"invalid address",
|
2021-05-17 10:13:08 +00:00
|
|
|
func() {
|
|
|
|
req = &types.QueryCodeRequest{
|
2021-06-29 15:08:07 +00:00
|
|
|
Address: invalidAddress,
|
2021-05-17 10:13:08 +00:00
|
|
|
}
|
|
|
|
exp := &types.QueryCodeResponse{}
|
|
|
|
expCode = exp.Code
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"success",
|
|
|
|
func() {
|
|
|
|
expCode = []byte("code")
|
2021-06-08 17:10:29 +00:00
|
|
|
suite.app.EvmKeeper.SetCode(suite.address, expCode)
|
2021-05-17 10:13:08 +00:00
|
|
|
|
|
|
|
req = &types.QueryCodeRequest{
|
|
|
|
Address: suite.address.String(),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
|
|
|
|
suite.SetupTest() // reset
|
|
|
|
|
|
|
|
tc.malleate()
|
|
|
|
ctx := sdk.WrapSDKContext(suite.ctx)
|
|
|
|
res, err := suite.queryClient.Code(ctx, req)
|
|
|
|
|
|
|
|
if tc.expPass {
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().NotNil(res)
|
|
|
|
|
|
|
|
suite.Require().Equal(expCode, res.Code)
|
|
|
|
} else {
|
|
|
|
suite.Require().Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) TestQueryTxLogs() {
|
|
|
|
var (
|
|
|
|
req *types.QueryTxLogsRequest
|
|
|
|
expLogs []*types.Log
|
|
|
|
)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
msg string
|
|
|
|
malleate func()
|
|
|
|
expPass bool
|
|
|
|
}{
|
|
|
|
{"empty hash",
|
|
|
|
func() {
|
|
|
|
req = &types.QueryTxLogsRequest{
|
|
|
|
Hash: ethcmn.Hash{}.String(),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{"logs not found",
|
|
|
|
func() {
|
|
|
|
hash := ethcmn.BytesToHash([]byte("hash"))
|
|
|
|
req = &types.QueryTxLogsRequest{
|
|
|
|
Hash: hash.String(),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"success",
|
|
|
|
func() {
|
|
|
|
hash := ethcmn.BytesToHash([]byte("tx_hash"))
|
|
|
|
|
|
|
|
expLogs = []*types.Log{
|
|
|
|
{
|
|
|
|
Address: suite.address.String(),
|
|
|
|
Topics: []string{ethcmn.BytesToHash([]byte("topic")).String()},
|
|
|
|
Data: []byte("data"),
|
|
|
|
BlockNumber: 1,
|
|
|
|
TxHash: hash.String(),
|
|
|
|
TxIndex: 1,
|
|
|
|
BlockHash: ethcmn.BytesToHash([]byte("block_hash")).String(),
|
|
|
|
Index: 0,
|
|
|
|
Removed: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-06-08 17:10:29 +00:00
|
|
|
suite.app.EvmKeeper.SetLogs(hash, types.LogsToEthereum(expLogs))
|
2021-05-17 10:13:08 +00:00
|
|
|
|
|
|
|
req = &types.QueryTxLogsRequest{
|
|
|
|
Hash: hash.String(),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
|
|
|
|
suite.SetupTest() // reset
|
|
|
|
|
|
|
|
tc.malleate()
|
|
|
|
ctx := sdk.WrapSDKContext(suite.ctx)
|
|
|
|
res, err := suite.queryClient.TxLogs(ctx, req)
|
|
|
|
|
|
|
|
if tc.expPass {
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().NotNil(res)
|
|
|
|
|
|
|
|
suite.Require().Equal(expLogs, res.Logs)
|
|
|
|
} else {
|
|
|
|
suite.Require().Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) TestQueryBlockLogs() {
|
|
|
|
var (
|
|
|
|
req *types.QueryBlockLogsRequest
|
|
|
|
expLogs []types.TransactionLogs
|
|
|
|
)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
msg string
|
|
|
|
malleate func()
|
|
|
|
expPass bool
|
|
|
|
}{
|
|
|
|
{"empty hash",
|
|
|
|
func() {
|
|
|
|
req = &types.QueryBlockLogsRequest{
|
|
|
|
Hash: ethcmn.Hash{}.String(),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{"logs not found",
|
|
|
|
func() {
|
|
|
|
hash := ethcmn.BytesToHash([]byte("hash"))
|
|
|
|
req = &types.QueryBlockLogsRequest{
|
|
|
|
Hash: hash.String(),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"success",
|
|
|
|
func() {
|
|
|
|
|
|
|
|
hash := ethcmn.BytesToHash([]byte("block_hash"))
|
|
|
|
expLogs = []types.TransactionLogs{
|
|
|
|
{
|
|
|
|
Hash: ethcmn.BytesToHash([]byte("tx_hash_0")).String(),
|
|
|
|
Logs: []*types.Log{
|
|
|
|
{
|
|
|
|
Address: suite.address.String(),
|
|
|
|
Topics: []string{ethcmn.BytesToHash([]byte("topic")).String()},
|
|
|
|
Data: []byte("data"),
|
|
|
|
BlockNumber: 1,
|
|
|
|
TxHash: ethcmn.BytesToHash([]byte("tx_hash_0")).String(),
|
|
|
|
TxIndex: 1,
|
|
|
|
BlockHash: ethcmn.BytesToHash([]byte("block_hash")).String(),
|
|
|
|
Index: 0,
|
|
|
|
Removed: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Hash: ethcmn.BytesToHash([]byte("tx_hash_1")).String(),
|
|
|
|
Logs: []*types.Log{
|
|
|
|
{
|
|
|
|
Address: suite.address.String(),
|
|
|
|
Topics: []string{ethcmn.BytesToHash([]byte("topic")).String()},
|
|
|
|
Data: []byte("data"),
|
|
|
|
BlockNumber: 1,
|
|
|
|
TxHash: ethcmn.BytesToHash([]byte("tx_hash_1")).String(),
|
|
|
|
TxIndex: 1,
|
|
|
|
BlockHash: ethcmn.BytesToHash([]byte("block_hash")).String(),
|
|
|
|
Index: 0,
|
|
|
|
Removed: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Address: suite.address.String(),
|
|
|
|
Topics: []string{ethcmn.BytesToHash([]byte("topic_1")).String()},
|
|
|
|
Data: []byte("data_1"),
|
|
|
|
BlockNumber: 1,
|
|
|
|
TxHash: ethcmn.BytesToHash([]byte("tx_hash_1")).String(),
|
|
|
|
TxIndex: 1,
|
|
|
|
BlockHash: ethcmn.BytesToHash([]byte("block_hash")).String(),
|
|
|
|
Index: 0,
|
|
|
|
Removed: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-06-08 17:10:29 +00:00
|
|
|
suite.app.EvmKeeper.SetLogs(ethcmn.BytesToHash([]byte("tx_hash_0")), types.LogsToEthereum(expLogs[0].Logs))
|
|
|
|
suite.app.EvmKeeper.SetLogs(ethcmn.BytesToHash([]byte("tx_hash_1")), types.LogsToEthereum(expLogs[1].Logs))
|
2021-05-17 10:13:08 +00:00
|
|
|
|
|
|
|
req = &types.QueryBlockLogsRequest{
|
|
|
|
Hash: hash.String(),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
|
|
|
|
suite.SetupTest() // reset
|
|
|
|
|
|
|
|
tc.malleate()
|
|
|
|
ctx := sdk.WrapSDKContext(suite.ctx)
|
|
|
|
res, err := suite.queryClient.BlockLogs(ctx, req)
|
|
|
|
|
|
|
|
if tc.expPass {
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().NotNil(res)
|
|
|
|
|
|
|
|
suite.Require().Equal(expLogs, res.TxLogs)
|
|
|
|
} else {
|
|
|
|
suite.Require().Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) TestQueryBlockBloom() {
|
|
|
|
var (
|
|
|
|
req *types.QueryBlockBloomRequest
|
|
|
|
expBloom []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
msg string
|
|
|
|
malleate func()
|
|
|
|
expPass bool
|
|
|
|
}{
|
2021-07-20 15:16:02 +00:00
|
|
|
{"bad height",
|
|
|
|
func() {
|
|
|
|
req = &types.QueryBlockBloomRequest{Height: -2}
|
|
|
|
},
|
2021-05-17 10:13:08 +00:00
|
|
|
false,
|
|
|
|
},
|
2021-07-12 10:25:15 +00:00
|
|
|
{
|
|
|
|
"bloom from transient store",
|
|
|
|
func() {
|
2021-07-20 15:16:02 +00:00
|
|
|
req = &types.QueryBlockBloomRequest{Height: 1}
|
2021-07-12 10:25:15 +00:00
|
|
|
bloom := ethtypes.BytesToBloom([]byte("bloom"))
|
|
|
|
expBloom = bloom.Bytes()
|
|
|
|
suite.app.EvmKeeper.WithContext(suite.ctx.WithBlockHeight(1))
|
|
|
|
suite.app.EvmKeeper.SetBlockBloomTransient(bloom.Big())
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
2021-05-17 10:13:08 +00:00
|
|
|
{"bloom not found for height",
|
|
|
|
func() {
|
2021-07-20 15:16:02 +00:00
|
|
|
req = &types.QueryBlockBloomRequest{Height: 100}
|
2021-05-17 10:13:08 +00:00
|
|
|
bloom := ethtypes.BytesToBloom([]byte("bloom"))
|
|
|
|
expBloom = bloom.Bytes()
|
2021-07-12 10:25:15 +00:00
|
|
|
suite.ctx = suite.ctx.WithBlockHeight(100)
|
2021-05-17 10:13:08 +00:00
|
|
|
suite.app.EvmKeeper.SetBlockBloom(suite.ctx, 2, bloom)
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"success",
|
|
|
|
func() {
|
2021-07-20 15:16:02 +00:00
|
|
|
req = &types.QueryBlockBloomRequest{Height: 3}
|
2021-05-17 10:13:08 +00:00
|
|
|
bloom := ethtypes.BytesToBloom([]byte("bloom"))
|
|
|
|
expBloom = bloom.Bytes()
|
2021-07-20 15:16:02 +00:00
|
|
|
suite.ctx = suite.ctx.WithBlockHeight(3)
|
|
|
|
suite.app.EvmKeeper.SetBlockBloom(suite.ctx, 3, bloom)
|
2021-05-17 10:13:08 +00:00
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
|
|
|
|
suite.SetupTest() // reset
|
|
|
|
|
|
|
|
tc.malleate()
|
|
|
|
ctx := sdk.WrapSDKContext(suite.ctx)
|
|
|
|
ctx = metadata.AppendToOutgoingContext(ctx, grpctypes.GRPCBlockHeightHeader, fmt.Sprintf("%d", suite.ctx.BlockHeight()))
|
|
|
|
res, err := suite.queryClient.BlockBloom(ctx, req)
|
|
|
|
|
|
|
|
if tc.expPass {
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().NotNil(res)
|
|
|
|
|
|
|
|
suite.Require().Equal(expBloom, res.Bloom)
|
|
|
|
} else {
|
|
|
|
suite.Require().Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) TestQueryParams() {
|
|
|
|
ctx := sdk.WrapSDKContext(suite.ctx)
|
|
|
|
expParams := types.DefaultParams()
|
|
|
|
|
|
|
|
res, err := suite.queryClient.Params(ctx, &types.QueryParamsRequest{})
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Equal(expParams, res.Params)
|
|
|
|
}
|
2021-06-22 10:14:40 +00:00
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) TestQueryValidatorAccount() {
|
|
|
|
var (
|
|
|
|
req *types.QueryValidatorAccountRequest
|
|
|
|
expAccount *types.QueryValidatorAccountResponse
|
|
|
|
)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
msg string
|
|
|
|
malleate func()
|
|
|
|
expPass bool
|
|
|
|
}{
|
2021-06-29 15:08:07 +00:00
|
|
|
{"invalid address",
|
2021-06-22 10:14:40 +00:00
|
|
|
func() {
|
|
|
|
expAccount = &types.QueryValidatorAccountResponse{
|
|
|
|
AccountAddress: sdk.AccAddress(ethcmn.Address{}.Bytes()).String(),
|
|
|
|
}
|
|
|
|
req = &types.QueryValidatorAccountRequest{
|
|
|
|
ConsAddress: "",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"success",
|
|
|
|
func() {
|
|
|
|
expAccount = &types.QueryValidatorAccountResponse{
|
|
|
|
AccountAddress: sdk.AccAddress(suite.address.Bytes()).String(),
|
2021-06-22 10:49:18 +00:00
|
|
|
Sequence: 0,
|
|
|
|
AccountNumber: 0,
|
2021-06-22 10:14:40 +00:00
|
|
|
}
|
|
|
|
req = &types.QueryValidatorAccountRequest{
|
|
|
|
ConsAddress: suite.consAddress.String(),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"success with seq and account number",
|
|
|
|
func() {
|
|
|
|
acc := suite.app.AccountKeeper.GetAccount(suite.ctx, suite.address.Bytes())
|
|
|
|
suite.Require().NoError(acc.SetSequence(10))
|
|
|
|
suite.Require().NoError(acc.SetAccountNumber(1))
|
|
|
|
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
|
|
|
|
|
|
|
|
expAccount = &types.QueryValidatorAccountResponse{
|
|
|
|
AccountAddress: sdk.AccAddress(suite.address.Bytes()).String(),
|
2021-06-22 10:49:18 +00:00
|
|
|
Sequence: 10,
|
|
|
|
AccountNumber: 1,
|
2021-06-22 10:14:40 +00:00
|
|
|
}
|
|
|
|
req = &types.QueryValidatorAccountRequest{
|
|
|
|
ConsAddress: suite.consAddress.String(),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
|
|
|
|
suite.SetupTest() // reset
|
|
|
|
|
|
|
|
tc.malleate()
|
|
|
|
ctx := sdk.WrapSDKContext(suite.ctx)
|
|
|
|
res, err := suite.queryClient.ValidatorAccount(ctx, req)
|
|
|
|
|
|
|
|
if tc.expPass {
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().NotNil(res)
|
|
|
|
|
|
|
|
suite.Require().Equal(expAccount, res)
|
|
|
|
} else {
|
|
|
|
suite.Require().Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-06-22 10:49:18 +00:00
|
|
|
}
|
2021-07-23 14:24:36 +00:00
|
|
|
|
|
|
|
// DeployTestContract deploy a test erc20 contract and returns the contract address
|
|
|
|
func (suite *KeeperTestSuite) deployTestContract(owner common.Address, supply *big.Int) common.Address {
|
|
|
|
ctx := sdk.WrapSDKContext(suite.ctx)
|
|
|
|
chainID := suite.app.EvmKeeper.ChainID()
|
|
|
|
|
|
|
|
ctorArgs, err := contractABI.Pack("", owner, supply)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
data := append(contractBin, ctorArgs...)
|
|
|
|
args, err := json.Marshal(&types.CallArgs{
|
|
|
|
From: &suite.address,
|
|
|
|
Data: (*hexutil.Bytes)(&data),
|
|
|
|
})
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
res, err := suite.queryClient.EstimateGas(ctx, &types.EthCallRequest{
|
|
|
|
Args: args,
|
2021-08-18 14:11:51 +00:00
|
|
|
GasCap: 25_000_000,
|
2021-07-23 14:24:36 +00:00
|
|
|
})
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
nonce := suite.app.EvmKeeper.GetNonce(suite.address)
|
|
|
|
erc20DeployTx := types.NewTxContract(
|
|
|
|
chainID,
|
|
|
|
nonce,
|
|
|
|
nil, // amount
|
|
|
|
res.Gas, // gasLimit
|
|
|
|
nil, // gasPrice
|
|
|
|
data, // input
|
|
|
|
nil, // accesses
|
|
|
|
)
|
|
|
|
erc20DeployTx.From = suite.address.Hex()
|
|
|
|
err = erc20DeployTx.Sign(ethtypes.LatestSignerForChainID(chainID), suite.signer)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
rsp, err := suite.app.EvmKeeper.EthereumTx(ctx, erc20DeployTx)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Empty(rsp.VmError)
|
|
|
|
return crypto.CreateAddress(suite.address, nonce)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) TestEstimateGas() {
|
|
|
|
ctx := sdk.WrapSDKContext(suite.ctx)
|
|
|
|
gasHelper := hexutil.Uint64(20000)
|
|
|
|
|
|
|
|
var (
|
|
|
|
args types.CallArgs
|
|
|
|
gasCap uint64
|
|
|
|
)
|
|
|
|
testCases := []struct {
|
|
|
|
msg string
|
|
|
|
malleate func()
|
|
|
|
expPass bool
|
|
|
|
expGas uint64
|
|
|
|
}{
|
|
|
|
// should success, because transfer value is zero
|
|
|
|
{"default args", func() {
|
|
|
|
args = types.CallArgs{To: &common.Address{}}
|
|
|
|
}, true, 21000},
|
|
|
|
// should fail, because the default From address(zero address) don't have fund
|
|
|
|
{"not enough balance", func() {
|
|
|
|
args = types.CallArgs{To: &common.Address{}, Value: (*hexutil.Big)(big.NewInt(100))}
|
|
|
|
}, false, 0},
|
|
|
|
// should success, enough balance now
|
|
|
|
{"enough balance", func() {
|
|
|
|
args = types.CallArgs{To: &common.Address{}, From: &suite.address, Value: (*hexutil.Big)(big.NewInt(100))}
|
|
|
|
}, false, 0},
|
|
|
|
// should success, because gas limit lower than 21000 is ignored
|
|
|
|
{"gas exceed allowance", func() {
|
|
|
|
args = types.CallArgs{To: &common.Address{}, Gas: &gasHelper}
|
|
|
|
}, true, 21000},
|
|
|
|
// should fail, invalid gas cap
|
|
|
|
{"gas exceed global allowance", func() {
|
|
|
|
args = types.CallArgs{To: &common.Address{}}
|
|
|
|
gasCap = 20000
|
|
|
|
}, false, 0},
|
|
|
|
// estimate gas of an erc20 contract deployment, the exact gas number is checked with geth
|
|
|
|
{"contract deployment", func() {
|
|
|
|
ctorArgs, err := contractABI.Pack("", &suite.address, sdk.NewIntWithDecimal(1000, 18).BigInt())
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
data := append(contractBin, ctorArgs...)
|
|
|
|
args = types.CallArgs{
|
|
|
|
From: &suite.address,
|
|
|
|
Data: (*hexutil.Bytes)(&data),
|
|
|
|
}
|
|
|
|
}, true, 1144643},
|
|
|
|
// estimate gas of an erc20 transfer, the exact gas number is checked with geth
|
|
|
|
{"erc20 transfer", func() {
|
|
|
|
contractAddr := suite.deployTestContract(suite.address, sdk.NewIntWithDecimal(1000, 18).BigInt())
|
|
|
|
suite.Commit()
|
|
|
|
transferData, err := contractABI.Pack("transfer", common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), big.NewInt(1000))
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
args = types.CallArgs{To: &contractAddr, From: &suite.address, Data: (*hexutil.Bytes)(&transferData)}
|
|
|
|
}, true, 51880},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
|
|
|
|
suite.SetupTest()
|
2021-08-18 14:11:51 +00:00
|
|
|
gasCap = 25_000_000
|
2021-07-23 14:24:36 +00:00
|
|
|
tc.malleate()
|
|
|
|
|
|
|
|
args, err := json.Marshal(&args)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
req := types.EthCallRequest{
|
|
|
|
Args: args,
|
|
|
|
GasCap: gasCap,
|
|
|
|
}
|
|
|
|
|
|
|
|
rsp, err := suite.queryClient.EstimateGas(ctx, &req)
|
|
|
|
if tc.expPass {
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Equal(tc.expGas, rsp.Gas)
|
|
|
|
} else {
|
|
|
|
suite.Require().Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|