Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize Genesis State #416

Merged
merged 15 commits into from
Feb 13, 2018
52 changes: 31 additions & 21 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

var mainHeaderKey = []byte("header")

// BaseApp - The ABCI application
// The ABCI application
type BaseApp struct {
logger log.Logger

Expand All @@ -32,33 +32,38 @@ type BaseApp struct {
// Main (uncached) state
cms sdk.CommitMultiStore

// Unmarshal []byte into sdk.Tx
// unmarshal []byte into sdk.Tx
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try to keep comments well formed (capitalized and with periods). https://github.com/tendermint/coding/blob/master/go/coding_standard.md#various

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did go-lint really complain about this ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, there are not sentences, as in the spec, only sentences should start capitalized. Everything else should start lowercase and end without a period... We should further clarify this in the spec

txDecoder sdk.TxDecoder

// Ante handler for fee and auth.
// unmarshal rawjsonbytes to initialize the application
// TODO unexpose and call from InitChain
InitStater sdk.InitStater

// ante handler for fee and auth
defaultAnteHandler sdk.AnteHandler

// Handle any kind of message.
// handle any kind of message
router Router

//--------------------
// Volatile

// CheckTx state, a cache-wrap of `.cms`.
// CheckTx state, a cache-wrap of `.cms`
msCheck sdk.CacheMultiStore

// DeliverTx state, a cache-wrap of `.cms`.
// DeliverTx state, a cache-wrap of `.cms`
msDeliver sdk.CacheMultiStore

// Current block header
// current block header
header *abci.Header

// Cached validator changes from DeliverTx.
// cached validator changes from DeliverTx
valUpdates []abci.Validator
}

var _ abci.Application = &BaseApp{}

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

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

// 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(...) {}
*/

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

// Load application version
func (app *BaseApp) LoadVersion(version int64, mainKey sdk.StoreKey) error {
app.cms.LoadVersion(version)
return app.initFromStore(mainKey)
Expand Down Expand Up @@ -174,7 +184,7 @@ func (app *BaseApp) initFromStore(mainKey sdk.StoreKey) error {

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

// Implements ABCI.
// Implements ABCI
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why get rid of periods? It's also in the coding conventions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my previous comment (not a sentence)

func (app *BaseApp) Info(req abci.RequestInfo) abci.ResponseInfo {

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

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

// Implements ABCI.
// Implements ABCI
func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitChain) {
// TODO: Use req.Validators
// TODO: Use req.AppStateJSON (?)
return
}

Expand All @@ -209,7 +220,7 @@ func (app *BaseApp) Query(req abci.RequestQuery) (res abci.ResponseQuery) {
return queryable.Query(req)
}

// Implements ABCI.
// 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 +230,7 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg
return
}

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

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

}

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

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

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

// Implements ABCI.
// Implements ABCI
func (app *BaseApp) Commit() (res abci.ResponseCommit) {
app.msDeliver.Write()
commitID := app.cms.Commit()
Expand All @@ -361,9 +372,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
5 changes: 4 additions & 1 deletion baseapp/context.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package baseapp

import sdk "github.com/cosmos/cosmos-sdk/types"
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// NewContext returns a new Context suitable for AnteHandler (and indirectly Handler) processing.
// NOTE: txBytes may be nil to support TestApp.RunCheckTx
Expand All @@ -12,6 +14,7 @@ func (app *BaseApp) NewContext(isCheckTx bool, txBytes []byte) sdk.Context {
} else {
store = app.msDeliver
}

if store == nil {
panic("BaseApp.NewContext() requires BeginBlock(): missing store")
}
Expand Down
32 changes: 32 additions & 0 deletions baseapp/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package baseapp

import (
"encoding/json"
"io/ioutil"
)

// TODO: remove from here and pass the AppState
// through InitChain

// GenesisDoc defines the initial conditions for a tendermint blockchain, in particular its validator set.
type GenesisDoc struct {
AppState json.RawMessage `json:"app_state,omitempty"`
}

// GenesisDocFromFile reads JSON data from a file and unmarshalls it into a GenesisDoc.
func GenesisDocFromFile(genDocFile string) (*GenesisDoc, error) {
if genDocFile == "" {
var g GenesisDoc
return &g, nil
}
jsonBlob, err := ioutil.ReadFile(genDocFile)
if err != nil {
return nil, err
}
genDoc := GenesisDoc{}
err = json.Unmarshal(jsonBlob, &genDoc)
if err != nil {
return nil, err
}
return &genDoc, nil
}
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
10 changes: 9 additions & 1 deletion baseapp/testapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func NewTestApp(bapp *BaseApp) *TestApp {
return app
}

// 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 +57,43 @@ func (tapp *TestApp) ensureBeginBlock() {
}
}

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

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

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

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

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

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

// return a cache-wrap DeliverTx state of multistore
func (tapp *TestApp) MultiStoreDeliver() sdk.MultiStore {
return tapp.BaseApp.msDeliver
}
Expand All @@ -97,11 +105,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
Loading