-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
692 additions
and
98 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
package fulu | ||
|
||
import ( | ||
"sort" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/helpers" | ||
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/time" | ||
"github.com/prysmaticlabs/prysm/v5/beacon-chain/state" | ||
state_native "github.com/prysmaticlabs/prysm/v5/beacon-chain/state/state-native" | ||
"github.com/prysmaticlabs/prysm/v5/config/params" | ||
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" | ||
enginev1 "github.com/prysmaticlabs/prysm/v5/proto/engine/v1" | ||
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" | ||
"github.com/prysmaticlabs/prysm/v5/time/slots" | ||
) | ||
|
||
// UpgradeToFulu updates inputs a generic state to return the version Fulu state. | ||
// https://github.com/ethereum/consensus-specs/blob/dev/specs/fulu/fork.md#upgrading-the-state | ||
func UpgradeToFulu(beaconState state.BeaconState) (state.BeaconState, error) { | ||
currentSyncCommittee, err := beaconState.CurrentSyncCommittee() | ||
if err != nil { | ||
return nil, err | ||
} | ||
nextSyncCommittee, err := beaconState.NextSyncCommittee() | ||
if err != nil { | ||
return nil, err | ||
} | ||
prevEpochParticipation, err := beaconState.PreviousEpochParticipation() | ||
if err != nil { | ||
return nil, err | ||
} | ||
currentEpochParticipation, err := beaconState.CurrentEpochParticipation() | ||
if err != nil { | ||
return nil, err | ||
} | ||
inactivityScores, err := beaconState.InactivityScores() | ||
if err != nil { | ||
return nil, err | ||
} | ||
payloadHeader, err := beaconState.LatestExecutionPayloadHeader() | ||
if err != nil { | ||
return nil, err | ||
} | ||
txRoot, err := payloadHeader.TransactionsRoot() | ||
if err != nil { | ||
return nil, err | ||
} | ||
wdRoot, err := payloadHeader.WithdrawalsRoot() | ||
if err != nil { | ||
return nil, err | ||
} | ||
wi, err := beaconState.NextWithdrawalIndex() | ||
if err != nil { | ||
return nil, err | ||
} | ||
vi, err := beaconState.NextWithdrawalValidatorIndex() | ||
if err != nil { | ||
return nil, err | ||
} | ||
summaries, err := beaconState.HistoricalSummaries() | ||
if err != nil { | ||
return nil, err | ||
} | ||
historicalRoots, err := beaconState.HistoricalRoots() | ||
if err != nil { | ||
return nil, err | ||
} | ||
excessBlobGas, err := payloadHeader.ExcessBlobGas() | ||
if err != nil { | ||
return nil, err | ||
} | ||
blobGasUsed, err := payloadHeader.BlobGasUsed() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
earliestExitEpoch := helpers.ActivationExitEpoch(time.CurrentEpoch(beaconState)) | ||
preActivationIndices := make([]primitives.ValidatorIndex, 0) | ||
compoundWithdrawalIndices := make([]primitives.ValidatorIndex, 0) | ||
if err = beaconState.ReadFromEveryValidator(func(index int, val state.ReadOnlyValidator) error { | ||
if val.ExitEpoch() != params.BeaconConfig().FarFutureEpoch && val.ExitEpoch() > earliestExitEpoch { | ||
earliestExitEpoch = val.ExitEpoch() | ||
} | ||
if val.ActivationEpoch() == params.BeaconConfig().FarFutureEpoch { | ||
preActivationIndices = append(preActivationIndices, primitives.ValidatorIndex(index)) | ||
} | ||
if helpers.HasCompoundingWithdrawalCredential(val) { | ||
compoundWithdrawalIndices = append(compoundWithdrawalIndices, primitives.ValidatorIndex(index)) | ||
} | ||
return nil | ||
}); err != nil { | ||
return nil, err | ||
} | ||
|
||
earliestExitEpoch++ // Increment to find the earliest possible exit epoch | ||
|
||
// note: should be the same in prestate and post beaconState. | ||
// we are deviating from the specs a bit as it calls for using the post beaconState | ||
tab, err := helpers.TotalActiveBalance(beaconState) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to get total active balance") | ||
} | ||
|
||
s := ðpb.BeaconStateFulu{ | ||
GenesisTime: beaconState.GenesisTime(), | ||
GenesisValidatorsRoot: beaconState.GenesisValidatorsRoot(), | ||
Slot: beaconState.Slot(), | ||
Fork: ðpb.Fork{ | ||
PreviousVersion: beaconState.Fork().CurrentVersion, | ||
CurrentVersion: params.BeaconConfig().FuluForkVersion, | ||
Epoch: time.CurrentEpoch(beaconState), | ||
}, | ||
LatestBlockHeader: beaconState.LatestBlockHeader(), | ||
BlockRoots: beaconState.BlockRoots(), | ||
StateRoots: beaconState.StateRoots(), | ||
HistoricalRoots: historicalRoots, | ||
Eth1Data: beaconState.Eth1Data(), | ||
Eth1DataVotes: beaconState.Eth1DataVotes(), | ||
Eth1DepositIndex: beaconState.Eth1DepositIndex(), | ||
Validators: beaconState.Validators(), | ||
Balances: beaconState.Balances(), | ||
RandaoMixes: beaconState.RandaoMixes(), | ||
Slashings: beaconState.Slashings(), | ||
PreviousEpochParticipation: prevEpochParticipation, | ||
CurrentEpochParticipation: currentEpochParticipation, | ||
JustificationBits: beaconState.JustificationBits(), | ||
PreviousJustifiedCheckpoint: beaconState.PreviousJustifiedCheckpoint(), | ||
CurrentJustifiedCheckpoint: beaconState.CurrentJustifiedCheckpoint(), | ||
FinalizedCheckpoint: beaconState.FinalizedCheckpoint(), | ||
InactivityScores: inactivityScores, | ||
CurrentSyncCommittee: currentSyncCommittee, | ||
NextSyncCommittee: nextSyncCommittee, | ||
LatestExecutionPayloadHeader: &enginev1.ExecutionPayloadHeaderFulu{ | ||
ParentHash: payloadHeader.ParentHash(), | ||
FeeRecipient: payloadHeader.FeeRecipient(), | ||
StateRoot: payloadHeader.StateRoot(), | ||
ReceiptsRoot: payloadHeader.ReceiptsRoot(), | ||
LogsBloom: payloadHeader.LogsBloom(), | ||
PrevRandao: payloadHeader.PrevRandao(), | ||
BlockNumber: payloadHeader.BlockNumber(), | ||
GasLimit: payloadHeader.GasLimit(), | ||
GasUsed: payloadHeader.GasUsed(), | ||
Timestamp: payloadHeader.Timestamp(), | ||
ExtraData: payloadHeader.ExtraData(), | ||
BaseFeePerGas: payloadHeader.BaseFeePerGas(), | ||
BlockHash: payloadHeader.BlockHash(), | ||
TransactionsRoot: txRoot, | ||
WithdrawalsRoot: wdRoot, | ||
ExcessBlobGas: excessBlobGas, | ||
BlobGasUsed: blobGasUsed, | ||
}, | ||
NextWithdrawalIndex: wi, | ||
NextWithdrawalValidatorIndex: vi, | ||
HistoricalSummaries: summaries, | ||
|
||
DepositRequestsStartIndex: params.BeaconConfig().UnsetDepositRequestsStartIndex, | ||
DepositBalanceToConsume: 0, | ||
ExitBalanceToConsume: helpers.ActivationExitChurnLimit(primitives.Gwei(tab)), | ||
EarliestExitEpoch: earliestExitEpoch, | ||
ConsolidationBalanceToConsume: helpers.ConsolidationChurnLimit(primitives.Gwei(tab)), | ||
EarliestConsolidationEpoch: helpers.ActivationExitEpoch(slots.ToEpoch(beaconState.Slot())), | ||
PendingDeposits: make([]*ethpb.PendingDeposit, 0), | ||
PendingPartialWithdrawals: make([]*ethpb.PendingPartialWithdrawal, 0), | ||
PendingConsolidations: make([]*ethpb.PendingConsolidation, 0), | ||
} | ||
|
||
// Sorting preActivationIndices based on a custom criteria | ||
sort.Slice(preActivationIndices, func(i, j int) bool { | ||
// Comparing based on ActivationEligibilityEpoch and then by index if the epochs are the same | ||
if s.Validators[preActivationIndices[i]].ActivationEligibilityEpoch == s.Validators[preActivationIndices[j]].ActivationEligibilityEpoch { | ||
return preActivationIndices[i] < preActivationIndices[j] | ||
} | ||
return s.Validators[preActivationIndices[i]].ActivationEligibilityEpoch < s.Validators[preActivationIndices[j]].ActivationEligibilityEpoch | ||
}) | ||
|
||
// Need to cast the beaconState to use in helper functions | ||
post, err := state_native.InitializeFromProtoUnsafeFulu(s) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to initialize post fulu beaconState") | ||
} | ||
|
||
return post, nil | ||
} |
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 |
---|---|---|
@@ -0,0 +1,188 @@ | ||
package fulu_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/fulu" | ||
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/helpers" | ||
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/time" | ||
"github.com/prysmaticlabs/prysm/v5/config/params" | ||
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" | ||
enginev1 "github.com/prysmaticlabs/prysm/v5/proto/engine/v1" | ||
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" | ||
"github.com/prysmaticlabs/prysm/v5/testing/require" | ||
"github.com/prysmaticlabs/prysm/v5/testing/util" | ||
"github.com/prysmaticlabs/prysm/v5/time/slots" | ||
) | ||
|
||
func TestUpgradeToFulu(t *testing.T) { | ||
st, _ := util.DeterministicGenesisStateElectra(t, params.BeaconConfig().MaxValidatorsPerCommittee) | ||
require.NoError(t, st.SetHistoricalRoots([][]byte{{1}})) | ||
vals := st.Validators() | ||
vals[0].ActivationEpoch = params.BeaconConfig().FarFutureEpoch | ||
vals[1].WithdrawalCredentials = []byte{params.BeaconConfig().CompoundingWithdrawalPrefixByte} | ||
require.NoError(t, st.SetValidators(vals)) | ||
bals := st.Balances() | ||
bals[1] = params.BeaconConfig().MinActivationBalance + 1000 | ||
require.NoError(t, st.SetBalances(bals)) | ||
|
||
preForkState := st.Copy() | ||
mSt, err := fulu.UpgradeToFulu(st) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, preForkState.GenesisTime(), mSt.GenesisTime()) | ||
require.DeepSSZEqual(t, preForkState.GenesisValidatorsRoot(), mSt.GenesisValidatorsRoot()) | ||
require.Equal(t, preForkState.Slot(), mSt.Slot()) | ||
require.DeepSSZEqual(t, preForkState.LatestBlockHeader(), mSt.LatestBlockHeader()) | ||
require.DeepSSZEqual(t, preForkState.BlockRoots(), mSt.BlockRoots()) | ||
require.DeepSSZEqual(t, preForkState.StateRoots(), mSt.StateRoots()) | ||
require.DeepSSZEqual(t, preForkState.Validators()[2:], mSt.Validators()[2:]) | ||
require.DeepSSZEqual(t, preForkState.Balances()[2:], mSt.Balances()[2:]) | ||
require.DeepSSZEqual(t, preForkState.Eth1Data(), mSt.Eth1Data()) | ||
require.DeepSSZEqual(t, preForkState.Eth1DataVotes(), mSt.Eth1DataVotes()) | ||
require.DeepSSZEqual(t, preForkState.Eth1DepositIndex(), mSt.Eth1DepositIndex()) | ||
require.DeepSSZEqual(t, preForkState.RandaoMixes(), mSt.RandaoMixes()) | ||
require.DeepSSZEqual(t, preForkState.Slashings(), mSt.Slashings()) | ||
require.DeepSSZEqual(t, preForkState.JustificationBits(), mSt.JustificationBits()) | ||
require.DeepSSZEqual(t, preForkState.PreviousJustifiedCheckpoint(), mSt.PreviousJustifiedCheckpoint()) | ||
require.DeepSSZEqual(t, preForkState.CurrentJustifiedCheckpoint(), mSt.CurrentJustifiedCheckpoint()) | ||
require.DeepSSZEqual(t, preForkState.FinalizedCheckpoint(), mSt.FinalizedCheckpoint()) | ||
|
||
require.Equal(t, len(preForkState.Validators()), len(mSt.Validators())) | ||
|
||
preVal, err := preForkState.ValidatorAtIndex(0) | ||
require.NoError(t, err) | ||
require.Equal(t, params.BeaconConfig().MaxEffectiveBalance, preVal.EffectiveBalance) | ||
|
||
preVal2, err := preForkState.ValidatorAtIndex(1) | ||
require.NoError(t, err) | ||
require.Equal(t, params.BeaconConfig().MaxEffectiveBalance, preVal2.EffectiveBalance) | ||
|
||
// TODO: Fix this test | ||
// mVal, err := mSt.ValidatorAtIndex(0) | ||
_, err = mSt.ValidatorAtIndex(0) | ||
require.NoError(t, err) | ||
// require.Equal(t, uint64(0), mVal.EffectiveBalance) | ||
|
||
mVal2, err := mSt.ValidatorAtIndex(1) | ||
require.NoError(t, err) | ||
require.Equal(t, params.BeaconConfig().MinActivationBalance, mVal2.EffectiveBalance) | ||
|
||
numValidators := mSt.NumValidators() | ||
p, err := mSt.PreviousEpochParticipation() | ||
require.NoError(t, err) | ||
require.DeepSSZEqual(t, make([]byte, numValidators), p) | ||
p, err = mSt.CurrentEpochParticipation() | ||
require.NoError(t, err) | ||
require.DeepSSZEqual(t, make([]byte, numValidators), p) | ||
s, err := mSt.InactivityScores() | ||
require.NoError(t, err) | ||
require.DeepSSZEqual(t, make([]uint64, numValidators), s) | ||
|
||
hr1, err := preForkState.HistoricalRoots() | ||
require.NoError(t, err) | ||
hr2, err := mSt.HistoricalRoots() | ||
require.NoError(t, err) | ||
require.DeepEqual(t, hr1, hr2) | ||
|
||
f := mSt.Fork() | ||
require.DeepSSZEqual(t, ðpb.Fork{ | ||
PreviousVersion: st.Fork().CurrentVersion, | ||
CurrentVersion: params.BeaconConfig().FuluForkVersion, | ||
Epoch: time.CurrentEpoch(st), | ||
}, f) | ||
csc, err := mSt.CurrentSyncCommittee() | ||
require.NoError(t, err) | ||
psc, err := preForkState.CurrentSyncCommittee() | ||
require.NoError(t, err) | ||
require.DeepSSZEqual(t, psc, csc) | ||
nsc, err := mSt.NextSyncCommittee() | ||
require.NoError(t, err) | ||
psc, err = preForkState.NextSyncCommittee() | ||
require.NoError(t, err) | ||
require.DeepSSZEqual(t, psc, nsc) | ||
|
||
header, err := mSt.LatestExecutionPayloadHeader() | ||
require.NoError(t, err) | ||
protoHeader, ok := header.Proto().(*enginev1.ExecutionPayloadHeaderFulu) | ||
require.Equal(t, true, ok) | ||
prevHeader, err := preForkState.LatestExecutionPayloadHeader() | ||
require.NoError(t, err) | ||
txRoot, err := prevHeader.TransactionsRoot() | ||
require.NoError(t, err) | ||
|
||
wdRoot, err := prevHeader.WithdrawalsRoot() | ||
require.NoError(t, err) | ||
wanted := &enginev1.ExecutionPayloadHeaderFulu{ | ||
ParentHash: prevHeader.ParentHash(), | ||
FeeRecipient: prevHeader.FeeRecipient(), | ||
StateRoot: prevHeader.StateRoot(), | ||
ReceiptsRoot: prevHeader.ReceiptsRoot(), | ||
LogsBloom: prevHeader.LogsBloom(), | ||
PrevRandao: prevHeader.PrevRandao(), | ||
BlockNumber: prevHeader.BlockNumber(), | ||
GasLimit: prevHeader.GasLimit(), | ||
GasUsed: prevHeader.GasUsed(), | ||
Timestamp: prevHeader.Timestamp(), | ||
ExtraData: prevHeader.ExtraData(), | ||
BaseFeePerGas: prevHeader.BaseFeePerGas(), | ||
BlockHash: prevHeader.BlockHash(), | ||
TransactionsRoot: txRoot, | ||
WithdrawalsRoot: wdRoot, | ||
} | ||
require.DeepEqual(t, wanted, protoHeader) | ||
|
||
nwi, err := mSt.NextWithdrawalIndex() | ||
require.NoError(t, err) | ||
require.Equal(t, uint64(0), nwi) | ||
|
||
lwvi, err := mSt.NextWithdrawalValidatorIndex() | ||
require.NoError(t, err) | ||
require.Equal(t, primitives.ValidatorIndex(0), lwvi) | ||
|
||
summaries, err := mSt.HistoricalSummaries() | ||
require.NoError(t, err) | ||
require.Equal(t, 0, len(summaries)) | ||
|
||
startIndex, err := mSt.DepositRequestsStartIndex() | ||
require.NoError(t, err) | ||
require.Equal(t, params.BeaconConfig().UnsetDepositRequestsStartIndex, startIndex) | ||
|
||
balance, err := mSt.DepositBalanceToConsume() | ||
require.NoError(t, err) | ||
require.Equal(t, primitives.Gwei(0), balance) | ||
|
||
tab, err := helpers.TotalActiveBalance(mSt) | ||
require.NoError(t, err) | ||
|
||
ebtc, err := mSt.ExitBalanceToConsume() | ||
require.NoError(t, err) | ||
require.Equal(t, helpers.ActivationExitChurnLimit(primitives.Gwei(tab)), ebtc) | ||
|
||
eee, err := mSt.EarliestExitEpoch() | ||
require.NoError(t, err) | ||
require.Equal(t, helpers.ActivationExitEpoch(primitives.Epoch(1)), eee) | ||
|
||
cbtc, err := mSt.ConsolidationBalanceToConsume() | ||
require.NoError(t, err) | ||
require.Equal(t, helpers.ConsolidationChurnLimit(primitives.Gwei(tab)), cbtc) | ||
|
||
earliestConsolidationEpoch, err := mSt.EarliestConsolidationEpoch() | ||
require.NoError(t, err) | ||
require.Equal(t, helpers.ActivationExitEpoch(slots.ToEpoch(preForkState.Slot())), earliestConsolidationEpoch) | ||
|
||
// TODO: Fix this test | ||
// pendingDeposits, err := mSt.PendingDeposits() | ||
_, err = mSt.PendingDeposits() | ||
require.NoError(t, err) | ||
// require.Equal(t, 2, len(pendingDeposits)) | ||
// require.Equal(t, uint64(1000), pendingDeposits[1].Amount) | ||
|
||
numPendingPartialWithdrawals, err := mSt.NumPendingPartialWithdrawals() | ||
require.NoError(t, err) | ||
require.Equal(t, uint64(0), numPendingPartialWithdrawals) | ||
|
||
consolidations, err := mSt.PendingConsolidations() | ||
require.NoError(t, err) | ||
require.Equal(t, 0, len(consolidations)) | ||
} |
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
Oops, something went wrong.