2015-03-28 23:44:07 -07:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2017-12-20 23:53:15 -05:00
|
|
|
"bytes"
|
2015-03-28 23:44:07 -07:00
|
|
|
"errors"
|
2017-01-19 13:33:58 +04:00
|
|
|
"fmt"
|
2015-03-28 23:44:07 -07:00
|
|
|
|
2017-01-19 13:33:58 +04:00
|
|
|
fail "github.com/ebuchman/fail-test"
|
2017-02-14 15:33:14 -05:00
|
|
|
abci "github.com/tendermint/abci/types"
|
2017-01-19 13:33:58 +04:00
|
|
|
crypto "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"
|
2017-12-27 19:21:16 -05:00
|
|
|
dbm "github.com/tendermint/tmlibs/db"
|
2017-05-02 11:53:32 +04:00
|
|
|
"github.com/tendermint/tmlibs/log"
|
2015-03-28 23:44:07 -07:00
|
|
|
)
|
|
|
|
|
2017-04-14 15:33:19 -04:00
|
|
|
//--------------------------------------------------
|
|
|
|
// Execute the block
|
|
|
|
|
2017-12-27 19:21:16 -05:00
|
|
|
// ValExecBlock executes the block and returns the responses. It does NOT mutate State.
|
2017-01-19 13:33:58 +04:00
|
|
|
// + validates the block
|
|
|
|
// + executes block.Txs on the proxyAppConn
|
2017-12-27 19:21:16 -05:00
|
|
|
func (blockExec *BlockExecutor) ValExecBlock(s State, block *types.Block) (*ABCIResponses, error) {
|
2016-11-19 19:32:35 -05:00
|
|
|
if err := s.validateBlock(block); err != nil {
|
2017-01-19 13:33:58 +04:00
|
|
|
return nil, ErrInvalidBlock(err)
|
2015-03-28 23:44:07 -07:00
|
|
|
}
|
2015-12-01 20:12:01 -08:00
|
|
|
|
2017-12-27 19:21:16 -05:00
|
|
|
abciResponses, err := execBlockOnProxyApp(blockExec.logger, blockExec.proxyApp, 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.
|
2017-01-19 13:33:58 +04:00
|
|
|
return nil, ErrProxyAppConn(err)
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
|
|
|
|
2017-04-14 15:33:19 -04:00
|
|
|
return abciResponses, nil
|
2015-03-28 23:44:07 -07:00
|
|
|
}
|
|
|
|
|
2016-01-06 17:14:20 -08:00
|
|
|
// Executes block's transactions on proxyAppConn.
|
2017-01-19 13:33:58 +04:00
|
|
|
// Returns a list of transaction results and updates to the validator set
|
2017-12-27 19:21:16 -05:00
|
|
|
func execBlockOnProxyApp(logger log.Logger, proxyAppConn proxy.AppConnConsensus, block *types.Block) (*ABCIResponses, error) {
|
2016-01-06 17:14:20 -08:00
|
|
|
var validTxs, invalidTxs = 0, 0
|
2015-12-01 20:12:01 -08:00
|
|
|
|
2017-04-10 22:41:07 +04:00
|
|
|
txIndex := 0
|
2017-04-14 15:33:19 -04:00
|
|
|
abciResponses := NewABCIResponses(block)
|
|
|
|
|
2015-12-01 20:12:01 -08:00
|
|
|
// Execute transactions and get hash
|
2017-01-12 15:53:32 -05:00
|
|
|
proxyCb := func(req *abci.Request, res *abci.Response) {
|
2016-05-14 12:33:27 -04:00
|
|
|
switch r := res.Value.(type) {
|
2017-01-12 15:55:03 -05: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-12-25 13:47:16 -05:00
|
|
|
txRes := r.DeliverTx
|
|
|
|
if txRes.Code == abci.CodeTypeOK {
|
2017-01-19 13:33:58 +04:00
|
|
|
validTxs++
|
2016-01-06 17:14:20 -08:00
|
|
|
} else {
|
2017-12-25 13:47:16 -05:00
|
|
|
logger.Debug("Invalid tx", "code", txRes.Code, "log", txRes.Log)
|
2017-01-19 13:33:58 +04:00
|
|
|
invalidTxs++
|
|
|
|
}
|
2017-12-25 13:47:16 -05:00
|
|
|
abciResponses.DeliverTx[txIndex] = txRes
|
2017-11-15 15:07:08 -06:00
|
|
|
txIndex++
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
|
|
|
}
|
2016-01-06 17:14:20 -08:00
|
|
|
proxyAppConn.SetResponseCallback(proxyCb)
|
|
|
|
|
2017-12-15 12:12:45 -06:00
|
|
|
// determine which validators did not sign last block
|
2017-12-11 11:36:04 -06:00
|
|
|
absentVals := make([]int32, 0)
|
2017-12-15 12:12:45 -06:00
|
|
|
for valI, vote := range block.LastCommit.Precommits {
|
|
|
|
if vote == nil {
|
|
|
|
absentVals = append(absentVals, int32(valI))
|
2017-12-11 11:36:04 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-25 13:47:16 -05:00
|
|
|
// TODO: determine which validators were byzantine
|
|
|
|
|
2016-11-03 19:51:22 -04:00
|
|
|
// Begin block
|
2017-11-29 11:22:52 -06:00
|
|
|
_, err := proxyAppConn.BeginBlockSync(abci.RequestBeginBlock{
|
2017-12-11 11:36:04 -06:00
|
|
|
Hash: block.Hash(),
|
|
|
|
Header: types.TM2PB.Header(block.Header),
|
|
|
|
AbsentValidators: absentVals,
|
|
|
|
ByzantineValidators: nil,
|
2017-09-22 11:42:40 -04:00
|
|
|
})
|
2016-11-03 19:51:22 -04:00
|
|
|
if err != nil {
|
2017-06-14 12:50:49 +04:00
|
|
|
logger.Error("Error in proxyAppConn.BeginBlock", "err", err)
|
2017-04-14 15:33:19 -04:00
|
|
|
return nil, err
|
2016-11-03 19:51:22 -04:00
|
|
|
}
|
2016-08-06 23:30:46 -04:00
|
|
|
|
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 {
|
2017-01-12 15:55:03 -05:00
|
|
|
proxyAppConn.DeliverTxAsync(tx)
|
2016-01-06 17:14:20 -08:00
|
|
|
if err := proxyAppConn.Error(); err != nil {
|
2017-04-14 15:33:19 -04:00
|
|
|
return nil, err
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
|
|
|
}
|
2016-03-05 20:57:36 -08:00
|
|
|
|
|
|
|
// End block
|
2017-12-02 01:47:55 -05:00
|
|
|
abciResponses.EndBlock, err = proxyAppConn.EndBlockSync(abci.RequestEndBlock{block.Height})
|
2016-03-05 20:57:36 -08:00
|
|
|
if err != nil {
|
2017-06-14 12:50:49 +04:00
|
|
|
logger.Error("Error in proxyAppConn.EndBlock", "err", err)
|
2017-04-14 15:33:19 -04:00
|
|
|
return nil, err
|
2016-03-05 20:57:36 -08:00
|
|
|
}
|
2016-09-11 15:32:33 -04:00
|
|
|
|
2017-06-14 14:41:36 +08:00
|
|
|
logger.Info("Executed block", "height", block.Height, "validTxs", validTxs, "invalidTxs", invalidTxs)
|
2017-12-22 14:07:46 -06:00
|
|
|
|
|
|
|
valUpdates := abciResponses.EndBlock.ValidatorUpdates
|
2017-12-21 11:52:26 -06:00
|
|
|
if len(valUpdates) > 0 {
|
|
|
|
logger.Info("Updates to validators", "updates", abci.ValidatorsString(valUpdates))
|
2016-11-22 18:55:42 -05:00
|
|
|
}
|
2017-04-14 15:33:19 -04:00
|
|
|
|
|
|
|
return abciResponses, nil
|
2016-11-19 19:32:35 -05:00
|
|
|
}
|
|
|
|
|
2017-12-22 14:07:46 -06:00
|
|
|
func updateValidators(currentSet *types.ValidatorSet, updates []*abci.Validator) error {
|
2017-12-25 14:07:45 -06:00
|
|
|
// If more or equal than 1/3 of total voting power changed in one block, then
|
|
|
|
// a light client could never prove the transition externally. See
|
|
|
|
// ./lite/doc.go for details on how a light client tracks validators.
|
|
|
|
vp23, err := changeInVotingPowerMoreOrEqualToOneThird(currentSet, updates)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-12-22 14:07:46 -06:00
|
|
|
}
|
2017-12-25 14:07:45 -06:00
|
|
|
if vp23 {
|
|
|
|
return errors.New("the change in voting power must be strictly less than 1/3")
|
2017-12-22 14:07:46 -06:00
|
|
|
}
|
2016-11-19 19:32:35 -05:00
|
|
|
|
2017-12-22 14:07:46 -06:00
|
|
|
for _, v := range updates {
|
2016-11-19 19:32:35 -05:00
|
|
|
pubkey, err := crypto.PubKeyFromBytes(v.PubKey) // NOTE: expects go-wire encoded pubkey
|
|
|
|
if err != nil {
|
2016-12-02 00:12:06 -05:00
|
|
|
return err
|
2016-11-19 19:32:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
address := pubkey.Address()
|
|
|
|
power := int64(v.Power)
|
2017-12-01 19:04:53 -06:00
|
|
|
// mind the overflow from int64
|
2016-11-19 19:32:35 -05:00
|
|
|
if power < 0 {
|
2017-12-22 14:07:46 -06:00
|
|
|
return fmt.Errorf("Power (%d) overflows int64", v.Power)
|
2016-11-19 19:32:35 -05:00
|
|
|
}
|
|
|
|
|
2017-12-22 14:07:46 -06:00
|
|
|
_, val := currentSet.GetByAddress(address)
|
2016-11-19 19:32:35 -05:00
|
|
|
if val == nil {
|
|
|
|
// add val
|
2017-12-22 14:07:46 -06:00
|
|
|
added := currentSet.Add(types.NewValidator(pubkey, power))
|
2016-11-19 19:32:35 -05:00
|
|
|
if !added {
|
2017-12-22 14:07:46 -06:00
|
|
|
return fmt.Errorf("Failed to add new validator %X with voting power %d", address, power)
|
2016-11-19 19:32:35 -05:00
|
|
|
}
|
|
|
|
} else if v.Power == 0 {
|
|
|
|
// remove val
|
2017-12-22 14:07:46 -06:00
|
|
|
_, removed := currentSet.Remove(address)
|
2016-11-19 19:32:35 -05:00
|
|
|
if !removed {
|
2017-12-22 14:07:46 -06:00
|
|
|
return fmt.Errorf("Failed to remove validator %X", address)
|
2016-11-19 19:32:35 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// update val
|
|
|
|
val.VotingPower = power
|
2017-12-22 14:07:46 -06:00
|
|
|
updated := currentSet.Update(val)
|
2016-11-19 19:32:35 -05:00
|
|
|
if !updated {
|
2017-12-22 14:07:46 -06:00
|
|
|
return fmt.Errorf("Failed to update validator %X with voting power %d", address, power)
|
2016-11-19 19:32:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-02 00:12:06 -05:00
|
|
|
return nil
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
|
|
|
|
2017-12-25 14:07:45 -06:00
|
|
|
func changeInVotingPowerMoreOrEqualToOneThird(currentSet *types.ValidatorSet, updates []*abci.Validator) (bool, error) {
|
|
|
|
threshold := currentSet.TotalVotingPower() * 1 / 3
|
|
|
|
acc := int64(0)
|
|
|
|
|
|
|
|
for _, v := range updates {
|
|
|
|
pubkey, err := crypto.PubKeyFromBytes(v.PubKey) // NOTE: expects go-wire encoded pubkey
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
address := pubkey.Address()
|
|
|
|
power := int64(v.Power)
|
|
|
|
// mind the overflow from int64
|
|
|
|
if power < 0 {
|
|
|
|
return false, fmt.Errorf("Power (%d) overflows int64", v.Power)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, val := currentSet.GetByAddress(address)
|
|
|
|
if val == nil {
|
|
|
|
acc += power
|
|
|
|
} else {
|
|
|
|
np := val.VotingPower - power
|
|
|
|
if np < 0 {
|
|
|
|
np = -np
|
|
|
|
}
|
|
|
|
acc += np
|
|
|
|
}
|
|
|
|
|
|
|
|
if acc >= threshold {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2016-09-11 13:16:23 -04:00
|
|
|
//-----------------------------------------------------
|
|
|
|
// Validate block
|
2015-08-10 20:38:45 -07:00
|
|
|
|
2017-12-20 23:53:15 -05:00
|
|
|
// MakeBlock builds a block with the given txs and commit from the current state.
|
2017-12-27 17:50:16 -05:00
|
|
|
func (s State) MakeBlock(height int64, txs []types.Tx, commit *types.Commit) (*types.Block, *types.PartSet) {
|
2017-12-20 23:53:15 -05:00
|
|
|
// build base block
|
|
|
|
block := types.MakeBlock(height, txs, commit)
|
|
|
|
|
|
|
|
// fill header with state data
|
|
|
|
block.ChainID = s.ChainID
|
|
|
|
block.TotalTxs = s.LastBlockTotalTx + block.NumTxs
|
|
|
|
block.LastBlockID = s.LastBlockID
|
|
|
|
block.ValidatorsHash = s.Validators.Hash()
|
|
|
|
block.AppHash = s.AppHash
|
2017-12-26 19:56:39 -05:00
|
|
|
block.ConsensusHash = s.ConsensusParams.Hash()
|
2017-12-26 19:53:26 -05:00
|
|
|
block.LastResultsHash = s.LastResultsHash
|
2017-12-20 23:53:15 -05:00
|
|
|
|
|
|
|
return block, block.MakePartSet(s.ConsensusParams.BlockGossip.BlockPartSizeBytes)
|
|
|
|
}
|
|
|
|
|
2017-12-27 14:27:37 -05:00
|
|
|
// ValidateBlock validates the block against the state.
|
|
|
|
func (s State) ValidateBlock(block *types.Block) error {
|
|
|
|
return s.validateBlock(block)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s State) validateBlock(b *types.Block) error {
|
2017-12-26 20:00:45 -05:00
|
|
|
// validate internal consistency
|
2017-12-20 23:53:15 -05:00
|
|
|
if err := b.ValidateBasic(); err != nil {
|
2016-09-11 13:16:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-26 20:00:45 -05:00
|
|
|
// validate basic info
|
2017-12-20 23:53:15 -05:00
|
|
|
if b.ChainID != s.ChainID {
|
|
|
|
return fmt.Errorf("Wrong Block.Header.ChainID. Expected %v, got %v", s.ChainID, b.ChainID)
|
|
|
|
}
|
|
|
|
if b.Height != s.LastBlockHeight+1 {
|
|
|
|
return fmt.Errorf("Wrong Block.Header.Height. Expected %v, got %v", s.LastBlockHeight+1, b.Height)
|
|
|
|
}
|
|
|
|
/* TODO: Determine bounds for Time
|
|
|
|
See blockchain/reactor "stopSyncingDurationMinutes"
|
|
|
|
|
|
|
|
if !b.Time.After(lastBlockTime) {
|
|
|
|
return errors.New("Invalid Block.Header.Time")
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2017-12-26 20:00:45 -05:00
|
|
|
// validate prev block info
|
|
|
|
if !b.LastBlockID.Equals(s.LastBlockID) {
|
|
|
|
return fmt.Errorf("Wrong Block.Header.LastBlockID. Expected %v, got %v", s.LastBlockID, b.LastBlockID)
|
|
|
|
}
|
2017-12-20 23:53:15 -05:00
|
|
|
newTxs := int64(len(b.Data.Txs))
|
|
|
|
if b.TotalTxs != s.LastBlockTotalTx+newTxs {
|
|
|
|
return fmt.Errorf("Wrong Block.Header.TotalTxs. Expected %v, got %v", s.LastBlockTotalTx+newTxs, b.TotalTxs)
|
|
|
|
}
|
|
|
|
|
2017-12-26 20:00:45 -05:00
|
|
|
// validate app info
|
2017-12-20 23:53:15 -05:00
|
|
|
if !bytes.Equal(b.AppHash, s.AppHash) {
|
|
|
|
return fmt.Errorf("Wrong Block.Header.AppHash. Expected %X, got %v", s.AppHash, b.AppHash)
|
|
|
|
}
|
2017-12-26 19:56:39 -05:00
|
|
|
if !bytes.Equal(b.ConsensusHash, s.ConsensusParams.Hash()) {
|
|
|
|
return fmt.Errorf("Wrong Block.Header.ConsensusHash. Expected %X, got %v", s.ConsensusParams.Hash(), b.ConsensusHash)
|
2017-12-20 23:53:15 -05:00
|
|
|
}
|
2017-12-26 19:53:26 -05:00
|
|
|
if !bytes.Equal(b.LastResultsHash, s.LastResultsHash) {
|
|
|
|
return fmt.Errorf("Wrong Block.Header.LastResultsHash. Expected %X, got %v", s.LastResultsHash, b.LastResultsHash)
|
2017-12-22 16:43:45 +01:00
|
|
|
}
|
2017-12-26 20:00:45 -05:00
|
|
|
if !bytes.Equal(b.ValidatorsHash, s.Validators.Hash()) {
|
|
|
|
return fmt.Errorf("Wrong Block.Header.ValidatorsHash. Expected %X, got %v", s.Validators.Hash(), b.ValidatorsHash)
|
|
|
|
}
|
2017-12-20 23:53:15 -05:00
|
|
|
|
2016-09-11 13:16:23 -04:00
|
|
|
// Validate block LastCommit.
|
2017-12-20 23:53:15 -05:00
|
|
|
if b.Height == 1 {
|
|
|
|
if len(b.LastCommit.Precommits) != 0 {
|
2016-09-11 13:16:23 -04:00
|
|
|
return errors.New("Block at height 1 (first block) should have no LastCommit precommits")
|
|
|
|
}
|
|
|
|
} else {
|
2017-12-20 23:53:15 -05:00
|
|
|
if len(b.LastCommit.Precommits) != s.LastValidators.Size() {
|
2017-12-22 14:07:46 -06:00
|
|
|
return fmt.Errorf("Invalid block commit size. Expected %v, got %v",
|
|
|
|
s.LastValidators.Size(), len(b.LastCommit.Precommits))
|
2016-09-11 13:16:23 -04:00
|
|
|
}
|
|
|
|
err := s.LastValidators.VerifyCommit(
|
2017-12-20 23:53:15 -05:00
|
|
|
s.ChainID, s.LastBlockID, b.Height-1, b.LastCommit)
|
2016-09-11 13:16:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-26 20:34:57 -05:00
|
|
|
for _, ev := range b.Evidence.Evidence {
|
2017-12-27 19:21:16 -05:00
|
|
|
if err := VerifyEvidence(s, ev); err != nil {
|
|
|
|
return types.NewEvidenceInvalidErr(ev, err)
|
|
|
|
}
|
|
|
|
/* // Needs a db ...
|
|
|
|
valset, err := LoadValidators(s.db, ev.Height())
|
|
|
|
if err != nil {
|
|
|
|
// XXX/TODO: what do we do if we can't load the valset?
|
|
|
|
// eg. if we have pruned the state or height is too high?
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := VerifyEvidenceValidator(valSet, ev); err != nil {
|
2017-08-28 19:46:38 -04:00
|
|
|
return types.NewEvidenceInvalidErr(ev, err)
|
2017-07-25 12:29:38 -04:00
|
|
|
}
|
2017-12-27 19:21:16 -05:00
|
|
|
*/
|
2017-07-25 12:29:38 -04:00
|
|
|
}
|
2017-07-09 14:10:00 -04:00
|
|
|
|
2016-09-11 13:16:23 -04:00
|
|
|
return nil
|
2015-12-01 20:12:01 -08:00
|
|
|
}
|
2016-08-23 21:44:07 -04:00
|
|
|
|
2017-12-27 19:21:16 -05:00
|
|
|
// XXX: What's cheaper (ie. what should be checked first):
|
|
|
|
// evidence internal validity (ie. sig checks) or validator existed (fetch historical val set from db)
|
|
|
|
|
2017-12-27 14:27:37 -05:00
|
|
|
// VerifyEvidence verifies the evidence fully by checking it is internally
|
2017-12-27 19:21:16 -05:00
|
|
|
// consistent and sufficiently recent.
|
|
|
|
func VerifyEvidence(s State, evidence types.Evidence) error {
|
2017-12-27 14:27:37 -05:00
|
|
|
height := s.LastBlockHeight
|
2017-12-27 19:21:16 -05:00
|
|
|
|
2017-12-27 14:27:37 -05:00
|
|
|
evidenceAge := height - evidence.Height()
|
|
|
|
maxAge := s.ConsensusParams.EvidenceParams.MaxAge
|
|
|
|
if evidenceAge > maxAge {
|
2017-12-27 19:21:16 -05:00
|
|
|
return fmt.Errorf("Evidence from height %d is too old. Min height is %d",
|
2017-12-27 14:27:37 -05:00
|
|
|
evidence.Height(), height-maxAge)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := evidence.Verify(s.ChainID); err != nil {
|
2017-12-27 19:21:16 -05:00
|
|
|
return err
|
2017-12-27 14:27:37 -05:00
|
|
|
}
|
2017-12-27 19:21:16 -05:00
|
|
|
return nil
|
|
|
|
}
|
2017-12-27 14:27:37 -05:00
|
|
|
|
2017-12-27 19:21:16 -05:00
|
|
|
// VerifyEvidenceValidator returns the voting power of the validator at the height of the evidence.
|
|
|
|
// It returns an error if the validator did not exist or does not match that loaded from the historical validator set.
|
|
|
|
func VerifyEvidenceValidator(valset *types.ValidatorSet, evidence types.Evidence) (priority int64, err error) {
|
2017-12-27 14:27:37 -05:00
|
|
|
// The address must have been an active validator at the height
|
|
|
|
ev := evidence
|
|
|
|
height, addr, idx := ev.Height(), ev.Address(), ev.Index()
|
|
|
|
valIdx, val := valset.GetByAddress(addr)
|
|
|
|
if val == nil {
|
|
|
|
return priority, fmt.Errorf("Address %X was not a validator at height %d", addr, height)
|
|
|
|
} else if idx != valIdx {
|
|
|
|
return priority, fmt.Errorf("Address %X was validator %d at height %d, not %d", addr, valIdx, height, idx)
|
|
|
|
}
|
|
|
|
|
|
|
|
priority = val.VotingPower
|
|
|
|
return priority, nil
|
|
|
|
}
|
|
|
|
|
2017-04-14 15:33:19 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
2017-04-17 15:24:44 -07:00
|
|
|
// ApplyBlock validates & executes the block, updates state w/ ABCI responses,
|
2017-04-14 15:33:19 -04:00
|
|
|
// then commits and updates the mempool atomically, then saves state.
|
|
|
|
|
2017-12-27 14:27:37 -05:00
|
|
|
// BlockExecutor provides the context and accessories for properly executing a block.
|
|
|
|
type BlockExecutor struct {
|
2017-12-27 19:21:16 -05:00
|
|
|
db dbm.DB
|
|
|
|
logger log.Logger
|
|
|
|
|
2017-12-27 14:27:37 -05:00
|
|
|
txEventPublisher types.TxEventPublisher
|
|
|
|
proxyApp proxy.AppConnConsensus
|
|
|
|
|
|
|
|
mempool types.Mempool
|
|
|
|
evpool types.EvidencePool
|
|
|
|
}
|
|
|
|
|
2017-12-27 19:21:16 -05:00
|
|
|
func NewBlockExecutor(db dbm.DB, logger log.Logger, txEventer types.TxEventPublisher, proxyApp proxy.AppConnConsensus,
|
|
|
|
mempool types.Mempool, evpool types.EvidencePool) *BlockExecutor {
|
|
|
|
return &BlockExecutor{
|
|
|
|
db,
|
|
|
|
logger,
|
|
|
|
txEventer,
|
|
|
|
proxyApp,
|
|
|
|
mempool,
|
|
|
|
evpool,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-21 16:12:07 -04:00
|
|
|
// ApplyBlock validates the block against the state, executes it against the app,
|
|
|
|
// commits it, and saves the block and state. It's the only function that needs to be called
|
|
|
|
// from outside this package to process and commit an entire block.
|
2017-12-27 19:21:16 -05:00
|
|
|
// It takes a blockID to avoid recomputing the parts hash.
|
|
|
|
func (blockExec *BlockExecutor) ApplyBlock(s State, blockID types.BlockID, block *types.Block) (State, error) {
|
2016-09-11 13:16:23 -04:00
|
|
|
|
2017-12-27 19:21:16 -05:00
|
|
|
abciResponses, err := blockExec.ValExecBlock(s, block)
|
2016-09-11 13:16:23 -04:00
|
|
|
if err != nil {
|
2017-12-27 19:21:16 -05:00
|
|
|
return s, fmt.Errorf("Exec failed for application: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Fire events
|
|
|
|
/*
|
|
|
|
tx := types.Tx(req.GetDeliverTx().Tx)
|
|
|
|
txEventPublisher.PublishEventTx(types.EventDataTx{types.TxResult{
|
|
|
|
Height: block.Height,
|
|
|
|
Index: uint32(txIndex),
|
|
|
|
Tx: tx,
|
|
|
|
Result: *txRes,
|
|
|
|
}})
|
|
|
|
*/
|
2016-09-11 13:16:23 -04:00
|
|
|
|
2017-04-14 15:33:19 -04:00
|
|
|
fail.Fail() // XXX
|
|
|
|
|
|
|
|
// save the results before we commit
|
2017-12-27 19:21:16 -05:00
|
|
|
SaveABCIResponses(blockExec.db, block.Height, abciResponses)
|
2017-04-14 15:33:19 -04:00
|
|
|
|
|
|
|
fail.Fail() // XXX
|
|
|
|
|
2017-12-27 19:21:16 -05:00
|
|
|
// update the state with the block and responses
|
|
|
|
s, err = s.NextState(blockID, block.Header, abciResponses)
|
2017-12-21 17:46:25 -05:00
|
|
|
if err != nil {
|
2017-12-27 19:21:16 -05:00
|
|
|
return s, fmt.Errorf("Commit failed for application: %v", err)
|
2017-12-21 17:46:25 -05:00
|
|
|
}
|
2017-04-14 15:33:19 -04:00
|
|
|
|
2016-09-11 13:16:23 -04:00
|
|
|
// lock mempool, commit state, update mempoool
|
2017-12-27 19:21:16 -05:00
|
|
|
appHash, err := blockExec.Commit(block)
|
2016-09-11 13:16:23 -04:00
|
|
|
if err != nil {
|
2017-12-27 19:21:16 -05:00
|
|
|
return s, fmt.Errorf("Commit failed for application: %v", err)
|
2016-09-11 13:16:23 -04:00
|
|
|
}
|
2017-01-19 13:33:58 +04:00
|
|
|
|
2017-04-14 15:33:19 -04:00
|
|
|
fail.Fail() // XXX
|
|
|
|
|
2017-09-04 18:27:04 -04:00
|
|
|
// save the state and the validators
|
2017-12-27 19:21:16 -05:00
|
|
|
s.Save(blockExec.db, appHash)
|
2017-04-14 15:33:19 -04:00
|
|
|
|
2017-12-27 19:21:16 -05:00
|
|
|
return s, nil
|
2016-09-11 13:16:23 -04:00
|
|
|
}
|
2016-08-23 21:44:07 -04:00
|
|
|
|
2017-12-27 19:21:16 -05:00
|
|
|
// Commit locks the mempool, runs the ABCI Commit message, and updates the mempool.
|
|
|
|
// It returns the result of calling abci.Commit (the AppHash), and an error.
|
2017-08-21 16:12:07 -04:00
|
|
|
// The 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.
|
2017-12-27 19:21:16 -05:00
|
|
|
func (blockExec *BlockExecutor) Commit(block *types.Block) ([]byte, error) {
|
|
|
|
blockExec.mempool.Lock()
|
|
|
|
defer blockExec.mempool.Unlock()
|
2016-08-25 00:18:03 -04:00
|
|
|
|
|
|
|
// Commit block, get hash back
|
2017-12-27 19:21:16 -05:00
|
|
|
res, err := blockExec.proxyApp.CommitSync()
|
2017-11-22 18:55:09 -06:00
|
|
|
if err != nil {
|
2017-12-27 19:21:16 -05:00
|
|
|
blockExec.logger.Error("Client error during proxyAppConn.CommitSync", "err", err)
|
|
|
|
return nil, err
|
2017-11-22 18:55:09 -06:00
|
|
|
}
|
2016-08-25 00:18:03 -04:00
|
|
|
if res.IsErr() {
|
2017-12-27 19:21:16 -05:00
|
|
|
blockExec.logger.Error("Error in proxyAppConn.CommitSync", "err", res)
|
|
|
|
return nil, res
|
2016-08-25 00:18:03 -04:00
|
|
|
}
|
|
|
|
if res.Log != "" {
|
2017-12-27 19:21:16 -05:00
|
|
|
blockExec.logger.Debug("Commit.Log: " + res.Log)
|
2016-08-25 00:18:03 -04:00
|
|
|
}
|
|
|
|
|
2017-12-27 19:21:16 -05:00
|
|
|
blockExec.logger.Info("Committed state", "height", block.Height, "txs", block.NumTxs, "hash", res.Data)
|
2017-05-22 08:16:25 -04:00
|
|
|
|
2017-12-27 19:21:16 -05:00
|
|
|
// Update evpool
|
|
|
|
blockExec.evpool.MarkEvidenceAsCommitted(block.Evidence.Evidence)
|
2016-08-25 00:18:03 -04:00
|
|
|
|
|
|
|
// Update mempool.
|
2017-12-27 19:21:16 -05:00
|
|
|
if err := blockExec.mempool.Update(block.Height, block.Txs); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Data, nil
|
2016-08-25 00:18:03 -04:00
|
|
|
}
|
|
|
|
|
2017-08-21 16:12:07 -04:00
|
|
|
// ExecCommitBlock executes and commits a block on the proxyApp without validating or mutating the state.
|
|
|
|
// It returns the application root hash (result of abci.Commit).
|
2017-12-27 19:21:16 -05:00
|
|
|
func ExecCommitBlock(appConnConsensus proxy.AppConnConsensus, block *types.Block, logger log.Logger) ([]byte, error) {
|
|
|
|
_, err := execBlockOnProxyApp(logger, appConnConsensus, block)
|
2017-02-17 11:32:56 -05:00
|
|
|
if err != nil {
|
2017-05-02 11:53:32 +04:00
|
|
|
logger.Error("Error executing block on proxy app", "height", block.Height, "err", err)
|
2017-02-17 11:32:56 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Commit block, get hash back
|
2017-11-22 18:55:09 -06:00
|
|
|
res, err := appConnConsensus.CommitSync()
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Client error during proxyAppConn.CommitSync", "err", res)
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-17 11:32:56 -05:00
|
|
|
if res.IsErr() {
|
2017-06-14 12:50:49 +04:00
|
|
|
logger.Error("Error in proxyAppConn.CommitSync", "err", res)
|
2017-02-17 11:32:56 -05:00
|
|
|
return nil, res
|
|
|
|
}
|
|
|
|
if res.Log != "" {
|
2017-05-02 11:53:32 +04:00
|
|
|
logger.Info("Commit.Log: " + res.Log)
|
2017-02-17 11:32:56 -05:00
|
|
|
}
|
|
|
|
return res.Data, nil
|
|
|
|
}
|