cosmos-sdk/app.App -> cosmos-sdk/baseapp.BaseApp

This commit is contained in:
Jae Kwon 2018-01-20 20:08:00 -08:00
parent bd8bbf9d98
commit 633eaa87b3
15 changed files with 85 additions and 86 deletions

View File

@ -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

View File

@ -1,4 +1,4 @@
package app package baseapp
import ( import (
"bytes" "bytes"
@ -16,11 +16,11 @@ import (
var mainHeaderKey = []byte("header") var mainHeaderKey = []byte("header")
// App - The ABCI application // BaseApp - The ABCI application
type App struct { type BaseApp struct {
logger log.Logger logger log.Logger
// App name from abci.Info // Application name from abci.Info
name string name string
// Main (uncached) state // Main (uncached) state
@ -51,10 +51,10 @@ type App struct {
valUpdates []abci.Validator valUpdates []abci.Validator
} }
var _ abci.Application = &App{} var _ abci.Application = &BaseApp{}
func NewApp(name string, ms sdk.CommitMultiStore) *App { func NewBaseApp(name string, ms sdk.CommitMultiStore) *BaseApp {
return &App{ return &BaseApp{
logger: makeDefaultLogger(), logger: makeDefaultLogger(),
name: name, name: name,
ms: ms, 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 return app.name
} }
func (app *App) SetTxDecoder(txDecoder sdk.TxDecoder) { func (app *BaseApp) SetTxDecoder(txDecoder sdk.TxDecoder) {
app.txDecoder = txDecoder app.txDecoder = txDecoder
} }
func (app *App) SetDefaultAnteHandler(ah sdk.AnteHandler) { func (app *BaseApp) SetDefaultAnteHandler(ah sdk.AnteHandler) {
app.defaultAnteHandler = ah app.defaultAnteHandler = ah
} }
func (app *App) Router() Router { func (app *BaseApp) Router() Router {
return app.router return app.router
} }
/* TODO consider: /* TODO consider:
func (app *App) SetBeginBlocker(...) {} func (app *BaseApp) SetBeginBlocker(...) {}
func (app *App) SetEndBlocker(...) {} func (app *BaseApp) SetEndBlocker(...) {}
func (app *App) SetInitStater(...) {} func (app *BaseApp) SetInitStater(...) {}
*/ */
func (app *App) LoadLatestVersion(mainKey sdk.SubstoreKey) error { func (app *BaseApp) LoadLatestVersion(mainKey sdk.SubstoreKey) error {
app.ms.LoadLatestVersion() app.ms.LoadLatestVersion()
return app.initFromStore(mainKey) 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) app.ms.LoadVersion(version)
return app.initFromStore(mainKey) return app.initFromStore(mainKey)
} }
// The last CommitID of the multistore. // The last CommitID of the multistore.
func (app *App) LastCommitID() sdk.CommitID { func (app *BaseApp) LastCommitID() sdk.CommitID {
return app.ms.LastCommitID() return app.ms.LastCommitID()
} }
// The last commited block height. // The last commited block height.
func (app *App) LastBlockHeight() int64 { func (app *BaseApp) LastBlockHeight() int64 {
return app.ms.LastCommitID().Version return app.ms.LastCommitID().Version
} }
// Initializes the remaining logic from app.ms. // 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() lastCommitID := app.ms.LastCommitID()
main := app.ms.GetKVStore(mainKey) main := app.ms.GetKVStore(mainKey)
header := abci.Header{} header := abci.Header{}
// Main store should exist. // Main store should exist.
if main == nil { 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://<mainHeaderKey>. // If we've committed before, we expect main://<mainHeaderKey>.
@ -133,7 +133,7 @@ func (app *App) initFromStore(mainKey sdk.SubstoreKey) error {
} }
} }
// Set App state. // Set BaseApp state.
app.header = header app.header = header
app.msCheck = nil app.msCheck = nil
app.msDeliver = nil app.msDeliver = nil
@ -145,7 +145,7 @@ func (app *App) initFromStore(mainKey sdk.SubstoreKey) error {
//---------------------------------------- //----------------------------------------
// Implements ABCI // Implements ABCI
func (app *App) Info(req abci.RequestInfo) abci.ResponseInfo { func (app *BaseApp) Info(req abci.RequestInfo) abci.ResponseInfo {
lastCommitID := app.ms.LastCommitID() lastCommitID := app.ms.LastCommitID()
@ -157,25 +157,25 @@ func (app *App) Info(req abci.RequestInfo) abci.ResponseInfo {
} }
// Implements ABCI // Implements ABCI
func (app *App) SetOption(req abci.RequestSetOption) (res abci.ResponseSetOption) { func (app *BaseApp) SetOption(req abci.RequestSetOption) (res abci.ResponseSetOption) {
// TODO: Implement // TODO: Implement
return return
} }
// Implements ABCI // 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 // TODO: Use req.Validators
return return
} }
// Implements ABCI // 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 // TODO: See app/query.go
return return
} }
// Implements ABCI // 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.header = req.Header
app.msDeliver = app.ms.CacheMultiStore() app.msDeliver = app.ms.CacheMultiStore()
app.msCheck = app.ms.CacheMultiStore() app.msCheck = app.ms.CacheMultiStore()
@ -183,7 +183,7 @@ func (app *App) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeginBl
} }
// Implements ABCI // 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) result := app.runTx(true, txBytes)
@ -202,7 +202,7 @@ func (app *App) CheckTx(txBytes []byte) (res abci.ResponseCheckTx) {
} }
// Implements ABCI // 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) 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. // Handle any panics.
defer func() { defer func() {
@ -278,14 +278,14 @@ func (app *App) runTx(isCheckTx bool, txBytes []byte) (result sdk.Result) {
} }
// Implements ABCI // 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 res.ValidatorUpdates = app.valUpdates
app.valUpdates = nil app.valUpdates = nil
return return
} }
// Implements ABCI // Implements ABCI
func (app *App) Commit() (res abci.ResponseCommit) { func (app *BaseApp) Commit() (res abci.ResponseCommit) {
app.msDeliver.Write() app.msDeliver.Write()
commitID := app.ms.Commit() commitID := app.ms.Commit()
app.logger.Debug("Commit synced", app.logger.Debug("Commit synced",

View File

@ -1,4 +1,4 @@
package app package baseapp
import ( import (
"bytes" "bytes"
@ -37,7 +37,7 @@ func TestBasic(t *testing.T) {
store, storeKeys := newCommitMultiStore() store, storeKeys := newCommitMultiStore()
// Create app. // Create app.
app := NewApp(t.Name(), store) app := NewBaseApp(t.Name(), store)
app.SetTxDecoder(func(txBytes []byte) (sdk.Tx, error) { app.SetTxDecoder(func(txBytes []byte) (sdk.Tx, error) {
var ttx testTx var ttx testTx
fromJSON(txBytes, &ttx) fromJSON(txBytes, &ttx)

10
baseapp/doc.go Normal file
View File

@ -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

View File

@ -1,4 +1,4 @@
package app package baseapp
/* /*
XXX Make this work with MultiStore. XXX Make this work with MultiStore.

View File

@ -1,4 +1,4 @@
package app package baseapp
import ( import (
"regexp" "regexp"

View File

@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
apm "github.com/cosmos/cosmos-sdk/app" bam "github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/tendermint/abci/server" "github.com/tendermint/abci/server"
"github.com/tendermint/go-wire" "github.com/tendermint/go-wire"
@ -14,7 +14,7 @@ import (
const appName = "BasecoinApp" const appName = "BasecoinApp"
type BasecoinApp struct { type BasecoinApp struct {
*apm.App *bam.BaseApp
cdc *wire.Codec cdc *wire.Codec
multiStore sdk.CommitMultiStore multiStore sdk.CommitMultiStore
@ -33,7 +33,7 @@ func NewBasecoinApp() *BasecoinApp {
var app = &BasecoinApp{} var app = &BasecoinApp{}
app.initCapKeys() // ./capkeys.go app.initCapKeys() // ./capkeys.go
app.initStores() // ./stores.go app.initStores() // ./stores.go
app.initSDKApp() // ./sdkapp.go app.initBaseApp() // ./baseapp.go
app.initRoutes() // ./routes.go app.initRoutes() // ./routes.go
// TODO: Load genesis // TODO: Load genesis

View File

@ -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)
}

View File

@ -6,7 +6,7 @@ import (
// initRoutes() happens after initCapKeys(), initStores(), and initSDKApp(). // initRoutes() happens after initCapKeys(), initStores(), and initSDKApp().
func (app *BasecoinApp) initRoutes() { func (app *BasecoinApp) initRoutes() {
var router = app.App.Router() var router = app.BaseApp.Router()
var accStore = app.accStore var accStore = app.accStore
// All handlers must be added here. // All handlers must be added here.

View File

@ -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)
}

View File

@ -10,7 +10,7 @@ import (
cmn "github.com/tendermint/tmlibs/common" cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db" 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" "github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
) )
@ -35,21 +35,21 @@ func main() {
multiStore := store.NewCommitMultiStore(db) multiStore := store.NewCommitMultiStore(db)
multiStore.SetSubstoreLoader(mainStoreKey, loader) multiStore.SetSubstoreLoader(mainStoreKey, loader)
// Set everything on the app and load latest // Set everything on the baseApp and load latest
app := app.NewApp("dummy", multiStore) baseApp := bam.NewBaseApp("dummy", multiStore)
// Set Tx decoder // 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) fmt.Println(err)
os.Exit(1) os.Exit(1)
} }
// Start the ABCI server // 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 { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)