2020-12-15 18:43:06 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
|
|
"github.com/cosmos/ethermint/crypto/ethsecp256k1"
|
|
|
|
ethermint "github.com/cosmos/ethermint/types"
|
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) TestBalanceInvariant() {
|
|
|
|
privkey, err := ethsecp256k1.GenerateKey()
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
address := ethcmn.HexToAddress(privkey.PubKey().Address().String())
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
malleate func()
|
|
|
|
expBroken bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"balance mismatch",
|
|
|
|
func() {
|
|
|
|
acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, address.Bytes())
|
|
|
|
suite.Require().NotNil(acc)
|
2021-04-18 16:39:15 +00:00
|
|
|
suite.app.BankKeeper.SetBalance(suite.ctx, acc.GetAddress(), ethermint.NewPhotonCoinInt64(1))
|
2020-12-15 18:43:06 +00:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
suite.app.EvmKeeper.CommitStateDB.SetBalance(address, big.NewInt(1000))
|
2020-12-15 18:43:06 +00:00
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"balance ok",
|
|
|
|
func() {
|
|
|
|
acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, address.Bytes())
|
|
|
|
suite.Require().NotNil(acc)
|
2021-04-18 16:39:15 +00:00
|
|
|
suite.app.BankKeeper.SetBalance(suite.ctx, acc.GetAddress(), ethermint.NewPhotonCoinInt64(1))
|
2020-12-15 18:43:06 +00:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
suite.app.EvmKeeper.CommitStateDB.SetBalance(address, big.NewInt(1))
|
2020-12-15 18:43:06 +00:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"invalid account type",
|
|
|
|
func() {
|
|
|
|
acc := authtypes.NewBaseAccountWithAddress(address.Bytes())
|
2021-04-17 10:00:07 +00:00
|
|
|
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
|
2020-12-15 18:43:06 +00:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(tc.name, func() {
|
|
|
|
suite.SetupTest() // reset values
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
suite.app.EvmKeeper.CommitStateDB.WithContext(suite.ctx)
|
2020-12-15 18:43:06 +00:00
|
|
|
tc.malleate()
|
|
|
|
|
|
|
|
_, broken := suite.app.EvmKeeper.BalanceInvariant()(suite.ctx)
|
|
|
|
if tc.expBroken {
|
|
|
|
suite.Require().True(broken)
|
|
|
|
} else {
|
|
|
|
suite.Require().False(broken)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) TestNonceInvariant() {
|
|
|
|
privkey, err := ethsecp256k1.GenerateKey()
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
address := ethcmn.HexToAddress(privkey.PubKey().Address().String())
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
malleate func()
|
|
|
|
expBroken bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"nonce mismatch",
|
|
|
|
func() {
|
|
|
|
acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, address.Bytes())
|
|
|
|
suite.Require().NotNil(acc)
|
|
|
|
err := acc.SetSequence(1)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
suite.app.EvmKeeper.CommitStateDB.SetNonce(address, 100)
|
2020-12-15 18:43:06 +00:00
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"nonce ok",
|
|
|
|
func() {
|
|
|
|
acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, address.Bytes())
|
|
|
|
suite.Require().NotNil(acc)
|
|
|
|
err := acc.SetSequence(1)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
suite.app.EvmKeeper.CommitStateDB.SetNonce(address, 1)
|
2020-12-15 18:43:06 +00:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"invalid account type",
|
|
|
|
func() {
|
|
|
|
acc := authtypes.NewBaseAccountWithAddress(address.Bytes())
|
2021-04-17 10:00:07 +00:00
|
|
|
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
|
2020-12-15 18:43:06 +00:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(tc.name, func() {
|
|
|
|
suite.SetupTest() // reset values
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
suite.app.EvmKeeper.CommitStateDB.WithContext(suite.ctx)
|
2020-12-15 18:43:06 +00:00
|
|
|
tc.malleate()
|
|
|
|
|
|
|
|
_, broken := suite.app.EvmKeeper.NonceInvariant()(suite.ctx)
|
|
|
|
if tc.expBroken {
|
|
|
|
suite.Require().True(broken)
|
|
|
|
} else {
|
|
|
|
suite.Require().False(broken)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|