Skip to content

Commit

Permalink
GET
Browse files Browse the repository at this point in the history
  • Loading branch information
rkapka committed Apr 10, 2024
1 parent f7912e7 commit 615feb1
Show file tree
Hide file tree
Showing 16 changed files with 211 additions and 47 deletions.
1 change: 1 addition & 0 deletions beacon-chain/cache/depositcache/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ go_library(
"//beacon-chain/cache:go_default_library",
"//config/fieldparams:go_default_library",
"//config/params:go_default_library",
"//consensus-types:go_default_library",
"//container/trie:go_default_library",
"//crypto/hash:go_default_library",
"//encoding/bytesutil:go_default_library",
Expand Down
5 changes: 5 additions & 0 deletions beacon-chain/cache/depositcache/deposits_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/prysmaticlabs/prysm/v5/beacon-chain/cache"
fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams"
"github.com/prysmaticlabs/prysm/v5/config/params"
consensus_types "github.com/prysmaticlabs/prysm/v5/consensus-types"
"github.com/prysmaticlabs/prysm/v5/container/trie"
"github.com/prysmaticlabs/prysm/v5/encoding/bytesutil"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
Expand Down Expand Up @@ -312,6 +313,10 @@ func (dc *DepositCache) PruneProofs(ctx context.Context, untilDepositIndex int64
return nil
}

func (dc *DepositCache) Snapshot() (*ethpb.DepositSnapshot, error) {
return nil, consensus_types.ErrUnsupportedField
}

// Deposits returns the cached internal deposit tree.
func (fd *FinalizedDeposits) Deposits() cache.MerkleTree {
if fd.deposits != nil {
Expand Down
13 changes: 13 additions & 0 deletions beacon-chain/cache/depositcache/testing/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
load("@prysm//tools/go:def.bzl", "go_library")

go_library(
name = "go_default_library",
testonly = true,
srcs = ["mock.go"],
importpath = "github.com/prysmaticlabs/prysm/v5/beacon-chain/cache/depositcache/testing",
visibility = ["//visibility:public"],
deps = [
"//beacon-chain/cache:go_default_library",
"//proto/prysm/v1alpha1:go_default_library",
],
)
61 changes: 61 additions & 0 deletions beacon-chain/cache/depositcache/testing/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package testing

import (
"context"
"math/big"

"github.com/prysmaticlabs/prysm/v5/beacon-chain/cache"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
)

type MockDepositFetcher struct {
Snap *ethpb.DepositSnapshot
}

func (MockDepositFetcher) AllDeposits(_ context.Context, _ *big.Int) []*ethpb.Deposit {
panic("implement me")
}

func (MockDepositFetcher) AllDepositContainers(_ context.Context) []*ethpb.DepositContainer {
panic("implement me")
}

func (MockDepositFetcher) DepositByPubkey(_ context.Context, _ []byte) (*ethpb.Deposit, *big.Int) {
panic("implement me")
}

func (MockDepositFetcher) DepositsNumberAndRootAtHeight(_ context.Context, _ *big.Int) (uint64, [32]byte) {
panic("implement me")
}

func (MockDepositFetcher) InsertPendingDeposit(_ context.Context, _ *ethpb.Deposit, _ uint64, _ int64, _ [32]byte) {
panic("implement me")
}

func (MockDepositFetcher) PendingDeposits(_ context.Context, _ *big.Int) []*ethpb.Deposit {
panic("implement me")
}

func (MockDepositFetcher) PendingContainers(_ context.Context, _ *big.Int) []*ethpb.DepositContainer {
panic("implement me")
}

func (MockDepositFetcher) PrunePendingDeposits(_ context.Context, _ int64) {
panic("implement me")
}

func (MockDepositFetcher) PruneProofs(_ context.Context, _ int64) error {
panic("implement me")
}

func (m MockDepositFetcher) Snapshot() (*ethpb.DepositSnapshot, error) {
return m.Snap, nil
}

func (MockDepositFetcher) FinalizedDeposits(_ context.Context) (cache.FinalizedDeposits, error) {
panic("implement me")
}

func (MockDepositFetcher) NonFinalizedDeposits(_ context.Context, _ int64, _ *big.Int) []*ethpb.Deposit {
panic("implement me")
}
4 changes: 4 additions & 0 deletions beacon-chain/cache/depositsnapshot/deposit_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ func (c *Cache) InsertPendingDeposit(ctx context.Context, d *ethpb.Deposit, bloc
span.AddAttributes(trace.Int64Attribute("count", int64(len(c.pendingDeposits))))
}

func (c *Cache) Snapshot() (*ethpb.DepositSnapshot, error) {
return c.finalizedDeposits.depositTree.ToProto()
}

// Deposits returns the cached internal deposit tree.
func (fd *finalizedDepositsContainer) Deposits() cache.MerkleTree {
return fd.depositTree
Expand Down
1 change: 1 addition & 0 deletions beacon-chain/cache/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type DepositFetcher interface {
PendingContainers(ctx context.Context, untilBlk *big.Int) []*ethpb.DepositContainer
PrunePendingDeposits(ctx context.Context, merkleTreeIndex int64)
PruneProofs(ctx context.Context, untilDepositIndex int64) error
Snapshot() (*ethpb.DepositSnapshot, error)
FinalizedFetcher
}

Expand Down
5 changes: 5 additions & 0 deletions beacon-chain/deterministic-genesis/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ func (s *Service) PruneProofs(ctx context.Context, untilDepositIndex int64) erro
return nil
}

func (s *Service) Snapshot() (*ethpb.DepositSnapshot, error) {
log.Error("Snapshot should not be called")
return nil, nil
}

// Config options for the interop service.
type Config struct {
GenesisTime uint64
Expand Down
7 changes: 7 additions & 0 deletions beacon-chain/rpc/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ func (s *Service) beaconEndpoints(
FinalizationFetcher: s.cfg.FinalizationFetcher,
ForkchoiceFetcher: s.cfg.ForkchoiceFetcher,
CoreService: coreService,
DepositFetcher: s.cfg.DepositFetcher,
}

const namespace = "beacon"
Expand Down Expand Up @@ -548,6 +549,12 @@ func (s *Service) beaconEndpoints(
handler: server.GetValidatorBalances,
methods: []string{http.MethodGet, http.MethodPost},
},
{
template: "/eth/v1/beacon/deposit_snapshot",
name: namespace + ",GetDepositSnapshot",
handler: server.GetDepositSnapshot,
methods: []string{http.MethodGet},
},
}
}

Expand Down
2 changes: 2 additions & 0 deletions beacon-chain/rpc/eth/beacon/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ go_library(
"//api/server:go_default_library",
"//api/server/structs:go_default_library",
"//beacon-chain/blockchain:go_default_library",
"//beacon-chain/cache:go_default_library",
"//beacon-chain/cache/depositsnapshot:go_default_library",
"//beacon-chain/core/altair:go_default_library",
"//beacon-chain/core/blocks:go_default_library",
Expand Down Expand Up @@ -79,6 +80,7 @@ go_test(
"//api/server:go_default_library",
"//api/server/structs:go_default_library",
"//beacon-chain/blockchain/testing:go_default_library",
"//beacon-chain/cache/depositcache/testing:go_default_library",
"//beacon-chain/cache/depositsnapshot:go_default_library",
"//beacon-chain/core/signing:go_default_library",
"//beacon-chain/core/time:go_default_library",
Expand Down
23 changes: 9 additions & 14 deletions beacon-chain/rpc/eth/beacon/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2054,29 +2054,24 @@ func (s *Server) GetGenesis(w http.ResponseWriter, r *http.Request) {
// GetDepositSnapshot retrieves the EIP-4881 Deposit Tree Snapshot. Either a JSON or,
// if the Accept header was added, bytes serialized by SSZ will be returned.
func (s *Server) GetDepositSnapshot(w http.ResponseWriter, r *http.Request) {
ctx, span := trace.StartSpan(r.Context(), "beacon.GetDepositSnapshot")
_, span := trace.StartSpan(r.Context(), "beacon.GetDepositSnapshot")
defer span.End()

if s.BeaconDB == nil {
httputil.HandleError(w, "Could not retrieve beaconDB", http.StatusInternalServerError)
return
}
eth1data, err := s.BeaconDB.ExecutionChainData(ctx)
snapshot, err := s.DepositFetcher.Snapshot()
if err != nil {
httputil.HandleError(w, "Could not retrieve execution chain data: "+err.Error(), http.StatusInternalServerError)
return
}
if eth1data == nil {
httputil.HandleError(w, "Could not retrieve execution chain data: empty Eth1Data", http.StatusInternalServerError)
httputil.HandleError(w, "Could not retrieve snapshot: "+err.Error(), http.StatusInternalServerError)
return
}
snapshot := eth1data.DepositSnapshot
if snapshot == nil || len(snapshot.Finalized) == 0 {
httputil.HandleError(w, "No Finalized Snapshot Available", http.StatusNotFound)
httputil.HandleError(w, "Could not retrieve snapshot: empty snapshot", http.StatusInternalServerError)
return
}
if len(snapshot.Finalized) > depositsnapshot.DepositContractDepth {
httputil.HandleError(w, "Retrieved invalid deposit snapshot", http.StatusInternalServerError)
httputil.HandleError(
w,
fmt.Sprintf("Snapshot depth %d bigger than deposit contract depth %d", len(snapshot.Finalized), depositsnapshot.DepositContractDepth),
http.StatusInternalServerError,
)
return
}
if httputil.RespondWithSsz(r) {
Expand Down
9 changes: 2 additions & 7 deletions beacon-chain/rpc/eth/beacon/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"github.com/ethereum/go-ethereum/common/hexutil"
testing2 "github.com/prysmaticlabs/prysm/v5/beacon-chain/cache/depositcache/testing"
mockp2p "github.com/prysmaticlabs/prysm/v5/beacon-chain/p2p/testing"
logTest "github.com/sirupsen/logrus/hooks/test"
"go.uber.org/mock/gomock"
Expand Down Expand Up @@ -3575,7 +3576,6 @@ func TestGetGenesis(t *testing.T) {
}

func TestGetDepositSnapshot(t *testing.T) {
beaconDB := dbTest.SetupDB(t)
mockTrie := depositsnapshot.NewDepositTree()
deposits := [][32]byte{
bytesutil.ToBytes32([]byte{1}),
Expand All @@ -3596,13 +3596,8 @@ func TestGetDepositSnapshot(t *testing.T) {
require.NoError(t, err)
root, err := snapshot.CalculateRoot()
require.NoError(t, err)
chainData := &eth.ETH1ChainData{
DepositSnapshot: snapshot.ToProto(),
}
err = beaconDB.SaveExecutionChainData(context.Background(), chainData)
require.NoError(t, err)
s := Server{
BeaconDB: beaconDB,
DepositFetcher: &testing2.MockDepositFetcher{Snap: snapshot.ToProto()},
}

request := httptest.NewRequest(http.MethodGet, "/eth/v1/beacon/deposit_snapshot", nil)
Expand Down
2 changes: 2 additions & 0 deletions beacon-chain/rpc/eth/beacon/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package beacon

import (
"github.com/prysmaticlabs/prysm/v5/beacon-chain/blockchain"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/cache"
blockfeed "github.com/prysmaticlabs/prysm/v5/beacon-chain/core/feed/block"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/feed/operation"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/db"
Expand Down Expand Up @@ -48,4 +49,5 @@ type Server struct {
BLSChangesPool blstoexec.PoolManager
ForkchoiceFetcher blockchain.ForkchoiceFetcher
CoreService *core.Service
DepositFetcher cache.DepositFetcher
}
24 changes: 22 additions & 2 deletions testing/endtoend/evaluators/beaconapi/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ var requests = map[string]endpoint{
withParams(func(_ primitives.Epoch) []string {
return []string{"head"}
})),
"/beacon/blob_sidecars/{param1}": newMetadata[structs.SidecarsResponse](v1PathTemplate,
withStart(params.BeaconConfig().DenebForkEpoch),
withSsz(),
withParams(func(_ primitives.Epoch) []string {
return []string{"head"}
})),
"/beacon/deposit_snapshot": newMetadata[structs.DepositSnapshot](v1PathTemplate,
withStart(13), // we need to wait until finalization
withSsz()),
"/beacon/blinded_blocks/{param1}": newMetadata[structs.GetBlockV2Response](v1PathTemplate,
withSsz(),
withParams(func(_ primitives.Epoch) []string {
Expand Down Expand Up @@ -161,8 +170,19 @@ var requests = map[string]endpoint{
}
return compareJSON(pResp, lhResp)
})),
"/config/spec": newMetadata[structs.GetSpecResponse](v1PathTemplate,
withSanityCheckOnly()),
"/config/deposit_contract": newMetadata[structs.GetDepositContractResponse](v1PathTemplate),
"/debug/beacon/heads": newMetadata[structs.GetForkChoiceHeadsV2Response](v2PathTemplate),
"/debug/beacon/states/{param1}": newMetadata[structs.GetBeaconStateV2Response](v2PathTemplate,
withSanityCheckOnly(),
withSsz(),
withParams(func(_ primitives.Epoch) []string {
return []string{"head"}
})),
"/debug/beacon/heads": newMetadata[structs.GetForkChoiceHeadsV2Response](v2PathTemplate,
withSanityCheckOnly()),
"/debug/fork_choice": newMetadata[structs.GetForkChoiceDumpResponse](v1PathTemplate,
withSanityCheckOnly()),
"/node/identity": newMetadata[structs.GetIdentityResponse](v1PathTemplate,
withCustomEval(func(p interface{}, _ interface{}) error {
pResp, ok := p.(*structs.GetIdentityResponse)
Expand Down Expand Up @@ -242,7 +262,7 @@ var requests = map[string]endpoint{
}
if lhResp.Data[0].Slot == "0" {
// remove the first item from lighthouse data since lighthouse is returning a value despite no proposer
// there is no proposer on slot 0 so prysm don't return anything for slot 0
// there is no proposer on slot 0 so prysm doesn't return anything for slot 0
lhResp.Data = lhResp.Data[1:]
}
return compareJSON(pResp, lhResp)
Expand Down
23 changes: 23 additions & 0 deletions testing/endtoend/evaluators/beaconapi/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import "github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"

type endpoint interface {
getBasePath() string
sanityCheckOnlyEnabled() bool
enableSanityCheckOnly()
sszEnabled() bool
enableSsz()
getSszResp() []byte // retrieves the Prysm SSZ response
Expand All @@ -22,6 +24,7 @@ type endpoint interface {

type apiEndpoint[Resp any] struct {
basePath string
sanity bool
ssz bool
start primitives.Epoch
req interface{}
Expand All @@ -36,6 +39,14 @@ func (e *apiEndpoint[Resp]) getBasePath() string {
return e.basePath
}

func (e *apiEndpoint[Resp]) sanityCheckOnlyEnabled() bool {
return e.sanity
}

func (e *apiEndpoint[Resp]) enableSanityCheckOnly() {
e.sanity = true
}

func (e *apiEndpoint[Resp]) sszEnabled() bool {
return e.ssz
}
Expand Down Expand Up @@ -109,30 +120,42 @@ func newMetadata[Resp any](basePath string, opts ...endpointOpt) *apiEndpoint[Re

type endpointOpt func(endpoint)

// We only care if the request was successful, without comparing responses.
func withSanityCheckOnly() endpointOpt {
return func(e endpoint) {
e.enableSanityCheckOnly()
}
}

// We request SSZ data too.
func withSsz() endpointOpt {
return func(e endpoint) {
e.enableSsz()
}
}

// We begin issuing the request at a particular epoch.
func withStart(start primitives.Epoch) endpointOpt {
return func(e endpoint) {
e.setStart(start)
}
}

// We pass in a specific request object.
func withReq(req interface{}) endpointOpt {
return func(e endpoint) {
e.setReq(req)
}
}

// We specify URL parameters.
func withParams(f func(currentEpoch primitives.Epoch) []string) endpointOpt {
return func(e endpoint) {
e.setParams(f)
}
}

// We perform custom evaluation on responses.
func withCustomEval(f func(interface{}, interface{}) error) endpointOpt {
return func(e endpoint) {
e.setCustomEval(f)
Expand Down
Loading

0 comments on commit 615feb1

Please sign in to comment.