Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes and tweaks #2953

Merged
merged 9 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5,078 changes: 2,564 additions & 2,514 deletions apiary.apib

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions counterparty-core/counterpartycore/lib/api/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,11 +694,11 @@ def unpack(db, datahex: str, block_index: int = None):
# RPS
elif message_type_id == messages.rps.ID:
message_type_name = "rps"
message_data = messages.rps.unpack(message, return_dict=True)
message_data = {"error": "RPS messages decoding is not implemented"}
# RPS Resolve
elif message_type_id == messages.rpsresolve.ID:
message_type_name = "rpsresolve"
message_data = messages.rpsresolve.unpack(message, return_dict=True)
message_data = {"error": "RPS Resolve messages decoding is not implemented"}
# Sweep
elif message_type_id == messages.sweep.ID:
message_type_name = "sweep"
Expand Down
12 changes: 10 additions & 2 deletions counterparty-core/counterpartycore/lib/api/composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sys
import time
from collections import OrderedDict
from decimal import Decimal as D

from arc4 import ARC4
from bitcoinutils.keys import P2pkhAddress, P2shAddress, P2wpkhAddress, PublicKey
Expand Down Expand Up @@ -783,6 +784,7 @@ def prepare_inputs_and_change(db, source, outputs, unspent_list, construct_param
needed_fee = sat_per_vbyte * adjusted_vsize
if max_fee is not None:
needed_fee = min(needed_fee, max_fee)
needed_fee = int(needed_fee)
# if change is enough for needed fee, add change output and break
if change_amount > needed_fee:
change_amount = int(change_amount - needed_fee)
Expand Down Expand Up @@ -932,7 +934,7 @@ def check_transaction_sanity(tx_info, composed_tx, construct_params):
"encoding": (str, "auto", "The encoding method to use"),
"validate": (bool, True, "Validate the transaction"),
# fee parameters
"sat_per_vbyte": (int, None, "The fee per vbyte in satoshis"),
"sat_per_vbyte": (float, None, "The fee per vbyte in satoshis"),
"confirmation_target": (
int,
config.ESTIMATE_FEE_CONF_TARGET,
Expand Down Expand Up @@ -1020,11 +1022,17 @@ def check_transaction_sanity(tx_info, composed_tx, construct_params):
]


def fee_per_kb_to_sat_per_vbyte(fee_per_kb):
if fee_per_kb is None or fee_per_kb == 0:
return 0
return float(D(fee_per_kb) / D(1024))


def prepare_construct_params(construct_params):
cleaned_construct_params = construct_params.copy()
# copy deprecated parameters to new ones
for deprecated_param, new_param, copyer in [
("fee_per_kb", "sat_per_vbyte", lambda x: max(x // 1024, 1)),
("fee_per_kb", "sat_per_vbyte", fee_per_kb_to_sat_per_vbyte),
("fee_provided", "max_fee", lambda x: x),
("dust_return_pubkey", "mutlisig_pubkey", lambda x: x),
("return_psbt", "verbose", lambda x: x),
Expand Down
16 changes: 9 additions & 7 deletions counterparty-core/counterpartycore/lib/backend/bitcoind.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def get_json_response(response, retry=0):
except json.decoder.JSONDecodeError as e: # noqa: F841
if response.status_code == 200:
logger.warning(
f"Received invalid JSON with status 200 from Bitcoin Core: {response.text}. Retrying in 5 seconds..."
f"Received invalid JSON with status 200 from Bitcoin Core: {response.text}. Retrying in 5 seconds...",
stack_info=True,
)
time.sleep(5)
if retry < 5:
Expand Down Expand Up @@ -91,7 +92,8 @@ def rpc_call(payload, retry=0):
raise
except (Timeout, ReadTimeout, ConnectionError, ChunkedEncodingError):
logger.warning(
f"Could not connect to backend at `{clean_url_for_log(url)}`. (Attempt: {tries})"
f"Could not connect to backend at `{clean_url_for_log(url)}`. (Attempt: {tries})",
stack_info=True,
)
time.sleep(5)
except Exception as e:
Expand All @@ -116,7 +118,7 @@ def rpc_call(payload, retry=0):
warning_message = f"Error calling {payload}: {response_json['error']}. Sleeping for ten seconds and retrying."
if response_json["error"]["code"] == -5: # RPC_INVALID_ADDRESS_OR_KEY
warning_message += f" Is `txindex` enabled in {config.BTC_NAME} Core?"
logger.warning(warning_message)
logger.warning(warning_message, stack_info=True)
if should_retry():
# If Bitcoin Core takes more than `sys.getrecursionlimit() * 10 = 9970`
# seconds to start, this'll hit the maximum recursion depth limit.
Expand Down Expand Up @@ -374,11 +376,11 @@ def add_block_in_cache(block_index, block):
add_transaction_in_cache(transaction["tx_hash"], transaction)


def get_decoded_transaction(tx_hash, block_index=None):
def get_decoded_transaction(tx_hash, block_index=None, no_retry=False):
if tx_hash in TRANSACTIONS_CACHE:
return TRANSACTIONS_CACHE[tx_hash]

raw_tx = getrawtransaction(tx_hash)
raw_tx = getrawtransaction(tx_hash, no_retry=no_retry)
tx = deserialize.deserialize_tx(raw_tx, block_index=block_index)

add_transaction_in_cache(tx_hash, tx)
Expand Down Expand Up @@ -494,10 +496,10 @@ def list_unspent(source, allow_unconfirmed_inputs):
return []


def get_vin_info(vin):
def get_vin_info(vin, no_retry=False):
# Note: We don't know what block the `vin` is in, and the block might
# have been from a while ago, so this call may not hit the cache.
vin_ctx = get_decoded_transaction(vin["hash"])
vin_ctx = get_decoded_transaction(vin["hash"], no_retry=no_retry)

is_segwit = vin_ctx["segwit"]
vout = vin_ctx["vout"][vin["n"]]
Expand Down
7 changes: 6 additions & 1 deletion counterparty-core/counterpartycore/lib/parser/gettxinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,12 @@ def get_transaction_sources(decoded_tx):
outputs_value = 0

for vin in decoded_tx["vin"]: # Loop through inputs.
vout_value, script_pubkey, _is_segwit = backend.bitcoind.get_vin_info(vin)
try:
vout_value, script_pubkey, _is_segwit = backend.bitcoind.get_vin_info(
vin, no_retry=CurrentState().parsing_mempool()
)
except exceptions.BitcoindRPCError as e:
raise DecodeError("vin not found") from e

outputs_value += vout_value

Expand Down
4 changes: 2 additions & 2 deletions counterparty-core/counterpartycore/protocol_changes.json
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@
},
"testnet4": {
"1": {
"value": ">QQ??If"
"value": ">QQ???"
}
}
},
Expand Down Expand Up @@ -399,7 +399,7 @@
},
"testnet4": {
"1": {
"value": 26
"value": 19
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion counterparty-core/counterpartycore/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ def multisig_pubkeyhashes_to_pubkeys(address, provided_pubkeys=None):
pubkeys = [DEFAULT_PARAMS["pubkey"][pubkeyhash] for pubkeyhash in pubkeyhashes]
return pubkeys[0]

def mocked_getrawtransaction(tx_hash, verbose=False, block_index=None):
def mocked_getrawtransaction(tx_hash, verbose=False, block_index=None, no_retry=False):
return util_test.getrawtransaction(
rawtransactions_db, tx_hash, verbose=verbose, block_index=block_index
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def test_deserialize():
)


def mock_get_decoded_transaction(tx_hash):
def mock_get_decoded_transaction(tx_hash, no_retry=False):
txs = {
"094246c10d8b95f39662b92971588a205db77d89ffe0f21816733019a703cff9": "0100000001c47705b604b5b375fb43b6a7a632e20a7c10eb11d3202c00bd659e673d4d9396010000006a47304402204bc0847f52965c645e164078cfb5d743eb918c4fddaf4f592056b3470445e2c602202986c27c2f0f3b858b8fee94bf712338bc0ab8ff462edcea285a835143e10532012102e6dd23598e1d2428ecf7eb59c27fdfeeb7a27c26906e96dc1f3d5ebba6e54d08ffffffff02893000000000000017a9148760df63af4701313b244bf5ccd7479914843da18778cb0000000000001976a914533c940b158eae03f5bf71f1195d757c819c2e0c88ac00000000",
"05e7e9f59f155b28311a5e2860388783b839027b6529889de791351fe172752d": "020000000001016d72a3d323f82e76dcbf5fe9448a91ea9e68649e313d9a43822c0b27308a7b080200000017160014f6a785077f78695c12d51078ea7d9c10641f24acffffffff0208420000000000002251202525a906d3d870c6c00a2bfd63824c6597a4eddd8d24392f42ffbb2e6991fc5dcb8d04000000000017a914f54105af74fb10e70e899901b6ac4593ac20eea1870247304402205bd9f7e2ebe915532309548aad4e36f4b4feb856dab74f1b0e4df5292c0dbb4102202ca4d61fca54d08e2fd077c7e11154d2f271cf7102bb355c16dcb480d48dd57001210395c693bfc3a4d00e4380bec0d85871a1d0083618f8f01663199261d011e2a2bb00000000",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12522,7 +12522,7 @@
},
{
"name": "sat_per_vbyte",
"type": "int",
"type": "float",
"default": null,
"description": "The fee per vbyte in satoshis",
"required": false
Expand Down Expand Up @@ -12775,7 +12775,7 @@
},
{
"name": "sat_per_vbyte",
"type": "int",
"type": "float",
"default": null,
"description": "The fee per vbyte in satoshis",
"required": false
Expand Down Expand Up @@ -13010,7 +13010,7 @@
},
{
"name": "sat_per_vbyte",
"type": "int",
"type": "float",
"default": null,
"description": "The fee per vbyte in satoshis",
"required": false
Expand Down Expand Up @@ -13252,7 +13252,7 @@
},
{
"name": "sat_per_vbyte",
"type": "int",
"type": "float",
"default": null,
"description": "The fee per vbyte in satoshis",
"required": false
Expand Down Expand Up @@ -13487,7 +13487,7 @@
},
{
"name": "sat_per_vbyte",
"type": "int",
"type": "float",
"default": null,
"description": "The fee per vbyte in satoshis",
"required": false
Expand Down Expand Up @@ -13734,7 +13734,7 @@
},
{
"name": "sat_per_vbyte",
"type": "int",
"type": "float",
"default": null,
"description": "The fee per vbyte in satoshis",
"required": false
Expand Down Expand Up @@ -14007,7 +14007,7 @@
},
{
"name": "sat_per_vbyte",
"type": "int",
"type": "float",
"default": null,
"description": "The fee per vbyte in satoshis",
"required": false
Expand Down Expand Up @@ -14254,7 +14254,7 @@
},
{
"name": "sat_per_vbyte",
"type": "int",
"type": "float",
"default": null,
"description": "The fee per vbyte in satoshis",
"required": false
Expand Down Expand Up @@ -14555,7 +14555,7 @@
},
{
"name": "sat_per_vbyte",
"type": "int",
"type": "float",
"default": null,
"description": "The fee per vbyte in satoshis",
"required": false
Expand Down Expand Up @@ -14830,7 +14830,7 @@
},
{
"name": "sat_per_vbyte",
"type": "int",
"type": "float",
"default": null,
"description": "The fee per vbyte in satoshis",
"required": false
Expand Down Expand Up @@ -15095,7 +15095,7 @@
},
{
"name": "sat_per_vbyte",
"type": "int",
"type": "float",
"default": null,
"description": "The fee per vbyte in satoshis",
"required": false
Expand Down Expand Up @@ -15370,7 +15370,7 @@
},
{
"name": "sat_per_vbyte",
"type": "int",
"type": "float",
"default": null,
"description": "The fee per vbyte in satoshis",
"required": false
Expand Down Expand Up @@ -15617,7 +15617,7 @@
},
{
"name": "sat_per_vbyte",
"type": "int",
"type": "float",
"default": null,
"description": "The fee per vbyte in satoshis",
"required": false
Expand Down Expand Up @@ -15877,7 +15877,7 @@
},
{
"name": "sat_per_vbyte",
"type": "int",
"type": "float",
"default": null,
"description": "The fee per vbyte in satoshis",
"required": false
Expand Down Expand Up @@ -16224,7 +16224,7 @@
},
{
"name": "sat_per_vbyte",
"type": "int",
"type": "float",
"default": null,
"description": "The fee per vbyte in satoshis",
"required": false
Expand Down Expand Up @@ -16466,7 +16466,7 @@
},
{
"name": "sat_per_vbyte",
"type": "int",
"type": "float",
"default": null,
"description": "The fee per vbyte in satoshis",
"required": false
Expand Down Expand Up @@ -16721,7 +16721,7 @@
},
{
"name": "sat_per_vbyte",
"type": "int",
"type": "float",
"default": null,
"description": "The fee per vbyte in satoshis",
"required": false
Expand Down Expand Up @@ -16977,7 +16977,7 @@
},
{
"name": "sat_per_vbyte",
"type": "int",
"type": "float",
"default": null,
"description": "The fee per vbyte in satoshis",
"required": false
Expand Down Expand Up @@ -17219,7 +17219,7 @@
},
{
"name": "sat_per_vbyte",
"type": "int",
"type": "float",
"default": null,
"description": "The fee per vbyte in satoshis",
"required": false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,7 @@
),
"out": (
{
"sat_per_vbyte": 1,
"sat_per_vbyte": 1023 / 1024,
"max_fee": 666,
"mutlisig_pubkey": DEFAULT_PARAMS["pubkey"][ADDR[0]],
"verbose": True,
Expand Down
Loading
Loading