github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/sdk/python/tests/test_zmq_service.py (about) 1 # Copyright 2018 Intel Corporation 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # ----------------------------------------------------------------------------- 15 16 import unittest 17 18 from sawtooth_sdk.consensus.zmq_service import ZmqService 19 from sawtooth_sdk.messaging.future import Future 20 from sawtooth_sdk.messaging.future import FutureResult 21 from sawtooth_sdk.protobuf import consensus_pb2 22 from sawtooth_sdk.protobuf.validator_pb2 import Message 23 24 25 class TestService(unittest.TestCase): 26 def setUp(self): 27 self.mock_stream = unittest.mock.Mock() 28 self.service = ZmqService( 29 stream=self.mock_stream, 30 timeout=10, 31 name='name', 32 version='version') 33 34 def _make_future(self, message_type, content): 35 fut = Future('test') 36 fut.set_result(FutureResult( 37 message_type=message_type, 38 content=content)) 39 return fut 40 41 def test_send_to(self): 42 self.mock_stream.send.return_value = self._make_future( 43 message_type=Message.CONSENSUS_SEND_TO_RESPONSE, 44 content=consensus_pb2.ConsensusSendToResponse( 45 status=consensus_pb2.ConsensusSendToResponse.OK 46 ).SerializeToString()) 47 48 self.service.send_to( 49 peer_id=b'peer_id', 50 message_type='message_type', 51 payload=b'payload') 52 53 self.mock_stream.send.assert_called_with( 54 message_type=Message.CONSENSUS_SEND_TO_REQUEST, 55 content=consensus_pb2.ConsensusSendToRequest( 56 message=consensus_pb2.ConsensusPeerMessage( 57 message_type='message_type', 58 content=b'payload', 59 name='name', 60 version='version'), 61 peer_id=b'peer_id').SerializeToString()) 62 63 def test_broadcast(self): 64 self.mock_stream.send.return_value = self._make_future( 65 message_type=Message.CONSENSUS_BROADCAST_RESPONSE, 66 content=consensus_pb2.ConsensusBroadcastResponse( 67 status=consensus_pb2.ConsensusBroadcastResponse.OK 68 ).SerializeToString()) 69 70 self.service.broadcast( 71 message_type='message_type', 72 payload=b'payload') 73 74 self.mock_stream.send.assert_called_with( 75 message_type=Message.CONSENSUS_BROADCAST_REQUEST, 76 content=consensus_pb2.ConsensusBroadcastRequest( 77 message=consensus_pb2.ConsensusPeerMessage( 78 message_type='message_type', 79 content=b'payload', 80 name='name', 81 version='version')).SerializeToString()) 82 83 def test_initialize_block(self): 84 self.mock_stream.send.return_value = self._make_future( 85 message_type=Message.CONSENSUS_INITIALIZE_BLOCK_RESPONSE, 86 content=consensus_pb2.ConsensusInitializeBlockResponse( 87 status=consensus_pb2.ConsensusInitializeBlockResponse.OK 88 ).SerializeToString()) 89 90 self.service.initialize_block(previous_id=b'test') 91 92 self.mock_stream.send.assert_called_with( 93 message_type=Message.CONSENSUS_INITIALIZE_BLOCK_REQUEST, 94 content=consensus_pb2.ConsensusInitializeBlockRequest( 95 previous_id=b'test').SerializeToString()) 96 97 def test_summarize_block(self): 98 self.mock_stream.send.return_value = self._make_future( 99 message_type=Message.CONSENSUS_SUMMARIZE_BLOCK_RESPONSE, 100 content=consensus_pb2.ConsensusSummarizeBlockResponse( 101 status=consensus_pb2.ConsensusSummarizeBlockResponse.OK, 102 summary=b'summary').SerializeToString()) 103 104 result = self.service.summarize_block() 105 106 self.mock_stream.send.assert_called_with( 107 message_type=Message.CONSENSUS_SUMMARIZE_BLOCK_REQUEST, 108 content=consensus_pb2.ConsensusSummarizeBlockRequest() 109 .SerializeToString()) 110 111 self.assertEqual(result, b'summary') 112 113 def test_finalize_block(self): 114 self.mock_stream.send.return_value = self._make_future( 115 message_type=Message.CONSENSUS_FINALIZE_BLOCK_RESPONSE, 116 content=consensus_pb2.ConsensusFinalizeBlockResponse( 117 status=consensus_pb2.ConsensusFinalizeBlockResponse.OK, 118 block_id=b'block_id').SerializeToString()) 119 120 result = self.service.finalize_block(data=b'test') 121 122 self.mock_stream.send.assert_called_with( 123 message_type=Message.CONSENSUS_FINALIZE_BLOCK_REQUEST, 124 content=consensus_pb2.ConsensusFinalizeBlockRequest( 125 data=b'test').SerializeToString()) 126 127 self.assertEqual(result, b'block_id') 128 129 def test_cancel_block(self): 130 self.mock_stream.send.return_value = self._make_future( 131 message_type=Message.CONSENSUS_CANCEL_BLOCK_RESPONSE, 132 content=consensus_pb2.ConsensusCancelBlockResponse( 133 status=consensus_pb2.ConsensusCancelBlockResponse.OK 134 ).SerializeToString()) 135 136 self.service.cancel_block() 137 138 request = consensus_pb2.ConsensusCancelBlockRequest() 139 140 self.mock_stream.send.assert_called_with( 141 message_type=Message.CONSENSUS_CANCEL_BLOCK_REQUEST, 142 content=request.SerializeToString()) 143 144 def test_check_blocks(self): 145 self.mock_stream.send.return_value = self._make_future( 146 message_type=Message.CONSENSUS_CHECK_BLOCKS_RESPONSE, 147 content=consensus_pb2.ConsensusCheckBlocksResponse( 148 status=consensus_pb2.ConsensusCheckBlocksResponse.OK 149 ).SerializeToString()) 150 151 self.service.check_blocks(priority=[b'test1', b'test2']) 152 153 self.mock_stream.send.assert_called_with( 154 message_type=Message.CONSENSUS_CHECK_BLOCKS_REQUEST, 155 content=consensus_pb2.ConsensusCheckBlocksRequest( 156 block_ids=[b'test1', b'test2']).SerializeToString()) 157 158 def test_commit_block(self): 159 self.mock_stream.send.return_value = self._make_future( 160 message_type=Message.CONSENSUS_COMMIT_BLOCK_RESPONSE, 161 content=consensus_pb2.ConsensusCommitBlockResponse( 162 status=consensus_pb2.ConsensusCommitBlockResponse.OK 163 ).SerializeToString()) 164 165 self.service.commit_block(block_id=b'test') 166 167 self.mock_stream.send.assert_called_with( 168 message_type=Message.CONSENSUS_COMMIT_BLOCK_REQUEST, 169 content=consensus_pb2.ConsensusCommitBlockRequest( 170 block_id=b'test').SerializeToString()) 171 172 def test_ignore_block(self): 173 self.mock_stream.send.return_value = self._make_future( 174 message_type=Message.CONSENSUS_IGNORE_BLOCK_RESPONSE, 175 content=consensus_pb2.ConsensusIgnoreBlockResponse( 176 status=consensus_pb2.ConsensusIgnoreBlockResponse.OK 177 ).SerializeToString()) 178 179 self.service.ignore_block(block_id=b'test') 180 181 self.mock_stream.send.assert_called_with( 182 message_type=Message.CONSENSUS_IGNORE_BLOCK_REQUEST, 183 content=consensus_pb2.ConsensusIgnoreBlockRequest( 184 block_id=b'test').SerializeToString()) 185 186 def test_fail_block(self): 187 self.mock_stream.send.return_value = self._make_future( 188 message_type=Message.CONSENSUS_FAIL_BLOCK_RESPONSE, 189 content=consensus_pb2.ConsensusFailBlockResponse( 190 status=consensus_pb2.ConsensusFailBlockResponse.OK 191 ).SerializeToString()) 192 193 self.service.fail_block(block_id=b'test') 194 195 self.mock_stream.send.assert_called_with( 196 message_type=Message.CONSENSUS_FAIL_BLOCK_REQUEST, 197 content=consensus_pb2.ConsensusFailBlockRequest( 198 block_id=b'test').SerializeToString()) 199 200 def test_get_blocks(self): 201 block_1 = consensus_pb2.ConsensusBlock( 202 block_id=b'block1', 203 previous_id=b'block0', 204 signer_id=b'signer1', 205 block_num=1, 206 payload=b'test1') 207 208 block_2 = consensus_pb2.ConsensusBlock( 209 block_id=b'block2', 210 previous_id=b'block1', 211 signer_id=b'signer2', 212 block_num=2, 213 payload=b'test2') 214 215 self.mock_stream.send.return_value = self._make_future( 216 message_type=Message.CONSENSUS_BLOCKS_GET_RESPONSE, 217 content=consensus_pb2.ConsensusBlocksGetResponse( 218 status=consensus_pb2.ConsensusBlocksGetResponse.OK, 219 blocks=[block_1, block_2]).SerializeToString()) 220 221 blocks = self.service.get_blocks(block_ids=[b'id1', b'id2']) 222 223 self.mock_stream.send.assert_called_with( 224 message_type=Message.CONSENSUS_BLOCKS_GET_REQUEST, 225 content=consensus_pb2.ConsensusBlocksGetRequest( 226 block_ids=[b'id1', b'id2']).SerializeToString()) 227 228 self.assertEqual({ 229 block_id: ( 230 block.previous_id, 231 block.signer_id, 232 block.block_num, 233 block.payload) 234 for block_id, block in blocks.items() 235 }, { 236 b'block1': (b'block0', b'signer1', 1, b'test1'), 237 b'block2': (b'block1', b'signer2', 2, b'test2'), 238 }) 239 240 def test_get_chain_head(self): 241 block = consensus_pb2.ConsensusBlock( 242 block_id=b'block', 243 previous_id=b'block0', 244 signer_id=b'signer', 245 block_num=1, 246 payload=b'test') 247 248 self.mock_stream.send.return_value = self._make_future( 249 message_type=Message.CONSENSUS_CHAIN_HEAD_GET_RESPONSE, 250 content=consensus_pb2.ConsensusChainHeadGetResponse( 251 status=consensus_pb2.ConsensusChainHeadGetResponse.OK, 252 block=block).SerializeToString()) 253 254 chain_head = self.service.get_chain_head() 255 256 self.mock_stream.send.assert_called_with( 257 message_type=Message.CONSENSUS_CHAIN_HEAD_GET_REQUEST, 258 content=consensus_pb2.ConsensusChainHeadGetRequest() 259 .SerializeToString()) 260 261 self.assertEqual(chain_head.block_id, b'block') 262 self.assertEqual(chain_head.previous_id, b'block0') 263 self.assertEqual(chain_head.signer_id, b'signer') 264 self.assertEqual(chain_head.block_num, 1) 265 self.assertEqual(chain_head.payload, b'test') 266 267 def test_get_settings(self): 268 self.mock_stream.send.return_value = self._make_future( 269 message_type=Message.CONSENSUS_SETTINGS_GET_RESPONSE, 270 content=consensus_pb2.ConsensusSettingsGetResponse( 271 status=consensus_pb2.ConsensusSettingsGetResponse.OK, 272 entries=[ 273 consensus_pb2.ConsensusSettingsEntry( 274 key='key1', 275 value='value1'), 276 consensus_pb2.ConsensusSettingsEntry( 277 key='key2', 278 value='value2')]).SerializeToString()) 279 280 entries = self.service.get_settings( 281 block_id=b'test', 282 settings=['test1', 'test2']) 283 284 self.mock_stream.send.assert_called_with( 285 message_type=Message.CONSENSUS_SETTINGS_GET_REQUEST, 286 content=consensus_pb2.ConsensusSettingsGetRequest( 287 block_id=b'test', 288 keys=['test1', 'test2']).SerializeToString()) 289 290 self.assertEqual( 291 entries, { 292 'key1': 'value1', 293 'key2': 'value2', 294 }) 295 296 def test_get_state(self): 297 self.mock_stream.send.return_value = self._make_future( 298 message_type=Message.CONSENSUS_STATE_GET_RESPONSE, 299 content=consensus_pb2.ConsensusStateGetResponse( 300 status=consensus_pb2.ConsensusStateGetResponse.OK, 301 entries=[ 302 consensus_pb2.ConsensusStateEntry( 303 address='address1', 304 data=b'data1'), 305 consensus_pb2.ConsensusStateEntry( 306 address='address2', 307 data=b'data2')]).SerializeToString()) 308 309 entries = self.service.get_state( 310 block_id=b'test', 311 addresses=['test1', 'test2']) 312 313 self.mock_stream.send.assert_called_with( 314 message_type=Message.CONSENSUS_STATE_GET_REQUEST, 315 content=consensus_pb2.ConsensusStateGetRequest( 316 block_id=b'test', 317 addresses=['test1', 'test2']).SerializeToString()) 318 319 self.assertEqual( 320 entries, { 321 'address1': b'data1', 322 'address2': b'data2', 323 })