def test_stats_reward(self): from zilpool.database.pow import PowResult config = get_database_debug_config() drop_all() init_db(config) totol_rewards = [] for i in range(10): self.save_result(i, i, "miner1", "worker1") self.save_result(i, i * 2 + 10, "miner1", "worker2") self.save_result(i, i * 5, "miner2", "worker1") totol_rewards.append(i + i * 2 + 10 + i * 5) rewards = PowResult.epoch_rewards() assert rewards["count"] == 30 assert rewards["verified"] == 15 assert rewards["rewards"] == sum(totol_rewards) rewards = PowResult.epoch_rewards(miner_wallet="miner1") assert rewards["count"] == 20 assert rewards["verified"] == 10 assert rewards["rewards"] == sum(totol_rewards) - sum( [i * 5 for i in range(10)]) rewards = PowResult.epoch_rewards(miner_wallet="miner1", worker_name="worker1") assert rewards["count"] == 10 assert rewards["verified"] == 5 assert rewards["rewards"] == sum(range(10)) for i in range(10): rewards = PowResult.epoch_rewards(block_num=i) assert rewards["count"] == 3 if i % 2 == 0: assert rewards["verified"] == 3 else: assert rewards["verified"] == 0 assert rewards["rewards"] == totol_rewards[i] rewards = PowResult.epoch_rewards(block_num=(0, 4)) assert rewards["count"] == 3 * 5 assert rewards["rewards"] == sum(totol_rewards[0:5]) rewards = PowResult.epoch_rewards(block_num=9, miner_wallet="miner1") assert rewards["count"] == 2 assert rewards["rewards"] == totol_rewards[9] - 45 rewards = PowResult.epoch_rewards(block_num=9, miner_wallet="miner2") assert rewards["count"] == 1 assert rewards["rewards"] == 45 rewards = PowResult.epoch_rewards(block_num=9, miner_wallet="miner1", worker_name="worker2") assert rewards["count"] == 1 assert rewards["rewards"] == 28 rewards = PowResult.rewards_by_miners(block_num=5) assert len(rewards) == 2
async def start_servers(conf_file=None, host=None, port=None): from zilpool.common import utils, mail, blockchain from zilpool.database import init_db, connect_to_db # merge user's config with default.conf config = utils.merge_config(conf_file) # setup logfile setup_logging(config["logging"]) # init tools mail.EmailClient.set_config(config) # init database connect_to_db(config) init_db(config) # init Zilliqa network APIs blockchain.Zilliqa.init(config) # init app app = web.Application(debug=config["debug"]) init_apis(app, config) init_website(app, config) # start background tasks app["config"] = config app.on_startup.append(backgound.start_background_tasks) app.on_cleanup.append(backgound.cleanup_background_tasks) # start the server if port is None: port = config["api_server"].get("port", "4202") if host is None: host = config["api_server"].get("host", "0.0.0.0") runner = web.AppRunner(app) loop = asyncio.get_event_loop() await runner.setup() #loop.run_until_complete(runner.setup()) site = web.TCPSite(runner, host=host, port=port) #loop.run_until_complete(site.start()) await (site.start()) # update config update_config(site, config) # start stratum server await start_stratum(config)
def test_paginate(self): from zilpool.database.miner import Miner from zilpool.database.zilnode import ZilNode config = get_database_debug_config() drop_all() init_db(config) for i in range(149): node = ZilNode(pub_key=f"pub_key_{i}", pow_fee=i, email=f"email_{i}") node.save() page = ZilNode.paginate(page=0, per_page=50) assert len(page) == 50 assert page[0]["pub_key"] == ZilNode.get_one().pub_key page = ZilNode.paginate(page=2, per_page=50) assert len(page) == 49 all_nodes = ZilNode.get_all() for i in range(49): node = page[i] assert node["pub_key"] == all_nodes[100 + i].pub_key for i in range(201): m = Miner(wallet_address=f"wallet_address_{i}") m.save() all_miners = Miner.get_all() total = len(all_miners) page = Miner.paginate(page=0, per_page=50) assert len(page) == 50 assert page[0]["wallet_address"] == Miner.get_one().wallet_address page = Miner.paginate(page=4, per_page=50) assert len(page) == 1 assert page[0]["wallet_address"] == all_miners[total - 1].wallet_address page = Miner.paginate(page=1, per_page=50) assert len(page) == 50 for i in range(50): miner = page[i] assert miner["wallet_address"] == all_miners[50 + i].wallet_address
def test_admin(self): import time from zilpool.database.zilnode import ZilNodeOwner from zilpool.database.ziladmin import ZilAdminToken config = get_database_debug_config() drop_all() init_db(config) action = "verify_owner_email" email = "*****@*****.**" ZilNodeOwner.create(email=email) token = ZilAdminToken.create_token(action, ext_data={"email": email}) assert token admin_token = ZilAdminToken.verify_token(token, action) assert admin_token assert admin_token.ext_data["email"] == email owner = ZilNodeOwner.get_one(email=email) assert owner assert not owner.email_verified admin_token.do_action() owner.reload() assert owner.email_verified token = ZilAdminToken.create_token(action, ext_data=None, expire_secs=0.1) token2 = ZilAdminToken.create_token(action, ext_data=None, expire_secs=1.5) assert token assert token2 assert token != token2 time.sleep(1) admin_token = ZilAdminToken.verify_token(token, action) admin_token2 = ZilAdminToken.verify_token(token2, action) assert admin_token is None assert admin_token2 is not None
def start_servers(conf_file=None, host=None, port=None): from zilpool.common import utils, mail from zilpool.database import init_db, connect_to_db # merge user's config with default.conf config = utils.merge_config(conf_file) # setup logfile setup_logging(config["logging"]) # init tools mail.EmailClient.set_config(config) # init database connect_to_db(config) init_db(config) # init app app = web.Application(debug=config["debug"]) init_apis(app, config) init_website(app, config) # start the server if port is None: port = config["api_server"].get("port", "4202") if host is None: host = config["api_server"].get("host", "0.0.0.0") runner = web.AppRunner(app) loop = asyncio.get_event_loop() loop.run_until_complete(runner.setup()) site = web.TCPSite(runner, host=host, port=port) loop.run_until_complete(site.start()) # update config update_config(site, config) # start ioloop loop.run_forever()
def test_site_settings(self): from datetime import datetime from zilpool.database.ziladmin import SiteSettings config = get_database_debug_config() drop_all() init_db(config) drop_all() setting0 = SiteSettings.get_setting() assert setting0 is None setting = SiteSettings.update_setting("admin", min_fee=1.2, max_dispatch=300, inc_expire=1.5, notification="notify") assert setting is not None assert setting.min_fee == 1.2 assert setting.max_dispatch == 300 assert setting.inc_expire == 1.5 assert setting.notification == "notify" assert setting.created <= datetime.now() assert setting.admin == "admin" setting2 = SiteSettings.get_setting() assert setting2 == setting setting3 = SiteSettings.update_setting(admin="new admin", notification="notify 22") assert setting3 is not None assert setting3 != setting assert setting3.min_fee == 1.2 assert setting3.max_dispatch == 300 assert setting3.inc_expire == 1.5 assert setting3.notification == "notify 22" assert setting3.created <= datetime.now() assert setting3.admin == "new admin"