Skip to content
This repository has been archived by the owner on Nov 15, 2021. It is now read-only.

np-api-server auto exit #1021

Merged
merged 7 commits into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ Changelog

All notable changes to this project are documented in this file.

[0.9.2] In progress
-------------------
- Fix shutdown operations when initializing np-api-server and setting minpeers/maxpeers, opening a wallet, or changing databases


[0.9.1] 2019-09-16
------------------
- Reformat wallet verbose output to include big-endian scripthash
Expand Down
2 changes: 1 addition & 1 deletion neo/Prompt/PromptData.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ def close_wallet():
Blockchain.Default().PersistCompleted.on_change -= PromptData.Wallet.ProcessNewBlock
PromptData.Wallet.Close()
PromptData.Wallet = None
print("Closed wallet %s" % path)
print(f"Closed wallet {path}")
return True
32 changes: 19 additions & 13 deletions neo/bin/api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
from neo.Wallets.utils import to_aes_key
from neo.Implementations.Wallets.peewee.UserWallet import UserWallet
from neo.Network.p2pservice import NetworkService
from neo.Settings import settings
from neo.Settings import settings, PrivnetConnectionError
from neo.Utils.plugin import load_class_from_path
from neo.Wallets.utils import to_aes_key
from contextlib import suppress
Expand Down Expand Up @@ -152,7 +152,11 @@ async def setup_and_start(loop):
elif args.testnet:
settings.setup_testnet()
elif args.privnet:
settings.setup_privnet()
try:
settings.setup_privnet()
except PrivnetConnectionError as e:
print(e)
raise SystemExit
elif args.coznet:
settings.setup_coznet()

Expand Down Expand Up @@ -180,21 +184,21 @@ def set_max_peers(num_peers) -> bool:
if minpeers and maxpeers:
if minpeers > maxpeers:
print("minpeers setting cannot be bigger than maxpeers setting")
return
raise SystemExit
if not set_min_peers(minpeers) or not set_max_peers(maxpeers):
return
raise SystemExit
elif minpeers:
if not set_min_peers(minpeers):
return
raise SystemExit
if minpeers > settings.CONNECTED_PEER_MAX:
if not set_max_peers(minpeers):
return
raise SystemExit
elif maxpeers:
if not set_max_peers(maxpeers):
return
raise SystemExit
if maxpeers < settings.CONNECTED_PEER_MIN:
if not set_min_peers(maxpeers):
return
raise SystemExit

if args.syslog or args.syslog_local is not None:
# Setup the syslog facility
Expand Down Expand Up @@ -223,15 +227,15 @@ def set_max_peers(num_peers) -> bool:
if args.wallet:
if not os.path.exists(args.wallet):
print("Wallet file not found")
return
raise SystemExit

passwd = os.environ.get('NEO_PYTHON_JSONRPC_WALLET_PASSWORD', None)
if not passwd:
try:
passwd = prompt("[password]> ", is_password=True)
except KeyboardInterrupt:
print("Wallet opening cancelled")
return
raise SystemExit

password_key = to_aes_key(passwd)
try:
Expand All @@ -240,7 +244,7 @@ def set_max_peers(num_peers) -> bool:

except Exception as e:
print(f"Could not open wallet {e}")
return
raise SystemExit
else:
wallet = None

Expand Down Expand Up @@ -269,7 +273,7 @@ def set_max_peers(num_peers) -> bool:
sys.exit()
api_server_rpc = rpc_class(wallet=wallet)

runner = web.AppRunner(api_server_rpc.app)
runner = web.AppRunner(api_server_rpc.app, handle_signals=True)
await runner.setup()
site = web.TCPSite(runner, args.host, args.port_rpc)
await site.start()
Expand All @@ -282,7 +286,7 @@ def set_max_peers(num_peers) -> bool:
logger.error(err)
sys.exit()
api_server_rest = rest_api()
runner = web.AppRunner(api_server_rest.app)
runner = web.AppRunner(api_server_rpc.app, handle_signals=True)
await runner.setup()
site = web.TCPSite(runner, args.host, args.port_rpc)
await site.start()
Expand Down Expand Up @@ -327,7 +331,9 @@ def main():

wallet = main_task.result()
if wallet:
path = wallet._path
wallet.Close()
logger.info(f"Closed wallet {path}")


if __name__ == "__main__":
Expand Down