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