Skip to content

Commit

Permalink
move logger to state
Browse files Browse the repository at this point in the history
also remove redundant root.go logger
  • Loading branch information
melekes authored and ethanfrey committed May 14, 2017
1 parent c92c9de commit e42849b
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 23 deletions.
5 changes: 3 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand Down
5 changes: 0 additions & 5 deletions cmd/basecoin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
6 changes: 0 additions & 6 deletions cmd/counter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 5 additions & 7 deletions state/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}))
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion state/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
11 changes: 9 additions & 2 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand All @@ -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))
Expand Down
4 changes: 4 additions & 0 deletions state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)

Expand All @@ -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()
}

Expand Down

0 comments on commit e42849b

Please sign in to comment.