Skip to content

Commit

Permalink
Improve stability of tests in the voting_power_of_a_comment group
Browse files Browse the repository at this point in the history
  • Loading branch information
Michał Kudela committed Jan 9, 2025
1 parent 98a4106 commit 4108a14
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest

import test_tools as tt
from hive_local_tools.functional.python.operation import Account


@pytest.fixture()
Expand Down Expand Up @@ -43,3 +44,11 @@ def prepare_environment(request: pytest.FixtureRequest) -> tuple[tt.InitNode, tt
tt.logger.info(f"Test uses hardfork : {node.api.database.get_hardfork_properties().current_hardfork_version}")

return node, wallet


@pytest.fixture()
def alice(prepare_environment) -> Account:
node, wallet = prepare_environment
alice = Account("alice", node, wallet)
alice.update_account_info()
return alice
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
from __future__ import annotations

import operator
from typing import TYPE_CHECKING

import pytest

import test_tools as tt

from .block_log.generate_block_log import NUMBER_OF_REPLIES_TO_POST

if TYPE_CHECKING:
from .conftest import Account


@pytest.mark.parametrize("hardfork_version", [27])
@pytest.mark.parametrize(("weight", "op"), [(100, [operator.gt, operator.gt]), (-100, [operator.eq, operator.lt])])
@pytest.mark.parametrize(("weight", "operator_"), [(100, operator.gt), (-100, operator.lt)])
def test_voting_power_of_a_comment_on_hf27(
prepare_environment: tuple[tt.InitNode, tt.Wallet], hardfork_version: str, weight: int, op: list[operator]
prepare_environment: tuple[tt.InitNode, tt.Wallet],
hardfork_version: str,
weight: int,
operator_: operator,
alice: Account,
) -> None:
"""
Behavior on hardfork 27:
Expand All @@ -25,31 +33,47 @@ def test_voting_power_of_a_comment_on_hf27(
- vote (13) using both downvote_manabar and voting_manabar, is also of maximum power of vote, but current_mana of voting_manabar are reduced
- Each subsequent new vote has a lower voting power (rshares).
"""
voting_mana_history = []
downvoting_mana_history = []
vote_absorbing_both_mana_bars = 13

node, wallet = prepare_environment

vote_absorbing_both_mana_bars = 13
previous_vote_power = 0
for comment_num in range(NUMBER_OF_REPLIES_TO_POST):
wallet.api.vote("alice", "bob", f"comment-{comment_num}", weight)
logging_comment_manabar(node, "alice")

if comment_num == 0:
previous_vote_power = get_rshares(node, f"comment-{comment_num}")
wallet.api.vote(alice.name, "bob", f"comment-{comment_num}", weight)
alice.update_account_info()
voting_mana_history.append(alice.vote_manabar.current_mana)
downvoting_mana_history.append(alice.downvote_manabar.current_mana)
logging_comment_manabar(node, alice.name)

rshares_history = get_rshares_history(node)

for num, (rshares, voting_mana, downvote_mana) in enumerate(
zip(rshares_history, voting_mana_history, downvoting_mana_history)
):
previous_rshares_value = rshares_history[num - 1]
previous_voting_mana_value = voting_mana_history[num - 1]
previous_downvoting_mana_value = downvoting_mana_history[num - 1]
if num == 0:
continue

operator_ = op[0] if comment_num < vote_absorbing_both_mana_bars else op[1]
actual_vote_power = get_rshares(node, f"comment-{comment_num}")

err = f"comment-{comment_num} should {previous_vote_power=} should be {operator_.__name__} {actual_vote_power=}"
assert operator_(previous_vote_power, actual_vote_power), err

previous_vote_power = actual_vote_power
if num < vote_absorbing_both_mana_bars and weight < 0:
assert rshares == previous_rshares_value, "rshares should not change during the first 13 downvotes."
assert downvote_mana < previous_downvoting_mana_value, "The account did not incur a downvote mana cost."
else:
err = f"comment-{num} should {previous_rshares_value} should be {operator_.__name__} {rshares}"
assert operator_(previous_rshares_value, rshares), err
assert downvote_mana == previous_downvoting_mana_value, "Downvote mana should not change at this stage."
assert voting_mana < previous_voting_mana_value, "The account did not incur a vote mana cost."


@pytest.mark.parametrize("hardfork_version", ["current"])
@pytest.mark.parametrize("weight", [100, -100])
def test_voting_power_of_a_comment_on_current_hardfork(
prepare_environment: tuple[tt.InitNode, tt.Wallet], hardfork_version: str, weight: int
prepare_environment: tuple[tt.InitNode, tt.Wallet],
hardfork_version: str,
weight: int,
alice: Account,
) -> None:
"""
Behavior on hardfork 28+:
Expand All @@ -62,26 +86,48 @@ def test_voting_power_of_a_comment_on_current_hardfork(
"""
node, wallet = prepare_environment

maximum_vote_power = None
voting_mana_history = []
downvoting_mana_history = []
for comment_num in range(NUMBER_OF_REPLIES_TO_POST):
try:
wallet.api.vote("alice", "bob", f"comment-{comment_num}", weight)
wallet.api.vote(alice.name, "bob", f"comment-{comment_num}", weight)
except tt.exceptions.CommunicationError as error:
message = str(error)
assert "Account does not have enough mana" in message, "Not found expected error message."

rshares_history = get_rshares_history(node)
assert all(rs == rshares_history[0] for rs in rshares_history), "Votes should have the same power value."

for num, (voting_mana, downvote) in enumerate(zip(voting_mana_history, downvoting_mana_history)):
if num == 0:
continue

previous_voting_mana_value = voting_mana_history[num - 1]
previous_downvoting_mana_value = downvoting_mana_history[num - 1]

if weight < 0:
assert comment_num == 62, "Incorrect maximum number of votes and downvotes made with maximum power."
if num < 12:
assert downvote < previous_downvoting_mana_value, "Downvote mana did not change after voting."
err_msg = "Upvote mana decreased unexpectedly during downvote."
assert voting_mana == alice.rc_manabar.max_mana, err_msg
else:
err_msg = "When downvoting with 0% downvote mana, the upvote mana was not correctly retrieved."
assert voting_mana < previous_voting_mana_value, err_msg
else:
assert comment_num == 50, "Incorrect maximum number of votes made with maximum power."
assert voting_mana < previous_voting_mana_value, "Upvote mana did not change after voting."
err_msg = "Downvote mana changed unexpectedly during upvote."
assert all(d_mana == downvoting_mana_history[0] for d_mana in downvoting_mana_history), err_msg
return

actual_vote_power = get_rshares(node, f"comment-{comment_num}")
alice.update_account_info()
voting_mana_history.append(alice.vote_manabar.current_mana)
downvoting_mana_history.append(alice.downvote_manabar.current_mana)

tt.logger.info(f"Vote for comment-{comment_num}")
logging_comment_manabar(node, "alice")
if comment_num == 0:
maximum_vote_power = actual_vote_power
continue
logging_comment_manabar(node, alice.name)

assert maximum_vote_power == actual_vote_power, (
"The account cast a comment vote with a power other than the maximum. "
f"Maximum vote power: {maximum_vote_power}, current_vote_power: {actual_vote_power}"
)
raise AssertionError("Account `alice` mana has been not exhausted.")


Expand All @@ -101,15 +147,14 @@ def logging_comment_manabar(node: tt.InitNode, name: str) -> None:
)


def get_rshares(node: tt.InitNode, permlink: str) -> int:
vop = (
node.api.account_history.get_account_history(
account="alice", include_reversible=True, start=-1, limit=1, operation_filter_high=256
)
.history[0][1]
.op
)

assert vop.type == "effective_comment_vote_operation"
assert vop.value.permlink == permlink
return vop.value.rshares
def get_rshares_history(node: tt.InitNode) -> list[int]:
return [
o[1].op.value.rshares
for o in node.api.account_history.get_account_history(
account="alice",
include_reversible=True,
start=-1,
limit=1000,
operation_filter_high=256,
).history
]

0 comments on commit 4108a14

Please sign in to comment.