github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/common/configtx/tool/provisional/provisional.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8                   http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package provisional
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/hyperledger/fabric/common/cauthdsl"
    23  	"github.com/hyperledger/fabric/common/configtx"
    24  	genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig"
    25  	configtxchannel "github.com/hyperledger/fabric/common/configvalues/channel"
    26  	configtxapplication "github.com/hyperledger/fabric/common/configvalues/channel/application"
    27  	configtxorderer "github.com/hyperledger/fabric/common/configvalues/channel/orderer"
    28  	configvaluesmsp "github.com/hyperledger/fabric/common/configvalues/msp"
    29  	"github.com/hyperledger/fabric/common/genesis"
    30  	"github.com/hyperledger/fabric/common/policies"
    31  	"github.com/hyperledger/fabric/msp"
    32  	"github.com/hyperledger/fabric/orderer/common/bootstrap"
    33  	cb "github.com/hyperledger/fabric/protos/common"
    34  	ab "github.com/hyperledger/fabric/protos/orderer"
    35  	pb "github.com/hyperledger/fabric/protos/peer"
    36  
    37  	logging "github.com/op/go-logging"
    38  )
    39  
    40  var logger = logging.MustGetLogger("common/configtx/tool/provisional")
    41  
    42  // Generator can either create an orderer genesis block or config template
    43  type Generator interface {
    44  	bootstrap.Helper
    45  
    46  	// ChannelTemplate returns a template which can be used to help initialize a channel
    47  	ChannelTemplate() configtx.Template
    48  }
    49  
    50  const (
    51  	// ConsensusTypeSolo identifies the solo consensus implementation.
    52  	ConsensusTypeSolo = "solo"
    53  	// ConsensusTypeKafka identifies the Kafka-based consensus implementation.
    54  	ConsensusTypeKafka = "kafka"
    55  	// ConsensusTypeSbft identifies the SBFT consensus implementation.
    56  	ConsensusTypeSbft = "sbft"
    57  
    58  	// TestChainID is the default value of ChainID. It is used by all testing
    59  	// networks. It it necessary to set and export this variable so that test
    60  	// clients can connect without being rejected for targetting a chain which
    61  	// does not exist.
    62  	TestChainID = "testchainid"
    63  
    64  	// AcceptAllPolicyKey is the key of the AcceptAllPolicy.
    65  	AcceptAllPolicyKey = "AcceptAllPolicy"
    66  
    67  	// BlockValidationPolicyKey
    68  	BlockValidationPolicyKey = "BlockValidation"
    69  )
    70  
    71  // DefaultChainCreationPolicyNames is the default value of ChainCreatorsKey.
    72  var DefaultChainCreationPolicyNames = []string{AcceptAllPolicyKey}
    73  
    74  type bootstrapper struct {
    75  	channelGroups              []*cb.ConfigGroup
    76  	ordererGroups              []*cb.ConfigGroup
    77  	applicationGroups          []*cb.ConfigGroup
    78  	ordererSystemChannelGroups []*cb.ConfigGroup
    79  }
    80  
    81  // New returns a new provisional bootstrap helper.
    82  func New(conf *genesisconfig.Profile) Generator {
    83  	bs := &bootstrapper{
    84  		channelGroups: []*cb.ConfigGroup{
    85  			// Chain Config Types
    86  			configtxchannel.DefaultHashingAlgorithm(),
    87  			configtxchannel.DefaultBlockDataHashingStructure(),
    88  			configtxchannel.TemplateOrdererAddresses(conf.Orderer.Addresses), // TODO, move to conf.Channel when it exists
    89  
    90  			// Default policies
    91  			policies.TemplateImplicitMetaAnyPolicy([]string{}, configvaluesmsp.ReadersPolicyKey),
    92  			policies.TemplateImplicitMetaAnyPolicy([]string{}, configvaluesmsp.WritersPolicyKey),
    93  			policies.TemplateImplicitMetaMajorityPolicy([]string{}, configvaluesmsp.AdminsPolicyKey),
    94  
    95  			// Temporary AcceptAllPolicy XXX, remove
    96  			cauthdsl.TemplatePolicy(AcceptAllPolicyKey, cauthdsl.AcceptAllPolicy),
    97  		},
    98  	}
    99  
   100  	if conf.Orderer != nil {
   101  		bs.ordererGroups = []*cb.ConfigGroup{
   102  			// Orderer Config Types
   103  			configtxorderer.TemplateConsensusType(conf.Orderer.OrdererType),
   104  			configtxorderer.TemplateBatchSize(&ab.BatchSize{
   105  				MaxMessageCount:   conf.Orderer.BatchSize.MaxMessageCount,
   106  				AbsoluteMaxBytes:  conf.Orderer.BatchSize.AbsoluteMaxBytes,
   107  				PreferredMaxBytes: conf.Orderer.BatchSize.PreferredMaxBytes,
   108  			}),
   109  			configtxorderer.TemplateBatchTimeout(conf.Orderer.BatchTimeout.String()),
   110  			configtxorderer.TemplateIngressPolicyNames([]string{AcceptAllPolicyKey}),
   111  			configtxorderer.TemplateEgressPolicyNames([]string{AcceptAllPolicyKey}),
   112  
   113  			// Initialize the default Reader/Writer/Admins orderer policies, as well as block validation policy
   114  			policies.TemplateImplicitMetaPolicyWithSubPolicy([]string{configtxorderer.GroupKey}, BlockValidationPolicyKey, configvaluesmsp.WritersPolicyKey, cb.ImplicitMetaPolicy_ANY),
   115  			policies.TemplateImplicitMetaAnyPolicy([]string{configtxorderer.GroupKey}, configvaluesmsp.ReadersPolicyKey),
   116  			policies.TemplateImplicitMetaAnyPolicy([]string{configtxorderer.GroupKey}, configvaluesmsp.WritersPolicyKey),
   117  			policies.TemplateImplicitMetaMajorityPolicy([]string{configtxorderer.GroupKey}, configvaluesmsp.AdminsPolicyKey),
   118  		}
   119  
   120  		for _, org := range conf.Orderer.Organizations {
   121  			mspConfig, err := msp.GetVerifyingMspConfig(org.MSPDir, org.BCCSP, org.ID)
   122  			if err != nil {
   123  				logger.Panicf("Error loading MSP configuration for org %s: %s", org.Name, err)
   124  			}
   125  			bs.ordererGroups = append(bs.ordererGroups, configvaluesmsp.TemplateGroupMSP([]string{configtxorderer.GroupKey, org.Name}, mspConfig))
   126  		}
   127  
   128  		switch conf.Orderer.OrdererType {
   129  		case ConsensusTypeSolo, ConsensusTypeSbft:
   130  		case ConsensusTypeKafka:
   131  			bs.ordererGroups = append(bs.ordererGroups, configtxorderer.TemplateKafkaBrokers(conf.Orderer.Kafka.Brokers))
   132  		default:
   133  			panic(fmt.Errorf("Wrong consenter type value given: %s", conf.Orderer.OrdererType))
   134  		}
   135  
   136  		bs.ordererSystemChannelGroups = []*cb.ConfigGroup{
   137  			// Policies
   138  			configtxorderer.TemplateChainCreationPolicyNames(DefaultChainCreationPolicyNames),
   139  		}
   140  	}
   141  
   142  	if conf.Application != nil {
   143  
   144  		bs.applicationGroups = []*cb.ConfigGroup{
   145  			// Initialize the default Reader/Writer/Admins application policies
   146  			policies.TemplateImplicitMetaAnyPolicy([]string{configtxapplication.GroupKey}, configvaluesmsp.ReadersPolicyKey),
   147  			policies.TemplateImplicitMetaAnyPolicy([]string{configtxapplication.GroupKey}, configvaluesmsp.WritersPolicyKey),
   148  			policies.TemplateImplicitMetaMajorityPolicy([]string{configtxapplication.GroupKey}, configvaluesmsp.AdminsPolicyKey),
   149  		}
   150  		for _, org := range conf.Application.Organizations {
   151  			mspConfig, err := msp.GetVerifyingMspConfig(org.MSPDir, org.BCCSP, org.ID)
   152  			if err != nil {
   153  				logger.Panicf("Error loading MSP configuration for org %s: %s", org.Name, err)
   154  			}
   155  			bs.applicationGroups = append(bs.applicationGroups, configvaluesmsp.TemplateGroupMSP([]string{configtxapplication.GroupKey, org.Name}, mspConfig))
   156  
   157  			var anchorProtos []*pb.AnchorPeer
   158  			for _, anchorPeer := range org.AnchorPeers {
   159  				anchorProtos = append(anchorProtos, &pb.AnchorPeer{
   160  					Host: anchorPeer.Host,
   161  					Port: int32(anchorPeer.Port),
   162  				})
   163  			}
   164  
   165  			bs.applicationGroups = append(bs.applicationGroups, configtxapplication.TemplateAnchorPeers(org.Name, anchorProtos))
   166  		}
   167  
   168  	}
   169  
   170  	return bs
   171  }
   172  
   173  func (bs *bootstrapper) ChannelTemplate() configtx.Template {
   174  	return configtx.NewCompositeTemplate(
   175  		configtx.NewSimpleTemplate(bs.channelGroups...),
   176  		configtx.NewSimpleTemplate(bs.ordererGroups...),
   177  		configtx.NewSimpleTemplate(bs.applicationGroups...),
   178  	)
   179  }
   180  
   181  // XXX deprecate and remove
   182  func (bs *bootstrapper) GenesisBlock() *cb.Block {
   183  	block, err := genesis.NewFactoryImpl(
   184  		configtx.NewCompositeTemplate(
   185  			configtx.NewSimpleTemplate(bs.ordererSystemChannelGroups...),
   186  			bs.ChannelTemplate(),
   187  		),
   188  	).Block(TestChainID)
   189  
   190  	if err != nil {
   191  		panic(err)
   192  	}
   193  	return block
   194  }
   195  
   196  func (bs *bootstrapper) GenesisBlockForChannel(channelID string) *cb.Block {
   197  	block, err := genesis.NewFactoryImpl(
   198  		configtx.NewCompositeTemplate(
   199  			configtx.NewSimpleTemplate(bs.ordererSystemChannelGroups...),
   200  			bs.ChannelTemplate(),
   201  		),
   202  	).Block(channelID)
   203  
   204  	if err != nil {
   205  		panic(err)
   206  	}
   207  	return block
   208  }