def _process_missing_seq_num(self, chain: BaseChain, chain_id: bytes, missing_ranges: Set[int]) -> Iterable[bytes]: for b_i in missing_ranges: # Return all blocks with a sequence number for dot in chain.get_dots_by_seq_num(b_i): val = self.get_block_blob_by_dot(chain_id, dot) if not val: raise Exception("No block", chain_id, dot) yield val
def _find_first_conflicting_point(self, conf_dict: Dict, chain: BaseChain) -> Set[Dot]: to_request = set() for sn, hash_vals in conf_dict.items(): local_val = chain.get_all_short_hash_by_seq_num(sn) if not local_val: # Don't know this value => request from peer to_request.update(Dot((sn, k) for k in hash_vals)) continue diff_val = local_val - set(hash_vals) sim_diff = set(hash_vals) - local_val if sim_diff: to_request.update(Dot((sn, k)) for k in sim_diff) # If there is a hash that is known if diff_val: # First inconsistency point met return {Dot((sn, k)) for k in diff_val}, to_request return set(), to_request
def _process_conflicting( self, chain: BaseChain, chain_id: bytes, conflict_dict: Dict[Dot, Dict[int, Tuple[ShortKey]]], val_to_request: Set, ): # for c in conflict_dict: # val = self.get_block_blob_by_dot(chain_id, c) # if not val: # raise Exception("No block", chain_id, c) # yield val for conf_dot, conf_dict in conflict_dict.items(): if not conf_dict: val = self.get_block_blob_by_dot(chain_id, conf_dot) if val: yield val continue current_point, to_request = self._find_first_conflicting_point( conf_dict, chain) val_to_request.update(to_request) while (current_point and max(current_point)[0] < conf_dot[0] and conf_dot not in current_point): new_point = set() for d in current_point: val = self.get_block_blob_by_dot(chain_id, d) if not val: continue yield val l = chain.get_next_links(d) if l: new_point.update(set(l)) current_point = new_point val = self.get_block_blob_by_dot(chain_id, conf_dot) if val: yield val
def insert_to_chain(chain_obj: BaseChain, blk: BamiBlock, personal_chain: bool = True): block_links = blk.links if not personal_chain else blk.previous block_seq_num = blk.com_seq_num if not personal_chain else blk.sequence_number yield chain_obj.add_block(block_links, block_seq_num, blk.hash)