-
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
6 changed files
with
217 additions
and
14 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
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
105 changes: 105 additions & 0 deletions
105
beacon-chain/core/epbs/execution_payload_envelope_test.go
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,105 @@ | ||
package epbs | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
state_native "github.com/prysmaticlabs/prysm/v5/beacon-chain/state/state-native" | ||
consensus_epbs "github.com/prysmaticlabs/prysm/v5/consensus-types/epbs" | ||
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" | ||
) | ||
|
||
func TestProcessPayloadStateTransition(t *testing.T) { | ||
bh := [32]byte{'h'} | ||
payload := &enginev1.ExecutionPayloadElectra{BlockHash: bh[:]} | ||
p := &enginev1.ExecutionPayloadEnvelope{Payload: payload} | ||
e, err := consensus_epbs.WrappedROExecutionPayloadEnvelope(p) | ||
require.NoError(t, err) | ||
stpb := ðpb.BeaconStateEPBS{Slot: 3} | ||
st, err := state_native.InitializeFromProtoUnsafeEpbs(stpb) | ||
require.NoError(t, err) | ||
ctx := context.Background() | ||
|
||
lbh, err := st.LatestBlockHash() | ||
require.NoError(t, err) | ||
require.Equal(t, [32]byte{}, [32]byte(lbh)) | ||
|
||
require.NoError(t, processPayloadStateTransition(ctx, st, e)) | ||
|
||
lbh, err = st.LatestBlockHash() | ||
require.NoError(t, err) | ||
require.Equal(t, bh, [32]byte(lbh)) | ||
|
||
lfs, err := st.LatestFullSlot() | ||
require.NoError(t, err) | ||
require.Equal(t, lfs, st.Slot()) | ||
} | ||
|
||
func Test_validateAgainstHeader(t *testing.T) { | ||
bh := [32]byte{'h'} | ||
payload := &enginev1.ExecutionPayloadElectra{BlockHash: bh[:]} | ||
p := &enginev1.ExecutionPayloadEnvelope{Payload: payload} | ||
e, err := consensus_epbs.WrappedROExecutionPayloadEnvelope(p) | ||
require.NoError(t, err) | ||
stpb := ðpb.BeaconStateEPBS{Slot: 3} | ||
st, err := state_native.InitializeFromProtoUnsafeEpbs(stpb) | ||
require.NoError(t, err) | ||
ctx := context.Background() | ||
require.ErrorContains(t, "invalid nil latest block header", validateAgainstHeader(ctx, st, e)) | ||
|
||
prest, _ := util.DeterministicGenesisStateEpbs(t, 64) | ||
require.ErrorContains(t, "attempted to wrap nil object", validateAgainstHeader(ctx, prest, e)) | ||
|
||
br := [32]byte{'r'} | ||
p.BeaconBlockRoot = br[:] | ||
require.ErrorContains(t, "beacon block root does not match previous header", validateAgainstHeader(ctx, prest, e)) | ||
|
||
header := prest.LatestBlockHeader() | ||
require.NoError(t, err) | ||
headerRoot, err := header.HashTreeRoot() | ||
require.NoError(t, err) | ||
p.BeaconBlockRoot = headerRoot[:] | ||
require.NoError(t, validateAgainstHeader(ctx, prest, e)) | ||
} | ||
|
||
func Test_validateAgainstCommittedBid(t *testing.T) { | ||
payload := &enginev1.ExecutionPayloadElectra{} | ||
p := &enginev1.ExecutionPayloadEnvelope{Payload: payload, BuilderIndex: 1} | ||
e, err := consensus_epbs.WrappedROExecutionPayloadEnvelope(p) | ||
require.NoError(t, err) | ||
h := &enginev1.ExecutionPayloadHeaderEPBS{} | ||
require.ErrorContains(t, "builder index does not match committed header", validateAgainstCommittedBid(h, e)) | ||
|
||
h.BuilderIndex = 1 | ||
require.ErrorContains(t, "attempted to wrap nil object", validateAgainstCommittedBid(h, e)) | ||
p.BlobKzgCommitments = make([][]byte, 6) | ||
for i := range p.BlobKzgCommitments { | ||
p.BlobKzgCommitments[i] = make([]byte, 48) | ||
} | ||
h.BlobKzgCommitmentsRoot = make([]byte, 32) | ||
require.ErrorContains(t, "blob KZG commitments root does not match committed header", validateAgainstCommittedBid(h, e)) | ||
|
||
root, err := e.BlobKzgCommitmentsRoot() | ||
require.NoError(t, err) | ||
h.BlobKzgCommitmentsRoot = root[:] | ||
require.NoError(t, validateAgainstCommittedBid(h, e)) | ||
} | ||
|
||
func TestCheckPostStateRoot(t *testing.T) { | ||
payload := &enginev1.ExecutionPayloadElectra{} | ||
p := &enginev1.ExecutionPayloadEnvelope{Payload: payload, BuilderIndex: 1} | ||
e, err := consensus_epbs.WrappedROExecutionPayloadEnvelope(p) | ||
require.NoError(t, err) | ||
ctx := context.Background() | ||
st, _ := util.DeterministicGenesisStateEpbs(t, 64) | ||
require.ErrorContains(t, "attempted to wrap nil object", checkPostStateRoot(ctx, st, e)) | ||
p.StateRoot = make([]byte, 32) | ||
require.ErrorContains(t, "state root mismatch", checkPostStateRoot(ctx, st, e)) | ||
root, err := st.HashTreeRoot(ctx) | ||
require.NoError(t, err) | ||
p.StateRoot = root[:] | ||
require.NoError(t, checkPostStateRoot(ctx, st, e)) | ||
} |
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
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
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,92 @@ | ||
package util | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/helpers" | ||
"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" | ||
"github.com/prysmaticlabs/prysm/v5/crypto/bls" | ||
enginev1 "github.com/prysmaticlabs/prysm/v5/proto/engine/v1" | ||
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" | ||
) | ||
|
||
// emptyGenesisStateEpbs returns an empty genesis state in ePBS format. | ||
func emptyGenesisStateEpbs() (state.BeaconState, error) { | ||
st := ðpb.BeaconStateEPBS{ | ||
// Misc fields. | ||
Slot: 0, | ||
Fork: ðpb.Fork{ | ||
PreviousVersion: params.BeaconConfig().BellatrixForkVersion, | ||
CurrentVersion: params.BeaconConfig().DenebForkVersion, | ||
Epoch: 0, | ||
}, | ||
// Validator registry fields. | ||
Validators: []*ethpb.Validator{}, | ||
Balances: []uint64{}, | ||
InactivityScores: []uint64{}, | ||
|
||
JustificationBits: []byte{0}, | ||
HistoricalRoots: [][]byte{}, | ||
CurrentEpochParticipation: []byte{}, | ||
PreviousEpochParticipation: []byte{}, | ||
|
||
// Eth1 data. | ||
Eth1Data: ðpb.Eth1Data{}, | ||
Eth1DataVotes: []*ethpb.Eth1Data{}, | ||
Eth1DepositIndex: 0, | ||
|
||
LatestExecutionPayloadHeader: &enginev1.ExecutionPayloadHeaderEPBS{}, | ||
|
||
DepositBalanceToConsume: primitives.Gwei(0), | ||
ExitBalanceToConsume: primitives.Gwei(0), | ||
ConsolidationBalanceToConsume: primitives.Gwei(0), | ||
} | ||
return state_native.InitializeFromProtoEpbs(st) | ||
} | ||
|
||
// genesisBeaconStateEpbs returns the genesis beacon state. | ||
func genesisBeaconStateEpbs(ctx context.Context, deposits []*ethpb.Deposit, genesisTime uint64, eth1Data *ethpb.Eth1Data) (state.BeaconState, error) { | ||
st, err := emptyGenesisStateElectra() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Process initial deposits. | ||
st, err = helpers.UpdateGenesisEth1Data(st, deposits, eth1Data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
st, err = processPreGenesisDeposits(ctx, st, deposits) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "could not process validator deposits") | ||
} | ||
|
||
return buildGenesisBeaconStateElectra(genesisTime, st, st.Eth1Data()) | ||
} | ||
|
||
// DeterministicGenesisStateEpbs returns a genesis state in ePBS format made using the deterministic deposits. | ||
func DeterministicGenesisStateEpbs(t testing.TB, numValidators uint64) (state.BeaconState, []bls.SecretKey) { | ||
deposits, privKeys, err := DeterministicDepositsAndKeys(numValidators) | ||
if err != nil { | ||
t.Fatal(errors.Wrapf(err, "failed to get %d deposits", numValidators)) | ||
} | ||
eth1Data, err := DeterministicEth1Data(len(deposits)) | ||
if err != nil { | ||
t.Fatal(errors.Wrapf(err, "failed to get eth1data for %d deposits", numValidators)) | ||
} | ||
beaconState, err := genesisBeaconStateElectra(context.Background(), deposits, uint64(0), eth1Data) | ||
if err != nil { | ||
t.Fatal(errors.Wrapf(err, "failed to get genesis beacon state of %d validators", numValidators)) | ||
} | ||
if err := setKeysToActive(beaconState); err != nil { | ||
t.Fatal(errors.Wrapf(err, "failed to set keys to active")) | ||
} | ||
resetCache() | ||
return beaconState, privKeys | ||
} |