|
| 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 peer |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + pb "github.com/hyperledger/fabric/protos/peer" |
| 23 | +) |
| 24 | + |
| 25 | +//MockResponseSet is used for processing CC to Peer comm |
| 26 | +//such as GET/PUT/DEL state. The MockResponse contains the |
| 27 | +//response to be returned for each input received.from the |
| 28 | +//CC. Every stub call will generate a response |
| 29 | +type MockResponseSet struct { |
| 30 | + //DoneFunc is invoked when all I/O is done for this |
| 31 | + //response set |
| 32 | + DoneFunc func(int, error) |
| 33 | + |
| 34 | + //ErrorFunc is invoked at any step when the input does not |
| 35 | + //match the received message |
| 36 | + ErrorFunc func(int, error) |
| 37 | + |
| 38 | + //Responses contained the expected received message (optional) |
| 39 | + //and response to send (optional) |
| 40 | + Responses []*MockResponse |
| 41 | +} |
| 42 | + |
| 43 | +//MockResponse contains the expected received message (optional) |
| 44 | +//and response to send (optional) |
| 45 | +type MockResponse struct { |
| 46 | + RecvMsg *pb.ChaincodeMessage |
| 47 | + RespMsg *pb.ChaincodeMessage |
| 48 | +} |
| 49 | + |
| 50 | +// MockCCComm implements the mock communication between chaincode and peer |
| 51 | +// We'd need two MockCCComm for communication. The receiver and sender will |
| 52 | +// be switched between the two. |
| 53 | +type MockCCComm struct { |
| 54 | + name string |
| 55 | + bailOnError bool |
| 56 | + sendOnRecv *pb.ChaincodeMessage |
| 57 | + recvStream chan *pb.ChaincodeMessage |
| 58 | + sendStream chan *pb.ChaincodeMessage |
| 59 | + respIndex int |
| 60 | + respSet *MockResponseSet |
| 61 | +} |
| 62 | + |
| 63 | +//Send sends a message |
| 64 | +func (s *MockCCComm) Send(msg *pb.ChaincodeMessage) error { |
| 65 | + s.sendStream <- msg |
| 66 | + return nil |
| 67 | +} |
| 68 | + |
| 69 | +//Recv receives a message |
| 70 | +func (s *MockCCComm) Recv() (*pb.ChaincodeMessage, error) { |
| 71 | + msg := <-s.recvStream |
| 72 | + return msg, nil |
| 73 | +} |
| 74 | + |
| 75 | +//CloseSend closes send |
| 76 | +func (s *MockCCComm) CloseSend() error { |
| 77 | + return nil |
| 78 | +} |
| 79 | + |
| 80 | +//GetRecvStream returns the recvStream |
| 81 | +func (s *MockCCComm) GetRecvStream() chan *pb.ChaincodeMessage { |
| 82 | + return s.recvStream |
| 83 | +} |
| 84 | + |
| 85 | +//GetSendStream returns the sendStream |
| 86 | +func (s *MockCCComm) GetSendStream() chan *pb.ChaincodeMessage { |
| 87 | + return s.sendStream |
| 88 | +} |
| 89 | + |
| 90 | +//Quit closes the channels...this will also close chaincode side |
| 91 | +func (s *MockCCComm) Quit() { |
| 92 | + if s.recvStream != nil { |
| 93 | + close(s.recvStream) |
| 94 | + s.recvStream = nil |
| 95 | + } |
| 96 | + |
| 97 | + if s.sendStream != nil { |
| 98 | + close(s.sendStream) |
| 99 | + s.sendStream = nil |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +//SetBailOnError will cause Run to return on any error |
| 104 | +func (s *MockCCComm) SetBailOnError(b bool) { |
| 105 | + s.bailOnError = b |
| 106 | +} |
| 107 | + |
| 108 | +//SetResponses sets responses for an Init or Invoke |
| 109 | +func (s *MockCCComm) SetResponses(respSet *MockResponseSet) { |
| 110 | + s.respSet = respSet |
| 111 | + s.respIndex = 0 |
| 112 | +} |
| 113 | + |
| 114 | +//Run receives and sends indefinitely |
| 115 | +func (s *MockCCComm) Run() error { |
| 116 | + for { |
| 117 | + msg, err := s.Recv() |
| 118 | + |
| 119 | + if err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + |
| 123 | + if err = s.respond(msg); err != nil { |
| 124 | + if s.bailOnError { |
| 125 | + return err |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func (s *MockCCComm) respond(msg *pb.ChaincodeMessage) error { |
| 132 | + var err error |
| 133 | + if s.respIndex < len(s.respSet.Responses) { |
| 134 | + mockResp := s.respSet.Responses[s.respIndex] |
| 135 | + if mockResp.RecvMsg != nil { |
| 136 | + if msg.Type != mockResp.RecvMsg.Type { |
| 137 | + if s.respSet.ErrorFunc != nil { |
| 138 | + s.respSet.ErrorFunc(s.respIndex, fmt.Errorf("Invalid message expected %d received %d", int32(mockResp.RecvMsg.Type), int32(msg.Type))) |
| 139 | + s.respIndex = s.respIndex + 1 |
| 140 | + return nil |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + if mockResp.RespMsg != nil { |
| 146 | + err = s.Send(mockResp.RespMsg) |
| 147 | + } |
| 148 | + |
| 149 | + s.respIndex = s.respIndex + 1 |
| 150 | + |
| 151 | + if s.respIndex == len(s.respSet.Responses) { |
| 152 | + if s.respSet.DoneFunc != nil { |
| 153 | + s.respSet.DoneFunc(s.respIndex, nil) |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + return err |
| 158 | +} |
0 commit comments