Skip to content

Commit

Permalink
Merge pull request #1537 from CounterpartyXCP/welcome
Browse files Browse the repository at this point in the history
Welcome message with more info
  • Loading branch information
adamkrellenstein authored Mar 20, 2024
2 parents 281ddee + 2497b83 commit c7ee3da
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 45 deletions.
64 changes: 48 additions & 16 deletions counterparty-cli/counterpartycli/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import argparse
import logging
from urllib.parse import quote_plus as urlencode

from termcolor import cprint

Expand Down Expand Up @@ -58,19 +59,50 @@
[('--utxo-locks-max-age',), {'type': int, 'default': config.DEFAULT_UTXO_LOCKS_MAX_AGE, 'help': 'how long to keep a lock on a UTXO being tracked'}],
]

def welcome_message(action, server_configfile):
cprint(f'Running v{config.__version__} of {config.FULL_APP_NAME}.', 'magenta')

# print some info
cprint(f"Configuration file: {server_configfile}", 'light_grey')
cprint(f"Counterparty database: {config.DATABASE}", 'light_grey')
if config.LOG:
cprint(f'Writing log to file: `{config.LOG}`', 'light_grey')
else:
cprint('Warning: log disabled', 'yellow')
if config.API_LOG:
cprint(f'Writing API accesses log to file: `{config.API_LOG}`', 'light_grey')
else:
cprint('Warning: API log disabled', 'yellow')

if config.VERBOSE:
if config.TESTNET:
cprint('NETWORK: Testnet', 'light_grey')
elif config.REGTEST:
cprint('NETWORK: Regtest', 'light_grey')
else:
cprint('NETWORK: Mainnet', 'light_grey')

pass_str = f":{urlencode(config.BACKEND_PASSWORD)}@"
cleaned_backend_url = config.BACKEND_URL.replace(pass_str, ":*****@")
cprint(f'BACKEND_URL: {cleaned_backend_url}', 'light_grey')
cprint(f'INDEXD_URL: {config.INDEXD_URL}', 'light_grey')
pass_str = f":{urlencode(config.RPC_PASSWORD)}@"
cleaned_rpc_url = config.RPC.replace(pass_str, ":*****@")
cprint(f'RPC: {cleaned_rpc_url}', 'light_grey')

cprint(f"{'-' * 30} {action} {'-' * 30}\n", 'green')


class VersionError(Exception):
pass
def main():
cprint(f'Running v{config.__version__} of {config.FULL_APP_NAME}.', 'magenta')

if os.name == 'nt':
from counterpartylib.lib import util_windows
#patch up cmd.exe's "challenged" (i.e. broken/non-existent) UTF-8 logging
util_windows.fix_win32_unicode()

# Post installation tasks
generate_config_files()
server_configfile = generate_config_files()

# Parse command-line arguments.
parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -119,7 +151,7 @@ def main():
parser_checkdb = subparsers.add_parser('check-db', help='do an integrity check on the database')
add_config_arguments(parser_checkdb, CONFIG_ARGS, configfile)

parser_show_config = subparsers.add_parser('show-config', help='Show counterparty-server configuration')
parser_show_config = subparsers.add_parser('show-params', help='Show counterparty-server configuration')
add_config_arguments(parser_show_config, CONFIG_ARGS, configfile)

args = parser.parse_args()
Expand All @@ -131,11 +163,9 @@ def main():

# Configuration
init_args = dict(database_file=args.database_file,
log_file=args.log_file, api_log_file=args.api_log_file, no_log_files=args.no_log_files,
testnet=args.testnet, testcoin=args.testcoin, regtest=args.regtest,
customnet=args.customnet,
api_limit_rows=args.api_limit_rows,
backend_name=args.backend_name,
backend_connect=args.backend_connect,
backend_port=args.backend_port,
backend_user=args.backend_user,
Expand All @@ -149,12 +179,16 @@ def main():
requests_timeout=args.requests_timeout,
rpc_batch_size=args.rpc_batch_size,
check_asset_conservation=not args.no_check_asset_conservation,
force=args.force, verbose=args.verbose, quiet=args.quiet,
force=args.force,
p2sh_dust_return_pubkey=args.p2sh_dust_return_pubkey,
utxo_locks_max_addresses=args.utxo_locks_max_addresses,
utxo_locks_max_age=args.utxo_locks_max_age)

server.initialise_config(**init_args)
server.initialise_log_config(
verbose=args.verbose, quiet=args.quiet,
log_file=args.log_file, api_log_file=args.api_log_file, no_log_files=args.no_log_files,
testnet=args.testnet, testcoin=args.testcoin, regtest=args.regtest,
)

# set up logging
log.set_up(
Expand All @@ -163,14 +197,12 @@ def main():
log_file=config.LOG,
log_in_console=args.action == 'start'
)

server.initialise_config(**init_args)

logger.info(f'Running v{APP_VERSION} of {APP_NAME}.')

# print some info
if config.LOG:
cprint(f'Writing log to file: `{config.LOG}`', 'light_grey')
if args.action == 'start' and config.API_LOG:
cprint(f'Writing API accesses log to file: `{config.API_LOG}`', 'light_grey')
cprint(f"{'-' * 30} {args.action} {'-' * 30}\n", 'green')
welcome_message(args.action, server_configfile)

# Bootstrapping
if args.action == 'bootstrap':
Expand All @@ -193,8 +225,8 @@ def main():
elif args.action == 'start':
server.start_all(catch_up=args.catch_up)

elif args.action == 'show-config':
server.show_config()
elif args.action == 'show-params':
server.show_params()

elif args.action == 'vacuum':
server.vacuum()
Expand Down
1 change: 1 addition & 0 deletions counterparty-cli/counterpartycli/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def generate_config_files():
if not os.path.exists(client_configfile):
client_known_config = server_to_client_config(server_known_config)
generate_config_file(client_configfile, CLIENT_CONFIG_ARGS, client_known_config)
return server_configfile

def zip_folder(folder_path, zip_path):
zip_file = zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED)
Expand Down
100 changes: 71 additions & 29 deletions counterparty-lib/counterpartylib/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,71 @@ def get_lock():


def initialise(*args, **kwargs):
initialise_log_config(
verbose=kwargs.pop('verbose', False),
quiet=kwargs.pop('quiet', False),
log_file=kwargs.pop('log_file', None),
api_log_file=kwargs.pop('api_log_file', None),
no_log_files=kwargs.pop('no_log_files', False),
testnet=kwargs.get('testnet', False),
testcoin=kwargs.get('testcoin', False),
regtest=kwargs.get('regtest', False),
)
initialise_config(*args, **kwargs)
return initialise_db()


def initialise_config(database_file=None, log_file=None, api_log_file=None, no_log_files=False,
def initialise_log_config(
verbose=False, quiet=False, log_file=None, api_log_file=None, no_log_files=False,
testnet=False, testcoin=False, regtest=False
):
# Log directory
log_dir = appdirs.user_log_dir(appauthor=config.XCP_NAME, appname=config.APP_NAME)
if not os.path.isdir(log_dir):
os.makedirs(log_dir, mode=0o755)

# Set up logging.
config.VERBOSE = verbose
config.QUIET = quiet

network = ''
if testnet:
network += '.testnet'
if regtest:
network += '.regtest'
if testcoin:
network += '.testcoin'

# Log
if no_log_files:
config.LOG = None
elif not log_file: # default location
filename = f'server{network}.log'
config.LOG = os.path.join(log_dir, filename)
else: # user-specified location
config.LOG = log_file

if no_log_files: # no file logging
config.API_LOG = None
elif not api_log_file: # default location
filename = f'server{network}.access.log'
config.API_LOG = os.path.join(log_dir, filename)
else: # user-specified location
config.API_LOG = api_log_file


def initialise_config(database_file=None,
testnet=False, testcoin=False, regtest=False,
api_limit_rows=1000,
backend_name=None, backend_connect=None, backend_port=None,
backend_connect=None, backend_port=None,
backend_user=None, backend_password=None,
indexd_connect=None, indexd_port=None,
backend_ssl=False, backend_ssl_no_verify=False,
backend_poll_interval=None,
rpc_host=None, rpc_port=None,
rpc_user=None, rpc_password=None,
rpc_no_allow_cors=False,
force=False, verbose=False, quiet=False,
force=False,
requests_timeout=config.DEFAULT_REQUESTS_TIMEOUT,
rpc_batch_size=config.DEFAULT_RPC_BATCH_SIZE,
check_asset_conservation=config.DEFAULT_CHECK_ASSET_CONSERVATION,
Expand All @@ -109,6 +158,12 @@ def initialise_config(database_file=None, log_file=None, api_log_file=None, no_l
estimate_fee_per_kb=None,
customnet=None):

# log config alreasdy initialized
logger.debug('VERBOSE: %s', config.VERBOSE)
logger.debug('QUIET: %s', config.QUIET)
logger.debug('LOG: %s', config.LOG)
logger.debug('API_LOG: %s', config.API_LOG)

# Data directory
data_dir = appdirs.user_data_dir(appauthor=config.XCP_NAME, appname=config.APP_NAME, roaming=True)
if not os.path.isdir(data_dir):
Expand Down Expand Up @@ -140,10 +195,13 @@ def initialise_config(database_file=None, log_file=None, api_log_file=None, no_l

if config.TESTNET:
bitcoinlib.SelectParams('testnet')
logger.debug('NETWORK: testnet')
elif config.REGTEST:
bitcoinlib.SelectParams('regtest')
logger.debug('NETWORK: regtest')
else:
bitcoinlib.SelectParams('mainnet')
logger.debug('NETWORK: mainnet')

network = ''
if config.TESTNET:
Expand All @@ -160,31 +218,7 @@ def initialise_config(database_file=None, log_file=None, api_log_file=None, no_l
filename = f'{config.APP_NAME}{network}.db'
config.DATABASE = os.path.join(data_dir, filename)

# Log directory
log_dir = appdirs.user_log_dir(appauthor=config.XCP_NAME, appname=config.APP_NAME)
if not os.path.isdir(log_dir):
os.makedirs(log_dir, mode=0o755)

# Log
if no_log_files:
config.LOG = None
elif not log_file: # default location
filename = f'server{network}.log'
config.LOG = os.path.join(log_dir, filename)
else: # user-specified location
config.LOG = log_file

# Set up logging.
config.VERBOSE = verbose
config.QUIET = quiet

if no_log_files: # no file logging
config.API_LOG = None
elif not api_log_file: # default location
filename = f'server{network}.access.log'
config.API_LOG = os.path.join(log_dir, filename)
else: # user-specified location
config.API_LOG = api_log_file
logger.debug('DATABASE: %s', config.DATABASE)

config.API_LIMIT_ROWS = api_limit_rows

Expand Down Expand Up @@ -258,6 +292,9 @@ def initialise_config(database_file=None, log_file=None, api_log_file=None, no_l
config.BACKEND_URL = 'https://' + config.BACKEND_URL
else:
config.BACKEND_URL = 'http://' + config.BACKEND_URL

cleaned_backend_url = config.BACKEND_URL.replace(f':{config.BACKEND_PASSWORD}@', ':*****@')
logger.debug('BACKEND_URL: %s', cleaned_backend_url)

# Indexd RPC host
if indexd_connect:
Expand Down Expand Up @@ -286,6 +323,8 @@ def initialise_config(database_file=None, log_file=None, api_log_file=None, no_l
# Construct Indexd URL.
config.INDEXD_URL = 'http://' + config.INDEXD_CONNECT + ':' + str(config.INDEXD_PORT)

logger.debug('INDEXD_URL: %s', config.INDEXD_URL)

##############
# THINGS WE SERVE

Expand Down Expand Up @@ -550,7 +589,7 @@ def check_database():
cprint('Database checks complete.', 'green')


def show_config():
def show_params():
output = vars(config)
for k in list(output.keys()):
if k.isupper():
Expand All @@ -572,6 +611,9 @@ def configure_rpc(rpc_password=None):
else:
config.RPC = 'http://' + config.RPC_HOST + ':' + str(config.RPC_PORT) + config.RPC_WEBROOT

cleaned_rpc_url = config.RPC.replace(f':{urlencode(config.RPC_PASSWORD)}@', ':*****@')
logger.debug('RPC: %s', cleaned_rpc_url)


def bootstrap():
data_dir = appdirs.user_data_dir(appauthor=config.XCP_NAME, appname=config.APP_NAME, roaming=True)
Expand Down

0 comments on commit c7ee3da

Please sign in to comment.