github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/sdk/python/sawtooth_sdk/consensus/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 abc 17 18 19 class Block: 20 def __init__(self, block): 21 self.block_id = block.block_id 22 self.previous_id = block.previous_id 23 self.signer_id = block.signer_id 24 self.block_num = block.block_num 25 self.payload = block.payload 26 self.summary = block.summary 27 28 29 class Service(metaclass=abc.ABCMeta): 30 '''Provides methods that allow the consensus engine to issue commands 31 and requests.''' 32 33 # -- P2P -- 34 35 @abc.abstractmethod 36 def send_to(self, peer_id, message_type, payload): 37 '''Send a consensus message to a specific connected peer. 38 39 Args: 40 peer_id (bytes) 41 message_type (str) 42 payload (bytes) 43 ''' 44 45 @abc.abstractmethod 46 def broadcast(self, message_type, payload): 47 '''Broadcast a message to all connected peers. 48 49 Args: 50 message_type (str) 51 payload (bytes) 52 ''' 53 54 # -- Block Creation -- 55 56 @abc.abstractmethod 57 def initialize_block(self, previous_id): 58 '''Initialize a new block with PREVIOUS_ID and begin adding batches to 59 it. If no PREVIOUS_ID is specified, the current head will be 60 used. 61 62 Args: 63 previous_id (bytes or None) 64 ''' 65 66 @abc.abstractmethod 67 def summarize_block(self): 68 '''Stop adding batches to the current block and return a summary of its 69 contents. 70 71 Return: 72 bytes 73 ''' 74 75 @abc.abstractmethod 76 def finalize_block(self, data): 77 '''Insert the given consensus data into the block and sign it. If this 78 call is successful, the consensus engine will receive the block 79 afterwards. 80 81 Args: 82 data (bytes) 83 84 Return: 85 bytes 86 ''' 87 88 @abc.abstractmethod 89 def cancel_block(self): 90 '''Stop adding batches to the current block and abandon it.''' 91 92 # -- Block Directives -- 93 94 @abc.abstractmethod 95 def check_blocks(self, priority): 96 '''Update the prioritization of blocks to check to PRIORITY. 97 98 Args: 99 priority (list[bytes]) 100 ''' 101 102 @abc.abstractmethod 103 def commit_block(self, block_id): 104 '''Update the block that should be committed. 105 106 Args: 107 block_id (bytes) 108 ''' 109 110 @abc.abstractmethod 111 def ignore_block(self, block_id): 112 '''Signal that this block is no longer being committed. 113 114 Args: 115 block_id (bytes) 116 ''' 117 118 @abc.abstractmethod 119 def fail_block(self, block_id): 120 '''Mark this block as invalid from the perspective of consensus. 121 122 Args: 123 block_id (bytes) 124 ''' 125 126 # -- Queries -- 127 128 @abc.abstractmethod 129 def get_blocks(self, block_ids): 130 '''Retrive consensus-related information about blocks. 131 132 Args: 133 block_ids (list[bytes]) 134 135 Return: 136 dict[bytes, block] 137 ''' 138 139 @abc.abstractmethod 140 def get_chain_head(self): 141 '''Retrieve consensus-related information about the chain head. 142 143 Return: 144 block 145 ''' 146 147 @abc.abstractmethod 148 def get_settings(self, block_id, settings): 149 '''Read the value of settings as of the given block. 150 151 Args: 152 block_id (bytes) 153 settings (list[str]) 154 155 Return: 156 dict[str, str] 157 ''' 158 159 @abc.abstractmethod 160 def get_state(self, block_id, addresses): 161 '''Read values in state as of the given block. 162 163 Args: 164 block_id (bytes) 165 addresses (list[str]) 166 167 Return: 168 dict[str, bytes] 169 '''