github.com/anjalikarhana/fabric@v2.1.1+incompatible/orderer/common/multichannel/util_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package multichannel 8 9 import ( 10 "fmt" 11 12 cb "github.com/hyperledger/fabric-protos-go/common" 13 "github.com/hyperledger/fabric/common/capabilities" 14 "github.com/hyperledger/fabric/common/channelconfig" 15 "github.com/hyperledger/fabric/common/configtx" 16 "github.com/hyperledger/fabric/core/config/configtest" 17 "github.com/hyperledger/fabric/internal/configtxgen/encoder" 18 "github.com/hyperledger/fabric/internal/configtxgen/genesisconfig" 19 "github.com/hyperledger/fabric/orderer/common/blockcutter" 20 "github.com/hyperledger/fabric/orderer/common/msgprocessor" 21 "github.com/hyperledger/fabric/orderer/consensus" 22 "github.com/hyperledger/fabric/protoutil" 23 ) 24 25 type mockConsenter struct { 26 } 27 28 func (mc *mockConsenter) HandleChain(support consensus.ConsenterSupport, metadata *cb.Metadata) (consensus.Chain, error) { 29 return &mockChain{ 30 queue: make(chan *cb.Envelope), 31 cutter: support.BlockCutter(), 32 support: support, 33 metadata: metadata, 34 done: make(chan struct{}), 35 }, nil 36 } 37 38 type mockChain struct { 39 queue chan *cb.Envelope 40 cutter blockcutter.Receiver 41 support consensus.ConsenterSupport 42 metadata *cb.Metadata 43 done chan struct{} 44 } 45 46 func (mch *mockChain) Errored() <-chan struct{} { 47 return nil 48 } 49 50 func (mch *mockChain) Order(env *cb.Envelope, configSeq uint64) error { 51 mch.queue <- env 52 return nil 53 } 54 55 func (mch *mockChain) Configure(config *cb.Envelope, configSeq uint64) error { 56 mch.queue <- config 57 return nil 58 } 59 60 func (mch *mockChain) WaitReady() error { 61 return nil 62 } 63 64 func (mch *mockChain) Start() { 65 go func() { 66 defer close(mch.done) 67 for { 68 msg, ok := <-mch.queue 69 if !ok { 70 return 71 } 72 73 chdr, err := protoutil.ChannelHeader(msg) 74 if err != nil { 75 logger.Panicf("If a message has arrived to this point, it should already have had header inspected once: %s", err) 76 } 77 78 class := mch.support.ClassifyMsg(chdr) 79 switch class { 80 case msgprocessor.ConfigMsg: 81 batch := mch.support.BlockCutter().Cut() 82 if batch != nil { 83 block := mch.support.CreateNextBlock(batch) 84 mch.support.WriteBlock(block, nil) 85 } 86 87 _, err := mch.support.ProcessNormalMsg(msg) 88 if err != nil { 89 logger.Warningf("Discarding bad config message: %s", err) 90 continue 91 } 92 block := mch.support.CreateNextBlock([]*cb.Envelope{msg}) 93 mch.support.WriteConfigBlock(block, nil) 94 case msgprocessor.NormalMsg: 95 batches, _ := mch.support.BlockCutter().Ordered(msg) 96 for _, batch := range batches { 97 block := mch.support.CreateNextBlock(batch) 98 mch.support.WriteBlock(block, nil) 99 } 100 case msgprocessor.ConfigUpdateMsg: 101 logger.Panicf("Not expecting msg class ConfigUpdateMsg here") 102 default: 103 logger.Panicf("Unsupported msg classification: %v", class) 104 } 105 } 106 }() 107 } 108 109 func (mch *mockChain) Halt() { 110 close(mch.queue) 111 } 112 113 func makeConfigTx(chainID string, i int) *cb.Envelope { 114 group := protoutil.NewConfigGroup() 115 group.Groups[channelconfig.OrdererGroupKey] = protoutil.NewConfigGroup() 116 group.Groups[channelconfig.OrdererGroupKey].Values[fmt.Sprintf("%d", i)] = &cb.ConfigValue{ 117 Value: []byte(fmt.Sprintf("%d", i)), 118 } 119 return makeConfigTxFromConfigUpdateEnvelope(chainID, &cb.ConfigUpdateEnvelope{ 120 ConfigUpdate: protoutil.MarshalOrPanic(&cb.ConfigUpdate{ 121 WriteSet: group, 122 }), 123 }) 124 } 125 126 func makeConfigTxFull(chainID string, i int) *cb.Envelope { 127 gConf := genesisconfig.Load(genesisconfig.SampleInsecureSoloProfile, configtest.GetDevConfigDir()) 128 gConf.Orderer.Capabilities = map[string]bool{ 129 capabilities.OrdererV2_0: true, 130 } 131 gConf.Orderer.MaxChannels = 10 132 channelGroup, err := encoder.NewChannelGroup(gConf) 133 if err != nil { 134 return nil 135 } 136 137 return makeConfigTxFromConfigUpdateEnvelope(chainID, &cb.ConfigUpdateEnvelope{ 138 ConfigUpdate: protoutil.MarshalOrPanic(&cb.ConfigUpdate{ 139 WriteSet: channelGroup, 140 }), 141 }) 142 } 143 144 func makeConfigTxMig(chainID string, i int) *cb.Envelope { 145 gConf := genesisconfig.Load(genesisconfig.SampleInsecureSoloProfile, configtest.GetDevConfigDir()) 146 gConf.Orderer.Capabilities = map[string]bool{ 147 capabilities.OrdererV2_0: true, 148 } 149 gConf.Orderer.OrdererType = "kafka" 150 channelGroup, err := encoder.NewChannelGroup(gConf) 151 if err != nil { 152 return nil 153 } 154 155 return makeConfigTxFromConfigUpdateEnvelope(chainID, &cb.ConfigUpdateEnvelope{ 156 ConfigUpdate: protoutil.MarshalOrPanic(&cb.ConfigUpdate{ 157 WriteSet: channelGroup, 158 }), 159 }) 160 } 161 162 func wrapConfigTx(env *cb.Envelope) *cb.Envelope { 163 result, err := protoutil.CreateSignedEnvelope(cb.HeaderType_ORDERER_TRANSACTION, "testchannelid", mockCrypto(), env, msgVersion, epoch) 164 if err != nil { 165 panic(err) 166 } 167 return result 168 } 169 170 func makeConfigTxFromConfigUpdateEnvelope(chainID string, configUpdateEnv *cb.ConfigUpdateEnvelope) *cb.Envelope { 171 configUpdateTx, err := protoutil.CreateSignedEnvelope(cb.HeaderType_CONFIG_UPDATE, chainID, mockCrypto(), configUpdateEnv, msgVersion, epoch) 172 if err != nil { 173 panic(err) 174 } 175 configTx, err := protoutil.CreateSignedEnvelope(cb.HeaderType_CONFIG, chainID, mockCrypto(), &cb.ConfigEnvelope{ 176 Config: &cb.Config{Sequence: 1, ChannelGroup: configtx.UnmarshalConfigUpdateOrPanic(configUpdateEnv.ConfigUpdate).WriteSet}, 177 LastUpdate: configUpdateTx}, 178 msgVersion, epoch) 179 if err != nil { 180 panic(err) 181 } 182 return configTx 183 } 184 185 func makeNormalTx(chainID string, i int) *cb.Envelope { 186 payload := &cb.Payload{ 187 Header: &cb.Header{ 188 ChannelHeader: protoutil.MarshalOrPanic(&cb.ChannelHeader{ 189 Type: int32(cb.HeaderType_ENDORSER_TRANSACTION), 190 ChannelId: chainID, 191 }), 192 SignatureHeader: protoutil.MarshalOrPanic(&cb.SignatureHeader{}), 193 }, 194 Data: []byte(fmt.Sprintf("%d", i)), 195 } 196 return &cb.Envelope{ 197 Payload: protoutil.MarshalOrPanic(payload), 198 } 199 }