Skip to content

Commit

Permalink
new pack and unpack for sweep
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Mar 3, 2025
1 parent b625ade commit 0da25a5
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 23 deletions.
61 changes: 51 additions & 10 deletions counterparty-core/counterpartycore/lib/messages/sweep.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
)
from counterpartycore.lib.ledger.currentstate import CurrentState
from counterpartycore.lib.parser import messagetype, protocol
from counterpartycore.lib.utils import address
from counterpartycore.lib.utils import address, helpers

logger = logging.getLogger(config.LOGGER_NAME)

Expand Down Expand Up @@ -70,28 +70,69 @@ def compose(
db, source: str, destination: str, flags: int, memo: str, skip_validation: bool = False
):
if memo is None:
memo = b""
memo_bytes = b""

Check warning on line 73 in counterparty-core/counterpartycore/lib/messages/sweep.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/sweep.py#L73

Added line #L73 was not covered by tests
elif flags & FLAG_BINARY_MEMO:
memo = bytes.fromhex(memo)
memo_bytes = bytes.fromhex(memo)

Check warning on line 75 in counterparty-core/counterpartycore/lib/messages/sweep.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/sweep.py#L75

Added line #L75 was not covered by tests
else:
memo = memo.encode("utf-8")
memo = struct.pack(f">{len(memo)}s", memo)
memo_bytes = memo.encode("utf-8")
memo_bytes = struct.pack(f">{len(memo)}s", memo)

Check warning on line 78 in counterparty-core/counterpartycore/lib/messages/sweep.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/sweep.py#L77-L78

Added lines #L77 - L78 were not covered by tests

block_index = CurrentState().current_block_index()
problems, _total_fee = validate(db, source, destination, flags, memo, block_index)
problems, _total_fee = validate(db, source, destination, flags, memo_bytes, block_index)

Check warning on line 81 in counterparty-core/counterpartycore/lib/messages/sweep.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/sweep.py#L81

Added line #L81 was not covered by tests
if problems and not skip_validation:
raise exceptions.ComposeError(problems)

short_address_bytes = address.pack(destination)

data = messagetype.pack(ID)
data += struct.pack(FORMAT, short_address_bytes, flags)
data += memo
if protocol.enabled("taproot_support"):
data = struct.pack(config.SHORT_TXTYPE_FORMAT, ID)
data_content = b"|".join(

Check warning on line 89 in counterparty-core/counterpartycore/lib/messages/sweep.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/sweep.py#L87-L89

Added lines #L87 - L89 were not covered by tests
[
short_address_bytes,
helpers.int_to_bytes(flags),
memo_bytes,
]
)
logger.warning(f"data_content: {data_content}")

Check warning

Code scanning / pylint

Use lazy % formatting in logging functions. Warning

Use lazy % formatting in logging functions.
data += struct.pack(f">{len(data_content)}s", data_content)

Check warning on line 97 in counterparty-core/counterpartycore/lib/messages/sweep.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/sweep.py#L96-L97

Added lines #L96 - L97 were not covered by tests
else:
data = messagetype.pack(ID)
data += struct.pack(FORMAT, short_address_bytes, flags)
data += memo

Check warning on line 101 in counterparty-core/counterpartycore/lib/messages/sweep.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/sweep.py#L99-L101

Added lines #L99 - L101 were not covered by tests

return (source, [], data)


def new_unpack(message):
try:
data_content = struct.unpack(f">{len(message)}s", message)[0].split(b"|")
logger.warning(f"data_content unpack: {data_content}")

Check warning

Code scanning / pylint

Use lazy % formatting in logging functions. Warning

Use lazy % formatting in logging functions.
arg_count = len(data_content)
(

Check warning on line 111 in counterparty-core/counterpartycore/lib/messages/sweep.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/sweep.py#L107-L111

Added lines #L107 - L111 were not covered by tests
short_address_bytes,
flags_bytes,
) = data_content[0 : arg_count - 1]
# The memo is placed last to be able to contain `|`.
memo_bytes = b"|".join(data_content[arg_count - 1 :])

Check warning on line 116 in counterparty-core/counterpartycore/lib/messages/sweep.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/sweep.py#L116

Added line #L116 was not covered by tests

flags = helpers.bytes_to_int(flags_bytes)

Check warning on line 118 in counterparty-core/counterpartycore/lib/messages/sweep.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/sweep.py#L118

Added line #L118 was not covered by tests

full_address = address.unpack(short_address_bytes)

Check warning on line 120 in counterparty-core/counterpartycore/lib/messages/sweep.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/sweep.py#L120

Added line #L120 was not covered by tests

return {

Check warning on line 122 in counterparty-core/counterpartycore/lib/messages/sweep.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/sweep.py#L122

Added line #L122 was not covered by tests
"destination": full_address,
"flags": flags,
"memo": memo_bytes,
}
except Exception as e: # pylint: disable=broad-exception-caught
logger.error(f"sweep unpack error: {e}")

Check warning

Code scanning / pylint

Use lazy % formatting in logging functions. Warning

Use lazy % formatting in logging functions.
raise exceptions.UnpackError("could not unpack") from e

Check warning on line 129 in counterparty-core/counterpartycore/lib/messages/sweep.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/sweep.py#L127-L129

Added lines #L127 - L129 were not covered by tests


def unpack(message):
if protocol.enabled("taproot_support"):
return new_unpack(message)

Check warning on line 134 in counterparty-core/counterpartycore/lib/messages/sweep.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/sweep.py#L133-L134

Added lines #L133 - L134 were not covered by tests

try:
memo_bytes_length = len(message) - LENGTH
if memo_bytes_length < 0:
Expand All @@ -105,7 +146,6 @@ def unpack(message):
memo_bytes = None
elif not flags & FLAG_BINARY_MEMO:
memo_bytes = memo_bytes.decode("utf-8")

# unpack address
full_address = address.unpack(short_address_bytes)
except struct.error as e:
Expand All @@ -128,6 +168,7 @@ def parse(db, tx, message):
# Unpack message.
try:
unpacked = unpack(message)
logger.warning("unpacked: %s", unpacked)

Check warning on line 171 in counterparty-core/counterpartycore/lib/messages/sweep.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/sweep.py#L171

Added line #L171 was not covered by tests
destination, flags, memo_bytes = (
unpacked["destination"],
unpacked["flags"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import logging
import struct

from counterparty_rs import utils # pylint: disable=no-name-in-module

from counterpartycore.lib import config, exceptions, ledger
from counterpartycore.lib.messages.versions import send1
from counterpartycore.lib.parser import messagetype, protocol
Expand All @@ -21,6 +19,7 @@
def new_unpack(message):
try:
data_content = struct.unpack(f">{len(message)}s", message)[0].split(b"|")
logger.warning(f"data_content unpack: {data_content}")

Check warning

Code scanning / pylint

Use lazy % formatting in logging functions. Warning

Use lazy % formatting in logging functions.
arg_count = len(data_content)
(
asset_id_bytes,
Expand All @@ -37,7 +36,7 @@ def new_unpack(message):

quantity = helpers.bytes_to_int(quantity_bytes)

full_address = utils.unpack_address(short_address_bytes)
full_address = address.unpack(short_address_bytes)

return {
"asset": asset,
Expand All @@ -46,6 +45,7 @@ def new_unpack(message):
"memo": memo_bytes,
}
except Exception as e: # pylint: disable=broad-exception-caught
logger.error(f"enhanced send unpack error: {e}")

Check warning

Code scanning / pylint

Use lazy % formatting in logging functions. Warning

Use lazy % formatting in logging functions.
raise exceptions.UnpackError("could not unpack") from e


Expand Down Expand Up @@ -195,6 +195,7 @@ def compose(
memo_bytes,
]
)
logger.warning(f"data_content: {data_content}")

Check warning

Code scanning / pylint

Use lazy % formatting in logging functions. Warning

Use lazy % formatting in logging functions.
data += struct.pack(f">{len(data_content)}s", data_content)
else:
memo_bytes = struct.pack(f">{len(memo)}s", memo)
Expand Down
11 changes: 2 additions & 9 deletions counterparty-core/counterpartycore/lib/utils/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,8 @@ def pack(address):
"""
if enabled("taproot_support"):
try:
packed = bytes(utils.pack_address(address, config.NETWORK_NAME))
logger.warning(f"{address} packed: {packed}")
return packed
return bytes(utils.pack_address(address, config.NETWORK_NAME))
except Exception as e: # pylint: disable=broad-except # noqa: F841
logger.error(f"Error packing address: {e}")
raise exceptions.AddressError( # noqa: B904

Check warning on line 85 in counterparty-core/counterpartycore/lib/utils/address.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/utils/address.py#L84-L85

Added lines #L84 - L85 were not covered by tests
f"The address {address} is not a valid bitcoin address ({config.NETWORK_NAME})"
) from e
Expand Down Expand Up @@ -123,12 +120,8 @@ def unpack(short_address_bytes):
"""
if enabled("taproot_support"):
try:
logger.warning(f"short_address_bytes: {short_address_bytes}")
unpacked = utils.unpack_address(short_address_bytes, config.NETWORK_NAME)
logger.warning(f"unpacked: {unpacked}")
return unpacked
return utils.unpack_address(short_address_bytes, config.NETWORK_NAME)
except Exception as e: # pylint: disable=broad-except # noqa: F841
logger.error(f"Error unpacking address: {e}")
raise exceptions.DecodeError( # noqa: B904

Check warning on line 125 in counterparty-core/counterpartycore/lib/utils/address.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/utils/address.py#L124-L125

Added lines #L124 - L125 were not covered by tests
f"T{short_address_bytes} is not a valid packed bitcoin address ({config.NETWORK_NAME})"
) from e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ def test_taproot_scenario_2(empty_ledger_db, bitcoind_mock, defaults):
defaults,
defaults["addresses"][0],
defaults["p2tr_addresses"][0],
"",
"4330fdfa070eb72f65742a23fe9e68bbf2e67c55d28ad4b85ad11288f2eeb7ab",
)

0 comments on commit 0da25a5

Please sign in to comment.