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_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_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 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_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_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_active_sp_holder_reward_single_acc_2hf(wallet: Wallet, not_witness_post): post = not_witness_post account = post["author"] def check_reward_operation(start, stop): rewards = [] for i in range(start, stop): wallet.get_block(i, wait_for_block=True) ops = wallet.get_ops_in_block(i) rewards = [data["op"][1] for _, data in ops if data["op"][0].startswith("active")] if rewards: break # Next assert also checks if legacy reward is not provided anymore assert len(rewards) == 1, "Should be provided single active_sp_holder_reward payment." assert rewards[0]["sp_holder"] == account, "Reward should be payed to specified user." return Amount(rewards[0]["reward"]) apply_hardfork(wallet, 2) validate_response(wallet.post_comment(**post), wallet.post_comment.__name__) validate_response(wallet.vote(account, account, post["permlink"], 10000), wallet.vote.__name__) account_before = wallet.get_account(account) dgp_before = wallet.get_dynamic_global_properties() assert account_before["active_sp_holders_pending_sp_reward"] == dgp_before["total_pending_sp"] # 60 sec for testnet, one week for mainnet reward_period_sec = int(wallet.get_config()["SCORUM_ACTIVE_SP_HOLDERS_REWARD_PERIOD"] / 1000000) # microesc -> sec expected_cashout = to_date(account_before["last_vote_time"], tmdelta={"seconds": reward_period_sec}) actual_cashout = to_date(account_before["active_sp_holders_cashout_time"]) assert actual_cashout == expected_cashout, \ "Actual cashout time calculated incorrectly: '%s', expected '%s'" % \ (date_to_str(actual_cashout), date_to_str(expected_cashout)) blocks_to_wait = int(reward_period_sec / 3) + 1 last_block = dgp_before["head_block_number"] reward = check_reward_operation(last_block, last_block + blocks_to_wait) account_after = wallet.get_account(account) assert account_before["balance"] == account_after["balance"] # until advertising is locked assert account_before["scorumpower"] != account_after["scorumpower"] reset_cashout = to_date(account_after["active_sp_holders_cashout_time"]) expected_cashout = to_date(account_before["active_sp_holders_cashout_time"], tmdelta={"seconds": reward_period_sec}) assert reset_cashout == expected_cashout, \ "Cashout time for active_sp_holder_reward was not reset: '%s', expected '%s'" % \ (date_to_str(reset_cashout), date_to_str(expected_cashout)) dgp_after = wallet.get_dynamic_global_properties() assert dgp_before["total_pending_scr"] == dgp_after["total_pending_scr"] # until advertising is locked assert Amount(dgp_after["total_pending_sp"]) == Amount("0 SP") assert dgp_before["total_pending_sp"] != dgp_after["total_pending_sp"] assert dgp_before["total_scorumpower"] != dgp_after["total_scorumpower"] balance_change = Amount(account_after["scorumpower"]) - Amount(account_before["scorumpower"]) assert balance_change == reward, \ "Balance change is not equal with reward: '%s', expected '%s'" % (balance_change, reward) last_block = dgp_after["head_block_number"] check_reward_operation(last_block + 1, last_block + blocks_to_wait)
def test_get_discussions_by_created_exclude_tags(wallet: Wallet, by_tags, exclude_tags, expected_cnt, only_posts): for post in only_posts: wallet.post_comment(**post) posts = wallet.get_discussions_by( "created", **{ "tags": by_tags, "limit": 100, "tags_logical_and": False, "exclude_tags": exclude_tags }) validate_response(posts, wallet.get_discussions_by.__name__) assert len(posts) == expected_cnt for post in posts: tags = set(json.loads(post["json_metadata"])["tags"]) assert not tags.intersection(set(exclude_tags))
def test_get_discussions_by_created(wallet: Wallet, alice_post, bob_post): validate_response(wallet.post_comment(**alice_post), wallet.post_comment.__name__) validate_response(wallet.post_comment(**bob_post), wallet.post_comment.__name__) posts = wallet.get_discussions_by( "created", **{ "tags": ["hockey", "football"], "limit": 100, "tags_logical_and": False }) validate_response(posts, wallet.get_discussions_by.__name__) assert len(posts) == 2 # check that returned latest created post assert posts[0]["permlink"] == "bob-post" and posts[0]["author"] == "bob", \ "Posts were not created in correct order" assert posts[1]["permlink"] == "alice-post" and posts[1]["author"] == "alice", \ "Posts were not created in correct order"
def test_get_posts_and_comments_pagination(wallet: Wallet, posts): for post in posts: wallet.post_comment(**post) limit = 2 # should be > 1 responses = [] response = wallet.get_posts_and_comments(**{"limit": limit}) validate_response(response, wallet.get_posts_and_comments.__name__) while len(response) >= limit: responses += response[:-1] response = wallet.get_posts_and_comments( **{ "start_author": response[-1]["author"], "start_permlink": response[-1]["permlink"], "limit": 100 }) validate_response(response, wallet.get_posts_and_comments.__name__) responses += response assert len(responses) == len(posts)
def test_active_sp_holder_reward_legacy(wallet: Wallet, post, accounts): op_name = "active_sp_holders_reward_legacy" validate_response(wallet.post_comment(**post), wallet.post_comment.__name__) for acc in accounts: validate_response(wallet.vote(acc, post["author"], post["permlink"]), wallet.vote.__name__) last_block = wallet.get_dynamic_global_properties()["head_block_number"] # e.g. next N blocks should be payed active_sp_holders_reward_legacy for i in range(last_block, last_block + 5): wallet.get_block(i, wait_for_block=True) ops = wallet.get_ops_in_block(i) rewards = [data["op"][1]["rewarded"] for _, data in ops if data["op"][0] == op_name][0] assert len(rewards) == len(accounts), "Was provided unexpected amount of '%s' operations." % op_name for acc, _ in rewards: assert acc in accounts, "Provided payment to unexpected account: '%s'" % acc
def test_validate_get_content( wallet: Wallet, post_with_multilvl_comments, initdelegate_post, bob_comment_lv1, alice_comment_lv2 ): for post in post_with_multilvl_comments: validate_response(wallet.post_comment(**post), wallet.post_comment.__name__) time_config = wallet.get_config() def validate_cashout_interval(comment: dict): date_start = to_date(comment['created']) date_finish = to_date(comment['cashout_time']) delta = date_finish - date_start cashout_window = int(time_config["SCORUM_CASHOUT_WINDOW_SECONDS"]) assert delta.total_seconds() == cashout_window def validate_url(comment: dict): if comment['parent_author']: assert comment['url'] == '/{category}/@{root_author}/{root_permlink}#@{author}/{permlink}' \ .format(category=comment['category'], root_author=post['author'], root_permlink=post['permlink'], author=comment['author'], permlink=comment['permlink']) else: assert comment['url'] == '/{}/@{}/{}'.format(comment['category'], comment['author'], comment['permlink']) def validate_content(comment, comment_kwargs, parent=None): for key, value in comment_kwargs.items(): assert comment[key] == value, '{} value differs from expected'.format(key) assert comment['category'] == initdelegate_post['parent_permlink'] expected_depth = parent['depth'] + 1 if parent else 0 assert comment['depth'] == expected_depth assert comment['root_title'] == initdelegate_post['title'] assert comment['root_comment'] == post['id'] validate_cashout_interval(comment) validate_url(comment) post = wallet.get_content(initdelegate_post['author'], initdelegate_post['permlink']) validate_content(post, initdelegate_post) comment_level_1 = wallet.get_content(bob_comment_lv1['author'], bob_comment_lv1['permlink']) validate_content(comment_level_1, bob_comment_lv1, post) comment_level_2 = wallet.get_content(alice_comment_lv2['author'], alice_comment_lv2['permlink']) validate_content(comment_level_2, alice_comment_lv2, comment_level_1)
def test_get_discussions_by_author_order(wallet: Wallet, initdelegate_post): permlinks = ["initdelegate-post-%d" % i for i in range(1, 5)] post_creation_interval = int( int(wallet.get_config()["SCORUM_MIN_ROOT_COMMENT_INTERVAL"]) / 1000000) for permlink in permlinks: initdelegate_post["permlink"] = permlink res = wallet.post_comment(**initdelegate_post) validate_response(res, wallet.post_comment.__name__) if permlink != permlinks[-1]: time.sleep(post_creation_interval) # 5 min for each post on prod discussions = wallet.get_discussions_by( "author", **{ "start_author": DEFAULT_WITNESS, "limit": len(permlinks) }) validate_response(discussions, "get_discussions_by_author") total_posts = len(permlinks) for current in range(0, total_posts): opposite = total_posts - current - 1 assert permlinks[current] == discussions[opposite]["permlink"], \ "Broken posts order, Post %d should be on %d position." % (current, opposite)
def test_get_paid_posts_comments_by_author(wallet: Wallet, initdelegate_post): # REQUIREMENTS: return an array of posts and comments belonging to the given author that have reached cashout time. # This method should allow for pagination. The query should include field that will filter posts/comments that # have 0 SP rewards. The posts/comments should be sorted by last_payout field in the descending order. permlinks = ["initdelegate-post-%d" % i for i in range(1, 2)] post_creation_interval = int( int(wallet.get_config()["SCORUM_MIN_ROOT_COMMENT_INTERVAL"]) / 1000000) for permlink in permlinks: initdelegate_post["permlink"] = permlink res = wallet.post_comment(**initdelegate_post) validate_response(res, wallet.post_comment.__name__) # print(wallet.get_content(DEFAULT_WITNESS, permlink)) if permlink != permlinks[-1]: time.sleep(post_creation_interval) # 5 min for each post on prod posts = wallet.get_paid_posts_comments_by_author(**{ "start_author": DEFAULT_WITNESS, "limit": len(permlinks) }) assert len(posts) == len(permlinks)
def test_get_content(wallet: Wallet, posts): for post in posts: wallet.post_comment(**post) validate_response(wallet.get_content(post['author'], post['permlink']), wallet.get_content.__name__)
def test_vote_operation_valid(wallet: Wallet, post, weight): account = post["author"] validate_response(wallet.post_comment(**post), wallet.post_comment.__name__) validate_response(wallet.vote(account, account, post["permlink"], weight=weight), wallet.vote.__name__)
def test_vote_operation_2hf_invalid(wallet: Wallet, post, weight): apply_hardfork(wallet, 2) account = post["author"] validate_response(wallet.post_comment(**post), wallet.post_comment.__name__) validate_error_response(wallet.vote(account, account, post["permlink"], weight=weight), wallet.vote.__name__)
def test_post_comment(wallet: Wallet, posts): for post in posts: validate_response(wallet.post_comment(**post), wallet.post_comment.__name__)