Skip to content

Commit a86c175

Browse files
committed
implement PR suggestions
1 parent a0cdd3c commit a86c175

24 files changed

+97
-101
lines changed

hathor/conf/mainnet.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# Genesis stuff
2626
# output addr: HJB2yxxsHtudGGy3jmVeadwMfRi2zNCKKD
2727
GENESIS_OUTPUT_SCRIPT=bytes.fromhex('76a9147fd4ae0e4fb2d2854e76d359029d8078bb99649e88ac'),
28-
GENESIS_TIMESTAMP=1578075305,
28+
GENESIS_BLOCK_TIMESTAMP=1578075305,
2929
GENESIS_BLOCK_NONCE=2591358,
3030
GENESIS_BLOCK_HASH=bytes.fromhex('000006cb93385b8b87a545a1cbb6197e6caff600c12cc12fc54250d39c8088fc'),
3131
GENESIS_TX1_NONCE=7715,

hathor/conf/mainnet.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ WHITELIST_URL: https://hathor-public-files.s3.amazonaws.com/whitelist_peer_ids
88

99
# Genesis stuff
1010
GENESIS_OUTPUT_SCRIPT: 76a9147fd4ae0e4fb2d2854e76d359029d8078bb99649e88ac
11-
GENESIS_TIMESTAMP: 1578075305
11+
GENESIS_BLOCK_TIMESTAMP: 1578075305
1212
GENESIS_BLOCK_NONCE: 2591358
1313
GENESIS_BLOCK_HASH: 000006cb93385b8b87a545a1cbb6197e6caff600c12cc12fc54250d39c8088fc
1414
GENESIS_TX1_NONCE: 7715

hathor/conf/settings.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,20 @@ def MAXIMUM_NUMBER_OF_HALVINGS(self) -> int:
9898
GENESIS_OUTPUT_SCRIPT: bytes = bytes.fromhex('76a914a584cf48b161e4a49223ed220df30037ab740e0088ac')
9999

100100
# Genesis timestamps, nonces and hashes
101-
GENESIS_TIMESTAMP: int = 1572636343 # used as is for genesis_block, +1 for genesis_tx1 and +2 for genesis_tx2
101+
102+
# Timestamp used for the genesis block
103+
GENESIS_BLOCK_TIMESTAMP: int = 1572636343
104+
105+
@property
106+
def GENESIS_TX1_TIMESTAMP(self) -> int:
107+
"""Timestamp used for the first genesis transaction."""
108+
return self.GENESIS_BLOCK_TIMESTAMP + 1
109+
110+
@property
111+
def GENESIS_TX2_TIMESTAMP(self) -> int:
112+
"""Timestamp used for the second genesis transaction."""
113+
return self.GENESIS_BLOCK_TIMESTAMP + 2
114+
102115
GENESIS_BLOCK_NONCE: int = 3526202
103116
GENESIS_BLOCK_HASH: bytes = bytes.fromhex('000007eb968a6cdf0499e2d033faf1e163e0dc9cf41876acad4d421836972038')
104117
GENESIS_TX1_NONCE: int = 12595

hathor/conf/testnet.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
BOOTSTRAP_DNS=['golf.testnet.hathor.network'],
2626
# Genesis stuff
2727
GENESIS_OUTPUT_SCRIPT=bytes.fromhex('76a914a584cf48b161e4a49223ed220df30037ab740e0088ac'),
28-
GENESIS_TIMESTAMP=1577836800,
28+
GENESIS_BLOCK_TIMESTAMP=1577836800,
2929
GENESIS_BLOCK_NONCE=826272,
3030
GENESIS_BLOCK_HASH=bytes.fromhex('0000033139d08176d1051fb3a272c3610457f0c7f686afbe0afe3d37f966db85'),
3131
GENESIS_TX1_NONCE=190,

hathor/conf/testnet.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ BOOTSTRAP_DNS:
66

77
# Genesis stuff
88
GENESIS_OUTPUT_SCRIPT: 76a914a584cf48b161e4a49223ed220df30037ab740e0088ac
9-
GENESIS_TIMESTAMP: 1577836800
9+
GENESIS_BLOCK_TIMESTAMP: 1577836800
1010
GENESIS_BLOCK_NONCE: 826272
1111
GENESIS_BLOCK_HASH: 0000033139d08176d1051fb3a272c3610457f0c7f686afbe0afe3d37f966db85
1212
GENESIS_TX1_NONCE: 190

hathor/indexes/height_index.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def get_genesis_block_entry(self) -> IndexEntry:
5454
"""Return the index entry for the genesis block."""
5555
return IndexEntry(
5656
self._settings.GENESIS_BLOCK_HASH,
57-
self._settings.GENESIS_TIMESTAMP
57+
self._settings.GENESIS_BLOCK_TIMESTAMP
5858
)
5959

6060
def get_scope(self) -> Scope:

hathor/indexes/memory_info_index.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,10 @@ def get_db_name(self) -> Optional[str]:
3636
return None
3737

3838
def force_clear(self) -> None:
39-
from hathor.transaction.genesis import get_genesis_block, get_genesis_tx2
4039
self._block_count = 1
4140
self._tx_count = 2
42-
self._first_timestamp = get_genesis_block(self._settings).timestamp
43-
self._latest_timestamp = get_genesis_tx2(self._settings).timestamp
41+
self._first_timestamp = self._settings.GENESIS_BLOCK_TIMESTAMP
42+
self._latest_timestamp = self._settings.GENESIS_TX2_TIMESTAMP
4443

4544
def update_timestamps(self, tx: BaseTransaction) -> None:
4645
if tx.is_genesis:

hathor/p2p/utils.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from hathor.conf.get_settings import get_settings
3232
from hathor.indexes.height_index import HeightInfo
3333
from hathor.p2p.peer_discovery import DNSPeerDiscovery
34-
from hathor.transaction.genesis import get_genesis_hash
34+
from hathor.transaction.genesis import get_representation_for_all_genesis
3535

3636

3737
def discover_hostname() -> Optional[str]:
@@ -76,7 +76,7 @@ def get_genesis_short_hash() -> str:
7676
""" Return the first 7 chars of the GENESIS_HASH used for validation that the genesis are the same
7777
"""
7878
settings = get_settings()
79-
return get_genesis_hash(settings).hex()[:7]
79+
return get_representation_for_all_genesis(settings).hex()[:7]
8080

8181

8282
def get_settings_hello_dict() -> dict[str, Any]:

hathor/simulator/simulator.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def start(self) -> None:
127127
assert not self._started
128128
self._started = True
129129
self._patches_rc_increment()
130-
first_timestamp = self.settings.GENESIS_TIMESTAMP
130+
first_timestamp = self.settings.GENESIS_BLOCK_TIMESTAMP
131131
dt = self.rng.randint(3600, 120 * 24 * 3600)
132132
self._clock.advance(first_timestamp + dt)
133133
self.log.debug('randomized step: clock advance start', dt=dt)

hathor/transaction/genesis.py

+2-45
Original file line numberDiff line numberDiff line change
@@ -15,54 +15,11 @@
1515
from typing import TYPE_CHECKING
1616

1717
from hathor.conf.settings import HathorSettings
18-
from hathor.transaction import BaseTransaction, Block, Transaction, TxOutput
1918

2019
if TYPE_CHECKING:
2120
from hathor.transaction.storage import TransactionStorage # noqa: F401
2221

2322

24-
def get_genesis_block(settings: HathorSettings) -> Block:
25-
"""Return the genesis block."""
26-
return Block(
27-
hash=settings.GENESIS_BLOCK_HASH,
28-
nonce=settings.GENESIS_BLOCK_NONCE,
29-
timestamp=settings.GENESIS_TIMESTAMP,
30-
weight=settings.MIN_BLOCK_WEIGHT,
31-
outputs=[
32-
TxOutput(settings.GENESIS_TOKENS, settings.GENESIS_OUTPUT_SCRIPT),
33-
],
34-
)
35-
36-
37-
def get_genesis_tx1(settings: HathorSettings) -> Transaction:
38-
"""Return the genesis tx1."""
39-
return Transaction(
40-
hash=settings.GENESIS_TX1_HASH,
41-
nonce=settings.GENESIS_TX1_NONCE,
42-
timestamp=settings.GENESIS_TIMESTAMP + 1,
43-
weight=settings.MIN_TX_WEIGHT,
44-
)
45-
46-
47-
def get_genesis_tx2(settings: HathorSettings) -> Transaction:
48-
"""Return the genesis tx2."""
49-
return Transaction(
50-
hash=settings.GENESIS_TX2_HASH,
51-
nonce=settings.GENESIS_TX2_NONCE,
52-
timestamp=settings.GENESIS_TIMESTAMP + 2,
53-
weight=settings.MIN_TX_WEIGHT,
54-
)
55-
56-
57-
def get_all_genesis(settings: HathorSettings) -> list[BaseTransaction]:
58-
"""Return all genesis vertices."""
59-
return [
60-
get_genesis_block(settings),
61-
get_genesis_tx1(settings),
62-
get_genesis_tx2(settings)
63-
]
64-
65-
6623
def get_all_genesis_hashes(settings: HathorSettings) -> list[bytes]:
6724
"""Return all genesis hashes."""
6825
return [
@@ -72,8 +29,8 @@ def get_all_genesis_hashes(settings: HathorSettings) -> list[bytes]:
7229
]
7330

7431

75-
def get_genesis_hash(settings: HathorSettings) -> bytes:
76-
"""Return a single hash representing all genesis."""
32+
def get_representation_for_all_genesis(settings: HathorSettings) -> bytes:
33+
"""Return a single hash representing all genesis vertices."""
7734
import hashlib
7835
h = hashlib.sha256()
7936
for tx_hash in get_all_genesis_hashes(settings):

hathor/transaction/storage/transaction_storage.py

+50-10
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from hathor.indexes.height_index import HeightInfo
2929
from hathor.profiler import get_cpu_profiler
3030
from hathor.pubsub import PubSubManager
31-
from hathor.transaction.base_transaction import BaseTransaction
31+
from hathor.transaction.base_transaction import BaseTransaction, TxOutput
3232
from hathor.transaction.block import Block
3333
from hathor.transaction.storage.exceptions import (
3434
TransactionDoesNotExist,
@@ -305,7 +305,13 @@ def get_best_block(self) -> Block:
305305
def _save_or_verify_genesis(self) -> None:
306306
"""Save all genesis in the storage."""
307307
self._saving_genesis = True
308-
for tx in self._get_genesis_from_settings():
308+
genesis_txs = [
309+
self._construct_genesis_block(),
310+
self._construct_genesis_tx1(),
311+
self._construct_genesis_tx2(),
312+
]
313+
314+
for tx in genesis_txs:
309315
try:
310316
assert tx.hash is not None
311317
tx2 = self.get_transaction(tx.hash)
@@ -318,14 +324,6 @@ def _save_or_verify_genesis(self) -> None:
318324
self._genesis_cache[tx2.hash] = tx2
319325
self._saving_genesis = False
320326

321-
def _get_genesis_from_settings(self) -> list[BaseTransaction]:
322-
"""Return all genesis from settings."""
323-
from hathor.transaction.genesis import get_all_genesis
324-
all_genesis = get_all_genesis(self._settings)
325-
for tx in all_genesis:
326-
tx.storage = self
327-
return all_genesis
328-
329327
def _save_to_weakref(self, tx: BaseTransaction) -> None:
330328
""" Save transaction to weakref.
331329
"""
@@ -1089,6 +1087,48 @@ def compute_transactions_that_became_invalid(self, new_best_height: int) -> list
10891087
to_remove.append(tx)
10901088
return to_remove
10911089

1090+
def _construct_genesis_block(self) -> Block:
1091+
"""Return the genesis block."""
1092+
block = Block(
1093+
storage=self,
1094+
nonce=self._settings.GENESIS_BLOCK_NONCE,
1095+
timestamp=self._settings.GENESIS_BLOCK_TIMESTAMP,
1096+
weight=self._settings.MIN_BLOCK_WEIGHT,
1097+
outputs=[
1098+
TxOutput(self._settings.GENESIS_TOKENS, self._settings.GENESIS_OUTPUT_SCRIPT),
1099+
],
1100+
)
1101+
block.update_hash()
1102+
1103+
assert block.hash == self._settings.GENESIS_BLOCK_HASH
1104+
return block
1105+
1106+
def _construct_genesis_tx1(self) -> Transaction:
1107+
"""Return the genesis tx1."""
1108+
tx1 = Transaction(
1109+
storage=self,
1110+
nonce=self._settings.GENESIS_TX1_NONCE,
1111+
timestamp=self._settings.GENESIS_TX1_TIMESTAMP,
1112+
weight=self._settings.MIN_TX_WEIGHT,
1113+
)
1114+
tx1.update_hash()
1115+
1116+
assert tx1.hash == self._settings.GENESIS_TX1_HASH
1117+
return tx1
1118+
1119+
def _construct_genesis_tx2(self) -> Transaction:
1120+
"""Return the genesis tx2."""
1121+
tx2 = Transaction(
1122+
storage=self,
1123+
nonce=self._settings.GENESIS_TX2_NONCE,
1124+
timestamp=self._settings.GENESIS_TX2_TIMESTAMP,
1125+
weight=self._settings.MIN_TX_WEIGHT,
1126+
)
1127+
tx2.update_hash()
1128+
1129+
assert tx2.hash == self._settings.GENESIS_TX2_HASH
1130+
return tx2
1131+
10921132

10931133
class BaseTransactionStorage(TransactionStorage):
10941134
indexes: Optional[IndexesManager]

tests/others/fixtures/invalid_byte_hathor_settings_fixture.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ENABLE_PEER_WHITELIST: true
77
WHITELIST_URL: https://hathor-public-files.s3.amazonaws.com/whitelist_peer_ids
88

99
GENESIS_OUTPUT_SCRIPT: 76a9147fd4ae0e4fb2d2854e76d359029d8078bb99649e88ac
10-
GENESIS_TIMESTAMP: 1578075305
10+
GENESIS_BLOCK_TIMESTAMP: 1578075305
1111
GENESIS_BLOCK_NONCE: 2591358
1212
GENESIS_BLOCK_HASH: 000006cb93385b8b87a545a1cbb6197e6caff600c12cc12fc54250d39c8088fc
1313
GENESIS_TX1_NONCE: 7715

tests/others/fixtures/invalid_features_hathor_settings_fixture.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ENABLE_PEER_WHITELIST: true
77
WHITELIST_URL: https://hathor-public-files.s3.amazonaws.com/whitelist_peer_ids
88

99
GENESIS_OUTPUT_SCRIPT: 76a9147fd4ae0e4fb2d2854e76d359029d8078bb99649e88ac
10-
GENESIS_TIMESTAMP: 1578075305
10+
GENESIS_BLOCK_TIMESTAMP: 1578075305
1111
GENESIS_BLOCK_NONCE: 2591358
1212
GENESIS_BLOCK_HASH: 000006cb93385b8b87a545a1cbb6197e6caff600c12cc12fc54250d39c8088fc
1313
GENESIS_TX1_NONCE: 7715

tests/others/fixtures/missing_hathor_settings_fixture.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ENABLE_PEER_WHITELIST: true
66
WHITELIST_URL: https://hathor-public-files.s3.amazonaws.com/whitelist_peer_ids
77

88
GENESIS_OUTPUT_SCRIPT: 76a9147fd4ae0e4fb2d2854e76d359029d8078bb99649e88ac
9-
GENESIS_TIMESTAMP: 1578075305
9+
GENESIS_BLOCK_TIMESTAMP: 1578075305
1010
GENESIS_BLOCK_NONCE: 2591358
1111
GENESIS_BLOCK_HASH: 000006cb93385b8b87a545a1cbb6197e6caff600c12cc12fc54250d39c8088fc
1212
GENESIS_TX1_NONCE: 7715

tests/others/fixtures/valid_hathor_settings_fixture.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ENABLE_PEER_WHITELIST: true
77
WHITELIST_URL: https://hathor-public-files.s3.amazonaws.com/whitelist_peer_ids
88

99
GENESIS_OUTPUT_SCRIPT: 76a9147fd4ae0e4fb2d2854e76d359029d8078bb99649e88ac
10-
GENESIS_TIMESTAMP: 1578075305
10+
GENESIS_BLOCK_TIMESTAMP: 1578075305
1111
GENESIS_BLOCK_NONCE: 2591358
1212
GENESIS_BLOCK_HASH: 000006cb93385b8b87a545a1cbb6197e6caff600c12cc12fc54250d39c8088fc
1313
GENESIS_TX1_NONCE: 7715

tests/others/test_hathor_settings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def test_valid_hathor_settings_from_yaml(filepath):
4141
MIN_TX_WEIGHT_COEFFICIENT=0,
4242
MIN_TX_WEIGHT=8,
4343
GENESIS_OUTPUT_SCRIPT=bytes.fromhex('76a9147fd4ae0e4fb2d2854e76d359029d8078bb99649e88ac'),
44-
GENESIS_TIMESTAMP=1578075305,
44+
GENESIS_BLOCK_TIMESTAMP=1578075305,
4545
GENESIS_BLOCK_NONCE=2591358,
4646
GENESIS_BLOCK_HASH=bytes.fromhex('000006cb93385b8b87a545a1cbb6197e6caff600c12cc12fc54250d39c8088fc'),
4747
GENESIS_TX1_NONCE=7715,

tests/p2p/test_split_brain.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class BaseHathorSyncMethodsTestCase(unittest.TestCase):
1515
def setUp(self):
1616
super().setUp()
1717

18-
first_timestamp = self._settings.GENESIS_TIMESTAMP
18+
first_timestamp = self._settings.GENESIS_BLOCK_TIMESTAMP
1919
self.clock.advance(first_timestamp + self.rng.randint(3600, 120*24*3600))
2020

2121
self.network = 'testnet'

tests/resources/wallet/test_thin_wallet.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from hathor.conf import HathorSettings
66
from hathor.crypto.util import decode_address
77
from hathor.daa import minimum_tx_weight
8-
from hathor.transaction import Transaction, TxInput, TxOutput, genesis
8+
from hathor.transaction import Transaction, TxInput, TxOutput
99
from hathor.transaction.scripts import P2PKH, create_output_script, parse_address_script
1010
from hathor.wallet.resources.thin_wallet import (
1111
AddressHistoryResource,
@@ -275,7 +275,7 @@ def test_error_request(self):
275275

276276
resource = SendTokensResource(self.manager)
277277
request = TestDummyRequest('POST', 'thin_wallet/send_tokens', {})
278-
dummy_tx = genesis.get_genesis_block(self._settings)
278+
dummy_tx = Transaction()
279279

280280
self.assertIsNotNone(request._finishedDeferreds)
281281
resource._err_tx_resolve('Error', _Context(tx=dummy_tx, request=request), 'error')

tests/tx/test_block.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919
from hathor.conf import HathorSettings
2020
from hathor.conf.get_settings import get_settings
2121
from hathor.transaction import Block, TransactionMetadata
22-
from hathor.transaction.genesis import get_genesis_block
23-
from hathor.transaction.storage import TransactionStorage
22+
from hathor.transaction.storage import TransactionMemoryStorage, TransactionStorage
2423

2524

2625
def test_calculate_feature_activation_bit_counts_genesis():
2726
settings = get_settings()
28-
result = get_genesis_block(settings).calculate_feature_activation_bit_counts()
27+
storage = TransactionMemoryStorage()
28+
genesis_block = storage.get_transaction(settings.GENESIS_BLOCK_HASH)
29+
assert isinstance(genesis_block, Block)
30+
result = genesis_block.calculate_feature_activation_bit_counts()
2931

3032
assert result == [0, 0, 0, 0]
3133

tests/tx/test_indexes2.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def setUp(self):
2525
# how many transactions will be generated on the same timestamp before increasing it by 1
2626
self.transactions = []
2727
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]
28-
ts = settings.GENESIS_TIMESTAMP
28+
ts = settings.GENESIS_BLOCK_TIMESTAMP
2929
for rep in repetitions:
3030
for _ in range(rep):
3131
tx = FakeTransaction(self.rng.randbytes(32), ts)

tests/tx/test_mining.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_block_template_after_genesis(self) -> None:
4242
reward=settings.INITIAL_TOKEN_UNITS_PER_BLOCK * 100,
4343
weight=1.0,
4444
timestamp_now=int(manager.reactor.seconds()),
45-
timestamp_min=settings.GENESIS_TIMESTAMP + 3,
45+
timestamp_min=settings.GENESIS_BLOCK_TIMESTAMP + 3,
4646
timestamp_max=0xffffffff, # no limit for next block after genesis
4747
# parents=[tx.hash for tx in self.genesis_blocks + self.genesis_txs],
4848
parents=block_templates[0].parents,

tests/tx/test_stratum.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
StratumFactory,
2222
)
2323
from hathor.transaction.block import Block
24+
from hathor.transaction.storage import TransactionMemoryStorage
2425
from tests import unittest
2526

2627

@@ -252,8 +253,8 @@ class BaseStratumClientTest(unittest.TestCase):
252253

253254
def setUp(self):
254255
super().setUp()
255-
from hathor.transaction.genesis import get_genesis_block
256-
self.block = get_genesis_block(self._settings)
256+
storage = TransactionMemoryStorage()
257+
self.block = storage.get_transaction(self._settings.GENESIS_BLOCK_HASH)
257258
self.transport = StringTransportWithDisconnection()
258259
self.protocol = StratumClient()
259260
self.protocol.makeConnection(self.transport)

0 commit comments

Comments
 (0)