Skip to content

Commit

Permalink
Merge pull request #2873 from CounterpartyXCP/composer2
Browse files Browse the repository at this point in the history
Composer V2
  • Loading branch information
ouziel-slama authored Jan 3, 2025
2 parents 3598fff + 35d1a46 commit 7d48ba5
Show file tree
Hide file tree
Showing 81 changed files with 23,915 additions and 26,349 deletions.
9,946 changes: 4,784 additions & 5,162 deletions apiary.apib

Large diffs are not rendered by default.

3 changes: 2 additions & 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 Expand Up @@ -596,6 +596,7 @@ def start(self, args):
if self.process is not None:
raise Exception("API Server is already running")
self.process = Process(
name="API",
target=run_api_server,
args=(vars(args), self.server_ready_value, self.stop_event, os.getpid()),
)
Expand Down
79 changes: 52 additions & 27 deletions counterparty-core/counterpartycore/lib/api/api_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,16 @@
import jsonrpc
from counterpartycore.lib import (
backend,
composer,
config,
deserialize,
exceptions,
gettxinfo,
ledger,
message_type,
script,
transaction,
transaction_helper,
util,
)
from counterpartycore.lib.api import compose as api_compose
from counterpartycore.lib.api import util as api_util
from counterpartycore.lib.api.api_watcher import STATE_DB_TABLES
from counterpartycore.lib.database import LedgerDBConnectionPool, StateDBConnectionPool
Expand Down Expand Up @@ -104,6 +102,28 @@
"transactions",
]

COMPOSABLE_TRANSACTIONS = [
"bet",
"broadcast",
"btcpay",
"burn",
"cancel",
"destroy",
"dispenser",
"dispense",
"dividend",
"issuance",
"versions.mpma",
"order",
"send",
"sweep",
"utxo",
"fairminter",
"fairmint",
"attach",
"detach",
"move",
]

JSON_RPC_ERROR_API_COMPOSE = -32001 # code to use for error composing transaction result

Expand Down Expand Up @@ -447,6 +467,20 @@ def gen_decorator(f):
return gen_decorator


def split_compose_params(**kwargs):
transaction_args = {}
common_args = {}
private_key_wif = None
for key, value in kwargs.items():
if key in composer.CONSTRUCT_PARAMS:
common_args[key] = value
elif key == "privkey":
private_key_wif = value
else:
transaction_args[key] = value
return transaction_args, common_args, private_key_wif


class APIStatusPoller(threading.Thread):
"""Perform regular checks on the state of the backend and the database."""

Expand Down Expand Up @@ -559,9 +593,7 @@ def sql(query, bindings=None):
# Generate dynamically create_{transaction} methods
def generate_create_method(tx):
def create_method(**kwargs):
transaction_args, common_args, private_key_wif = api_compose.split_compose_params(
**kwargs
)
transaction_args, common_args, private_key_wif = split_compose_params(**kwargs)
extended_tx_info = old_style_api = False
if "extended_tx_info" in common_args:
extended_tx_info = common_args["extended_tx_info"]
Expand All @@ -573,27 +605,24 @@ def create_method(**kwargs):
common_args.pop(v2_arg, None)
if "fee" in transaction_args and "exact_fee" not in common_args:
common_args["exact_fee"] = transaction_args.pop("fee")
common_args["accept_missing_params"] = True
try:
with LedgerDBConnectionPool().connection() as db:
transaction_info = transaction.compose_transaction(
transaction_info = composer.compose_transaction(
db,
name=tx,
params=transaction_args,
accept_missing_params=True,
**common_args,
tx,
transaction_args,
common_args,
)
if extended_tx_info:
transaction_info["tx_hex"] = transaction_info["unsigned_tx_hex"]
transaction_info["pretx_hex"] = transaction_info["unsigned_pretx_hex"]
del transaction_info["unsigned_tx_hex"]
del transaction_info["unsigned_pretx_hex"]
transaction_info["tx_hex"] = transaction_info["rawtransaction"]
del transaction_info["rawtransaction"]
return transaction_info
tx_hexes = list(
filter(
None,
[
transaction_info["unsigned_tx_hex"],
transaction_info["unsigned_pretx_hex"],
transaction_info["rawtransaction"],
],
)
) # filter out None
Expand Down Expand Up @@ -622,7 +651,7 @@ def create_method(**kwargs):

return create_method

for tx in api_compose.COMPOSABLE_TRANSACTIONS:
for tx in COMPOSABLE_TRANSACTIONS:
create_method = generate_create_method(tx)
create_method.__name__ = f"create_{tx}"
dispatcher.add_method(create_method)
Expand Down Expand Up @@ -987,9 +1016,7 @@ def unpack(data_hex):
@dispatcher.add_method
# TODO: Rename this method.
def search_pubkey(pubkeyhash, provided_pubkeys=None):
return transaction_helper.transaction_outputs.pubkeyhash_to_pubkey(
pubkeyhash, provided_pubkeys=provided_pubkeys
)
return backend.electrs.search_pubkey(pubkeyhash)

@dispatcher.add_method
def get_dispenser_info(tx_hash=None, tx_index=None):
Expand Down Expand Up @@ -1181,7 +1208,7 @@ def handle_rest(path_args, flask_request):
error = "No query_type provided."
return flask.Response(error, 400, mimetype="application/json")
# Check if message type or table name are valid.
if (compose and query_type not in api_compose.COMPOSABLE_TRANSACTIONS) or (
if (compose and query_type not in COMPOSABLE_TRANSACTIONS) or (
not compose and query_type not in API_TABLES
):
error = f'No such query type in supported queries: "{query_type}".'
Expand All @@ -1192,9 +1219,7 @@ def handle_rest(path_args, flask_request):
query_data = {}

if compose:
transaction_args, common_args, private_key_wif = api_compose.split_compose_params(
**extra_args
)
transaction_args, common_args, private_key_wif = split_compose_params(**extra_args)

# Must have some additional transaction arguments.
if not len(transaction_args):
Expand All @@ -1204,8 +1229,8 @@ def handle_rest(path_args, flask_request):
# Compose the transaction.
try:
with LedgerDBConnectionPool().connection() as db:
query_data, data = transaction.compose_transaction(
db, name=query_type, params=transaction_args, **common_args
query_data, data = composer.compose_transaction(
db, query_type, transaction_args, common_args
)
except (
script.AddressError,
Expand Down
Loading

0 comments on commit 7d48ba5

Please sign in to comment.