Repository navigation
Expand file tree
/
Copy pathhandler.py
More file actions
336 lines (289 loc) · 14.1 KB
/
Copy pathhandler.py
File metadata and controls
336 lines (289 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# SPDX-License-Identifier: Apache-2.0
import datetime
from typing import AsyncIterable
import asyncio
import grpc
from fabric_protos.peer import chaincode_shim_pb2 as ccshim_pb2
from fabric_protos.peer import chaincode_pb2 as cc_pb2
from fabric_protos.peer import proposal_response_pb2 as pr_pb
from fabric_chaincode_python.fabric_shim.stub import ChaincodeStub
from fabric_chaincode_python.fabric_shim.msg_queue_handler import MsgQueueHandler, QueueMessage
from fabric_chaincode_python.fabric_shim.response import new_error_msg, ResponseCode
from fabric_chaincode_python.fabric_shim.utils import generate_logging_prefix
from fabric_chaincode_python.fabric_shim.logging import LOGGER
from fabric_chaincode_python.fabric_shim.interfaces import Chaincode
class STATES:
CREATED = "created" # start state
ESTABLISHED = "established" # connection established
READY = "ready" # ready for requests
MIN_UNICODE_RUNE_VALUE = '\u0000' # U + 0000
MAX_UNICODE_RUNE_VALUE = '\u0010FFFF' # U+10FFFF - maximum (and unallocated) code point
COMPOSITEKEY_NS = '\x00'
EMPTY_KEY_SUBSTITUTE = '\x01'
class Handler:
def __init__(self, cc_id: str, cc: Chaincode) -> None:
self.chaincode_id = cc_pb2.ChaincodeID()
self.chaincode_id.name = cc_id
self.chaincode = cc
self.msg_queue_handler = None
self.context = None
self.state = STATES.CREATED
self._pending_tasks = set()
def _track_task(self, coro):
"""Schedule message handling while surfacing task failures in logs."""
task = asyncio.create_task(coro)
self._pending_tasks.add(task)
def _done_callback(done_task):
self._pending_tasks.discard(done_task)
try:
exc = done_task.exception()
except asyncio.CancelledError:
return
if exc is not None:
LOGGER.exception('Unhandled exception while processing peer message', exc_info=exc)
task.add_done_callback(_done_callback)
async def handle_stub_interaction(self, msg, action="Invoke"):
"""handle_message calls the Init | Invoke function of the associated chaincode."""
# Get the function and args from Payload
cc_input = cc_pb2.ChaincodeInput()
cc_input.ParseFromString(msg.payload)
stub = ChaincodeStub(self, msg.channel_id, msg.txid, cc_input, msg.proposal)
# Normalize action and call appropriate chaincode method
action_normalized = (action or "").lower()
if action_normalized == 'init':
method = 'Init'
resp: pr_pb.Response = await self.chaincode.init(stub)
else:
method = 'Invoke'
resp: pr_pb.Response = await self.chaincode.invoke(stub)
# check that a response object has been returned otherwise assume an error.
if not resp or not resp.status:
err_msg = '%s Calling chaincode %s() has not called success or error.' \
% (generate_logging_prefix(msg.channel_id, msg.txid), method)
LOGGER.info(err_msg)
resp = pr_pb.Response(
status=ResponseCode.ERROR,
message=err_msg
)
LOGGER.info('%s Calling chaincode %s(), response status: %s'
% (generate_logging_prefix(msg.channel_id, msg.txid), method, resp.status))
if resp.status >= ResponseCode.ERROR:
err_msg = '%s Calling chaincode %s() returned error response [%s]. Sending COMPLETED message back to peer' \
% (generate_logging_prefix(msg.channel_id, msg.txid), method, resp.message)
LOGGER.info(err_msg)
next_state_msg = ccshim_pb2.ChaincodeMessage(
type=ccshim_pb2.ChaincodeMessage.COMPLETED,
payload=resp.SerializeToString(),
txid=msg.txid,
channel_id=msg.channel_id
)
else:
LOGGER.info('%s Calling chaincode %s() succeeded. Sending COMPLETED message back to peer'
% (generate_logging_prefix(msg.channel_id, msg.txid), method))
next_state_msg = ccshim_pb2.ChaincodeMessage(
type=ccshim_pb2.ChaincodeMessage.COMPLETED,
payload=resp.SerializeToString(),
txid=msg.txid,
channel_id=msg.channel_id
)
await self.context.write(next_state_msg)
async def handle_message_ready(self, msg):
"""handle_message_ready handles messages received from the peer when the handler is in the "ready" state."""
if msg.type == ccshim_pb2.ChaincodeMessage.RESPONSE or msg.type == ccshim_pb2.ChaincodeMessage.ERROR:
LOGGER.info("+++ ERROR or RESPONSE +++")
await self.msg_queue_handler.handle_msg_response(msg)
elif msg.type == ccshim_pb2.ChaincodeMessage.INIT:
LOGGER.info("+++ call INIT +++")
await self.handle_stub_interaction(msg, "Init")
return
elif msg.type == ccshim_pb2.ChaincodeMessage.TRANSACTION:
LOGGER.info("+++ call INVOKE +++")
await self.handle_stub_interaction(msg, "Invoke")
return
else:
await self.context.write(new_error_msg(msg, self.state))
async def handle_message_established(self, msg):
"""
handle_message_established handles messages received from the peer when the handler is in the "established" state.
"""
if msg.type != ccshim_pb2.ChaincodeMessage.READY:
LOGGER.error(f'Chaincode is in "established" state, can only process messages of type "ready", '
f'but received "{msg.type}"')
await self.context.write(new_error_msg(msg, self.state))
else:
LOGGER.info('Successfully established communication with peer node. State transferred to "ready"')
self.state = STATES.READY
async def handle_message_created(self, msg):
"""handle_message_created handles messages received from the peer when the handler is in the "created" state."""
if msg.type != ccshim_pb2.ChaincodeMessage.REGISTERED:
LOGGER.error(f'Chaincode is in "created" state, can only process messages of type "registered", '
f'but received "{msg.type}"')
await self.context.write(new_error_msg(msg, self.state))
else:
LOGGER.info('Successfully registered with peer node. State transferred to "established"')
self.state = STATES.ESTABLISHED
async def handle_message(self, msg: ccshim_pb2.ChaincodeMessage):
"""handle_message message handles loop for shim side of chaincode/peer stream."""
LOGGER.warning('-->> Look out!')
# TODO: ?
if msg.type == ccshim_pb2.ChaincodeMessage.KEEPALIVE:
LOGGER.info('-| KEEPALIVE')
return
if self.state == STATES.READY:
await self.handle_message_ready(msg)
elif self.state == STATES.ESTABLISHED:
await self.handle_message_established(msg)
elif self.state == STATES.CREATED:
await self.handle_message_created(msg)
else:
await self.context.write(new_error_msg(msg, self.state))
async def chat_with_peer(self, stream: AsyncIterable[ccshim_pb2.ChaincodeMessage], context: grpc.aio.ServicerContext):
"""chat stream for peer-chaincode interactions post connection"""
self.state = STATES.CREATED
self.context = context
self.msg_queue_handler = MsgQueueHandler(self)
# Send the ChaincodeID during register.
cm = ccshim_pb2.ChaincodeMessage(
type=ccshim_pb2.ChaincodeMessage.REGISTER, payload=self.chaincode_id.SerializeToString())
cm.timestamp.FromDatetime(datetime.datetime.now())
# Register on the stream
await self.context.write(cm)
async for receive_message in stream:
LOGGER.info('Received message')
if receive_message is None:
err_str = "received nil message, ending chaincode stream"
LOGGER.error(err_str)
return ccshim_pb2.ChaincodeMessage(
type=ccshim_pb2.ChaincodeMessage.ERROR, payload=err_str.encode(encoding='utf-8'))
else:
# Keep handling asynchronous so the stream can continue receiving
# response frames needed by in-flight request futures.
self._track_task(self.handle_message(receive_message))
LOGGER.info(f'->>>> proposal {receive_message.proposal}')
LOGGER.info(f'->>>> payload {receive_message.payload}')
LOGGER.info(f'->>>> channel ID {receive_message.channel_id}')
LOGGER.info(f'->>>> Tx ID {receive_message.txid}')
if self._pending_tasks:
await asyncio.gather(*self._pending_tasks, return_exceptions=True)
async def handle_get_state(self, collection, key, channel_id, tx_id):
msg_pb = ccshim_pb2.GetState()
msg_pb.key = key
msg_pb.collection = collection
msg = ccshim_pb2.ChaincodeMessage(
type=ccshim_pb2.ChaincodeMessage.GET_STATE,
payload=msg_pb.SerializeToString(),
txid=tx_id,
channel_id=channel_id
)
result = await self.__ask_peer_and_listen(msg, 'GetState')
return result.payload
async def handle_put_state(self, collection, key, value, channel_id, tx_id):
msg_pb = ccshim_pb2.PutState()
msg_pb.key = key
msg_pb.value = value
msg_pb.collection = collection
msg = ccshim_pb2.ChaincodeMessage(
type=ccshim_pb2.ChaincodeMessage.PUT_STATE,
payload=msg_pb.SerializeToString(),
txid=tx_id,
channel_id=channel_id
)
return await self.__ask_peer_and_listen(msg, 'PutState')
async def handle_delete_state(self, collection, key, channel_id, tx_id):
msg_pb = ccshim_pb2.DelState()
msg_pb.key = key
msg_pb.collection = collection
msg = ccshim_pb2.ChaincodeMessage(
type=ccshim_pb2.ChaincodeMessage.DEL_STATE,
payload=msg_pb.SerializeToString(),
txid=tx_id,
channel_id=channel_id
)
return await self.__ask_peer_and_listen(msg, 'DeleteState')
async def handle_get_state_by_range(self, collection, start_key, end_key,
channel_id, tx_id, metadata=None):
"""Send a GetStateByRange request to the peer.
``metadata`` (if present) is a serialised :class:`QueryMetadata`
message — used for pagination.
"""
msg_pb = ccshim_pb2.GetStateByRange()
# NB: the protobuf field names are camelCase (startKey/endKey), not
# snake_case.
msg_pb.startKey = start_key or ""
msg_pb.endKey = end_key or ""
msg_pb.collection = collection
if metadata is not None:
msg_pb.metadata = metadata
msg = ccshim_pb2.ChaincodeMessage(
type=ccshim_pb2.ChaincodeMessage.GET_STATE_BY_RANGE,
payload=msg_pb.SerializeToString(),
txid=tx_id,
channel_id=channel_id,
)
return await self.__ask_peer_and_listen(msg, 'GetStateByRange')
async def handle_get_query_result(self, collection, query, channel_id, tx_id,
metadata=None):
"""Send a GetQueryResult (CouchDB rich query) request to the peer."""
msg_pb = ccshim_pb2.GetQueryResult()
msg_pb.query = query
msg_pb.collection = collection
if metadata is not None:
msg_pb.metadata = metadata
msg = ccshim_pb2.ChaincodeMessage(
type=ccshim_pb2.ChaincodeMessage.GET_QUERY_RESULT,
payload=msg_pb.SerializeToString(),
txid=tx_id,
channel_id=channel_id,
)
return await self.__ask_peer_and_listen(msg, 'GetQueryResult')
async def handle_get_history_for_key(self, key, channel_id, tx_id):
"""Send a GetHistoryForKey request to the peer."""
msg_pb = ccshim_pb2.GetHistoryForKey()
msg_pb.key = key
msg = ccshim_pb2.ChaincodeMessage(
type=ccshim_pb2.ChaincodeMessage.GET_HISTORY_FOR_KEY,
payload=msg_pb.SerializeToString(),
txid=tx_id,
channel_id=channel_id,
)
return await self.__ask_peer_and_listen(msg, 'GetHistoryForKey')
async def handle_query_state_next(self, query_id, channel_id, tx_id):
"""Fetch the next page of an in-flight paged query."""
msg_pb = ccshim_pb2.QueryStateNext()
msg_pb.id = query_id
msg = ccshim_pb2.ChaincodeMessage(
type=ccshim_pb2.ChaincodeMessage.QUERY_STATE_NEXT,
payload=msg_pb.SerializeToString(),
txid=tx_id,
channel_id=channel_id,
)
return await self.__ask_peer_and_listen(msg, 'QueryStateNext')
async def handle_query_state_close(self, query_id, channel_id, tx_id):
"""Close an in-flight paged query."""
msg_pb = ccshim_pb2.QueryStateClose()
msg_pb.id = query_id
msg = ccshim_pb2.ChaincodeMessage(
type=ccshim_pb2.ChaincodeMessage.QUERY_STATE_CLOSE,
payload=msg_pb.SerializeToString(),
txid=tx_id,
channel_id=channel_id,
)
return await self.__ask_peer_and_listen(msg, 'QueryStateClose')
async def handle_invoke_chaincode(self, chaincode_name, args, channel_id, tx_id):
"""Invoke another chaincode by name."""
ci = cc_pb2.ChaincodeInput()
for a in args:
ci.args.append(a.encode() if isinstance(a, str) else a)
msg = ccshim_pb2.ChaincodeMessage(
type=ccshim_pb2.ChaincodeMessage.INVOKE_CHAINCODE,
payload=ci.SerializeToString(),
txid=tx_id,
channel_id=channel_id,
)
return await self.__ask_peer_and_listen(msg, 'InvokeChaincode')
async def __ask_peer_and_listen(self, msg, action):
loop = asyncio.get_running_loop()
fut = loop.create_future()
message = QueueMessage(msg, action, fut)
await self.msg_queue_handler.queue_msg(message)
return await fut