Skip to content
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
2 changes: 1 addition & 1 deletion hathor/builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ def _get_or_create_indexes_manager(self) -> IndexesManager:
return self._indexes_manager

if self._force_memory_index or self._storage_type == StorageType.MEMORY:
self._indexes_manager = MemoryIndexesManager()
self._indexes_manager = MemoryIndexesManager(settings=self._get_or_create_settings())

elif self._storage_type == StorageType.ROCKSDB:
rocksdb_storage = self._get_or_create_rocksdb_storage()
Expand Down
7 changes: 5 additions & 2 deletions hathor/indexes/base_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Optional

Expand All @@ -22,6 +24,7 @@
from hathor.transaction.base_transaction import BaseTransaction

if TYPE_CHECKING: # pragma: no cover
from hathor.conf.settings import HathorSettings
from hathor.indexes.manager import IndexesManager

logger = get_logger()
Expand All @@ -33,8 +36,8 @@ class BaseIndex(ABC):
This class exists so we can interact with indexes without knowing anything specific to its implemented. It was
created to generalize how we initialize indexes and keep track of which ones are up-to-date.
"""
def __init__(self) -> None:
self._settings = get_global_settings()
def __init__(self, *, settings: HathorSettings | None = None) -> None:
self._settings = settings or get_global_settings()
self.log = logger.new()

def init_start(self, indexes_manager: 'IndexesManager') -> None:
Expand Down
5 changes: 3 additions & 2 deletions hathor/indexes/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from structlog import get_logger

from hathor.conf.settings import HathorSettings
from hathor.indexes.address_index import AddressIndex
from hathor.indexes.base_index import BaseIndex
from hathor.indexes.height_index import HeightIndex
Expand Down Expand Up @@ -259,7 +260,7 @@ def del_tx(self, tx: BaseTransaction, *, remove_all: bool = False, relax_assert:


class MemoryIndexesManager(IndexesManager):
def __init__(self) -> None:
def __init__(self, *, settings: HathorSettings | None = None) -> None:
from hathor.indexes.memory_height_index import MemoryHeightIndex
from hathor.indexes.memory_info_index import MemoryInfoIndex
from hathor.indexes.memory_timestamp_index import MemoryTimestampIndex
Expand All @@ -277,7 +278,7 @@ def __init__(self) -> None:
self.addresses = None
self.tokens = None
self.utxo = None
self.height = MemoryHeightIndex()
self.height = MemoryHeightIndex(settings=settings)
self.mempool_tips = None

# XXX: this has to be at the end of __init__, after everything has been initialized
Expand Down
5 changes: 3 additions & 2 deletions hathor/indexes/memory_height_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from typing import Optional

from hathor.conf.settings import HathorSettings
from hathor.indexes.height_index import HeightIndex, HeightInfo, IndexEntry


Expand All @@ -23,8 +24,8 @@ class MemoryHeightIndex(HeightIndex):

_index: list[IndexEntry]

def __init__(self) -> None:
super().__init__()
def __init__(self, *, settings: HathorSettings | None = None) -> None:
super().__init__(settings=settings)
self.force_clear()

def get_db_name(self) -> Optional[str]:
Expand Down
31 changes: 19 additions & 12 deletions hathor/transaction/base_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

import base64
import datetime
import hashlib
Expand All @@ -38,6 +40,7 @@
if TYPE_CHECKING:
from _hashlib import HASH

from hathor.conf.settings import HathorSettings
from hathor.transaction.storage import TransactionStorage # noqa: F401

logger = get_logger()
Expand Down Expand Up @@ -138,17 +141,20 @@ class BaseTransaction(ABC):
# bits reserved for future use, depending on the configuration.
signal_bits: int

def __init__(self,
nonce: int = 0,
timestamp: Optional[int] = None,
signal_bits: int = 0,
version: TxVersion = TxVersion.REGULAR_BLOCK,
weight: float = 0,
inputs: Optional[list['TxInput']] = None,
outputs: Optional[list['TxOutput']] = None,
parents: Optional[list[VertexId]] = None,
hash: Optional[VertexId] = None,
storage: Optional['TransactionStorage'] = None) -> None:
def __init__(
self,
nonce: int = 0,
timestamp: Optional[int] = None,
signal_bits: int = 0,
version: TxVersion = TxVersion.REGULAR_BLOCK,
weight: float = 0,
inputs: Optional[list['TxInput']] = None,
outputs: Optional[list['TxOutput']] = None,
parents: Optional[list[VertexId]] = None,
hash: Optional[VertexId] = None,
storage: Optional['TransactionStorage'] = None,
settings: HathorSettings | None = None,
) -> None:
"""
Nonce: nonce used for the proof-of-work
Timestamp: moment of creation
Expand All @@ -161,7 +167,7 @@ def __init__(self,
assert signal_bits <= _ONE_BYTE, f'signal_bits {hex(signal_bits)} must not be larger than one byte'
assert version <= _ONE_BYTE, f'version {hex(version)} must not be larger than one byte'

self._settings = get_global_settings()
self._settings = settings or get_global_settings()
self.nonce = nonce
self.timestamp = timestamp or int(time.time())
self.signal_bits = signal_bits
Expand Down Expand Up @@ -630,6 +636,7 @@ def get_metadata(self, *, force_reload: bool = False, use_storage: bool = True)
min_height = 0 if self.is_genesis else None

metadata = TransactionMetadata(
settings=self._settings,
hash=self._hash,
accumulated_weight=self.weight,
height=height,
Expand Down
42 changes: 29 additions & 13 deletions hathor/transaction/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

import base64
from itertools import starmap, zip_longest
from operator import add
Expand All @@ -30,6 +32,7 @@
from hathor.utils.int import get_bit_list

if TYPE_CHECKING:
from hathor.conf.settings import HathorSettings
from hathor.transaction.storage import TransactionStorage # noqa: F401

# Signal bits (B), version (B), outputs len (B)
Expand All @@ -42,19 +45,32 @@
class Block(BaseTransaction):
SERIALIZATION_NONCE_SIZE = 16

def __init__(self,
nonce: int = 0,
timestamp: Optional[int] = None,
signal_bits: int = 0,
version: TxVersion = TxVersion.REGULAR_BLOCK,
weight: float = 0,
outputs: Optional[list[TxOutput]] = None,
parents: Optional[list[bytes]] = None,
hash: Optional[bytes] = None,
data: bytes = b'',
storage: Optional['TransactionStorage'] = None) -> None:
super().__init__(nonce=nonce, timestamp=timestamp, signal_bits=signal_bits, version=version, weight=weight,
outputs=outputs or [], parents=parents or [], hash=hash, storage=storage)
def __init__(
self,
nonce: int = 0,
timestamp: Optional[int] = None,
signal_bits: int = 0,
version: TxVersion = TxVersion.REGULAR_BLOCK,
weight: float = 0,
outputs: Optional[list[TxOutput]] = None,
parents: Optional[list[bytes]] = None,
hash: Optional[bytes] = None,
data: bytes = b'',
storage: Optional['TransactionStorage'] = None,
settings: HathorSettings | None = None,
) -> None:
super().__init__(
nonce=nonce,
timestamp=timestamp,
signal_bits=signal_bits,
version=version,
weight=weight,
outputs=outputs or [],
parents=parents or [],
hash=hash,
storage=storage,
settings=settings,
)
self.data = data

def _get_formatted_fields_dict(self, short: bool = True) -> dict[str, str]:
Expand Down
45 changes: 31 additions & 14 deletions hathor/transaction/merge_mined_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

from typing import TYPE_CHECKING, Any, Optional

from hathor.transaction.aux_pow import BitcoinAuxPow
Expand All @@ -20,24 +22,39 @@
from hathor.transaction.util import VerboseCallback

if TYPE_CHECKING:
from hathor.conf.settings import HathorSettings
from hathor.transaction.storage import TransactionStorage # noqa: F401


class MergeMinedBlock(Block):
def __init__(self,
nonce: int = 0,
timestamp: Optional[int] = None,
signal_bits: int = 0,
version: TxVersion = TxVersion.MERGE_MINED_BLOCK,
weight: float = 0,
outputs: Optional[list[TxOutput]] = None,
parents: Optional[list[bytes]] = None,
hash: Optional[bytes] = None,
data: bytes = b'',
aux_pow: Optional[BitcoinAuxPow] = None,
storage: Optional['TransactionStorage'] = None) -> None:
super().__init__(nonce=nonce, timestamp=timestamp, signal_bits=signal_bits, version=version, weight=weight,
data=data, outputs=outputs or [], parents=parents or [], hash=hash, storage=storage)
def __init__(
self,
nonce: int = 0,
timestamp: Optional[int] = None,
signal_bits: int = 0,
version: TxVersion = TxVersion.MERGE_MINED_BLOCK,
weight: float = 0,
outputs: Optional[list[TxOutput]] = None,
parents: Optional[list[bytes]] = None,
hash: Optional[bytes] = None,
data: bytes = b'',
aux_pow: Optional[BitcoinAuxPow] = None,
storage: Optional['TransactionStorage'] = None,
settings: HathorSettings | None = None,
) -> None:
super().__init__(
nonce=nonce,
timestamp=timestamp,
signal_bits=signal_bits,
version=version,
weight=weight,
data=data,
outputs=outputs or [],
parents=parents or [],
hash=hash,
storage=storage,
settings=settings
)
self.aux_pow = aux_pow

def _get_formatted_fields_dict(self, short: bool = True) -> dict[str, str]:
Expand Down
5 changes: 4 additions & 1 deletion hathor/transaction/poa/poa_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from hathor.conf.settings import HathorSettings
from hathor.consensus import poa
from hathor.transaction import Block, TxOutput, TxVersion
from hathor.transaction.storage import TransactionStorage
Expand All @@ -36,6 +37,7 @@ def __init__(
storage: TransactionStorage | None = None,
signer_id: bytes = b'',
signature: bytes = b'',
settings: HathorSettings | None = None,
) -> None:
assert not outputs, 'PoaBlocks must not have outputs'
super().__init__(
Expand All @@ -48,7 +50,8 @@ def __init__(
parents=parents or [],
hash=hash,
data=data,
storage=storage
storage=storage,
settings=settings,
)
self.signer_id = signer_id
self.signature = signature
Expand Down
3 changes: 3 additions & 0 deletions hathor/transaction/storage/transaction_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,7 @@ def compute_transactions_that_became_invalid(self, new_best_height: int) -> list
def _construct_genesis_block(self) -> Block:
"""Return the genesis block."""
block = Block(
settings=self._settings,
storage=self,
nonce=self._settings.GENESIS_BLOCK_NONCE,
timestamp=self._settings.GENESIS_BLOCK_TIMESTAMP,
Expand All @@ -1127,6 +1128,7 @@ def _construct_genesis_block(self) -> Block:
def _construct_genesis_tx1(self) -> Transaction:
"""Return the genesis tx1."""
tx1 = Transaction(
settings=self._settings,
storage=self,
nonce=self._settings.GENESIS_TX1_NONCE,
timestamp=self._settings.GENESIS_TX1_TIMESTAMP,
Expand All @@ -1140,6 +1142,7 @@ def _construct_genesis_tx1(self) -> Transaction:
def _construct_genesis_tx2(self) -> Transaction:
"""Return the genesis tx2."""
tx2 = Transaction(
settings=self._settings,
storage=self,
nonce=self._settings.GENESIS_TX2_NONCE,
timestamp=self._settings.GENESIS_TX2_TIMESTAMP,
Expand Down
45 changes: 30 additions & 15 deletions hathor/transaction/token_creation_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from typing_extensions import override

from hathor.conf.settings import HathorSettings
from hathor.transaction.base_transaction import TxInput, TxOutput, TxVersion
from hathor.transaction.storage import TransactionStorage # noqa: F401
from hathor.transaction.transaction import TokenInfo, Transaction
Expand All @@ -35,21 +36,35 @@


class TokenCreationTransaction(Transaction):
def __init__(self,
nonce: int = 0,
timestamp: Optional[int] = None,
signal_bits: int = 0,
version: TxVersion = TxVersion.TOKEN_CREATION_TRANSACTION,
weight: float = 0,
inputs: Optional[list[TxInput]] = None,
outputs: Optional[list[TxOutput]] = None,
parents: Optional[list[bytes]] = None,
hash: Optional[bytes] = None,
token_name: str = '',
token_symbol: str = '',
storage: Optional['TransactionStorage'] = None) -> None:
super().__init__(nonce=nonce, timestamp=timestamp, signal_bits=signal_bits, version=version, weight=weight,
inputs=inputs, outputs=outputs or [], parents=parents or [], hash=hash, storage=storage)
def __init__(
self,
nonce: int = 0,
timestamp: Optional[int] = None,
signal_bits: int = 0,
version: TxVersion = TxVersion.TOKEN_CREATION_TRANSACTION,
weight: float = 0,
inputs: Optional[list[TxInput]] = None,
outputs: Optional[list[TxOutput]] = None,
parents: Optional[list[bytes]] = None,
hash: Optional[bytes] = None,
token_name: str = '',
token_symbol: str = '',
storage: Optional['TransactionStorage'] = None,
settings: HathorSettings | None = None,
) -> None:
super().__init__(
nonce=nonce,
timestamp=timestamp,
signal_bits=signal_bits,
version=version,
weight=weight,
inputs=inputs,
outputs=outputs or [],
parents=parents or [],
hash=hash,
storage=storage,
settings=settings,
)
self.token_name = token_name
self.token_symbol = token_symbol
# for this special tx, its own hash is used as the created token uid. We're artificially
Expand Down
Loading