Skip to content
This repository has been archived by the owner on Jul 1, 2021. It is now read-only.

Constants arguments clean-up and shuffling lookahead #271

Merged
merged 13 commits into from
Feb 16, 2019

Conversation

hwwhww
Copy link
Contributor

@hwwhww hwwhww commented Feb 12, 2019

What was wrong?

  1. Refactor: clean up constants arguments by adding CommitteeConfig
  2. Fix Update helpers for shuffling lookahead and boundary fixes #258

How was it fixed?

1. Refactor: clean up constants arguments by adding CommitteeConfig

Sorry for that this PR became more about refactoring than just add a new feature.
When I tried to implement #258, I have to revamp get_crosslink_committees_at_slot helper; and with this new feature, I have to pass four more constants into get_crosslink_committees_at_slot and all the function that calls it.
That's a stronger signal of that some 💩 code here that we need to fix...
Per #146 (comment), I mentioned passing the shuffling-required constants into ShufflingConfig, but after some recent changes, it looks better to have a subset of BeaconConfig - CommitteeConfig to mitigate it.

class CommitteeConfig:
    def __init__(self, config: BeaconConfig):
        # Basic
        self.genesis_epoch = config.GENESIS_EPOCH
        self.shard_count = config.SHARD_COUNT
        self.epoch_length = config.EPOCH_LENGTH
        self.target_committee_size = config.TARGET_COMMITTEE_SIZE

        # For seed
        self.seed_lookahead = config.SEED_LOOKAHEAD
        self.entry_exit_delay = config.ENTRY_EXIT_DELAY
        self.latest_index_roots_length = config.LATEST_INDEX_ROOTS_LENGTH
        self.latest_randao_mixes_length = config.LATEST_INDEX_ROOTS_LENGTH
  1. It's initialized with BeaconConfig in StateMachine layer.
  2. Also, BeaconConfig has been moved from eth2/beacon/state_machines/configs.pyeth2/beacon/configs.py.
  3. Pros: less ugly arguments passing.
  4. Cons: lose some awareness of what are the configuration parameters we are testing.

While this PR is WIP, @pipermerriam @jannikluhn @ralexstokes @djrtwo , any thoughts on this refactoring?

2. Shuffling lookahead

Cute Animal Picture

aquarium-ocean-fish-jumping-dolphin-animal-water-906176

eth2/beacon/configs.py Outdated Show resolved Hide resolved
eth2/beacon/committee_helpers.py Outdated Show resolved Hide resolved
Copy link
Contributor

@djrtwo djrtwo left a comment

Choose a reason for hiding this comment

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

CommitteeConfig seems like a reasonable compromise. That said I'm not fully satisfied :/


epoch = slot_to_epoch(slot, epoch_length)
current_epoch = state.current_epoch(epoch_length)
previous_epoch = state.previous_epoch(epoch_length, genesis_epoch)
Copy link
Contributor

Choose a reason for hiding this comment

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

BeaconState knows the genesis_time and thus the genesis_slot/epoch. I would argue that because this is an method on the state, that we can forgo the genesis_epoch param here for cleanliness.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Isn't genesis_time the unix timestamp when chain starts, while genesis_slot and (genesis_epoch) is the offset pivot to slot? Or do you mean, we can find a way to set genesis_slot as a configuration inside BeaconState, not as SSZ field?
On the other hand, it seems it can be solved by ethereum/consensus-specs#504.

Copy link
Contributor

Choose a reason for hiding this comment

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

ahh. I see. Leave as is for now. This might change when we finish the great integer debate ethereum/consensus-specs#626


epoch = slot_to_epoch(slot, epoch_length)
current_epoch = state.current_epoch(epoch_length)
previous_epoch = state.previous_epoch(epoch_length, genesis_epoch)
next_epoch = EpochNumber(current_epoch + 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

suggest adding BeaconState.next_epoch for symmetry

@@ -258,3 +265,46 @@ def create_mock_signed_attestations_at_slot(
keymap,
config.EPOCH_LENGTH,
)


def get_next_epoch_committee_assignment(
Copy link
Contributor

Choose a reason for hiding this comment

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

I simplified the logic of get_next_epoch_committee_assignment in the latest spec release by passing in registry_change rather than returning both values. Looks like this function is still using the outer [False, True] loop but just returning one assignment.

https://github.com/ethereum/eth2.0-specs/blob/master/specs/validator/0_beacon-chain-validator.md#responsibility-lookahead

@@ -111,9 +111,11 @@ def _settle_penality_to_validator_and_whistleblower(
state.validator_balances[index] -= whistleblower_reward
validator.penalized_epoch = slot_to_epoch(state.slot)
"""
EPOCH_LENGTH = committee_config.EPOCH_LENGTH
Copy link
Contributor

Choose a reason for hiding this comment

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

capital letters here is not consistent with config assignments in other functions (example: get_crosslink_committees_at_slot)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I prefer to use the lower letter for consistency too.

I remember that @ChihChengLiang introduced capital letters in some other functions, and now there are two different style in the codebase. Can we unify it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Will fix them to lower cases in my existing PRs

"""
Penalize the validator with the given ``index``.

Exit the validator, penalize the validator, and reward the whistleblower.
"""
state = exit_validator(state, index, epoch_length, entry_exit_delay)
EPOCH_LENGTH = committee_config.EPOCH_LENGTH
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above. Either way is fine imo, but we should be consistent

Copy link
Member

@pipermerriam pipermerriam left a comment

Choose a reason for hiding this comment

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

Mostly just browsed code. Will leave it to others to verify correctness.

state.latest_index_roots,
genesis_epoch % latest_index_roots_length,
index_root,
latest_index_roots = tuple(
Copy link
Member

Choose a reason for hiding this comment

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

equivalent to either of the following if you like them more

# using itertools.repeat
tuple(itertools.repeat(genesis_active_index_root, latest_index_roots_length))

# using tuple multiplication
(genesis_active_index_root,) * latest_index_roots_length

@hwwhww hwwhww requested a review from djrtwo February 15, 2019 17:06
@hwwhww hwwhww merged commit 06c4d1b into ethereum:master Feb 16, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update helpers for shuffling lookahead and boundary fixes
5 participants