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
|
|
|
|
import importlib
|
|
|
|
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
|
2018-11-03 23:50:34 +01:00
|
|
|
|
2020-01-03 04:18:49 +01:00
|
|
|
import lbry
|
2020-04-11 23:27:41 +02:00
|
|
|
from lbry.db import Database
|
2019-12-31 20:52:57 +01:00
|
|
|
from lbry.wallet.server.server import Server
|
|
|
|
from lbry.wallet.server.env import Env
|
2020-01-03 04:18:49 +01:00
|
|
|
from lbry.wallet import Wallet, Ledger, RegTestLedger, WalletManager, Account, BlockHeightEvent
|
2018-11-03 23:50:34 +01:00
|
|
|
|
|
|
|
|
2018-12-14 22:15:59 +01:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-11-03 23:50:34 +01:00
|
|
|
def get_spvserver_from_ledger(ledger_module):
|
|
|
|
spvserver_path, regtest_class_name = ledger_module.__spvserver__.rsplit('.', 1)
|
|
|
|
spvserver_module = importlib.import_module(spvserver_path)
|
|
|
|
return getattr(spvserver_module, regtest_class_name)
|
|
|
|
|
|
|
|
|
|
|
|
def get_blockchain_node_from_ledger(ledger_module):
|
|
|
|
return BlockchainNode(
|
|
|
|
ledger_module.__node_url__,
|
|
|
|
os.path.join(ledger_module.__node_bin__, ledger_module.__node_daemon__),
|
|
|
|
os.path.join(ledger_module.__node_bin__, ledger_module.__node_cli__)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class Conductor:
|
|
|
|
|
2020-01-03 04:18:49 +01:00
|
|
|
def __init__(self, seed=None):
|
|
|
|
self.manager_module = WalletManager
|
|
|
|
self.spv_module = get_spvserver_from_ledger(lbry.wallet)
|
2018-11-03 23:50:34 +01:00
|
|
|
|
2020-01-03 04:18:49 +01:00
|
|
|
self.blockchain_node = get_blockchain_node_from_ledger(lbry.wallet)
|
2018-11-03 23:50:34 +01:00
|
|
|
self.spv_node = SPVNode(self.spv_module)
|
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
|
|
|
)
|
2018-11-03 23:50:34 +01:00
|
|
|
|
|
|
|
self.blockchain_started = False
|
|
|
|
self.spv_started = False
|
|
|
|
self.wallet_started = False
|
|
|
|
|
2018-12-14 22:15:59 +01:00
|
|
|
self.log = log.getChild('conductor')
|
|
|
|
|
2018-11-03 23:50:34 +01:00
|
|
|
async def start_blockchain(self):
|
2018-12-14 22:15:59 +01:00
|
|
|
if not self.blockchain_started:
|
2020-04-01 19:52:14 +02:00
|
|
|
asyncio.create_task(self.blockchain_node.start())
|
|
|
|
await self.blockchain_node.running.wait()
|
2018-12-14 22:15:59 +01:00
|
|
|
await self.blockchain_node.generate(200)
|
|
|
|
self.blockchain_started = True
|
|
|
|
|
|
|
|
async def stop_blockchain(self):
|
|
|
|
if self.blockchain_started:
|
|
|
|
await self.blockchain_node.stop(cleanup=True)
|
|
|
|
self.blockchain_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:
|
2019-01-14 04:38:36 +01:00
|
|
|
await self.spv_node.start(self.blockchain_node)
|
2018-12-14 22:15:59 +01:00
|
|
|
self.spv_started = True
|
|
|
|
|
|
|
|
async def stop_spv(self):
|
|
|
|
if self.spv_started:
|
|
|
|
await self.spv_node.stop(cleanup=True)
|
|
|
|
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
|
|
|
|
|
|
|
|
async def stop_wallet(self):
|
|
|
|
if self.wallet_started:
|
|
|
|
await self.wallet_node.stop(cleanup=True)
|
|
|
|
self.wallet_started = False
|
2018-11-03 23:50:34 +01:00
|
|
|
|
|
|
|
async def start(self):
|
2018-12-14 22:15:59 +01:00
|
|
|
await self.start_blockchain()
|
|
|
|
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,
|
|
|
|
self.stop_blockchain
|
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
|
|
|
|
|
|
|
|
|
|
|
class WalletNode:
|
|
|
|
|
2020-01-03 04:18:49 +01:00
|
|
|
def __init__(self, manager_class: Type[WalletManager], ledger_class: Type[Ledger],
|
2019-10-13 01:33:16 +02:00
|
|
|
verbose: bool = False, port: int = 5280, default_seed: 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
|
2018-11-04 00:45:28 +01:00
|
|
|
self.data_path: Optional[str] = None
|
2019-01-14 04:38:36 +01:00
|
|
|
self.port = port
|
2019-10-13 01:33:16 +02:00
|
|
|
self.default_seed = default_seed
|
2018-11-03 23:50:34 +01:00
|
|
|
|
2019-02-11 05:11:47 +01:00
|
|
|
async def start(self, spv_node: 'SPVNode', seed=None, connect=True):
|
2018-11-03 23:50:34 +01:00
|
|
|
self.data_path = tempfile.mkdtemp()
|
2019-09-20 06:05:37 +02:00
|
|
|
wallets_dir = os.path.join(self.data_path, 'wallets')
|
|
|
|
os.mkdir(wallets_dir)
|
|
|
|
wallet_file_name = os.path.join(wallets_dir, 'my_wallet.json')
|
2018-11-04 00:45:28 +01:00
|
|
|
with open(wallet_file_name, 'w') as wallet_file:
|
|
|
|
wallet_file.write('{"version": 1, "accounts": []}\n')
|
2020-04-11 23:27:41 +02:00
|
|
|
db_driver = os.environ.get('TEST_DB', 'sqlite')
|
|
|
|
if db_driver == 'sqlite':
|
|
|
|
db = 'sqlite:///'+os.path.join(self.data_path, self.ledger_class.get_id(), 'blockchain.db')
|
|
|
|
elif db_driver == 'postgres':
|
2020-04-12 17:06:05 +02:00
|
|
|
db_connection = 'postgres:postgres@localhost:5432'
|
2020-04-11 23:27:41 +02:00
|
|
|
db_name = f'lbry_test_{self.port}'
|
2020-04-12 17:06:05 +02:00
|
|
|
meta_db = Database(f'postgresql+psycopg2://{db_connection}/postgres')
|
2020-04-11 23:27:41 +02:00
|
|
|
await meta_db.drop(db_name)
|
|
|
|
await meta_db.create(db_name)
|
2020-04-12 17:06:05 +02:00
|
|
|
db = f'postgresql+psycopg2://{db_connection}/{db_name}'
|
2020-04-11 23:27:41 +02:00
|
|
|
else:
|
|
|
|
raise RuntimeError(f"Unsupported database driver: {db_driver}")
|
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,
|
|
|
|
'default_servers': [(spv_node.hostname, spv_node.port)],
|
2020-04-11 23:27:41 +02:00
|
|
|
'data_path': self.data_path,
|
|
|
|
'db': Database(db)
|
2018-11-03 23:50:34 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
'wallets': [wallet_file_name]
|
|
|
|
})
|
|
|
|
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:
|
|
|
|
|
2019-08-29 06:12:49 +02:00
|
|
|
def __init__(self, coin_class, node_number=1):
|
2018-11-03 23:50:34 +01:00
|
|
|
self.coin_class = coin_class
|
|
|
|
self.controller = None
|
|
|
|
self.data_path = None
|
2018-11-04 00:45:28 +01:00
|
|
|
self.server = 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
|
2019-07-16 11:23:44 +02:00
|
|
|
self.session_timeout = 600
|
2019-08-29 06:12:49 +02:00
|
|
|
self.rpc_port = '0' # disabled by default
|
2018-11-03 23:50:34 +01:00
|
|
|
|
2019-11-05 08:02:05 +01:00
|
|
|
async def start(self, blockchain_node: 'BlockchainNode', extraconf=None):
|
2018-11-03 23:50:34 +01:00
|
|
|
self.data_path = tempfile.mkdtemp()
|
|
|
|
conf = {
|
2020-01-12 04:48:46 +01:00
|
|
|
'DESCRIPTION': '',
|
|
|
|
'PAYMENT_ADDRESS': '',
|
|
|
|
'DAILY_FEE': '0',
|
2018-11-03 23:50:34 +01:00
|
|
|
'DB_DIRECTORY': self.data_path,
|
2019-01-14 04:38:36 +01:00
|
|
|
'DAEMON_URL': blockchain_node.rpc_url,
|
2018-11-03 23:50:34 +01:00
|
|
|
'REORG_LIMIT': '100',
|
2019-01-16 19:05:23 +01:00
|
|
|
'HOST': self.hostname,
|
2019-07-11 05:07:58 +02:00
|
|
|
'TCP_PORT': str(self.port),
|
2019-07-16 11:23:44 +02:00
|
|
|
'SESSION_TIMEOUT': str(self.session_timeout),
|
2019-07-20 16:03:24 +02:00
|
|
|
'MAX_QUERY_WORKERS': '0',
|
|
|
|
'INDIVIDUAL_TAG_INDEXES': '',
|
2019-08-29 06:12:49 +02:00
|
|
|
'RPC_PORT': self.rpc_port
|
2018-11-03 23:50:34 +01:00
|
|
|
}
|
2019-11-05 08:02:05 +01:00
|
|
|
if extraconf:
|
|
|
|
conf.update(extraconf)
|
2019-01-14 04:38:36 +01:00
|
|
|
# TODO: don't use os.environ
|
2018-11-03 23:50:34 +01:00
|
|
|
os.environ.update(conf)
|
|
|
|
self.server = Server(Env(self.coin_class))
|
2018-12-07 05:13:08 +01:00
|
|
|
self.server.mempool.refresh_secs = self.server.bp.prefetcher.polling_delay = 0.5
|
2018-11-03 23:50:34 +01:00
|
|
|
await self.server.start()
|
|
|
|
|
|
|
|
async def stop(self, cleanup=True):
|
|
|
|
try:
|
2018-12-15 04:31:01 +01:00
|
|
|
await self.server.stop()
|
2018-11-03 23:50:34 +01:00
|
|
|
finally:
|
|
|
|
cleanup and self.cleanup()
|
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
shutil.rmtree(self.data_path, ignore_errors=True)
|
|
|
|
|
|
|
|
|
|
|
|
class BlockchainProcess(asyncio.SubprocessProtocol):
|
|
|
|
|
|
|
|
IGNORE_OUTPUT = [
|
|
|
|
b'keypool keep',
|
|
|
|
b'keypool reserve',
|
|
|
|
b'keypool return',
|
|
|
|
]
|
|
|
|
|
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()
|
2018-12-14 22:15:59 +01:00
|
|
|
self.log = log.getChild('blockchain')
|
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())
|
2019-02-28 18:00:44 +01:00
|
|
|
if b'Done loading' 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
|
|
|
|
|
|
|
|
|
|
|
class BlockchainNode:
|
|
|
|
|
2019-12-12 04:07:33 +01:00
|
|
|
P2SH_SEGWIT_ADDRESS = "p2sh-segwit"
|
|
|
|
BECH32_ADDRESS = "bech32"
|
|
|
|
|
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)
|
2018-12-14 22:15:59 +01:00
|
|
|
self.log = log.getChild('blockchain')
|
2018-11-03 23:50:34 +01:00
|
|
|
self.data_path = None
|
|
|
|
self.protocol = None
|
|
|
|
self.transport = None
|
2019-12-20 17:04:21 +01:00
|
|
|
self.block_expected = 0
|
2019-04-06 21:27:58 +02:00
|
|
|
self.hostname = 'localhost'
|
|
|
|
self.peerport = 9246 + 2 # avoid conflict with default peer port
|
|
|
|
self.rpcport = 9245 + 2 # avoid conflict with default rpc port
|
2019-01-14 04:38:36 +01:00
|
|
|
self.rpcuser = 'rpcuser'
|
|
|
|
self.rpcpassword = 'rpcpassword'
|
2020-04-01 19:52:14 +02:00
|
|
|
self.stopped = False
|
|
|
|
self.restart_ready = asyncio.Event()
|
|
|
|
self.restart_ready.set()
|
|
|
|
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
|
|
|
|
|
|
|
def is_expected_block(self, e: BlockHeightEvent):
|
2019-12-20 17:04:21 +01:00
|
|
|
return self.block_expected == e.height
|
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):
|
|
|
|
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):
|
|
|
|
assert self.ensure()
|
|
|
|
self.data_path = tempfile.mkdtemp()
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
asyncio.get_child_watcher().attach_loop(loop)
|
2019-08-14 15:26:02 +02:00
|
|
|
command = [
|
2018-11-03 23:50:34 +01:00
|
|
|
self.daemon_bin,
|
2019-04-06 21:27:58 +02:00
|
|
|
f'-datadir={self.data_path}', '-printtoconsole', '-regtest', '-server', '-txindex',
|
|
|
|
f'-rpcuser={self.rpcuser}', f'-rpcpassword={self.rpcpassword}', f'-rpcport={self.rpcport}',
|
|
|
|
f'-port={self.peerport}'
|
2019-08-14 15:26:02 +02:00
|
|
|
]
|
2018-11-03 23:50:34 +01:00
|
|
|
self.log.info(' '.join(command))
|
2020-04-01 19:52:14 +02:00
|
|
|
while not self.stopped:
|
|
|
|
if self.running.is_set():
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
continue
|
|
|
|
await self.restart_ready.wait()
|
|
|
|
try:
|
|
|
|
self.transport, self.protocol = await loop.subprocess_exec(
|
|
|
|
BlockchainProcess, *command
|
|
|
|
)
|
|
|
|
await self.protocol.ready.wait()
|
|
|
|
assert not self.protocol.stopped.is_set()
|
|
|
|
self.running.set()
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
self.running.clear()
|
|
|
|
raise
|
2020-04-01 21:11:45 +02:00
|
|
|
except Exception as e:
|
2020-04-01 19:52:14 +02:00
|
|
|
self.running.clear()
|
2020-04-01 21:11:45 +02:00
|
|
|
log.exception('failed to start lbrycrdd', exc_info=e)
|
2018-11-03 23:50:34 +01:00
|
|
|
|
|
|
|
async def stop(self, cleanup=True):
|
2020-04-01 19:52:14 +02:00
|
|
|
self.stopped = True
|
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()
|
2018-11-03 23:50:34 +01:00
|
|
|
finally:
|
|
|
|
if cleanup:
|
|
|
|
self.cleanup()
|
|
|
|
|
2020-04-01 19:52:14 +02:00
|
|
|
async def clear_mempool(self):
|
|
|
|
self.restart_ready.clear()
|
|
|
|
self.transport.terminate()
|
|
|
|
await self.protocol.stopped.wait()
|
|
|
|
self.transport.close()
|
|
|
|
self.running.clear()
|
|
|
|
os.remove(os.path.join(self.data_path, 'regtest', 'mempool.dat'))
|
|
|
|
self.restart_ready.set()
|
|
|
|
await self.running.wait()
|
|
|
|
|
2018-11-03 23:50:34 +01:00
|
|
|
def cleanup(self):
|
|
|
|
shutil.rmtree(self.data_path, ignore_errors=True)
|
|
|
|
|
|
|
|
async def _cli_cmnd(self, *args):
|
|
|
|
cmnd_args = [
|
2019-04-06 21:27:58 +02:00
|
|
|
self.cli_bin, f'-datadir={self.data_path}', '-regtest',
|
|
|
|
f'-rpcuser={self.rpcuser}', f'-rpcpassword={self.rpcpassword}', f'-rpcport={self.rpcport}'
|
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(
|
|
|
|
*cmnd_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
|
|
|
|
)
|
2018-11-04 00:45:28 +01:00
|
|
|
out, _ = await process.communicate()
|
2019-12-31 23:47:24 +01:00
|
|
|
result = out.decode().strip()
|
|
|
|
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
|
2018-11-03 23:50:34 +01:00
|
|
|
return self._cli_cmnd('generate', str(blocks))
|
|
|
|
|
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')
|
|
|
|
|
2019-12-12 15:04:13 +01:00
|
|
|
def get_new_address(self, address_type):
|
|
|
|
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):
|
|
|
|
return float(await self._cli_cmnd('getbalance'))
|
|
|
|
|
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):
|
|
|
|
return json.loads(await self._cli_cmnd('signrawtransactionwithwallet', tx))['hex'].encode()
|
|
|
|
|
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')
|