github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/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 Pos uint64 73 grpc.ClientStream 74 RecvCnt int32 75 MockRecv func(mock *MockBlocksDeliverer) (*orderer.DeliverResponse, error) 76 } 77 78 // Recv gets responses from the ordering service, currently mocked to return 79 // only one response with empty block. 80 func (mock *MockBlocksDeliverer) Recv() (*orderer.DeliverResponse, error) { 81 atomic.AddInt32(&mock.RecvCnt, 1) 82 return mock.MockRecv(mock) 83 } 84 85 // MockRecv mock for the Recv function 86 func MockRecv(mock *MockBlocksDeliverer) (*orderer.DeliverResponse, error) { 87 pos := mock.Pos 88 89 // Advance position for the next call 90 mock.Pos++ 91 return &orderer.DeliverResponse{ 92 Type: &orderer.DeliverResponse_Block{ 93 Block: &common.Block{ 94 Header: &common.BlockHeader{ 95 Number: pos, 96 DataHash: []byte{}, 97 PreviousHash: []byte{}, 98 }, 99 Data: &common.BlockData{ 100 Data: [][]byte{}, 101 }, 102 }}, 103 }, nil 104 } 105 106 // Send sends the envelope with request for the blocks for ordering service 107 // currently mocked and not doing anything 108 func (mock *MockBlocksDeliverer) Send(env *common.Envelope) error { 109 payload, _ := utils.GetPayload(env) 110 seekInfo := &orderer.SeekInfo{} 111 112 proto.Unmarshal(payload.Data, seekInfo) 113 114 // Read starting position 115 switch t := seekInfo.Start.Type.(type) { 116 case *orderer.SeekPosition_Oldest: 117 mock.Pos = 0 118 case *orderer.SeekPosition_Specified: 119 mock.Pos = t.Specified.Number 120 } 121 return nil 122 } 123 124 func (mock *MockBlocksDeliverer) Close() {} 125 126 // MockLedgerInfo mocking implementation of LedgerInfo interface, needed 127 // for test initialization purposes 128 type MockLedgerInfo struct { 129 Height uint64 130 } 131 132 // LedgerHeight returns mocked value to the ledger height 133 func (li *MockLedgerInfo) LedgerHeight() (uint64, error) { 134 return atomic.LoadUint64(&li.Height), nil 135 }