Context-accessible logger field

This commit is contained in:
Christopher Goes 2018-04-23 12:35:09 +02:00
parent a674d75016
commit 62fc3e2c49
No known key found for this signature in database
GPG Key ID: E828D98232D328D3
13 changed files with 43 additions and 25 deletions

View File

@ -27,6 +27,7 @@ FEATURES:
* Create genesis transactions with `gaiad init gen-tx`
* New genesis account keys are automatically added to the client keybase (introduce `--client-home` flag)
* Initialize with genesis txs using `--gen-txs` flag
* Context now has access to the applicaiton logger
BREAKING CHANGES

View File

@ -210,9 +210,9 @@ func (app *BaseApp) initFromStore(mainKey sdk.StoreKey) error {
// NewContext returns a new Context with the correct store, the given header, and nil txBytes.
func (app *BaseApp) NewContext(isCheckTx bool, header abci.Header) sdk.Context {
if isCheckTx {
return sdk.NewContext(app.checkState.ms, header, true, nil)
return sdk.NewContext(app.checkState.ms, header, true, nil, log.NewNopLogger())
}
return sdk.NewContext(app.deliverState.ms, header, false, nil)
return sdk.NewContext(app.deliverState.ms, header, false, nil, log.NewNopLogger())
}
type state struct {
@ -228,7 +228,7 @@ func (app *BaseApp) setCheckState(header abci.Header) {
ms := app.cms.CacheMultiStore()
app.checkState = &state{
ms: ms,
ctx: sdk.NewContext(ms, header, true, nil),
ctx: sdk.NewContext(ms, header, true, nil, log.NewNopLogger()),
}
}
@ -236,7 +236,7 @@ func (app *BaseApp) setDeliverState(header abci.Header) {
ms := app.cms.CacheMultiStore()
app.deliverState = &state{
ms: ms,
ctx: sdk.NewContext(ms, header, false, nil),
ctx: sdk.NewContext(ms, header, false, nil, log.NewNopLogger()),
}
}

View File

@ -6,6 +6,7 @@ import (
"github.com/stretchr/testify/assert"
abci "github.com/tendermint/abci/types"
"github.com/tendermint/tmlibs/log"
sdk "github.com/cosmos/cosmos-sdk/types"
wire "github.com/cosmos/cosmos-sdk/wire"
@ -19,7 +20,7 @@ func TestPowHandler(t *testing.T) {
auth.RegisterBaseAccount(cdc)
am := auth.NewAccountMapper(cdc, capKey, &auth.BaseAccount{})
ctx := sdk.NewContext(ms, abci.Header{}, false, nil)
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger())
config := NewConfig("pow", int64(1))
ck := bank.NewKeeper(am)
keeper := NewKeeper(capKey, config, ck, DefaultCodespace)

View File

@ -7,6 +7,7 @@ import (
abci "github.com/tendermint/abci/types"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
@ -32,7 +33,7 @@ func TestPowKeeperGetSet(t *testing.T) {
auth.RegisterBaseAccount(cdc)
am := auth.NewAccountMapper(cdc, capKey, &auth.BaseAccount{})
ctx := sdk.NewContext(ms, abci.Header{}, false, nil)
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger())
config := NewConfig("pow", int64(1))
ck := bank.NewKeeper(am)
keeper := NewKeeper(capKey, config, ck, DefaultCodespace)

View File

@ -10,6 +10,7 @@ import (
abci "github.com/tendermint/abci/types"
crypto "github.com/tendermint/go-crypto"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
@ -32,7 +33,7 @@ func setupMultiStore() (sdk.MultiStore, *sdk.KVStoreKey, *sdk.KVStoreKey) {
func TestKeeperGetSet(t *testing.T) {
ms, _, capKey := setupMultiStore()
ctx := sdk.NewContext(ms, abci.Header{}, false, nil)
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger())
stakeKeeper := NewKeeper(capKey, bank.NewKeeper(nil), DefaultCodespace)
addr := sdk.Address([]byte("some-address"))
@ -59,7 +60,7 @@ func TestBonding(t *testing.T) {
cdc := wire.NewCodec()
auth.RegisterBaseAccount(cdc)
ctx := sdk.NewContext(ms, abci.Header{}, false, nil)
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger())
accountMapper := auth.NewAccountMapper(cdc, authKey, &auth.BaseAccount{})
coinKeeper := bank.NewKeeper(accountMapper)

View File

@ -7,10 +7,9 @@ import (
"github.com/golang/protobuf/proto"
abci "github.com/tendermint/abci/types"
"github.com/tendermint/tmlibs/log"
)
// TODO: Add a default logger.
/*
The intent of Context is for it to be an immutable object that can be
cloned and updated cheaply with WithValue() and passed forward to the
@ -31,7 +30,7 @@ type Context struct {
}
// create a new context
func NewContext(ms MultiStore, header abci.Header, isCheckTx bool, txBytes []byte) Context {
func NewContext(ms MultiStore, header abci.Header, isCheckTx bool, txBytes []byte, logger log.Logger) Context {
c := Context{
Context: context.Background(),
pst: newThePast(),
@ -43,6 +42,7 @@ func NewContext(ms MultiStore, header abci.Header, isCheckTx bool, txBytes []byt
c = c.WithChainID(header.ChainID)
c = c.WithIsCheckTx(isCheckTx)
c = c.WithTxBytes(txBytes)
c = c.WithLogger(logger)
return c
}
@ -126,6 +126,7 @@ const (
contextKeyChainID
contextKeyIsCheckTx
contextKeyTxBytes
contextKeyLogger
)
// NOTE: Do not expose MultiStore.
@ -151,6 +152,9 @@ func (c Context) IsCheckTx() bool {
func (c Context) TxBytes() []byte {
return c.Value(contextKeyTxBytes).([]byte)
}
func (c Context) Logger() log.Logger {
return c.Value(contextKeyLogger).(log.Logger)
}
func (c Context) WithMultiStore(ms MultiStore) Context {
return c.withValue(contextKeyMultiStore, ms)
}
@ -170,6 +174,9 @@ func (c Context) WithIsCheckTx(isCheckTx bool) Context {
func (c Context) WithTxBytes(txBytes []byte) Context {
return c.withValue(contextKeyTxBytes, txBytes)
}
func (c Context) WithLogger(logger log.Logger) Context {
return c.withValue(contextKeyLogger, logger)
}
// Cache the multistore and return a new cached context. The cached context is
// written to the context when writeCache is called.

View File

@ -6,6 +6,7 @@ import (
"github.com/stretchr/testify/assert"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
"github.com/cosmos/cosmos-sdk/store"
"github.com/cosmos/cosmos-sdk/types"
@ -14,7 +15,7 @@ import (
func TestContextGetOpShouldNeverPanic(t *testing.T) {
var ms types.MultiStore
ctx := types.NewContext(ms, abci.Header{}, false, nil)
ctx := types.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger())
indices := []int64{
-10, 1, 0, 10, 20,
}
@ -29,7 +30,7 @@ func defaultContext(key types.StoreKey) types.Context {
cms := store.NewCommitMultiStore(db)
cms.MountStoreWithDB(key, types.StoreTypeIAVL, db)
cms.LoadLatestVersion()
ctx := types.NewContext(cms, abci.Header{}, false, nil)
ctx := types.NewContext(cms, abci.Header{}, false, nil, log.NewNopLogger())
return ctx
}

View File

@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/require"
abci "github.com/tendermint/abci/types"
crypto "github.com/tendermint/go-crypto"
"github.com/tendermint/tmlibs/log"
sdk "github.com/cosmos/cosmos-sdk/types"
wire "github.com/cosmos/cosmos-sdk/wire"
@ -73,7 +74,7 @@ func TestAnteHandlerSigErrors(t *testing.T) {
RegisterBaseAccount(cdc)
mapper := NewAccountMapper(cdc, capKey, &BaseAccount{})
anteHandler := NewAnteHandler(mapper, BurnFeeHandler)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger())
// keys and addresses
priv1, addr1 := privAndAddr()
@ -114,7 +115,7 @@ func TestAnteHandlerSequences(t *testing.T) {
RegisterBaseAccount(cdc)
mapper := NewAccountMapper(cdc, capKey, &BaseAccount{})
anteHandler := NewAnteHandler(mapper, BurnFeeHandler)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger())
// keys and addresses
priv1, addr1 := privAndAddr()
@ -180,7 +181,7 @@ func TestAnteHandlerFees(t *testing.T) {
RegisterBaseAccount(cdc)
mapper := NewAccountMapper(cdc, capKey, &BaseAccount{})
anteHandler := NewAnteHandler(mapper, BurnFeeHandler)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger())
// keys and addresses
priv1, addr1 := privAndAddr()
@ -217,7 +218,7 @@ func TestAnteHandlerBadSignBytes(t *testing.T) {
RegisterBaseAccount(cdc)
mapper := NewAccountMapper(cdc, capKey, &BaseAccount{})
anteHandler := NewAnteHandler(mapper, BurnFeeHandler)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger())
// keys and addresses
priv1, addr1 := privAndAddr()
@ -292,7 +293,7 @@ func TestAnteHandlerSetPubKey(t *testing.T) {
RegisterBaseAccount(cdc)
mapper := NewAccountMapper(cdc, capKey, &BaseAccount{})
anteHandler := NewAnteHandler(mapper, BurnFeeHandler)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger())
// keys and addresses
priv1, addr1 := privAndAddr()

View File

@ -6,13 +6,14 @@ import (
"github.com/stretchr/testify/assert"
abci "github.com/tendermint/abci/types"
"github.com/tendermint/tmlibs/log"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func TestContextWithSigners(t *testing.T) {
ms, _ := setupMultiStore()
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger())
_, _, addr1 := keyPubAddr()
_, _, addr2 := keyPubAddr()

View File

@ -7,6 +7,7 @@ import (
abci "github.com/tendermint/abci/types"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
@ -28,7 +29,7 @@ func TestAccountMapperGetSet(t *testing.T) {
RegisterBaseAccount(cdc)
// make context and mapper
ctx := sdk.NewContext(ms, abci.Header{}, false, nil)
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger())
mapper := NewAccountMapper(cdc, capKey, &BaseAccount{})
addr := sdk.Address([]byte("some-address"))

View File

@ -7,6 +7,7 @@ import (
abci "github.com/tendermint/abci/types"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
@ -30,7 +31,7 @@ func TestKeeper(t *testing.T) {
cdc := wire.NewCodec()
auth.RegisterBaseAccount(cdc)
ctx := sdk.NewContext(ms, abci.Header{}, false, nil)
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger())
accountMapper := auth.NewAccountMapper(cdc, authKey, &auth.BaseAccount{})
coinKeeper := NewKeeper(accountMapper)
@ -116,7 +117,7 @@ func TestSendKeeper(t *testing.T) {
cdc := wire.NewCodec()
auth.RegisterBaseAccount(cdc)
ctx := sdk.NewContext(ms, abci.Header{}, false, nil)
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger())
accountMapper := auth.NewAccountMapper(cdc, authKey, &auth.BaseAccount{})
coinKeeper := NewKeeper(accountMapper)
sendKeeper := NewSendKeeper(accountMapper)
@ -185,7 +186,7 @@ func TestViewKeeper(t *testing.T) {
cdc := wire.NewCodec()
auth.RegisterBaseAccount(cdc)
ctx := sdk.NewContext(ms, abci.Header{}, false, nil)
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger())
accountMapper := auth.NewAccountMapper(cdc, authKey, &auth.BaseAccount{})
coinKeeper := NewKeeper(accountMapper)
viewKeeper := NewViewKeeper(accountMapper)

View File

@ -8,6 +8,7 @@ import (
abci "github.com/tendermint/abci/types"
"github.com/tendermint/go-crypto"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
@ -23,7 +24,7 @@ func defaultContext(key sdk.StoreKey) sdk.Context {
cms := store.NewCommitMultiStore(db)
cms.MountStoreWithDB(key, sdk.StoreTypeIAVL, db)
cms.LoadLatestVersion()
ctx := sdk.NewContext(cms, abci.Header{}, false, nil)
ctx := sdk.NewContext(cms, abci.Header{}, false, nil, log.NewNopLogger())
return ctx
}

View File

@ -10,6 +10,7 @@ import (
abci "github.com/tendermint/abci/types"
crypto "github.com/tendermint/go-crypto"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
@ -157,7 +158,7 @@ func createTestInput(t *testing.T, isCheckTx bool, initCoins int64) (sdk.Context
err := ms.LoadLatestVersion()
require.Nil(t, err)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "foochainid"}, isCheckTx, nil)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "foochainid"}, isCheckTx, nil, log.NewNopLogger())
cdc := makeTestCodec()
accountMapper := auth.NewAccountMapper(
cdc, // amino codec