From 4aab7af4110e2f0e053fa05ceaf53a9687e372d8 Mon Sep 17 00:00:00 2001 From: Ouziel Slama Date: Mon, 6 Jan 2025 12:52:32 +0000 Subject: [PATCH] delete python deserializer --- .../counterpartycore/lib/backend/bitcoind.py | 25 --- .../counterpartycore/lib/bc_data_stream.py | 156 ----------------- .../counterpartycore/lib/blocks.py | 8 +- .../counterpartycore/lib/composer.py | 2 +- .../counterpartycore/lib/deserialize.py | 165 ++++-------------- .../counterpartycore/lib/follow.py | 7 +- .../counterpartycore/lib/gettxinfo.py | 2 +- .../counterpartycore/lib/mempool.py | 2 +- .../counterpartycore/test/conftest.py | 5 + .../counterpartycore/test/deserialize_test.py | 16 +- .../fixtures/contract_vectors/gettxinfo.py | 30 +++- .../counterpartycore/test/util_test.py | 7 +- counterparty-rs/src/indexer/bitcoin_client.rs | 1 + 13 files changed, 93 insertions(+), 333 deletions(-) delete mode 100644 counterparty-core/counterpartycore/lib/bc_data_stream.py diff --git a/counterparty-core/counterpartycore/lib/backend/bitcoind.py b/counterparty-core/counterpartycore/lib/backend/bitcoind.py index acf322d857..b9088ba793 100644 --- a/counterparty-core/counterpartycore/lib/backend/bitcoind.py +++ b/counterparty-core/counterpartycore/lib/backend/bitcoind.py @@ -350,21 +350,6 @@ def add_block_in_cache(block_index, block): add_transaction_in_cache(transaction["tx_hash"], transaction) -def get_decoded_block(block_index): - if block_index in BLOCKS_CACHE: - # remove from cache when used - return BLOCKS_CACHE.pop(block_index) - - block_hash = getblockhash(block_index) - raw_block = getblock(block_hash) - use_txid = util.enabled("correct_segwit_txids", block_index=block_index) - block = deserialize.deserialize_block(raw_block, use_txid=use_txid) - - add_block_in_cache(block_index, block) - - return block - - def get_decoded_transaction(tx_hash, block_index=None): if isinstance(tx_hash, bytes): tx_hash = ib2h(tx_hash) @@ -389,16 +374,6 @@ def get_utxo_value(tx_hash, vout): return get_tx_out_amount(tx_hash, vout) -class BlockFetcher: - def __init__(self, first_block) -> None: - self.current_block = first_block - - def get_block(self): - block = get_decoded_block(self.current_block) - self.current_block += 1 - return block - - def sendrawtransaction(signedhex: str): """ Proxy to `sendrawtransaction` RPC call. diff --git a/counterparty-core/counterpartycore/lib/bc_data_stream.py b/counterparty-core/counterpartycore/lib/bc_data_stream.py deleted file mode 100644 index f65c1ea3b0..0000000000 --- a/counterparty-core/counterpartycore/lib/bc_data_stream.py +++ /dev/null @@ -1,156 +0,0 @@ -# -# Workalike python implementation of Bitcoin's CDataStream class. -# -import mmap -import struct - -from .exceptions import SerializationError - - -class BCDataStream(object): - def __init__(self): - self.input = None - self.read_cursor = 0 - - def clear(self): - self.input = None - self.read_cursor = 0 - - def write(self, bytes): # Initialize with string of bytes - if self.input is None: - self.input = bytes - else: - self.input += bytes - - def map_file(self, file, start): # Initialize with bytes from file - self.input = mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) - self.read_cursor = start - - def map_hex(self, data): # Initialize with hex string - data_bytes = bytes.fromhex(data) - self.input = data_bytes - self.read_cursor = 0 - - def seek_file(self, position): - self.read_cursor = position - - def close_file(self): - self.input.close() - - def read_string(self): - # Strings are encoded depending on length: - # 0 to 252 : 1-byte-length followed by bytes (if any) - # 253 to 65,535 : byte'253' 2-byte-length followed by bytes - # 65,536 to 4,294,967,295 : byte '254' 4-byte-length followed by bytes - # ... and the Bitcoin client is coded to understand: - # greater than 4,294,967,295 : byte '255' 8-byte-length followed by bytes of string - # ... but I don't think it actually handles any strings that big. - if self.input is None: - raise SerializationError("call write(bytes) before trying to deserialize") - - try: - length = self.read_compact_size() - except IndexError: - raise SerializationError("attempt to read past end of buffer") # noqa: B904 - - return self.read_bytes(length) - - def write_string(self, string): - # Length-encoded as with read-string - self.write_compact_size(len(string)) - self.write(string) - - def read_bytes(self, length): - try: - result = self.input[self.read_cursor : self.read_cursor + length] - self.read_cursor += length - return result - except IndexError: - raise SerializationError("attempt to read past end of buffer") # noqa: B904 - - def read_boolean(self): - return self.read_bytes(1)[0] != chr(0) - - def read_int16(self): - return self._read_num("