github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/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 ) 30 31 // MockGossipServiceAdapter mocking structure for gossip service, used to initialize 32 // the blocks providers implementation and asserts the number 33 // of function calls used. 34 type MockGossipServiceAdapter struct { 35 AddPayloadsCnt int32 36 37 GossipCallsCnt int32 38 } 39 40 // PeersOfChannel returns the slice with peers participating in given channel 41 func (*MockGossipServiceAdapter) PeersOfChannel(gossip_common.ChainID) []discovery.NetworkMember { 42 return []discovery.NetworkMember{} 43 } 44 45 // AddPayload adds gossip payload to the local state transfer buffer 46 func (mock *MockGossipServiceAdapter) AddPayload(chainID string, payload *gossip_proto.Payload) error { 47 atomic.AddInt32(&mock.AddPayloadsCnt, 1) 48 return nil 49 } 50 51 // Gossip message to the all peers 52 func (mock *MockGossipServiceAdapter) Gossip(msg *gossip_proto.GossipMessage) { 53 atomic.AddInt32(&mock.GossipCallsCnt, 1) 54 } 55 56 // MockBlocksDeliverer mocking structure of BlocksDeliverer interface to initialize 57 // the blocks provider implementation 58 type MockBlocksDeliverer struct { 59 Pos uint64 60 61 RecvCnt int32 62 63 MockRecv func(mock *MockBlocksDeliverer) (*orderer.DeliverResponse, error) 64 } 65 66 // Recv gets responses from the ordering service, currently mocked to return 67 // only one response with empty block. 68 func (mock *MockBlocksDeliverer) Recv() (*orderer.DeliverResponse, error) { 69 atomic.AddInt32(&mock.RecvCnt, 1) 70 return mock.MockRecv(mock) 71 } 72 73 // MockRecv mock for the Recv function 74 func MockRecv(mock *MockBlocksDeliverer) (*orderer.DeliverResponse, error) { 75 pos := mock.Pos 76 77 // Advance position for the next call 78 mock.Pos++ 79 return &orderer.DeliverResponse{ 80 Type: &orderer.DeliverResponse_Block{ 81 Block: &common.Block{ 82 Header: &common.BlockHeader{ 83 Number: pos, 84 DataHash: []byte{}, 85 PreviousHash: []byte{}, 86 }, 87 Data: &common.BlockData{ 88 Data: [][]byte{}, 89 }, 90 }}, 91 }, nil 92 } 93 94 // Send sends the envelope with request for the blocks for ordering service 95 // currently mocked and not doing anything 96 func (mock *MockBlocksDeliverer) Send(env *common.Envelope) error { 97 payload, _ := utils.GetPayload(env) 98 seekInfo := &orderer.SeekInfo{} 99 100 proto.Unmarshal(payload.Data, seekInfo) 101 102 // Read starting position 103 switch t := seekInfo.Start.Type.(type) { 104 case *orderer.SeekPosition_Oldest: 105 { 106 mock.Pos = 0 107 } 108 case *orderer.SeekPosition_Specified: 109 { 110 mock.Pos = t.Specified.Number 111 } 112 } 113 return nil 114 } 115 116 // MockLedgerInfo mocking implementation of LedgerInfo interface, needed 117 // for test initialization purposes 118 type MockLedgerInfo struct { 119 Height uint64 120 } 121 122 // LedgerHeight returns mocked value to the ledger height 123 func (li *MockLedgerInfo) LedgerHeight() (uint64, error) { 124 return li.Height, nil 125 }