2021-11-08 20:18:22 +01:00
|
|
|
# pylint: disable=import-error
|
2018-11-03 23:50:34 +01:00
|
|
|
import os
|
2019-12-12 04:07:33 +01:00
|
|
|
import json
|
2018-11-03 23:50:34 +01:00
|
|
|
import shutil
|
|
|
|
import asyncio
|
|
|
|
import zipfile
|
|
|
|
import tarfile
|
|
|
|
import logging
|
|
|
|
import tempfile
|
|
|
|
import subprocess
|
2021-11-08 20:18:22 +01:00
|
|
|
import platform
|
2021-09-01 08:37:57 +02:00
|
|
|
|
2018-11-03 23:50:34 +01:00
|
|
|
from binascii import hexlify
|
2018-11-04 00:45:28 +01:00
|
|
|
from typing import Type, Optional
|
2018-11-21 04:57:59 +01:00
|
|
|
import urllib.request
|
2021-01-19 08:37:31 +01:00
|
|
|
from uuid import uuid4
|
2018-11-03 23:50:34 +01:00
|
|
|
|
2022-05-06 09:32:35 +02:00
|
|
|
|
2020-01-03 04:18:49 +01:00
|
|
|
import lbry
|
|
|
|
from lbry.wallet import Wallet, Ledger, RegTestLedger, WalletManager, Account, BlockHeightEvent
|
2021-06-14 19:06:31 +02:00
|
|
|
from lbry.conf import KnownHubsList, Config
|
2018-11-03 23:50:34 +01:00
|
|
|
|
2018-12-14 22:15:59 +01:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2022-05-09 17:34:28 +02:00
|
|
|
try:
|
|
|
|
from hub.herald.env import ServerEnv
|
|
|
|
from hub.scribe.env import BlockchainEnv
|
|
|
|
from hub.elastic_sync.env import ElasticEnv
|
|
|
|
from hub.herald.service import HubServerService
|
|
|
|
from hub.elastic_sync.service import ElasticSyncService
|
|
|
|
from hub.scribe.service import BlockchainProcessorService
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
2018-12-14 22:15:59 +01:00
|
|
|
|
2021-11-08 20:18:22 +01:00
|
|
|
def get_lbcd_node_from_ledger(ledger_module):
|
|
|
|
return LBCDNode(
|
|
|
|
ledger_module.__lbcd_url__,
|
|
|
|
ledger_module.__lbcd__,
|
|
|
|
ledger_module.__lbcctl__
|
|
|
|
)
|
2018-11-03 23:50:34 +01:00
|
|
|
|
|
|
|
|
2021-11-08 20:18:22 +01:00
|
|
|
def get_lbcwallet_node_from_ledger(ledger_module):
|
|
|
|
return LBCWalletNode(
|
|
|
|
ledger_module.__lbcwallet_url__,
|
|
|
|
ledger_module.__lbcwallet__,
|
|
|
|
ledger_module.__lbcctl__
|
2018-11-03 23:50:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class Conductor:
|
|
|
|
|
2020-01-03 04:18:49 +01:00
|
|
|
def __init__(self, seed=None):
|
|
|
|
self.manager_module = WalletManager
|
2021-11-08 20:18:22 +01:00
|
|
|
self.lbcd_node = get_lbcd_node_from_ledger(lbry.wallet)
|
|
|
|
self.lbcwallet_node = get_lbcwallet_node_from_ledger(lbry.wallet)
|
|
|
|
self.spv_node = SPVNode()
|
2019-10-13 01:33:16 +02:00
|
|
|
self.wallet_node = WalletNode(
|
2020-01-03 04:18:49 +01:00
|
|
|
self.manager_module, RegTestLedger, default_seed=seed
|
2019-10-13 01:33:16 +02:00
|
|
|
)
|
2021-11-08 20:18:22 +01:00
|
|
|
self.lbcd_started = False
|
|
|
|
self.lbcwallet_started = False
|
2018-11-03 23:50:34 +01:00
|
|
|
self.spv_started = False
|
|
|
|
self.wallet_started = False
|
|
|
|
|
2018-12-14 22:15:59 +01:00
|
|
|
self.log = log.getChild('conductor')
|
|
|
|
|
2021-11-08 20:18:22 +01:00
|
|
|
async def start_lbcd(self):
|
|
|
|
if not self.lbcd_started:
|
|
|
|
await self.lbcd_node.start()
|
|
|
|
self.lbcd_started = True
|
2018-12-14 22:15:59 +01:00
|
|
|
|
2021-11-08 20:18:22 +01:00
|
|
|
async def stop_lbcd(self, cleanup=True):
|
|
|
|
if self.lbcd_started:
|
|
|
|
await self.lbcd_node.stop(cleanup)
|
|
|
|
self.lbcd_started = False
|
2018-11-03 23:50:34 +01:00
|
|
|
|
|
|
|
async def start_spv(self):
|
2018-12-14 22:15:59 +01:00
|
|
|
if not self.spv_started:
|
2021-11-08 20:18:22 +01:00
|
|
|
await self.spv_node.start(self.lbcwallet_node)
|
2018-12-14 22:15:59 +01:00
|
|
|
self.spv_started = True
|
|
|
|
|
2021-11-08 20:18:22 +01:00
|
|
|
async def stop_spv(self, cleanup=True):
|
2018-12-14 22:15:59 +01:00
|
|
|
if self.spv_started:
|
2021-11-08 20:18:22 +01:00
|
|
|
await self.spv_node.stop(cleanup)
|
2018-12-14 22:15:59 +01:00
|
|
|
self.spv_started = False
|
2018-11-03 23:50:34 +01:00
|
|
|
|
|
|
|
async def start_wallet(self):
|
2018-12-14 22:15:59 +01:00
|
|
|
if not self.wallet_started:
|
2019-01-14 04:38:36 +01:00
|
|
|
await self.wallet_node.start(self.spv_node)
|
2018-12-14 22:15:59 +01:00
|
|
|
self.wallet_started = True
|
|
|
|
|
2021-11-08 20:18:22 +01:00
|
|
|
async def stop_wallet(self, cleanup=True):
|
2018-12-14 22:15:59 +01:00
|
|
|
if self.wallet_started:
|
2021-11-08 20:18:22 +01:00
|
|
|
await self.wallet_node.stop(cleanup)
|
2018-12-14 22:15:59 +01:00
|
|
|
self.wallet_started = False
|
2018-11-03 23:50:34 +01:00
|
|
|
|
2021-11-08 20:18:22 +01:00
|
|
|
async def start_lbcwallet(self, clean=True):
|
|
|
|
if not self.lbcwallet_started:
|
|
|
|
await self.lbcwallet_node.start()
|
|
|
|
if clean:
|
|
|
|
mining_addr = await self.lbcwallet_node.get_new_address()
|
|
|
|
self.lbcwallet_node.mining_addr = mining_addr
|
|
|
|
await self.lbcwallet_node.generate(200)
|
|
|
|
# unlock the wallet for the next 1 hour
|
|
|
|
await self.lbcwallet_node.wallet_passphrase("password", 3600)
|
|
|
|
self.lbcwallet_started = True
|
|
|
|
|
|
|
|
async def stop_lbcwallet(self, cleanup=True):
|
|
|
|
if self.lbcwallet_started:
|
|
|
|
await self.lbcwallet_node.stop(cleanup)
|
|
|
|
self.lbcwallet_started = False
|
|
|
|
|
2018-11-03 23:50:34 +01:00
|
|
|
async def start(self):
|
2021-11-08 20:18:22 +01:00
|
|
|
await self.start_lbcd()
|
|
|
|
await self.start_lbcwallet()
|
2018-12-14 22:15:59 +01:00
|
|
|
await self.start_spv()
|
|
|
|
await self.start_wallet()
|
2018-11-03 23:50:34 +01:00
|
|
|
|
|
|
|
async def stop(self):
|
2018-12-14 22:15:59 +01:00
|
|
|
all_the_stops = [
|
2019-01-13 17:44:32 +01:00
|
|
|
self.stop_wallet,
|
|
|
|
self.stop_spv,
|
2021-11-08 20:18:22 +01:00
|
|
|
self.stop_lbcwallet,
|
|
|
|
self.stop_lbcd
|
2018-12-14 22:15:59 +01:00
|
|
|
]
|
|
|
|
for stop in all_the_stops:
|
2018-11-03 23:50:34 +01:00
|
|
|
try:
|
2018-12-14 22:15:59 +01:00
|
|
|
await stop()
|
2018-11-03 23:50:34 +01:00
|
|
|
except Exception as e:
|
2018-12-14 22:15:59 +01:00
|
|
|
log.exception('Exception raised while stopping services:', exc_info=e)
|
2018-11-03 23:50:34 +01:00
|
|
|
|
2021-11-08 20:18:22 +01:00
|
|
|
async def clear_mempool(self):
|
|
|
|
await self.stop_lbcwallet(cleanup=False)
|
|
|
|
await self.stop_lbcd(cleanup=False)
|
|
|
|
await self.start_lbcd()
|
|
|
|
await self.start_lbcwallet(clean=False)
|
|
|
|
|
2018-11-03 23:50:34 +01:00
|
|
|
|
|
|
|
class WalletNode:
|
|
|
|
|
2020-01-03 04:18:49 +01:00
|
|
|
def __init__(self, manager_class: Type[WalletManager], ledger_class: Type[Ledger],
|
2021-06-14 19:06:31 +02:00
|
|
|
verbose: bool = False, port: int = 5280, default_seed: str = None,
|
|
|
|
data_path: str = None) -> None:
|
2018-11-03 23:50:34 +01:00
|
|
|
self.manager_class = manager_class
|
|
|
|
self.ledger_class = ledger_class
|
|
|
|
self.verbose = verbose
|
2020-01-03 04:18:49 +01:00
|
|
|
self.manager: Optional[WalletManager] = None
|
|
|
|
self.ledger: Optional[Ledger] = None
|
2018-11-04 00:45:28 +01:00
|
|
|
self.wallet: Optional[Wallet] = None
|
2020-01-03 04:18:49 +01:00
|
|
|
self.account: Optional[Account] = None
|
2021-06-14 19:06:31 +02:00
|
|
|
self.data_path: str = data_path or tempfile.mkdtemp()
|
2019-01-14 04:38:36 +01:00
|
|
|
self.port = port
|
2019-10-13 01:33:16 +02:00
|
|
|
self.default_seed = default_seed
|
2021-06-14 19:06:31 +02:00
|
|
|
self.known_hubs = KnownHubsList()
|
2018-11-03 23:50:34 +01:00
|
|
|
|
2021-06-14 19:06:31 +02:00
|
|
|
async def start(self, spv_node: 'SPVNode', seed=None, connect=True, config=None):
|
2019-09-20 06:05:37 +02:00
|
|
|
wallets_dir = os.path.join(self.data_path, 'wallets')
|
|
|
|
wallet_file_name = os.path.join(wallets_dir, 'my_wallet.json')
|
2021-11-08 20:18:22 +01:00
|
|
|
if not os.path.isdir(wallets_dir):
|
|
|
|
os.mkdir(wallets_dir)
|
|
|
|
with open(wallet_file_name, 'w') as wallet_file:
|
|
|
|
wallet_file.write('{"version": 1, "accounts": []}\n')
|
2018-11-03 23:50:34 +01:00
|
|
|
self.manager = self.manager_class.from_config({
|
|
|
|
'ledgers': {
|
|
|
|
self.ledger_class.get_id(): {
|
2019-01-14 04:38:36 +01:00
|
|
|
'api_port': self.port,
|
2021-06-14 19:06:31 +02:00
|
|
|
'explicit_servers': [(spv_node.hostname, spv_node.port)],
|
|
|
|
'default_servers': Config.lbryum_servers.default,
|
|
|
|
'data_path': self.data_path,
|
2021-03-13 06:49:33 +01:00
|
|
|
'known_hubs': config.known_hubs if config else KnownHubsList(),
|
2021-03-13 06:31:03 +01:00
|
|
|
'hub_timeout': 30,
|
2021-03-13 06:49:33 +01:00
|
|
|
'concurrent_hub_requests': 32,
|
2022-04-08 16:53:25 +02:00
|
|
|
'fee_per_name_char': 200000
|
2018-11-03 23:50:34 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
'wallets': [wallet_file_name]
|
|
|
|
})
|
2021-06-14 19:06:31 +02:00
|
|
|
self.manager.config = config
|
2018-11-03 23:50:34 +01:00
|
|
|
self.ledger = self.manager.ledgers[self.ledger_class]
|
|
|
|
self.wallet = self.manager.default_wallet
|
2019-10-13 01:50:12 +02:00
|
|
|
if not self.wallet:
|
|
|
|
raise ValueError('Wallet is required.')
|
2019-10-13 01:33:16 +02:00
|
|
|
if seed or self.default_seed:
|
2020-01-03 04:18:49 +01:00
|
|
|
Account.from_dict(
|
2019-10-13 01:33:16 +02:00
|
|
|
self.ledger, self.wallet, {'seed': seed or self.default_seed}
|
2018-11-19 04:54:00 +01:00
|
|
|
)
|
2019-01-14 04:44:46 +01:00
|
|
|
else:
|
2019-10-13 01:50:12 +02:00
|
|
|
self.wallet.generate_account(self.ledger)
|
2018-11-03 23:50:34 +01:00
|
|
|
self.account = self.wallet.default_account
|
2019-02-11 05:11:47 +01:00
|
|
|
if connect:
|
|
|
|
await self.manager.start()
|
2018-11-03 23:50:34 +01:00
|
|
|
|
|
|
|
async def stop(self, cleanup=True):
|
|
|
|
try:
|
|
|
|
await self.manager.stop()
|
|
|
|
finally:
|
|
|
|
cleanup and self.cleanup()
|
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
shutil.rmtree(self.data_path, ignore_errors=True)
|
|
|
|
|
|
|
|
|
|
|
|
class SPVNode:
|
2021-11-08 20:18:22 +01:00
|
|
|
def __init__(self, node_number=1):
|
|
|
|
self.node_number = node_number
|
2018-11-03 23:50:34 +01:00
|
|
|
self.controller = None
|
|
|
|
self.data_path = None
|
2021-11-08 20:18:22 +01:00
|
|
|
self.server: Optional[HubServerService] = None
|
|
|
|
self.writer: Optional[BlockchainProcessorService] = None
|
|
|
|
self.es_writer: Optional[ElasticSyncService] = None
|
2019-01-14 04:38:36 +01:00
|
|
|
self.hostname = 'localhost'
|
2019-08-29 06:12:49 +02:00
|
|
|
self.port = 50001 + node_number # avoid conflict with default daemon
|
2021-03-12 17:00:30 +01:00
|
|
|
self.udp_port = self.port
|
2021-11-08 20:18:22 +01:00
|
|
|
self.elastic_notifier_port = 19080 + node_number
|
2022-11-11 20:39:38 +01:00
|
|
|
self.elastic_services = f'localhost:9200/localhost:{self.elastic_notifier_port}'
|
2019-07-16 11:23:44 +02:00
|
|
|
self.session_timeout = 600
|
2021-11-08 20:18:22 +01:00
|
|
|
self.stopped = True
|
2021-09-29 19:06:55 +02:00
|
|
|
self.index_name = uuid4().hex
|
2018-11-03 23:50:34 +01:00
|
|
|
|
2021-11-08 20:18:22 +01:00
|
|
|
async def start(self, lbcwallet_node: 'LBCWalletNode', extraconf=None):
|
|
|
|
if not self.stopped:
|
|
|
|
log.warning("spv node is already running")
|
|
|
|
return
|
|
|
|
self.stopped = False
|
|
|
|
try:
|
|
|
|
self.data_path = tempfile.mkdtemp()
|
|
|
|
conf = {
|
|
|
|
'description': '',
|
|
|
|
'payment_address': '',
|
|
|
|
'daily_fee': '0',
|
|
|
|
'db_dir': self.data_path,
|
|
|
|
'daemon_url': lbcwallet_node.rpc_url,
|
|
|
|
'reorg_limit': 100,
|
|
|
|
'host': self.hostname,
|
|
|
|
'tcp_port': self.port,
|
|
|
|
'udp_port': self.udp_port,
|
2022-11-11 20:39:38 +01:00
|
|
|
'elastic_services': self.elastic_services,
|
2021-11-08 20:18:22 +01:00
|
|
|
'session_timeout': self.session_timeout,
|
|
|
|
'max_query_workers': 0,
|
|
|
|
'es_index_prefix': self.index_name,
|
2022-05-09 17:34:28 +02:00
|
|
|
'chain': 'regtest',
|
|
|
|
'index_address_status': False
|
2021-11-08 20:18:22 +01:00
|
|
|
}
|
|
|
|
if extraconf:
|
|
|
|
conf.update(extraconf)
|
2022-05-09 17:34:28 +02:00
|
|
|
self.writer = BlockchainProcessorService(
|
|
|
|
BlockchainEnv(db_dir=self.data_path, daemon_url=lbcwallet_node.rpc_url,
|
|
|
|
reorg_limit=100, max_query_workers=0, chain='regtest', index_address_status=False)
|
|
|
|
)
|
|
|
|
self.server = HubServerService(ServerEnv(**conf))
|
|
|
|
self.es_writer = ElasticSyncService(
|
|
|
|
ElasticEnv(
|
|
|
|
db_dir=self.data_path, reorg_limit=100, max_query_workers=0, chain='regtest',
|
|
|
|
elastic_notifier_port=self.elastic_notifier_port,
|
2022-05-27 15:59:11 +02:00
|
|
|
es_index_prefix=self.index_name,
|
|
|
|
filtering_channel_ids=(extraconf or {}).get('filtering_channel_ids'),
|
2022-05-09 17:34:28 +02:00
|
|
|
blocking_channel_ids=(extraconf or {}).get('blocking_channel_ids')
|
|
|
|
)
|
|
|
|
)
|
2021-11-08 20:18:22 +01:00
|
|
|
await self.writer.start()
|
|
|
|
await self.es_writer.start()
|
|
|
|
await self.server.start()
|
|
|
|
except Exception as e:
|
|
|
|
self.stopped = True
|
2022-06-16 19:32:18 +02:00
|
|
|
log.exception("failed to start spv node")
|
2021-11-08 20:18:22 +01:00
|
|
|
raise e
|
2018-11-03 23:50:34 +01:00
|
|
|
|
|
|
|
async def stop(self, cleanup=True):
|
2021-06-14 19:06:31 +02:00
|
|
|
if self.stopped:
|
2021-11-08 20:18:22 +01:00
|
|
|
log.warning("spv node is already stopped")
|
2021-06-14 19:06:31 +02:00
|
|
|
return
|
2018-11-03 23:50:34 +01:00
|
|
|
try:
|
2018-12-15 04:31:01 +01:00
|
|
|
await self.server.stop()
|
2021-11-08 20:18:22 +01:00
|
|
|
await self.es_writer.delete_index()
|
|
|
|
await self.es_writer.stop()
|
|
|
|
await self.writer.stop()
|
2021-06-14 19:06:31 +02:00
|
|
|
self.stopped = True
|
2021-11-08 20:18:22 +01:00
|
|
|
except Exception as e:
|
|
|
|
log.exception("failed to stop spv node")
|
|
|
|
raise e
|
2018-11-03 23:50:34 +01:00
|
|
|
finally:
|
|
|
|
cleanup and self.cleanup()
|
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
shutil.rmtree(self.data_path, ignore_errors=True)
|
|
|
|
|
|
|
|
|
2021-11-08 20:18:22 +01:00
|
|
|
class LBCDProcess(asyncio.SubprocessProtocol):
|
2018-11-03 23:50:34 +01:00
|
|
|
|
|
|
|
IGNORE_OUTPUT = [
|
|
|
|
b'keypool keep',
|
|
|
|
b'keypool reserve',
|
|
|
|
b'keypool return',
|
2021-11-08 20:18:22 +01:00
|
|
|
b'Block submitted',
|
2018-11-03 23:50:34 +01:00
|
|
|
]
|
|
|
|
|
2018-12-14 22:15:59 +01:00
|
|
|
def __init__(self):
|
2018-11-03 23:50:34 +01:00
|
|
|
self.ready = asyncio.Event()
|
|
|
|
self.stopped = asyncio.Event()
|
2021-11-08 20:18:22 +01:00
|
|
|
self.log = log.getChild('lbcd')
|
2018-11-03 23:50:34 +01:00
|
|
|
|
|
|
|
def pipe_data_received(self, fd, data):
|
|
|
|
if self.log and not any(ignore in data for ignore in self.IGNORE_OUTPUT):
|
|
|
|
if b'Error:' in data:
|
2019-02-11 19:01:07 +01:00
|
|
|
self.log.error(data.decode())
|
2018-11-03 23:50:34 +01:00
|
|
|
else:
|
2019-02-11 19:01:07 +01:00
|
|
|
self.log.info(data.decode())
|
2018-11-03 23:50:34 +01:00
|
|
|
if b'Error:' in data:
|
|
|
|
self.ready.set()
|
2019-02-11 19:01:07 +01:00
|
|
|
raise SystemError(data.decode())
|
2021-11-08 20:18:22 +01:00
|
|
|
if b'RPCS: RPC server listening on' in data:
|
2018-11-03 23:50:34 +01:00
|
|
|
self.ready.set()
|
|
|
|
|
|
|
|
def process_exited(self):
|
|
|
|
self.stopped.set()
|
2020-02-07 21:29:27 +01:00
|
|
|
self.ready.set()
|
2018-11-03 23:50:34 +01:00
|
|
|
|
|
|
|
|
2021-11-08 20:18:22 +01:00
|
|
|
class WalletProcess(asyncio.SubprocessProtocol):
|
2018-11-03 23:50:34 +01:00
|
|
|
|
2021-11-08 20:18:22 +01:00
|
|
|
IGNORE_OUTPUT = [
|
|
|
|
]
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.ready = asyncio.Event()
|
|
|
|
self.stopped = asyncio.Event()
|
|
|
|
self.log = log.getChild('lbcwallet')
|
|
|
|
self.transport: Optional[asyncio.transports.SubprocessTransport] = None
|
2019-12-12 04:07:33 +01:00
|
|
|
|
2021-11-08 20:18:22 +01:00
|
|
|
def pipe_data_received(self, fd, data):
|
|
|
|
if self.log and not any(ignore in data for ignore in self.IGNORE_OUTPUT):
|
|
|
|
if b'Error:' in data:
|
|
|
|
self.log.error(data.decode())
|
|
|
|
else:
|
|
|
|
self.log.info(data.decode())
|
|
|
|
if b'Error:' in data:
|
|
|
|
self.ready.set()
|
|
|
|
raise SystemError(data.decode())
|
|
|
|
if b'WLLT: Finished rescan' in data:
|
|
|
|
self.ready.set()
|
|
|
|
|
|
|
|
def process_exited(self):
|
|
|
|
self.stopped.set()
|
|
|
|
self.ready.set()
|
|
|
|
|
|
|
|
|
|
|
|
class LBCDNode:
|
2020-01-03 04:18:49 +01:00
|
|
|
def __init__(self, url, daemon, cli):
|
2018-11-03 23:50:34 +01:00
|
|
|
self.latest_release_url = url
|
|
|
|
self.project_dir = os.path.dirname(os.path.dirname(__file__))
|
|
|
|
self.bin_dir = os.path.join(self.project_dir, 'bin')
|
|
|
|
self.daemon_bin = os.path.join(self.bin_dir, daemon)
|
|
|
|
self.cli_bin = os.path.join(self.bin_dir, cli)
|
2021-11-08 20:18:22 +01:00
|
|
|
self.log = log.getChild('lbcd')
|
|
|
|
self.data_path = tempfile.mkdtemp()
|
2018-11-03 23:50:34 +01:00
|
|
|
self.protocol = None
|
|
|
|
self.transport = None
|
2019-04-06 21:27:58 +02:00
|
|
|
self.hostname = 'localhost'
|
2021-11-08 20:18:22 +01:00
|
|
|
self.peerport = 29246
|
|
|
|
self.rpcport = 29245
|
2019-01-14 04:38:36 +01:00
|
|
|
self.rpcuser = 'rpcuser'
|
|
|
|
self.rpcpassword = 'rpcpassword'
|
2021-11-08 20:18:22 +01:00
|
|
|
self.stopped = True
|
2020-04-01 19:52:14 +02:00
|
|
|
self.running = asyncio.Event()
|
2019-01-14 04:38:36 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def rpc_url(self):
|
2019-04-06 21:27:58 +02:00
|
|
|
return f'http://{self.rpcuser}:{self.rpcpassword}@{self.hostname}:{self.rpcport}/'
|
2018-11-03 23:50:34 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def exists(self):
|
|
|
|
return (
|
|
|
|
os.path.exists(self.cli_bin) and
|
|
|
|
os.path.exists(self.daemon_bin)
|
|
|
|
)
|
|
|
|
|
|
|
|
def download(self):
|
2021-11-08 20:18:22 +01:00
|
|
|
uname = platform.uname()
|
|
|
|
target_os = str.lower(uname.system)
|
|
|
|
target_arch = str.replace(uname.machine, 'x86_64', 'amd64')
|
|
|
|
target_platform = target_os + '_' + target_arch
|
|
|
|
self.latest_release_url = str.replace(self.latest_release_url, 'TARGET_PLATFORM', target_platform)
|
|
|
|
|
2018-11-03 23:50:34 +01:00
|
|
|
downloaded_file = os.path.join(
|
|
|
|
self.bin_dir,
|
|
|
|
self.latest_release_url[self.latest_release_url.rfind('/')+1:]
|
|
|
|
)
|
|
|
|
|
|
|
|
if not os.path.exists(self.bin_dir):
|
|
|
|
os.mkdir(self.bin_dir)
|
|
|
|
|
|
|
|
if not os.path.exists(downloaded_file):
|
|
|
|
self.log.info('Downloading: %s', self.latest_release_url)
|
2018-11-21 04:57:59 +01:00
|
|
|
with urllib.request.urlopen(self.latest_release_url) as response:
|
|
|
|
with open(downloaded_file, 'wb') as out_file:
|
|
|
|
shutil.copyfileobj(response, out_file)
|
2018-11-03 23:50:34 +01:00
|
|
|
|
|
|
|
self.log.info('Extracting: %s', downloaded_file)
|
|
|
|
|
|
|
|
if downloaded_file.endswith('.zip'):
|
2018-11-04 00:45:28 +01:00
|
|
|
with zipfile.ZipFile(downloaded_file) as dotzip:
|
|
|
|
dotzip.extractall(self.bin_dir)
|
2018-11-03 23:50:34 +01:00
|
|
|
# zipfile bug https://bugs.python.org/issue15795
|
|
|
|
os.chmod(self.cli_bin, 0o755)
|
|
|
|
os.chmod(self.daemon_bin, 0o755)
|
|
|
|
|
|
|
|
elif downloaded_file.endswith('.tar.gz'):
|
|
|
|
with tarfile.open(downloaded_file) as tar:
|
|
|
|
tar.extractall(self.bin_dir)
|
|
|
|
|
|
|
|
return self.exists
|
|
|
|
|
|
|
|
def ensure(self):
|
|
|
|
return self.exists or self.download()
|
|
|
|
|
|
|
|
async def start(self):
|
2021-11-08 20:18:22 +01:00
|
|
|
if not self.stopped:
|
|
|
|
return
|
|
|
|
self.stopped = False
|
|
|
|
try:
|
|
|
|
assert self.ensure()
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
asyncio.get_child_watcher().attach_loop(loop)
|
|
|
|
command = [
|
|
|
|
self.daemon_bin,
|
|
|
|
'--notls',
|
|
|
|
f'--datadir={self.data_path}',
|
|
|
|
'--regtest', f'--listen=127.0.0.1:{self.peerport}', f'--rpclisten=127.0.0.1:{self.rpcport}',
|
|
|
|
'--txindex', f'--rpcuser={self.rpcuser}', f'--rpcpass={self.rpcpassword}'
|
|
|
|
]
|
|
|
|
self.log.info(' '.join(command))
|
|
|
|
self.transport, self.protocol = await loop.subprocess_exec(
|
|
|
|
LBCDProcess, *command
|
|
|
|
)
|
|
|
|
await self.protocol.ready.wait()
|
|
|
|
assert not self.protocol.stopped.is_set()
|
|
|
|
self.running.set()
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
self.running.clear()
|
|
|
|
self.stopped = True
|
|
|
|
raise
|
|
|
|
except Exception as e:
|
|
|
|
self.running.clear()
|
|
|
|
self.stopped = True
|
|
|
|
log.exception('failed to start lbcd', exc_info=e)
|
|
|
|
raise
|
|
|
|
|
|
|
|
async def stop(self, cleanup=True):
|
|
|
|
if self.stopped:
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
if self.transport:
|
|
|
|
self.transport.terminate()
|
|
|
|
await self.protocol.stopped.wait()
|
|
|
|
self.transport.close()
|
|
|
|
except Exception as e:
|
|
|
|
log.exception('failed to stop lbcd', exc_info=e)
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
self.log.info("Done shutting down " + self.daemon_bin)
|
|
|
|
self.stopped = True
|
|
|
|
if cleanup:
|
|
|
|
self.cleanup()
|
|
|
|
self.running.clear()
|
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
assert self.stopped
|
|
|
|
shutil.rmtree(self.data_path, ignore_errors=True)
|
|
|
|
|
|
|
|
|
|
|
|
class LBCWalletNode:
|
|
|
|
P2SH_SEGWIT_ADDRESS = "p2sh-segwit"
|
|
|
|
BECH32_ADDRESS = "bech32"
|
|
|
|
|
|
|
|
def __init__(self, url, lbcwallet, cli):
|
|
|
|
self.latest_release_url = url
|
|
|
|
self.project_dir = os.path.dirname(os.path.dirname(__file__))
|
|
|
|
self.bin_dir = os.path.join(self.project_dir, 'bin')
|
|
|
|
self.lbcwallet_bin = os.path.join(self.bin_dir, lbcwallet)
|
|
|
|
self.cli_bin = os.path.join(self.bin_dir, cli)
|
|
|
|
self.log = log.getChild('lbcwallet')
|
|
|
|
self.protocol = None
|
|
|
|
self.transport = None
|
|
|
|
self.hostname = 'localhost'
|
|
|
|
self.lbcd_rpcport = 29245
|
|
|
|
self.lbcwallet_rpcport = 29244
|
|
|
|
self.rpcuser = 'rpcuser'
|
|
|
|
self.rpcpassword = 'rpcpassword'
|
2018-11-03 23:50:34 +01:00
|
|
|
self.data_path = tempfile.mkdtemp()
|
2021-11-08 20:18:22 +01:00
|
|
|
self.stopped = True
|
|
|
|
self.running = asyncio.Event()
|
|
|
|
self.block_expected = 0
|
|
|
|
self.mining_addr = ''
|
|
|
|
|
|
|
|
@property
|
|
|
|
def rpc_url(self):
|
|
|
|
# FIXME: somehow the hub/sdk doesn't learn the blocks through the Walet RPC port, why?
|
|
|
|
# return f'http://{self.rpcuser}:{self.rpcpassword}@{self.hostname}:{self.lbcwallet_rpcport}/'
|
|
|
|
return f'http://{self.rpcuser}:{self.rpcpassword}@{self.hostname}:{self.lbcd_rpcport}/'
|
|
|
|
|
|
|
|
def is_expected_block(self, e: BlockHeightEvent):
|
|
|
|
return self.block_expected == e.height
|
|
|
|
|
|
|
|
@property
|
|
|
|
def exists(self):
|
|
|
|
return (
|
|
|
|
os.path.exists(self.lbcwallet_bin)
|
|
|
|
)
|
|
|
|
|
|
|
|
def download(self):
|
|
|
|
uname = platform.uname()
|
|
|
|
target_os = str.lower(uname.system)
|
|
|
|
target_arch = str.replace(uname.machine, 'x86_64', 'amd64')
|
|
|
|
target_platform = target_os + '_' + target_arch
|
|
|
|
self.latest_release_url = str.replace(self.latest_release_url, 'TARGET_PLATFORM', target_platform)
|
|
|
|
|
|
|
|
downloaded_file = os.path.join(
|
|
|
|
self.bin_dir,
|
|
|
|
self.latest_release_url[self.latest_release_url.rfind('/')+1:]
|
|
|
|
)
|
|
|
|
|
|
|
|
if not os.path.exists(self.bin_dir):
|
|
|
|
os.mkdir(self.bin_dir)
|
|
|
|
|
|
|
|
if not os.path.exists(downloaded_file):
|
|
|
|
self.log.info('Downloading: %s', self.latest_release_url)
|
|
|
|
with urllib.request.urlopen(self.latest_release_url) as response:
|
|
|
|
with open(downloaded_file, 'wb') as out_file:
|
|
|
|
shutil.copyfileobj(response, out_file)
|
|
|
|
|
|
|
|
self.log.info('Extracting: %s', downloaded_file)
|
|
|
|
|
|
|
|
if downloaded_file.endswith('.zip'):
|
|
|
|
with zipfile.ZipFile(downloaded_file) as dotzip:
|
|
|
|
dotzip.extractall(self.bin_dir)
|
|
|
|
# zipfile bug https://bugs.python.org/issue15795
|
|
|
|
os.chmod(self.lbcwallet_bin, 0o755)
|
|
|
|
|
|
|
|
elif downloaded_file.endswith('.tar.gz'):
|
|
|
|
with tarfile.open(downloaded_file) as tar:
|
|
|
|
tar.extractall(self.bin_dir)
|
|
|
|
|
|
|
|
return self.exists
|
|
|
|
|
|
|
|
def ensure(self):
|
|
|
|
return self.exists or self.download()
|
|
|
|
|
|
|
|
async def start(self):
|
|
|
|
assert self.ensure()
|
2018-11-03 23:50:34 +01:00
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
asyncio.get_child_watcher().attach_loop(loop)
|
2021-11-08 20:18:22 +01:00
|
|
|
|
2019-08-14 15:26:02 +02:00
|
|
|
command = [
|
2021-11-08 20:18:22 +01:00
|
|
|
self.lbcwallet_bin,
|
|
|
|
'--noservertls', '--noclienttls',
|
|
|
|
'--regtest',
|
|
|
|
f'--rpcconnect=127.0.0.1:{self.lbcd_rpcport}', f'--rpclisten=127.0.0.1:{self.lbcwallet_rpcport}',
|
|
|
|
'--createtemp', f'--appdata={self.data_path}',
|
|
|
|
f'--username={self.rpcuser}', f'--password={self.rpcpassword}'
|
2019-08-14 15:26:02 +02:00
|
|
|
]
|
2018-11-03 23:50:34 +01:00
|
|
|
self.log.info(' '.join(command))
|
2021-11-08 20:18:22 +01:00
|
|
|
try:
|
|
|
|
self.transport, self.protocol = await loop.subprocess_exec(
|
|
|
|
WalletProcess, *command
|
|
|
|
)
|
|
|
|
self.protocol.transport = self.transport
|
|
|
|
await self.protocol.ready.wait()
|
|
|
|
assert not self.protocol.stopped.is_set()
|
|
|
|
self.running.set()
|
|
|
|
self.stopped = False
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
self.running.clear()
|
|
|
|
raise
|
|
|
|
except Exception as e:
|
|
|
|
self.running.clear()
|
|
|
|
log.exception('failed to start lbcwallet', exc_info=e)
|
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
assert self.stopped
|
|
|
|
shutil.rmtree(self.data_path, ignore_errors=True)
|
2018-11-03 23:50:34 +01:00
|
|
|
|
|
|
|
async def stop(self, cleanup=True):
|
2021-11-08 20:18:22 +01:00
|
|
|
if self.stopped:
|
|
|
|
return
|
2018-11-03 23:50:34 +01:00
|
|
|
try:
|
|
|
|
self.transport.terminate()
|
|
|
|
await self.protocol.stopped.wait()
|
2018-12-05 07:13:31 +01:00
|
|
|
self.transport.close()
|
2021-11-08 20:18:22 +01:00
|
|
|
except Exception as e:
|
|
|
|
log.exception('failed to stop lbcwallet', exc_info=e)
|
|
|
|
raise
|
2018-11-03 23:50:34 +01:00
|
|
|
finally:
|
2021-11-08 20:18:22 +01:00
|
|
|
self.log.info("Done shutting down " + self.lbcwallet_bin)
|
|
|
|
self.stopped = True
|
2018-11-03 23:50:34 +01:00
|
|
|
if cleanup:
|
|
|
|
self.cleanup()
|
2021-11-08 20:18:22 +01:00
|
|
|
self.running.clear()
|
2018-11-03 23:50:34 +01:00
|
|
|
|
|
|
|
async def _cli_cmnd(self, *args):
|
|
|
|
cmnd_args = [
|
2021-11-08 20:18:22 +01:00
|
|
|
self.cli_bin,
|
|
|
|
f'--rpcuser={self.rpcuser}', f'--rpcpass={self.rpcpassword}', '--notls', '--regtest', '--wallet'
|
2018-11-03 23:50:34 +01:00
|
|
|
] + list(args)
|
|
|
|
self.log.info(' '.join(cmnd_args))
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
asyncio.get_child_watcher().attach_loop(loop)
|
|
|
|
process = await asyncio.create_subprocess_exec(
|
2021-11-08 20:18:22 +01:00
|
|
|
*cmnd_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE
|
2018-11-03 23:50:34 +01:00
|
|
|
)
|
2021-11-08 20:18:22 +01:00
|
|
|
out, err = await process.communicate()
|
2019-12-31 23:47:24 +01:00
|
|
|
result = out.decode().strip()
|
2021-11-08 20:18:22 +01:00
|
|
|
err = err.decode().strip()
|
|
|
|
if len(result) <= 0 and err.startswith('-'):
|
|
|
|
raise Exception(err)
|
|
|
|
if err and 'creating a default config file' not in err:
|
|
|
|
log.warning(err)
|
2019-12-31 23:47:24 +01:00
|
|
|
self.log.info(result)
|
|
|
|
if result.startswith('error code'):
|
|
|
|
raise Exception(result)
|
|
|
|
return result
|
2018-11-03 23:50:34 +01:00
|
|
|
|
|
|
|
def generate(self, blocks):
|
2019-12-20 17:04:21 +01:00
|
|
|
self.block_expected += blocks
|
2021-11-08 20:18:22 +01:00
|
|
|
return self._cli_cmnd('generatetoaddress', str(blocks), self.mining_addr)
|
|
|
|
|
|
|
|
def generate_to_address(self, blocks, addr):
|
|
|
|
self.block_expected += blocks
|
|
|
|
return self._cli_cmnd('generatetoaddress', str(blocks), addr)
|
|
|
|
|
|
|
|
def wallet_passphrase(self, passphrase, timeout):
|
|
|
|
return self._cli_cmnd('walletpassphrase', passphrase, str(timeout))
|
2018-11-03 23:50:34 +01:00
|
|
|
|
2019-06-18 23:50:41 +02:00
|
|
|
def invalidate_block(self, blockhash):
|
2018-11-04 00:45:28 +01:00
|
|
|
return self._cli_cmnd('invalidateblock', blockhash)
|
2018-11-03 23:50:34 +01:00
|
|
|
|
2019-06-18 23:50:41 +02:00
|
|
|
def get_block_hash(self, block):
|
|
|
|
return self._cli_cmnd('getblockhash', str(block))
|
|
|
|
|
2020-04-01 21:26:35 +02:00
|
|
|
def sendrawtransaction(self, tx):
|
|
|
|
return self._cli_cmnd('sendrawtransaction', tx)
|
|
|
|
|
2020-04-01 19:52:14 +02:00
|
|
|
async def get_block(self, block_hash):
|
|
|
|
return json.loads(await self._cli_cmnd('getblock', block_hash, '1'))
|
|
|
|
|
2018-11-03 23:50:34 +01:00
|
|
|
def get_raw_change_address(self):
|
|
|
|
return self._cli_cmnd('getrawchangeaddress')
|
|
|
|
|
2021-11-08 20:18:22 +01:00
|
|
|
def get_new_address(self, address_type='legacy'):
|
2019-12-12 15:04:13 +01:00
|
|
|
return self._cli_cmnd('getnewaddress', "", address_type)
|
2019-12-12 04:07:33 +01:00
|
|
|
|
2018-11-03 23:50:34 +01:00
|
|
|
async def get_balance(self):
|
2021-06-02 15:51:08 +02:00
|
|
|
return await self._cli_cmnd('getbalance')
|
2018-11-03 23:50:34 +01:00
|
|
|
|
2018-11-04 00:45:28 +01:00
|
|
|
def send_to_address(self, address, amount):
|
|
|
|
return self._cli_cmnd('sendtoaddress', address, str(amount))
|
2018-11-03 23:50:34 +01:00
|
|
|
|
|
|
|
def send_raw_transaction(self, tx):
|
|
|
|
return self._cli_cmnd('sendrawtransaction', tx.decode())
|
|
|
|
|
2019-12-12 04:07:33 +01:00
|
|
|
def create_raw_transaction(self, inputs, outputs):
|
|
|
|
return self._cli_cmnd('createrawtransaction', json.dumps(inputs), json.dumps(outputs))
|
|
|
|
|
|
|
|
async def sign_raw_transaction_with_wallet(self, tx):
|
2021-11-08 20:18:22 +01:00
|
|
|
# the "withwallet" portion should only come into play if we are doing segwit.
|
|
|
|
# and "withwallet" doesn't exist on lbcd yet.
|
|
|
|
result = await self._cli_cmnd('signrawtransaction', tx)
|
|
|
|
return json.loads(result)['hex'].encode()
|
2019-12-12 04:07:33 +01:00
|
|
|
|
2018-11-03 23:50:34 +01:00
|
|
|
def decode_raw_transaction(self, tx):
|
|
|
|
return self._cli_cmnd('decoderawtransaction', hexlify(tx.raw).decode())
|
|
|
|
|
|
|
|
def get_raw_transaction(self, txid):
|
|
|
|
return self._cli_cmnd('getrawtransaction', txid, '1')
|