Refactor to use FeeHandler function
This commit is contained in:
parent
7fafa9e021
commit
39ee95abe4
|
@ -36,6 +36,9 @@ type BasecoinApp struct {
|
|||
|
||||
// Manage getting and setting accounts
|
||||
accountMapper sdk.AccountMapper
|
||||
|
||||
// Handle fees
|
||||
feeHandler sdk.FeeHandler
|
||||
}
|
||||
|
||||
func NewBasecoinApp(logger log.Logger, db dbm.DB) *BasecoinApp {
|
||||
|
@ -60,6 +63,9 @@ func NewBasecoinApp(logger log.Logger, db dbm.DB) *BasecoinApp {
|
|||
&types.AppAccount{}, // prototype
|
||||
).Seal()
|
||||
|
||||
// Define the feeHandler.
|
||||
app.feeHandler = func(ctx sdk.Context, fees sdk.Coins) {}
|
||||
|
||||
// Add handlers.
|
||||
coinKeeper := bank.NewCoinKeeper(app.accountMapper)
|
||||
ibcMapper := ibc.NewIBCMapper(app.cdc, app.capKeyIBCStore)
|
||||
|
@ -73,7 +79,7 @@ func NewBasecoinApp(logger log.Logger, db dbm.DB) *BasecoinApp {
|
|||
app.SetTxDecoder(app.txDecoder)
|
||||
app.SetInitChainer(app.initChainer)
|
||||
app.MountStoresIAVL(app.capKeyMainStore, app.capKeyAccountStore, app.capKeyIBCStore, app.capKeyStakingStore)
|
||||
app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper))
|
||||
app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper, app.feeHandler))
|
||||
err := app.LoadLatestVersion(app.capKeyMainStore)
|
||||
if err != nil {
|
||||
cmn.Exit(err.Error())
|
||||
|
|
|
@ -40,6 +40,9 @@ type DemocoinApp struct {
|
|||
|
||||
// Manage getting and setting accounts
|
||||
accountMapper sdk.AccountMapper
|
||||
|
||||
// Handle fees
|
||||
feeHandler sdk.FeeHandler
|
||||
}
|
||||
|
||||
func NewDemocoinApp(logger log.Logger, db dbm.DB) *DemocoinApp {
|
||||
|
@ -65,6 +68,9 @@ func NewDemocoinApp(logger log.Logger, db dbm.DB) *DemocoinApp {
|
|||
&types.AppAccount{}, // prototype
|
||||
).Seal()
|
||||
|
||||
// Define the feeHandler.
|
||||
app.feeHandler = func(ctx sdk.Context, fee sdk.Coins) {}
|
||||
|
||||
// Add handlers.
|
||||
coinKeeper := bank.NewCoinKeeper(app.accountMapper)
|
||||
coolKeeper := cool.NewKeeper(app.capKeyMainStore, coinKeeper)
|
||||
|
@ -83,7 +89,7 @@ func NewDemocoinApp(logger log.Logger, db dbm.DB) *DemocoinApp {
|
|||
app.SetTxDecoder(app.txDecoder)
|
||||
app.SetInitChainer(app.initChainerFn(coolKeeper, powKeeper))
|
||||
app.MountStoresIAVL(app.capKeyMainStore, app.capKeyAccountStore, app.capKeyPowStore, app.capKeyIBCStore, app.capKeyStakingStore)
|
||||
app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper))
|
||||
app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper, app.feeHandler))
|
||||
err := app.LoadLatestVersion(app.capKeyMainStore)
|
||||
if err != nil {
|
||||
cmn.Exit(err.Error())
|
||||
|
|
|
@ -48,8 +48,6 @@ type AccountMapper interface {
|
|||
NewAccountWithAddress(ctx Context, addr Address) Account
|
||||
GetAccount(ctx Context, addr Address) Account
|
||||
SetAccount(ctx Context, acc Account)
|
||||
GetFeePool(ctx Context) Coins
|
||||
SetFeePool(ctx Context, coins Coins)
|
||||
}
|
||||
|
||||
// AccountDecoder unmarshals account bytes
|
||||
|
|
|
@ -3,5 +3,8 @@ package types
|
|||
// core function variable which application runs for transactions
|
||||
type Handler func(ctx Context, msg Msg) Result
|
||||
|
||||
// core function variable which application runs to handle fees
|
||||
type FeeHandler func(ctx Context, fee Coins)
|
||||
|
||||
// If newCtx.IsZero(), ctx is used instead.
|
||||
type AnteHandler func(ctx Context, tx Tx) (newCtx Context, result Result, abort bool)
|
||||
|
|
|
@ -11,7 +11,7 @@ import (
|
|||
// NewAnteHandler returns an AnteHandler that checks
|
||||
// and increments sequence numbers, checks signatures,
|
||||
// and deducts fees from the first signer.
|
||||
func NewAnteHandler(accountMapper sdk.AccountMapper) sdk.AnteHandler {
|
||||
func NewAnteHandler(accountMapper sdk.AccountMapper, feeHandler sdk.FeeHandler) sdk.AnteHandler {
|
||||
return func(
|
||||
ctx sdk.Context, tx sdk.Tx,
|
||||
) (_ sdk.Context, _ sdk.Result, abort bool) {
|
||||
|
@ -74,9 +74,7 @@ func NewAnteHandler(accountMapper sdk.AccountMapper) sdk.AnteHandler {
|
|||
// TODO: min fee
|
||||
if !fee.Amount.IsZero() {
|
||||
signerAcc, res = deductFees(signerAcc, fee)
|
||||
pool := accountMapper.GetFeePool(ctx)
|
||||
pool = pool.Plus(fee.Amount)
|
||||
accountMapper.SetFeePool(ctx, pool)
|
||||
feeHandler(ctx, fee.Amount)
|
||||
if !res.IsOK() {
|
||||
return ctx, res, true
|
||||
}
|
||||
|
|
|
@ -12,6 +12,9 @@ import (
|
|||
wire "github.com/cosmos/cosmos-sdk/wire"
|
||||
)
|
||||
|
||||
func nopFeeHandler(ctx sdk.Context, fee sdk.Coins) {
|
||||
}
|
||||
|
||||
func newTestMsg(addrs ...sdk.Address) *sdk.TestMsg {
|
||||
return sdk.NewTestMsg(addrs...)
|
||||
}
|
||||
|
@ -72,7 +75,7 @@ func TestAnteHandlerSigErrors(t *testing.T) {
|
|||
cdc := wire.NewCodec()
|
||||
RegisterBaseAccount(cdc)
|
||||
mapper := NewAccountMapper(cdc, capKey, &BaseAccount{})
|
||||
anteHandler := NewAnteHandler(mapper)
|
||||
anteHandler := NewAnteHandler(mapper, nopFeeHandler)
|
||||
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil)
|
||||
|
||||
// keys and addresses
|
||||
|
@ -113,7 +116,7 @@ func TestAnteHandlerSequences(t *testing.T) {
|
|||
cdc := wire.NewCodec()
|
||||
RegisterBaseAccount(cdc)
|
||||
mapper := NewAccountMapper(cdc, capKey, &BaseAccount{})
|
||||
anteHandler := NewAnteHandler(mapper)
|
||||
anteHandler := NewAnteHandler(mapper, nopFeeHandler)
|
||||
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil)
|
||||
|
||||
// keys and addresses
|
||||
|
@ -179,7 +182,7 @@ func TestAnteHandlerFees(t *testing.T) {
|
|||
cdc := wire.NewCodec()
|
||||
RegisterBaseAccount(cdc)
|
||||
mapper := NewAccountMapper(cdc, capKey, &BaseAccount{})
|
||||
anteHandler := NewAnteHandler(mapper)
|
||||
anteHandler := NewAnteHandler(mapper, nopFeeHandler)
|
||||
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil)
|
||||
|
||||
// keys and addresses
|
||||
|
@ -216,7 +219,7 @@ func TestAnteHandlerBadSignBytes(t *testing.T) {
|
|||
cdc := wire.NewCodec()
|
||||
RegisterBaseAccount(cdc)
|
||||
mapper := NewAccountMapper(cdc, capKey, &BaseAccount{})
|
||||
anteHandler := NewAnteHandler(mapper)
|
||||
anteHandler := NewAnteHandler(mapper, nopFeeHandler)
|
||||
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil)
|
||||
|
||||
// keys and addresses
|
||||
|
@ -291,7 +294,7 @@ func TestAnteHandlerSetPubKey(t *testing.T) {
|
|||
cdc := wire.NewCodec()
|
||||
RegisterBaseAccount(cdc)
|
||||
mapper := NewAccountMapper(cdc, capKey, &BaseAccount{})
|
||||
anteHandler := NewAnteHandler(mapper)
|
||||
anteHandler := NewAnteHandler(mapper, nopFeeHandler)
|
||||
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil)
|
||||
|
||||
// keys and addresses
|
||||
|
|
|
@ -51,33 +51,6 @@ func (am accountMapper) Seal() sealedAccountMapper {
|
|||
return sealedAccountMapper{am}
|
||||
}
|
||||
|
||||
var feePoolKey = []byte("feePool")
|
||||
|
||||
// Implements sdk.AccountMapper.
|
||||
func (am accountMapper) GetFeePool(ctx sdk.Context) sdk.Coins {
|
||||
store := ctx.KVStore(am.key)
|
||||
bz := store.Get(feePoolKey)
|
||||
if bz == nil {
|
||||
return sdk.Coins{}
|
||||
}
|
||||
var coins sdk.Coins
|
||||
err := am.cdc.UnmarshalBinaryBare(bz, &coins)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return coins
|
||||
}
|
||||
|
||||
// Implements sdk.AccountMapper.
|
||||
func (am accountMapper) SetFeePool(ctx sdk.Context, coins sdk.Coins) {
|
||||
store := ctx.KVStore(am.key)
|
||||
bz, err := am.cdc.MarshalBinaryBare(coins)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
store.Set(feePoolKey, bz)
|
||||
}
|
||||
|
||||
// Implements sdk.AccountMapper.
|
||||
func (am accountMapper) NewAccountWithAddress(ctx sdk.Context, addr sdk.Address) sdk.Account {
|
||||
acc := am.clonePrototype()
|
||||
|
|
|
@ -71,19 +71,3 @@ func TestAccountMapperSealed(t *testing.T) {
|
|||
mapperSealed := mapper.Seal()
|
||||
assert.Panics(t, func() { mapperSealed.WireCodec() })
|
||||
}
|
||||
|
||||
func TestAccountMapperFeePool(t *testing.T) {
|
||||
ms, capKey := setupMultiStore()
|
||||
cdc := wire.NewCodec()
|
||||
ctx := sdk.NewContext(ms, abci.Header{}, false, nil)
|
||||
mapper := NewAccountMapper(cdc, capKey, &BaseAccount{})
|
||||
|
||||
// default empty
|
||||
pool := mapper.GetFeePool(ctx)
|
||||
assert.Equal(t, pool, sdk.Coins{})
|
||||
|
||||
// get after set
|
||||
mapper.SetFeePool(ctx, sdk.Coins{sdk.Coin{"doge", 1}})
|
||||
pool = mapper.GetFeePool(ctx)
|
||||
assert.Equal(t, pool, sdk.Coins{sdk.Coin{"doge", 1}})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue