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