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

chore(node-api): stub for state finality checkpoints #2124

Draft
wants to merge 1 commit into
base: get-validators-filter
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
41 changes: 41 additions & 0 deletions mod/node-api/handlers/beacon/historical.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
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"
)

func (h *Handler[_, ContextT, _, _, _]) GetStateRoot(c ContextT) (any, error) {
Expand Down Expand Up @@ -72,3 +73,43 @@ func (h *Handler[_, ContextT, _, _, _]) GetStateFork(c ContextT) (any, error) {
Data: beacontypes.ForkData{Fork: fork},
}, nil
}

func (h *Handler[_, ContextT, _, _, _]) GetStateFinalityCheckpoints(
c ContextT,
) (any, error) {
req, err := utils.BindAndValidate[beacontypes.GetStateForkRequest](
c, h.Logger(),
)
if err != nil {
return nil, err
}
slot, err := utils.SlotFromStateID(req.StateID, h.backend)
if err != nil {
return nil, err
}
fork, err := h.backend.StateForkAtSlot(slot)
if err != nil {
return nil, err
}

return beacontypes.ValidatorResponse{
ExecutionOptimistic: false, // stubbed
Finalized: false, // stubbed
Data: beacontypes.FinalityCheckpointsData{
// In case finality is not achieved yet, return the
// epoch 0 and ZERO_HASH as root.
PreviousJustified: common.Checkpoint{
Epoch: fork.GetEpoch(),
Root: [32]byte{},
},
CurrentJustified: common.Checkpoint{
Epoch: fork.GetEpoch(),
Root: [32]byte{},
},
Finalized: common.Checkpoint{
Epoch: 0,
Root: [32]byte{},
},
},
}, nil
}
2 changes: 1 addition & 1 deletion mod/node-api/handlers/beacon/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (h *Handler[_, ContextT, _, _, _]) RegisterRoutes(
{
Method: http.MethodGet,
Path: "/eth/v1/beacon/states/:state_id/finality_checkpoints",
Handler: h.NotImplemented,
Handler: h.GetStateFinalityCheckpoints,
},
{
Method: http.MethodGet,
Expand Down
6 changes: 6 additions & 0 deletions mod/node-api/handlers/beacon/types/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,9 @@ func (fr ForkData) MarshalJSON() ([]byte, error) {
Epoch: strconv.FormatUint(fr.GetEpoch().Unwrap(), 10),
})
}

type FinalityCheckpointsData struct {
PreviousJustified common.Checkpoint `json:"previous_justified"`
CurrentJustified common.Checkpoint `json:"current_justified"`
Finalized common.Checkpoint `json:"finalized"`
}
23 changes: 23 additions & 0 deletions mod/primitives/pkg/common/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
package common

import (
encodinghex "encoding/hex"
"strconv"

"github.com/berachain/beacon-kit/mod/chain-spec/pkg/chain"
"github.com/berachain/beacon-kit/mod/primitives/pkg/bytes"
"github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/hex"
Expand Down Expand Up @@ -118,3 +121,23 @@ func (r Root) MarshalJSON() ([]byte, error) {
func (r *Root) UnmarshalJSON(input []byte) error {
return r.UnmarshalText(input[1 : len(input)-1])
}

// Checkpoint provides information about a checkpoint.
type Checkpoint struct {
Epoch math.Epoch
Root Root `ssz-size:"32"`
}

// checkpointJSON is an internal representation of the struct.
type checkpointJSON struct {
Epoch string `json:"epoch"`
Root string `json:"root"`
}

// MarshalJSON implements json.Marshaler.
func (c Checkpoint) MarshalJSON() ([]byte, error) {
return json.Marshal(&checkpointJSON{
Epoch: strconv.FormatUint(uint64(c.Epoch), 10),
Root: "0x" + encodinghex.EncodeToString(c.Root[:]),
})
}
Loading