github.com/true-sqn/fabric@v2.1.1+incompatible/orderer/mocks/common/multichannel/multichannel.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  	cb "github.com/hyperledger/fabric-protos-go/common"
    11  	"github.com/hyperledger/fabric/common/channelconfig"
    12  	"github.com/hyperledger/fabric/orderer/common/blockcutter"
    13  	"github.com/hyperledger/fabric/orderer/common/msgprocessor"
    14  	mockblockcutter "github.com/hyperledger/fabric/orderer/mocks/common/blockcutter"
    15  	"github.com/hyperledger/fabric/protoutil"
    16  )
    17  
    18  // ConsenterSupport is used to mock the multichannel.ConsenterSupport interface
    19  // Whenever a block is written, it writes to the Batches channel to allow for synchronization
    20  type ConsenterSupport struct {
    21  	// SharedConfigVal is the value returned by SharedConfig()
    22  	SharedConfigVal channelconfig.Orderer
    23  
    24  	// ChannelConfigVal is the value returned by ChannelConfig()
    25  	ChannelConfigVal channelconfig.Channel
    26  
    27  	// BlockCutterVal is the value returned by BlockCutter()
    28  	BlockCutterVal *mockblockcutter.Receiver
    29  
    30  	// BlockByIndex maps block numbers to retrieved values of these blocks
    31  	BlockByIndex map[uint64]*cb.Block
    32  
    33  	// Blocks is the channel where WriteBlock writes the most recently created block,
    34  	Blocks chan *cb.Block
    35  
    36  	// ChannelIDVal is the value returned by ChannelID()
    37  	ChannelIDVal string
    38  
    39  	// HeightVal is the value returned by Height()
    40  	HeightVal uint64
    41  
    42  	// NextBlockVal stores the block created by the most recent CreateNextBlock() call
    43  	NextBlockVal *cb.Block
    44  
    45  	// ClassifyMsgVal is returned by ClassifyMsg
    46  	ClassifyMsgVal msgprocessor.Classification
    47  
    48  	// ConfigSeqVal is returned as the configSeq for Process*Msg
    49  	ConfigSeqVal uint64
    50  
    51  	// ProcessNormalMsgErr is returned as the error for ProcessNormalMsg
    52  	ProcessNormalMsgErr error
    53  
    54  	// ProcessConfigUpdateMsgVal is returned as the error for ProcessConfigUpdateMsg
    55  	ProcessConfigUpdateMsgVal *cb.Envelope
    56  
    57  	// ProcessConfigUpdateMsgErr is returned as the error for ProcessConfigUpdateMsg
    58  	ProcessConfigUpdateMsgErr error
    59  
    60  	// ProcessConfigMsgVal is returned as the error for ProcessConfigMsg
    61  	ProcessConfigMsgVal *cb.Envelope
    62  
    63  	// ProcessConfigMsgErr is returned by ProcessConfigMsg
    64  	ProcessConfigMsgErr error
    65  
    66  	// SequenceVal is returned by Sequence
    67  	SequenceVal uint64
    68  
    69  	// BlockVerificationErr is returned by VerifyBlockSignature
    70  	BlockVerificationErr error
    71  }
    72  
    73  // Block returns the block with the given number or nil if not found
    74  func (mcs *ConsenterSupport) Block(number uint64) *cb.Block {
    75  	return mcs.BlockByIndex[number]
    76  }
    77  
    78  // BlockCutter returns BlockCutterVal
    79  func (mcs *ConsenterSupport) BlockCutter() blockcutter.Receiver {
    80  	return mcs.BlockCutterVal
    81  }
    82  
    83  // SharedConfig returns SharedConfigVal
    84  func (mcs *ConsenterSupport) SharedConfig() channelconfig.Orderer {
    85  	return mcs.SharedConfigVal
    86  }
    87  
    88  // ChannelConfig returns ChannelConfigVal
    89  func (mcs *ConsenterSupport) ChannelConfig() channelconfig.Channel {
    90  	return mcs.ChannelConfigVal
    91  }
    92  
    93  // CreateNextBlock creates a simple block structure with the given data
    94  func (mcs *ConsenterSupport) CreateNextBlock(data []*cb.Envelope) *cb.Block {
    95  	block := protoutil.NewBlock(0, nil)
    96  	mtxs := make([][]byte, len(data))
    97  	for i := range data {
    98  		mtxs[i] = protoutil.MarshalOrPanic(data[i])
    99  	}
   100  	block.Data = &cb.BlockData{Data: mtxs}
   101  	mcs.NextBlockVal = block
   102  	return block
   103  }
   104  
   105  // WriteBlock writes data to the Blocks channel
   106  func (mcs *ConsenterSupport) WriteBlock(block *cb.Block, encodedMetadataValue []byte) {
   107  	if encodedMetadataValue != nil {
   108  		block.Metadata.Metadata[cb.BlockMetadataIndex_ORDERER] = protoutil.MarshalOrPanic(&cb.Metadata{Value: encodedMetadataValue})
   109  	}
   110  	mcs.Append(block)
   111  }
   112  
   113  // WriteConfigBlock calls WriteBlock
   114  func (mcs *ConsenterSupport) WriteConfigBlock(block *cb.Block, encodedMetadataValue []byte) {
   115  	mcs.WriteBlock(block, encodedMetadataValue)
   116  }
   117  
   118  // ChannelID returns the channel ID this specific consenter instance is associated with
   119  func (mcs *ConsenterSupport) ChannelID() string {
   120  	return mcs.ChannelIDVal
   121  }
   122  
   123  // Height returns the number of blocks of the chain this specific consenter instance is associated with
   124  func (mcs *ConsenterSupport) Height() uint64 {
   125  	return mcs.HeightVal
   126  }
   127  
   128  // Sign returns the bytes passed in
   129  func (mcs *ConsenterSupport) Sign(message []byte) ([]byte, error) {
   130  	return message, nil
   131  }
   132  
   133  // Serialize returns bytes
   134  func (mcs *ConsenterSupport) Serialize() ([]byte, error) {
   135  	return []byte("creator"), nil
   136  }
   137  
   138  // NewSignatureHeader returns an empty signature header
   139  func (mcs *ConsenterSupport) NewSignatureHeader() (*cb.SignatureHeader, error) {
   140  	return &cb.SignatureHeader{}, nil
   141  }
   142  
   143  // ClassifyMsg returns ClassifyMsgVal, ClassifyMsgErr
   144  func (mcs *ConsenterSupport) ClassifyMsg(chdr *cb.ChannelHeader) msgprocessor.Classification {
   145  	return mcs.ClassifyMsgVal
   146  }
   147  
   148  // ProcessNormalMsg returns ConfigSeqVal, ProcessNormalMsgErr
   149  func (mcs *ConsenterSupport) ProcessNormalMsg(env *cb.Envelope) (configSeq uint64, err error) {
   150  	return mcs.ConfigSeqVal, mcs.ProcessNormalMsgErr
   151  }
   152  
   153  // ProcessConfigUpdateMsg returns ProcessConfigUpdateMsgVal, ConfigSeqVal, ProcessConfigUpdateMsgErr
   154  func (mcs *ConsenterSupport) ProcessConfigUpdateMsg(env *cb.Envelope) (config *cb.Envelope, configSeq uint64, err error) {
   155  	return mcs.ProcessConfigUpdateMsgVal, mcs.ConfigSeqVal, mcs.ProcessConfigUpdateMsgErr
   156  }
   157  
   158  // ProcessConfigMsg returns ProcessConfigMsgVal, ConfigSeqVal, ProcessConfigMsgErr
   159  func (mcs *ConsenterSupport) ProcessConfigMsg(env *cb.Envelope) (*cb.Envelope, uint64, error) {
   160  	return mcs.ProcessConfigMsgVal, mcs.ConfigSeqVal, mcs.ProcessConfigMsgErr
   161  }
   162  
   163  // Sequence returns SequenceVal
   164  func (mcs *ConsenterSupport) Sequence() uint64 {
   165  	return mcs.SequenceVal
   166  }
   167  
   168  // VerifyBlockSignature verifies a signature of a block
   169  func (mcs *ConsenterSupport) VerifyBlockSignature(_ []*protoutil.SignedData, _ *cb.ConfigEnvelope) error {
   170  	return mcs.BlockVerificationErr
   171  }
   172  
   173  // Append appends a new block to the ledger in its raw form,
   174  // unlike WriteBlock that also mutates its metadata.
   175  func (mcs *ConsenterSupport) Append(block *cb.Block) error {
   176  	mcs.HeightVal++
   177  	mcs.Blocks <- block
   178  	return nil
   179  }