github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/validator/sawtooth_validator/consensus/notifier.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 hashlib 17 import logging 18 19 from sawtooth_validator.protobuf import consensus_pb2 20 from sawtooth_validator.protobuf import validator_pb2 21 22 LOGGER = logging.getLogger(__name__) 23 24 25 class ConsensusNotifier: 26 """Handles sending notifications to the consensus engine using the provided 27 interconnect service.""" 28 29 def __init__(self, consensus_service): 30 self._service = consensus_service 31 32 def _notify(self, message_type, message): 33 futures = self._service.send_all( 34 message_type, 35 message.SerializeToString()) 36 for future in futures: 37 future.result() 38 39 def notify_peer_connected(self, peer_id): 40 """A new peer was added""" 41 self._notify( 42 validator_pb2.Message.CONSENSUS_NOTIFY_PEER_CONNECTED, 43 consensus_pb2.ConsensusNotifyPeerConnected( 44 consensus_pb2.ConsensusPeerInfo( 45 peer_id=bytes.fromhex(peer_id)))) 46 47 def notify_peer_disconnected(self, peer_id): 48 """An existing peer was dropped""" 49 self._notify( 50 validator_pb2.Message.CONSENSUS_NOTIFY_PEER_DISCONNECTED, 51 consensus_pb2.ConsensusNotifyPeerDisconnected( 52 peer_id=bytes.fromhex(peer_id))) 53 54 def notify_peer_message(self, message, sender_id): 55 """A new message was received from a peer""" 56 self._notify( 57 validator_pb2.Message.CONSENSUS_NOTIFY_PEER_MESSAGE, 58 consensus_pb2.ConsensusNotifyPeerMessage( 59 message=message, 60 sender_id=sender_id)) 61 62 def notify_block_new(self, block): 63 """A new block was received and passed initial consensus validation""" 64 summary = hashlib.sha256() 65 for batch in block.batches: 66 summary.update(batch.header_signature.encode()) 67 self._notify( 68 validator_pb2.Message.CONSENSUS_NOTIFY_BLOCK_NEW, 69 consensus_pb2.ConsensusNotifyBlockNew( 70 block=consensus_pb2.ConsensusBlock( 71 block_id=bytes.fromhex(block.identifier), 72 previous_id=bytes.fromhex(block.previous_block_id), 73 signer_id=bytes.fromhex(block.header.signer_public_key), 74 block_num=block.block_num, 75 payload=block.consensus, 76 summary=summary.digest()))) 77 78 def notify_block_valid(self, block_id): 79 """This block can be committed successfully""" 80 self._notify( 81 validator_pb2.Message.CONSENSUS_NOTIFY_BLOCK_VALID, 82 consensus_pb2.ConsensusNotifyBlockValid( 83 block_id=bytes.fromhex(block_id))) 84 85 def notify_block_invalid(self, block_id): 86 """This block cannot be committed successfully""" 87 self._notify( 88 validator_pb2.Message.CONSENSUS_NOTIFY_BLOCK_INVALID, 89 consensus_pb2.ConsensusNotifyBlockInvalid( 90 block_id=bytes.fromhex(block_id))) 91 92 def notify_block_commit(self, block_id): 93 """This block has been committed""" 94 self._notify( 95 validator_pb2.Message.CONSENSUS_NOTIFY_BLOCK_COMMIT, 96 consensus_pb2.ConsensusNotifyBlockCommit( 97 block_id=bytes.fromhex(block_id)))