init genesis WIP, also making golint compliant

This commit is contained in:
rigelrozanski 2018-02-03 02:09:20 +01:00
parent 9c00dda4eb
commit 40fd4589c1
6 changed files with 52 additions and 19 deletions

View File

@ -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(),
@ -88,22 +89,26 @@ 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
}
@ -111,25 +116,26 @@ func (app *BaseApp) Router() 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
}
@ -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()
@ -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
@ -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
@ -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.
@ -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.
@ -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
@ -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()
@ -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

View File

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

View File

@ -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)
@ -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),
@ -28,6 +32,7 @@ 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")
@ -35,6 +40,7 @@ func (rtr *router) AddRoute(r string, h sdk.Handler) {
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 {

View File

@ -17,6 +17,7 @@ type TestApp struct {
*abci.ResponseEndBlock
}
// NewTestApp - new app for tests
func NewTestApp(bapp *BaseApp) *TestApp {
app := &TestApp{
BaseApp: bapp,
@ -24,6 +25,7 @@ func NewTestApp(bapp *BaseApp) *TestApp {
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.")
@ -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
}
@ -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

View File

@ -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
@ -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 {
@ -46,6 +48,7 @@ func NewBasecoinApp() *BasecoinApp {
return app
}
// RunForever - BasecoinApp execution and cleanup
func (app *BasecoinApp) RunForever() {
// Start the ABCI server
@ -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)

View File

@ -11,6 +11,7 @@ func (app *BasecoinApp) initBaseApp() {
app.BaseApp = bapp
app.router = bapp.Router()
app.initBaseAppTxDecoder()
app.initBaseAppInitStater()
}
func (app *BasecoinApp) initBaseAppTxDecoder() {
@ -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, ...)
})
}