github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/orderer/common/multichannel/ledger_resources.go (about)

     1  /*
     2  Copyright hechain. 2017 All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package multichannel
     8  
     9  import (
    10  	"github.com/hechain20/hechain/bccsp"
    11  	"github.com/hechain20/hechain/common/channelconfig"
    12  	"github.com/hechain20/hechain/common/ledger/blockledger"
    13  	"github.com/hechain20/hechain/common/policies"
    14  	"github.com/hechain20/hechain/protoutil"
    15  	"github.com/hyperledger/fabric-protos-go/common"
    16  	"github.com/pkg/errors"
    17  )
    18  
    19  // checkResources makes sure that the channel config is compatible with this binary and logs sanity checks
    20  func checkResources(res channelconfig.Resources) error {
    21  	channelconfig.LogSanityChecks(res)
    22  	oc, ok := res.OrdererConfig()
    23  	if !ok {
    24  		return errors.New("config does not contain orderer config")
    25  	}
    26  	if err := oc.Capabilities().Supported(); err != nil {
    27  		return errors.WithMessagef(err, "config requires unsupported orderer capabilities: %s", err)
    28  	}
    29  	if err := res.ChannelConfig().Capabilities().Supported(); err != nil {
    30  		return errors.WithMessagef(err, "config requires unsupported channel capabilities: %s", err)
    31  	}
    32  	return nil
    33  }
    34  
    35  // checkResourcesOrPanic invokes checkResources and panics if an error is returned
    36  func checkResourcesOrPanic(res channelconfig.Resources) {
    37  	if err := checkResources(res); err != nil {
    38  		logger.Panicf("[channel %s] %s", res.ConfigtxValidator().ChannelID(), err)
    39  	}
    40  }
    41  
    42  type mutableResources interface {
    43  	channelconfig.Resources
    44  	Update(*channelconfig.Bundle)
    45  }
    46  
    47  type configResources struct {
    48  	mutableResources
    49  	bccsp bccsp.BCCSP
    50  }
    51  
    52  func (cr *configResources) CreateBundle(channelID string, config *common.Config) (*channelconfig.Bundle, error) {
    53  	return channelconfig.NewBundle(channelID, config, cr.bccsp)
    54  }
    55  
    56  func (cr *configResources) Update(bndl *channelconfig.Bundle) {
    57  	checkResourcesOrPanic(bndl)
    58  	cr.mutableResources.Update(bndl)
    59  }
    60  
    61  func (cr *configResources) SharedConfig() channelconfig.Orderer {
    62  	oc, ok := cr.OrdererConfig()
    63  	if !ok {
    64  		logger.Panicf("[channel %s] has no orderer configuration", cr.ConfigtxValidator().ChannelID())
    65  	}
    66  	return oc
    67  }
    68  
    69  type ledgerResources struct {
    70  	*configResources
    71  	blockledger.ReadWriter
    72  }
    73  
    74  // ChannelID passes through to the underlying configtx.Validator
    75  func (lr *ledgerResources) ChannelID() string {
    76  	return lr.ConfigtxValidator().ChannelID()
    77  }
    78  
    79  // VerifyBlockSignature verifies a signature of a block.
    80  // It has an optional argument of a configuration envelope which would make the block verification to use validation
    81  // rules based on the given configuration in the ConfigEnvelope. If the config envelope passed is nil, then the
    82  // validation rules used are the ones that were applied at commit of previous blocks.
    83  func (lr *ledgerResources) VerifyBlockSignature(sd []*protoutil.SignedData, envelope *common.ConfigEnvelope) error {
    84  	policyMgr := lr.PolicyManager()
    85  	// If the envelope passed isn't nil, we should use a different policy manager.
    86  	if envelope != nil {
    87  		bundle, err := channelconfig.NewBundle(lr.ChannelID(), envelope.Config, lr.bccsp)
    88  		if err != nil {
    89  			return err
    90  		}
    91  		policyMgr = bundle.PolicyManager()
    92  	}
    93  	policy, exists := policyMgr.GetPolicy(policies.BlockValidation)
    94  	if !exists {
    95  		return errors.Errorf("policy %s wasn't found", policies.BlockValidation)
    96  	}
    97  	err := policy.EvaluateSignedData(sd)
    98  	if err != nil {
    99  		return errors.WithMessage(err, "block verification failed")
   100  	}
   101  	return nil
   102  }
   103  
   104  // Block returns a block with the following number, or nil if such a block doesn't exist.
   105  func (lr *ledgerResources) Block(number uint64) *common.Block {
   106  	if lr.Height() <= number {
   107  		return nil
   108  	}
   109  	return blockledger.GetBlock(lr, number)
   110  }