github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/core/deliverservice/mocks/blocksprovider.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 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 mocks
    18  
    19  import (
    20  	"sync/atomic"
    21  
    22  	"github.com/golang/protobuf/proto"
    23  	gossip_common "github.com/hyperledger/fabric/gossip/common"
    24  	"github.com/hyperledger/fabric/gossip/discovery"
    25  	"github.com/hyperledger/fabric/protos/common"
    26  	gossip_proto "github.com/hyperledger/fabric/protos/gossip"
    27  	"github.com/hyperledger/fabric/protos/orderer"
    28  	"github.com/hyperledger/fabric/protos/utils"
    29  	"golang.org/x/net/context"
    30  	"google.golang.org/grpc"
    31  )
    32  
    33  // MockGossipServiceAdapter mocking structure for gossip service, used to initialize
    34  // the blocks providers implementation and asserts the number
    35  // of function calls used.
    36  type MockGossipServiceAdapter struct {
    37  	AddPayloadsCnt int32
    38  
    39  	GossipBlockDisseminations chan uint64
    40  }
    41  
    42  type MockAtomicBroadcastClient struct {
    43  	BD *MockBlocksDeliverer
    44  }
    45  
    46  func (mabc *MockAtomicBroadcastClient) Broadcast(ctx context.Context, opts ...grpc.CallOption) (orderer.AtomicBroadcast_BroadcastClient, error) {
    47  	panic("Should not be used")
    48  }
    49  func (mabc *MockAtomicBroadcastClient) Deliver(ctx context.Context, opts ...grpc.CallOption) (orderer.AtomicBroadcast_DeliverClient, error) {
    50  	return mabc.BD, nil
    51  }
    52  
    53  // PeersOfChannel returns the slice with peers participating in given channel
    54  func (*MockGossipServiceAdapter) PeersOfChannel(gossip_common.ChainID) []discovery.NetworkMember {
    55  	return []discovery.NetworkMember{}
    56  }
    57  
    58  // AddPayload adds gossip payload to the local state transfer buffer
    59  func (mock *MockGossipServiceAdapter) AddPayload(chainID string, payload *gossip_proto.Payload) error {
    60  	atomic.AddInt32(&mock.AddPayloadsCnt, 1)
    61  	return nil
    62  }
    63  
    64  // Gossip message to the all peers
    65  func (mock *MockGossipServiceAdapter) Gossip(msg *gossip_proto.GossipMessage) {
    66  	mock.GossipBlockDisseminations <- msg.GetDataMsg().Payload.SeqNum
    67  }
    68  
    69  // MockBlocksDeliverer mocking structure of BlocksDeliverer interface to initialize
    70  // the blocks provider implementation
    71  type MockBlocksDeliverer struct {
    72  	DisconnectCalled chan struct{}
    73  	CloseCalled      chan struct{}
    74  	Pos              uint64
    75  	grpc.ClientStream
    76  	RecvCnt  int32
    77  	MockRecv func(mock *MockBlocksDeliverer) (*orderer.DeliverResponse, error)
    78  }
    79  
    80  // Recv gets responses from the ordering service, currently mocked to return
    81  // only one response with empty block.
    82  func (mock *MockBlocksDeliverer) Recv() (*orderer.DeliverResponse, error) {
    83  	atomic.AddInt32(&mock.RecvCnt, 1)
    84  	return mock.MockRecv(mock)
    85  }
    86  
    87  // MockRecv mock for the Recv function
    88  func MockRecv(mock *MockBlocksDeliverer) (*orderer.DeliverResponse, error) {
    89  	pos := mock.Pos
    90  
    91  	// Advance position for the next call
    92  	mock.Pos++
    93  	return &orderer.DeliverResponse{
    94  		Type: &orderer.DeliverResponse_Block{
    95  			Block: &common.Block{
    96  				Header: &common.BlockHeader{
    97  					Number:       pos,
    98  					DataHash:     []byte{},
    99  					PreviousHash: []byte{},
   100  				},
   101  				Data: &common.BlockData{
   102  					Data: [][]byte{},
   103  				},
   104  			}},
   105  	}, nil
   106  }
   107  
   108  // Send sends the envelope with request for the blocks for ordering service
   109  // currently mocked and not doing anything
   110  func (mock *MockBlocksDeliverer) Send(env *common.Envelope) error {
   111  	payload, _ := utils.GetPayload(env)
   112  	seekInfo := &orderer.SeekInfo{}
   113  
   114  	proto.Unmarshal(payload.Data, seekInfo)
   115  
   116  	// Read starting position
   117  	switch t := seekInfo.Start.Type.(type) {
   118  	case *orderer.SeekPosition_Oldest:
   119  		mock.Pos = 0
   120  	case *orderer.SeekPosition_Specified:
   121  		mock.Pos = t.Specified.Number
   122  	}
   123  	return nil
   124  }
   125  
   126  func (mock *MockBlocksDeliverer) Disconnect() {
   127  	mock.DisconnectCalled <- struct{}{}
   128  }
   129  
   130  func (mock *MockBlocksDeliverer) Close() {
   131  	if mock.CloseCalled == nil {
   132  		return
   133  	}
   134  	mock.CloseCalled <- struct{}{}
   135  }
   136  
   137  // MockLedgerInfo mocking implementation of LedgerInfo interface, needed
   138  // for test initialization purposes
   139  type MockLedgerInfo struct {
   140  	Height uint64
   141  }
   142  
   143  // LedgerHeight returns mocked value to the ledger height
   144  func (li *MockLedgerInfo) LedgerHeight() (uint64, error) {
   145  	return atomic.LoadUint64(&li.Height), nil
   146  }