github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/orderer/multichain/util_test.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 multichain
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/hyperledger/fabric/common/config"
    23  	"github.com/hyperledger/fabric/common/configtx"
    24  	"github.com/hyperledger/fabric/common/configtx/tool/provisional"
    25  	"github.com/hyperledger/fabric/orderer/common/blockcutter"
    26  	cb "github.com/hyperledger/fabric/protos/common"
    27  	"github.com/hyperledger/fabric/protos/utils"
    28  )
    29  
    30  type mockConsenter struct {
    31  }
    32  
    33  func (mc *mockConsenter) HandleChain(support ConsenterSupport, metadata *cb.Metadata) (Chain, error) {
    34  	return &mockChain{
    35  		queue:    make(chan *cb.Envelope),
    36  		cutter:   support.BlockCutter(),
    37  		support:  support,
    38  		metadata: metadata,
    39  		done:     make(chan struct{}),
    40  	}, nil
    41  }
    42  
    43  type mockChain struct {
    44  	queue    chan *cb.Envelope
    45  	cutter   blockcutter.Receiver
    46  	support  ConsenterSupport
    47  	metadata *cb.Metadata
    48  	done     chan struct{}
    49  }
    50  
    51  func (mch *mockChain) Errored() <-chan struct{} {
    52  	return nil
    53  }
    54  
    55  func (mch *mockChain) Enqueue(env *cb.Envelope) bool {
    56  	mch.queue <- env
    57  	return true
    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  			batches, committers, _ := mch.cutter.Ordered(msg)
    69  			for i, batch := range batches {
    70  				block := mch.support.CreateNextBlock(batch)
    71  				mch.support.WriteBlock(block, committers[i], nil)
    72  			}
    73  		}
    74  	}()
    75  }
    76  
    77  func (mch *mockChain) Halt() {
    78  	close(mch.queue)
    79  }
    80  
    81  func makeConfigTx(chainID string, i int) *cb.Envelope {
    82  	group := cb.NewConfigGroup()
    83  	group.Groups[config.OrdererGroupKey] = cb.NewConfigGroup()
    84  	group.Groups[config.OrdererGroupKey].Values[fmt.Sprintf("%d", i)] = &cb.ConfigValue{
    85  		Value: []byte(fmt.Sprintf("%d", i)),
    86  	}
    87  	configTemplate := configtx.NewSimpleTemplate(group)
    88  	configEnv, err := configTemplate.Envelope(chainID)
    89  	if err != nil {
    90  		panic(err)
    91  	}
    92  	return makeConfigTxFromConfigUpdateEnvelope(chainID, configEnv)
    93  }
    94  
    95  func wrapConfigTx(env *cb.Envelope) *cb.Envelope {
    96  	result, err := utils.CreateSignedEnvelope(cb.HeaderType_ORDERER_TRANSACTION, provisional.TestChainID, mockCrypto(), env, msgVersion, epoch)
    97  	if err != nil {
    98  		panic(err)
    99  	}
   100  	return result
   101  }
   102  
   103  func makeConfigTxFromConfigUpdateEnvelope(chainID string, configUpdateEnv *cb.ConfigUpdateEnvelope) *cb.Envelope {
   104  	configUpdateTx, err := utils.CreateSignedEnvelope(cb.HeaderType_CONFIG_UPDATE, chainID, mockCrypto(), configUpdateEnv, msgVersion, epoch)
   105  	if err != nil {
   106  		panic(err)
   107  	}
   108  	configTx, err := utils.CreateSignedEnvelope(cb.HeaderType_CONFIG, chainID, mockCrypto(), &cb.ConfigEnvelope{
   109  		Config:     &cb.Config{Sequence: 1, ChannelGroup: configtx.UnmarshalConfigUpdateOrPanic(configUpdateEnv.ConfigUpdate).WriteSet},
   110  		LastUpdate: configUpdateTx},
   111  		msgVersion, epoch)
   112  	if err != nil {
   113  		panic(err)
   114  	}
   115  	return configTx
   116  }
   117  
   118  func makeNormalTx(chainID string, i int) *cb.Envelope {
   119  	payload := &cb.Payload{
   120  		Header: &cb.Header{
   121  			ChannelHeader: utils.MarshalOrPanic(&cb.ChannelHeader{
   122  				Type:      int32(cb.HeaderType_ENDORSER_TRANSACTION),
   123  				ChannelId: chainID,
   124  			}),
   125  			SignatureHeader: utils.MarshalOrPanic(&cb.SignatureHeader{}),
   126  		},
   127  		Data: []byte(fmt.Sprintf("%d", i)),
   128  	}
   129  	return &cb.Envelope{
   130  		Payload: utils.MarshalOrPanic(payload),
   131  	}
   132  }