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
4 changes: 3 additions & 1 deletion extras/custom_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function check_do_not_use_builtin_random_in_tests() {
hathor/merged_mining/debug_api.py
hathor/client.py
hathor/cli/tx_generator.py
tests/test_utils/test_leb128.py
)
exclude_params=()
for item in "${exclude[@]}"; do
Expand All @@ -81,9 +82,10 @@ function check_deprecated_typing() {
}

function check_do_not_import_tests_in_hathor() {
if grep -R '\<.*import .*tests.*\>\|\<.*from .*tests.* import\>' "hathor"; then
if grep -R '\<.*import .*tests.*\>\|\<.*from .*tests.* import\>' "hathor" | grep -v '# skip-import-tests-custom-check'; then
echo 'do not import test definitions in the hathor module'
echo 'move them from tests to hathor instead'
echo 'alternatively, comment `# skip-import-tests-custom-check` to exclude a line.'
return 1
fi
return 0
Expand Down
81 changes: 79 additions & 2 deletions hathor/builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
from hathor.indexes import IndexesManager, RocksDBIndexesManager
from hathor.manager import HathorManager
from hathor.mining.cpu_mining_service import CpuMiningService
from hathor.nanocontracts import NCRocksDBStorageFactory, NCStorageFactory
from hathor.nanocontracts.catalog import NCBlueprintCatalog
from hathor.nanocontracts.nc_exec_logs import NCLogConfig, NCLogStorage
from hathor.nanocontracts.sorter.types import NCSorterCallable
from hathor.p2p.manager import ConnectionsManager
from hathor.p2p.peer import PrivatePeer
from hathor.pubsub import PubSubManager
Expand Down Expand Up @@ -164,6 +168,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_indices: bool = False

self._sync_v2_support: SyncSupportLevel = SyncSupportLevel.ENABLED

Expand All @@ -182,6 +187,12 @@ def __init__(self) -> None:
self._enable_ipv6: bool = False
self._disable_ipv4: bool = False

self._nc_anti_mev: bool = False

self._nc_storage_factory: NCStorageFactory | None = None
self._nc_log_storage: NCLogStorage | None = None
self._nc_log_config: NCLogConfig = NCLogConfig.NONE

def build(self) -> BuildArtifacts:
if self.artifacts is not None:
raise ValueError('cannot call build twice')
Expand Down Expand Up @@ -214,6 +225,9 @@ def build(self) -> BuildArtifacts:
vertex_parser = self._get_or_create_vertex_parser()
poa_block_producer = self._get_or_create_poa_block_producer()

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 @@ -223,6 +237,9 @@ def build(self) -> BuildArtifacts:
if self._enable_utxo_index:
indexes.enable_utxo_index()

if self._enable_nc_indices:
indexes.enable_nc_indices()

kwargs: dict[str, Any] = {}

if self._enable_event_queue is not None:
Expand Down Expand Up @@ -276,7 +293,7 @@ def build(self) -> BuildArtifacts:
rocksdb_storage=rocksdb_storage,
stratum_factory=stratum_factory,
feature_service=feature_service,
bit_signaling_service=bit_signaling_service
bit_signaling_service=bit_signaling_service,
)

return self.artifacts
Expand Down Expand Up @@ -351,6 +368,34 @@ 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

rocksdb_storage = self._get_or_create_rocksdb_storage()
self._nc_storage_factory = NCRocksDBStorageFactory(rocksdb_storage)
return self._nc_storage_factory

def _get_nc_calls_sorter(self) -> NCSorterCallable:
if self._nc_anti_mev:
from hathor.nanocontracts.sorter.random_sorter import random_nc_calls_sorter
return random_nc_calls_sorter
else:
from hathor.nanocontracts.sorter.timestamp_sorter import timestamp_nc_calls_sorter
return timestamp_nc_calls_sorter

def _get_or_create_nc_log_storage(self) -> NCLogStorage:
if self._nc_log_storage is not None:
return self._nc_log_storage

rocksdb_storage = self._get_or_create_rocksdb_storage()
self._nc_log_storage = NCLogStorage(
settings=self._get_or_create_settings(),
path=rocksdb_storage.path,
config=self._nc_log_config,
)
return self._nc_log_storage

def _get_or_create_consensus(self) -> ConsensusAlgorithm:
if self._consensus is None:
soft_voided_tx_ids = self._get_soft_voided_tx_ids()
Expand All @@ -359,6 +404,11 @@ def _get_or_create_consensus(self) -> ConsensusAlgorithm:

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 @@ -429,12 +479,14 @@ def _get_or_create_tx_storage(self) -> TransactionStorage:
store_indexes = None

rocksdb_storage = self._get_or_create_rocksdb_storage()
nc_storage_factory = self._get_or_create_nc_storage_factory()
vertex_parser = self._get_or_create_vertex_parser()
self._tx_storage = TransactionRocksDBStorage(
rocksdb_storage,
indexes=store_indexes,
settings=settings,
vertex_parser=vertex_parser,
nc_storage_factory=nc_storage_factory,
)

if self._tx_storage_cache:
Expand All @@ -443,7 +495,12 @@ def _get_or_create_tx_storage(self) -> TransactionStorage:
if self._tx_storage_cache_capacity is not None:
kwargs['capacity'] = self._tx_storage_cache_capacity
self._tx_storage = TransactionCacheStorage(
self._tx_storage, reactor, indexes=indexes, settings=settings, **kwargs
self._tx_storage,
reactor,
indexes=indexes,
settings=settings,
nc_storage_factory=nc_storage_factory,
**kwargs
)

return self._tx_storage
Expand Down Expand Up @@ -658,6 +715,11 @@ def enable_utxo_index(self) -> 'Builder':
self._enable_utxo_index = True
return self

def enable_nc_indices(self) -> 'Builder':
self.check_if_can_modify()
self._enable_nc_indices = True
return self

def enable_wallet_index(self) -> 'Builder':
if self._tx_storage or self._indexes_manager:
raise ValueError('cannot enable index after tx storage or indexes manager is set')
Expand Down Expand Up @@ -744,6 +806,16 @@ def disable_ipv4(self) -> 'Builder':
self._disable_ipv4 = True
return self

def enable_nc_anti_mev(self) -> 'Builder':
self.check_if_can_modify()
self._nc_anti_mev = True
return self

def disable_nc_anti_mev(self) -> 'Builder':
self.check_if_can_modify()
self._nc_anti_mev = False
return self

def set_soft_voided_tx_ids(self, soft_voided_tx_ids: set[bytes]) -> 'Builder':
self.check_if_can_modify()
self._soft_voided_tx_ids = soft_voided_tx_ids
Expand All @@ -769,3 +841,8 @@ def set_poa_signer(self, signer: PoaSigner) -> 'Builder':
self.check_if_can_modify()
self._poa_signer = signer
return self

def set_nc_log_config(self, config: NCLogConfig) -> 'Builder':
self.check_if_can_modify()
self._nc_log_config = config
return self
31 changes: 29 additions & 2 deletions hathor/builder/cli_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
from hathor.daa import TestMode
from hathor.event.storage import EventRocksDBStorage, EventStorage
from hathor.event.websocket.factory import EventWebsocketFactory
from hathor.nanocontracts import 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 @@ -134,6 +135,8 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
if self._args.data else RocksDBStorage.create_temp(cache_capacity)
)

self.nc_storage_factory: NCStorageFactory = NCRocksDBStorageFactory(self.rocksdb_storage)

# Initialize indexes manager.
indexes = RocksDBIndexesManager(self.rocksdb_storage, settings=settings)

Expand All @@ -143,7 +146,11 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
# only TransactionCacheStorage should have indexes.
kwargs['indexes'] = indexes
tx_storage = TransactionRocksDBStorage(
self.rocksdb_storage, settings=settings, vertex_parser=vertex_parser, **kwargs
self.rocksdb_storage,
settings=settings,
vertex_parser=vertex_parser,
nc_storage_factory=self.nc_storage_factory,
**kwargs
)
event_storage = EventRocksDBStorage(self.rocksdb_storage)
feature_storage = FeatureActivationStorage(settings=settings, rocksdb_storage=self.rocksdb_storage)
Expand All @@ -158,7 +165,13 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
self.check_or_raise(self._args.cache_interval is None, 'cannot use --disable-cache with --cache-interval')

if not self._args.disable_cache:
tx_storage = TransactionCacheStorage(tx_storage, reactor, indexes=indexes, settings=settings)
tx_storage = TransactionCacheStorage(
tx_storage,
reactor,
indexes=indexes,
settings=settings,
nc_storage_factory=self.nc_storage_factory,
)
tx_storage.capacity = self._args.cache_size if self._args.cache_size is not None else DEFAULT_CACHE_SIZE
if self._args.cache_interval:
tx_storage.interval = self._args.cache_interval
Expand All @@ -167,6 +180,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 @@ -213,6 +230,16 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
self.log.debug('enable utxo index')
tx_storage.indexes.enable_utxo_index()

self.check_or_raise(
not self._args.nc_history_index,
'--nc-history-index has been deprecated, use --nc-indices instead',
)
Comment on lines +233 to +236
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can just remove this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Postponed thread.

if self._args.nc_indices and tx_storage.indexes is not None:
self.log.debug('enable nano indices')
tx_storage.indexes.enable_nc_indices()

assert self.nc_storage_factory is not None

soft_voided_tx_ids = set(settings.SOFT_VOIDED_TX_IDS)
consensus_algorithm = ConsensusAlgorithm(
soft_voided_tx_ids,
Expand Down
7 changes: 7 additions & 0 deletions hathor/cli/run_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def create_parser(cls) -> ArgumentParser:
"""
from hathor.cli.util import create_parser
from hathor.feature_activation.feature import Feature
from hathor.nanocontracts.nc_exec_logs import NCLogConfig
parser = create_parser(prefix=cls.env_vars_prefix)

parser.add_argument('--hostname', help='Hostname used to be accessed by other peers')
Expand Down Expand Up @@ -115,6 +116,9 @@ 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=SUPPRESS) # moved to --nc-indices
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Postponed thread.

parser.add_argument('--nc-indices', action='store_true',
help='Enable indices related to nano contracts')
Comment on lines +120 to +121
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While "indices" is technically correct, "indexes" is also correct, I'd rather use that since it's what we've been using before, there's also this style guide which opts for indexes too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Postponed thread.

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 Expand Up @@ -165,6 +169,9 @@ def create_parser(cls) -> ArgumentParser:
help='Enables listening on IPv6 interface and connecting to IPv6 peers')
parser.add_argument('--x-disable-ipv4', action='store_true',
help='Disables connecting to IPv4 peers')
possible_nc_exec_logs = [config.value for config in NCLogConfig]
parser.add_argument('--nc-exec-logs', default=NCLogConfig.NONE, choices=possible_nc_exec_logs,
help=f'Enable saving Nano Contracts execution logs. One of {possible_nc_exec_logs}')
return parser

def prepare(self, *, register_resources: bool = True) -> None:
Expand Down
4 changes: 4 additions & 0 deletions hathor/cli/run_node_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from pydantic import Extra

from hathor.feature_activation.feature import Feature # skip-cli-import-custom-check
from hathor.nanocontracts.nc_exec_logs import NCLogConfig # skip-cli-import-custom-check
from hathor.utils.pydantic import BaseModel # skip-cli-import-custom-check


Expand Down Expand Up @@ -87,3 +88,6 @@ class RunNodeArgs(BaseModel, extra=Extra.allow):
x_enable_ipv6: bool
x_disable_ipv4: bool
localnet: bool
nc_history_index: bool
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Postponed thread.

nc_indices: bool
nc_exec_logs: NCLogConfig
13 changes: 11 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'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it going to be alpha or bravo now? It probably doesn't sync with the current alpha so we might as well bump it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Postponed thread.

# Genesis stuff
GENESIS_OUTPUT_SCRIPT=bytes.fromhex('76a91478e804bf8aa68332c6c1ada274ac598178b972bf88ac'),
GENESIS_BLOCK_TIMESTAMP=1677601898,
Expand All @@ -34,5 +34,14 @@
MIN_TX_WEIGHT=8,
CHECKPOINTS=[],
ENABLE_NANO_CONTRACTS=True,
BLUEPRINTS={},
ENABLE_ON_CHAIN_BLUEPRINTS=True,
NC_ON_CHAIN_BLUEPRINT_ALLOWED_ADDRESSES=[
'WWFiNeWAFSmgtjm4ht2MydwS5GY3kMJsEK',
],
BLUEPRINTS={
bytes.fromhex('3cb032600bdf7db784800e4ea911b10676fa2f67591f82bb62628c234e771595'): 'Bet',
},
SOFT_VOIDED_TX_IDS=list(map(bytes.fromhex, [
'0000003dd5802b05f430a1f54304879173550c0944b49d74321bb9125ee727cb',
])),
)
11 changes: 10 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,11 @@ MIN_TX_WEIGHT_K: 0
MIN_TX_WEIGHT_COEFFICIENT: 0
MIN_TX_WEIGHT: 8
ENABLE_NANO_CONTRACTS: true
ENABLE_ON_CHAIN_BLUEPRINTS: true
NC_ON_CHAIN_BLUEPRINT_ALLOWED_ADDRESSES:
- WWFiNeWAFSmgtjm4ht2MydwS5GY3kMJsEK
BLUEPRINTS:
3cb032600bdf7db784800e4ea911b10676fa2f67591f82bb62628c234e771595: Bet

SOFT_VOIDED_TX_IDS:
- 0000003dd5802b05f430a1f54304879173550c0944b49d74321bb9125ee727cb
4 changes: 4 additions & 0 deletions hathor/conf/unittests.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@
default_threshold=3
),
ENABLE_NANO_CONTRACTS=True,
ENABLE_ON_CHAIN_BLUEPRINTS=True,
NC_ON_CHAIN_BLUEPRINT_ALLOWED_ADDRESSES=[
'HFwHrQHUftQ7obLj7xbQjG4ZEwvyVXeyoE',
],
)
16 changes: 15 additions & 1 deletion hathor/conf/unittests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,23 @@ 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
ENABLE_ON_CHAIN_BLUEPRINTS: true

NC_ON_CHAIN_BLUEPRINT_ALLOWED_ADDRESSES:
# keypair wallet:
# - privkey:
# MIH0MF8GCSqGSIb3DQEFDTBSMDEGCSqGSIb3DQEFDDAkBBCIdovnmKjK3KUc61YGgja0AgIIAD
# AMBggqhkiG9w0CCQUAMB0GCWCGSAFlAwQBKgQQl2CJT4I2IUzRNoU9hyOWEwSBkLznN9Nunel+
# kK0FXpk//z0ZAnIyVacfHklCxFGyOj1VSjor0CHzH2Gmblvr+m7lCmRmqSVAwJpplqQYdBUF6s
# R9djHLY6svPY0o//dqQ/xM7QiY2FHlb3JQCTu7DaMflqPcJXlRXAFyoACnmj4/lUJWgrcWalar
# CSI+8rIillg3AU8/2gfoB1BxulVIIG35SQ==
# - password:
# OCBtestPW
- HFwHrQHUftQ7obLj7xbQjG4ZEwvyVXeyoE
Loading