2022-07-23 09:16:23 +00:00
|
|
|
package backend
|
|
|
|
|
2022-07-29 10:35:07 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
2022-09-05 14:07:56 +00:00
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
2022-07-29 10:35:07 +00:00
|
|
|
"github.com/tendermint/tendermint/abci/types"
|
|
|
|
tmrpctypes "github.com/tendermint/tendermint/rpc/core/types"
|
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
|
|
|
|
"github.com/evmos/ethermint/rpc/backend/mocks"
|
|
|
|
ethrpc "github.com/evmos/ethermint/rpc/types"
|
|
|
|
"github.com/evmos/ethermint/tests"
|
|
|
|
evmtypes "github.com/evmos/ethermint/x/evm/types"
|
|
|
|
)
|
2022-07-23 09:16:23 +00:00
|
|
|
|
|
|
|
func (suite *BackendTestSuite) TestBlockNumber() {
|
|
|
|
testCases := []struct {
|
2022-07-29 10:35:07 +00:00
|
|
|
name string
|
|
|
|
registerMock func()
|
2022-07-23 09:16:23 +00:00
|
|
|
expBlockNumber hexutil.Uint64
|
|
|
|
expPass bool
|
|
|
|
}{
|
|
|
|
{
|
2022-07-29 10:35:07 +00:00
|
|
|
"fail - invalid block header height",
|
|
|
|
func() {
|
|
|
|
height := int64(1)
|
|
|
|
var header metadata.MD
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
|
2022-07-29 10:35:07 +00:00
|
|
|
RegisterParamsInvalidHeight(queryClient, &header, int64(height))
|
|
|
|
},
|
|
|
|
0x0,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fail - invalid block header",
|
2022-07-23 09:16:23 +00:00
|
|
|
func() {
|
2022-07-29 10:35:07 +00:00
|
|
|
height := int64(1)
|
|
|
|
var header metadata.MD
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
|
2022-07-29 10:35:07 +00:00
|
|
|
RegisterParamsInvalidHeader(queryClient, &header, int64(height))
|
|
|
|
},
|
|
|
|
0x0,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass - app state header height 1",
|
|
|
|
func() {
|
|
|
|
height := int64(1)
|
|
|
|
var header metadata.MD
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
|
2022-07-29 10:35:07 +00:00
|
|
|
RegisterParams(queryClient, &header, int64(height))
|
2022-07-23 09:16:23 +00:00
|
|
|
},
|
|
|
|
0x1,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2022-07-29 10:35:07 +00:00
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
|
|
|
|
suite.SetupTest() // reset test and queries
|
|
|
|
tc.registerMock()
|
|
|
|
|
|
|
|
blockNumber, err := suite.backend.BlockNumber()
|
|
|
|
|
|
|
|
if tc.expPass {
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Equal(tc.expBlockNumber, blockNumber)
|
|
|
|
} else {
|
|
|
|
suite.Require().Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *BackendTestSuite) TestGetBlockByNumber() {
|
|
|
|
var (
|
|
|
|
blockRes *tmrpctypes.ResultBlockResults
|
|
|
|
resBlock *tmrpctypes.ResultBlock
|
|
|
|
)
|
|
|
|
msgEthereumTx, bz := suite.buildEthereumTx()
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
blockNumber ethrpc.BlockNumber
|
|
|
|
fullTx bool
|
|
|
|
baseFee *big.Int
|
|
|
|
validator sdk.AccAddress
|
|
|
|
tx *evmtypes.MsgEthereumTx
|
|
|
|
txBz []byte
|
|
|
|
registerMock func(ethrpc.BlockNumber, sdk.Int, sdk.AccAddress, []byte)
|
|
|
|
expNoop bool
|
|
|
|
expPass bool
|
|
|
|
}{
|
|
|
|
{
|
2022-08-08 13:01:42 +00:00
|
|
|
"pass - tendermint block not found",
|
2022-07-29 10:35:07 +00:00
|
|
|
ethrpc.BlockNumber(1),
|
|
|
|
true,
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
sdk.AccAddress(tests.GenerateAddress().Bytes()),
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
func(blockNum ethrpc.BlockNumber, _ sdk.Int, _ sdk.AccAddress, _ []byte) {
|
|
|
|
height := blockNum.Int64()
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterBlockError(client, height)
|
|
|
|
},
|
2022-08-08 13:01:42 +00:00
|
|
|
true,
|
|
|
|
true,
|
2022-07-29 10:35:07 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass - block not found (e.g. request block height that is greater than current one)",
|
|
|
|
ethrpc.BlockNumber(1),
|
|
|
|
true,
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
sdk.AccAddress(tests.GenerateAddress().Bytes()),
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
func(blockNum ethrpc.BlockNumber, baseFee sdk.Int, validator sdk.AccAddress, txBz []byte) {
|
|
|
|
height := blockNum.Int64()
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
resBlock, _ = RegisterBlockNotFound(client, height)
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass - block results error",
|
|
|
|
ethrpc.BlockNumber(1),
|
|
|
|
true,
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
sdk.AccAddress(tests.GenerateAddress().Bytes()),
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
func(blockNum ethrpc.BlockNumber, baseFee sdk.Int, validator sdk.AccAddress, txBz []byte) {
|
|
|
|
height := blockNum.Int64()
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
resBlock, _ = RegisterBlock(client, height, txBz)
|
|
|
|
RegisterBlockResultsError(client, blockNum.Int64())
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass - without tx",
|
|
|
|
ethrpc.BlockNumber(1),
|
|
|
|
true,
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
sdk.AccAddress(tests.GenerateAddress().Bytes()),
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
func(blockNum ethrpc.BlockNumber, baseFee sdk.Int, validator sdk.AccAddress, txBz []byte) {
|
|
|
|
height := blockNum.Int64()
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
resBlock, _ = RegisterBlock(client, height, txBz)
|
|
|
|
blockRes, _ = RegisterBlockResults(client, blockNum.Int64())
|
|
|
|
RegisterConsensusParams(client, height)
|
|
|
|
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
|
2022-07-29 10:35:07 +00:00
|
|
|
RegisterBaseFee(queryClient, baseFee)
|
|
|
|
RegisterValidatorAccount(queryClient, validator)
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass - with tx",
|
|
|
|
ethrpc.BlockNumber(1),
|
|
|
|
true,
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
sdk.AccAddress(tests.GenerateAddress().Bytes()),
|
|
|
|
msgEthereumTx,
|
|
|
|
bz,
|
|
|
|
func(blockNum ethrpc.BlockNumber, baseFee sdk.Int, validator sdk.AccAddress, txBz []byte) {
|
|
|
|
height := blockNum.Int64()
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
resBlock, _ = RegisterBlock(client, height, txBz)
|
|
|
|
blockRes, _ = RegisterBlockResults(client, blockNum.Int64())
|
|
|
|
RegisterConsensusParams(client, height)
|
|
|
|
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
|
2022-07-29 10:35:07 +00:00
|
|
|
RegisterBaseFee(queryClient, baseFee)
|
|
|
|
RegisterValidatorAccount(queryClient, validator)
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
|
|
|
|
suite.SetupTest() // reset test and queries
|
|
|
|
tc.registerMock(tc.blockNumber, sdk.NewIntFromBigInt(tc.baseFee), tc.validator, tc.txBz)
|
|
|
|
|
|
|
|
block, err := suite.backend.GetBlockByNumber(tc.blockNumber, tc.fullTx)
|
|
|
|
|
|
|
|
if tc.expPass {
|
|
|
|
if tc.expNoop {
|
|
|
|
suite.Require().Nil(block)
|
|
|
|
} else {
|
|
|
|
expBlock := suite.buildFormattedBlock(
|
|
|
|
blockRes,
|
|
|
|
resBlock,
|
|
|
|
tc.fullTx,
|
|
|
|
tc.tx,
|
|
|
|
tc.validator,
|
|
|
|
tc.baseFee,
|
|
|
|
)
|
|
|
|
suite.Require().Equal(expBlock, block)
|
|
|
|
}
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
suite.Require().Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *BackendTestSuite) TestGetBlockByHash() {
|
|
|
|
var (
|
|
|
|
blockRes *tmrpctypes.ResultBlockResults
|
|
|
|
resBlock *tmrpctypes.ResultBlock
|
|
|
|
)
|
|
|
|
msgEthereumTx, bz := suite.buildEthereumTx()
|
|
|
|
|
|
|
|
block := tmtypes.MakeBlock(1, []tmtypes.Tx{bz}, nil, nil)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
hash common.Hash
|
|
|
|
fullTx bool
|
|
|
|
baseFee *big.Int
|
|
|
|
validator sdk.AccAddress
|
|
|
|
tx *evmtypes.MsgEthereumTx
|
|
|
|
txBz []byte
|
|
|
|
registerMock func(common.Hash, sdk.Int, sdk.AccAddress, []byte)
|
|
|
|
expNoop bool
|
|
|
|
expPass bool
|
|
|
|
}{
|
2022-09-05 14:07:56 +00:00
|
|
|
{
|
|
|
|
"fail - tendermint failed to get block",
|
|
|
|
common.BytesToHash(block.Hash()),
|
|
|
|
true,
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
sdk.AccAddress(tests.GenerateAddress().Bytes()),
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
func(hash common.Hash, baseFee sdk.Int, validator sdk.AccAddress, txBz []byte) {
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterBlockByHashError(client, hash, txBz)
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"noop - tendermint blockres not found",
|
|
|
|
common.BytesToHash(block.Hash()),
|
|
|
|
true,
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
sdk.AccAddress(tests.GenerateAddress().Bytes()),
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
func(hash common.Hash, baseFee sdk.Int, validator sdk.AccAddress, txBz []byte) {
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterBlockByHashNotFound(client, hash, txBz)
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"noop - tendermint failed to fetch block result",
|
|
|
|
common.BytesToHash(block.Hash()),
|
|
|
|
true,
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
sdk.AccAddress(tests.GenerateAddress().Bytes()),
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
func(hash common.Hash, baseFee sdk.Int, validator sdk.AccAddress, txBz []byte) {
|
|
|
|
height := int64(1)
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
resBlock, _ = RegisterBlockByHash(client, hash, txBz)
|
|
|
|
|
|
|
|
RegisterBlockResultsError(client, height)
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
},
|
2022-07-29 10:35:07 +00:00
|
|
|
{
|
|
|
|
"pass - without tx",
|
|
|
|
common.BytesToHash(block.Hash()),
|
|
|
|
true,
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
sdk.AccAddress(tests.GenerateAddress().Bytes()),
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
func(hash common.Hash, baseFee sdk.Int, validator sdk.AccAddress, txBz []byte) {
|
|
|
|
height := int64(1)
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
resBlock, _ = RegisterBlockByHash(client, hash, txBz)
|
|
|
|
|
|
|
|
blockRes, _ = RegisterBlockResults(client, height)
|
|
|
|
RegisterConsensusParams(client, height)
|
|
|
|
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
|
2022-07-29 10:35:07 +00:00
|
|
|
RegisterBaseFee(queryClient, baseFee)
|
|
|
|
RegisterValidatorAccount(queryClient, validator)
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass - with tx",
|
|
|
|
common.BytesToHash(block.Hash()),
|
|
|
|
true,
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
sdk.AccAddress(tests.GenerateAddress().Bytes()),
|
|
|
|
msgEthereumTx,
|
|
|
|
bz,
|
|
|
|
func(hash common.Hash, baseFee sdk.Int, validator sdk.AccAddress, txBz []byte) {
|
|
|
|
height := int64(1)
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
resBlock, _ = RegisterBlockByHash(client, hash, txBz)
|
|
|
|
|
|
|
|
blockRes, _ = RegisterBlockResults(client, height)
|
|
|
|
RegisterConsensusParams(client, height)
|
|
|
|
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
|
2022-07-29 10:35:07 +00:00
|
|
|
RegisterBaseFee(queryClient, baseFee)
|
|
|
|
RegisterValidatorAccount(queryClient, validator)
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
|
|
|
|
suite.SetupTest() // reset test and queries
|
|
|
|
tc.registerMock(tc.hash, sdk.NewIntFromBigInt(tc.baseFee), tc.validator, tc.txBz)
|
|
|
|
|
|
|
|
block, err := suite.backend.GetBlockByHash(tc.hash, tc.fullTx)
|
|
|
|
|
|
|
|
if tc.expPass {
|
|
|
|
if tc.expNoop {
|
|
|
|
suite.Require().Nil(block)
|
|
|
|
} else {
|
|
|
|
expBlock := suite.buildFormattedBlock(
|
|
|
|
blockRes,
|
|
|
|
resBlock,
|
|
|
|
tc.fullTx,
|
|
|
|
tc.tx,
|
|
|
|
tc.validator,
|
|
|
|
tc.baseFee,
|
|
|
|
)
|
|
|
|
suite.Require().Equal(expBlock, block)
|
|
|
|
}
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
suite.Require().Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 14:07:56 +00:00
|
|
|
func (suite *BackendTestSuite) TestGetBlockTransactionCountByHash() {
|
|
|
|
_, bz := suite.buildEthereumTx()
|
|
|
|
block := tmtypes.MakeBlock(1, []tmtypes.Tx{bz}, nil, nil)
|
|
|
|
emptyBlock := tmtypes.MakeBlock(1, []tmtypes.Tx{}, nil, nil)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
hash common.Hash
|
|
|
|
registerMock func(common.Hash)
|
|
|
|
expCount hexutil.Uint
|
|
|
|
expPass bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"fail - block not found",
|
|
|
|
common.BytesToHash(emptyBlock.Hash()),
|
|
|
|
func(hash common.Hash) {
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterBlockByHashError(client, hash, nil)
|
|
|
|
},
|
|
|
|
hexutil.Uint(0),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fail - tendermint client failed to get block result",
|
|
|
|
common.BytesToHash(emptyBlock.Hash()),
|
|
|
|
func(hash common.Hash) {
|
|
|
|
height := int64(1)
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterBlockByHash(client, hash, nil)
|
|
|
|
RegisterBlockResultsError(client, height)
|
|
|
|
},
|
|
|
|
hexutil.Uint(0),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass - block without tx",
|
|
|
|
common.BytesToHash(emptyBlock.Hash()),
|
|
|
|
func(hash common.Hash) {
|
|
|
|
height := int64(1)
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterBlockByHash(client, hash, nil)
|
|
|
|
RegisterBlockResults(client, height)
|
|
|
|
},
|
|
|
|
hexutil.Uint(0),
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass - block with tx",
|
|
|
|
common.BytesToHash(block.Hash()),
|
|
|
|
func(hash common.Hash) {
|
|
|
|
height := int64(1)
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterBlockByHash(client, hash, bz)
|
|
|
|
RegisterBlockResults(client, height)
|
|
|
|
},
|
|
|
|
hexutil.Uint(1),
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
|
|
|
|
suite.SetupTest() // reset test and queries
|
|
|
|
|
|
|
|
tc.registerMock(tc.hash)
|
|
|
|
count := suite.backend.GetBlockTransactionCountByHash(tc.hash)
|
|
|
|
if tc.expPass {
|
|
|
|
suite.Require().Equal(tc.expCount, *count)
|
|
|
|
} else {
|
|
|
|
suite.Require().Nil(count)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *BackendTestSuite) TestGetBlockTransactionCountByNumber() {
|
|
|
|
_, bz := suite.buildEthereumTx()
|
|
|
|
block := tmtypes.MakeBlock(1, []tmtypes.Tx{bz}, nil, nil)
|
|
|
|
emptyBlock := tmtypes.MakeBlock(1, []tmtypes.Tx{}, nil, nil)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
blockNum ethrpc.BlockNumber
|
|
|
|
registerMock func(ethrpc.BlockNumber)
|
|
|
|
expCount hexutil.Uint
|
|
|
|
expPass bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"fail - block not found",
|
|
|
|
ethrpc.BlockNumber(emptyBlock.Height),
|
|
|
|
func(blockNum ethrpc.BlockNumber) {
|
|
|
|
height := blockNum.Int64()
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterBlockError(client, height)
|
|
|
|
},
|
|
|
|
hexutil.Uint(0),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fail - tendermint client failed to get block result",
|
|
|
|
ethrpc.BlockNumber(emptyBlock.Height),
|
|
|
|
func(blockNum ethrpc.BlockNumber) {
|
|
|
|
height := blockNum.Int64()
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterBlock(client, height, nil)
|
|
|
|
RegisterBlockResultsError(client, height)
|
|
|
|
},
|
|
|
|
hexutil.Uint(0),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass - block without tx",
|
|
|
|
ethrpc.BlockNumber(emptyBlock.Height),
|
|
|
|
func(blockNum ethrpc.BlockNumber) {
|
|
|
|
height := blockNum.Int64()
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterBlock(client, height, nil)
|
|
|
|
RegisterBlockResults(client, height)
|
|
|
|
},
|
|
|
|
hexutil.Uint(0),
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass - block with tx",
|
|
|
|
ethrpc.BlockNumber(block.Height),
|
|
|
|
func(blockNum ethrpc.BlockNumber) {
|
|
|
|
height := blockNum.Int64()
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterBlock(client, height, bz)
|
|
|
|
RegisterBlockResults(client, height)
|
|
|
|
},
|
|
|
|
hexutil.Uint(1),
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
|
|
|
|
suite.SetupTest() // reset test and queries
|
|
|
|
|
|
|
|
tc.registerMock(tc.blockNum)
|
|
|
|
count := suite.backend.GetBlockTransactionCountByNumber(tc.blockNum)
|
|
|
|
if tc.expPass {
|
|
|
|
suite.Require().Equal(tc.expCount, *count)
|
|
|
|
} else {
|
|
|
|
suite.Require().Nil(count)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *BackendTestSuite) TestTendermintBlockByNumber() {
|
2022-07-29 10:35:07 +00:00
|
|
|
var expResultBlock *tmrpctypes.ResultBlock
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
blockNumber ethrpc.BlockNumber
|
|
|
|
registerMock func(ethrpc.BlockNumber)
|
|
|
|
found bool
|
|
|
|
expPass bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"fail - client error",
|
|
|
|
ethrpc.BlockNumber(1),
|
|
|
|
func(blockNum ethrpc.BlockNumber) {
|
|
|
|
height := blockNum.Int64()
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterBlockError(client, height)
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"noop - block not found",
|
|
|
|
ethrpc.BlockNumber(1),
|
|
|
|
func(blockNum ethrpc.BlockNumber) {
|
|
|
|
height := blockNum.Int64()
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterBlockNotFound(client, height)
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fail - blockNum < 0 with app state height error",
|
|
|
|
ethrpc.BlockNumber(-1),
|
|
|
|
func(_ ethrpc.BlockNumber) {
|
|
|
|
appHeight := int64(1)
|
|
|
|
var header metadata.MD
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
|
2022-07-29 10:35:07 +00:00
|
|
|
RegisterParamsError(queryClient, &header, appHeight)
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass - blockNum < 0 with app state height >= 1",
|
|
|
|
ethrpc.BlockNumber(-1),
|
|
|
|
func(blockNum ethrpc.BlockNumber) {
|
|
|
|
appHeight := int64(1)
|
|
|
|
var header metadata.MD
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
|
2022-07-29 10:35:07 +00:00
|
|
|
RegisterParams(queryClient, &header, appHeight)
|
|
|
|
|
|
|
|
tmHeight := appHeight
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
expResultBlock, _ = RegisterBlock(client, tmHeight, nil)
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass - blockNum = 0 (defaults to blockNum = 1 due to a difference between tendermint heights and geth heights",
|
|
|
|
ethrpc.BlockNumber(0),
|
|
|
|
func(blockNum ethrpc.BlockNumber) {
|
|
|
|
height := blockNum.Int64()
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
expResultBlock, _ = RegisterBlock(client, height, nil)
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass - blockNum = 1",
|
|
|
|
ethrpc.BlockNumber(1),
|
|
|
|
func(blockNum ethrpc.BlockNumber) {
|
|
|
|
height := blockNum.Int64()
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
expResultBlock, _ = RegisterBlock(client, height, nil)
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
|
|
|
|
suite.SetupTest() // reset test and queries
|
|
|
|
|
|
|
|
tc.registerMock(tc.blockNumber)
|
2022-09-05 14:07:56 +00:00
|
|
|
resultBlock, err := suite.backend.TendermintBlockByNumber(tc.blockNumber)
|
2022-07-29 10:35:07 +00:00
|
|
|
|
|
|
|
if tc.expPass {
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
if !tc.found {
|
|
|
|
suite.Require().Nil(resultBlock)
|
|
|
|
} else {
|
|
|
|
suite.Require().Equal(expResultBlock, resultBlock)
|
|
|
|
suite.Require().Equal(expResultBlock.Block.Header.Height, resultBlock.Block.Header.Height)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
suite.Require().Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 14:07:56 +00:00
|
|
|
func (suite *BackendTestSuite) TestTendermintBlockResultByNumber() {
|
2022-07-29 10:35:07 +00:00
|
|
|
var expBlockRes *tmrpctypes.ResultBlockResults
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
blockNumber int64
|
|
|
|
registerMock func(int64)
|
|
|
|
expPass bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"fail",
|
|
|
|
1,
|
|
|
|
func(blockNum int64) {
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterBlockResultsError(client, blockNum)
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass",
|
|
|
|
1,
|
|
|
|
func(blockNum int64) {
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterBlockResults(client, blockNum)
|
|
|
|
|
|
|
|
expBlockRes = &tmrpctypes.ResultBlockResults{
|
|
|
|
Height: blockNum,
|
|
|
|
TxsResults: []*types.ResponseDeliverTx{{Code: 0, GasUsed: 0}},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
|
|
|
|
suite.SetupTest() // reset test and queries
|
|
|
|
tc.registerMock(tc.blockNumber)
|
|
|
|
|
2022-09-05 14:07:56 +00:00
|
|
|
blockRes, err := suite.backend.TendermintBlockResultByNumber(&tc.blockNumber)
|
2022-07-29 10:35:07 +00:00
|
|
|
|
|
|
|
if tc.expPass {
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Equal(expBlockRes, blockRes)
|
|
|
|
} else {
|
|
|
|
suite.Require().Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 14:07:56 +00:00
|
|
|
func (suite *BackendTestSuite) TestBlockNumberFromTendermint() {
|
|
|
|
var resBlock *tmrpctypes.ResultBlock
|
|
|
|
|
|
|
|
_, bz := suite.buildEthereumTx()
|
|
|
|
block := tmtypes.MakeBlock(1, []tmtypes.Tx{bz}, nil, nil)
|
|
|
|
blockNum := ethrpc.NewBlockNumber(big.NewInt(block.Height))
|
|
|
|
blockHash := common.BytesToHash(block.Hash())
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
blockNum *ethrpc.BlockNumber
|
|
|
|
hash *common.Hash
|
|
|
|
registerMock func(*common.Hash)
|
|
|
|
expPass bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"error - without blockHash or blockNum",
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
func(hash *common.Hash) {},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"error - with blockHash, tendermint client failed to get block",
|
|
|
|
nil,
|
|
|
|
&blockHash,
|
|
|
|
func(hash *common.Hash) {
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterBlockByHashError(client, *hash, bz)
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass - with blockHash",
|
|
|
|
nil,
|
|
|
|
&blockHash,
|
|
|
|
func(hash *common.Hash) {
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
resBlock, _ = RegisterBlockByHash(client, *hash, bz)
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass - without blockHash & with blockNumber",
|
|
|
|
&blockNum,
|
|
|
|
nil,
|
|
|
|
func(hash *common.Hash) {},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
|
|
|
|
suite.SetupTest() // reset test and queries
|
|
|
|
|
|
|
|
blockNrOrHash := ethrpc.BlockNumberOrHash{
|
|
|
|
BlockNumber: tc.blockNum,
|
|
|
|
BlockHash: tc.hash,
|
|
|
|
}
|
|
|
|
|
|
|
|
tc.registerMock(tc.hash)
|
|
|
|
blockNum, err := suite.backend.BlockNumberFromTendermint(blockNrOrHash)
|
|
|
|
|
|
|
|
if tc.expPass {
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
if tc.hash == nil {
|
|
|
|
suite.Require().Equal(*tc.blockNum, blockNum)
|
|
|
|
} else {
|
|
|
|
expHeight := ethrpc.NewBlockNumber(big.NewInt(resBlock.Block.Height))
|
|
|
|
suite.Require().Equal(expHeight, blockNum)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
suite.Require().Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *BackendTestSuite) TestBlockNumberFromTendermintByHash() {
|
|
|
|
var resBlock *tmrpctypes.ResultBlock
|
|
|
|
|
|
|
|
_, bz := suite.buildEthereumTx()
|
|
|
|
block := tmtypes.MakeBlock(1, []tmtypes.Tx{bz}, nil, nil)
|
|
|
|
emptyBlock := tmtypes.MakeBlock(1, []tmtypes.Tx{}, nil, nil)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
hash common.Hash
|
|
|
|
registerMock func(common.Hash)
|
|
|
|
expPass bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"fail - tendermint client failed to get block",
|
|
|
|
common.BytesToHash(block.Hash()),
|
|
|
|
func(hash common.Hash) {
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterBlockByHashError(client, hash, bz)
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass - block without tx",
|
|
|
|
common.BytesToHash(emptyBlock.Hash()),
|
|
|
|
func(hash common.Hash) {
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
resBlock, _ = RegisterBlockByHash(client, hash, bz)
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass - block with tx",
|
|
|
|
common.BytesToHash(block.Hash()),
|
|
|
|
func(hash common.Hash) {
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
resBlock, _ = RegisterBlockByHash(client, hash, bz)
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
|
|
|
|
suite.SetupTest() // reset test and queries
|
|
|
|
|
|
|
|
tc.registerMock(tc.hash)
|
|
|
|
blockNum, err := suite.backend.BlockNumberFromTendermintByHash(tc.hash)
|
|
|
|
if tc.expPass {
|
|
|
|
expHeight := big.NewInt(resBlock.Block.Height)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Equal(expHeight, blockNum)
|
|
|
|
} else {
|
|
|
|
suite.Require().Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-29 10:35:07 +00:00
|
|
|
func (suite *BackendTestSuite) TestBlockBloom() {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
blockRes *tmrpctypes.ResultBlockResults
|
|
|
|
expBlockBloom ethtypes.Bloom
|
|
|
|
expPass bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"fail - empty block result",
|
|
|
|
&tmrpctypes.ResultBlockResults{},
|
|
|
|
ethtypes.Bloom{},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fail - non block bloom event type",
|
|
|
|
&tmrpctypes.ResultBlockResults{
|
|
|
|
EndBlockEvents: []types.Event{{Type: evmtypes.EventTypeEthereumTx}},
|
|
|
|
},
|
|
|
|
ethtypes.Bloom{},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fail - nonblock bloom attribute key",
|
|
|
|
&tmrpctypes.ResultBlockResults{
|
|
|
|
EndBlockEvents: []types.Event{
|
|
|
|
{
|
|
|
|
Type: evmtypes.EventTypeBlockBloom,
|
|
|
|
Attributes: []types.EventAttribute{
|
|
|
|
{Key: []byte(evmtypes.AttributeKeyEthereumTxHash)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ethtypes.Bloom{},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
2022-09-05 14:07:56 +00:00
|
|
|
"pass - block bloom attribute key",
|
2022-07-29 10:35:07 +00:00
|
|
|
&tmrpctypes.ResultBlockResults{
|
|
|
|
EndBlockEvents: []types.Event{
|
|
|
|
{
|
|
|
|
Type: evmtypes.EventTypeBlockBloom,
|
|
|
|
Attributes: []types.EventAttribute{
|
|
|
|
{Key: []byte(bAttributeKeyEthereumBloom)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ethtypes.Bloom{},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
|
|
|
|
blockBloom, err := suite.backend.BlockBloom(tc.blockRes)
|
|
|
|
|
|
|
|
if tc.expPass {
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Equal(tc.expBlockBloom, blockBloom)
|
|
|
|
} else {
|
|
|
|
suite.Require().Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 14:07:56 +00:00
|
|
|
func (suite *BackendTestSuite) TestGetEthBlockFromTendermint() {
|
2022-07-29 10:35:07 +00:00
|
|
|
msgEthereumTx, bz := suite.buildEthereumTx()
|
|
|
|
emptyBlock := tmtypes.MakeBlock(1, []tmtypes.Tx{}, nil, nil)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
baseFee *big.Int
|
|
|
|
validator sdk.AccAddress
|
|
|
|
height int64
|
|
|
|
resBlock *tmrpctypes.ResultBlock
|
|
|
|
blockRes *tmrpctypes.ResultBlockResults
|
|
|
|
fullTx bool
|
|
|
|
registerMock func(sdk.Int, sdk.AccAddress, int64)
|
|
|
|
expTxs bool
|
|
|
|
expPass bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"pass - block without tx",
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
sdk.AccAddress(common.Address{}.Bytes()),
|
|
|
|
int64(1),
|
|
|
|
&tmrpctypes.ResultBlock{Block: emptyBlock},
|
|
|
|
&tmrpctypes.ResultBlockResults{
|
|
|
|
Height: 1,
|
|
|
|
TxsResults: []*types.ResponseDeliverTx{{Code: 0, GasUsed: 0}},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
func(baseFee sdk.Int, validator sdk.AccAddress, height int64) {
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
|
2022-07-29 10:35:07 +00:00
|
|
|
RegisterBaseFee(queryClient, baseFee)
|
|
|
|
RegisterValidatorAccount(queryClient, validator)
|
|
|
|
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterConsensusParams(client, height)
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass - block with tx - with BaseFee error",
|
|
|
|
nil,
|
|
|
|
sdk.AccAddress(tests.GenerateAddress().Bytes()),
|
|
|
|
int64(1),
|
|
|
|
&tmrpctypes.ResultBlock{
|
|
|
|
Block: tmtypes.MakeBlock(1, []tmtypes.Tx{bz}, nil, nil),
|
|
|
|
},
|
|
|
|
&tmrpctypes.ResultBlockResults{
|
|
|
|
Height: 1,
|
|
|
|
TxsResults: []*types.ResponseDeliverTx{{Code: 0, GasUsed: 0}},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
func(baseFee sdk.Int, validator sdk.AccAddress, height int64) {
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
|
2022-07-29 10:35:07 +00:00
|
|
|
RegisterBaseFeeError(queryClient)
|
|
|
|
RegisterValidatorAccount(queryClient, validator)
|
|
|
|
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterConsensusParams(client, height)
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass - block with tx - with ValidatorAccount error",
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
sdk.AccAddress(common.Address{}.Bytes()),
|
|
|
|
int64(1),
|
|
|
|
&tmrpctypes.ResultBlock{
|
|
|
|
Block: tmtypes.MakeBlock(1, []tmtypes.Tx{bz}, nil, nil),
|
|
|
|
},
|
|
|
|
&tmrpctypes.ResultBlockResults{
|
|
|
|
Height: 1,
|
|
|
|
TxsResults: []*types.ResponseDeliverTx{{Code: 0, GasUsed: 0}},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
func(baseFee sdk.Int, validator sdk.AccAddress, height int64) {
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
|
2022-07-29 10:35:07 +00:00
|
|
|
RegisterBaseFee(queryClient, baseFee)
|
|
|
|
RegisterValidatorAccountError(queryClient)
|
|
|
|
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterConsensusParams(client, height)
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass - block with tx - with ConsensusParams error - BlockMaxGas defaults to max uint32",
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
sdk.AccAddress(tests.GenerateAddress().Bytes()),
|
|
|
|
int64(1),
|
|
|
|
&tmrpctypes.ResultBlock{
|
|
|
|
Block: tmtypes.MakeBlock(1, []tmtypes.Tx{bz}, nil, nil),
|
|
|
|
},
|
|
|
|
&tmrpctypes.ResultBlockResults{
|
|
|
|
Height: 1,
|
|
|
|
TxsResults: []*types.ResponseDeliverTx{{Code: 0, GasUsed: 0}},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
func(baseFee sdk.Int, validator sdk.AccAddress, height int64) {
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
|
2022-07-29 10:35:07 +00:00
|
|
|
RegisterBaseFee(queryClient, baseFee)
|
|
|
|
RegisterValidatorAccount(queryClient, validator)
|
|
|
|
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterConsensusParamsError(client, height)
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass - block with tx - with ShouldIgnoreGasUsed - empty txs",
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
sdk.AccAddress(tests.GenerateAddress().Bytes()),
|
|
|
|
int64(1),
|
|
|
|
&tmrpctypes.ResultBlock{
|
|
|
|
Block: tmtypes.MakeBlock(1, []tmtypes.Tx{bz}, nil, nil),
|
|
|
|
},
|
|
|
|
&tmrpctypes.ResultBlockResults{
|
|
|
|
Height: 1,
|
|
|
|
TxsResults: []*types.ResponseDeliverTx{
|
|
|
|
{
|
|
|
|
Code: 11,
|
|
|
|
GasUsed: 0,
|
|
|
|
Log: "no block gas left to run tx: out of gas",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
func(baseFee sdk.Int, validator sdk.AccAddress, height int64) {
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
|
2022-07-29 10:35:07 +00:00
|
|
|
RegisterBaseFee(queryClient, baseFee)
|
|
|
|
RegisterValidatorAccount(queryClient, validator)
|
|
|
|
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterConsensusParams(client, height)
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass - block with tx - non fullTx",
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
sdk.AccAddress(tests.GenerateAddress().Bytes()),
|
|
|
|
int64(1),
|
|
|
|
&tmrpctypes.ResultBlock{
|
|
|
|
Block: tmtypes.MakeBlock(1, []tmtypes.Tx{bz}, nil, nil),
|
|
|
|
},
|
|
|
|
&tmrpctypes.ResultBlockResults{
|
|
|
|
Height: 1,
|
|
|
|
TxsResults: []*types.ResponseDeliverTx{{Code: 0, GasUsed: 0}},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
func(baseFee sdk.Int, validator sdk.AccAddress, height int64) {
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
|
2022-07-29 10:35:07 +00:00
|
|
|
RegisterBaseFee(queryClient, baseFee)
|
|
|
|
RegisterValidatorAccount(queryClient, validator)
|
|
|
|
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterConsensusParams(client, height)
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass - block with tx",
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
sdk.AccAddress(tests.GenerateAddress().Bytes()),
|
|
|
|
int64(1),
|
|
|
|
&tmrpctypes.ResultBlock{
|
|
|
|
Block: tmtypes.MakeBlock(1, []tmtypes.Tx{bz}, nil, nil),
|
|
|
|
},
|
|
|
|
&tmrpctypes.ResultBlockResults{
|
|
|
|
Height: 1,
|
|
|
|
TxsResults: []*types.ResponseDeliverTx{{Code: 0, GasUsed: 0}},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
func(baseFee sdk.Int, validator sdk.AccAddress, height int64) {
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
|
2022-07-29 10:35:07 +00:00
|
|
|
RegisterBaseFee(queryClient, baseFee)
|
|
|
|
RegisterValidatorAccount(queryClient, validator)
|
|
|
|
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterConsensusParams(client, height)
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
|
|
|
|
suite.SetupTest() // reset test and queries
|
|
|
|
tc.registerMock(sdk.NewIntFromBigInt(tc.baseFee), tc.validator, tc.height)
|
|
|
|
|
2022-09-05 14:07:56 +00:00
|
|
|
block, err := suite.backend.RPCBlockFromTendermintBlock(tc.resBlock, tc.blockRes, tc.fullTx)
|
2022-07-29 10:35:07 +00:00
|
|
|
|
|
|
|
var expBlock map[string]interface{}
|
|
|
|
header := tc.resBlock.Block.Header
|
|
|
|
gasLimit := int64(^uint32(0)) // for `MaxGas = -1` (DefaultConsensusParams)
|
|
|
|
gasUsed := new(big.Int).SetUint64(uint64(tc.blockRes.TxsResults[0].GasUsed))
|
|
|
|
|
|
|
|
root := common.Hash{}.Bytes()
|
|
|
|
receipt := ethtypes.NewReceipt(root, false, gasUsed.Uint64())
|
|
|
|
bloom := ethtypes.CreateBloom(ethtypes.Receipts{receipt})
|
|
|
|
|
|
|
|
ethRPCTxs := []interface{}{}
|
|
|
|
|
|
|
|
if tc.expTxs {
|
|
|
|
if tc.fullTx {
|
|
|
|
rpcTx, err := ethrpc.NewRPCTransaction(
|
|
|
|
msgEthereumTx.AsTransaction(),
|
|
|
|
common.BytesToHash(header.Hash()),
|
|
|
|
uint64(header.Height),
|
|
|
|
uint64(0),
|
|
|
|
tc.baseFee,
|
|
|
|
)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
ethRPCTxs = []interface{}{rpcTx}
|
|
|
|
} else {
|
|
|
|
ethRPCTxs = []interface{}{common.HexToHash(msgEthereumTx.Hash)}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expBlock = ethrpc.FormatBlock(
|
|
|
|
header,
|
|
|
|
tc.resBlock.Block.Size(),
|
|
|
|
gasLimit,
|
|
|
|
gasUsed,
|
|
|
|
ethRPCTxs,
|
|
|
|
bloom,
|
|
|
|
common.BytesToAddress(tc.validator.Bytes()),
|
|
|
|
tc.baseFee,
|
|
|
|
)
|
|
|
|
|
|
|
|
if tc.expPass {
|
|
|
|
suite.Require().Equal(expBlock, block)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
} else {
|
|
|
|
suite.Require().Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 14:07:56 +00:00
|
|
|
func (suite *BackendTestSuite) TestEthMsgsFromTendermintBlock() {
|
|
|
|
msgEthereumTx, bz := suite.buildEthereumTx()
|
2022-07-29 10:35:07 +00:00
|
|
|
|
|
|
|
testCases := []struct {
|
2022-09-05 14:07:56 +00:00
|
|
|
name string
|
|
|
|
resBlock *tmrpctypes.ResultBlock
|
|
|
|
blockRes *tmrpctypes.ResultBlockResults
|
|
|
|
expMsgs []*evmtypes.MsgEthereumTx
|
2022-07-29 10:35:07 +00:00
|
|
|
}{
|
|
|
|
{
|
2022-09-05 14:07:56 +00:00
|
|
|
"tx in not included in block - unsuccessful tx without ExceedBlockGasLimit error",
|
|
|
|
&tmrpctypes.ResultBlock{
|
|
|
|
Block: tmtypes.MakeBlock(1, []tmtypes.Tx{bz}, nil, nil),
|
2022-07-29 10:35:07 +00:00
|
|
|
},
|
|
|
|
&tmrpctypes.ResultBlockResults{
|
2022-09-05 14:07:56 +00:00
|
|
|
TxsResults: []*types.ResponseDeliverTx{
|
2022-07-29 10:35:07 +00:00
|
|
|
{
|
2022-09-05 14:07:56 +00:00
|
|
|
Code: 1,
|
2022-07-29 10:35:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-09-05 14:07:56 +00:00
|
|
|
[]*evmtypes.MsgEthereumTx(nil),
|
2022-07-29 10:35:07 +00:00
|
|
|
},
|
|
|
|
{
|
2022-09-05 14:07:56 +00:00
|
|
|
"tx included in block - unsuccessful tx with ExceedBlockGasLimit error",
|
|
|
|
&tmrpctypes.ResultBlock{
|
|
|
|
Block: tmtypes.MakeBlock(1, []tmtypes.Tx{bz}, nil, nil),
|
|
|
|
},
|
2022-07-29 10:35:07 +00:00
|
|
|
&tmrpctypes.ResultBlockResults{
|
2022-09-05 14:07:56 +00:00
|
|
|
TxsResults: []*types.ResponseDeliverTx{
|
2022-07-29 10:35:07 +00:00
|
|
|
{
|
2022-09-05 14:07:56 +00:00
|
|
|
Code: 1,
|
|
|
|
Log: ethrpc.ExceedBlockGasLimitError,
|
2022-07-29 10:35:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-09-05 14:07:56 +00:00
|
|
|
[]*evmtypes.MsgEthereumTx{msgEthereumTx},
|
2022-07-29 10:35:07 +00:00
|
|
|
},
|
|
|
|
{
|
2022-09-05 14:07:56 +00:00
|
|
|
"pass",
|
|
|
|
&tmrpctypes.ResultBlock{
|
|
|
|
Block: tmtypes.MakeBlock(1, []tmtypes.Tx{bz}, nil, nil),
|
|
|
|
},
|
2022-07-29 10:35:07 +00:00
|
|
|
&tmrpctypes.ResultBlockResults{
|
2022-09-05 14:07:56 +00:00
|
|
|
TxsResults: []*types.ResponseDeliverTx{
|
2022-07-29 10:35:07 +00:00
|
|
|
{
|
2022-09-05 14:07:56 +00:00
|
|
|
Code: 0,
|
|
|
|
Log: ethrpc.ExceedBlockGasLimitError,
|
2022-07-29 10:35:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-09-05 14:07:56 +00:00
|
|
|
[]*evmtypes.MsgEthereumTx{msgEthereumTx},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
|
|
|
|
suite.SetupTest() // reset test and queries
|
|
|
|
|
|
|
|
msgs := suite.backend.EthMsgsFromTendermintBlock(tc.resBlock, tc.blockRes)
|
|
|
|
suite.Require().Equal(tc.expMsgs, msgs)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *BackendTestSuite) TestHeaderByNumber() {
|
|
|
|
var expResultBlock *tmrpctypes.ResultBlock
|
|
|
|
|
|
|
|
_, bz := suite.buildEthereumTx()
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
blockNumber ethrpc.BlockNumber
|
|
|
|
baseFee *big.Int
|
|
|
|
registerMock func(ethrpc.BlockNumber, sdk.Int)
|
|
|
|
expPass bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"fail - tendermint client failed to get block",
|
|
|
|
ethrpc.BlockNumber(1),
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
func(blockNum ethrpc.BlockNumber, baseFee sdk.Int) {
|
|
|
|
height := blockNum.Int64()
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterBlockError(client, height)
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fail - block not found for height",
|
|
|
|
ethrpc.BlockNumber(1),
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
func(blockNum ethrpc.BlockNumber, baseFee sdk.Int) {
|
|
|
|
height := blockNum.Int64()
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterBlockNotFound(client, height)
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fail - block not found for height",
|
|
|
|
ethrpc.BlockNumber(1),
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
func(blockNum ethrpc.BlockNumber, baseFee sdk.Int) {
|
|
|
|
height := blockNum.Int64()
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterBlock(client, height, nil)
|
|
|
|
RegisterBlockResultsError(client, height)
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass - without Base Fee, failed to fetch from prunned block",
|
|
|
|
ethrpc.BlockNumber(1),
|
|
|
|
nil,
|
|
|
|
func(blockNum ethrpc.BlockNumber, baseFee sdk.Int) {
|
|
|
|
height := blockNum.Int64()
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
expResultBlock, _ = RegisterBlock(client, height, nil)
|
|
|
|
RegisterBlockResults(client, height)
|
|
|
|
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
|
2022-07-29 10:35:07 +00:00
|
|
|
RegisterBaseFeeError(queryClient)
|
|
|
|
},
|
2022-09-05 14:07:56 +00:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass - blockNum = 1, without tx",
|
|
|
|
ethrpc.BlockNumber(1),
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
func(blockNum ethrpc.BlockNumber, baseFee sdk.Int) {
|
|
|
|
height := blockNum.Int64()
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
expResultBlock, _ = RegisterBlock(client, height, nil)
|
|
|
|
RegisterBlockResults(client, height)
|
|
|
|
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
|
2022-09-05 14:07:56 +00:00
|
|
|
RegisterBaseFee(queryClient, baseFee)
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass - blockNum = 1, with tx",
|
|
|
|
ethrpc.BlockNumber(1),
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
func(blockNum ethrpc.BlockNumber, baseFee sdk.Int) {
|
|
|
|
height := blockNum.Int64()
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
expResultBlock, _ = RegisterBlock(client, height, bz)
|
|
|
|
RegisterBlockResults(client, height)
|
|
|
|
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
|
2022-09-05 14:07:56 +00:00
|
|
|
RegisterBaseFee(queryClient, baseFee)
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
|
|
|
|
suite.SetupTest() // reset test and queries
|
|
|
|
|
|
|
|
tc.registerMock(tc.blockNumber, sdk.NewIntFromBigInt(tc.baseFee))
|
|
|
|
header, err := suite.backend.HeaderByNumber(tc.blockNumber)
|
|
|
|
|
|
|
|
if tc.expPass {
|
|
|
|
expHeader := ethrpc.EthHeaderFromTendermint(expResultBlock.Block.Header, ethtypes.Bloom{}, tc.baseFee)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Equal(expHeader, header)
|
|
|
|
} else {
|
|
|
|
suite.Require().Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *BackendTestSuite) TestHeaderByHash() {
|
|
|
|
var expResultBlock *tmrpctypes.ResultBlock
|
|
|
|
|
|
|
|
_, bz := suite.buildEthereumTx()
|
|
|
|
block := tmtypes.MakeBlock(1, []tmtypes.Tx{bz}, nil, nil)
|
|
|
|
emptyBlock := tmtypes.MakeBlock(1, []tmtypes.Tx{}, nil, nil)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
hash common.Hash
|
|
|
|
baseFee *big.Int
|
|
|
|
registerMock func(common.Hash, sdk.Int)
|
|
|
|
expPass bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"fail - tendermint client failed to get block",
|
|
|
|
common.BytesToHash(block.Hash()),
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
func(hash common.Hash, baseFee sdk.Int) {
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterBlockByHashError(client, hash, bz)
|
|
|
|
},
|
2022-07-29 10:35:07 +00:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
2022-09-05 14:07:56 +00:00
|
|
|
"fail - block not found for height",
|
|
|
|
common.BytesToHash(block.Hash()),
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
func(hash common.Hash, baseFee sdk.Int) {
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterBlockByHashNotFound(client, hash, bz)
|
2022-07-29 10:35:07 +00:00
|
|
|
},
|
2022-09-05 14:07:56 +00:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fail - block not found for height",
|
|
|
|
common.BytesToHash(block.Hash()),
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
func(hash common.Hash, baseFee sdk.Int) {
|
|
|
|
height := int64(1)
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterBlockByHash(client, hash, bz)
|
|
|
|
RegisterBlockResultsError(client, height)
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass - without Base Fee, failed to fetch from prunned block",
|
|
|
|
common.BytesToHash(block.Hash()),
|
|
|
|
nil,
|
|
|
|
func(hash common.Hash, baseFee sdk.Int) {
|
|
|
|
height := int64(1)
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
expResultBlock, _ = RegisterBlockByHash(client, hash, bz)
|
|
|
|
RegisterBlockResults(client, height)
|
|
|
|
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
|
2022-07-29 10:35:07 +00:00
|
|
|
RegisterBaseFeeError(queryClient)
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2022-09-05 14:07:56 +00:00
|
|
|
"pass - blockNum = 1, without tx",
|
|
|
|
common.BytesToHash(emptyBlock.Hash()),
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
func(hash common.Hash, baseFee sdk.Int) {
|
|
|
|
height := int64(1)
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
expResultBlock, _ = RegisterBlockByHash(client, hash, nil)
|
|
|
|
RegisterBlockResults(client, height)
|
|
|
|
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
|
2022-09-05 14:07:56 +00:00
|
|
|
RegisterBaseFee(queryClient, baseFee)
|
2022-07-29 10:35:07 +00:00
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2022-09-05 14:07:56 +00:00
|
|
|
"pass - with tx",
|
|
|
|
common.BytesToHash(block.Hash()),
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
func(hash common.Hash, baseFee sdk.Int) {
|
|
|
|
height := int64(1)
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
expResultBlock, _ = RegisterBlockByHash(client, hash, bz)
|
|
|
|
RegisterBlockResults(client, height)
|
|
|
|
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
|
2022-07-29 10:35:07 +00:00
|
|
|
RegisterBaseFee(queryClient, baseFee)
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
|
|
|
|
suite.SetupTest() // reset test and queries
|
|
|
|
|
2022-09-05 14:07:56 +00:00
|
|
|
tc.registerMock(tc.hash, sdk.NewIntFromBigInt(tc.baseFee))
|
|
|
|
header, err := suite.backend.HeaderByHash(tc.hash)
|
2022-07-29 10:35:07 +00:00
|
|
|
|
|
|
|
if tc.expPass {
|
2022-09-05 14:07:56 +00:00
|
|
|
expHeader := ethrpc.EthHeaderFromTendermint(expResultBlock.Block.Header, ethtypes.Bloom{}, tc.baseFee)
|
2022-07-29 10:35:07 +00:00
|
|
|
suite.Require().NoError(err)
|
2022-09-05 14:07:56 +00:00
|
|
|
suite.Require().Equal(expHeader, header)
|
2022-07-29 10:35:07 +00:00
|
|
|
} else {
|
|
|
|
suite.Require().Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 14:07:56 +00:00
|
|
|
func (suite *BackendTestSuite) TestEthBlockByNumber() {
|
2022-07-29 10:35:07 +00:00
|
|
|
msgEthereumTx, bz := suite.buildEthereumTx()
|
2022-09-05 14:07:56 +00:00
|
|
|
emptyBlock := tmtypes.MakeBlock(1, []tmtypes.Tx{}, nil, nil)
|
2022-07-29 10:35:07 +00:00
|
|
|
|
|
|
|
testCases := []struct {
|
2022-09-05 14:07:56 +00:00
|
|
|
name string
|
|
|
|
blockNumber ethrpc.BlockNumber
|
|
|
|
registerMock func(ethrpc.BlockNumber)
|
|
|
|
expEthBlock *ethtypes.Block
|
|
|
|
expPass bool
|
2022-07-29 10:35:07 +00:00
|
|
|
}{
|
|
|
|
{
|
2022-09-05 14:07:56 +00:00
|
|
|
"fail - tendermint client failed to get block",
|
|
|
|
ethrpc.BlockNumber(1),
|
|
|
|
func(blockNum ethrpc.BlockNumber) {
|
|
|
|
height := blockNum.Int64()
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterBlockError(client, height)
|
2022-07-29 10:35:07 +00:00
|
|
|
},
|
2022-09-05 14:07:56 +00:00
|
|
|
nil,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fail - block result not found for height",
|
|
|
|
ethrpc.BlockNumber(1),
|
|
|
|
func(blockNum ethrpc.BlockNumber) {
|
|
|
|
height := blockNum.Int64()
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterBlock(client, height, nil)
|
|
|
|
RegisterBlockResultsError(client, blockNum.Int64())
|
2022-07-29 10:35:07 +00:00
|
|
|
},
|
2022-09-05 14:07:56 +00:00
|
|
|
nil,
|
|
|
|
false,
|
2022-07-29 10:35:07 +00:00
|
|
|
},
|
|
|
|
{
|
2022-09-05 14:07:56 +00:00
|
|
|
"pass - block without tx",
|
|
|
|
ethrpc.BlockNumber(1),
|
|
|
|
func(blockNum ethrpc.BlockNumber) {
|
|
|
|
height := blockNum.Int64()
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterBlock(client, height, nil)
|
|
|
|
|
|
|
|
RegisterBlockResults(client, blockNum.Int64())
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
|
2022-09-05 14:07:56 +00:00
|
|
|
baseFee := sdk.NewInt(1)
|
|
|
|
RegisterBaseFee(queryClient, baseFee)
|
|
|
|
},
|
|
|
|
ethtypes.NewBlock(
|
|
|
|
ethrpc.EthHeaderFromTendermint(
|
|
|
|
emptyBlock.Header,
|
|
|
|
ethtypes.Bloom{},
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
),
|
|
|
|
[]*ethtypes.Transaction{},
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pass - block with tx",
|
|
|
|
ethrpc.BlockNumber(1),
|
|
|
|
func(blockNum ethrpc.BlockNumber) {
|
|
|
|
height := blockNum.Int64()
|
|
|
|
client := suite.backend.clientCtx.Client.(*mocks.Client)
|
|
|
|
RegisterBlock(client, height, bz)
|
|
|
|
|
|
|
|
RegisterBlockResults(client, blockNum.Int64())
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
|
2022-09-05 14:07:56 +00:00
|
|
|
baseFee := sdk.NewInt(1)
|
|
|
|
RegisterBaseFee(queryClient, baseFee)
|
|
|
|
},
|
|
|
|
ethtypes.NewBlock(
|
|
|
|
ethrpc.EthHeaderFromTendermint(
|
|
|
|
emptyBlock.Header,
|
|
|
|
ethtypes.Bloom{},
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
),
|
|
|
|
[]*ethtypes.Transaction{msgEthereumTx.AsTransaction()},
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
trie.NewStackTrie(nil),
|
|
|
|
),
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
|
|
|
|
suite.SetupTest() // reset test and queries
|
|
|
|
tc.registerMock(tc.blockNumber)
|
|
|
|
|
|
|
|
ethBlock, err := suite.backend.EthBlockByNumber(tc.blockNumber)
|
|
|
|
|
|
|
|
if tc.expPass {
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Equal(tc.expEthBlock.Header(), ethBlock.Header())
|
|
|
|
suite.Require().Equal(tc.expEthBlock.Uncles(), ethBlock.Uncles())
|
|
|
|
suite.Require().Equal(tc.expEthBlock.ReceiptHash(), ethBlock.ReceiptHash())
|
|
|
|
for i, tx := range tc.expEthBlock.Transactions() {
|
|
|
|
suite.Require().Equal(tx.Data(), ethBlock.Transactions()[i].Data())
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
suite.Require().Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *BackendTestSuite) TestEthBlockFromTendermintBlock() {
|
|
|
|
msgEthereumTx, bz := suite.buildEthereumTx()
|
|
|
|
emptyBlock := tmtypes.MakeBlock(1, []tmtypes.Tx{}, nil, nil)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
baseFee *big.Int
|
|
|
|
resBlock *tmrpctypes.ResultBlock
|
|
|
|
blockRes *tmrpctypes.ResultBlockResults
|
|
|
|
registerMock func(sdk.Int, int64)
|
|
|
|
expEthBlock *ethtypes.Block
|
|
|
|
expPass bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"pass - block without tx",
|
|
|
|
sdk.NewInt(1).BigInt(),
|
2022-07-29 10:35:07 +00:00
|
|
|
&tmrpctypes.ResultBlock{
|
2022-09-05 14:07:56 +00:00
|
|
|
Block: emptyBlock,
|
2022-07-29 10:35:07 +00:00
|
|
|
},
|
|
|
|
&tmrpctypes.ResultBlockResults{
|
2022-09-05 14:07:56 +00:00
|
|
|
Height: 1,
|
|
|
|
TxsResults: []*types.ResponseDeliverTx{{Code: 0, GasUsed: 0}},
|
2022-07-29 10:35:07 +00:00
|
|
|
},
|
2022-09-05 14:07:56 +00:00
|
|
|
func(baseFee sdk.Int, blockNum int64) {
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
|
2022-09-05 14:07:56 +00:00
|
|
|
RegisterBaseFee(queryClient, baseFee)
|
|
|
|
},
|
|
|
|
ethtypes.NewBlock(
|
|
|
|
ethrpc.EthHeaderFromTendermint(
|
|
|
|
emptyBlock.Header,
|
|
|
|
ethtypes.Bloom{},
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
),
|
|
|
|
[]*ethtypes.Transaction{},
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
true,
|
2022-07-29 10:35:07 +00:00
|
|
|
},
|
|
|
|
{
|
2022-09-05 14:07:56 +00:00
|
|
|
"pass - block with tx",
|
|
|
|
sdk.NewInt(1).BigInt(),
|
2022-07-29 10:35:07 +00:00
|
|
|
&tmrpctypes.ResultBlock{
|
|
|
|
Block: tmtypes.MakeBlock(1, []tmtypes.Tx{bz}, nil, nil),
|
|
|
|
},
|
|
|
|
&tmrpctypes.ResultBlockResults{
|
2022-09-05 14:07:56 +00:00
|
|
|
Height: 1,
|
|
|
|
TxsResults: []*types.ResponseDeliverTx{{Code: 0, GasUsed: 0}},
|
|
|
|
EndBlockEvents: []types.Event{
|
2022-07-29 10:35:07 +00:00
|
|
|
{
|
2022-09-05 14:07:56 +00:00
|
|
|
Type: evmtypes.EventTypeBlockBloom,
|
|
|
|
Attributes: []types.EventAttribute{
|
|
|
|
{Key: []byte(bAttributeKeyEthereumBloom)},
|
|
|
|
},
|
2022-07-29 10:35:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-09-05 14:07:56 +00:00
|
|
|
func(baseFee sdk.Int, blockNum int64) {
|
2022-10-11 18:43:32 +00:00
|
|
|
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
|
2022-09-05 14:07:56 +00:00
|
|
|
RegisterBaseFee(queryClient, baseFee)
|
|
|
|
},
|
|
|
|
ethtypes.NewBlock(
|
|
|
|
ethrpc.EthHeaderFromTendermint(
|
|
|
|
emptyBlock.Header,
|
|
|
|
ethtypes.Bloom{},
|
|
|
|
sdk.NewInt(1).BigInt(),
|
|
|
|
),
|
|
|
|
[]*ethtypes.Transaction{msgEthereumTx.AsTransaction()},
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
trie.NewStackTrie(nil),
|
|
|
|
),
|
|
|
|
true,
|
2022-07-29 10:35:07 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
|
|
|
|
suite.SetupTest() // reset test and queries
|
2022-09-05 14:07:56 +00:00
|
|
|
tc.registerMock(sdk.NewIntFromBigInt(tc.baseFee), tc.blockRes.Height)
|
2022-07-23 09:16:23 +00:00
|
|
|
|
2022-09-05 14:07:56 +00:00
|
|
|
ethBlock, err := suite.backend.EthBlockFromTendermintBlock(tc.resBlock, tc.blockRes)
|
|
|
|
|
|
|
|
if tc.expPass {
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Equal(tc.expEthBlock.Header(), ethBlock.Header())
|
|
|
|
suite.Require().Equal(tc.expEthBlock.Uncles(), ethBlock.Uncles())
|
|
|
|
suite.Require().Equal(tc.expEthBlock.ReceiptHash(), ethBlock.ReceiptHash())
|
|
|
|
for i, tx := range tc.expEthBlock.Transactions() {
|
|
|
|
suite.Require().Equal(tx.Data(), ethBlock.Transactions()[i].Data())
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
suite.Require().Error(err)
|
|
|
|
}
|
2022-07-29 10:35:07 +00:00
|
|
|
})
|
2022-07-23 09:16:23 +00:00
|
|
|
}
|
|
|
|
}
|