github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/orderer/mocks/multichain/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 multichain 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/filter" 24 mockblockcutter "github.com/hyperledger/fabric/orderer/mocks/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/multichain") 32 33 // ConsenterSupport is used to mock the multichain.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 55 // BlockCutter returns BlockCutterVal 56 func (mcs *ConsenterSupport) BlockCutter() blockcutter.Receiver { 57 return mcs.BlockCutterVal 58 } 59 60 // SharedConfig returns SharedConfigVal 61 func (mcs *ConsenterSupport) SharedConfig() config.Orderer { 62 return mcs.SharedConfigVal 63 } 64 65 // CreateNextBlock creates a simple block structure with the given data 66 func (mcs *ConsenterSupport) CreateNextBlock(data []*cb.Envelope) *cb.Block { 67 block := cb.NewBlock(0, nil) 68 mtxs := make([][]byte, len(data)) 69 for i := range data { 70 mtxs[i] = utils.MarshalOrPanic(data[i]) 71 } 72 block.Data = &cb.BlockData{Data: mtxs} 73 mcs.NextBlockVal = block 74 return block 75 } 76 77 // WriteBlock writes data to the Blocks channel 78 // Note that _committers is ignored by this mock implementation 79 func (mcs *ConsenterSupport) WriteBlock(block *cb.Block, _committers []filter.Committer, encodedMetadataValue []byte) *cb.Block { 80 if encodedMetadataValue != nil { 81 block.Metadata.Metadata[cb.BlockMetadataIndex_ORDERER] = utils.MarshalOrPanic(&cb.Metadata{Value: encodedMetadataValue}) 82 } 83 mcs.HeightVal++ 84 mcs.Blocks <- block 85 return block 86 } 87 88 // ChainID returns the chain ID this specific consenter instance is associated with 89 func (mcs *ConsenterSupport) ChainID() string { 90 return mcs.ChainIDVal 91 } 92 93 // Height returns the number of blocks of the chain this specific consenter instance is associated with 94 func (mcs *ConsenterSupport) Height() uint64 { 95 return mcs.HeightVal 96 } 97 98 // Sign returns the bytes passed in 99 func (mcs *ConsenterSupport) Sign(message []byte) ([]byte, error) { 100 return message, nil 101 } 102 103 // NewSignatureHeader returns an empty signature header 104 func (mcs *ConsenterSupport) NewSignatureHeader() (*cb.SignatureHeader, error) { 105 return &cb.SignatureHeader{}, nil 106 }