From 1684ec163fad41a9d97512a6e08082e966cd6567 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Fri, 14 Apr 2017 20:21:50 -0400 Subject: [PATCH] ABCIResponses not needed as field in state --- state/execution.go | 14 +++++++------- state/state.go | 41 +++++++++++++++++++++-------------------- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/state/execution.go b/state/execution.go index 943a64f7f..fb0eac02e 100644 --- a/state/execution.go +++ b/state/execution.go @@ -43,7 +43,6 @@ func execBlockOnProxyApp(eventCache types.Fireable, proxyAppConn proxy.AppConnCo var validTxs, invalidTxs = 0, 0 txIndex := 0 - abciResponses := NewABCIResponses(block) // Execute transactions and get hash @@ -64,7 +63,7 @@ func execBlockOnProxyApp(eventCache types.Fireable, proxyAppConn proxy.AppConnCo txError = txResult.Code.String() } - abciResponses.TxResults[txIndex] = &types.TxResult{uint64(block.Height), uint32(txIndex), *txResult} + abciResponses.DeliverTx[txIndex] = txResult txIndex++ // NOTE: if we count we can access the tx from the block instead of @@ -103,7 +102,7 @@ func execBlockOnProxyApp(eventCache types.Fireable, proxyAppConn proxy.AppConnCo fail.Fail() // XXX // End block - respEndBlock, err := proxyAppConn.EndBlockSync(uint64(block.Height)) + abciResponses.EndBlock, err = proxyAppConn.EndBlockSync(uint64(block.Height)) if err != nil { log.Warn("Error in proxyAppConn.EndBlock", "error", err) return nil, err @@ -111,11 +110,12 @@ func execBlockOnProxyApp(eventCache types.Fireable, proxyAppConn proxy.AppConnCo fail.Fail() // XXX + valDiff := abciResponses.EndBlock.Diffs + log.Info("Executed block", "height", block.Height, "valid txs", validTxs, "invalid txs", invalidTxs) - if len(respEndBlock.Diffs) > 0 { - log.Info("Update to validator set", "updates", abci.ValidatorsString(respEndBlock.Diffs)) + if len(valDiff) > 0 { + log.Info("Update to validator set", "updates", abci.ValidatorsString(valDiff)) } - abciResponses.Validators = respEndBlock.Diffs return abciResponses, nil } @@ -229,7 +229,7 @@ func (s *State) ApplyBlock(eventCache types.Fireable, proxyAppConn proxy.AppConn fail.Fail() // XXX // now update the block and validators - s.SetBlockAndValidators(block.Header, partsHeader) + s.SetBlockAndValidators(block.Header, partsHeader, abciResponses) // lock mempool, commit state, update mempoool err = s.CommitStateUpdateMempool(proxyAppConn, block, mempool) diff --git a/state/state.go b/state/state.go index 8a27104cd..604f0892a 100644 --- a/state/state.go +++ b/state/state.go @@ -68,8 +68,6 @@ func loadState(db dbm.DB, key []byte) *State { } // TODO: ensure that buf is completely read. } - - s.LoadABCIResponses() return s } @@ -84,8 +82,7 @@ func (s *State) Copy() *State { Validators: s.Validators.Copy(), LastValidators: s.LastValidators.Copy(), AppHash: s.AppHash, - abciResponses: s.abciResponses, // pointer here, not value - TxIndexer: s.TxIndexer, // pointer here, not value + TxIndexer: s.TxIndexer, // pointer here, not value } } @@ -96,34 +93,36 @@ func (s *State) Save() { } // Sets the ABCIResponses in the state and writes them to disk +// in case we crash after app.Commit and before s.Save() func (s *State) SaveABCIResponses(abciResponses *ABCIResponses) { - s.abciResponses = abciResponses // save the validators to the db - s.db.SetSync(abciResponsesKey, s.abciResponses.Bytes()) + s.db.SetSync(abciResponsesKey, abciResponses.Bytes()) // save the tx results using the TxIndexer + // NOTE: these may be overwriting, but the values should be the same. batch := txindexer.NewBatch() - for i, r := range s.abciResponses.TxResults { - tx := s.abciResponses.Txs[i] - batch.Index(tx.Hash(), *r) + for i, d := range abciResponses.DeliverTx { + tx := abciResponses.txs[i] + batch.Index(tx.Hash(), types.TxResult{uint64(abciResponses.height), uint32(i), *d}) } s.TxIndexer.Batch(batch) } -func (s *State) LoadABCIResponses() { - s.abciResponses = new(ABCIResponses) +func (s *State) LoadABCIResponses() *ABCIResponses { + abciResponses := new(ABCIResponses) buf := s.db.Get(abciResponsesKey) if len(buf) != 0 { r, n, err := bytes.NewReader(buf), new(int), new(error) - wire.ReadBinaryPtr(&s.abciResponses.Validators, r, 0, n, err) + wire.ReadBinaryPtr(&abciResponses.EndBlock.Diffs, r, 0, n, err) if *err != nil { // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED Exit(Fmt("Data has been corrupted or its spec has changed: %v\n", *err)) } // TODO: ensure that buf is completely read. } + return abciResponses } func (s *State) Equals(s2 *State) bool { @@ -141,14 +140,14 @@ func (s *State) Bytes() []byte { // Mutate state variables to match block and validators // after running EndBlock -func (s *State) SetBlockAndValidators(header *types.Header, blockPartsHeader types.PartSetHeader) { +func (s *State) SetBlockAndValidators(header *types.Header, blockPartsHeader types.PartSetHeader, abciResponses *ABCIResponses) { // copy the valset prevValSet := s.Validators.Copy() nextValSet := prevValSet.Copy() // update the validator set with the latest abciResponses - err := updateValidators(nextValSet, s.abciResponses.Validators) + err := updateValidators(nextValSet, abciResponses.EndBlock.Diffs) if err != nil { log.Warn("Error changing validator set", "error", err) // TODO: err or carry on? @@ -192,23 +191,25 @@ func GetState(config cfg.Config, stateDB dbm.DB) *State { // ABCIResponses holds intermediate state during block processing type ABCIResponses struct { - Validators []*abci.Validator // changes to the validator set + height int + txs types.Txs // for reference later - Txs types.Txs // for reference later - TxResults []*types.TxResult // results of the txs, populated in the proxyCb + DeliverTx []*abci.ResponseDeliverTx // results of the txs, populated in the proxyCb + EndBlock abci.ResponseEndBlock // changes to the validator set } func NewABCIResponses(block *types.Block) *ABCIResponses { return &ABCIResponses{ - Txs: block.Data.Txs, - TxResults: make([]*types.TxResult, block.NumTxs), + height: block.Height, + txs: block.Data.Txs, + DeliverTx: make([]*abci.ResponseDeliverTx, block.NumTxs), } } // Serialize the list of validators func (a *ABCIResponses) Bytes() []byte { buf, n, err := new(bytes.Buffer), new(int), new(error) - wire.WriteBinary(a.Validators, buf, n, err) + wire.WriteBinary(a.EndBlock, buf, n, err) if *err != nil { PanicCrisis(*err) }