Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add /eth/v1/beacon/deposit_snapshot endpoint #13514

Merged
merged 28 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions beacon-chain/rpc/eth/beacon/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ go_test(
"//api:go_default_library",
"//api/server:go_default_library",
"//beacon-chain/blockchain/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",
"//beacon-chain/core/transition:go_default_library",
Expand Down
49 changes: 49 additions & 0 deletions beacon-chain/rpc/eth/beacon/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2070,3 +2070,52 @@ func (s *Server) GetGenesis(w http.ResponseWriter, r *http.Request) {
}
httputil.WriteJson(w, resp)
}

// 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")
defer span.End()

if s.BeaconDB == nil {
httputil.HandleError(w, "Could not retrieve beaconDB", http.StatusInternalServerError)
return
}
eth1data, err := s.BeaconDB.ExecutionChainData(ctx)
saolyn marked this conversation as resolved.
Show resolved Hide resolved
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)
return
}
snapshot := eth1data.DepositSnapshot
if snapshot == nil || len(snapshot.Finalized) == 0 {
httputil.HandleError(w, "No Finalized Snapshot Available", http.StatusNotFound)
saolyn marked this conversation as resolved.
Show resolved Hide resolved
return
}
finalized := make([][]string, 0, len(snapshot.Finalized))
for _, f := range snapshot.Finalized {
finalized = append(finalized, []string{hexutil.Encode(f)})
}
response := &GetDepositSnapshotResponse{
Data: &DepositSnapshot{
Finalized: finalized,
DepositRoot: hexutil.Encode(snapshot.DepositRoot),
DepositCount: strconv.FormatUint(snapshot.DepositCount, 10),
ExecutionBlockHash: hexutil.Encode(snapshot.ExecutionHash),
ExecutionBlockHeight: strconv.FormatUint(snapshot.ExecutionDepth, 10),
},
}
if httputil.RespondWithSsz(r) {
sszData, err := response.MarshalSSZ()
saolyn marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
httputil.HandleError(w, "Could not marshal deposit snapshot into SSZ", http.StatusInternalServerError)
saolyn marked this conversation as resolved.
Show resolved Hide resolved
return
}
httputil.WriteSsz(w, sszData, "deposit_snapshot.ssz")
} else {
saolyn marked this conversation as resolved.
Show resolved Hide resolved
httputil.WriteJson(w, response)
}
}
62 changes: 62 additions & 0 deletions beacon-chain/rpc/eth/beacon/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/prysmaticlabs/go-bitfield"
"github.com/prysmaticlabs/prysm/v4/api"
chainMock "github.com/prysmaticlabs/prysm/v4/beacon-chain/blockchain/testing"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/cache/depositsnapshot"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/core/transition"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/db"
dbTest "github.com/prysmaticlabs/prysm/v4/beacon-chain/db/testing"
Expand Down Expand Up @@ -3617,3 +3618,64 @@ func TestGetGenesis(t *testing.T) {
assert.StringContains(t, "Chain genesis info is not yet known", e.Message)
})
}

func TestGetDepositSnapshot(t *testing.T) {
beaconDB := dbTest.SetupDB(t)
mockTrie := depositsnapshot.NewDepositTree()
saolyn marked this conversation as resolved.
Show resolved Hide resolved
deposits := [][32]byte{
bytesutil.ToBytes32([]byte{1}),
bytesutil.ToBytes32([]byte{2}),
bytesutil.ToBytes32([]byte{3}),
}
finalized := 2
for _, leaf := range deposits {
err := mockTrie.Insert(leaf[:], 0)
require.NoError(t, err)
}
err := mockTrie.Finalize(1, deposits[1], 1)
require.NoError(t, err)
err = mockTrie.Finalize(2, deposits[2], 2)
require.NoError(t, err)

snapshot, err := mockTrie.GetSnapshot()
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,
}

request := httptest.NewRequest(http.MethodGet, "/eth/v1/beacon/deposit_snapshot", nil)
writer := httptest.NewRecorder()
writer.Body = &bytes.Buffer{}
t.Run("JSON response", func(t *testing.T) {
s.GetDepositSnapshot(writer, request)
assert.Equal(t, http.StatusOK, writer.Code)
resp := &GetDepositSnapshotResponse{}
require.NoError(t, json.Unmarshal(writer.Body.Bytes(), resp))
require.NotNil(t, resp.Data)

assert.Equal(t, hexutil.Encode(root[:]), resp.Data.DepositRoot)
assert.Equal(t, hexutil.Encode(deposits[2][:]), resp.Data.ExecutionBlockHash)
assert.Equal(t, strconv.Itoa(mockTrie.NumOfItems()), resp.Data.DepositCount)
assert.Equal(t, finalized, len(resp.Data.Finalized))
})
t.Run("SSZ response", func(t *testing.T) {
request.Header.Set("Accept", api.OctetStreamMediaType)
s.GetDepositSnapshot(writer, request)
assert.Equal(t, http.StatusOK, writer.Code)
resp := &GetDepositSnapshotResponse{}
require.NoError(t, resp.UnmarshalSSZ(writer.Body.Bytes()))
require.NotNil(t, resp.Data)

assert.Equal(t, hexutil.Encode(root[:]), resp.Data.DepositRoot)
assert.Equal(t, hexutil.Encode(deposits[2][:]), resp.Data.ExecutionBlockHash)
assert.Equal(t, strconv.Itoa(mockTrie.NumOfItems()), resp.Data.DepositCount)
assert.Equal(t, finalized, len(resp.Data.Finalized))
})
}
15 changes: 15 additions & 0 deletions beacon-chain/rpc/eth/beacon/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package beacon
import (
"encoding/json"

ssz "github.com/prysmaticlabs/fastssz"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/eth/shared"
)

Expand Down Expand Up @@ -188,3 +189,17 @@ type GetAttesterSlashingsResponse struct {
type GetProposerSlashingsResponse struct {
Data []*shared.ProposerSlashing `json:"data"`
}

type GetDepositSnapshotResponse struct {
Data *DepositSnapshot `json:"data"`
ssz.Marshaler
saolyn marked this conversation as resolved.
Show resolved Hide resolved
ssz.Unmarshaler
}

type DepositSnapshot struct {
Finalized [][]string `json:"finalized"`
DepositRoot string `json:"deposit_root"`
DepositCount string `json:"deposit_count"`
ExecutionBlockHash string `json:"execution_block_hash"`
ExecutionBlockHeight string `json:"execution_block_height"`
}
1 change: 1 addition & 0 deletions beacon-chain/rpc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ func (s *Service) initializeBeaconServerRoutes(beaconServer *beacon.Server) {
s.cfg.Router.HandleFunc("/eth/v1/beacon/blocks/{block_id}/attestations", beaconServer.GetBlockAttestations).Methods(http.MethodGet)
s.cfg.Router.HandleFunc("/eth/v1/beacon/blinded_blocks/{block_id}", beaconServer.GetBlindedBlock).Methods(http.MethodGet)
s.cfg.Router.HandleFunc("/eth/v1/beacon/blocks/{block_id}/root", beaconServer.GetBlockRoot).Methods(http.MethodGet)
s.cfg.Router.HandleFunc("/eth/v1/beacon/deposit_snapshot", beaconServer.GetDepositSnapshot).Methods(http.MethodGet)
s.cfg.Router.HandleFunc("/eth/v1/beacon/pool/attestations", beaconServer.ListAttestations).Methods(http.MethodGet)
s.cfg.Router.HandleFunc("/eth/v1/beacon/pool/attestations", beaconServer.SubmitAttestations).Methods(http.MethodPost)
s.cfg.Router.HandleFunc("/eth/v1/beacon/pool/voluntary_exits", beaconServer.ListVoluntaryExits).Methods(http.MethodGet)
Expand Down
28 changes: 14 additions & 14 deletions beacon-chain/rpc/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,20 @@ func TestServer_InitializeRoutes(t *testing.T) {
"/eth/v1/beacon/blocks/{block_id}/attestations": {http.MethodGet},
"/eth/v1/beacon/blob_sidecars/{block_id}": {http.MethodGet},
"/eth/v1/beacon/rewards/sync_committee/{block_id}": {http.MethodPost},
//"/eth/v1/beacon/deposit_snapshot": {http.MethodGet}, not implemented
"/eth/v1/beacon/rewards/blocks/{block_id}": {http.MethodGet},
"/eth/v1/beacon/rewards/attestations/{epoch}": {http.MethodPost},
"/eth/v1/beacon/blinded_blocks/{block_id}": {http.MethodGet},
"/eth/v1/beacon/light_client/bootstrap/{block_root}": {http.MethodGet},
"/eth/v1/beacon/light_client/updates": {http.MethodGet},
"/eth/v1/beacon/light_client/finality_update": {http.MethodGet},
"/eth/v1/beacon/light_client/optimistic_update": {http.MethodGet},
"/eth/v1/beacon/pool/attestations": {http.MethodGet, http.MethodPost},
"/eth/v1/beacon/pool/attester_slashings": {http.MethodGet, http.MethodPost},
"/eth/v1/beacon/pool/proposer_slashings": {http.MethodGet, http.MethodPost},
"/eth/v1/beacon/pool/sync_committees": {http.MethodPost},
"/eth/v1/beacon/pool/voluntary_exits": {http.MethodGet, http.MethodPost},
"/eth/v1/beacon/pool/bls_to_execution_changes": {http.MethodGet, http.MethodPost},
"/eth/v1/beacon/deposit_snapshot": {http.MethodGet},
"/eth/v1/beacon/rewards/blocks/{block_id}": {http.MethodGet},
"/eth/v1/beacon/rewards/attestations/{epoch}": {http.MethodPost},
"/eth/v1/beacon/blinded_blocks/{block_id}": {http.MethodGet},
"/eth/v1/beacon/light_client/bootstrap/{block_root}": {http.MethodGet},
"/eth/v1/beacon/light_client/updates": {http.MethodGet},
"/eth/v1/beacon/light_client/finality_update": {http.MethodGet},
"/eth/v1/beacon/light_client/optimistic_update": {http.MethodGet},
"/eth/v1/beacon/pool/attestations": {http.MethodGet, http.MethodPost},
"/eth/v1/beacon/pool/attester_slashings": {http.MethodGet, http.MethodPost},
"/eth/v1/beacon/pool/proposer_slashings": {http.MethodGet, http.MethodPost},
"/eth/v1/beacon/pool/sync_committees": {http.MethodPost},
"/eth/v1/beacon/pool/voluntary_exits": {http.MethodGet, http.MethodPost},
"/eth/v1/beacon/pool/bls_to_execution_changes": {http.MethodGet, http.MethodPost},
}

builderRoutes := map[string][]string{
Expand Down
Loading