Skip to content

Commit

Permalink
chore: add reset_and_pause_abci
Browse files Browse the repository at this point in the history
  • Loading branch information
0xArdi committed Oct 4, 2023
1 parent 8236e7a commit ed97b16
Show file tree
Hide file tree
Showing 16 changed files with 1,019 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/valory/skills/reset_pause_abci/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Reset and pause abci

## Description

This module contains the ABCI reset and pause skill for an AEA. It implements an ABCI
application.

## Behaviours

* `ResetAndPauseBehaviour`

Reset state.

* `ResetPauseABCIConsensusBehaviour`

This behaviour manages the consensus stages for the reset and pause abci app.

## Handlers

* `ResetPauseABCIHandler`
* `HttpHandler`
* `SigningHandler`

25 changes: 25 additions & 0 deletions packages/valory/skills/reset_pause_abci/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2021-2022 Valory AG
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ------------------------------------------------------------------------------

"""This module contains the Reset & Pause skill for an AEA."""

from aea.configurations.base import PublicId


PUBLIC_ID = PublicId.from_str("valory/reset_pause_abci:0.1.0")
99 changes: 99 additions & 0 deletions packages/valory/skills/reset_pause_abci/behaviours.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2021-2023 Valory AG
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ------------------------------------------------------------------------------

"""This module contains the behaviours for the 'reset_pause_abci' skill."""

from abc import ABC
from typing import Generator, Set, Type, cast

from packages.valory.skills.abstract_round_abci.base import BaseSynchronizedData
from packages.valory.skills.abstract_round_abci.behaviours import (
AbstractRoundBehaviour,
BaseBehaviour,
)
from packages.valory.skills.reset_pause_abci.models import Params, SharedState
from packages.valory.skills.reset_pause_abci.payloads import ResetPausePayload
from packages.valory.skills.reset_pause_abci.rounds import (
ResetAndPauseRound,
ResetPauseAbciApp,
)


class ResetAndPauseBaseBehaviour(BaseBehaviour, ABC):
"""Reset behaviour."""

@property
def synchronized_data(self) -> BaseSynchronizedData:
"""Return the synchronized data."""
return cast(
BaseSynchronizedData,
cast(SharedState, self.context.state).synchronized_data,
)

@property
def params(self) -> Params:
"""Return the params."""
return cast(Params, self.context.params)


class ResetAndPauseBehaviour(ResetAndPauseBaseBehaviour):
"""Reset and pause behaviour."""

matching_round = ResetAndPauseRound

def async_act(self) -> Generator:
"""
Do the action.
Steps:
- Trivially log the behaviour.
- Sleep for configured interval.
- Build a registration transaction.
- Send the transaction and wait for it to be mined.
- Wait until ABCI application transitions to the next round.
- Go to the next behaviour (set done event).
"""
# + 1 because `period_count` starts from 0
n_periods_done = self.synchronized_data.period_count + 1
reset_tm_nodes = n_periods_done % self.params.reset_tendermint_after == 0
if reset_tm_nodes:
tendermint_reset = yield from self.reset_tendermint_with_wait()
if not tendermint_reset:
return
else:
yield from self.wait_from_last_timestamp(self.params.reset_pause_duration)
self.context.logger.info("Period end.")
self.context.benchmark_tool.save(self.synchronized_data.period_count)

payload = ResetPausePayload(
self.context.agent_address, self.synchronized_data.period_count
)
yield from self.send_a2a_transaction(payload, reset_tm_nodes)
yield from self.wait_until_round_end()
self.set_done()


class ResetPauseABCIConsensusBehaviour(AbstractRoundBehaviour):
"""This behaviour manages the consensus stages for the reset_pause_abci app."""

initial_behaviour_cls = ResetAndPauseBehaviour
abci_app_cls = ResetPauseAbciApp
behaviours: Set[Type[BaseBehaviour]] = {
ResetAndPauseBehaviour, # type: ignore
}
91 changes: 91 additions & 0 deletions packages/valory/skills/reset_pause_abci/dialogues.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2021-2023 Valory AG
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ------------------------------------------------------------------------------

"""This module contains the classes required for dialogue management."""

from packages.valory.skills.abstract_round_abci.dialogues import (
AbciDialogue as BaseAbciDialogue,
)
from packages.valory.skills.abstract_round_abci.dialogues import (
AbciDialogues as BaseAbciDialogues,
)
from packages.valory.skills.abstract_round_abci.dialogues import (
ContractApiDialogue as BaseContractApiDialogue,
)
from packages.valory.skills.abstract_round_abci.dialogues import (
ContractApiDialogues as BaseContractApiDialogues,
)
from packages.valory.skills.abstract_round_abci.dialogues import (
HttpDialogue as BaseHttpDialogue,
)
from packages.valory.skills.abstract_round_abci.dialogues import (
HttpDialogues as BaseHttpDialogues,
)
from packages.valory.skills.abstract_round_abci.dialogues import (
IpfsDialogue as BaseIpfsDialogue,
)
from packages.valory.skills.abstract_round_abci.dialogues import (
IpfsDialogues as BaseIpfsDialogues,
)
from packages.valory.skills.abstract_round_abci.dialogues import (
LedgerApiDialogue as BaseLedgerApiDialogue,
)
from packages.valory.skills.abstract_round_abci.dialogues import (
LedgerApiDialogues as BaseLedgerApiDialogues,
)
from packages.valory.skills.abstract_round_abci.dialogues import (
SigningDialogue as BaseSigningDialogue,
)
from packages.valory.skills.abstract_round_abci.dialogues import (
SigningDialogues as BaseSigningDialogues,
)
from packages.valory.skills.abstract_round_abci.dialogues import (
TendermintDialogue as BaseTendermintDialogue,
)
from packages.valory.skills.abstract_round_abci.dialogues import (
TendermintDialogues as BaseTendermintDialogues,
)


AbciDialogue = BaseAbciDialogue
AbciDialogues = BaseAbciDialogues


HttpDialogue = BaseHttpDialogue
HttpDialogues = BaseHttpDialogues


SigningDialogue = BaseSigningDialogue
SigningDialogues = BaseSigningDialogues


LedgerApiDialogue = BaseLedgerApiDialogue
LedgerApiDialogues = BaseLedgerApiDialogues


ContractApiDialogue = BaseContractApiDialogue
ContractApiDialogues = BaseContractApiDialogues


TendermintDialogue = BaseTendermintDialogue
TendermintDialogues = BaseTendermintDialogues


IpfsDialogue = BaseIpfsDialogue
IpfsDialogues = BaseIpfsDialogues
19 changes: 19 additions & 0 deletions packages/valory/skills/reset_pause_abci/fsm_specification.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
alphabet_in:
- DONE
- NO_MAJORITY
- RESET_AND_PAUSE_TIMEOUT
default_start_state: ResetAndPauseRound
final_states:
- FinishedResetAndPauseErrorRound
- FinishedResetAndPauseRound
label: ResetPauseAbciApp
start_states:
- ResetAndPauseRound
states:
- FinishedResetAndPauseErrorRound
- FinishedResetAndPauseRound
- ResetAndPauseRound
transition_func:
(ResetAndPauseRound, DONE): FinishedResetAndPauseRound
(ResetAndPauseRound, NO_MAJORITY): FinishedResetAndPauseErrorRound
(ResetAndPauseRound, RESET_AND_PAUSE_TIMEOUT): FinishedResetAndPauseErrorRound
51 changes: 51 additions & 0 deletions packages/valory/skills/reset_pause_abci/handlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2021-2023 Valory AG
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ------------------------------------------------------------------------------

"""This module contains the handler for the 'reset_pause_abci' skill."""

from packages.valory.skills.abstract_round_abci.handlers import (
ABCIRoundHandler as BaseABCIRoundHandler,
)
from packages.valory.skills.abstract_round_abci.handlers import (
ContractApiHandler as BaseContractApiHandler,
)
from packages.valory.skills.abstract_round_abci.handlers import (
HttpHandler as BaseHttpHandler,
)
from packages.valory.skills.abstract_round_abci.handlers import (
IpfsHandler as BaseIpfsHandler,
)
from packages.valory.skills.abstract_round_abci.handlers import (
LedgerApiHandler as BaseLedgerApiHandler,
)
from packages.valory.skills.abstract_round_abci.handlers import (
SigningHandler as BaseSigningHandler,
)
from packages.valory.skills.abstract_round_abci.handlers import (
TendermintHandler as BaseTendermintHandler,
)


ABCIHandler = BaseABCIRoundHandler
HttpHandler = BaseHttpHandler
SigningHandler = BaseSigningHandler
LedgerApiHandler = BaseLedgerApiHandler
ContractApiHandler = BaseContractApiHandler
TendermintHandler = BaseTendermintHandler
IpfsHandler = BaseIpfsHandler
55 changes: 55 additions & 0 deletions packages/valory/skills/reset_pause_abci/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2021-2023 Valory AG
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ------------------------------------------------------------------------------

"""This module contains the shared state for the 'reset_pause_abci' application."""

from packages.valory.skills.abstract_round_abci.models import BaseParams
from packages.valory.skills.abstract_round_abci.models import (
BenchmarkTool as BaseBenchmarkTool,
)
from packages.valory.skills.abstract_round_abci.models import Requests as BaseRequests
from packages.valory.skills.abstract_round_abci.models import (
SharedState as BaseSharedState,
)
from packages.valory.skills.reset_pause_abci.rounds import Event, ResetPauseAbciApp


MARGIN = 5

Requests = BaseRequests
BenchmarkTool = BaseBenchmarkTool


class SharedState(BaseSharedState):
"""Keep the current shared state of the skill."""

abci_app_cls = ResetPauseAbciApp

def setup(self) -> None:
"""Set up."""
super().setup()
ResetPauseAbciApp.event_to_timeout[
Event.ROUND_TIMEOUT
] = self.context.params.round_timeout_seconds
ResetPauseAbciApp.event_to_timeout[Event.RESET_AND_PAUSE_TIMEOUT] = (
self.context.params.reset_pause_duration + MARGIN
)


Params = BaseParams
Loading

0 comments on commit ed97b16

Please sign in to comment.