move logger to state

also remove redundant root.go logger
This commit is contained in:
Anton Kaliaev 2017-05-14 11:24:33 +02:00 committed by Ethan Frey
parent c92c9de342
commit e42849b4b8
7 changed files with 23 additions and 23 deletions

View File

@ -42,6 +42,7 @@ func NewBasecoin(eyesCli *eyes.Client) *Basecoin {
func (app *Basecoin) SetLogger(l log.Logger) { func (app *Basecoin) SetLogger(l log.Logger) {
app.logger = l app.logger = l
app.state.SetLogger(l.With("module", "state"))
} }
// XXX For testing, not thread safe! // XXX For testing, not thread safe!
@ -104,7 +105,7 @@ func (app *Basecoin) DeliverTx(txBytes []byte) (res abci.Result) {
} }
// Validate and exec tx // 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() { if res.IsErr() {
return res.PrependLog("Error in DeliverTx") return res.PrependLog("Error in DeliverTx")
} }
@ -125,7 +126,7 @@ func (app *Basecoin) CheckTx(txBytes []byte) (res abci.Result) {
} }
// Validate tx // 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() { if res.IsErr() {
return res.PrependLog("Error in CheckTx") return res.PrependLog("Error in CheckTx")
} }

View File

@ -7,11 +7,6 @@ import (
"github.com/tendermint/basecoin/cmd/commands" "github.com/tendermint/basecoin/cmd/commands"
"github.com/tendermint/tmlibs/cli" "github.com/tendermint/tmlibs/cli"
"github.com/tendermint/tmlibs/log"
)
var (
logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "main")
) )
func main() { func main() {

View File

@ -7,15 +7,9 @@ import (
"github.com/tendermint/basecoin/cmd/commands" "github.com/tendermint/basecoin/cmd/commands"
"github.com/tendermint/tmlibs/cli" "github.com/tendermint/tmlibs/cli"
"github.com/tendermint/tmlibs/log"
)
var (
logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "main")
) )
func main() { func main() {
var RootCmd = &cobra.Command{ var RootCmd = &cobra.Command{
Use: "counter", Use: "counter",
Short: "demo plugin for basecoin", Short: "demo plugin for basecoin",

View File

@ -5,12 +5,10 @@ import (
"github.com/tendermint/basecoin/types" "github.com/tendermint/basecoin/types"
cmn "github.com/tendermint/tmlibs/common" cmn "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tmlibs/events" "github.com/tendermint/tmlibs/events"
"github.com/tendermint/tmlibs/log"
) )
// If the tx is invalid, a TMSP error will be returned. // 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() chainID := state.GetChainID()
// Exec tx // Exec tx
@ -96,11 +94,11 @@ func ExecTx(state *State, pgz *types.Plugins, tx types.Tx, isCheckTx bool, evc e
signBytes := tx.SignBytes(chainID) signBytes := tx.SignBytes(chainID)
res = validateInputAdvanced(inAcc, signBytes, tx.Input) res = validateInputAdvanced(inAcc, signBytes, tx.Input)
if res.IsErr() { 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()") return res.PrependLog("in validateInputAdvanced()")
} }
if !tx.Input.Coins.IsGTE(types.Coins{tx.Fee}) { 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})) 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) res = plugin.RunTx(cache, ctx, tx.Data)
if res.IsOK() { if res.IsOK() {
cache.CacheSync() cache.CacheSync()
logger.Info("Successful execution") state.logger.Info("Successful execution")
// Fire events // Fire events
/* /*
if evc != nil { if evc != nil {
@ -145,7 +143,7 @@ func ExecTx(state *State, pgz *types.Plugins, tx types.Tx, isCheckTx bool, evc e
} }
*/ */
} else { } else {
logger.Info("AppTx failed", "error", res) state.logger.Info("AppTx failed", "error", res)
// Just return the coins and return. // Just return the coins and return.
inAccCopy.Balance = inAccCopy.Balance.Plus(coins) inAccCopy.Balance = inAccCopy.Balance.Plus(coins)
// But take the gas // But take the gas

View File

@ -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 initBalIn := et.state.GetAccount(et.accIn.Account.PubKey.Address()).Balance
initBalOut := et.state.GetAccount(et.accOut.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 endBalIn := et.state.GetAccount(et.accIn.Account.PubKey.Address()).Balance
endBalOut := et.state.GetAccount(et.accOut.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.store = types.NewMemKVStore()
et.state = NewState(et.store) et.state = NewState(et.store)
et.state.SetLogger(log.TestingLogger())
et.state.SetChainID(et.chainID) et.state.SetChainID(et.chainID)
// NOTE we dont run acc2State here // NOTE we dont run acc2State here

View File

@ -3,9 +3,10 @@ package state
import ( import (
abci "github.com/tendermint/abci/types" abci "github.com/tendermint/abci/types"
"github.com/tendermint/basecoin/types" "github.com/tendermint/basecoin/types"
. "github.com/tendermint/tmlibs/common" wire "github.com/tendermint/go-wire"
"github.com/tendermint/go-wire"
eyes "github.com/tendermint/merkleeyes/client" eyes "github.com/tendermint/merkleeyes/client"
. "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tmlibs/log"
) )
// CONTRACT: State should be quick to copy. // CONTRACT: State should be quick to copy.
@ -15,6 +16,7 @@ type State struct {
store types.KVStore store types.KVStore
readCache map[string][]byte // optional, for caching writes to store readCache map[string][]byte // optional, for caching writes to store
writeCache *types.KVCache // optional, for caching writes w/o writing to store writeCache *types.KVCache // optional, for caching writes w/o writing to store
logger log.Logger
} }
func NewState(store types.KVStore) *State { func NewState(store types.KVStore) *State {
@ -23,9 +25,14 @@ func NewState(store types.KVStore) *State {
store: store, store: store,
readCache: make(map[string][]byte), readCache: make(map[string][]byte),
writeCache: nil, writeCache: nil,
logger: log.NewNopLogger(),
} }
} }
func (s *State) SetLogger(l log.Logger) {
s.logger = l
}
func (s *State) SetChainID(chainID string) { func (s *State) SetChainID(chainID string) {
s.chainID = chainID s.chainID = chainID
s.store.Set([]byte("base/chain_id"), []byte(chainID)) s.store.Set([]byte("base/chain_id"), []byte(chainID))

View File

@ -6,6 +6,7 @@ import (
"github.com/tendermint/basecoin/types" "github.com/tendermint/basecoin/types"
eyes "github.com/tendermint/merkleeyes/client" eyes "github.com/tendermint/merkleeyes/client"
"github.com/tendermint/tmlibs/log"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -16,6 +17,7 @@ func TestState(t *testing.T) {
//States and Stores for tests //States and Stores for tests
store := types.NewMemKVStore() store := types.NewMemKVStore()
state := NewState(store) state := NewState(store)
state.SetLogger(log.TestingLogger())
cache := state.CacheWrap() cache := state.CacheWrap()
eyesCli := eyes.NewLocalClient("", 0) eyesCli := eyes.NewLocalClient("", 0)
@ -29,12 +31,14 @@ func TestState(t *testing.T) {
reset := func() { reset := func() {
store = types.NewMemKVStore() store = types.NewMemKVStore()
state = NewState(store) state = NewState(store)
state.SetLogger(log.TestingLogger())
cache = state.CacheWrap() cache = state.CacheWrap()
} }
//set the state to using the eyesCli instead of MemKVStore //set the state to using the eyesCli instead of MemKVStore
useEyesCli := func() { useEyesCli := func() {
state = NewState(eyesCli) state = NewState(eyesCli)
state.SetLogger(log.TestingLogger())
cache = state.CacheWrap() cache = state.CacheWrap()
} }