github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/hyperledger/fabric-config/configtx/example_test.go (about)

     1  /*
     2  Copyright IBM Corp All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package configtx_test
     8  
     9  import (
    10  	"crypto/elliptic"
    11  	"crypto/rand"
    12  	"encoding/pem"
    13  	"fmt"
    14  	"github.com/hellobchain/newcryptosm/ecdsa"
    15  	"github.com/hellobchain/newcryptosm/x509"
    16  	"github.com/hellobchain/newcryptosm/x509/pkix"
    17  	configtx2 "github.com/hellobchain/third_party/hyperledger/fabric-config/configtx"
    18  	"github.com/hellobchain/third_party/hyperledger/fabric-config/configtx/membership"
    19  	"github.com/hellobchain/third_party/hyperledger/fabric-config/configtx/orderer"
    20  	"log"
    21  	"math/big"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/golang/protobuf/proto"
    26  	cb "github.com/hyperledger/fabric-protos-go/common"
    27  	mb "github.com/hyperledger/fabric-protos-go/msp"
    28  	ob "github.com/hyperledger/fabric-protos-go/orderer"
    29  	pb "github.com/hyperledger/fabric-protos-go/peer"
    30  	. "github.com/onsi/gomega"
    31  )
    32  
    33  const (
    34  	// Arbitrary valid pem encoded x509 certificate from crypto/x509 tests.
    35  	// The contents of the certifcate don't matter, we just need a valid certificate
    36  	// to pass marshaling/unmarshalling.
    37  	dummyCert = `-----BEGIN CERTIFICATE-----
    38  MIIDATCCAemgAwIBAgIRAKQkkrFx1T/dgB/Go/xBM5swDQYJKoZIhvcNAQELBQAw
    39  EjEQMA4GA1UEChMHQWNtZSBDbzAeFw0xNjA4MTcyMDM2MDdaFw0xNzA4MTcyMDM2
    40  MDdaMBIxEDAOBgNVBAoTB0FjbWUgQ28wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAw
    41  ggEKAoIBAQDAoJtjG7M6InsWwIo+l3qq9u+g2rKFXNu9/mZ24XQ8XhV6PUR+5HQ4
    42  jUFWC58ExYhottqK5zQtKGkw5NuhjowFUgWB/VlNGAUBHtJcWR/062wYrHBYRxJH
    43  qVXOpYKbIWwFKoXu3hcpg/CkdOlDWGKoZKBCwQwUBhWE7MDhpVdQ+ZljUJWL+FlK
    44  yQK5iRsJd5TGJ6VUzLzdT4fmN2DzeK6GLeyMpVpU3sWV90JJbxWQ4YrzkKzYhMmB
    45  EcpXTG2wm+ujiHU/k2p8zlf8Sm7VBM/scmnMFt0ynNXop4FWvJzEm1G0xD2t+e2I
    46  5Utr04dOZPCgkm++QJgYhtZvgW7ZZiGTAgMBAAGjUjBQMA4GA1UdDwEB/wQEAwIF
    47  oDATBgNVHSUEDDAKBggrBgEFBQcDATAMBgNVHRMBAf8EAjAAMBsGA1UdEQQUMBKC
    48  EHRlc3QuZXhhbXBsZS5jb20wDQYJKoZIhvcNAQELBQADggEBADpqKQxrthH5InC7
    49  X96UP0OJCu/lLEMkrjoEWYIQaFl7uLPxKH5AmQPH4lYwF7u7gksR7owVG9QU9fs6
    50  1fK7II9CVgCd/4tZ0zm98FmU4D0lHGtPARrrzoZaqVZcAvRnFTlPX5pFkPhVjjai
    51  /mkxX9LpD8oK1445DFHxK5UjLMmPIIWd8EOi+v5a+hgGwnJpoW7hntSl8kHMtTmy
    52  fnnktsblSUV4lRCit0ymC7Ojhe+gzCCwkgs5kDzVVag+tnl/0e2DloIjASwOhpbH
    53  KVcg7fBd484ht/sS+l0dsB4KDOSpd8JzVDMF8OZqlaydizoJO0yWr9GbCN1+OKq5
    54  EhLrEqU=
    55  -----END CERTIFICATE-----
    56  `
    57  
    58  	// Arbitrary valid pem encoded ec private key.
    59  	// The contents of the private key don't matter, we just need a valid
    60  	// EC private key to pass marshaling/unmarshalling.
    61  	dummyPrivateKey = `-----BEGIN EC PRIVATE KEY-----
    62  MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgDZUgDvKixfLi8cK8
    63  /TFLY97TDmQV3J2ygPpvuI8jSdihRANCAARRN3xgbPIR83dr27UuDaf2OJezpEJx
    64  UC3v06+FD8MUNcRAboqt4akehaNNSh7MMZI+HdnsM4RXN2y8NePUQsPL
    65  -----END EC PRIVATE KEY-----
    66  `
    67  
    68  	// Arbitrary valid pem encoded x509 crl.
    69  	// The contents of the CRL don't matter, we just need a valid
    70  	// CRL to pass marshaling/unmarshalling.
    71  	dummyCRL = `-----BEGIN X509 CRL-----
    72  MIIBYDCBygIBATANBgkqhkiG9w0BAQUFADBDMRMwEQYKCZImiZPyLGQBGRYDY29t
    73  MRcwFQYKCZImiZPyLGQBGRYHZXhhbXBsZTETMBEGA1UEAxMKRXhhbXBsZSBDQRcN
    74  MDUwMjA1MTIwMDAwWhcNMDUwMjA2MTIwMDAwWjAiMCACARIXDTA0MTExOTE1NTcw
    75  M1owDDAKBgNVHRUEAwoBAaAvMC0wHwYDVR0jBBgwFoAUCGivhTPIOUp6+IKTjnBq
    76  SiCELDIwCgYDVR0UBAMCAQwwDQYJKoZIhvcNAQEFBQADgYEAItwYffcIzsx10NBq
    77  m60Q9HYjtIFutW2+DvsVFGzIF20f7pAXom9g5L2qjFXejoRvkvifEBInr0rUL4Xi
    78  NkR9qqNMJTgV/wD9Pn7uPSYS69jnK2LiK8NGgO94gtEVxtCccmrLznrtZ5mLbnCB
    79  fUNCdMGmr8FVF6IzTNYGmCuk/C4=
    80  -----END X509 CRL-----
    81  `
    82  )
    83  
    84  // This example shows the basic usage of the package: modifying, computing, and signing
    85  // a config update.
    86  func Example_basic() {
    87  	baseConfig := fetchSystemChannelConfig()
    88  	c := configtx2.New(baseConfig)
    89  
    90  	err := c.Consortium("SampleConsortium").SetChannelCreationPolicy(configtx2.Policy{
    91  		Type: configtx2.ImplicitMetaPolicyType,
    92  		Rule: "MAJORITY Admins",
    93  	})
    94  	if err != nil {
    95  		panic(err)
    96  	}
    97  
    98  	// Compute the delta
    99  	marshaledUpdate, err := c.ComputeMarshaledUpdate("testsyschannel")
   100  	if err != nil {
   101  		panic(err)
   102  	}
   103  
   104  	// Collect the necessary signatures
   105  	// The example respresents a 2 peer 1 org channel, to meet the policies defined
   106  	// the transaction will be signed by both peers
   107  	configSignatures := []*cb.ConfigSignature{}
   108  
   109  	peer1SigningIdentity := createSigningIdentity()
   110  	peer2SigningIdentity := createSigningIdentity()
   111  
   112  	signingIdentities := []configtx2.SigningIdentity{
   113  		peer1SigningIdentity,
   114  		peer2SigningIdentity,
   115  	}
   116  
   117  	for _, si := range signingIdentities {
   118  		// Create a signature for the config update with the specified signer identity
   119  		configSignature, err := si.CreateConfigSignature(marshaledUpdate)
   120  		if err != nil {
   121  			panic(err)
   122  		}
   123  
   124  		configSignatures = append(configSignatures, configSignature)
   125  	}
   126  
   127  	// Create the envelope with the list of config signatures
   128  	env, err := configtx2.NewEnvelope(marshaledUpdate, configSignatures...)
   129  	if err != nil {
   130  		panic(err)
   131  	}
   132  
   133  	// Sign the envelope with a signing identity
   134  	err = peer1SigningIdentity.SignEnvelope(env)
   135  	if err != nil {
   136  		panic(err)
   137  	}
   138  }
   139  
   140  // This example updates an existing orderer configuration.
   141  func ExampleOrdererGroup_SetConfiguration() {
   142  	baseConfig := fetchChannelConfig()
   143  	c := configtx2.New(baseConfig)
   144  	o := c.Orderer()
   145  
   146  	// Must retrieve the current orderer configuration from block and modify
   147  	// the desired values
   148  	oConfig, err := o.Configuration()
   149  	if err != nil {
   150  		panic(err)
   151  	}
   152  
   153  	oConfig.Kafka.Brokers = []string{"kafka0:9092", "kafka1:9092", "kafka2:9092"}
   154  	oConfig.BatchSize.MaxMessageCount = 500
   155  
   156  	err = o.SetConfiguration(oConfig)
   157  	if err != nil {
   158  		panic(nil)
   159  	}
   160  }
   161  
   162  // This example shows the addition and removal of ACLs.
   163  func Example_aCLs() {
   164  	baseConfig := fetchChannelConfig()
   165  	c := configtx2.New(baseConfig)
   166  	a := c.Application()
   167  
   168  	acls := map[string]string{
   169  		"peer/Propose": "/Channel/Application/Writers",
   170  	}
   171  
   172  	err := a.SetACLs(acls)
   173  	if err != nil {
   174  		panic(err)
   175  	}
   176  
   177  	aclsToDelete := []string{"event/Block"}
   178  
   179  	err = a.RemoveACLs(aclsToDelete)
   180  	if err != nil {
   181  		panic(err)
   182  	}
   183  }
   184  
   185  // This example shows the addition of an anchor peer and the removal of
   186  // an existing anchor peer.
   187  func Example_anchorPeers() {
   188  	baseConfig := fetchChannelConfig()
   189  	c := configtx2.New(baseConfig)
   190  	applicationOrg1 := c.Application().Organization("Org1")
   191  
   192  	newAnchorPeer := configtx2.Address{
   193  		Host: "127.0.0.2",
   194  		Port: 7051,
   195  	}
   196  
   197  	// Add a new anchor peer
   198  	err := applicationOrg1.AddAnchorPeer(newAnchorPeer)
   199  	if err != nil {
   200  		panic(err)
   201  	}
   202  
   203  	oldAnchorPeer := configtx2.Address{
   204  		Host: "127.0.0.1",
   205  		Port: 7051,
   206  	}
   207  
   208  	// Remove an anchor peer
   209  	err = applicationOrg1.RemoveAnchorPeer(oldAnchorPeer)
   210  	if err != nil {
   211  		panic(err)
   212  	}
   213  }
   214  
   215  // This example shows the addition and removal policies from different config
   216  // groups.
   217  func Example_policies() {
   218  	baseConfig := fetchChannelConfig()
   219  	c := configtx2.New(baseConfig)
   220  	applicationOrg1 := c.Application().Organization("Org1")
   221  
   222  	err := applicationOrg1.SetPolicy(
   223  		"TestPolicy",
   224  		configtx2.Policy{
   225  			Type: configtx2.ImplicitMetaPolicyType,
   226  			Rule: "MAJORITY Endorsement",
   227  		})
   228  	if err != nil {
   229  		panic(err)
   230  	}
   231  
   232  	err = applicationOrg1.RemovePolicy(configtx2.WritersPolicyKey)
   233  	if err != nil {
   234  		panic(err)
   235  	}
   236  
   237  	o := c.Orderer()
   238  	ordererOrg := o.Organization("OrdererOrg")
   239  
   240  	err = ordererOrg.RemovePolicy(configtx2.WritersPolicyKey)
   241  	if err != nil {
   242  		panic(err)
   243  	}
   244  
   245  	err = ordererOrg.SetPolicy(
   246  		"TestPolicy",
   247  		configtx2.Policy{
   248  			Type: configtx2.ImplicitMetaPolicyType,
   249  			Rule: "MAJORITY Endorsement",
   250  		})
   251  	if err != nil {
   252  		panic(err)
   253  	}
   254  
   255  	err = o.RemovePolicy(configtx2.WritersPolicyKey)
   256  	if err != nil {
   257  		panic(err)
   258  	}
   259  
   260  	err = o.SetPolicy("TestPolicy", configtx2.Policy{
   261  		Type: configtx2.ImplicitMetaPolicyType,
   262  		Rule: "MAJORITY Endorsement",
   263  	})
   264  	if err != nil {
   265  		panic(err)
   266  	}
   267  }
   268  
   269  // This example shows the bulk replacement of multiple policies
   270  // for different config groups.
   271  func Example_policies2() {
   272  	baseConfig := fetchChannelConfig()
   273  	c := configtx2.New(baseConfig)
   274  
   275  	a := c.Application()
   276  	newAppPolicies := map[string]configtx2.Policy{
   277  		configtx2.ReadersPolicyKey: {
   278  			Type: configtx2.ImplicitMetaPolicyType,
   279  			Rule: "ANY Readers",
   280  		},
   281  		configtx2.WritersPolicyKey: {
   282  			Type: configtx2.ImplicitMetaPolicyType,
   283  			Rule: "ANY Writers",
   284  		},
   285  		configtx2.AdminsPolicyKey: {
   286  			Type: configtx2.ImplicitMetaPolicyType,
   287  			Rule: "MAJORITY Admins",
   288  		},
   289  		configtx2.EndorsementPolicyKey: {
   290  			Type: configtx2.ImplicitMetaPolicyType,
   291  			Rule: "MAJORITY Endorsement",
   292  		},
   293  		configtx2.LifecycleEndorsementPolicyKey: {
   294  			Type: configtx2.ImplicitMetaPolicyType,
   295  			Rule: "MAJORITY Endorsement",
   296  		},
   297  		"TestPolicy1": {
   298  			Type: configtx2.ImplicitMetaPolicyType,
   299  			Rule: "MAJORITY Endorsement",
   300  		},
   301  		"TestPolicy2": {
   302  			Type: configtx2.ImplicitMetaPolicyType,
   303  			Rule: "MAJORITY Admins",
   304  		},
   305  	}
   306  	err := a.SetPolicies(newAppPolicies)
   307  	if err != nil {
   308  		panic(err)
   309  	}
   310  
   311  	o := c.Orderer()
   312  	newOrdererPolicies := map[string]configtx2.Policy{
   313  		configtx2.ReadersPolicyKey: {
   314  			Type: configtx2.ImplicitMetaPolicyType,
   315  			Rule: "ANY Readers",
   316  		},
   317  		configtx2.WritersPolicyKey: {
   318  			Type: configtx2.ImplicitMetaPolicyType,
   319  			Rule: "ANY Writers",
   320  		},
   321  		configtx2.AdminsPolicyKey: {
   322  			Type: configtx2.ImplicitMetaPolicyType,
   323  			Rule: "MAJORITY Admins",
   324  		},
   325  		configtx2.BlockValidationPolicyKey: {
   326  			Type: configtx2.ImplicitMetaPolicyType,
   327  			Rule: "ANY Writers",
   328  		},
   329  		"TestPolicy1": {
   330  			Type: configtx2.ImplicitMetaPolicyType,
   331  			Rule: "MAJORITY Admins",
   332  		},
   333  		"TestPolicy2": {
   334  			Type: configtx2.ImplicitMetaPolicyType,
   335  			Rule: "MAJORITY Writers",
   336  		},
   337  	}
   338  	err = o.SetPolicies(newOrdererPolicies)
   339  	if err != nil {
   340  		panic(err)
   341  	}
   342  }
   343  
   344  // This example shows the addition of an orderer endpoint and the removal of
   345  // an existing orderer endpoint.
   346  func Example_ordererEndpoints() {
   347  	baseConfig := fetchChannelConfig()
   348  	c := configtx2.New(baseConfig)
   349  	ordererOrg := c.Orderer().Organization("OrdererOrg")
   350  
   351  	err := ordererOrg.SetEndpoint(
   352  		configtx2.Address{Host: "127.0.0.3", Port: 8050},
   353  	)
   354  	if err != nil {
   355  		panic(err)
   356  	}
   357  
   358  	err = ordererOrg.RemoveEndpoint(
   359  		configtx2.Address{Host: "127.0.0.1", Port: 9050},
   360  	)
   361  	if err != nil {
   362  		panic(err)
   363  	}
   364  }
   365  
   366  // This example shows the addition and removal of organizations from
   367  // config groups.
   368  func Example_organizations() {
   369  	baseConfig := fetchChannelConfig()
   370  	c := configtx2.New(baseConfig)
   371  	a := c.Application()
   372  
   373  	appOrg := configtx2.Organization{
   374  		Name: "Org2",
   375  		MSP:  baseMSP(&testing.T{}),
   376  		Policies: map[string]configtx2.Policy{
   377  			configtx2.AdminsPolicyKey: {
   378  				Type: configtx2.ImplicitMetaPolicyType,
   379  				Rule: "MAJORITY Admins",
   380  			},
   381  			configtx2.EndorsementPolicyKey: {
   382  				Type: configtx2.ImplicitMetaPolicyType,
   383  				Rule: "MAJORITY Endorsement",
   384  			},
   385  			configtx2.LifecycleEndorsementPolicyKey: {
   386  				Type: configtx2.ImplicitMetaPolicyType,
   387  				Rule: "MAJORITY Endorsement",
   388  			},
   389  			configtx2.ReadersPolicyKey: {
   390  				Type: configtx2.ImplicitMetaPolicyType,
   391  				Rule: "ANY Readers",
   392  			},
   393  			configtx2.WritersPolicyKey: {
   394  				Type: configtx2.ImplicitMetaPolicyType,
   395  				Rule: "ANY Writers",
   396  			},
   397  		},
   398  		AnchorPeers: []configtx2.Address{
   399  			{
   400  				Host: "127.0.0.1",
   401  				Port: 7051,
   402  			},
   403  		},
   404  	}
   405  
   406  	err := a.SetOrganization(appOrg)
   407  	if err != nil {
   408  		panic(err)
   409  	}
   410  
   411  	a.RemoveOrganization("Org2")
   412  
   413  	o := c.Orderer()
   414  
   415  	// Orderer Organization
   416  	ordererOrg := appOrg
   417  	ordererOrg.Name = "OrdererOrg2"
   418  	ordererOrg.AnchorPeers = nil
   419  
   420  	err = o.SetOrganization(ordererOrg)
   421  	if err != nil {
   422  		panic(err)
   423  	}
   424  
   425  	o.RemoveOrganization("OrdererOrg2")
   426  }
   427  
   428  // This example shows updating the individual orderer configuration values.
   429  func ExampleOrdererGroup_SetConfiguration_individual() {
   430  	baseConfig := fetchChannelConfig()
   431  	c := configtx2.New(baseConfig)
   432  	o := c.Orderer()
   433  
   434  	err := o.BatchSize().SetMaxMessageCount(500)
   435  	if err != nil {
   436  		panic(err)
   437  	}
   438  
   439  	err = o.AddConsenter(orderer.Consenter{
   440  		Address:       orderer.EtcdAddress{Host: "host1", Port: 7050},
   441  		ClientTLSCert: generateCert(),
   442  		ServerTLSCert: generateCert(),
   443  	})
   444  	if err != nil {
   445  		panic(err)
   446  	}
   447  
   448  	err = o.EtcdRaftOptions().SetElectionInterval(50)
   449  	if err != nil {
   450  		panic(err)
   451  	}
   452  }
   453  
   454  func ExampleNewSystemChannelGenesisBlock() {
   455  	channel := configtx2.Channel{
   456  		Consortiums: []configtx2.Consortium{
   457  			{
   458  				Name: "Consortium1",
   459  				Organizations: []configtx2.Organization{
   460  					{
   461  						Name: "Org1MSP",
   462  						Policies: map[string]configtx2.Policy{
   463  							configtx2.ReadersPolicyKey: {
   464  								Type: configtx2.SignaturePolicyType,
   465  								Rule: "OR('Org1MSP.admin', 'Org1MSP.peer'," +
   466  									"'Org1MSP.client')",
   467  							},
   468  							configtx2.WritersPolicyKey: {
   469  								Type: configtx2.SignaturePolicyType,
   470  								Rule: "OR('Org1MSP.admin', 'Org1MSP.client')",
   471  							},
   472  							configtx2.AdminsPolicyKey: {
   473  								Type: configtx2.SignaturePolicyType,
   474  								Rule: "OR('Org1MSP.admin')",
   475  							},
   476  							configtx2.EndorsementPolicyKey: {
   477  								Type: configtx2.SignaturePolicyType,
   478  								Rule: "OR('Org1MSP.peer')",
   479  							},
   480  						},
   481  						MSP: baseMSP(&testing.T{}),
   482  					},
   483  				},
   484  			},
   485  		},
   486  		Orderer: configtx2.Orderer{
   487  			Policies: map[string]configtx2.Policy{
   488  				configtx2.ReadersPolicyKey: {
   489  					Type: configtx2.ImplicitMetaPolicyType,
   490  					Rule: "ANY Readers",
   491  				},
   492  				configtx2.WritersPolicyKey: {
   493  					Type: configtx2.ImplicitMetaPolicyType,
   494  					Rule: "ANY Writers",
   495  				},
   496  				configtx2.AdminsPolicyKey: {
   497  					Type: configtx2.ImplicitMetaPolicyType,
   498  					Rule: "MAJORITY Admins",
   499  				},
   500  				configtx2.BlockValidationPolicyKey: {
   501  					Type: configtx2.ImplicitMetaPolicyType,
   502  					Rule: "ANY Writers",
   503  				},
   504  			},
   505  			OrdererType: orderer.ConsensusTypeSolo,
   506  			Organizations: []configtx2.Organization{
   507  				{
   508  					Name: "OrdererMSP",
   509  					Policies: map[string]configtx2.Policy{
   510  						configtx2.ReadersPolicyKey: {
   511  							Type: configtx2.SignaturePolicyType,
   512  							Rule: "OR('OrdererMSP.member')",
   513  						},
   514  						configtx2.WritersPolicyKey: {
   515  							Type: configtx2.SignaturePolicyType,
   516  							Rule: "OR('OrdererMSP.member')",
   517  						},
   518  						configtx2.AdminsPolicyKey: {
   519  							Type: configtx2.SignaturePolicyType,
   520  							Rule: "OR('OrdererMSP.admin')",
   521  						},
   522  					},
   523  					OrdererEndpoints: []string{
   524  						"localhost:123",
   525  					},
   526  					MSP: baseMSP(&testing.T{}),
   527  				},
   528  			},
   529  			Capabilities: []string{"V1_3"},
   530  			BatchSize: orderer.BatchSize{
   531  				MaxMessageCount:   100,
   532  				AbsoluteMaxBytes:  100,
   533  				PreferredMaxBytes: 100,
   534  			},
   535  			BatchTimeout: 2 * time.Second,
   536  			State:        orderer.ConsensusStateNormal,
   537  		},
   538  		Capabilities: []string{"V2_0"},
   539  		Policies: map[string]configtx2.Policy{
   540  			configtx2.ReadersPolicyKey: {
   541  				Type: configtx2.ImplicitMetaPolicyType,
   542  				Rule: "ANY Readers",
   543  			},
   544  			configtx2.WritersPolicyKey: {
   545  				Type: configtx2.ImplicitMetaPolicyType,
   546  				Rule: "ANY Writers",
   547  			},
   548  			configtx2.AdminsPolicyKey: {
   549  				Type: configtx2.ImplicitMetaPolicyType,
   550  				Rule: "MAJORITY Admins",
   551  			},
   552  		},
   553  		Consortium: "",
   554  	}
   555  
   556  	channelID := "testSystemChannel"
   557  	_, err := configtx2.NewSystemChannelGenesisBlock(channel, channelID)
   558  	if err != nil {
   559  		panic(err)
   560  	}
   561  }
   562  
   563  func ExampleNewApplicationChannelGenesisBlock() {
   564  	channel := configtx2.Channel{
   565  		Orderer: configtx2.Orderer{
   566  			OrdererType: orderer.ConsensusTypeEtcdRaft,
   567  			Organizations: []configtx2.Organization{
   568  				{
   569  					Name: "OrdererMSP",
   570  					Policies: map[string]configtx2.Policy{
   571  						configtx2.ReadersPolicyKey: {
   572  							Type: configtx2.SignaturePolicyType,
   573  							Rule: "OR('OrdererMSP.member')",
   574  						},
   575  						configtx2.WritersPolicyKey: {
   576  							Type: configtx2.SignaturePolicyType,
   577  							Rule: "OR('OrdererMSP.member')",
   578  						},
   579  						configtx2.AdminsPolicyKey: {
   580  							Type: configtx2.SignaturePolicyType,
   581  							Rule: "OR('OrdererMSP.admin')",
   582  						},
   583  					},
   584  					OrdererEndpoints: []string{
   585  						"host1:7050",
   586  					},
   587  					MSP: baseMSP(&testing.T{}),
   588  				},
   589  			},
   590  			EtcdRaft: orderer.EtcdRaft{
   591  				Consenters: []orderer.Consenter{
   592  					{
   593  						Address: orderer.EtcdAddress{
   594  							Host: "host1",
   595  							Port: 7050,
   596  						},
   597  						ClientTLSCert: generateCert(),
   598  						ServerTLSCert: generateCert(),
   599  					},
   600  				},
   601  				Options: orderer.EtcdRaftOptions{
   602  					TickInterval:         "500ms",
   603  					ElectionTick:         10,
   604  					HeartbeatTick:        1,
   605  					MaxInflightBlocks:    5,
   606  					SnapshotIntervalSize: 16 * 1024 * 1024,
   607  				},
   608  			},
   609  			Policies: map[string]configtx2.Policy{
   610  				configtx2.ReadersPolicyKey: {
   611  					Type: configtx2.ImplicitMetaPolicyType,
   612  					Rule: "ANY Readers",
   613  				},
   614  				configtx2.WritersPolicyKey: {
   615  					Type: configtx2.ImplicitMetaPolicyType,
   616  					Rule: "ANY Writers",
   617  				},
   618  				configtx2.AdminsPolicyKey: {
   619  					Type: configtx2.ImplicitMetaPolicyType,
   620  					Rule: "MAJORITY Admins",
   621  				},
   622  				configtx2.BlockValidationPolicyKey: {
   623  					Type: configtx2.ImplicitMetaPolicyType,
   624  					Rule: "ANY Writers",
   625  				},
   626  			},
   627  			Capabilities: []string{"V2_0"},
   628  			BatchSize: orderer.BatchSize{
   629  				MaxMessageCount:   100,
   630  				AbsoluteMaxBytes:  100,
   631  				PreferredMaxBytes: 100,
   632  			},
   633  			BatchTimeout: 2 * time.Second,
   634  			State:        orderer.ConsensusStateNormal,
   635  		},
   636  		Application: configtx2.Application{
   637  			Organizations: []configtx2.Organization{
   638  				{
   639  					Name: "Org1MSP",
   640  					Policies: map[string]configtx2.Policy{
   641  						configtx2.ReadersPolicyKey: {
   642  							Type: configtx2.SignaturePolicyType,
   643  							Rule: "OR('Org1MSP.admin', 'Org1MSP.peer'," +
   644  								"'Org1MSP.client')",
   645  						},
   646  						configtx2.WritersPolicyKey: {
   647  							Type: configtx2.SignaturePolicyType,
   648  							Rule: "OR('Org1MSP.admin', 'Org1MSP.client')",
   649  						},
   650  						configtx2.AdminsPolicyKey: {
   651  							Type: configtx2.SignaturePolicyType,
   652  							Rule: "OR('Org1MSP.admin')",
   653  						},
   654  						configtx2.EndorsementPolicyKey: {
   655  							Type: configtx2.SignaturePolicyType,
   656  							Rule: "OR('Org1MSP.peer')",
   657  						},
   658  					},
   659  					MSP: baseMSP(&testing.T{}),
   660  				},
   661  				{
   662  					Name: "Org2MSP",
   663  					Policies: map[string]configtx2.Policy{
   664  						configtx2.ReadersPolicyKey: {
   665  							Type: configtx2.SignaturePolicyType,
   666  							Rule: "OR('Org2MSP.admin', 'Org2MSP.peer'," +
   667  								"'Org2MSP.client')",
   668  						},
   669  						configtx2.WritersPolicyKey: {
   670  							Type: configtx2.SignaturePolicyType,
   671  							Rule: "OR('Org2MSP.admin', 'Org2MSP.client')",
   672  						},
   673  						configtx2.AdminsPolicyKey: {
   674  							Type: configtx2.SignaturePolicyType,
   675  							Rule: "OR('Org2MSP.admin')",
   676  						},
   677  						configtx2.EndorsementPolicyKey: {
   678  							Type: configtx2.SignaturePolicyType,
   679  							Rule: "OR('Org2MSP.peer')",
   680  						},
   681  					},
   682  					MSP: baseMSP(&testing.T{}),
   683  				},
   684  			},
   685  			Capabilities: []string{"V2_0"},
   686  			ACLs: map[string]string{
   687  				"event/Block": "/Channel/Application/Readers",
   688  			},
   689  			Policies: map[string]configtx2.Policy{
   690  				configtx2.ReadersPolicyKey: {
   691  					Type: configtx2.ImplicitMetaPolicyType,
   692  					Rule: "ANY Readers",
   693  				},
   694  				configtx2.WritersPolicyKey: {
   695  					Type: configtx2.ImplicitMetaPolicyType,
   696  					Rule: "ANY Writers",
   697  				},
   698  				configtx2.AdminsPolicyKey: {
   699  					Type: configtx2.ImplicitMetaPolicyType,
   700  					Rule: "MAJORITY Admins",
   701  				},
   702  				configtx2.EndorsementPolicyKey: {
   703  					Type: configtx2.ImplicitMetaPolicyType,
   704  					Rule: "MAJORITY Endorsement",
   705  				},
   706  				configtx2.LifecycleEndorsementPolicyKey: {
   707  					Type: configtx2.ImplicitMetaPolicyType,
   708  					Rule: "MAJORITY Endorsement",
   709  				},
   710  			},
   711  		},
   712  		Capabilities: []string{"V2_0"},
   713  		Policies: map[string]configtx2.Policy{
   714  			configtx2.ReadersPolicyKey: {
   715  				Type: configtx2.ImplicitMetaPolicyType,
   716  				Rule: "ANY Readers",
   717  			},
   718  			configtx2.WritersPolicyKey: {
   719  				Type: configtx2.ImplicitMetaPolicyType,
   720  				Rule: "ANY Writers",
   721  			},
   722  			configtx2.AdminsPolicyKey: {
   723  				Type: configtx2.ImplicitMetaPolicyType,
   724  				Rule: "MAJORITY Admins",
   725  			},
   726  		},
   727  	}
   728  
   729  	channelID := "testchannel"
   730  	_, err := configtx2.NewApplicationChannelGenesisBlock(channel, channelID)
   731  	if err != nil {
   732  		panic(err)
   733  	}
   734  }
   735  
   736  func ExampleNewMarshaledCreateChannelTx() {
   737  	channel := configtx2.Channel{
   738  		Consortium: "SampleConsortium",
   739  		Application: configtx2.Application{
   740  			Organizations: []configtx2.Organization{
   741  				{
   742  					Name: "Org1",
   743  				},
   744  				{
   745  					Name: "Org2",
   746  				},
   747  			},
   748  			Capabilities: []string{"V1_3"},
   749  			ACLs: map[string]string{
   750  				"event/Block": "/Channel/Application/Readers",
   751  			},
   752  			Policies: map[string]configtx2.Policy{
   753  				configtx2.ReadersPolicyKey: {
   754  					Type: configtx2.ImplicitMetaPolicyType,
   755  					Rule: "ANY Readers",
   756  				},
   757  				configtx2.WritersPolicyKey: {
   758  					Type: configtx2.ImplicitMetaPolicyType,
   759  					Rule: "ANY Writers",
   760  				},
   761  				configtx2.AdminsPolicyKey: {
   762  					Type: configtx2.ImplicitMetaPolicyType,
   763  					Rule: "MAJORITY Admins",
   764  				},
   765  				configtx2.EndorsementPolicyKey: {
   766  					Type: configtx2.ImplicitMetaPolicyType,
   767  					Rule: "MAJORITY Endorsement",
   768  				},
   769  				configtx2.LifecycleEndorsementPolicyKey: {
   770  					Type: configtx2.ImplicitMetaPolicyType,
   771  					Rule: "MAJORITY Endorsement",
   772  				},
   773  			},
   774  		},
   775  	}
   776  
   777  	_, err := configtx2.NewMarshaledCreateChannelTx(channel, "testchannel")
   778  	if err != nil {
   779  		panic(err)
   780  	}
   781  }
   782  
   783  // This example shows the addition of a certificate to an application org's intermediate
   784  // certificate list.
   785  func ExampleApplicationOrg_SetMSP() {
   786  	baseConfig := fetchChannelConfig()
   787  	c := configtx2.New(baseConfig)
   788  	applicationOrg1 := c.Application().Organization("Org1")
   789  
   790  	msp, err := applicationOrg1.MSP().Configuration()
   791  	if err != nil {
   792  		panic(err)
   793  	}
   794  
   795  	newIntermediateCert := &x509.Certificate{
   796  		KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
   797  		IsCA:     true,
   798  	}
   799  
   800  	msp.IntermediateCerts = append(msp.IntermediateCerts, newIntermediateCert)
   801  
   802  	err = applicationOrg1.SetMSP(msp)
   803  	if err != nil {
   804  		panic(err)
   805  	}
   806  }
   807  
   808  // This example shows the addition of a certificate to an orderer org's intermediate
   809  // certificate list.
   810  func ExampleOrdererOrg_SetMSP() {
   811  	baseConfig := fetchChannelConfig()
   812  	c := configtx2.New(baseConfig)
   813  	ordererOrg := c.Orderer().Organization("OrdererOrg")
   814  
   815  	msp, err := ordererOrg.MSP().Configuration()
   816  	if err != nil {
   817  		panic(err)
   818  	}
   819  
   820  	newIntermediateCert := &x509.Certificate{
   821  		KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
   822  		IsCA:     true,
   823  	}
   824  
   825  	msp.IntermediateCerts = append(msp.IntermediateCerts, newIntermediateCert)
   826  
   827  	err = ordererOrg.SetMSP(msp)
   828  	if err != nil {
   829  		panic(err)
   830  	}
   831  }
   832  
   833  // This example shows the addition of a certificate to a consortium org's intermediate
   834  // certificate list.
   835  func ExampleConsortiumOrg_SetMSP() {
   836  	baseConfig := fetchSystemChannelConfig()
   837  	c := configtx2.New(baseConfig)
   838  
   839  	sampleConsortiumOrg1 := c.Consortium("SampleConsortium").Organization("Org1")
   840  
   841  	msp, err := sampleConsortiumOrg1.MSP().Configuration()
   842  	if err != nil {
   843  		panic(err)
   844  	}
   845  
   846  	newIntermediateCert := &x509.Certificate{
   847  		KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
   848  		IsCA:     true,
   849  	}
   850  
   851  	msp.IntermediateCerts = append(msp.IntermediateCerts, newIntermediateCert)
   852  
   853  	err = sampleConsortiumOrg1.SetMSP(msp)
   854  	if err != nil {
   855  		panic(err)
   856  	}
   857  }
   858  
   859  func ExampleOrdererGroup_RemoveLegacyKafkaBrokers() {
   860  	baseConfig := fetchChannelConfig()
   861  	c := configtx2.New(baseConfig)
   862  	ordererConfig, err := c.Orderer().Configuration()
   863  	if err != nil {
   864  		panic(err)
   865  	}
   866  	ordererConfig.OrdererType = orderer.ConsensusTypeEtcdRaft
   867  	ordererConfig.EtcdRaft = orderer.EtcdRaft{
   868  		Consenters: []orderer.Consenter{
   869  			{
   870  				Address: orderer.EtcdAddress{
   871  					Host: "host1",
   872  					Port: 7050,
   873  				},
   874  				ClientTLSCert: generateCert(),
   875  				ServerTLSCert: generateCert(),
   876  			},
   877  		},
   878  	}
   879  	c.Orderer().RemoveLegacyKafkaBrokers()
   880  }
   881  
   882  // fetchChannelConfig mocks retrieving the config transaction from the most recent configuration block.
   883  func fetchChannelConfig() *cb.Config {
   884  	return &cb.Config{
   885  		ChannelGroup: &cb.ConfigGroup{
   886  			Groups: map[string]*cb.ConfigGroup{
   887  				configtx2.OrdererGroupKey: {
   888  					Version: 1,
   889  					Groups: map[string]*cb.ConfigGroup{
   890  						"OrdererOrg": {
   891  							Groups: map[string]*cb.ConfigGroup{},
   892  							Values: map[string]*cb.ConfigValue{
   893  								configtx2.EndpointsKey: {
   894  									ModPolicy: configtx2.AdminsPolicyKey,
   895  									Value: marshalOrPanic(&cb.OrdererAddresses{
   896  										Addresses: []string{"127.0.0.1:7050"},
   897  									}),
   898  								},
   899  								configtx2.MSPKey: {
   900  									ModPolicy: configtx2.AdminsPolicyKey,
   901  									Value: marshalOrPanic(&mb.MSPConfig{
   902  										Config: []byte{},
   903  									}),
   904  								},
   905  							},
   906  							Policies: map[string]*cb.ConfigPolicy{
   907  								configtx2.AdminsPolicyKey: {
   908  									ModPolicy: configtx2.AdminsPolicyKey,
   909  									Policy: &cb.Policy{
   910  										Type: 3,
   911  										Value: marshalOrPanic(&cb.ImplicitMetaPolicy{
   912  											Rule:      cb.ImplicitMetaPolicy_MAJORITY,
   913  											SubPolicy: configtx2.AdminsPolicyKey,
   914  										}),
   915  									},
   916  								},
   917  								configtx2.ReadersPolicyKey: {
   918  									ModPolicy: configtx2.AdminsPolicyKey,
   919  									Policy: &cb.Policy{
   920  										Type: 3,
   921  										Value: marshalOrPanic(&cb.ImplicitMetaPolicy{
   922  											Rule:      cb.ImplicitMetaPolicy_ANY,
   923  											SubPolicy: configtx2.ReadersPolicyKey,
   924  										}),
   925  									},
   926  								},
   927  								configtx2.WritersPolicyKey: {
   928  									ModPolicy: configtx2.AdminsPolicyKey,
   929  									Policy: &cb.Policy{
   930  										Type: 3,
   931  										Value: marshalOrPanic(&cb.ImplicitMetaPolicy{
   932  											Rule:      cb.ImplicitMetaPolicy_ANY,
   933  											SubPolicy: configtx2.WritersPolicyKey,
   934  										}),
   935  									},
   936  								},
   937  							},
   938  							ModPolicy: configtx2.AdminsPolicyKey,
   939  						},
   940  					},
   941  					Values: map[string]*cb.ConfigValue{
   942  						orderer.ConsensusTypeKey: {
   943  							ModPolicy: configtx2.AdminsPolicyKey,
   944  							Value: marshalOrPanic(&ob.ConsensusType{
   945  								Type: orderer.ConsensusTypeKafka,
   946  							}),
   947  						},
   948  						orderer.ChannelRestrictionsKey: {
   949  							ModPolicy: configtx2.AdminsPolicyKey,
   950  							Value: marshalOrPanic(&ob.ChannelRestrictions{
   951  								MaxCount: 1,
   952  							}),
   953  						},
   954  						configtx2.CapabilitiesKey: {
   955  							ModPolicy: configtx2.AdminsPolicyKey,
   956  							Value: marshalOrPanic(&cb.Capabilities{
   957  								Capabilities: map[string]*cb.Capability{
   958  									"V1_3": {},
   959  								},
   960  							}),
   961  						},
   962  						orderer.KafkaBrokersKey: {
   963  							ModPolicy: configtx2.AdminsPolicyKey,
   964  							Value: marshalOrPanic(&ob.KafkaBrokers{
   965  								Brokers: []string{"kafka0:9092", "kafka1:9092"},
   966  							}),
   967  						},
   968  						orderer.BatchTimeoutKey: {
   969  							Value: marshalOrPanic(&ob.BatchTimeout{
   970  								Timeout: "15s",
   971  							}),
   972  						},
   973  						orderer.BatchSizeKey: {
   974  							Value: marshalOrPanic(&ob.BatchSize{
   975  								MaxMessageCount:   100,
   976  								AbsoluteMaxBytes:  100,
   977  								PreferredMaxBytes: 100,
   978  							}),
   979  						},
   980  					},
   981  					Policies: map[string]*cb.ConfigPolicy{
   982  						configtx2.AdminsPolicyKey: {
   983  							ModPolicy: configtx2.AdminsPolicyKey,
   984  							Policy: &cb.Policy{
   985  								Type: 3,
   986  								Value: marshalOrPanic(&cb.ImplicitMetaPolicy{
   987  									Rule:      cb.ImplicitMetaPolicy_MAJORITY,
   988  									SubPolicy: configtx2.AdminsPolicyKey,
   989  								}),
   990  							},
   991  						},
   992  						configtx2.ReadersPolicyKey: {
   993  							ModPolicy: configtx2.AdminsPolicyKey,
   994  							Policy: &cb.Policy{
   995  								Type: 3,
   996  								Value: marshalOrPanic(&cb.ImplicitMetaPolicy{
   997  									Rule:      cb.ImplicitMetaPolicy_ANY,
   998  									SubPolicy: configtx2.ReadersPolicyKey,
   999  								}),
  1000  							},
  1001  						},
  1002  						configtx2.WritersPolicyKey: {
  1003  							ModPolicy: configtx2.AdminsPolicyKey,
  1004  							Policy: &cb.Policy{
  1005  								Type: 3,
  1006  								Value: marshalOrPanic(&cb.ImplicitMetaPolicy{
  1007  									Rule:      cb.ImplicitMetaPolicy_ANY,
  1008  									SubPolicy: configtx2.WritersPolicyKey,
  1009  								}),
  1010  							},
  1011  						},
  1012  						configtx2.BlockValidationPolicyKey: {
  1013  							ModPolicy: configtx2.AdminsPolicyKey,
  1014  							Policy: &cb.Policy{
  1015  								Type: 3,
  1016  								Value: marshalOrPanic(&cb.ImplicitMetaPolicy{
  1017  									Rule:      cb.ImplicitMetaPolicy_ANY,
  1018  									SubPolicy: configtx2.WritersPolicyKey,
  1019  								}),
  1020  							},
  1021  						},
  1022  					},
  1023  				},
  1024  				configtx2.ApplicationGroupKey: {
  1025  					Groups: map[string]*cb.ConfigGroup{
  1026  						"Org1": {
  1027  							Groups: map[string]*cb.ConfigGroup{},
  1028  							Values: map[string]*cb.ConfigValue{
  1029  								configtx2.AnchorPeersKey: {
  1030  									ModPolicy: configtx2.AdminsPolicyKey,
  1031  									Value: marshalOrPanic(&pb.AnchorPeers{
  1032  										AnchorPeers: []*pb.AnchorPeer{
  1033  											{Host: "127.0.0.1", Port: 7050},
  1034  										},
  1035  									}),
  1036  								},
  1037  								configtx2.MSPKey: {
  1038  									ModPolicy: configtx2.AdminsPolicyKey,
  1039  									Value: marshalOrPanic(&mb.MSPConfig{
  1040  										Config: []byte{},
  1041  									}),
  1042  								},
  1043  							},
  1044  						},
  1045  					},
  1046  					Values: map[string]*cb.ConfigValue{
  1047  						configtx2.ACLsKey: {
  1048  							ModPolicy: configtx2.AdminsPolicyKey,
  1049  							Value: marshalOrPanic(&pb.ACLs{
  1050  								Acls: map[string]*pb.APIResource{
  1051  									"event/block": {PolicyRef: "/Channel/Application/Readers"},
  1052  								},
  1053  							}),
  1054  						},
  1055  						configtx2.CapabilitiesKey: {
  1056  							ModPolicy: configtx2.AdminsPolicyKey,
  1057  							Value: marshalOrPanic(&cb.Capabilities{
  1058  								Capabilities: map[string]*cb.Capability{
  1059  									"V1_3": {},
  1060  								},
  1061  							}),
  1062  						},
  1063  					},
  1064  					Policies: map[string]*cb.ConfigPolicy{
  1065  						configtx2.LifecycleEndorsementPolicyKey: {
  1066  							ModPolicy: configtx2.AdminsPolicyKey,
  1067  							Policy: &cb.Policy{
  1068  								Type: 3,
  1069  								Value: marshalOrPanic(&cb.ImplicitMetaPolicy{
  1070  									Rule:      cb.ImplicitMetaPolicy_MAJORITY,
  1071  									SubPolicy: configtx2.AdminsPolicyKey,
  1072  								}),
  1073  							},
  1074  						},
  1075  						configtx2.AdminsPolicyKey: {
  1076  							ModPolicy: configtx2.AdminsPolicyKey,
  1077  							Policy: &cb.Policy{
  1078  								Type: 3,
  1079  								Value: marshalOrPanic(&cb.ImplicitMetaPolicy{
  1080  									Rule:      cb.ImplicitMetaPolicy_MAJORITY,
  1081  									SubPolicy: configtx2.AdminsPolicyKey,
  1082  								}),
  1083  							},
  1084  						},
  1085  						configtx2.ReadersPolicyKey: {
  1086  							ModPolicy: configtx2.AdminsPolicyKey,
  1087  							Policy: &cb.Policy{
  1088  								Type: 3,
  1089  								Value: marshalOrPanic(&cb.ImplicitMetaPolicy{
  1090  									Rule:      cb.ImplicitMetaPolicy_ANY,
  1091  									SubPolicy: configtx2.ReadersPolicyKey,
  1092  								}),
  1093  							},
  1094  						},
  1095  						configtx2.WritersPolicyKey: {
  1096  							ModPolicy: configtx2.AdminsPolicyKey,
  1097  							Policy: &cb.Policy{
  1098  								Type: 3,
  1099  								Value: marshalOrPanic(&cb.ImplicitMetaPolicy{
  1100  									Rule:      cb.ImplicitMetaPolicy_ANY,
  1101  									SubPolicy: configtx2.WritersPolicyKey,
  1102  								}),
  1103  							},
  1104  						},
  1105  					},
  1106  				},
  1107  			},
  1108  			Values: map[string]*cb.ConfigValue{},
  1109  			Policies: map[string]*cb.ConfigPolicy{
  1110  				configtx2.AdminsPolicyKey: {
  1111  					ModPolicy: configtx2.AdminsPolicyKey,
  1112  					Policy: &cb.Policy{
  1113  						Type: 3,
  1114  						Value: marshalOrPanic(&cb.ImplicitMetaPolicy{
  1115  							Rule:      cb.ImplicitMetaPolicy_MAJORITY,
  1116  							SubPolicy: configtx2.AdminsPolicyKey,
  1117  						}),
  1118  					},
  1119  				},
  1120  				configtx2.ReadersPolicyKey: {
  1121  					ModPolicy: configtx2.AdminsPolicyKey,
  1122  					Policy: &cb.Policy{
  1123  						Type: 3,
  1124  						Value: marshalOrPanic(&cb.ImplicitMetaPolicy{
  1125  							Rule:      cb.ImplicitMetaPolicy_ANY,
  1126  							SubPolicy: configtx2.ReadersPolicyKey,
  1127  						}),
  1128  					},
  1129  				},
  1130  				configtx2.WritersPolicyKey: {
  1131  					ModPolicy: configtx2.AdminsPolicyKey,
  1132  					Policy: &cb.Policy{
  1133  						Type: 3,
  1134  						Value: marshalOrPanic(&cb.ImplicitMetaPolicy{
  1135  							Rule:      cb.ImplicitMetaPolicy_ANY,
  1136  							SubPolicy: configtx2.WritersPolicyKey,
  1137  						}),
  1138  					},
  1139  				},
  1140  			},
  1141  		},
  1142  	}
  1143  }
  1144  
  1145  // marshalOrPanic is a helper for proto marshal.
  1146  func marshalOrPanic(pb proto.Message) []byte {
  1147  	data, err := proto.Marshal(pb)
  1148  	if err != nil {
  1149  		panic(err)
  1150  	}
  1151  
  1152  	return data
  1153  }
  1154  
  1155  // createSigningIdentity returns a identity that can be used for signing transactions.
  1156  // Signing identity can be retrieved from MSP configuration for each peer.
  1157  func createSigningIdentity() configtx2.SigningIdentity {
  1158  	privKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  1159  	if err != nil {
  1160  		panic(fmt.Sprintf("Failed to generate private key: %v", err))
  1161  	}
  1162  
  1163  	return configtx2.SigningIdentity{
  1164  		Certificate: generateCert(),
  1165  		PrivateKey:  privKey,
  1166  		MSPID:       "Org1MSP",
  1167  	}
  1168  }
  1169  
  1170  // generateCert creates a certificate for the SigningIdentity.
  1171  func generateCert() *x509.Certificate {
  1172  	serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
  1173  	serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
  1174  	if err != nil {
  1175  		log.Fatalf("Failed to generate serial number: %s", err)
  1176  	}
  1177  
  1178  	return &x509.Certificate{
  1179  		SerialNumber: serialNumber,
  1180  		Subject: pkix.Name{
  1181  			CommonName:   "Wile E. Coyote",
  1182  			Organization: []string{"Acme Co"},
  1183  		},
  1184  		NotBefore:             time.Now(),
  1185  		NotAfter:              time.Now().Add(365 * 24 * time.Hour),
  1186  		KeyUsage:              x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
  1187  		ExtKeyUsage:           []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
  1188  		BasicConstraintsValid: true,
  1189  	}
  1190  }
  1191  
  1192  func fetchSystemChannelConfig() *cb.Config {
  1193  	return &cb.Config{
  1194  		ChannelGroup: &cb.ConfigGroup{
  1195  			Groups: map[string]*cb.ConfigGroup{
  1196  				configtx2.ConsortiumsGroupKey: {
  1197  					Groups: map[string]*cb.ConfigGroup{
  1198  						"SampleConsortium": {
  1199  							Groups: map[string]*cb.ConfigGroup{
  1200  								"Org1": {
  1201  									Groups:    map[string]*cb.ConfigGroup{},
  1202  									Policies:  map[string]*cb.ConfigPolicy{},
  1203  									Values:    map[string]*cb.ConfigValue{},
  1204  									ModPolicy: "Admins",
  1205  									Version:   0,
  1206  								},
  1207  								"Org2": {
  1208  									Groups:    map[string]*cb.ConfigGroup{},
  1209  									Policies:  map[string]*cb.ConfigPolicy{},
  1210  									Values:    map[string]*cb.ConfigValue{},
  1211  									ModPolicy: "Admins",
  1212  									Version:   0,
  1213  								},
  1214  							},
  1215  							Values: map[string]*cb.ConfigValue{
  1216  								configtx2.ChannelCreationPolicyKey: {
  1217  									ModPolicy: "/Channel/Orderer/Admins",
  1218  									Value: marshalOrPanic(&cb.Policy{
  1219  										Type: 3,
  1220  										Value: marshalOrPanic(&cb.ImplicitMetaPolicy{
  1221  											Rule:      cb.ImplicitMetaPolicy_ANY,
  1222  											SubPolicy: configtx2.AdminsPolicyKey,
  1223  										}),
  1224  									}),
  1225  								},
  1226  							},
  1227  						},
  1228  						"SampleConsortium2": {
  1229  							Groups: map[string]*cb.ConfigGroup{},
  1230  							Values: map[string]*cb.ConfigValue{
  1231  								configtx2.ChannelCreationPolicyKey: {
  1232  									ModPolicy: "/Channel/Orderer/Admins",
  1233  									Value: marshalOrPanic(&cb.Policy{
  1234  										Type: 3,
  1235  										Value: marshalOrPanic(&cb.ImplicitMetaPolicy{
  1236  											Rule:      cb.ImplicitMetaPolicy_ANY,
  1237  											SubPolicy: configtx2.AdminsPolicyKey,
  1238  										}),
  1239  									}),
  1240  								},
  1241  							},
  1242  						},
  1243  					},
  1244  					Values:   map[string]*cb.ConfigValue{},
  1245  					Policies: map[string]*cb.ConfigPolicy{},
  1246  				},
  1247  			},
  1248  		},
  1249  	}
  1250  }
  1251  
  1252  // baseMSP creates a basic MSP struct for organization.
  1253  func baseMSP(t *testing.T) configtx2.MSP {
  1254  	gt := NewGomegaWithT(t)
  1255  
  1256  	certBlock, _ := pem.Decode([]byte(dummyCert))
  1257  	gt.Expect(certBlock).NotTo(BeNil())
  1258  	cert, err := x509.ParseCertificate(certBlock.Bytes)
  1259  	gt.Expect(err).NotTo(HaveOccurred())
  1260  
  1261  	crlBlock, _ := pem.Decode([]byte(dummyCRL))
  1262  	gt.Expect(crlBlock).NotTo(BeNil())
  1263  	crl, err := x509.ParseCRL(crlBlock.Bytes)
  1264  	gt.Expect(err).NotTo(HaveOccurred())
  1265  
  1266  	return configtx2.MSP{
  1267  		Name:              "MSPID",
  1268  		RootCerts:         []*x509.Certificate{cert},
  1269  		IntermediateCerts: []*x509.Certificate{cert},
  1270  		Admins:            []*x509.Certificate{cert},
  1271  		RevocationList:    []*pkix.CertificateList{crl},
  1272  		OrganizationalUnitIdentifiers: []membership.OUIdentifier{
  1273  			{
  1274  				Certificate:                  cert,
  1275  				OrganizationalUnitIdentifier: "OUID",
  1276  			},
  1277  		},
  1278  		CryptoConfig: membership.CryptoConfig{
  1279  			SignatureHashFamily:            "SHA3",
  1280  			IdentityIdentifierHashFunction: "SHA256",
  1281  		},
  1282  		TLSRootCerts:         []*x509.Certificate{cert},
  1283  		TLSIntermediateCerts: []*x509.Certificate{cert},
  1284  		NodeOUs: membership.NodeOUs{
  1285  			ClientOUIdentifier: membership.OUIdentifier{
  1286  				Certificate:                  cert,
  1287  				OrganizationalUnitIdentifier: "OUID",
  1288  			},
  1289  			PeerOUIdentifier: membership.OUIdentifier{
  1290  				Certificate:                  cert,
  1291  				OrganizationalUnitIdentifier: "OUID",
  1292  			},
  1293  			AdminOUIdentifier: membership.OUIdentifier{
  1294  				Certificate:                  cert,
  1295  				OrganizationalUnitIdentifier: "OUID",
  1296  			},
  1297  			OrdererOUIdentifier: membership.OUIdentifier{
  1298  				Certificate:                  cert,
  1299  				OrganizationalUnitIdentifier: "OUID",
  1300  			},
  1301  		},
  1302  	}
  1303  }