Skip to content

Commit 75c753a

Browse files
committed
Merge branch 'fairminterv2' into protocolchanges
2 parents bbe1592 + 3156cfd commit 75c753a

25 files changed

+14843
-499
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/astral-sh/ruff-pre-commit
3-
rev: v0.9.9
3+
rev: v0.9.10
44
hooks:
55
- id: ruff
66
args: [ --fix ]

apiary.apib

+7,641-21
Large diffs are not rendered by default.

counterparty-core/.coveragerc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[run]
2-
concurrency = multiprocessing
2+
concurrency = multiprocessing,thread
33
parallel = true
44
sigterm = true

counterparty-core/counterpartycore/lib/api/queries.py

+31-23
Original file line numberDiff line numberDiff line change
@@ -1946,6 +1946,7 @@ def get_balances_by_addresses(
19461946
state_db,
19471947
addresses: str,
19481948
type: BalanceType = "all", # pylint: disable=W0622
1949+
asset: str = None,
19491950
cursor: str = None,
19501951
limit: int = 100,
19511952
offset: int = None,
@@ -1955,32 +1956,36 @@ def get_balances_by_addresses(
19551956
Returns the balances of several addresses
19561957
:param str addresses: Comma separated list of addresses (e.g. $ADDRESS_1,$ADDRESS_2)
19571958
:param str type: The type of balances to return
1959+
:param str asset: The asset to return (e.g. XCP)
19581960
:param str cursor: The last index of the balances to return
19591961
:param int limit: The maximum number of balances to return (e.g. 5)
19601962
:param int offset: The number of lines to skip before returning results (overrides the `cursor` parameter)
19611963
:param str sort: The sort order of the balances to return (overrides the `cursor` parameter) (e.g. quantity:desc)
19621964
"""
1963-
where = [
1964-
{"address__in": addresses.split(","), "quantity__gt": 0},
1965-
{"utxo_address__in": addresses.split(","), "quantity__gt": 0},
1966-
]
1967-
if type == "utxo":
1968-
where.pop(0)
1969-
elif type == "address":
1970-
where.pop(1)
1971-
1972-
assets_result = select_rows(
1973-
state_db,
1974-
"balances",
1975-
select="DISTINCT asset AS asset",
1976-
where=where,
1977-
order="ASC",
1978-
cursor_field="asset",
1979-
last_cursor=cursor,
1980-
offset=offset,
1981-
limit=limit,
1982-
)
1983-
assets = [asset["asset"] for asset in assets_result.result]
1965+
if asset is None:
1966+
where = [
1967+
{"address__in": addresses.split(","), "quantity__gt": 0},
1968+
{"utxo_address__in": addresses.split(","), "quantity__gt": 0},
1969+
]
1970+
if type == "utxo":
1971+
where.pop(0)
1972+
elif type == "address":
1973+
where.pop(1)
1974+
1975+
assets_result = select_rows(
1976+
state_db,
1977+
"balances",
1978+
select="DISTINCT asset AS asset",
1979+
where=where,
1980+
order="ASC",
1981+
cursor_field="asset",
1982+
last_cursor=cursor,
1983+
offset=offset,
1984+
limit=limit,
1985+
)
1986+
assets = [asset["asset"] for asset in assets_result.result]
1987+
else:
1988+
assets = [asset]
19841989

19851990
where = [
19861991
{"address__in": addresses.split(","), "asset__in": assets, "quantity__gt": 0},
@@ -2028,8 +2033,11 @@ def get_balances_by_addresses(
20282033
}
20292034
)
20302035
result.append(current_balances)
2031-
2032-
return QueryResult(result, assets_result.next_cursor, "balances", assets_result.result_count)
2036+
if asset is None:
2037+
return QueryResult(
2038+
result, assets_result.next_cursor, "balances", assets_result.result_count
2039+
)
2040+
return QueryResult(result, None, "balances", 1)
20332041

20342042

20352043
def get_balances_by_address_and_asset(

counterparty-core/counterpartycore/lib/backend/bitcoind.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def clean_url_for_log(url):
4242

4343
# for testing
4444
def should_retry():
45-
if CurrentState().ledger_state() == "Stopping":
45+
if CurrentState().stopping():
4646
return False
4747
return True
4848

counterparty-core/counterpartycore/lib/cli/server.py

+15-27
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import _thread
22
import binascii
3-
import cProfile
43
import decimal
54
import logging
65
import multiprocessing
76
import os
8-
import pstats
97
import threading
108
import time
119

@@ -20,10 +18,11 @@
2018
)
2119
from counterpartycore.lib.api import apiserver as api_v2
2220
from counterpartycore.lib.api import apiv1, dbbuilder
21+
from counterpartycore.lib.backend import rsfetcher
2322
from counterpartycore.lib.cli import bootstrap, log
2423
from counterpartycore.lib.ledger.backendheight import BackendHeight
2524
from counterpartycore.lib.ledger.currentstate import CurrentState
26-
from counterpartycore.lib.monitors import slack
25+
from counterpartycore.lib.monitors import profiler, slack
2726
from counterpartycore.lib.parser import blocks, check, follow
2827
from counterpartycore.lib.utils import database, helpers
2928

@@ -87,7 +86,7 @@ def __init__(self, args, log_stream=None, stop_when_ready=False):
8786
self.api_stop_event = None
8887
self.backend_height_thread = None
8988
self.log_stream = log_stream
90-
self.profiler = None
89+
self.periodic_profiler = None
9190
self.stop_when_ready = stop_when_ready
9291
self.stopped = False
9392

@@ -174,14 +173,12 @@ def run_server(self):
174173

175174
# Catch Up
176175
if config.PROFILE:
177-
logger.info("Starting profiler before catchup...")
178-
self.profiler = cProfile.Profile()
179-
self.profiler.enable()
180-
blocks.catch_up(self.db)
181-
logger.info("Stopping profiler after catchup...")
182-
self.profiler.disable()
183-
else:
184-
blocks.catch_up(self.db)
176+
self.periodic_profiler = profiler.PeriodicProfilerThread(
177+
interval_minutes=config.PROFILE_INTERVAL_MINUTES
178+
)
179+
self.periodic_profiler.start()
180+
181+
blocks.catch_up(self.db)
185182

186183
if self.stop_when_ready:
187184
slack.trigger_webhook()
@@ -206,8 +203,9 @@ def stop(self):
206203
if self.stopped:
207204
return
208205
logger.info("Shutting down...")
209-
if self.db:
210-
CurrentState().set_ledger_state(self.db, "Stopping")
206+
207+
CurrentState().set_stopping()
208+
rsfetcher.RSFetcher().stop()
211209

212210
# Ensure all threads are stopped
213211
if self.follower_daemon:
@@ -225,19 +223,9 @@ def stop(self):
225223
if self.apiserver_v2:
226224
self.apiserver_v2.stop()
227225

228-
# Dump profiling data
229-
if config.PROFILE and self.profiler:
230-
logger.info("Dumping profiling data...")
231-
profile_path = os.path.join(config.CACHE_DIR, "catchup.prof")
232-
try:
233-
self.profiler.dump_stats(profile_path)
234-
stats = pstats.Stats(self.profiler).sort_stats("cumtime")
235-
stats.print_stats(20)
236-
stats = pstats.Stats(self.profiler).sort_stats("tottime")
237-
stats.print_stats(20)
238-
except Exception as e: # pylint: disable=broad-except
239-
logger.error("Error dumping profiler stats: %s", e)
240-
self.profiler = None
226+
if self.periodic_profiler:
227+
logger.info("Stopping periodic profiler...")
228+
self.periodic_profiler.stop()
241229

242230
self.stopped = True
243231
logger.info("Shutdown complete.")

counterparty-core/counterpartycore/lib/config.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
# Semantic Version
10-
__version__ = "10.10.1" # for hatch
10+
__version__ = "11.0.0-alpha.1" # for hatch
1111
VERSION_STRING = __version__
1212
version = VERSION_STRING.split("-", maxsplit=1)[0].split(".")
1313
VERSION_MAJOR = int(version[0])
@@ -29,6 +29,7 @@
2929
"10.8.0": [("rollback", 871780)],
3030
"10.9.0-rc.1": [("rollback", 871780)],
3131
"10.9.0": [("rollback", 871780)],
32+
"11.0.0-alpha.1": [("reparse", 0)],
3233
},
3334
"testnet3": {
3435
"10.3.0": [("reparse", 0)],
@@ -39,9 +40,11 @@
3940
"10.9.0-rc.1": [("rollback", 3522632)],
4041
"10.9.0": [("rollback", 3522632)],
4142
"10.10.0": [("rollback", 3522632)],
43+
"11.0.0-alpha.1": [("reparse", 0)],
4244
},
4345
"testnet4": {
4446
"10.10.0": [("rollback", 64492)],
47+
"11.0.0-alpha.1": [("reparse", 0)],
4548
},
4649
}
4750

@@ -250,3 +253,5 @@
250253
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
251254
LEDGER_DB_MIGRATIONS_DIR = os.path.join(CURRENT_DIR, "ledger", "migrations")
252255
STATE_DB_MIGRATIONS_DIR = os.path.join(CURRENT_DIR, "api", "migrations")
256+
257+
PROFILE_INTERVAL_MINUTES = 15

counterparty-core/counterpartycore/lib/ledger/currentstate.py

+6
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ def ledger_state(self):
7575
with LedgerDBConnectionPool().connection() as ledger_db:
7676
return get_config_value(ledger_db, "LEDGER_STATE") or "Starting"
7777

78+
def set_stopping(self):
79+
self.state["STOPPING"] = True
80+
81+
def stopping(self):
82+
return self.state.get("STOPPING", False)
83+
7884

7985
class ConsensusHashBuilder(metaclass=helpers.SingletonMeta):
8086
def __init__(self):

counterparty-core/counterpartycore/lib/ledger/events.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,7 @@ def debit(db, address, asset, quantity, tx_index, action=None, event=None):
269269
}
270270
insert_record(db, "debits", bindings, "DEBIT")
271271

272-
ConsensusHashBuilder().append_to_block_ledger(f"{block_index}{address}{asset}{quantity}")
273-
ConsensusHashBuilder().append_to_block_migration(
272+
ConsensusHashBuilder().append_to_block_ledger(
274273
f"{block_index}{str(address)[0:36]}{asset}{quantity}"
275274
)
276275
return utxo_address
@@ -349,8 +348,7 @@ def credit(db, address, asset, quantity, tx_index, action=None, event=None):
349348
}
350349
insert_record(db, "credits", bindings, "CREDIT")
351350

352-
ConsensusHashBuilder().append_to_block_ledger(f"{block_index}{address}{asset}{quantity}")
353-
ConsensusHashBuilder().append_to_block_migration(
351+
ConsensusHashBuilder().append_to_block_ledger(
354352
f"{block_index}{str(address)[0:36]}{asset}{quantity}"
355353
)
356354

0 commit comments

Comments
 (0)