From e42849b4b897a23711ff75409c31948328436099 Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Sun, 14 May 2017 11:24:33 +0200 Subject: [PATCH] move logger to state also remove redundant root.go logger --- app/app.go | 5 +++-- cmd/basecoin/main.go | 5 ----- cmd/counter/main.go | 6 ------ state/execution.go | 12 +++++------- state/execution_test.go | 3 ++- state/state.go | 11 +++++++++-- state/state_test.go | 4 ++++ 7 files changed, 23 insertions(+), 23 deletions(-) diff --git a/app/app.go b/app/app.go index 5a9e37ea83..94d37c43db 100644 --- a/app/app.go +++ b/app/app.go @@ -42,6 +42,7 @@ func NewBasecoin(eyesCli *eyes.Client) *Basecoin { func (app *Basecoin) SetLogger(l log.Logger) { app.logger = l + app.state.SetLogger(l.With("module", "state")) } // XXX For testing, not thread safe! @@ -104,7 +105,7 @@ func (app *Basecoin) DeliverTx(txBytes []byte) (res abci.Result) { } // Validate and exec tx - res = sm.ExecTx(app.state, app.plugins, tx, false, nil, app.logger.With("module", "state")) + res = sm.ExecTx(app.state, app.plugins, tx, false, nil) if res.IsErr() { return res.PrependLog("Error in DeliverTx") } @@ -125,7 +126,7 @@ func (app *Basecoin) CheckTx(txBytes []byte) (res abci.Result) { } // Validate tx - res = sm.ExecTx(app.cacheState, app.plugins, tx, true, nil, app.logger.With("module", "state")) + res = sm.ExecTx(app.cacheState, app.plugins, tx, true, nil) if res.IsErr() { return res.PrependLog("Error in CheckTx") } diff --git a/cmd/basecoin/main.go b/cmd/basecoin/main.go index 0dc07d9e6e..76ba6d5e93 100644 --- a/cmd/basecoin/main.go +++ b/cmd/basecoin/main.go @@ -7,11 +7,6 @@ import ( "github.com/tendermint/basecoin/cmd/commands" "github.com/tendermint/tmlibs/cli" - "github.com/tendermint/tmlibs/log" -) - -var ( - logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "main") ) func main() { diff --git a/cmd/counter/main.go b/cmd/counter/main.go index a79e9da6d0..8254fce271 100644 --- a/cmd/counter/main.go +++ b/cmd/counter/main.go @@ -7,15 +7,9 @@ import ( "github.com/tendermint/basecoin/cmd/commands" "github.com/tendermint/tmlibs/cli" - "github.com/tendermint/tmlibs/log" -) - -var ( - logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "main") ) func main() { - var RootCmd = &cobra.Command{ Use: "counter", Short: "demo plugin for basecoin", diff --git a/state/execution.go b/state/execution.go index 40bb706bb4..246246846d 100644 --- a/state/execution.go +++ b/state/execution.go @@ -5,12 +5,10 @@ import ( "github.com/tendermint/basecoin/types" cmn "github.com/tendermint/tmlibs/common" "github.com/tendermint/tmlibs/events" - "github.com/tendermint/tmlibs/log" ) // If the tx is invalid, a TMSP error will be returned. -func ExecTx(state *State, pgz *types.Plugins, tx types.Tx, isCheckTx bool, evc events.Fireable, logger log.Logger) abci.Result { - +func ExecTx(state *State, pgz *types.Plugins, tx types.Tx, isCheckTx bool, evc events.Fireable) abci.Result { chainID := state.GetChainID() // Exec tx @@ -96,11 +94,11 @@ func ExecTx(state *State, pgz *types.Plugins, tx types.Tx, isCheckTx bool, evc e signBytes := tx.SignBytes(chainID) res = validateInputAdvanced(inAcc, signBytes, tx.Input) if res.IsErr() { - logger.Info(cmn.Fmt("validateInputAdvanced failed on %X: %v", tx.Input.Address, res)) + state.logger.Info(cmn.Fmt("validateInputAdvanced failed on %X: %v", tx.Input.Address, res)) return res.PrependLog("in validateInputAdvanced()") } if !tx.Input.Coins.IsGTE(types.Coins{tx.Fee}) { - logger.Info(cmn.Fmt("Sender did not send enough to cover the fee %X", tx.Input.Address)) + state.logger.Info(cmn.Fmt("Sender did not send enough to cover the fee %X", tx.Input.Address)) return abci.ErrBaseInsufficientFunds.AppendLog(cmn.Fmt("input coins is %v, but fee is %v", tx.Input.Coins, types.Coins{tx.Fee})) } @@ -132,7 +130,7 @@ func ExecTx(state *State, pgz *types.Plugins, tx types.Tx, isCheckTx bool, evc e res = plugin.RunTx(cache, ctx, tx.Data) if res.IsOK() { cache.CacheSync() - logger.Info("Successful execution") + state.logger.Info("Successful execution") // Fire events /* if evc != nil { @@ -145,7 +143,7 @@ func ExecTx(state *State, pgz *types.Plugins, tx types.Tx, isCheckTx bool, evc e } */ } else { - logger.Info("AppTx failed", "error", res) + state.logger.Info("AppTx failed", "error", res) // Just return the coins and return. inAccCopy.Balance = inAccCopy.Balance.Plus(coins) // But take the gas diff --git a/state/execution_test.go b/state/execution_test.go index 0f07d52da9..b8e526ae48 100644 --- a/state/execution_test.go +++ b/state/execution_test.go @@ -43,7 +43,7 @@ func (et *execTest) exec(tx *types.SendTx, checkTx bool) (res abci.Result, inGot initBalIn := et.state.GetAccount(et.accIn.Account.PubKey.Address()).Balance initBalOut := et.state.GetAccount(et.accOut.Account.PubKey.Address()).Balance - res = ExecTx(et.state, nil, tx, checkTx, nil, log.TestingLogger().With("module", "state")) + res = ExecTx(et.state, nil, tx, checkTx, nil) endBalIn := et.state.GetAccount(et.accIn.Account.PubKey.Address()).Balance endBalOut := et.state.GetAccount(et.accOut.Account.PubKey.Address()).Balance @@ -64,6 +64,7 @@ func (et *execTest) reset() { et.store = types.NewMemKVStore() et.state = NewState(et.store) + et.state.SetLogger(log.TestingLogger()) et.state.SetChainID(et.chainID) // NOTE we dont run acc2State here diff --git a/state/state.go b/state/state.go index 5555dae913..835ff088b2 100644 --- a/state/state.go +++ b/state/state.go @@ -3,9 +3,10 @@ package state import ( abci "github.com/tendermint/abci/types" "github.com/tendermint/basecoin/types" - . "github.com/tendermint/tmlibs/common" - "github.com/tendermint/go-wire" + wire "github.com/tendermint/go-wire" eyes "github.com/tendermint/merkleeyes/client" + . "github.com/tendermint/tmlibs/common" + "github.com/tendermint/tmlibs/log" ) // CONTRACT: State should be quick to copy. @@ -15,6 +16,7 @@ type State struct { store types.KVStore readCache map[string][]byte // optional, for caching writes to store writeCache *types.KVCache // optional, for caching writes w/o writing to store + logger log.Logger } func NewState(store types.KVStore) *State { @@ -23,9 +25,14 @@ func NewState(store types.KVStore) *State { store: store, readCache: make(map[string][]byte), writeCache: nil, + logger: log.NewNopLogger(), } } +func (s *State) SetLogger(l log.Logger) { + s.logger = l +} + func (s *State) SetChainID(chainID string) { s.chainID = chainID s.store.Set([]byte("base/chain_id"), []byte(chainID)) diff --git a/state/state_test.go b/state/state_test.go index 1d35f2c4f6..dae620027a 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -6,6 +6,7 @@ import ( "github.com/tendermint/basecoin/types" eyes "github.com/tendermint/merkleeyes/client" + "github.com/tendermint/tmlibs/log" "github.com/stretchr/testify/assert" ) @@ -16,6 +17,7 @@ func TestState(t *testing.T) { //States and Stores for tests store := types.NewMemKVStore() state := NewState(store) + state.SetLogger(log.TestingLogger()) cache := state.CacheWrap() eyesCli := eyes.NewLocalClient("", 0) @@ -29,12 +31,14 @@ func TestState(t *testing.T) { reset := func() { store = types.NewMemKVStore() state = NewState(store) + state.SetLogger(log.TestingLogger()) cache = state.CacheWrap() } //set the state to using the eyesCli instead of MemKVStore useEyesCli := func() { state = NewState(eyesCli) + state.SetLogger(log.TestingLogger()) cache = state.CacheWrap() }