github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/orderer/mocks/common/multichannel/multichain.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 multichannel 18 19 import ( 20 "github.com/hyperledger/fabric/common/config" 21 mockconfig "github.com/hyperledger/fabric/common/mocks/config" 22 "github.com/hyperledger/fabric/orderer/common/blockcutter" 23 "github.com/hyperledger/fabric/orderer/common/multichannel" 24 mockblockcutter "github.com/hyperledger/fabric/orderer/mocks/common/blockcutter" 25 cb "github.com/hyperledger/fabric/protos/common" 26 "github.com/hyperledger/fabric/protos/utils" 27 28 "github.com/op/go-logging" 29 ) 30 31 var logger = logging.MustGetLogger("orderer/mocks/multichannel") 32 33 // ConsenterSupport is used to mock the multichannel.ConsenterSupport interface 34 // Whenever a block is written, it writes to the Batches channel to allow for synchronization 35 type ConsenterSupport struct { 36 // SharedConfigVal is the value returned by SharedConfig() 37 SharedConfigVal *mockconfig.Orderer 38 39 // BlockCutterVal is the value returned by BlockCutter() 40 BlockCutterVal *mockblockcutter.Receiver 41 42 // Blocks is the channel where WriteBlock writes the most recently created block 43 Blocks chan *cb.Block 44 45 // ChainIDVal is the value returned by ChainID() 46 ChainIDVal string 47 48 // HeightVal is the value returned by Height() 49 HeightVal uint64 50 51 // NextBlockVal stores the block created by the most recent CreateNextBlock() call 52 NextBlockVal *cb.Block 53 54 // ClassifyMsgVal is returned by ClassifyMsg 55 ClassifyMsgVal multichannel.MsgClassification 56 57 // ClassifyMsgErr is the err returned by ClassifyMsg 58 ClassifyMsgErr error 59 60 // ConfigSeqVal is returned as the configSeq for Process*Msg 61 ConfigSeqVal uint64 62 63 // ProcessNormalMsgErr is returned as the error for ProcessNormalMsg 64 ProcessNormalMsgErr error 65 66 // ProcessConfigUpdateMsgVal is returned as the error for ProcessConfigUpdateMsg 67 ProcessConfigUpdateMsgVal *cb.Envelope 68 69 // ProcessConfigUpdateMsgErr is returned as the error for ProcessConfigUpdateMsg 70 ProcessConfigUpdateMsgErr error 71 } 72 73 // BlockCutter returns BlockCutterVal 74 func (mcs *ConsenterSupport) BlockCutter() blockcutter.Receiver { 75 return mcs.BlockCutterVal 76 } 77 78 // SharedConfig returns SharedConfigVal 79 func (mcs *ConsenterSupport) SharedConfig() config.Orderer { 80 return mcs.SharedConfigVal 81 } 82 83 // CreateNextBlock creates a simple block structure with the given data 84 func (mcs *ConsenterSupport) CreateNextBlock(data []*cb.Envelope) *cb.Block { 85 block := cb.NewBlock(0, nil) 86 mtxs := make([][]byte, len(data)) 87 for i := range data { 88 mtxs[i] = utils.MarshalOrPanic(data[i]) 89 } 90 block.Data = &cb.BlockData{Data: mtxs} 91 mcs.NextBlockVal = block 92 return block 93 } 94 95 // WriteBlock writes data to the Blocks channel 96 func (mcs *ConsenterSupport) WriteBlock(block *cb.Block, encodedMetadataValue []byte) *cb.Block { 97 if encodedMetadataValue != nil { 98 block.Metadata.Metadata[cb.BlockMetadataIndex_ORDERER] = utils.MarshalOrPanic(&cb.Metadata{Value: encodedMetadataValue}) 99 } 100 mcs.HeightVal++ 101 mcs.Blocks <- block 102 return block 103 } 104 105 // WriteConfigBlock calls WriteBlock 106 func (mcs *ConsenterSupport) WriteConfigBlock(block *cb.Block, encodedMetadataValue []byte) *cb.Block { 107 return mcs.WriteBlock(block, encodedMetadataValue) 108 } 109 110 // ChainID returns the chain ID this specific consenter instance is associated with 111 func (mcs *ConsenterSupport) ChainID() string { 112 return mcs.ChainIDVal 113 } 114 115 // Height returns the number of blocks of the chain this specific consenter instance is associated with 116 func (mcs *ConsenterSupport) Height() uint64 { 117 return mcs.HeightVal 118 } 119 120 // Sign returns the bytes passed in 121 func (mcs *ConsenterSupport) Sign(message []byte) ([]byte, error) { 122 return message, nil 123 } 124 125 // NewSignatureHeader returns an empty signature header 126 func (mcs *ConsenterSupport) NewSignatureHeader() (*cb.SignatureHeader, error) { 127 return &cb.SignatureHeader{}, nil 128 } 129 130 // ClassifyMsg returns ClassifyMsgVal, ClassifyMsgErr 131 func (mcs *ConsenterSupport) ClassifyMsg(env *cb.Envelope) (multichannel.MsgClassification, error) { 132 return mcs.ClassifyMsgVal, mcs.ClassifyMsgErr 133 } 134 135 // ProcessNormalMsg returns ConfigSeqVal, ProcessNormalMsgErr 136 func (mcs *ConsenterSupport) ProcessNormalMsg(env *cb.Envelope) (configSeq uint64, err error) { 137 return mcs.ConfigSeqVal, mcs.ProcessNormalMsgErr 138 } 139 140 // ProcessConfigUpdateMsg returns ProcessConfigUpdateMsgVal, ConfigSeqVal, ProcessConfigUpdateMsgErr 141 func (mcs *ConsenterSupport) ProcessConfigUpdateMsg(env *cb.Envelope) (config *cb.Envelope, configSeq uint64, err error) { 142 return mcs.ProcessConfigUpdateMsgVal, mcs.ConfigSeqVal, mcs.ProcessConfigUpdateMsgErr 143 }