Skip to content

Commit f469192

Browse files
committed
feat: change transaction storage to fit protocol
1 parent 0c84969 commit f469192

File tree

8 files changed

+47
-68
lines changed

8 files changed

+47
-68
lines changed

hathor/builder/builder.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
from hathor.nanocontracts.nc_exec_logs import NCLogConfig, NCLogStorage
4040
from hathor.nanocontracts.runner.runner import RunnerFactory
4141
from hathor.nanocontracts.sorter.types import NCSorterCallable
42-
from hathor.nanocontracts.tx_storage_proxy import TransactionStorageProxy
4342
from hathor.p2p.manager import ConnectionsManager
4443
from hathor.p2p.peer import PrivatePeer
4544
from hathor.pubsub import PubSubManager
@@ -437,7 +436,7 @@ def _get_or_create_runner_factory(self) -> RunnerFactory:
437436
self._runner_factory = RunnerFactory(
438437
reactor=self._get_reactor(),
439438
settings=self._get_or_create_settings(),
440-
tx_storage_proxy=TransactionStorageProxy(self._get_or_create_tx_storage()),
439+
tx_storage=self._get_or_create_tx_storage(),
441440
nc_storage_factory=self._get_or_create_nc_storage_factory(),
442441
)
443442
return self._runner_factory

hathor/nanocontracts/runner/runner.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,20 @@ class Runner:
122122
MAX_RECURSION_DEPTH: int = 100
123123
MAX_CALL_COUNTER: int = 250
124124

125+
_runtime_version: NanoRuntimeVersion
126+
125127
def __init__(
126128
self,
127129
*,
128130
reactor: ReactorProtocol,
129131
settings: HathorSettings,
130132
runtime_version: NanoRuntimeVersion,
131-
tx_storage_proxy: NCTransactionStorageProxy,
133+
tx_storage: NCTransactionStorageProxy,
132134
storage_factory: NCStorageFactory,
133135
block_storage: NCBlockStorage,
134136
seed: bytes | None,
135137
) -> None:
136-
self.tx_storage_proxy = tx_storage_proxy
138+
self.tx_storage = tx_storage
137139
self.storage_factory = storage_factory
138140
self.block_storage = block_storage
139141
self._storages: dict[ContractId, NCContractStorage] = {}
@@ -1143,7 +1145,7 @@ def _create_blueprint_instance(self, blueprint_id: BlueprintId, changes_tracker:
11431145
"""Create a new blueprint instance."""
11441146
assert self._call_info is not None
11451147
env = BlueprintEnvironment(self, self._call_info.nc_logger, changes_tracker)
1146-
blueprint_class = self.tx_storage_proxy.get_blueprint_class(blueprint_id)
1148+
blueprint_class = self.tx_storage.get_blueprint_class(blueprint_id)
11471149
return blueprint_class(env)
11481150

11491151
@_forbid_syscall_from_view('create_deposit_token')
@@ -1270,7 +1272,7 @@ def syscall_change_blueprint(self, blueprint_id: BlueprintId) -> None:
12701272

12711273
# The blueprint must exist. If an unknown blueprint is provided, it will raise an BlueprintDoesNotExist
12721274
# exception.
1273-
self.tx_storage_proxy.get_blueprint_class(blueprint_id)
1275+
self.tx_storage.get_blueprint_class(blueprint_id)
12741276

12751277
nc_storage = self.get_current_changes_tracker()
12761278
nc_storage.set_blueprint_id(blueprint_id)
@@ -1315,7 +1317,7 @@ def _get_token(self, token_uid: TokenUid) -> TokenDescription:
13151317
)
13161318

13171319
try:
1318-
return self.tx_storage_proxy.get_token_description(token_uid)
1320+
return self.tx_storage.get_token_description(token_uid)
13191321
except TransactionDoesNotExist:
13201322
raise NCInvalidSyscall(
13211323
f'contract {call_record.contract_id.hex()} could not find {token_uid.hex()} token'
@@ -1445,19 +1447,19 @@ def forbid_call_on_view(self, name: str) -> None:
14451447

14461448

14471449
class RunnerFactory:
1448-
__slots__ = ('reactor', 'settings', 'tx_storage_proxy', 'nc_storage_factory')
1450+
__slots__ = ('reactor', 'settings', 'tx_storage', 'nc_storage_factory')
14491451

14501452
def __init__(
14511453
self,
14521454
*,
14531455
reactor: ReactorProtocol,
14541456
settings: HathorSettings,
1455-
tx_storage_proxy: NCTransactionStorageProxy,
1457+
tx_storage: NCTransactionStorageProxy,
14561458
nc_storage_factory: NCStorageFactory,
14571459
) -> None:
14581460
self.reactor = reactor
14591461
self.settings = settings
1460-
self.tx_storage_proxy = tx_storage_proxy
1462+
self.tx_storage = tx_storage
14611463
self.nc_storage_factory = nc_storage_factory
14621464

14631465
def create(
@@ -1471,7 +1473,7 @@ def create(
14711473
reactor=self.reactor,
14721474
settings=self.settings,
14731475
runtime_version=runtime_version,
1474-
tx_storage_proxy=self.tx_storage_proxy,
1476+
tx_storage=self.tx_storage,
14751477
storage_factory=self.nc_storage_factory,
14761478
block_storage=block_storage,
14771479
seed=seed,

hathor/nanocontracts/tx_storage_proxy.py

Lines changed: 0 additions & 45 deletions
This file was deleted.

hathor/transaction/storage/transaction_storage.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
TokenCreationTransactionDoesNotExist,
3838
TransactionDoesNotExist,
3939
TransactionIsNotABlock,
40+
TransactionMetadataDoesNotExist,
4041
TransactionNotInAllowedScopeError,
4142
)
4243
from hathor.transaction.storage.migrations import (
@@ -53,6 +54,7 @@
5354
from hathor.transaction.transaction_metadata import TransactionMetadata
5455
from hathor.transaction.vertex_children import VertexChildrenService
5556
from hathor.types import VertexId
57+
from hathorlib.token_info import TokenDescription
5658

5759
if TYPE_CHECKING:
5860
from hathor.conf.settings import HathorSettings
@@ -575,6 +577,30 @@ def get_token_creation_transaction(self, hash_bytes: bytes) -> TokenCreationTran
575577
raise TokenCreationTransactionDoesNotExist(hash_bytes)
576578
return tx
577579

580+
def get_token_description(self, token_uid: bytes) -> TokenDescription:
581+
"""Get the confirmed token description for `token_uid`.
582+
583+
:param token_uid: Unique token id to fetch.
584+
:raises TransactionDoesNotExist: If the transaction with the given hash does not exist.
585+
:raises TokenCreationTransactionDoesNotExist: If the transaction exists but is not a TokenCreationTransaction.
586+
:raises TransactionMetadataDoesNotExist: The tx that creates this token has not been confirmed by any block.
587+
:return: The TokenCreationTransaction instance.
588+
"""
589+
# Check the transaction storage for existing tokens
590+
token_creation_tx = self.get_token_creation_transaction(token_uid)
591+
592+
if token_creation_tx.get_metadata().first_block is None:
593+
raise TransactionMetadataDoesNotExist(
594+
f"The {token_uid.hex()} token is not confirmed by any block"
595+
)
596+
597+
return TokenDescription(
598+
token_version=token_creation_tx.token_version,
599+
token_name=token_creation_tx.token_name,
600+
token_symbol=token_creation_tx.token_symbol,
601+
token_id=token_creation_tx.hash
602+
)
603+
578604
def get_block_by_height(self, height: int) -> Optional[Block]:
579605
"""Return a block in the best blockchain from the height index. This is fast."""
580606
ancestor_hash = self.indexes.height.get(height)

hathor_cli/builder.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
from hathor.mining.cpu_mining_service import CpuMiningService
3737
from hathor.nanocontracts.nc_exec_logs import NCLogStorage
3838
from hathor.nanocontracts.runner.runner import RunnerFactory
39-
from hathor.nanocontracts.tx_storage_proxy import TransactionStorageProxy
4039
from hathor.p2p.manager import ConnectionsManager
4140
from hathor.p2p.peer import PrivatePeer
4241
from hathor.p2p.peer_endpoint import PeerEndpoint
@@ -235,7 +234,7 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
235234
runner_factory = RunnerFactory(
236235
reactor=reactor,
237236
settings=settings,
238-
tx_storage_proxy=TransactionStorageProxy(tx_storage),
237+
tx_storage=tx_storage,
239238
nc_storage_factory=self.nc_storage_factory,
240239
)
241240

hathor_tests/nanocontracts/blueprints/unittest.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from hathor.nanocontracts.nano_runtime_version import NanoRuntimeVersion
1010
from hathor.nanocontracts.nc_exec_logs import NCLogConfig
1111
from hathor.nanocontracts.on_chain_blueprint import Code, OnChainBlueprint
12-
from hathor.nanocontracts.tx_storage_proxy import TransactionStorageProxy
1312
from hathor.nanocontracts.types import Address, BlueprintId, ContractId, NCAction, TokenUid, VertexId
1413
from hathor.nanocontracts.vertex_data import BlockData, create_vertex_data_from_vertex
1514
from hathor.transaction import Transaction, Vertex
@@ -68,7 +67,7 @@ def _get_contract_instance(self, contract_id: ContractId, *, locked: bool) -> Bl
6867
nc_logger = NCLogger(__reactor__=runner.reactor, __nc_id__=contract_id)
6968
env = BlueprintEnvironment(runner, nc_logger, contract_storage, disable_cache=True)
7069
blueprint_id = runner.get_blueprint_id(contract_id)
71-
blueprint_class = runner.tx_storage_proxy.get_blueprint_class(blueprint_id)
70+
blueprint_class = runner.tx_storage.get_blueprint_class(blueprint_id)
7271
contract = blueprint_class(env)
7372
return contract
7473

@@ -126,7 +125,7 @@ def _register_blueprint_contents(
126125
def build_runner(self, runtime_version: NanoRuntimeVersion = NanoRuntimeVersion.V2) -> TestRunner:
127126
"""Create a test runner."""
128127
return TestRunner(
129-
tx_storage_proxy=TransactionStorageProxy(self.manager.tx_storage),
128+
tx_storage=self.manager.tx_storage,
130129
settings=self._settings,
131130
reactor=self.reactor,
132131
runtime_version=runtime_version,

hathor_tests/nanocontracts/utils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,21 @@ def __init__(
3434
self,
3535
*,
3636
runtime_version: NanoRuntimeVersion,
37-
tx_storage_proxy: NCTransactionStorageProxy,
37+
tx_storage: NCTransactionStorageProxy,
3838
settings: HathorSettings,
3939
reactor: ReactorProtocol,
4040
seed: bytes | None = None,
4141
) -> None:
4242
if seed is None:
4343
seed = b'x' * 32
44-
assert isinstance(tx_storage_proxy.storage, TransactionRocksDBStorage)
45-
storage_factory = NCRocksDBStorageFactory(tx_storage_proxy.storage._rocksdb_storage)
46-
store = RocksDBNodeTrieStore(tx_storage_proxy.storage._rocksdb_storage)
44+
assert isinstance(tx_storage, TransactionRocksDBStorage)
45+
storage_factory = NCRocksDBStorageFactory(tx_storage._rocksdb_storage)
46+
store = RocksDBNodeTrieStore(tx_storage._rocksdb_storage)
4747
block_trie = PatriciaTrie(store)
4848
block_storage = NCBlockStorage(block_trie)
4949
self._runner: Runner = Runner(
5050
runtime_version=runtime_version,
51-
tx_storage_proxy=tx_storage_proxy,
51+
tx_storage=tx_storage,
5252
storage_factory=storage_factory,
5353
block_storage=block_storage,
5454
settings=settings,

hathorlib/hathorlib/nanocontracts/tx_storage_proxy.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@
1414

1515
from __future__ import annotations
1616

17-
from typing import Any, Protocol
17+
from typing import Protocol
1818

1919
from hathorlib.nanocontracts.blueprint import Blueprint
2020
from hathorlib.nanocontracts.types import BlueprintId, TokenUid
2121
from hathorlib.token_info import TokenDescription
2222

2323

2424
class NCTransactionStorageProxy(Protocol):
25-
storage: Any
2625
def get_blueprint_class(self, blueprint_id: BlueprintId) -> type[Blueprint]: ...
2726
def get_token_description(self, token_uid: TokenUid) -> TokenDescription: ...

0 commit comments

Comments
 (0)