Skip to content

Commit

Permalink
Flatten EIP-7251 Consolidation Requests encoding (#12167)
Browse files Browse the repository at this point in the history
In summary, does away with a lot of earlier complexities related to
parsing and encoding consolidation requests. These are now just byte
strings, essentially.

Refer to the following 
- ethereum/EIPs#8857
-
https://github.com/ethereum/EIPs/pull/8934/files#diff-01ca405a121a1fc77e03df916c4f331b030c2467a4eb66545a34e98a6e476fd6R541
(Address update)

Needs interface update -
erigontech/interfaces#237

(Tasks board - #12106)
  • Loading branch information
somnathb1 authored Oct 11, 2024
1 parent 8959ee8 commit 6c82d93
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 220 deletions.
16 changes: 5 additions & 11 deletions consensus/misc/eip7251.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,25 @@
package misc

import (
"github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon-lib/log/v3"
"github.com/erigontech/erigon/consensus"
"github.com/erigontech/erigon/core/types"
"github.com/erigontech/erigon/params"
)

const ConsolidationRequestDataLen = 116

func DequeueConsolidationRequests7251(syscall consensus.SystemCall) types.Requests {
res, err := syscall(params.ConsolidationRequestAddress, nil)
if err != nil {
log.Warn("Err with syscall to ConsolidationRequestAddress", "err", err)
return nil
}
// Parse out the consolidations - using the bytes array returned
// Just append the contract outputs as the encoded request data
var reqs types.Requests
lenPerReq := 20 + 48 + 48 // addr + sourcePubkey + targetPubkey
for i := 0; i <= len(res)-lenPerReq; i += lenPerReq {
var sourcePubKey [48]byte
copy(sourcePubKey[:], res[i+20:i+68])
var targetPubKey [48]byte
copy(targetPubKey[:], res[i+68:i+116])
for i := 0; i <= len(res)-ConsolidationRequestDataLen; i += ConsolidationRequestDataLen {
wr := &types.ConsolidationRequest{
SourceAddress: common.BytesToAddress(res[i : i+20]),
SourcePubKey: sourcePubKey,
TargetPubKey: targetPubKey,
RequestData: [ConsolidationRequestDataLen]byte(res[i : i+ConsolidationRequestDataLen]),
}
reqs = append(reqs, wr)
}
Expand Down
85 changes: 26 additions & 59 deletions core/types/consolidation_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,105 +22,72 @@ import (
"errors"
"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"
)

// EIP-7251 Consolidation Request see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-7251.md
type ConsolidationRequest struct {
SourceAddress libcommon.Address
SourcePubKey [BLSPubKeyLen]byte
TargetPubKey [BLSPubKeyLen]byte
RequestData [ConsolidationRequestDataLen]byte
}

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

func (w *ConsolidationRequest) RequestType() byte {
func (c *ConsolidationRequest) RequestType() byte {
return ConsolidationRequestType
}

func (w *ConsolidationRequest) EncodingSize() (encodingSize int) {
encodingSize += 119 // 1 + 20 + 1 + 48 + 1 + 48 (0x80 + addrSize, 0x80 + BLSPubKeyLen, 0x80 + BLSPubKeyLen)
encodingSize += rlp2.ListPrefixLen(encodingSize)
encodingSize += 1 // RequestType
return
func (c *ConsolidationRequest) EncodingSize() (encodingSize int) {
return ConsolidationRequestDataLen + 1 // RequestType
}
func (w *ConsolidationRequest) EncodeRLP(b io.Writer) (err error) {
var buf bytes.Buffer
bb := make([]byte, 10)
if err = rlp.Encode(&buf, w.SourceAddress); err != nil {
return err
}
if err = rlp.Encode(&buf, w.SourcePubKey); err != nil {
return err
}
if err = rlp.Encode(&buf, w.TargetPubKey); err != nil {
return err
}
l := rlp2.EncodeListPrefix(buf.Len(), bb)
func (c *ConsolidationRequest) EncodeRLP(b io.Writer) (err error) {

if _, err = b.Write([]byte{ConsolidationRequestType}); err != nil {
return err
}
if _, err = b.Write(bb[0:l]); err != nil {
return err
}
if _, err = b.Write(buf.Bytes()); err != nil {
if _, err = b.Write(c.RequestData[:]); err != nil {
return err
}
return
}

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

func (d *ConsolidationRequest) UnmarshalJSON(input []byte) error {
func (c *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(tt.RequestData) != ConsolidationRequestDataLen {
return errors.New("Cannot unmarshal consolidation request data, length mismatch")
}
if len(sourceKey) != BLSPubKeyLen {
return errors.New("ConsolidationRequest SourcePubKey not equal to BLSPubkeyLen after UnmarshalJSON")
c.RequestData = [ConsolidationRequestDataLen]byte(hexutility.MustDecodeString(tt.RequestData))
return nil
}

func (c *ConsolidationRequest) copy() Request {
return &ConsolidationRequest{
RequestData: [ConsolidationRequestDataLen]byte(bytes.Clone(c.RequestData[:])),
}
targetKey, err := hexutil.Decode(tt.TargetPubKey)
if err != nil {
return err
}
if len(targetKey) != BLSPubKeyLen {
return errors.New("ConsolidationRequest TargetPubKey len not equal to BLSSiglen after UnmarshalJSON")
}

func (c *ConsolidationRequest) DecodeRLP(input []byte) error {
if len(input) != ConsolidationRequestDataLen+1 {
return errors.New("Incorrect size for decoding ConsolidationRequest RLP")
}
d.SourceAddress = tt.SourceAddress
d.SourcePubKey = [BLSPubKeyLen]byte(sourceKey)
d.TargetPubKey = [BLSPubKeyLen]byte(targetKey)
c.RequestData = [ConsolidationRequestDataLen]byte(input[1:])
return nil
}

func (w *ConsolidationRequest) DecodeRLP(input []byte) error { return rlp.DecodeBytes(input[1:], w) }
func (w *ConsolidationRequest) copy() Request {
return &ConsolidationRequest{
SourceAddress: w.SourceAddress,
SourcePubKey: w.SourcePubKey,
TargetPubKey: w.TargetPubKey,
}
func (c *ConsolidationRequest) Encode() []byte {
return append([]byte{ConsolidationRequestType}, c.RequestData[:]...)
}

type ConsolidationRequests []*ConsolidationRequest
Expand Down
8 changes: 2 additions & 6 deletions core/types/encdec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ func (tr *TRand) RandDepositRequest() *DepositRequest {

func (tr *TRand) RandConsolidationRequest() *ConsolidationRequest {
return &ConsolidationRequest{
SourceAddress: [20]byte(tr.RandBytes(20)),
SourcePubKey: [48]byte(tr.RandBytes(48)),
TargetPubKey: [48]byte(tr.RandBytes(48)),
RequestData: [ConsolidationRequestDataLen]byte(tr.RandBytes(ConsolidationRequestDataLen)),
}
}

Expand Down Expand Up @@ -416,9 +414,7 @@ func compareWithdrawalRequests(t *testing.T, a, b *WithdrawalRequest) {
}

func compareConsolidationRequests(t *testing.T, a, b *ConsolidationRequest) {
check(t, "ConsolidationRequest.SourceAddress", a.SourceAddress, b.SourceAddress)
check(t, "ConsolidationRequest.SourcePubKey", a.SourcePubKey, b.SourcePubKey)
check(t, "ConsolidationRequest.TargetPubKey", a.TargetPubKey, b.TargetPubKey)
check(t, "ConsolidationRequest.RequestData", a.RequestData, b.RequestData)
}

func checkRequests(t *testing.T, a, b Request) {
Expand Down
3 changes: 2 additions & 1 deletion core/types/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import (
const WithdrawalRequestType byte = 0x01
const DepositRequestType byte = 0x00
const ConsolidationRequestType byte = 0x02
const WithdrawalRequestDataLen = 76 // addr + pubkey + amt
const ConsolidationRequestDataLen = 116 // addr + sourcePubkey + targetPubkey
const WithdrawalRequestDataLen = 76 // addr + pubkey + amt

type Request interface {
EncodeRLP(io.Writer) error
Expand Down
2 changes: 1 addition & 1 deletion erigon-lib/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.22.0

require (
github.com/erigontech/erigon-snapshot v1.3.1-0.20240814160410-2ce37904b978
github.com/erigontech/interfaces v0.0.0-20240930141537-21d2f6889ec4
github.com/erigontech/interfaces v0.0.0-20241011102608-29c1d07f457d
github.com/erigontech/mdbx-go v0.38.4
github.com/erigontech/secp256k1 v1.1.0
github.com/rs/dnscache v0.0.0-20211102005908-e0241e321417
Expand Down
4 changes: 2 additions & 2 deletions erigon-lib/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/erigontech/erigon-snapshot v1.3.1-0.20240814160410-2ce37904b978 h1:7ECOf7Us3+/706WGZXIX84qQc6zmxQby8fGbFLiqFlU=
github.com/erigontech/erigon-snapshot v1.3.1-0.20240814160410-2ce37904b978/go.mod h1:ooHlCl+eEYzebiPu+FP6Q6SpPUeMADn8Jxabv3IKb9M=
github.com/erigontech/interfaces v0.0.0-20240930141537-21d2f6889ec4 h1:MFWfaE+BwmFXfNfwwuyeNImS16HIsDGB0q3u7nCpIhY=
github.com/erigontech/interfaces v0.0.0-20240930141537-21d2f6889ec4/go.mod h1:N7OUkhkcagp9+7yb4ycHsG2VWCOmuJ1ONBecJshxtLE=
github.com/erigontech/interfaces v0.0.0-20241011102608-29c1d07f457d h1:T0xEfGinQBrv1aV2WdIQCBoCPOn2CKKH5hi35o/V0m8=
github.com/erigontech/interfaces v0.0.0-20241011102608-29c1d07f457d/go.mod h1:N7OUkhkcagp9+7yb4ycHsG2VWCOmuJ1ONBecJshxtLE=
github.com/erigontech/mdbx-go v0.38.4 h1:S9T7mTe9KPcFe4dOoOtVdI6gPzht9y7wMnYfUBgrQLo=
github.com/erigontech/mdbx-go v0.38.4/go.mod h1:IcOLQDPw3VM/asP6T5JVPPN4FHHgJtY16XfYjzWKVNI=
github.com/erigontech/secp256k1 v1.1.0 h1:mO3YJMUSoASE15Ya//SoHiisptUhdXExuMUN1M0X9qY=
Expand Down
Loading

0 comments on commit 6c82d93

Please sign in to comment.