From 88f7fd64f337d057bdaf11ddfee20888f1cea351 Mon Sep 17 00:00:00 2001 From: Aditya Sripal Date: Mon, 25 Jun 2018 13:11:04 -0700 Subject: [PATCH 01/11] Added chain-id to context in InitChain --- baseapp/baseapp.go | 4 ++-- baseapp/baseapp_test.go | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 3150e84a5ed1..6aa0a06435fe 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -291,8 +291,8 @@ func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitC return } - // Initialize the deliver state and run initChain - app.setDeliverState(abci.Header{}) + // Initialize the deliver state with ChainID and run initChain + app.setDeliverState(abci.Header{ChainID: req.ChainId}) app.initChainer(app.deliverState.ctx, req) // no error // NOTE: we don't commit, but BeginBlock for block 1 diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index c939996c76d7..dcd87c3a59c2 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -202,10 +202,12 @@ 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 app.Commit() res = app.Query(query) assert.Equal(t, value, res.Value) + chainId := app.deliverState.ctx.ChainID + assert.Equal(t, "test-chain-id", chainId, "ChainID not set correctly in InitChain") // reload app app = NewBaseApp(name, nil, logger, db) From ce75c7f6a5b0f10fa628d816a48bf5e3c481e084 Mon Sep 17 00:00:00 2001 From: Aditya Sripal Date: Mon, 25 Jun 2018 13:14:58 -0700 Subject: [PATCH 02/11] Fix bug in test --- baseapp/baseapp_test.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index dcd87c3a59c2..fda2dd9450d2 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -203,12 +203,15 @@ func TestInitChainer(t *testing.T) { // set initChainer and try again - should see the value app.SetInitChainer(initChainer) 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 not set correctly in InitChain") + app.Commit() res = app.Query(query) assert.Equal(t, value, res.Value) - chainId := app.deliverState.ctx.ChainID - assert.Equal(t, "test-chain-id", chainId, "ChainID not set correctly in InitChain") - + // reload app app = NewBaseApp(name, nil, logger, db) app.MountStoresIAVL(capKey, capKey2) From 1b99b8a69416adb04aebee73b7837f5a996e3dec Mon Sep 17 00:00:00 2001 From: Aditya Sripal Date: Mon, 25 Jun 2018 13:15:53 -0700 Subject: [PATCH 03/11] fmt --- baseapp/baseapp_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index fda2dd9450d2..a1bcff0040a9 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -203,7 +203,7 @@ func TestInitChainer(t *testing.T) { // set initChainer and try again - should see the value app.SetInitChainer(initChainer) 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 not set correctly in InitChain") @@ -211,7 +211,7 @@ func TestInitChainer(t *testing.T) { app.Commit() res = app.Query(query) assert.Equal(t, value, res.Value) - + // reload app app = NewBaseApp(name, nil, logger, db) app.MountStoresIAVL(capKey, capKey2) From abfcb2fa06f9f162199c99496ed8626bd64a5a67 Mon Sep 17 00:00:00 2001 From: Aditya Sripal Date: Mon, 25 Jun 2018 13:21:15 -0700 Subject: [PATCH 04/11] Appease linter --- baseapp/baseapp_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index a1bcff0040a9..8061169bc9c3 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -205,8 +205,8 @@ func TestInitChainer(t *testing.T) { 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 not set correctly in InitChain") + chainID := app.deliverState.ctx.ChainID() + assert.Equal(t, "test-chain-id", chainID, "ChainID not set correctly in InitChain") app.Commit() res = app.Query(query) From 63522219a8f7eabb566ac8283c6546f6912ee96c Mon Sep 17 00:00:00 2001 From: Aditya Sripal Date: Mon, 25 Jun 2018 13:37:06 -0700 Subject: [PATCH 05/11] updated changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f1ef031e463..122a3d7e9ed9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ FIXES * Fixed bug where chain ID wasn't passed properly in x/bank REST handler * Fixed bug where `democli account` didn't decode the account data correctly * \#1343 - fixed unnecessary parallelism in CI +* \#1367 - set ChainID in InitChain ## 0.19.0 From 6b3c305f55a4880269bf9350b09c5ec09c46ab4c Mon Sep 17 00:00:00 2001 From: Aditya Sripal Date: Mon, 25 Jun 2018 15:04:53 -0700 Subject: [PATCH 06/11] Remove chainID hack --- x/auth/ante.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/x/auth/ante.go b/x/auth/ante.go index 946a278e61f9..ccc79fa14bfa 100644 --- a/x/auth/ante.go +++ b/x/auth/ante.go @@ -5,7 +5,6 @@ import ( "fmt" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/spf13/viper" ) const ( @@ -72,12 +71,6 @@ func NewAnteHandler(am AccountMapper, fck FeeCollectionKeeper) sdk.AnteHandler { accNums[i] = sigs[i].AccountNumber } fee := stdTx.Fee - chainID := ctx.ChainID() - // XXX: major hack; need to get ChainID - // into the app right away (#565) - if chainID == "" { - chainID = viper.GetString("chain-id") - } // Check sig and nonce and collect signer accounts. var signerAccs = make([]Account, len(signerAddrs)) From dc7b0d449f3a9ddfd7f5dd8ec32c86e6fc0a1106 Mon Sep 17 00:00:00 2001 From: Aditya Sripal Date: Mon, 25 Jun 2018 15:20:42 -0700 Subject: [PATCH 07/11] setCheckState in InitChain --- baseapp/baseapp.go | 6 ++---- baseapp/baseapp_test.go | 5 ++++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 6aa0a06435fe..dd6b0c7e969f 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 } @@ -291,8 +288,9 @@ func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitC return } - // Initialize the deliver state with ChainID and run initChain + // 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}) app.initChainer(app.deliverState.ctx, req) // no error // NOTE: we don't commit, but BeginBlock for block 1 diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index 8061169bc9c3..7a6ef3eaf884 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -206,7 +206,10 @@ func TestInitChainer(t *testing.T) { // assert that chainID is set correctly in InitChain chainID := app.deliverState.ctx.ChainID() - assert.Equal(t, "test-chain-id", chainID, "ChainID not set correctly in InitChain") + 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) From e4f319452724276560f03b6a606a8ba695048f7e Mon Sep 17 00:00:00 2001 From: Aditya Sripal Date: Mon, 25 Jun 2018 15:57:08 -0700 Subject: [PATCH 08/11] Fix bug --- baseapp/baseapp.go | 10 ++++++---- baseapp/baseapp_test.go | 8 +++++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index dd6b0c7e969f..6d661adfc46e 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -284,13 +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) { - if app.initChainer == nil { - return - } - // 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 + } app.initChainer(app.deliverState.ctx, req) // no error // NOTE: we don't commit, but BeginBlock for block 1 @@ -381,6 +381,8 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg // if this is a test and InitChain was never called. if app.deliverState == nil { app.setDeliverState(req.Header) + } else { + 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 7a6ef3eaf884..eedf48c6d623 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -386,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) @@ -405,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() } From cf60ad5d6618314fed7d89b374b4f4f114ae3c3c Mon Sep 17 00:00:00 2001 From: Aditya Sripal Date: Mon, 25 Jun 2018 16:18:15 -0700 Subject: [PATCH 09/11] Fix initialization errors in example tests --- examples/basecoin/app/app_test.go | 1 + examples/democoin/app/app_test.go | 1 + 2 files changed, 2 insertions(+) diff --git a/examples/basecoin/app/app_test.go b/examples/basecoin/app/app_test.go index 23bc531c033d..3c2f54805ce1 100644 --- a/examples/basecoin/app/app_test.go +++ b/examples/basecoin/app/app_test.go @@ -71,6 +71,7 @@ func TestGenesis(t *testing.T) { // reload app and ensure the account is still there bapp = NewBasecoinApp(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) diff --git a/examples/democoin/app/app_test.go b/examples/democoin/app/app_test.go index 01264399aed0..a477ef79bbb6 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) From e8a5e468cc8f300906ebd2e2325021416fdce0ec Mon Sep 17 00:00:00 2001 From: Aditya Sripal Date: Wed, 27 Jun 2018 12:42:12 -0700 Subject: [PATCH 10/11] Initialize app tests with default stake genesis --- examples/basecoin/app/app_test.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/examples/basecoin/app/app_test.go b/examples/basecoin/app/app_test.go index 3c2f54805ce1..c3ba39b3d319 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,7 +74,16 @@ func TestGenesis(t *testing.T) { // reload app and ensure the account is still there bapp = NewBasecoinApp(logger, db) - bapp.InitChain(abci.RequestInitChain{AppStateBytes: []byte("{}")}) + // 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) From f933e35560481c085813aa8605262c85a82da2ca Mon Sep 17 00:00:00 2001 From: Aditya Sripal Date: Wed, 27 Jun 2018 15:32:02 -0700 Subject: [PATCH 11/11] fix comments --- baseapp/baseapp.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 21d84570868d..9c4d100cd3b1 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -377,11 +377,14 @@ 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 {