go_study/fabric-main/orderer/common/multichannel/ledger_resources.go

91 lines
2.6 KiB
Go

/*
Copyright IBM Corp. 2017 All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package multichannel
import (
"github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric/bccsp"
"github.com/hyperledger/fabric/common/channelconfig"
"github.com/hyperledger/fabric/common/ledger/blockledger"
"github.com/hyperledger/fabric/protoutil"
"github.com/pkg/errors"
)
// checkResources makes sure that the channel config is compatible with this binary and logs sanity checks
func checkResources(res channelconfig.Resources) error {
channelconfig.LogSanityChecks(res)
oc, ok := res.OrdererConfig()
if !ok {
return errors.New("config does not contain orderer config")
}
if err := oc.Capabilities().Supported(); err != nil {
return errors.WithMessagef(err, "config requires unsupported orderer capabilities: %s", err)
}
if err := res.ChannelConfig().Capabilities().Supported(); err != nil {
return errors.WithMessagef(err, "config requires unsupported channel capabilities: %s", err)
}
return nil
}
// checkResourcesOrPanic invokes checkResources and panics if an error is returned
func checkResourcesOrPanic(res channelconfig.Resources) {
if err := checkResources(res); err != nil {
logger.Panicf("[channel %s] %s", res.ConfigtxValidator().ChannelID(), err)
}
}
type mutableResources interface {
channelconfig.Resources
Update(*channelconfig.Bundle)
}
type configResources struct {
mutableResources
bccsp bccsp.BCCSP
}
func (cr *configResources) CreateBundle(channelID string, config *common.Config) (*channelconfig.Bundle, error) {
return channelconfig.NewBundle(channelID, config, cr.bccsp)
}
func (cr *configResources) Update(bndl *channelconfig.Bundle) {
checkResourcesOrPanic(bndl)
cr.mutableResources.Update(bndl)
}
func (cr *configResources) SharedConfig() channelconfig.Orderer {
oc, ok := cr.OrdererConfig()
if !ok {
logger.Panicf("[channel %s] has no orderer configuration", cr.ConfigtxValidator().ChannelID())
}
return oc
}
type ledgerResources struct {
*configResources
blockledger.ReadWriter
signatureVerifier protoutil.BlockVerifierFunc
}
// ChannelID passes through to the underlying configtx.Validator
func (lr *ledgerResources) ChannelID() string {
return lr.ConfigtxValidator().ChannelID()
}
// SignatureVerifier verifies a signature of a block.
func (lr *ledgerResources) SignatureVerifier() protoutil.BlockVerifierFunc {
return lr.signatureVerifier
}
// Block returns a block with the following number, or nil if such a block doesn't exist.
func (lr *ledgerResources) Block(number uint64) *common.Block {
if lr.Height() <= number {
return nil
}
return blockledger.GetBlock(lr, number)
}