tendermint/state/execution.go

184 lines
5.2 KiB
Go
Raw Normal View History

package state
import (
"errors"
2015-12-01 20:12:01 -08:00
"fmt"
. "github.com/tendermint/go-common"
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-12-01 20:12:01 -08:00
tmsp "github.com/tendermint/tmsp/types"
)
// Validate block
func (s *State) ValidateBlock(block *types.Block) error {
return s.validateBlock(block)
}
2015-12-01 20:12:01 -08:00
// Execute the block to mutate State.
// 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.
err := s.validateBlock(block)
if err != nil {
return err
}
2015-12-01 20:12:01 -08:00
// Update the validator set
valSet := s.Validators.Copy()
// Update valSet with signatures from block.
updateValidatorsWithBlock(s.LastValidators, valSet, block)
// TODO: Update the validator set (e.g. block.Data.ValidatorUpdates?)
nextValSet := valSet.Copy()
// Execute the block txs
err = s.execBlockOnProxyApp(eventCache, proxyAppConn, block)
2015-12-01 20:12:01 -08:00
if err != nil {
// There was some error in proxyApp
// TODO Report error and wait for proxyApp to be available.
2015-12-01 20:12:01 -08:00
return err
}
// All good!
nextValSet.IncrementAccum(1)
s.LastBlockHeight = block.Height
2016-08-16 14:59:19 -07:00
s.LastBlockID = types.BlockID{block.Hash(), blockPartsHeader}
2015-12-01 20:12:01 -08:00
s.LastBlockTime = block.Time
s.Validators = nextValSet
s.LastValidators = valSet
2015-12-01 20:12:01 -08:00
return nil
}
// Executes block's transactions on proxyAppConn.
// TODO: Generate a bitmap or otherwise store tx validity in state.
2016-10-09 23:58:13 -07:00
func (s *State) execBlockOnProxyApp(eventCache types.Fireable, proxyAppConn proxy.AppConnConsensus, block *types.Block) error {
2016-06-27 17:43:09 -07:00
var validTxs, invalidTxs = 0, 0
2015-12-01 20:12:01 -08:00
// Execute transactions and get hash
2016-01-31 08:11:50 -08:00
proxyCb := func(req *tmsp.Request, res *tmsp.Response) {
2016-05-14 09:33:27 -07:00
switch r := res.Value.(type) {
case *tmsp.Response_AppendTx:
2016-01-25 14:34:08 -08:00
// TODO: make use of res.Log
// TODO: make use of this info
// Blocks may include invalid txs.
// reqAppendTx := req.(tmsp.RequestAppendTx)
txError := ""
apTx := r.AppendTx
if apTx.Code == tmsp.CodeType_OK {
validTxs += 1
} else {
2016-05-14 09:33:27 -07:00
log.Debug("Invalid tx", "code", r.AppendTx.Code, "log", r.AppendTx.Log)
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
event := types.EventDataTx{
Tx: req.GetAppendTx().Tx,
Result: apTx.Data,
Code: apTx.Code,
Log: apTx.Log,
Error: txError,
}
2016-10-09 23:58:13 -07:00
types.FireEventTx(eventCache, event)
2015-12-01 20:12:01 -08:00
}
}
proxyAppConn.SetResponseCallback(proxyCb)
2016-11-03 16:51:22 -07:00
// Begin block
err := proxyAppConn.BeginBlockSync(uint64(block.Height))
if err != nil {
log.Warn("Error in proxyAppConn.BeginBlock", "error", err)
return err
}
2016-03-05 20:57:36 -08:00
// Run txs of block
for _, tx := range block.Txs {
proxyAppConn.AppendTxAsync(tx)
if err := proxyAppConn.Error(); err != nil {
2015-12-01 20:12:01 -08:00
return err
}
}
2016-03-05 20:57:36 -08:00
// End block
2016-03-06 18:02:29 -08:00
changedValidators, 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)
return err
}
// TODO: Do something with changedValidators
2016-11-03 17:13:39 -07:00
log.Debug("TODO: Do something with changedValidators", "changedValidators", changedValidators)
2016-03-05 20:57:36 -08:00
2016-01-12 16:30:31 -08:00
log.Info(Fmt("ExecBlock got %v valid txs and %v invalid txs", validTxs, invalidTxs))
2015-12-01 20:12:01 -08:00
return nil
}
func (s *State) validateBlock(block *types.Block) error {
// Basic block validation.
2016-08-16 14:59:19 -07:00
err := block.ValidateBasic(s.ChainID, s.LastBlockHeight, s.LastBlockID, s.LastBlockTime, s.AppHash)
if err != nil {
return err
}
2016-04-02 09:10:16 -07:00
// Validate block LastCommit.
if block.Height == 1 {
2016-04-02 09:10:16 -07:00
if len(block.LastCommit.Precommits) != 0 {
return errors.New("Block at height 1 (first block) should have no LastCommit precommits")
}
} else {
2016-04-02 09:10:16 -07:00
if len(block.LastCommit.Precommits) != s.LastValidators.Size() {
return fmt.Errorf("Invalid block commit size. Expected %v, got %v",
s.LastValidators.Size(), len(block.LastCommit.Precommits))
}
2016-04-02 09:10:16 -07:00
err := s.LastValidators.VerifyCommit(
2016-08-16 14:59:19 -07:00
s.ChainID, s.LastBlockID, block.Height-1, block.LastCommit)
if err != nil {
return err
}
}
2015-12-01 20:12:01 -08:00
return nil
}
// Updates the LastCommitHeight of the validators in valSet, in place.
2016-04-02 09:10:16 -07:00
// Assumes that lastValSet matches the valset of block.LastCommit
2015-12-01 20:12:01 -08:00
// CONTRACT: lastValSet is not mutated.
func updateValidatorsWithBlock(lastValSet *types.ValidatorSet, valSet *types.ValidatorSet, block *types.Block) {
2016-04-02 09:10:16 -07:00
for i, precommit := range block.LastCommit.Precommits {
2015-06-21 19:11:21 -07:00
if precommit == nil {
continue
}
2015-12-01 20:12:01 -08:00
_, val := lastValSet.GetByIndex(i)
if val == nil {
2015-07-19 16:42:52 -07:00
PanicCrisis(Fmt("Failed to fetch validator at index %v", i))
}
2015-12-01 20:12:01 -08:00
if _, val_ := valSet.GetByAddress(val.Address); val_ != nil {
val_.LastCommitHeight = block.Height - 1
2015-12-01 20:12:01 -08:00
updated := valSet.Update(val_)
if !updated {
2015-11-01 11:34:08 -08:00
PanicCrisis("Failed to update validator LastCommitHeight")
}
} else {
2015-12-01 20:12:01 -08:00
// XXX This is not an error if validator was removed.
// But, we don't mutate validators yet so go ahead and panic.
2015-07-19 16:42:52 -07:00
PanicCrisis("Could not find validator")
}
}
2015-05-12 17:40:19 -07:00
}
//-----------------------------------------------------------------------------
type InvalidTxError struct {
2016-01-25 14:34:08 -08:00
Tx types.Tx
2016-02-04 18:40:02 -08:00
Code tmsp.CodeType
}
func (txErr InvalidTxError) Error() string {
2016-01-25 14:34:08 -08:00
return Fmt("Invalid tx: [%v] code: [%v]", txErr.Tx, txErr.Code)
2015-12-01 20:12:01 -08:00
}