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_create_link_to_com_chain(self, monkeypatch): """ Test creating a linked half that points back towards a previous block """ key = default_eccrypto.generate_key(u"curve25519") com_key = default_eccrypto.generate_key(u"curve25519").pub().key_to_bin() db = MockDBManager() com_link = Links(((1, ShortKey("30303030")),)) link = FakeBlock(com_id=com_key, links=com_link) monkeypatch.setattr( MockDBManager, "get_chain", lambda _, chain_id: MockChain() if chain_id == com_key else None, ) monkeypatch.setattr( MockChain, "consistent_terminal", Links((link.com_dot,)), ) block = BamiBlock.create( b"test", encode_raw({"id": 42}), db, key.pub().key_to_bin(), com_id=com_key ) # include the personal community # Attach to the assert block.links == Links((link.com_dot,)) assert block.previous == Links((GENESIS_DOT,)) assert block.sequence_number == GENESIS_SEQ assert block.com_seq_num == link.com_seq_num + 1 assert block.public_key == key.pub().key_to_bin() assert block.signature == EMPTY_SIG assert block.type == b"test" assert block.transaction == encode_raw({"id": 42}) assert block.com_id == com_key
def test_create_next_pers(self, monkeypatch): """ Test creating a block that points towards a previous block """ db = MockDBManager() prev = FakeBlock() monkeypatch.setattr( MockDBManager, "get_chain", lambda _, chain_id: MockChain(), ) monkeypatch.setattr( MockChain, "consistent_terminal", Links((prev.pers_dot, )), ) block = BamiBlock.create(b"test", encode_raw({b"id": 42}), db, prev.public_key) assert block.previous == Links((prev.pers_dot, )) assert block.sequence_number == prev.sequence_number + 1 assert block.public_key == prev.public_key assert block.signature == EMPTY_SIG assert block.type == b"test" assert block.transaction == encode_raw({b"id": 42}) assert block.com_id == EMPTY_PK assert block.com_seq_num == UNKNOWN_SEQ
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_seq_num(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()) # init chain chain_id = self.chain_id self.dbms.chains[chain_id] = MockChain() frontier_diff = FrontierDiff(Ranges(((1, 2), )), {}) 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