Skip to content

Commit

Permalink
core/types: Fix JSON (un)marshalling of request types (#11346)
Browse files Browse the repository at this point in the history
This PR resolves issues around `encoding/json` not being able to
automatically parse/un-parse request objects defined as it is, as
observed in pectra-devnet-1 kurtosis tests
  • Loading branch information
somnathb1 authored Jul 29, 2024
1 parent 872c902 commit 9337209
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 5 deletions.
45 changes: 45 additions & 0 deletions core/types/consolidation_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ package types

import (
"bytes"
"encoding/json"
"fmt"
"io"

libcommon "github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon-lib/common/hexutil"
"github.com/erigontech/erigon-lib/common/hexutility"
rlp2 "github.com/erigontech/erigon-lib/rlp"
"github.com/erigontech/erigon/rlp"
)
Expand All @@ -32,6 +36,12 @@ type ConsolidationRequest struct {
TargetPubKey [BLSPubKeyLen]byte
}

type ConsolidationRequestJson struct {
SourceAddress libcommon.Address `json:"sourceAddress"`
SourcePubKey string `json:"sourcePubkey"`
TargetPubKey string `json:"targetPubkey"`
}

func (w *ConsolidationRequest) RequestType() byte {
return ConsolidationRequestType
}
Expand Down Expand Up @@ -68,6 +78,41 @@ func (w *ConsolidationRequest) EncodeRLP(b io.Writer) (err error) {
return
}

func (d *ConsolidationRequest) MarshalJSON() ([]byte, error) {
tt := ConsolidationRequestJson{
SourceAddress: d.SourceAddress,
SourcePubKey: hexutility.Encode(d.SourcePubKey[:]),
TargetPubKey: hexutility.Encode(d.TargetPubKey[:]),
}
return json.Marshal(tt)
}

func (d *ConsolidationRequest) UnmarshalJSON(input []byte) error {
tt := ConsolidationRequestJson{}
err := json.Unmarshal(input, &tt)
if err != nil {
return err
}
sourceKey, err := hexutil.Decode(tt.SourcePubKey)
if err != nil {
return err
}
if len(sourceKey) != BLSPubKeyLen {
return fmt.Errorf("Unmarshalled pubkey not equal to BLSPubkeyLen")

Check failure on line 101 in core/types/consolidation_request.go

View workflow job for this annotation

GitHub Actions / tests (ubuntu-22.04)

fmt.Errorf can be replaced with errors.New (perfsprint)
}
targetKey, err := hexutil.Decode(tt.TargetPubKey)
if err != nil {
return err
}
if len(targetKey) != BLSSigLen {
return fmt.Errorf("Unmarshalled TargetPubKey len not equal to BLSSiglen")

Check failure on line 108 in core/types/consolidation_request.go

View workflow job for this annotation

GitHub Actions / tests (ubuntu-22.04)

fmt.Errorf can be replaced with errors.New (perfsprint)
}
d.SourceAddress = tt.SourceAddress
d.SourcePubKey = [48]byte(sourceKey)
d.TargetPubKey = [48]byte(targetKey)
return nil
}

func (w *ConsolidationRequest) DecodeRLP(input []byte) error { return rlp.DecodeBytes(input[1:], w) }
func (w *ConsolidationRequest) copy() Request {
return &ConsolidationRequest{
Expand Down
62 changes: 57 additions & 5 deletions core/types/deposit_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ package types
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"io"

libcommon "github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon-lib/common/hexutil"
"github.com/erigontech/erigon-lib/common/hexutility"

rlp2 "github.com/erigontech/erigon-lib/rlp"
"github.com/erigontech/erigon/accounts/abi"
"github.com/erigontech/erigon/rlp"
Expand All @@ -48,11 +52,19 @@ var (
)

type DepositRequest struct {
Pubkey [BLSPubKeyLen]byte `json:"pubkey"` // public key of validator
WithdrawalCredentials libcommon.Hash `json:"withdrawalCredentials"` // beneficiary of the validator
Amount uint64 `json:"amount"` // deposit size in Gwei
Signature [BLSSigLen]byte `json:"signature"` // signature over deposit msg
Index uint64 `json:"index"` // deposit count value
Pubkey [BLSPubKeyLen]byte // public key of validator
WithdrawalCredentials libcommon.Hash // beneficiary of the validator
Amount uint64 // deposit size in Gwei
Signature [BLSSigLen]byte // signature over deposit msg
Index uint64 // deposit count value
}

type DepositRequestJson struct {
Pubkey string `json:"pubkey"`
WithdrawalCredentials libcommon.Hash `json:"withdrawalCredentials"`
Amount hexutil.Uint64 `json:"amount"`
Signature string `json:"signature"`
Index hexutil.Uint64 `json:"index"`
}

func (d *DepositRequest) RequestType() byte { return DepositRequestType }
Expand Down Expand Up @@ -110,6 +122,46 @@ func (d *DepositRequest) EncodingSize() (encodingSize int) {
return
}

func (d *DepositRequest) MarshalJSON() ([]byte, error) {
tt := DepositRequestJson{
Pubkey: hexutility.Encode(d.Pubkey[:]),
WithdrawalCredentials: d.WithdrawalCredentials,
Amount: hexutil.Uint64(d.Amount),
Signature: hexutility.Encode(d.Signature[:]),
Index: hexutil.Uint64(d.Index),
}
return json.Marshal(tt)
}

func (d *DepositRequest) UnmarshalJSON(input []byte) error {
tt := DepositRequestJson{}
err := json.Unmarshal(input, &tt)
if err != nil {
return err
}
pubkey, err := hexutil.Decode(tt.Pubkey)
if err != nil {
return err
}
if len(pubkey) != BLSPubKeyLen {
return fmt.Errorf("Unmarshalled pubkey len not equal to BLSPubkeyLen")

Check failure on line 147 in core/types/deposit_request.go

View workflow job for this annotation

GitHub Actions / tests (ubuntu-22.04)

fmt.Errorf can be replaced with errors.New (perfsprint)
}
sig, err := hexutil.Decode(tt.Signature)
if err != nil {
return err
}
if len(sig) != BLSSigLen {
return fmt.Errorf("Unmarshalled Signature len not equal to BLSSiglen")
}

d.Pubkey = [48]byte(pubkey)
d.Signature = [96]byte(sig)
d.WithdrawalCredentials = tt.WithdrawalCredentials
d.Amount = tt.Amount.Uint64()
d.Index = tt.Index.Uint64()
return nil
}

// field type overrides for abi upacking
type depositUnpacking struct {
Pubkey []byte
Expand Down
41 changes: 41 additions & 0 deletions core/types/withdrawal_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ package types

import (
"bytes"
"encoding/json"
"fmt"

// "fmt"
"io"

libcommon "github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon-lib/common/hexutil"
"github.com/erigontech/erigon-lib/common/hexutility"
rlp2 "github.com/erigontech/erigon-lib/rlp"
"github.com/erigontech/erigon/rlp"
)
Expand All @@ -33,6 +38,12 @@ type WithdrawalRequest struct {
Amount uint64
}

type WithdrawalRequestJson struct {
SourceAddress libcommon.Address `json:"sourceAddress"`
ValidatorPubkey string `json:"validatorPubkey"`
Amount hexutil.Uint64 `json:"amount"`
}

func (w *WithdrawalRequest) RequestType() byte {
return WithdrawalRequestType
}
Expand Down Expand Up @@ -81,6 +92,36 @@ func (w *WithdrawalRequest) copy() Request {
}
}

func (w *WithdrawalRequest) MarshalJSON() ([]byte, error) {
tt := WithdrawalRequestJson{
SourceAddress: w.SourceAddress,
ValidatorPubkey: hexutility.Encode(w.ValidatorPubkey[:]),
Amount: hexutil.Uint64(w.Amount),
}
return json.Marshal(tt)
}

func (w *WithdrawalRequest) UnmarshalJSON(input []byte) error {
tt := WithdrawalRequestJson{}
err := json.Unmarshal(input, &tt)
if err != nil {
return err
}

validatorKey, err := hexutil.Decode(tt.ValidatorPubkey)
if err != nil {
return err
}
if len(validatorKey) != 48 {
return fmt.Errorf("decoded validatorKey len after UnmarshalJSON doesn't match BLSKeyLen")
}

w.ValidatorPubkey = [48]byte(validatorKey)
w.Amount = tt.Amount.Uint64()
w.SourceAddress = tt.SourceAddress
return nil
}

type WithdrawalRequests []*WithdrawalRequest

// Len returns the length of s.
Expand Down

0 comments on commit 9337209

Please sign in to comment.