Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests/performatives #107

Merged
merged 5 commits into from
Sep 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 101 additions & 22 deletions tests/test_skills/test_keep3r_job/test_behaviours.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
Keep3rJobRoundBehaviour,
PathSelectionBehaviour,
PerformWorkBehaviour,
RawTx,
WaitingBehaviour,
)
from packages.keep3r_co.skills.keep3r_job.handlers import (
Expand Down Expand Up @@ -63,6 +64,9 @@
SynchronizedData,
WaitingRound,
)
from packages.valory.contracts.gnosis_safe.contract import (
PUBLIC_ID as GNOSIS_SAFE_CONTRACT_ID,
)
from packages.valory.contracts.keep3r_test_job.contract import (
PUBLIC_ID as TEST_JOB_CONTRACT_ID,
)
Expand All @@ -88,6 +92,18 @@
AGENT_ADDRESS = "0x1Cc0771e65FC90308DB2f7Fd02482ac4d1B82A18"
SOME_CONTRACT_ADDRESS = "0xaed599aadfee8e32cedb59db2b1120d33a7bacfd"

DUMMY_RAW_TX: RawTx = {
"from": SOME_CONTRACT_ADDRESS,
"to": SOME_CONTRACT_ADDRESS,
"data": "0x7cf5dab00000000000000000000000000000000000000000000000000000000000000005",
"nonce": 0,
"value": 0,
"gas": 43242,
"maxFeePerGas": 2000000000,
"maxPriorityFeePerGas": 1000000000,
"chainId": 1,
}


class DummyRoundId:
"""Dummy class for setting round_id for exit condition."""
Expand Down Expand Up @@ -144,7 +160,7 @@ def fast_forward(self, data: Optional[Dict[str, Any]] = None) -> None:
)
assert self.current_behaviour.behaviour_id == self.behaviour_class.behaviour_id

def mock_keep3r_v1_call(self, contract_callable: str, data: Any) -> None:
def mock_read_keep3r_v1(self, contract_callable: str, data: Any) -> None:
"""Mock keep3r V1 contract call"""

self.mock_contract_api_request(
Expand All @@ -163,9 +179,29 @@ def mock_keep3r_v1_call(self, contract_callable: str, data: Any) -> None:
),
)

def mock_test_job_call(self, contract_callable: str, data: Any) -> None:
"""Mock TestJob contract call"""
def mock_keep3r_v1_raw_tx(self, contract_callable: str, data: Any) -> None:
"""Mock keep3r V1 raw transaction"""

self.mock_contract_api_request(
request_kwargs=dict(
performative=ContractApiMessage.Performative.GET_RAW_TRANSACTION,
callable=contract_callable,
),
contract_id=str(KEEP3R_V1_CONTRACT_ID),
response_kwargs=dict(
performative=ContractApiMessage.Performative.RAW_TRANSACTION,
callable=contract_callable,
raw_transaction=ContractApiMessage.RawTransaction(
ledger_id="ethereum",
body={"data": data},
),
),
)

def mock_workable_call(self, data: bool) -> None:
"""Mock TestJob workable contract call"""

contract_callable = "workable"
self.mock_contract_api_request(
request_kwargs=dict(
performative=ContractApiMessage.Performative.GET_STATE,
Expand All @@ -182,6 +218,46 @@ def mock_test_job_call(self, contract_callable: str, data: Any) -> None:
),
)

def mock_build_work_tx_call(self, data: RawTx) -> None:
"""Mock build work transaction"""

contract_callable = "build_work_tx"
self.mock_contract_api_request(
request_kwargs=dict(
performative=ContractApiMessage.Performative.GET_RAW_TRANSACTION,
callable=contract_callable,
),
contract_id=str(TEST_JOB_CONTRACT_ID),
response_kwargs=dict(
performative=ContractApiMessage.Performative.RAW_TRANSACTION,
callable=contract_callable,
raw_transaction=ContractApiMessage.RawTransaction(
ledger_id="ethereum",
body={"data": data}, # type: ignore
),
),
)

def mock_build_safe_raw_tx(self) -> None:
"""Mock build safe raw transaction"""

self.mock_contract_api_request(
request_kwargs=dict(
performative=ContractApiMessage.Performative.GET_RAW_TRANSACTION,
),
contract_id=str(GNOSIS_SAFE_CONTRACT_ID),
response_kwargs=dict(
performative=ContractApiMessage.Performative.RAW_TRANSACTION,
callable="get_raw_safe_transaction_hash",
raw_transaction=ContractApiMessage.RawTransaction(
ledger_id="ethereum",
body={
"tx_hash": "0xb0e6add595e00477cf347d09797b156719dc5233283ac76e4efce2a674fe72d9"
},
),
),
)

def mock_ethereum_ledger_state_call(self, data: Any) -> None:
"""Mock ethereum ledger get state call"""

Expand Down Expand Up @@ -212,7 +288,7 @@ class TestPathSelectionBehaviour(Keep3rJobFSMBehaviourBaseCase):
def test_blacklisted(self) -> None:
"""Test path_selection to blacklisted."""

self.mock_keep3r_v1_call("blacklist", True)
self.mock_read_keep3r_v1("blacklist", True)
self.mock_a2a_transaction()
self._test_done_flag_set()
self.end_round(done_event=Event.BLACKLISTED)
Expand All @@ -222,7 +298,7 @@ def test_blacklisted(self) -> None:
def test_insufficient_funds(self) -> None:
"""Test path_selection to insufficient funds."""

self.mock_keep3r_v1_call("blacklist", False)
self.mock_read_keep3r_v1("blacklist", False)
self.mock_ethereum_get_balance(amount=-1)
self.mock_a2a_transaction()
self._test_done_flag_set()
Expand All @@ -232,9 +308,9 @@ def test_insufficient_funds(self) -> None:
def test_not_bonded(self) -> None:
"""Test path_selection to not bonded."""

self.mock_keep3r_v1_call("blacklist", False)
self.mock_read_keep3r_v1("blacklist", False)
self.mock_ethereum_get_balance(amount=0)
self.mock_keep3r_v1_call("bondings", 0)
self.mock_read_keep3r_v1("bondings", 0)
self.mock_a2a_transaction()
self._test_done_flag_set()
self.end_round(done_event=Event.NOT_BONDED)
Expand All @@ -243,10 +319,10 @@ def test_not_bonded(self) -> None:
def test_not_activated(self) -> None:
"""Test path_selection to not activated."""

self.mock_keep3r_v1_call("blacklist", False)
self.mock_read_keep3r_v1("blacklist", False)
self.mock_ethereum_get_balance(amount=0)
self.mock_keep3r_v1_call("bondings", 1)
self.mock_keep3r_v1_call("BOND", 3 * SECONDS_PER_DAY)
self.mock_read_keep3r_v1("bondings", 1)
self.mock_read_keep3r_v1("BOND", 3 * SECONDS_PER_DAY)
self.mock_get_latest_block(block={"timestamp": 0})
self.mock_a2a_transaction()
self._test_done_flag_set()
Expand All @@ -256,10 +332,10 @@ def test_not_activated(self) -> None:
def test_healthy(self) -> None:
"""Test path_selection to healthy."""

self.mock_keep3r_v1_call("blacklist", False)
self.mock_read_keep3r_v1("blacklist", False)
self.mock_ethereum_get_balance(amount=0)
self.mock_keep3r_v1_call("bondings", 1)
self.mock_keep3r_v1_call("BOND", 3 * SECONDS_PER_DAY)
self.mock_read_keep3r_v1("bondings", 1)
self.mock_read_keep3r_v1("BOND", 3 * SECONDS_PER_DAY)
self.mock_get_latest_block(block={"timestamp": 3 * SECONDS_PER_DAY + 1})
self.mock_a2a_transaction()
self._test_done_flag_set()
Expand All @@ -275,7 +351,8 @@ class TestBondingBehaviour(Keep3rJobFSMBehaviourBaseCase):
def test_bonding_tx(self) -> None:
"""Test bonding tx"""

self.mock_keep3r_v1_call("build_bond_tx", {})
self.mock_keep3r_v1_raw_tx("build_bond_tx", DUMMY_RAW_TX)
self.mock_build_safe_raw_tx()
self.mock_a2a_transaction()
self._test_done_flag_set()
self.end_round(done_event=Event.BONDING_TX)
Expand All @@ -291,8 +368,8 @@ class TestWaitingBehaviour(Keep3rJobFSMBehaviourBaseCase):
def test_waiting(self) -> None:
"""Test waiting"""

self.mock_keep3r_v1_call("bondings", 0)
self.mock_keep3r_v1_call("BOND", 1)
self.mock_read_keep3r_v1("bondings", 0)
self.mock_read_keep3r_v1("BOND", 1)
self.mock_get_latest_block(block={"timestamp": 2})
self.mock_a2a_transaction()
self._test_done_flag_set()
Expand All @@ -308,7 +385,8 @@ class TestActivationBehaviour(Keep3rJobFSMBehaviourBaseCase):
def test_activation_tx(self) -> None:
"""Test activation tx"""

self.mock_keep3r_v1_call("build_activation_tx", {})
self.mock_keep3r_v1_raw_tx("build_activation_tx", DUMMY_RAW_TX)
self.mock_build_safe_raw_tx()
self.mock_a2a_transaction()
self._test_done_flag_set()
self.end_round(done_event=Event.ACTIVATION_TX)
Expand All @@ -324,7 +402,7 @@ class TestGetJobsBehaviour(Keep3rJobFSMBehaviourBaseCase):
def test_get_jobs(self) -> None:
"""Test get_jobs."""

self.mock_keep3r_v1_call("get_jobs", ["some_job_address"])
self.mock_read_keep3r_v1("get_jobs", ["some_job_address"])
self.mock_a2a_transaction()
self._test_done_flag_set()
self.end_round(done_event=Event.DONE)
Expand All @@ -339,7 +417,8 @@ class TestPerformWorkBehaviour(Keep3rJobFSMBehaviourBaseCase):
def test_run(self) -> None:
"""Test perform work."""

self.mock_test_job_call("build_work_tx", {})
self.mock_build_work_tx_call(DUMMY_RAW_TX)
self.mock_build_safe_raw_tx()
self.mock_a2a_transaction()
self._test_done_flag_set()
self.end_round(done_event=Event.WORK_TX)
Expand Down Expand Up @@ -383,7 +462,7 @@ def test_is_workable(
) -> None:
"""Test is_workable."""

self.mock_test_job_call("workable", is_workable)
self.mock_workable_call(is_workable)
self.behaviour.act_wrapper()
self.mock_a2a_transaction()
self._test_done_flag_set()
Expand All @@ -408,7 +487,7 @@ def test_is_profitable(
) -> None:
"""Test is_profitable."""

self.mock_keep3r_v1_call("credits", credits)
self.mock_read_keep3r_v1("credits", credits)
self.behaviour.act_wrapper()
self.mock_a2a_transaction()
self._test_done_flag_set()
Expand All @@ -434,5 +513,5 @@ def test_top_up(
self.behaviour.act_wrapper()
self.mock_a2a_transaction()
self._test_done_flag_set()
self.end_round(done_event=Event.TOP_UP)
self.end_round(done_event=event)
Comment on lines -437 to +516
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addresses this comment

assert self.current_behaviour.behaviour_id == next_round.round_id