From 987c0469da2b9e7d61e48c74b4bb6c02afb19083 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Mon, 16 Oct 2017 15:56:23 +0200 Subject: [PATCH] Pull out basecoin stuff --- app/app.go | 109 ------------------------------------------------ app/bc.go | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 109 deletions(-) create mode 100644 app/bc.go diff --git a/app/app.go b/app/app.go index b89042d8e..df0e2d207 100644 --- a/app/app.go +++ b/app/app.go @@ -9,9 +9,6 @@ import ( cmn "github.com/tendermint/tmlibs/common" "github.com/tendermint/tmlibs/log" - sdk "github.com/cosmos/cosmos-sdk" - "github.com/cosmos/cosmos-sdk/errors" - "github.com/cosmos/cosmos-sdk/stack" sm "github.com/cosmos/cosmos-sdk/state" "github.com/cosmos/cosmos-sdk/version" ) @@ -22,112 +19,6 @@ const ( ChainKey = "chain_id" ) -// Basecoin - The ABCI application -type Basecoin struct { - *BaseApp - handler sdk.Handler - tick Ticker -} - -// Ticker - tick function -type Ticker func(sm.SimpleDB) ([]*abci.Validator, error) - -var _ abci.Application = &Basecoin{} - -// NewBasecoin - create a new instance of the basecoin application -func NewBasecoin(handler sdk.Handler, store *Store, logger log.Logger) *Basecoin { - return &Basecoin{ - BaseApp: NewBaseApp(store, logger), - handler: handler, - } -} - -// NewBasecoinTick - create a new instance of the basecoin application with tick functionality -func NewBasecoinTick(handler sdk.Handler, store *Store, logger log.Logger, tick Ticker) *Basecoin { - return &Basecoin{ - BaseApp: NewBaseApp(store, logger), - handler: handler, - tick: tick, - } -} - -// InitState - used to setup state (was SetOption) -// to be used by InitChain later -func (app *Basecoin) InitState(key string, value string) string { - module, key := splitKey(key) - state := app.state.Append() - - if module == ModuleNameBase { - if key == ChainKey { - app.info.SetChainID(state, value) - return "Success" - } - return fmt.Sprintf("Error: unknown base option: %s", key) - } - - log, err := app.handler.InitState(app.Logger(), state, module, key, value) - if err == nil { - return log - } - return "Error: " + err.Error() -} - -// DeliverTx - ABCI -func (app *Basecoin) DeliverTx(txBytes []byte) abci.Result { - tx, err := sdk.LoadTx(txBytes) - if err != nil { - return errors.Result(err) - } - - ctx := stack.NewContext( - app.GetChainID(), - app.height, - app.Logger().With("call", "delivertx"), - ) - res, err := app.handler.DeliverTx(ctx, app.state.Append(), tx) - - if err != nil { - return errors.Result(err) - } - app.AddValChange(res.Diff) - return sdk.ToABCI(res) -} - -// CheckTx - ABCI -func (app *Basecoin) CheckTx(txBytes []byte) abci.Result { - tx, err := sdk.LoadTx(txBytes) - if err != nil { - return errors.Result(err) - } - - ctx := stack.NewContext( - app.GetChainID(), - app.height, - app.Logger().With("call", "checktx"), - ) - res, err := app.handler.CheckTx(ctx, app.state.Check(), tx) - - if err != nil { - return errors.Result(err) - } - return sdk.ToABCI(res) -} - -// BeginBlock - ABCI -func (app *Basecoin) BeginBlock(req abci.RequestBeginBlock) { - // call the embeded Begin - app.BaseApp.BeginBlock(req) - - // now execute tick - if app.tick != nil { - diff, err := app.tick(app.state.Append()) - if err != nil { - panic(err) - } - app.AddValChange(diff) - } -} - /////////////////////////// Move to SDK /////// // BaseApp contains a data store and all info needed diff --git a/app/bc.go b/app/bc.go new file mode 100644 index 000000000..a7dbf80fa --- /dev/null +++ b/app/bc.go @@ -0,0 +1,119 @@ +package app + +import ( + "fmt" + + abci "github.com/tendermint/abci/types" + "github.com/tendermint/tmlibs/log" + + sdk "github.com/cosmos/cosmos-sdk" + "github.com/cosmos/cosmos-sdk/errors" + "github.com/cosmos/cosmos-sdk/stack" + sm "github.com/cosmos/cosmos-sdk/state" +) + +// Basecoin - The ABCI application +type Basecoin struct { + *BaseApp + handler sdk.Handler + tick Ticker +} + +// Ticker - tick function +type Ticker func(sm.SimpleDB) ([]*abci.Validator, error) + +var _ abci.Application = &Basecoin{} + +// NewBasecoin - create a new instance of the basecoin application +func NewBasecoin(handler sdk.Handler, store *Store, logger log.Logger) *Basecoin { + return &Basecoin{ + BaseApp: NewBaseApp(store, logger), + handler: handler, + } +} + +// NewBasecoinTick - create a new instance of the basecoin application with tick functionality +func NewBasecoinTick(handler sdk.Handler, store *Store, logger log.Logger, tick Ticker) *Basecoin { + return &Basecoin{ + BaseApp: NewBaseApp(store, logger), + handler: handler, + tick: tick, + } +} + +// InitState - used to setup state (was SetOption) +// to be used by InitChain later +func (app *Basecoin) InitState(key string, value string) string { + module, key := splitKey(key) + state := app.state.Append() + + if module == ModuleNameBase { + if key == ChainKey { + app.info.SetChainID(state, value) + return "Success" + } + return fmt.Sprintf("Error: unknown base option: %s", key) + } + + log, err := app.handler.InitState(app.Logger(), state, module, key, value) + if err == nil { + return log + } + return "Error: " + err.Error() +} + +// DeliverTx - ABCI +func (app *Basecoin) DeliverTx(txBytes []byte) abci.Result { + tx, err := sdk.LoadTx(txBytes) + if err != nil { + return errors.Result(err) + } + + ctx := stack.NewContext( + app.GetChainID(), + app.height, + app.Logger().With("call", "delivertx"), + ) + res, err := app.handler.DeliverTx(ctx, app.state.Append(), tx) + + if err != nil { + return errors.Result(err) + } + app.AddValChange(res.Diff) + return sdk.ToABCI(res) +} + +// CheckTx - ABCI +func (app *Basecoin) CheckTx(txBytes []byte) abci.Result { + tx, err := sdk.LoadTx(txBytes) + if err != nil { + return errors.Result(err) + } + + ctx := stack.NewContext( + app.GetChainID(), + app.height, + app.Logger().With("call", "checktx"), + ) + res, err := app.handler.CheckTx(ctx, app.state.Check(), tx) + + if err != nil { + return errors.Result(err) + } + return sdk.ToABCI(res) +} + +// BeginBlock - ABCI +func (app *Basecoin) BeginBlock(req abci.RequestBeginBlock) { + // call the embeded Begin + app.BaseApp.BeginBlock(req) + + // now execute tick + if app.tick != nil { + diff, err := app.tick(app.state.Append()) + if err != nil { + panic(err) + } + app.AddValChange(diff) + } +}