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

[v10.10.0] Code cleaning #2937

Merged
merged 40 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
7fe6b46
split vector file
Ouziel Jan 13, 2025
17439b5
Move python tests from counterparty-rs to counterparty-core
Ouziel Jan 13, 2025
eb25e84
introduce tools module
Ouziel Jan 13, 2025
34b2465
Remove counterparty-wallet
Ouziel Jan 13, 2025
fc7d0d1
continue to move files
Ouziel Jan 13, 2025
0682954
rename mpma_util to utils
Ouziel Jan 13, 2025
d164bab
move address.py in messages.utils
Ouziel Jan 13, 2025
cc27c6a
move more files
Ouziel Jan 13, 2025
91abaf2
move more files
Ouziel Jan 13, 2025
f3f44c6
fix cli
Ouziel Jan 14, 2025
a2b4620
Clean script.py; Move more files and functions
Ouziel Jan 14, 2025
12ba3f1
Clean script.py; Move more files and functions; ruff
Ouziel Jan 14, 2025
a7f92ef
more cleaning
Ouziel Jan 14, 2025
d860824
more cleaning; add utils module
Ouziel Jan 14, 2025
c23bd6f
more cleaning
Ouziel Jan 14, 2025
42272f9
more cleaning
Ouziel Jan 14, 2025
cf47698
util.py almost clean
Ouziel Jan 14, 2025
eea083b
clean util.py
Ouziel Jan 14, 2025
d75e8dc
more cleaning
Ouziel Jan 15, 2025
cbc0580
remove _ from files names
Ouziel Jan 15, 2025
57d2b67
clean api/util.py
Ouziel Jan 15, 2025
4303533
Merge branch 'develop' into cleaning
Ouziel Jan 15, 2025
71ccee8
Kill util.CURRENT_BLOCK_INDEX
Ouziel Jan 15, 2025
0befea3
try to kill util.CURRENT_BACKEND_HEIGHT
Ouziel Jan 15, 2025
3546adb
fix tests; finish to kill CURRENT_BACKEND_HEIGHT
Ouziel Jan 15, 2025
a3a2bdf
kill util.CURRENT_BLOCK_TIME; fixes
Ouziel Jan 15, 2025
c43fa92
fixe tests
Ouziel Jan 16, 2025
c019679
No more globals
Ouziel Jan 16, 2025
1652446
use yoyo migrations to initialize Ledger DB
Ouziel Jan 16, 2025
85eb071
Remove globals BLOCK_LEDGER and BLOCK_JOURNAL
Ouziel Jan 16, 2025
21ee60f
clean custom exceptions
Ouziel Jan 16, 2025
3d29a7d
fixes
Ouziel Jan 16, 2025
288b1b2
clean ledger import
Ouziel Jan 16, 2025
41ebe8f
fix circular import
Ouziel Jan 16, 2025
95221bc
Revert "clean ledger import"
Ouziel Jan 16, 2025
0207d6a
fix import, start to split ledger.py
Ouziel Jan 16, 2025
8936396
progress in ledger.py splitting
Ouziel Jan 16, 2025
85a5807
progress in ledger.py cleaning
Ouziel Jan 16, 2025
ce96d22
finish to clean ledger.py
Ouziel Jan 16, 2025
6dffdfe
add release notes
Ouziel Jan 16, 2025
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
1 change: 1 addition & 0 deletions counterparty-core/counterpartycore/lib/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import cli # noqa F401
73 changes: 0 additions & 73 deletions counterparty-core/counterpartycore/lib/address.py

This file was deleted.

20 changes: 10 additions & 10 deletions counterparty-core/counterpartycore/lib/api/api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@
import flask
import requests
from bitcoin.wallet import CBitcoinAddressError
from counterpartycore import server
from flask import Flask, request
from flask_httpauth import HTTPBasicAuth
from sentry_sdk import capture_exception
from sentry_sdk import configure_scope as configure_sentry_scope
from sentry_sdk import start_span as start_sentry_span

from counterpartycore.lib import (
check,
config,
database,
exceptions,
ledger,
script,
sentry,
util,
)
from counterpartycore.lib.api import api_watcher, dbbuilder, queries, wsgi
Expand All @@ -31,12 +33,10 @@
inject_details,
to_json,
)
from counterpartycore.lib.cli import server
from counterpartycore.lib.database import LedgerDBConnectionPool, StateDBConnectionPool
from flask import Flask, request
from flask_httpauth import HTTPBasicAuth
from sentry_sdk import capture_exception
from sentry_sdk import configure_scope as configure_sentry_scope
from sentry_sdk import start_span as start_sentry_span
from counterpartycore.lib.parser import check
from counterpartycore.lib.tools import sentry

multiprocessing.set_start_method("spawn", force=True)

Expand Down Expand Up @@ -358,7 +358,7 @@ def handle_route(**kwargs):
exceptions.ComposeError,
exceptions.UnpackError,
CBitcoinAddressError,
script.AddressError,
exceptions.AddressError,
exceptions.ElectrsError,
OverflowError,
) as e:
Expand Down
40 changes: 19 additions & 21 deletions counterparty-core/counterpartycore/lib/api/api_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@
import threading
import time

import counterpartycore.lib.sentry as sentry # noqa: F401
import flask
import jsonrpc
from flask import request
from flask_httpauth import HTTPBasicAuth
from jsonrpc import dispatcher
from jsonrpc.exceptions import JSONRPCDispatchException
from sentry_sdk import configure_scope as configure_sentry_scope
from werkzeug.serving import make_server
from xmltodict import unparse as serialize_to_xml

from counterpartycore.lib import (
backend,
composer,
config,
deserialize,
exceptions,
gettxinfo,
ledger,
message_type,
script,
util,
)
from counterpartycore.lib.api import util as api_util
Expand All @@ -50,18 +53,13 @@
sweep, # noqa: F401
)
from counterpartycore.lib.messages.versions import enhanced_send # noqa: E402
from counterpartycore.lib.telemetry.util import ( # noqa: E402
from counterpartycore.lib.parser import deserialize, gettxinfo, message_type
from counterpartycore.lib.tools import sentry
from counterpartycore.lib.tools.telemetry.util import ( # noqa: E402
get_uptime,
is_docker,
is_force_enabled,
)
from flask import request
from flask_httpauth import HTTPBasicAuth
from jsonrpc import dispatcher
from jsonrpc.exceptions import JSONRPCDispatchException
from sentry_sdk import configure_scope as configure_sentry_scope
from werkzeug.serving import make_server
from xmltodict import unparse as serialize_to_xml

D = decimal.Decimal

Expand Down Expand Up @@ -204,7 +202,7 @@ def get_rows(
def value_to_marker(value):
# if value is an array place holder is (?,?,?,..)
if isinstance(value, list):
return f"""({','.join(['?' for e in range(0, len(value))])})"""
return f"""({",".join(["?" for e in range(0, len(value))])})"""
else:
return """?"""

Expand Down Expand Up @@ -290,10 +288,10 @@ def value_to_marker(value):
for filter_ in filters:
case_sensitive = False if "case_sensitive" not in filter_ else filter_["case_sensitive"]
if filter_["op"] == "LIKE" and case_sensitive == False: # noqa: E712
filter_["field"] = f"""UPPER({filter_['field']})"""
filter_["field"] = f"""UPPER({filter_["field"]})"""
filter_["value"] = filter_["value"].upper()
marker = value_to_marker(filter_["value"])
conditions.append(f"""{filter_['field']} {filter_['op']} {marker}""")
conditions.append(f"""{filter_["field"]} {filter_["op"]} {marker}""")
if isinstance(filter_["value"], list):
bindings += filter_["value"]
else:
Expand Down Expand Up @@ -334,10 +332,10 @@ def value_to_marker(value):
statement += """ WHERE"""
all_conditions = []
if len(conditions) > 0:
all_conditions.append(f"""({f' {filterop.upper()} '.join(conditions)})""")
all_conditions.append(f"""({f" {filterop.upper()} ".join(conditions)})""")
if len(more_conditions) > 0:
all_conditions.append(f"""({' AND '.join(more_conditions)})""")
statement += f""" {' AND '.join(all_conditions)}"""
all_conditions.append(f"""({" AND ".join(more_conditions)})""")
statement += f""" {" AND ".join(all_conditions)}"""

# ORDER BY
if order_by != None: # noqa: E711
Expand Down Expand Up @@ -637,7 +635,7 @@ def create_method(**kwargs):
return tx_hexes
except (
TypeError,
script.AddressError,
exceptions.AddressError,
exceptions.ComposeError,
exceptions.TransactionError,
exceptions.BalanceError,
Expand Down Expand Up @@ -1233,7 +1231,7 @@ def handle_rest(path_args, flask_request):
db, query_type, transaction_args, common_args
)
except (
script.AddressError,
exceptions.AddressError,
exceptions.ComposeError,
exceptions.TransactionError,
exceptions.BalanceError,
Expand Down
6 changes: 2 additions & 4 deletions counterparty-core/counterpartycore/lib/api/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
backend,
composer,
config,
deserialize,
exceptions,
gas,
gettxinfo,
message_type,
messages,
util,
)
from counterpartycore.lib.messages import gas
from counterpartycore.lib.messages.attach import ID as UTXO_ID
from counterpartycore.lib.parser import deserialize, gettxinfo, message_type

D = decimal.Decimal

Expand Down
4 changes: 3 additions & 1 deletion counterparty-core/counterpartycore/lib/api/dbbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import sys
import time

from counterpartycore.lib import config, log
from yoyo import get_backend, read_migrations
from yoyo.exceptions import LockTimeout
from yoyo.migrations import topological_sort

from counterpartycore.lib import config
from counterpartycore.lib.cli import log

logger = logging.getLogger(config.LOGGER_NAME)

CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
Expand Down
3 changes: 2 additions & 1 deletion counterparty-core/counterpartycore/lib/api/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import typing
from typing import Literal

from counterpartycore.lib.api.util import divide
from sentry_sdk import start_span as start_sentry_span

from counterpartycore.lib.api.util import divide

OrderStatus = Literal["all", "open", "expired", "filled", "cancelled"]
OrderMatchesStatus = Literal["all", "pending", "completed", "expired"]
BetStatus = Literal["cancelled", "dropped", "expired", "filled", "open"]
Expand Down
3 changes: 2 additions & 1 deletion counterparty-core/counterpartycore/lib/api/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import flask
import requests
import werkzeug
from docstring_parser import parse as parse_docstring

from counterpartycore.lib import (
backend,
composer,
Expand All @@ -20,7 +22,6 @@
util,
)
from counterpartycore.lib.api import compose
from docstring_parser import parse as parse_docstring

D = decimal.Decimal
logger = logging.getLogger(config.LOGGER_NAME)
Expand Down
8 changes: 5 additions & 3 deletions counterparty-core/counterpartycore/lib/api/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
import gunicorn.app.base
import waitress
import waitress.server
from counterpartycore.lib import config, database, ledger, log, util
from counterpartycore.lib.api import api_watcher
from counterpartycore.lib.api.util import BackendHeight
from gunicorn import util as gunicorn_util
from gunicorn.arbiter import Arbiter
from gunicorn.errors import AppImportError
from werkzeug.serving import make_server

from counterpartycore.lib import config, database, ledger, util
from counterpartycore.lib.api import api_watcher
from counterpartycore.lib.api.util import BackendHeight
from counterpartycore.lib.cli import log

multiprocessing.set_start_method("spawn", force=True)

logger = logging.getLogger(config.LOGGER_NAME)
Expand Down
9 changes: 0 additions & 9 deletions counterparty-core/counterpartycore/lib/arc4.py

This file was deleted.

40 changes: 36 additions & 4 deletions counterparty-core/counterpartycore/lib/backend/bitcoind.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
from threading import current_thread

import requests
from bitcoinutils.keys import PublicKey
from requests.exceptions import ChunkedEncodingError, ConnectionError, ReadTimeout, Timeout

from counterpartycore.lib import config, deserialize, exceptions, script, util
from counterpartycore.lib import config, exceptions, util
from counterpartycore.lib.parser import deserialize
from counterpartycore.lib.util import ib2h

logger = logging.getLogger(config.LOGGER_NAME)
Expand Down Expand Up @@ -412,7 +414,10 @@ def search_pubkey_in_transactions(pubkeyhash, tx_hashes):
# catch unhexlify errs for when txinwitness[1] isn't a witness program (eg; for P2W)
try:
pubkey = vin["txinwitness"][1]
if pubkeyhash == script.pubkey_to_p2whash2(pubkey):
if (
pubkeyhash
== PublicKey.from_hex(pubkey).get_segwit_address().to_string()
):
return pubkey
except binascii.Error:
pass
Expand All @@ -423,7 +428,15 @@ def search_pubkey_in_transactions(pubkeyhash, tx_hashes):
# catch unhexlify errs for when asm[1] isn't a pubkey (eg; for P2SH)
try:
pubkey = asm[3]
if pubkeyhash == script.pubkey_to_pubkeyhash(util.unhexlify(pubkey)):
if (
pubkeyhash
== PublicKey.from_hex(pubkey).get_address(compressed=False).to_string()
):
return pubkey
if (
pubkeyhash
== PublicKey.from_hex(pubkey).get_address(compressed=True).to_string()
):
return pubkey
except binascii.Error:
pass
Expand All @@ -432,7 +445,15 @@ def search_pubkey_in_transactions(pubkeyhash, tx_hashes):
if len(asm) == 3: # p2pk
try:
pubkey = asm[1]
if pubkeyhash == script.pubkey_to_pubkeyhash(util.unhexlify(pubkey)):
if (
pubkeyhash
== PublicKey.from_hex(pubkey).get_address(compressed=False).to_string()
):
return pubkey
if (
pubkeyhash
== PublicKey.from_hex(pubkey).get_address(compressed=True).to_string()
):
return pubkey
except binascii.Error:
pass
Expand Down Expand Up @@ -462,3 +483,14 @@ def list_unspent(source, allow_unconfirmed_inputs):
return unspent_list

return []


def get_vin_info(vin):
# 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"])

is_segwit = vin_ctx["segwit"]
vout = vin_ctx["vout"][vin["n"]]

return vout["value"], vout["script_pub_key"], is_segwit
Loading
Loading