Skip to content

feat(cli): Add feature flag and CLI parameters for Nano Contracts #941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 9, 2024
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
6 changes: 6 additions & 0 deletions hathor/builder/cli_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
reactor_type=type(reactor).__name__,
)

# XXX Remove this protection after Nano Contracts are launched.
if settings.NETWORK_NAME not in {'nano-testnet-alpha', 'unittests'}:
# Add protection to prevent enabling Nano Contracts due to misconfigurations.
self.check_or_raise(not settings.ENABLE_NANO_CONTRACTS,
'configuration error: NanoContracts can only be enabled on localnets for now')

tx_storage: TransactionStorage
event_storage: EventStorage
indexes: IndexesManager
Expand Down
10 changes: 8 additions & 2 deletions hathor/cli/run_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ def create_parser(cls) -> ArgumentParser:
parser.add_argument('--auto-hostname', action='store_true', help='Try to discover the hostname automatically')
parser.add_argument('--unsafe-mode',
help='Enable unsafe parameters. **NEVER USE IT IN PRODUCTION ENVIRONMENT**')
parser.add_argument('--testnet', action='store_true', help='Connect to Hathor testnet')

netargs = parser.add_mutually_exclusive_group()
netargs.add_argument('--nano-testnet', action='store_true', help='Connect to Hathor nano-testnet')
netargs.add_argument('--testnet', action='store_true', help='Connect to Hathor testnet')

parser.add_argument('--test-mode-tx-weight', action='store_true',
help='Reduces tx weight to 1 for testing purposes')
parser.add_argument('--dns', action='append', help='Seed DNS')
Expand Down Expand Up @@ -355,7 +359,7 @@ def check_python_version(self) -> None:

def __init__(self, *, argv=None):
from hathor.cli.run_node_args import RunNodeArgs
from hathor.conf import TESTNET_SETTINGS_FILEPATH
from hathor.conf import NANO_TESTNET_SETTINGS_FILEPATH, TESTNET_SETTINGS_FILEPATH
from hathor.conf.get_settings import get_global_settings
self.log = logger.new()

Expand All @@ -372,6 +376,8 @@ def __init__(self, *, argv=None):
os.environ['HATHOR_CONFIG_YAML'] = self._args.config_yaml
elif self._args.testnet:
os.environ['HATHOR_CONFIG_YAML'] = TESTNET_SETTINGS_FILEPATH
elif self._args.nano_testnet:
os.environ['HATHOR_CONFIG_YAML'] = NANO_TESTNET_SETTINGS_FILEPATH

try:
get_global_settings()
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 @@ -76,3 +76,4 @@ class RunNodeArgs(BaseModel, extra=Extra.allow):
signal_not_support: set[Feature]
x_asyncio_reactor: bool
x_ipython_kernel: bool
nano_testnet: bool
2 changes: 2 additions & 0 deletions hathor/conf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@

MAINNET_SETTINGS_FILEPATH = str(parent_dir / 'mainnet.yml')
TESTNET_SETTINGS_FILEPATH = str(parent_dir / 'testnet.yml')
NANO_TESTNET_SETTINGS_FILEPATH = str(parent_dir / 'nano_testnet.yml')
UNITTESTS_SETTINGS_FILEPATH = str(parent_dir / 'unittests.yml')

__all__ = [
'MAINNET_SETTINGS_FILEPATH',
'TESTNET_SETTINGS_FILEPATH',
'NANO_TESTNET_SETTINGS_FILEPATH',
'UNITTESTS_SETTINGS_FILEPATH',
'HathorSettings',
]
38 changes: 38 additions & 0 deletions hathor/conf/nano_testnet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2022 Hathor Labs
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from hathor.conf.settings import HathorSettings

SETTINGS = HathorSettings(
P2PKH_VERSION_BYTE=b'\x49',
MULTISIG_VERSION_BYTE=b'\x87',
NETWORK_NAME='nano-testnet-alpha',
BOOTSTRAP_DNS=[],
# Genesis stuff
GENESIS_OUTPUT_SCRIPT=bytes.fromhex('76a91478e804bf8aa68332c6c1ada274ac598178b972bf88ac'),
GENESIS_BLOCK_TIMESTAMP=1677601898,
GENESIS_BLOCK_NONCE=7881594,
GENESIS_BLOCK_HASH=bytes.fromhex('000003472f6a17c2199e24c481a4326c217d07376acd9598651f8413c008554d'),
GENESIS_TX1_NONCE=110,
GENESIS_TX1_HASH=bytes.fromhex('0008f0e9dbe6e4bbc3a85fce7494fee70011b9c7e72f5276daa2a235355ac013'),
GENESIS_TX2_NONCE=180,
GENESIS_TX2_HASH=bytes.fromhex('008d81d9d58a43fd9649f33483d804a4417247b4d4e4e01d64406c4177fee0c2'),
# tx weight parameters. With these settings, tx weight is always 8
MIN_TX_WEIGHT_K=0,
MIN_TX_WEIGHT_COEFFICIENT=0,
MIN_TX_WEIGHT=8,
CHECKPOINTS=[],
ENABLE_NANO_CONTRACTS=True,
BLUEPRINTS={},
)
20 changes: 20 additions & 0 deletions hathor/conf/nano_testnet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
P2PKH_VERSION_BYTE: x49
MULTISIG_VERSION_BYTE: x87
NETWORK_NAME: nano-testnet-alpha
BOOTSTRAP_DNS: []

# Genesis stuff
GENESIS_OUTPUT_SCRIPT: 76a91478e804bf8aa68332c6c1ada274ac598178b972bf88ac
GENESIS_BLOCK_TIMESTAMP: 1677601898
GENESIS_BLOCK_NONCE: 7881594
GENESIS_BLOCK_HASH: 000003472f6a17c2199e24c481a4326c217d07376acd9598651f8413c008554d
GENESIS_TX1_NONCE: 110
GENESIS_TX1_HASH: 0008f0e9dbe6e4bbc3a85fce7494fee70011b9c7e72f5276daa2a235355ac013
GENESIS_TX2_NONCE: 180
GENESIS_TX2_HASH: 008d81d9d58a43fd9649f33483d804a4417247b4d4e4e01d64406c4177fee0c2

# tx weight parameters. With these settings tx weight is always 8
MIN_TX_WEIGHT_K: 0
MIN_TX_WEIGHT_COEFFICIENT: 0
MIN_TX_WEIGHT: 8
ENABLE_NANO_CONTRACTS: true
26 changes: 25 additions & 1 deletion hathor/conf/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,15 @@ def GENESIS_TX2_TIMESTAMP(self) -> int:
OLD_MAX_MERKLE_PATH_LENGTH: int = 12
NEW_MAX_MERKLE_PATH_LENGTH: int = 20

# Used to enable nano contracts.
#
# This should NEVER be enabled for mainnet and testnet, since both networks will
# activate Nano Contracts through the Feature Activation.
ENABLE_NANO_CONTRACTS: bool = False

# List of enabled blueprints.
BLUEPRINTS: dict[bytes, 'str'] = {}

@classmethod
def from_yaml(cls, *, filepath: str) -> 'HathorSettings':
"""Takes a filepath to a yaml file and returns a validated HathorSettings instance."""
Expand All @@ -449,6 +458,17 @@ def _parse_checkpoints(checkpoints: Union[dict[int, str], list[Checkpoint]]) ->
return checkpoints


def _parse_blueprints(blueprints_raw: dict[str, str]) -> dict[bytes, str]:
"""Parse dict[str, str] into dict[bytes, str]."""
blueprints: dict[bytes, str] = {}
for _id_str, _name in blueprints_raw.items():
_id = bytes.fromhex(_id_str)
if _id in blueprints:
raise TypeError(f'Duplicate blueprint id: {_id_str}')
blueprints[_id] = _name
return blueprints


def _parse_hex_str(hex_str: Union[str, bytes]) -> bytes:
"""Parse a raw hex string into bytes."""
if isinstance(hex_str, str):
Expand Down Expand Up @@ -480,5 +500,9 @@ def _parse_hex_str(hex_str: Union[str, bytes]) -> bytes:
_parse_checkpoints=pydantic.validator(
'CHECKPOINTS',
pre=True
)(_parse_checkpoints)
)(_parse_checkpoints),
_parse_blueprints=pydantic.validator(
'BLUEPRINTS',
pre=True
)(_parse_blueprints)
)
3 changes: 2 additions & 1 deletion hathor/conf/unittests.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@
evaluation_interval=4,
max_signal_bits=4,
default_threshold=3
)
),
ENABLE_NANO_CONTRACTS=True,
)
1 change: 1 addition & 0 deletions hathor/conf/unittests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ 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
Expand Down
12 changes: 11 additions & 1 deletion tests/others/test_hathor_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@
from pydantic import ValidationError

from hathor.checkpoint import Checkpoint
from hathor.conf import MAINNET_SETTINGS_FILEPATH, TESTNET_SETTINGS_FILEPATH, UNITTESTS_SETTINGS_FILEPATH
from hathor.conf import (
MAINNET_SETTINGS_FILEPATH,
NANO_TESTNET_SETTINGS_FILEPATH,
TESTNET_SETTINGS_FILEPATH,
UNITTESTS_SETTINGS_FILEPATH,
)
from hathor.conf.mainnet import SETTINGS as MAINNET_SETTINGS
from hathor.conf.nano_testnet import SETTINGS as NANO_TESTNET_SETTINGS
from hathor.conf.settings import HathorSettings
from hathor.conf.testnet import SETTINGS as TESTNET_SETTINGS
from hathor.conf.unittests import SETTINGS as UNITTESTS_SETTINGS
Expand Down Expand Up @@ -115,3 +121,7 @@ def test_testnet_settings_migration():

def test_unittests_settings_migration():
assert UNITTESTS_SETTINGS == HathorSettings.from_yaml(filepath=UNITTESTS_SETTINGS_FILEPATH)


def test_nano_testnet_settings_migration():
assert NANO_TESTNET_SETTINGS == HathorSettings.from_yaml(filepath=NANO_TESTNET_SETTINGS_FILEPATH)