Skip to content

refactor(daa): remove global daa test mode #802

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

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
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
15 changes: 8 additions & 7 deletions hathor/builder/cli_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def check_or_raise(self, condition: bool, message: str) -> None:
def create_manager(self, reactor: Reactor) -> HathorManager:
import hathor
from hathor.conf.get_settings import get_settings, get_settings_source
from hathor.daa import TestMode, _set_test_mode
from hathor.daa import TestMode
from hathor.event.storage import EventMemoryStorage, EventRocksDBStorage, EventStorage
from hathor.event.websocket.factory import EventWebsocketFactory
from hathor.p2p.netfilter.utils import add_peer_id_blacklist
Expand Down Expand Up @@ -208,7 +208,13 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
not_support_features=self._args.signal_not_support
)

daa = DifficultyAdjustmentAlgorithm(settings=settings)
test_mode = TestMode.DISABLED
if self._args.test_mode_tx_weight:
test_mode = TestMode.TEST_TX_WEIGHT
if self.wallet:
self.wallet.test_mode = True

daa = DifficultyAdjustmentAlgorithm(settings=settings, test_mode=test_mode)

vertex_verifiers = VertexVerifiers.create_defaults(
settings=settings,
Expand Down Expand Up @@ -281,11 +287,6 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
if self._args.bootstrap:
p2p_manager.add_peer_discovery(BootstrapPeerDiscovery(self._args.bootstrap))

if self._args.test_mode_tx_weight:
_set_test_mode(TestMode.TEST_TX_WEIGHT)
if self.wallet:
self.wallet.test_mode = True

if self._args.x_rocksdb_indexes:
self.log.warn('--x-rocksdb-indexes is now the default, no need to specify it')
if self._args.memory_indexes:
Expand Down
23 changes: 9 additions & 14 deletions hathor/daa.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from enum import IntFlag
from math import log
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, ClassVar, Optional

from structlog import get_logger

Expand All @@ -45,26 +45,21 @@ class TestMode(IntFlag):
TEST_ALL_WEIGHT = 3


TEST_MODE = TestMode.DISABLED


def _set_test_mode(mode: TestMode) -> None:
global TEST_MODE
logger.debug('change DAA test mode', from_mode=TEST_MODE.name, to_mode=mode.name)
TEST_MODE = mode


class DifficultyAdjustmentAlgorithm:
# TODO: This singleton is temporary, and only used in PeerId. It should be removed from there, and then from here.
singleton: ClassVar[Optional['DifficultyAdjustmentAlgorithm']] = None

def __init__(self, *, settings: HathorSettings) -> None:
def __init__(self, *, settings: HathorSettings, test_mode: TestMode = TestMode.DISABLED) -> None:
self._settings = settings
self.AVG_TIME_BETWEEN_BLOCKS = self._settings.AVG_TIME_BETWEEN_BLOCKS
self.MIN_BLOCK_WEIGHT = self._settings.MIN_BLOCK_WEIGHT
self.TEST_MODE = test_mode
DifficultyAdjustmentAlgorithm.singleton = self

@cpu.profiler(key=lambda _, block: 'calculate_block_difficulty!{}'.format(block.hash.hex()))
def calculate_block_difficulty(self, block: 'Block') -> float:
""" Calculate block weight according to the ascendents of `block`, using calculate_next_weight."""
if TEST_MODE & TestMode.TEST_BLOCK_WEIGHT:
if self.TEST_MODE & TestMode.TEST_BLOCK_WEIGHT:
return 1.0

if block.is_genesis:
Expand All @@ -79,7 +74,7 @@ def calculate_next_weight(self, parent_block: 'Block', timestamp: int) -> float:

The weight must not be less than `MIN_BLOCK_WEIGHT`.
"""
if TEST_MODE & TestMode.TEST_BLOCK_WEIGHT:
if self.TEST_MODE & TestMode.TEST_BLOCK_WEIGHT:
return 1.0

from hathor.transaction import sum_weights
Expand Down Expand Up @@ -166,7 +161,7 @@ def minimum_tx_weight(self, tx: 'Transaction') -> float:
"""
# In test mode we don't validate the minimum weight for tx
# We do this to allow generating many txs for testing
if TEST_MODE & TestMode.TEST_TX_WEIGHT:
if self.TEST_MODE & TestMode.TEST_TX_WEIGHT:
return 1.0

if tx.is_genesis:
Expand Down
9 changes: 6 additions & 3 deletions hathor/p2p/peer_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
from twisted.internet.interfaces import ISSLTransport
from twisted.internet.ssl import Certificate, CertificateOptions, TLSVersion, trustRootFromCertificates

from hathor import daa
from hathor.conf.get_settings import get_settings
from hathor.daa import DifficultyAdjustmentAlgorithm
from hathor.p2p.utils import connection_string_to_host, discover_dns, generate_certificate
from hathor.util import not_none

if TYPE_CHECKING:
from hathor.p2p.protocol import HathorProtocol # noqa: F401
Expand Down Expand Up @@ -347,7 +348,8 @@ def validate_entrypoint(self, protocol: 'HathorProtocol') -> Generator[Any, Any,
break
host = connection_string_to_host(entrypoint)
# TODO: don't use `daa.TEST_MODE` for this
result = yield discover_dns(host, daa.TEST_MODE)
test_mode = not_none(DifficultyAdjustmentAlgorithm.singleton).TEST_MODE
result = yield discover_dns(host, test_mode)
if protocol.connection_string in result:
# Found the entrypoint
found_entrypoint = True
Expand All @@ -366,7 +368,8 @@ def validate_entrypoint(self, protocol: 'HathorProtocol') -> Generator[Any, Any,
if connection_host == host:
found_entrypoint = True
break
result = yield discover_dns(host, daa.TEST_MODE)
test_mode = not_none(DifficultyAdjustmentAlgorithm.singleton).TEST_MODE
result = yield discover_dns(host, test_mode)
if connection_host in [connection_string_to_host(x) for x in result]:
# Found the entrypoint
found_entrypoint = True
Expand Down
5 changes: 1 addition & 4 deletions hathor/simulator/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from hathor.builder import BuildArtifacts, Builder
from hathor.conf.get_settings import get_settings
from hathor.conf.settings import HathorSettings
from hathor.daa import DifficultyAdjustmentAlgorithm, TestMode, _set_test_mode
from hathor.daa import DifficultyAdjustmentAlgorithm
from hathor.feature_activation.feature_service import FeatureService
from hathor.manager import HathorManager
from hathor.p2p.peer_id import PeerId
Expand Down Expand Up @@ -63,7 +63,6 @@ def _apply_patches(cls):
Patches:

- disable Transaction.resolve method
- set DAA test-mode to DISABLED (will actually run the pow function, that won't actually verify the pow)
"""
from hathor.transaction import BaseTransaction

Expand All @@ -75,8 +74,6 @@ def resolve(self: BaseTransaction, update_time: bool = True) -> bool:
cls._original_resolve = BaseTransaction.resolve
BaseTransaction.resolve = resolve

_set_test_mode(TestMode.DISABLED)

@classmethod
def _remove_patches(cls):
""" Remove the patches previously applied.
Expand Down
4 changes: 2 additions & 2 deletions tests/p2p/test_split_brain.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from mnemonic import Mnemonic

from hathor.daa import TestMode, _set_test_mode
from hathor.daa import TestMode
from hathor.graphviz import GraphvizVisualizer
from hathor.simulator import FakeConnection
from hathor.wallet import HDWallet
Expand All @@ -24,8 +24,8 @@ def create_peer(self, network, unlock_wallet=True):
wallet = HDWallet(gap_limit=2)
wallet._manually_initialize()

_set_test_mode(TestMode.TEST_ALL_WEIGHT)
manager = super().create_peer(network, wallet=wallet)
manager.daa.TEST_MODE = TestMode.TEST_ALL_WEIGHT
manager.avg_time_between_blocks = 64

# Don't use it anywhere else. It is unsafe to generate mnemonic words like this.
Expand Down
4 changes: 2 additions & 2 deletions tests/resources/base_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from twisted.web import server
from twisted.web.test.requesthelper import DummyRequest

from hathor.daa import TestMode, _set_test_mode
from hathor.daa import TestMode
from hathor.util import json_dumpb, json_loadb
from tests import unittest

Expand All @@ -19,7 +19,7 @@ def setUp(self, *, utxo_index: bool = False, unlock_wallet: bool = True) -> None
unlock_wallet=unlock_wallet
)
self.manager.allow_mining_without_peers()
_set_test_mode(TestMode.TEST_ALL_WEIGHT)
self.manager.daa.TEST_MODE = TestMode.TEST_ALL_WEIGHT

def tearDown(self):
return self.manager.stop()
Expand Down
6 changes: 3 additions & 3 deletions tests/resources/transaction/test_create_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from twisted.internet.defer import inlineCallbacks

from hathor.daa import TestMode, _set_test_mode
from hathor.daa import TestMode
from hathor.transaction import Transaction
from hathor.transaction.resources import CreateTxResource
from hathor.transaction.scripts import P2PKH, create_base_script
Expand Down Expand Up @@ -195,7 +195,7 @@ def test_spend_tx_by_script(self):

@inlineCallbacks
def test_tx_propagate(self):
_set_test_mode(TestMode.DISABLED) # disable test_mode so the weight is not 1
self.manager.daa.TEST_MODE = TestMode.DISABLED # disable test_mode so the weight is not 1
src_tx = self.unspent_tx
output_address = 'HNXsVtRUmwDCtpcCJUrH4QiHo9kUKx199A'
resp = (yield self.web.post('create_tx', {
Expand Down Expand Up @@ -233,7 +233,7 @@ def test_tx_propagate(self):

@inlineCallbacks
def test_tx_propagate_multiple_inputs(self):
_set_test_mode(TestMode.DISABLED) # disable test_mode so the weight is not 1
self.manager.daa.TEST_MODE = TestMode.DISABLED # disable test_mode so the weight is not 1
output_address = 'HNXsVtRUmwDCtpcCJUrH4QiHo9kUKx199A'
resp = (yield self.web.post('create_tx', {
'inputs': [
Expand Down
4 changes: 2 additions & 2 deletions tests/resources/wallet/test_send_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from twisted.internet.defer import inlineCallbacks

from hathor.daa import TestMode, _set_test_mode
from hathor.daa import TestMode
from hathor.p2p.resources import MiningResource
from hathor.wallet.resources import BalanceResource, HistoryResource, SendTokensResource
from tests import unittest
Expand Down Expand Up @@ -168,7 +168,7 @@ def test_post(self):

@inlineCallbacks
def test_tx_weight(self):
_set_test_mode(TestMode.DISABLED)
self.manager.daa.TEST_MODE = TestMode.DISABLED
add_new_blocks(self.manager, 3, advance_clock=1)
add_blocks_unlock_reward(self.manager)
self.reactor.advance(3)
Expand Down
8 changes: 4 additions & 4 deletions tests/tx/test_blockchain.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from itertools import chain

from hathor.conf import HathorSettings
from hathor.daa import DifficultyAdjustmentAlgorithm, TestMode, _set_test_mode
from hathor.daa import DifficultyAdjustmentAlgorithm, TestMode
from hathor.transaction import sum_weights
from hathor.transaction.storage import TransactionMemoryStorage
from tests import unittest
Expand Down Expand Up @@ -390,8 +390,8 @@ def test_block_rewards(self):

def test_daa_sanity(self):
# sanity test the DAA
_set_test_mode(TestMode.DISABLED)
manager = self.create_peer('testnet', tx_storage=self.tx_storage)
manager.daa.TEST_MODE = TestMode.DISABLED
N = settings.BLOCK_DIFFICULTY_N_BLOCKS
T = settings.AVG_TIME_BETWEEN_BLOCKS
manager.avg_time_between_blocks = T
Expand All @@ -417,7 +417,7 @@ def test_daa_sanity(self):
self.assertLess(new_weight, base_weight)

def test_daa_weight_decay_amount(self):
_set_test_mode(TestMode.DISABLED)
self.daa.TEST_MODE = TestMode.DISABLED
amount = settings.WEIGHT_DECAY_AMOUNT

for distance in range(0, settings.WEIGHT_DECAY_ACTIVATE_DISTANCE, 10):
Expand All @@ -434,8 +434,8 @@ def test_daa_weight_decay_amount(self):
self.assertAlmostEqual(self.daa.get_weight_decay_amount(distance), 11 * amount)

def test_daa_weight_decay_blocks(self):
_set_test_mode(TestMode.DISABLED)
manager = self.create_peer('testnet', tx_storage=self.tx_storage)
manager.daa.TEST_MODE = TestMode.DISABLED
amount = settings.WEIGHT_DECAY_AMOUNT

manager.daa.AVG_TIME_BETWEEN_BLOCKS = settings.AVG_TIME_BETWEEN_BLOCKS
Expand Down
4 changes: 2 additions & 2 deletions tests/tx/test_cache_storage.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from hathor.daa import TestMode, _set_test_mode
from hathor.daa import TestMode
from hathor.transaction import Transaction, TransactionMetadata
from hathor.transaction.storage import TransactionCacheStorage
from tests import unittest
Expand Down Expand Up @@ -144,7 +144,7 @@ def test_flush_thread(self):
self.cache_storage._flush_to_storage(self.cache_storage.dirty_txs.copy())

def test_topological_sort_dfs(self):
_set_test_mode(TestMode.TEST_ALL_WEIGHT)
self.manager.daa.TEST_MODE = TestMode.TEST_ALL_WEIGHT
add_new_blocks(self.manager, 11, advance_clock=1)
tx = add_new_transactions(self.manager, 1, advance_clock=1)[0]

Expand Down
6 changes: 3 additions & 3 deletions tests/tx/test_genesis.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from hathor.conf import HathorSettings
from hathor.daa import DifficultyAdjustmentAlgorithm, TestMode, _set_test_mode
from hathor.daa import DifficultyAdjustmentAlgorithm, TestMode
from hathor.transaction.storage import TransactionMemoryStorage
from hathor.verification.verification_service import VerificationService, VertexVerifiers
from hathor.verification.vertex_verifier import VertexVerifier
Expand Down Expand Up @@ -70,10 +70,10 @@ def test_genesis_weight(self):

# Validate the block and tx weight
# in test mode weight is always 1
_set_test_mode(TestMode.TEST_ALL_WEIGHT)
self._daa.TEST_MODE = TestMode.TEST_ALL_WEIGHT
self.assertEqual(self._daa.calculate_block_difficulty(genesis_block), 1)
self.assertEqual(self._daa.minimum_tx_weight(genesis_tx), 1)

_set_test_mode(TestMode.DISABLED)
self._daa.TEST_MODE = TestMode.DISABLED
self.assertEqual(self._daa.calculate_block_difficulty(genesis_block), genesis_block.weight)
self.assertEqual(self._daa.minimum_tx_weight(genesis_tx), genesis_tx.weight)
4 changes: 2 additions & 2 deletions tests/tx/test_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from math import isinf, isnan

from hathor.crypto.util import decode_address, get_address_from_public_key, get_private_key_from_bytes
from hathor.daa import TestMode, _set_test_mode
from hathor.daa import TestMode
from hathor.transaction import MAX_OUTPUT_VALUE, Block, Transaction, TxInput, TxOutput
from hathor.transaction.exceptions import (
BlockWithInputs,
Expand Down Expand Up @@ -629,8 +629,8 @@ def test_update_timestamp(self):
self.assertEquals(tx.timestamp, ts)

def test_propagation_error(self):
_set_test_mode(TestMode.DISABLED)
manager = self.create_peer('testnet', unlock_wallet=True)
manager.daa.TEST_MODE = TestMode.DISABLED

# 1. propagate genesis
genesis_block = self.genesis_blocks[0]
Expand Down
8 changes: 4 additions & 4 deletions tests/tx/test_tx_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from twisted.trial import unittest

from hathor.conf import HathorSettings
from hathor.daa import TestMode, _set_test_mode
from hathor.daa import TestMode
from hathor.transaction import Block, Transaction, TxInput, TxOutput
from hathor.transaction.scripts import P2PKH
from hathor.transaction.storage.exceptions import TransactionDoesNotExist
Expand Down Expand Up @@ -145,7 +145,7 @@ def test_storage_basic_v2(self):
self.assertEqual(set(tx_parents_hash), {self.genesis_txs[0].hash, self.genesis_txs[1].hash})

def test_vertices_count(self):
_set_test_mode(TestMode.TEST_ALL_WEIGHT)
self.manager.daa.TEST_MODE = TestMode.TEST_ALL_WEIGHT

blocks_count = 1
txs_count = 2
Expand Down Expand Up @@ -522,7 +522,7 @@ def _add_new_block(self, parents=None):
return block

def test_best_block_tips_cache(self):
_set_test_mode(TestMode.TEST_ALL_WEIGHT)
self.manager.daa.TEST_MODE = TestMode.TEST_ALL_WEIGHT
self.manager.wallet.unlock(b'MYPASS')
spent_blocks = add_new_blocks(self.manager, 10)
self.assertEqual(self.tx_storage._best_block_tips_cache, [spent_blocks[-1].hash])
Expand All @@ -534,7 +534,7 @@ def test_best_block_tips_cache(self):
self.assertEqual(self.tx_storage._best_block_tips_cache, [latest_blocks[-1].hash])

def test_topological_sort(self):
_set_test_mode(TestMode.TEST_ALL_WEIGHT)
self.manager.daa.TEST_MODE = TestMode.TEST_ALL_WEIGHT
_total = 0
blocks = add_new_blocks(self.manager, 1, advance_clock=1)
_total += len(blocks)
Expand Down
Loading