Skip to content

Commit

Permalink
init genesis WIP, also making golint compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
rigelrozanski committed Feb 8, 2018
1 parent 9c00dda commit 40fd458
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 19 deletions.
35 changes: 20 additions & 15 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type BaseApp struct {

var _ abci.Application = &BaseApp{}

// NewBaseApp - create and name new BaseApp
func NewBaseApp(name string) *BaseApp {
var baseapp = &BaseApp{
logger: makeDefaultLogger(),
Expand Down Expand Up @@ -88,48 +89,53 @@ func (app *BaseApp) initMultiStore() {
app.cms = cms
}

// Name - BaseApp Name
func (app *BaseApp) Name() string {
return app.name
}

// MountStore - Mount a store to the provided key in the BaseApp multistore
func (app *BaseApp) MountStore(key sdk.StoreKey, typ sdk.StoreType) {
app.cms.MountStoreWithDB(key, typ, app.db)
}

// nolint
func (app *BaseApp) SetTxDecoder(txDecoder sdk.TxDecoder) {
app.txDecoder = txDecoder
}

func (app *BaseApp) SetInitStater(initStater sdk.InitStater) {
app.initStater = initStater
}
func (app *BaseApp) SetDefaultAnteHandler(ah sdk.AnteHandler) {
app.defaultAnteHandler = ah
}

func (app *BaseApp) Router() Router {
return app.router
}

/* TODO consider:
func (app *BaseApp) SetBeginBlocker(...) {}
func (app *BaseApp) SetEndBlocker(...) {}
func (app *BaseApp) SetInitStater(...) {}
*/

// LoadLatestVersion - TODO add description
func (app *BaseApp) LoadLatestVersion(mainKey sdk.StoreKey) error {
app.cms.LoadLatestVersion()
return app.initFromStore(mainKey)
}

// LoadVersion - load application version
func (app *BaseApp) LoadVersion(version int64, mainKey sdk.StoreKey) error {
app.cms.LoadVersion(version)
return app.initFromStore(mainKey)
}

// The last CommitID of the multistore.
// LastCommitID - The last CommitID of the multistore.
func (app *BaseApp) LastCommitID() sdk.CommitID {
return app.cms.LastCommitID()
}

// The last commited block height.
// LastBlockHeight - The last commited block height.
func (app *BaseApp) LastBlockHeight() int64 {
return app.cms.LastCommitID().Version
}
Expand Down Expand Up @@ -174,7 +180,7 @@ func (app *BaseApp) initFromStore(mainKey sdk.StoreKey) error {

//----------------------------------------

// Implements ABCI.
// Info - Implements ABCI
func (app *BaseApp) Info(req abci.RequestInfo) abci.ResponseInfo {

lastCommitID := app.cms.LastCommitID()
Expand All @@ -186,13 +192,13 @@ func (app *BaseApp) Info(req abci.RequestInfo) abci.ResponseInfo {
}
}

// Implements ABCI.
// SetOption - Implements ABCI
func (app *BaseApp) SetOption(req abci.RequestSetOption) (res abci.ResponseSetOption) {
// TODO: Implement
return
}

// Implements ABCI.
// InitChain - Implements ABCI
func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitChain) {
// TODO: Use req.Validators
return
Expand All @@ -209,7 +215,7 @@ func (app *BaseApp) Query(req abci.RequestQuery) (res abci.ResponseQuery) {
return queryable.Query(req)
}

// Implements ABCI.
// BeginBlock - Implements ABCI
func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeginBlock) {
// NOTE: For consistency we should unset these upon EndBlock.
app.header = &req.Header
Expand All @@ -219,7 +225,7 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg
return
}

// Implements ABCI.
// CheckTx - Implements ABCI
func (app *BaseApp) CheckTx(txBytes []byte) (res abci.ResponseCheckTx) {

// Decode the Tx.
Expand All @@ -245,7 +251,7 @@ func (app *BaseApp) CheckTx(txBytes []byte) (res abci.ResponseCheckTx) {

}

// Implements ABCI.
// DeliverTx - Implements ABCI
func (app *BaseApp) DeliverTx(txBytes []byte) (res abci.ResponseDeliverTx) {

// Decode the Tx.
Expand Down Expand Up @@ -333,7 +339,7 @@ func (app *BaseApp) runTx(isCheckTx bool, txBytes []byte, tx sdk.Tx) (result sdk
return result
}

// Implements ABCI.
// EndBlock - Implements ABCI
func (app *BaseApp) EndBlock(req abci.RequestEndBlock) (res abci.ResponseEndBlock) {
res.ValidatorUpdates = app.valUpdates
app.valUpdates = nil
Expand All @@ -343,7 +349,7 @@ func (app *BaseApp) EndBlock(req abci.RequestEndBlock) (res abci.ResponseEndBloc
return
}

// Implements ABCI.
// Commit - Implements ABCI
func (app *BaseApp) Commit() (res abci.ResponseCommit) {
app.msDeliver.Write()
commitID := app.cms.Commit()
Expand All @@ -361,9 +367,8 @@ func (app *BaseApp) Commit() (res abci.ResponseCommit) {
func (app *BaseApp) getMultiStore(isCheckTx bool) sdk.MultiStore {
if isCheckTx {
return app.msCheck
} else {
return app.msDeliver
}
return app.msDeliver
}

// Return index of list with validator of same PubKey, or -1 if no match
Expand Down
2 changes: 1 addition & 1 deletion baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestBasic(t *testing.T) {
}

// Not matched.
j += 1
j++
}
}
assert.Equal(t, len(valUpdates), 0, "Some validator updates were unexpected")
Expand Down
6 changes: 6 additions & 0 deletions baseapp/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// Router - TODO add description
type Router interface {
AddRoute(r string, h sdk.Handler)
Route(path string) (h sdk.Handler)
Expand All @@ -20,6 +21,9 @@ type router struct {
routes []route
}

// nolint
// NewRouter - create new router
// TODO either make Function unexported or make return type (router) Exported
func NewRouter() *router {
return &router{
routes: make([]route, 0),
Expand All @@ -28,13 +32,15 @@ func NewRouter() *router {

var isAlpha = regexp.MustCompile(`^[a-zA-Z]+$`).MatchString

// AddRoute - TODO add description
func (rtr *router) AddRoute(r string, h sdk.Handler) {
if !isAlpha(r) {
panic("route expressions can only contain alphanumeric characters")
}
rtr.routes = append(rtr.routes, route{r, h})
}

// Route - TODO add description
// TODO handle expressive matches.
func (rtr *router) Route(path string) (h sdk.Handler) {
for _, route := range rtr.routes {
Expand Down
11 changes: 10 additions & 1 deletion baseapp/testapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ type TestApp struct {
*abci.ResponseEndBlock
}

// NewTestApp - new app for tests
func NewTestApp(bapp *BaseApp) *TestApp {
app := &TestApp{
BaseApp: bapp,
}
return app
}

// RunBeginBlock - Execute BaseApp BeginBlock
func (tapp *TestApp) RunBeginBlock() {
if tapp.header != nil {
panic("TestApp.header not nil, BeginBlock already run, or EndBlock not yet run.")
Expand Down Expand Up @@ -56,36 +58,43 @@ func (tapp *TestApp) ensureBeginBlock() {
}
}

// RunCheckTx - run tx through CheckTx of TestApp
func (tapp *TestApp) RunCheckTx(tx sdk.Tx) sdk.Result {
tapp.ensureBeginBlock()
return tapp.BaseApp.runTx(true, nil, tx)
}

// RunDeliverTx - run tx through DeliverTx of TestApp
func (tapp *TestApp) RunDeliverTx(tx sdk.Tx) sdk.Result {
tapp.ensureBeginBlock()
return tapp.BaseApp.runTx(false, nil, tx)
}

// RunCheckMsg - run tx through CheckTx of TestApp
// NOTE: Skips authentication by wrapping msg in testTx{}.
func (tapp *TestApp) RunCheckMsg(msg sdk.Msg) sdk.Result {
var tx = testTx{msg}
return tapp.RunCheckTx(tx)
}

// RunDeliverMsg - run tx through DeliverTx of TestApp
// NOTE: Skips authentication by wrapping msg in testTx{}.
func (tapp *TestApp) RunDeliverMsg(msg sdk.Msg) sdk.Result {
var tx = testTx{msg}
return tapp.RunDeliverTx(tx)
}

// CommitMultiStore - return the commited multistore
func (tapp *TestApp) CommitMultiStore() sdk.CommitMultiStore {
return tapp.BaseApp.cms
}

// MultiStoreCheck - return a cache-wrap CheckTx state of multistore
func (tapp *TestApp) MultiStoreCheck() sdk.MultiStore {
return tapp.BaseApp.msCheck
}

// MultiStoreDeliver - return a cache-wrap DeliverTx state of multistore
func (tapp *TestApp) MultiStoreDeliver() sdk.MultiStore {
return tapp.BaseApp.msDeliver
}
Expand All @@ -97,11 +106,11 @@ type testTx struct {
sdk.Msg
}

// nolint
func (tx testTx) GetMsg() sdk.Msg { return tx.Msg }
func (tx testTx) GetSigners() []crypto.Address { return nil }
func (tx testTx) GetFeePayer() crypto.Address { return nil }
func (tx testTx) GetSignatures() []sdk.StdSignature { return nil }

func IsTestAppTx(tx sdk.Tx) bool {
_, ok := tx.(testTx)
return ok
Expand Down
7 changes: 5 additions & 2 deletions examples/basecoin/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import (

const appName = "BasecoinApp"

// BasecoinApp - extended ABCI application
type BasecoinApp struct {
*bam.BaseApp
router bam.Router
cdc *wire.Codec
multiStore sdk.CommitMultiStore
multiStore sdk.CommitMultiStore //TODO distinguish this store from *bam.BaseApp.cms <- is this one master?? confused

// The key to access the substores.
capKeyMainStore *sdk.KVStoreKey
Expand All @@ -27,6 +28,7 @@ type BasecoinApp struct {
accountMapper sdk.AccountMapper
}

// NewBasecoinApp - create new BasecoinApp
// TODO: This should take in more configuration options.
func NewBasecoinApp() *BasecoinApp {

Expand All @@ -46,6 +48,7 @@ func NewBasecoinApp() *BasecoinApp {
return app
}

// RunForever - BasecoinApp execution and cleanup
func (app *BasecoinApp) RunForever() {

// Start the ABCI server
Expand All @@ -64,7 +67,7 @@ func (app *BasecoinApp) RunForever() {

}

// Load the stores.
// Load the stores
func (app *BasecoinApp) loadStores() {
if err := app.LoadLatestVersion(app.capKeyMainStore); err != nil {
fmt.Println(err)
Expand Down
10 changes: 10 additions & 0 deletions examples/basecoin/app/init_baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func (app *BasecoinApp) initBaseApp() {
app.BaseApp = bapp
app.router = bapp.Router()
app.initBaseAppTxDecoder()
app.initBaseAppInitStater()
}

func (app *BasecoinApp) initBaseAppTxDecoder() {
Expand All @@ -26,3 +27,12 @@ func (app *BasecoinApp) initBaseAppTxDecoder() {
return tx, nil
})
}

// used to define the custom logic for initialization
func (app *BasecoinApp) initBaseAppInitStater() {
accountMapper := app.accountMapper
app.BaseApp.SetInitStater(func(ctx sdk.Context, stateJSON []byte) sdk.Error {
// TODO: parse JSON
//accountMapper.SetAccount(ctx, ...)
})
}

0 comments on commit 40fd458

Please sign in to comment.