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 1 commit
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,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 +36,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) | ||
} | ||
|
@@ -88,6 +81,7 @@ func ExportGenesis(ctx sdk.Context, k Keeper, ak types.AccountKeeper) GenesisSta | |
for _, account := range accounts { | ||
ethAccount, ok := account.(*ethermint.EthAccount) | ||
if !ok { | ||
// ignore non EthAccounts | ||
continue | ||
} | ||
|
||
|
@@ -98,12 +92,8 @@ 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, | ||
} | ||
|
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,58 @@ | ||
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" | ||
|
||
// RegisterInvariants registers the evm module invariants | ||
func RegisterInvariants(ir sdk.InvariantRegistry, k Keeper) { | ||
ir.RegisterRoute(types.ModuleName, balanceInvariant, k.BalanceInvariant()) | ||
} | ||
|
||
// 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("amount balances 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,76 @@ | ||
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) | ||
} | ||
}) | ||
} | ||
} |
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
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
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