From 03c08a4eb4f7ec5e048f69f6858611de25e3c4a8 Mon Sep 17 00:00:00 2001 From: fudongbai <296179868@qq.com> Date: Wed, 18 Sep 2019 14:21:30 +0800 Subject: [PATCH 1/3] support blockhash context --- baseapp/baseapp.go | 4 ++-- types/context.go | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 63d0a14fb..e0daaba09 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -521,11 +521,11 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg // nil, since it is reset on Commit. if app.DeliverState == nil { app.SetDeliverState(req.Header) - app.DeliverState.Ctx = app.DeliverState.Ctx.WithVoteInfos(req.LastCommitInfo.GetVotes()) + app.DeliverState.Ctx = app.DeliverState.Ctx.WithVoteInfos(req.LastCommitInfo.GetVotes()).WithBlockHash(req.Hash) } 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).WithBlockHeight(req.Header.Height) + app.DeliverState.Ctx = app.DeliverState.Ctx.WithBlockHeader(req.Header).WithBlockHeight(req.Header.Height).WithBlockHash(req.Hash) } if app.beginBlocker != nil { diff --git a/types/context.go b/types/context.go index 6589af6ae..96eecf901 100644 --- a/types/context.go +++ b/types/context.go @@ -131,6 +131,7 @@ const ( contextKeyMultiStore contextKey = iota contextKeyBlockHeader contextKeyBlockHeight + contextKeyBlockHash contextKeyConsensusParams contextKeyChainID contextKeyRunTxMode @@ -149,6 +150,10 @@ func (c Context) BlockHeader() abci.Header { return c.Value(contextKeyBlockHeade func (c Context) BlockHeight() int64 { return c.Value(contextKeyBlockHeight).(int64) } +func (c Context) BlockHash() []byte { + return c.Value(contextKeyBlockHash).([]byte) +} + func (c Context) ConsensusParams() abci.ConsensusParams { return c.Value(contextKeyConsensusParams).(abci.ConsensusParams) } @@ -193,6 +198,10 @@ func (c Context) WithBlockHeader(header abci.Header) Context { return c.withValue(contextKeyBlockHeader, header) } +func (c Context) WithBlockHash(hash []byte) Context { + return c.withValue(contextKeyBlockHash, hash) +} + func (c Context) WithBlockTime(newTime time.Time) Context { newHeader := c.BlockHeader() newHeader.Time = newTime From 763f57f955626764afc501a6bb9cf1e9bc60a217 Mon Sep 17 00:00:00 2001 From: fudongbai <296179868@qq.com> Date: Wed, 23 Oct 2019 17:59:08 +0800 Subject: [PATCH 2/3] add log --- PENDING.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/PENDING.md b/PENDING.md index 4d87301f0..dc1e0a5f1 100644 --- a/PENDING.md +++ b/PENDING.md @@ -34,7 +34,8 @@ IMPROVEMENTS * Gaia -* SDK +* SDK +* [\#167](https://github.com/binance-chain/bnc-cosmos-sdk/pull/167) [sdk] add blockhash context * Tendermint From cf3803901cd8eaf671f93ffb75cb621b60ebfd81 Mon Sep 17 00:00:00 2001 From: fudongbai <296179868@qq.com> Date: Wed, 23 Oct 2019 18:01:54 +0800 Subject: [PATCH 3/3] fix review suggestion --- baseapp/baseapp.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index e0daaba09..9f8dcd93d 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -525,7 +525,7 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg } 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).WithBlockHeight(req.Header.Height).WithBlockHash(req.Hash) + app.DeliverState.Ctx = app.DeliverState.Ctx.WithBlockHash(req.Hash).WithBlockHeader(req.Header).WithBlockHeight(req.Header.Height) } if app.beginBlocker != nil {