From 9b4838d96e846b2e26e7acc7632c6ac458b056fe Mon Sep 17 00:00:00 2001 From: Aditya Date: Wed, 27 Jun 2018 15:45:10 -0700 Subject: [PATCH] Merge PR #1367: Set ChainID on InitChain * Added chain-id to context in InitChain * Fix bug in test * fmt * Appease linter * updated changelog * Remove chainID hack * setCheckState in InitChain * Fix bug * Fix initialization errors in example tests * Initialize app tests with default stake genesis * fix comments --- CHANGELOG.md | 1 + baseapp/baseapp.go | 19 +++++++++++-------- baseapp/baseapp_test.go | 18 ++++++++++++++---- examples/basecoin/app/app_test.go | 13 +++++++++++++ examples/democoin/app/app_test.go | 1 + 5 files changed, 40 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f73eaa84c..2a2ed1de36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,7 @@ FIXES * Fixed bug where chain ID wasn't passed properly in x/bank REST handler, removed Viper hack from ante handler * Fixed bug where `democli account` didn't decode the account data correctly * \#1343 - fixed unnecessary parallelism in CI +* \#1367 - set ChainID in InitChain * \#1353 - CLI: Show pool shares fractions in human-readable format * \#1258 - printing big.rat's can no longer overflow int64 diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index b1c7e54605..9c4d100cd3 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -224,9 +224,6 @@ func (app *BaseApp) initFromStore(mainKey sdk.StoreKey) error { } */ - // initialize Check state - app.setCheckState(abci.Header{}) - return nil } @@ -287,12 +284,13 @@ func (app *BaseApp) SetOption(req abci.RequestSetOption) (res abci.ResponseSetOp // Implements ABCI // InitChain runs the initialization logic directly on the CommitMultiStore and commits it. func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitChain) { + // Initialize the deliver state and check state with ChainID and run initChain + app.setDeliverState(abci.Header{ChainID: req.ChainId}) + app.setCheckState(abci.Header{ChainID: req.ChainId}) + if app.initChainer == nil { return } - - // Initialize the deliver state and run initChain - app.setDeliverState(abci.Header{}) app.initChainer(app.deliverState.ctx, req) // no error // NOTE: we don't commit, but BeginBlock for block 1 @@ -379,10 +377,15 @@ func (app *BaseApp) Query(req abci.RequestQuery) (res abci.ResponseQuery) { func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeginBlock) { // Initialize the DeliverTx state. // If this is the first block, it should already - // be initialized in InitChain. It may also be nil - // if this is a test and InitChain was never called. + // be initialized in InitChain. + // Otherwise app.deliverState will be nil, since it + // is reset on Commit. if app.deliverState == nil { app.setDeliverState(req.Header) + } else { + // In the first block, app.deliverState.ctx will already be initialized + // by InitChain. Context is now updated with Header information. + app.deliverState.ctx = app.deliverState.ctx.WithBlockHeader(req.Header) } if app.beginBlocker != nil { res = app.beginBlocker(app.deliverState.ctx, req) diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index dc46b38d15..c208ec3b8f 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -202,7 +202,15 @@ func TestInitChainer(t *testing.T) { // set initChainer and try again - should see the value app.SetInitChainer(initChainer) - app.InitChain(abci.RequestInitChain{AppStateBytes: []byte("{}")}) // must have valid JSON genesis file, even if empty + app.InitChain(abci.RequestInitChain{AppStateBytes: []byte("{}"), ChainId: "test-chain-id"}) // must have valid JSON genesis file, even if empty + + // assert that chainID is set correctly in InitChain + chainID := app.deliverState.ctx.ChainID() + assert.Equal(t, "test-chain-id", chainID, "ChainID in deliverState not set correctly in InitChain") + + chainID = app.checkState.ctx.ChainID() + assert.Equal(t, "test-chain-id", chainID, "ChainID in checkState not set correctly in InitChain") + app.Commit() res = app.Query(query) assert.Equal(t, value, res.Value) @@ -378,13 +386,15 @@ func TestSimulateTx(t *testing.T) { return ttx, nil }) + app.InitChain(abci.RequestInitChain{}) + nBlocks := 3 for blockN := 0; blockN < nBlocks; blockN++ { // block1 header.Height = int64(blockN + 1) app.BeginBlock(abci.RequestBeginBlock{Header: header}) result := app.Simulate(tx) - require.Equal(t, result.Code, sdk.ABCICodeOK) + require.Equal(t, result.Code, sdk.ABCICodeOK, result.Log) require.Equal(t, int64(80), result.GasUsed) counter-- encoded, err := json.Marshal(tx) @@ -397,8 +407,8 @@ func TestSimulateTx(t *testing.T) { require.Equal(t, queryResult.Code, uint32(sdk.ABCICodeOK)) var res sdk.Result app.cdc.MustUnmarshalBinary(queryResult.Value, &res) - require.Equal(t, sdk.ABCICodeOK, res.Code) - require.Equal(t, int64(160), res.GasUsed) + require.Equal(t, sdk.ABCICodeOK, res.Code, res.Log) + require.Equal(t, int64(160), res.GasUsed, res.Log) app.EndBlock(abci.RequestEndBlock{}) app.Commit() } diff --git a/examples/basecoin/app/app_test.go b/examples/basecoin/app/app_test.go index 23bc531c03..c3ba39b3d3 100644 --- a/examples/basecoin/app/app_test.go +++ b/examples/basecoin/app/app_test.go @@ -2,7 +2,9 @@ package app import ( "os" + "fmt" "testing" + "encoding/json" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -12,6 +14,7 @@ import ( "github.com/cosmos/cosmos-sdk/wire" "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/stake" + gen "github.com/cosmos/cosmos-sdk/x/stake/types" abci "github.com/tendermint/abci/types" crypto "github.com/tendermint/go-crypto" @@ -71,6 +74,16 @@ func TestGenesis(t *testing.T) { // reload app and ensure the account is still there bapp = NewBasecoinApp(logger, db) + // Initialize stake data with default genesis state + stakedata := gen.DefaultGenesisState() + genState, err := json.Marshal(stakedata) + if err != nil { + panic(err) + } + + // InitChain with default stake data. Initializes deliverState and checkState context + bapp.InitChain(abci.RequestInitChain{AppStateBytes: []byte(fmt.Sprintf("{\"stake\": %s}", string(genState)))}) + ctx = bapp.BaseApp.NewContext(true, abci.Header{}) res1 = bapp.accountMapper.GetAccount(ctx, baseAcc.Address) assert.Equal(t, acc, res1) diff --git a/examples/democoin/app/app_test.go b/examples/democoin/app/app_test.go index 01264399ae..a477ef79bb 100644 --- a/examples/democoin/app/app_test.go +++ b/examples/democoin/app/app_test.go @@ -55,6 +55,7 @@ func TestGenesis(t *testing.T) { // reload app and ensure the account is still there bapp = NewDemocoinApp(logger, db) + bapp.InitChain(abci.RequestInitChain{AppStateBytes: []byte("{}")}) ctx = bapp.BaseApp.NewContext(true, abci.Header{}) res1 = bapp.accountMapper.GetAccount(ctx, baseAcc.Address) assert.Equal(t, acc, res1)