This repository has been archived by the owner on Jul 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 145
Add arg to take Unix integer as genesis time #686
Merged
ChihChengLiang
merged 4 commits into
ethereum:master
from
ChihChengLiang:set-genesis-time
Jun 4, 2019
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,10 +11,14 @@ | |
import time | ||
from typing import ( | ||
Any, | ||
Callable, | ||
Dict, | ||
Tuple, | ||
) | ||
|
||
from eth_utils import ( | ||
humanize_seconds, | ||
) | ||
from ruamel.yaml import ( | ||
YAML, | ||
) | ||
|
@@ -76,6 +80,18 @@ def __init__(self, name: str, root_dir: Path) -> None: | |
self.validator_keys_dir = self.client_dir / VALIDATOR_KEY_DIR | ||
|
||
|
||
def get_genesis_time_from_constant(genesis_time: Timestamp) -> Callable[[], Timestamp]: | ||
def get_genesis_time() -> Timestamp: | ||
return genesis_time | ||
return get_genesis_time | ||
|
||
|
||
def get_genesis_time_from_delay(genesis_delay: Second)-> Callable[[], Timestamp]: | ||
def get_genesis_time() -> Timestamp: | ||
return Timestamp(int(time.time()) + genesis_delay) | ||
return get_genesis_time | ||
|
||
|
||
class NetworkGeneratorPlugin(BaseMainProcessPlugin): | ||
@property | ||
def name(self) -> str: | ||
|
@@ -100,11 +116,19 @@ def configure_parser(cls, arg_parser: ArgumentParser, subparser: _SubParsersActi | |
type=int, | ||
default=100, | ||
) | ||
testnet_generator_parser.add_argument( | ||
|
||
genesis_time_group = testnet_generator_parser.add_mutually_exclusive_group( | ||
required=True, | ||
) | ||
genesis_time_group.add_argument( | ||
"--genesis-delay", | ||
help="Seconds before genesis time from now", | ||
help="Set seconds delay after the genesis state is created as genesis time", | ||
type=int, | ||
) | ||
genesis_time_group.add_argument( | ||
"--genesis-time", | ||
help="Set a genesis time as Unix int, e.g. 1559292765", | ||
type=int, | ||
default=60, | ||
) | ||
|
||
testnet_generator_parser.set_defaults(func=cls.run_generate_testnet_dir) | ||
|
@@ -115,12 +139,19 @@ def run_generate_testnet_dir(cls, args: Namespace, trinity_config: TrinityConfig | |
logger.info("Generating testnet") | ||
network_dir = args.network_dir | ||
if len(os.listdir(network_dir)) > 0: | ||
logger.error("This directory is not empty, won't create network files here.") | ||
logger.error("This directory is not empty, won't create network files here") | ||
sys.exit(1) | ||
|
||
clients = cls.generate_trinity_root_dirs(network_dir) | ||
keymap = cls.generate_keys(args.num, network_dir, clients) | ||
cls.generate_genesis_state(args.genesis_delay, network_dir, keymap, clients) | ||
|
||
get_genesis_time = ( | ||
get_genesis_time_from_constant(args.genesis_time) | ||
if args.genesis_time is not None | ||
else get_genesis_time_from_delay(args.genesis_delay) | ||
) | ||
ChihChengLiang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
cls.generate_genesis_state(get_genesis_time, network_dir, keymap, clients) | ||
|
||
logger.info(bold_green("Network generation completed")) | ||
|
||
|
@@ -130,7 +161,7 @@ def generate_keys(cls, | |
network_dir: Path, | ||
clients: Tuple[Client, ...]) -> Dict[Any, Any]: | ||
logger = cls.get_logger() | ||
logger.info(f"Creating {num} validators' keys") | ||
logger.info("Creating %s validators' keys", num) | ||
keys_dir = network_dir / KEYS_DIR | ||
ChihChengLiang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
keys_dir.mkdir() | ||
|
||
|
@@ -156,7 +187,7 @@ def generate_keys(cls, | |
|
||
@classmethod | ||
def generate_genesis_state(cls, | ||
genesis_delay: Second, | ||
get_genesis_time: Callable[[], Timestamp], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reviewing this a second time I'm a little confused by this API. Can you explain why we don't/can't just pass in a firm timestamp? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are two possibilities for An alternative could be def generate_genesis_state(..., genesis_time, genesis_delay=None):
state, _ = create_mock_genesis(
...
genesis_time=genesis_time if genesis_time is not None else 0,
)
if genesis_delay is not None:
state = state.copy(genesis_time=Timestamp(int(time.time()) + genesis_delay))
... |
||
network_dir: Path, | ||
keymap: Dict[Any, Any], | ||
clients: Tuple[Client, ...]) -> None: | ||
|
@@ -172,8 +203,11 @@ def generate_genesis_state(cls, | |
genesis_block_class=state_machine_class.block_class, | ||
genesis_time=dummy_time, | ||
) | ||
logger.info(f"Genesis time will be {genesis_delay} seconds from now") | ||
genesis_time = Timestamp(int(time.time()) + genesis_delay) | ||
genesis_time = get_genesis_time() | ||
logger.info( | ||
ChihChengLiang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"Genesis time will be %s from now", | ||
humanize_seconds(genesis_time - int(time.time())), | ||
) | ||
state = state.copy( | ||
genesis_time=genesis_time, | ||
) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could probably simplify this by changing this to simply compute what the equivalent would be in
--genesis-time
and to have it populate that value ontogenesis_time
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, the description "Seconds before genesis time from now" should be changed to "Set seconds delay after the genesis state is created as genesis time".
Since the usage of
--genesis-delay
option is for debugging, and the genesis state takes a long time to create, the "delay" actually starts after the latter is done. So for example when debugging ineth2/beacon/scripts/run_beacon_nodes.py
, the choice of delay can be independent of the time to create the genesis state.