package backend
import (
"fmt"
"math/big"
rpc "github.com/cerc-io/laconicd/rpc/types"
"github.com/cerc-io/laconicd/tests"
"github.com/ethereum/go-ethereum/common/hexutil"
ethrpc "github.com/ethereum/go-ethereum/rpc"
"google.golang.org/grpc/metadata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/tendermint/tendermint/abci/types"
tmrpctypes "github.com/tendermint/tendermint/rpc/core/types"
"github.com/cerc-io/laconicd/rpc/backend/mocks"
evmtypes "github.com/cerc-io/laconicd/x/evm/types"
feemarkettypes "github.com/cerc-io/laconicd/x/feemarket/types"
)
func (suite *BackendTestSuite) TestBaseFee() {
baseFee := sdk.NewInt(1)
testCases := []struct {
name string
blockRes *tmrpctypes.ResultBlockResults
registerMock func()
expBaseFee *big.Int
expPass bool
}{
{
"fail - grpc BaseFee error",
&tmrpctypes.ResultBlockResults{Height: 1},
func() {
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
RegisterBaseFeeError(queryClient)
},
nil,
false,
"fail - grpc BaseFee error - with non feemarket block event",
&tmrpctypes.ResultBlockResults{
Height: 1,
BeginBlockEvents: []types.Event{
Type: evmtypes.EventTypeBlockBloom,
"fail - grpc BaseFee error - with feemarket block event",
Type: feemarkettypes.EventTypeFeeMarket,
"fail - grpc BaseFee error - with feemarket block event with wrong attribute value",
Attributes: []types.EventAttribute{
{Value: []byte{0x1}},
"fail - grpc baseFee error - with feemarket block event with baseFee attribute value",
{Value: []byte(baseFee.String())},
baseFee.BigInt(),
true,
"fail - base fee or london fork not enabled",
RegisterBaseFeeDisabled(queryClient)
"pass",
RegisterBaseFee(queryClient, baseFee)
}
for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
suite.SetupTest() // reset test and queries
tc.registerMock()
baseFee, err := suite.backend.BaseFee(tc.blockRes)
if tc.expPass {
suite.Require().NoError(err)
suite.Require().Equal(tc.expBaseFee, baseFee)
} else {
suite.Require().Error(err)
})
func (suite *BackendTestSuite) TestChainId() {
expChainId := (*hexutil.Big)(big.NewInt(9000))
expChainId *hexutil.Big
"pass - block is at or past the EIP-155 replay-protection fork block, return chainID from config ",
var header metadata.MD
RegisterParamsInvalidHeight(queryClient, &header, int64(1))
expChainId,
suite.Run(fmt.Sprintf("case %s", tc.name), func() {
chainId, err := suite.backend.ChainID()
suite.Require().Equal(tc.expChainId, chainId)
func (suite *BackendTestSuite) TestGetCoinbase() {
validatorAcc := sdk.AccAddress(tests.GenerateAddress().Bytes())
accAddr sdk.AccAddress
"fail - Can't retrieve status from node",
client := suite.backend.clientCtx.Client.(*mocks.Client)
RegisterStatusError(client)
validatorAcc,
"fail - Can't query validator account",
RegisterStatus(client)
RegisterValidatorAccountError(queryClient)
"pass - Gets coinbase account",
RegisterValidatorAccount(queryClient, validatorAcc)
accAddr, err := suite.backend.GetCoinbase()
suite.Require().Equal(tc.accAddr, accAddr)
func (suite *BackendTestSuite) TestSuggestGasTipCap() {
baseFee *big.Int
expGasTipCap *big.Int
"pass - London hardfork not enabled or feemarket not enabled ",
func() {},
big.NewInt(0),
"pass - Gets the suggest gas tip cap ",
maxDelta, err := suite.backend.SuggestGasTipCap(tc.baseFee)
suite.Require().Equal(tc.expGasTipCap, maxDelta)
func (suite *BackendTestSuite) TestGlobalMinGasPrice() {
expMinGasPrice sdk.Dec
"fail - Can't get FeeMarket params",
feeMarketCleint := suite.backend.queryClient.FeeMarket.(*mocks.FeeMarketQueryClient)
RegisterFeeMarketParamsError(feeMarketCleint, int64(1))
sdk.ZeroDec(),
globalMinGasPrice, err := suite.backend.GlobalMinGasPrice()
suite.Require().Equal(tc.expMinGasPrice, globalMinGasPrice)
func (suite *BackendTestSuite) TestFeeHistory() {
registerMock func(validator sdk.AccAddress)
userBlockCount ethrpc.DecimalOrHex
latestBlock ethrpc.BlockNumber
expFeeHistory *rpc.FeeHistoryResult
validator sdk.AccAddress
"fail - can't get params ",
func(validator sdk.AccAddress) {
suite.backend.cfg.JSONRPC.FeeHistoryCap = 0
RegisterParamsError(queryClient, &header, ethrpc.BlockNumber(1).Int64())
1,
0,
"fail - user block count higher than max block count ",
RegisterParams(queryClient, &header, ethrpc.BlockNumber(1).Int64())
"fail - Tendermint block fetching error ",
suite.backend.cfg.JSONRPC.FeeHistoryCap = 2
RegisterBlockError(client, ethrpc.BlockNumber(1).Int64())
"fail - Eth block fetching error",
RegisterBlock(client, ethrpc.BlockNumber(1).Int64(), nil)
RegisterBlockResultsError(client, 1)
"fail - Invalid base fee",
//baseFee := sdk.NewInt(1)
RegisterBlockResults(client, 1)
RegisterValidatorAccount(queryClient, validator)
RegisterConsensusParams(client, 1)
sdk.AccAddress(tests.GenerateAddress().Bytes()),
"pass - Valid FeeHistoryResults object",
&rpc.FeeHistoryResult{
OldestBlock: (*hexutil.Big)(big.NewInt(0)),
BaseFee: []*hexutil.Big{(*hexutil.Big)(big.NewInt(1))},
GasUsedRatio: []float64{0},
Reward: [][]*hexutil.Big{{(*hexutil.Big)(big.NewInt(0)), (*hexutil.Big)(big.NewInt(0)), (*hexutil.Big)(big.NewInt(0)), (*hexutil.Big)(big.NewInt(0))}},
tc.registerMock(tc.validator)
feeHistory, err := suite.backend.FeeHistory(tc.userBlockCount, tc.latestBlock, []float64{25, 50, 75, 100})
suite.Require().Equal(feeHistory, tc.expFeeHistory)