From 6112578d07c97033380ac4c5902c5bcb108d8340 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Thu, 28 Dec 2017 19:35:56 -0500 Subject: [PATCH] ValidateBlock is a method on blockExec --- consensus/state.go | 7 ++++--- node/node.go | 2 +- state/execution.go | 10 +++++++++- state/validation.go | 7 ++++--- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/consensus/state.go b/consensus/state.go index 69858da08..518d81c58 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -153,6 +153,7 @@ func (cs *ConsensusState) SetLogger(l log.Logger) { // SetEventBus sets event bus. func (cs *ConsensusState) SetEventBus(b *types.EventBus) { cs.eventBus = b + cs.blockExec.SetEventBus(b) } // String returns a string. @@ -922,7 +923,7 @@ func (cs *ConsensusState) defaultDoPrevote(height int64, round int) { } // Validate proposal block - err := sm.ValidateBlock(cs.state, cs.ProposalBlock) + err := cs.blockExec.ValidateBlock(cs.state, cs.ProposalBlock) if err != nil { // ProposalBlock is invalid, prevote nil. logger.Error("enterPrevote: ProposalBlock is invalid", "err", err) @@ -1030,7 +1031,7 @@ func (cs *ConsensusState) enterPrecommit(height int64, round int) { if cs.ProposalBlock.HashesTo(blockID.Hash) { cs.Logger.Info("enterPrecommit: +2/3 prevoted proposal block. Locking", "hash", blockID.Hash) // Validate the block. - if err := sm.ValidateBlock(cs.state, cs.ProposalBlock); err != nil { + if err := cs.blockExec.ValidateBlock(cs.state, cs.ProposalBlock); err != nil { cmn.PanicConsensus(cmn.Fmt("enterPrecommit: +2/3 prevoted for an invalid block: %v", err)) } cs.LockedRound = round @@ -1165,7 +1166,7 @@ func (cs *ConsensusState) finalizeCommit(height int64) { if !block.HashesTo(blockID.Hash) { cmn.PanicSanity(cmn.Fmt("Cannot finalizeCommit, ProposalBlock does not hash to commit hash")) } - if err := sm.ValidateBlock(cs.state, block); err != nil { + if err := cs.blockExec.ValidateBlock(cs.state, block); err != nil { cmn.PanicConsensus(cmn.Fmt("+2/3 committed an invalid block: %v", err)) } diff --git a/node/node.go b/node/node.go index fe51b941c..04b1fb140 100644 --- a/node/node.go +++ b/node/node.go @@ -291,7 +291,7 @@ func NewNode(config *cfg.Config, eventBus.SetLogger(logger.With("module", "events")) // services which will be publishing and/or subscribing for messages (events) - blockExec.SetEventBus(eventBus) + // consensusReactor will set it on consensusState and blockExecutor consensusReactor.SetEventBus(eventBus) // Transaction indexing diff --git a/state/execution.go b/state/execution.go index 8b05733a7..b3acd711f 100644 --- a/state/execution.go +++ b/state/execution.go @@ -56,6 +56,14 @@ func (blockExec *BlockExecutor) SetEventBus(eventBus types.BlockEventPublisher) blockExec.eventBus = eventBus } +// ValidateBlock validates the given block against the given state. +// If the block is invalid, it returns an error. +// Validation does not mutate state, but does require historical information from the stateDB, +// ie. to verify evidence from a validator at an old height. +func (blockExec *BlockExecutor) ValidateBlock(s State, block *types.Block) error { + return validateBlock(blockExec.db, s, block) +} + // ApplyBlock validates the block against the state, executes it against the app, // fires the relevent events, commits the app, and saves the new state and responses. // It's the only function that needs to be called @@ -63,7 +71,7 @@ func (blockExec *BlockExecutor) SetEventBus(eventBus types.BlockEventPublisher) // It takes a blockID to avoid recomputing the parts hash. func (blockExec *BlockExecutor) ApplyBlock(s State, blockID types.BlockID, block *types.Block) (State, error) { - if err := validateBlock(s, block); err != nil { + if err := blockExec.ValidateBlock(s, block); err != nil { return s, ErrInvalidBlock(err) } diff --git a/state/validation.go b/state/validation.go index 692008406..5c9197bcc 100644 --- a/state/validation.go +++ b/state/validation.go @@ -6,17 +6,18 @@ import ( "fmt" "github.com/tendermint/tendermint/types" + dbm "github.com/tendermint/tmlibs/db" ) //----------------------------------------------------- // Validate block // ValidateBlock validates the block against the state. -func ValidateBlock(s State, block *types.Block) error { - return validateBlock(s, block) +func _ValidateBlock(s State, block *types.Block) error { + return validateBlock(dbm.NewMemDB(), s, block) } -func validateBlock(s State, b *types.Block) error { +func validateBlock(stateDB dbm.DB, s State, b *types.Block) error { // validate internal consistency if err := b.ValidateBasic(); err != nil { return err