Skip to content

Commit

Permalink
Add events field in transactions results
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Dec 4, 2024
1 parent ebb248d commit 83f4462
Show file tree
Hide file tree
Showing 7 changed files with 8,933 additions and 4,634 deletions.
6,382 changes: 3,986 additions & 2,396 deletions apiary.apib

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion counterparty-core/counterpartycore/lib/api/api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,11 @@ def handle_route(**kwargs):

next_cursor = None
result_count = None
table = None
if isinstance(result, queries.QueryResult):
next_cursor = result.next_cursor
result_count = result.result_count
table = result.table
result = result.result

result = clean_rowids_and_confirmed_fields(result)
Expand All @@ -394,7 +396,7 @@ def handle_route(**kwargs):
if verbose.lower() in ["true", "1"]:
with LedgerDBConnectionPool().connection() as ledger_db:
with StateDBConnectionPool().connection() as state_db:
result = inject_details(ledger_db, state_db, result)
result = inject_details(ledger_db, state_db, result, table)

return return_result(
200,
Expand Down
21 changes: 13 additions & 8 deletions counterparty-core/counterpartycore/lib/api/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,11 @@


class QueryResult:
def __init__(self, result, next_cursor, result_count=None):
def __init__(self, result, next_cursor, table, result_count=None):
self.result = result
self.next_cursor = next_cursor
self.result_count = result_count
self.table = table


def select_rows(
Expand Down Expand Up @@ -321,13 +322,13 @@ def select_rows(
break
row["params"] = json.loads(row["params"])

return QueryResult(result, next_cursor, result_count)
return QueryResult(result, next_cursor, table, result_count)


def select_row(db, table, where, select="*", group_by=""):
query_result = select_rows(db, table, where, limit=1, select=select, group_by=group_by)
if query_result.result:
return QueryResult(query_result.result[0], None, 1)
return QueryResult(query_result.result[0], None, table, 1)
return None


Expand Down Expand Up @@ -841,7 +842,7 @@ def get_events_by_addresses(
offset=offset,
select="message_index AS event_index, event, bindings AS params, tx_hash, block_index",
)
return QueryResult(result.result, events.next_cursor, events.result_count)
return QueryResult(result.result, events.next_cursor, "messages", events.result_count)


def get_all_mempool_events(
Expand Down Expand Up @@ -1817,7 +1818,7 @@ def utxos_with_balances(state_db, utxos: str):
if utxo not in result:
result[utxo] = False

return QueryResult(result, None, len(utxo_list))
return QueryResult(result, None, "balances", len(utxo_list))


def get_balances_by_addresses(
Expand Down Expand Up @@ -1907,7 +1908,7 @@ def get_balances_by_addresses(
)
result.append(current_balances)

return QueryResult(result, assets_result.next_cursor, assets_result.result_count)
return QueryResult(result, assets_result.next_cursor, "balances", assets_result.result_count)


def get_balances_by_address_and_asset(
Expand Down Expand Up @@ -2789,7 +2790,9 @@ def get_orders_by_two_assets(
else:
order["market_dir"] = "BUY"
order["market_price"] = divide(order["get_quantity"], order["give_quantity"])
return QueryResult(query_result.result, query_result.next_cursor, query_result.result_count)
return QueryResult(
query_result.result, query_result.next_cursor, "orders", query_result.result_count
)


def get_asset_holders(
Expand Down Expand Up @@ -2964,7 +2967,9 @@ def get_order_matches_by_two_assets(
else:
order["market_dir"] = "BUY"
order["market_price"] = divide(order["backward_quantity"], order["forward_quantity"])
return QueryResult(query_result.result, query_result.next_cursor, query_result.result_count)
return QueryResult(
query_result.result, query_result.next_cursor, "order_matches", query_result.result_count
)


def get_btcpays_by_order(
Expand Down
57 changes: 51 additions & 6 deletions counterparty-core/counterpartycore/lib/api/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,49 @@ def inject_unpacked_data(ledger_db, result_list):
return enriched_result_list


def inject_details(ledger_db, state_db, result):
def inject_transactions_events(ledger_db, state_db, result_list):
if len(result_list) == 0:
return result_list
if "tx_hash" not in result_list[0]:
return result_list

cursor = ledger_db.cursor()

transaction_hashes = [tx["tx_hash"] for tx in result_list]
exclude_events = [
"NEW_TRANSACTION",
"TRANSACTION_PARSED",
"CREDIT",
"DEBIT",
"INCREMENT_TRANSACTION_COUNT",
"NEW_TRANSACTION_OUTPUT",
]
sql = f"""
SELECT message_index AS event_index, event, bindings AS params, tx_hash, block_index

Check warning

Code scanning / Bandit

Possible SQL injection vector through string-based query construction. Warning

Possible SQL injection vector through string-based query construction.
FROM messages
WHERE tx_hash IN ({",".join("?" * len(transaction_hashes))})
AND event NOT IN ({",".join("?" * len(exclude_events))})
""" # noqa S608
events = cursor.execute(sql, transaction_hashes + exclude_events).fetchall()
for event in events:
event["params"] = json.loads(event["params"])

events = inject_dispensers(ledger_db, state_db, events)
events = inject_fiat_prices(ledger_db, events)
events = inject_issuances_and_block_times(ledger_db, state_db, events)
events = inject_normalized_quantities(events)

events_by_tx_hash = {}
for event in events:
if event["tx_hash"] not in events_by_tx_hash:
events_by_tx_hash[event["tx_hash"]] = []
events_by_tx_hash[event["tx_hash"]].append(event)
for result_item in result_list:
result_item["events"] = events_by_tx_hash.get(result_item["tx_hash"], [])
return result_list


def inject_details(ledger_db, state_db, result, table=None):
if isinstance(result, (int, str)):
return result
# let's work with a list
Expand All @@ -731,11 +773,14 @@ def inject_details(ledger_db, state_db, result):
result_list = [result]
result_is_dict = True

result_list = inject_dispensers(ledger_db, state_db, result_list)
result_list = inject_fiat_prices(ledger_db, result_list)
result_list = inject_unpacked_data(ledger_db, result_list)
result_list = inject_issuances_and_block_times(ledger_db, state_db, result_list)
result_list = inject_normalized_quantities(result_list)
if table == "transactions":
result_list = inject_transactions_events(ledger_db, state_db, result_list)
result_list = inject_unpacked_data(ledger_db, result_list)
else:
result_list = inject_dispensers(ledger_db, state_db, result_list)
result_list = inject_fiat_prices(ledger_db, result_list)
result_list = inject_issuances_and_block_times(ledger_db, state_db, result_list)
result_list = inject_normalized_quantities(result_list)

if result_is_dict:
result = result_list[0]
Expand Down
Loading

0 comments on commit 83f4462

Please sign in to comment.