github.com/osdi23p228/fabric@v0.0.0-20221218062954-77808885f5db/orderer/common/channelparticipation/validator.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package channelparticipation
     8  
     9  import (
    10  	"errors"
    11  	"fmt"
    12  
    13  	cb "github.com/hyperledger/fabric-protos-go/common"
    14  	"github.com/osdi23p228/fabric/bccsp/factory"
    15  	"github.com/osdi23p228/fabric/common/channelconfig"
    16  	"github.com/osdi23p228/fabric/protoutil"
    17  )
    18  
    19  // ValidateJoinBlock returns whether this block can be used as a join block for the channel participation API
    20  // and whether it is an system channel if it contains consortiums, or otherwise
    21  // and application channel if an application group exists.
    22  func ValidateJoinBlock(channelID string, configBlock *cb.Block) (isAppChannel bool, err error) {
    23  	if !protoutil.IsConfigBlock(configBlock) {
    24  		return false, errors.New("block is not a config block")
    25  	}
    26  
    27  	envelope, err := protoutil.ExtractEnvelope(configBlock, 0)
    28  	if err != nil {
    29  		return false, err
    30  	}
    31  
    32  	cryptoProvider := factory.GetDefault()
    33  	bundle, err := channelconfig.NewBundleFromEnvelope(envelope, cryptoProvider)
    34  	if err != nil {
    35  		return false, err
    36  	}
    37  
    38  	// Check channel id from join block matches
    39  	if bundle.ConfigtxValidator().ChannelID() != channelID {
    40  		return false, fmt.Errorf("config block channelID [%s] does not match passed channelID [%s]",
    41  			bundle.ConfigtxValidator().ChannelID(), channelID)
    42  	}
    43  
    44  	// Check channel type
    45  	_, isSystemChannel := bundle.ConsortiumsConfig()
    46  	if !isSystemChannel {
    47  		_, isAppChannel = bundle.ApplicationConfig()
    48  		if !isAppChannel {
    49  			return false, errors.New("invalid config: must have at least one of application or consortiums")
    50  		}
    51  	}
    52  
    53  	return isAppChannel, err
    54  }