async def test_one_gossip_round(set_vals, monkeypatch, mocker): monkeypatch.setattr(MockDBManager, "get_chain", lambda _, __: MockChain()) front = Frontier(((1, "val1"), ), (), ()) monkeypatch.setattr(MockChain, "frontier", front) monkeypatch.setattr( MockNextPeerSelection, "get_next_gossip_peers", lambda _, subcom, chain_id, frontier, fanout: [p.overlay.my_peer for p in set_vals.nodes], ) monkeypatch.setattr( MockDBManager, "reconcile", lambda _, c_id, frontier, pub_key: FrontierDiff(((1, 1), ), {}), ) monkeypatch.setattr(MockSettings, "frontier_gossip_collect_time", 0.1) monkeypatch.setattr(MockSettings, "frontier_gossip_fanout", 5) monkeypatch.setattr( MockDBManager, "get_block_blobs_by_frontier_diff", lambda _, c_id, f_diff, __: [b"blob1"], ) spy = mocker.spy(set_vals.nodes[0].overlay, "send_packet") set_vals.nodes[0].overlay.frontier_gossip_sync_task(set_vals.community_id) spy.assert_called()
def test_blocks_frontier_with_extra_request(self, monkeypatch, std_vals): monkeypatch.setattr(MockBlockStore, "get_hash_by_dot", lambda _, dot_bytes: self.test_hash) monkeypatch.setattr(MockBlockStore, "get_block_by_hash", lambda _, blob_hash: self.block_blob) monkeypatch.setattr(MockChain, "get_dots_by_seq_num", lambda _, seq_num: ("dot1", "dot2")) local_vers = {2: {"ef1"}, 7: {"ef1"}} monkeypatch.setattr( MockChain, "get_all_short_hash_by_seq_num", lambda _, seq_num: local_vers.get(seq_num), ) monkeypatch.setattr( MockChain, "get_next_links", lambda _, dot: ((dot[0] + 1, ShortKey("efef")), ), ) # init chain chain_id = self.chain_id self.dbms.chains[chain_id] = MockChain() frontier_diff = FrontierDiff( (), {(10, ShortKey("efef")): { 2: ("ef1", ), 7: ("ef2", ) }}) set_to_request = set() blobs = self.dbms.get_block_blobs_by_frontier_diff( chain_id, frontier_diff, set_to_request) assert len(set_to_request) == 1 assert len(blobs) == 1
def test_blocks_by_frontier_diff_no_chain(self, monkeypatch, std_vals): monkeypatch.setattr(MockBlockStore, "get_hash_by_dot", lambda _, dot_bytes: self.test_hash) monkeypatch.setattr(MockBlockStore, "get_block_by_hash", lambda _, blob_hash: self.block_blob) monkeypatch.setattr(MockChain, "get_dots_by_seq_num", lambda _, seq_num: list("dot1")) # init chain chain_id = self.chain_id # self.dbms.chains[chain_id] = MockChain() frontier_diff = FrontierDiff(Ranges(((1, 1), )), {}) set_to_request = set() blobs = self.dbms.get_block_blobs_by_frontier_diff( chain_id, frontier_diff, set_to_request) assert len(set_to_request) == 0 assert len(list(blobs)) == 0
def test_blocks_by_frontier_diff(self, monkeypatch, std_vals): monkeypatch.setattr(MockBlockStore, "get_hash_by_dot", lambda _, dot_bytes: bytes(dot_bytes)) monkeypatch.setattr(MockBlockStore, "get_block_by_hash", lambda _, blob_hash: bytes(blob_hash)) monkeypatch.setattr(MockChain, "get_dots_by_seq_num", lambda _, seq_num: ("dot1", "dot2")) # init chain chain_id = self.chain_id self.dbms.chains[chain_id] = MockChain() frontier_diff = FrontierDiff(Ranges(((1, 2), )), {(1, ShortKey("efef")): {}}) vals_to_request = set() blobs = self.dbms.get_block_blobs_by_frontier_diff( chain_id, frontier_diff, vals_to_request) assert len(vals_to_request) == 0 assert len(blobs) == 3
def received_blocks_request( self, peer: Peer, payload: BlocksRequestPayload ) -> None: f_diff = FrontierDiff.from_bytes(payload.frontier_diff) chain_id = payload.subcom_id vals_to_request = set() self.logger.debug( "Received block request %s from peer %s. Audit chain: %s", f_diff, peer, chain_id.startswith(b"w"), ) blocks = self.persistence.get_block_blobs_by_frontier_diff( chain_id, f_diff, vals_to_request ) self.logger.debug( "Sending %s blocks to peer %s. Audit chain %s", len(blocks), peer, chain_id.startswith(b"w"), ) for block in blocks: self.send_packet(peer, RawBlockPayload(block))
def test_frontier_diff_bytes_convert(val): f = FrontierDiff(val.holes, val.conflicts) assert FrontierDiff.from_bytes(f.to_bytes()) == f