def add_block(self, block_id, root='merkle_root'): txn_header = TransactionHeader(batcher_pubkey='pubkey', family_name='family', family_version='0.0', nonce=block_id, signer_pubkey='pubkey') txn = Transaction(header=txn_header.SerializeToString(), header_signature=block_id, payload=b'payload') batch_header = BatchHeader(signer_pubkey='pubkey', transaction_ids=[txn.header_signature]) batch = Batch(header=batch_header.SerializeToString(), header_signature=block_id, transactions=[txn]) blk_header = BlockHeader(block_num=len(self._store), previous_block_id=self._chain_head_id, signer_pubkey='pubkey', batch_ids=[batch.header_signature], consensus=b'consensus', state_root_hash=root) block = Block(header=blk_header.SerializeToString(), header_signature=block_id, batches=[batch]) self._store[block_id] = block self._chain_head_id = block_id
def make_blocks(cls, *block_ids): """Returns Block objects with the specified ids, and each with one Batch with one Transaction with matching ids. """ blocks = [] for block_id in block_ids: batches = cls.make_batches(block_id) blk_header = BlockHeader( block_num=len(blocks), previous_block_id=blocks[-1].header_signature if blocks else '', signer_public_key='public_key', batch_ids=[b.header_signature for b in batches], consensus=b'consensus', state_root_hash='root_hash') block = Block(header=blk_header.SerializeToString(), header_signature=block_id, batches=batches) blocks.append(block) return blocks
def get_blocks(self, head_id=None): blocks = [] current_id = head_id or self._chain_head_id while current_id in self._store: blocks.append(self._store[current_id]) header = BlockHeader() header.ParseFromString(blocks[-1].header) current_id = header.previous_block_id return blocks