2015-03-28 23:44:07 -07:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2016-09-11 12:32:33 -07:00
|
|
|
"bytes"
|
2015-03-28 23:44:07 -07:00
|
|
|
"errors"
|
|
|
|
|
2016-09-11 12:32:33 -07:00
|
|
|
"github.com/ebuchman/fail-test"
|
|
|
|
|
2017-02-14 12:33:14 -08:00
|
|
|
abci "github.com/tendermint/abci/types"
|
2015-10-22 17:39:06 -07:00
|
|
|
. "github.com/tendermint/go-common"
|
2016-11-16 13:13:17 -08:00
|
|
|
cfg "github.com/tendermint/go-config"
|
2016-11-19 16:32:35 -08:00
|
|
|
"github.com/tendermint/go-crypto"
|
2015-12-01 20:12:01 -08:00
|
|
|
"github.com/tendermint/tendermint/proxy"
|
2015-04-01 17:30:16 -07:00
|
|
|
"github.com/tendermint/tendermint/types"
|
2015-03-28 23:44:07 -07:00
|
|
|
)
|
|
|
|
|
2016-09-11 10:16:23 -07:00
|
|
|
//--------------------------------------------------
|
|
|
|
// Execute the block
|
|
|
|
|
2015-12-01 20:12:01 -08:00
|
|
|
// Execute the block to mutate State.
|
2016-01-06 17:14:20 -08:00
|
|
|
// Validates block and then executes Data.Txs in the block.
|
2016-10-09 23:58:13 -07:00
|
|
|
func (s *State) ExecBlock(eventCache types.Fireable, proxyAppConn proxy.AppConnConsensus, block *types.Block, blockPartsHeader types.PartSetHeader) error {
|
2015-12-01 20:12:01 -08:00
|
|
|
|
|
|
|
// Validate the block.
|
2016-11-19 16:32:35 -08:00
|
|
|
if err := s.validateBlock(block); err != nil {
|
2016-09-11 10:16:23 -07:00
|
|
|
return ErrInvalidBlock(err)
|
2015-03-28 23:44:07 -07:00
|
|
|
}
|
2015-12-01 20:12:01 -08:00
|
|
|
|
2016-11-19 16:32:35 -08:00
|
|
|
// compute bitarray of validators that signed
|
2016-11-16 17:58:53 -08:00
|
|
|
signed := commitBitArrayFromBlock(block)
|
2016-11-19 16:32:35 -08:00
|
|
|
_ = signed // TODO send on begin block
|
2016-11-16 17:58:53 -08:00
|
|
|
|
2016-11-19 16:32:35 -08:00
|
|
|
// copy the valset
|
|
|
|
valSet := s.Validators.Copy()
|
2015-12-01 20:12:01 -08:00
|
|
|
nextValSet := valSet.Copy()
|
|
|
|
|
2016-01-06 17:14:20 -08:00
|
|
|
// Execute the block txs
|
2016-11-19 16:32:35 -08:00
|
|
|
changedValidators, err := execBlockOnProxyApp(eventCache, proxyAppConn, block)
|
2015-12-01 20:12:01 -08:00
|
|
|
if err != nil {
|
2016-01-06 17:14:20 -08:00
|
|
|
// There was some error in proxyApp
|
|
|
|
// TODO Report error and wait for proxyApp to be available.
|
2016-09-11 10:16:23 -07:00
|
|
|
return ErrProxyAppConn(err)
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
|
|
|
|
2016-11-19 16:32:35 -08:00
|
|
|
// update the validator set
|
2016-12-01 21:12:06 -08:00
|
|
|
err = updateValidators(nextValSet, changedValidators)
|
2016-11-19 17:29:07 -08:00
|
|
|
if err != nil {
|
2016-11-19 16:32:35 -08:00
|
|
|
log.Warn("Error changing validator set", "error", err)
|
|
|
|
// TODO: err or carry on?
|
|
|
|
}
|
|
|
|
|
2015-12-01 20:12:01 -08:00
|
|
|
// All good!
|
2016-08-23 18:44:07 -07:00
|
|
|
// Update validator accums and set state variables
|
2015-12-01 20:12:01 -08:00
|
|
|
nextValSet.IncrementAccum(1)
|
2016-08-23 18:44:07 -07:00
|
|
|
s.SetBlockAndValidators(block.Header, blockPartsHeader, valSet, nextValSet)
|
2015-12-01 20:12:01 -08:00
|
|
|
|
2016-09-11 10:16:23 -07:00
|
|
|
// save state with updated height/blockhash/validators
|
|
|
|
// but stale apphash, in case we fail between Commit and Save
|
2016-12-22 12:01:22 -08:00
|
|
|
s.SaveIntermediate()
|
|
|
|
|
|
|
|
fail.Fail() // XXX
|
2016-09-11 10:16:23 -07:00
|
|
|
|
2015-03-28 23:44:07 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-06 17:14:20 -08:00
|
|
|
// Executes block's transactions on proxyAppConn.
|
2016-11-19 16:32:35 -08:00
|
|
|
// Returns a list of updates to the validator set
|
2016-01-06 17:14:20 -08:00
|
|
|
// TODO: Generate a bitmap or otherwise store tx validity in state.
|
2017-01-12 12:53:32 -08:00
|
|
|
func execBlockOnProxyApp(eventCache types.Fireable, proxyAppConn proxy.AppConnConsensus, block *types.Block) ([]*abci.Validator, error) {
|
2016-06-27 17:43:09 -07:00
|
|
|
|
2016-01-06 17:14:20 -08:00
|
|
|
var validTxs, invalidTxs = 0, 0
|
2015-12-01 20:12:01 -08:00
|
|
|
|
|
|
|
// Execute transactions and get hash
|
2017-01-12 12:53:32 -08:00
|
|
|
proxyCb := func(req *abci.Request, res *abci.Response) {
|
2016-05-14 09:33:27 -07:00
|
|
|
switch r := res.Value.(type) {
|
2017-01-12 12:55:03 -08:00
|
|
|
case *abci.Response_DeliverTx:
|
2016-01-25 14:34:08 -08:00
|
|
|
// TODO: make use of res.Log
|
2016-01-06 17:14:20 -08:00
|
|
|
// TODO: make use of this info
|
|
|
|
// Blocks may include invalid txs.
|
2017-01-12 12:55:03 -08:00
|
|
|
// reqDeliverTx := req.(abci.RequestDeliverTx)
|
2016-10-01 13:12:48 -07:00
|
|
|
txError := ""
|
2017-01-12 12:55:03 -08:00
|
|
|
apTx := r.DeliverTx
|
2017-01-12 12:53:32 -08:00
|
|
|
if apTx.Code == abci.CodeType_OK {
|
2016-01-06 17:14:20 -08:00
|
|
|
validTxs += 1
|
|
|
|
} else {
|
2017-01-12 12:55:03 -08:00
|
|
|
log.Debug("Invalid tx", "code", r.DeliverTx.Code, "log", r.DeliverTx.Log)
|
2016-01-06 17:14:20 -08:00
|
|
|
invalidTxs += 1
|
2016-10-09 23:58:13 -07:00
|
|
|
txError = apTx.Code.String()
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
2016-06-27 17:43:09 -07:00
|
|
|
// NOTE: if we count we can access the tx from the block instead of
|
|
|
|
// pulling it from the req
|
2016-10-01 13:12:48 -07:00
|
|
|
event := types.EventDataTx{
|
2017-01-12 12:55:03 -08:00
|
|
|
Tx: req.GetDeliverTx().Tx,
|
2016-11-30 14:28:41 -08:00
|
|
|
Data: apTx.Data,
|
|
|
|
Code: apTx.Code,
|
|
|
|
Log: apTx.Log,
|
|
|
|
Error: txError,
|
2016-10-01 13:12:48 -07:00
|
|
|
}
|
2016-10-09 23:58:13 -07:00
|
|
|
types.FireEventTx(eventCache, event)
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
|
|
|
}
|
2016-01-06 17:14:20 -08:00
|
|
|
proxyAppConn.SetResponseCallback(proxyCb)
|
|
|
|
|
2016-11-03 16:51:22 -07:00
|
|
|
// Begin block
|
2016-09-10 20:35:18 -07:00
|
|
|
err := proxyAppConn.BeginBlockSync(block.Hash(), types.TM2PB.Header(block.Header))
|
2016-11-03 16:51:22 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Warn("Error in proxyAppConn.BeginBlock", "error", err)
|
2016-11-19 16:32:35 -08:00
|
|
|
return nil, err
|
2016-11-03 16:51:22 -07:00
|
|
|
}
|
2016-08-06 20:30:46 -07:00
|
|
|
|
2016-09-11 12:32:33 -07:00
|
|
|
fail.Fail() // XXX
|
|
|
|
|
2016-03-05 20:57:36 -08:00
|
|
|
// Run txs of block
|
2016-01-06 17:14:20 -08:00
|
|
|
for _, tx := range block.Txs {
|
2016-09-11 12:32:33 -07:00
|
|
|
fail.FailRand(len(block.Txs)) // XXX
|
2017-01-12 12:55:03 -08:00
|
|
|
proxyAppConn.DeliverTxAsync(tx)
|
2016-01-06 17:14:20 -08:00
|
|
|
if err := proxyAppConn.Error(); err != nil {
|
2016-11-19 16:32:35 -08:00
|
|
|
return nil, err
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
|
|
|
}
|
2016-03-05 20:57:36 -08:00
|
|
|
|
2016-09-11 12:32:33 -07:00
|
|
|
fail.Fail() // XXX
|
|
|
|
|
2016-03-05 20:57:36 -08:00
|
|
|
// End block
|
2017-01-12 12:13:47 -08:00
|
|
|
respEndBlock, err := proxyAppConn.EndBlockSync(uint64(block.Height))
|
2016-03-05 20:57:36 -08:00
|
|
|
if err != nil {
|
|
|
|
log.Warn("Error in proxyAppConn.EndBlock", "error", err)
|
2016-11-19 16:32:35 -08:00
|
|
|
return nil, err
|
2016-03-05 20:57:36 -08:00
|
|
|
}
|
2016-09-11 12:32:33 -07:00
|
|
|
|
|
|
|
fail.Fail() // XXX
|
|
|
|
|
2016-11-22 15:55:42 -08:00
|
|
|
log.Info("Executed block", "height", block.Height, "valid txs", validTxs, "invalid txs", invalidTxs)
|
2017-01-12 12:13:47 -08:00
|
|
|
if len(respEndBlock.Diffs) > 0 {
|
2017-01-12 12:53:32 -08:00
|
|
|
log.Info("Update to validator set", "updates", abci.ValidatorsString(respEndBlock.Diffs))
|
2016-11-22 15:55:42 -08:00
|
|
|
}
|
2017-01-12 12:13:47 -08:00
|
|
|
return respEndBlock.Diffs, nil
|
2016-11-19 16:32:35 -08:00
|
|
|
}
|
|
|
|
|
2017-01-12 12:53:32 -08:00
|
|
|
func updateValidators(validators *types.ValidatorSet, changedValidators []*abci.Validator) error {
|
2016-11-19 16:32:35 -08:00
|
|
|
// TODO: prevent change of 1/3+ at once
|
|
|
|
|
|
|
|
for _, v := range changedValidators {
|
|
|
|
pubkey, err := crypto.PubKeyFromBytes(v.PubKey) // NOTE: expects go-wire encoded pubkey
|
|
|
|
if err != nil {
|
2016-12-01 21:12:06 -08:00
|
|
|
return err
|
2016-11-19 16:32:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
address := pubkey.Address()
|
|
|
|
power := int64(v.Power)
|
|
|
|
// mind the overflow from uint64
|
|
|
|
if power < 0 {
|
2016-12-01 21:12:06 -08:00
|
|
|
return errors.New(Fmt("Power (%d) overflows int64", v.Power))
|
2016-11-19 16:32:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
_, val := validators.GetByAddress(address)
|
|
|
|
if val == nil {
|
|
|
|
// add val
|
|
|
|
added := validators.Add(types.NewValidator(pubkey, power))
|
|
|
|
if !added {
|
2016-12-01 21:12:06 -08:00
|
|
|
return errors.New(Fmt("Failed to add new validator %X with voting power %d", address, power))
|
2016-11-19 16:32:35 -08:00
|
|
|
}
|
|
|
|
} else if v.Power == 0 {
|
|
|
|
// remove val
|
|
|
|
_, removed := validators.Remove(address)
|
|
|
|
if !removed {
|
2016-12-01 21:12:06 -08:00
|
|
|
return errors.New(Fmt("Failed to remove validator %X)"))
|
2016-11-19 16:32:35 -08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// update val
|
|
|
|
val.VotingPower = power
|
|
|
|
updated := validators.Update(val)
|
|
|
|
if !updated {
|
2016-12-01 21:12:06 -08:00
|
|
|
return errors.New(Fmt("Failed to update validator %X with voting power %d", address, power))
|
2016-11-19 16:32:35 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-01 21:12:06 -08:00
|
|
|
return nil
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
|
|
|
|
2016-11-16 17:58:53 -08:00
|
|
|
// return a bit array of validators that signed the last commit
|
|
|
|
// NOTE: assumes commits have already been authenticated
|
|
|
|
func commitBitArrayFromBlock(block *types.Block) *BitArray {
|
|
|
|
signed := NewBitArray(len(block.LastCommit.Precommits))
|
2016-04-02 09:10:16 -07:00
|
|
|
for i, precommit := range block.LastCommit.Precommits {
|
2016-11-22 15:55:42 -08:00
|
|
|
if precommit != nil {
|
|
|
|
signed.SetIndex(i, true) // val_.LastCommitHeight = block.Height - 1
|
2015-03-28 23:44:07 -07:00
|
|
|
}
|
|
|
|
}
|
2016-11-16 17:58:53 -08:00
|
|
|
return signed
|
2015-05-12 17:40:19 -07:00
|
|
|
}
|
2015-08-10 20:38:45 -07:00
|
|
|
|
2016-09-11 10:16:23 -07:00
|
|
|
//-----------------------------------------------------
|
|
|
|
// Validate block
|
2015-08-10 20:38:45 -07:00
|
|
|
|
2016-09-11 10:16:23 -07:00
|
|
|
func (s *State) ValidateBlock(block *types.Block) error {
|
|
|
|
return s.validateBlock(block)
|
2015-08-10 20:38:45 -07:00
|
|
|
}
|
|
|
|
|
2016-09-11 10:16:23 -07:00
|
|
|
func (s *State) validateBlock(block *types.Block) error {
|
|
|
|
// Basic block validation.
|
|
|
|
err := block.ValidateBasic(s.ChainID, s.LastBlockHeight, s.LastBlockID, s.LastBlockTime, s.AppHash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate block LastCommit.
|
|
|
|
if block.Height == 1 {
|
|
|
|
if len(block.LastCommit.Precommits) != 0 {
|
|
|
|
return errors.New("Block at height 1 (first block) should have no LastCommit precommits")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if len(block.LastCommit.Precommits) != s.LastValidators.Size() {
|
|
|
|
return errors.New(Fmt("Invalid block commit size. Expected %v, got %v",
|
|
|
|
s.LastValidators.Size(), len(block.LastCommit.Precommits)))
|
|
|
|
}
|
|
|
|
err := s.LastValidators.VerifyCommit(
|
|
|
|
s.ChainID, s.LastBlockID, block.Height-1, block.LastCommit)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
2016-08-23 18:44:07 -07:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2016-09-11 10:16:23 -07:00
|
|
|
// ApplyBlock executes the block, then commits and updates the mempool atomically
|
|
|
|
|
|
|
|
// Execute and commit block against app, save block and state
|
2016-11-03 17:38:09 -07:00
|
|
|
func (s *State) ApplyBlock(eventCache types.Fireable, proxyAppConn proxy.AppConnConsensus,
|
2016-09-11 10:16:23 -07:00
|
|
|
block *types.Block, partsHeader types.PartSetHeader, mempool Mempool) error {
|
|
|
|
|
|
|
|
// Run the block on the State:
|
|
|
|
// + update validator sets
|
|
|
|
// + run txs on the proxyAppConn
|
|
|
|
err := s.ExecBlock(eventCache, proxyAppConn, block, partsHeader)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(Fmt("Exec failed for application: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
// lock mempool, commit state, update mempoool
|
|
|
|
err = s.CommitStateUpdateMempool(proxyAppConn, block, mempool)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(Fmt("Commit failed for application: %v", err))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-08-23 18:44:07 -07:00
|
|
|
|
2016-08-24 21:18:03 -07:00
|
|
|
// mempool must be locked during commit and update
|
|
|
|
// because state is typically reset on Commit and old txs must be replayed
|
|
|
|
// against committed state before new txs are run in the mempool, lest they be invalid
|
|
|
|
func (s *State) CommitStateUpdateMempool(proxyAppConn proxy.AppConnConsensus, block *types.Block, mempool Mempool) error {
|
|
|
|
mempool.Lock()
|
|
|
|
defer mempool.Unlock()
|
|
|
|
|
|
|
|
// Commit block, get hash back
|
|
|
|
res := proxyAppConn.CommitSync()
|
|
|
|
if res.IsErr() {
|
|
|
|
log.Warn("Error in proxyAppConn.CommitSync", "error", res)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
if res.Log != "" {
|
|
|
|
log.Debug("Commit.Log: " + res.Log)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the state's new AppHash
|
|
|
|
s.AppHash = res.Data
|
|
|
|
|
|
|
|
// Update mempool.
|
|
|
|
mempool.Update(block.Height, block.Txs)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-11 10:16:23 -07:00
|
|
|
// Updates to the mempool need to be synchronized with committing a block
|
|
|
|
// so apps can reset their transient state on Commit
|
|
|
|
type Mempool interface {
|
|
|
|
Lock()
|
|
|
|
Unlock()
|
|
|
|
Update(height int, txs []types.Tx)
|
|
|
|
}
|
2016-08-24 21:18:03 -07:00
|
|
|
|
2016-12-06 20:01:55 -08:00
|
|
|
type MockMempool struct {
|
2016-09-11 10:16:23 -07:00
|
|
|
}
|
2016-08-24 21:18:03 -07:00
|
|
|
|
2016-12-06 20:01:55 -08:00
|
|
|
func (m MockMempool) Lock() {}
|
|
|
|
func (m MockMempool) Unlock() {}
|
|
|
|
func (m MockMempool) Update(height int, txs []types.Tx) {}
|
2016-09-11 10:16:23 -07:00
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
2016-11-05 18:48:39 -07:00
|
|
|
// Handshake with app to sync to latest state of core by replaying blocks
|
2016-09-11 10:16:23 -07:00
|
|
|
|
2016-11-05 18:48:39 -07:00
|
|
|
// TODO: Should we move blockchain/store.go to its own package?
|
|
|
|
type BlockStore interface {
|
|
|
|
Height() int
|
|
|
|
LoadBlock(height int) *types.Block
|
2016-11-22 15:55:42 -08:00
|
|
|
LoadBlockMeta(height int) *types.BlockMeta
|
2016-09-11 10:16:23 -07:00
|
|
|
}
|
|
|
|
|
2016-11-05 18:48:39 -07:00
|
|
|
type Handshaker struct {
|
2016-11-16 13:13:17 -08:00
|
|
|
config cfg.Config
|
|
|
|
state *State
|
|
|
|
store BlockStore
|
2016-09-11 10:16:23 -07:00
|
|
|
|
2016-11-05 18:48:39 -07:00
|
|
|
nBlocks int // number of blocks applied to the state
|
2016-09-11 12:32:33 -07:00
|
|
|
}
|
|
|
|
|
2016-11-16 13:13:17 -08:00
|
|
|
func NewHandshaker(config cfg.Config, state *State, store BlockStore) *Handshaker {
|
|
|
|
return &Handshaker{config, state, store, 0}
|
2016-09-11 12:32:33 -07:00
|
|
|
}
|
|
|
|
|
2016-11-22 15:55:42 -08:00
|
|
|
// TODO: retry the handshake/replay if it fails ?
|
2016-11-05 18:48:39 -07:00
|
|
|
func (h *Handshaker) Handshake(proxyApp proxy.AppConns) error {
|
|
|
|
// handshake is done via info request on the query conn
|
2017-01-12 12:13:47 -08:00
|
|
|
res, err := proxyApp.Query().InfoSync()
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(Fmt("Error calling Info: %v", err))
|
2016-11-05 18:48:39 -07:00
|
|
|
}
|
|
|
|
|
2017-01-12 12:13:47 -08:00
|
|
|
blockHeight := int(res.LastBlockHeight) // XXX: beware overflow
|
|
|
|
appHash := res.LastBlockAppHash
|
2016-11-05 18:48:39 -07:00
|
|
|
|
2017-01-12 12:53:32 -08:00
|
|
|
log.Notice("ABCI Handshake", "appHeight", blockHeight, "appHash", appHash)
|
2016-11-05 18:48:39 -07:00
|
|
|
|
2017-01-12 12:13:47 -08:00
|
|
|
// TODO: check version
|
2016-09-11 10:16:23 -07:00
|
|
|
|
2016-11-05 18:48:39 -07:00
|
|
|
// replay blocks up to the latest in the blockstore
|
2017-01-12 12:13:47 -08:00
|
|
|
err = h.ReplayBlocks(appHash, blockHeight, proxyApp.Consensus())
|
2016-11-05 18:48:39 -07:00
|
|
|
if err != nil {
|
|
|
|
return errors.New(Fmt("Error on replay: %v", err))
|
|
|
|
}
|
|
|
|
|
2016-12-22 16:30:09 -08:00
|
|
|
// Save the state
|
|
|
|
h.state.Save()
|
|
|
|
|
2016-11-05 18:48:39 -07:00
|
|
|
// TODO: (on restart) replay mempool
|
|
|
|
|
|
|
|
return nil
|
2016-08-24 21:18:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replay all blocks after blockHeight and ensure the result matches the current state.
|
2016-11-22 15:55:42 -08:00
|
|
|
func (h *Handshaker) ReplayBlocks(appHash []byte, appBlockHeight int, appConnConsensus proxy.AppConnConsensus) error {
|
2016-08-23 18:44:07 -07:00
|
|
|
|
2016-11-22 15:55:42 -08:00
|
|
|
storeBlockHeight := h.store.Height()
|
2016-12-22 12:01:22 -08:00
|
|
|
stateBlockHeight := h.state.LastBlockHeight
|
2017-01-12 12:53:32 -08:00
|
|
|
log.Notice("ABCI Replay Blocks", "appHeight", appBlockHeight, "storeHeight", storeBlockHeight, "stateHeight", stateBlockHeight)
|
2016-12-22 12:01:22 -08:00
|
|
|
|
|
|
|
if storeBlockHeight == 0 {
|
|
|
|
return nil
|
|
|
|
} else if storeBlockHeight < appBlockHeight {
|
2016-09-11 12:32:33 -07:00
|
|
|
// if the app is ahead, there's nothing we can do
|
2016-11-22 15:55:42 -08:00
|
|
|
return ErrAppBlockHeightTooHigh{storeBlockHeight, appBlockHeight}
|
2016-09-11 10:16:23 -07:00
|
|
|
|
2016-11-22 15:55:42 -08:00
|
|
|
} else if storeBlockHeight == appBlockHeight {
|
2016-12-22 12:01:22 -08:00
|
|
|
// We ran Commit, but if we crashed before state.Save(),
|
|
|
|
// load the intermediate state and update the state.AppHash.
|
2017-01-12 12:53:32 -08:00
|
|
|
// NOTE: If ABCI allowed rollbacks, we could just replay the
|
2016-12-22 12:01:22 -08:00
|
|
|
// block even though it's been committed
|
|
|
|
stateAppHash := h.state.AppHash
|
|
|
|
lastBlockAppHash := h.store.LoadBlock(storeBlockHeight).AppHash
|
|
|
|
|
|
|
|
if bytes.Equal(stateAppHash, appHash) {
|
|
|
|
// we're all synced up
|
2017-01-12 12:53:32 -08:00
|
|
|
log.Debug("ABCI RelpayBlocks: Already synced")
|
2016-12-22 12:01:22 -08:00
|
|
|
} else if bytes.Equal(stateAppHash, lastBlockAppHash) {
|
|
|
|
// we crashed after commit and before saving state,
|
|
|
|
// so load the intermediate state and update the hash
|
|
|
|
h.state.LoadIntermediate()
|
2016-11-05 18:48:39 -07:00
|
|
|
h.state.AppHash = appHash
|
2017-01-12 12:53:32 -08:00
|
|
|
log.Debug("ABCI RelpayBlocks: Loaded intermediate state and updated state.AppHash")
|
2016-12-22 12:01:22 -08:00
|
|
|
} else {
|
|
|
|
PanicSanity(Fmt("Unexpected state.AppHash: state.AppHash %X; app.AppHash %X, lastBlock.AppHash %X", stateAppHash, appHash, lastBlockAppHash))
|
|
|
|
|
2016-09-11 10:16:23 -07:00
|
|
|
}
|
2016-11-19 16:59:56 -08:00
|
|
|
return nil
|
2016-09-11 12:32:33 -07:00
|
|
|
|
2016-12-22 12:01:22 -08:00
|
|
|
} else if storeBlockHeight == appBlockHeight+1 &&
|
|
|
|
storeBlockHeight == stateBlockHeight+1 {
|
|
|
|
// We crashed after saving the block
|
|
|
|
// but before Commit (both the state and app are behind),
|
|
|
|
// so just replay the block
|
2016-09-11 12:32:33 -07:00
|
|
|
|
2016-12-22 12:01:22 -08:00
|
|
|
// check that the lastBlock.AppHash matches the state apphash
|
2016-11-22 15:55:42 -08:00
|
|
|
block := h.store.LoadBlock(storeBlockHeight)
|
2016-11-05 18:48:39 -07:00
|
|
|
if !bytes.Equal(block.Header.AppHash, appHash) {
|
2016-11-22 15:55:42 -08:00
|
|
|
return ErrLastStateMismatch{storeBlockHeight, block.Header.AppHash, appHash}
|
2016-09-11 12:32:33 -07:00
|
|
|
}
|
|
|
|
|
2016-11-22 15:55:42 -08:00
|
|
|
blockMeta := h.store.LoadBlockMeta(storeBlockHeight)
|
|
|
|
|
2016-11-19 16:59:56 -08:00
|
|
|
h.nBlocks += 1
|
|
|
|
var eventCache types.Fireable // nil
|
|
|
|
|
2016-12-22 12:01:22 -08:00
|
|
|
// replay the latest block
|
2017-02-14 12:33:14 -08:00
|
|
|
return h.state.ApplyBlock(eventCache, appConnConsensus, block, blockMeta.BlockID.PartsHeader, MockMempool{})
|
2016-12-22 12:01:22 -08:00
|
|
|
} else if storeBlockHeight != stateBlockHeight {
|
|
|
|
// unless we failed before committing or saving state (previous 2 case),
|
|
|
|
// the store and state should be at the same height!
|
|
|
|
PanicSanity(Fmt("Expected storeHeight (%d) and stateHeight (%d) to match.", storeBlockHeight, stateBlockHeight))
|
2016-09-11 10:16:23 -07:00
|
|
|
} else {
|
2016-12-22 12:01:22 -08:00
|
|
|
// store is more than one ahead,
|
|
|
|
// so app wants to replay many blocks
|
|
|
|
|
2016-09-11 10:16:23 -07:00
|
|
|
// replay all blocks starting with appBlockHeight+1
|
2016-11-19 16:59:56 -08:00
|
|
|
var eventCache types.Fireable // nil
|
2016-12-22 12:01:22 -08:00
|
|
|
|
2017-01-05 20:16:42 -08:00
|
|
|
// TODO: use stateBlockHeight instead and let the consensus state
|
|
|
|
// do the replay
|
|
|
|
|
2016-11-19 16:59:56 -08:00
|
|
|
var appHash []byte
|
2016-11-22 15:55:42 -08:00
|
|
|
for i := appBlockHeight + 1; i <= storeBlockHeight; i++ {
|
2016-11-19 16:59:56 -08:00
|
|
|
h.nBlocks += 1
|
|
|
|
block := h.store.LoadBlock(i)
|
|
|
|
_, err := execBlockOnProxyApp(eventCache, appConnConsensus, block)
|
|
|
|
if err != nil {
|
2016-11-22 15:55:42 -08:00
|
|
|
log.Warn("Error executing block on proxy app", "height", i, "err", err)
|
|
|
|
return err
|
2016-11-19 16:59:56 -08:00
|
|
|
}
|
|
|
|
// Commit block, get hash back
|
|
|
|
res := appConnConsensus.CommitSync()
|
|
|
|
if res.IsErr() {
|
|
|
|
log.Warn("Error in proxyAppConn.CommitSync", "error", res)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
if res.Log != "" {
|
|
|
|
log.Info("Commit.Log: " + res.Log)
|
|
|
|
}
|
|
|
|
appHash = res.Data
|
|
|
|
}
|
|
|
|
if !bytes.Equal(h.state.AppHash, appHash) {
|
2016-12-22 12:01:22 -08:00
|
|
|
return errors.New(Fmt("Tendermint state.AppHash does not match AppHash after replay. Got %X, expected %X", appHash, h.state.AppHash))
|
2016-09-11 10:16:23 -07:00
|
|
|
}
|
2016-11-22 15:55:42 -08:00
|
|
|
return nil
|
2016-08-24 21:18:03 -07:00
|
|
|
}
|
2016-12-22 12:01:22 -08:00
|
|
|
return nil
|
2016-08-24 21:18:03 -07:00
|
|
|
}
|