mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-10 22:07:59 -05:00
Compare commits
22 Commits
backfill-i
...
hdiff-gloa
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
76ee116ff6 | ||
|
|
ddc788f676 | ||
|
|
01ef644640 | ||
|
|
8e0be88e70 | ||
|
|
fb64c2f0c6 | ||
|
|
4585cdc932 | ||
|
|
aa47435c91 | ||
|
|
80eba4e6dd | ||
|
|
606294e17f | ||
|
|
977e923692 | ||
|
|
013b6b1d60 | ||
|
|
66ff6f70b8 | ||
|
|
5b1a9fb077 | ||
|
|
35bc9b1a0f | ||
|
|
02dca85251 | ||
|
|
39b2163702 | ||
|
|
d67ee62efa | ||
|
|
9f9401e615 | ||
|
|
6b89d839f6 | ||
|
|
d826a3c7fe | ||
|
|
900f162467 | ||
|
|
5266d34a22 |
48
WORKSPACE
48
WORKSPACE
@@ -158,15 +158,15 @@ oci_register_toolchains(
|
||||
|
||||
http_archive(
|
||||
name = "io_bazel_rules_go",
|
||||
integrity = "sha256-JD8o94crTb2DFiJJR8nMAGdBAW95zIENB4cbI+JnrI4=",
|
||||
patch_args = ["-p1"],
|
||||
patches = [
|
||||
# Expose internals of go_test for custom build transitions.
|
||||
"//third_party:io_bazel_rules_go_test.patch",
|
||||
],
|
||||
strip_prefix = "rules_go-cf3c3af34bd869b864f5f2b98e2f41c2b220d6c9",
|
||||
sha256 = "a729c8ed2447c90fe140077689079ca0acfb7580ec41637f312d650ce9d93d96",
|
||||
urls = [
|
||||
"https://github.com/bazel-contrib/rules_go/archive/cf3c3af34bd869b864f5f2b98e2f41c2b220d6c9.tar.gz",
|
||||
"https://mirror.bazel.build/github.com/bazel-contrib/rules_go/releases/download/v0.57.0/rules_go-v0.57.0.zip",
|
||||
"https://github.com/bazel-contrib/rules_go/releases/download/v0.57.0/rules_go-v0.57.0.zip",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -208,7 +208,7 @@ load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_depe
|
||||
go_rules_dependencies()
|
||||
|
||||
go_register_toolchains(
|
||||
go_version = "1.24.6",
|
||||
go_version = "1.25.1",
|
||||
nogo = "@//:nogo",
|
||||
)
|
||||
|
||||
@@ -300,22 +300,6 @@ filegroup(
|
||||
url = "https://github.com/ethereum/bls12-381-tests/releases/download/%s/bls_tests_yaml.tar.gz" % bls_test_version,
|
||||
)
|
||||
|
||||
http_archive(
|
||||
name = "eth2_networks",
|
||||
build_file_content = """
|
||||
filegroup(
|
||||
name = "configs",
|
||||
srcs = glob([
|
||||
"shared/**/config.yaml",
|
||||
]),
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
""",
|
||||
sha256 = "77e7e3ed65e33b7bb19d30131f4c2bb39e4dfeb188ab9ae84651c3cc7600131d",
|
||||
strip_prefix = "eth2-networks-934c948e69205dcf2deb87e4ae6cc140c335f94d",
|
||||
url = "https://github.com/eth-clients/eth2-networks/archive/934c948e69205dcf2deb87e4ae6cc140c335f94d.tar.gz",
|
||||
)
|
||||
|
||||
http_archive(
|
||||
name = "holesky_testnet",
|
||||
build_file_content = """
|
||||
@@ -327,9 +311,9 @@ filegroup(
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
""",
|
||||
integrity = "sha256-YVFFrCmjoGZ3fXMWpsCpSsYbANy1grnqYwOLKIg2SsA=",
|
||||
strip_prefix = "holesky-32a72e21c6e53c262f27d50dd540cb654517d03a",
|
||||
url = "https://github.com/eth-clients/holesky/archive/32a72e21c6e53c262f27d50dd540cb654517d03a.tar.gz", # 2025-03-17
|
||||
integrity = "sha256-htyxg8Ln2o8eCiifFN7/hcHGZg8Ir9CPzCEx+FUnnCs=",
|
||||
strip_prefix = "holesky-8aec65f11f0c986d6b76b2eb902420635eb9b815",
|
||||
url = "https://github.com/eth-clients/holesky/archive/8aec65f11f0c986d6b76b2eb902420635eb9b815.tar.gz",
|
||||
)
|
||||
|
||||
http_archive(
|
||||
@@ -359,9 +343,9 @@ filegroup(
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
""",
|
||||
integrity = "sha256-b5F7Wg9LLMqGRIpP2uqb/YsSFVn2ynzlV7g/Nb1EFLk=",
|
||||
strip_prefix = "sepolia-562d9938f08675e9ba490a1dfba21fb05843f39f",
|
||||
url = "https://github.com/eth-clients/sepolia/archive/562d9938f08675e9ba490a1dfba21fb05843f39f.tar.gz", # 2025-03-17
|
||||
integrity = "sha256-+UZgfvBcea0K0sbvAJZOz5ZNmxdWZYbohP38heUuc6w=",
|
||||
strip_prefix = "sepolia-f9158732adb1a2a6440613ad2232eb50e7384c4f",
|
||||
url = "https://github.com/eth-clients/sepolia/archive/f9158732adb1a2a6440613ad2232eb50e7384c4f.tar.gz",
|
||||
)
|
||||
|
||||
http_archive(
|
||||
@@ -375,17 +359,17 @@ filegroup(
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
""",
|
||||
integrity = "sha256-dPiEWUd8QvbYGwGtIm0QtCekitVLOLsW5rpQIGzz8PU=",
|
||||
strip_prefix = "hoodi-828c2c940e1141092bd4bb979cef547ea926d272",
|
||||
url = "https://github.com/eth-clients/hoodi/archive/828c2c940e1141092bd4bb979cef547ea926d272.tar.gz",
|
||||
integrity = "sha256-G+4c9c/vci1OyPrQJnQCI+ZCv/E0cWN4hrHDY3i7ns0=",
|
||||
strip_prefix = "hoodi-b6ee51b2045a5e7fe3efac52534f75b080b049c6",
|
||||
url = "https://github.com/eth-clients/hoodi/archive/b6ee51b2045a5e7fe3efac52534f75b080b049c6.tar.gz",
|
||||
)
|
||||
|
||||
http_archive(
|
||||
name = "com_google_protobuf",
|
||||
sha256 = "9bd87b8280ef720d3240514f884e56a712f2218f0d693b48050c836028940a42",
|
||||
strip_prefix = "protobuf-25.1",
|
||||
sha256 = "7c3ebd7aaedd86fa5dc479a0fda803f602caaf78d8aff7ce83b89e1b8ae7442a",
|
||||
strip_prefix = "protobuf-28.3",
|
||||
urls = [
|
||||
"https://github.com/protocolbuffers/protobuf/archive/v25.1.tar.gz",
|
||||
"https://github.com/protocolbuffers/protobuf/archive/v28.3.tar.gz",
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
@@ -30,10 +30,11 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
getExecHeaderPath = "/eth/v1/builder/header/{{.Slot}}/{{.ParentHash}}/{{.Pubkey}}"
|
||||
getStatus = "/eth/v1/builder/status"
|
||||
postBlindedBeaconBlockPath = "/eth/v1/builder/blinded_blocks"
|
||||
postRegisterValidatorPath = "/eth/v1/builder/validators"
|
||||
getExecHeaderPath = "/eth/v1/builder/header/{{.Slot}}/{{.ParentHash}}/{{.Pubkey}}"
|
||||
getStatus = "/eth/v1/builder/status"
|
||||
postBlindedBeaconBlockPath = "/eth/v1/builder/blinded_blocks"
|
||||
postBlindedBeaconBlockV2Path = "/eth/v2/builder/blinded_blocks"
|
||||
postRegisterValidatorPath = "/eth/v1/builder/validators"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -512,7 +513,7 @@ func (c *Client) SubmitBlindedBlockPostFulu(ctx context.Context, sb interfaces.R
|
||||
}
|
||||
|
||||
// Post the blinded block - the response should only contain a status code (no payload)
|
||||
_, _, err = c.do(ctx, http.MethodPost, postBlindedBeaconBlockPath, bytes.NewBuffer(body), http.StatusAccepted, postOpts)
|
||||
_, _, err = c.do(ctx, http.MethodPost, postBlindedBeaconBlockV2Path, bytes.NewBuffer(body), http.StatusAccepted, postOpts)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error posting the blinded block to the builder api post-Fulu")
|
||||
}
|
||||
|
||||
@@ -1561,7 +1561,7 @@ func TestSubmitBlindedBlockPostFulu(t *testing.T) {
|
||||
t.Run("success", func(t *testing.T) {
|
||||
hc := &http.Client{
|
||||
Transport: roundtrip(func(r *http.Request) (*http.Response, error) {
|
||||
require.Equal(t, postBlindedBeaconBlockPath, r.URL.Path)
|
||||
require.Equal(t, postBlindedBeaconBlockV2Path, r.URL.Path)
|
||||
require.Equal(t, "bellatrix", r.Header.Get("Eth-Consensus-Version"))
|
||||
require.Equal(t, api.JsonMediaType, r.Header.Get("Content-Type"))
|
||||
require.Equal(t, api.JsonMediaType, r.Header.Get("Accept"))
|
||||
@@ -1586,7 +1586,7 @@ func TestSubmitBlindedBlockPostFulu(t *testing.T) {
|
||||
t.Run("success_ssz", func(t *testing.T) {
|
||||
hc := &http.Client{
|
||||
Transport: roundtrip(func(r *http.Request) (*http.Response, error) {
|
||||
require.Equal(t, postBlindedBeaconBlockPath, r.URL.Path)
|
||||
require.Equal(t, postBlindedBeaconBlockV2Path, r.URL.Path)
|
||||
require.Equal(t, "bellatrix", r.Header.Get(api.VersionHeader))
|
||||
require.Equal(t, api.OctetStreamMediaType, r.Header.Get("Content-Type"))
|
||||
require.Equal(t, api.OctetStreamMediaType, r.Header.Get("Accept"))
|
||||
@@ -1612,7 +1612,7 @@ func TestSubmitBlindedBlockPostFulu(t *testing.T) {
|
||||
t.Run("error_response", func(t *testing.T) {
|
||||
hc := &http.Client{
|
||||
Transport: roundtrip(func(r *http.Request) (*http.Response, error) {
|
||||
require.Equal(t, postBlindedBeaconBlockPath, r.URL.Path)
|
||||
require.Equal(t, postBlindedBeaconBlockV2Path, r.URL.Path)
|
||||
require.Equal(t, "bellatrix", r.Header.Get("Eth-Consensus-Version"))
|
||||
message := ErrorMessage{
|
||||
Code: 400,
|
||||
|
||||
@@ -56,3 +56,19 @@ type ForkChoiceNodeExtraData struct {
|
||||
TimeStamp string `json:"timestamp"`
|
||||
Target string `json:"target"`
|
||||
}
|
||||
|
||||
type GetDebugDataColumnSidecarsResponse struct {
|
||||
Version string `json:"version"`
|
||||
ExecutionOptimistic bool `json:"execution_optimistic"`
|
||||
Finalized bool `json:"finalized"`
|
||||
Data []*DataColumnSidecar `json:"data"`
|
||||
}
|
||||
|
||||
type DataColumnSidecar struct {
|
||||
Index string `json:"index"`
|
||||
Column []string `json:"column"`
|
||||
KzgCommitments []string `json:"kzg_commitments"`
|
||||
KzgProofs []string `json:"kzg_proofs"`
|
||||
SignedBeaconBlockHeader *SignedBeaconBlockHeader `json:"signed_block_header"`
|
||||
KzgCommitmentsInclusionProof []string `json:"kzg_commitments_inclusion_proof"`
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package blockchain
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/core/blocks"
|
||||
@@ -746,14 +745,14 @@ func (s *Service) areDataColumnsAvailable(
|
||||
}
|
||||
|
||||
// Get a map of data column indices that are not currently available.
|
||||
missingMap, err := missingDataColumnIndices(s.dataColumnStorage, root, peerInfo.CustodyColumns)
|
||||
missing, err := missingDataColumnIndices(s.dataColumnStorage, root, peerInfo.CustodyColumns)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "missing data columns")
|
||||
}
|
||||
|
||||
// If there are no missing indices, all data column sidecars are available.
|
||||
// This is the happy path.
|
||||
if len(missingMap) == 0 {
|
||||
if len(missing) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -770,33 +769,17 @@ func (s *Service) areDataColumnsAvailable(
|
||||
// Avoid logging if DA check is called after next slot start.
|
||||
if nextSlot.After(time.Now()) {
|
||||
timer := time.AfterFunc(time.Until(nextSlot), func() {
|
||||
missingMapCount := uint64(len(missingMap))
|
||||
missingCount := uint64(len(missing))
|
||||
|
||||
if missingMapCount == 0 {
|
||||
if missingCount == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
var (
|
||||
expected interface{} = "all"
|
||||
missing interface{} = "all"
|
||||
)
|
||||
|
||||
numberOfColumns := params.BeaconConfig().NumberOfColumns
|
||||
colMapCount := uint64(len(peerInfo.CustodyColumns))
|
||||
|
||||
if colMapCount < numberOfColumns {
|
||||
expected = uint64MapToSortedSlice(peerInfo.CustodyColumns)
|
||||
}
|
||||
|
||||
if missingMapCount < numberOfColumns {
|
||||
missing = uint64MapToSortedSlice(missingMap)
|
||||
}
|
||||
|
||||
log.WithFields(logrus.Fields{
|
||||
"slot": block.Slot(),
|
||||
"root": fmt.Sprintf("%#x", root),
|
||||
"columnsExpected": expected,
|
||||
"columnsWaiting": missing,
|
||||
"columnsExpected": helpers.SortedPrettySliceFromMap(peerInfo.CustodyColumns),
|
||||
"columnsWaiting": helpers.SortedPrettySliceFromMap(missing),
|
||||
}).Warning("Data columns still missing at slot end")
|
||||
})
|
||||
defer timer.Stop()
|
||||
@@ -812,7 +795,7 @@ func (s *Service) areDataColumnsAvailable(
|
||||
|
||||
for _, index := range idents.Indices {
|
||||
// This is a data column we are expecting.
|
||||
if _, ok := missingMap[index]; ok {
|
||||
if _, ok := missing[index]; ok {
|
||||
storedDataColumnsCount++
|
||||
}
|
||||
|
||||
@@ -823,10 +806,10 @@ func (s *Service) areDataColumnsAvailable(
|
||||
}
|
||||
|
||||
// Remove the index from the missing map.
|
||||
delete(missingMap, index)
|
||||
delete(missing, index)
|
||||
|
||||
// Return if there is no more missing data columns.
|
||||
if len(missingMap) == 0 {
|
||||
if len(missing) == 0 {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -834,10 +817,10 @@ func (s *Service) areDataColumnsAvailable(
|
||||
case <-ctx.Done():
|
||||
var missingIndices interface{} = "all"
|
||||
numberOfColumns := params.BeaconConfig().NumberOfColumns
|
||||
missingIndicesCount := uint64(len(missingMap))
|
||||
missingIndicesCount := uint64(len(missing))
|
||||
|
||||
if missingIndicesCount < numberOfColumns {
|
||||
missingIndices = uint64MapToSortedSlice(missingMap)
|
||||
missingIndices = helpers.SortedPrettySliceFromMap(missing)
|
||||
}
|
||||
|
||||
return errors.Wrapf(ctx.Err(), "data column sidecars slot: %d, BlockRoot: %#x, missing: %v", block.Slot(), root, missingIndices)
|
||||
@@ -921,16 +904,6 @@ func (s *Service) areBlobsAvailable(ctx context.Context, root [fieldparams.RootL
|
||||
}
|
||||
}
|
||||
|
||||
// uint64MapToSortedSlice produces a sorted uint64 slice from a map.
|
||||
func uint64MapToSortedSlice(input map[uint64]bool) []uint64 {
|
||||
output := make([]uint64, 0, len(input))
|
||||
for idx := range input {
|
||||
output = append(output, idx)
|
||||
}
|
||||
slices.Sort[[]uint64](output)
|
||||
return output
|
||||
}
|
||||
|
||||
// lateBlockTasks is called 4 seconds into the slot and performs tasks
|
||||
// related to late blocks. It emits a MissedSlot state feed event.
|
||||
// It calls FCU and sets the right attributes if we are proposing next slot
|
||||
|
||||
@@ -12,6 +12,46 @@ import (
|
||||
"github.com/OffchainLabs/prysm/v6/proto/prysm/v1alpha1/attestation"
|
||||
)
|
||||
|
||||
// ConvertToAltair converts a Phase 0 beacon state to an Altair beacon state.
|
||||
func ConvertToAltair(state state.BeaconState) (state.BeaconState, error) {
|
||||
epoch := time.CurrentEpoch(state)
|
||||
|
||||
numValidators := state.NumValidators()
|
||||
s := ðpb.BeaconStateAltair{
|
||||
GenesisTime: uint64(state.GenesisTime().Unix()),
|
||||
GenesisValidatorsRoot: state.GenesisValidatorsRoot(),
|
||||
Slot: state.Slot(),
|
||||
Fork: ðpb.Fork{
|
||||
PreviousVersion: state.Fork().CurrentVersion,
|
||||
CurrentVersion: params.BeaconConfig().AltairForkVersion,
|
||||
Epoch: epoch,
|
||||
},
|
||||
LatestBlockHeader: state.LatestBlockHeader(),
|
||||
BlockRoots: state.BlockRoots(),
|
||||
StateRoots: state.StateRoots(),
|
||||
HistoricalRoots: state.HistoricalRoots(),
|
||||
Eth1Data: state.Eth1Data(),
|
||||
Eth1DataVotes: state.Eth1DataVotes(),
|
||||
Eth1DepositIndex: state.Eth1DepositIndex(),
|
||||
Validators: state.Validators(),
|
||||
Balances: state.Balances(),
|
||||
RandaoMixes: state.RandaoMixes(),
|
||||
Slashings: state.Slashings(),
|
||||
PreviousEpochParticipation: make([]byte, numValidators),
|
||||
CurrentEpochParticipation: make([]byte, numValidators),
|
||||
JustificationBits: state.JustificationBits(),
|
||||
PreviousJustifiedCheckpoint: state.PreviousJustifiedCheckpoint(),
|
||||
CurrentJustifiedCheckpoint: state.CurrentJustifiedCheckpoint(),
|
||||
FinalizedCheckpoint: state.FinalizedCheckpoint(),
|
||||
InactivityScores: make([]uint64, numValidators),
|
||||
}
|
||||
newState, err := state_native.InitializeFromProtoUnsafeAltair(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newState, nil
|
||||
}
|
||||
|
||||
// UpgradeToAltair updates input state to return the version Altair state.
|
||||
//
|
||||
// Spec code:
|
||||
@@ -64,39 +104,7 @@ import (
|
||||
// post.next_sync_committee = get_next_sync_committee(post)
|
||||
// return post
|
||||
func UpgradeToAltair(ctx context.Context, state state.BeaconState) (state.BeaconState, error) {
|
||||
epoch := time.CurrentEpoch(state)
|
||||
|
||||
numValidators := state.NumValidators()
|
||||
s := ðpb.BeaconStateAltair{
|
||||
GenesisTime: uint64(state.GenesisTime().Unix()),
|
||||
GenesisValidatorsRoot: state.GenesisValidatorsRoot(),
|
||||
Slot: state.Slot(),
|
||||
Fork: ðpb.Fork{
|
||||
PreviousVersion: state.Fork().CurrentVersion,
|
||||
CurrentVersion: params.BeaconConfig().AltairForkVersion,
|
||||
Epoch: epoch,
|
||||
},
|
||||
LatestBlockHeader: state.LatestBlockHeader(),
|
||||
BlockRoots: state.BlockRoots(),
|
||||
StateRoots: state.StateRoots(),
|
||||
HistoricalRoots: state.HistoricalRoots(),
|
||||
Eth1Data: state.Eth1Data(),
|
||||
Eth1DataVotes: state.Eth1DataVotes(),
|
||||
Eth1DepositIndex: state.Eth1DepositIndex(),
|
||||
Validators: state.Validators(),
|
||||
Balances: state.Balances(),
|
||||
RandaoMixes: state.RandaoMixes(),
|
||||
Slashings: state.Slashings(),
|
||||
PreviousEpochParticipation: make([]byte, numValidators),
|
||||
CurrentEpochParticipation: make([]byte, numValidators),
|
||||
JustificationBits: state.JustificationBits(),
|
||||
PreviousJustifiedCheckpoint: state.PreviousJustifiedCheckpoint(),
|
||||
CurrentJustifiedCheckpoint: state.CurrentJustifiedCheckpoint(),
|
||||
FinalizedCheckpoint: state.FinalizedCheckpoint(),
|
||||
InactivityScores: make([]uint64, numValidators),
|
||||
}
|
||||
|
||||
newState, err := state_native.InitializeFromProtoUnsafeAltair(s)
|
||||
newState, err := ConvertToAltair(state)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -184,45 +184,54 @@ func NewGenesisBlockForState(ctx context.Context, st state.BeaconState) (interfa
|
||||
})
|
||||
case *ethpb.BeaconStateElectra:
|
||||
return blocks.NewSignedBeaconBlock(ðpb.SignedBeaconBlockElectra{
|
||||
Block: ðpb.BeaconBlockElectra{
|
||||
ParentRoot: params.BeaconConfig().ZeroHash[:],
|
||||
StateRoot: root[:],
|
||||
Body: ðpb.BeaconBlockBodyElectra{
|
||||
RandaoReveal: make([]byte, 96),
|
||||
Eth1Data: ðpb.Eth1Data{
|
||||
DepositRoot: make([]byte, 32),
|
||||
BlockHash: make([]byte, 32),
|
||||
},
|
||||
Graffiti: make([]byte, 32),
|
||||
SyncAggregate: ðpb.SyncAggregate{
|
||||
SyncCommitteeBits: make([]byte, fieldparams.SyncCommitteeLength/8),
|
||||
SyncCommitteeSignature: make([]byte, fieldparams.BLSSignatureLength),
|
||||
},
|
||||
ExecutionPayload: &enginev1.ExecutionPayloadDeneb{
|
||||
ParentHash: make([]byte, 32),
|
||||
FeeRecipient: make([]byte, 20),
|
||||
StateRoot: make([]byte, 32),
|
||||
ReceiptsRoot: make([]byte, 32),
|
||||
LogsBloom: make([]byte, 256),
|
||||
PrevRandao: make([]byte, 32),
|
||||
ExtraData: make([]byte, 0),
|
||||
BaseFeePerGas: make([]byte, 32),
|
||||
BlockHash: make([]byte, 32),
|
||||
Transactions: make([][]byte, 0),
|
||||
Withdrawals: make([]*enginev1.Withdrawal, 0),
|
||||
},
|
||||
BlsToExecutionChanges: make([]*ethpb.SignedBLSToExecutionChange, 0),
|
||||
BlobKzgCommitments: make([][]byte, 0),
|
||||
ExecutionRequests: &enginev1.ExecutionRequests{
|
||||
Withdrawals: make([]*enginev1.WithdrawalRequest, 0),
|
||||
Deposits: make([]*enginev1.DepositRequest, 0),
|
||||
Consolidations: make([]*enginev1.ConsolidationRequest, 0),
|
||||
},
|
||||
},
|
||||
},
|
||||
Block: electraGenesisBlock(root),
|
||||
Signature: params.BeaconConfig().EmptySignature[:],
|
||||
})
|
||||
case *ethpb.BeaconStateFulu:
|
||||
return blocks.NewSignedBeaconBlock(ðpb.SignedBeaconBlockFulu{
|
||||
Block: electraGenesisBlock(root),
|
||||
Signature: params.BeaconConfig().EmptySignature[:],
|
||||
})
|
||||
default:
|
||||
return nil, ErrUnrecognizedState
|
||||
}
|
||||
}
|
||||
|
||||
func electraGenesisBlock(root [fieldparams.RootLength]byte) *ethpb.BeaconBlockElectra {
|
||||
return ðpb.BeaconBlockElectra{
|
||||
ParentRoot: params.BeaconConfig().ZeroHash[:],
|
||||
StateRoot: root[:],
|
||||
Body: ðpb.BeaconBlockBodyElectra{
|
||||
RandaoReveal: make([]byte, 96),
|
||||
Eth1Data: ðpb.Eth1Data{
|
||||
DepositRoot: make([]byte, 32),
|
||||
BlockHash: make([]byte, 32),
|
||||
},
|
||||
Graffiti: make([]byte, 32),
|
||||
SyncAggregate: ðpb.SyncAggregate{
|
||||
SyncCommitteeBits: make([]byte, fieldparams.SyncCommitteeLength/8),
|
||||
SyncCommitteeSignature: make([]byte, fieldparams.BLSSignatureLength),
|
||||
},
|
||||
ExecutionPayload: &enginev1.ExecutionPayloadDeneb{
|
||||
ParentHash: make([]byte, 32),
|
||||
FeeRecipient: make([]byte, 20),
|
||||
StateRoot: make([]byte, 32),
|
||||
ReceiptsRoot: make([]byte, 32),
|
||||
LogsBloom: make([]byte, 256),
|
||||
PrevRandao: make([]byte, 32),
|
||||
ExtraData: make([]byte, 0),
|
||||
BaseFeePerGas: make([]byte, 32),
|
||||
BlockHash: make([]byte, 32),
|
||||
Transactions: make([][]byte, 0),
|
||||
Withdrawals: make([]*enginev1.Withdrawal, 0),
|
||||
},
|
||||
BlsToExecutionChanges: make([]*ethpb.SignedBLSToExecutionChange, 0),
|
||||
BlobKzgCommitments: make([][]byte, 0),
|
||||
ExecutionRequests: &enginev1.ExecutionRequests{
|
||||
Withdrawals: make([]*enginev1.WithdrawalRequest, 0),
|
||||
Deposits: make([]*enginev1.DepositRequest, 0),
|
||||
Consolidations: make([]*enginev1.ConsolidationRequest, 0),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,129 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// ConvertToElectra converts a Deneb beacon state to an Electra beacon state. It does not perform any fork logic.
|
||||
func ConvertToElectra(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
|
||||
}
|
||||
excessBlobGas, err := payloadHeader.ExcessBlobGas()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
blobGasUsed, err := payloadHeader.BlobGasUsed()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s := ðpb.BeaconStateElectra{
|
||||
GenesisTime: uint64(beaconState.GenesisTime().Unix()),
|
||||
GenesisValidatorsRoot: beaconState.GenesisValidatorsRoot(),
|
||||
Slot: beaconState.Slot(),
|
||||
Fork: ðpb.Fork{
|
||||
PreviousVersion: beaconState.Fork().CurrentVersion,
|
||||
CurrentVersion: params.BeaconConfig().ElectraForkVersion,
|
||||
Epoch: time.CurrentEpoch(beaconState),
|
||||
},
|
||||
LatestBlockHeader: beaconState.LatestBlockHeader(),
|
||||
BlockRoots: beaconState.BlockRoots(),
|
||||
StateRoots: beaconState.StateRoots(),
|
||||
HistoricalRoots: beaconState.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.ExecutionPayloadHeaderDeneb{
|
||||
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,
|
||||
EarliestConsolidationEpoch: helpers.ActivationExitEpoch(slots.ToEpoch(beaconState.Slot())),
|
||||
PendingDeposits: make([]*ethpb.PendingDeposit, 0),
|
||||
PendingPartialWithdrawals: make([]*ethpb.PendingPartialWithdrawal, 0),
|
||||
PendingConsolidations: make([]*ethpb.PendingConsolidation, 0),
|
||||
}
|
||||
|
||||
// need to cast the beaconState to use in helper functions
|
||||
post, err := state_native.InitializeFromProtoUnsafeElectra(s)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to initialize post electra beaconState")
|
||||
}
|
||||
return post, nil
|
||||
}
|
||||
|
||||
// UpgradeToElectra updates inputs a generic state to return the version Electra state.
|
||||
//
|
||||
// nolint:dupword
|
||||
|
||||
@@ -7,6 +7,7 @@ go_library(
|
||||
visibility = [
|
||||
"//beacon-chain:__subpackages__",
|
||||
"//cmd/prysmctl/testnet:__pkg__",
|
||||
"//consensus-types/hdiff:__subpackages__",
|
||||
"//testing/spectest:__subpackages__",
|
||||
"//validator/client:__pkg__",
|
||||
],
|
||||
|
||||
@@ -17,6 +17,35 @@ import (
|
||||
// UpgradeToFulu updates inputs a generic state to return the version Fulu state.
|
||||
// https://github.com/ethereum/consensus-specs/blob/master/specs/fulu/fork.md#upgrading-the-state
|
||||
func UpgradeToFulu(ctx context.Context, beaconState state.BeaconState) (state.BeaconState, error) {
|
||||
s, err := convertToFuluPB(beaconState)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not convert to fulu")
|
||||
}
|
||||
proposerLookahead, err := helpers.InitializeProposerLookahead(ctx, beaconState, slots.ToEpoch(beaconState.Slot()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.ProposerLookahead = proposerLookahead
|
||||
post, err := state_native.InitializeFromProtoUnsafeFulu(s)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to initialize post fulu beaconState")
|
||||
}
|
||||
return post, nil
|
||||
}
|
||||
|
||||
func ConvertToFulu(beaconState state.BeaconState) (state.BeaconState, error) {
|
||||
s, err := convertToFuluPB(beaconState)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not convert to fulu pb")
|
||||
}
|
||||
post, err := state_native.InitializeFromProtoUnsafeFulu(s)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to initialize post fulu beaconState")
|
||||
}
|
||||
return post, nil
|
||||
}
|
||||
|
||||
func convertToFuluPB(beaconState state.BeaconState) (*ethpb.BeaconStateFulu, error) {
|
||||
currentSyncCommittee, err := beaconState.CurrentSyncCommittee()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -105,11 +134,6 @@ func UpgradeToFulu(ctx context.Context, beaconState state.BeaconState) (state.Be
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
proposerLookahead, err := helpers.InitializeProposerLookahead(ctx, beaconState, slots.ToEpoch(beaconState.Slot()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s := ðpb.BeaconStateFulu{
|
||||
GenesisTime: uint64(beaconState.GenesisTime().Unix()),
|
||||
GenesisValidatorsRoot: beaconState.GenesisValidatorsRoot(),
|
||||
@@ -171,14 +195,6 @@ func UpgradeToFulu(ctx context.Context, beaconState state.BeaconState) (state.Be
|
||||
PendingDeposits: pendingDeposits,
|
||||
PendingPartialWithdrawals: pendingPartialWithdrawals,
|
||||
PendingConsolidations: pendingConsolidations,
|
||||
ProposerLookahead: proposerLookahead,
|
||||
}
|
||||
|
||||
// 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
|
||||
return s, nil
|
||||
}
|
||||
|
||||
17
beacon-chain/core/gloas/BUILD.bazel
Normal file
17
beacon-chain/core/gloas/BUILD.bazel
Normal file
@@ -0,0 +1,17 @@
|
||||
load("@prysm//tools/go:def.bzl", "go_library")
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["upgrade.go"],
|
||||
importpath = "github.com/OffchainLabs/prysm/v6/beacon-chain/core/gloas",
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//beacon-chain/core/time:go_default_library",
|
||||
"//beacon-chain/state:go_default_library",
|
||||
"//beacon-chain/state/state-native:go_default_library",
|
||||
"//config/fieldparams:go_default_library",
|
||||
"//config/params:go_default_library",
|
||||
"//proto/prysm/v1alpha1:go_default_library",
|
||||
"@com_github_pkg_errors//:go_default_library",
|
||||
],
|
||||
)
|
||||
156
beacon-chain/core/gloas/upgrade.go
Normal file
156
beacon-chain/core/gloas/upgrade.go
Normal file
@@ -0,0 +1,156 @@
|
||||
package gloas
|
||||
|
||||
import (
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/core/time"
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/state"
|
||||
state_native "github.com/OffchainLabs/prysm/v6/beacon-chain/state/state-native"
|
||||
fieldparams "github.com/OffchainLabs/prysm/v6/config/fieldparams"
|
||||
"github.com/OffchainLabs/prysm/v6/config/params"
|
||||
ethpb "github.com/OffchainLabs/prysm/v6/proto/prysm/v1alpha1"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func ConvertToGloas(beaconState state.BeaconState) (state.BeaconState, error) {
|
||||
s, err := convertToGloasPB(beaconState)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not convert to gloas pb")
|
||||
}
|
||||
// Initialize Gloas-specific fields
|
||||
s.ExecutionPayloadAvailability = make([]byte, fieldparams.BlockRootsLength/8)
|
||||
s.BuilderPendingPayments = make([]*ethpb.BuilderPendingPayment, 2*fieldparams.SlotsPerEpoch)
|
||||
s.BuilderPendingWithdrawals = make([]*ethpb.BuilderPendingWithdrawal, 0)
|
||||
s.LatestBlockHash = make([]byte, 32)
|
||||
s.LatestWithdrawalsRoot = make([]byte, 32)
|
||||
|
||||
post, err := state_native.InitializeFromProtoUnsafeGloas(s)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to initialize post gloas beaconState")
|
||||
}
|
||||
return post, nil
|
||||
}
|
||||
|
||||
func convertToGloasPB(beaconState state.BeaconState) (*ethpb.BeaconStateGloas, 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
|
||||
}
|
||||
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
|
||||
}
|
||||
depositRequestsStartIndex, err := beaconState.DepositRequestsStartIndex()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
depositBalanceToConsume, err := beaconState.DepositBalanceToConsume()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
exitBalanceToConsume, err := beaconState.ExitBalanceToConsume()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
earliestExitEpoch, err := beaconState.EarliestExitEpoch()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
consolidationBalanceToConsume, err := beaconState.ConsolidationBalanceToConsume()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
earliestConsolidationEpoch, err := beaconState.EarliestConsolidationEpoch()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pendingDeposits, err := beaconState.PendingDeposits()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pendingPartialWithdrawals, err := beaconState.PendingPartialWithdrawals()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pendingConsolidations, err := beaconState.PendingConsolidations()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
proposerLookaheadIndices, err := beaconState.ProposerLookahead()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
proposerLookahead := make([]uint64, len(proposerLookaheadIndices))
|
||||
for i, idx := range proposerLookaheadIndices {
|
||||
proposerLookahead[i] = uint64(idx)
|
||||
}
|
||||
|
||||
s := ðpb.BeaconStateGloas{
|
||||
GenesisTime: uint64(beaconState.GenesisTime().Unix()),
|
||||
GenesisValidatorsRoot: beaconState.GenesisValidatorsRoot(),
|
||||
Slot: beaconState.Slot(),
|
||||
Fork: ðpb.Fork{
|
||||
PreviousVersion: beaconState.Fork().CurrentVersion,
|
||||
CurrentVersion: params.BeaconConfig().GloasForkVersion,
|
||||
Epoch: time.CurrentEpoch(beaconState),
|
||||
},
|
||||
LatestBlockHeader: beaconState.LatestBlockHeader(),
|
||||
BlockRoots: beaconState.BlockRoots(),
|
||||
StateRoots: beaconState.StateRoots(),
|
||||
HistoricalRoots: beaconState.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,
|
||||
LatestExecutionPayloadBid: nil, // Not present in pre-Gloas states
|
||||
NextWithdrawalIndex: wi,
|
||||
NextWithdrawalValidatorIndex: vi,
|
||||
HistoricalSummaries: summaries,
|
||||
|
||||
DepositRequestsStartIndex: depositRequestsStartIndex,
|
||||
DepositBalanceToConsume: depositBalanceToConsume,
|
||||
ExitBalanceToConsume: exitBalanceToConsume,
|
||||
EarliestExitEpoch: earliestExitEpoch,
|
||||
ConsolidationBalanceToConsume: consolidationBalanceToConsume,
|
||||
EarliestConsolidationEpoch: earliestConsolidationEpoch,
|
||||
PendingDeposits: pendingDeposits,
|
||||
PendingPartialWithdrawals: pendingPartialWithdrawals,
|
||||
PendingConsolidations: pendingConsolidations,
|
||||
ProposerLookahead: proposerLookahead,
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
@@ -10,6 +10,7 @@ go_library(
|
||||
"legacy.go",
|
||||
"metrics.go",
|
||||
"randao.go",
|
||||
"ranges.go",
|
||||
"rewards_penalties.go",
|
||||
"shuffle.go",
|
||||
"sync_committee.go",
|
||||
@@ -56,6 +57,7 @@ go_test(
|
||||
"private_access_fuzz_noop_test.go", # keep
|
||||
"private_access_test.go",
|
||||
"randao_test.go",
|
||||
"ranges_test.go",
|
||||
"rewards_penalties_test.go",
|
||||
"shuffle_test.go",
|
||||
"sync_committee_test.go",
|
||||
|
||||
62
beacon-chain/core/helpers/ranges.go
Normal file
62
beacon-chain/core/helpers/ranges.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
)
|
||||
|
||||
// SortedSliceFromMap takes a map with uint64 keys and returns a sorted slice of the keys.
|
||||
func SortedSliceFromMap(toSort map[uint64]bool) []uint64 {
|
||||
slice := make([]uint64, 0, len(toSort))
|
||||
for key := range toSort {
|
||||
slice = append(slice, key)
|
||||
}
|
||||
|
||||
slices.Sort(slice)
|
||||
return slice
|
||||
}
|
||||
|
||||
// PrettySlice returns a pretty string representation of a sorted slice of uint64.
|
||||
// `sortedSlice` must be sorted in ascending order.
|
||||
// Example: [1,2,3,5,6,7,8,10] -> "1-3,5-8,10"
|
||||
func PrettySlice(sortedSlice []uint64) string {
|
||||
if len(sortedSlice) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
var result string
|
||||
start := sortedSlice[0]
|
||||
end := sortedSlice[0]
|
||||
|
||||
for i := 1; i < len(sortedSlice); i++ {
|
||||
if sortedSlice[i] == end+1 {
|
||||
end = sortedSlice[i]
|
||||
continue
|
||||
}
|
||||
|
||||
if start == end {
|
||||
result += fmt.Sprintf("%d,", start)
|
||||
start = sortedSlice[i]
|
||||
end = sortedSlice[i]
|
||||
continue
|
||||
}
|
||||
|
||||
result += fmt.Sprintf("%d-%d,", start, end)
|
||||
start = sortedSlice[i]
|
||||
end = sortedSlice[i]
|
||||
}
|
||||
|
||||
if start == end {
|
||||
result += fmt.Sprintf("%d", start)
|
||||
return result
|
||||
}
|
||||
|
||||
result += fmt.Sprintf("%d-%d", start, end)
|
||||
return result
|
||||
}
|
||||
|
||||
// SortedPrettySliceFromMap combines SortedSliceFromMap and PrettySlice to return a pretty string representation of the keys in a map.
|
||||
func SortedPrettySliceFromMap(toSort map[uint64]bool) string {
|
||||
sorted := SortedSliceFromMap(toSort)
|
||||
return PrettySlice(sorted)
|
||||
}
|
||||
64
beacon-chain/core/helpers/ranges_test.go
Normal file
64
beacon-chain/core/helpers/ranges_test.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package helpers_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/core/helpers"
|
||||
"github.com/OffchainLabs/prysm/v6/testing/require"
|
||||
)
|
||||
|
||||
func TestSortedSliceFromMap(t *testing.T) {
|
||||
input := map[uint64]bool{5: true, 3: true, 8: true, 1: true}
|
||||
expected := []uint64{1, 3, 5, 8}
|
||||
|
||||
actual := helpers.SortedSliceFromMap(input)
|
||||
require.Equal(t, len(expected), len(actual))
|
||||
|
||||
for i := range expected {
|
||||
require.Equal(t, expected[i], actual[i])
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrettySlice(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input []uint64
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "empty slice",
|
||||
input: []uint64{},
|
||||
expected: "",
|
||||
},
|
||||
{
|
||||
name: "only distinct elements",
|
||||
input: []uint64{1, 3, 5, 7, 9},
|
||||
expected: "1,3,5,7,9",
|
||||
},
|
||||
{
|
||||
name: "single range",
|
||||
input: []uint64{1, 2, 3, 4, 5},
|
||||
expected: "1-5",
|
||||
},
|
||||
{
|
||||
name: "multiple ranges and distinct elements",
|
||||
input: []uint64{1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 14},
|
||||
expected: "1-3,5-8,10,12-14",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
actual := helpers.PrettySlice(tt.input)
|
||||
require.Equal(t, tt.expected, actual)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSortedPrettySliceFromMap(t *testing.T) {
|
||||
input := map[uint64]bool{5: true, 7: true, 8: true, 10: true}
|
||||
expected := "5,7-8,10"
|
||||
|
||||
actual := helpers.SortedPrettySliceFromMap(input)
|
||||
require.Equal(t, expected, actual)
|
||||
}
|
||||
@@ -109,15 +109,14 @@ func DataColumnSidecars(rows []kzg.CellsAndProofs, src ConstructionPopulator) ([
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "rotate cells and proofs")
|
||||
}
|
||||
info, err := src.extract()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "extract block info")
|
||||
}
|
||||
|
||||
maxIdx := params.BeaconConfig().NumberOfColumns
|
||||
roSidecars := make([]blocks.RODataColumn, 0, maxIdx)
|
||||
for idx := range maxIdx {
|
||||
info, err := src.extract()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "extract block info")
|
||||
}
|
||||
|
||||
sidecar := ðpb.DataColumnSidecar{
|
||||
Index: idx,
|
||||
Column: cells[idx],
|
||||
|
||||
@@ -122,18 +122,18 @@ type BlobStorage struct {
|
||||
func (bs *BlobStorage) WarmCache() {
|
||||
start := time.Now()
|
||||
if bs.layoutName == LayoutNameFlat {
|
||||
log.Info("Blob filesystem cache warm-up started. This may take a few minutes.")
|
||||
log.Info("Blob filesystem cache warm-up started. This may take a few minutes")
|
||||
} else {
|
||||
log.Info("Blob filesystem cache warm-up started.")
|
||||
log.Info("Blob filesystem cache warm-up started")
|
||||
}
|
||||
|
||||
if err := warmCache(bs.layout, bs.cache); err != nil {
|
||||
log.WithError(err).Error("Error encountered while warming up blob filesystem cache.")
|
||||
log.WithError(err).Error("Error encountered while warming up blob filesystem cache")
|
||||
}
|
||||
if err := bs.migrateLayouts(); err != nil {
|
||||
log.WithError(err).Error("Error encountered while migrating blob storage.")
|
||||
log.WithError(err).Error("Error encountered while migrating blob storage")
|
||||
}
|
||||
log.WithField("elapsed", time.Since(start)).Info("Blob filesystem cache warm-up complete.")
|
||||
log.WithField("elapsed", time.Since(start)).Info("Blob filesystem cache warm-up complete")
|
||||
}
|
||||
|
||||
// If any blob storage directories are found for layouts besides the configured layout, migrate them.
|
||||
|
||||
@@ -381,6 +381,7 @@ func (s *Service) internalBroadcastDataColumnSidecar(
|
||||
"timeSinceSlotStart": time.Since(slotStartTime),
|
||||
"root": fmt.Sprintf("%#x", dataColumnSidecar.BlockRoot()),
|
||||
"columnSubnet": columnSubnet,
|
||||
"blobCount": len(dataColumnSidecar.Column),
|
||||
}).Debug("Broadcasted data column sidecar")
|
||||
|
||||
// Increase the number of successful broadcasts.
|
||||
|
||||
@@ -10,6 +10,8 @@ import (
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var errNoCustodyInfo = errors.New("no custody info available")
|
||||
|
||||
var _ CustodyManager = (*Service)(nil)
|
||||
|
||||
// EarliestAvailableSlot returns the earliest available slot.
|
||||
@@ -30,7 +32,7 @@ func (s *Service) CustodyGroupCount() (uint64, error) {
|
||||
defer s.custodyInfoLock.Unlock()
|
||||
|
||||
if s.custodyInfo == nil {
|
||||
return 0, errors.New("no custody info available")
|
||||
return 0, errNoCustodyInfo
|
||||
}
|
||||
|
||||
return s.custodyInfo.groupCount, nil
|
||||
|
||||
@@ -79,7 +79,7 @@ func (quicProtocol) ENRKey() string { return quickProtocolEnrKey }
|
||||
func newListener(listenerCreator func() (*discover.UDPv5, error)) (*listenerWrapper, error) {
|
||||
rawListener, err := listenerCreator()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not create new listener")
|
||||
return nil, errors.Wrap(err, "create new listener")
|
||||
}
|
||||
return &listenerWrapper{
|
||||
listener: rawListener,
|
||||
@@ -536,7 +536,7 @@ func (s *Service) createListener(
|
||||
int(s.cfg.QUICPort),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not create local node")
|
||||
return nil, errors.Wrap(err, "create local node")
|
||||
}
|
||||
|
||||
bootNodes := make([]*enode.Node, 0, len(s.cfg.Discv5BootStrapAddrs))
|
||||
@@ -604,13 +604,27 @@ func (s *Service) createLocalNode(
|
||||
localNode = initializeSyncCommSubnets(localNode)
|
||||
|
||||
if params.FuluEnabled() {
|
||||
custodyGroupCount, err := s.CustodyGroupCount()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not retrieve custody group count")
|
||||
}
|
||||
// TODO: Replace this quick fix with a proper synchronization scheme (chan?)
|
||||
const delay = 1 * time.Second
|
||||
|
||||
custodyGroupCountEntry := peerdas.Cgc(custodyGroupCount)
|
||||
localNode.Set(custodyGroupCountEntry)
|
||||
var custodyGroupCount uint64
|
||||
|
||||
err := errNoCustodyInfo
|
||||
for errors.Is(err, errNoCustodyInfo) {
|
||||
custodyGroupCount, err = s.CustodyGroupCount()
|
||||
if errors.Is(err, errNoCustodyInfo) {
|
||||
log.WithField("delay", delay).Debug("No custody info available yet, retrying later")
|
||||
time.Sleep(delay)
|
||||
continue
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "retrieve custody group count")
|
||||
}
|
||||
|
||||
custodyGroupCountEntry := peerdas.Cgc(custodyGroupCount)
|
||||
localNode.Set(custodyGroupCountEntry)
|
||||
}
|
||||
}
|
||||
|
||||
if s.cfg != nil && s.cfg.HostAddress != "" {
|
||||
@@ -652,7 +666,7 @@ func (s *Service) startDiscoveryV5(
|
||||
}
|
||||
wrappedListener, err := newListener(createListener)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not create listener")
|
||||
return nil, errors.Wrap(err, "create listener")
|
||||
}
|
||||
record := wrappedListener.Self()
|
||||
|
||||
|
||||
@@ -106,7 +106,7 @@ func (s *Service) endpoints(
|
||||
}
|
||||
|
||||
if enableDebug {
|
||||
endpoints = append(endpoints, s.debugEndpoints(stater)...)
|
||||
endpoints = append(endpoints, s.debugEndpoints(stater, blocker)...)
|
||||
}
|
||||
|
||||
return endpoints
|
||||
@@ -1097,7 +1097,7 @@ func (s *Service) lightClientEndpoints() []endpoint {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) debugEndpoints(stater lookup.Stater) []endpoint {
|
||||
func (s *Service) debugEndpoints(stater lookup.Stater, blocker lookup.Blocker) []endpoint {
|
||||
server := &debug.Server{
|
||||
BeaconDB: s.cfg.BeaconDB,
|
||||
HeadFetcher: s.cfg.HeadFetcher,
|
||||
@@ -1107,6 +1107,8 @@ func (s *Service) debugEndpoints(stater lookup.Stater) []endpoint {
|
||||
ForkchoiceFetcher: s.cfg.ForkchoiceFetcher,
|
||||
FinalizationFetcher: s.cfg.FinalizationFetcher,
|
||||
ChainInfoFetcher: s.cfg.ChainInfoFetcher,
|
||||
GenesisTimeFetcher: s.cfg.GenesisTimeFetcher,
|
||||
Blocker: blocker,
|
||||
}
|
||||
|
||||
const namespace = "debug"
|
||||
@@ -1141,6 +1143,16 @@ func (s *Service) debugEndpoints(stater lookup.Stater) []endpoint {
|
||||
handler: server.GetForkChoice,
|
||||
methods: []string{http.MethodGet},
|
||||
},
|
||||
{
|
||||
template: "/eth/v1/debug/beacon/data_column_sidecars/{block_id}",
|
||||
name: namespace + ".GetDataColumnSidecars",
|
||||
middleware: []middleware.Middleware{
|
||||
middleware.AcceptHeaderHandler([]string{api.JsonMediaType, api.OctetStreamMediaType}),
|
||||
middleware.AcceptEncodingHeaderHandler(),
|
||||
},
|
||||
handler: server.DataColumnSidecars,
|
||||
methods: []string{http.MethodGet},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -80,9 +80,10 @@ func Test_endpoints(t *testing.T) {
|
||||
}
|
||||
|
||||
debugRoutes := map[string][]string{
|
||||
"/eth/v2/debug/beacon/states/{state_id}": {http.MethodGet},
|
||||
"/eth/v2/debug/beacon/heads": {http.MethodGet},
|
||||
"/eth/v1/debug/fork_choice": {http.MethodGet},
|
||||
"/eth/v2/debug/beacon/states/{state_id}": {http.MethodGet},
|
||||
"/eth/v2/debug/beacon/heads": {http.MethodGet},
|
||||
"/eth/v1/debug/fork_choice": {http.MethodGet},
|
||||
"/eth/v1/debug/beacon/data_column_sidecars/{block_id}": {http.MethodGet},
|
||||
}
|
||||
|
||||
eventsRoutes := map[string][]string{
|
||||
|
||||
@@ -109,10 +109,10 @@ func (s *Server) GetRandao(w http.ResponseWriter, r *http.Request) {
|
||||
// future epochs and epochs too far back are not supported.
|
||||
randaoEpochLowerBound := uint64(0)
|
||||
// Lower bound should not underflow.
|
||||
if uint64(stEpoch) > uint64(st.RandaoMixesLength()) {
|
||||
randaoEpochLowerBound = uint64(stEpoch) - uint64(st.RandaoMixesLength())
|
||||
if uint64(stEpoch) >= uint64(st.RandaoMixesLength()) {
|
||||
randaoEpochLowerBound = uint64(stEpoch) - uint64(st.RandaoMixesLength()) + 1
|
||||
}
|
||||
if epoch > stEpoch || uint64(epoch) < randaoEpochLowerBound+1 {
|
||||
if epoch > stEpoch || (uint64(epoch) < randaoEpochLowerBound) {
|
||||
httputil.HandleError(w, "Epoch is out of range for the randao mixes of the state", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -192,8 +192,14 @@ func TestGetRandao(t *testing.T) {
|
||||
assert.Equal(t, hexutil.Encode(mixOld[:]), resp.Data.Randao)
|
||||
})
|
||||
t.Run("head state below `EpochsPerHistoricalVector`", func(t *testing.T) {
|
||||
s.Stater = &testutil.MockStater{
|
||||
BeaconState: headSt,
|
||||
s := &Server{
|
||||
Stater: &testutil.MockStater{
|
||||
BeaconState: headSt,
|
||||
},
|
||||
HeadFetcher: chainService,
|
||||
OptimisticModeFetcher: chainService,
|
||||
FinalizationFetcher: chainService,
|
||||
BeaconDB: db,
|
||||
}
|
||||
|
||||
request := httptest.NewRequest(http.MethodGet, "http://example.com//eth/v1/beacon/states/{state_id}/randao", nil)
|
||||
@@ -303,6 +309,74 @@ func TestGetRandao(t *testing.T) {
|
||||
require.NoError(t, json.Unmarshal(writer.Body.Bytes(), resp))
|
||||
assert.DeepEqual(t, true, resp.Finalized)
|
||||
})
|
||||
t.Run("early epoch scenario - epoch 0 from state at epoch (EpochsPerHistoricalVector - 1)", func(t *testing.T) {
|
||||
// Create a state at early epoch
|
||||
earlyEpochState, err := util.NewBeaconState()
|
||||
require.NoError(t, err)
|
||||
earlyEpoch := params.BeaconConfig().EpochsPerHistoricalVector - 1
|
||||
require.NoError(t, earlyEpochState.SetSlot(params.BeaconConfig().SlotsPerEpoch*primitives.Slot(earlyEpoch)))
|
||||
|
||||
// Set up RANDAO mix for epoch 0
|
||||
// In real networks, this would be the ETH1 block hash used for genesis
|
||||
epoch0Randao := bytesutil.ToBytes32([]byte("epoch0"))
|
||||
require.NoError(t, earlyEpochState.UpdateRandaoMixesAtIndex(0, epoch0Randao))
|
||||
|
||||
earlyServer := &Server{
|
||||
Stater: &testutil.MockStater{
|
||||
BeaconState: earlyEpochState,
|
||||
},
|
||||
HeadFetcher: &chainMock.ChainService{},
|
||||
OptimisticModeFetcher: &chainMock.ChainService{},
|
||||
FinalizationFetcher: &chainMock.ChainService{},
|
||||
}
|
||||
|
||||
// Query epoch 0 from state at epoch (EpochsPerHistoricalVector - 1) - should succeed
|
||||
request := httptest.NewRequest(http.MethodGet, "http://example.com//eth/v1/beacon/states/{state_id}/randao?epoch=0", nil)
|
||||
request.SetPathValue("state_id", "head")
|
||||
writer := httptest.NewRecorder()
|
||||
writer.Body = &bytes.Buffer{}
|
||||
|
||||
earlyServer.GetRandao(writer, request)
|
||||
require.Equal(t, http.StatusOK, writer.Code, "Early epoch queries should succeed when within bounds")
|
||||
|
||||
resp := &structs.GetRandaoResponse{}
|
||||
require.NoError(t, json.Unmarshal(writer.Body.Bytes(), resp))
|
||||
assert.Equal(t, hexutil.Encode(epoch0Randao[:]), resp.Data.Randao)
|
||||
})
|
||||
t.Run("early epoch scenario - epoch 0 from state at epoch EpochsPerHistoricalVector", func(t *testing.T) {
|
||||
// Create a state at early epoch
|
||||
earlyEpochState, err := util.NewBeaconState()
|
||||
require.NoError(t, err)
|
||||
earlyEpoch := params.BeaconConfig().EpochsPerHistoricalVector
|
||||
require.NoError(t, earlyEpochState.SetSlot(params.BeaconConfig().SlotsPerEpoch*primitives.Slot(earlyEpoch)))
|
||||
|
||||
// Set up RANDAO mix for epoch 0
|
||||
// In real networks, this would be the ETH1 block hash used for genesis
|
||||
epoch0Randao := bytesutil.ToBytes32([]byte("epoch0"))
|
||||
require.NoError(t, earlyEpochState.UpdateRandaoMixesAtIndex(0, epoch0Randao))
|
||||
|
||||
earlyServer := &Server{
|
||||
Stater: &testutil.MockStater{
|
||||
BeaconState: earlyEpochState,
|
||||
},
|
||||
HeadFetcher: &chainMock.ChainService{},
|
||||
OptimisticModeFetcher: &chainMock.ChainService{},
|
||||
FinalizationFetcher: &chainMock.ChainService{},
|
||||
}
|
||||
|
||||
// Query epoch 0 from state at epoch EpochsPerHistoricalVector - should fail
|
||||
request := httptest.NewRequest(http.MethodGet, "http://example.com//eth/v1/beacon/states/{state_id}/randao?epoch=0", nil)
|
||||
request.SetPathValue("state_id", "head")
|
||||
writer := httptest.NewRecorder()
|
||||
writer.Body = &bytes.Buffer{}
|
||||
|
||||
earlyServer.GetRandao(writer, request)
|
||||
require.Equal(t, http.StatusBadRequest, writer.Code)
|
||||
e := &httputil.DefaultJsonError{}
|
||||
require.NoError(t, json.Unmarshal(writer.Body.Bytes(), e))
|
||||
assert.Equal(t, http.StatusBadRequest, e.Code)
|
||||
require.StringContains(t, "Epoch is out of range for the randao mixes of the state", e.Message)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_currentCommitteeIndicesFromState(t *testing.T) {
|
||||
|
||||
@@ -71,7 +71,7 @@ func TestBlobs(t *testing.T) {
|
||||
e := &httputil.DefaultJsonError{}
|
||||
require.NoError(t, json.Unmarshal(writer.Body.Bytes(), e))
|
||||
assert.Equal(t, http.StatusBadRequest, e.Code)
|
||||
assert.StringContains(t, "blobs are not supported for Phase 0 fork", e.Message)
|
||||
assert.StringContains(t, "not supported for Phase 0 fork", e.Message)
|
||||
})
|
||||
t.Run("head", func(t *testing.T) {
|
||||
u := "http://foo.example/head"
|
||||
@@ -336,11 +336,23 @@ func TestBlobs(t *testing.T) {
|
||||
require.Equal(t, false, resp.Finalized)
|
||||
})
|
||||
t.Run("slot before Deneb fork", func(t *testing.T) {
|
||||
// Create and save a pre-Deneb block at slot 31
|
||||
predenebBlock := util.NewBeaconBlock()
|
||||
predenebBlock.Block.Slot = 31
|
||||
util.SaveBlock(t, t.Context(), db, predenebBlock)
|
||||
|
||||
u := "http://foo.example/31"
|
||||
request := httptest.NewRequest("GET", u, nil)
|
||||
writer := httptest.NewRecorder()
|
||||
writer.Body = &bytes.Buffer{}
|
||||
s.Blocker = &lookup.BeaconDbBlocker{}
|
||||
s.Blocker = &lookup.BeaconDbBlocker{
|
||||
ChainInfoFetcher: &mockChain.ChainService{},
|
||||
GenesisTimeFetcher: &testutil.MockGenesisTimeFetcher{
|
||||
Genesis: time.Now(),
|
||||
},
|
||||
BeaconDB: db,
|
||||
BlobStorage: bs,
|
||||
}
|
||||
|
||||
s.Blobs(writer, request)
|
||||
|
||||
@@ -348,7 +360,7 @@ func TestBlobs(t *testing.T) {
|
||||
e := &httputil.DefaultJsonError{}
|
||||
require.NoError(t, json.Unmarshal(writer.Body.Bytes(), e))
|
||||
assert.Equal(t, http.StatusBadRequest, e.Code)
|
||||
assert.StringContains(t, "blobs are not supported before Deneb fork", e.Message)
|
||||
assert.StringContains(t, "not supported before", e.Message)
|
||||
})
|
||||
t.Run("malformed block ID", func(t *testing.T) {
|
||||
u := "http://foo.example/foo"
|
||||
@@ -609,7 +621,7 @@ func TestGetBlobs(t *testing.T) {
|
||||
e := &httputil.DefaultJsonError{}
|
||||
require.NoError(t, json.Unmarshal(writer.Body.Bytes(), e))
|
||||
assert.Equal(t, http.StatusBadRequest, e.Code)
|
||||
assert.StringContains(t, "blobs are not supported for Phase 0 fork", e.Message)
|
||||
assert.StringContains(t, "not supported for Phase 0 fork", e.Message)
|
||||
})
|
||||
t.Run("head", func(t *testing.T) {
|
||||
u := "http://foo.example/head"
|
||||
@@ -808,11 +820,22 @@ func TestGetBlobs(t *testing.T) {
|
||||
require.Equal(t, false, resp.Finalized)
|
||||
})
|
||||
t.Run("slot before Deneb fork", func(t *testing.T) {
|
||||
// Create and save a pre-Deneb block at slot 31
|
||||
predenebBlock := util.NewBeaconBlock()
|
||||
predenebBlock.Block.Slot = 31
|
||||
util.SaveBlock(t, t.Context(), db, predenebBlock)
|
||||
|
||||
u := "http://foo.example/31"
|
||||
request := httptest.NewRequest("GET", u, nil)
|
||||
writer := httptest.NewRecorder()
|
||||
writer.Body = &bytes.Buffer{}
|
||||
s.Blocker = &lookup.BeaconDbBlocker{}
|
||||
s.Blocker = &lookup.BeaconDbBlocker{
|
||||
BeaconDB: db,
|
||||
ChainInfoFetcher: &mockChain.ChainService{},
|
||||
GenesisTimeFetcher: &testutil.MockGenesisTimeFetcher{
|
||||
Genesis: time.Now(),
|
||||
},
|
||||
}
|
||||
|
||||
s.GetBlobs(writer, request)
|
||||
|
||||
@@ -820,7 +843,7 @@ func TestGetBlobs(t *testing.T) {
|
||||
e := &httputil.DefaultJsonError{}
|
||||
require.NoError(t, json.Unmarshal(writer.Body.Bytes(), e))
|
||||
assert.Equal(t, http.StatusBadRequest, e.Code)
|
||||
assert.StringContains(t, "blobs are not supported before Deneb fork", e.Message)
|
||||
assert.StringContains(t, "not supported before", e.Message)
|
||||
})
|
||||
t.Run("malformed block ID", func(t *testing.T) {
|
||||
u := "http://foo.example/foo"
|
||||
|
||||
@@ -13,13 +13,19 @@ go_library(
|
||||
"//api/server/structs:go_default_library",
|
||||
"//beacon-chain/blockchain:go_default_library",
|
||||
"//beacon-chain/db:go_default_library",
|
||||
"//beacon-chain/rpc/core:go_default_library",
|
||||
"//beacon-chain/rpc/eth/helpers:go_default_library",
|
||||
"//beacon-chain/rpc/eth/shared:go_default_library",
|
||||
"//beacon-chain/rpc/lookup:go_default_library",
|
||||
"//config/params:go_default_library",
|
||||
"//consensus-types/blocks:go_default_library",
|
||||
"//consensus-types/primitives:go_default_library",
|
||||
"//monitoring/tracing/trace:go_default_library",
|
||||
"//network/httputil:go_default_library",
|
||||
"//proto/prysm/v1alpha1:go_default_library",
|
||||
"//runtime/version:go_default_library",
|
||||
"@com_github_ethereum_go_ethereum//common/hexutil:go_default_library",
|
||||
"@com_github_pkg_errors//:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -34,9 +40,13 @@ go_test(
|
||||
"//beacon-chain/db/testing:go_default_library",
|
||||
"//beacon-chain/forkchoice/doubly-linked-tree:go_default_library",
|
||||
"//beacon-chain/forkchoice/types:go_default_library",
|
||||
"//beacon-chain/rpc/core:go_default_library",
|
||||
"//beacon-chain/rpc/testutil:go_default_library",
|
||||
"//config/params:go_default_library",
|
||||
"//consensus-types/blocks:go_default_library",
|
||||
"//consensus-types/primitives:go_default_library",
|
||||
"//encoding/bytesutil:go_default_library",
|
||||
"//proto/prysm/v1alpha1:go_default_library",
|
||||
"//runtime/version:go_default_library",
|
||||
"//testing/assert:go_default_library",
|
||||
"//testing/require:go_default_library",
|
||||
|
||||
@@ -4,19 +4,31 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/OffchainLabs/prysm/v6/api"
|
||||
"github.com/OffchainLabs/prysm/v6/api/server/structs"
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/rpc/core"
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/rpc/eth/helpers"
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/rpc/eth/shared"
|
||||
"github.com/OffchainLabs/prysm/v6/config/params"
|
||||
"github.com/OffchainLabs/prysm/v6/consensus-types/blocks"
|
||||
"github.com/OffchainLabs/prysm/v6/consensus-types/primitives"
|
||||
"github.com/OffchainLabs/prysm/v6/monitoring/tracing/trace"
|
||||
"github.com/OffchainLabs/prysm/v6/network/httputil"
|
||||
ethpb "github.com/OffchainLabs/prysm/v6/proto/prysm/v1alpha1"
|
||||
"github.com/OffchainLabs/prysm/v6/runtime/version"
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const errMsgStateFromConsensus = "Could not convert consensus state to response"
|
||||
const (
|
||||
errMsgStateFromConsensus = "Could not convert consensus state to response"
|
||||
)
|
||||
|
||||
// GetBeaconStateV2 returns the full beacon state for a given state ID.
|
||||
func (s *Server) GetBeaconStateV2(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -208,3 +220,181 @@ func (s *Server) GetForkChoice(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
httputil.WriteJson(w, resp)
|
||||
}
|
||||
|
||||
// DataColumnSidecars retrieves data column sidecars for a given block id.
|
||||
func (s *Server) DataColumnSidecars(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, span := trace.StartSpan(r.Context(), "debug.DataColumnSidecars")
|
||||
defer span.End()
|
||||
|
||||
// Check if we're before Fulu fork - data columns are only available from Fulu onwards
|
||||
fuluForkEpoch := params.BeaconConfig().FuluForkEpoch
|
||||
if fuluForkEpoch == math.MaxUint64 {
|
||||
httputil.HandleError(w, "Data columns are not supported - Fulu fork not configured", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Check if we're before Fulu fork based on current slot
|
||||
currentSlot := s.GenesisTimeFetcher.CurrentSlot()
|
||||
currentEpoch := primitives.Epoch(currentSlot / params.BeaconConfig().SlotsPerEpoch)
|
||||
if currentEpoch < fuluForkEpoch {
|
||||
httputil.HandleError(w, "Data columns are not supported - before Fulu fork", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
indices, err := parseDataColumnIndices(r.URL)
|
||||
if err != nil {
|
||||
httputil.HandleError(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
segments := strings.Split(r.URL.Path, "/")
|
||||
blockId := segments[len(segments)-1]
|
||||
|
||||
verifiedDataColumns, rpcErr := s.Blocker.DataColumns(ctx, blockId, indices)
|
||||
if rpcErr != nil {
|
||||
code := core.ErrorReasonToHTTP(rpcErr.Reason)
|
||||
switch code {
|
||||
case http.StatusBadRequest:
|
||||
httputil.HandleError(w, "Bad request: "+rpcErr.Err.Error(), code)
|
||||
return
|
||||
case http.StatusNotFound:
|
||||
httputil.HandleError(w, "Not found: "+rpcErr.Err.Error(), code)
|
||||
return
|
||||
case http.StatusInternalServerError:
|
||||
httputil.HandleError(w, "Internal server error: "+rpcErr.Err.Error(), code)
|
||||
return
|
||||
default:
|
||||
httputil.HandleError(w, rpcErr.Err.Error(), code)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
blk, err := s.Blocker.Block(ctx, []byte(blockId))
|
||||
if err != nil {
|
||||
httputil.HandleError(w, "Could not fetch block: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if blk == nil {
|
||||
httputil.HandleError(w, "Block not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if httputil.RespondWithSsz(r) {
|
||||
sszResp, err := buildDataColumnSidecarsSSZResponse(verifiedDataColumns)
|
||||
if err != nil {
|
||||
httputil.HandleError(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set(api.VersionHeader, version.String(blk.Version()))
|
||||
httputil.WriteSsz(w, sszResp)
|
||||
return
|
||||
}
|
||||
|
||||
blkRoot, err := blk.Block().HashTreeRoot()
|
||||
if err != nil {
|
||||
httputil.HandleError(w, "Could not hash block: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
isOptimistic, err := s.OptimisticModeFetcher.IsOptimisticForRoot(ctx, blkRoot)
|
||||
if err != nil {
|
||||
httputil.HandleError(w, "Could not check if block is optimistic: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
data := buildDataColumnSidecarsJsonResponse(verifiedDataColumns)
|
||||
resp := &structs.GetDebugDataColumnSidecarsResponse{
|
||||
Version: version.String(blk.Version()),
|
||||
Data: data,
|
||||
ExecutionOptimistic: isOptimistic,
|
||||
Finalized: s.FinalizationFetcher.IsFinalized(ctx, blkRoot),
|
||||
}
|
||||
w.Header().Set(api.VersionHeader, version.String(blk.Version()))
|
||||
httputil.WriteJson(w, resp)
|
||||
}
|
||||
|
||||
// parseDataColumnIndices filters out invalid and duplicate data column indices
|
||||
func parseDataColumnIndices(url *url.URL) ([]int, error) {
|
||||
numberOfColumns := params.BeaconConfig().NumberOfColumns
|
||||
rawIndices := url.Query()["indices"]
|
||||
indices := make([]int, 0, numberOfColumns)
|
||||
invalidIndices := make([]string, 0)
|
||||
loop:
|
||||
for _, raw := range rawIndices {
|
||||
ix, err := strconv.Atoi(raw)
|
||||
if err != nil {
|
||||
invalidIndices = append(invalidIndices, raw)
|
||||
continue
|
||||
}
|
||||
if !(0 <= ix && uint64(ix) < numberOfColumns) {
|
||||
invalidIndices = append(invalidIndices, raw)
|
||||
continue
|
||||
}
|
||||
for i := range indices {
|
||||
if ix == indices[i] {
|
||||
continue loop
|
||||
}
|
||||
}
|
||||
indices = append(indices, ix)
|
||||
}
|
||||
|
||||
if len(invalidIndices) > 0 {
|
||||
return nil, fmt.Errorf("requested data column indices %v are invalid", invalidIndices)
|
||||
}
|
||||
return indices, nil
|
||||
}
|
||||
|
||||
func buildDataColumnSidecarsJsonResponse(verifiedDataColumns []blocks.VerifiedRODataColumn) []*structs.DataColumnSidecar {
|
||||
sidecars := make([]*structs.DataColumnSidecar, len(verifiedDataColumns))
|
||||
for i, dc := range verifiedDataColumns {
|
||||
column := make([]string, len(dc.Column))
|
||||
for j, cell := range dc.Column {
|
||||
column[j] = hexutil.Encode(cell)
|
||||
}
|
||||
|
||||
kzgCommitments := make([]string, len(dc.KzgCommitments))
|
||||
for j, commitment := range dc.KzgCommitments {
|
||||
kzgCommitments[j] = hexutil.Encode(commitment)
|
||||
}
|
||||
|
||||
kzgProofs := make([]string, len(dc.KzgProofs))
|
||||
for j, proof := range dc.KzgProofs {
|
||||
kzgProofs[j] = hexutil.Encode(proof)
|
||||
}
|
||||
|
||||
kzgCommitmentsInclusionProof := make([]string, len(dc.KzgCommitmentsInclusionProof))
|
||||
for j, proof := range dc.KzgCommitmentsInclusionProof {
|
||||
kzgCommitmentsInclusionProof[j] = hexutil.Encode(proof)
|
||||
}
|
||||
|
||||
sidecars[i] = &structs.DataColumnSidecar{
|
||||
Index: strconv.FormatUint(dc.Index, 10),
|
||||
Column: column,
|
||||
KzgCommitments: kzgCommitments,
|
||||
KzgProofs: kzgProofs,
|
||||
SignedBeaconBlockHeader: structs.SignedBeaconBlockHeaderFromConsensus(dc.SignedBlockHeader),
|
||||
KzgCommitmentsInclusionProof: kzgCommitmentsInclusionProof,
|
||||
}
|
||||
}
|
||||
return sidecars
|
||||
}
|
||||
|
||||
// buildDataColumnSidecarsSSZResponse builds SSZ response for data column sidecars
|
||||
func buildDataColumnSidecarsSSZResponse(verifiedDataColumns []blocks.VerifiedRODataColumn) ([]byte, error) {
|
||||
if len(verifiedDataColumns) == 0 {
|
||||
return []byte{}, nil
|
||||
}
|
||||
|
||||
// Pre-allocate buffer for all sidecars using the known SSZ size
|
||||
sizePerSidecar := (ðpb.DataColumnSidecar{}).SizeSSZ()
|
||||
ssz := make([]byte, 0, sizePerSidecar*len(verifiedDataColumns))
|
||||
|
||||
// Marshal and append each sidecar
|
||||
for i, sidecar := range verifiedDataColumns {
|
||||
sszrep, err := sidecar.MarshalSSZ()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to marshal data column sidecar at index %d", i)
|
||||
}
|
||||
ssz = append(ssz, sszrep...)
|
||||
}
|
||||
|
||||
return ssz, nil
|
||||
}
|
||||
|
||||
@@ -2,9 +2,13 @@ package debug
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"math"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/OffchainLabs/prysm/v6/api"
|
||||
@@ -13,9 +17,13 @@ import (
|
||||
dbtest "github.com/OffchainLabs/prysm/v6/beacon-chain/db/testing"
|
||||
doublylinkedtree "github.com/OffchainLabs/prysm/v6/beacon-chain/forkchoice/doubly-linked-tree"
|
||||
forkchoicetypes "github.com/OffchainLabs/prysm/v6/beacon-chain/forkchoice/types"
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/rpc/core"
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/rpc/testutil"
|
||||
"github.com/OffchainLabs/prysm/v6/config/params"
|
||||
"github.com/OffchainLabs/prysm/v6/consensus-types/blocks"
|
||||
"github.com/OffchainLabs/prysm/v6/consensus-types/primitives"
|
||||
"github.com/OffchainLabs/prysm/v6/encoding/bytesutil"
|
||||
ethpb "github.com/OffchainLabs/prysm/v6/proto/prysm/v1alpha1"
|
||||
"github.com/OffchainLabs/prysm/v6/runtime/version"
|
||||
"github.com/OffchainLabs/prysm/v6/testing/assert"
|
||||
"github.com/OffchainLabs/prysm/v6/testing/require"
|
||||
@@ -515,3 +523,285 @@ func TestGetForkChoice(t *testing.T) {
|
||||
require.NoError(t, json.Unmarshal(writer.Body.Bytes(), resp))
|
||||
require.Equal(t, "2", resp.FinalizedCheckpoint.Epoch)
|
||||
}
|
||||
|
||||
func TestDataColumnSidecars(t *testing.T) {
|
||||
t.Run("Fulu fork not configured", func(t *testing.T) {
|
||||
// Save the original config
|
||||
originalConfig := params.BeaconConfig()
|
||||
defer func() { params.OverrideBeaconConfig(originalConfig) }()
|
||||
|
||||
// Set Fulu fork epoch to MaxUint64 (unconfigured)
|
||||
config := params.BeaconConfig().Copy()
|
||||
config.FuluForkEpoch = math.MaxUint64
|
||||
params.OverrideBeaconConfig(config)
|
||||
|
||||
chainService := &blockchainmock.ChainService{}
|
||||
|
||||
// Create a mock blocker to avoid nil pointer
|
||||
mockBlocker := &testutil.MockBlocker{}
|
||||
|
||||
s := &Server{
|
||||
GenesisTimeFetcher: chainService,
|
||||
Blocker: mockBlocker,
|
||||
}
|
||||
|
||||
request := httptest.NewRequest(http.MethodGet, "http://example.com/eth/v1/debug/beacon/data_column_sidecars/head", nil)
|
||||
writer := httptest.NewRecorder()
|
||||
writer.Body = &bytes.Buffer{}
|
||||
|
||||
s.DataColumnSidecars(writer, request)
|
||||
require.Equal(t, http.StatusBadRequest, writer.Code)
|
||||
assert.StringContains(t, "Data columns are not supported - Fulu fork not configured", writer.Body.String())
|
||||
})
|
||||
|
||||
t.Run("Before Fulu fork", func(t *testing.T) {
|
||||
// Save the original config
|
||||
originalConfig := params.BeaconConfig()
|
||||
defer func() { params.OverrideBeaconConfig(originalConfig) }()
|
||||
|
||||
// Set Fulu fork epoch to 100
|
||||
config := params.BeaconConfig().Copy()
|
||||
config.FuluForkEpoch = 100
|
||||
params.OverrideBeaconConfig(config)
|
||||
|
||||
chainService := &blockchainmock.ChainService{}
|
||||
currentSlot := primitives.Slot(0) // Current slot 0 (epoch 0, before epoch 100)
|
||||
chainService.Slot = ¤tSlot
|
||||
|
||||
// Create a mock blocker to avoid nil pointer
|
||||
mockBlocker := &testutil.MockBlocker{
|
||||
DataColumnsFunc: func(ctx context.Context, id string, indices []int) ([]blocks.VerifiedRODataColumn, *core.RpcError) {
|
||||
return nil, &core.RpcError{Err: errors.New("before Fulu fork"), Reason: core.BadRequest}
|
||||
},
|
||||
}
|
||||
|
||||
s := &Server{
|
||||
GenesisTimeFetcher: chainService,
|
||||
Blocker: mockBlocker,
|
||||
}
|
||||
|
||||
request := httptest.NewRequest(http.MethodGet, "http://example.com/eth/v1/debug/beacon/data_column_sidecars/head", nil)
|
||||
writer := httptest.NewRecorder()
|
||||
writer.Body = &bytes.Buffer{}
|
||||
|
||||
s.DataColumnSidecars(writer, request)
|
||||
require.Equal(t, http.StatusBadRequest, writer.Code)
|
||||
assert.StringContains(t, "Data columns are not supported - before Fulu fork", writer.Body.String())
|
||||
})
|
||||
|
||||
t.Run("Invalid indices", func(t *testing.T) {
|
||||
// Save the original config
|
||||
originalConfig := params.BeaconConfig()
|
||||
defer func() { params.OverrideBeaconConfig(originalConfig) }()
|
||||
|
||||
// Set Fulu fork epoch to 0 (already activated)
|
||||
config := params.BeaconConfig().Copy()
|
||||
config.FuluForkEpoch = 0
|
||||
params.OverrideBeaconConfig(config)
|
||||
|
||||
chainService := &blockchainmock.ChainService{}
|
||||
currentSlot := primitives.Slot(0) // Current slot 0 (epoch 0, at fork)
|
||||
chainService.Slot = ¤tSlot
|
||||
|
||||
// Create a mock blocker to avoid nil pointer
|
||||
mockBlocker := &testutil.MockBlocker{
|
||||
DataColumnsFunc: func(ctx context.Context, id string, indices []int) ([]blocks.VerifiedRODataColumn, *core.RpcError) {
|
||||
return nil, &core.RpcError{Err: errors.New("invalid index"), Reason: core.BadRequest}
|
||||
},
|
||||
}
|
||||
|
||||
s := &Server{
|
||||
GenesisTimeFetcher: chainService,
|
||||
Blocker: mockBlocker,
|
||||
}
|
||||
|
||||
// Test with invalid index (out of range)
|
||||
request := httptest.NewRequest(http.MethodGet, "http://example.com/eth/v1/debug/beacon/data_column_sidecars/head?indices=9999", nil)
|
||||
writer := httptest.NewRecorder()
|
||||
writer.Body = &bytes.Buffer{}
|
||||
|
||||
s.DataColumnSidecars(writer, request)
|
||||
require.Equal(t, http.StatusBadRequest, writer.Code)
|
||||
assert.StringContains(t, "requested data column indices [9999] are invalid", writer.Body.String())
|
||||
})
|
||||
|
||||
t.Run("Block not found", func(t *testing.T) {
|
||||
// Save the original config
|
||||
originalConfig := params.BeaconConfig()
|
||||
defer func() { params.OverrideBeaconConfig(originalConfig) }()
|
||||
|
||||
// Set Fulu fork epoch to 0 (already activated)
|
||||
config := params.BeaconConfig().Copy()
|
||||
config.FuluForkEpoch = 0
|
||||
params.OverrideBeaconConfig(config)
|
||||
|
||||
chainService := &blockchainmock.ChainService{}
|
||||
currentSlot := primitives.Slot(0) // Current slot 0
|
||||
chainService.Slot = ¤tSlot
|
||||
|
||||
// Create a mock blocker that returns block not found
|
||||
mockBlocker := &testutil.MockBlocker{
|
||||
DataColumnsFunc: func(ctx context.Context, id string, indices []int) ([]blocks.VerifiedRODataColumn, *core.RpcError) {
|
||||
return nil, &core.RpcError{Err: errors.New("block not found"), Reason: core.NotFound}
|
||||
},
|
||||
BlockToReturn: nil, // Block not found
|
||||
}
|
||||
|
||||
s := &Server{
|
||||
GenesisTimeFetcher: chainService,
|
||||
Blocker: mockBlocker,
|
||||
}
|
||||
|
||||
request := httptest.NewRequest(http.MethodGet, "http://example.com/eth/v1/debug/beacon/data_column_sidecars/head", nil)
|
||||
writer := httptest.NewRecorder()
|
||||
writer.Body = &bytes.Buffer{}
|
||||
|
||||
s.DataColumnSidecars(writer, request)
|
||||
require.Equal(t, http.StatusNotFound, writer.Code)
|
||||
})
|
||||
|
||||
t.Run("Empty data columns", func(t *testing.T) {
|
||||
// Save the original config
|
||||
originalConfig := params.BeaconConfig()
|
||||
defer func() { params.OverrideBeaconConfig(originalConfig) }()
|
||||
|
||||
// Set Fulu fork epoch to 0
|
||||
config := params.BeaconConfig().Copy()
|
||||
config.FuluForkEpoch = 0
|
||||
params.OverrideBeaconConfig(config)
|
||||
|
||||
// Create a simple test block
|
||||
signedTestBlock := util.NewBeaconBlock()
|
||||
roBlock, err := blocks.NewSignedBeaconBlock(signedTestBlock)
|
||||
require.NoError(t, err)
|
||||
|
||||
chainService := &blockchainmock.ChainService{}
|
||||
currentSlot := primitives.Slot(0) // Current slot 0
|
||||
chainService.Slot = ¤tSlot
|
||||
chainService.OptimisticRoots = make(map[[32]byte]bool)
|
||||
chainService.FinalizedRoots = make(map[[32]byte]bool)
|
||||
|
||||
mockBlocker := &testutil.MockBlocker{
|
||||
DataColumnsFunc: func(ctx context.Context, id string, indices []int) ([]blocks.VerifiedRODataColumn, *core.RpcError) {
|
||||
return []blocks.VerifiedRODataColumn{}, nil // Empty data columns
|
||||
},
|
||||
BlockToReturn: roBlock,
|
||||
}
|
||||
|
||||
s := &Server{
|
||||
GenesisTimeFetcher: chainService,
|
||||
OptimisticModeFetcher: chainService,
|
||||
FinalizationFetcher: chainService,
|
||||
Blocker: mockBlocker,
|
||||
}
|
||||
|
||||
request := httptest.NewRequest(http.MethodGet, "http://example.com/eth/v1/debug/beacon/data_column_sidecars/head", nil)
|
||||
writer := httptest.NewRecorder()
|
||||
writer.Body = &bytes.Buffer{}
|
||||
|
||||
s.DataColumnSidecars(writer, request)
|
||||
require.Equal(t, http.StatusOK, writer.Code)
|
||||
|
||||
resp := &structs.GetDebugDataColumnSidecarsResponse{}
|
||||
require.NoError(t, json.Unmarshal(writer.Body.Bytes(), resp))
|
||||
require.Equal(t, 0, len(resp.Data))
|
||||
})
|
||||
}
|
||||
|
||||
func TestParseDataColumnIndices(t *testing.T) {
|
||||
// Save the original config
|
||||
originalConfig := params.BeaconConfig()
|
||||
defer func() { params.OverrideBeaconConfig(originalConfig) }()
|
||||
|
||||
// Set NumberOfColumns to 128 for testing
|
||||
config := params.BeaconConfig().Copy()
|
||||
config.NumberOfColumns = 128
|
||||
params.OverrideBeaconConfig(config)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
queryParams map[string][]string
|
||||
expected []int
|
||||
expectError bool
|
||||
}{
|
||||
{
|
||||
name: "no indices",
|
||||
queryParams: map[string][]string{},
|
||||
expected: []int{},
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "valid indices",
|
||||
queryParams: map[string][]string{"indices": {"0", "1", "127"}},
|
||||
expected: []int{0, 1, 127},
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "duplicate indices",
|
||||
queryParams: map[string][]string{"indices": {"0", "1", "0"}},
|
||||
expected: []int{0, 1},
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "invalid string index",
|
||||
queryParams: map[string][]string{"indices": {"abc"}},
|
||||
expected: nil,
|
||||
expectError: true,
|
||||
},
|
||||
{
|
||||
name: "negative index",
|
||||
queryParams: map[string][]string{"indices": {"-1"}},
|
||||
expected: nil,
|
||||
expectError: true,
|
||||
},
|
||||
{
|
||||
name: "index too large",
|
||||
queryParams: map[string][]string{"indices": {"128"}}, // 128 >= NumberOfColumns (128)
|
||||
expected: nil,
|
||||
expectError: true,
|
||||
},
|
||||
{
|
||||
name: "mixed valid and invalid",
|
||||
queryParams: map[string][]string{"indices": {"0", "abc", "1"}},
|
||||
expected: nil,
|
||||
expectError: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
u, err := url.Parse("http://example.com/test")
|
||||
require.NoError(t, err)
|
||||
|
||||
q := u.Query()
|
||||
for key, values := range tt.queryParams {
|
||||
for _, value := range values {
|
||||
q.Add(key, value)
|
||||
}
|
||||
}
|
||||
u.RawQuery = q.Encode()
|
||||
|
||||
result, err := parseDataColumnIndices(u)
|
||||
|
||||
if tt.expectError {
|
||||
assert.NotNil(t, err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
assert.DeepEqual(t, tt.expected, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildDataColumnSidecarsSSZResponse(t *testing.T) {
|
||||
t.Run("empty data columns", func(t *testing.T) {
|
||||
result, err := buildDataColumnSidecarsSSZResponse([]blocks.VerifiedRODataColumn{})
|
||||
require.NoError(t, err)
|
||||
require.DeepEqual(t, []byte{}, result)
|
||||
})
|
||||
|
||||
t.Run("get SSZ size", func(t *testing.T) {
|
||||
size := (ðpb.DataColumnSidecar{}).SizeSSZ()
|
||||
assert.Equal(t, true, size > 0)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -20,4 +20,6 @@ type Server struct {
|
||||
ForkchoiceFetcher blockchain.ForkchoiceFetcher
|
||||
FinalizationFetcher blockchain.FinalizationFetcher
|
||||
ChainInfoFetcher blockchain.ChainInfoFetcher
|
||||
GenesisTimeFetcher blockchain.TimeFetcher
|
||||
Blocker lookup.Blocker
|
||||
}
|
||||
|
||||
@@ -39,8 +39,8 @@ func HandleIsOptimisticError(w http.ResponseWriter, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
var blockRootsNotFoundErr *lookup.BlockRootsNotFoundError
|
||||
if errors.As(err, &blockRootsNotFoundErr) {
|
||||
var blockNotFoundErr *lookup.BlockNotFoundError
|
||||
if errors.As(err, &blockNotFoundErr) {
|
||||
httputil.HandleError(w, "Could not check optimistic status: "+err.Error(), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -138,7 +138,7 @@ func isStateRootOptimistic(
|
||||
return true, errors.Wrapf(err, "could not get block roots for slot %d", st.Slot())
|
||||
}
|
||||
if !has {
|
||||
return true, lookup.NewBlockRootsNotFoundError()
|
||||
return true, lookup.NewBlockNotFoundError("no block roots returned from the database")
|
||||
}
|
||||
for _, r := range roots {
|
||||
b, err := database.Block(ctx, r)
|
||||
|
||||
@@ -286,8 +286,8 @@ func TestIsOptimistic(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
mf := &testutil.MockStater{BeaconState: st}
|
||||
_, err = IsOptimistic(ctx, []byte(hexutil.Encode(bytesutil.PadTo([]byte("root"), 32))), cs, mf, cs, db)
|
||||
var blockRootsNotFoundErr *lookup.BlockRootsNotFoundError
|
||||
require.Equal(t, true, errors.As(err, &blockRootsNotFoundErr))
|
||||
var blockNotFoundErr *lookup.BlockNotFoundError
|
||||
require.Equal(t, true, errors.As(err, &blockNotFoundErr))
|
||||
})
|
||||
})
|
||||
|
||||
@@ -395,11 +395,11 @@ func TestHandleIsOptimisticError(t *testing.T) {
|
||||
})
|
||||
t.Run("no block roots error handled as 404", func(t *testing.T) {
|
||||
rr := httptest.NewRecorder()
|
||||
blockRootsErr := lookup.NewBlockRootsNotFoundError()
|
||||
HandleIsOptimisticError(rr, blockRootsErr)
|
||||
blockNotFoundErr := lookup.NewBlockNotFoundError("no block roots returned from the database")
|
||||
HandleIsOptimisticError(rr, blockNotFoundErr)
|
||||
|
||||
require.Equal(t, http.StatusNotFound, rr.Code)
|
||||
require.StringContains(t, blockRootsErr.Error(), rr.Body.String())
|
||||
require.StringContains(t, blockNotFoundErr.Error(), rr.Body.String())
|
||||
})
|
||||
|
||||
t.Run("generic error handled as 500", func(t *testing.T) {
|
||||
|
||||
@@ -24,17 +24,19 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type BlockRootsNotFoundError struct {
|
||||
// BlockNotFoundError represents an error when a block cannot be found.
|
||||
type BlockNotFoundError struct {
|
||||
message string
|
||||
}
|
||||
|
||||
func NewBlockRootsNotFoundError() *BlockRootsNotFoundError {
|
||||
return &BlockRootsNotFoundError{
|
||||
message: "no block roots returned from the database",
|
||||
// NewBlockNotFoundError creates a new BlockNotFoundError with a custom message.
|
||||
func NewBlockNotFoundError(msg string) *BlockNotFoundError {
|
||||
return &BlockNotFoundError{
|
||||
message: msg,
|
||||
}
|
||||
}
|
||||
|
||||
func (e BlockRootsNotFoundError) Error() string {
|
||||
func (e *BlockNotFoundError) Error() string {
|
||||
return e.message
|
||||
}
|
||||
|
||||
@@ -59,6 +61,7 @@ func (e BlockIdParseError) Error() string {
|
||||
type Blocker interface {
|
||||
Block(ctx context.Context, id []byte) (interfaces.ReadOnlySignedBeaconBlock, error)
|
||||
Blobs(ctx context.Context, id string, opts ...options.BlobsOption) ([]*blocks.VerifiedROBlob, *core.RpcError)
|
||||
DataColumns(ctx context.Context, id string, indices []int) ([]blocks.VerifiedRODataColumn, *core.RpcError)
|
||||
}
|
||||
|
||||
// BeaconDbBlocker is an implementation of Blocker. It retrieves blocks from the beacon chain database.
|
||||
@@ -70,6 +73,141 @@ type BeaconDbBlocker struct {
|
||||
DataColumnStorage *filesystem.DataColumnStorage
|
||||
}
|
||||
|
||||
// resolveBlockIDByRootOrSlot resolves a block ID that is either a root (hex string or raw bytes) or a slot number.
|
||||
func (p *BeaconDbBlocker) resolveBlockIDByRootOrSlot(ctx context.Context, id string) ([fieldparams.RootLength]byte, interfaces.ReadOnlySignedBeaconBlock, error) {
|
||||
var rootSlice []byte
|
||||
|
||||
// Check if it's a hex-encoded root
|
||||
if bytesutil.IsHex([]byte(id)) {
|
||||
var err error
|
||||
rootSlice, err = bytesutil.DecodeHexWithLength(id, fieldparams.RootLength)
|
||||
if err != nil {
|
||||
e := NewBlockIdParseError(err)
|
||||
return [32]byte{}, nil, &e
|
||||
}
|
||||
} else if len(id) == 32 {
|
||||
// Handle raw 32-byte root
|
||||
rootSlice = []byte(id)
|
||||
} else {
|
||||
// Try to parse as slot number
|
||||
slot, err := strconv.ParseUint(id, 10, 64)
|
||||
if err != nil {
|
||||
e := NewBlockIdParseError(err)
|
||||
return [32]byte{}, nil, &e
|
||||
}
|
||||
|
||||
// Get block roots for the slot
|
||||
ok, roots, err := p.BeaconDB.BlockRootsBySlot(ctx, primitives.Slot(slot))
|
||||
if err != nil {
|
||||
return [32]byte{}, nil, errors.Wrapf(err, "could not retrieve block roots for slot %d", slot)
|
||||
}
|
||||
if !ok || len(roots) == 0 {
|
||||
return [32]byte{}, nil, NewBlockNotFoundError(fmt.Sprintf("no blocks found at slot %d", slot))
|
||||
}
|
||||
|
||||
// Find the canonical block root
|
||||
if p.ChainInfoFetcher == nil {
|
||||
return [32]byte{}, nil, errors.New("chain info fetcher is not configured")
|
||||
}
|
||||
|
||||
for _, root := range roots {
|
||||
canonical, err := p.ChainInfoFetcher.IsCanonical(ctx, root)
|
||||
if err != nil {
|
||||
return [32]byte{}, nil, errors.Wrapf(err, "could not determine if block root is canonical")
|
||||
}
|
||||
if canonical {
|
||||
rootSlice = root[:]
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// If no canonical block found, rootSlice remains nil
|
||||
if rootSlice == nil {
|
||||
// No canonical block at this slot
|
||||
return [32]byte{}, nil, NewBlockNotFoundError(fmt.Sprintf("no canonical block found at slot %d", slot))
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch the block using the root
|
||||
root := bytesutil.ToBytes32(rootSlice)
|
||||
blk, err := p.BeaconDB.Block(ctx, root)
|
||||
if err != nil {
|
||||
return [32]byte{}, nil, errors.Wrapf(err, "failed to retrieve block %#x from db", rootSlice)
|
||||
}
|
||||
if blk == nil {
|
||||
return [32]byte{}, nil, NewBlockNotFoundError(fmt.Sprintf("block %#x not found in db", rootSlice))
|
||||
}
|
||||
|
||||
return root, blk, nil
|
||||
}
|
||||
|
||||
// resolveBlockID resolves a block ID to root and signed block.
|
||||
// Fork validation is handled outside this function by the calling methods.
|
||||
func (p *BeaconDbBlocker) resolveBlockID(ctx context.Context, id string) ([fieldparams.RootLength]byte, interfaces.ReadOnlySignedBeaconBlock, error) {
|
||||
switch id {
|
||||
case "genesis":
|
||||
blk, err := p.BeaconDB.GenesisBlock(ctx)
|
||||
if err != nil {
|
||||
return [32]byte{}, nil, errors.Wrap(err, "could not retrieve genesis block")
|
||||
}
|
||||
if blk == nil {
|
||||
return [32]byte{}, nil, NewBlockNotFoundError("genesis block not found")
|
||||
}
|
||||
root, err := blk.Block().HashTreeRoot()
|
||||
if err != nil {
|
||||
return [32]byte{}, nil, errors.Wrap(err, "could not get genesis block root")
|
||||
}
|
||||
return root, blk, nil
|
||||
|
||||
case "head":
|
||||
blk, err := p.ChainInfoFetcher.HeadBlock(ctx)
|
||||
if err != nil {
|
||||
return [32]byte{}, nil, errors.Wrap(err, "could not retrieve head block")
|
||||
}
|
||||
if blk == nil {
|
||||
return [32]byte{}, nil, NewBlockNotFoundError("head block not found")
|
||||
}
|
||||
root, err := blk.Block().HashTreeRoot()
|
||||
if err != nil {
|
||||
return [32]byte{}, nil, errors.Wrap(err, "could not get head block root")
|
||||
}
|
||||
return root, blk, nil
|
||||
|
||||
case "finalized":
|
||||
finalized := p.ChainInfoFetcher.FinalizedCheckpt()
|
||||
if finalized == nil {
|
||||
return [32]byte{}, nil, errors.New("received nil finalized checkpoint")
|
||||
}
|
||||
finalizedRoot := bytesutil.ToBytes32(finalized.Root)
|
||||
blk, err := p.BeaconDB.Block(ctx, finalizedRoot)
|
||||
if err != nil {
|
||||
return [32]byte{}, nil, errors.Wrap(err, "could not retrieve finalized block")
|
||||
}
|
||||
if blk == nil {
|
||||
return [32]byte{}, nil, NewBlockNotFoundError(fmt.Sprintf("finalized block %#x not found", finalizedRoot))
|
||||
}
|
||||
return finalizedRoot, blk, nil
|
||||
|
||||
case "justified":
|
||||
jcp := p.ChainInfoFetcher.CurrentJustifiedCheckpt()
|
||||
if jcp == nil {
|
||||
return [32]byte{}, nil, errors.New("received nil justified checkpoint")
|
||||
}
|
||||
justifiedRoot := bytesutil.ToBytes32(jcp.Root)
|
||||
blk, err := p.BeaconDB.Block(ctx, justifiedRoot)
|
||||
if err != nil {
|
||||
return [32]byte{}, nil, errors.Wrap(err, "could not retrieve justified block")
|
||||
}
|
||||
if blk == nil {
|
||||
return [32]byte{}, nil, NewBlockNotFoundError(fmt.Sprintf("justified block %#x not found", justifiedRoot))
|
||||
}
|
||||
return justifiedRoot, blk, nil
|
||||
|
||||
default:
|
||||
return p.resolveBlockIDByRootOrSlot(ctx, id)
|
||||
}
|
||||
}
|
||||
|
||||
// Block returns the beacon block for a given identifier. The identifier can be one of:
|
||||
// - "head" (canonical head in node's view)
|
||||
// - "genesis"
|
||||
@@ -79,71 +217,9 @@ type BeaconDbBlocker struct {
|
||||
// - <hex encoded block root with '0x' prefix>
|
||||
// - <block root>
|
||||
func (p *BeaconDbBlocker) Block(ctx context.Context, id []byte) (interfaces.ReadOnlySignedBeaconBlock, error) {
|
||||
var err error
|
||||
var blk interfaces.ReadOnlySignedBeaconBlock
|
||||
switch string(id) {
|
||||
case "head":
|
||||
blk, err = p.ChainInfoFetcher.HeadBlock(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not retrieve head block")
|
||||
}
|
||||
case "finalized":
|
||||
finalized := p.ChainInfoFetcher.FinalizedCheckpt()
|
||||
finalizedRoot := bytesutil.ToBytes32(finalized.Root)
|
||||
blk, err = p.BeaconDB.Block(ctx, finalizedRoot)
|
||||
if err != nil {
|
||||
return nil, errors.New("could not get finalized block from db")
|
||||
}
|
||||
case "genesis":
|
||||
blk, err = p.BeaconDB.GenesisBlock(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not retrieve genesis block")
|
||||
}
|
||||
default:
|
||||
if bytesutil.IsHex(id) {
|
||||
decoded, err := hexutil.Decode(string(id))
|
||||
if err != nil {
|
||||
e := NewBlockIdParseError(err)
|
||||
return nil, &e
|
||||
}
|
||||
blk, err = p.BeaconDB.Block(ctx, bytesutil.ToBytes32(decoded))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not retrieve block")
|
||||
}
|
||||
} else if len(id) == 32 {
|
||||
blk, err = p.BeaconDB.Block(ctx, bytesutil.ToBytes32(id))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not retrieve block")
|
||||
}
|
||||
} else {
|
||||
slot, err := strconv.ParseUint(string(id), 10, 64)
|
||||
if err != nil {
|
||||
e := NewBlockIdParseError(err)
|
||||
return nil, &e
|
||||
}
|
||||
blks, err := p.BeaconDB.BlocksBySlot(ctx, primitives.Slot(slot))
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "could not retrieve blocks for slot %d", slot)
|
||||
}
|
||||
_, roots, err := p.BeaconDB.BlockRootsBySlot(ctx, primitives.Slot(slot))
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "could not retrieve block roots for slot %d", slot)
|
||||
}
|
||||
numBlks := len(blks)
|
||||
if numBlks == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
for i, b := range blks {
|
||||
canonical, err := p.ChainInfoFetcher.IsCanonical(ctx, roots[i])
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "could not determine if block root is canonical")
|
||||
}
|
||||
if canonical {
|
||||
blk = b
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
_, blk, err := p.resolveBlockID(ctx, string(id))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return blk, nil
|
||||
}
|
||||
@@ -171,88 +247,36 @@ func (p *BeaconDbBlocker) Blobs(ctx context.Context, id string, opts ...options.
|
||||
opt(cfg)
|
||||
}
|
||||
|
||||
// Resolve block ID to root
|
||||
var rootSlice []byte
|
||||
switch id {
|
||||
case "genesis":
|
||||
return nil, &core.RpcError{Err: errors.New("blobs are not supported for Phase 0 fork"), Reason: core.BadRequest}
|
||||
case "head":
|
||||
var err error
|
||||
rootSlice, err = p.ChainInfoFetcher.HeadRoot(ctx)
|
||||
if err != nil {
|
||||
return nil, &core.RpcError{Err: errors.Wrapf(err, "could not retrieve head root"), Reason: core.Internal}
|
||||
}
|
||||
case "finalized":
|
||||
fcp := p.ChainInfoFetcher.FinalizedCheckpt()
|
||||
if fcp == nil {
|
||||
return nil, &core.RpcError{Err: errors.New("received nil finalized checkpoint"), Reason: core.Internal}
|
||||
}
|
||||
rootSlice = fcp.Root
|
||||
case "justified":
|
||||
jcp := p.ChainInfoFetcher.CurrentJustifiedCheckpt()
|
||||
if jcp == nil {
|
||||
return nil, &core.RpcError{Err: errors.New("received nil justified checkpoint"), Reason: core.Internal}
|
||||
}
|
||||
rootSlice = jcp.Root
|
||||
default:
|
||||
if bytesutil.IsHex([]byte(id)) {
|
||||
var err error
|
||||
rootSlice, err = bytesutil.DecodeHexWithLength(id, fieldparams.RootLength)
|
||||
if err != nil {
|
||||
return nil, &core.RpcError{Err: NewBlockIdParseError(err), Reason: core.BadRequest}
|
||||
}
|
||||
} else {
|
||||
slot, err := strconv.ParseUint(id, 10, 64)
|
||||
if err != nil {
|
||||
return nil, &core.RpcError{Err: NewBlockIdParseError(err), Reason: core.BadRequest}
|
||||
}
|
||||
denebStart, err := slots.EpochStart(params.BeaconConfig().DenebForkEpoch)
|
||||
if err != nil {
|
||||
return nil, &core.RpcError{Err: errors.Wrap(err, "could not calculate Deneb start slot"), Reason: core.Internal}
|
||||
}
|
||||
if primitives.Slot(slot) < denebStart {
|
||||
return nil, &core.RpcError{Err: errors.New("blobs are not supported before Deneb fork"), Reason: core.BadRequest}
|
||||
}
|
||||
ok, roots, err := p.BeaconDB.BlockRootsBySlot(ctx, primitives.Slot(slot))
|
||||
if !ok {
|
||||
return nil, &core.RpcError{Err: fmt.Errorf("no block roots at slot %d", slot), Reason: core.NotFound}
|
||||
}
|
||||
if err != nil {
|
||||
return nil, &core.RpcError{Err: errors.Wrapf(err, "failed to get block roots for slot %d", slot), Reason: core.Internal}
|
||||
}
|
||||
rootSlice = roots[0][:]
|
||||
if len(roots) == 1 {
|
||||
break
|
||||
}
|
||||
for _, blockRoot := range roots {
|
||||
canonical, err := p.ChainInfoFetcher.IsCanonical(ctx, blockRoot)
|
||||
if err != nil {
|
||||
return nil, &core.RpcError{Err: errors.Wrapf(err, "could not determine if block %#x is canonical", blockRoot), Reason: core.Internal}
|
||||
}
|
||||
if canonical {
|
||||
rootSlice = blockRoot[:]
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
// Check for genesis block first (not supported for blobs)
|
||||
if id == "genesis" {
|
||||
return nil, &core.RpcError{Err: errors.New("not supported for Phase 0 fork"), Reason: core.BadRequest}
|
||||
}
|
||||
|
||||
root := bytesutil.ToBytes32(rootSlice)
|
||||
|
||||
roSignedBlock, err := p.BeaconDB.Block(ctx, root)
|
||||
// Resolve block ID to root and block
|
||||
root, roSignedBlock, err := p.resolveBlockID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, &core.RpcError{Err: errors.Wrapf(err, "failed to retrieve block %#x from db", rootSlice), Reason: core.Internal}
|
||||
var blockNotFound *BlockNotFoundError
|
||||
var blockIdParseErr *BlockIdParseError
|
||||
|
||||
reason := core.Internal // Default to Internal for unexpected errors
|
||||
if errors.As(err, &blockNotFound) {
|
||||
reason = core.NotFound
|
||||
} else if errors.As(err, &blockIdParseErr) {
|
||||
reason = core.BadRequest
|
||||
}
|
||||
return nil, &core.RpcError{Err: err, Reason: core.ErrorReason(reason)}
|
||||
}
|
||||
|
||||
if roSignedBlock == nil {
|
||||
return nil, &core.RpcError{Err: fmt.Errorf("block %#x not found in db", rootSlice), Reason: core.NotFound}
|
||||
slot := roSignedBlock.Block().Slot()
|
||||
if slots.ToEpoch(slot) < params.BeaconConfig().DenebForkEpoch {
|
||||
return nil, &core.RpcError{Err: errors.New("blobs are not supported before Deneb fork"), Reason: core.BadRequest}
|
||||
}
|
||||
|
||||
roBlock := roSignedBlock.Block()
|
||||
|
||||
commitments, err := roBlock.Body().BlobKzgCommitments()
|
||||
if err != nil {
|
||||
return nil, &core.RpcError{Err: errors.Wrapf(err, "failed to retrieve kzg commitments from block %#x", rootSlice), Reason: core.Internal}
|
||||
return nil, &core.RpcError{Err: errors.Wrapf(err, "failed to retrieve kzg commitments from block %#x", root), Reason: core.Internal}
|
||||
}
|
||||
|
||||
// If there are no commitments return 200 w/ empty list
|
||||
@@ -266,7 +290,7 @@ func (p *BeaconDbBlocker) Blobs(ctx context.Context, id string, opts ...options.
|
||||
if fuluForkEpoch != primitives.Epoch(math.MaxUint64) {
|
||||
fuluForkSlot, err = slots.EpochStart(fuluForkEpoch)
|
||||
if err != nil {
|
||||
return nil, &core.RpcError{Err: errors.Wrap(err, "could not calculate peerDAS start slot"), Reason: core.Internal}
|
||||
return nil, &core.RpcError{Err: errors.Wrap(err, "could not calculate Fulu start slot"), Reason: core.Internal}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -461,3 +485,93 @@ func (p *BeaconDbBlocker) neededDataColumnSidecars(root [fieldparams.RootLength]
|
||||
|
||||
return verifiedRoSidecars, nil
|
||||
}
|
||||
|
||||
// DataColumns returns the data column sidecars for a given block id identifier and column indices. The identifier can be one of:
|
||||
// - "head" (canonical head in node's view)
|
||||
// - "genesis"
|
||||
// - "finalized"
|
||||
// - "justified"
|
||||
// - <slot>
|
||||
// - <hex encoded block root with '0x' prefix>
|
||||
// - <block root>
|
||||
//
|
||||
// cases:
|
||||
// - no block, 404
|
||||
// - block exists, before Fulu fork, 400 (data columns are not supported before Fulu fork)
|
||||
func (p *BeaconDbBlocker) DataColumns(ctx context.Context, id string, indices []int) ([]blocks.VerifiedRODataColumn, *core.RpcError) {
|
||||
// Check for genesis block first (not supported for data columns)
|
||||
if id == "genesis" {
|
||||
return nil, &core.RpcError{Err: errors.New("data columns are not supported for Phase 0 fork"), Reason: core.BadRequest}
|
||||
}
|
||||
|
||||
// Resolve block ID to root and block
|
||||
root, roSignedBlock, err := p.resolveBlockID(ctx, id)
|
||||
if err != nil {
|
||||
var blockNotFound *BlockNotFoundError
|
||||
var blockIdParseErr *BlockIdParseError
|
||||
|
||||
reason := core.Internal // Default to Internal for unexpected errors
|
||||
if errors.As(err, &blockNotFound) {
|
||||
reason = core.NotFound
|
||||
} else if errors.As(err, &blockIdParseErr) {
|
||||
reason = core.BadRequest
|
||||
}
|
||||
return nil, &core.RpcError{Err: err, Reason: core.ErrorReason(reason)}
|
||||
}
|
||||
|
||||
slot := roSignedBlock.Block().Slot()
|
||||
fuluForkEpoch := params.BeaconConfig().FuluForkEpoch
|
||||
fuluForkSlot, err := slots.EpochStart(fuluForkEpoch)
|
||||
if err != nil {
|
||||
return nil, &core.RpcError{Err: errors.Wrap(err, "could not calculate Fulu start slot"), Reason: core.Internal}
|
||||
}
|
||||
if slot < fuluForkSlot {
|
||||
return nil, &core.RpcError{Err: errors.New("data columns are not supported before Fulu fork"), Reason: core.BadRequest}
|
||||
}
|
||||
|
||||
roBlock := roSignedBlock.Block()
|
||||
|
||||
commitments, err := roBlock.Body().BlobKzgCommitments()
|
||||
if err != nil {
|
||||
return nil, &core.RpcError{Err: errors.Wrapf(err, "failed to retrieve kzg commitments from block %#x", root), Reason: core.Internal}
|
||||
}
|
||||
|
||||
// If there are no commitments return 200 w/ empty list
|
||||
if len(commitments) == 0 {
|
||||
return make([]blocks.VerifiedRODataColumn, 0), nil
|
||||
}
|
||||
|
||||
// Get column indices to retrieve
|
||||
columnIndices := make([]uint64, 0)
|
||||
if len(indices) == 0 {
|
||||
// If no indices specified, return all columns this node is custodying
|
||||
summary := p.DataColumnStorage.Summary(root)
|
||||
stored := summary.Stored()
|
||||
for index := range stored {
|
||||
columnIndices = append(columnIndices, index)
|
||||
}
|
||||
} else {
|
||||
// Validate and convert indices
|
||||
numberOfColumns := params.BeaconConfig().NumberOfColumns
|
||||
for _, index := range indices {
|
||||
if index < 0 || uint64(index) >= numberOfColumns {
|
||||
return nil, &core.RpcError{
|
||||
Err: fmt.Errorf("requested index %d is outside valid range [0, %d)", index, numberOfColumns),
|
||||
Reason: core.BadRequest,
|
||||
}
|
||||
}
|
||||
columnIndices = append(columnIndices, uint64(index))
|
||||
}
|
||||
}
|
||||
|
||||
// Retrieve data column sidecars from storage
|
||||
verifiedRoDataColumns, err := p.DataColumnStorage.Get(root, columnIndices)
|
||||
if err != nil {
|
||||
return nil, &core.RpcError{
|
||||
Err: errors.Wrapf(err, "could not retrieve data columns for block root %#x", root),
|
||||
Reason: core.Internal,
|
||||
}
|
||||
}
|
||||
|
||||
return verifiedRoDataColumns, nil
|
||||
}
|
||||
|
||||
@@ -61,11 +61,12 @@ func TestGetBlock(t *testing.T) {
|
||||
fetcher := &BeaconDbBlocker{
|
||||
BeaconDB: beaconDB,
|
||||
ChainInfoFetcher: &mockChain.ChainService{
|
||||
DB: beaconDB,
|
||||
Block: wsb,
|
||||
Root: headBlock.BlockRoot,
|
||||
FinalizedCheckPoint: ðpb.Checkpoint{Root: blkContainers[64].BlockRoot},
|
||||
CanonicalRoots: canonicalRoots,
|
||||
DB: beaconDB,
|
||||
Block: wsb,
|
||||
Root: headBlock.BlockRoot,
|
||||
FinalizedCheckPoint: ðpb.Checkpoint{Root: blkContainers[64].BlockRoot},
|
||||
CurrentJustifiedCheckPoint: ðpb.Checkpoint{Root: blkContainers[32].BlockRoot},
|
||||
CanonicalRoots: canonicalRoots,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -108,6 +109,11 @@ func TestGetBlock(t *testing.T) {
|
||||
blockID: []byte("finalized"),
|
||||
want: blkContainers[64].Block.(*ethpb.BeaconBlockContainer_Phase0Block).Phase0Block,
|
||||
},
|
||||
{
|
||||
name: "justified",
|
||||
blockID: []byte("justified"),
|
||||
want: blkContainers[32].Block.(*ethpb.BeaconBlockContainer_Phase0Block).Phase0Block,
|
||||
},
|
||||
{
|
||||
name: "genesis",
|
||||
blockID: []byte("genesis"),
|
||||
@@ -162,6 +168,112 @@ func TestGetBlock(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBlobsErrorHandling(t *testing.T) {
|
||||
params.SetupTestConfigCleanup(t)
|
||||
cfg := params.BeaconConfig().Copy()
|
||||
cfg.DenebForkEpoch = 1
|
||||
params.OverrideBeaconConfig(cfg)
|
||||
|
||||
ctx := t.Context()
|
||||
db := testDB.SetupDB(t)
|
||||
|
||||
t.Run("non-existent block by root returns 404", func(t *testing.T) {
|
||||
blocker := &BeaconDbBlocker{
|
||||
BeaconDB: db,
|
||||
}
|
||||
|
||||
_, rpcErr := blocker.Blobs(ctx, "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef")
|
||||
require.NotNil(t, rpcErr)
|
||||
require.Equal(t, core.ErrorReason(core.NotFound), rpcErr.Reason)
|
||||
require.StringContains(t, "not found", rpcErr.Err.Error())
|
||||
})
|
||||
|
||||
t.Run("non-existent block by slot returns 404", func(t *testing.T) {
|
||||
blocker := &BeaconDbBlocker{
|
||||
BeaconDB: db,
|
||||
ChainInfoFetcher: &mockChain.ChainService{},
|
||||
}
|
||||
|
||||
_, rpcErr := blocker.Blobs(ctx, "999999")
|
||||
require.NotNil(t, rpcErr)
|
||||
require.Equal(t, core.ErrorReason(core.NotFound), rpcErr.Reason)
|
||||
require.StringContains(t, "no blocks found at slot", rpcErr.Err.Error())
|
||||
})
|
||||
|
||||
t.Run("genesis block not found returns 404", func(t *testing.T) {
|
||||
blocker := &BeaconDbBlocker{
|
||||
BeaconDB: db,
|
||||
}
|
||||
|
||||
// Note: genesis blocks don't support blobs, so this returns BadRequest
|
||||
_, rpcErr := blocker.Blobs(ctx, "genesis")
|
||||
require.NotNil(t, rpcErr)
|
||||
require.Equal(t, core.ErrorReason(core.BadRequest), rpcErr.Reason)
|
||||
require.StringContains(t, "not supported for Phase 0", rpcErr.Err.Error())
|
||||
})
|
||||
|
||||
t.Run("finalized block not found returns 404", func(t *testing.T) {
|
||||
// Set up a finalized checkpoint pointing to a non-existent block
|
||||
nonExistentRoot := bytesutil.PadTo([]byte("nonexistent"), 32)
|
||||
blocker := &BeaconDbBlocker{
|
||||
BeaconDB: db,
|
||||
ChainInfoFetcher: &mockChain.ChainService{
|
||||
FinalizedCheckPoint: ðpb.Checkpoint{Root: nonExistentRoot},
|
||||
},
|
||||
}
|
||||
|
||||
_, rpcErr := blocker.Blobs(ctx, "finalized")
|
||||
require.NotNil(t, rpcErr)
|
||||
require.Equal(t, core.ErrorReason(core.NotFound), rpcErr.Reason)
|
||||
require.StringContains(t, "finalized block", rpcErr.Err.Error())
|
||||
require.StringContains(t, "not found", rpcErr.Err.Error())
|
||||
})
|
||||
|
||||
t.Run("justified block not found returns 404", func(t *testing.T) {
|
||||
// Set up a justified checkpoint pointing to a non-existent block
|
||||
nonExistentRoot := bytesutil.PadTo([]byte("nonexistent2"), 32)
|
||||
blocker := &BeaconDbBlocker{
|
||||
BeaconDB: db,
|
||||
ChainInfoFetcher: &mockChain.ChainService{
|
||||
CurrentJustifiedCheckPoint: ðpb.Checkpoint{Root: nonExistentRoot},
|
||||
},
|
||||
}
|
||||
|
||||
_, rpcErr := blocker.Blobs(ctx, "justified")
|
||||
require.NotNil(t, rpcErr)
|
||||
require.Equal(t, core.ErrorReason(core.NotFound), rpcErr.Reason)
|
||||
require.StringContains(t, "justified block", rpcErr.Err.Error())
|
||||
require.StringContains(t, "not found", rpcErr.Err.Error())
|
||||
})
|
||||
|
||||
t.Run("invalid block ID returns 400", func(t *testing.T) {
|
||||
blocker := &BeaconDbBlocker{
|
||||
BeaconDB: db,
|
||||
}
|
||||
|
||||
_, rpcErr := blocker.Blobs(ctx, "invalid-hex")
|
||||
require.NotNil(t, rpcErr)
|
||||
require.Equal(t, core.ErrorReason(core.BadRequest), rpcErr.Reason)
|
||||
require.StringContains(t, "could not parse block ID", rpcErr.Err.Error())
|
||||
})
|
||||
|
||||
t.Run("database error returns 500", func(t *testing.T) {
|
||||
// Create a pre-Deneb block with valid slot
|
||||
predenebBlock := util.NewBeaconBlock()
|
||||
predenebBlock.Block.Slot = 100
|
||||
util.SaveBlock(t, ctx, db, predenebBlock)
|
||||
|
||||
// Create blocker without ChainInfoFetcher to trigger internal error when checking canonical status
|
||||
blocker := &BeaconDbBlocker{
|
||||
BeaconDB: db,
|
||||
}
|
||||
|
||||
_, rpcErr := blocker.Blobs(ctx, "100")
|
||||
require.NotNil(t, rpcErr)
|
||||
require.Equal(t, core.ErrorReason(core.Internal), rpcErr.Reason)
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetBlob(t *testing.T) {
|
||||
const (
|
||||
slot = 123
|
||||
@@ -217,9 +329,9 @@ func TestGetBlob(t *testing.T) {
|
||||
for _, blob := range fuluBlobSidecars {
|
||||
var kzgBlob kzg.Blob
|
||||
copy(kzgBlob[:], blob.Blob)
|
||||
cellsAndProogs, err := kzg.ComputeCellsAndKZGProofs(&kzgBlob)
|
||||
cellsAndProofs, err := kzg.ComputeCellsAndKZGProofs(&kzgBlob)
|
||||
require.NoError(t, err)
|
||||
cellsAndProofsList = append(cellsAndProofsList, cellsAndProogs)
|
||||
cellsAndProofsList = append(cellsAndProofsList, cellsAndProofs)
|
||||
}
|
||||
|
||||
roDataColumnSidecars, err := peerdas.DataColumnSidecars(cellsAndProofsList, peerdas.PopulateFromBlock(fuluBlock))
|
||||
@@ -240,14 +352,17 @@ func TestGetBlob(t *testing.T) {
|
||||
blocker := &BeaconDbBlocker{}
|
||||
_, rpcErr := blocker.Blobs(ctx, "genesis")
|
||||
require.Equal(t, http.StatusBadRequest, core.ErrorReasonToHTTP(rpcErr.Reason))
|
||||
require.StringContains(t, "blobs are not supported for Phase 0 fork", rpcErr.Err.Error())
|
||||
require.StringContains(t, "not supported for Phase 0 fork", rpcErr.Err.Error())
|
||||
})
|
||||
|
||||
t.Run("head", func(t *testing.T) {
|
||||
setupDeneb(t)
|
||||
|
||||
blocker := &BeaconDbBlocker{
|
||||
ChainInfoFetcher: &mockChain.ChainService{Root: denebBlockRoot[:]},
|
||||
ChainInfoFetcher: &mockChain.ChainService{
|
||||
Root: denebBlockRoot[:],
|
||||
Block: denebBlock,
|
||||
},
|
||||
GenesisTimeFetcher: &testutil.MockGenesisTimeFetcher{
|
||||
Genesis: time.Now(),
|
||||
},
|
||||
@@ -326,6 +441,7 @@ func TestGetBlob(t *testing.T) {
|
||||
setupDeneb(t)
|
||||
|
||||
blocker := &BeaconDbBlocker{
|
||||
ChainInfoFetcher: &mockChain.ChainService{},
|
||||
GenesisTimeFetcher: &testutil.MockGenesisTimeFetcher{
|
||||
Genesis: time.Now(),
|
||||
},
|
||||
@@ -491,6 +607,31 @@ func TestGetBlob(t *testing.T) {
|
||||
require.DeepSSZEqual(t, initialBlobSidecarPb, retrievedBlobSidecarPb)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("pre-deneb block should return 400", func(t *testing.T) {
|
||||
// Setup with Deneb fork at epoch 1, so slot 0 is before Deneb
|
||||
params.SetupTestConfigCleanup(t)
|
||||
cfg := params.BeaconConfig().Copy()
|
||||
cfg.DenebForkEpoch = 1
|
||||
params.OverrideBeaconConfig(cfg)
|
||||
|
||||
// Create a pre-Deneb block (slot 0, which is epoch 0)
|
||||
predenebBlock := util.NewBeaconBlock()
|
||||
predenebBlock.Block.Slot = 0
|
||||
util.SaveBlock(t, ctx, db, predenebBlock)
|
||||
predenebBlockRoot, err := predenebBlock.Block.HashTreeRoot()
|
||||
require.NoError(t, err)
|
||||
|
||||
blocker := &BeaconDbBlocker{
|
||||
BeaconDB: db,
|
||||
}
|
||||
|
||||
_, rpcErr := blocker.Blobs(ctx, hexutil.Encode(predenebBlockRoot[:]))
|
||||
require.NotNil(t, rpcErr)
|
||||
require.Equal(t, core.ErrorReason(core.BadRequest), rpcErr.Reason)
|
||||
require.Equal(t, http.StatusBadRequest, core.ErrorReasonToHTTP(rpcErr.Reason))
|
||||
require.StringContains(t, "not supported before", rpcErr.Err.Error())
|
||||
})
|
||||
}
|
||||
|
||||
func TestBlobs_CommitmentOrdering(t *testing.T) {
|
||||
@@ -653,3 +794,300 @@ func TestBlobs_CommitmentOrdering(t *testing.T) {
|
||||
require.StringContains(t, "0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", rpcErr.Err.Error())
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetDataColumns(t *testing.T) {
|
||||
const (
|
||||
blobCount = 4
|
||||
fuluForkEpoch = 2
|
||||
)
|
||||
|
||||
setupFulu := func(t *testing.T) {
|
||||
params.SetupTestConfigCleanup(t)
|
||||
cfg := params.BeaconConfig().Copy()
|
||||
cfg.DenebForkEpoch = 1
|
||||
cfg.FuluForkEpoch = fuluForkEpoch
|
||||
params.OverrideBeaconConfig(cfg)
|
||||
}
|
||||
|
||||
setupPreFulu := func(t *testing.T) {
|
||||
params.SetupTestConfigCleanup(t)
|
||||
cfg := params.BeaconConfig().Copy()
|
||||
cfg.DenebForkEpoch = 1
|
||||
cfg.FuluForkEpoch = 1000 // Set to a high epoch to ensure we're before Fulu
|
||||
params.OverrideBeaconConfig(cfg)
|
||||
}
|
||||
|
||||
ctx := t.Context()
|
||||
db := testDB.SetupDB(t)
|
||||
|
||||
// Start the trusted setup.
|
||||
err := kzg.Start()
|
||||
require.NoError(t, err)
|
||||
|
||||
// Create Fulu block and convert blob sidecars to data column sidecars.
|
||||
fuluForkSlot := fuluForkEpoch * params.BeaconConfig().SlotsPerEpoch
|
||||
fuluBlock, fuluBlobSidecars := util.GenerateTestElectraBlockWithSidecar(t, [fieldparams.RootLength]byte{}, fuluForkSlot, blobCount)
|
||||
fuluBlockRoot := fuluBlock.Root()
|
||||
|
||||
cellsAndProofsList := make([]kzg.CellsAndProofs, 0, len(fuluBlobSidecars))
|
||||
for _, blob := range fuluBlobSidecars {
|
||||
var kzgBlob kzg.Blob
|
||||
copy(kzgBlob[:], blob.Blob)
|
||||
cellsAndProofs, err := kzg.ComputeCellsAndKZGProofs(&kzgBlob)
|
||||
require.NoError(t, err)
|
||||
cellsAndProofsList = append(cellsAndProofsList, cellsAndProofs)
|
||||
}
|
||||
|
||||
roDataColumnSidecars, err := peerdas.DataColumnSidecars(cellsAndProofsList, peerdas.PopulateFromBlock(fuluBlock))
|
||||
require.NoError(t, err)
|
||||
|
||||
verifiedRoDataColumnSidecars := make([]blocks.VerifiedRODataColumn, 0, len(roDataColumnSidecars))
|
||||
for _, roDataColumn := range roDataColumnSidecars {
|
||||
verifiedRoDataColumn := blocks.NewVerifiedRODataColumn(roDataColumn)
|
||||
verifiedRoDataColumnSidecars = append(verifiedRoDataColumnSidecars, verifiedRoDataColumn)
|
||||
}
|
||||
|
||||
err = db.SaveBlock(t.Context(), fuluBlock)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, dataColumnStorage := filesystem.NewEphemeralDataColumnStorageAndFs(t)
|
||||
err = dataColumnStorage.Save(verifiedRoDataColumnSidecars)
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Run("pre-fulu fork", func(t *testing.T) {
|
||||
setupPreFulu(t)
|
||||
|
||||
// Create a block at slot 123 (before Fulu fork since FuluForkEpoch is set to MaxUint64)
|
||||
preFuluBlock := util.NewBeaconBlock()
|
||||
preFuluBlock.Block.Slot = 123
|
||||
util.SaveBlock(t, ctx, db, preFuluBlock)
|
||||
|
||||
blocker := &BeaconDbBlocker{
|
||||
GenesisTimeFetcher: &testutil.MockGenesisTimeFetcher{
|
||||
Genesis: time.Now(),
|
||||
},
|
||||
ChainInfoFetcher: &mockChain.ChainService{},
|
||||
BeaconDB: db,
|
||||
}
|
||||
|
||||
_, rpcErr := blocker.DataColumns(ctx, "123", nil)
|
||||
require.NotNil(t, rpcErr)
|
||||
require.Equal(t, core.ErrorReason(core.BadRequest), rpcErr.Reason)
|
||||
require.StringContains(t, "not supported before Fulu fork", rpcErr.Err.Error())
|
||||
})
|
||||
|
||||
t.Run("genesis", func(t *testing.T) {
|
||||
setupFulu(t)
|
||||
|
||||
blocker := &BeaconDbBlocker{
|
||||
GenesisTimeFetcher: &testutil.MockGenesisTimeFetcher{
|
||||
Genesis: time.Now(),
|
||||
},
|
||||
ChainInfoFetcher: &mockChain.ChainService{},
|
||||
}
|
||||
|
||||
_, rpcErr := blocker.DataColumns(ctx, "genesis", nil)
|
||||
require.NotNil(t, rpcErr)
|
||||
require.Equal(t, http.StatusBadRequest, core.ErrorReasonToHTTP(rpcErr.Reason))
|
||||
require.StringContains(t, "not supported for Phase 0 fork", rpcErr.Err.Error())
|
||||
})
|
||||
|
||||
t.Run("head", func(t *testing.T) {
|
||||
setupFulu(t)
|
||||
|
||||
blocker := &BeaconDbBlocker{
|
||||
ChainInfoFetcher: &mockChain.ChainService{
|
||||
Root: fuluBlockRoot[:],
|
||||
Block: fuluBlock,
|
||||
},
|
||||
GenesisTimeFetcher: &testutil.MockGenesisTimeFetcher{
|
||||
Genesis: time.Now(),
|
||||
},
|
||||
BeaconDB: db,
|
||||
DataColumnStorage: dataColumnStorage,
|
||||
}
|
||||
|
||||
retrievedDataColumns, rpcErr := blocker.DataColumns(ctx, "head", nil)
|
||||
require.IsNil(t, rpcErr)
|
||||
require.Equal(t, len(verifiedRoDataColumnSidecars), len(retrievedDataColumns))
|
||||
|
||||
// Create a map of expected indices for easier verification
|
||||
expectedIndices := make(map[uint64]bool)
|
||||
for _, expected := range verifiedRoDataColumnSidecars {
|
||||
expectedIndices[expected.RODataColumn.DataColumnSidecar.Index] = true
|
||||
}
|
||||
|
||||
// Verify we got data columns with the expected indices
|
||||
for _, actual := range retrievedDataColumns {
|
||||
require.Equal(t, true, expectedIndices[actual.RODataColumn.DataColumnSidecar.Index])
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("finalized", func(t *testing.T) {
|
||||
setupFulu(t)
|
||||
|
||||
blocker := &BeaconDbBlocker{
|
||||
ChainInfoFetcher: &mockChain.ChainService{FinalizedCheckPoint: ðpb.Checkpoint{Root: fuluBlockRoot[:]}},
|
||||
GenesisTimeFetcher: &testutil.MockGenesisTimeFetcher{
|
||||
Genesis: time.Now(),
|
||||
},
|
||||
BeaconDB: db,
|
||||
DataColumnStorage: dataColumnStorage,
|
||||
}
|
||||
|
||||
retrievedDataColumns, rpcErr := blocker.DataColumns(ctx, "finalized", nil)
|
||||
require.IsNil(t, rpcErr)
|
||||
require.Equal(t, len(verifiedRoDataColumnSidecars), len(retrievedDataColumns))
|
||||
})
|
||||
|
||||
t.Run("justified", func(t *testing.T) {
|
||||
setupFulu(t)
|
||||
|
||||
blocker := &BeaconDbBlocker{
|
||||
ChainInfoFetcher: &mockChain.ChainService{CurrentJustifiedCheckPoint: ðpb.Checkpoint{Root: fuluBlockRoot[:]}},
|
||||
GenesisTimeFetcher: &testutil.MockGenesisTimeFetcher{
|
||||
Genesis: time.Now(),
|
||||
},
|
||||
BeaconDB: db,
|
||||
DataColumnStorage: dataColumnStorage,
|
||||
}
|
||||
|
||||
retrievedDataColumns, rpcErr := blocker.DataColumns(ctx, "justified", nil)
|
||||
require.IsNil(t, rpcErr)
|
||||
require.Equal(t, len(verifiedRoDataColumnSidecars), len(retrievedDataColumns))
|
||||
})
|
||||
|
||||
t.Run("root", func(t *testing.T) {
|
||||
setupFulu(t)
|
||||
|
||||
blocker := &BeaconDbBlocker{
|
||||
GenesisTimeFetcher: &testutil.MockGenesisTimeFetcher{
|
||||
Genesis: time.Now(),
|
||||
},
|
||||
BeaconDB: db,
|
||||
DataColumnStorage: dataColumnStorage,
|
||||
}
|
||||
|
||||
retrievedDataColumns, rpcErr := blocker.DataColumns(ctx, hexutil.Encode(fuluBlockRoot[:]), nil)
|
||||
require.IsNil(t, rpcErr)
|
||||
require.Equal(t, len(verifiedRoDataColumnSidecars), len(retrievedDataColumns))
|
||||
})
|
||||
|
||||
t.Run("slot", func(t *testing.T) {
|
||||
setupFulu(t)
|
||||
|
||||
blocker := &BeaconDbBlocker{
|
||||
GenesisTimeFetcher: &testutil.MockGenesisTimeFetcher{
|
||||
Genesis: time.Now(),
|
||||
},
|
||||
ChainInfoFetcher: &mockChain.ChainService{},
|
||||
BeaconDB: db,
|
||||
DataColumnStorage: dataColumnStorage,
|
||||
}
|
||||
|
||||
slotStr := fmt.Sprintf("%d", fuluForkSlot)
|
||||
retrievedDataColumns, rpcErr := blocker.DataColumns(ctx, slotStr, nil)
|
||||
require.IsNil(t, rpcErr)
|
||||
require.Equal(t, len(verifiedRoDataColumnSidecars), len(retrievedDataColumns))
|
||||
})
|
||||
|
||||
t.Run("specific indices", func(t *testing.T) {
|
||||
setupFulu(t)
|
||||
|
||||
blocker := &BeaconDbBlocker{
|
||||
GenesisTimeFetcher: &testutil.MockGenesisTimeFetcher{
|
||||
Genesis: time.Now(),
|
||||
},
|
||||
BeaconDB: db,
|
||||
DataColumnStorage: dataColumnStorage,
|
||||
}
|
||||
|
||||
// Request specific indices (first 3 data columns)
|
||||
indices := []int{0, 1, 2}
|
||||
retrievedDataColumns, rpcErr := blocker.DataColumns(ctx, hexutil.Encode(fuluBlockRoot[:]), indices)
|
||||
require.IsNil(t, rpcErr)
|
||||
require.Equal(t, 3, len(retrievedDataColumns))
|
||||
|
||||
for i, dataColumn := range retrievedDataColumns {
|
||||
require.Equal(t, uint64(indices[i]), dataColumn.RODataColumn.DataColumnSidecar.Index)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("no data columns returns empty array", func(t *testing.T) {
|
||||
setupFulu(t)
|
||||
|
||||
_, emptyDataColumnStorage := filesystem.NewEphemeralDataColumnStorageAndFs(t)
|
||||
|
||||
blocker := &BeaconDbBlocker{
|
||||
GenesisTimeFetcher: &testutil.MockGenesisTimeFetcher{
|
||||
Genesis: time.Now(),
|
||||
},
|
||||
BeaconDB: db,
|
||||
DataColumnStorage: emptyDataColumnStorage,
|
||||
}
|
||||
|
||||
retrievedDataColumns, rpcErr := blocker.DataColumns(ctx, hexutil.Encode(fuluBlockRoot[:]), nil)
|
||||
require.IsNil(t, rpcErr)
|
||||
require.Equal(t, 0, len(retrievedDataColumns))
|
||||
})
|
||||
|
||||
t.Run("index too big", func(t *testing.T) {
|
||||
setupFulu(t)
|
||||
|
||||
blocker := &BeaconDbBlocker{
|
||||
GenesisTimeFetcher: &testutil.MockGenesisTimeFetcher{
|
||||
Genesis: time.Now(),
|
||||
},
|
||||
BeaconDB: db,
|
||||
DataColumnStorage: dataColumnStorage,
|
||||
}
|
||||
|
||||
_, rpcErr := blocker.DataColumns(ctx, hexutil.Encode(fuluBlockRoot[:]), []int{0, math.MaxInt})
|
||||
require.NotNil(t, rpcErr)
|
||||
require.Equal(t, core.ErrorReason(core.BadRequest), rpcErr.Reason)
|
||||
})
|
||||
|
||||
t.Run("outside retention period", func(t *testing.T) {
|
||||
setupFulu(t)
|
||||
|
||||
// Create a data column storage with very short retention period
|
||||
shortRetentionStorage, err := filesystem.NewDataColumnStorage(ctx,
|
||||
filesystem.WithDataColumnBasePath(t.TempDir()),
|
||||
filesystem.WithDataColumnRetentionEpochs(1), // Only 1 epoch retention
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Mock genesis time to make current slot much later than the block slot
|
||||
// This simulates being outside retention period
|
||||
genesisTime := time.Now().Add(-time.Duration(fuluForkSlot+1000) * time.Duration(params.BeaconConfig().SecondsPerSlot) * time.Second)
|
||||
blocker := &BeaconDbBlocker{
|
||||
GenesisTimeFetcher: &testutil.MockGenesisTimeFetcher{
|
||||
Genesis: genesisTime,
|
||||
},
|
||||
BeaconDB: db,
|
||||
DataColumnStorage: shortRetentionStorage,
|
||||
}
|
||||
|
||||
// Since the block is outside retention period, should return empty array
|
||||
retrievedDataColumns, rpcErr := blocker.DataColumns(ctx, hexutil.Encode(fuluBlockRoot[:]), nil)
|
||||
require.IsNil(t, rpcErr)
|
||||
require.Equal(t, 0, len(retrievedDataColumns))
|
||||
})
|
||||
|
||||
t.Run("block not found", func(t *testing.T) {
|
||||
setupFulu(t)
|
||||
|
||||
blocker := &BeaconDbBlocker{
|
||||
GenesisTimeFetcher: &testutil.MockGenesisTimeFetcher{
|
||||
Genesis: time.Now(),
|
||||
},
|
||||
BeaconDB: db,
|
||||
DataColumnStorage: dataColumnStorage,
|
||||
}
|
||||
|
||||
nonExistentRoot := bytesutil.PadTo([]byte("nonexistent"), 32)
|
||||
_, rpcErr := blocker.DataColumns(ctx, hexutil.Encode(nonExistentRoot), nil)
|
||||
require.NotNil(t, rpcErr)
|
||||
require.Equal(t, core.ErrorReason(core.NotFound), rpcErr.Reason)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -324,18 +324,18 @@ func (vs *Server) ProposeBeaconBlock(ctx context.Context, req *ethpb.GenericSign
|
||||
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
if err := vs.broadcastReceiveBlock(ctx, block, root); err != nil {
|
||||
if err := vs.broadcastReceiveBlock(ctx, &wg, block, root); err != nil {
|
||||
errChan <- errors.Wrap(err, "broadcast/receive block failed")
|
||||
return
|
||||
}
|
||||
errChan <- nil
|
||||
}()
|
||||
|
||||
wg.Wait()
|
||||
|
||||
if err := vs.broadcastAndReceiveSidecars(ctx, block, root, blobSidecars, dataColumnSidecars); err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "Could not broadcast/receive sidecars: %v", err)
|
||||
}
|
||||
wg.Wait()
|
||||
if err := <-errChan; err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "Could not broadcast/receive block: %v", err)
|
||||
}
|
||||
@@ -432,7 +432,26 @@ func (vs *Server) handleUnblindedBlock(
|
||||
}
|
||||
|
||||
// broadcastReceiveBlock broadcasts a block and handles its reception.
|
||||
func (vs *Server) broadcastReceiveBlock(ctx context.Context, block interfaces.SignedBeaconBlock, root [fieldparams.RootLength]byte) error {
|
||||
func (vs *Server) broadcastReceiveBlock(ctx context.Context, wg *sync.WaitGroup, block interfaces.SignedBeaconBlock, root [fieldparams.RootLength]byte) error {
|
||||
if err := vs.broadcastBlock(ctx, wg, block, root); err != nil {
|
||||
return errors.Wrap(err, "broadcast block")
|
||||
}
|
||||
|
||||
vs.BlockNotifier.BlockFeed().Send(&feed.Event{
|
||||
Type: blockfeed.ReceivedBlock,
|
||||
Data: &blockfeed.ReceivedBlockData{SignedBlock: block},
|
||||
})
|
||||
|
||||
if err := vs.BlockReceiver.ReceiveBlock(ctx, block, root, nil); err != nil {
|
||||
return errors.Wrap(err, "receive block")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (vs *Server) broadcastBlock(ctx context.Context, wg *sync.WaitGroup, block interfaces.SignedBeaconBlock, root [fieldparams.RootLength]byte) error {
|
||||
defer wg.Done()
|
||||
|
||||
protoBlock, err := block.Proto()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "protobuf conversion failed")
|
||||
@@ -440,11 +459,13 @@ func (vs *Server) broadcastReceiveBlock(ctx context.Context, block interfaces.Si
|
||||
if err := vs.P2P.Broadcast(ctx, protoBlock); err != nil {
|
||||
return errors.Wrap(err, "broadcast failed")
|
||||
}
|
||||
vs.BlockNotifier.BlockFeed().Send(&feed.Event{
|
||||
Type: blockfeed.ReceivedBlock,
|
||||
Data: &blockfeed.ReceivedBlockData{SignedBlock: block},
|
||||
})
|
||||
return vs.BlockReceiver.ReceiveBlock(ctx, block, root, nil)
|
||||
|
||||
log.WithFields(logrus.Fields{
|
||||
"slot": block.Block().Slot(),
|
||||
"root": fmt.Sprintf("%#x", root),
|
||||
}).Debug("Broadcasted block")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// broadcastAndReceiveBlobs handles the broadcasting and reception of blob sidecars.
|
||||
@@ -498,10 +519,6 @@ func (vs *Server) broadcastAndReceiveDataColumns(
|
||||
})
|
||||
}
|
||||
|
||||
if err := eg.Wait(); err != nil {
|
||||
return errors.Wrap(err, "wait for data columns to be broadcasted")
|
||||
}
|
||||
|
||||
if err := vs.DataColumnReceiver.ReceiveDataColumns(verifiedRODataColumns); err != nil {
|
||||
return errors.Wrap(err, "receive data column")
|
||||
}
|
||||
@@ -512,6 +529,11 @@ func (vs *Server) broadcastAndReceiveDataColumns(
|
||||
Data: &operation.DataColumnSidecarReceivedData{DataColumn: &verifiedRODataColumn}, // #nosec G601
|
||||
})
|
||||
}
|
||||
|
||||
if err := eg.Wait(); err != nil {
|
||||
return errors.Wrap(err, "wait for data columns to be broadcasted")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -145,7 +145,7 @@ func NewService(ctx context.Context, cfg *Config) *Service {
|
||||
log.WithError(err).Errorf("Could not listen to port in Start() %s", address)
|
||||
}
|
||||
s.listener = lis
|
||||
log.WithField("address", address).Info("gRPC server listening on port")
|
||||
log.WithField("address", address).Info("Beacon chain gRPC server listening")
|
||||
|
||||
opts := []grpc.ServerOption{
|
||||
grpc.StatsHandler(otelgrpc.NewServerHandler()),
|
||||
@@ -351,7 +351,7 @@ func (s *Service) Stop() error {
|
||||
s.cancel()
|
||||
if s.listener != nil {
|
||||
s.grpcServer.GracefulStop()
|
||||
log.Debug("Initiated graceful stop of gRPC server")
|
||||
log.Debug("Completed graceful stop of beacon-chain gRPC server")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -42,8 +42,9 @@ func TestLifecycle_OK(t *testing.T) {
|
||||
|
||||
rpcService.Start()
|
||||
|
||||
require.LogsContain(t, hook, "listening on port")
|
||||
require.LogsContain(t, hook, "Beacon chain gRPC server listening")
|
||||
assert.NoError(t, rpcService.Stop())
|
||||
require.LogsContain(t, hook, "Completed graceful stop of beacon-chain gRPC server")
|
||||
}
|
||||
|
||||
func TestStatus_CredentialError(t *testing.T) {
|
||||
@@ -84,7 +85,7 @@ func TestRPC_InsecureEndpoint(t *testing.T) {
|
||||
|
||||
rpcService.Start()
|
||||
|
||||
require.LogsContain(t, hook, "listening on port")
|
||||
require.LogsContain(t, hook, "Beacon chain gRPC server listening")
|
||||
require.LogsContain(t, hook, "You are using an insecure gRPC server")
|
||||
assert.NoError(t, rpcService.Stop())
|
||||
}
|
||||
|
||||
@@ -14,10 +14,13 @@ import (
|
||||
|
||||
// MockBlocker is a fake implementation of lookup.Blocker.
|
||||
type MockBlocker struct {
|
||||
BlockToReturn interfaces.ReadOnlySignedBeaconBlock
|
||||
ErrorToReturn error
|
||||
SlotBlockMap map[primitives.Slot]interfaces.ReadOnlySignedBeaconBlock
|
||||
RootBlockMap map[[32]byte]interfaces.ReadOnlySignedBeaconBlock
|
||||
BlockToReturn interfaces.ReadOnlySignedBeaconBlock
|
||||
ErrorToReturn error
|
||||
SlotBlockMap map[primitives.Slot]interfaces.ReadOnlySignedBeaconBlock
|
||||
RootBlockMap map[[32]byte]interfaces.ReadOnlySignedBeaconBlock
|
||||
DataColumnsFunc func(ctx context.Context, id string, indices []int) ([]blocks.VerifiedRODataColumn, *core.RpcError)
|
||||
DataColumnsToReturn []blocks.VerifiedRODataColumn
|
||||
DataColumnsErrorToReturn *core.RpcError
|
||||
}
|
||||
|
||||
// Block --
|
||||
@@ -40,3 +43,17 @@ func (m *MockBlocker) Block(_ context.Context, b []byte) (interfaces.ReadOnlySig
|
||||
func (*MockBlocker) Blobs(_ context.Context, _ string, _ ...options.BlobsOption) ([]*blocks.VerifiedROBlob, *core.RpcError) {
|
||||
return nil, &core.RpcError{}
|
||||
}
|
||||
|
||||
// DataColumns --
|
||||
func (m *MockBlocker) DataColumns(ctx context.Context, id string, indices []int) ([]blocks.VerifiedRODataColumn, *core.RpcError) {
|
||||
if m.DataColumnsFunc != nil {
|
||||
return m.DataColumnsFunc(ctx, id, indices)
|
||||
}
|
||||
if m.DataColumnsErrorToReturn != nil {
|
||||
return nil, m.DataColumnsErrorToReturn
|
||||
}
|
||||
if m.DataColumnsToReturn != nil {
|
||||
return m.DataColumnsToReturn, nil
|
||||
}
|
||||
return nil, &core.RpcError{}
|
||||
}
|
||||
|
||||
@@ -63,6 +63,7 @@ type ReadOnlyBeaconState interface {
|
||||
ReadOnlyDeposits
|
||||
ReadOnlyConsolidations
|
||||
ReadOnlyProposerLookahead
|
||||
ReadOnlyGloas
|
||||
ToProtoUnsafe() interface{}
|
||||
ToProto() interface{}
|
||||
GenesisTime() time.Time
|
||||
@@ -98,6 +99,7 @@ type WriteOnlyBeaconState interface {
|
||||
WriteOnlyWithdrawals
|
||||
WriteOnlyDeposits
|
||||
WriteOnlyProposerLookahead
|
||||
WriteOnlyGloas
|
||||
SetGenesisTime(val time.Time) error
|
||||
SetGenesisValidatorsRoot(val []byte) error
|
||||
SetSlot(val primitives.Slot) error
|
||||
@@ -266,6 +268,8 @@ type WriteOnlyEth1Data interface {
|
||||
SetEth1DepositIndex(val uint64) error
|
||||
ExitEpochAndUpdateChurn(exitBalance primitives.Gwei) (primitives.Epoch, error)
|
||||
ExitEpochAndUpdateChurnForTotalBal(totalActiveBalance primitives.Gwei, exitBalance primitives.Gwei) (primitives.Epoch, error)
|
||||
SetExitBalanceToConsume(val primitives.Gwei) error
|
||||
SetEarliestExitEpoch(val primitives.Epoch) error
|
||||
}
|
||||
|
||||
// WriteOnlyValidators defines a struct which only has write access to validators methods.
|
||||
@@ -333,6 +337,7 @@ type WriteOnlyWithdrawals interface {
|
||||
DequeuePendingPartialWithdrawals(num uint64) error
|
||||
SetNextWithdrawalIndex(i uint64) error
|
||||
SetNextWithdrawalValidatorIndex(i primitives.ValidatorIndex) error
|
||||
SetPendingPartialWithdrawals(val []*ethpb.PendingPartialWithdrawal) error
|
||||
}
|
||||
|
||||
type WriteOnlyConsolidations interface {
|
||||
@@ -353,6 +358,24 @@ type WriteOnlyProposerLookahead interface {
|
||||
SetProposerLookahead([]primitives.ValidatorIndex) error
|
||||
}
|
||||
|
||||
type ReadOnlyGloas interface {
|
||||
ExecutionPayloadAvailability() []byte
|
||||
BuilderPendingPayments() []*ethpb.BuilderPendingPayment
|
||||
BuilderPendingWithdrawals() []*ethpb.BuilderPendingWithdrawal
|
||||
LatestBlockHash() [32]byte
|
||||
LatestWithdrawalsRoot() [32]byte
|
||||
ExecutionPayloadBid() *ethpb.ExecutionPayloadBid
|
||||
}
|
||||
|
||||
type WriteOnlyGloas interface {
|
||||
SetExecutionPayloadAvailability([]byte) error
|
||||
SetBuilderPendingPayments([]*ethpb.BuilderPendingPayment) error
|
||||
SetBuilderPendingWithdrawals([]*ethpb.BuilderPendingWithdrawal) error
|
||||
SetLatestBlockHash([32]byte) error
|
||||
SetLatestWithdrawalsRoot([32]byte) error
|
||||
SetExecutionPayloadBid(*ethpb.ExecutionPayloadBid) error
|
||||
}
|
||||
|
||||
func IsNil(s BeaconState) bool {
|
||||
return s == nil || s.IsNil()
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ go_library(
|
||||
"getters_deposits.go",
|
||||
"getters_eth1.go",
|
||||
"getters_exit.go",
|
||||
"getters_gloas.go",
|
||||
"getters_misc.go",
|
||||
"getters_participation.go",
|
||||
"getters_payload_header.go",
|
||||
@@ -23,6 +24,7 @@ go_library(
|
||||
"getters_sync_committee.go",
|
||||
"getters_validator.go",
|
||||
"getters_withdrawal.go",
|
||||
"gloas.go",
|
||||
"hasher.go",
|
||||
"multi_value_slices.go",
|
||||
"proofs.go",
|
||||
@@ -35,6 +37,7 @@ go_library(
|
||||
"setters_deposit_requests.go",
|
||||
"setters_deposits.go",
|
||||
"setters_eth1.go",
|
||||
"setters_gloas.go",
|
||||
"setters_misc.go",
|
||||
"setters_participation.go",
|
||||
"setters_payload_header.go",
|
||||
|
||||
@@ -70,6 +70,14 @@ type BeaconState struct {
|
||||
pendingConsolidations []*ethpb.PendingConsolidation // pending_consolidations: List[PendingConsolidation, PENDING_CONSOLIDATIONS_LIMIT]
|
||||
proposerLookahead []primitives.ValidatorIndex // proposer_look_ahead: List[uint64, (MIN_LOOKAHEAD + 1)*SLOTS_PER_EPOCH]
|
||||
|
||||
// Gloas fields
|
||||
executionPayloadbid *ethpb.ExecutionPayloadBid
|
||||
executionPayloadAvailability []byte
|
||||
builderPendingPayments []*ethpb.BuilderPendingPayment
|
||||
builderPendingWithdrawals []*ethpb.BuilderPendingWithdrawal
|
||||
latestBlockHash []byte
|
||||
latestWithdrawalsRoot []byte
|
||||
|
||||
id uint64
|
||||
lock sync.RWMutex
|
||||
dirtyFields map[types.FieldIndex]bool
|
||||
@@ -125,6 +133,12 @@ type beaconStateMarshalable struct {
|
||||
PendingPartialWithdrawals []*ethpb.PendingPartialWithdrawal `json:"pending_partial_withdrawals" yaml:"pending_partial_withdrawals"`
|
||||
PendingConsolidations []*ethpb.PendingConsolidation `json:"pending_consolidations" yaml:"pending_consolidations"`
|
||||
ProposerLookahead []primitives.ValidatorIndex `json:"proposer_look_ahead" yaml:"proposer_look_ahead"`
|
||||
ExecutionPayloadBid *ethpb.ExecutionPayloadBid `json:"execution_payload_bid" yaml:"execution_payload_bid"`
|
||||
ExecutionPayloadAvailability []byte `json:"execution_payload_availability" yaml:"execution_payload_availability"`
|
||||
BuilderPendingPayments []*ethpb.BuilderPendingPayment `json:"builder_pending_payments" yaml:"builder_pending_payments"`
|
||||
BuilderPendingWithdrawals []*ethpb.BuilderPendingWithdrawal `json:"builder_pending_withdrawals" yaml:"builder_pending_withdrawals"`
|
||||
LatestBlockHash []byte `json:"latest_block_hash" yaml:"latest_block_hash"`
|
||||
LatestWithdrawalsRoot []byte `json:"latest_withdrawals_root" yaml:"latest_withdrawals_root"`
|
||||
}
|
||||
|
||||
func (b *BeaconState) MarshalJSON() ([]byte, error) {
|
||||
@@ -179,6 +193,12 @@ func (b *BeaconState) MarshalJSON() ([]byte, error) {
|
||||
PendingPartialWithdrawals: b.pendingPartialWithdrawals,
|
||||
PendingConsolidations: b.pendingConsolidations,
|
||||
ProposerLookahead: b.proposerLookahead,
|
||||
ExecutionPayloadBid: b.executionPayloadbid,
|
||||
ExecutionPayloadAvailability: b.executionPayloadAvailability,
|
||||
BuilderPendingPayments: b.builderPendingPayments,
|
||||
BuilderPendingWithdrawals: b.builderPendingWithdrawals,
|
||||
LatestBlockHash: b.latestBlockHash,
|
||||
LatestWithdrawalsRoot: b.latestWithdrawalsRoot,
|
||||
}
|
||||
return json.Marshal(marshalable)
|
||||
}
|
||||
|
||||
80
beacon-chain/state/state-native/getters_gloas.go
Normal file
80
beacon-chain/state/state-native/getters_gloas.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package state_native
|
||||
|
||||
import (
|
||||
"slices"
|
||||
|
||||
ethpb "github.com/OffchainLabs/prysm/v6/proto/prysm/v1alpha1"
|
||||
"github.com/OffchainLabs/prysm/v6/runtime/version"
|
||||
)
|
||||
|
||||
// ExecutionPayloadAvailability is a non-mutating call to the beacon state which returns the
|
||||
// execution payload availability.
|
||||
func (b *BeaconState) ExecutionPayloadAvailability() []byte {
|
||||
if b.version < version.Gloas {
|
||||
return nil
|
||||
}
|
||||
b.lock.RLock()
|
||||
defer b.lock.RUnlock()
|
||||
return slices.Clone(b.executionPayloadAvailability)
|
||||
}
|
||||
|
||||
// LatestBlockHash is a non-mutating call to the beacon state which returns the
|
||||
// latest block hash.
|
||||
func (b *BeaconState) LatestBlockHash() [32]byte {
|
||||
if b.version < version.Gloas {
|
||||
return [32]byte{}
|
||||
}
|
||||
b.lock.RLock()
|
||||
defer b.lock.RUnlock()
|
||||
var hash [32]byte
|
||||
|
||||
copy(hash[:], b.latestBlockHash)
|
||||
return hash
|
||||
}
|
||||
|
||||
// LatestWithdrawalsRoot is a non-mutating call to the beacon state which returns the
|
||||
// latest withdrawals root.
|
||||
func (b *BeaconState) LatestWithdrawalsRoot() [32]byte {
|
||||
if b.version < version.Gloas {
|
||||
return [32]byte{}
|
||||
}
|
||||
b.lock.RLock()
|
||||
defer b.lock.RUnlock()
|
||||
|
||||
var root [32]byte
|
||||
copy(root[:], b.latestWithdrawalsRoot)
|
||||
return root
|
||||
}
|
||||
|
||||
// BuilderPendingPayments is a non-mutating call to the beacon state which returns the
|
||||
// builder pending payments.
|
||||
func (b *BeaconState) BuilderPendingPayments() []*ethpb.BuilderPendingPayment {
|
||||
if b.version < version.Gloas {
|
||||
return nil
|
||||
}
|
||||
b.lock.RLock()
|
||||
defer b.lock.RUnlock()
|
||||
return b.builderPendingPaymentsVal()
|
||||
}
|
||||
|
||||
// BuilderPendingWithdrawals is a non-mutating call to the beacon state which returns the
|
||||
// builder pending withdrawals.
|
||||
func (b *BeaconState) BuilderPendingWithdrawals() []*ethpb.BuilderPendingWithdrawal {
|
||||
if b.version < version.Gloas {
|
||||
return nil
|
||||
}
|
||||
b.lock.RLock()
|
||||
defer b.lock.RUnlock()
|
||||
return b.builderPendingWithdrawalsVal()
|
||||
}
|
||||
|
||||
// ExecutionPayloadBid is a non-mutating call to the beacon state which returns the
|
||||
// execution payload bid.
|
||||
func (b *BeaconState) ExecutionPayloadBid() *ethpb.ExecutionPayloadBid {
|
||||
if b.version < version.Gloas {
|
||||
return nil
|
||||
}
|
||||
b.lock.RLock()
|
||||
defer b.lock.RUnlock()
|
||||
return b.executionPayloadBidVal()
|
||||
}
|
||||
@@ -259,6 +259,56 @@ func (b *BeaconState) ToProtoUnsafe() interface{} {
|
||||
PendingConsolidations: b.pendingConsolidations,
|
||||
ProposerLookahead: lookahead,
|
||||
}
|
||||
case version.Gloas:
|
||||
lookahead := make([]uint64, len(b.proposerLookahead))
|
||||
for i, v := range b.proposerLookahead {
|
||||
lookahead[i] = uint64(v)
|
||||
}
|
||||
return ðpb.BeaconStateGloas{
|
||||
GenesisTime: b.genesisTime,
|
||||
GenesisValidatorsRoot: gvrCopy[:],
|
||||
Slot: b.slot,
|
||||
Fork: b.fork,
|
||||
LatestBlockHeader: b.latestBlockHeader,
|
||||
BlockRoots: br,
|
||||
StateRoots: sr,
|
||||
HistoricalRoots: b.historicalRoots.Slice(),
|
||||
Eth1Data: b.eth1Data,
|
||||
Eth1DataVotes: b.eth1DataVotes,
|
||||
Eth1DepositIndex: b.eth1DepositIndex,
|
||||
Validators: vals,
|
||||
Balances: bals,
|
||||
RandaoMixes: rm,
|
||||
Slashings: b.slashings,
|
||||
PreviousEpochParticipation: b.previousEpochParticipation,
|
||||
CurrentEpochParticipation: b.currentEpochParticipation,
|
||||
JustificationBits: b.justificationBits,
|
||||
PreviousJustifiedCheckpoint: b.previousJustifiedCheckpoint,
|
||||
CurrentJustifiedCheckpoint: b.currentJustifiedCheckpoint,
|
||||
FinalizedCheckpoint: b.finalizedCheckpoint,
|
||||
InactivityScores: inactivityScores,
|
||||
CurrentSyncCommittee: b.currentSyncCommittee,
|
||||
NextSyncCommittee: b.nextSyncCommittee,
|
||||
NextWithdrawalIndex: b.nextWithdrawalIndex,
|
||||
NextWithdrawalValidatorIndex: b.nextWithdrawalValidatorIndex,
|
||||
HistoricalSummaries: b.historicalSummaries,
|
||||
DepositRequestsStartIndex: b.depositRequestsStartIndex,
|
||||
DepositBalanceToConsume: b.depositBalanceToConsume,
|
||||
ExitBalanceToConsume: b.exitBalanceToConsume,
|
||||
EarliestExitEpoch: b.earliestExitEpoch,
|
||||
ConsolidationBalanceToConsume: b.consolidationBalanceToConsume,
|
||||
EarliestConsolidationEpoch: b.earliestConsolidationEpoch,
|
||||
PendingDeposits: b.pendingDeposits,
|
||||
PendingPartialWithdrawals: b.pendingPartialWithdrawals,
|
||||
PendingConsolidations: b.pendingConsolidations,
|
||||
ProposerLookahead: lookahead,
|
||||
ExecutionPayloadAvailability: b.executionPayloadAvailability,
|
||||
BuilderPendingPayments: b.builderPendingPayments,
|
||||
BuilderPendingWithdrawals: b.builderPendingWithdrawals,
|
||||
LatestBlockHash: b.latestBlockHash,
|
||||
LatestWithdrawalsRoot: b.latestWithdrawalsRoot,
|
||||
LatestExecutionPayloadBid: b.executionPayloadbid,
|
||||
}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
@@ -510,6 +560,56 @@ func (b *BeaconState) ToProto() interface{} {
|
||||
PendingConsolidations: b.pendingConsolidationsVal(),
|
||||
ProposerLookahead: lookahead,
|
||||
}
|
||||
case version.Gloas:
|
||||
lookahead := make([]uint64, len(b.proposerLookahead))
|
||||
for i, v := range b.proposerLookahead {
|
||||
lookahead[i] = uint64(v)
|
||||
}
|
||||
return ðpb.BeaconStateGloas{
|
||||
GenesisTime: b.genesisTime,
|
||||
GenesisValidatorsRoot: gvrCopy[:],
|
||||
Slot: b.slot,
|
||||
Fork: b.forkVal(),
|
||||
LatestBlockHeader: b.latestBlockHeaderVal(),
|
||||
BlockRoots: br,
|
||||
StateRoots: sr,
|
||||
HistoricalRoots: b.historicalRoots.Slice(),
|
||||
Eth1Data: b.eth1DataVal(),
|
||||
Eth1DataVotes: b.eth1DataVotesVal(),
|
||||
Eth1DepositIndex: b.eth1DepositIndex,
|
||||
Validators: b.validatorsVal(),
|
||||
Balances: b.balancesVal(),
|
||||
RandaoMixes: rm,
|
||||
Slashings: b.slashingsVal(),
|
||||
PreviousEpochParticipation: b.previousEpochParticipationVal(),
|
||||
CurrentEpochParticipation: b.currentEpochParticipationVal(),
|
||||
JustificationBits: b.justificationBitsVal(),
|
||||
PreviousJustifiedCheckpoint: b.previousJustifiedCheckpointVal(),
|
||||
CurrentJustifiedCheckpoint: b.currentJustifiedCheckpointVal(),
|
||||
FinalizedCheckpoint: b.finalizedCheckpointVal(),
|
||||
InactivityScores: b.inactivityScoresVal(),
|
||||
CurrentSyncCommittee: b.currentSyncCommitteeVal(),
|
||||
NextSyncCommittee: b.nextSyncCommitteeVal(),
|
||||
LatestExecutionPayloadBid: b.executionPayloadbid.Copy(),
|
||||
NextWithdrawalIndex: b.nextWithdrawalIndex,
|
||||
NextWithdrawalValidatorIndex: b.nextWithdrawalValidatorIndex,
|
||||
HistoricalSummaries: b.historicalSummariesVal(),
|
||||
DepositRequestsStartIndex: b.depositRequestsStartIndex,
|
||||
DepositBalanceToConsume: b.depositBalanceToConsume,
|
||||
ExitBalanceToConsume: b.exitBalanceToConsume,
|
||||
EarliestExitEpoch: b.earliestExitEpoch,
|
||||
ConsolidationBalanceToConsume: b.consolidationBalanceToConsume,
|
||||
EarliestConsolidationEpoch: b.earliestConsolidationEpoch,
|
||||
PendingDeposits: b.pendingDepositsVal(),
|
||||
PendingPartialWithdrawals: b.pendingPartialWithdrawalsVal(),
|
||||
PendingConsolidations: b.pendingConsolidationsVal(),
|
||||
ProposerLookahead: lookahead,
|
||||
ExecutionPayloadAvailability: b.executionPayloadAvailabilityVal(),
|
||||
BuilderPendingPayments: b.builderPendingPaymentsVal(),
|
||||
BuilderPendingWithdrawals: b.builderPendingWithdrawalsVal(),
|
||||
LatestBlockHash: b.latestBlockHashVal(),
|
||||
LatestWithdrawalsRoot: b.latestWithdrawalsRootVal(),
|
||||
}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ func TestNextWithdrawalValidatorIndex(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExpectedWithdrawals(t *testing.T) {
|
||||
for _, stateVersion := range []int{version.Capella, version.Deneb, version.Electra, version.Fulu} {
|
||||
for _, stateVersion := range []int{version.Capella, version.Deneb, version.Electra, version.Fulu, version.Gloas} {
|
||||
t.Run(version.String(stateVersion), func(t *testing.T) {
|
||||
t.Run("no withdrawals", func(t *testing.T) {
|
||||
s := state_native.EmptyStateFromVersion(t, stateVersion)
|
||||
|
||||
77
beacon-chain/state/state-native/gloas.go
Normal file
77
beacon-chain/state/state-native/gloas.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package state_native
|
||||
|
||||
import (
|
||||
ethpb "github.com/OffchainLabs/prysm/v6/proto/prysm/v1alpha1"
|
||||
)
|
||||
|
||||
// executionPayloadAvailabilityVal returns a copy of the execution payload availability.
|
||||
// This assumes that a lock is already held on BeaconState.
|
||||
func (b *BeaconState) executionPayloadAvailabilityVal() []byte {
|
||||
if b.executionPayloadAvailability == nil {
|
||||
return nil
|
||||
}
|
||||
availability := make([]byte, len(b.executionPayloadAvailability))
|
||||
copy(availability, b.executionPayloadAvailability)
|
||||
return availability
|
||||
}
|
||||
|
||||
// builderPendingPaymentsVal returns a copy of the builder pending payments.
|
||||
// This assumes that a lock is already held on BeaconState.
|
||||
func (b *BeaconState) builderPendingPaymentsVal() []*ethpb.BuilderPendingPayment {
|
||||
if b.builderPendingPayments == nil {
|
||||
return nil
|
||||
}
|
||||
payments := make([]*ethpb.BuilderPendingPayment, len(b.builderPendingPayments))
|
||||
for i, payment := range b.builderPendingPayments {
|
||||
if payment != nil {
|
||||
payments[i] = payment.Copy()
|
||||
}
|
||||
}
|
||||
return payments
|
||||
}
|
||||
|
||||
// builderPendingWithdrawalsVal returns a copy of the builder pending withdrawals.
|
||||
// This assumes that a lock is already held on BeaconState.
|
||||
func (b *BeaconState) builderPendingWithdrawalsVal() []*ethpb.BuilderPendingWithdrawal {
|
||||
if b.builderPendingWithdrawals == nil {
|
||||
return nil
|
||||
}
|
||||
withdrawals := make([]*ethpb.BuilderPendingWithdrawal, len(b.builderPendingWithdrawals))
|
||||
for i, withdrawal := range b.builderPendingWithdrawals {
|
||||
if withdrawal != nil {
|
||||
withdrawals[i] = withdrawal.Copy()
|
||||
}
|
||||
}
|
||||
return withdrawals
|
||||
}
|
||||
|
||||
// latestBlockHashVal returns a copy of the latest block hash.
|
||||
// This assumes that a lock is already held on BeaconState.
|
||||
func (b *BeaconState) latestBlockHashVal() []byte {
|
||||
if b.latestBlockHash == nil {
|
||||
return nil
|
||||
}
|
||||
hash := make([]byte, len(b.latestBlockHash))
|
||||
copy(hash, b.latestBlockHash)
|
||||
return hash
|
||||
}
|
||||
|
||||
// latestWithdrawalsRootVal returns a copy of the latest withdrawals root.
|
||||
// This assumes that a lock is already held on BeaconState.
|
||||
func (b *BeaconState) latestWithdrawalsRootVal() []byte {
|
||||
if b.latestWithdrawalsRoot == nil {
|
||||
return nil
|
||||
}
|
||||
root := make([]byte, len(b.latestWithdrawalsRoot))
|
||||
copy(root, b.latestWithdrawalsRoot)
|
||||
return root
|
||||
}
|
||||
|
||||
// executionPayloadBidVal returns a copy of the execution payload bid.
|
||||
// This assumes that a lock is already held on BeaconState.
|
||||
func (b *BeaconState) executionPayloadBidVal() *ethpb.ExecutionPayloadBid {
|
||||
if b.executionPayloadbid == nil {
|
||||
return nil
|
||||
}
|
||||
return b.executionPayloadbid.Copy()
|
||||
}
|
||||
@@ -43,6 +43,8 @@ func ComputeFieldRootsWithHasher(ctx context.Context, state *BeaconState) ([][]b
|
||||
fieldRoots = make([][]byte, params.BeaconConfig().BeaconStateElectraFieldCount)
|
||||
case version.Fulu:
|
||||
fieldRoots = make([][]byte, params.BeaconConfig().BeaconStateFuluFieldCount)
|
||||
case version.Gloas:
|
||||
fieldRoots = make([][]byte, params.BeaconConfig().BeaconStateGloasFieldCount)
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown state version %s", version.String(state.version))
|
||||
}
|
||||
@@ -245,7 +247,7 @@ func ComputeFieldRootsWithHasher(ctx context.Context, state *BeaconState) ([][]b
|
||||
fieldRoots[types.LatestExecutionPayloadHeaderCapella.RealPosition()] = executionPayloadRoot[:]
|
||||
}
|
||||
|
||||
if state.version >= version.Deneb {
|
||||
if state.version >= version.Deneb && state.version < version.Gloas {
|
||||
// Execution payload root.
|
||||
executionPayloadRoot, err := state.latestExecutionPayloadHeaderDeneb.HashTreeRoot()
|
||||
if err != nil {
|
||||
@@ -254,6 +256,15 @@ func ComputeFieldRootsWithHasher(ctx context.Context, state *BeaconState) ([][]b
|
||||
fieldRoots[types.LatestExecutionPayloadHeaderDeneb.RealPosition()] = executionPayloadRoot[:]
|
||||
}
|
||||
|
||||
if state.version == version.Gloas {
|
||||
// Execution payload bid root for Gloas.
|
||||
executionPayloadRoot, err := state.executionPayloadbid.HashTreeRoot()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fieldRoots[types.ExecutionPayloadBid.RealPosition()] = executionPayloadRoot[:]
|
||||
}
|
||||
|
||||
if state.version >= version.Capella {
|
||||
// Next withdrawal index root.
|
||||
nextWithdrawalIndexRoot := make([]byte, 32)
|
||||
@@ -328,5 +339,44 @@ func ComputeFieldRootsWithHasher(ctx context.Context, state *BeaconState) ([][]b
|
||||
}
|
||||
fieldRoots[types.ProposerLookahead.RealPosition()] = proposerLookaheadRoot[:]
|
||||
}
|
||||
|
||||
if state.version >= version.Gloas {
|
||||
// Pack execution payload availability bitvector into 32-byte chunks
|
||||
// Calculate number of chunks based on actual field size (1024 bytes for mainnet, 8 bytes for minimal)
|
||||
numChunks := (len(state.executionPayloadAvailability) + 31) / 32
|
||||
chunks := make([][32]byte, numChunks)
|
||||
for i := 0; i < numChunks; i++ {
|
||||
start := i * 32
|
||||
end := start + 32
|
||||
if end > len(state.executionPayloadAvailability) {
|
||||
end = len(state.executionPayloadAvailability)
|
||||
}
|
||||
copy(chunks[i][:], state.executionPayloadAvailability[start:end])
|
||||
}
|
||||
limit := uint64(len(chunks)) // For fixed bitvector, limit equals number of chunks
|
||||
epaRoot, err := ssz.BitwiseMerkleize(chunks, uint64(len(chunks)), limit)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not merkleize execution payload availability")
|
||||
}
|
||||
fieldRoots[types.ExecutionPayloadAvailability.RealPosition()] = epaRoot[:]
|
||||
|
||||
bppRoot, err := stateutil.BuilderPendingPaymentsRoot(state.builderPendingPayments)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not compute builder pending payments merkleization")
|
||||
}
|
||||
fieldRoots[types.BuilderPendingPayments.RealPosition()] = bppRoot[:]
|
||||
|
||||
bpwRoot, err := stateutil.BuilderPendingWithdrawalsRoot(state.builderPendingWithdrawals)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not compute builder pending withdrawals merkleization")
|
||||
}
|
||||
fieldRoots[types.BuilderPendingWithdrawals.RealPosition()] = bpwRoot[:]
|
||||
|
||||
lbhRoot := bytesutil.ToBytes32(state.latestBlockHash)
|
||||
fieldRoots[types.LatestBlockHash.RealPosition()] = lbhRoot[:]
|
||||
|
||||
lwrRoot := bytesutil.ToBytes32(state.latestWithdrawalsRoot)
|
||||
fieldRoots[types.LatestWithdrawalsRoot.RealPosition()] = lwrRoot[:]
|
||||
}
|
||||
return fieldRoots, nil
|
||||
}
|
||||
|
||||
@@ -91,3 +91,33 @@ func (b *BeaconState) exitEpochAndUpdateChurn(totalActiveBalance primitives.Gwei
|
||||
|
||||
return b.earliestExitEpoch, nil
|
||||
}
|
||||
|
||||
// SetExitBalanceToConsume sets the exit balance to consume. This method mutates the state.
|
||||
func (b *BeaconState) SetExitBalanceToConsume(exitBalanceToConsume primitives.Gwei) error {
|
||||
if b.version < version.Electra {
|
||||
return errNotSupported("SetExitBalanceToConsume", b.version)
|
||||
}
|
||||
|
||||
b.lock.Lock()
|
||||
defer b.lock.Unlock()
|
||||
|
||||
b.exitBalanceToConsume = exitBalanceToConsume
|
||||
b.markFieldAsDirty(types.ExitBalanceToConsume)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetEarliestExitEpoch sets the earliest exit epoch. This method mutates the state.
|
||||
func (b *BeaconState) SetEarliestExitEpoch(earliestExitEpoch primitives.Epoch) error {
|
||||
if b.version < version.Electra {
|
||||
return errNotSupported("SetEarliestExitEpoch", b.version)
|
||||
}
|
||||
|
||||
b.lock.Lock()
|
||||
defer b.lock.Unlock()
|
||||
|
||||
b.earliestExitEpoch = earliestExitEpoch
|
||||
b.markFieldAsDirty(types.EarliestExitEpoch)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
136
beacon-chain/state/state-native/setters_gloas.go
Normal file
136
beacon-chain/state/state-native/setters_gloas.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package state_native
|
||||
|
||||
import (
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/state/state-native/types"
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/state/stateutil"
|
||||
fieldparams "github.com/OffchainLabs/prysm/v6/config/fieldparams"
|
||||
ethpb "github.com/OffchainLabs/prysm/v6/proto/prysm/v1alpha1"
|
||||
"github.com/OffchainLabs/prysm/v6/runtime/version"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// SetExecutionPayloadAvailability is a mutating call to the beacon state which sets the
|
||||
// execution payload availability.
|
||||
func (b *BeaconState) SetExecutionPayloadAvailability(availability []byte) error {
|
||||
if b.version < version.Gloas {
|
||||
return errNotSupported("SetExecutionPayloadAvailability", b.version)
|
||||
}
|
||||
expectedLength := fieldparams.BlockRootsLength / 8
|
||||
if len(availability) != expectedLength {
|
||||
return errors.Errorf("invalid length for execution payload availability: got %d, expected %d", len(availability), expectedLength)
|
||||
}
|
||||
b.lock.Lock()
|
||||
defer b.lock.Unlock()
|
||||
b.sharedFieldReferences[types.ExecutionPayloadAvailability].MinusRef()
|
||||
b.sharedFieldReferences[types.ExecutionPayloadAvailability] = stateutil.NewRef(1)
|
||||
|
||||
b.executionPayloadAvailability = availability
|
||||
|
||||
b.markFieldAsDirty(types.ExecutionPayloadAvailability)
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetBuilderPendingPayments is a mutating call to the beacon state which sets the
|
||||
// builder pending payments.
|
||||
func (b *BeaconState) SetBuilderPendingPayments(payments []*ethpb.BuilderPendingPayment) error {
|
||||
if b.version < version.Gloas {
|
||||
return errNotSupported("SetBuilderPendingPayments", b.version)
|
||||
}
|
||||
expectedLength := 2 * fieldparams.SlotsPerEpoch
|
||||
if len(payments) != expectedLength {
|
||||
return errors.Errorf("invalid length for builder pending payments: got %d, expected %d", len(payments), expectedLength)
|
||||
}
|
||||
b.lock.Lock()
|
||||
defer b.lock.Unlock()
|
||||
b.sharedFieldReferences[types.BuilderPendingPayments].MinusRef()
|
||||
b.sharedFieldReferences[types.BuilderPendingPayments] = stateutil.NewRef(1)
|
||||
|
||||
// Create deep copies of the payments
|
||||
b.builderPendingPayments = make([]*ethpb.BuilderPendingPayment, len(payments))
|
||||
for i, payment := range payments {
|
||||
if payment != nil {
|
||||
b.builderPendingPayments[i] = payment.Copy()
|
||||
}
|
||||
}
|
||||
|
||||
b.markFieldAsDirty(types.BuilderPendingPayments)
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetBuilderPendingWithdrawals is a mutating call to the beacon state which sets the
|
||||
// builder pending withdrawals.
|
||||
func (b *BeaconState) SetBuilderPendingWithdrawals(withdrawals []*ethpb.BuilderPendingWithdrawal) error {
|
||||
if b.version < version.Gloas {
|
||||
return errNotSupported("SetBuilderPendingWithdrawals", b.version)
|
||||
}
|
||||
b.lock.Lock()
|
||||
defer b.lock.Unlock()
|
||||
b.sharedFieldReferences[types.BuilderPendingWithdrawals].MinusRef()
|
||||
b.sharedFieldReferences[types.BuilderPendingWithdrawals] = stateutil.NewRef(1)
|
||||
|
||||
// Create deep copies of the withdrawals
|
||||
b.builderPendingWithdrawals = make([]*ethpb.BuilderPendingWithdrawal, len(withdrawals))
|
||||
for i, withdrawal := range withdrawals {
|
||||
if withdrawal != nil {
|
||||
b.builderPendingWithdrawals[i] = withdrawal.Copy()
|
||||
}
|
||||
}
|
||||
|
||||
b.markFieldAsDirty(types.BuilderPendingWithdrawals)
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetLatestBlockHash is a mutating call to the beacon state which sets the
|
||||
// latest block hash.
|
||||
func (b *BeaconState) SetLatestBlockHash(hash [32]byte) error {
|
||||
if b.version < version.Gloas {
|
||||
return errNotSupported("SetLatestBlockHash", b.version)
|
||||
}
|
||||
b.lock.Lock()
|
||||
defer b.lock.Unlock()
|
||||
b.sharedFieldReferences[types.LatestBlockHash].MinusRef()
|
||||
b.sharedFieldReferences[types.LatestBlockHash] = stateutil.NewRef(1)
|
||||
|
||||
b.latestBlockHash = hash[:]
|
||||
|
||||
b.markFieldAsDirty(types.LatestBlockHash)
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetLatestWithdrawalsRoot is a mutating call to the beacon state which sets the
|
||||
// latest withdrawals root.
|
||||
func (b *BeaconState) SetLatestWithdrawalsRoot(root [32]byte) error {
|
||||
if b.version < version.Gloas {
|
||||
return errNotSupported("SetLatestWithdrawalsRoot", b.version)
|
||||
}
|
||||
b.lock.Lock()
|
||||
defer b.lock.Unlock()
|
||||
b.sharedFieldReferences[types.LatestWithdrawalsRoot].MinusRef()
|
||||
b.sharedFieldReferences[types.LatestWithdrawalsRoot] = stateutil.NewRef(1)
|
||||
|
||||
b.latestWithdrawalsRoot = root[:]
|
||||
|
||||
b.markFieldAsDirty(types.LatestWithdrawalsRoot)
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetExecutionPayloadBid is a mutating call to the beacon state which sets the
|
||||
// execution payload bid.
|
||||
func (b *BeaconState) SetExecutionPayloadBid(bid *ethpb.ExecutionPayloadBid) error {
|
||||
if b.version < version.Gloas {
|
||||
return errNotSupported("SetExecutionPayloadBid", b.version)
|
||||
}
|
||||
b.lock.Lock()
|
||||
defer b.lock.Unlock()
|
||||
b.sharedFieldReferences[types.ExecutionPayloadBid].MinusRef()
|
||||
b.sharedFieldReferences[types.ExecutionPayloadBid] = stateutil.NewRef(1)
|
||||
|
||||
if bid == nil {
|
||||
b.executionPayloadbid = nil
|
||||
} else {
|
||||
b.executionPayloadbid = bid.Copy()
|
||||
}
|
||||
|
||||
b.markFieldAsDirty(types.ExecutionPayloadBid)
|
||||
return nil
|
||||
}
|
||||
@@ -100,3 +100,22 @@ func (b *BeaconState) DequeuePendingPartialWithdrawals(n uint64) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetPendingPartialWithdrawals sets the pending partial withdrawals. This method mutates the state.
|
||||
func (b *BeaconState) SetPendingPartialWithdrawals(pendingPartialWithdrawals []*eth.PendingPartialWithdrawal) error {
|
||||
if b.version < version.Electra {
|
||||
return errNotSupported("SetPendingPartialWithdrawals", b.version)
|
||||
}
|
||||
|
||||
b.lock.Lock()
|
||||
defer b.lock.Unlock()
|
||||
|
||||
if pendingPartialWithdrawals == nil {
|
||||
return errors.New("cannot set nil pending partial withdrawals")
|
||||
}
|
||||
|
||||
b.pendingPartialWithdrawals = pendingPartialWithdrawals
|
||||
b.markFieldAsDirty(types.PendingPartialWithdrawals)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -112,6 +112,29 @@ var (
|
||||
electraFields,
|
||||
types.ProposerLookahead,
|
||||
)
|
||||
|
||||
gloasFields = append(
|
||||
altairFields,
|
||||
types.NextWithdrawalIndex,
|
||||
types.NextWithdrawalValidatorIndex,
|
||||
types.HistoricalSummaries,
|
||||
types.DepositRequestsStartIndex,
|
||||
types.DepositBalanceToConsume,
|
||||
types.ExitBalanceToConsume,
|
||||
types.EarliestExitEpoch,
|
||||
types.ConsolidationBalanceToConsume,
|
||||
types.EarliestConsolidationEpoch,
|
||||
types.PendingDeposits,
|
||||
types.PendingPartialWithdrawals,
|
||||
types.PendingConsolidations,
|
||||
types.ProposerLookahead,
|
||||
types.ExecutionPayloadBid,
|
||||
types.ExecutionPayloadAvailability,
|
||||
types.BuilderPendingPayments,
|
||||
types.BuilderPendingWithdrawals,
|
||||
types.LatestBlockHash,
|
||||
types.LatestWithdrawalsRoot,
|
||||
)
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -122,6 +145,7 @@ const (
|
||||
denebSharedFieldRefCount = 7
|
||||
electraSharedFieldRefCount = 10
|
||||
fuluSharedFieldRefCount = 11
|
||||
gloasSharedFieldRefCount = 13
|
||||
)
|
||||
|
||||
// InitializeFromProtoPhase0 the beacon state from a protobuf representation.
|
||||
@@ -159,6 +183,11 @@ func InitializeFromProtoFulu(st *ethpb.BeaconStateFulu) (state.BeaconState, erro
|
||||
return InitializeFromProtoUnsafeFulu(proto.Clone(st).(*ethpb.BeaconStateFulu))
|
||||
}
|
||||
|
||||
// InitializeFromProtoGloas the beacon state from a protobuf representation.
|
||||
func InitializeFromProtoGloas(st *ethpb.BeaconStateGloas) (state.BeaconState, error) {
|
||||
return InitializeFromProtoUnsafeGloas(proto.Clone(st).(*ethpb.BeaconStateGloas))
|
||||
}
|
||||
|
||||
// InitializeFromProtoUnsafePhase0 directly uses the beacon state protobuf fields
|
||||
// and sets them as fields of the BeaconState type.
|
||||
func InitializeFromProtoUnsafePhase0(st *ethpb.BeaconState) (state.BeaconState, error) {
|
||||
@@ -650,6 +679,11 @@ func InitializeFromProtoUnsafeFulu(st *ethpb.BeaconStateFulu) (state.BeaconState
|
||||
for i, v := range st.ProposerLookahead {
|
||||
proposerLookahead[i] = primitives.ValidatorIndex(v)
|
||||
}
|
||||
// Proposer lookahead must be exactly 2 * SLOTS_PER_EPOCH in length. We fill in with zeroes instead of erroring out here
|
||||
for i := len(proposerLookahead); i < 2*fieldparams.SlotsPerEpoch; i++ {
|
||||
proposerLookahead = append(proposerLookahead, 0)
|
||||
}
|
||||
|
||||
fieldCount := params.BeaconConfig().BeaconStateFuluFieldCount
|
||||
b := &BeaconState{
|
||||
version: version.Fulu,
|
||||
@@ -731,6 +765,109 @@ func InitializeFromProtoUnsafeFulu(st *ethpb.BeaconStateFulu) (state.BeaconState
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// InitializeFromProtoUnsafeGloas directly uses the beacon state protobuf fields
|
||||
// and sets them as fields of the BeaconState type.
|
||||
func InitializeFromProtoUnsafeGloas(st *ethpb.BeaconStateGloas) (state.BeaconState, error) {
|
||||
if st == nil {
|
||||
return nil, errors.New("received nil state")
|
||||
}
|
||||
|
||||
hRoots := customtypes.HistoricalRoots(make([][32]byte, len(st.HistoricalRoots)))
|
||||
for i, r := range st.HistoricalRoots {
|
||||
hRoots[i] = bytesutil.ToBytes32(r)
|
||||
}
|
||||
|
||||
proposerLookahead := make([]primitives.ValidatorIndex, len(st.ProposerLookahead))
|
||||
for i, v := range st.ProposerLookahead {
|
||||
proposerLookahead[i] = primitives.ValidatorIndex(v)
|
||||
}
|
||||
fieldCount := params.BeaconConfig().BeaconStateGloasFieldCount
|
||||
b := &BeaconState{
|
||||
version: version.Gloas,
|
||||
genesisTime: st.GenesisTime,
|
||||
genesisValidatorsRoot: bytesutil.ToBytes32(st.GenesisValidatorsRoot),
|
||||
slot: st.Slot,
|
||||
fork: st.Fork,
|
||||
latestBlockHeader: st.LatestBlockHeader,
|
||||
historicalRoots: hRoots,
|
||||
eth1Data: st.Eth1Data,
|
||||
eth1DataVotes: st.Eth1DataVotes,
|
||||
eth1DepositIndex: st.Eth1DepositIndex,
|
||||
slashings: st.Slashings,
|
||||
previousEpochParticipation: st.PreviousEpochParticipation,
|
||||
currentEpochParticipation: st.CurrentEpochParticipation,
|
||||
justificationBits: st.JustificationBits,
|
||||
previousJustifiedCheckpoint: st.PreviousJustifiedCheckpoint,
|
||||
currentJustifiedCheckpoint: st.CurrentJustifiedCheckpoint,
|
||||
finalizedCheckpoint: st.FinalizedCheckpoint,
|
||||
currentSyncCommittee: st.CurrentSyncCommittee,
|
||||
nextSyncCommittee: st.NextSyncCommittee,
|
||||
nextWithdrawalIndex: st.NextWithdrawalIndex,
|
||||
nextWithdrawalValidatorIndex: st.NextWithdrawalValidatorIndex,
|
||||
historicalSummaries: st.HistoricalSummaries,
|
||||
depositRequestsStartIndex: st.DepositRequestsStartIndex,
|
||||
depositBalanceToConsume: st.DepositBalanceToConsume,
|
||||
exitBalanceToConsume: st.ExitBalanceToConsume,
|
||||
earliestExitEpoch: st.EarliestExitEpoch,
|
||||
consolidationBalanceToConsume: st.ConsolidationBalanceToConsume,
|
||||
earliestConsolidationEpoch: st.EarliestConsolidationEpoch,
|
||||
pendingDeposits: st.PendingDeposits,
|
||||
pendingPartialWithdrawals: st.PendingPartialWithdrawals,
|
||||
pendingConsolidations: st.PendingConsolidations,
|
||||
proposerLookahead: proposerLookahead,
|
||||
executionPayloadbid: st.LatestExecutionPayloadBid,
|
||||
executionPayloadAvailability: st.ExecutionPayloadAvailability,
|
||||
builderPendingPayments: st.BuilderPendingPayments,
|
||||
builderPendingWithdrawals: st.BuilderPendingWithdrawals,
|
||||
latestBlockHash: st.LatestBlockHash,
|
||||
latestWithdrawalsRoot: st.LatestWithdrawalsRoot,
|
||||
dirtyFields: make(map[types.FieldIndex]bool, fieldCount),
|
||||
dirtyIndices: make(map[types.FieldIndex][]uint64, fieldCount),
|
||||
stateFieldLeaves: make(map[types.FieldIndex]*fieldtrie.FieldTrie, fieldCount),
|
||||
rebuildTrie: make(map[types.FieldIndex]bool, fieldCount),
|
||||
valMapHandler: stateutil.NewValMapHandler(st.Validators),
|
||||
}
|
||||
|
||||
b.blockRootsMultiValue = NewMultiValueBlockRoots(st.BlockRoots)
|
||||
b.stateRootsMultiValue = NewMultiValueStateRoots(st.StateRoots)
|
||||
b.randaoMixesMultiValue = NewMultiValueRandaoMixes(st.RandaoMixes)
|
||||
b.balancesMultiValue = NewMultiValueBalances(st.Balances)
|
||||
b.validatorsMultiValue = NewMultiValueValidators(st.Validators)
|
||||
b.inactivityScoresMultiValue = NewMultiValueInactivityScores(st.InactivityScores)
|
||||
b.sharedFieldReferences = make(map[types.FieldIndex]*stateutil.Reference, gloasSharedFieldRefCount)
|
||||
|
||||
for _, f := range gloasFields {
|
||||
b.dirtyFields[f] = true
|
||||
b.rebuildTrie[f] = true
|
||||
b.dirtyIndices[f] = []uint64{}
|
||||
trie, err := fieldtrie.NewFieldTrie(f, types.BasicArray, nil, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b.stateFieldLeaves[f] = trie
|
||||
}
|
||||
|
||||
// Initialize field reference tracking for shared data.
|
||||
b.sharedFieldReferences[types.HistoricalRoots] = stateutil.NewRef(1)
|
||||
b.sharedFieldReferences[types.Eth1DataVotes] = stateutil.NewRef(1)
|
||||
b.sharedFieldReferences[types.Slashings] = stateutil.NewRef(1)
|
||||
b.sharedFieldReferences[types.PreviousEpochParticipationBits] = stateutil.NewRef(1)
|
||||
b.sharedFieldReferences[types.CurrentEpochParticipationBits] = stateutil.NewRef(1)
|
||||
b.sharedFieldReferences[types.ExecutionPayloadBid] = stateutil.NewRef(1) // New in Gloas.
|
||||
b.sharedFieldReferences[types.HistoricalSummaries] = stateutil.NewRef(1)
|
||||
b.sharedFieldReferences[types.PendingDeposits] = stateutil.NewRef(1)
|
||||
b.sharedFieldReferences[types.PendingPartialWithdrawals] = stateutil.NewRef(1)
|
||||
b.sharedFieldReferences[types.PendingConsolidations] = stateutil.NewRef(1)
|
||||
b.sharedFieldReferences[types.ProposerLookahead] = stateutil.NewRef(1)
|
||||
b.sharedFieldReferences[types.BuilderPendingPayments] = stateutil.NewRef(1) // New in Gloas.
|
||||
b.sharedFieldReferences[types.BuilderPendingWithdrawals] = stateutil.NewRef(1) // New in Gloas.
|
||||
|
||||
state.Count.Inc()
|
||||
// Finalizer runs when dst is being destroyed in garbage collection.
|
||||
runtime.SetFinalizer(b, finalizerCleanup)
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// Copy returns a deep copy of the beacon state.
|
||||
func (b *BeaconState) Copy() state.BeaconState {
|
||||
b.lock.RLock()
|
||||
@@ -752,6 +889,8 @@ func (b *BeaconState) Copy() state.BeaconState {
|
||||
fieldCount = params.BeaconConfig().BeaconStateElectraFieldCount
|
||||
case version.Fulu:
|
||||
fieldCount = params.BeaconConfig().BeaconStateFuluFieldCount
|
||||
case version.Gloas:
|
||||
fieldCount = params.BeaconConfig().BeaconStateGloasFieldCount
|
||||
}
|
||||
|
||||
dst := &BeaconState{
|
||||
@@ -806,6 +945,12 @@ func (b *BeaconState) Copy() state.BeaconState {
|
||||
latestExecutionPayloadHeader: b.latestExecutionPayloadHeader.Copy(),
|
||||
latestExecutionPayloadHeaderCapella: b.latestExecutionPayloadHeaderCapella.Copy(),
|
||||
latestExecutionPayloadHeaderDeneb: b.latestExecutionPayloadHeaderDeneb.Copy(),
|
||||
executionPayloadbid: b.executionPayloadbid.Copy(),
|
||||
executionPayloadAvailability: b.executionPayloadAvailabilityVal(),
|
||||
builderPendingPayments: b.builderPendingPaymentsVal(),
|
||||
builderPendingWithdrawals: b.builderPendingWithdrawalsVal(),
|
||||
latestBlockHash: b.latestBlockHashVal(),
|
||||
latestWithdrawalsRoot: b.latestWithdrawalsRootVal(),
|
||||
|
||||
id: types.Enumerator.Inc(),
|
||||
|
||||
@@ -842,6 +987,8 @@ func (b *BeaconState) Copy() state.BeaconState {
|
||||
dst.sharedFieldReferences = make(map[types.FieldIndex]*stateutil.Reference, electraSharedFieldRefCount)
|
||||
case version.Fulu:
|
||||
dst.sharedFieldReferences = make(map[types.FieldIndex]*stateutil.Reference, fuluSharedFieldRefCount)
|
||||
case version.Gloas:
|
||||
dst.sharedFieldReferences = make(map[types.FieldIndex]*stateutil.Reference, gloasSharedFieldRefCount)
|
||||
}
|
||||
|
||||
for field, ref := range b.sharedFieldReferences {
|
||||
@@ -937,6 +1084,8 @@ func (b *BeaconState) initializeMerkleLayers(ctx context.Context) error {
|
||||
b.dirtyFields = make(map[types.FieldIndex]bool, params.BeaconConfig().BeaconStateElectraFieldCount)
|
||||
case version.Fulu:
|
||||
b.dirtyFields = make(map[types.FieldIndex]bool, params.BeaconConfig().BeaconStateFuluFieldCount)
|
||||
case version.Gloas:
|
||||
b.dirtyFields = make(map[types.FieldIndex]bool, params.BeaconConfig().BeaconStateGloasFieldCount)
|
||||
default:
|
||||
return fmt.Errorf("unknown state version (%s) when computing dirty fields in merklization", version.String(b.version))
|
||||
}
|
||||
@@ -1175,6 +1324,31 @@ func (b *BeaconState) rootSelector(ctx context.Context, field types.FieldIndex)
|
||||
return stateutil.PendingConsolidationsRoot(b.pendingConsolidations)
|
||||
case types.ProposerLookahead:
|
||||
return stateutil.ProposerLookaheadRoot(b.proposerLookahead)
|
||||
case types.ExecutionPayloadBid:
|
||||
return b.executionPayloadbid.HashTreeRoot()
|
||||
case types.ExecutionPayloadAvailability:
|
||||
// Calculate number of chunks based on actual field size (1024 bytes for mainnet, 8 bytes for minimal)
|
||||
numChunks := (len(b.executionPayloadAvailability) + 31) / 32
|
||||
chunks := make([][32]byte, numChunks)
|
||||
for i := 0; i < numChunks; i++ {
|
||||
start := i * 32
|
||||
end := start + 32
|
||||
if end > len(b.executionPayloadAvailability) {
|
||||
end = len(b.executionPayloadAvailability)
|
||||
}
|
||||
copy(chunks[i][:], b.executionPayloadAvailability[start:end])
|
||||
}
|
||||
limit := uint64(len(chunks)) // For fixed bitvector, limit equals number of chunks
|
||||
return ssz.BitwiseMerkleize(chunks, uint64(len(chunks)), limit)
|
||||
|
||||
case types.BuilderPendingPayments:
|
||||
return stateutil.BuilderPendingPaymentsRoot(b.builderPendingPayments)
|
||||
case types.BuilderPendingWithdrawals:
|
||||
return stateutil.BuilderPendingWithdrawalsRoot(b.builderPendingWithdrawals)
|
||||
case types.LatestBlockHash:
|
||||
return bytesutil.ToBytes32(b.latestBlockHash), nil
|
||||
case types.LatestWithdrawalsRoot:
|
||||
return bytesutil.ToBytes32(b.latestWithdrawalsRoot), nil
|
||||
}
|
||||
return [32]byte{}, errors.New("invalid field index provided")
|
||||
}
|
||||
|
||||
@@ -88,6 +88,8 @@ func (f FieldIndex) String() string {
|
||||
return "latestExecutionPayloadHeaderCapella"
|
||||
case LatestExecutionPayloadHeaderDeneb:
|
||||
return "latestExecutionPayloadHeaderDeneb"
|
||||
case ExecutionPayloadBid:
|
||||
return "executionPayloadBid"
|
||||
case NextWithdrawalIndex:
|
||||
return "nextWithdrawalIndex"
|
||||
case NextWithdrawalValidatorIndex:
|
||||
@@ -114,6 +116,16 @@ func (f FieldIndex) String() string {
|
||||
return "pendingConsolidations"
|
||||
case ProposerLookahead:
|
||||
return "proposerLookahead"
|
||||
case ExecutionPayloadAvailability:
|
||||
return "executionPayloadAvailability"
|
||||
case BuilderPendingPayments:
|
||||
return "builderPendingPayments"
|
||||
case BuilderPendingWithdrawals:
|
||||
return "builderPendingWithdrawals"
|
||||
case LatestBlockHash:
|
||||
return "latestBlockHash"
|
||||
case LatestWithdrawalsRoot:
|
||||
return "latestWithdrawalsRoot"
|
||||
default:
|
||||
return fmt.Sprintf("unknown field index number: %d", f)
|
||||
}
|
||||
@@ -171,7 +183,7 @@ func (f FieldIndex) RealPosition() int {
|
||||
return 22
|
||||
case NextSyncCommittee:
|
||||
return 23
|
||||
case LatestExecutionPayloadHeader, LatestExecutionPayloadHeaderCapella, LatestExecutionPayloadHeaderDeneb:
|
||||
case LatestExecutionPayloadHeader, LatestExecutionPayloadHeaderCapella, LatestExecutionPayloadHeaderDeneb, ExecutionPayloadBid:
|
||||
return 24
|
||||
case NextWithdrawalIndex:
|
||||
return 25
|
||||
@@ -199,6 +211,16 @@ func (f FieldIndex) RealPosition() int {
|
||||
return 36
|
||||
case ProposerLookahead:
|
||||
return 37
|
||||
case ExecutionPayloadAvailability:
|
||||
return 38
|
||||
case BuilderPendingPayments:
|
||||
return 39
|
||||
case BuilderPendingWithdrawals:
|
||||
return 40
|
||||
case LatestBlockHash:
|
||||
return 41
|
||||
case LatestWithdrawalsRoot:
|
||||
return 42
|
||||
default:
|
||||
return -1
|
||||
}
|
||||
@@ -251,6 +273,7 @@ const (
|
||||
LatestExecutionPayloadHeader
|
||||
LatestExecutionPayloadHeaderCapella
|
||||
LatestExecutionPayloadHeaderDeneb
|
||||
ExecutionPayloadBid // Gloas: EIP-7732
|
||||
NextWithdrawalIndex
|
||||
NextWithdrawalValidatorIndex
|
||||
HistoricalSummaries
|
||||
@@ -264,6 +287,11 @@ const (
|
||||
PendingPartialWithdrawals // Electra: EIP-7251
|
||||
PendingConsolidations // Electra: EIP-7251
|
||||
ProposerLookahead // Fulu: EIP-7917
|
||||
ExecutionPayloadAvailability // Gloas: EIP-7732
|
||||
BuilderPendingPayments // Gloas: EIP-7732
|
||||
BuilderPendingWithdrawals // Gloas: EIP-7732
|
||||
LatestBlockHash // Gloas: EIP-7732
|
||||
LatestWithdrawalsRoot // Gloas: EIP-7732
|
||||
)
|
||||
|
||||
// Enumerator keeps track of the number of states created since the node's start.
|
||||
|
||||
@@ -4,6 +4,8 @@ go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"block_header_root.go",
|
||||
"builder_pending_payments_root.go",
|
||||
"builder_pending_withdrawals_root.go",
|
||||
"eth1_root.go",
|
||||
"field_root_attestation.go",
|
||||
"field_root_eth1.go",
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package stateutil
|
||||
|
||||
import (
|
||||
"github.com/OffchainLabs/prysm/v6/encoding/ssz"
|
||||
ethpb "github.com/OffchainLabs/prysm/v6/proto/prysm/v1alpha1"
|
||||
)
|
||||
|
||||
func BuilderPendingPaymentsRoot(slice []*ethpb.BuilderPendingPayment) ([32]byte, error) {
|
||||
vectorSize := len(slice)
|
||||
roots := make([][32]byte, vectorSize)
|
||||
for i := 0; i < len(slice) && i < vectorSize; i++ {
|
||||
r, err := slice[i].HashTreeRoot()
|
||||
if err != nil {
|
||||
return [32]byte{}, err
|
||||
}
|
||||
roots[i] = r
|
||||
}
|
||||
return ssz.MerkleizeVector(roots, uint64(len(roots))), nil
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package stateutil
|
||||
|
||||
import (
|
||||
"github.com/OffchainLabs/prysm/v6/encoding/ssz"
|
||||
ethpb "github.com/OffchainLabs/prysm/v6/proto/prysm/v1alpha1"
|
||||
)
|
||||
|
||||
func BuilderPendingWithdrawalsRoot(slice []*ethpb.BuilderPendingWithdrawal) ([32]byte, error) {
|
||||
return ssz.SliceRoot(slice, 1048576)
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/blockchain"
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/core/helpers"
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/core/peerdas"
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/db/filesystem"
|
||||
prysmP2P "github.com/OffchainLabs/prysm/v6/beacon-chain/p2p"
|
||||
@@ -187,7 +188,7 @@ func requestSidecarsFromStorage(
|
||||
requestedIndicesMap map[uint64]bool,
|
||||
roots map[[fieldparams.RootLength]byte]bool,
|
||||
) (map[[fieldparams.RootLength]byte][]blocks.VerifiedRODataColumn, error) {
|
||||
requestedIndices := sortedSliceFromMap(requestedIndicesMap)
|
||||
requestedIndices := helpers.SortedSliceFromMap(requestedIndicesMap)
|
||||
|
||||
result := make(map[[fieldparams.RootLength]byte][]blocks.VerifiedRODataColumn, len(roots))
|
||||
|
||||
@@ -599,7 +600,7 @@ func assembleAvailableSidecarsForRoot(
|
||||
root [fieldparams.RootLength]byte,
|
||||
indices map[uint64]bool,
|
||||
) ([]blocks.VerifiedRODataColumn, error) {
|
||||
stored, err := storage.Get(root, sortedSliceFromMap(indices))
|
||||
stored, err := storage.Get(root, helpers.SortedSliceFromMap(indices))
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "storage get for root %#x", root)
|
||||
}
|
||||
@@ -802,25 +803,27 @@ func sendDataColumnSidecarsRequest(
|
||||
roDataColumns = append(roDataColumns, localRoDataColumns...)
|
||||
}
|
||||
|
||||
prettyByRangeRequests := make([]map[string]any, 0, len(byRangeRequests))
|
||||
for _, request := range byRangeRequests {
|
||||
prettyRequest := map[string]any{
|
||||
"startSlot": request.StartSlot,
|
||||
"count": request.Count,
|
||||
"columns": request.Columns,
|
||||
if logrus.GetLevel() >= logrus.DebugLevel {
|
||||
prettyByRangeRequests := make([]map[string]any, 0, len(byRangeRequests))
|
||||
for _, request := range byRangeRequests {
|
||||
prettyRequest := map[string]any{
|
||||
"startSlot": request.StartSlot,
|
||||
"count": request.Count,
|
||||
"columns": helpers.PrettySlice(request.Columns),
|
||||
}
|
||||
|
||||
prettyByRangeRequests = append(prettyByRangeRequests, prettyRequest)
|
||||
}
|
||||
|
||||
prettyByRangeRequests = append(prettyByRangeRequests, prettyRequest)
|
||||
log.WithFields(logrus.Fields{
|
||||
"respondedSidecars": len(roDataColumns),
|
||||
"requestCount": len(byRangeRequests),
|
||||
"type": "byRange",
|
||||
"duration": time.Since(start),
|
||||
"requests": prettyByRangeRequests,
|
||||
}).Debug("Received data column sidecars")
|
||||
}
|
||||
|
||||
log.WithFields(logrus.Fields{
|
||||
"respondedSidecars": len(roDataColumns),
|
||||
"requestCount": len(byRangeRequests),
|
||||
"type": "byRange",
|
||||
"duration": time.Since(start),
|
||||
"requests": prettyByRangeRequests,
|
||||
}).Debug("Received data column sidecars")
|
||||
|
||||
return roDataColumns, nil
|
||||
}
|
||||
|
||||
@@ -895,7 +898,7 @@ func buildByRangeRequests(
|
||||
}
|
||||
}
|
||||
|
||||
columns := sortedSliceFromMap(reference)
|
||||
columns := helpers.SortedSliceFromMap(reference)
|
||||
startSlot, endSlot := slots[0], slots[len(slots)-1]
|
||||
totalCount := uint64(endSlot - startSlot + 1)
|
||||
|
||||
@@ -920,7 +923,7 @@ func buildByRootRequest(indicesByRoot map[[fieldparams.RootLength]byte]map[uint6
|
||||
for root, indices := range indicesByRoot {
|
||||
identifier := ð.DataColumnsByRootIdentifier{
|
||||
BlockRoot: root[:],
|
||||
Columns: sortedSliceFromMap(indices),
|
||||
Columns: helpers.SortedSliceFromMap(indices),
|
||||
}
|
||||
identifiers = append(identifiers, identifier)
|
||||
}
|
||||
@@ -1173,17 +1176,6 @@ func compareIndices(left, right map[uint64]bool) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// sortedSliceFromMap converts a map[uint64]bool to a sorted slice of keys.
|
||||
func sortedSliceFromMap(m map[uint64]bool) []uint64 {
|
||||
result := make([]uint64, 0, len(m))
|
||||
for k := range m {
|
||||
result = append(result, k)
|
||||
}
|
||||
|
||||
slices.Sort(result)
|
||||
return result
|
||||
}
|
||||
|
||||
// computeTotalCount calculates the total count of indices across all roots.
|
||||
func computeTotalCount(input map[[fieldparams.RootLength]byte]map[uint64]bool) int {
|
||||
totalCount := 0
|
||||
|
||||
@@ -1007,13 +1007,6 @@ func TestCompareIndices(t *testing.T) {
|
||||
require.Equal(t, true, compareIndices(left, right))
|
||||
}
|
||||
|
||||
func TestSlortedSliceFromMap(t *testing.T) {
|
||||
input := map[uint64]bool{54: true, 23: true, 35: true}
|
||||
expected := []uint64{23, 35, 54}
|
||||
actual := sortedSliceFromMap(input)
|
||||
require.DeepEqual(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestComputeTotalCount(t *testing.T) {
|
||||
input := map[[fieldparams.RootLength]byte]map[uint64]bool{
|
||||
[fieldparams.RootLength]byte{1}: {1: true, 3: true},
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/core/helpers"
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/core/peerdas"
|
||||
fieldparams "github.com/OffchainLabs/prysm/v6/config/fieldparams"
|
||||
"github.com/OffchainLabs/prysm/v6/config/params"
|
||||
@@ -95,7 +96,7 @@ func (s *Service) processDataColumnSidecarsFromReconstruction(ctx context.Contex
|
||||
"slot": slot,
|
||||
"proposerIndex": proposerIndex,
|
||||
"count": len(unseenIndices),
|
||||
"indices": sortedSliceFromMap(unseenIndices),
|
||||
"indices": helpers.SortedPrettySliceFromMap(unseenIndices),
|
||||
"duration": duration,
|
||||
}).Debug("Reconstructed data column sidecars")
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ go_library(
|
||||
"//beacon-chain/blockchain:go_default_library",
|
||||
"//beacon-chain/core/feed/block:go_default_library",
|
||||
"//beacon-chain/core/feed/state:go_default_library",
|
||||
"//beacon-chain/core/helpers:go_default_library",
|
||||
"//beacon-chain/core/peerdas:go_default_library",
|
||||
"//beacon-chain/core/transition:go_default_library",
|
||||
"//beacon-chain/das:go_default_library",
|
||||
|
||||
@@ -3,12 +3,12 @@ package initialsync
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/core/helpers"
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/core/peerdas"
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/db"
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/db/filesystem"
|
||||
@@ -430,9 +430,9 @@ func (f *blocksFetcher) fetchSidecars(ctx context.Context, pid peer.ID, peers []
|
||||
}
|
||||
|
||||
if len(missingIndicesByRoot) > 0 {
|
||||
prettyMissingIndicesByRoot := make(map[string][]uint64, len(missingIndicesByRoot))
|
||||
prettyMissingIndicesByRoot := make(map[string]string, len(missingIndicesByRoot))
|
||||
for root, indices := range missingIndicesByRoot {
|
||||
prettyMissingIndicesByRoot[fmt.Sprintf("%#x", root)] = sortedSliceFromMap(indices)
|
||||
prettyMissingIndicesByRoot[fmt.Sprintf("%#x", root)] = helpers.SortedPrettySliceFromMap(indices)
|
||||
}
|
||||
return "", errors.Errorf("some sidecars are still missing after fetch: %v", prettyMissingIndicesByRoot)
|
||||
}
|
||||
@@ -727,17 +727,6 @@ func (f *blocksFetcher) fetchBlobsFromPeer(ctx context.Context, bwb []blocks.Blo
|
||||
return "", errNoPeersAvailable
|
||||
}
|
||||
|
||||
// sortedSliceFromMap returns a sorted slice of keys from a map.
|
||||
func sortedSliceFromMap(m map[uint64]bool) []uint64 {
|
||||
result := make([]uint64, 0, len(m))
|
||||
for k := range m {
|
||||
result = append(result, k)
|
||||
}
|
||||
|
||||
slices.Sort(result)
|
||||
return result
|
||||
}
|
||||
|
||||
// requestBlocks is a wrapper for handling BeaconBlocksByRangeRequest requests/streams.
|
||||
func (f *blocksFetcher) requestBlocks(
|
||||
ctx context.Context,
|
||||
|
||||
@@ -1349,14 +1349,6 @@ func TestBlockFetcher_HasSufficientBandwidth(t *testing.T) {
|
||||
assert.Equal(t, 2, len(receivedPeers))
|
||||
}
|
||||
|
||||
func TestSortedSliceFromMap(t *testing.T) {
|
||||
m := map[uint64]bool{1: true, 3: true, 2: true, 4: true}
|
||||
expected := []uint64{1, 2, 3, 4}
|
||||
|
||||
actual := sortedSliceFromMap(m)
|
||||
require.DeepSSZEqual(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestFetchSidecars(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
t.Run("No blocks", func(t *testing.T) {
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/blockchain"
|
||||
blockfeed "github.com/OffchainLabs/prysm/v6/beacon-chain/core/feed/block"
|
||||
statefeed "github.com/OffchainLabs/prysm/v6/beacon-chain/core/feed/state"
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/core/helpers"
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/core/peerdas"
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/das"
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/db"
|
||||
@@ -479,7 +480,7 @@ func (s *Service) fetchOriginDataColumnSidecars(roBlock blocks.ROBlock, delay ti
|
||||
// Some sidecars are still missing.
|
||||
log := log.WithFields(logrus.Fields{
|
||||
"attempt": attempt,
|
||||
"missingIndices": sortedSliceFromMap(missingIndicesByRoot[root]),
|
||||
"missingIndices": helpers.SortedPrettySliceFromMap(missingIndicesByRoot[root]),
|
||||
"delay": delay,
|
||||
})
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/core/helpers"
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/core/peerdas"
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/db/filesystem"
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/execution"
|
||||
@@ -121,9 +122,9 @@ func (s *Service) requestAndSaveMissingDataColumnSidecars(blks []blocks.ROBlock)
|
||||
}
|
||||
|
||||
if len(missingIndicesByRoot) > 0 {
|
||||
prettyMissingIndicesByRoot := make(map[string][]uint64, len(missingIndicesByRoot))
|
||||
prettyMissingIndicesByRoot := make(map[string]string, len(missingIndicesByRoot))
|
||||
for root, indices := range missingIndicesByRoot {
|
||||
prettyMissingIndicesByRoot[fmt.Sprintf("%#x", root)] = sortedSliceFromMap(indices)
|
||||
prettyMissingIndicesByRoot[fmt.Sprintf("%#x", root)] = helpers.SortedPrettySliceFromMap(indices)
|
||||
}
|
||||
return errors.Errorf("some sidecars are still missing after fetch: %v", prettyMissingIndicesByRoot)
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"slices"
|
||||
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/blockchain"
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/core/helpers"
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/p2p"
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/p2p/encoder"
|
||||
p2ptypes "github.com/OffchainLabs/prysm/v6/beacon-chain/p2p/types"
|
||||
@@ -598,7 +599,7 @@ func isSidecarIndexRequested(request *ethpb.DataColumnSidecarsByRangeRequest) Da
|
||||
return func(sidecar blocks.RODataColumn) error {
|
||||
columnIndex := sidecar.Index
|
||||
if !requestedIndices[columnIndex] {
|
||||
requested := sortedSliceFromMap(requestedIndices)
|
||||
requested := helpers.SortedPrettySliceFromMap(requestedIndices)
|
||||
return errors.Errorf("data column sidecar index %d returned by the peer but not found in requested indices %v", columnIndex, requested)
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/blockchain"
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/core/helpers"
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/core/peerdas"
|
||||
"github.com/OffchainLabs/prysm/v6/beacon-chain/core/transition/interop"
|
||||
"github.com/OffchainLabs/prysm/v6/config/features"
|
||||
@@ -227,7 +228,7 @@ func (s *Service) processDataColumnSidecarsFromExecution(ctx context.Context, so
|
||||
"iteration": iteration,
|
||||
"type": source.Type(),
|
||||
"count": len(unseenIndices),
|
||||
"indices": sortedSliceFromMap(unseenIndices),
|
||||
"indices": helpers.SortedPrettySliceFromMap(unseenIndices),
|
||||
}).Debug("Constructed data column sidecars from the execution client")
|
||||
}
|
||||
|
||||
|
||||
3
changelog/hyunchel_fix-misleading-log-msg.md
Normal file
3
changelog/hyunchel_fix-misleading-log-msg.md
Normal file
@@ -0,0 +1,3 @@
|
||||
### Changed
|
||||
|
||||
- Clarified misleading log messages in beacon-chain/rpc/service gRPC module.
|
||||
3
changelog/james-prysm_debug-data-columns.md
Normal file
3
changelog/james-prysm_debug-data-columns.md
Normal file
@@ -0,0 +1,3 @@
|
||||
### Added
|
||||
|
||||
- Adding `/eth/v1/debug/beacon/data_column_sidecars/{block_id}` endpoint.
|
||||
3
changelog/james-prysm_fix-justified-blocker.md
Normal file
3
changelog/james-prysm_fix-justified-blocker.md
Normal file
@@ -0,0 +1,3 @@
|
||||
### Fixed
|
||||
|
||||
- Fixed 'justified' block support missing on blocker.Block and optimized logic between blocker.Block and blocker.Blob.
|
||||
3
changelog/jihoonsong_support-fulu-genesis.md
Normal file
3
changelog/jihoonsong_support-fulu-genesis.md
Normal file
@@ -0,0 +1,3 @@
|
||||
### Added
|
||||
|
||||
- Support Fulu genesis block.
|
||||
4
changelog/manu-broadcast.md
Normal file
4
changelog/manu-broadcast.md
Normal file
@@ -0,0 +1,4 @@
|
||||
### Changed
|
||||
- Broadcast block then sidecars, instead block and sidecars concurrently
|
||||
- Broadcast and receive sidecars in concurrently, instead sequentially
|
||||
|
||||
2
changelog/manu-fix-cgc-not-initialized.md
Normal file
2
changelog/manu-fix-cgc-not-initialized.md
Normal file
@@ -0,0 +1,2 @@
|
||||
### Fixed
|
||||
- In P2P service start, wait for the custody info to be correctly initialized.
|
||||
2
changelog/manu-logs-datacolumns.md
Normal file
2
changelog/manu-logs-datacolumns.md
Normal file
@@ -0,0 +1,2 @@
|
||||
## Changed
|
||||
- Improve logging of data column sidecars
|
||||
2
changelog/manu-wait.md
Normal file
2
changelog/manu-wait.md
Normal file
@@ -0,0 +1,2 @@
|
||||
### Fixed
|
||||
- `createLocalNode`: Wait before retrying to retrieve the custody group count if not present.
|
||||
3
changelog/muzry_fix_prysmctl_panics.md
Normal file
3
changelog/muzry_fix_prysmctl_panics.md
Normal file
@@ -0,0 +1,3 @@
|
||||
### Fixed
|
||||
|
||||
- Fix prysmctl panic when baseFee is not set in genesis.json
|
||||
3
changelog/muzry_fix_state_randao.md
Normal file
3
changelog/muzry_fix_state_randao.md
Normal file
@@ -0,0 +1,3 @@
|
||||
### Fixed
|
||||
|
||||
- Fix getStateRandao not returning historic RANDAO mix values
|
||||
3
changelog/potuz_hdiff_diff_type.md
Normal file
3
changelog/potuz_hdiff_diff_type.md
Normal file
@@ -0,0 +1,3 @@
|
||||
### Added
|
||||
|
||||
- Add native state diff type and marshalling functions
|
||||
3
changelog/pvl-blst-go-repo.md
Normal file
3
changelog/pvl-blst-go-repo.md
Normal file
@@ -0,0 +1,3 @@
|
||||
### Changed
|
||||
|
||||
- Changed blst dependency from `http_archive` to `go_repository` so that gazelle can keep it in sync with go.mod.
|
||||
5
changelog/pvl-go-1.25.md
Normal file
5
changelog/pvl-go-1.25.md
Normal file
@@ -0,0 +1,5 @@
|
||||
### Changed
|
||||
|
||||
- Updated go to v1.25.1
|
||||
- Updated rules_go to v0.57.0
|
||||
- Updated protobuf to 28.3
|
||||
6
changelog/pvl-update-eth-clients.md
Normal file
6
changelog/pvl-update-eth-clients.md
Normal file
@@ -0,0 +1,6 @@
|
||||
### Ignored
|
||||
|
||||
- Updated pinned commit for eth-clients/holesky
|
||||
- Updated pinned commit for eth-clients/hoodi
|
||||
- Updated pinned commit for eth-clients/sepolia
|
||||
- Removed deprecated dependency for eth-clients/eth2-networks
|
||||
@@ -0,0 +1,2 @@
|
||||
### Fixed
|
||||
- fix race in PriorityQueue.Pop by checking emptiness under write lock. (#15726)
|
||||
3
changelog/syjn99_ssz-ql-bitlist-bitvector.md
Normal file
3
changelog/syjn99_ssz-ql-bitlist-bitvector.md
Normal file
@@ -0,0 +1,3 @@
|
||||
### Added
|
||||
|
||||
- SSZ-QL: Handle `Bitlist` and `Bitvector` types.
|
||||
3
changelog/ttsao_add-gloas-beacon-state.md
Normal file
3
changelog/ttsao_add-gloas-beacon-state.md
Normal file
@@ -0,0 +1,3 @@
|
||||
### Added
|
||||
|
||||
- Implement Gloas state
|
||||
3
changelog/ttsao_add-gloas-protobufs.md
Normal file
3
changelog/ttsao_add-gloas-protobufs.md
Normal file
@@ -0,0 +1,3 @@
|
||||
### Added
|
||||
|
||||
- Add Gloas protobuf definitions with spec tests and SSZ serialization support
|
||||
3
changelog/ttsao_fix-blinded-block-v2-endpoint.md
Normal file
3
changelog/ttsao_fix-blinded-block-v2-endpoint.md
Normal file
@@ -0,0 +1,3 @@
|
||||
### Fixed
|
||||
|
||||
- Use v2 endpoint for blinded block submission post-Fulu
|
||||
3
changelog/ttsao_set-fulu-fork-epochs.md
Normal file
3
changelog/ttsao_set-fulu-fork-epochs.md
Normal file
@@ -0,0 +1,3 @@
|
||||
### Changed
|
||||
|
||||
- Set Fulu fork epochs for Holesky, Hoodi, and Sepolia testnets
|
||||
@@ -34,7 +34,11 @@ go_test(
|
||||
deps = [
|
||||
"//crypto/bls:go_default_library",
|
||||
"//runtime/interop:go_default_library",
|
||||
"//runtime/version:go_default_library",
|
||||
"//testing/assert:go_default_library",
|
||||
"//testing/require:go_default_library",
|
||||
"@com_github_ethereum_go_ethereum//core:go_default_library",
|
||||
"@com_github_ethereum_go_ethereum//core/types:go_default_library",
|
||||
"@com_github_ethereum_go_ethereum//params:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -279,6 +279,14 @@ func generateGenesis(ctx context.Context) (state.BeaconState, error) {
|
||||
if v > version.Altair {
|
||||
// set ttd to zero so EL goes post-merge immediately
|
||||
gen.Config.TerminalTotalDifficulty = big.NewInt(0)
|
||||
if gen.BaseFee == nil {
|
||||
return nil, errors.New("baseFeePerGas must be set in genesis.json for Post-Merge networks (after Altair)")
|
||||
}
|
||||
} else {
|
||||
if gen.BaseFee == nil {
|
||||
gen.BaseFee = big.NewInt(1000000000) // 1 Gwei default
|
||||
log.WithField("baseFeePerGas", "1000000000").Warn("BaseFeePerGas not specified in genesis.json, using default value of 1 Gwei")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
gen = interop.GethTestnetGenesis(time.Unix(int64(f.GenesisTime), 0), params.BeaconConfig())
|
||||
|
||||
@@ -1,14 +1,21 @@
|
||||
package testnet
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/OffchainLabs/prysm/v6/crypto/bls"
|
||||
"github.com/OffchainLabs/prysm/v6/runtime/interop"
|
||||
"github.com/OffchainLabs/prysm/v6/runtime/version"
|
||||
"github.com/OffchainLabs/prysm/v6/testing/assert"
|
||||
"github.com/OffchainLabs/prysm/v6/testing/require"
|
||||
"github.com/ethereum/go-ethereum/core"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/params"
|
||||
)
|
||||
|
||||
func Test_genesisStateFromJSONValidators(t *testing.T) {
|
||||
@@ -48,3 +55,76 @@ func createGenesisDepositData(t *testing.T, numKeys int) []*depositDataJSON {
|
||||
}
|
||||
return jsonData
|
||||
}
|
||||
|
||||
func Test_generateGenesis_BaseFeeValidation(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
forkVersion int
|
||||
baseFee *big.Int
|
||||
expectError bool
|
||||
errorMsg string
|
||||
}{
|
||||
{
|
||||
name: "Pre-merge Altair network without baseFee - should use default",
|
||||
forkVersion: version.Altair,
|
||||
baseFee: nil,
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "Post-merge Bellatrix network without baseFee - should error",
|
||||
forkVersion: version.Bellatrix,
|
||||
baseFee: nil,
|
||||
expectError: true,
|
||||
errorMsg: "baseFeePerGas must be set in genesis.json for Post-Merge networks (after Altair)",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Save original flags
|
||||
originalFlags := generateGenesisStateFlags
|
||||
defer func() {
|
||||
generateGenesisStateFlags = originalFlags
|
||||
}()
|
||||
|
||||
// Set up test flags
|
||||
generateGenesisStateFlags.NumValidators = 2
|
||||
generateGenesisStateFlags.GenesisTime = 1609459200
|
||||
generateGenesisStateFlags.ForkName = version.String(tt.forkVersion)
|
||||
|
||||
// Create a minimal genesis JSON for testing
|
||||
genesis := &core.Genesis{
|
||||
BaseFee: tt.baseFee,
|
||||
Difficulty: big.NewInt(0),
|
||||
GasLimit: 15000000,
|
||||
Alloc: types.GenesisAlloc{},
|
||||
Config: ¶ms.ChainConfig{
|
||||
ChainID: big.NewInt(32382),
|
||||
},
|
||||
}
|
||||
|
||||
// Create temporary genesis JSON file
|
||||
genesisJSON, err := json.Marshal(genesis)
|
||||
require.NoError(t, err)
|
||||
|
||||
tmpFile := t.TempDir() + "/genesis.json"
|
||||
err = writeFile(tmpFile, genesisJSON)
|
||||
require.NoError(t, err)
|
||||
|
||||
generateGenesisStateFlags.GethGenesisJsonIn = tmpFile
|
||||
|
||||
ctx := context.Background()
|
||||
_, err = generateGenesis(ctx)
|
||||
|
||||
if tt.expectError {
|
||||
require.ErrorContains(t, tt.errorMsg, err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func writeFile(path string, data []byte) error {
|
||||
return os.WriteFile(path, data, 0644)
|
||||
}
|
||||
|
||||
@@ -67,7 +67,6 @@ go_test(
|
||||
"testdata/e2e_config.yaml",
|
||||
"@consensus_spec//:spec_data",
|
||||
"@consensus_spec_tests//:test_data",
|
||||
"@eth2_networks//:configs",
|
||||
"@holesky_testnet//:configs",
|
||||
"@hoodi_testnet//:configs",
|
||||
"@mainnet//:configs",
|
||||
|
||||
@@ -158,6 +158,7 @@ type BeaconChainConfig struct {
|
||||
BeaconStateDenebFieldCount int // BeaconStateDenebFieldCount defines how many fields are in beacon state post upgrade to Deneb.
|
||||
BeaconStateElectraFieldCount int // BeaconStateElectraFieldCount defines how many fields are in beacon state post upgrade to Electra.
|
||||
BeaconStateFuluFieldCount int // BeaconStateFuluFieldCount defines how many fields are in beacon state post upgrade to Fulu.
|
||||
BeaconStateGloasFieldCount int // BeaconStateGloasFieldCount defines how many fields are in beacon state post upgrade to Gloas.
|
||||
|
||||
// Slasher constants.
|
||||
WeakSubjectivityPeriod primitives.Epoch // WeakSubjectivityPeriod defines the time period expressed in number of epochs were proof of stake network should validate block headers and attestations for slashable events.
|
||||
@@ -180,6 +181,7 @@ type BeaconChainConfig struct {
|
||||
ElectraForkEpoch primitives.Epoch `yaml:"ELECTRA_FORK_EPOCH" spec:"true"` // ElectraForkEpoch is used to represent the assigned fork epoch for electra.
|
||||
FuluForkVersion []byte `yaml:"FULU_FORK_VERSION" spec:"true"` // FuluForkVersion is used to represent the fork version for fulu.
|
||||
FuluForkEpoch primitives.Epoch `yaml:"FULU_FORK_EPOCH" spec:"true"` // FuluForkEpoch is used to represent the assigned fork epoch for fulu.
|
||||
GloasForkVersion []byte `yaml:"GLOAS_FORK_VERSION" spec:"true"`
|
||||
|
||||
ForkVersionSchedule map[[fieldparams.VersionLength]byte]primitives.Epoch // Schedule of fork epochs by version.
|
||||
ForkVersionNames map[[fieldparams.VersionLength]byte]string // Human-readable names of fork versions.
|
||||
|
||||
@@ -30,6 +30,7 @@ var placeholderFields = []string{
|
||||
"ATTESTATION_DUE_BPS",
|
||||
"ATTESTATION_DUE_BPS_GLOAS",
|
||||
"BLOB_SIDECAR_SUBNET_COUNT_FULU",
|
||||
"CELLS_PER_EXT_BLOB",
|
||||
"CONTRIBUTION_DUE_BPS",
|
||||
"CONTRIBUTION_DUE_BPS_GLOAS",
|
||||
"EIP6110_FORK_EPOCH",
|
||||
@@ -45,10 +46,13 @@ var placeholderFields = []string{
|
||||
"EIP7928_FORK_EPOCH",
|
||||
"EIP7928_FORK_VERSION",
|
||||
"EPOCHS_PER_SHUFFLING_PHASE",
|
||||
"FIELD_ELEMENTS_PER_CELL", // Configured as a constant in config/fieldparams/mainnet.go
|
||||
"FIELD_ELEMENTS_PER_EXT_BLOB", // Configured in proto/ssz_proto_library.bzl
|
||||
"GLOAS_FORK_EPOCH",
|
||||
"GLOAS_FORK_VERSION",
|
||||
"INCLUSION_LIST_SUBMISSION_DEADLINE",
|
||||
"INCLUSION_LIST_SUBMISSION_DUE_BPS",
|
||||
"KZG_COMMITMENTS_INCLUSION_PROOF_DEPTH", // Configured in proto/ssz_proto_library.bzl
|
||||
"MAX_BYTES_PER_INCLUSION_LIST",
|
||||
"MAX_REQUEST_BLOB_SIDECARS_FULU",
|
||||
"MAX_REQUEST_INCLUSION_LIST",
|
||||
@@ -388,6 +392,7 @@ func presetsFilePath(t *testing.T, config string) []string {
|
||||
path.Join(fPath, "presets", config, "capella.yaml"),
|
||||
path.Join(fPath, "presets", config, "deneb.yaml"),
|
||||
path.Join(fPath, "presets", config, "electra.yaml"),
|
||||
path.Join(fPath, "presets", config, "fulu.yaml"),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -197,6 +197,7 @@ var mainnetBeaconConfig = &BeaconChainConfig{
|
||||
BeaconStateDenebFieldCount: 28,
|
||||
BeaconStateElectraFieldCount: 37,
|
||||
BeaconStateFuluFieldCount: 38,
|
||||
BeaconStateGloasFieldCount: 43,
|
||||
|
||||
// Slasher related values.
|
||||
WeakSubjectivityPeriod: 54000,
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package params
|
||||
|
||||
import "math"
|
||||
|
||||
// UseHoleskyNetworkConfig uses the Holesky beacon chain specific network config.
|
||||
func UseHoleskyNetworkConfig() {
|
||||
cfg := BeaconNetworkConfig().Copy()
|
||||
@@ -41,12 +39,21 @@ func HoleskyConfig() *BeaconChainConfig {
|
||||
cfg.DenebForkVersion = []byte{0x05, 0x1, 0x70, 0x0}
|
||||
cfg.ElectraForkEpoch = 115968 // Mon, Feb 24 at 21:55:12 UTC
|
||||
cfg.ElectraForkVersion = []byte{0x06, 0x1, 0x70, 0x0}
|
||||
cfg.FuluForkEpoch = math.MaxUint64
|
||||
cfg.FuluForkVersion = []byte{0x07, 0x1, 0x70, 0x0} // TODO: Define holesky fork version for fulu. This is a placeholder value.
|
||||
cfg.FuluForkEpoch = 165120 // 2025-10-01 08:48:00 UTC
|
||||
cfg.FuluForkVersion = []byte{0x07, 0x1, 0x70, 0x0}
|
||||
cfg.TerminalTotalDifficulty = "0"
|
||||
cfg.DepositContractAddress = "0x4242424242424242424242424242424242424242"
|
||||
cfg.EjectionBalance = 28000000000
|
||||
cfg.BlobSchedule = []BlobScheduleEntry{}
|
||||
cfg.BlobSchedule = []BlobScheduleEntry{
|
||||
{
|
||||
MaxBlobsPerBlock: 15,
|
||||
Epoch: 166400, // 2025-10-07 01:20:00 UTC
|
||||
},
|
||||
{
|
||||
MaxBlobsPerBlock: 21,
|
||||
Epoch: 167936, // 2025-10-13 21:10:24 UTC
|
||||
},
|
||||
}
|
||||
cfg.InitializeForkSchedule()
|
||||
return cfg
|
||||
}
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
package params
|
||||
|
||||
import (
|
||||
"math"
|
||||
)
|
||||
|
||||
// UseHoodiNetworkConfig uses the Hoodi beacon chain specific network config.
|
||||
func UseHoodiNetworkConfig() {
|
||||
cfg := BeaconNetworkConfig().Copy()
|
||||
@@ -49,11 +45,20 @@ func HoodiConfig() *BeaconChainConfig {
|
||||
cfg.DenebForkVersion = []byte{0x50, 0x00, 0x09, 0x10}
|
||||
cfg.ElectraForkEpoch = 2048
|
||||
cfg.ElectraForkVersion = []byte{0x60, 0x00, 0x09, 0x10}
|
||||
cfg.FuluForkEpoch = math.MaxUint64
|
||||
cfg.FuluForkEpoch = 50688 // 2025-10-28 18:53:12 UTC
|
||||
cfg.FuluForkVersion = []byte{0x70, 0x00, 0x09, 0x10}
|
||||
cfg.TerminalTotalDifficulty = "0"
|
||||
cfg.DepositContractAddress = "0x00000000219ab540356cBB839Cbe05303d7705Fa"
|
||||
cfg.BlobSchedule = []BlobScheduleEntry{}
|
||||
cfg.BlobSchedule = []BlobScheduleEntry{
|
||||
{
|
||||
MaxBlobsPerBlock: 15,
|
||||
Epoch: 52480, // 2025-11-05 18:02:00 UTC
|
||||
},
|
||||
{
|
||||
MaxBlobsPerBlock: 21,
|
||||
Epoch: 54016, // 2025-11-12 13:52:24 UTC
|
||||
},
|
||||
}
|
||||
cfg.DefaultBuilderGasLimit = uint64(60000000)
|
||||
cfg.InitializeForkSchedule()
|
||||
return cfg
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package params
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
eth1Params "github.com/ethereum/go-ethereum/params"
|
||||
)
|
||||
|
||||
@@ -46,12 +44,21 @@ func SepoliaConfig() *BeaconChainConfig {
|
||||
cfg.DenebForkVersion = []byte{0x90, 0x00, 0x00, 0x73}
|
||||
cfg.ElectraForkEpoch = 222464 // Wed, Mar 5 at 07:29:36 UTC
|
||||
cfg.ElectraForkVersion = []byte{0x90, 0x00, 0x00, 0x74}
|
||||
cfg.FuluForkEpoch = math.MaxUint64
|
||||
cfg.FuluForkVersion = []byte{0x90, 0x00, 0x00, 0x75} // TODO: Define sepolia fork version for fulu. This is a placeholder value.
|
||||
cfg.FuluForkEpoch = 272640 // 2025-10-14 07:36:00 UTC
|
||||
cfg.FuluForkVersion = []byte{0x90, 0x00, 0x00, 0x75}
|
||||
cfg.TerminalTotalDifficulty = "17000000000000000"
|
||||
cfg.DepositContractAddress = "0x7f02C3E3c98b133055B8B348B2Ac625669Ed295D"
|
||||
cfg.DefaultBuilderGasLimit = uint64(60000000)
|
||||
cfg.BlobSchedule = []BlobScheduleEntry{}
|
||||
cfg.BlobSchedule = []BlobScheduleEntry{
|
||||
{
|
||||
MaxBlobsPerBlock: 15,
|
||||
Epoch: 274176, // 2025-10-21 03:26:24 UTC
|
||||
},
|
||||
{
|
||||
MaxBlobsPerBlock: 21,
|
||||
Epoch: 275712, // 2025-10-27 23:16:48 UTC
|
||||
},
|
||||
}
|
||||
cfg.InitializeForkSchedule()
|
||||
return cfg
|
||||
}
|
||||
|
||||
@@ -42,6 +42,12 @@ func NewWrappedExecutionData(v proto.Message) (interfaces.ExecutionData, error)
|
||||
return WrappedExecutionPayloadDeneb(pbStruct.Payload)
|
||||
case *enginev1.ExecutionBundleFulu:
|
||||
return WrappedExecutionPayloadDeneb(pbStruct.Payload)
|
||||
case *enginev1.ExecutionPayloadHeader:
|
||||
return WrappedExecutionPayloadHeader(pbStruct)
|
||||
case *enginev1.ExecutionPayloadHeaderCapella:
|
||||
return WrappedExecutionPayloadHeaderCapella(pbStruct)
|
||||
case *enginev1.ExecutionPayloadHeaderDeneb:
|
||||
return WrappedExecutionPayloadHeaderDeneb(pbStruct)
|
||||
default:
|
||||
return nil, errors.Wrapf(ErrUnsupportedVersion, "type %T", pbStruct)
|
||||
}
|
||||
|
||||
60
consensus-types/hdiff/BUILD.bazel
Normal file
60
consensus-types/hdiff/BUILD.bazel
Normal file
@@ -0,0 +1,60 @@
|
||||
load("@prysm//tools/go:def.bzl", "go_library", "go_test")
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"state_diff.go",
|
||||
"state_diff_gloas.go",
|
||||
],
|
||||
importpath = "github.com/OffchainLabs/prysm/v6/consensus-types/hdiff",
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//beacon-chain/core/altair:go_default_library",
|
||||
"//beacon-chain/core/capella:go_default_library",
|
||||
"//beacon-chain/core/deneb:go_default_library",
|
||||
"//beacon-chain/core/electra:go_default_library",
|
||||
"//beacon-chain/core/execution:go_default_library",
|
||||
"//beacon-chain/core/fulu:go_default_library",
|
||||
"//beacon-chain/core/gloas:go_default_library",
|
||||
"//beacon-chain/state:go_default_library",
|
||||
"//config/fieldparams:go_default_library",
|
||||
"//consensus-types/blocks:go_default_library",
|
||||
"//consensus-types/helpers:go_default_library",
|
||||
"//consensus-types/interfaces:go_default_library",
|
||||
"//consensus-types/primitives:go_default_library",
|
||||
"//proto/engine/v1:go_default_library",
|
||||
"//proto/prysm/v1alpha1:go_default_library",
|
||||
"//runtime/version:go_default_library",
|
||||
"@com_github_golang_snappy//:go_default_library",
|
||||
"@com_github_pkg_errors//:go_default_library",
|
||||
"@com_github_prysmaticlabs_fastssz//:go_default_library",
|
||||
"@com_github_prysmaticlabs_go_bitfield//:go_default_library",
|
||||
"@com_github_sirupsen_logrus//:go_default_library",
|
||||
"@org_golang_google_protobuf//proto:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = [
|
||||
"fuzz_test.go",
|
||||
"property_test.go",
|
||||
"security_test.go",
|
||||
"state_diff_test.go",
|
||||
],
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//beacon-chain/core/transition:go_default_library",
|
||||
"//beacon-chain/state:go_default_library",
|
||||
"//beacon-chain/state/state-native:go_default_library",
|
||||
"//config/fieldparams:go_default_library",
|
||||
"//consensus-types/blocks:go_default_library",
|
||||
"//consensus-types/primitives:go_default_library",
|
||||
"//proto/prysm/v1alpha1:go_default_library",
|
||||
"//runtime/version:go_default_library",
|
||||
"//testing/require:go_default_library",
|
||||
"//testing/util:go_default_library",
|
||||
"@com_github_golang_snappy//:go_default_library",
|
||||
"@com_github_pkg_errors//:go_default_library",
|
||||
],
|
||||
)
|
||||
491
consensus-types/hdiff/fuzz_test.go
Normal file
491
consensus-types/hdiff/fuzz_test.go
Normal file
@@ -0,0 +1,491 @@
|
||||
package hdiff
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
ethpb "github.com/OffchainLabs/prysm/v6/proto/prysm/v1alpha1"
|
||||
"github.com/OffchainLabs/prysm/v6/testing/util"
|
||||
)
|
||||
|
||||
// FuzzNewHdiff tests parsing variations of realistic diffs
|
||||
func FuzzNewHdiff(f *testing.F) {
|
||||
// Add seed corpus with various valid diffs from realistic scenarios
|
||||
sizes := []uint64{8, 16, 32}
|
||||
for _, size := range sizes {
|
||||
source, _ := util.DeterministicGenesisStateElectra(f, size)
|
||||
|
||||
// Create various realistic target states
|
||||
scenarios := []string{"slot_change", "balance_change", "validator_change", "multiple_changes"}
|
||||
for _, scenario := range scenarios {
|
||||
target := source.Copy()
|
||||
|
||||
switch scenario {
|
||||
case "slot_change":
|
||||
_ = target.SetSlot(source.Slot() + 1)
|
||||
case "balance_change":
|
||||
balances := target.Balances()
|
||||
if len(balances) > 0 {
|
||||
balances[0] += 1000000000
|
||||
_ = target.SetBalances(balances)
|
||||
}
|
||||
case "validator_change":
|
||||
validators := target.Validators()
|
||||
if len(validators) > 0 {
|
||||
validators[0].EffectiveBalance += 1000000000
|
||||
_ = target.SetValidators(validators)
|
||||
}
|
||||
case "multiple_changes":
|
||||
_ = target.SetSlot(source.Slot() + 5)
|
||||
balances := target.Balances()
|
||||
validators := target.Validators()
|
||||
if len(balances) > 0 && len(validators) > 0 {
|
||||
balances[0] += 2000000000
|
||||
validators[0].EffectiveBalance += 1000000000
|
||||
_ = target.SetBalances(balances)
|
||||
_ = target.SetValidators(validators)
|
||||
}
|
||||
}
|
||||
|
||||
validDiff, err := Diff(source, target)
|
||||
if err == nil {
|
||||
f.Add(validDiff.StateDiff, validDiff.ValidatorDiffs, validDiff.BalancesDiff)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
f.Fuzz(func(t *testing.T, stateDiff, validatorDiffs, balancesDiff []byte) {
|
||||
// Limit input sizes to reasonable bounds
|
||||
if len(stateDiff) > 5000 || len(validatorDiffs) > 5000 || len(balancesDiff) > 5000 {
|
||||
return
|
||||
}
|
||||
|
||||
input := HdiffBytes{
|
||||
StateDiff: stateDiff,
|
||||
ValidatorDiffs: validatorDiffs,
|
||||
BalancesDiff: balancesDiff,
|
||||
}
|
||||
|
||||
// Test parsing - should not panic even with corrupted but bounded data
|
||||
_, err := newHdiff(input)
|
||||
_ = err // Expected to fail with corrupted data
|
||||
})
|
||||
}
|
||||
|
||||
// FuzzNewStateDiff tests the newStateDiff function with random compressed input
|
||||
func FuzzNewStateDiff(f *testing.F) {
|
||||
// Add seed corpus
|
||||
source, _ := util.DeterministicGenesisStateElectra(f, 16)
|
||||
target := source.Copy()
|
||||
_ = target.SetSlot(source.Slot() + 5)
|
||||
|
||||
diff, err := diffToState(source, target)
|
||||
if err == nil {
|
||||
serialized := diff.serialize()
|
||||
f.Add(serialized)
|
||||
}
|
||||
|
||||
// Add edge cases
|
||||
f.Add([]byte{})
|
||||
f.Add([]byte{0x01})
|
||||
f.Add([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07})
|
||||
|
||||
f.Fuzz(func(t *testing.T, data []byte) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Errorf("newStateDiff panicked: %v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
// Should never panic, only return error
|
||||
_, err := newStateDiff(data)
|
||||
_ = err
|
||||
})
|
||||
}
|
||||
|
||||
// FuzzNewValidatorDiffs tests validator diff deserialization
|
||||
func FuzzNewValidatorDiffs(f *testing.F) {
|
||||
// Add seed corpus
|
||||
source, _ := util.DeterministicGenesisStateElectra(f, 8)
|
||||
target := source.Copy()
|
||||
vals := target.Validators()
|
||||
if len(vals) > 0 {
|
||||
modifiedVal := ðpb.Validator{
|
||||
PublicKey: vals[0].PublicKey,
|
||||
WithdrawalCredentials: vals[0].WithdrawalCredentials,
|
||||
EffectiveBalance: vals[0].EffectiveBalance + 1000,
|
||||
Slashed: !vals[0].Slashed,
|
||||
ActivationEligibilityEpoch: vals[0].ActivationEligibilityEpoch,
|
||||
ActivationEpoch: vals[0].ActivationEpoch,
|
||||
ExitEpoch: vals[0].ExitEpoch,
|
||||
WithdrawableEpoch: vals[0].WithdrawableEpoch,
|
||||
}
|
||||
vals[0] = modifiedVal
|
||||
_ = target.SetValidators(vals)
|
||||
|
||||
// Create a simple diff for fuzzing - we'll just use raw bytes
|
||||
_, err := diffToVals(source, target)
|
||||
if err == nil {
|
||||
// Add some realistic validator diff bytes for the corpus
|
||||
f.Add([]byte{1, 0, 0, 0, 0, 0, 0, 0}) // Simple validator diff
|
||||
}
|
||||
}
|
||||
|
||||
// Add edge cases
|
||||
f.Add([]byte{})
|
||||
f.Add([]byte{0x01, 0x02, 0x03, 0x04})
|
||||
|
||||
f.Fuzz(func(t *testing.T, data []byte) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Errorf("newValidatorDiffs panicked: %v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
_, err := newValidatorDiffs(data)
|
||||
_ = err
|
||||
})
|
||||
}
|
||||
|
||||
// FuzzNewBalancesDiff tests balance diff deserialization
|
||||
func FuzzNewBalancesDiff(f *testing.F) {
|
||||
// Add seed corpus
|
||||
source, _ := util.DeterministicGenesisStateElectra(f, 8)
|
||||
target := source.Copy()
|
||||
balances := target.Balances()
|
||||
if len(balances) > 0 {
|
||||
balances[0] += 1000
|
||||
_ = target.SetBalances(balances)
|
||||
|
||||
// Create a simple diff for fuzzing - we'll just use raw bytes
|
||||
_, err := diffToBalances(source, target)
|
||||
if err == nil {
|
||||
// Add some realistic balance diff bytes for the corpus
|
||||
f.Add([]byte{1, 0, 0, 0, 0, 0, 0, 0}) // Simple balance diff
|
||||
}
|
||||
}
|
||||
|
||||
// Add edge cases
|
||||
f.Add([]byte{})
|
||||
f.Add([]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08})
|
||||
|
||||
f.Fuzz(func(t *testing.T, data []byte) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Errorf("newBalancesDiff panicked: %v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
_, err := newBalancesDiff(data)
|
||||
_ = err
|
||||
})
|
||||
}
|
||||
|
||||
// FuzzApplyDiff tests applying variations of valid diffs
|
||||
func FuzzApplyDiff(f *testing.F) {
|
||||
// Test with realistic state variations, not random data
|
||||
ctx := context.Background()
|
||||
|
||||
// Add seed corpus with various valid scenarios
|
||||
sizes := []uint64{8, 16, 32, 64}
|
||||
for _, size := range sizes {
|
||||
source, _ := util.DeterministicGenesisStateElectra(f, size)
|
||||
target := source.Copy()
|
||||
|
||||
// Different types of realistic changes
|
||||
scenarios := []func(){
|
||||
func() { _ = target.SetSlot(source.Slot() + 1) }, // Slot change
|
||||
func() { // Balance change
|
||||
balances := target.Balances()
|
||||
if len(balances) > 0 {
|
||||
balances[0] += 1000000000 // 1 ETH
|
||||
_ = target.SetBalances(balances)
|
||||
}
|
||||
},
|
||||
func() { // Validator change
|
||||
validators := target.Validators()
|
||||
if len(validators) > 0 {
|
||||
validators[0].EffectiveBalance += 1000000000
|
||||
_ = target.SetValidators(validators)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
for _, scenario := range scenarios {
|
||||
testTarget := source.Copy()
|
||||
scenario()
|
||||
|
||||
validDiff, err := Diff(source, testTarget)
|
||||
if err == nil {
|
||||
f.Add(validDiff.StateDiff, validDiff.ValidatorDiffs, validDiff.BalancesDiff)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
f.Fuzz(func(t *testing.T, stateDiff, validatorDiffs, balancesDiff []byte) {
|
||||
// Only test with reasonable sized inputs
|
||||
if len(stateDiff) > 10000 || len(validatorDiffs) > 10000 || len(balancesDiff) > 10000 {
|
||||
return
|
||||
}
|
||||
|
||||
// Create fresh source state for each test
|
||||
source, _ := util.DeterministicGenesisStateElectra(t, 8)
|
||||
|
||||
diff := HdiffBytes{
|
||||
StateDiff: stateDiff,
|
||||
ValidatorDiffs: validatorDiffs,
|
||||
BalancesDiff: balancesDiff,
|
||||
}
|
||||
|
||||
// Apply diff - errors are expected for fuzzed data
|
||||
_, err := ApplyDiff(ctx, source, diff)
|
||||
_ = err // Expected to fail with invalid data
|
||||
})
|
||||
}
|
||||
|
||||
// FuzzReadPendingAttestation tests the pending attestation deserialization
|
||||
func FuzzReadPendingAttestation(f *testing.F) {
|
||||
// Add edge cases - this function is particularly vulnerable
|
||||
f.Add([]byte{})
|
||||
f.Add([]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}) // 8 bytes
|
||||
f.Add(make([]byte, 200)) // Larger than expected
|
||||
|
||||
// Add a case with large reported length
|
||||
largeLength := make([]byte, 8)
|
||||
binary.LittleEndian.PutUint64(largeLength, 0xFFFFFFFF) // Large bits length
|
||||
f.Add(largeLength)
|
||||
|
||||
f.Fuzz(func(t *testing.T, data []byte) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Errorf("readPendingAttestation panicked: %v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
// Make a copy since the function modifies the slice
|
||||
dataCopy := make([]byte, len(data))
|
||||
copy(dataCopy, data)
|
||||
|
||||
_, err := readPendingAttestation(&dataCopy)
|
||||
_ = err
|
||||
})
|
||||
}
|
||||
|
||||
// FuzzKmpIndex tests the KMP algorithm implementation
|
||||
func FuzzKmpIndex(f *testing.F) {
|
||||
// Test with integer pointers to match the actual usage
|
||||
f.Add(0, "1,2,3", "1,2,3,4,5")
|
||||
f.Add(3, "1,2,3", "1,2,3,1,2,3")
|
||||
f.Add(0, "", "1,2,3")
|
||||
|
||||
f.Fuzz(func(t *testing.T, lens int, patternStr string, textStr string) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Errorf("kmpIndex panicked: %v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
// Parse comma-separated strings into int slices
|
||||
var pattern, text []int
|
||||
if patternStr != "" {
|
||||
for _, s := range strings.Split(patternStr, ",") {
|
||||
if val, err := strconv.Atoi(strings.TrimSpace(s)); err == nil {
|
||||
pattern = append(pattern, val)
|
||||
}
|
||||
}
|
||||
}
|
||||
if textStr != "" {
|
||||
for _, s := range strings.Split(textStr, ",") {
|
||||
if val, err := strconv.Atoi(strings.TrimSpace(s)); err == nil {
|
||||
text = append(text, val)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Convert to pointer slices as used in actual code
|
||||
patternPtrs := make([]*int, len(pattern))
|
||||
for i := range pattern {
|
||||
val := pattern[i]
|
||||
patternPtrs[i] = &val
|
||||
}
|
||||
|
||||
textPtrs := make([]*int, len(text))
|
||||
for i := range text {
|
||||
val := text[i]
|
||||
textPtrs[i] = &val
|
||||
}
|
||||
|
||||
integerEquals := func(a, b *int) bool {
|
||||
if a == nil && b == nil {
|
||||
return true
|
||||
}
|
||||
if a == nil || b == nil {
|
||||
return false
|
||||
}
|
||||
return *a == *b
|
||||
}
|
||||
|
||||
// Clamp lens to reasonable range to avoid infinite loops
|
||||
if lens < 0 {
|
||||
lens = 0
|
||||
}
|
||||
if lens > len(textPtrs) {
|
||||
lens = len(textPtrs)
|
||||
}
|
||||
|
||||
result := kmpIndex(lens, textPtrs, integerEquals)
|
||||
|
||||
// Basic sanity check
|
||||
if result < 0 || result > lens {
|
||||
t.Errorf("kmpIndex returned invalid result: %d for lens=%d", result, lens)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// FuzzComputeLPS tests the LPS computation for KMP
|
||||
func FuzzComputeLPS(f *testing.F) {
|
||||
// Add seed cases
|
||||
f.Add("1,2,1")
|
||||
f.Add("1,1,1")
|
||||
f.Add("1,2,3,4")
|
||||
f.Add("")
|
||||
|
||||
f.Fuzz(func(t *testing.T, patternStr string) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Errorf("computeLPS panicked: %v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
// Parse comma-separated string into int slice
|
||||
var pattern []int
|
||||
if patternStr != "" {
|
||||
for _, s := range strings.Split(patternStr, ",") {
|
||||
if val, err := strconv.Atoi(strings.TrimSpace(s)); err == nil {
|
||||
pattern = append(pattern, val)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Convert to pointer slice
|
||||
patternPtrs := make([]*int, len(pattern))
|
||||
for i := range pattern {
|
||||
val := pattern[i]
|
||||
patternPtrs[i] = &val
|
||||
}
|
||||
|
||||
integerEquals := func(a, b *int) bool {
|
||||
if a == nil && b == nil {
|
||||
return true
|
||||
}
|
||||
if a == nil || b == nil {
|
||||
return false
|
||||
}
|
||||
return *a == *b
|
||||
}
|
||||
|
||||
result := computeLPS(patternPtrs, integerEquals)
|
||||
|
||||
// Verify result length matches input
|
||||
if len(result) != len(pattern) {
|
||||
t.Errorf("computeLPS returned wrong length: got %d, expected %d", len(result), len(pattern))
|
||||
}
|
||||
|
||||
// Verify all LPS values are non-negative and within bounds
|
||||
for i, lps := range result {
|
||||
if lps < 0 || lps > i {
|
||||
t.Errorf("Invalid LPS value at index %d: %d", i, lps)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// FuzzDiffToBalances tests balance diff computation
|
||||
func FuzzDiffToBalances(f *testing.F) {
|
||||
f.Fuzz(func(t *testing.T, sourceData, targetData []byte) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Errorf("diffToBalances panicked: %v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
// Convert byte data to balance arrays
|
||||
var sourceBalances, targetBalances []uint64
|
||||
|
||||
// Parse source balances (8 bytes per uint64)
|
||||
for i := 0; i+7 < len(sourceData) && len(sourceBalances) < 100; i += 8 {
|
||||
balance := binary.LittleEndian.Uint64(sourceData[i : i+8])
|
||||
sourceBalances = append(sourceBalances, balance)
|
||||
}
|
||||
|
||||
// Parse target balances
|
||||
for i := 0; i+7 < len(targetData) && len(targetBalances) < 100; i += 8 {
|
||||
balance := binary.LittleEndian.Uint64(targetData[i : i+8])
|
||||
targetBalances = append(targetBalances, balance)
|
||||
}
|
||||
|
||||
// Create states with the provided balances
|
||||
source, _ := util.DeterministicGenesisStateElectra(t, 1)
|
||||
target, _ := util.DeterministicGenesisStateElectra(t, 1)
|
||||
|
||||
if len(sourceBalances) > 0 {
|
||||
_ = source.SetBalances(sourceBalances)
|
||||
}
|
||||
if len(targetBalances) > 0 {
|
||||
_ = target.SetBalances(targetBalances)
|
||||
}
|
||||
|
||||
result, err := diffToBalances(source, target)
|
||||
|
||||
// If no error, verify result consistency
|
||||
if err == nil && len(result) > 0 {
|
||||
// Result length should match target length
|
||||
if len(result) != len(target.Balances()) {
|
||||
t.Errorf("diffToBalances result length mismatch: got %d, expected %d",
|
||||
len(result), len(target.Balances()))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// FuzzValidatorsEqual tests validator comparison
|
||||
func FuzzValidatorsEqual(f *testing.F) {
|
||||
f.Fuzz(func(t *testing.T, data []byte) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Errorf("validatorsEqual panicked: %v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
// Create two validators and fuzz their fields
|
||||
if len(data) < 16 {
|
||||
return
|
||||
}
|
||||
|
||||
source, _ := util.DeterministicGenesisStateElectra(t, 2)
|
||||
validators := source.Validators()
|
||||
if len(validators) < 2 {
|
||||
return
|
||||
}
|
||||
|
||||
val1 := validators[0]
|
||||
val2 := validators[1]
|
||||
|
||||
// Modify validator fields based on fuzz data
|
||||
if len(data) > 0 && data[0]%2 == 0 {
|
||||
val2.EffectiveBalance = val1.EffectiveBalance + uint64(data[0])
|
||||
}
|
||||
if len(data) > 1 && data[1]%2 == 0 {
|
||||
val2.Slashed = !val1.Slashed
|
||||
}
|
||||
|
||||
// Create ReadOnlyValidator wrappers if needed
|
||||
// Since validatorsEqual expects ReadOnlyValidator interface,
|
||||
// we'll skip this test for now as it requires state wrapper implementation
|
||||
_ = val1
|
||||
_ = val2
|
||||
})
|
||||
}
|
||||
390
consensus-types/hdiff/property_test.go
Normal file
390
consensus-types/hdiff/property_test.go
Normal file
@@ -0,0 +1,390 @@
|
||||
package hdiff
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"math"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/OffchainLabs/prysm/v6/consensus-types/primitives"
|
||||
"github.com/OffchainLabs/prysm/v6/testing/require"
|
||||
"github.com/OffchainLabs/prysm/v6/testing/util"
|
||||
)
|
||||
|
||||
// PropertyTestRoundTrip verifies that diff->apply is idempotent with realistic data
|
||||
func FuzzPropertyRoundTrip(f *testing.F) {
|
||||
f.Fuzz(func(t *testing.T, slotDelta uint64, balanceData []byte, validatorData []byte) {
|
||||
// Limit to realistic ranges
|
||||
if slotDelta > 32 { // Max one epoch
|
||||
slotDelta = slotDelta % 32
|
||||
}
|
||||
|
||||
// Convert byte data to realistic deltas and changes
|
||||
var balanceDeltas []int64
|
||||
var validatorChanges []bool
|
||||
|
||||
// Parse balance deltas - limit to realistic amounts (8 bytes per int64)
|
||||
for i := 0; i+7 < len(balanceData) && len(balanceDeltas) < 20; i += 8 {
|
||||
delta := int64(binary.LittleEndian.Uint64(balanceData[i : i+8]))
|
||||
// Keep deltas realistic (max 10 ETH change)
|
||||
if delta > 10000000000 {
|
||||
delta = delta % 10000000000
|
||||
}
|
||||
if delta < -10000000000 {
|
||||
delta = -((-delta) % 10000000000)
|
||||
}
|
||||
balanceDeltas = append(balanceDeltas, delta)
|
||||
}
|
||||
|
||||
// Parse validator changes (1 byte per bool) - limit to small number
|
||||
for i := 0; i < len(validatorData) && len(validatorChanges) < 10; i++ {
|
||||
validatorChanges = append(validatorChanges, validatorData[i]%2 == 0)
|
||||
}
|
||||
|
||||
ctx := t.Context()
|
||||
|
||||
// Create source state with reasonable size
|
||||
validatorCount := uint64(len(validatorChanges) + 8) // Minimum 8 validators
|
||||
if validatorCount > 64 {
|
||||
validatorCount = 64 // Cap at 64 for performance
|
||||
}
|
||||
source, _ := util.DeterministicGenesisStateElectra(t, validatorCount)
|
||||
|
||||
// Create target state with modifications
|
||||
target := source.Copy()
|
||||
|
||||
// Apply slot change
|
||||
_ = target.SetSlot(source.Slot() + primitives.Slot(slotDelta))
|
||||
|
||||
// Apply realistic balance changes
|
||||
if len(balanceDeltas) > 0 {
|
||||
balances := target.Balances()
|
||||
for i, delta := range balanceDeltas {
|
||||
if i >= len(balances) {
|
||||
break
|
||||
}
|
||||
// Apply realistic balance changes with safe bounds
|
||||
if delta < 0 {
|
||||
if uint64(-delta) > balances[i] {
|
||||
balances[i] = 0 // Can't go below 0
|
||||
} else {
|
||||
balances[i] -= uint64(-delta)
|
||||
}
|
||||
} else {
|
||||
// Cap at reasonable maximum (1000 ETH)
|
||||
maxBalance := uint64(1000000000000) // 1000 ETH in Gwei
|
||||
if balances[i]+uint64(delta) > maxBalance {
|
||||
balances[i] = maxBalance
|
||||
} else {
|
||||
balances[i] += uint64(delta)
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = target.SetBalances(balances)
|
||||
}
|
||||
|
||||
// Apply realistic validator changes
|
||||
if len(validatorChanges) > 0 {
|
||||
validators := target.Validators()
|
||||
for i, shouldChange := range validatorChanges {
|
||||
if i >= len(validators) {
|
||||
break
|
||||
}
|
||||
if shouldChange {
|
||||
// Make realistic changes - small effective balance adjustments
|
||||
validators[i].EffectiveBalance += 1000000000 // 1 ETH
|
||||
}
|
||||
}
|
||||
_ = target.SetValidators(validators)
|
||||
}
|
||||
|
||||
// Create diff
|
||||
diff, err := Diff(source, target)
|
||||
if err != nil {
|
||||
// If diff creation fails, that's acceptable for malformed inputs
|
||||
return
|
||||
}
|
||||
|
||||
// Apply diff
|
||||
result, err := ApplyDiff(ctx, source, diff)
|
||||
if err != nil {
|
||||
// If diff application fails, that's acceptable
|
||||
return
|
||||
}
|
||||
|
||||
// Verify round-trip property: source + diff = target
|
||||
require.Equal(t, target.Slot(), result.Slot())
|
||||
|
||||
// Verify balance consistency
|
||||
targetBalances := target.Balances()
|
||||
resultBalances := result.Balances()
|
||||
require.Equal(t, len(targetBalances), len(resultBalances))
|
||||
for i := range targetBalances {
|
||||
require.Equal(t, targetBalances[i], resultBalances[i], "Balance mismatch at index %d", i)
|
||||
}
|
||||
|
||||
// Verify validator consistency
|
||||
targetVals := target.Validators()
|
||||
resultVals := result.Validators()
|
||||
require.Equal(t, len(targetVals), len(resultVals))
|
||||
for i := range targetVals {
|
||||
require.Equal(t, targetVals[i].Slashed, resultVals[i].Slashed, "Validator slashing mismatch at index %d", i)
|
||||
require.Equal(t, targetVals[i].EffectiveBalance, resultVals[i].EffectiveBalance, "Validator balance mismatch at index %d", i)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// PropertyTestReasonablePerformance verifies operations complete quickly with realistic data
|
||||
func FuzzPropertyResourceBounds(f *testing.F) {
|
||||
f.Fuzz(func(t *testing.T, validatorCount uint8, slotDelta uint8, changeCount uint8) {
|
||||
// Use realistic parameters
|
||||
validators := uint64(validatorCount%64 + 8) // 8-71 validators
|
||||
slots := uint64(slotDelta % 32) // 0-31 slots
|
||||
changes := int(changeCount % 10) // 0-9 changes
|
||||
|
||||
// Create realistic states
|
||||
source, _ := util.DeterministicGenesisStateElectra(t, validators)
|
||||
target := source.Copy()
|
||||
|
||||
// Apply realistic changes
|
||||
_ = target.SetSlot(source.Slot() + primitives.Slot(slots))
|
||||
|
||||
if changes > 0 {
|
||||
validatorList := target.Validators()
|
||||
for i := 0; i < changes && i < len(validatorList); i++ {
|
||||
validatorList[i].EffectiveBalance += 1000000000 // 1 ETH
|
||||
}
|
||||
_ = target.SetValidators(validatorList)
|
||||
}
|
||||
|
||||
// Operations should complete quickly
|
||||
start := time.Now()
|
||||
diff, err := Diff(source, target)
|
||||
duration := time.Since(start)
|
||||
|
||||
if err == nil {
|
||||
// Should be fast
|
||||
require.Equal(t, true, duration < time.Second, "Diff creation too slow: %v", duration)
|
||||
|
||||
// Apply should also be fast
|
||||
start = time.Now()
|
||||
_, err = ApplyDiff(t.Context(), source, diff)
|
||||
duration = time.Since(start)
|
||||
|
||||
if err == nil {
|
||||
require.Equal(t, true, duration < time.Second, "Diff application too slow: %v", duration)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// PropertyTestDiffSize verifies that diffs are smaller than full states for typical cases
|
||||
func FuzzPropertyDiffEfficiency(f *testing.F) {
|
||||
f.Fuzz(func(t *testing.T, slotDelta uint64, numChanges uint8) {
|
||||
if slotDelta > 100 {
|
||||
slotDelta = slotDelta % 100
|
||||
}
|
||||
if numChanges > 10 {
|
||||
numChanges = numChanges % 10
|
||||
}
|
||||
|
||||
// Create states with small differences
|
||||
source, _ := util.DeterministicGenesisStateElectra(t, 64)
|
||||
target := source.Copy()
|
||||
|
||||
_ = target.SetSlot(source.Slot() + primitives.Slot(slotDelta))
|
||||
|
||||
// Make a few small changes
|
||||
if numChanges > 0 {
|
||||
validators := target.Validators()
|
||||
for i := uint8(0); i < numChanges && int(i) < len(validators); i++ {
|
||||
validators[i].EffectiveBalance += 1000
|
||||
}
|
||||
_ = target.SetValidators(validators)
|
||||
}
|
||||
|
||||
// Create diff
|
||||
diff, err := Diff(source, target)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// For small changes, diff should be much smaller than full state
|
||||
sourceSSZ, err := source.MarshalSSZ()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
diffSize := len(diff.StateDiff) + len(diff.ValidatorDiffs) + len(diff.BalancesDiff)
|
||||
|
||||
// Diff should be smaller than full state for small changes
|
||||
if numChanges <= 5 && slotDelta <= 10 {
|
||||
require.Equal(t, true, diffSize < len(sourceSSZ)/2,
|
||||
"Diff size %d should be less than half of state size %d", diffSize, len(sourceSSZ))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// PropertyTestBalanceConservation verifies that balance operations don't create/destroy value unexpectedly
|
||||
func FuzzPropertyBalanceConservation(f *testing.F) {
|
||||
f.Fuzz(func(t *testing.T, balanceData []byte) {
|
||||
// Convert byte data to balance changes
|
||||
var balanceChanges []int64
|
||||
for i := 0; i+7 < len(balanceData) && len(balanceChanges) < 50; i += 8 {
|
||||
change := int64(binary.LittleEndian.Uint64(balanceData[i : i+8]))
|
||||
balanceChanges = append(balanceChanges, change)
|
||||
}
|
||||
|
||||
source, _ := util.DeterministicGenesisStateElectra(t, uint64(len(balanceChanges)+10))
|
||||
originalBalances := source.Balances()
|
||||
|
||||
// Calculate total before
|
||||
var totalBefore uint64
|
||||
for _, balance := range originalBalances {
|
||||
totalBefore += balance
|
||||
}
|
||||
|
||||
// Apply balance changes via diff system
|
||||
target := source.Copy()
|
||||
targetBalances := target.Balances()
|
||||
|
||||
var totalDelta int64
|
||||
for i, delta := range balanceChanges {
|
||||
if i >= len(targetBalances) {
|
||||
break
|
||||
}
|
||||
|
||||
// Prevent underflow
|
||||
if delta < 0 && uint64(-delta) > targetBalances[i] {
|
||||
totalDelta += int64(targetBalances[i]) // Lost amount
|
||||
targetBalances[i] = 0
|
||||
} else if delta < 0 {
|
||||
targetBalances[i] -= uint64(-delta)
|
||||
totalDelta += delta
|
||||
} else {
|
||||
// Prevent overflow
|
||||
if uint64(delta) > math.MaxUint64-targetBalances[i] {
|
||||
gained := math.MaxUint64 - targetBalances[i]
|
||||
totalDelta += int64(gained)
|
||||
targetBalances[i] = math.MaxUint64
|
||||
} else {
|
||||
targetBalances[i] += uint64(delta)
|
||||
totalDelta += delta
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = target.SetBalances(targetBalances)
|
||||
|
||||
// Apply through diff system
|
||||
diff, err := Diff(source, target)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
result, err := ApplyDiff(t.Context(), source, diff)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Calculate total after
|
||||
resultBalances := result.Balances()
|
||||
var totalAfter uint64
|
||||
for _, balance := range resultBalances {
|
||||
totalAfter += balance
|
||||
}
|
||||
|
||||
// Verify conservation (accounting for intended changes)
|
||||
expectedTotal := totalBefore
|
||||
if totalDelta >= 0 {
|
||||
expectedTotal += uint64(totalDelta)
|
||||
} else {
|
||||
if uint64(-totalDelta) <= expectedTotal {
|
||||
expectedTotal -= uint64(-totalDelta)
|
||||
} else {
|
||||
expectedTotal = 0
|
||||
}
|
||||
}
|
||||
|
||||
require.Equal(t, expectedTotal, totalAfter,
|
||||
"Balance conservation violated: before=%d, delta=%d, expected=%d, actual=%d",
|
||||
totalBefore, totalDelta, expectedTotal, totalAfter)
|
||||
})
|
||||
}
|
||||
|
||||
// PropertyTestMonotonicSlot verifies slot only increases
|
||||
func FuzzPropertyMonotonicSlot(f *testing.F) {
|
||||
f.Fuzz(func(t *testing.T, slotDelta uint64) {
|
||||
source, _ := util.DeterministicGenesisStateElectra(t, 16)
|
||||
target := source.Copy()
|
||||
|
||||
targetSlot := source.Slot() + primitives.Slot(slotDelta)
|
||||
_ = target.SetSlot(targetSlot)
|
||||
|
||||
diff, err := Diff(source, target)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
result, err := ApplyDiff(t.Context(), source, diff)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Slot should never decrease
|
||||
require.Equal(t, true, result.Slot() >= source.Slot(),
|
||||
"Slot decreased from %d to %d", source.Slot(), result.Slot())
|
||||
|
||||
// Slot should match target
|
||||
require.Equal(t, targetSlot, result.Slot())
|
||||
})
|
||||
}
|
||||
|
||||
// PropertyTestValidatorIndexIntegrity verifies validator indices remain consistent
|
||||
func FuzzPropertyValidatorIndices(f *testing.F) {
|
||||
f.Fuzz(func(t *testing.T, changeData []byte) {
|
||||
// Convert byte data to boolean changes
|
||||
var changes []bool
|
||||
for i := 0; i < len(changeData) && len(changes) < 20; i++ {
|
||||
changes = append(changes, changeData[i]%2 == 0)
|
||||
}
|
||||
|
||||
source, _ := util.DeterministicGenesisStateElectra(t, uint64(len(changes)+5))
|
||||
target := source.Copy()
|
||||
|
||||
// Apply changes
|
||||
validators := target.Validators()
|
||||
for i, shouldChange := range changes {
|
||||
if i >= len(validators) {
|
||||
break
|
||||
}
|
||||
if shouldChange {
|
||||
validators[i].EffectiveBalance += 1000
|
||||
}
|
||||
}
|
||||
_ = target.SetValidators(validators)
|
||||
|
||||
diff, err := Diff(source, target)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
result, err := ApplyDiff(t.Context(), source, diff)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Validator count should not decrease
|
||||
require.Equal(t, true, len(result.Validators()) >= len(source.Validators()),
|
||||
"Validator count decreased from %d to %d", len(source.Validators()), len(result.Validators()))
|
||||
|
||||
// Public keys should be preserved for existing validators
|
||||
sourceVals := source.Validators()
|
||||
resultVals := result.Validators()
|
||||
for i := range sourceVals {
|
||||
if i < len(resultVals) {
|
||||
require.Equal(t, sourceVals[i].PublicKey, resultVals[i].PublicKey,
|
||||
"Public key changed at validator index %d", i)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
392
consensus-types/hdiff/security_test.go
Normal file
392
consensus-types/hdiff/security_test.go
Normal file
@@ -0,0 +1,392 @@
|
||||
package hdiff
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/OffchainLabs/prysm/v6/testing/require"
|
||||
"github.com/OffchainLabs/prysm/v6/testing/util"
|
||||
)
|
||||
|
||||
// TestIntegerOverflowProtection tests protection against balance overflow attacks
|
||||
func TestIntegerOverflowProtection(t *testing.T) {
|
||||
source, _ := util.DeterministicGenesisStateElectra(t, 8)
|
||||
|
||||
// Test balance overflow in diffToBalances - use realistic values
|
||||
t.Run("balance_diff_overflow", func(t *testing.T) {
|
||||
target := source.Copy()
|
||||
balances := target.Balances()
|
||||
|
||||
// Set high but realistic balance values (32 ETH in Gwei = 32e9)
|
||||
balances[0] = 32000000000 // 32 ETH
|
||||
balances[1] = 64000000000 // 64 ETH
|
||||
_ = target.SetBalances(balances)
|
||||
|
||||
// This should work fine with realistic values
|
||||
diffs, err := diffToBalances(source, target)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify the diffs are reasonable
|
||||
require.Equal(t, true, len(diffs) > 0, "Should have balance diffs")
|
||||
})
|
||||
|
||||
// Test reasonable balance changes
|
||||
t.Run("realistic_balance_changes", func(t *testing.T) {
|
||||
// Create realistic balance changes (slashing, rewards)
|
||||
balancesDiff := []int64{1000000000, -500000000, 2000000000} // 1 ETH gain, 0.5 ETH loss, 2 ETH gain
|
||||
|
||||
// Apply to state with normal balances
|
||||
testSource := source.Copy()
|
||||
normalBalances := []uint64{32000000000, 32000000000, 32000000000} // 32 ETH each
|
||||
_ = testSource.SetBalances(normalBalances)
|
||||
|
||||
// This should work fine
|
||||
result, err := applyBalancesDiff(testSource, balancesDiff)
|
||||
require.NoError(t, err)
|
||||
|
||||
resultBalances := result.Balances()
|
||||
require.Equal(t, uint64(33000000000), resultBalances[0]) // 33 ETH
|
||||
require.Equal(t, uint64(31500000000), resultBalances[1]) // 31.5 ETH
|
||||
require.Equal(t, uint64(34000000000), resultBalances[2]) // 34 ETH
|
||||
})
|
||||
}
|
||||
|
||||
// TestReasonablePerformance tests that operations complete in reasonable time
|
||||
func TestReasonablePerformance(t *testing.T) {
|
||||
t.Run("large_state_performance", func(t *testing.T) {
|
||||
// Test with a large but realistic validator set
|
||||
source, _ := util.DeterministicGenesisStateElectra(t, 1000) // 1000 validators
|
||||
target := source.Copy()
|
||||
|
||||
// Make realistic changes
|
||||
_ = target.SetSlot(source.Slot() + 32) // One epoch
|
||||
validators := target.Validators()
|
||||
for i := 0; i < 100; i++ { // 10% of validators changed
|
||||
validators[i].EffectiveBalance += 1000000000 // 1 ETH change
|
||||
}
|
||||
_ = target.SetValidators(validators)
|
||||
|
||||
// Should complete quickly
|
||||
start := time.Now()
|
||||
diff, err := Diff(source, target)
|
||||
duration := time.Since(start)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, true, duration < time.Second, "Diff creation took too long: %v", duration)
|
||||
require.Equal(t, true, len(diff.StateDiff) > 0, "Should have state diff")
|
||||
})
|
||||
|
||||
t.Run("realistic_diff_application", func(t *testing.T) {
|
||||
// Test applying diffs to large states
|
||||
source, _ := util.DeterministicGenesisStateElectra(t, 500)
|
||||
target := source.Copy()
|
||||
_ = target.SetSlot(source.Slot() + 1)
|
||||
|
||||
// Create and apply diff
|
||||
diff, err := Diff(source, target)
|
||||
require.NoError(t, err)
|
||||
|
||||
start := time.Now()
|
||||
result, err := ApplyDiff(t.Context(), source, diff)
|
||||
duration := time.Since(start)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, target.Slot(), result.Slot())
|
||||
require.Equal(t, true, duration < time.Second, "Diff application took too long: %v", duration)
|
||||
})
|
||||
}
|
||||
|
||||
// TestStateTransitionValidation tests realistic state transition scenarios
|
||||
func TestStateTransitionValidation(t *testing.T) {
|
||||
t.Run("validator_slashing_scenario", func(t *testing.T) {
|
||||
source, _ := util.DeterministicGenesisStateElectra(t, 10)
|
||||
target := source.Copy()
|
||||
|
||||
// Simulate validator slashing (realistic scenario)
|
||||
validators := target.Validators()
|
||||
validators[0].Slashed = true
|
||||
validators[0].EffectiveBalance = 0 // Slashed validator loses balance
|
||||
_ = target.SetValidators(validators)
|
||||
|
||||
// This should work fine
|
||||
diff, err := Diff(source, target)
|
||||
require.NoError(t, err)
|
||||
|
||||
result, err := ApplyDiff(t.Context(), source, diff)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, true, result.Validators()[0].Slashed)
|
||||
require.Equal(t, uint64(0), result.Validators()[0].EffectiveBalance)
|
||||
})
|
||||
|
||||
t.Run("epoch_transition_scenario", func(t *testing.T) {
|
||||
source, _ := util.DeterministicGenesisStateElectra(t, 64)
|
||||
target := source.Copy()
|
||||
|
||||
// Simulate epoch transition with multiple changes
|
||||
_ = target.SetSlot(source.Slot() + 32) // One epoch
|
||||
|
||||
// Some validators get rewards, others get penalties
|
||||
balances := target.Balances()
|
||||
for i := 0; i < len(balances); i++ {
|
||||
if i%2 == 0 {
|
||||
balances[i] += 100000000 // 0.1 ETH reward
|
||||
} else {
|
||||
if balances[i] > 50000000 {
|
||||
balances[i] -= 50000000 // 0.05 ETH penalty
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = target.SetBalances(balances)
|
||||
|
||||
// This should work smoothly
|
||||
diff, err := Diff(source, target)
|
||||
require.NoError(t, err)
|
||||
|
||||
result, err := ApplyDiff(t.Context(), source, diff)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, target.Slot(), result.Slot())
|
||||
})
|
||||
|
||||
t.Run("consistent_state_root", func(t *testing.T) {
|
||||
// Test that diffs preserve state consistency
|
||||
source, _ := util.DeterministicGenesisStateElectra(t, 32)
|
||||
target := source.Copy()
|
||||
|
||||
// Make minimal changes
|
||||
_ = target.SetSlot(source.Slot() + 1)
|
||||
|
||||
// Diff and apply should be consistent
|
||||
diff, err := Diff(source, target)
|
||||
require.NoError(t, err)
|
||||
|
||||
result, err := ApplyDiff(t.Context(), source, diff)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Result should match target
|
||||
require.Equal(t, target.Slot(), result.Slot())
|
||||
require.Equal(t, len(target.Validators()), len(result.Validators()))
|
||||
require.Equal(t, len(target.Balances()), len(result.Balances()))
|
||||
})
|
||||
}
|
||||
|
||||
// TestSerializationRoundTrip tests serialization consistency
|
||||
func TestSerializationRoundTrip(t *testing.T) {
|
||||
t.Run("diff_serialization_consistency", func(t *testing.T) {
|
||||
// Test that serialization and deserialization are consistent
|
||||
source, _ := util.DeterministicGenesisStateElectra(t, 16)
|
||||
target := source.Copy()
|
||||
|
||||
// Make changes
|
||||
_ = target.SetSlot(source.Slot() + 5)
|
||||
validators := target.Validators()
|
||||
validators[0].EffectiveBalance += 1000000000
|
||||
_ = target.SetValidators(validators)
|
||||
|
||||
// Create diff
|
||||
diff1, err := Diff(source, target)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Deserialize and re-serialize
|
||||
hdiff, err := newHdiff(diff1)
|
||||
require.NoError(t, err)
|
||||
|
||||
diff2 := hdiff.serialize()
|
||||
|
||||
// Apply both diffs - should get same result
|
||||
result1, err := ApplyDiff(t.Context(), source, diff1)
|
||||
require.NoError(t, err)
|
||||
|
||||
result2, err := ApplyDiff(t.Context(), source, diff2)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, result1.Slot(), result2.Slot())
|
||||
require.Equal(t, result1.Validators()[0].EffectiveBalance, result2.Validators()[0].EffectiveBalance)
|
||||
})
|
||||
|
||||
t.Run("empty_diff_handling", func(t *testing.T) {
|
||||
// Test that empty diffs are handled correctly
|
||||
source, _ := util.DeterministicGenesisStateElectra(t, 8)
|
||||
target := source.Copy() // No changes
|
||||
|
||||
// Should create minimal diff
|
||||
diff, err := Diff(source, target)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Apply should work and return equivalent state
|
||||
result, err := ApplyDiff(t.Context(), source, diff)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, source.Slot(), result.Slot())
|
||||
require.Equal(t, len(source.Validators()), len(result.Validators()))
|
||||
})
|
||||
|
||||
t.Run("compression_efficiency", func(t *testing.T) {
|
||||
// Test that compression is working effectively
|
||||
source, _ := util.DeterministicGenesisStateElectra(t, 100)
|
||||
target := source.Copy()
|
||||
|
||||
// Make small changes
|
||||
_ = target.SetSlot(source.Slot() + 1)
|
||||
validators := target.Validators()
|
||||
validators[0].EffectiveBalance += 1000000000
|
||||
_ = target.SetValidators(validators)
|
||||
|
||||
// Create diff
|
||||
diff, err := Diff(source, target)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Get full state size
|
||||
fullStateSSZ, err := target.MarshalSSZ()
|
||||
require.NoError(t, err)
|
||||
|
||||
// Diff should be much smaller than full state
|
||||
diffSize := len(diff.StateDiff) + len(diff.ValidatorDiffs) + len(diff.BalancesDiff)
|
||||
require.Equal(t, true, diffSize < len(fullStateSSZ)/2,
|
||||
"Diff should be smaller than full state: diff=%d, full=%d", diffSize, len(fullStateSSZ))
|
||||
})
|
||||
}
|
||||
|
||||
// TestKMPSecurity tests the KMP algorithm for security issues
|
||||
func TestKMPSecurity(t *testing.T) {
|
||||
t.Run("nil_pointer_handling", func(t *testing.T) {
|
||||
// Test with nil pointers in the pattern/text
|
||||
pattern := []*int{nil, nil, nil}
|
||||
text := []*int{nil, nil, nil, nil, nil}
|
||||
|
||||
equals := func(a, b *int) bool {
|
||||
if a == nil && b == nil {
|
||||
return true
|
||||
}
|
||||
if a == nil || b == nil {
|
||||
return false
|
||||
}
|
||||
return *a == *b
|
||||
}
|
||||
|
||||
// Should not panic - result can be any integer
|
||||
result := kmpIndex(len(pattern), text, equals)
|
||||
_ = result // Any result is valid, just ensure no panic
|
||||
})
|
||||
|
||||
t.Run("empty_pattern_edge_case", func(t *testing.T) {
|
||||
var pattern []*int
|
||||
text := []*int{new(int), new(int)}
|
||||
|
||||
equals := func(a, b *int) bool { return a == b }
|
||||
|
||||
result := kmpIndex(0, text, equals)
|
||||
require.Equal(t, 0, result, "Empty pattern should return 0")
|
||||
_ = pattern // Silence unused variable warning
|
||||
})
|
||||
|
||||
t.Run("realistic_pattern_performance", func(t *testing.T) {
|
||||
// Test with realistic sizes to ensure good performance
|
||||
realisticSize := 100 // More realistic for validator arrays
|
||||
pattern := make([]*int, realisticSize)
|
||||
text := make([]*int, realisticSize*2)
|
||||
|
||||
// Create realistic pattern
|
||||
for i := range pattern {
|
||||
val := i % 10 // More variation
|
||||
pattern[i] = &val
|
||||
}
|
||||
for i := range text {
|
||||
val := i % 10
|
||||
text[i] = &val
|
||||
}
|
||||
|
||||
equals := func(a, b *int) bool {
|
||||
if a == nil && b == nil {
|
||||
return true
|
||||
}
|
||||
if a == nil || b == nil {
|
||||
return false
|
||||
}
|
||||
return *a == *b
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
result := kmpIndex(len(pattern), text, equals)
|
||||
duration := time.Since(start)
|
||||
|
||||
// Should complete quickly with realistic inputs
|
||||
require.Equal(t, true, duration < time.Second,
|
||||
"KMP took too long: %v", duration)
|
||||
_ = result // Any result is valid, just ensure performance is good
|
||||
})
|
||||
}
|
||||
|
||||
// TestConcurrencySafety tests thread safety of the hdiff operations
|
||||
func TestConcurrencySafety(t *testing.T) {
|
||||
t.Run("concurrent_diff_creation", func(t *testing.T) {
|
||||
source, _ := util.DeterministicGenesisStateElectra(t, 32)
|
||||
target := source.Copy()
|
||||
_ = target.SetSlot(source.Slot() + 1)
|
||||
|
||||
const numGoroutines = 10
|
||||
const iterations = 100
|
||||
|
||||
var wg sync.WaitGroup
|
||||
errors := make(chan error, numGoroutines*iterations)
|
||||
|
||||
for i := 0; i < numGoroutines; i++ {
|
||||
wg.Add(1)
|
||||
go func(workerID int) {
|
||||
defer wg.Done()
|
||||
|
||||
for j := 0; j < iterations; j++ {
|
||||
_, err := Diff(source, target)
|
||||
if err != nil {
|
||||
errors <- fmt.Errorf("worker %d iteration %d: %v", workerID, j, err)
|
||||
}
|
||||
}
|
||||
}(i)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
close(errors)
|
||||
|
||||
// Check for any errors
|
||||
for err := range errors {
|
||||
t.Error(err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("concurrent_diff_application", func(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
source, _ := util.DeterministicGenesisStateElectra(t, 16)
|
||||
target := source.Copy()
|
||||
_ = target.SetSlot(source.Slot() + 5)
|
||||
|
||||
diff, err := Diff(source, target)
|
||||
require.NoError(t, err)
|
||||
|
||||
const numGoroutines = 10
|
||||
var wg sync.WaitGroup
|
||||
errors := make(chan error, numGoroutines)
|
||||
|
||||
for i := 0; i < numGoroutines; i++ {
|
||||
wg.Add(1)
|
||||
go func(workerID int) {
|
||||
defer wg.Done()
|
||||
|
||||
// Each goroutine needs its own copy of the source state
|
||||
localSource := source.Copy()
|
||||
_, err := ApplyDiff(ctx, localSource, diff)
|
||||
if err != nil {
|
||||
errors <- fmt.Errorf("worker %d: %v", workerID, err)
|
||||
}
|
||||
}(i)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
close(errors)
|
||||
|
||||
// Check for any errors
|
||||
for err := range errors {
|
||||
t.Error(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
2212
consensus-types/hdiff/state_diff.go
Normal file
2212
consensus-types/hdiff/state_diff.go
Normal file
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user