def test_get_posts_and_comments(wallet: Wallet, posts): for post in posts: wallet.post_comment(**post) response = wallet.get_posts_and_comments(**{"limit": len(posts)}) validate_response(response, wallet.get_posts_and_comments.__name__) assert len(response) == len(posts)
def test_devcommittee_active_withdraw(wallet: Wallet): amount = Amount("10.000000000 SP") validate_response( wallet.devcommittee_withdraw_vesting(DEFAULT_WITNESS, amount), wallet.devcommittee_withdraw_vesting.__name__ ) proposals = wallet.list_proposals() validate_response(proposals, wallet.list_proposals.__name__) assert len(proposals) == 1, "Was created %d proposals, expected only one: %s" % (len(proposals), proposals) validate_response(wallet.proposal_vote(DEFAULT_WITNESS, proposals[0]["id"]), wallet.proposal_vote.__name__) transfers = wallet.get_devcommittee_transfers() validate_response(transfers, wallet.get_devcommittee_transfers.__name__) assert len(transfers) == 1, "Was created more transfers then was expected." withdraw = transfers[0] expect(withdraw["status"] == "active") expect(Amount(withdraw["withdrawn"]) == Amount("0 SP")) # e.g. any payment was not provided yet expect(withdraw["op"][0] == "proposal_virtual") expect(withdraw["op"][1]["proposal_op"][0] == "development_committee_withdraw_vesting") expect(Amount(withdraw["op"][1]["proposal_op"][1]["vesting_shares"]) == amount) assert_expectations()
def test_get_ops_history(wallet: Wallet, block_num): wallet.get_block(block_num, wait_for_block=True) response = wallet.get_ops_history(-1, 100, 0) # get all operations validate_response(response, wallet.get_ops_history.__name__) assert len( response ) == block_num, "Each block should be provided 'producer_reward' operation."
def test_wallet_spend_cash(): """ Testing Spending cash """ wallet = Wallet(20) wallet.spend_cash(10) assert wallet.balance == 10
def test_wallet_spend_cash_raises_exception_on_insufficient_amount(): """ Asserts one cant spend more money than what's in the wallet """ wallet = Wallet() with pytest.raises(InsufficientAmount): wallet.spend_cash(100)
def test_registration_schedule(wallet: Wallet, genesis: Genesis): def expected_reward_value(schedule): registration_bonus = Amount(genesis['registration_bonus']) total_accounts = len(wallet.list_accounts()) for s in schedule: if total_accounts <= s['users']: return registration_bonus * s['bonus_percent'] // 100 return registration_bonus * schedule[-1]['bonus_percent'] // 100 registration_schedule = list(genesis['registration_schedule']) total_users_in_schedule = 0 for stage in registration_schedule: total_users_in_schedule += stage['users'] stage['users'] = total_users_in_schedule names = ['martin', 'doug', 'kevin', 'joe', 'jim'] accounts = [Account(name) for name in names] for account in accounts: wallet.create_account_by_committee( DEFAULT_WITNESS, account.name, active=account.active.get_public_key(), owner=account.owner.get_public_key(), posting=account.posting.get_public_key()) assert wallet.get_account_sp_balance(account.name) == expected_reward_value(registration_schedule), \ '{} sp balance differs from expected'.format(account.name)
def test_get_ops_history_pagination(wallet: Wallet, block_num): wallet.get_block(block_num, wait_for_block=True) response = wallet.get_ops_history(1, 1, 0) # get first operation validate_response(response, wallet.get_ops_history.__name__) assert len(response) == 1, "Should be returned single operation." assert response[0][ 0] == 0, "Should be returned first operation (with 'id' = 0)"
def test_single_winner_pending_payments(wallet_3hf: Wallet, budget): change_auction_coeffs(wallet_3hf, [100], budget['type']) update_budget_time(wallet_3hf, budget, start=3, deadline=300) winner = copy(budget) winner.update({"owner": "test.test1", 'uuid': gen_uid()}) potato = copy(budget) # e.g. not winner (4th place: gold, silver, bronze, potato) potato.update({"owner": "test.test2", 'uuid': gen_uid()}) looser = copy(budget) looser.update({"owner": "test.test3", 'uuid': gen_uid(), 'balance': "0.100000000 SCR"}) budgets = [winner, potato, looser] wallet_3hf.broadcast_multiple_ops( "create_budget_operation", budgets, {winner['owner'], potato['owner'], looser['owner']} ) [update_budget_balance(wallet_3hf, b) for b in budgets] assert winner['per_block'] == potato['per_block'] assert looser['per_block'] != winner['per_block'] assert Amount(winner[OUTGO]) == Amount(potato['per_block']) and Amount(winner[INCOME]) == Amount() assert Amount(potato[OUTGO]) == Amount() and Amount(potato[INCOME]) == Amount(potato['per_block']) assert Amount(looser[OUTGO]) == Amount() and Amount(looser[INCOME]) == Amount(looser['per_block'])
def test_cashout_budgets_distribution(wallet_3hf: Wallet, budget, count, sync_start): budgets, last_block = create_budgets(wallet_3hf, budget, count, sync_start) owners = [b['owner'] for b in budgets] cfg = wallet_3hf.get_config() cashout_blocks_cnt = int(cfg["SCORUM_ADVERTISING_CASHOUT_PERIOD_SEC"] / cfg["SCORUM_BLOCK_INTERVAL"]) # collect data before cashout capital_before, accounts_balances_before = get_affected_balances(wallet_3hf, owners) [update_budget_balance(wallet_3hf, b) for b in budgets] # wait until cashout wallet_3hf.get_block(last_block + cashout_blocks_cnt, wait_for_block=True) # collect data after cashout capital_after, accounts_balances_after = get_affected_balances(wallet_3hf, owners) [update_budget_balance(wallet_3hf, b) for b in budgets] # calc delta between 'after', 'before' cashout states capital_delta = get_capital_delta(capital_before, capital_after) accounts_balances_delta = get_accounts_delta(accounts_balances_after, accounts_balances_before) # calc total payments total = get_total_sums(budgets, accounts_balances_delta, cashout_blocks_cnt) # provide checks assert capital_delta['dev_pool_scr_balance'] == total['to_dev_pool'] assert capital_delta['content_balancer_scr'] + \ capital_delta['content_reward_fund_scr_balance'] == total['to_activity_pool'] assert capital_delta['dev_pool_scr_balance'] + \ capital_delta['content_balancer_scr'] + \ capital_delta['content_reward_fund_scr_balance'] == total['spend']
def setUp(self): self.blockchain = Blockchain() self.chain = self.blockchain.chain self.mempool = self.blockchain.mempool self.network = self.blockchain.network self.wallet = Wallet() self.initial_address = self.wallet.addresses[0]
def test_invalid_coeffs(wallet_3hf: Wallet, budget_type, coeffs): response = wallet_3hf.development_committee_change_budgets_auction_properties( DEFAULT_WITNESS, coeffs, 86400, budget_type ) validate_error_response(response, wallet_3hf.development_committee_change_budgets_auction_properties.__name__) proposals = wallet_3hf.list_proposals() assert len(proposals) == 0, "Was created %d proposals, expected only one: %s" % (len(proposals), proposals) assert coeffs != wallet_3hf.get_auction_coefficients(budget_type), "Coefficients was changed, but they shouldn't."
def test_wallet_add_cash(): """ Testing adding cash """ wallet = Wallet(10) wallet.add_cash(90) assert wallet.balance == 100
def test_update_budget_current_winners(wallet_3hf: Wallet, opened_budgets, json_metadata): budget = opened_budgets[0] winners_before = wallet_3hf.get_current_winners(budget['type']) wallet_3hf.update_budget(budget['uuid'], budget['owner'], json_metadata, budget['type']) winners_after = wallet_3hf.get_current_winners(budget['type']) assert all(winners_before[i]['id'] == winners_after[i]['id'] for i in range(len(winners_before)))
def test_get_comments(wallet: Wallet, post_with_multilvl_comments): posts = post_with_multilvl_comments # ugly workaround for post in posts: wallet.post_comment(**post) for i in range(1, len(posts)): comments = wallet.get_comments(posts[i - 1]["author"], posts[i - 1]["permlink"], i) validate_response(comments, wallet.get_comments.__name__) assert len(comments) == 1, "Should be returned single comment"
def read_wallet(id=None): w = Wallet() if id == None: response = {'mensagem':'Falda nome da wallet!'} elif w.read_wallet(id): response = {'nome':id, 'wallet':w.public} else: response = {'mensagem':f'Não existe cateira para: {id}, Favor criar!'} return jsonify(response), 200
def test_update_budget_invalid_meta(wallet_3hf: Wallet, budget, json_metadata): update_budget_time(wallet_3hf, budget) wallet_3hf.create_budget(**budget) update_budget_balance(wallet_3hf, budget) response = wallet_3hf.update_budget(budget['uuid'], budget['owner'], json_metadata, budget['type']) validate_error_response(response, wallet_3hf.update_budget.__name__, RE_PARSE_ERROR) budget_obj = wallet_3hf.get_budget(budget['uuid'], budget['type']) assert budget_obj['json_metadata'] == budget["json_metadata"]
def test_cashout_scr_rewards(wallet_3hf: Wallet, budget, post): balancer_delay = 7 # blocks to wait until SCR will be in each required pool cfg = wallet_3hf.get_config() # Advertising cashout_blocks count adv_cash_blocks = int(cfg["SCORUM_ADVERTISING_CASHOUT_PERIOD_SEC"] / cfg["SCORUM_BLOCK_INTERVAL"]) # Post / comment cashout blocks count # post_cash_blocks = int(cfg["SCORUM_CASHOUT_WINDOW_SECONDS"] / cfg["SCORUM_BLOCK_INTERVAL"]) # Active SP holders cashout blocks count asph_cash_blocks = int(cfg["SCORUM_ACTIVE_SP_HOLDERS_REWARD_PERIOD"] / 1000000 / cfg["SCORUM_BLOCK_INTERVAL"]) - 1 update_budget_time(wallet_3hf, budget, deadline=300) response = wallet_3hf.create_budget(**budget) budget_cashout_block = response['block_num'] + adv_cash_blocks + balancer_delay wallet_3hf.get_block(response['block_num'] + balancer_delay, wait_for_block=True) wallet_3hf.post_comment(**post) # post_cashout_block = response['block_num'] + post_cash_blocks response = wallet_3hf.vote(DEFAULT_WITNESS, post['author'], post['permlink']) active_sph_cashout_block = response['block_num'] + asph_cash_blocks blocks_ops = [ (budget_cashout_block, 'producer_reward'), (active_sph_cashout_block, 'active_sp_holders_reward'), # (post_cashout_block, 'author_reward'), # (post_cashout_block, 'curator_reward') ] for cashout_block, op in blocks_ops: wallet_3hf.get_block(cashout_block, wait_for_block=True) ops = check_virt_ops(wallet_3hf, cashout_block, cashout_block, {op}) assert any(Amount(data['reward']) > 0 and 'SCR' in data['reward'] for name, data in ops if name == op)
def test_transfer(wallet: Wallet): initdelegate_balance_before = wallet.get_account_scr_balance('initdelegate') amount = initdelegate_balance_before - Amount('5.000000000 SCR') alice_balance_before = wallet.get_account_scr_balance('alice') print(wallet.transfer('initdelegate', 'alice', amount)) initdelegate_balance_after = wallet.get_account_scr_balance('initdelegate') alice_balance_after = wallet.get_account_scr_balance('alice') assert initdelegate_balance_after == initdelegate_balance_before - amount assert alice_balance_after == alice_balance_before + amount
def test_get_posts_and_comments_truncate(wallet: Wallet, posts, truncate_body): for post in posts: wallet.post_comment(**post) response = wallet.get_posts_and_comments(**{ "limit": len(posts), "truncate_body": truncate_body }) validate_response(response, wallet.get_posts_and_comments.__name__) assert len(response) == len(posts) for p in response: assert len(p["body"]) <= truncate_body
def test_account_zero_withdraw(wallet: Wallet, account, amount): response = wallet.withdraw(account, amount) validate_response(response, wallet.withdraw.__name__) response = wallet.withdraw(account, Amount("0 SP")) validate_response(response, wallet.withdraw.__name__) transfers = wallet.get_account_transfers(account) expect(len(transfers) == 2, "Was created more withdrawals then was expected.") expect(transfers[0][1]["status"] == "interrupted") expect(transfers[1][1]["status"] == "empty") assert_expectations()
def test_budget_impact_on_rewards(wallet: Wallet, genesis: Genesis): def get_reward_per_block(): last_confirmed_block = wallet.get_witness( DEFAULT_WITNESS)['last_confirmed_block_num'] sp_balance_before_block_confirm = wallet.get_account_sp_balance( DEFAULT_WITNESS) circulating_capital_before = wallet.get_circulating_capital() new_confirmed_block = last_confirmed_block while new_confirmed_block == last_confirmed_block: new_confirmed_block = wallet.get_witness( DEFAULT_WITNESS)['last_confirmed_block_num'] witness_reward = wallet.get_account_sp_balance( DEFAULT_WITNESS) - sp_balance_before_block_confirm full_content_reward = wallet.get_circulating_capital( ) - circulating_capital_before activity_content_reward = full_content_reward * 95 / 100 assert witness_reward == full_content_reward - activity_content_reward, 'witness reward per block != expected' return full_content_reward def calculate_rewards_from_genesis(): blocks_per_month = 864000 days_in_month = 30 days_in_2_years = 730 rewards_supply = Amount(genesis['rewards_supply']) rewards_per_block = rewards_supply * days_in_month / days_in_2_years / blocks_per_month return rewards_per_block ''' content reward before balancer decrease it ''' expected_content_reward_on_start = calculate_rewards_from_genesis() content_reward_on_start = get_reward_per_block() assert expected_content_reward_on_start == content_reward_on_start, 'content reward on start != expected' ''' wait for balancer decrease content reward ''' wallet.get_block(25, wait_for_block=True, time_to_wait=60) content_reward_after_balancer_decrease = get_reward_per_block() assert content_reward_after_balancer_decrease < content_reward_on_start, 'content reward not decreased by balancer' ''' open budget with large amount and short lifetime to instantly increase reward pool which enforce balancer to increase content reward ''' wallet.create_budget(DEFAULT_WITNESS, Amount("10000.000000000 SCR"), fmt_time_from_now(), fmt_time_from_now(30)) content_reward_after_budget_open = get_reward_per_block() assert content_reward_after_budget_open > content_reward_after_balancer_decrease, \ 'content reward not increased after budget open'
def test_get_contents(wallet: Wallet, posts): for post in posts: wallet.post_comment(**post) response = wallet.get_contents([{ "author": p["author"], "permlink": p["permlink"] } for p in posts]) validate_response(response, wallet.get_contents.__name__) assert len(response) == len( posts), "Should be returned all created posts and comments"
def test_close_before_starttime(wallet_3hf: Wallet, budget, moderator): update_budget_time(wallet_3hf, budget, start=30, deadline=60) # to delay opening time for budget budget_balance = Amount(budget["balance"]) balance_before = wallet_3hf.get_account_scr_balance(budget["owner"]) wallet_3hf.create_budget(**budget) update_budget_balance(wallet_3hf, budget) # update budget params / set budget id balance_after_create = wallet_3hf.get_account_scr_balance(budget["owner"]) assert balance_before - balance_after_create == budget_balance empower_advertising_moderator(wallet_3hf, moderator) response = wallet_3hf.close_budget_by_advertising_moderator( budget["uuid"], moderator, budget["type"]) validate_response( response, wallet_3hf.close_budget_by_advertising_moderator.__name__) balance_after_close = wallet_3hf.get_account_scr_balance(budget["owner"]) assert balance_after_close == balance_after_create + budget_balance check_virt_ops( wallet_3hf, response['block_num'], response['block_num'], { 'close_budget_by_advertising_moderator', 'budget_closing', 'budget_owner_income' }) assert len(wallet_3hf.get_budgets([budget['owner']], budget['type'])) == 0 assert len(wallet_3hf.list_buddget_owners(budget_type=budget['type'])) == 0
def test_get_parents(wallet: Wallet, posts): for post in posts: wallet.post_comment(**post) content = wallet.get_content(post["author"], post["permlink"]) parents = wallet.get_parents(**{ "author": post["author"], "permlink": post["permlink"] }) validate_response(parents, wallet.get_parents.__name__) assert len(parents) == content["depth"] current = post for parent in parents: assert current["parent_permlink"] == parent["permlink"] current = parent
def test_transfer_invalid_amount(wallet: Wallet): initdelegate_balance_before = wallet.get_account_scr_balance('initdelegate') amount = initdelegate_balance_before + Amount('0.000000001 SCR') alice_balance_before = wallet.get_account_scr_balance('alice') response = wallet.transfer('initdelegate', 'alice', amount) initdelegate_balance_after = wallet.get_account_scr_balance('initdelegate') alice_balance_after = wallet.get_account_scr_balance('alice') assert initdelegate_balance_after == initdelegate_balance_before assert alice_balance_after == alice_balance_before assert 'Account does not have sufficient funds for transfer' in response['error']['message']
def test_create_budgets(wallet_3hf: Wallet, node, opened_budgets_same_acc, budget): assert len(wallet_3hf.get_budgets([budget['owner']], budget['type'])) == len(opened_budgets_same_acc) budgets_summary = wallet_3hf.get_dynamic_global_properties()['advertising'] for b in opened_budgets_same_acc: update_budget_balance(wallet_3hf, b) # check that sum of budgets 'param' == summary 'param' in DGP assert all( sum([Amount(b[v]) for b in opened_budgets_same_acc], Amount("0 SCR")) == Amount( budgets_summary[DGP_BUDGETS[budget['type']]][k] ) for k, v in DGP_PARAMS_MAP.items() )
def test_devcommittee_withdraw_gt_pool(wallet: Wallet): devcommittee = wallet.get_development_committee() validate_response(devcommittee, wallet.get_development_committee.__name__) amount = Amount(devcommittee["sp_balance"]) + Amount("100.000000000 SP") validate_response( wallet.devcommittee_withdraw_vesting(DEFAULT_WITNESS, amount), wallet.devcommittee_withdraw_vesting.__name__ ) proposals = wallet.list_proposals() validate_response(proposals, wallet.list_proposals.__name__) expect(len(proposals) == 1, "Was created %d proposals, expected only one: %s" % (len(proposals), proposals)) validate_error_response(wallet.proposal_vote(DEFAULT_WITNESS, proposals[0]["id"]), wallet.proposal_vote.__name__)
def test_unknown_uuid(wallet_3hf: Wallet, opened_budgets, uuid, moderator): empower_advertising_moderator(wallet_3hf, moderator) validate_error_response( wallet_3hf.close_budget_by_advertising_moderator( uuid(), moderator, opened_budgets[0]["type"]), wallet_3hf.close_budget_by_advertising_moderator.__name__, RE_BUDGET_NOT_EXIST)
def post_comment(post_kwargs, node): with Wallet(node.get_chain_id(), node.rpc_endpoint, node.genesis.get_accounts()) as w: w.login("", "") w.get_api_by_name('database_api') w.get_api_by_name('network_broadcast_api') return w.post_comment(**post_kwargs)
def create_wallet(): response_cod = None json = request.get_json() if json == None: response_cod = 400 response = {'mensagem':'Falta wallet_id!'} elif json['wallet_id'] != '': w = Wallet() w.create_wallet(json['wallet_id']) if w.read_wallet(json['wallet_id']): response = {'mensagem':'Carteira já existe!', 'carteira':w.public} response = {'mensagem':f'wallet({json["wallet_id"]}) -> {w.public}'} else: response = {'mensagem':'Falta wallet_id!'} response_cod = 400 return jsonify(response), response_cod if response_cod else 201