예제 #1
0
    def initialize_block(self, previous_id):
        request = (
            consensus_pb2.ConsensusInitializeBlockRequest(
                previous_id=previous_id)
            if previous_id
            else consensus_pb2.ConsensusInitializeBlockRequest()
        )

        response_type = consensus_pb2.ConsensusInitializeBlockResponse

        response = self._send(
            request=request,
            message_type=Message.CONSENSUS_INITIALIZE_BLOCK_REQUEST,
            response_type=response_type)

        status = response.status

        if status == response_type.INVALID_STATE:
            raise exceptions.InvalidState(
                'Cannot initialize block in current state')

        if status == response_type.UNKNOWN_BLOCK:
            raise exceptions.UnknownBlock()

        if status != response_type.OK:
            raise exceptions.ReceiveError(
                'Failed with status {}'.format(status))
예제 #2
0
    def get_state(self, block_id, addresses):
        request = consensus_pb2.ConsensusStateGetRequest(
            block_id=block_id,
            addresses=addresses)

        response_type = consensus_pb2.ConsensusStateGetResponse

        response = self._send(
            request=request,
            message_type=Message.CONSENSUS_STATE_GET_REQUEST,
            response_type=response_type)

        status = response.status

        if status == response_type.UNKNOWN_BLOCK:
            raise exceptions.UnknownBlock()

        if status != response_type.OK:
            raise exceptions.ReceiveError(
                'Failed with status {}'.format(status))

        return {
            entry.address: entry.data
            for entry in response.entries
        }
예제 #3
0
    def get_settings(self, block_id, settings):
        request = consensus_pb2.ConsensusSettingsGetRequest(
            block_id=block_id,
            keys=settings)

        response_type = consensus_pb2.ConsensusSettingsGetResponse

        response = self._send(
            request=request,
            message_type=Message.CONSENSUS_SETTINGS_GET_REQUEST,
            response_type=response_type)

        status = response.status

        if status == response_type.UNKNOWN_BLOCK:
            raise exceptions.UnknownBlock()

        if status != response_type.OK:
            raise exceptions.ReceiveError(
                'Failed with status {}'.format(status))

        return {
            entry.key: entry.value
            for entry in response.entries
        }
예제 #4
0
    def fail_block(self, block_id):
        request = consensus_pb2.ConsensusFailBlockRequest(block_id=block_id)

        response_type = consensus_pb2.ConsensusFailBlockResponse

        response = self._send(
            request=request,
            message_type=Message.CONSENSUS_FAIL_BLOCK_REQUEST,
            response_type=response_type)

        status = response.status

        if status == response_type.UNKNOWN_BLOCK:
            raise exceptions.UnknownBlock()

        if status != response_type.OK:
            raise exceptions.ReceiveError(
                'Failed with status {}'.format(status))
예제 #5
0
    def check_blocks(self, priority):
        request = consensus_pb2.ConsensusCheckBlocksRequest(block_ids=priority)

        response_type = consensus_pb2.ConsensusCheckBlocksResponse

        response = self._send(
            request=request,
            message_type=Message.CONSENSUS_CHECK_BLOCKS_REQUEST,
            response_type=response_type)

        status = response.status

        if status == response_type.UNKNOWN_BLOCK:
            raise exceptions.UnknownBlock()

        if status != response_type.OK:
            raise exceptions.ReceiveError(
                'Failed with status {}'.format(status))
예제 #6
0
    def get_blocks(self, block_ids):
        request = consensus_pb2.ConsensusBlocksGetRequest(block_ids=block_ids)

        response_type = consensus_pb2.ConsensusBlocksGetResponse

        response = self._send(
            request=request,
            message_type=Message.CONSENSUS_BLOCKS_GET_REQUEST,
            response_type=response_type)

        status = response.status

        if status == response_type.UNKNOWN_BLOCK:
            raise exceptions.UnknownBlock()

        if status != response_type.OK:
            raise exceptions.ReceiveError(
                'Failed with status {}'.format(status))

        return {block.block_id: Block(block) for block in response.blocks}