Skip to content

Commit

Permalink
add withAppStat param
Browse files Browse the repository at this point in the history
  • Loading branch information
unclezoro committed Nov 28, 2018
1 parent 6fad36f commit 44fcecc
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 28 deletions.
2 changes: 1 addition & 1 deletion blockchain/reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func newBlockchainReactor(logger log.Logger, maxBlockHeight int64) *BlockchainRe
fastSync := true
var nilApp proxy.AppConnConsensus
blockExec := sm.NewBlockExecutor(dbm.NewMemDB(), log.TestingLogger(), nilApp,
sm.MockMempool{}, sm.MockEvidencePool{})
sm.MockMempool{}, sm.MockEvidencePool{}, true)

bcReactor := NewBlockchainReactor(state.Copy(), blockExec, blockStore, fastSync)
bcReactor.SetLogger(logger.With("module", "blockchain"))
Expand Down
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ type BaseConfig struct {
// If true, query the ABCI app on connecting to a new peer
// so the app can decide if we should keep the connection or not
FilterPeers bool `mapstructure:"filter_peers"` // false

// Whether application get state
WithAppStat bool `mapstructure:"with_app_stat"`
}

// DefaultBaseConfig returns a default base configuration for a Tendermint node
Expand All @@ -161,6 +164,7 @@ func DefaultBaseConfig() BaseConfig {
FilterPeers: false,
DBBackend: "leveldb",
DBPath: "data",
WithAppStat: true,
}
}

Expand Down
3 changes: 3 additions & 0 deletions config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ prof_laddr = "{{ .BaseConfig.ProfListenAddress }}"
# so the app can decide if we should keep the connection or not
filter_peers = {{ .BaseConfig.FilterPeers }}
# If false, will not check appHash when apply block
with_app_stat = {{ .BaseConfig.WithAppStat }}
##### advanced configuration options #####
##### rpc server configuration options #####
Expand Down
2 changes: 1 addition & 1 deletion consensus/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func newConsensusStateWithConfigAndBlockStore(thisConfig *cfg.Config, state sm.S

// Make ConsensusState
stateDB := dbm.NewMemDB()
blockExec := sm.NewBlockExecutor(stateDB, log.TestingLogger(), proxyAppConnCon, mempool, evpool)
blockExec := sm.NewBlockExecutor(stateDB, log.TestingLogger(), proxyAppConnCon, mempool, evpool, true)
cs := NewConsensusState(thisConfig.Consensus, state, blockExec, blockStore, mempool, evpool)
cs.SetLogger(log.TestingLogger().With("module", "consensus"))
cs.SetPrivValidator(pv)
Expand Down
2 changes: 1 addition & 1 deletion consensus/reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func TestReactorWithEvidence(t *testing.T) {
evpool := newMockEvidencePool(privVals[vIdx].GetAddress())

// Make ConsensusState
blockExec := sm.NewBlockExecutor(stateDB, log.TestingLogger(), proxyAppConnCon, mempool, evpool)
blockExec := sm.NewBlockExecutor(stateDB, log.TestingLogger(), proxyAppConnCon, mempool, evpool, true)
cs := NewConsensusState(thisConfig.Consensus, state, blockExec, blockStore, mempool, evpool)
cs.SetLogger(log.TestingLogger().With("module", "consensus"))
cs.SetPrivValidator(pv)
Expand Down
8 changes: 5 additions & 3 deletions consensus/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,12 @@ type Handshaker struct {
genDoc *types.GenesisDoc
logger log.Logger

nBlocks int // number of blocks applied to the state
withAppStat bool
nBlocks int // number of blocks applied to the state
}

func NewHandshaker(stateDB dbm.DB, state sm.State,
store sm.BlockStore, genDoc *types.GenesisDoc) *Handshaker {
store sm.BlockStore, genDoc *types.GenesisDoc, withAppStat bool) *Handshaker {

return &Handshaker{
stateDB: stateDB,
Expand All @@ -212,6 +213,7 @@ func NewHandshaker(stateDB dbm.DB, state sm.State,
genDoc: genDoc,
logger: log.NewNopLogger(),
nBlocks: 0,
withAppStat: withAppStat,
}
}

Expand Down Expand Up @@ -407,7 +409,7 @@ func (h *Handshaker) replayBlock(state sm.State, height int64, proxyApp proxy.Ap
block := h.store.LoadBlock(height)
meta := h.store.LoadBlockMeta(height)

blockExec := sm.NewBlockExecutor(h.stateDB, h.logger, proxyApp, sm.MockMempool{}, sm.MockEvidencePool{})
blockExec := sm.NewBlockExecutor(h.stateDB, h.logger, proxyApp, sm.MockMempool{}, sm.MockEvidencePool{}, h.withAppStat)

var err error
state, err = blockExec.ApplyBlock(state, meta.BlockID, block)
Expand Down
4 changes: 2 additions & 2 deletions consensus/replay_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func newConsensusStateForReplay(config cfg.BaseConfig, csConfig *cfg.ConsensusCo
cmn.Exit(fmt.Sprintf("Error starting proxy app conns: %v", err))
}

handshaker := NewHandshaker(stateDB, state, blockStore, gdoc)
handshaker := NewHandshaker(stateDB, state, blockStore, gdoc, true)
err = handshaker.Handshake(proxyApp)
if err != nil {
cmn.Exit(fmt.Sprintf("Error on handshake: %v", err))
Expand All @@ -316,7 +316,7 @@ func newConsensusStateForReplay(config cfg.BaseConfig, csConfig *cfg.ConsensusCo
}

mempool, evpool := sm.MockMempool{}, sm.MockEvidencePool{}
blockExec := sm.NewBlockExecutor(stateDB, log.TestingLogger(), proxyApp.Consensus(), mempool, evpool)
blockExec := sm.NewBlockExecutor(stateDB, log.TestingLogger(), proxyApp.Consensus(), mempool, evpool, true)

consensusState := NewConsensusState(csConfig, state.Copy(), blockExec,
blockStore, mempool, evpool)
Expand Down
6 changes: 3 additions & 3 deletions consensus/replay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func testHandshakeReplay(t *testing.T, nBlocks int, mode uint) {

// now start the app using the handshake - it should sync
genDoc, _ := sm.MakeGenesisDocFromFile(config.GenesisFile())
handshaker := NewHandshaker(stateDB, state, store, genDoc)
handshaker := NewHandshaker(stateDB, state, store, genDoc, true)
proxyApp := proxy.NewAppConns(clientCreator2)
if err := proxyApp.Start(); err != nil {
t.Fatalf("Error starting proxy app connections: %v", err)
Expand Down Expand Up @@ -393,7 +393,7 @@ func testHandshakeReplay(t *testing.T, nBlocks int, mode uint) {

func applyBlock(stateDB dbm.DB, st sm.State, blk *types.Block, proxyApp proxy.AppConns) sm.State {
testPartSize := types.BlockPartSizeBytes
blockExec := sm.NewBlockExecutor(stateDB, log.TestingLogger(), proxyApp.Consensus(), mempool, evpool)
blockExec := sm.NewBlockExecutor(stateDB, log.TestingLogger(), proxyApp.Consensus(), mempool, evpool, true)

blkID := types.BlockID{blk.Hash(), blk.MakePartSet(testPartSize).Header()}
newState, err := blockExec.ApplyBlock(st, blkID, blk)
Expand Down Expand Up @@ -645,7 +645,7 @@ func TestInitChainUpdateValidators(t *testing.T) {

// now start the app using the handshake - it should sync
genDoc, _ := sm.MakeGenesisDocFromFile(config.GenesisFile())
handshaker := NewHandshaker(stateDB, state, store, genDoc)
handshaker := NewHandshaker(stateDB, state, store, genDoc, true)
proxyApp := proxy.NewAppConns(clientCreator)
if err := proxyApp.Start(); err != nil {
t.Fatalf("Error starting proxy app connections: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion consensus/wal_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func WALWithNBlocks(numBlocks int) (data []byte, err error) {
defer eventBus.Stop()
mempool := sm.MockMempool{}
evpool := sm.MockEvidencePool{}
blockExec := sm.NewBlockExecutor(stateDB, log.TestingLogger(), proxyApp.Consensus(), mempool, evpool)
blockExec := sm.NewBlockExecutor(stateDB, log.TestingLogger(), proxyApp.Consensus(), mempool, evpool, true)
consensusState := NewConsensusState(config.Consensus, state.Copy(), blockExec, blockStore, mempool, evpool)
consensusState.SetLogger(logger)
consensusState.SetEventBus(eventBus)
Expand Down
4 changes: 2 additions & 2 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func NewNode(config *cfg.Config,
// Create the handshaker, which calls RequestInfo and replays any blocks
// as necessary to sync tendermint with the app.
consensusLogger := logger.With("module", "consensus")
handshaker := cs.NewHandshaker(stateDB, state, blockStore, genDoc)
handshaker := cs.NewHandshaker(stateDB, state, blockStore, genDoc, config.WithAppStat)
handshaker.SetLogger(consensusLogger)
if err := handshaker.Handshake(proxyApp); err != nil {
return nil, fmt.Errorf("Error during handshake: %v", err)
Expand Down Expand Up @@ -289,7 +289,7 @@ func NewNode(config *cfg.Config,

blockExecLogger := logger.With("module", "state")
// make block executor for consensus and blockchain reactors to execute blocks
blockExec := sm.NewBlockExecutor(stateDB, blockExecLogger, proxyApp.Consensus(), mempool, evidencePool)
blockExec := sm.NewBlockExecutor(stateDB, blockExecLogger, proxyApp.Consensus(), mempool, evidencePool, config.WithAppStat)

// Make BlockchainReactor
bcReactor := bc.NewBlockchainReactor(state.Copy(), blockExec, blockStore, fastSync)
Expand Down
20 changes: 12 additions & 8 deletions state/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,23 @@ type BlockExecutor struct {
evpool EvidencePool

logger log.Logger

// whether to check appHash
withAppState bool
}

// NewBlockExecutor returns a new BlockExecutor with a NopEventBus.
// Call SetEventBus to provide one.
func NewBlockExecutor(db dbm.DB, logger log.Logger, proxyApp proxy.AppConnConsensus,
mempool Mempool, evpool EvidencePool) *BlockExecutor {
mempool Mempool, evpool EvidencePool, withAppState bool) *BlockExecutor {
return &BlockExecutor{
db: db,
proxyApp: proxyApp,
eventBus: types.NopEventBus{},
mempool: mempool,
evpool: evpool,
logger: logger,
db: db,
proxyApp: proxyApp,
eventBus: types.NopEventBus{},
mempool: mempool,
evpool: evpool,
logger: logger,
withAppState: withAppState,
}
}

Expand All @@ -60,7 +64,7 @@ func (blockExec *BlockExecutor) SetEventBus(eventBus types.BlockEventPublisher)
// 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(state State, block *types.Block) error {
return validateBlock(blockExec.db, state, block)
return validateBlock(blockExec.db, state, block, blockExec.withAppState)
}

// ApplyBlock validates the block against the state, executes it against the app,
Expand Down
4 changes: 2 additions & 2 deletions state/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestApplyBlock(t *testing.T) {
state, stateDB := state(1, 1)

blockExec := NewBlockExecutor(stateDB, log.TestingLogger(), proxyApp.Consensus(),
MockMempool{}, MockEvidencePool{})
MockMempool{}, MockEvidencePool{}, true)

block := makeBlock(state, 1)
blockID := types.BlockID{block.Hash(), block.MakePartSet(testPartSize).Header()}
Expand Down Expand Up @@ -247,7 +247,7 @@ func TestEndBlockValidatorUpdates(t *testing.T) {
state, stateDB := state(1, 1)

blockExec := NewBlockExecutor(stateDB, log.TestingLogger(), proxyApp.Consensus(),
MockMempool{}, MockEvidencePool{})
MockMempool{}, MockEvidencePool{}, true)
eventBus := types.NewEventBus()
err = eventBus.Start()
require.NoError(t, err)
Expand Down
6 changes: 3 additions & 3 deletions state/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
//-----------------------------------------------------
// Validate block

func validateBlock(stateDB dbm.DB, state State, block *types.Block) error {
func validateBlock(stateDB dbm.DB, state State, block *types.Block, withAppStat bool) error {
// Validate internal consistency.
if err := block.ValidateBasic(); err != nil {
return err
Expand Down Expand Up @@ -53,7 +53,7 @@ func validateBlock(stateDB dbm.DB, state State, block *types.Block) error {
}

// Validate app info
if !bytes.Equal(block.AppHash, state.AppHash) {
if withAppStat && !bytes.Equal(block.AppHash, state.AppHash) {
return fmt.Errorf(
"Wrong Block.Header.AppHash. Expected %X, got %v",
state.AppHash,
Expand All @@ -67,7 +67,7 @@ func validateBlock(stateDB dbm.DB, state State, block *types.Block) error {
block.ConsensusHash,
)
}
if !bytes.Equal(block.LastResultsHash, state.LastResultsHash) {
if withAppStat && !bytes.Equal(block.LastResultsHash, state.LastResultsHash) {
return fmt.Errorf(
"Wrong Block.Header.LastResultsHash. Expected %X, got %v",
state.LastResultsHash,
Expand Down
2 changes: 1 addition & 1 deletion state/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func TestValidateBlock(t *testing.T) {
state, _ := state(1, 1)

blockExec := NewBlockExecutor(dbm.NewMemDB(), log.TestingLogger(), nil, nil, nil)
blockExec := NewBlockExecutor(dbm.NewMemDB(), log.TestingLogger(), nil, nil, nil, true)

// proper block must pass
block := makeBlock(state, 1)
Expand Down

0 comments on commit 44fcecc

Please sign in to comment.