bitbucket.org/number571/tendermint@v0.8.14/internal/blockchain/v2/processor_context.go (about)

     1  package v2
     2  
     3  import (
     4  	"fmt"
     5  
     6  	cons "bitbucket.org/number571/tendermint/internal/consensus"
     7  	"bitbucket.org/number571/tendermint/state"
     8  	"bitbucket.org/number571/tendermint/types"
     9  )
    10  
    11  type processorContext interface {
    12  	applyBlock(blockID types.BlockID, block *types.Block) error
    13  	verifyCommit(chainID string, blockID types.BlockID, height int64, commit *types.Commit) error
    14  	saveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit)
    15  	tmState() state.State
    16  	setState(state.State)
    17  	recordConsMetrics(block *types.Block)
    18  }
    19  
    20  type pContext struct {
    21  	store   blockStore
    22  	applier blockApplier
    23  	state   state.State
    24  	metrics *cons.Metrics
    25  }
    26  
    27  func newProcessorContext(st blockStore, ex blockApplier, s state.State, m *cons.Metrics) *pContext {
    28  	return &pContext{
    29  		store:   st,
    30  		applier: ex,
    31  		state:   s,
    32  		metrics: m,
    33  	}
    34  }
    35  
    36  func (pc *pContext) applyBlock(blockID types.BlockID, block *types.Block) error {
    37  	newState, err := pc.applier.ApplyBlock(pc.state, blockID, block)
    38  	pc.state = newState
    39  	return err
    40  }
    41  
    42  func (pc pContext) tmState() state.State {
    43  	return pc.state
    44  }
    45  
    46  func (pc *pContext) setState(state state.State) {
    47  	pc.state = state
    48  }
    49  
    50  func (pc pContext) verifyCommit(chainID string, blockID types.BlockID, height int64, commit *types.Commit) error {
    51  	return pc.state.Validators.VerifyCommitLight(chainID, blockID, height, commit)
    52  }
    53  
    54  func (pc *pContext) saveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) {
    55  	pc.store.SaveBlock(block, blockParts, seenCommit)
    56  }
    57  
    58  func (pc *pContext) recordConsMetrics(block *types.Block) {
    59  	pc.metrics.RecordConsMetrics(block)
    60  }
    61  
    62  type mockPContext struct {
    63  	applicationBL  []int64
    64  	verificationBL []int64
    65  	state          state.State
    66  }
    67  
    68  func newMockProcessorContext(
    69  	state state.State,
    70  	verificationBlackList []int64,
    71  	applicationBlackList []int64) *mockPContext {
    72  	return &mockPContext{
    73  		applicationBL:  applicationBlackList,
    74  		verificationBL: verificationBlackList,
    75  		state:          state,
    76  	}
    77  }
    78  
    79  func (mpc *mockPContext) applyBlock(blockID types.BlockID, block *types.Block) error {
    80  	for _, h := range mpc.applicationBL {
    81  		if h == block.Height {
    82  			return fmt.Errorf("generic application error")
    83  		}
    84  	}
    85  	mpc.state.LastBlockHeight = block.Height
    86  	return nil
    87  }
    88  
    89  func (mpc *mockPContext) verifyCommit(chainID string, blockID types.BlockID, height int64, commit *types.Commit) error {
    90  	for _, h := range mpc.verificationBL {
    91  		if h == height {
    92  			return fmt.Errorf("generic verification error")
    93  		}
    94  	}
    95  	return nil
    96  }
    97  
    98  func (mpc *mockPContext) saveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) {
    99  
   100  }
   101  
   102  func (mpc *mockPContext) setState(state state.State) {
   103  	mpc.state = state
   104  }
   105  
   106  func (mpc *mockPContext) tmState() state.State {
   107  	return mpc.state
   108  }
   109  
   110  func (mpc *mockPContext) recordConsMetrics(block *types.Block) {
   111  
   112  }