From 633eaa87b38d9c5b0e938fe6b4f1dfd016247e76 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Sat, 20 Jan 2018 20:08:00 -0800 Subject: [PATCH] cosmos-sdk/app.App -> cosmos-sdk/baseapp.BaseApp --- app/doc.go | 11 ---- app/app.go => baseapp/baseapp.go | 62 +++++++++++----------- app/app_test.go => baseapp/baseapp_test.go | 4 +- baseapp/doc.go | 10 ++++ {app => baseapp}/query.go | 2 +- {app => baseapp}/router.go | 2 +- {app => baseapp}/testdata/genesis.json | 0 {app => baseapp}/testdata/genesis2.json | 0 {app => baseapp}/testdata/genesis2b.json | 0 {app => baseapp}/testdata/genesis3.json | 0 examples/basecoin/app/app.go | 6 +-- examples/basecoin/app/baseapp.go | 29 ++++++++++ examples/basecoin/app/routes.go | 2 +- examples/basecoin/app/sdkapp.go | 29 ---------- examples/dummy/main.go | 14 ++--- 15 files changed, 85 insertions(+), 86 deletions(-) delete mode 100644 app/doc.go rename app/app.go => baseapp/baseapp.go (75%) rename app/app_test.go => baseapp/baseapp_test.go (98%) create mode 100644 baseapp/doc.go rename {app => baseapp}/query.go (98%) rename {app => baseapp}/router.go (97%) rename {app => baseapp}/testdata/genesis.json (100%) rename {app => baseapp}/testdata/genesis2.json (100%) rename {app => baseapp}/testdata/genesis2b.json (100%) rename {app => baseapp}/testdata/genesis3.json (100%) create mode 100644 examples/basecoin/app/baseapp.go delete mode 100644 examples/basecoin/app/sdkapp.go diff --git a/app/doc.go b/app/doc.go deleted file mode 100644 index c207ff31d..000000000 --- a/app/doc.go +++ /dev/null @@ -1,11 +0,0 @@ -/* -Package app contains data structures that provide basic data storage -functionality and act as a bridge between the ABCI interface and the SDK -abstractions. - -BaseApp has no state except the CommitMultiStore you provide upon init. You must -also provide a Handler. - -Transaction parsing is typically handled by the first Decorator. -*/ -package app diff --git a/app/app.go b/baseapp/baseapp.go similarity index 75% rename from app/app.go rename to baseapp/baseapp.go index 3f190463b..f2a15a99f 100644 --- a/app/app.go +++ b/baseapp/baseapp.go @@ -1,4 +1,4 @@ -package app +package baseapp import ( "bytes" @@ -16,11 +16,11 @@ import ( var mainHeaderKey = []byte("header") -// App - The ABCI application -type App struct { +// BaseApp - The ABCI application +type BaseApp struct { logger log.Logger - // App name from abci.Info + // Application name from abci.Info name string // Main (uncached) state @@ -51,10 +51,10 @@ type App struct { valUpdates []abci.Validator } -var _ abci.Application = &App{} +var _ abci.Application = &BaseApp{} -func NewApp(name string, ms sdk.CommitMultiStore) *App { - return &App{ +func NewBaseApp(name string, ms sdk.CommitMultiStore) *BaseApp { + return &BaseApp{ logger: makeDefaultLogger(), name: name, ms: ms, @@ -62,57 +62,57 @@ func NewApp(name string, ms sdk.CommitMultiStore) *App { } } -func (app *App) Name() string { +func (app *BaseApp) Name() string { return app.name } -func (app *App) SetTxDecoder(txDecoder sdk.TxDecoder) { +func (app *BaseApp) SetTxDecoder(txDecoder sdk.TxDecoder) { app.txDecoder = txDecoder } -func (app *App) SetDefaultAnteHandler(ah sdk.AnteHandler) { +func (app *BaseApp) SetDefaultAnteHandler(ah sdk.AnteHandler) { app.defaultAnteHandler = ah } -func (app *App) Router() Router { +func (app *BaseApp) Router() Router { return app.router } /* TODO consider: -func (app *App) SetBeginBlocker(...) {} -func (app *App) SetEndBlocker(...) {} -func (app *App) SetInitStater(...) {} +func (app *BaseApp) SetBeginBlocker(...) {} +func (app *BaseApp) SetEndBlocker(...) {} +func (app *BaseApp) SetInitStater(...) {} */ -func (app *App) LoadLatestVersion(mainKey sdk.SubstoreKey) error { +func (app *BaseApp) LoadLatestVersion(mainKey sdk.SubstoreKey) error { app.ms.LoadLatestVersion() return app.initFromStore(mainKey) } -func (app *App) LoadVersion(version int64, mainKey sdk.SubstoreKey) error { +func (app *BaseApp) LoadVersion(version int64, mainKey sdk.SubstoreKey) error { app.ms.LoadVersion(version) return app.initFromStore(mainKey) } // The last CommitID of the multistore. -func (app *App) LastCommitID() sdk.CommitID { +func (app *BaseApp) LastCommitID() sdk.CommitID { return app.ms.LastCommitID() } // The last commited block height. -func (app *App) LastBlockHeight() int64 { +func (app *BaseApp) LastBlockHeight() int64 { return app.ms.LastCommitID().Version } // Initializes the remaining logic from app.ms. -func (app *App) initFromStore(mainKey sdk.SubstoreKey) error { +func (app *BaseApp) initFromStore(mainKey sdk.SubstoreKey) error { lastCommitID := app.ms.LastCommitID() main := app.ms.GetKVStore(mainKey) header := abci.Header{} // Main store should exist. if main == nil { - return errors.New("App expects MultiStore with 'main' KVStore") + return errors.New("BaseApp expects MultiStore with 'main' KVStore") } // If we've committed before, we expect main://. @@ -133,7 +133,7 @@ func (app *App) initFromStore(mainKey sdk.SubstoreKey) error { } } - // Set App state. + // Set BaseApp state. app.header = header app.msCheck = nil app.msDeliver = nil @@ -145,7 +145,7 @@ func (app *App) initFromStore(mainKey sdk.SubstoreKey) error { //---------------------------------------- // Implements ABCI -func (app *App) Info(req abci.RequestInfo) abci.ResponseInfo { +func (app *BaseApp) Info(req abci.RequestInfo) abci.ResponseInfo { lastCommitID := app.ms.LastCommitID() @@ -157,25 +157,25 @@ func (app *App) Info(req abci.RequestInfo) abci.ResponseInfo { } // Implements ABCI -func (app *App) SetOption(req abci.RequestSetOption) (res abci.ResponseSetOption) { +func (app *BaseApp) SetOption(req abci.RequestSetOption) (res abci.ResponseSetOption) { // TODO: Implement return } // Implements ABCI -func (app *App) InitChain(req abci.RequestInitChain) (res abci.ResponseInitChain) { +func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitChain) { // TODO: Use req.Validators return } // Implements ABCI -func (app *App) Query(req abci.RequestQuery) (res abci.ResponseQuery) { +func (app *BaseApp) Query(req abci.RequestQuery) (res abci.ResponseQuery) { // TODO: See app/query.go return } // Implements ABCI -func (app *App) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeginBlock) { +func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeginBlock) { app.header = req.Header app.msDeliver = app.ms.CacheMultiStore() app.msCheck = app.ms.CacheMultiStore() @@ -183,7 +183,7 @@ func (app *App) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeginBl } // Implements ABCI -func (app *App) CheckTx(txBytes []byte) (res abci.ResponseCheckTx) { +func (app *BaseApp) CheckTx(txBytes []byte) (res abci.ResponseCheckTx) { result := app.runTx(true, txBytes) @@ -202,7 +202,7 @@ func (app *App) CheckTx(txBytes []byte) (res abci.ResponseCheckTx) { } // Implements ABCI -func (app *App) DeliverTx(txBytes []byte) (res abci.ResponseDeliverTx) { +func (app *BaseApp) DeliverTx(txBytes []byte) (res abci.ResponseDeliverTx) { result := app.runTx(false, txBytes) @@ -226,7 +226,7 @@ func (app *App) DeliverTx(txBytes []byte) (res abci.ResponseDeliverTx) { } } -func (app *App) runTx(isCheckTx bool, txBytes []byte) (result sdk.Result) { +func (app *BaseApp) runTx(isCheckTx bool, txBytes []byte) (result sdk.Result) { // Handle any panics. defer func() { @@ -278,14 +278,14 @@ func (app *App) runTx(isCheckTx bool, txBytes []byte) (result sdk.Result) { } // Implements ABCI -func (app *App) EndBlock(req abci.RequestEndBlock) (res abci.ResponseEndBlock) { +func (app *BaseApp) EndBlock(req abci.RequestEndBlock) (res abci.ResponseEndBlock) { res.ValidatorUpdates = app.valUpdates app.valUpdates = nil return } // Implements ABCI -func (app *App) Commit() (res abci.ResponseCommit) { +func (app *BaseApp) Commit() (res abci.ResponseCommit) { app.msDeliver.Write() commitID := app.ms.Commit() app.logger.Debug("Commit synced", diff --git a/app/app_test.go b/baseapp/baseapp_test.go similarity index 98% rename from app/app_test.go rename to baseapp/baseapp_test.go index 5dd99a012..6178faf77 100644 --- a/app/app_test.go +++ b/baseapp/baseapp_test.go @@ -1,4 +1,4 @@ -package app +package baseapp import ( "bytes" @@ -37,7 +37,7 @@ func TestBasic(t *testing.T) { store, storeKeys := newCommitMultiStore() // Create app. - app := NewApp(t.Name(), store) + app := NewBaseApp(t.Name(), store) app.SetTxDecoder(func(txBytes []byte) (sdk.Tx, error) { var ttx testTx fromJSON(txBytes, &ttx) diff --git a/baseapp/doc.go b/baseapp/doc.go new file mode 100644 index 000000000..631a01dc5 --- /dev/null +++ b/baseapp/doc.go @@ -0,0 +1,10 @@ +/* +Package baseapp contains data structures that provide basic data storage +functionality and act as a bridge between the ABCI interface and the SDK +abstractions. + +BaseApp has no state except the CommitMultiStore you provide upon init. + +See examples/basecoin/app/* for usage. +*/ +package baseapp diff --git a/app/query.go b/baseapp/query.go similarity index 98% rename from app/query.go rename to baseapp/query.go index 908a551ea..6527fda4f 100644 --- a/app/query.go +++ b/baseapp/query.go @@ -1,4 +1,4 @@ -package app +package baseapp /* XXX Make this work with MultiStore. diff --git a/app/router.go b/baseapp/router.go similarity index 97% rename from app/router.go rename to baseapp/router.go index 46bcb02f9..a15be8451 100644 --- a/app/router.go +++ b/baseapp/router.go @@ -1,4 +1,4 @@ -package app +package baseapp import ( "regexp" diff --git a/app/testdata/genesis.json b/baseapp/testdata/genesis.json similarity index 100% rename from app/testdata/genesis.json rename to baseapp/testdata/genesis.json diff --git a/app/testdata/genesis2.json b/baseapp/testdata/genesis2.json similarity index 100% rename from app/testdata/genesis2.json rename to baseapp/testdata/genesis2.json diff --git a/app/testdata/genesis2b.json b/baseapp/testdata/genesis2b.json similarity index 100% rename from app/testdata/genesis2b.json rename to baseapp/testdata/genesis2b.json diff --git a/app/testdata/genesis3.json b/baseapp/testdata/genesis3.json similarity index 100% rename from app/testdata/genesis3.json rename to baseapp/testdata/genesis3.json diff --git a/examples/basecoin/app/app.go b/examples/basecoin/app/app.go index b22a1ccb3..cee058398 100644 --- a/examples/basecoin/app/app.go +++ b/examples/basecoin/app/app.go @@ -4,7 +4,7 @@ import ( "fmt" "os" - apm "github.com/cosmos/cosmos-sdk/app" + bam "github.com/cosmos/cosmos-sdk/baseapp" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/tendermint/abci/server" "github.com/tendermint/go-wire" @@ -14,7 +14,7 @@ import ( const appName = "BasecoinApp" type BasecoinApp struct { - *apm.App + *bam.BaseApp cdc *wire.Codec multiStore sdk.CommitMultiStore @@ -33,7 +33,7 @@ func NewBasecoinApp() *BasecoinApp { var app = &BasecoinApp{} app.initCapKeys() // ./capkeys.go app.initStores() // ./stores.go - app.initSDKApp() // ./sdkapp.go + app.initBaseApp() // ./baseapp.go app.initRoutes() // ./routes.go // TODO: Load genesis diff --git a/examples/basecoin/app/baseapp.go b/examples/basecoin/app/baseapp.go new file mode 100644 index 000000000..0902d3adf --- /dev/null +++ b/examples/basecoin/app/baseapp.go @@ -0,0 +1,29 @@ +package app + +import ( + "github.com/cosmos/cosmos-sdk/baseapp" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" +) + +// initBaseApp() happens after initCapKeys() and initStores(). +// initBaseApp() happens before initRoutes(). +func (app *BasecoinApp) initBaseApp() { + app.BaseApp = baseapp.NewBaseApp(appName, app.multiStore) + app.initBaseAppTxDecoder() + app.initBaseAppAnteHandler() +} + +func (app *BasecoinApp) initBaseAppTxDecoder() { + cdc := makeTxCodec() + app.BaseApp.SetTxDecoder(func(txBytes []byte) (sdk.Tx, error) { + var tx = sdk.StdTx{} + err := cdc.UnmarshalBinary(txBytes, &tx) + return tx, err + }) +} + +func (app *BasecoinApp) initBaseAppAnteHandler() { + var authAnteHandler = auth.NewAnteHandler(app.accStore) + app.BaseApp.SetDefaultAnteHandler(authAnteHandler) +} diff --git a/examples/basecoin/app/routes.go b/examples/basecoin/app/routes.go index f56436804..9da53aa34 100644 --- a/examples/basecoin/app/routes.go +++ b/examples/basecoin/app/routes.go @@ -6,7 +6,7 @@ import ( // initRoutes() happens after initCapKeys(), initStores(), and initSDKApp(). func (app *BasecoinApp) initRoutes() { - var router = app.App.Router() + var router = app.BaseApp.Router() var accStore = app.accStore // All handlers must be added here. diff --git a/examples/basecoin/app/sdkapp.go b/examples/basecoin/app/sdkapp.go deleted file mode 100644 index 1405a1f30..000000000 --- a/examples/basecoin/app/sdkapp.go +++ /dev/null @@ -1,29 +0,0 @@ -package app - -import ( - apm "github.com/cosmos/cosmos-sdk/app" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/auth" -) - -// initSDKApp() happens after initCapKeys() and initStores(). -// initSDKApp() happens before initRoutes(). -func (app *BasecoinApp) initSDKApp() { - app.App = apm.NewApp(appName, app.multiStore) - app.initSDKAppTxDecoder() - app.initSDKAppAnteHandler() -} - -func (app *BasecoinApp) initSDKAppTxDecoder() { - cdc := makeTxCodec() - app.App.SetTxDecoder(func(txBytes []byte) (sdk.Tx, error) { - var tx = sdk.StdTx{} - err := cdc.UnmarshalBinary(txBytes, &tx) - return tx, err - }) -} - -func (app *BasecoinApp) initSDKAppAnteHandler() { - var authAnteHandler = auth.NewAnteHandler(app.accStore) - app.App.SetDefaultAnteHandler(authAnteHandler) -} diff --git a/examples/dummy/main.go b/examples/dummy/main.go index 59261a604..95806db61 100644 --- a/examples/dummy/main.go +++ b/examples/dummy/main.go @@ -10,7 +10,7 @@ import ( cmn "github.com/tendermint/tmlibs/common" dbm "github.com/tendermint/tmlibs/db" - "github.com/cosmos/cosmos-sdk/app" + bam "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/store" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -35,21 +35,21 @@ func main() { multiStore := store.NewCommitMultiStore(db) multiStore.SetSubstoreLoader(mainStoreKey, loader) - // Set everything on the app and load latest - app := app.NewApp("dummy", multiStore) + // Set everything on the baseApp and load latest + baseApp := bam.NewBaseApp("dummy", multiStore) // Set Tx decoder - app.SetTxDecoder(decodeTx) + baseApp.SetTxDecoder(decodeTx) - app.Router().AddRoute("dummy", DummyHandler(mainStoreKey)) + baseApp.Router().AddRoute("dummy", DummyHandler(mainStoreKey)) - if err := app.LoadLatestVersion(mainStoreKey); err != nil { + if err := baseApp.LoadLatestVersion(mainStoreKey); err != nil { fmt.Println(err) os.Exit(1) } // Start the ABCI server - srv, err := server.NewServer("0.0.0.0:46658", "socket", app) + srv, err := server.NewServer("0.0.0.0:46658", "socket", baseApp) if err != nil { fmt.Println(err) os.Exit(1)