Skip to content

Commit

Permalink
delete python deserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Jan 6, 2025
1 parent 9f13146 commit 4aab7af
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 333 deletions.
25 changes: 0 additions & 25 deletions counterparty-core/counterpartycore/lib/backend/bitcoind.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
Expand Down
156 changes: 0 additions & 156 deletions counterparty-core/counterpartycore/lib/bc_data_stream.py

This file was deleted.

8 changes: 6 additions & 2 deletions counterparty-core/counterpartycore/lib/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1304,8 +1304,12 @@ def parse_new_block(db, decoded_block, tx_index=None):
break
current_block_hash = backend.bitcoind.getblockhash(previous_block_index + 1)
raw_current_block = backend.bitcoind.getblock(current_block_hash)
decoded_block = deserialize.deserialize_block(raw_current_block, use_txid=True)

decoded_block = deserialize.deserialize_block(
raw_current_block,
use_txid=True,
parse_vouts=True,
block_index=previous_block_index + 1,
)
logger.warning("Blockchain reorganization detected at block %s.", previous_block_index)
# rollback to the previous block
rollback(db, block_index=previous_block_index + 1)
Expand Down
2 changes: 1 addition & 1 deletion counterparty-core/counterpartycore/lib/composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ def check_transaction_sanity(tx_info, composed_tx, construct_params):

# check if source address matches the first input address
first_utxo_txid = decoded_tx["vin"][0]["hash"]
first_utxo_txid = util.inverse_hash(binascii.hexlify(first_utxo_txid).decode("utf-8"))
# first_utxo_txid = util.inverse_hash(binascii.hexlify(first_utxo_txid).decode("utf-8"))
first_utxo = f"{first_utxo_txid}:{decoded_tx['vin'][0]['n']}"

if util.is_utxo_format(source):
Expand Down
165 changes: 36 additions & 129 deletions counterparty-core/counterpartycore/lib/deserialize.py
Original file line number Diff line number Diff line change
@@ -1,129 +1,36 @@
from counterpartycore.lib.bc_data_stream import BCDataStream
from counterpartycore.lib.util import (
b2h,
double_hash,
ib2h,
)


def read_tx_in(vds):
tx_in = {}
tx_in["hash"] = vds.read_bytes(32)
tx_in["n"] = vds.read_uint32()
script_sig_size = vds.read_compact_size()
tx_in["script_sig"] = vds.read_bytes(script_sig_size)
tx_in["sequence"] = vds.read_uint32()
tx_in["coinbase"] = False
if (
tx_in["hash"]
== b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
):
tx_in["coinbase"] = True
return tx_in


def read_tx_out(vds):
tx_out = {}
tx_out["value"] = vds.read_int64()
script = vds.read_bytes(vds.read_compact_size())
tx_out["script_pub_key"] = script
return tx_out


def read_transaction(vds, use_txid=True):
transaction = {}
start_pos = vds.read_cursor
transaction["version"] = vds.read_int32()

flag = vds.read_bytes(2)
if flag == b"\x00\x01":
transaction["segwit"] = True
else:
transaction["segwit"] = False
vds.read_cursor = vds.read_cursor - 2

transaction["coinbase"] = False
transaction["vin"] = []
for _i in range(vds.read_compact_size()): # noqa: B007
vin = read_tx_in(vds)
transaction["vin"].append(vin)
transaction["coinbase"] = transaction["coinbase"] or vin["coinbase"]

transaction["vout"] = []
for i in range(vds.read_compact_size()): # noqa: B007
transaction["vout"].append(read_tx_out(vds))

transaction["vtxinwit"] = []
if transaction["segwit"]:
offset_before_tx_witnesses = vds.read_cursor - start_pos
for vin in transaction["vin"]: # noqa: B007
witnesses_count = vds.read_compact_size()
if witnesses_count == 0:
transaction["vtxinwit"].append([])
else:
vin_witnesses = []
for i in range(witnesses_count): # noqa: B007
witness_length = vds.read_compact_size()
witness = vds.read_bytes(witness_length)
vin_witnesses.append(witness)
transaction["vtxinwit"].append(vin_witnesses)

transaction["lock_time"] = vds.read_uint32()
data = vds.input[start_pos : vds.read_cursor]

transaction["tx_hash"] = ib2h(double_hash(data))
transaction["tx_id"] = transaction["tx_hash"]
if transaction["segwit"]:
hash_data = data[:4] + data[6:offset_before_tx_witnesses] + data[-4:]
transaction["tx_id"] = ib2h(double_hash(hash_data))
if use_txid:
transaction["tx_hash"] = transaction["tx_id"]

transaction["__data__"] = b2h(data)

return transaction


def read_block_header(vds):
block_header = {}
block_header["magic_bytes"] = vds.read_int32()
# if block_header['magic_bytes'] != 118034699:
# raise Exception('Not a block')
block_header["block_size"] = vds.read_int32()
header_start = vds.read_cursor
block_header["version"] = vds.read_int32()
block_header["hash_prev"] = ib2h(vds.read_bytes(32))
block_header["hash_merkle_root"] = ib2h(vds.read_bytes(32))
block_header["block_time"] = vds.read_uint32()
block_header["bits"] = vds.read_uint32()
block_header["nonce"] = vds.read_uint32()
header_end = vds.read_cursor
header = vds.input[header_start:header_end]
block_header["block_hash"] = ib2h(double_hash(header))
# block_header['__header__'] = b2h(header)
return block_header


def read_block(vds, only_header=False, use_txid=True):
block = read_block_header(vds)
if only_header:
return block
block["transaction_count"] = vds.read_compact_size()
block["transactions"] = []
for _i in range(block["transaction_count"]): # noqa: B007
block["transactions"].append(read_transaction(vds, use_txid=use_txid))
return block


def deserialize_tx(tx_hex, use_txid):
ds = BCDataStream()
ds.map_hex(tx_hex)
tx = read_transaction(ds, use_txid=use_txid)
return tx


def deserialize_block(block_hex, use_txid, only_header=False):
block_hex = ("00" * 8) + block_hex # fake magic bytes and block size
ds = BCDataStream()
ds.map_hex(block_hex)
return read_block(ds, only_header=only_header, use_txid=use_txid)
from counterparty_rs import indexer

Check warning

Code scanning / pylint

No name 'indexer' in module 'counterparty_rs'. Warning

No name 'indexer' in module 'counterparty_rs'.

from counterpartycore.lib import config, util


def deserialize_tx(tx_hex, use_txid, parse_vouts=False, block_index=None):
deserializer = indexer.Deserializer(
{
"rpc_address": "",
"rpc_user": "",
"rpc_password": "",
"network": config.NETWORK_NAME,
"db_dir": "",
"log_file": "",
}
)
return deserializer.parse_transaction(
tx_hex, block_index or util.CURRENT_BLOCK_INDEX, parse_vouts, use_txid
)


def deserialize_block(block_hex, use_txid, parse_vouts=False, block_index=None):
# block_hex = ("00" * 8) + block_hex # fake magic bytes and block size
deserializer = indexer.Deserializer(
{
"rpc_address": "",
"rpc_user": "",
"rpc_password": "",
"network": config.NETWORK_NAME,
"db_dir": "",
"log_file": "",
}
)
return deserializer.parse_block(
block_hex, block_index or util.CURRENT_BLOCK_INDEX, parse_vouts, use_txid
)
7 changes: 6 additions & 1 deletion counterparty-core/counterpartycore/lib/follow.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,12 @@ def check_software_version_if_needed(self):

def receive_rawblock(self, body):
# parse blocks as they come in
decoded_block = deserialize.deserialize_block(body.hex(), use_txid=True)
decoded_block = deserialize.deserialize_block(
body.hex(),
use_txid=True,
parse_vouts=True,
block_index=util.CURRENT_BLOCK_INDEX + 1,
)
# check if already parsed by block.catch_up()
existing_block = ledger.get_block_by_hash(self.db, decoded_block["block_hash"])
if existing_block is None:
Expand Down
Loading

0 comments on commit 4aab7af

Please sign in to comment.