From 81ffe019bfae22af07d90615cd203fd056e1433e Mon Sep 17 00:00:00 2001 From: Somnath Banerjee Date: Fri, 26 Jul 2024 15:30:21 +0400 Subject: [PATCH 1/5] Fix JSON unmarshalling of request types --- core/types/consolidation_request.go | 31 +++++++++++++++++++++++++ core/types/deposit_request.go | 35 +++++++++++++++++++++++++++++ core/types/withdrawal_request.go | 30 +++++++++++++++++++++++++ 3 files changed, 96 insertions(+) diff --git a/core/types/consolidation_request.go b/core/types/consolidation_request.go index 04a5f20bac2..137af28a0c1 100644 --- a/core/types/consolidation_request.go +++ b/core/types/consolidation_request.go @@ -18,9 +18,12 @@ package types import ( "bytes" + "encoding/json" + "fmt" "io" libcommon "github.com/ledgerwatch/erigon-lib/common" + "github.com/ledgerwatch/erigon-lib/common/hexutil" rlp2 "github.com/ledgerwatch/erigon-lib/rlp" "github.com/ledgerwatch/erigon/rlp" ) @@ -68,6 +71,34 @@ func (w *ConsolidationRequest) EncodeRLP(b io.Writer) (err error) { return } +func (d *ConsolidationRequest) UnmarshalJSON(input []byte) error { + type auxJson struct { + SourceAddress libcommon.Address `json:"sourceAddress"` + SourcePubKey string `json:"sourcePubkey"` + TargetPubKey string `json:"targetPubkey"` + } + tt := auxJson{} + err := json.Unmarshal(input, &tt) + if err != nil { + return err + } + hexkey1, err := hexutil.Decode(tt.SourcePubKey) + if err != nil { + return err + } + if len(hexkey1) != BLSPubKeyLen { + return fmt.Errorf("Unmarshalled pubkey not equal to BLSPubkeyLen") + } + hexkey2, err := hexutil.Decode(tt.TargetPubKey) + if len(hexkey2) != BLSSigLen { + return fmt.Errorf("Unmarshalled Sig not equal to BLSSiglen") + } + d.SourceAddress = tt.SourceAddress + d.SourcePubKey = [48]byte(hexkey1) + d.TargetPubKey = [48]byte(hexkey2) + return nil +} + func (w *ConsolidationRequest) DecodeRLP(input []byte) error { return rlp.DecodeBytes(input[1:], w) } func (w *ConsolidationRequest) copy() Request { return &ConsolidationRequest{ diff --git a/core/types/deposit_request.go b/core/types/deposit_request.go index af522fdffba..dabdaaf27e6 100644 --- a/core/types/deposit_request.go +++ b/core/types/deposit_request.go @@ -19,10 +19,12 @@ package types import ( "bytes" "encoding/binary" + "encoding/json" "fmt" "io" libcommon "github.com/ledgerwatch/erigon-lib/common" + "github.com/ledgerwatch/erigon-lib/common/hexutil" rlp2 "github.com/ledgerwatch/erigon-lib/rlp" "github.com/ledgerwatch/erigon/accounts/abi" "github.com/ledgerwatch/erigon/rlp" @@ -110,6 +112,39 @@ func (d *DepositRequest) EncodingSize() (encodingSize int) { return } +func (d *DepositRequest) UnmarshalJSON(input []byte) error { + type auxJson struct { + Pubkey string `json:"pubkey"` // public key of validator + WithdrawalCredentials libcommon.Hash `json:"withdrawalCredentials"` // beneficiary of the validator + Amount hexutil.Uint64 `json:"amount"` // deposit size in Gwei + Signature string `json:"signature"` // signature over deposit msg + Index hexutil.Uint64 `json:"index"` // deposit count value + } + tt := auxJson{} + err := json.Unmarshal(input, &tt) + if err != nil { + return err + } + hexkey1, err := hexutil.Decode(tt.Pubkey) + if err != nil { + return err + } + if len(hexkey1) != BLSPubKeyLen { + return fmt.Errorf("Unmarshalled pubkey not equal to BLSPubkeyLen") + } + hexkey2, err := hexutil.Decode(tt.Signature) + if len(hexkey2) != BLSSigLen { + return fmt.Errorf("Unmarshalled Sig not equal to BLSSiglen") + } + + d.Pubkey = [48]byte(hexkey1) + d.Signature = [96]byte(hexkey2) + 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 diff --git a/core/types/withdrawal_request.go b/core/types/withdrawal_request.go index 28d180eea5b..db42159cbdb 100644 --- a/core/types/withdrawal_request.go +++ b/core/types/withdrawal_request.go @@ -18,10 +18,14 @@ package types import ( "bytes" + "encoding/json" + "fmt" + // "fmt" "io" libcommon "github.com/ledgerwatch/erigon-lib/common" + "github.com/ledgerwatch/erigon-lib/common/hexutil" rlp2 "github.com/ledgerwatch/erigon-lib/rlp" "github.com/ledgerwatch/erigon/rlp" ) @@ -81,6 +85,32 @@ func (w *WithdrawalRequest) copy() Request { } } +func (w *WithdrawalRequest) UnmarshalJSON(input []byte) error { + type auxJson struct { + SourceAddress libcommon.Address `json:"sourceAddress"` + ValidatorPubkey string `json:"validatorPubkey"` + Amount hexutil.Uint64 `json:"amount"` + } + tt := auxJson{} + err := json.Unmarshal(input, &tt) + if err != nil { + return err + } + + hexkey, err := hexutil.Decode(tt.ValidatorPubkey) + if err != nil { + return err + } + if len(hexkey) != 48 { + return fmt.Errorf("decode hexkey len after UnmarshalJSON doesn't match BLSKeyLen") + } + + w.ValidatorPubkey = [48]byte(hexkey) + w.Amount = tt.Amount.Uint64() + w.SourceAddress = tt.SourceAddress + return nil +} + type WithdrawalRequests []*WithdrawalRequest // Len returns the length of s. From 08b10531ac26aefc6189f612b089623f8cb507d1 Mon Sep 17 00:00:00 2001 From: Somnath Banerjee Date: Fri, 26 Jul 2024 19:13:23 +0400 Subject: [PATCH 2/5] Fix unmarshal --- core/types/consolidation_request.go | 23 ++++++++++++----- core/types/deposit_request.go | 40 +++++++++++++++++++---------- core/types/withdrawal_request.go | 23 ++++++++++++----- 3 files changed, 61 insertions(+), 25 deletions(-) diff --git a/core/types/consolidation_request.go b/core/types/consolidation_request.go index 137af28a0c1..92c4e676e1d 100644 --- a/core/types/consolidation_request.go +++ b/core/types/consolidation_request.go @@ -24,6 +24,7 @@ import ( libcommon "github.com/ledgerwatch/erigon-lib/common" "github.com/ledgerwatch/erigon-lib/common/hexutil" + "github.com/ledgerwatch/erigon-lib/common/hexutility" rlp2 "github.com/ledgerwatch/erigon-lib/rlp" "github.com/ledgerwatch/erigon/rlp" ) @@ -35,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 } @@ -71,13 +78,17 @@ func (w *ConsolidationRequest) EncodeRLP(b io.Writer) (err error) { return } -func (d *ConsolidationRequest) UnmarshalJSON(input []byte) error { - type auxJson struct { - SourceAddress libcommon.Address `json:"sourceAddress"` - SourcePubKey string `json:"sourcePubkey"` - TargetPubKey string `json:"targetPubkey"` +func (d *ConsolidationRequest) MarshalJSON() ([]byte, error) { + tt := ConsolidationRequestJson{ + SourceAddress: d.SourceAddress, + SourcePubKey: hexutility.Encode(d.SourcePubKey[:]), + TargetPubKey: hexutility.Encode(d.TargetPubKey[:]), } - tt := auxJson{} + return json.Marshal(tt) +} + +func (d *ConsolidationRequest) UnmarshalJSON(input []byte) error { + tt := ConsolidationRequestJson{} err := json.Unmarshal(input, &tt) if err != nil { return err diff --git a/core/types/deposit_request.go b/core/types/deposit_request.go index dabdaaf27e6..3bd2c69e0b9 100644 --- a/core/types/deposit_request.go +++ b/core/types/deposit_request.go @@ -25,6 +25,8 @@ import ( libcommon "github.com/ledgerwatch/erigon-lib/common" "github.com/ledgerwatch/erigon-lib/common/hexutil" + "github.com/ledgerwatch/erigon-lib/common/hexutility" + rlp2 "github.com/ledgerwatch/erigon-lib/rlp" "github.com/ledgerwatch/erigon/accounts/abi" "github.com/ledgerwatch/erigon/rlp" @@ -50,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 } @@ -112,15 +122,19 @@ 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 { - type auxJson struct { - Pubkey string `json:"pubkey"` // public key of validator - WithdrawalCredentials libcommon.Hash `json:"withdrawalCredentials"` // beneficiary of the validator - Amount hexutil.Uint64 `json:"amount"` // deposit size in Gwei - Signature string `json:"signature"` // signature over deposit msg - Index hexutil.Uint64 `json:"index"` // deposit count value - } - tt := auxJson{} + tt := DepositRequestJson{} err := json.Unmarshal(input, &tt) if err != nil { return err diff --git a/core/types/withdrawal_request.go b/core/types/withdrawal_request.go index db42159cbdb..0743dd9a620 100644 --- a/core/types/withdrawal_request.go +++ b/core/types/withdrawal_request.go @@ -26,6 +26,7 @@ import ( libcommon "github.com/ledgerwatch/erigon-lib/common" "github.com/ledgerwatch/erigon-lib/common/hexutil" + "github.com/ledgerwatch/erigon-lib/common/hexutility" rlp2 "github.com/ledgerwatch/erigon-lib/rlp" "github.com/ledgerwatch/erigon/rlp" ) @@ -37,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 } @@ -85,13 +92,17 @@ func (w *WithdrawalRequest) copy() Request { } } -func (w *WithdrawalRequest) UnmarshalJSON(input []byte) error { - type auxJson struct { - SourceAddress libcommon.Address `json:"sourceAddress"` - ValidatorPubkey string `json:"validatorPubkey"` - Amount hexutil.Uint64 `json:"amount"` +func (w *WithdrawalRequest) MarshalJSON() ([]byte, error) { + tt := WithdrawalRequestJson{ + SourceAddress: w.SourceAddress, + ValidatorPubkey: hexutility.Encode(w.ValidatorPubkey[:]), + Amount: hexutil.Uint64(w.Amount), } - tt := auxJson{} + return json.Marshal(tt) +} + +func (w *WithdrawalRequest) UnmarshalJSON(input []byte) error { + tt := WithdrawalRequestJson{} err := json.Unmarshal(input, &tt) if err != nil { return err From dedc9f2b2979c8af1f325bd310dfb69aa0362016 Mon Sep 17 00:00:00 2001 From: Somnath Banerjee Date: Fri, 26 Jul 2024 19:48:21 +0400 Subject: [PATCH 3/5] Fix imports --- core/types/consolidation_request.go | 10 +++++----- core/types/deposit_request.go | 12 ++++++------ core/types/withdrawal_request.go | 10 +++++----- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/core/types/consolidation_request.go b/core/types/consolidation_request.go index 92c4e676e1d..704c70e0af4 100644 --- a/core/types/consolidation_request.go +++ b/core/types/consolidation_request.go @@ -22,11 +22,11 @@ import ( "fmt" "io" - libcommon "github.com/ledgerwatch/erigon-lib/common" - "github.com/ledgerwatch/erigon-lib/common/hexutil" - "github.com/ledgerwatch/erigon-lib/common/hexutility" - rlp2 "github.com/ledgerwatch/erigon-lib/rlp" - "github.com/ledgerwatch/erigon/rlp" + 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" ) // EIP-7251 Consolidation Request see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-7251.md diff --git a/core/types/deposit_request.go b/core/types/deposit_request.go index 3bd2c69e0b9..e9216b999f3 100644 --- a/core/types/deposit_request.go +++ b/core/types/deposit_request.go @@ -23,13 +23,13 @@ import ( "fmt" "io" - libcommon "github.com/ledgerwatch/erigon-lib/common" - "github.com/ledgerwatch/erigon-lib/common/hexutil" - "github.com/ledgerwatch/erigon-lib/common/hexutility" + libcommon "github.com/erigontech/erigon-lib/common" + "github.com/erigontech/erigon-lib/common/hexutil" + "github.com/erigontech/erigon-lib/common/hexutility" - rlp2 "github.com/ledgerwatch/erigon-lib/rlp" - "github.com/ledgerwatch/erigon/accounts/abi" - "github.com/ledgerwatch/erigon/rlp" + rlp2 "github.com/erigontech/erigon-lib/rlp" + "github.com/erigontech/erigon/accounts/abi" + "github.com/erigontech/erigon/rlp" ) const ( diff --git a/core/types/withdrawal_request.go b/core/types/withdrawal_request.go index 0743dd9a620..1bdc7c65fb8 100644 --- a/core/types/withdrawal_request.go +++ b/core/types/withdrawal_request.go @@ -24,11 +24,11 @@ import ( // "fmt" "io" - libcommon "github.com/ledgerwatch/erigon-lib/common" - "github.com/ledgerwatch/erigon-lib/common/hexutil" - "github.com/ledgerwatch/erigon-lib/common/hexutility" - rlp2 "github.com/ledgerwatch/erigon-lib/rlp" - "github.com/ledgerwatch/erigon/rlp" + 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" ) // EIP-7002 Withdrawal Request see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-7002.md From 6ee96d2a6e2e471ae0a70d6b16dd6cf1cf180a1b Mon Sep 17 00:00:00 2001 From: Somnath Banerjee Date: Fri, 26 Jul 2024 19:49:02 +0400 Subject: [PATCH 4/5] fmt --- core/types/consolidation_request.go | 4 ++-- core/types/deposit_request.go | 18 +++++++++--------- core/types/withdrawal_request.go | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/core/types/consolidation_request.go b/core/types/consolidation_request.go index 704c70e0af4..5d5f1e33df0 100644 --- a/core/types/consolidation_request.go +++ b/core/types/consolidation_request.go @@ -81,8 +81,8 @@ func (w *ConsolidationRequest) EncodeRLP(b io.Writer) (err error) { func (d *ConsolidationRequest) MarshalJSON() ([]byte, error) { tt := ConsolidationRequestJson{ SourceAddress: d.SourceAddress, - SourcePubKey: hexutility.Encode(d.SourcePubKey[:]), - TargetPubKey: hexutility.Encode(d.TargetPubKey[:]), + SourcePubKey: hexutility.Encode(d.SourcePubKey[:]), + TargetPubKey: hexutility.Encode(d.TargetPubKey[:]), } return json.Marshal(tt) } diff --git a/core/types/deposit_request.go b/core/types/deposit_request.go index e9216b999f3..00943a8e984 100644 --- a/core/types/deposit_request.go +++ b/core/types/deposit_request.go @@ -52,11 +52,11 @@ var ( ) type DepositRequest struct { - 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 + 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 { @@ -124,11 +124,11 @@ func (d *DepositRequest) EncodingSize() (encodingSize int) { func (d *DepositRequest) MarshalJSON() ([]byte, error) { tt := DepositRequestJson{ - Pubkey: hexutility.Encode(d.Pubkey[:]), + Pubkey: hexutility.Encode(d.Pubkey[:]), WithdrawalCredentials: d.WithdrawalCredentials, - Amount: hexutil.Uint64(d.Amount), - Signature: hexutility.Encode(d.Signature[:]), - Index: hexutil.Uint64(d.Index), + Amount: hexutil.Uint64(d.Amount), + Signature: hexutility.Encode(d.Signature[:]), + Index: hexutil.Uint64(d.Index), } return json.Marshal(tt) } diff --git a/core/types/withdrawal_request.go b/core/types/withdrawal_request.go index 1bdc7c65fb8..530c5932a35 100644 --- a/core/types/withdrawal_request.go +++ b/core/types/withdrawal_request.go @@ -94,9 +94,9 @@ func (w *WithdrawalRequest) copy() Request { func (w *WithdrawalRequest) MarshalJSON() ([]byte, error) { tt := WithdrawalRequestJson{ - SourceAddress: w.SourceAddress, + SourceAddress: w.SourceAddress, ValidatorPubkey: hexutility.Encode(w.ValidatorPubkey[:]), - Amount: hexutil.Uint64(w.Amount), + Amount: hexutil.Uint64(w.Amount), } return json.Marshal(tt) } From e1cc41245d5d90d79f1fd08af48eae1576dfdc93 Mon Sep 17 00:00:00 2001 From: Somnath Banerjee Date: Fri, 26 Jul 2024 20:23:06 +0400 Subject: [PATCH 5/5] Fix err and comments --- core/types/consolidation_request.go | 17 ++++++++++------- core/types/deposit_request.go | 19 +++++++++++-------- core/types/withdrawal_request.go | 8 ++++---- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/core/types/consolidation_request.go b/core/types/consolidation_request.go index 5d5f1e33df0..caad3b2c982 100644 --- a/core/types/consolidation_request.go +++ b/core/types/consolidation_request.go @@ -93,20 +93,23 @@ func (d *ConsolidationRequest) UnmarshalJSON(input []byte) error { if err != nil { return err } - hexkey1, err := hexutil.Decode(tt.SourcePubKey) + sourceKey, err := hexutil.Decode(tt.SourcePubKey) if err != nil { return err } - if len(hexkey1) != BLSPubKeyLen { + if len(sourceKey) != BLSPubKeyLen { return fmt.Errorf("Unmarshalled pubkey not equal to BLSPubkeyLen") } - hexkey2, err := hexutil.Decode(tt.TargetPubKey) - if len(hexkey2) != BLSSigLen { - return fmt.Errorf("Unmarshalled Sig not equal to BLSSiglen") + 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(hexkey1) - d.TargetPubKey = [48]byte(hexkey2) + d.SourcePubKey = [48]byte(sourceKey) + d.TargetPubKey = [48]byte(targetKey) return nil } diff --git a/core/types/deposit_request.go b/core/types/deposit_request.go index 00943a8e984..0abb90e7521 100644 --- a/core/types/deposit_request.go +++ b/core/types/deposit_request.go @@ -139,20 +139,23 @@ func (d *DepositRequest) UnmarshalJSON(input []byte) error { if err != nil { return err } - hexkey1, err := hexutil.Decode(tt.Pubkey) + pubkey, err := hexutil.Decode(tt.Pubkey) if err != nil { return err } - if len(hexkey1) != BLSPubKeyLen { - return fmt.Errorf("Unmarshalled pubkey not equal to BLSPubkeyLen") + if len(pubkey) != BLSPubKeyLen { + return fmt.Errorf("Unmarshalled pubkey len not equal to BLSPubkeyLen") } - hexkey2, err := hexutil.Decode(tt.Signature) - if len(hexkey2) != BLSSigLen { - return fmt.Errorf("Unmarshalled Sig not equal to BLSSiglen") + 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(hexkey1) - d.Signature = [96]byte(hexkey2) + d.Pubkey = [48]byte(pubkey) + d.Signature = [96]byte(sig) d.WithdrawalCredentials = tt.WithdrawalCredentials d.Amount = tt.Amount.Uint64() d.Index = tt.Index.Uint64() diff --git a/core/types/withdrawal_request.go b/core/types/withdrawal_request.go index 530c5932a35..bf36eee602d 100644 --- a/core/types/withdrawal_request.go +++ b/core/types/withdrawal_request.go @@ -108,15 +108,15 @@ func (w *WithdrawalRequest) UnmarshalJSON(input []byte) error { return err } - hexkey, err := hexutil.Decode(tt.ValidatorPubkey) + validatorKey, err := hexutil.Decode(tt.ValidatorPubkey) if err != nil { return err } - if len(hexkey) != 48 { - return fmt.Errorf("decode hexkey len after UnmarshalJSON doesn't match BLSKeyLen") + if len(validatorKey) != 48 { + return fmt.Errorf("decoded validatorKey len after UnmarshalJSON doesn't match BLSKeyLen") } - w.ValidatorPubkey = [48]byte(hexkey) + w.ValidatorPubkey = [48]byte(validatorKey) w.Amount = tt.Amount.Uint64() w.SourceAddress = tt.SourceAddress return nil