Merge PR #2442: Support removal of accounts in account mapper

This commit is contained in:
Alexander Bezobchuk 2018-10-04 14:20:31 +00:00 committed by Christopher Goes
parent f6e3e61e07
commit 2e040dd971
3 changed files with 46 additions and 0 deletions

View File

@ -111,6 +111,7 @@ FEATURES
* [x/auth] \#2376 Remove FeePayer() from StdTx
* [x/stake] [\#1672](https://github.com/cosmos/cosmos-sdk/issues/1672) Implement
basis for the validator commission model.
* [x/auth] Support account removal in the account mapper.
* Tendermint

View File

@ -83,6 +83,13 @@ func (am AccountMapper) SetAccount(ctx sdk.Context, acc Account) {
store.Set(AddressStoreKey(addr), bz)
}
// RemoveAccount removes an account for the account mapper store.
func (am AccountMapper) RemoveAccount(ctx sdk.Context, acc Account) {
addr := acc.GetAddress()
store := ctx.KVStore(am.key)
store.Delete(AddressStoreKey(addr))
}
// Implements sdk.AccountMapper.
func (am AccountMapper) IterateAccounts(ctx sdk.Context, process func(Account) (stop bool)) {
store := ctx.KVStore(am.key)

View File

@ -61,6 +61,44 @@ func TestAccountMapperGetSet(t *testing.T) {
require.Equal(t, newSequence, acc.GetSequence())
}
func TestAccountMapperRemoveAccount(t *testing.T) {
ms, capKey, _ := setupMultiStore()
cdc := codec.New()
RegisterBaseAccount(cdc)
// make context and mapper
ctx := sdk.NewContext(ms, abci.Header{}, false, log.NewNopLogger())
mapper := NewAccountMapper(cdc, capKey, ProtoBaseAccount)
addr1 := sdk.AccAddress([]byte("addr1"))
addr2 := sdk.AccAddress([]byte("addr2"))
// create accounts
acc1 := mapper.NewAccountWithAddress(ctx, addr1)
acc2 := mapper.NewAccountWithAddress(ctx, addr2)
accSeq1 := int64(20)
accSeq2 := int64(40)
acc1.SetSequence(accSeq1)
acc2.SetSequence(accSeq2)
mapper.SetAccount(ctx, acc1)
mapper.SetAccount(ctx, acc2)
acc1 = mapper.GetAccount(ctx, addr1)
require.NotNil(t, acc1)
require.Equal(t, accSeq1, acc1.GetSequence())
// remove one account
mapper.RemoveAccount(ctx, acc1)
acc1 = mapper.GetAccount(ctx, addr1)
require.Nil(t, acc1)
acc2 = mapper.GetAccount(ctx, addr2)
require.NotNil(t, acc2)
require.Equal(t, accSeq2, acc2.GetSequence())
}
func BenchmarkAccountMapperGetAccountFound(b *testing.B) {
ms, capKey, _ := setupMultiStore()
cdc := codec.New()