Skip to content

Commit

Permalink
add purge keystore promt in bot-community.py init. + some formatting (#…
Browse files Browse the repository at this point in the history
…84)

Closes #83
  • Loading branch information
clangenb authored May 31, 2021
1 parent e72c585 commit 4f7d711
Showing 1 changed file with 35 additions and 18 deletions.
53 changes: 35 additions & 18 deletions client/bot-community.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"""
import argparse
import glob
import os

import geojson

Expand All @@ -29,14 +31,15 @@
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


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()
Expand All @@ -53,30 +56,44 @@ 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()


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):
Expand All @@ -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]

Expand All @@ -105,27 +122,27 @@ 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()


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()
Expand Down

0 comments on commit 4f7d711

Please sign in to comment.