This commit is contained in:
rigelrozanski 2018-02-09 04:33:42 +00:00
parent 849139ebeb
commit 17acf9e18d
5 changed files with 101 additions and 7 deletions

View File

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

71
baseapp/genesis.go Normal file
View File

@ -0,0 +1,71 @@
package baseapp
import (
"encoding/json"
"io/ioutil"
"time"
crypto "github.com/tendermint/go-crypto"
cmn "github.com/tendermint/tmlibs/common"
)
// TODO this is dup code from tendermint-core to avoid dep issues
// should probably remove from both SDK / Tendermint and move to tmlibs
// ^^^^^^^^^^^^^ This is my preference to avoid DEP hell
// or sync up versioning and just reference tendermint
// GenesisDoc defines the initial conditions for a tendermint blockchain, in particular its validator set.
type GenesisDoc struct {
GenesisTime time.Time `json:"genesis_time"`
ChainID string `json:"chain_id"`
ConsensusParams *ConsensusParams `json:"consensus_params,omitempty"`
Validators []GenesisValidator `json:"validators"`
AppHash cmn.HexBytes `json:"app_hash"`
AppState json.RawMessage `json:"app_state,omitempty"`
}
//nolint TODO remove
type ConsensusParams struct {
BlockSize `json:"block_size_params"`
TxSize `json:"tx_size_params"`
BlockGossip `json:"block_gossip_params"`
EvidenceParams `json:"evidence_params"`
}
type GenesisValidator struct {
PubKey crypto.PubKey `json:"pub_key"`
Power int64 `json:"power"`
Name string `json:"name"`
}
type BlockSize struct {
MaxBytes int `json:"max_bytes"` // NOTE: must not be 0 nor greater than 100MB
MaxTxs int `json:"max_txs"`
MaxGas int64 `json:"max_gas"`
}
type TxSize struct {
MaxBytes int `json:"max_bytes"`
MaxGas int64 `json:"max_gas"`
}
type BlockGossip struct {
BlockPartSizeBytes int `json:"block_part_size_bytes"` // NOTE: must not be 0
}
type EvidenceParams struct {
MaxAge int64 `json:"max_age"` // only accept new evidence more recent than this
}
// 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
}

View File

@ -8,6 +8,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/tendermint/abci/server"
abci "github.com/tendermint/abci/types"
"github.com/tendermint/go-wire"
cmn "github.com/tendermint/tmlibs/common"
)
@ -36,6 +37,7 @@ func NewBasecoinApp(genesisPath string) *BasecoinApp {
// Create and configure app.
var app = &BasecoinApp{}
app.initCapKeys() // ./init_capkeys.go
app.initBaseApp() // ./init_baseapp.go
app.initStores() // ./init_stores.go
@ -48,13 +50,33 @@ func NewBasecoinApp(genesisPath string) *BasecoinApp {
// TODO: InitChain with validators from genesis transaction bytes
// load application initial state
err = app.BaseApp.InitStater(genesisiDoc.AppState)
// set first begin block
header := abci.Header{
ChainID: "",
Height: 0,
Time: -1, // TODO
NumTxs: -1, // TODO
LastCommitHash: []byte{0x00},
DataHash: nil, // TODO
ValidatorsHash: nil, // TODO
AppHash: nil, // TODO
}
app.BaseApp.BeginBlock(abci.RequestBeginBlock{
Hash: nil, // TODO
Header: header,
AbsentValidators: nil, // TODO
ByzantineValidators: nil, // TODO
})
ctxCheckTx := app.BaseApp.NewContext(true, nil)
ctxDeliverTx := app.BaseApp.NewContext(false, nil)
err = app.BaseApp.InitStater(ctxCheckTx, ctxDeliverTx, genesisiDoc.AppState)
if err != nil {
panic(fmt.Errorf("error loading application genesis state: %v", err))
}
app.loadStores()
return app
}

View File

@ -34,10 +34,8 @@ func (app *BasecoinApp) initBaseAppTxDecoder() {
// define the custom logic for basecoin initialization
func (app *BasecoinApp) initBaseAppInitStater() {
accountMapper := app.accountMapper
ctxCheckTx := app.BaseApp.NewContext(true, nil)
ctxDeliverTx := app.BaseApp.NewContext(false, nil)
app.BaseApp.SetInitStater(func(stateJSON []byte) sdk.Error {
app.BaseApp.SetInitStater(func(ctxCheckTx, ctxDeliverTx sdk.Context, stateJSON []byte) sdk.Error {
var accs []*types.AppAccount

View File

@ -1,4 +1,4 @@
package types
// function variable used to initialize application state at genesis
type InitStater func(stateJSON []byte) Error
type InitStater func(ctxCheckTx, ctxDeliverTx Context, stateJSON []byte) Error