-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(simulator): Add a trigger to stop the simulator when a condition…
… is satisfied
- Loading branch information
Showing
6 changed files
with
167 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Copyright 2023 Hathor Labs | ||
# | ||
# 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. | ||
|
||
from hathor.simulator.miner.abstract_miner import AbstractMiner | ||
from hathor.simulator.miner.geometric_miner import GeometricMiner | ||
|
||
__all__ = [ | ||
'AbstractMiner', | ||
'GeometricMiner', | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Copyright 2023 Hathor Labs | ||
# | ||
# 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. | ||
|
||
from abc import ABC, abstractmethod | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from hathor.simulator.miner import AbstractMiner | ||
from hathor.wallet import BaseWallet | ||
|
||
|
||
class Trigger(ABC): | ||
"""Abstract class to stop simulation when a certain condition is satisfied.""" | ||
@abstractmethod | ||
def should_stop(self) -> bool: | ||
"""This method must return True when the stop condition is satisfied.""" | ||
raise NotImplementedError | ||
|
||
|
||
class StopAfterNMinedBlocks(Trigger): | ||
"""Stop the simulation after `miner` finds N blocks. Note that these blocks might be orphan.""" | ||
def __init__(self, miner: 'AbstractMiner', *, quantity: int) -> None: | ||
self.miner = miner | ||
self.quantity = quantity | ||
self.reset() | ||
|
||
def reset(self) -> None: | ||
"""Reset the counter, so this trigger can be reused.""" | ||
self.initial_blocks_found = self.miner.get_blocks_found() | ||
|
||
def should_stop(self) -> bool: | ||
diff = self.miner.get_blocks_found() - self.initial_blocks_found | ||
return diff >= self.quantity | ||
|
||
|
||
class StopAfterMinimumBalance(Trigger): | ||
"""Stop the simulation after `wallet` reaches a minimum unlocked balance.""" | ||
def __init__(self, wallet: 'BaseWallet', token_uid: bytes, minimum_balance: int) -> None: | ||
self.wallet = wallet | ||
self.token_uid = token_uid | ||
self.minimum_balance = minimum_balance | ||
|
||
def should_stop(self) -> bool: | ||
balance = self.wallet.balance[self.token_uid].available | ||
return balance >= self.minimum_balance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from hathor.p2p.peer_id import PeerId | ||
from hathor.simulator import Simulator | ||
from hathor.simulator.trigger import StopAfterMinimumBalance, StopAfterNMinedBlocks | ||
from tests import unittest | ||
|
||
|
||
class TriggerTestCase(unittest.TestCase): | ||
def setUp(self): | ||
super().setUp() | ||
|
||
self.simulator = Simulator() | ||
self.simulator.start() | ||
|
||
peer_id = PeerId() | ||
self.manager1 = self.simulator.create_peer(peer_id=peer_id) | ||
self.manager1.allow_mining_without_peers() | ||
|
||
print('-' * 30) | ||
print('Simulation seed config:', self.simulator.seed) | ||
print('-' * 30) | ||
|
||
def test_stop_after_n_mined_blocks(self): | ||
miner1 = self.simulator.create_miner(self.manager1, hashpower=1e6) | ||
miner1.start() | ||
|
||
reactor = self.simulator.get_reactor() | ||
|
||
t0 = reactor.seconds() | ||
trigger = StopAfterNMinedBlocks(miner1, quantity=3) | ||
self.assertEqual(miner1.get_blocks_found(), 0) | ||
self.assertTrue(self.simulator.run(3600, trigger=trigger)) | ||
self.assertEqual(miner1.get_blocks_found(), 3) | ||
self.assertLess(reactor.seconds(), t0 + 3600) | ||
|
||
trigger.reset() | ||
self.assertTrue(self.simulator.run(3600, trigger=trigger)) | ||
self.assertEqual(miner1.get_blocks_found(), 6) | ||
|
||
t0 = reactor.seconds() | ||
trigger = StopAfterNMinedBlocks(miner1, quantity=10) | ||
self.assertTrue(self.simulator.run(3600, trigger=trigger)) | ||
self.assertEqual(miner1.get_blocks_found(), 16) | ||
self.assertLess(reactor.seconds(), t0 + 3600) | ||
|
||
def test_stop_after_minimum_balance(self): | ||
miner1 = self.simulator.create_miner(self.manager1, hashpower=1e6) | ||
miner1.start() | ||
|
||
wallet = self.manager1.wallet | ||
settings = self.simulator.settings | ||
|
||
minimum_balance = 1000_00 # 16 blocks | ||
token_uid = settings.HATHOR_TOKEN_UID | ||
|
||
trigger = StopAfterMinimumBalance(wallet, token_uid, minimum_balance) | ||
self.assertLess(wallet.balance[token_uid].available, minimum_balance) | ||
self.assertTrue(self.simulator.run(3600, trigger=trigger)) | ||
self.assertGreaterEqual(wallet.balance[token_uid].available, minimum_balance) |