2021-08-30 03:14:58 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
sdkmath "cosmossdk.io/math"
|
2021-08-30 03:14:58 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2022-10-10 10:38:33 +00:00
|
|
|
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
|
2021-08-30 03:14:58 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
|
2022-12-14 11:01:06 +00:00
|
|
|
laconicd "github.com/cerc-io/laconicd/types"
|
2022-09-07 06:36:11 +00:00
|
|
|
"github.com/cerc-io/laconicd/x/evm/types"
|
2021-08-30 03:14:58 +00:00
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func SetupContract(b *testing.B) (*KeeperTestSuite, common.Address) {
|
|
|
|
suite := KeeperTestSuite{}
|
2022-10-10 10:38:33 +00:00
|
|
|
suite.SetupTest()
|
2021-08-30 03:14:58 +00:00
|
|
|
|
2022-12-14 11:01:06 +00:00
|
|
|
amt := sdk.Coins{laconicd.NewPhotonCoinInt64(1000000000000000000)}
|
2021-08-30 03:14:58 +00:00
|
|
|
err := suite.app.BankKeeper.MintCoins(suite.ctx, types.ModuleName, amt)
|
|
|
|
require.NoError(b, err)
|
|
|
|
err = suite.app.BankKeeper.SendCoinsFromModuleToAccount(suite.ctx, types.ModuleName, suite.address.Bytes(), amt)
|
|
|
|
require.NoError(b, err)
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
contractAddr := suite.DeployTestContract(b, suite.address, sdkmath.NewIntWithDecimal(1000, 18).BigInt())
|
2021-08-30 03:14:58 +00:00
|
|
|
suite.Commit()
|
|
|
|
|
|
|
|
return &suite, contractAddr
|
|
|
|
}
|
|
|
|
|
2021-11-08 13:04:49 +00:00
|
|
|
func SetupTestMessageCall(b *testing.B) (*KeeperTestSuite, common.Address) {
|
|
|
|
suite := KeeperTestSuite{}
|
2022-10-10 10:38:33 +00:00
|
|
|
suite.SetupTest()
|
2021-11-08 13:04:49 +00:00
|
|
|
|
2022-12-14 11:01:06 +00:00
|
|
|
amt := sdk.Coins{laconicd.NewPhotonCoinInt64(1000000000000000000)}
|
2021-11-08 13:04:49 +00:00
|
|
|
err := suite.app.BankKeeper.MintCoins(suite.ctx, types.ModuleName, amt)
|
|
|
|
require.NoError(b, err)
|
|
|
|
err = suite.app.BankKeeper.SendCoinsFromModuleToAccount(suite.ctx, types.ModuleName, suite.address.Bytes(), amt)
|
|
|
|
require.NoError(b, err)
|
|
|
|
|
|
|
|
contractAddr := suite.DeployTestMessageCall(b)
|
|
|
|
suite.Commit()
|
|
|
|
|
|
|
|
return &suite, contractAddr
|
|
|
|
}
|
|
|
|
|
2021-08-30 03:14:58 +00:00
|
|
|
type TxBuilder func(suite *KeeperTestSuite, contract common.Address) *types.MsgEthereumTx
|
|
|
|
|
|
|
|
func DoBenchmark(b *testing.B, txBuilder TxBuilder) {
|
|
|
|
suite, contractAddr := SetupContract(b)
|
|
|
|
|
|
|
|
msg := txBuilder(suite, contractAddr)
|
|
|
|
msg.From = suite.address.Hex()
|
|
|
|
err := msg.Sign(ethtypes.LatestSignerForChainID(suite.app.EvmKeeper.ChainID()), suite.signer)
|
|
|
|
require.NoError(b, err)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
ctx, _ := suite.ctx.CacheContext()
|
|
|
|
|
|
|
|
// deduct fee first
|
2022-05-18 05:57:11 +00:00
|
|
|
txData, err := types.UnpackTxData(msg.Data)
|
|
|
|
require.NoError(b, err)
|
2021-08-30 03:14:58 +00:00
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
fees := sdk.Coins{sdk.NewCoin(suite.EvmDenom(), sdkmath.NewIntFromBigInt(txData.Fee()))}
|
|
|
|
err = authante.DeductFees(suite.app.BankKeeper, suite.ctx, suite.app.AccountKeeper.GetAccount(ctx, msg.GetFrom()), fees)
|
2022-05-18 05:57:11 +00:00
|
|
|
require.NoError(b, err)
|
2021-08-30 03:14:58 +00:00
|
|
|
|
|
|
|
rsp, err := suite.app.EvmKeeper.EthereumTx(sdk.WrapSDKContext(ctx), msg)
|
|
|
|
require.NoError(b, err)
|
|
|
|
require.False(b, rsp.Failed())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkTokenTransfer(b *testing.B) {
|
|
|
|
DoBenchmark(b, func(suite *KeeperTestSuite, contract common.Address) *types.MsgEthereumTx {
|
2021-10-20 19:00:17 +00:00
|
|
|
input, err := types.ERC20Contract.ABI.Pack("transfer", common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), big.NewInt(1000))
|
2021-08-30 03:14:58 +00:00
|
|
|
require.NoError(b, err)
|
2022-01-05 07:28:27 +00:00
|
|
|
nonce := suite.app.EvmKeeper.GetNonce(suite.ctx, suite.address)
|
2021-10-05 15:38:20 +00:00
|
|
|
return types.NewTx(suite.app.EvmKeeper.ChainID(), nonce, &contract, big.NewInt(0), 410000, big.NewInt(1), nil, nil, input, nil)
|
2021-08-30 03:14:58 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEmitLogs(b *testing.B) {
|
|
|
|
DoBenchmark(b, func(suite *KeeperTestSuite, contract common.Address) *types.MsgEthereumTx {
|
2021-10-20 19:00:17 +00:00
|
|
|
input, err := types.ERC20Contract.ABI.Pack("benchmarkLogs", big.NewInt(1000))
|
2021-08-30 03:14:58 +00:00
|
|
|
require.NoError(b, err)
|
2022-01-05 07:28:27 +00:00
|
|
|
nonce := suite.app.EvmKeeper.GetNonce(suite.ctx, suite.address)
|
2021-10-05 15:38:20 +00:00
|
|
|
return types.NewTx(suite.app.EvmKeeper.ChainID(), nonce, &contract, big.NewInt(0), 4100000, big.NewInt(1), nil, nil, input, nil)
|
2021-08-30 03:14:58 +00:00
|
|
|
})
|
|
|
|
}
|
2021-09-24 15:58:22 +00:00
|
|
|
|
|
|
|
func BenchmarkTokenTransferFrom(b *testing.B) {
|
|
|
|
DoBenchmark(b, func(suite *KeeperTestSuite, contract common.Address) *types.MsgEthereumTx {
|
2021-10-20 19:00:17 +00:00
|
|
|
input, err := types.ERC20Contract.ABI.Pack("transferFrom", suite.address, common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), big.NewInt(0))
|
2021-09-24 15:58:22 +00:00
|
|
|
require.NoError(b, err)
|
2022-01-05 07:28:27 +00:00
|
|
|
nonce := suite.app.EvmKeeper.GetNonce(suite.ctx, suite.address)
|
2021-10-05 15:38:20 +00:00
|
|
|
return types.NewTx(suite.app.EvmKeeper.ChainID(), nonce, &contract, big.NewInt(0), 410000, big.NewInt(1), nil, nil, input, nil)
|
2021-09-24 15:58:22 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkTokenMint(b *testing.B) {
|
|
|
|
DoBenchmark(b, func(suite *KeeperTestSuite, contract common.Address) *types.MsgEthereumTx {
|
2021-10-20 19:00:17 +00:00
|
|
|
input, err := types.ERC20Contract.ABI.Pack("mint", common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), big.NewInt(1000))
|
2021-09-24 15:58:22 +00:00
|
|
|
require.NoError(b, err)
|
2022-01-05 07:28:27 +00:00
|
|
|
nonce := suite.app.EvmKeeper.GetNonce(suite.ctx, suite.address)
|
2021-10-05 15:38:20 +00:00
|
|
|
return types.NewTx(suite.app.EvmKeeper.ChainID(), nonce, &contract, big.NewInt(0), 410000, big.NewInt(1), nil, nil, input, nil)
|
2021-09-24 15:58:22 +00:00
|
|
|
})
|
|
|
|
}
|
2021-10-06 11:28:13 +00:00
|
|
|
|
2021-11-08 13:04:49 +00:00
|
|
|
func BenchmarkMessageCall(b *testing.B) {
|
|
|
|
suite, contract := SetupTestMessageCall(b)
|
|
|
|
|
|
|
|
input, err := types.TestMessageCall.ABI.Pack("benchmarkMessageCall", big.NewInt(10000))
|
|
|
|
require.NoError(b, err)
|
2022-01-05 07:28:27 +00:00
|
|
|
nonce := suite.app.EvmKeeper.GetNonce(suite.ctx, suite.address)
|
2021-11-08 13:04:49 +00:00
|
|
|
msg := types.NewTx(suite.app.EvmKeeper.ChainID(), nonce, &contract, big.NewInt(0), 25000000, big.NewInt(1), nil, nil, input, nil)
|
|
|
|
|
|
|
|
msg.From = suite.address.Hex()
|
|
|
|
err = msg.Sign(ethtypes.LatestSignerForChainID(suite.app.EvmKeeper.ChainID()), suite.signer)
|
|
|
|
require.NoError(b, err)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
ctx, _ := suite.ctx.CacheContext()
|
|
|
|
|
|
|
|
// deduct fee first
|
|
|
|
txData, err := types.UnpackTxData(msg.Data)
|
|
|
|
require.NoError(b, err)
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
fees := sdk.Coins{sdk.NewCoin(suite.EvmDenom(), sdkmath.NewIntFromBigInt(txData.Fee()))}
|
|
|
|
err = authante.DeductFees(suite.app.BankKeeper, suite.ctx, suite.app.AccountKeeper.GetAccount(ctx, msg.GetFrom()), fees)
|
2021-11-08 13:04:49 +00:00
|
|
|
require.NoError(b, err)
|
|
|
|
|
|
|
|
rsp, err := suite.app.EvmKeeper.EthereumTx(sdk.WrapSDKContext(ctx), msg)
|
|
|
|
require.NoError(b, err)
|
|
|
|
require.False(b, rsp.Failed())
|
|
|
|
}
|
|
|
|
}
|