Skip to content

Commit

Permalink
Merge PR #2545: Rename AccountMapper to AccountKeeper
Browse files Browse the repository at this point in the history
  • Loading branch information
cwgoes authored Oct 21, 2018
2 parents 17b8100 + 17cf2ac commit 3e22a36
Show file tree
Hide file tree
Showing 49 changed files with 205 additions and 204 deletions.
1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ BREAKING CHANGES
* [x/stake] \#2508 Utilize Tendermint power for validator power key
* [x/stake] \#2531 Remove all inflation logic
* [x/mint] \#2531 Add minting module and inflation logic
* [x/auth] [\#2540](https://github.com/cosmos/cosmos-sdk/issues/2540) Rename `AccountMapper` to `AccountKeeper`.

* Tendermint
* Update tendermint version from v0.23.0 to v0.25.0, notable changes
Expand Down
16 changes: 8 additions & 8 deletions cmd/gaia/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type GaiaApp struct {
tkeyParams *sdk.TransientStoreKey

// Manage getting and setting accounts
accountMapper auth.AccountMapper
accountKeeper auth.AccountKeeper
feeCollectionKeeper auth.FeeCollectionKeeper
bankKeeper bank.Keeper
stakeKeeper stake.Keeper
Expand Down Expand Up @@ -91,15 +91,15 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptio
tkeyParams: sdk.NewTransientStoreKey("transient_params"),
}

// define the accountMapper
app.accountMapper = auth.NewAccountMapper(
// define the accountKeeper
app.accountKeeper = auth.NewAccountKeeper(
app.cdc,
app.keyAccount, // target store
auth.ProtoBaseAccount, // prototype
)

// add handlers
app.bankKeeper = bank.NewBaseKeeper(app.accountMapper)
app.bankKeeper = bank.NewBaseKeeper(app.accountKeeper)
app.feeCollectionKeeper = auth.NewFeeCollectionKeeper(
app.cdc,
app.keyFeeCollection,
Expand Down Expand Up @@ -159,7 +159,7 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptio
app.keySlashing, app.keyGov, app.keyFeeCollection, app.keyParams)
app.SetInitChainer(app.initChainer)
app.SetBeginBlocker(app.BeginBlocker)
app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper, app.feeCollectionKeeper))
app.SetAnteHandler(auth.NewAnteHandler(app.accountKeeper, app.feeCollectionKeeper))
app.MountStoresTransient(app.tkeyParams, app.tkeyStake, app.tkeyDistr)
app.SetEndBlocker(app.EndBlocker)

Expand Down Expand Up @@ -231,8 +231,8 @@ func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci
// load the accounts
for _, gacc := range genesisState.Accounts {
acc := gacc.ToAccount()
acc.AccountNumber = app.accountMapper.GetNextAccountNumber(ctx)
app.accountMapper.SetAccount(ctx, acc)
acc.AccountNumber = app.accountKeeper.GetNextAccountNumber(ctx)
app.accountKeeper.SetAccount(ctx, acc)
}

// load the initial stake information
Expand Down Expand Up @@ -299,7 +299,7 @@ func (app *GaiaApp) ExportAppStateAndValidators() (appState json.RawMessage, val
accounts = append(accounts, account)
return false
}
app.accountMapper.IterateAccounts(ctx, appendAccount)
app.accountKeeper.IterateAccounts(ctx, appendAccount)
genState := NewGenesisState(
accounts,
stake.WriteGenesis(ctx, app.stakeKeeper),
Expand Down
14 changes: 7 additions & 7 deletions cmd/gaia/app/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,23 @@ func appStateFn(r *rand.Rand, accs []simulation.Account) json.RawMessage {

func testAndRunTxs(app *GaiaApp) []simulation.WeightedOperation {
return []simulation.WeightedOperation{
{100, banksim.SingleInputSendMsg(app.accountMapper, app.bankKeeper)},
{100, banksim.SingleInputSendMsg(app.accountKeeper, app.bankKeeper)},
{5, govsim.SimulateSubmittingVotingAndSlashingForProposal(app.govKeeper, app.stakeKeeper)},
{100, govsim.SimulateMsgDeposit(app.govKeeper, app.stakeKeeper)},
{100, stakesim.SimulateMsgCreateValidator(app.accountMapper, app.stakeKeeper)},
{100, stakesim.SimulateMsgCreateValidator(app.accountKeeper, app.stakeKeeper)},
{5, stakesim.SimulateMsgEditValidator(app.stakeKeeper)},
{100, stakesim.SimulateMsgDelegate(app.accountMapper, app.stakeKeeper)},
{100, stakesim.SimulateMsgBeginUnbonding(app.accountMapper, app.stakeKeeper)},
{100, stakesim.SimulateMsgBeginRedelegate(app.accountMapper, app.stakeKeeper)},
{100, stakesim.SimulateMsgDelegate(app.accountKeeper, app.stakeKeeper)},
{100, stakesim.SimulateMsgBeginUnbonding(app.accountKeeper, app.stakeKeeper)},
{100, stakesim.SimulateMsgBeginRedelegate(app.accountKeeper, app.stakeKeeper)},
{100, slashingsim.SimulateMsgUnjail(app.slashingKeeper)},
}
}

func invariants(app *GaiaApp) []simulation.Invariant {
return []simulation.Invariant{
banksim.NonnegativeBalanceInvariant(app.accountMapper),
banksim.NonnegativeBalanceInvariant(app.accountKeeper),
govsim.AllInvariants(),
stakesim.AllInvariants(app.bankKeeper, app.stakeKeeper, app.accountMapper),
stakesim.AllInvariants(app.bankKeeper, app.stakeKeeper, app.accountKeeper),
slashingsim.AllInvariants(),
}
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/gaia/cmd/gaiadebug/hack.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ type GaiaApp struct {
tkeyParams *sdk.TransientStoreKey

// Manage getting and setting accounts
accountMapper auth.AccountMapper
accountKeeper auth.AccountKeeper
feeCollectionKeeper auth.FeeCollectionKeeper
bankKeeper bank.Keeper
stakeKeeper stake.Keeper
Expand All @@ -165,15 +165,15 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*bam.BaseAp
tkeyParams: sdk.NewTransientStoreKey("transient_params"),
}

// define the accountMapper
app.accountMapper = auth.NewAccountMapper(
// define the accountKeeper
app.accountKeeper = auth.NewAccountKeeper(
app.cdc,
app.keyAccount, // target store
auth.ProtoBaseAccount, // prototype
)

// add handlers
app.bankKeeper = bank.NewBaseKeeper(app.accountMapper)
app.bankKeeper = bank.NewBaseKeeper(app.accountKeeper)
app.paramsKeeper = params.NewKeeper(app.cdc, app.keyParams, app.tkeyParams)
app.stakeKeeper = stake.NewKeeper(app.cdc, app.keyStake, app.tkeyStake, app.bankKeeper, app.paramsKeeper.Subspace(stake.DefaultParamspace), app.RegisterCodespace(stake.DefaultCodespace))
app.slashingKeeper = slashing.NewKeeper(app.cdc, app.keySlashing, app.stakeKeeper, app.paramsKeeper.Subspace(slashing.DefaultParamspace), app.RegisterCodespace(slashing.DefaultCodespace))
Expand All @@ -187,7 +187,7 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*bam.BaseAp
app.SetInitChainer(app.initChainer)
app.SetBeginBlocker(app.BeginBlocker)
app.SetEndBlocker(app.EndBlocker)
app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper, app.feeCollectionKeeper))
app.SetAnteHandler(auth.NewAnteHandler(app.accountKeeper, app.feeCollectionKeeper))
app.MountStoresIAVL(app.keyMain, app.keyAccount, app.keyStake, app.keySlashing, app.keyParams)
app.MountStore(app.tkeyParams, sdk.StoreTypeTransient)
err := app.LoadLatestVersion(app.keyMain)
Expand Down Expand Up @@ -246,7 +246,7 @@ func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci
// load the accounts
for _, gacc := range genesisState.Accounts {
acc := gacc.ToAccount()
app.accountMapper.SetAccount(ctx, acc)
app.accountKeeper.SetAccount(ctx, acc)
}

// load the initial stake information
Expand Down
46 changes: 23 additions & 23 deletions docs/sdk/core/app3.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ type BaseAccount struct {

It simply contains a field for each of the methods.

### AccountMapper
### AccountKeeper

In previous apps using our `appAccount`, we handled
marshaling/unmarshaling the account from the store ourselves, by performing
Expand All @@ -95,36 +95,36 @@ to work with in our applications. In the SDK, we use the term `Mapper` to refer
to an abstaction over a KVStore that handles marshalling and unmarshalling a
particular data type to and from the underlying store.

The `x/auth` module provides an `AccountMapper` that allows us to get and
The `x/auth` module provides an `AccountKeeper` that allows us to get and
set `Account` types to the store. Note the benefit of using the `Account`
interface here - developers can implement their own account type that extends
the `BaseAccount` to store additional data without requiring another lookup from
the store.

Creating an AccountMapper is easy - we just need to specify a codec, a
Creating an AccountKeeper is easy - we just need to specify a codec, a
capability key, and a prototype of the object being encoded

```go
accountMapper := auth.NewAccountMapper(cdc, keyAccount, auth.ProtoBaseAccount)
accountKeeper := auth.NewAccountKeeper(cdc, keyAccount, auth.ProtoBaseAccount)
```

Then we can get, modify, and set accounts. For instance, we could double the
amount of coins in an account:

```go
acc := accountMapper.GetAccount(ctx, addr)
acc := accountKeeper.GetAccount(ctx, addr)
acc.SetCoins(acc.Coins.Plus(acc.Coins))
accountMapper.SetAccount(ctx, addr)
accountKeeper.SetAccount(ctx, addr)
```

Note that the `AccountMapper` takes a `Context` as the first argument, and will
Note that the `AccountKeeper` takes a `Context` as the first argument, and will
load the KVStore from there using the capability key it was granted on creation.

Also note that you must explicitly call `SetAccount` after mutating an account
for the change to persist!

See the [AccountMapper API
docs](https://godoc.org/github.com/cosmos/cosmos-sdk/x/auth#AccountMapper) for more information.
See the [AccountKeeper API
docs](https://godoc.org/github.com/cosmos/cosmos-sdk/x/auth#AccountKeeper) for more information.

## StdTx

Expand Down Expand Up @@ -224,10 +224,10 @@ all the relevant information.
As we saw in `App2`, we can use an `AnteHandler` to authenticate transactions
before we handle any of their internal messages. While previously we implemented
our own simple `AnteHandler`, the `x/auth` module provides a much more advanced
one that uses `AccountMapper` and works with `StdTx`:
one that uses `AccountKeeper` and works with `StdTx`:

```go
app.SetAnteHandler(auth.NewAnteHandler(accountMapper, feeKeeper))
app.SetAnteHandler(auth.NewAnteHandler(accountKeeper, feeKeeper))
```

The AnteHandler provided by `x/auth` enforces the following rules:
Expand Down Expand Up @@ -263,7 +263,7 @@ The fee is paid by the first address returned by `msg.GetSigners()` for the firs

## CoinKeeper

Now that we've seen the `auth.AccountMapper` and how its used to build a
Now that we've seen the `auth.AccountKeeper` and how its used to build a
complete AnteHandler, it's time to look at how to build higher-level
abstractions for taking action on accounts.

Expand All @@ -274,26 +274,26 @@ which expose only limitted functionality on the underlying types stored by the `

For instance, the `x/bank` module defines the canonical versions of `MsgSend`
and `MsgIssue` for the SDK, as well as a `Handler` for processing them. However,
rather than passing a `KVStore` or even an `AccountMapper` directly to the handler,
rather than passing a `KVStore` or even an `AccountKeeper` directly to the handler,
we introduce a `bank.Keeper`, which can only be used to transfer coins in and out of accounts.
This allows us to determine up front that the only effect the bank module's
`Handler` can have on the store is to change the amount of coins in an account -
it can't increment sequence numbers, change PubKeys, or otherwise.


A `bank.Keeper` is easily instantiated from an `AccountMapper`:
A `bank.Keeper` is easily instantiated from an `AccountKeeper`:

```go
bankKeeper = bank.NewBaseKeeper(accountMapper)
bankKeeper = bank.NewBaseKeeper(accountKeeper)
```

We can then use it within a handler, instead of working directly with the
`AccountMapper`. For instance, to add coins to an account:
`AccountKeeper`. For instance, to add coins to an account:

```go
// Finds account with addr in AccountMapper.
// Finds account with addr in AccountKeeper.
// Adds coins to account's coin array.
// Sets updated account in AccountMapper
// Sets updated account in AccountKeeper
app.bankKeeper.AddCoins(ctx, addr, coins)
```

Expand All @@ -311,12 +311,12 @@ accounts.
We use this `Keeper` paradigm extensively in the SDK as the way to define what
kind of functionality each module gets access to. In particular, we try to
follow the *principle of least authority*.
Rather than providing full blown access to the `KVStore` or the `AccountMapper`,
Rather than providing full blown access to the `KVStore` or the `AccountKeeper`,
we restrict access to a small number of functions that do very specific things.

## App3

With the `auth.AccountMapper` and `bank.Keeper` in hand,
With the `auth.AccountKeeper` and `bank.Keeper` in hand,
we're now ready to build `App3`.
The `x/auth` and `x/bank` modules do all the heavy lifting:

Expand All @@ -334,11 +334,11 @@ func NewApp3(logger log.Logger, db dbm.DB) *bapp.BaseApp {
keyFees := sdk.NewKVStoreKey("fee") // TODO

// Set various mappers/keepers to interact easily with underlying stores
accountMapper := auth.NewAccountMapper(cdc, keyAccount, auth.ProtoBaseAccount)
bankKeeper := bank.NewBaseKeeper(accountMapper)
accountKeeper := auth.NewAccountKeeper(cdc, keyAccount, auth.ProtoBaseAccount)
bankKeeper := bank.NewBaseKeeper(accountKeeper)
feeKeeper := auth.NewFeeCollectionKeeper(cdc, keyFees)

app.SetAnteHandler(auth.NewAnteHandler(accountMapper, feeKeeper))
app.SetAnteHandler(auth.NewAnteHandler(accountKeeper, feeKeeper))

// Register message routes.
// Note the handler gets access to
Expand Down
6 changes: 3 additions & 3 deletions docs/sdk/core/examples/app3.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ func NewApp3(logger log.Logger, db dbm.DB) *bapp.BaseApp {
keyFees := sdk.NewKVStoreKey("fee") // TODO

// Set various mappers/keepers to interact easily with underlying stores
accountMapper := auth.NewAccountMapper(cdc, keyAccount, auth.ProtoBaseAccount)
bankKeeper := bank.NewBaseKeeper(accountMapper)
accountKeeper := auth.NewAccountKeeper(cdc, keyAccount, auth.ProtoBaseAccount)
bankKeeper := bank.NewBaseKeeper(accountKeeper)
feeKeeper := auth.NewFeeCollectionKeeper(cdc, keyFees)

app.SetAnteHandler(auth.NewAnteHandler(accountMapper, feeKeeper))
app.SetAnteHandler(auth.NewAnteHandler(accountKeeper, feeKeeper))

// Register message routes.
// Note the handler gets access to
Expand Down
14 changes: 7 additions & 7 deletions docs/sdk/core/examples/app4.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ func NewApp4(logger log.Logger, db dbm.DB) *bapp.BaseApp {
keyAccount := sdk.NewKVStoreKey("acc")

// Set various mappers/keepers to interact easily with underlying stores
accountMapper := auth.NewAccountMapper(cdc, keyAccount, auth.ProtoBaseAccount)
bankKeeper := bank.NewBaseKeeper(accountMapper)
accountKeeper := auth.NewAccountKeeper(cdc, keyAccount, auth.ProtoBaseAccount)
bankKeeper := bank.NewBaseKeeper(accountKeeper)

// TODO
keyFees := sdk.NewKVStoreKey("fee")
feeKeeper := auth.NewFeeCollectionKeeper(cdc, keyFees)

app.SetAnteHandler(auth.NewAnteHandler(accountMapper, feeKeeper))
app.SetAnteHandler(auth.NewAnteHandler(accountKeeper, feeKeeper))

// Set InitChainer
app.SetInitChainer(NewInitChainer(cdc, accountMapper))
app.SetInitChainer(NewInitChainer(cdc, accountKeeper))

// Register message routes.
// Note the handler gets access to the account store.
Expand Down Expand Up @@ -76,7 +76,7 @@ func (ga *GenesisAccount) ToAccount() (acc *auth.BaseAccount, err error) {

// InitChainer will set initial balances for accounts as well as initial coin metadata
// MsgIssue can no longer be used to create new coin
func NewInitChainer(cdc *codec.Codec, accountMapper auth.AccountMapper) sdk.InitChainer {
func NewInitChainer(cdc *codec.Codec, accountKeeper auth.AccountKeeper) sdk.InitChainer {
return func(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
stateJSON := req.AppStateBytes

Expand All @@ -91,8 +91,8 @@ func NewInitChainer(cdc *codec.Codec, accountMapper auth.AccountMapper) sdk.Init
if err != nil {
panic(err)
}
acc.AccountNumber = accountMapper.GetNextAccountNumber(ctx)
accountMapper.SetAccount(ctx, acc)
acc.AccountNumber = accountKeeper.GetNextAccountNumber(ctx)
accountKeeper.SetAccount(ctx, acc)
}

return abci.ResponseInitChain{}
Expand Down
2 changes: 1 addition & 1 deletion docs/sdk/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (app *BasecoinApp) initRouterHandlers() {

// All handlers must be added here.
// The order matters.
app.router.AddRoute("bank", bank.NewHandler(app.accountMapper))
app.router.AddRoute("bank", bank.NewHandler(app.accountKeeper))
app.router.AddRoute("sketchy", sketchy.NewHandler())
}
```
Expand Down
6 changes: 3 additions & 3 deletions docs/sdk/sdk-by-examples/simple-governance/bridging-it-all.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type SimpleGovApp struct {
simpleGovKeeper simpleGov.Keeper

// Manage getting and setting accounts
accountMapper auth.AccountMapper
accountKeeper auth.AccountKeeper
}
```

Expand Down Expand Up @@ -206,7 +206,7 @@ var cdc = MakeCodec()
- Instantiate the keepers. Note that keepers generally need access to other module's keepers. In this case, make sure you only pass an instance of the keeper for the functionality that is needed. If a keeper only needs to read in another module's store, a read-only keeper should be passed to it.

```go
app.bankKeeper = bank.NewBaseKeeper(app.accountMapper)
app.bankKeeper = bank.NewBaseKeeper(app.accountKeeper)
app.stakeKeeper = simplestake.NewKeeper(app.capKeyStakingStore, app.bankKeeper,app.RegisterCodespace(simplestake.DefaultCodespace))
app.simpleGovKeeper = simpleGov.NewKeeper(app.capKeySimpleGovStore, app.bankKeeper, app.stakeKeeper, app.RegisterCodespace(simpleGov.DefaultCodespace))
```
Expand All @@ -225,7 +225,7 @@ app.Router().
```go
// Initialize BaseApp.
app.MountStoresIAVL(app.capKeyMainStore, app.capKeyAccountStore, app.capKeySimpleGovStore, app.capKeyStakingStore)
app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper, app.feeCollectionKeeper))
app.SetAnteHandler(auth.NewAnteHandler(app.accountKeeper, app.feeCollectionKeeper))
err := app.LoadLatestVersion(app.capKeyMainStore)
if err != nil {
cmn.Exit(err.Error())
Expand Down
Loading

0 comments on commit 3e22a36

Please sign in to comment.