|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 |
| -from typing import TYPE_CHECKING, Optional |
| 15 | +from typing import TYPE_CHECKING |
16 | 16 |
|
17 |
| -from hathor.conf import HathorSettings |
| 17 | +from hathor.conf.settings import HathorSettings |
18 | 18 | from hathor.transaction import BaseTransaction, Block, Transaction, TxOutput
|
19 | 19 |
|
20 | 20 | if TYPE_CHECKING:
|
21 | 21 | from hathor.transaction.storage import TransactionStorage # noqa: F401
|
22 | 22 |
|
23 |
| -settings = HathorSettings() |
24 | 23 |
|
25 |
| -BLOCK_GENESIS = Block( |
26 |
| - hash=settings.GENESIS_BLOCK_HASH, |
27 |
| - nonce=settings.GENESIS_BLOCK_NONCE, |
28 |
| - timestamp=settings.GENESIS_TIMESTAMP, |
29 |
| - weight=settings.MIN_BLOCK_WEIGHT, |
30 |
| - outputs=[ |
31 |
| - TxOutput(settings.GENESIS_TOKENS, settings.GENESIS_OUTPUT_SCRIPT), |
32 |
| - ], |
33 |
| -) |
34 |
| - |
35 |
| -TX_GENESIS1 = Transaction( |
36 |
| - hash=settings.GENESIS_TX1_HASH, |
37 |
| - nonce=settings.GENESIS_TX1_NONCE, |
38 |
| - timestamp=settings.GENESIS_TIMESTAMP + 1, |
39 |
| - weight=settings.MIN_TX_WEIGHT, |
40 |
| -) |
41 |
| - |
42 |
| -TX_GENESIS2 = Transaction( |
43 |
| - hash=settings.GENESIS_TX2_HASH, |
44 |
| - nonce=settings.GENESIS_TX2_NONCE, |
45 |
| - timestamp=settings.GENESIS_TIMESTAMP + 2, |
46 |
| - weight=settings.MIN_TX_WEIGHT, |
47 |
| -) |
48 |
| - |
49 |
| -GENESIS = [BLOCK_GENESIS, TX_GENESIS1, TX_GENESIS2] |
50 |
| - |
51 |
| -GENESIS_HASHES = [settings.GENESIS_BLOCK_HASH, settings.GENESIS_TX1_HASH, settings.GENESIS_TX2_HASH] |
52 |
| - |
53 |
| - |
54 |
| -def _get_genesis_hash() -> bytes: |
| 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 | + |
| 66 | +def get_all_genesis_hashes(settings: HathorSettings) -> list[bytes]: |
| 67 | + """Return all genesis hashes.""" |
| 68 | + return [ |
| 69 | + settings.GENESIS_BLOCK_HASH, |
| 70 | + settings.GENESIS_TX1_HASH, |
| 71 | + settings.GENESIS_TX2_HASH |
| 72 | + ] |
| 73 | + |
| 74 | + |
| 75 | +def get_genesis_hash(settings: HathorSettings) -> bytes: |
| 76 | + """Return a single hash representing all genesis.""" |
55 | 77 | import hashlib
|
56 | 78 | h = hashlib.sha256()
|
57 |
| - for tx in GENESIS: |
58 |
| - tx_hash = tx.hash |
59 |
| - assert tx_hash is not None |
| 79 | + for tx_hash in get_all_genesis_hashes(settings): |
60 | 80 | h.update(tx_hash)
|
61 | 81 | return h.digest()
|
62 | 82 |
|
63 | 83 |
|
64 |
| -GENESIS_HASH = _get_genesis_hash() |
65 |
| - |
66 |
| - |
67 |
| -def _get_genesis_transactions_unsafe(tx_storage: Optional['TransactionStorage']) -> list[BaseTransaction]: |
68 |
| - """You shouldn't get genesis directly. Please, get it from your storage instead.""" |
69 |
| - genesis = [] |
70 |
| - for tx in GENESIS: |
71 |
| - tx2 = tx.clone() |
72 |
| - tx2.storage = tx_storage |
73 |
| - genesis.append(tx2) |
74 |
| - return genesis |
75 |
| - |
76 |
| - |
77 |
| -def is_genesis(hash_bytes: bytes) -> bool: |
| 84 | +def is_genesis(hash_bytes: bytes, *, settings: HathorSettings) -> bool: |
78 | 85 | """Check whether hash is from a genesis transaction."""
|
79 |
| - for tx in GENESIS: |
80 |
| - if hash_bytes == tx.hash: |
81 |
| - return True |
82 |
| - return False |
| 86 | + return hash_bytes in get_all_genesis_hashes(settings) |
0 commit comments