prysm-pulse/beacon-chain/rpc/eth/shared/structs.go
Radosław Kapka 3a09405bb7
HTTP Beacon API: /eth/v1/validator/aggregate_and_proofs (#12686)
* HTTP Beacon API: `/eth/v1/validator/contribution_and_proofs`

* add comment to invalid test case

* fix validation and test

* review

* in progress

* implementation

* remove test file

* remove duplicate

* tests

* review

* test fixes

---------

Co-authored-by: james-prysm <90280386+james-prysm@users.noreply.github.com>
Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
2023-08-03 22:24:23 +00:00

231 lines
7.1 KiB
Go

package shared
import (
"strconv"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/prysmaticlabs/prysm/v4/consensus-types/primitives"
eth "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
)
type Attestation struct {
AggregationBits string `json:"aggregation_bits" validate:"required,hexadecimal"`
Data *AttestationData `json:"data" validate:"required"`
Signature string `json:"signature" validate:"required,hexadecimal"`
}
type AttestationData struct {
Slot string `json:"slot" validate:"required,number,gte=0"`
CommitteeIndex string `json:"index" validate:"required,number,gte=0"`
BeaconBlockRoot string `json:"beacon_block_root" validate:"required,hexadecimal"`
Source *Checkpoint `json:"source" validate:"required"`
Target *Checkpoint `json:"target" validate:"required"`
}
type Checkpoint struct {
Epoch string `json:"epoch" validate:"required,number,gte=0"`
Root string `json:"root" validate:"required,hexadecimal"`
}
type SignedContributionAndProof struct {
Message *ContributionAndProof `json:"message" validate:"required"`
Signature string `json:"signature" validate:"required,hexadecimal"`
}
type ContributionAndProof struct {
AggregatorIndex string `json:"aggregator_index" validate:"required,number,gte=0"`
Contribution *SyncCommitteeContribution `json:"contribution" validate:"required"`
SelectionProof string `json:"selection_proof" validate:"required,hexadecimal"`
}
type SyncCommitteeContribution struct {
Slot string `json:"slot" validate:"required,number,gte=0"`
BeaconBlockRoot string `json:"beacon_block_root" hex:"true" validate:"required,hexadecimal"`
SubcommitteeIndex string `json:"subcommittee_index" validate:"required,number,gte=0"`
AggregationBits string `json:"aggregation_bits" hex:"true" validate:"required,hexadecimal"`
Signature string `json:"signature" hex:"true" validate:"required,hexadecimal"`
}
type SignedAggregateAttestationAndProof struct {
Message *AggregateAttestationAndProof `json:"message" validate:"required"`
Signature string `json:"signature" validate:"required,hexadecimal"`
}
type AggregateAttestationAndProof struct {
AggregatorIndex string `json:"aggregator_index" validate:"required,number,gte=0"`
Aggregate *Attestation `json:"aggregate" validate:"required"`
SelectionProof string `json:"selection_proof" validate:"required,hexadecimal"`
}
func (s *SignedContributionAndProof) ToConsensus() (*eth.SignedContributionAndProof, error) {
msg, err := s.Message.ToConsensus()
if err != nil {
return nil, NewDecodeError(err, "Message")
}
sig, err := hexutil.Decode(s.Signature)
if err != nil {
return nil, NewDecodeError(err, "Signature")
}
return &eth.SignedContributionAndProof{
Message: msg,
Signature: sig,
}, nil
}
func (c *ContributionAndProof) ToConsensus() (*eth.ContributionAndProof, error) {
contribution, err := c.Contribution.ToConsensus()
if err != nil {
return nil, NewDecodeError(err, "Contribution")
}
aggregatorIndex, err := strconv.ParseUint(c.AggregatorIndex, 10, 64)
if err != nil {
return nil, NewDecodeError(err, "AggregatorIndex")
}
selectionProof, err := hexutil.Decode(c.SelectionProof)
if err != nil {
return nil, NewDecodeError(err, "SelectionProof")
}
return &eth.ContributionAndProof{
AggregatorIndex: primitives.ValidatorIndex(aggregatorIndex),
Contribution: contribution,
SelectionProof: selectionProof,
}, nil
}
func (s *SyncCommitteeContribution) ToConsensus() (*eth.SyncCommitteeContribution, error) {
slot, err := strconv.ParseUint(s.Slot, 10, 64)
if err != nil {
return nil, NewDecodeError(err, "Slot")
}
bbRoot, err := hexutil.Decode(s.BeaconBlockRoot)
if err != nil {
return nil, NewDecodeError(err, "BeaconBlockRoot")
}
subcommitteeIndex, err := strconv.ParseUint(s.SubcommitteeIndex, 10, 64)
if err != nil {
return nil, NewDecodeError(err, "SubcommitteeIndex")
}
aggBits, err := hexutil.Decode(s.AggregationBits)
if err != nil {
return nil, NewDecodeError(err, "AggregationBits")
}
sig, err := hexutil.Decode(s.Signature)
if err != nil {
return nil, NewDecodeError(err, "Signature")
}
return &eth.SyncCommitteeContribution{
Slot: primitives.Slot(slot),
BlockRoot: bbRoot,
SubcommitteeIndex: subcommitteeIndex,
AggregationBits: aggBits,
Signature: sig,
}, nil
}
func (s *SignedAggregateAttestationAndProof) ToConsensus() (*eth.SignedAggregateAttestationAndProof, error) {
msg, err := s.Message.ToConsensus()
if err != nil {
return nil, NewDecodeError(err, "Message")
}
sig, err := hexutil.Decode(s.Signature)
if err != nil {
return nil, NewDecodeError(err, "Signature")
}
return &eth.SignedAggregateAttestationAndProof{
Message: msg,
Signature: sig,
}, nil
}
func (a *AggregateAttestationAndProof) ToConsensus() (*eth.AggregateAttestationAndProof, error) {
aggIndex, err := strconv.ParseUint(a.AggregatorIndex, 10, 64)
if err != nil {
return nil, NewDecodeError(err, "AggregatorIndex")
}
agg, err := a.Aggregate.ToConsensus()
if err != nil {
return nil, NewDecodeError(err, "Aggregate")
}
proof, err := hexutil.Decode(a.SelectionProof)
if err != nil {
return nil, NewDecodeError(err, "SelectionProof")
}
return &eth.AggregateAttestationAndProof{
AggregatorIndex: primitives.ValidatorIndex(aggIndex),
Aggregate: agg,
SelectionProof: proof,
}, nil
}
func (a *Attestation) ToConsensus() (*eth.Attestation, error) {
aggBits, err := hexutil.Decode(a.AggregationBits)
if err != nil {
return nil, NewDecodeError(err, "AggregationBits")
}
data, err := a.Data.ToConsensus()
if err != nil {
return nil, NewDecodeError(err, "Data")
}
sig, err := hexutil.Decode(a.Signature)
if err != nil {
return nil, NewDecodeError(err, "Signature")
}
return &eth.Attestation{
AggregationBits: aggBits,
Data: data,
Signature: sig,
}, nil
}
func (a *AttestationData) ToConsensus() (*eth.AttestationData, error) {
slot, err := strconv.ParseUint(a.Slot, 10, 64)
if err != nil {
return nil, NewDecodeError(err, "Slot")
}
committeeIndex, err := strconv.ParseUint(a.CommitteeIndex, 10, 64)
if err != nil {
return nil, NewDecodeError(err, "CommitteeIndex")
}
bbRoot, err := hexutil.Decode(a.BeaconBlockRoot)
if err != nil {
return nil, NewDecodeError(err, "BeaconBlockRoot")
}
source, err := a.Source.ToConsensus()
if err != nil {
return nil, NewDecodeError(err, "Source")
}
target, err := a.Target.ToConsensus()
if err != nil {
return nil, NewDecodeError(err, "Target")
}
return &eth.AttestationData{
Slot: primitives.Slot(slot),
CommitteeIndex: primitives.CommitteeIndex(committeeIndex),
BeaconBlockRoot: bbRoot,
Source: source,
Target: target,
}, nil
}
func (c *Checkpoint) ToConsensus() (*eth.Checkpoint, error) {
epoch, err := strconv.ParseUint(c.Epoch, 10, 64)
if err != nil {
return nil, NewDecodeError(err, "Epoch")
}
root, err := hexutil.Decode(c.Root)
if err != nil {
return nil, NewDecodeError(err, "Root")
}
return &eth.Checkpoint{
Epoch: primitives.Epoch(epoch),
Root: root,
}, nil
}