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_create_genesis(self): """ Test creating a genesis block """ key = default_eccrypto.generate_key(u"curve25519") db = MockDBManager() block = BamiBlock.create(b"test", encode_raw({b"id": 42}), db, key.pub().key_to_bin()) assert block.previous == Links((GENESIS_DOT, )) assert block.sequence_number == GENESIS_SEQ assert block.public_key == key.pub().key_to_bin() 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 create_signed_block( self, block_type: bytes = b"unknown", transaction: Optional[bytes] = None, prefix: bytes = b"", com_id: bytes = None, links: Links = None, personal_links: Links = None, use_consistent_links: bool = True, ) -> BamiBlock: """ This function will create, sign, persist block with given parameters. Args: block_type: bytes of the block transaction: bytes blob of the transaction, or None to indicate an empty transaction payload prefix: prefix for the community id. For example b'w' - for witnessing transactions com_id: sub-community id if applicable links: explicitly link to certain links in the sub-community. Warning - may lead to forks! personal_links: explicitly link to certain blocks in the own chain. Warning - may lead to forks! use_consistent_links (): Returns: signed block """ if not transaction: transaction = encode_raw(b"") block = BamiBlock.create( block_type, transaction, self.persistence, self.my_pub_key_bin, com_id=com_id, com_links=links, pers_links=personal_links, com_prefix=prefix, use_consistent_links=use_consistent_links, ) block.sign(self.my_peer_key) self.validate_persist_block(block, self.my_peer) return block