Skip to content

Commit

Permalink
json get block handler refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kasey committed May 1, 2024
1 parent eda3e43 commit d30336c
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 491 deletions.
2 changes: 2 additions & 0 deletions api/server/structs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ go_library(
"//api/server:go_default_library",
"//beacon-chain/state:go_default_library",
"//config/fieldparams:go_default_library",
"//consensus-types/blocks:go_default_library",
"//consensus-types/interfaces:go_default_library",
"//consensus-types/primitives:go_default_library",
"//consensus-types/validator:go_default_library",
"//container/slice:go_default_library",
Expand Down
93 changes: 93 additions & 0 deletions api/server/structs/block.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
package structs

import "encoding/json"

// MessageJsoner describes a signed consensus type wrapper that can return the `.Message` field in a json envelope
// encoded as a []byte, for use as a json.RawMessage value when encoding the outer envelope.
type MessageJsoner interface {
MessageRawJson() ([]byte, error)
}

type SignedMessageJsoner interface {
MessageJsoner
SigString() string
}

type SignedBeaconBlock struct {
Message *BeaconBlock `json:"message"`
Signature string `json:"signature"`
}

var _ SignedMessageJsoner = &SignedBeaconBlock{}

func (s *SignedBeaconBlock) MessageRawJson() ([]byte, error) {
return json.Marshal(s.Message)
}

func (s *SignedBeaconBlock) SigString() string {
return s.Signature
}

type BeaconBlock struct {
Slot string `json:"slot"`
ProposerIndex string `json:"proposer_index"`
Expand All @@ -29,6 +52,16 @@ type SignedBeaconBlockAltair struct {
Signature string `json:"signature"`
}

var _ SignedMessageJsoner = &SignedBeaconBlockAltair{}

func (s *SignedBeaconBlockAltair) MessageRawJson() ([]byte, error) {
return json.Marshal(s.Message)
}

func (s *SignedBeaconBlockAltair) SigString() string {
return s.Signature
}

type BeaconBlockAltair struct {
Slot string `json:"slot"`
ProposerIndex string `json:"proposer_index"`
Expand All @@ -54,6 +87,16 @@ type SignedBeaconBlockBellatrix struct {
Signature string `json:"signature"`
}

var _ SignedMessageJsoner = &SignedBeaconBlockBellatrix{}

func (s *SignedBeaconBlockBellatrix) MessageRawJson() ([]byte, error) {
return json.Marshal(s.Message)
}

func (s *SignedBeaconBlockBellatrix) SigString() string {
return s.Signature
}

type BeaconBlockBellatrix struct {
Slot string `json:"slot"`
ProposerIndex string `json:"proposer_index"`
Expand All @@ -80,6 +123,16 @@ type SignedBlindedBeaconBlockBellatrix struct {
Signature string `json:"signature"`
}

var _ SignedMessageJsoner = &SignedBlindedBeaconBlockBellatrix{}

func (s *SignedBlindedBeaconBlockBellatrix) MessageRawJson() ([]byte, error) {
return json.Marshal(s.Message)
}

func (s *SignedBlindedBeaconBlockBellatrix) SigString() string {
return s.Signature
}

type BlindedBeaconBlockBellatrix struct {
Slot string `json:"slot"`
ProposerIndex string `json:"proposer_index"`
Expand All @@ -106,6 +159,16 @@ type SignedBeaconBlockCapella struct {
Signature string `json:"signature"`
}

var _ SignedMessageJsoner = &SignedBeaconBlockCapella{}

func (s *SignedBeaconBlockCapella) MessageRawJson() ([]byte, error) {
return json.Marshal(s.Message)
}

func (s *SignedBeaconBlockCapella) SigString() string {
return s.Signature
}

type BeaconBlockCapella struct {
Slot string `json:"slot"`
ProposerIndex string `json:"proposer_index"`
Expand Down Expand Up @@ -133,6 +196,16 @@ type SignedBlindedBeaconBlockCapella struct {
Signature string `json:"signature"`
}

var _ SignedMessageJsoner = &SignedBlindedBeaconBlockCapella{}

func (s *SignedBlindedBeaconBlockCapella) MessageRawJson() ([]byte, error) {
return json.Marshal(s.Message)
}

func (s *SignedBlindedBeaconBlockCapella) SigString() string {
return s.Signature
}

type BlindedBeaconBlockCapella struct {
Slot string `json:"slot"`
ProposerIndex string `json:"proposer_index"`
Expand Down Expand Up @@ -172,6 +245,16 @@ type SignedBeaconBlockDeneb struct {
Signature string `json:"signature"`
}

var _ SignedMessageJsoner = &SignedBeaconBlockDeneb{}

func (s *SignedBeaconBlockDeneb) MessageRawJson() ([]byte, error) {
return json.Marshal(s.Message)
}

func (s *SignedBeaconBlockDeneb) SigString() string {
return s.Signature
}

type BeaconBlockDeneb struct {
Slot string `json:"slot"`
ProposerIndex string `json:"proposer_index"`
Expand Down Expand Up @@ -208,6 +291,16 @@ type SignedBlindedBeaconBlockDeneb struct {
Signature string `json:"signature"`
}

var _ SignedMessageJsoner = &SignedBlindedBeaconBlockDeneb{}

func (s *SignedBlindedBeaconBlockDeneb) MessageRawJson() ([]byte, error) {
return json.Marshal(s.Message)
}

func (s *SignedBlindedBeaconBlockDeneb) SigString() string {
return s.Signature
}

type BlindedBeaconBlockBodyDeneb struct {
RandaoReveal string `json:"randao_reveal"`
Eth1Data *Eth1Data `json:"eth1_data"`
Expand Down
33 changes: 32 additions & 1 deletion api/server/structs/conversions_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v5/api/server"
fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams"
"github.com/prysmaticlabs/prysm/v5/consensus-types/interfaces"
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/v5/container/slice"
"github.com/prysmaticlabs/prysm/v5/encoding/bytesutil"
enginev1 "github.com/prysmaticlabs/prysm/v5/proto/engine/v1"
eth "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
)

var ErrUnsupportedConversion = errors.New("Could not determine api struct type to use for value")

func (h *SignedBeaconBlockHeader) ToConsensus() (*eth.SignedBeaconBlockHeader, error) {
msg, err := h.Message.ToConsensus()
if err != nil {
Expand Down Expand Up @@ -1852,7 +1856,34 @@ func BeaconBlockFromConsensus(b *eth.BeaconBlock) *BeaconBlock {
}
}

func SignedBeaconBlockFromConsensus(b *eth.SignedBeaconBlock) *SignedBeaconBlock {
func SignedBeaconBlockMessageJsoner(block interfaces.ReadOnlySignedBeaconBlock) (SignedMessageJsoner, error) {
pb, err := block.Proto()
if err != nil {
return nil, err
}
switch pbStruct := pb.(type) {
case *eth.SignedBeaconBlock:
return SignedBeaconBlockPhase0FromConsensus(pbStruct), nil
case *eth.SignedBeaconBlockAltair:
return SignedBeaconBlockAltairFromConsensus(pbStruct), nil
case *eth.SignedBlindedBeaconBlockBellatrix:
return SignedBlindedBeaconBlockBellatrixFromConsensus(pbStruct)
case *eth.SignedBeaconBlockBellatrix:
return SignedBeaconBlockBellatrixFromConsensus(pbStruct)
case *eth.SignedBlindedBeaconBlockCapella:
return SignedBlindedBeaconBlockCapellaFromConsensus(pbStruct)
case *eth.SignedBeaconBlockCapella:
return SignedBeaconBlockCapellaFromConsensus(pbStruct)
case *eth.SignedBlindedBeaconBlockDeneb:
return SignedBlindedBeaconBlockDenebFromConsensus(pbStruct)
case *eth.SignedBeaconBlockDeneb:
return SignedBeaconBlockDenebFromConsensus(pbStruct)
default:
return nil, ErrUnsupportedConversion
}
}

func SignedBeaconBlockPhase0FromConsensus(b *eth.SignedBeaconBlock) *SignedBeaconBlock {
return &SignedBeaconBlock{
Message: BeaconBlockFromConsensus(b.Block),
Signature: hexutil.Encode(b.Signature),
Expand Down
Loading

0 comments on commit d30336c

Please sign in to comment.