Skip to content
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

Update deterministic subnets upgrade to allow prefix computation #4959

Merged

Conversation

divagant-martian
Copy link
Collaborator

@divagant-martian divagant-martian commented Nov 28, 2023

Issue Addressed

Implements ethereum/consensus-specs#3545 in preparation for turning on deterministic subnet peers discovery

Proposed Changes

Add the spec values necessary to implement the change so that peer discovery can be tackled afterwards

Additional Info

found the 🐛 and now all the test values are verified with pyspec so I'm certain calculations are correct

The compute_subscribed_subnet function required two changes to work:

  1. proper types for subnet_prefix and shuffling_bits
  2. using parenthesis to specify the correct order of operations in python for the later (otherwise the result might not even fit in a uint64).

For the record this is the code:

def compute_subscribed_subnet(node_id: NodeID, epoch: Epoch, index: int) -> SubnetID:
    subnet_prefix = uint64(node_id >> (NODE_ID_BITS - config.ATTESTATION_SUBNET_PREFIX_BITS))
    shuffling_bits = uint64(node_id >> (NODE_ID_BITS - config.ATTESTATION_SUBNET_PREFIX_BITS - ATTESTATION_SUBNET_SHUFFLING_PREFIX_BITS) )% (1 << ATTESTATION_SUBNET_SHUFFLING_PREFIX_BITS)
    epoch_transition = (subnet_prefix +  (shuffling_bits * (config.EPOCHS_PER_SUBNET_SUBSCRIPTION >> ATTESTATION_SUBNET_SHUFFLING_PREFIX_BITS))) % config.EPOCHS_PER_SUBNET_SUBSCRIPTION
    permutation_seed = hash(uint_to_bytes(uint64((epoch + epoch_transition) // config.EPOCHS_PER_SUBNET_SUBSCRIPTION)))
    permutated_prefix = compute_shuffled_index(
        subnet_prefix,
        1 << config.ATTESTATION_SUBNET_PREFIX_BITS,
        permutation_seed,
    )
    return SubnetID((permutated_prefix + index) % config.ATTESTATION_SUBNET_COUNT)

@divagant-martian divagant-martian changed the base branch from stable to unstable November 28, 2023 23:14
@divagant-martian
Copy link
Collaborator Author

Tagging @michaelsproul since you have good eye for these things

@jimmygchen jimmygchen added the ready-for-review The code is ready for review label Nov 29, 2023
Copy link
Member

@AgeManning AgeManning left a comment

Choose a reason for hiding this comment

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

These changes look fine to me.

The conversion to u64's I think are fine, but if someone wants to double check it can't hurt us that might be one area to look at.

There is edge cases for panics, but only on wild configuration settings, so i'm cool with that.

@divagant-martian
Copy link
Collaborator Author

divagant-martian commented Nov 30, 2023

Yes, I am assuming that the number of bits we take for subnet prefix + shuffling will never reach 64, since this would require us to store a mapping for 1.8×10¹⁹ (2^64) node id prefixes -> subnet, per epoch of interest (probably at least the current and next one).

Note that the previous code has similar assumptions, since node_id_prefix had to fit in a usize so we assume that the prefix bits are also less than 64.

I don't think these assumptions are outrageous, since they are controlled by the spec and anyone changing them to crazy values can't cause harm to other nodes but their own.

But ofc happy to take suggestions from @michaelsproul when he gets the time

Copy link
Collaborator

@dapplion dapplion left a comment

Choose a reason for hiding this comment

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

Merged unstable to fix some conflicts with Dencun variables.

Logic looks good and matches ethereum/consensus-specs#3545

Should add a check at the start compute_subnets_for_epoch to check that prefix_bits + shuffling_prefix_bits < 64?

@AgeManning
Copy link
Member

I think we get this one in. @ackintosh has another that is dependent on this one also.

@AgeManning AgeManning merged commit 8353ec9 into sigp:unstable Jan 31, 2024
28 checks passed
@michaelsproul
Copy link
Member

michaelsproul commented Jan 31, 2024

@AgeManning gentle reminder to use mergify next time 🙏

@Mergifyio queue is the new bors r+

danielramirezch pushed a commit to danielramirezch/lighthouse that referenced this pull request Feb 14, 2024
…p#4959)

* add new spec field to spec

* add spec changes

* fix bug and update expected subnets with validation from pyspec

* fix values and make test prettier (?)

* fix style

---------

Co-authored-by: Lion - dapplion <35266934+dapplion@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Networking ready-for-review The code is ready for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants