From 88ef27f578517bef388795c0f7dfcea022a78eb9 Mon Sep 17 00:00:00 2001 From: Gabriel Levcovitz Date: Thu, 28 Sep 2023 19:48:44 -0300 Subject: [PATCH] refactor(settings): refactor genesis module --- hathor/builder/cli_builder.py | 5 +- hathor/conf/mainnet.py | 2 +- hathor/conf/mainnet.yml | 2 +- hathor/conf/settings.py | 15 ++++- hathor/conf/testnet.py | 2 +- hathor/conf/testnet.yml | 2 +- hathor/indexes/base_index.py | 2 + hathor/indexes/height_index.py | 11 +-- hathor/indexes/memory_height_index.py | 4 +- hathor/indexes/memory_info_index.py | 6 +- hathor/indexes/rocksdb_height_index.py | 5 +- hathor/p2p/utils.py | 5 +- hathor/simulator/simulator.py | 3 +- hathor/transaction/base_transaction.py | 2 +- hathor/transaction/genesis.py | 67 ++++--------------- .../storage/transaction_storage.py | 57 ++++++++++++++-- hathor/transaction/transaction_metadata.py | 14 ++-- .../invalid_byte_hathor_settings_fixture.yml | 2 +- ...valid_features_hathor_settings_fixture.yml | 2 +- .../missing_hathor_settings_fixture.yml | 2 +- .../valid_hathor_settings_fixture.yml | 2 +- tests/others/test_hathor_settings.py | 2 +- tests/p2p/test_split_brain.py | 3 +- tests/resources/wallet/test_thin_wallet.py | 4 +- tests/tx/test_block.py | 10 ++- tests/tx/test_indexes2.py | 2 +- tests/tx/test_mining.py | 2 +- tests/tx/test_stratum.py | 5 +- tests/tx/test_tx_storage.py | 16 +---- tests/unittest.py | 4 +- tests/utils.py | 4 +- 31 files changed, 138 insertions(+), 126 deletions(-) diff --git a/hathor/builder/cli_builder.py b/hathor/builder/cli_builder.py index b259c7c98..7dab5bdf5 100644 --- a/hathor/builder/cli_builder.py +++ b/hathor/builder/cli_builder.py @@ -31,7 +31,7 @@ from hathor.manager import HathorManager from hathor.p2p.manager import ConnectionsManager from hathor.p2p.peer_id import PeerId -from hathor.p2p.utils import discover_hostname +from hathor.p2p.utils import discover_hostname, get_genesis_short_hash from hathor.pubsub import PubSubManager from hathor.stratum import StratumFactory from hathor.util import Random, Reactor @@ -64,7 +64,6 @@ def create_manager(self, reactor: Reactor) -> HathorManager: from hathor.p2p.netfilter.utils import add_peer_id_blacklist from hathor.p2p.peer_discovery import BootstrapPeerDiscovery, DNSPeerDiscovery from hathor.storage import RocksDBStorage - from hathor.transaction import genesis from hathor.transaction.storage import ( TransactionCacheStorage, TransactionMemoryStorage, @@ -89,7 +88,7 @@ def create_manager(self, reactor: Reactor) -> HathorManager: 'hathor-core v{hathor}', hathor=hathor.__version__, pid=os.getpid(), - genesis=genesis.GENESIS_HASH.hex()[:7], + genesis=get_genesis_short_hash(), my_peer_id=str(peer_id.id), python=python, platform=platform.platform(), diff --git a/hathor/conf/mainnet.py b/hathor/conf/mainnet.py index c7aa6fe90..f66691cf3 100644 --- a/hathor/conf/mainnet.py +++ b/hathor/conf/mainnet.py @@ -25,7 +25,7 @@ # Genesis stuff # output addr: HJB2yxxsHtudGGy3jmVeadwMfRi2zNCKKD GENESIS_OUTPUT_SCRIPT=bytes.fromhex('76a9147fd4ae0e4fb2d2854e76d359029d8078bb99649e88ac'), - GENESIS_TIMESTAMP=1578075305, + GENESIS_BLOCK_TIMESTAMP=1578075305, GENESIS_BLOCK_NONCE=2591358, GENESIS_BLOCK_HASH=bytes.fromhex('000006cb93385b8b87a545a1cbb6197e6caff600c12cc12fc54250d39c8088fc'), GENESIS_TX1_NONCE=7715, diff --git a/hathor/conf/mainnet.yml b/hathor/conf/mainnet.yml index 3a4fb38fe..baa6fa16a 100644 --- a/hathor/conf/mainnet.yml +++ b/hathor/conf/mainnet.yml @@ -8,7 +8,7 @@ WHITELIST_URL: https://hathor-public-files.s3.amazonaws.com/whitelist_peer_ids # Genesis stuff GENESIS_OUTPUT_SCRIPT: 76a9147fd4ae0e4fb2d2854e76d359029d8078bb99649e88ac -GENESIS_TIMESTAMP: 1578075305 +GENESIS_BLOCK_TIMESTAMP: 1578075305 GENESIS_BLOCK_NONCE: 2591358 GENESIS_BLOCK_HASH: 000006cb93385b8b87a545a1cbb6197e6caff600c12cc12fc54250d39c8088fc GENESIS_TX1_NONCE: 7715 diff --git a/hathor/conf/settings.py b/hathor/conf/settings.py index f7af7faaa..be31f0dd5 100644 --- a/hathor/conf/settings.py +++ b/hathor/conf/settings.py @@ -98,7 +98,20 @@ def MAXIMUM_NUMBER_OF_HALVINGS(self) -> int: GENESIS_OUTPUT_SCRIPT: bytes = bytes.fromhex('76a914a584cf48b161e4a49223ed220df30037ab740e0088ac') # Genesis timestamps, nonces and hashes - GENESIS_TIMESTAMP: int = 1572636343 # used as is for genesis_block, +1 for genesis_tx1 and +2 for genesis_tx2 + + # Timestamp used for the genesis block + GENESIS_BLOCK_TIMESTAMP: int = 1572636343 + + @property + def GENESIS_TX1_TIMESTAMP(self) -> int: + """Timestamp used for the first genesis transaction.""" + return self.GENESIS_BLOCK_TIMESTAMP + 1 + + @property + def GENESIS_TX2_TIMESTAMP(self) -> int: + """Timestamp used for the second genesis transaction.""" + return self.GENESIS_BLOCK_TIMESTAMP + 2 + GENESIS_BLOCK_NONCE: int = 3526202 GENESIS_BLOCK_HASH: bytes = bytes.fromhex('000007eb968a6cdf0499e2d033faf1e163e0dc9cf41876acad4d421836972038') GENESIS_TX1_NONCE: int = 12595 diff --git a/hathor/conf/testnet.py b/hathor/conf/testnet.py index b79ac6fe4..5f36d8b6b 100644 --- a/hathor/conf/testnet.py +++ b/hathor/conf/testnet.py @@ -25,7 +25,7 @@ BOOTSTRAP_DNS=['golf.testnet.hathor.network'], # Genesis stuff GENESIS_OUTPUT_SCRIPT=bytes.fromhex('76a914a584cf48b161e4a49223ed220df30037ab740e0088ac'), - GENESIS_TIMESTAMP=1577836800, + GENESIS_BLOCK_TIMESTAMP=1577836800, GENESIS_BLOCK_NONCE=826272, GENESIS_BLOCK_HASH=bytes.fromhex('0000033139d08176d1051fb3a272c3610457f0c7f686afbe0afe3d37f966db85'), GENESIS_TX1_NONCE=190, diff --git a/hathor/conf/testnet.yml b/hathor/conf/testnet.yml index 81eaf1c58..e2e1da6d3 100644 --- a/hathor/conf/testnet.yml +++ b/hathor/conf/testnet.yml @@ -6,7 +6,7 @@ BOOTSTRAP_DNS: # Genesis stuff GENESIS_OUTPUT_SCRIPT: 76a914a584cf48b161e4a49223ed220df30037ab740e0088ac -GENESIS_TIMESTAMP: 1577836800 +GENESIS_BLOCK_TIMESTAMP: 1577836800 GENESIS_BLOCK_NONCE: 826272 GENESIS_BLOCK_HASH: 0000033139d08176d1051fb3a272c3610457f0c7f686afbe0afe3d37f966db85 GENESIS_TX1_NONCE: 190 diff --git a/hathor/indexes/base_index.py b/hathor/indexes/base_index.py index 6d452f6d3..22a782313 100644 --- a/hathor/indexes/base_index.py +++ b/hathor/indexes/base_index.py @@ -17,6 +17,7 @@ from structlog import get_logger +from hathor.conf.get_settings import get_settings from hathor.indexes.scope import Scope from hathor.transaction.base_transaction import BaseTransaction @@ -33,6 +34,7 @@ class BaseIndex(ABC): created to generalize how we initialize indexes and keep track of which ones are up-to-date. """ def __init__(self) -> None: + self._settings = get_settings() self.log = logger.new() def init_start(self, indexes_manager: 'IndexesManager') -> None: diff --git a/hathor/indexes/height_index.py b/hathor/indexes/height_index.py index 167787e69..7bf91e181 100644 --- a/hathor/indexes/height_index.py +++ b/hathor/indexes/height_index.py @@ -18,7 +18,6 @@ from hathor.indexes.base_index import BaseIndex from hathor.indexes.scope import Scope from hathor.transaction import BaseTransaction, Block -from hathor.transaction.genesis import BLOCK_GENESIS from hathor.types import VertexId from hathor.util import not_none @@ -41,9 +40,6 @@ class HeightInfo(NamedTuple): id: VertexId -BLOCK_GENESIS_ENTRY: IndexEntry = IndexEntry(not_none(BLOCK_GENESIS.hash), BLOCK_GENESIS.timestamp) - - class _AddToIndexItem(NamedTuple): height: int hash: bytes @@ -54,6 +50,13 @@ class HeightIndex(BaseIndex): """Store the block hash for each given height """ + def get_genesis_block_entry(self) -> IndexEntry: + """Return the index entry for the genesis block.""" + return IndexEntry( + self._settings.GENESIS_BLOCK_HASH, + self._settings.GENESIS_BLOCK_TIMESTAMP + ) + def get_scope(self) -> Scope: return SCOPE diff --git a/hathor/indexes/memory_height_index.py b/hathor/indexes/memory_height_index.py index db1ec4cc9..50bf03004 100644 --- a/hathor/indexes/memory_height_index.py +++ b/hathor/indexes/memory_height_index.py @@ -14,7 +14,7 @@ from typing import Optional -from hathor.indexes.height_index import BLOCK_GENESIS_ENTRY, HeightIndex, HeightInfo, IndexEntry +from hathor.indexes.height_index import HeightIndex, HeightInfo, IndexEntry class MemoryHeightIndex(HeightIndex): @@ -31,7 +31,7 @@ def get_db_name(self) -> Optional[str]: return None def force_clear(self) -> None: - self._index = [BLOCK_GENESIS_ENTRY] + self._index = [self.get_genesis_block_entry()] def _add(self, height: int, block_hash: bytes, timestamp: int, *, can_reorg: bool) -> None: if len(self._index) < height: diff --git a/hathor/indexes/memory_info_index.py b/hathor/indexes/memory_info_index.py index cddd73ff6..656cc7972 100644 --- a/hathor/indexes/memory_info_index.py +++ b/hathor/indexes/memory_info_index.py @@ -23,6 +23,7 @@ class MemoryInfoIndex(InfoIndex): def __init__(self): + super().__init__() self._block_count = 0 self._tx_count = 0 self._first_timestamp = 0 @@ -35,11 +36,10 @@ def get_db_name(self) -> Optional[str]: return None def force_clear(self) -> None: - from hathor.transaction.genesis import BLOCK_GENESIS, TX_GENESIS2 self._block_count = 1 self._tx_count = 2 - self._first_timestamp = BLOCK_GENESIS.timestamp - self._latest_timestamp = TX_GENESIS2.timestamp + self._first_timestamp = self._settings.GENESIS_BLOCK_TIMESTAMP + self._latest_timestamp = self._settings.GENESIS_TX2_TIMESTAMP def update_timestamps(self, tx: BaseTransaction) -> None: if tx.is_genesis: diff --git a/hathor/indexes/rocksdb_height_index.py b/hathor/indexes/rocksdb_height_index.py index 512606de8..562bbf43c 100644 --- a/hathor/indexes/rocksdb_height_index.py +++ b/hathor/indexes/rocksdb_height_index.py @@ -16,7 +16,7 @@ from structlog import get_logger -from hathor.indexes.height_index import BLOCK_GENESIS_ENTRY, HeightIndex, HeightInfo, IndexEntry +from hathor.indexes.height_index import HeightIndex, HeightInfo, IndexEntry from hathor.indexes.rocksdb_utils import RocksDBIndexUtils if TYPE_CHECKING: # pragma: no cover @@ -44,6 +44,7 @@ class RocksDBHeightIndex(HeightIndex, RocksDBIndexUtils): def __init__(self, db: 'rocksdb.DB', *, cf_name: Optional[bytes] = None) -> None: self.log = logger.new() + HeightIndex.__init__(self) RocksDBIndexUtils.__init__(self, db, cf_name or _CF_NAME_HEIGHT_INDEX) def get_db_name(self) -> Optional[str]: @@ -56,7 +57,7 @@ def force_clear(self) -> None: def _init_db(self) -> None: """ Initialize the database with the genesis entry.""" key_genesis = self._to_key(0) - value_genesis = self._to_value(BLOCK_GENESIS_ENTRY) + value_genesis = self._to_value(self.get_genesis_block_entry()) self._db.put((self._cf, key_genesis), value_genesis) def _to_key(self, height: int) -> bytes: diff --git a/hathor/p2p/utils.py b/hathor/p2p/utils.py index 33b633d34..6904da0a7 100644 --- a/hathor/p2p/utils.py +++ b/hathor/p2p/utils.py @@ -31,7 +31,7 @@ from hathor.conf.get_settings import get_settings from hathor.indexes.height_index import HeightInfo from hathor.p2p.peer_discovery import DNSPeerDiscovery -from hathor.transaction.genesis import GENESIS_HASH +from hathor.transaction.genesis import get_representation_for_all_genesis def discover_hostname() -> Optional[str]: @@ -75,7 +75,8 @@ def description_to_connection_string(description: str) -> tuple[str, Optional[st def get_genesis_short_hash() -> str: """ Return the first 7 chars of the GENESIS_HASH used for validation that the genesis are the same """ - return GENESIS_HASH.hex()[:7] + settings = get_settings() + return get_representation_for_all_genesis(settings).hex()[:7] def get_settings_hello_dict() -> dict[str, Any]: diff --git a/hathor/simulator/simulator.py b/hathor/simulator/simulator.py index cae937e03..27507baf9 100644 --- a/hathor/simulator/simulator.py +++ b/hathor/simulator/simulator.py @@ -28,7 +28,6 @@ from hathor.simulator.clock import HeapClock, MemoryReactorHeapClock from hathor.simulator.miner.geometric_miner import GeometricMiner from hathor.simulator.tx_generator import RandomTransactionGenerator -from hathor.transaction.genesis import _get_genesis_transactions_unsafe from hathor.util import Random from hathor.wallet import HDWallet @@ -128,7 +127,7 @@ def start(self) -> None: assert not self._started self._started = True self._patches_rc_increment() - first_timestamp = min(tx.timestamp for tx in _get_genesis_transactions_unsafe(None)) + first_timestamp = self.settings.GENESIS_BLOCK_TIMESTAMP dt = self.rng.randint(3600, 120 * 24 * 3600) self._clock.advance(first_timestamp + dt) self.log.debug('randomized step: clock advance start', dt=dt) diff --git a/hathor/transaction/base_transaction.py b/hathor/transaction/base_transaction.py index 0e4b2eee5..ea185893e 100644 --- a/hathor/transaction/base_transaction.py +++ b/hathor/transaction/base_transaction.py @@ -357,7 +357,7 @@ def is_genesis(self) -> bool: if self.hash is None: return False from hathor.transaction.genesis import is_genesis - return is_genesis(self.hash) + return is_genesis(self.hash, settings=self._settings) @abstractmethod def get_funds_fields_from_struct(self, buf: bytes, *, verbose: VerboseCallback = None) -> bytes: diff --git a/hathor/transaction/genesis.py b/hathor/transaction/genesis.py index 408428523..0d567515d 100644 --- a/hathor/transaction/genesis.py +++ b/hathor/transaction/genesis.py @@ -12,71 +12,32 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import TYPE_CHECKING, Optional +from typing import TYPE_CHECKING -from hathor.conf import HathorSettings -from hathor.transaction import BaseTransaction, Block, Transaction, TxOutput +from hathor.conf.settings import HathorSettings if TYPE_CHECKING: from hathor.transaction.storage import TransactionStorage # noqa: F401 -settings = HathorSettings() -BLOCK_GENESIS = Block( - hash=settings.GENESIS_BLOCK_HASH, - nonce=settings.GENESIS_BLOCK_NONCE, - timestamp=settings.GENESIS_TIMESTAMP, - weight=settings.MIN_BLOCK_WEIGHT, - outputs=[ - TxOutput(settings.GENESIS_TOKENS, settings.GENESIS_OUTPUT_SCRIPT), - ], -) +def get_all_genesis_hashes(settings: HathorSettings) -> list[bytes]: + """Return all genesis hashes.""" + return [ + settings.GENESIS_BLOCK_HASH, + settings.GENESIS_TX1_HASH, + settings.GENESIS_TX2_HASH + ] -TX_GENESIS1 = Transaction( - hash=settings.GENESIS_TX1_HASH, - nonce=settings.GENESIS_TX1_NONCE, - timestamp=settings.GENESIS_TIMESTAMP + 1, - weight=settings.MIN_TX_WEIGHT, -) -TX_GENESIS2 = Transaction( - hash=settings.GENESIS_TX2_HASH, - nonce=settings.GENESIS_TX2_NONCE, - timestamp=settings.GENESIS_TIMESTAMP + 2, - weight=settings.MIN_TX_WEIGHT, -) - -GENESIS = [BLOCK_GENESIS, TX_GENESIS1, TX_GENESIS2] - -GENESIS_HASHES = [settings.GENESIS_BLOCK_HASH, settings.GENESIS_TX1_HASH, settings.GENESIS_TX2_HASH] - - -def _get_genesis_hash() -> bytes: +def get_representation_for_all_genesis(settings: HathorSettings) -> bytes: + """Return a single hash representing all genesis vertices.""" import hashlib h = hashlib.sha256() - for tx in GENESIS: - tx_hash = tx.hash - assert tx_hash is not None + for tx_hash in get_all_genesis_hashes(settings): h.update(tx_hash) return h.digest() -GENESIS_HASH = _get_genesis_hash() - - -def _get_genesis_transactions_unsafe(tx_storage: Optional['TransactionStorage']) -> list[BaseTransaction]: - """You shouldn't get genesis directly. Please, get it from your storage instead.""" - genesis = [] - for tx in GENESIS: - tx2 = tx.clone() - tx2.storage = tx_storage - genesis.append(tx2) - return genesis - - -def is_genesis(hash_bytes: bytes) -> bool: +def is_genesis(hash_bytes: bytes, *, settings: HathorSettings) -> bool: """Check whether hash is from a genesis transaction.""" - for tx in GENESIS: - if hash_bytes == tx.hash: - return True - return False + return hash_bytes in get_all_genesis_hashes(settings) diff --git a/hathor/transaction/storage/transaction_storage.py b/hathor/transaction/storage/transaction_storage.py index 7aa3cffc4..727d4e856 100644 --- a/hathor/transaction/storage/transaction_storage.py +++ b/hathor/transaction/storage/transaction_storage.py @@ -28,7 +28,7 @@ from hathor.indexes.height_index import HeightInfo from hathor.profiler import get_cpu_profiler from hathor.pubsub import PubSubManager -from hathor.transaction.base_transaction import BaseTransaction +from hathor.transaction.base_transaction import BaseTransaction, TxOutput from hathor.transaction.block import Block from hathor.transaction.storage.exceptions import ( TransactionDoesNotExist, @@ -305,7 +305,13 @@ def get_best_block(self) -> Block: def _save_or_verify_genesis(self) -> None: """Save all genesis in the storage.""" self._saving_genesis = True - for tx in self._get_genesis_from_settings(): + genesis_txs = [ + self._construct_genesis_block(), + self._construct_genesis_tx1(), + self._construct_genesis_tx2(), + ] + + for tx in genesis_txs: try: assert tx.hash is not None tx2 = self.get_transaction(tx.hash) @@ -318,11 +324,6 @@ def _save_or_verify_genesis(self) -> None: self._genesis_cache[tx2.hash] = tx2 self._saving_genesis = False - def _get_genesis_from_settings(self) -> list[BaseTransaction]: - """Return all genesis from settings.""" - from hathor.transaction.genesis import _get_genesis_transactions_unsafe - return _get_genesis_transactions_unsafe(self) - def _save_to_weakref(self, tx: BaseTransaction) -> None: """ Save transaction to weakref. """ @@ -1086,6 +1087,48 @@ def compute_transactions_that_became_invalid(self, new_best_height: int) -> list to_remove.append(tx) return to_remove + def _construct_genesis_block(self) -> Block: + """Return the genesis block.""" + block = Block( + storage=self, + nonce=self._settings.GENESIS_BLOCK_NONCE, + timestamp=self._settings.GENESIS_BLOCK_TIMESTAMP, + weight=self._settings.MIN_BLOCK_WEIGHT, + outputs=[ + TxOutput(self._settings.GENESIS_TOKENS, self._settings.GENESIS_OUTPUT_SCRIPT), + ], + ) + block.update_hash() + + assert block.hash == self._settings.GENESIS_BLOCK_HASH + return block + + def _construct_genesis_tx1(self) -> Transaction: + """Return the genesis tx1.""" + tx1 = Transaction( + storage=self, + nonce=self._settings.GENESIS_TX1_NONCE, + timestamp=self._settings.GENESIS_TX1_TIMESTAMP, + weight=self._settings.MIN_TX_WEIGHT, + ) + tx1.update_hash() + + assert tx1.hash == self._settings.GENESIS_TX1_HASH + return tx1 + + def _construct_genesis_tx2(self) -> Transaction: + """Return the genesis tx2.""" + tx2 = Transaction( + storage=self, + nonce=self._settings.GENESIS_TX2_NONCE, + timestamp=self._settings.GENESIS_TX2_TIMESTAMP, + weight=self._settings.MIN_TX_WEIGHT, + ) + tx2.update_hash() + + assert tx2.hash == self._settings.GENESIS_TX2_HASH + return tx2 + class BaseTransactionStorage(TransactionStorage): indexes: Optional[IndexesManager] diff --git a/hathor/transaction/transaction_metadata.py b/hathor/transaction/transaction_metadata.py index fd0039cdf..c7bbbaf72 100644 --- a/hathor/transaction/transaction_metadata.py +++ b/hathor/transaction/transaction_metadata.py @@ -15,6 +15,7 @@ from collections import defaultdict from typing import TYPE_CHECKING, Any, Optional +from hathor.conf.get_settings import get_settings from hathor.feature_activation.feature import Feature from hathor.feature_activation.model.feature_state import FeatureState from hathor.transaction.validation_state import ValidationState @@ -127,8 +128,10 @@ def __init__( self.feature_activation_bit_counts = feature_activation_bit_counts + settings = get_settings() + # Genesis specific: - if hash is not None and is_genesis(hash): + if hash is not None and is_genesis(hash, settings=settings): self.validation = ValidationState.FULL def get_tx(self) -> 'BaseTransaction': @@ -248,10 +251,8 @@ def to_json_extended(self, tx_storage: 'TransactionStorage') -> dict[str, Any]: @classmethod def create_from_json(cls, data: dict[str, Any]) -> 'TransactionMetadata': - from hathor.transaction.genesis import is_genesis - - meta = cls() - meta.hash = bytes.fromhex(data['hash']) if data['hash'] else None + hash_ = bytes.fromhex(data['hash']) if data['hash'] else None + meta = cls(hash=hash_) for idx, hashes in data['spent_outputs']: for h_hex in hashes: meta.spent_outputs[idx].append(bytes.fromhex(h_hex)) @@ -293,9 +294,6 @@ def create_from_json(cls, data: dict[str, Any]) -> 'TransactionMetadata': _val_name = data.get('validation', None) meta.validation = ValidationState.from_name(_val_name) if _val_name is not None else ValidationState.INITIAL - if meta.hash is not None and is_genesis(meta.hash): - meta.validation = ValidationState.FULL - return meta def clone(self) -> 'TransactionMetadata': diff --git a/tests/others/fixtures/invalid_byte_hathor_settings_fixture.yml b/tests/others/fixtures/invalid_byte_hathor_settings_fixture.yml index a425f1cdd..b05b8f246 100644 --- a/tests/others/fixtures/invalid_byte_hathor_settings_fixture.yml +++ b/tests/others/fixtures/invalid_byte_hathor_settings_fixture.yml @@ -7,7 +7,7 @@ ENABLE_PEER_WHITELIST: true WHITELIST_URL: https://hathor-public-files.s3.amazonaws.com/whitelist_peer_ids GENESIS_OUTPUT_SCRIPT: 76a9147fd4ae0e4fb2d2854e76d359029d8078bb99649e88ac -GENESIS_TIMESTAMP: 1578075305 +GENESIS_BLOCK_TIMESTAMP: 1578075305 GENESIS_BLOCK_NONCE: 2591358 GENESIS_BLOCK_HASH: 000006cb93385b8b87a545a1cbb6197e6caff600c12cc12fc54250d39c8088fc GENESIS_TX1_NONCE: 7715 diff --git a/tests/others/fixtures/invalid_features_hathor_settings_fixture.yml b/tests/others/fixtures/invalid_features_hathor_settings_fixture.yml index e993f0cee..c2103afef 100644 --- a/tests/others/fixtures/invalid_features_hathor_settings_fixture.yml +++ b/tests/others/fixtures/invalid_features_hathor_settings_fixture.yml @@ -7,7 +7,7 @@ ENABLE_PEER_WHITELIST: true WHITELIST_URL: https://hathor-public-files.s3.amazonaws.com/whitelist_peer_ids GENESIS_OUTPUT_SCRIPT: 76a9147fd4ae0e4fb2d2854e76d359029d8078bb99649e88ac -GENESIS_TIMESTAMP: 1578075305 +GENESIS_BLOCK_TIMESTAMP: 1578075305 GENESIS_BLOCK_NONCE: 2591358 GENESIS_BLOCK_HASH: 000006cb93385b8b87a545a1cbb6197e6caff600c12cc12fc54250d39c8088fc GENESIS_TX1_NONCE: 7715 diff --git a/tests/others/fixtures/missing_hathor_settings_fixture.yml b/tests/others/fixtures/missing_hathor_settings_fixture.yml index f03053bc9..81719c264 100644 --- a/tests/others/fixtures/missing_hathor_settings_fixture.yml +++ b/tests/others/fixtures/missing_hathor_settings_fixture.yml @@ -6,7 +6,7 @@ ENABLE_PEER_WHITELIST: true WHITELIST_URL: https://hathor-public-files.s3.amazonaws.com/whitelist_peer_ids GENESIS_OUTPUT_SCRIPT: 76a9147fd4ae0e4fb2d2854e76d359029d8078bb99649e88ac -GENESIS_TIMESTAMP: 1578075305 +GENESIS_BLOCK_TIMESTAMP: 1578075305 GENESIS_BLOCK_NONCE: 2591358 GENESIS_BLOCK_HASH: 000006cb93385b8b87a545a1cbb6197e6caff600c12cc12fc54250d39c8088fc GENESIS_TX1_NONCE: 7715 diff --git a/tests/others/fixtures/valid_hathor_settings_fixture.yml b/tests/others/fixtures/valid_hathor_settings_fixture.yml index a1c8a847a..b0198476f 100644 --- a/tests/others/fixtures/valid_hathor_settings_fixture.yml +++ b/tests/others/fixtures/valid_hathor_settings_fixture.yml @@ -7,7 +7,7 @@ ENABLE_PEER_WHITELIST: true WHITELIST_URL: https://hathor-public-files.s3.amazonaws.com/whitelist_peer_ids GENESIS_OUTPUT_SCRIPT: 76a9147fd4ae0e4fb2d2854e76d359029d8078bb99649e88ac -GENESIS_TIMESTAMP: 1578075305 +GENESIS_BLOCK_TIMESTAMP: 1578075305 GENESIS_BLOCK_NONCE: 2591358 GENESIS_BLOCK_HASH: 000006cb93385b8b87a545a1cbb6197e6caff600c12cc12fc54250d39c8088fc GENESIS_TX1_NONCE: 7715 diff --git a/tests/others/test_hathor_settings.py b/tests/others/test_hathor_settings.py index e0bcf82e9..12040a0ca 100644 --- a/tests/others/test_hathor_settings.py +++ b/tests/others/test_hathor_settings.py @@ -41,7 +41,7 @@ def test_valid_hathor_settings_from_yaml(filepath): MIN_TX_WEIGHT_COEFFICIENT=0, MIN_TX_WEIGHT=8, GENESIS_OUTPUT_SCRIPT=bytes.fromhex('76a9147fd4ae0e4fb2d2854e76d359029d8078bb99649e88ac'), - GENESIS_TIMESTAMP=1578075305, + GENESIS_BLOCK_TIMESTAMP=1578075305, GENESIS_BLOCK_NONCE=2591358, GENESIS_BLOCK_HASH=bytes.fromhex('000006cb93385b8b87a545a1cbb6197e6caff600c12cc12fc54250d39c8088fc'), GENESIS_TX1_NONCE=7715, diff --git a/tests/p2p/test_split_brain.py b/tests/p2p/test_split_brain.py index 7bc2f44c6..804377f99 100644 --- a/tests/p2p/test_split_brain.py +++ b/tests/p2p/test_split_brain.py @@ -14,9 +14,8 @@ class BaseHathorSyncMethodsTestCase(unittest.TestCase): def setUp(self): super().setUp() - from hathor.transaction.genesis import _get_genesis_transactions_unsafe - first_timestamp = min(tx.timestamp for tx in _get_genesis_transactions_unsafe(None)) + first_timestamp = self._settings.GENESIS_BLOCK_TIMESTAMP self.clock.advance(first_timestamp + self.rng.randint(3600, 120*24*3600)) self.network = 'testnet' diff --git a/tests/resources/wallet/test_thin_wallet.py b/tests/resources/wallet/test_thin_wallet.py index 033a2050f..e9d0d4b31 100644 --- a/tests/resources/wallet/test_thin_wallet.py +++ b/tests/resources/wallet/test_thin_wallet.py @@ -5,7 +5,7 @@ from hathor.conf import HathorSettings from hathor.crypto.util import decode_address from hathor.daa import minimum_tx_weight -from hathor.transaction import Transaction, TxInput, TxOutput, genesis +from hathor.transaction import Transaction, TxInput, TxOutput from hathor.transaction.scripts import P2PKH, create_output_script, parse_address_script from hathor.wallet.resources.thin_wallet import ( AddressHistoryResource, @@ -275,7 +275,7 @@ def test_error_request(self): resource = SendTokensResource(self.manager) request = TestDummyRequest('POST', 'thin_wallet/send_tokens', {}) - dummy_tx = genesis.BLOCK_GENESIS + dummy_tx = Transaction() self.assertIsNotNone(request._finishedDeferreds) resource._err_tx_resolve('Error', _Context(tx=dummy_tx, request=request), 'error') diff --git a/tests/tx/test_block.py b/tests/tx/test_block.py index 0b925a236..a7b362dfe 100644 --- a/tests/tx/test_block.py +++ b/tests/tx/test_block.py @@ -17,13 +17,17 @@ import pytest from hathor.conf import HathorSettings +from hathor.conf.get_settings import get_settings from hathor.transaction import Block, TransactionMetadata -from hathor.transaction.genesis import BLOCK_GENESIS -from hathor.transaction.storage import TransactionStorage +from hathor.transaction.storage import TransactionMemoryStorage, TransactionStorage def test_calculate_feature_activation_bit_counts_genesis(): - result = BLOCK_GENESIS.calculate_feature_activation_bit_counts() + settings = get_settings() + storage = TransactionMemoryStorage() + genesis_block = storage.get_transaction(settings.GENESIS_BLOCK_HASH) + assert isinstance(genesis_block, Block) + result = genesis_block.calculate_feature_activation_bit_counts() assert result == [0, 0, 0, 0] diff --git a/tests/tx/test_indexes2.py b/tests/tx/test_indexes2.py index ea8aea4ea..edca6c0c7 100644 --- a/tests/tx/test_indexes2.py +++ b/tests/tx/test_indexes2.py @@ -25,7 +25,7 @@ def setUp(self): # how many transactions will be generated on the same timestamp before increasing it by 1 self.transactions = [] repetitions = [1, 1, 10, 10, 10, 2, 1, 0, 0, 5, 5, 5, 0, 1, 1, 10, 10, 10, 1, 2, 3, 1, 100, 100, 1, 100, 0, 1] - ts = settings.GENESIS_TIMESTAMP + ts = settings.GENESIS_BLOCK_TIMESTAMP for rep in repetitions: for _ in range(rep): tx = FakeTransaction(self.rng.randbytes(32), ts) diff --git a/tests/tx/test_mining.py b/tests/tx/test_mining.py index 8524966dd..822231907 100644 --- a/tests/tx/test_mining.py +++ b/tests/tx/test_mining.py @@ -42,7 +42,7 @@ def test_block_template_after_genesis(self) -> None: reward=settings.INITIAL_TOKEN_UNITS_PER_BLOCK * 100, weight=1.0, timestamp_now=int(manager.reactor.seconds()), - timestamp_min=settings.GENESIS_TIMESTAMP + 3, + timestamp_min=settings.GENESIS_BLOCK_TIMESTAMP + 3, timestamp_max=0xffffffff, # no limit for next block after genesis # parents=[tx.hash for tx in self.genesis_blocks + self.genesis_txs], parents=block_templates[0].parents, diff --git a/tests/tx/test_stratum.py b/tests/tx/test_stratum.py index 42d5082df..f059aacb8 100644 --- a/tests/tx/test_stratum.py +++ b/tests/tx/test_stratum.py @@ -21,6 +21,7 @@ StratumFactory, ) from hathor.transaction.block import Block +from hathor.transaction.storage import TransactionMemoryStorage from tests import unittest @@ -252,8 +253,8 @@ class BaseStratumClientTest(unittest.TestCase): def setUp(self): super().setUp() - from hathor.transaction.genesis import _get_genesis_transactions_unsafe - self.block = next(x for x in _get_genesis_transactions_unsafe(None) if x.is_block) + storage = TransactionMemoryStorage() + self.block = storage.get_transaction(self._settings.GENESIS_BLOCK_HASH) self.transport = StringTransportWithDisconnection() self.protocol = StratumClient() self.protocol.makeConnection(self.transport) diff --git a/tests/tx/test_tx_storage.py b/tests/tx/test_tx_storage.py index b2f83a0a2..909164169 100644 --- a/tests/tx/test_tx_storage.py +++ b/tests/tx/test_tx_storage.py @@ -18,7 +18,6 @@ from tests.utils import ( BURN_ADDRESS, HAS_ROCKSDB, - MIN_TIMESTAMP, add_blocks_unlock_reward, add_new_blocks, add_new_transactions, @@ -61,7 +60,8 @@ def setUp(self): block_parents = [tx.hash for tx in chain(self.genesis_blocks, self.genesis_txs)] output = TxOutput(200, P2PKH.create_output_script(BURN_ADDRESS)) - self.block = Block(timestamp=MIN_TIMESTAMP, weight=12, outputs=[output], parents=block_parents, + previous_timestamp = artifacts.settings.GENESIS_TX2_TIMESTAMP + self.block = Block(timestamp=previous_timestamp + 1, weight=12, outputs=[output], parents=block_parents, nonce=100781, storage=self.tx_storage) self.block.resolve() self.manager.verification_service.verify(self.block) @@ -77,7 +77,7 @@ def setUp(self): '620e78362cf2d908e9057ac235a63')) self.tx = Transaction( - timestamp=MIN_TIMESTAMP + 2, weight=10, nonce=932049, inputs=[tx_input], outputs=[output], + timestamp=previous_timestamp + 2, weight=10, nonce=932049, inputs=[tx_input], outputs=[output], tokens=[bytes.fromhex('0023be91834c973d6a6ddd1a0ae411807b7c8ef2a015afb5177ee64b666ce602')], parents=tx_parents, storage=self.tx_storage) self.tx.resolve() @@ -100,16 +100,6 @@ def test_genesis_ref(self): tx2 = self.tx_storage.get_transaction(tx.hash) self.assertTrue(tx is tx2) - from hathor.transaction.genesis import _get_genesis_transactions_unsafe - genesis_from_settings = _get_genesis_transactions_unsafe(None) - for tx in genesis_from_settings: - tx2 = self.tx_storage.get_transaction(tx.hash) - self.assertTrue(tx is not tx2) - for tx3 in genesis_set: - self.assertTrue(tx is not tx3) - if tx2 == tx3: - self.assertTrue(tx2 is tx3) - def test_genesis(self): self.assertEqual(1, len(self.genesis_blocks)) self.assertEqual(2, len(self.genesis_txs)) diff --git a/tests/unittest.py b/tests/unittest.py index e9ccfdbb3..837bec2e5 100644 --- a/tests/unittest.py +++ b/tests/unittest.py @@ -275,9 +275,9 @@ def assertIsTopological(self, tx_sequence: Iterator[BaseTransaction], message: O An initial set can be optionally provided. """ - from hathor.transaction.genesis import GENESIS_HASHES + from hathor.transaction.genesis import get_all_genesis_hashes - valid_deps = set(GENESIS_HASHES if initial is None else initial) + valid_deps = set(get_all_genesis_hashes(self._settings) if initial is None else initial) for tx in tx_sequence: assert tx.hash is not None diff --git a/tests/utils.py b/tests/utils.py index 2878e60a2..6a9403666 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -18,7 +18,7 @@ from hathor.event.model.event_data import TxData, TxMetadata from hathor.event.model.event_type import EventType from hathor.manager import HathorManager -from hathor.transaction import BaseTransaction, Transaction, TxInput, TxOutput, genesis +from hathor.transaction import BaseTransaction, Transaction, TxInput, TxOutput from hathor.transaction.scripts import P2PKH, HathorScript, Opcode, parse_address_script from hathor.transaction.token_creation_tx import TokenCreationTransaction from hathor.transaction.util import get_deposit_amount @@ -33,8 +33,6 @@ settings = HathorSettings() -MIN_TIMESTAMP = genesis.GENESIS[-1].timestamp + 1 - # useful for adding blocks to a different wallet BURN_ADDRESS = bytes.fromhex('28acbfb94571417423c1ed66f706730c4aea516ac5762cccb8')