This repository has been archived by the owner on Nov 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
evm: balance and nonce invariants (#661)
* evm: balance and nonce invariants * nonce invariant * changelog * use iterator on export
- Loading branch information
Showing
12 changed files
with
263 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package keeper | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported" | ||
|
||
ethermint "github.com/cosmos/ethermint/types" | ||
"github.com/cosmos/ethermint/x/evm/types" | ||
) | ||
|
||
const ( | ||
balanceInvariant = "balance" | ||
nonceInvariant = "nonce" | ||
) | ||
|
||
// RegisterInvariants registers the evm module invariants | ||
func RegisterInvariants(ir sdk.InvariantRegistry, k Keeper) { | ||
ir.RegisterRoute(types.ModuleName, balanceInvariant, k.BalanceInvariant()) | ||
ir.RegisterRoute(types.ModuleName, nonceInvariant, k.NonceInvariant()) | ||
} | ||
|
||
// BalanceInvariant checks that all auth module's EthAccounts in the application have the same balance | ||
// as the EVM one. | ||
func (k Keeper) BalanceInvariant() sdk.Invariant { | ||
return func(ctx sdk.Context) (string, bool) { | ||
var ( | ||
msg string | ||
count int | ||
) | ||
|
||
k.accountKeeper.IterateAccounts(ctx, func(account authexported.Account) bool { | ||
ethAccount, ok := account.(*ethermint.EthAccount) | ||
if !ok { | ||
// ignore non EthAccounts | ||
return false | ||
} | ||
|
||
evmDenom := k.GetParams(ctx).EvmDenom | ||
accountBalance := ethAccount.GetCoins().AmountOf(evmDenom) | ||
evmBalance := k.GetBalance(ctx, ethAccount.EthAddress()) | ||
|
||
if evmBalance.Cmp(accountBalance.BigInt()) != 0 { | ||
count++ | ||
msg += fmt.Sprintf( | ||
"\tbalance mismatch for address %s: account balance %s, evm balance %s\n", | ||
account.GetAddress(), accountBalance.String(), evmBalance.String(), | ||
) | ||
} | ||
|
||
return false | ||
}) | ||
|
||
broken := count != 0 | ||
|
||
return sdk.FormatInvariant( | ||
types.ModuleName, balanceInvariant, | ||
fmt.Sprintf("account balances mismatches found %d\n%s", count, msg), | ||
), broken | ||
} | ||
} | ||
|
||
// NonceInvariant checks that all auth module's EthAccounts in the application have the same nonce | ||
// sequence as the EVM. | ||
func (k Keeper) NonceInvariant() sdk.Invariant { | ||
return func(ctx sdk.Context) (string, bool) { | ||
var ( | ||
msg string | ||
count int | ||
) | ||
|
||
k.accountKeeper.IterateAccounts(ctx, func(account authexported.Account) bool { | ||
ethAccount, ok := account.(*ethermint.EthAccount) | ||
if !ok { | ||
// ignore non EthAccounts | ||
return false | ||
} | ||
|
||
evmNonce := k.GetNonce(ctx, ethAccount.EthAddress()) | ||
|
||
if evmNonce != ethAccount.Sequence { | ||
count++ | ||
msg += fmt.Sprintf( | ||
"\nonce mismatch for address %s: account nonce %d, evm nonce %d\n", | ||
account.GetAddress(), ethAccount.Sequence, evmNonce, | ||
) | ||
} | ||
|
||
return false | ||
}) | ||
|
||
broken := count != 0 | ||
|
||
return sdk.FormatInvariant( | ||
types.ModuleName, nonceInvariant, | ||
fmt.Sprintf("account nonces mismatches found %d\n%s", count, msg), | ||
), broken | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"math/big" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
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) | ||
err := acc.SetCoins(sdk.NewCoins(ethermint.NewPhotonCoinInt64(1))) | ||
suite.Require().NoError(err) | ||
suite.app.AccountKeeper.SetAccount(suite.ctx, acc) | ||
|
||
suite.app.EvmKeeper.SetBalance(suite.ctx, address, big.NewInt(1000)) | ||
}, | ||
true, | ||
}, | ||
{ | ||
"balance ok", | ||
func() { | ||
acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, address.Bytes()) | ||
suite.Require().NotNil(acc) | ||
err := acc.SetCoins(sdk.NewCoins(ethermint.NewPhotonCoinInt64(1))) | ||
suite.Require().NoError(err) | ||
suite.app.AccountKeeper.SetAccount(suite.ctx, acc) | ||
|
||
suite.app.EvmKeeper.SetBalance(suite.ctx, address, big.NewInt(1)) | ||
}, | ||
false, | ||
}, | ||
{ | ||
"invalid account type", | ||
func() { | ||
acc := authtypes.NewBaseAccountWithAddress(address.Bytes()) | ||
suite.app.AccountKeeper.SetAccount(suite.ctx, &acc) | ||
}, | ||
false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
suite.Run(tc.name, func() { | ||
suite.SetupTest() // reset values | ||
|
||
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) | ||
|
||
suite.app.EvmKeeper.SetNonce(suite.ctx, address, 100) | ||
}, | ||
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) | ||
|
||
suite.app.EvmKeeper.SetNonce(suite.ctx, address, 1) | ||
}, | ||
false, | ||
}, | ||
{ | ||
"invalid account type", | ||
func() { | ||
acc := authtypes.NewBaseAccountWithAddress(address.Bytes()) | ||
suite.app.AccountKeeper.SetAccount(suite.ctx, &acc) | ||
}, | ||
false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
suite.Run(tc.name, func() { | ||
suite.SetupTest() // reset values | ||
|
||
tc.malleate() | ||
|
||
_, broken := suite.app.EvmKeeper.NonceInvariant()(suite.ctx) | ||
if tc.expBroken { | ||
suite.Require().True(broken) | ||
} else { | ||
suite.Require().False(broken) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.