github.com/lzy4123/fabric@v2.1.1+incompatible/core/common/validation/statebased/v13.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package statebased 8 9 import ( 10 commonerrors "github.com/hyperledger/fabric/common/errors" 11 validation "github.com/hyperledger/fabric/core/handlers/validation/api/policies" 12 "github.com/hyperledger/fabric/protoutil" 13 "github.com/pkg/errors" 14 ) 15 16 // NewV13Evaluator returns a policy evaluator that checks 17 // 2 kinds of policies: 18 // 1) chaincode endorsement policies; 19 // 2) state-based endorsement policies. 20 func NewV13Evaluator(policySupport validation.PolicyEvaluator, vpmgr KeyLevelValidationParameterManager) *policyCheckerFactoryV13 { 21 return &policyCheckerFactoryV13{ 22 policySupport: policySupport, 23 vpmgr: vpmgr, 24 } 25 } 26 27 type policyCheckerFactoryV13 struct { 28 vpmgr KeyLevelValidationParameterManager 29 policySupport validation.PolicyEvaluator 30 } 31 32 func (p *policyCheckerFactoryV13) Evaluator(ccEP []byte) RWSetPolicyEvaluator { 33 return &baseEvaluator{ 34 epEvaluator: &policyCheckerV13{ 35 policySupport: p.policySupport, 36 ccEP: ccEP, 37 }, 38 vpmgr: p.vpmgr, 39 policySupport: p.policySupport, 40 } 41 } 42 43 /**********************************************************************************************************/ 44 /**********************************************************************************************************/ 45 46 type policyCheckerV13 struct { 47 someEPChecked bool 48 ccEPChecked bool 49 policySupport validation.PolicyEvaluator 50 ccEP []byte 51 } 52 53 func (p *policyCheckerV13) checkCCEPIfCondition(cc string, blockNum, txNum uint64, condition bool, sd []*protoutil.SignedData) commonerrors.TxValidationError { 54 if condition { 55 return nil 56 } 57 58 // validate against cc ep 59 err := p.policySupport.Evaluate(p.ccEP, sd) 60 if err != nil { 61 return policyErr(errors.Wrapf(err, "validation of endorsement policy for chaincode %s in tx %d:%d failed", cc, blockNum, txNum)) 62 } 63 64 p.ccEPChecked = true 65 p.someEPChecked = true 66 return nil 67 } 68 69 func (p *policyCheckerV13) CheckCCEPIfNotChecked(cc, coll string, blockNum, txNum uint64, sd []*protoutil.SignedData) commonerrors.TxValidationError { 70 return p.checkCCEPIfCondition(cc, blockNum, txNum, p.ccEPChecked, sd) 71 } 72 73 func (p *policyCheckerV13) CheckCCEPIfNoEPChecked(cc string, blockNum, txNum uint64, sd []*protoutil.SignedData) commonerrors.TxValidationError { 74 return p.checkCCEPIfCondition(cc, blockNum, txNum, p.someEPChecked, sd) 75 } 76 77 func (p *policyCheckerV13) SBEPChecked() { 78 p.someEPChecked = true 79 }