Skip to content

Commit

Permalink
fix: set the mech tools after the redeeming round
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamantios committed Sep 26, 2024
1 parent 78c0864 commit 4ef3827
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,7 @@ def _build_payload(self, redeem_tx_hex: Optional[str] = None) -> RedeemPayload:
agent = self.context.agent_address
tx_submitter = self.matching_round.auto_round_id()
benchmarking_enabled = self.benchmarking_mode.enabled
serialized_tools = json.dumps(self.mech_tools)
policy = self.policy.serialize()
utilized_tools = json.dumps(self.utilized_tools)
condition_ids = json.dumps(list(self.redeemed_condition_ids))
Expand All @@ -922,6 +923,7 @@ def _build_payload(self, redeem_tx_hex: Optional[str] = None) -> RedeemPayload:
tx_submitter,
redeem_tx_hex,
benchmarking_enabled,
serialized_tools,
policy,
utilized_tools,
condition_ids,
Expand All @@ -948,7 +950,8 @@ def _normal_act(self) -> Generator[None, None, Optional[RedeemPayload]]:
if not self.redeeming_progress.check_finished:
self.redeeming_progress.cleaned = yield from self._clean_redeem_info()

payload = RedeemPayload(self.context.agent_address)
serialized_tools = json.dumps(self.mech_tools)
payload = RedeemPayload(self.context.agent_address, mech_tools=serialized_tools)
if self.redeeming_progress.cleaned:
redeem_tx_hex = yield from self._prepare_safe_tx()
if redeem_tx_hex is not None:
Expand Down
1 change: 1 addition & 0 deletions packages/valory/skills/decision_maker_abci/payloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class MultisigTxPayload(BaseTxPayload):
class RedeemPayload(MultisigTxPayload):
"""Represents a transaction payload for preparing an on-chain transaction for redeeming."""

mech_tools: str = "[]"
policy: Optional[str] = None
utilized_tools: Optional[str] = None
redeemed_condition_ids: Optional[str] = None
Expand Down
39 changes: 38 additions & 1 deletion packages/valory/skills/decision_maker_abci/states/redeem.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"""This module contains the redeem state of the decision-making abci app."""

from enum import Enum
from typing import Optional, Tuple, Type
from typing import Any, Optional, Tuple, Type, cast

from packages.valory.skills.abstract_round_abci.base import (
BaseSynchronizedData,
Expand All @@ -37,18 +37,38 @@
)


IGNORED = "ignored"
MECH_TOOLS_FIELD = "mech_tools"


class RedeemRound(TxPreparationRound):
"""A round in which the agents prepare a tx to redeem the winnings."""

payload_class: Type[MultisigTxPayload] = RedeemPayload
mech_tools_name = get_name(SynchronizedData.available_mech_tools)
selection_key = TxPreparationRound.selection_key + (
mech_tools_name,
get_name(SynchronizedData.policy),
get_name(SynchronizedData.utilized_tools),
get_name(SynchronizedData.redeemed_condition_ids),
get_name(SynchronizedData.payout_so_far),
)
none_event = Event.NO_REDEEMING

@property
def most_voted_payload_values(
self,
) -> Tuple[Any, ...]:
"""Get the most voted payload values without considering the mech tools."""
most_voted_payload_values = super().most_voted_payload_values
# sender does not matter for the init as the `data` property used below to obtain the dictionary ignores it
most_voted_payload = RedeemPayload(IGNORED, *most_voted_payload_values)
most_voted_payload_dict = most_voted_payload.data
mech_tools = most_voted_payload_dict.pop(MECH_TOOLS_FIELD, None)
if mech_tools is None:
raise ValueError(f"`{MECH_TOOLS_FIELD}` must not be `None`")
return tuple(most_voted_payload_dict.values())

def end_block(self) -> Optional[Tuple[BaseSynchronizedData, Enum]]:
"""Process the end of the block."""
res = super().end_block()
Expand All @@ -65,4 +85,21 @@ def end_block(self) -> Optional[Tuple[BaseSynchronizedData, Enum]]:
self.synchronized_data.db.update(**update)
self.block_confirmations = 1

if res is None:
return None

synced_data, event = cast(Tuple[SynchronizedData, Enum], res)

# also update the mech tools if there is a majority, because the overridden property does not include it
if event != self.no_majority_event:
most_voted_payload_values = self.payload_values_count.most_common()[0][0]
# sender does not matter for the init as the `data` property used below to obtain the dictionary ignores it
most_voted_payload = RedeemPayload(IGNORED, *most_voted_payload_values)
mech_tools_update = most_voted_payload.mech_tools
updated_data = synced_data.update(
self.synchronized_data_class,
**{get_name(SynchronizedData.available_mech_tools): mech_tools_update},
)
return updated_data, event

return res

0 comments on commit 4ef3827

Please sign in to comment.