Skip to content

Commit

Permalink
Merge branch 'beacon-endpoints' into genesis-time
Browse files Browse the repository at this point in the history
  • Loading branch information
nidhi-singh02 authored Sep 20, 2024
2 parents dc4a4c5 + 7436bd6 commit 222f853
Show file tree
Hide file tree
Showing 11 changed files with 440 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,8 @@ packages:
recursive: False
with-expecter: true
all: True
github.com/berachain/beacon-kit/mod/storage/pkg/block:
config:
recursive: False
with-expecter: true
all: True
7 changes: 7 additions & 0 deletions mod/node-api/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,10 @@ func (b *Backend[
st := b.sb.StateFromContext(queryCtx)
return st.GetSlot()
}

// GetSlotByParentRoot returns the slot by a parent root from the block store.
func (b *Backend[
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
]) GetSlotByParentRoot(root common.Root) (math.Slot, error) {
return b.sb.BlockStore().GetSlotByParentRoot(root)
}
56 changes: 56 additions & 0 deletions mod/node-api/backend/mocks/block_store.mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions mod/node-api/backend/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ type BlockStore[BeaconBlockT any] interface {
GetSlotByStateRoot(root common.Root) (math.Slot, error)
// GetSlotByExecutionNumber retrieves the slot by a given execution number.
GetSlotByExecutionNumber(executionNumber math.U64) (math.Slot, error)
// GetSlotByParentRoot retrieves the slot by a given parent root.
GetSlotByParentRoot(root common.Root) (math.Slot, error)
}

// DepositStore defines the interface for deposit storage.
Expand Down
2 changes: 2 additions & 0 deletions mod/node-api/handlers/beacon/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type Backend[
GetSlotByBlockRoot(root common.Root) (math.Slot, error)
// GetSlotByStateRoot retrieves the slot by a given root from the store.
GetSlotByStateRoot(root common.Root) (math.Slot, error)
// GetSlotByParentRoot retrieves the slot by a given root from the store.
GetSlotByParentRoot(root common.Root) (math.Slot, error)
// GetHeadSlot retrieves the head slot from the store.
GetHeadSlot() (math.Slot, error)
}
Expand Down
85 changes: 71 additions & 14 deletions mod/node-api/handlers/beacon/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
package beacon

import (
"github.com/berachain/beacon-kit/mod/errors"
beacontypes "github.com/berachain/beacon-kit/mod/node-api/handlers/beacon/types"
"github.com/berachain/beacon-kit/mod/node-api/handlers/types"
"github.com/berachain/beacon-kit/mod/node-api/handlers/utils"
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
"github.com/berachain/beacon-kit/mod/primitives/pkg/crypto"
"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
)
Expand All @@ -37,27 +39,23 @@ func (h *Handler[
if err != nil {
return nil, err
}
var slot math.Slot
// If slot is not being passed in the request,
// by default fetch current head slot. Else use the slot from the request.
if req.Slot == "" {
slot, err = h.backend.GetHeadSlot()
if err != nil {
return nil, err
}
} else {
slot, err = utils.U64FromString(req.Slot)
if err != nil {
return nil, err
}

slot, err := h.determineSlot(&req)
if err != nil {
return nil, err
}
header, err := h.backend.BlockHeaderAtSlot(slot)
if err != nil {
return nil, err
}

root, err := h.backend.BlockRootAtSlot(slot)
if err != nil {
return nil, err
}

return types.Wrap(&beacontypes.BlockHeaderResponse[BeaconBlockHeaderT]{
Root: header.GetBodyRoot(),
Root: root,
Canonical: true,
Header: &beacontypes.BlockHeader[BeaconBlockHeaderT]{
Message: header,
Expand All @@ -66,6 +64,65 @@ func (h *Handler[
}), nil
}

func (h *Handler[
BeaconBlockHeaderT, ContextT, _, _,
]) determineSlot(req *beacontypes.GetBlockHeadersRequest) (math.Slot, error) {
var parentRoot common.Root
switch {
case req.Slot != "" && req.ParentRoot != "":
// Both slot and parent_root are provided
slot, err := utils.U64FromString(req.Slot)
if err != nil {
return 0, errors.Wrapf(err, "invalid slot: %v", req.Slot)
}
err = parentRoot.UnmarshalText([]byte(req.ParentRoot))
if err != nil {
return 0, errors.Wrapf(err, "invalid parent root: %v", req.ParentRoot)
}
// Verify that the provided slot and parent root match
verifiedSlot, errVerify := h.backend.GetSlotByParentRoot(parentRoot)
if errVerify != nil {
return 0, errVerify
}
if verifiedSlot != slot {
return 0, errors.New(
"provided slot does not match the slot for the given parent root",
)
}
return slot, nil

case req.Slot != "":
// If only slot is provided
slot, err := utils.U64FromString(req.Slot)
if err != nil {
return 0, err
}
return slot, nil

case req.ParentRoot != "":
// If only parent_root is provided

// Convert the string to common.Root
err := parentRoot.UnmarshalText([]byte(req.ParentRoot))
if err != nil {
return 0, errors.Wrapf(err, "invalid parent root: %v", req.ParentRoot)
}
slot, err := h.backend.GetSlotByParentRoot(parentRoot)
if err != nil {
return 0, err
}
return slot, nil

default:
// If neither slot nor parent_root is provided, fetch current head slot
slot, err := h.backend.GetHeadSlot()
if err != nil {
return 0, err
}
return slot, nil
}
}

func (h *Handler[
BeaconBlockHeaderT, ContextT, _, _,
]) GetBlockHeaderByID(c ContextT) (any, error) {
Expand Down
4 changes: 4 additions & 0 deletions mod/node-core/pkg/components/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@ type (
// number
// from the store.
GetSlotByExecutionNumber(executionNumber math.U64) (math.Slot, error)
// GetSlotByParentRoot retrieves the slot by a given parent root from the
// store.
GetSlotByParentRoot(root common.Root) (math.Slot, error)
}

ConsensusEngine interface {
Expand Down Expand Up @@ -1102,6 +1105,7 @@ type (
GetSlotByStateRoot(root common.Root) (math.Slot, error)
GetSlotByExecutionNumber(executionNumber math.U64) (math.Slot, error)
GetHeadSlot() (math.Slot, error)
GetSlotByParentRoot(root common.Root) (math.Slot, error)

NodeAPIBeaconBackend[
BeaconStateT, BeaconBlockHeaderT, ForkT, ValidatorT,
Expand Down
Loading

0 comments on commit 222f853

Please sign in to comment.