github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/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  	// Batches is the channel which WriteBlock writes data to
    43  	Batches chan []*cb.Envelope
    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  	// WriteBlockVal stores the block created by the most recent WriteBlock() call
    55  	WriteBlockVal *cb.Block
    56  }
    57  
    58  // BlockCutter returns BlockCutterVal
    59  func (mcs *ConsenterSupport) BlockCutter() blockcutter.Receiver {
    60  	return mcs.BlockCutterVal
    61  }
    62  
    63  // SharedConfig returns SharedConfigVal
    64  func (mcs *ConsenterSupport) SharedConfig() config.Orderer {
    65  	return mcs.SharedConfigVal
    66  }
    67  
    68  // CreateNextBlock creates a simple block structure with the given data
    69  func (mcs *ConsenterSupport) CreateNextBlock(data []*cb.Envelope) *cb.Block {
    70  	block := cb.NewBlock(0, nil)
    71  	mtxs := make([][]byte, len(data))
    72  	for i := range data {
    73  		mtxs[i] = utils.MarshalOrPanic(data[i])
    74  	}
    75  	block.Data = &cb.BlockData{Data: mtxs}
    76  	mcs.NextBlockVal = block
    77  	return block
    78  }
    79  
    80  // WriteBlock writes data to the Batches channel
    81  // Note that _committers is ignored by this mock implementation
    82  func (mcs *ConsenterSupport) WriteBlock(block *cb.Block, _committers []filter.Committer, encodedMetadataValue []byte) *cb.Block {
    83  	logger.Debugf("mockWriter: attempting to write batch")
    84  	umtxs := make([]*cb.Envelope, len(block.Data.Data))
    85  	for i := range block.Data.Data {
    86  		umtxs[i] = utils.UnmarshalEnvelopeOrPanic(block.Data.Data[i])
    87  	}
    88  	mcs.Batches <- umtxs
    89  	mcs.HeightVal++
    90  	if encodedMetadataValue != nil {
    91  		block.Metadata.Metadata[cb.BlockMetadataIndex_ORDERER] = utils.MarshalOrPanic(&cb.Metadata{Value: encodedMetadataValue})
    92  	}
    93  	mcs.WriteBlockVal = block
    94  	return block
    95  }
    96  
    97  // ChainID returns the chain ID this specific consenter instance is associated with
    98  func (mcs *ConsenterSupport) ChainID() string {
    99  	return mcs.ChainIDVal
   100  }
   101  
   102  // Height returns the number of blocks of the chain this specific consenter instance is associated with
   103  func (mcs *ConsenterSupport) Height() uint64 {
   104  	return mcs.HeightVal
   105  }
   106  
   107  // Sign returns the bytes passed in
   108  func (mcs *ConsenterSupport) Sign(message []byte) ([]byte, error) {
   109  	return message, nil
   110  }
   111  
   112  // NewSignatureHeader returns an empty signature header
   113  func (mcs *ConsenterSupport) NewSignatureHeader() (*cb.SignatureHeader, error) {
   114  	return &cb.SignatureHeader{}, nil
   115  }