def deserialize(self, raw): """Deserialize hex-encoded block/block header. Returns: Two-tuple of (block, block_header) """ raw = x(raw) try: if len(raw) == BlockHeader.header_length(): block_header = BlockHeader.deserialize(raw) return (None, block_header) else: # We don't use block.get_header() in case the header is # correct but the rest of the block isn't. block_header = BlockHeader.deserialize(raw[0:BlockHeader.header_length()]) block = Block.deserialize(raw) return (block, block_header) except Exception as e: return (None, None)
def deserialize(self, raw): """Deserialize hex-encoded block/block header. Returns: Two-tuple of (block, block_header) """ raw = x(raw) try: if len(raw) == BlockHeader.header_length(): block_header = BlockHeader.deserialize(raw) return (None, block_header) else: # We don't use block.get_header() in case the header is # correct but the rest of the block isn't. block_header = BlockHeader.deserialize( raw[0:BlockHeader.header_length()]) block = Block.deserialize(raw) return (block, block_header) except Exception as e: return (None, None)
def select_block_field(self, selected, deselected): if len(self.raw_block_edit.toPlainText()) < BlockHeader.header_length() * 2: return if not len(selected.indexes()): return index = selected.indexes()[0] row = index.row() header = [i[2] * 2 for i in self.header.fields] start = sum(header[0:row]) length = header[row] cursor = QTextCursor(self.raw_block_edit.document()) cursor.setPosition(start) cursor.setPosition(start + length, QTextCursor.KeepAnchor) self.raw_block_edit.setTextCursor(cursor)
def select_block_field(self, selected, deselected): if len(self.raw_block_edit.toPlainText() ) < BlockHeader.header_length() * 2: return if not len(selected.indexes()): return index = selected.indexes()[0] row = index.row() header = [i[2] * 2 for i in self.header.fields] start = sum(header[0:row]) length = header[row] cursor = QTextCursor(self.raw_block_edit.document()) cursor.setPosition(start) cursor.setPosition(start + length, QTextCursor.KeepAnchor) self.raw_block_edit.setTextCursor(cursor)
def on_tx_selection(self, selected, deselected): if not self.block or not len(selected.indexes()): return index = selected.indexes()[0] row = index.row() def tx_len(i): return len(self.block.vtx[i].serialize()) * 2 start = BlockHeader.header_length() * 2 + sum(tx_len(i) for i in range(row)) # Account for VarInt. _buf = BytesIO() VarIntSerializer.stream_serialize(len(self.block.vtx), _buf) start += len(_buf.getvalue()) * 2 length = len(self.block.vtx[row].serialize()) * 2 self.select_block_text(start, length)
def on_tx_selection(self, selected, deselected): if not self.block or not len(selected.indexes()): return index = selected.indexes()[0] row = index.row() def tx_len(i): return len(self.block.vtx[i].serialize()) * 2 start = BlockHeader.header_length() * 2 + sum( tx_len(i) for i in range(row)) # Account for VarInt. _buf = BytesIO() VarIntSerializer.stream_serialize(len(self.block.vtx), _buf) start += len(_buf.getvalue()) * 2 length = len(self.block.vtx[row].serialize()) * 2 self.select_block_text(start, length)
def test_bitcoin_fields(self): self.assertEqual(BlockHeader.header_length(), 80) header = BlockHeader.deserialize(bitcoin_raw_header) self.assertEqual(bitcoin_header_fields, header.fields)