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

core/types: Fix JSON (un)marshalling of request types #11346

Merged
merged 6 commits into from
Jul 29, 2024
Merged
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
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")
}
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")
}
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")
}
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
Loading