From 17acf9e18d2f76ab0a7a2cfe8eb57cf0061a69f8 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Fri, 9 Feb 2018 04:33:42 +0000 Subject: [PATCH] working --- baseapp/context.go | 5 +- baseapp/genesis.go | 71 +++++++++++++++++++++++++++ examples/basecoin/app/app.go | 26 +++++++++- examples/basecoin/app/init_baseapp.go | 4 +- types/genesis.go | 2 +- 5 files changed, 101 insertions(+), 7 deletions(-) create mode 100644 baseapp/genesis.go diff --git a/baseapp/context.go b/baseapp/context.go index 281220665..e5a60f967 100644 --- a/baseapp/context.go +++ b/baseapp/context.go @@ -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") } diff --git a/baseapp/genesis.go b/baseapp/genesis.go new file mode 100644 index 000000000..943f24bb8 --- /dev/null +++ b/baseapp/genesis.go @@ -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 +} diff --git a/examples/basecoin/app/app.go b/examples/basecoin/app/app.go index 29b6a950d..8fcc2bbe1 100644 --- a/examples/basecoin/app/app.go +++ b/examples/basecoin/app/app.go @@ -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 } diff --git a/examples/basecoin/app/init_baseapp.go b/examples/basecoin/app/init_baseapp.go index 70afb406a..525aa2f67 100644 --- a/examples/basecoin/app/init_baseapp.go +++ b/examples/basecoin/app/init_baseapp.go @@ -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 diff --git a/types/genesis.go b/types/genesis.go index eaf72b8b7..24c311feb 100644 --- a/types/genesis.go +++ b/types/genesis.go @@ -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