github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/sdk/python/sawtooth_sdk/consensus/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  from sawtooth_sdk.consensus.service import Service
    17  from sawtooth_sdk.consensus.service import Block
    18  from sawtooth_sdk.consensus import exceptions
    19  from sawtooth_sdk.protobuf import consensus_pb2
    20  from sawtooth_sdk.protobuf.validator_pb2 import Message
    21  
    22  
    23  class ZmqService(Service):
    24      def __init__(self, stream, timeout, name, version):
    25          self._stream = stream
    26          self._timeout = timeout
    27          self._name = name
    28          self._version = version
    29  
    30      def _send(self, request, message_type, response_type):
    31          response_bytes = self._stream.send(
    32              message_type=message_type,
    33              content=request.SerializeToString(),
    34          ).result(self._timeout).content
    35  
    36          response = response_type()
    37          response.ParseFromString(response_bytes)
    38  
    39          return response
    40  
    41      # -- P2P --
    42  
    43      def send_to(self, peer_id, message_type, payload):
    44          message = consensus_pb2.ConsensusPeerMessage(
    45              message_type=message_type,
    46              content=payload,
    47              name=self._name,
    48              version=self._version)
    49  
    50          request = consensus_pb2.ConsensusSendToRequest(
    51              message=message,
    52              peer_id=peer_id)
    53  
    54          response = self._send(
    55              request=request,
    56              message_type=Message.CONSENSUS_SEND_TO_REQUEST,
    57              response_type=consensus_pb2.ConsensusSendToResponse)
    58  
    59          if response.status != consensus_pb2.ConsensusSendToResponse.OK:
    60              raise exceptions.ReceiveError(
    61                  'Failed with status {}'.format(response.status))
    62  
    63      def broadcast(self, message_type, payload):
    64          message = consensus_pb2.ConsensusPeerMessage(
    65              message_type=message_type,
    66              content=payload,
    67              name=self._name,
    68              version=self._version)
    69  
    70          request = consensus_pb2.ConsensusBroadcastRequest(message=message)
    71  
    72          response = self._send(
    73              request=request,
    74              message_type=Message.CONSENSUS_BROADCAST_REQUEST,
    75              response_type=consensus_pb2.ConsensusBroadcastResponse)
    76  
    77          if response.status != consensus_pb2.ConsensusBroadcastResponse.OK:
    78              raise exceptions.ReceiveError(
    79                  'Failed with status {}'.format(response.status))
    80  
    81      # -- Block Creation --
    82  
    83      def initialize_block(self, previous_id=None):
    84          request = (
    85              consensus_pb2.ConsensusInitializeBlockRequest(
    86                  previous_id=previous_id)
    87              if previous_id
    88              else consensus_pb2.ConsensusInitializeBlockRequest()
    89          )
    90  
    91          response_type = consensus_pb2.ConsensusInitializeBlockResponse
    92  
    93          response = self._send(
    94              request=request,
    95              message_type=Message.CONSENSUS_INITIALIZE_BLOCK_REQUEST,
    96              response_type=response_type)
    97  
    98          status = response.status
    99  
   100          if status == response_type.INVALID_STATE:
   101              raise exceptions.InvalidState(
   102                  'Cannot initialize block in current state')
   103  
   104          if status == response_type.UNKNOWN_BLOCK:
   105              raise exceptions.UnknownBlock()
   106  
   107          if status != response_type.OK:
   108              raise exceptions.ReceiveError(
   109                  'Failed with status {}'.format(status))
   110  
   111      def summarize_block(self):
   112          request = consensus_pb2.ConsensusSummarizeBlockRequest()
   113  
   114          response_type = consensus_pb2.ConsensusSummarizeBlockResponse
   115  
   116          response = self._send(
   117              request=request,
   118              message_type=Message.CONSENSUS_SUMMARIZE_BLOCK_REQUEST,
   119              response_type=response_type)
   120  
   121          status = response.status
   122  
   123          if status == response_type.INVALID_STATE:
   124              raise exceptions.InvalidState(
   125                  'Cannot summarize block in current state')
   126  
   127          if status == response_type.BLOCK_NOT_READY:
   128              raise exceptions.BlockNotReady(
   129                  'Block not ready to be summarize')
   130  
   131          if status != response_type.OK:
   132              raise exceptions.ReceiveError(
   133                  'Failed with status {}'.format(status))
   134  
   135          return response.summary
   136  
   137      def finalize_block(self, data):
   138          request = consensus_pb2.ConsensusFinalizeBlockRequest(data=data)
   139  
   140          response_type = consensus_pb2.ConsensusFinalizeBlockResponse
   141  
   142          response = self._send(
   143              request=request,
   144              message_type=Message.CONSENSUS_FINALIZE_BLOCK_REQUEST,
   145              response_type=response_type)
   146  
   147          status = response.status
   148  
   149          if status == response_type.INVALID_STATE:
   150              raise exceptions.InvalidState(
   151                  'Cannot finalize block in current state')
   152  
   153          if status == response_type.BLOCK_NOT_READY:
   154              raise exceptions.BlockNotReady(
   155                  'Block not ready to be finalized')
   156  
   157          if status != response_type.OK:
   158              raise exceptions.ReceiveError(
   159                  'Failed with status {}'.format(status))
   160  
   161          return response.block_id
   162  
   163      def cancel_block(self):
   164          request = consensus_pb2.ConsensusCancelBlockRequest()
   165  
   166          response_type = consensus_pb2.ConsensusCancelBlockResponse
   167  
   168          response = self._send(
   169              request=request,
   170              message_type=Message.CONSENSUS_CANCEL_BLOCK_REQUEST,
   171              response_type=response_type)
   172  
   173          status = response.status
   174  
   175          if status == response_type.INVALID_STATE:
   176              raise exceptions.InvalidState(
   177                  'Cannot cancel block in current state')
   178  
   179          if status != response_type.OK:
   180              raise exceptions.ReceiveError(
   181                  'Failed with status {}'.format(status))
   182  
   183      # -- Block Directives --
   184  
   185      def check_blocks(self, priority):
   186          request = consensus_pb2.ConsensusCheckBlocksRequest(block_ids=priority)
   187  
   188          response_type = consensus_pb2.ConsensusCheckBlocksResponse
   189  
   190          response = self._send(
   191              request=request,
   192              message_type=Message.CONSENSUS_CHECK_BLOCKS_REQUEST,
   193              response_type=response_type)
   194  
   195          status = response.status
   196  
   197          if status == response_type.UNKNOWN_BLOCK:
   198              raise exceptions.UnknownBlock()
   199  
   200          if status != response_type.OK:
   201              raise exceptions.ReceiveError(
   202                  'Failed with status {}'.format(status))
   203  
   204      def commit_block(self, block_id):
   205          request = consensus_pb2.ConsensusCommitBlockRequest(block_id=block_id)
   206  
   207          response_type = consensus_pb2.ConsensusCommitBlockResponse
   208  
   209          response = self._send(
   210              request=request,
   211              message_type=Message.CONSENSUS_COMMIT_BLOCK_REQUEST,
   212              response_type=response_type)
   213  
   214          status = response.status
   215  
   216          if status == response_type.UNKNOWN_BLOCK:
   217              raise exceptions.UnknownBlock()
   218  
   219          if status != response_type.OK:
   220              raise exceptions.ReceiveError(
   221                  'Failed with status {}'.format(status))
   222  
   223      def ignore_block(self, block_id):
   224          request = consensus_pb2.ConsensusIgnoreBlockRequest(block_id=block_id)
   225  
   226          response_type = consensus_pb2.ConsensusIgnoreBlockResponse
   227  
   228          response = self._send(
   229              request=request,
   230              message_type=Message.CONSENSUS_IGNORE_BLOCK_REQUEST,
   231              response_type=response_type)
   232  
   233          status = response.status
   234  
   235          if status == response_type.UNKNOWN_BLOCK:
   236              raise exceptions.UnknownBlock()
   237  
   238          if status != response_type.OK:
   239              raise exceptions.ReceiveError(
   240                  'Failed with status {}'.format(status))
   241  
   242      def fail_block(self, block_id):
   243          request = consensus_pb2.ConsensusFailBlockRequest(block_id=block_id)
   244  
   245          response_type = consensus_pb2.ConsensusFailBlockResponse
   246  
   247          response = self._send(
   248              request=request,
   249              message_type=Message.CONSENSUS_FAIL_BLOCK_REQUEST,
   250              response_type=response_type)
   251  
   252          status = response.status
   253  
   254          if status == response_type.UNKNOWN_BLOCK:
   255              raise exceptions.UnknownBlock()
   256  
   257          if status != response_type.OK:
   258              raise exceptions.ReceiveError(
   259                  'Failed with status {}'.format(status))
   260  
   261      # -- Queries --
   262  
   263      def get_blocks(self, block_ids):
   264          request = consensus_pb2.ConsensusBlocksGetRequest(block_ids=block_ids)
   265  
   266          response_type = consensus_pb2.ConsensusBlocksGetResponse
   267  
   268          response = self._send(
   269              request=request,
   270              message_type=Message.CONSENSUS_BLOCKS_GET_REQUEST,
   271              response_type=response_type)
   272  
   273          status = response.status
   274  
   275          if status == response_type.UNKNOWN_BLOCK:
   276              raise exceptions.UnknownBlock()
   277  
   278          if status != response_type.OK:
   279              raise exceptions.ReceiveError(
   280                  'Failed with status {}'.format(status))
   281  
   282          return {
   283              block.block_id: Block(block)
   284              for block in response.blocks
   285          }
   286  
   287      def get_chain_head(self):
   288          request = consensus_pb2.ConsensusChainHeadGetRequest()
   289  
   290          response_type = consensus_pb2.ConsensusChainHeadGetResponse
   291  
   292          response = self._send(
   293              request=request,
   294              message_type=Message.CONSENSUS_CHAIN_HEAD_GET_REQUEST,
   295              response_type=response_type)
   296  
   297          status = response.status
   298  
   299          if status == response_type.NO_CHAIN_HEAD:
   300              raise exceptions.NoChainHead()
   301  
   302          if status != response_type.OK:
   303              raise exceptions.ReceiveError(
   304                  'Failed with status {}'.format(status))
   305  
   306          return Block(response.block)
   307  
   308      def get_settings(self, block_id, settings):
   309          request = consensus_pb2.ConsensusSettingsGetRequest(
   310              block_id=block_id,
   311              keys=settings)
   312  
   313          response_type = consensus_pb2.ConsensusSettingsGetResponse
   314  
   315          response = self._send(
   316              request=request,
   317              message_type=Message.CONSENSUS_SETTINGS_GET_REQUEST,
   318              response_type=response_type)
   319  
   320          status = response.status
   321  
   322          if status == response_type.UNKNOWN_BLOCK:
   323              raise exceptions.UnknownBlock()
   324  
   325          if status != response_type.OK:
   326              raise exceptions.ReceiveError(
   327                  'Failed with status {}'.format(status))
   328  
   329          return {
   330              entry.key: entry.value
   331              for entry in response.entries
   332          }
   333  
   334      def get_state(self, block_id, addresses):
   335          request = consensus_pb2.ConsensusStateGetRequest(
   336              block_id=block_id,
   337              addresses=addresses)
   338  
   339          response_type = consensus_pb2.ConsensusStateGetResponse
   340  
   341          response = self._send(
   342              request=request,
   343              message_type=Message.CONSENSUS_STATE_GET_REQUEST,
   344              response_type=response_type)
   345  
   346          status = response.status
   347  
   348          if status == response_type.UNKNOWN_BLOCK:
   349              raise exceptions.UnknownBlock()
   350  
   351          if status != response_type.OK:
   352              raise exceptions.ReceiveError(
   353                  'Failed with status {}'.format(status))
   354  
   355          return {
   356              entry.address: entry.data
   357              for entry in response.entries
   358          }