Skip to content

Commit

Permalink
Use separate constant names for post-Electra gindices
Browse files Browse the repository at this point in the history
  • Loading branch information
etan-status committed Jul 9, 2024
1 parent 835576a commit 63433ec
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 51 deletions.
6 changes: 3 additions & 3 deletions pysetup/spec_builders/electra.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def imports(cls, preset_name: str):
@classmethod
def hardcoded_ssz_dep_constants(cls) -> Dict[str, str]:
return {
'FINALIZED_ROOT_GINDEX': 'GeneralizedIndex(169)',
'CURRENT_SYNC_COMMITTEE_GINDEX': 'GeneralizedIndex(86)',
'NEXT_SYNC_COMMITTEE_GINDEX': 'GeneralizedIndex(87)',
'FINALIZED_ROOT_GINDEX_ELECTRA': 'GeneralizedIndex(169)',
'CURRENT_SYNC_COMMITTEE_GINDEX_ELECTRA': 'GeneralizedIndex(86)',
'NEXT_SYNC_COMMITTEE_GINDEX_ELECTRA': 'GeneralizedIndex(87)',
}
6 changes: 3 additions & 3 deletions specs/altair/light-client/full-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def create_light_client_bootstrap(state: BeaconState,
header=block_to_light_client_header(block),
current_sync_committee=state.current_sync_committee,
current_sync_committee_branch=CurrentSyncCommitteeBranch(
compute_merkle_proof(state, CURRENT_SYNC_COMMITTEE_GINDEX)),
compute_merkle_proof(state, current_sync_committee_gindex_at_slot(state.slot))),
)
```

Expand Down Expand Up @@ -124,7 +124,7 @@ def create_light_client_update(state: BeaconState,
if update_attested_period == update_signature_period:
update.next_sync_committee = attested_state.next_sync_committee
update.next_sync_committee_branch = NextSyncCommitteeBranch(
compute_merkle_proof(attested_state, NEXT_SYNC_COMMITTEE_GINDEX))
compute_merkle_proof(attested_state, next_sync_committee_gindex_at_slot(attested_state.slot)))

# Indicate finality whenever possible
if finalized_block is not None:
Expand All @@ -134,7 +134,7 @@ def create_light_client_update(state: BeaconState,
else:
assert attested_state.finalized_checkpoint.root == Bytes32()
update.finality_branch = FinalityBranch(
compute_merkle_proof(attested_state, FINALIZED_ROOT_GINDEX))
compute_merkle_proof(attested_state, finalized_root_gindex_at_slot(attested_state.slot)))

update.sync_aggregate = block.message.body.sync_aggregate
update.signature_slot = block.message.slot
Expand Down
8 changes: 4 additions & 4 deletions specs/electra/light-client/fork.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def upgrade_lc_bootstrap_to_electra(pre: deneb.LightClientBootstrap) -> LightCli
header=upgrade_lc_header_to_electra(pre.header),
current_sync_committee=pre.current_sync_committee,
current_sync_committee_branch=normalize_merkle_branch(
pre.current_sync_committee_branch, CURRENT_SYNC_COMMITTEE_GINDEX),
pre.current_sync_committee_branch, CURRENT_SYNC_COMMITTEE_GINDEX_ELECTRA),
)
```

Expand All @@ -81,10 +81,10 @@ def upgrade_lc_update_to_electra(pre: deneb.LightClientUpdate) -> LightClientUpd
attested_header=upgrade_lc_header_to_electra(pre.attested_header),
next_sync_committee=pre.next_sync_committee,
next_sync_committee_branch=normalize_merkle_branch(
pre.next_sync_committee_branch, NEXT_SYNC_COMMITTEE_GINDEX),
pre.next_sync_committee_branch, NEXT_SYNC_COMMITTEE_GINDEX_ELECTRA),
finalized_header=upgrade_lc_header_to_electra(pre.finalized_header),
finality_branch=normalize_merkle_branch(
pre.finality_branch, FINALIZED_ROOT_GINDEX),
pre.finality_branch, FINALIZED_ROOT_GINDEX_ELECTRA),
sync_aggregate=pre.sync_aggregate,
signature_slot=pre.signature_slot,
)
Expand All @@ -96,7 +96,7 @@ def upgrade_lc_finality_update_to_electra(pre: deneb.LightClientFinalityUpdate)
attested_header=upgrade_lc_header_to_electra(pre.attested_header),
finalized_header=upgrade_lc_header_to_electra(pre.finalized_header),
finality_branch=normalize_merkle_branch(
pre.finality_branch, FINALIZED_ROOT_GINDEX),
pre.finality_branch, FINALIZED_ROOT_GINDEX_ELECTRA),
sync_aggregate=pre.sync_aggregate,
signature_slot=pre.signature_slot,
)
Expand Down
26 changes: 17 additions & 9 deletions specs/electra/light-client/sync-protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,21 @@ Additional documents describes the impact of the upgrade on certain roles:
- [Full node](./full-node.md)
- [Networking](./p2p-interface.md)

## Custom types

| Name | SSZ equivalent | Description |
| - | - | - |
| `FinalityBranch` | `Vector[Bytes32, floorlog2(FINALIZED_ROOT_GINDEX_ELECTRA)]` | Merkle branch of `finalized_checkpoint.root` within `BeaconState` |
| `CurrentSyncCommitteeBranch` | `Vector[Bytes32, floorlog2(CURRENT_SYNC_COMMITTEE_GINDEX_ELECTRA)]` | Merkle branch of `current_sync_committee` within `BeaconState` |
| `NextSyncCommitteeBranch` | `Vector[Bytes32, floorlog2(NEXT_SYNC_COMMITTEE_GINDEX_ELECTRA)]` | Merkle branch of `next_sync_committee` within `BeaconState` |

## Constants

| Name | Value |
| - | - |
| `FINALIZED_ROOT_GINDEX` | `get_generalized_index(BeaconState, 'finalized_checkpoint', 'root')` (= 169) |
| `CURRENT_SYNC_COMMITTEE_GINDEX` | `get_generalized_index(BeaconState, 'current_sync_committee')` (= 86) |
| `NEXT_SYNC_COMMITTEE_GINDEX` | `get_generalized_index(BeaconState, 'next_sync_committee')` (= 87) |
| `FINALIZED_ROOT_GINDEX_ELECTRA` | `get_generalized_index(BeaconState, 'finalized_checkpoint', 'root')` (= 169) |
| `CURRENT_SYNC_COMMITTEE_GINDEX_ELECTRA` | `get_generalized_index(BeaconState, 'current_sync_committee')` (= 86) |
| `NEXT_SYNC_COMMITTEE_GINDEX_ELECTRA` | `get_generalized_index(BeaconState, 'next_sync_committee')` (= 87) |

## Helper functions

Expand All @@ -46,8 +54,8 @@ def finalized_root_gindex_at_slot(slot: Slot) -> GeneralizedIndex:

# [Modified in Electra]
if epoch >= ELECTRA_FORK_EPOCH:
return FINALIZED_ROOT_GINDEX
return GeneralizedIndex(altair.FINALIZED_ROOT_GINDEX)
return FINALIZED_ROOT_GINDEX_ELECTRA
return GeneralizedIndex(FINALIZED_ROOT_GINDEX)
```

### Modified `current_sync_committee_gindex_at_slot`
Expand All @@ -58,8 +66,8 @@ def current_sync_committee_gindex_at_slot(slot: Slot) -> GeneralizedIndex:

# [Modified in Electra]
if epoch >= ELECTRA_FORK_EPOCH:
return CURRENT_SYNC_COMMITTEE_GINDEX
return GeneralizedIndex(altair.CURRENT_SYNC_COMMITTEE_GINDEX)
return CURRENT_SYNC_COMMITTEE_GINDEX_ELECTRA
return GeneralizedIndex(CURRENT_SYNC_COMMITTEE_GINDEX)
```

### Modified `next_sync_committee_gindex_at_slot`
Expand All @@ -70,8 +78,8 @@ def next_sync_committee_gindex_at_slot(slot: Slot) -> GeneralizedIndex:

# [Modified in Electra]
if epoch >= ELECTRA_FORK_EPOCH:
return NEXT_SYNC_COMMITTEE_GINDEX
return GeneralizedIndex(altair.NEXT_SYNC_COMMITTEE_GINDEX)
return NEXT_SYNC_COMMITTEE_GINDEX_ELECTRA
return GeneralizedIndex(NEXT_SYNC_COMMITTEE_GINDEX)
```

### Modified `get_lc_execution_root`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,30 @@
with_light_client,
with_test_suite_name,
)
from eth2spec.test.helpers.light_client import (
latest_current_sync_committee_gindex,
latest_finalized_root_gindex,
latest_next_sync_committee_gindex,
)


@with_test_suite_name("BeaconState")
@with_light_client
@spec_state_test
def test_current_sync_committee_merkle_proof(spec, state):
yield "object", state
current_sync_committee_branch = spec.compute_merkle_proof(state, spec.CURRENT_SYNC_COMMITTEE_GINDEX)
gindex = latest_current_sync_committee_gindex(spec)
branch = spec.compute_merkle_proof(state, gindex)
yield "proof", {
"leaf": "0x" + state.current_sync_committee.hash_tree_root().hex(),
"leaf_index": spec.CURRENT_SYNC_COMMITTEE_GINDEX,
"branch": ['0x' + root.hex() for root in current_sync_committee_branch]
"leaf_index": gindex,
"branch": ['0x' + root.hex() for root in branch]
}
assert spec.is_valid_merkle_branch(
leaf=state.current_sync_committee.hash_tree_root(),
branch=current_sync_committee_branch,
depth=spec.floorlog2(spec.CURRENT_SYNC_COMMITTEE_GINDEX),
index=spec.get_subtree_index(spec.CURRENT_SYNC_COMMITTEE_GINDEX),
branch=branch,
depth=spec.floorlog2(gindex),
index=spec.get_subtree_index(gindex),
root=state.hash_tree_root(),
)

Expand All @@ -30,17 +36,18 @@ def test_current_sync_committee_merkle_proof(spec, state):
@spec_state_test
def test_next_sync_committee_merkle_proof(spec, state):
yield "object", state
next_sync_committee_branch = spec.compute_merkle_proof(state, spec.NEXT_SYNC_COMMITTEE_GINDEX)
gindex = latest_next_sync_committee_gindex(spec)
branch = spec.compute_merkle_proof(state, gindex)
yield "proof", {
"leaf": "0x" + state.next_sync_committee.hash_tree_root().hex(),
"leaf_index": spec.NEXT_SYNC_COMMITTEE_GINDEX,
"branch": ['0x' + root.hex() for root in next_sync_committee_branch]
"leaf_index": gindex,
"branch": ['0x' + root.hex() for root in branch]
}
assert spec.is_valid_merkle_branch(
leaf=state.next_sync_committee.hash_tree_root(),
branch=next_sync_committee_branch,
depth=spec.floorlog2(spec.NEXT_SYNC_COMMITTEE_GINDEX),
index=spec.get_subtree_index(spec.NEXT_SYNC_COMMITTEE_GINDEX),
branch=branch,
depth=spec.floorlog2(gindex),
index=spec.get_subtree_index(gindex),
root=state.hash_tree_root(),
)

Expand All @@ -50,17 +57,18 @@ def test_next_sync_committee_merkle_proof(spec, state):
@spec_state_test
def test_finality_root_merkle_proof(spec, state):
yield "object", state
finality_branch = spec.compute_merkle_proof(state, spec.FINALIZED_ROOT_GINDEX)
gindex = latest_finalized_root_gindex(spec)
branch = spec.compute_merkle_proof(state, gindex)
yield "proof", {
"leaf": "0x" + state.finalized_checkpoint.root.hex(),
"leaf_index": spec.FINALIZED_ROOT_GINDEX,
"branch": ['0x' + root.hex() for root in finality_branch]
"leaf_index": gindex,
"branch": ['0x' + root.hex() for root in branch]
}

assert spec.is_valid_merkle_branch(
leaf=state.finalized_checkpoint.root,
branch=finality_branch,
depth=spec.floorlog2(spec.FINALIZED_ROOT_GINDEX),
index=spec.get_subtree_index(spec.FINALIZED_ROOT_GINDEX),
branch=branch,
depth=spec.floorlog2(gindex),
index=spec.get_subtree_index(gindex),
root=state.hash_tree_root(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ def emit_update(test, spec, state, block, attested_state, attested_block, finali
data = d_spec.create_light_client_update(state, block, attested_state, attested_block, finalized_block)
if not with_next:
data.next_sync_committee = spec.SyncCommittee()
data.next_sync_committee_branch = \
[spec.Bytes32() for _ in range(spec.floorlog2(spec.NEXT_SYNC_COMMITTEE_GINDEX))]
data.next_sync_committee_branch = spec.NextSyncCommitteeBranch()
current_slot = state.slot

upgraded = upgrade_lc_update_to_new_spec(d_spec, test.s_spec, data, phases)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ def test_execution_merkle_proof(spec, state):
block = state_transition_with_full_block(spec, state, True, False)

yield "object", block.message.body
execution_branch = spec.compute_merkle_proof(block.message.body, spec.EXECUTION_PAYLOAD_GINDEX)
gindex = spec.EXECUTION_PAYLOAD_GINDEX
branch = spec.compute_merkle_proof(block.message.body, gindex)
yield "proof", {
"leaf": "0x" + block.message.body.execution_payload.hash_tree_root().hex(),
"leaf_index": spec.EXECUTION_PAYLOAD_GINDEX,
"branch": ['0x' + root.hex() for root in execution_branch]
"leaf_index": gindex,
"branch": ['0x' + root.hex() for root in branch]
}
assert spec.is_valid_merkle_branch(
leaf=block.message.body.execution_payload.hash_tree_root(),
branch=execution_branch,
depth=spec.floorlog2(spec.EXECUTION_PAYLOAD_GINDEX),
index=spec.get_subtree_index(spec.EXECUTION_PAYLOAD_GINDEX),
branch=branch,
depth=spec.floorlog2(gindex),
index=spec.get_subtree_index(gindex),
root=block.message.body.hash_tree_root(),
)
30 changes: 24 additions & 6 deletions tests/core/pyspec/eth2spec/test/helpers/light_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@
from math import floor


def latest_finalized_root_gindex(spec):
if hasattr(spec, 'FINALIZED_ROOT_GINDEX_ELECTRA'):
return spec.FINALIZED_ROOT_GINDEX_ELECTRA
return spec.FINALIZED_ROOT_GINDEX


def latest_current_sync_committee_gindex(spec):
if hasattr(spec, 'CURRENT_SYNC_COMMITTEE_GINDEX_ELECTRA'):
return spec.CURRENT_SYNC_COMMITTEE_GINDEX_ELECTRA
return spec.CURRENT_SYNC_COMMITTEE_GINDEX


def latest_next_sync_committee_gindex(spec):
if hasattr(spec, 'NEXT_SYNC_COMMITTEE_GINDEX_ELECTRA'):
return spec.NEXT_SYNC_COMMITTEE_GINDEX_ELECTRA
return spec.NEXT_SYNC_COMMITTEE_GINDEX


def compute_start_slot_at_sync_committee_period(spec, sync_committee_period):
return spec.compute_start_slot_at_epoch(sync_committee_period * spec.EPOCHS_PER_SYNC_COMMITTEE_PERIOD)

Expand Down Expand Up @@ -71,11 +89,11 @@ def create_update(spec,

if with_next:
update.next_sync_committee = attested_state.next_sync_committee
update.next_sync_committee_branch = spec.compute_merkle_proof(attested_state, spec.NEXT_SYNC_COMMITTEE_GINDEX)
update.next_sync_committee_branch = spec.compute_merkle_proof(attested_state, latest_next_sync_committee_gindex(spec))

if with_finality:
update.finalized_header = spec.block_to_light_client_header(finalized_block)
update.finality_branch = spec.compute_merkle_proof(attested_state, spec.FINALIZED_ROOT_GINDEX)
update.finality_branch = spec.compute_merkle_proof(attested_state, latest_finalized_root_gindex(spec))

update.sync_aggregate, update.signature_slot = get_sync_aggregate(
spec, attested_state, num_participants)
Expand Down Expand Up @@ -141,7 +159,7 @@ def check_lc_bootstrap_equal(spec, new_spec, data, upgraded):
new_spec,
data.current_sync_committee_branch,
upgraded.current_sync_committee_branch,
new_spec.CURRENT_SYNC_COMMITTEE_GINDEX,
latest_current_sync_committee_gindex(new_spec),
)


Expand Down Expand Up @@ -171,15 +189,15 @@ def check_lc_update_equal(spec, new_spec, data, upgraded):
new_spec,
data.next_sync_committee_branch,
upgraded.next_sync_committee_branch,
new_spec.NEXT_SYNC_COMMITTEE_GINDEX,
latest_next_sync_committee_gindex(new_spec),
)
check_lc_header_equal(spec, new_spec, data.finalized_header, upgraded.finalized_header)
check_merkle_branch_equal(
spec,
new_spec,
data.finality_branch,
upgraded.finality_branch,
new_spec.FINALIZED_ROOT_GINDEX,
latest_finalized_root_gindex(new_spec),
)
assert upgraded.sync_aggregate == data.sync_aggregate
assert upgraded.signature_slot == data.signature_slot
Expand Down Expand Up @@ -211,7 +229,7 @@ def check_lc_finality_update_equal(spec, new_spec, data, upgraded):
new_spec,
data.finality_branch,
upgraded.finality_branch,
new_spec.FINALIZED_ROOT_GINDEX,
latest_finalized_root_gindex(new_spec),
)
assert upgraded.sync_aggregate == data.sync_aggregate
assert upgraded.signature_slot == data.signature_slot
Expand Down

0 comments on commit 63433ec

Please sign in to comment.