github.com/okex/exchain@v1.8.0/libs/tendermint/blockchain/v2/processor_context.go (about)

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