Skip to content

Commit

Permalink
fix: profiler reactor initialization (#1058)
Browse files Browse the repository at this point in the history
  • Loading branch information
glevco authored and msbrogli committed Jul 5, 2024
1 parent 255fd58 commit 513844a
Show file tree
Hide file tree
Showing 91 changed files with 7,672 additions and 63 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ on: # yamllint disable-line rule:truthy
branches:
- master
- dev
- nano-master
tags:
- v*
pull_request:
branches:
- nano-master
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ all: check tests
# testing:

tests_cli = tests/cli/
tests_nano = tests/nanocontracts/ tests/tx/test_indexes_nc_history.py tests/resources/nanocontracts/
tests_lib = $(filter-out ${tests_cli} tests/__pycache__/, $(dir $(wildcard tests/*/.)))
tests_ci = extras/github/

Expand All @@ -24,6 +25,10 @@ pytest_flags = -p no:warnings --cov-report=term --cov-report=html --cov-report=x
#--implicit-reexport
#--no-implicit-reexport

.PHONY: tests-nano
tests-nano:
pytest --durations=10 --cov-report=html --cov=hathor/nanocontracts/ --cov-config=.coveragerc_full -p no:warnings $(tests_nano)

.PHONY: tests-cli
tests-cli:
pytest --durations=10 --cov=hathor/cli/ --cov-config=.coveragerc_full --cov-fail-under=27 -p no:warnings $(tests_cli)
Expand Down
43 changes: 42 additions & 1 deletion hathor/builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
from hathor.indexes import IndexesManager, MemoryIndexesManager, RocksDBIndexesManager
from hathor.manager import HathorManager
from hathor.mining.cpu_mining_service import CpuMiningService
from hathor.nanocontracts import NCMemoryStorageFactory, NCRocksDBStorageFactory, NCStorageFactory
from hathor.nanocontracts.catalog import NCBlueprintCatalog
from hathor.p2p.manager import ConnectionsManager
from hathor.p2p.peer_id import PeerId
from hathor.pubsub import PubSubManager
Expand Down Expand Up @@ -174,6 +176,7 @@ def __init__(self) -> None:
self._enable_address_index: bool = False
self._enable_tokens_index: bool = False
self._enable_utxo_index: bool = False
self._enable_nc_history_index: bool = False

self._sync_v1_support: SyncSupportLevel = SyncSupportLevel.UNAVAILABLE
self._sync_v2_support: SyncSupportLevel = SyncSupportLevel.UNAVAILABLE
Expand All @@ -189,6 +192,8 @@ def __init__(self) -> None:
self._consensus: ConsensusAlgorithm | None = None
self._p2p_manager: ConnectionsManager | None = None

self._nc_storage_factory: NCStorageFactory | None = None

def build(self) -> BuildArtifacts:
if self.artifacts is not None:
raise ValueError('cannot call build twice')
Expand Down Expand Up @@ -221,6 +226,9 @@ def build(self) -> BuildArtifacts:
cpu_mining_service = self._get_or_create_cpu_mining_service()
vertex_handler = self._get_or_create_vertex_handler()

if settings.ENABLE_NANO_CONTRACTS:
tx_storage.nc_catalog = self._get_nc_catalog()

if self._enable_address_index:
indexes.enable_address_index(pubsub)

Expand All @@ -230,6 +238,9 @@ def build(self) -> BuildArtifacts:
if self._enable_utxo_index:
indexes.enable_utxo_index()

if self._enable_nc_history_index:
indexes.enable_nc_history_index()

kwargs: dict[str, Any] = {}

if self._full_verification is not None:
Expand Down Expand Up @@ -358,15 +369,40 @@ def _get_or_create_execution_manager(self) -> ExecutionManager:

return self._execution_manager

def _get_or_create_nc_storage_factory(self) -> NCStorageFactory:
if self._nc_storage_factory is not None:
return self._nc_storage_factory

if self._storage_type == StorageType.MEMORY:
self._nc_storage_factory = NCMemoryStorageFactory()

elif self._storage_type == StorageType.ROCKSDB:
rocksdb_storage = self._get_or_create_rocksdb_storage()
self._nc_storage_factory = NCRocksDBStorageFactory(rocksdb_storage)

else:
raise NotImplementedError

return self._nc_storage_factory

def _get_or_create_consensus(self) -> ConsensusAlgorithm:
if self._consensus is None:
soft_voided_tx_ids = self._get_soft_voided_tx_ids()
pubsub = self._get_or_create_pubsub()
nc_storage_factory = self._get_or_create_nc_storage_factory()
execution_manager = self._get_or_create_execution_manager()
self._consensus = ConsensusAlgorithm(soft_voided_tx_ids, pubsub, execution_manager=execution_manager)
self._consensus = ConsensusAlgorithm(nc_storage_factory,
soft_voided_tx_ids,
pubsub,
execution_manager=execution_manager)

return self._consensus

def _get_nc_catalog(self) -> NCBlueprintCatalog:
from hathor.nanocontracts.catalog import generate_catalog_from_settings
settings = self._get_or_create_settings()
return generate_catalog_from_settings(settings)

def _get_or_create_pubsub(self) -> PubSubManager:
if self._pubsub is None:
self._pubsub = PubSubManager(self._get_reactor())
Expand Down Expand Up @@ -659,6 +695,11 @@ def enable_utxo_index(self) -> 'Builder':
self._enable_utxo_index = True
return self

def enable_nc_history_index(self) -> 'Builder':
self.check_if_can_modify()
self._enable_nc_history_index = True
return self

def enable_wallet_index(self) -> 'Builder':
self.check_if_can_modify()
self.enable_address_index()
Expand Down
15 changes: 15 additions & 0 deletions hathor/builder/cli_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
from hathor.daa import TestMode
from hathor.event.storage import EventMemoryStorage, EventRocksDBStorage, EventStorage
from hathor.event.websocket.factory import EventWebsocketFactory
from hathor.nanocontracts import NCMemoryStorageFactory, NCRocksDBStorageFactory, NCStorageFactory
from hathor.p2p.netfilter.utils import add_peer_id_blacklist
from hathor.p2p.peer_discovery import BootstrapPeerDiscovery, DNSPeerDiscovery
from hathor.storage import RocksDBStorage
Expand Down Expand Up @@ -121,6 +122,7 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
indexes: IndexesManager
feature_storage: FeatureActivationStorage | None = None
self.rocksdb_storage: Optional[RocksDBStorage] = None
self.nc_storage_factory: Optional[NCStorageFactory] = None
self.event_ws_factory: Optional[EventWebsocketFactory] = None

if self._args.memory_storage:
Expand All @@ -129,6 +131,7 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
indexes = MemoryIndexesManager()
tx_storage = TransactionMemoryStorage(indexes)
event_storage = EventMemoryStorage()
self.nc_storage_factory = NCMemoryStorageFactory()
self.check_or_raise(not self._args.x_rocksdb_indexes, 'RocksDB indexes require RocksDB data')
self.log.info('with storage', storage_class=type(tx_storage).__name__)
else:
Expand All @@ -139,6 +142,8 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
cache_capacity = self._args.rocksdb_cache
self.rocksdb_storage = RocksDBStorage(path=self._args.data, cache_capacity=cache_capacity)

self.nc_storage_factory = NCRocksDBStorageFactory(self.rocksdb_storage)

# Initialize indexes manager.
if self._args.memory_indexes:
indexes = MemoryIndexesManager()
Expand Down Expand Up @@ -166,6 +171,10 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
self.tx_storage = tx_storage
self.log.info('with indexes', indexes_class=type(tx_storage.indexes).__name__)

if settings.ENABLE_NANO_CONTRACTS:
from hathor.nanocontracts.catalog import generate_catalog_from_settings
self.tx_storage.nc_catalog = generate_catalog_from_settings(settings)

self.wallet = None
if self._args.wallet:
self.wallet = self.create_wallet()
Expand Down Expand Up @@ -246,8 +255,14 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
)
full_verification = True

if self._args.nc_history_index and tx_storage.indexes is not None:
self.log.debug('enable nano history index')
tx_storage.indexes.enable_nc_history_index()

assert self.nc_storage_factory is not None
soft_voided_tx_ids = set(settings.SOFT_VOIDED_TX_IDS)
consensus_algorithm = ConsensusAlgorithm(
self.nc_storage_factory,
soft_voided_tx_ids,
pubsub=pubsub,
execution_manager=execution_manager
Expand Down
12 changes: 12 additions & 0 deletions hathor/builder/resources_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,18 @@ def create_resources(self) -> server.Site:
(b'utxo_search', UtxoSearchResource(self.manager), root),
])

if settings.ENABLE_NANO_CONTRACTS:
from hathor.nanocontracts.resources import (
BlueprintInfoResource,
NanoContractHistoryResource,
NanoContractStateResource,
)
nc_resource = Resource()
root.putChild(b'nano_contract', nc_resource)
nc_resource.putChild(b'blueprint', BlueprintInfoResource(self.manager))
nc_resource.putChild(b'history', NanoContractHistoryResource(self.manager))
nc_resource.putChild(b'state', NanoContractStateResource(self.manager))

if self._args.enable_debug_api:
debug_resource = Resource()
root.putChild(b'_debug', debug_resource)
Expand Down
2 changes: 2 additions & 0 deletions hathor/cli/run_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ def create_parser(cls) -> ArgumentParser:
help='Create an index of transactions by address and allow searching queries')
parser.add_argument('--utxo-index', action='store_true',
help='Create an index of UTXOs by token/address/amount and allow searching queries')
parser.add_argument('--nc-history-index', action='store_true',
help='Enable the index of a nano contract tx history')
parser.add_argument('--prometheus', action='store_true', help='Send metric data to Prometheus')
parser.add_argument('--prometheus-prefix', default='',
help='A prefix that will be added in all Prometheus metrics')
Expand Down
1 change: 1 addition & 0 deletions hathor/cli/run_node_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,4 @@ class RunNodeArgs(BaseModel, extra=Extra.allow):
x_ipython_kernel: bool
nano_testnet: bool
log_vertex_bytes: bool
nc_history_index: bool
6 changes: 4 additions & 2 deletions hathor/conf/nano_testnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
P2PKH_VERSION_BYTE=b'\x49',
MULTISIG_VERSION_BYTE=b'\x87',
NETWORK_NAME='nano-testnet-alpha',
BOOTSTRAP_DNS=[],
BOOTSTRAP_DNS=['alpha.nano-testnet.hathor.network'],
# Genesis stuff
GENESIS_OUTPUT_SCRIPT=bytes.fromhex('76a91478e804bf8aa68332c6c1ada274ac598178b972bf88ac'),
GENESIS_BLOCK_TIMESTAMP=1677601898,
Expand All @@ -34,5 +34,7 @@
MIN_TX_WEIGHT=8,
CHECKPOINTS=[],
ENABLE_NANO_CONTRACTS=True,
BLUEPRINTS={},
BLUEPRINTS={
bytes.fromhex('3cb032600bdf7db784800e4ea911b10676fa2f67591f82bb62628c234e771595'): 'Bet',
},
)
5 changes: 4 additions & 1 deletion hathor/conf/nano_testnet.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
P2PKH_VERSION_BYTE: x49
MULTISIG_VERSION_BYTE: x87
NETWORK_NAME: nano-testnet-alpha
BOOTSTRAP_DNS: []
BOOTSTRAP_DNS:
- alpha.nano-testnet.hathor.network

# Genesis stuff
GENESIS_OUTPUT_SCRIPT: 76a91478e804bf8aa68332c6c1ada274ac598178b972bf88ac
Expand All @@ -18,3 +19,5 @@ MIN_TX_WEIGHT_K: 0
MIN_TX_WEIGHT_COEFFICIENT: 0
MIN_TX_WEIGHT: 8
ENABLE_NANO_CONTRACTS: true
BLUEPRINTS:
3cb032600bdf7db784800e4ea911b10676fa2f67591f82bb62628c234e771595: Bet
6 changes: 6 additions & 0 deletions hathor/conf/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,12 @@ def GENESIS_TX2_TIMESTAMP(self) -> int:
# List of enabled blueprints.
BLUEPRINTS: dict[bytes, 'str'] = {}

# Nano Contract: Maximum length when serializing each argument (in bytes)
NC_MAX_LENGTH_SERIALIZED_ARG: int = 1000

# Identifier used in metadata's voided_by when a Nano Contract method fails.
NC_EXECUTION_FAIL_ID: bytes = b'nc-fail'

@classmethod
def from_yaml(cls, *, filepath: str) -> 'HathorSettings':
"""Takes a filepath to a yaml file and returns a validated HathorSettings instance."""
Expand Down
3 changes: 3 additions & 0 deletions hathor/conf/unittests.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@
default_threshold=3
),
ENABLE_NANO_CONTRACTS=True,
BLUEPRINTS={
bytes.fromhex('3cb032600bdf7db784800e4ea911b10676fa2f67591f82bb62628c234e771595'): 'Bet'
},
)
5 changes: 4 additions & 1 deletion hathor/conf/unittests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ GENESIS_TX2_HASH: 33e14cb555a96967841dcbe0f95e9eab5810481d01de8f4f73afb8cce365e8
REWARD_SPEND_MIN_BLOCKS: 10
SLOW_ASSERTS: true
MAX_TX_WEIGHT_DIFF_ACTIVATION: 0.0
ENABLE_NANO_CONTRACTS: true

FEATURE_ACTIVATION:
evaluation_interval: 4
max_signal_bits: 4
default_threshold: 3

ENABLE_NANO_CONTRACTS: true
BLUEPRINTS:
3cb032600bdf7db784800e4ea911b10676fa2f67591f82bb62628c234e771595: Bet
Loading

0 comments on commit 513844a

Please sign in to comment.