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
evm: balance and nonce invariants #661
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported" | ||
|
||
ethcmn "github.com/ethereum/go-ethereum/common" | ||
|
||
|
@@ -20,7 +21,6 @@ func InitGenesis(ctx sdk.Context, k Keeper, accountKeeper types.AccountKeeper, d | |
for _, account := range data.Accounts { | ||
address := ethcmn.HexToAddress(account.Address) | ||
accAddress := sdk.AccAddress(address.Bytes()) | ||
|
||
// check that the EVM balance the matches the account balance | ||
acc := accountKeeper.GetAccount(ctx, accAddress) | ||
if acc == nil { | ||
|
@@ -37,17 +37,11 @@ func InitGenesis(ctx sdk.Context, k Keeper, accountKeeper types.AccountKeeper, d | |
} | ||
|
||
evmBalance := acc.GetCoins().AmountOf(evmDenom) | ||
if !evmBalance.Equal(account.Balance) { | ||
panic( | ||
fmt.Errorf( | ||
"balance mismatch for account %s, expected %s%s, got %s%s", | ||
account.Address, evmBalance, evmDenom, account.Balance, evmDenom, | ||
), | ||
) | ||
} | ||
|
||
k.SetBalance(ctx, address, account.Balance.BigInt()) | ||
k.SetNonce(ctx, address, acc.GetSequence()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this setter was missing |
||
k.SetBalance(ctx, address, evmBalance.BigInt()) | ||
k.SetCode(ctx, address, account.Code) | ||
|
||
for _, storage := range account.Storage { | ||
k.SetState(ctx, address, storage.Key, storage.Value) | ||
} | ||
|
@@ -83,12 +77,11 @@ func InitGenesis(ctx sdk.Context, k Keeper, accountKeeper types.AccountKeeper, d | |
func ExportGenesis(ctx sdk.Context, k Keeper, ak types.AccountKeeper) GenesisState { | ||
// nolint: prealloc | ||
var ethGenAccounts []types.GenesisAccount | ||
accounts := ak.GetAllAccounts(ctx) | ||
|
||
for _, account := range accounts { | ||
ak.IterateAccounts(ctx, func(account authexported.Account) bool { | ||
ethAccount, ok := account.(*ethermint.EthAccount) | ||
if !ok { | ||
continue | ||
// ignore non EthAccounts | ||
return false | ||
} | ||
|
||
addr := ethAccount.EthAddress() | ||
|
@@ -98,18 +91,15 @@ func ExportGenesis(ctx sdk.Context, k Keeper, ak types.AccountKeeper) GenesisSta | |
panic(err) | ||
} | ||
|
||
balanceInt := k.GetBalance(ctx, addr) | ||
balance := sdk.NewIntFromBigInt(balanceInt) | ||
|
||
genAccount := types.GenesisAccount{ | ||
Address: addr.String(), | ||
Balance: balance, | ||
Code: k.GetCode(ctx, addr), | ||
Storage: storage, | ||
} | ||
|
||
ethGenAccounts = append(ethGenAccounts, genAccount) | ||
} | ||
return false | ||
}) | ||
|
||
config, _ := k.GetChainConfig(ctx) | ||
|
||
|
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) | ||
araskachoi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}) | ||
} | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved this check to the invariant