mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 15:37:56 -05:00
Electra: BeaconState implementation (#13919)
* Electra: Beacon State * Electra: Beacon state fixes from PR 13919 * Add missing tests - part 1 * Split eip_7251_root.go into different files and reuse/share code with historical state summaries root. It's identical! * Add missing tests - part 2 * deposit receipts start index getters and setters (#13947) * adding in getters and setters for deposit receipts start index * adding tests * gaz * Add missing tests - part 3 of 3 Update the electra withdrawal example with a ssz state containing pending partial withdrawals * add tests for beacon-chain/state/state-native/getters_balance_deposits.go * Add electra field to testing/util/block.go execution payload * godoc commentary on public methods * Fix failing test * Add balances index out of bounds check and relevant tests. * Revert switch case electra * Instead of copying spectest data into testdata, use the spectest dependency * Deepsource fixes * Address @rkapka PR feedback * s/MaxPendingPartialsPerWithdrawalSweep/MaxPendingPartialsPerWithdrawalsSweep/ * Use multivalue slice compatible accessors for validator and balance in ActiveBalanceAtIndex * More @rkapka feedback. What a great reviewer! * More tests for branching logic in ExitEpochAndUpdateChurn * fix build --------- Co-authored-by: james-prysm <90280386+james-prysm@users.noreply.github.com>
This commit is contained in:
@@ -87,6 +87,8 @@ func FromForkVersion(cv [fieldparams.VersionLength]byte) (*VersionedUnmarshaler,
|
||||
fork = version.Capella
|
||||
case bytesutil.ToBytes4(cfg.DenebForkVersion):
|
||||
fork = version.Deneb
|
||||
case bytesutil.ToBytes4(cfg.ElectraForkVersion):
|
||||
fork = version.Electra
|
||||
default:
|
||||
return nil, errors.Wrapf(ErrForkNotFound, "version=%#x", cv)
|
||||
}
|
||||
@@ -152,6 +154,16 @@ func (cf *VersionedUnmarshaler) UnmarshalBeaconState(marshaled []byte) (s state.
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to init state trie from state, detected fork=%s", forkName)
|
||||
}
|
||||
case version.Electra:
|
||||
st := ðpb.BeaconStateElectra{}
|
||||
err = st.UnmarshalSSZ(marshaled)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to unmarshal state, detected fork=%s", forkName)
|
||||
}
|
||||
s, err = state_native.InitializeFromProtoUnsafeElectra(st)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to init state trie from state, detected fork=%s", forkName)
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("unable to initialize BeaconState for fork version=%s", forkName)
|
||||
}
|
||||
@@ -200,6 +212,8 @@ func (cf *VersionedUnmarshaler) UnmarshalBeaconBlock(marshaled []byte) (interfac
|
||||
blk = ðpb.SignedBeaconBlockCapella{}
|
||||
case version.Deneb:
|
||||
blk = ðpb.SignedBeaconBlockDeneb{}
|
||||
case version.Electra:
|
||||
blk = ðpb.SignedBeaconBlockElectra{}
|
||||
default:
|
||||
forkName := version.String(cf.Fork)
|
||||
return nil, fmt.Errorf("unable to initialize ReadOnlyBeaconBlock for fork version=%s at slot=%d", forkName, slot)
|
||||
@@ -235,6 +249,8 @@ func (cf *VersionedUnmarshaler) UnmarshalBlindedBeaconBlock(marshaled []byte) (i
|
||||
blk = ðpb.SignedBlindedBeaconBlockCapella{}
|
||||
case version.Deneb:
|
||||
blk = ðpb.SignedBlindedBeaconBlockDeneb{}
|
||||
case version.Electra:
|
||||
blk = ðpb.SignedBlindedBeaconBlockElectra{}
|
||||
default:
|
||||
forkName := version.String(cf.Fork)
|
||||
return nil, fmt.Errorf("unable to initialize ReadOnlyBeaconBlock for fork version=%s at slot=%d", forkName, slot)
|
||||
|
||||
@@ -47,7 +47,7 @@ func TestSlotFromBlock(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestByState(t *testing.T) {
|
||||
undo := util.HackDenebMaxuint(t)
|
||||
undo := util.HackElectraMaxuint(t)
|
||||
defer undo()
|
||||
bc := params.BeaconConfig()
|
||||
altairSlot, err := slots.EpochStart(bc.AltairForkEpoch)
|
||||
@@ -58,6 +58,8 @@ func TestByState(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
denebSlot, err := slots.EpochStart(bc.DenebForkEpoch)
|
||||
require.NoError(t, err)
|
||||
electraSlot, err := slots.EpochStart(bc.ElectraForkEpoch)
|
||||
require.NoError(t, err)
|
||||
cases := []struct {
|
||||
name string
|
||||
version int
|
||||
@@ -94,6 +96,12 @@ func TestByState(t *testing.T) {
|
||||
slot: denebSlot,
|
||||
forkversion: bytesutil.ToBytes4(bc.DenebForkVersion),
|
||||
},
|
||||
{
|
||||
name: "electra",
|
||||
version: version.Electra,
|
||||
slot: electraSlot,
|
||||
forkversion: bytesutil.ToBytes4(bc.ElectraForkVersion),
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
st, err := stateForVersion(c.version)
|
||||
@@ -126,6 +134,8 @@ func stateForVersion(v int) (state.BeaconState, error) {
|
||||
return util.NewBeaconStateCapella()
|
||||
case version.Deneb:
|
||||
return util.NewBeaconStateDeneb()
|
||||
case version.Electra:
|
||||
return util.NewBeaconStateElectra()
|
||||
default:
|
||||
return nil, fmt.Errorf("unrecognized version %d", v)
|
||||
}
|
||||
@@ -133,7 +143,7 @@ func stateForVersion(v int) (state.BeaconState, error) {
|
||||
|
||||
func TestUnmarshalState(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
undo := util.HackDenebMaxuint(t)
|
||||
undo := util.HackElectraMaxuint(t)
|
||||
defer undo()
|
||||
bc := params.BeaconConfig()
|
||||
altairSlot, err := slots.EpochStart(bc.AltairForkEpoch)
|
||||
@@ -144,6 +154,8 @@ func TestUnmarshalState(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
denebSlot, err := slots.EpochStart(bc.DenebForkEpoch)
|
||||
require.NoError(t, err)
|
||||
electraSlot, err := slots.EpochStart(bc.ElectraForkEpoch)
|
||||
require.NoError(t, err)
|
||||
cases := []struct {
|
||||
name string
|
||||
version int
|
||||
@@ -180,6 +192,12 @@ func TestUnmarshalState(t *testing.T) {
|
||||
slot: denebSlot,
|
||||
forkversion: bytesutil.ToBytes4(bc.DenebForkVersion),
|
||||
},
|
||||
{
|
||||
name: "electra",
|
||||
version: version.Electra,
|
||||
slot: electraSlot,
|
||||
forkversion: bytesutil.ToBytes4(bc.ElectraForkVersion),
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
st, err := stateForVersion(c.version)
|
||||
@@ -205,7 +223,7 @@ func TestUnmarshalState(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDetectAndUnmarshalBlock(t *testing.T) {
|
||||
undo := util.HackDenebMaxuint(t)
|
||||
undo := util.HackElectraMaxuint(t)
|
||||
defer undo()
|
||||
altairS, err := slots.EpochStart(params.BeaconConfig().AltairForkEpoch)
|
||||
require.NoError(t, err)
|
||||
@@ -215,6 +233,8 @@ func TestDetectAndUnmarshalBlock(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
denebS, err := slots.EpochStart(params.BeaconConfig().DenebForkEpoch)
|
||||
require.NoError(t, err)
|
||||
electraS, err := slots.EpochStart(params.BeaconConfig().ElectraForkEpoch)
|
||||
require.NoError(t, err)
|
||||
cases := []struct {
|
||||
b func(*testing.T, primitives.Slot) interfaces.ReadOnlySignedBeaconBlock
|
||||
name string
|
||||
@@ -260,6 +280,11 @@ func TestDetectAndUnmarshalBlock(t *testing.T) {
|
||||
b: signedTestBlockDeneb,
|
||||
slot: denebS,
|
||||
},
|
||||
{
|
||||
name: "first slot of electra",
|
||||
b: signedTestBlockElectra,
|
||||
slot: electraS,
|
||||
},
|
||||
{
|
||||
name: "bellatrix block in altair slot",
|
||||
b: signedTestBlockBellatrix,
|
||||
@@ -296,13 +321,14 @@ func TestDetectAndUnmarshalBlock(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUnmarshalBlock(t *testing.T) {
|
||||
undo := util.HackDenebMaxuint(t)
|
||||
undo := util.HackElectraMaxuint(t)
|
||||
defer undo()
|
||||
genv := bytesutil.ToBytes4(params.BeaconConfig().GenesisForkVersion)
|
||||
altairv := bytesutil.ToBytes4(params.BeaconConfig().AltairForkVersion)
|
||||
bellav := bytesutil.ToBytes4(params.BeaconConfig().BellatrixForkVersion)
|
||||
capellaV := bytesutil.ToBytes4(params.BeaconConfig().CapellaForkVersion)
|
||||
denebV := bytesutil.ToBytes4(params.BeaconConfig().DenebForkVersion)
|
||||
electraV := bytesutil.ToBytes4(params.BeaconConfig().ElectraForkVersion)
|
||||
altairS, err := slots.EpochStart(params.BeaconConfig().AltairForkEpoch)
|
||||
require.NoError(t, err)
|
||||
bellaS, err := slots.EpochStart(params.BeaconConfig().BellatrixForkEpoch)
|
||||
@@ -311,6 +337,8 @@ func TestUnmarshalBlock(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
denebS, err := slots.EpochStart(params.BeaconConfig().DenebForkEpoch)
|
||||
require.NoError(t, err)
|
||||
electraS, err := slots.EpochStart(params.BeaconConfig().ElectraForkEpoch)
|
||||
require.NoError(t, err)
|
||||
cases := []struct {
|
||||
b func(*testing.T, primitives.Slot) interfaces.ReadOnlySignedBeaconBlock
|
||||
name string
|
||||
@@ -365,6 +393,12 @@ func TestUnmarshalBlock(t *testing.T) {
|
||||
version: denebV,
|
||||
slot: denebS,
|
||||
},
|
||||
{
|
||||
name: "first slot of electra",
|
||||
b: signedTestBlockElectra,
|
||||
version: electraV,
|
||||
slot: electraS,
|
||||
},
|
||||
{
|
||||
name: "bellatrix block in altair slot",
|
||||
b: signedTestBlockBellatrix,
|
||||
@@ -409,13 +443,14 @@ func TestUnmarshalBlock(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUnmarshalBlindedBlock(t *testing.T) {
|
||||
undo := util.HackDenebMaxuint(t)
|
||||
undo := util.HackElectraMaxuint(t)
|
||||
defer undo()
|
||||
genv := bytesutil.ToBytes4(params.BeaconConfig().GenesisForkVersion)
|
||||
altairv := bytesutil.ToBytes4(params.BeaconConfig().AltairForkVersion)
|
||||
bellav := bytesutil.ToBytes4(params.BeaconConfig().BellatrixForkVersion)
|
||||
capellaV := bytesutil.ToBytes4(params.BeaconConfig().CapellaForkVersion)
|
||||
denebV := bytesutil.ToBytes4(params.BeaconConfig().DenebForkVersion)
|
||||
electraV := bytesutil.ToBytes4(params.BeaconConfig().ElectraForkVersion)
|
||||
altairS, err := slots.EpochStart(params.BeaconConfig().AltairForkEpoch)
|
||||
require.NoError(t, err)
|
||||
bellaS, err := slots.EpochStart(params.BeaconConfig().BellatrixForkEpoch)
|
||||
@@ -424,6 +459,8 @@ func TestUnmarshalBlindedBlock(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
denebS, err := slots.EpochStart(params.BeaconConfig().DenebForkEpoch)
|
||||
require.NoError(t, err)
|
||||
electraS, err := slots.EpochStart(params.BeaconConfig().ElectraForkEpoch)
|
||||
require.NoError(t, err)
|
||||
cases := []struct {
|
||||
b func(*testing.T, primitives.Slot) interfaces.ReadOnlySignedBeaconBlock
|
||||
name string
|
||||
@@ -485,6 +522,12 @@ func TestUnmarshalBlindedBlock(t *testing.T) {
|
||||
version: denebV,
|
||||
slot: denebS,
|
||||
},
|
||||
{
|
||||
name: "first slot of electra",
|
||||
b: signedTestBlindedBlockElectra,
|
||||
version: electraV,
|
||||
slot: electraS,
|
||||
},
|
||||
{
|
||||
name: "genesis block in altair slot",
|
||||
b: signedTestBlockGenesis,
|
||||
@@ -577,6 +620,14 @@ func signedTestBlockDeneb(t *testing.T, slot primitives.Slot) interfaces.ReadOnl
|
||||
return s
|
||||
}
|
||||
|
||||
func signedTestBlockElectra(t *testing.T, slot primitives.Slot) interfaces.ReadOnlySignedBeaconBlock {
|
||||
b := util.NewBeaconBlockElectra()
|
||||
b.Block.Slot = slot
|
||||
s, err := blocks.NewSignedBeaconBlock(b)
|
||||
require.NoError(t, err)
|
||||
return s
|
||||
}
|
||||
|
||||
func signedTestBlindedBlockDeneb(t *testing.T, slot primitives.Slot) interfaces.ReadOnlySignedBeaconBlock {
|
||||
b := util.NewBlindedBeaconBlockDeneb()
|
||||
b.Message.Slot = slot
|
||||
@@ -584,3 +635,11 @@ func signedTestBlindedBlockDeneb(t *testing.T, slot primitives.Slot) interfaces.
|
||||
require.NoError(t, err)
|
||||
return s
|
||||
}
|
||||
|
||||
func signedTestBlindedBlockElectra(t *testing.T, slot primitives.Slot) interfaces.ReadOnlySignedBeaconBlock {
|
||||
b := util.NewBlindedBeaconBlockElectra()
|
||||
b.Message.Slot = slot
|
||||
s, err := blocks.NewSignedBeaconBlock(b)
|
||||
require.NoError(t, err)
|
||||
return s
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user