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

Kill Addrindexrs #2864

Merged
merged 13 commits into from
Dec 19, 2024
12 changes: 6 additions & 6 deletions counterparty-core/counterpartycore/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,6 @@ def float_range_checker(arg):
"help": "pubkey to receive dust when multisig encoding is used for P2SH source (default: none)"
},
],
[
("--indexd-connect",),
{"default": "localhost", "help": "the hostname or IP of the indexd server"},
],
[("--indexd-port",), {"type": int, "help": "the indexd server port to connect to"}],
[
("--rpc-host",),
{
Expand Down Expand Up @@ -365,6 +360,12 @@ def float_range_checker(arg):
},
],
[("--bootstrap-url",), {"type": str, "help": "the URL of the bootstrap snapshot to use"}],
[
("--electr-url",),
{
"help": "the URL of the Electrum server",
},
],
]


Expand All @@ -384,7 +385,6 @@ def welcome_message(action, server_configfile):
pass_str = f":{urlencode(config.BACKEND_PASSWORD)}@"
cleaned_backend_url = config.BACKEND_URL.replace(pass_str, ":*****@")
cprint(f"Bitcoin Core: {cleaned_backend_url}", "light_grey")
cprint(f"AddrIndexRs: {config.INDEXD_URL}", "light_grey")

api_url = "http://"
if config.API_USER and config.API_PASSWORD:
Expand Down
1 change: 1 addition & 0 deletions counterparty-core/counterpartycore/lib/api/api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ def handle_route(**kwargs):
exceptions.UnpackError,
CBitcoinAddressError,
script.AddressError,
exceptions.ElectrError,
) as e:
return return_result(400, error=str(e), start_time=start_time, query_args=query_args)
except Exception as e:
Expand Down
34 changes: 12 additions & 22 deletions counterparty-core/counterpartycore/lib/api/api_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
)
from counterpartycore.lib.messages.versions import enhanced_send # noqa: E402
from counterpartycore.lib.telemetry.util import ( # noqa: E402
get_addrindexrs_version,
get_uptime,
is_docker,
is_force_enabled,
Expand Down Expand Up @@ -132,7 +131,7 @@
# check backend index
blocks_behind = backend.bitcoind.get_blocks_behind()
if blocks_behind > 5:
raise BackendError(f"Indexd is running {blocks_behind} blocks behind.")
raise BackendError(f"Bitcoind is running {blocks_behind} blocks behind.")

logger.debug("API Status Poller - Backend state check passed.")

Expand Down Expand Up @@ -820,22 +819,20 @@
last_message = None

try:
indexd_blocks_behind = backend.bitcoind.get_blocks_behind()
bitcoind_blocks_behind = backend.bitcoind.get_blocks_behind()
except: # noqa: E722
indexd_blocks_behind = latest_block_index if latest_block_index > 0 else 999999
indexd_caught_up = indexd_blocks_behind <= 1
bitcoind_blocks_behind = latest_block_index if latest_block_index > 0 else 999999
bitcoind_caught_up = bitcoind_blocks_behind <= 1

server_ready = caught_up and indexd_caught_up

addrindexrs_version = get_addrindexrs_version().split(".")
server_ready = caught_up and bitcoind_caught_up

return {
"server_ready": server_ready,
"db_caught_up": caught_up,
"bitcoin_block_count": latest_block_index,
"last_block": last_block,
"indexd_caught_up": indexd_caught_up,
"indexd_blocks_behind": indexd_blocks_behind,
"bitcoind_caught_up": bitcoind_caught_up,
"bitcoind_blocks_behind": bitcoind_blocks_behind,
"last_message_index": (last_message["message_index"] if last_message else -1),
"api_limit_rows": config.API_LIMIT_ROWS,
"running_testnet": config.TESTNET,
Expand All @@ -844,9 +841,6 @@
"version_major": config.VERSION_MAJOR,
"version_minor": config.VERSION_MINOR,
"version_revision": config.VERSION_REVISION,
"addrindexrs_version_major": int(addrindexrs_version[0]),
"addrindexrs_version_minor": int(addrindexrs_version[1]),
"addrindexrs_version_revision": int(addrindexrs_version[2]),
"uptime": int(get_uptime()),
"dockerized": is_docker(),
"force_enabled": is_force_enabled(),
Expand Down Expand Up @@ -925,17 +919,13 @@

@dispatcher.add_method
def search_raw_transactions(address, unconfirmed=True, only_tx_hashes=False):
return backend.addrindexrs.search_raw_transactions(
return backend.electr.get_history(
Fixed Show fixed Hide fixed
address, unconfirmed=unconfirmed, only_tx_hashes=only_tx_hashes
)

@dispatcher.add_method
def get_oldest_tx(address):
return backend.addrindexrs.get_oldest_tx(address, block_index=util.CURRENT_BLOCK_INDEX)

@dispatcher.add_method
def get_unspent_txouts(address, unconfirmed=False, unspent_tx_hash=None, order_by=None):
results = backend.addrindexrs.get_unspent_txouts(
results = backend.electr.get_utxos(
address, unconfirmed=unconfirmed, unspent_tx_hash=unspent_tx_hash
)
if order_by is None:
Expand All @@ -953,9 +943,9 @@
return backend.bitcoind.getrawtransaction(tx_hash, verbose=verbose)

@dispatcher.add_method
def getrawtransaction_batch(txhash_list, verbose=False, skip_missing=False):
return backend.addrindexrs.getrawtransaction_batch(
txhash_list, verbose=verbose, skip_missing=skip_missing
def getrawtransaction_batch(txhash_list, verbose=False):
return backend.bitcoind.getrawtransaction_batch(
txhash_list, verbose=verbose, return_dict=True
)

@dispatcher.add_method
Expand Down
9 changes: 4 additions & 5 deletions counterparty-core/counterpartycore/lib/api/routes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from counterpartycore.lib.api import compose, queries, util
from counterpartycore.lib.backend import addrindexrs, bitcoind
from counterpartycore.lib.backend import bitcoind, electr


def get_routes():
Expand Down Expand Up @@ -172,10 +172,9 @@ def get_routes():
"/v2/fairmints": queries.get_all_fairmints,
"/v2/fairmints/<tx_hash>": queries.get_fairmint,
### /bitcoin ###
"/v2/bitcoin/addresses/utxos": addrindexrs.get_unspent_txouts_by_addresses,
"/v2/bitcoin/addresses/<address>/transactions": addrindexrs.get_transactions_by_address,
"/v2/bitcoin/addresses/<address>/transactions/oldest": util.get_oldest_transaction_by_address,
"/v2/bitcoin/addresses/<address>/utxos": addrindexrs.get_unspent_txouts,
"/v2/bitcoin/addresses/utxos": electr.get_utxos_by_addresses,
"/v2/bitcoin/addresses/<address>/transactions": electr.get_history,
"/v2/bitcoin/addresses/<address>/utxos": electr.get_utxos,
"/v2/bitcoin/addresses/<address>/pubkey": util.pubkeyhash_to_pubkey,
"/v2/bitcoin/transactions/<tx_hash>": util.get_transaction,
"/v2/bitcoin/estimatesmartfee": bitcoind.fee_per_kb,
Expand Down
11 changes: 0 additions & 11 deletions counterparty-core/counterpartycore/lib/api/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,6 @@ def get_transaction(tx_hash: str, format: str = "json"):
return backend.bitcoind.getrawtransaction(tx_hash, verbose=format == "json")


def get_oldest_transaction_by_address(address: str, block_index: int = None):
"""
Get the oldest transaction for an address.
:param address: The address to search for. (e.g. $ADDRESS_9)
:param block_index: The block index to search from.
"""
return backend.addrindexrs.get_oldest_tx(
address, block_index=block_index or util.CURRENT_BLOCK_INDEX
)


def get_backend_height():
block_count = backend.bitcoind.getblockcount()
blocks_behind = backend.bitcoind.get_blocks_behind()
Expand Down
4 changes: 1 addition & 3 deletions counterparty-core/counterpartycore/lib/api/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import gunicorn.app.base
import waitress
import waitress.server
from counterpartycore.lib import backend, config, database, ledger, log, util
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
Expand All @@ -34,8 +34,6 @@ def refresh_current_state(ledger_db, state_db):
util.CURRENT_BLOCK_TIME = 0
util.CURRENT_BLOCK_INDEX = 0

backend.addrindexrs.clear_raw_transactions_cache()

if util.CURRENT_BACKEND_HEIGHT > util.CURRENT_BLOCK_INDEX:
logger.debug(
f"Counterparty is currently behind Bitcoin Core. ({util.CURRENT_BLOCK_INDEX} < {util.CURRENT_BACKEND_HEIGHT})"
Expand Down
23 changes: 17 additions & 6 deletions counterparty-core/counterpartycore/lib/backend/bitcoind.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,22 +149,33 @@ def getrawtransaction(tx_hash, verbose=False):
return rpc("getrawtransaction", [tx_hash, 1 if verbose else 0])


def getrawtransaction_batch(tx_hashes):
def getrawtransaction_batch(tx_hashes, verbose=False, return_dict=False):
if len(tx_hashes) == 0:
return {}
if len(tx_hashes) > config.MAX_RPC_BATCH_SIZE:
raise exceptions.BitcoindRPCError("Too many transactions requested")

payload = [
{"method": "getrawtransaction", "params": [tx_hash], "jsonrpc": "2.0", "id": i}
{
"method": "getrawtransaction",
"params": [tx_hash, 1 if verbose else 0],
"jsonrpc": "2.0",
"id": i,
}
for i, tx_hash in enumerate(tx_hashes)
]
results = rpc_call(payload)

raw_transactions = []
for result in results:
if "result" in result and result["result"] is not None:
raw_transactions.append(result["result"])
if return_dict:
raw_transactions = {}
for result in results:
if "result" in result and result["result"] is not None:
raw_transactions[tx_hashes[result["id"]]] = result["result"]
else:
raw_transactions = []
for result in results:
if "result" in result and result["result"] is not None:
raw_transactions.append(result["result"])

return raw_transactions

Expand Down
52 changes: 52 additions & 0 deletions counterparty-core/counterpartycore/lib/backend/electr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import requests

from counterpartycore.lib import config


def get_utxos(address, unconfirmed: bool = False, unspent_tx_hash: str = None):
"""
Returns a list of unspent outputs for a specific address
:param address: The address to search for (e.g. $ADDRESS_7)
:param unconfirmed: Include unconfirmed transactions
:param unspent_tx_hash: Filter by unspent_tx_hash
"""
url = f"{config.ELECTR_URL}/address/{address}/utxo"
Fixed Show fixed Hide fixed
utxo_list = requests.get(url, timeout=10).json()
result = []
for utxo in utxo_list:
if not utxo["status"]["confirmed"] and not unconfirmed:
continue
if unspent_tx_hash and utxo["txid"] != unspent_tx_hash:
continue
result.append(utxo)
return result


def get_history(address: str, unconfirmed: bool = False):
"""
Returns all transactions involving a given address
:param address: The address to search for (e.g. $ADDRESS_3)
"""
url = f"{config.ELECTR_URL}/address/{address}/history"
Fixed Show fixed Hide fixed
tx_list = requests.get(url, timeout=10).json()
result = []
for tx in tx_list:
if tx["status"]["confirmed"] or unconfirmed:
result.append(tx)
return result


def get_utxos_by_addresses(addresses: str, unconfirmed: bool = False, unspent_tx_hash: str = None):
"""
Returns a list of unspent outputs for a list of addresses
:param addresses: The addresses to search for (e.g. $ADDRESS_7,$ADDRESS_8)
:param unconfirmed: Include unconfirmed transactions
:param unspent_tx_hash: Filter by unspent_tx_hash
"""
unspents = []
for address in addresses.split(","):
address_unspents = get_utxos(address, unconfirmed, unspent_tx_hash)
for unspent in address_unspents:
unspent["address"] = address
unspents += address_unspents
return unspents
7 changes: 2 additions & 5 deletions counterparty-core/counterpartycore/lib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
VERSION_REVISION = int(version[2])
VERSION_PRE_RELEASE = "-".join(VERSION_STRING.split("-")[1:])

ADDRINDEXRS_VERSION = "0.4.6"
DEFAULT_ELECTR_URL_MAINNET = "https://api.counterparty.io:3000"
DEFAULT_ELECTR_URL_TESTNET = "https://api.counterparty.io:13000"

# When updating to a new verion, we are making a rollback if major version changes.
# If minor version changes and if needed, we are making a reparse from a given block.
Expand Down Expand Up @@ -71,10 +72,6 @@
DEFAULT_BACKEND_PORT_TESTNET = 18332
DEFAULT_BACKEND_PORT = 8332

DEFAULT_INDEXD_PORT_REGTEST = 18543
DEFAULT_INDEXD_PORT_TESTNET = 18432
DEFAULT_INDEXD_PORT = 8432

DEFAULT_ZMQ_SEQUENCE_PORT_REGTEST = 29332
DEFAULT_ZMQ_SEQUENCE_PORT_TESTNET = 19332
DEFAULT_ZMQ_SEQUENCE_PORT = 9332
Expand Down
4 changes: 4 additions & 0 deletions counterparty-core/counterpartycore/lib/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,7 @@ class NoPriceError(Exception):

class RSFetchError(Exception):
pass


class ElectrError(Exception):
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"1CEo6JeFgTTZBHRpnDUGWuTeWeG6X5fsHc-819476": {
"block_index": 819476,
"tx_hash": "58babe9a7ab0a3e5ba0faaab40697bed451f0cd2ea071d17ac86553a665064b4"
},
"1GsjsKKT4nH4GPmDnaxaZEDWgoBpmexwMA-820326": {
"block_index": 820321,
"tx_hash": "b61ac3ab1ba9d63d484e8f83e8b9607bd932c8f4b742095445c3527ab575d972"
},
"bc1qrdprg2uwug0r93eszza3jqk2nsp2475ggf5chu-820637": {
"block_index": 820633,
"tx_hash": "9eade5950a06f8e596283caaa95dba928aa2a5cd9c769e534dfc6a5fad188800"
},
"18xwWKKfkQmrAgaMVqHJ17Q9vzFAaXT4tf-821973": {
"block_index": 821851,
"tx_hash": "33bd1e9dbcdcded5206de365d5d9ab50f0b6b804a7da8c9e8e87cb8a2b1c98f4"
},
"1EaU4MjYNBqu2qaspXvUfqbz4i4sQ6GmhG-821973": {
"block_index": 821973,
"tx_hash": "72c867b209a24e5024b02a6935f3a1edb06c57f45284b4e2b484563458689572"
},
"bc1q8888888p9ffr9dwjs9g35fv6hwfqahfl8nw7l6-822887": {
"block_index": 822887,
"tx_hash": "cbf249d539e97e508a356ce86066262e487b9cdd00c783fcba40a6444c3b0a4c"
},
"bc1qvtrctrp9pggcfq3g4jfkrvn7g7sz0vskz2x0uk-823214": {
"block_index": 823214,
"tx_hash": "daa5c5d5e3e764f027e753f0064352a4a017982968c1fd04a7ea44c0701e640e"
},
"bc1qdc9aws4vdt0wcxlezreme486rht00qw69uhauq-824326": {
"block_index": 824322,
"tx_hash": "2dd3ba20ab7ba030b4e97bca43eebfbb6b0d07a5c535f891cbb101fbf4b7dbf9"
},
"bc1qzmgzxhmevcvu3x6pls2qmc7keucds26f6ssmsk-825182": {
"block_index": 825173,
"tx_hash": "9efc92753fb8c1a172253fa13c8b58ba55b733037d9ab8da2fe55de3d7e3970b"
},
"15W58br8XMQFHktmN2pHrkb44QPg7dEgTC-827798": {
"block_index": 787238,
"tx_hash": "bfc93ea574324f1794de90ee55f19eda423352578b57f17c618c6547a1043ebe"
},
"1L9J6k3vAQmg2ESsgLcYfHyLpZpQ4xgKuu-829953": {
"block_index": 829952,
"tx_hash": "c793679bf10990c18d904281af497254287fa09786ffc304d356a942054293a3"
},
"16k4dCds7kupHkWzRSmqDkHxJftuGCdmsd-832677": {
"block_index": 832675,
"tx_hash": "e1d917308b3a5bcaa592c0828d2192a322c8d2e988407a8c1781c8420fb5d60e"
},
"12xDGbtzh2zRtbfzVcdovUjDLJBCzisqgn-835815": {
"block_index": 835815,
"tx_hash": "4b3ad7bac2bf8f6573954cf9e0a0b0284c3f4303268a0748a5839b1aea3c7517"
},
"1Pp6exhf5bg2mTggVE3RjXo3a549UHb5qW-838178": {
"block_index": 838151,
"tx_hash": "4590db3233595442a1b0fb4bd0e6d08cf038bb5e78019558635f025a4ccbbad3"
},
"bc1qz80sh0wh026derm9pyg7xwv4lmryxktayxxfwq-842374": {
"block_index": 842374,
"tx_hash": "7a6149457a69efadd3ce92466a6bff712f962d8b56fba51fa4584b7a2b83b1de"
},
"bc1q2rn0c89mylzj6c26exda5y2nkezmzh4lh5rkkl-842375": {
"block_index": 842374,
"tx_hash": "3043db292b7f64cbe1cafa32bc733316b162d8a1f41f31a1e6cd224fd5b72415"
},
"bc1q4v00u7yf27g9ghju04th8cwc7jhnsyq5k966ff-844095": {
"block_index": 838068,
"tx_hash": "d74a68f8d30a1e18f8336ed91d2848465bb9496101fe71b8fe71edc9c0a59d1f"
},
"1GyyukjbQANSZsTrn31HDW8bQnhXYJW6d8-855009": {
"block_index": 855009,
"tx_hash": "6d4d1a1dee455935be72dfa0703e0d864eee6862067269040dab2dbb8cb0eddc"
},
"1LJCLfaKU6aNUn5w96cge7woiBE4Hn22x8-858887": {
"block_index": 0,
"tx_hash": "4587498c480700448b71753dbe86dabf9919c5a5cbff929cf1e4de4d2c161b33"
},
"1BxLHZ8fur3RMMyQWLGcNDqn5cyQxscuLd-864951": {
"block_index": 0,
"tx_hash": "c75fce0a6a28588816c302bbb6f8aa2a2e20824612f4013163c1e31edd314e07"
}
}
Loading
Loading