Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Dec 25, 2024
1 parent 5ab194a commit 466b77f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 46 deletions.
2 changes: 1 addition & 1 deletion counterparty-core/counterpartycore/lib/api/api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def prepare_args(route, **kwargs):
# inject args from request.args
for arg in route["args"]:
arg_name = arg["name"]
if arg_name in ["verbose"]:
if arg_name in ["verbose"] and "compose" not in route["function"].__name__:
continue
if arg_name in function_args:
continue
Expand Down
39 changes: 10 additions & 29 deletions counterparty-core/counterpartycore/lib/api/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

from counterpartycore.lib import (
backend,
composer,
config,
deserialize,
exceptions,
gas,
gettxinfo,
message_type,
messages,
transaction,
util,
)
from counterpartycore.lib.messages.utxo import ID as UTXO_ID
Expand Down Expand Up @@ -148,6 +148,11 @@
True,
"Validate the transaction",
),
"verbose": (
bool,
False,
"Verbose mode",
),
}


Expand All @@ -166,36 +171,12 @@ def split_compose_params(**kwargs):


def compose(db, name, params, **construct_args):
if name not in COMPOSABLE_TRANSACTIONS:
raise exceptions.TransactionError("Transaction type not composable.")
return_only_data = construct_args.pop("return_only_data", False)
return_psbt = construct_args.pop("return_psbt", False)
for v1_args in ["extended_tx_info", "old_style_api"]:
construct_args.pop(v1_args)
transaction_info = transaction.compose_transaction(
return composer.compose_transaction(
db,
name=name,
params=params,
**construct_args,
name,
params,
construct_args,
)
if return_only_data:
return {"data": transaction_info["data"]}
result = {
"params": params,
"name": name.split(".")[-1],
"data": transaction_info["data"],
"btc_in": transaction_info["btc_in"],
"btc_out": transaction_info["btc_out"],
"btc_change": transaction_info["btc_change"],
"btc_fee": transaction_info["btc_fee"],
}
if return_psbt:
result["psbt"] = backend.bitcoind.convert_to_psbt(transaction_info["unsigned_tx_hex"])
else:
result["rawtransaction"] = transaction_info["unsigned_tx_hex"]
if transaction_info.get("unsigned_pretx_hex"):
result["unsigned_pretx_hex"] = transaction_info["unsigned_pretx_hex"]
return result


def compose_bet(
Expand Down
35 changes: 21 additions & 14 deletions counterparty-core/counterpartycore/lib/composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

from bitcoinutils.keys import P2pkhAddress, P2wpkhAddress, PublicKey
from bitcoinutils.script import Script, b_to_h
from bitcoinutils.setup import setup
from bitcoinutils.transactions import Transaction, TxInput, TxOutput
from bitcoinutils.utils import is_address_bech32

from counterpartycore.lib import (
arc4,
Expand All @@ -27,10 +27,12 @@

def get_script(address):
try:
if is_address_bech32(address):
return P2wpkhAddress(address).to_script_pub_key()
return P2wpkhAddress(address).to_script_pub_key()
except ValueError:
pass
try:
return P2pkhAddress(address).to_script_pub_key()
except Exception as e:
except ValueError as e:
raise exceptions.ComposeError(f"Invalid address: {address}") from e


Expand Down Expand Up @@ -479,7 +481,7 @@ def ensure_utxo_is_first(utxo, unspent_list):
return new_unspent_list


def filter_utxos_with_balances(db, unspent_list, construct_params):
def filter_utxos_with_balances(db, source, unspent_list, construct_params):

Check warning

Code scanning / pylint

Either all return statements in a function should return an expression, or none of them should. Warning

Either all return statements in a function should return an expression, or none of them should.
use_utxos_with_balances = construct_params.get("use_utxos_with_balances", False)
if use_utxos_with_balances:
return unspent_list
Expand All @@ -488,6 +490,9 @@ def filter_utxos_with_balances(db, unspent_list, construct_params):
new_unspent_list = []
for utxo in unspent_list:
str_input = f"{utxo['txid']}:{utxo['vout']}"
if str_input == source:
new_unspent_list.append(utxo)
continue
utxo_balances = ledger.get_utxo_balances(db, str_input)
with_balances = len(utxo_balances) > 0 and any(

Check warning

Code scanning / pylint

Use a generator instead 'any(balance['quantity'] > 0 for balance in utxo_balances)'. Warning

Use a generator instead 'any(balance['quantity'] > 0 for balance in utxo_balances)'.
[balance["quantity"] > 0 for balance in utxo_balances]
Expand Down Expand Up @@ -534,10 +539,7 @@ def prepare_unspent_list(db, source, construct_params):
unspent_list = UTXOLocks().filter_unspent_list(unspent_list)

# exclude utxos with balances if needed
filter_utxos_with_balances(db, unspent_list, construct_params)

# sort unspent list by value
unspent_list = sorted(unspent_list, key=lambda x: x["value"], reverse=True)
filter_utxos_with_balances(db, source, unspent_list, construct_params)

# if source is an utxo, ensure it is first in the unspent list
if util.is_utxo_format(source):
Expand All @@ -551,6 +553,9 @@ def prepare_unspent_list(db, source, construct_params):
# complete unspent list with missing data (value or script_pub_key)
unspent_list = complete_unspent_list(unspent_list)

# sort unspent list by value
unspent_list = sorted(unspent_list, key=lambda x: x["value"], reverse=True)

return unspent_list


Expand Down Expand Up @@ -619,7 +624,7 @@ def prepare_inputs_and_change(db, source, outputs, unspent_list, construct_param
else:
change_address = source

outputs_total = sum(output["value"] for output in outputs)
outputs_total = sum(output.amount for output in outputs)

change_outputs = []
# try with one input and increase until the change is enough for the fee
Expand Down Expand Up @@ -749,6 +754,8 @@ def compose_data(db, name, params, accept_missing_params=False, skip_validation=


def compose_transaction(db, name, params, construct_params):
setup(config.NETWORK_NAME)

# prepare data
skip_validation = construct_params.get("validate", True)
source, destinations, data = compose_data(db, name, params, skip_validation=skip_validation)
Expand All @@ -766,15 +773,15 @@ def compose_transaction(db, name, params, construct_params):

# prepare inputs and change
inputs, btc_in, change_outputs = prepare_inputs_and_change(
source, outputs, unspent_list, construct_params
db, source, outputs, unspent_list, construct_params
)

if not construct_params.get("disable_utxo_lock", False):
UTXOLocks().lock_inputs(inputs)

# construct transaction
tx = Transaction(inputs, outputs + change_outputs)
unsigned_tx_hex = tx.serialize().hex()
unsigned_tx_hex = tx.serialize()
result = {
"rawtransaction": unsigned_tx_hex,
}
Expand All @@ -784,8 +791,8 @@ def compose_transaction(db, name, params, construct_params):
verbose = construct_params.get("return_psbt") # legacy

if verbose:
btc_out = sum(output.nValue for output in outputs)
btc_change = sum(change_output.nValue for change_output in change_outputs)
btc_out = sum(output.amount for output in outputs)
btc_change = sum(change_output.amount for change_output in change_outputs)
result = result | {
"btc_in": btc_in,
"btc_out": btc_out,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ def send_transaction(
if "exact_fee" not in params:
params["exact_fee"] = 10000 # fixed fee
# print("Inputs set:", params["inputs_set"])
params["verbose"] = True

result = self.compose(source, tx_name, params)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,14 @@ def control_result(
try:
assert result["result"] == expected_result
print(f"{item['title']}: " + colored("Success", "green"))
except AssertionError as e:
except AssertionError:
print(colored(f"Failed: {item['title']}", "red"))
expected_result_str = json.dumps(expected_result, indent=4, sort_keys=True)
got_result_str = json.dumps(result["result"], indent=4, sort_keys=True)
print(f"Expected: {expected_result_str}")
print(f"Got: {got_result_str}")
compare_strings(expected_result_str, got_result_str)
raise e from e
# raise e from e


def run_item(node, item, context):
Expand Down

0 comments on commit 466b77f

Please sign in to comment.