From 4f7d711a3384056439fcb6f4af64463596fdb363 Mon Sep 17 00:00:00 2001 From: clangenb <37865735+clangenb@users.noreply.github.com> Date: Mon, 31 May 2021 09:59:02 +0200 Subject: [PATCH] add purge keystore promt in bot-community.py init. + some formatting (#84) Closes #83 --- client/bot-community.py | 53 +++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/client/bot-community.py b/client/bot-community.py index 76348b5d..7442dbb7 100755 --- a/client/bot-community.py +++ b/client/bot-community.py @@ -18,6 +18,8 @@ """ import argparse +import glob +import os import geojson @@ -29,6 +31,7 @@ from py_client.ipfs import Ipfs, ICONS_PATH from py_client.communities import populate_locations, generate_community_spec, meta_json +KEYSTORE_PATH = './my_keystore' NUMBER_OF_LOCATIONS = 100 MAX_POPULATION = 12 * NUMBER_OF_LOCATIONS @@ -36,7 +39,7 @@ def random_community_spec(bootstrappers, ipfs_cid): point = geojson.utils.generate_random("Point", boundingBox=[-56, 41, -21, 13]) locations = populate_locations(point, NUMBER_OF_LOCATIONS) - print("created " + str(len(locations)) + " random locations around " + str(point)) + print(f'created {len(locations)} random locations around {point}.') name = '#' + '-'.join(RandomWords().random_words(count=1)) symbol = name[1:4].upper() @@ -53,16 +56,30 @@ def init_bootstrappers(client=Client()): return bootstrappers +def purge_keystore_prompt(): + accounts = glob.glob(KEYSTORE_PATH + '/*') + if accounts: + print(f'Keystore already contains {len(accounts)} accounts.') + should_clear = input('Do you want to purge the keystore? [y, n]') + if should_clear == 'y': + [os.remove(f) for f in accounts] + print('Purged the keystore.') + else: + print('Leaving keystore as is.') + + def init(client: str, port: str): + purge_keystore_prompt() + client = Client(rust_client=client, port=port) ipfs_cid = Ipfs.add_recursive(ICONS_PATH) - print("initializing community") + print('initializing community') b = init_bootstrappers(client) specfile = random_community_spec(b, ipfs_cid) - print("generated community spec: ", specfile) + print(f'generated community spec: {specfile}') cid = client.new_community(specfile) - print("created community with cid: ", cid) - f = open("cid.txt", "w") + print(f'created community with cid: {cid}') + f = open('cid.txt', 'w') f.write(cid) f.close() @@ -70,13 +87,13 @@ def init(client: str, port: str): def register_participants(client: Client, accounts, cid): bal = [client.balance(a, cid=cid) for a in accounts] total = sum(bal) - print("****** money supply is " + str(total)) - f = open("bot-stats.csv", "a") - f.write(str(len(accounts)) + ", " + str(total) + "\n") + print(f'****** money supply is {total}') + f = open('bot-stats.csv', 'a') + f.write(f'{len(accounts)}, {total}\n') f.close() if total > 0: n_newbies = min(floor(len(accounts) / 4.0), MAX_POPULATION - len(accounts)) - print("*** adding " + str(n_newbies) + " newbies") + print(f'*** adding {n_newbies} newbies') if n_newbies > 0: newbies = [] for n in range(0, n_newbies): @@ -85,15 +102,15 @@ def register_participants(client: Client, accounts, cid): client.await_block() accounts = client.list_accounts() - print("registering " + str(len(accounts)) + " participants") + print(f'registering {len(accounts)} participants') for p in accounts: - # print("registering " + p) + # print(f'registering {p}') client.register_participant(p, cid) def perform_meetup(client: Client, meetup, cid): n = len(meetup) - print("Performing meetup with " + str(n) + " participants") + print(f'Performing meetup with {n} participants') claims = [client.new_claim(p, n, cid) for p in meetup] @@ -105,19 +122,19 @@ def perform_meetup(client: Client, meetup, cid): def run(client: str, port: int): client = Client(rust_client=client, port=port) - f = open("cid.txt", "r") + f = open('cid.txt', 'r') cid = f.read() - print("cid is " + cid) + print(f'cid is {cid}') phase = client.get_phase() - print("phase is " + phase) + print(f'phase is {phase}') accounts = client.list_accounts() - print("number of known accounts: " + str(len(accounts))) + print(f'number of known accounts: {len(accounts)}') if phase == 'REGISTERING': register_participants(client, accounts, cid) client.await_block() if phase == 'ATTESTING': meetups = client.list_meetups(cid) - print("****** Performing " + str(len(meetups)) + " meetups") + print(f'****** Performing {len(meetups)} meetups') for meetup in meetups: perform_meetup(client, meetup, cid) client.await_block() @@ -125,7 +142,7 @@ def run(client: str, port: int): def benchmark(client: str, port: int): py_client = Client(rust_client=client, port=port) - print("will grow population forever") + print('will grow population forever') while True: run(client, port) py_client.await_block()