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

Flatten EIP-7251 Consolidation Requests encoding #12457

Merged
merged 1 commit into from
Oct 24, 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
17 changes: 5 additions & 12 deletions consensus/misc/eip7251.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,24 @@ package misc
import (
"github.com/ledgerwatch/log/v3"

"github.com/ledgerwatch/erigon-lib/common"

"github.com/ledgerwatch/erigon/consensus"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/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 @@ -6,105 +6,72 @@ import (
"errors"
"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"
)

// 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 @@ -88,9 +88,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 @@ -399,9 +397,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 @@ -15,7 +15,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 @@ -5,7 +5,7 @@ go 1.22.0
require (
github.com/erigontech/mdbx-go v0.27.24
github.com/ledgerwatch/erigon-snapshot v1.3.1-0.20240805114253-42da880260bb
github.com/ledgerwatch/interfaces v0.0.0-20241024115450-4e9be51e57e7
github.com/ledgerwatch/interfaces v0.0.0-20241024121018-582714adcf8a
github.com/ledgerwatch/log/v3 v3.9.0
github.com/ledgerwatch/secp256k1 v1.0.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 @@ -270,8 +270,8 @@ github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7
github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8=
github.com/ledgerwatch/erigon-snapshot v1.3.1-0.20240805114253-42da880260bb h1:bsoVxjnQGxhOODRmkdrbkRTB9+sIduguoNMSZPRRoTI=
github.com/ledgerwatch/erigon-snapshot v1.3.1-0.20240805114253-42da880260bb/go.mod h1:3AuPxZc85jkehh/HA9h8gabv5MSi3kb/ddtzBsTVJFo=
github.com/ledgerwatch/interfaces v0.0.0-20241024115450-4e9be51e57e7 h1:q6nNkackUuI9xbwcDdhh0ft2QJsoIVNRZe9KR08tkxU=
github.com/ledgerwatch/interfaces v0.0.0-20241024115450-4e9be51e57e7/go.mod h1:ugQv1QllJzBny3cKZKxUrSnykkjkBgm27eQM6dnGAcc=
github.com/ledgerwatch/interfaces v0.0.0-20241024121018-582714adcf8a h1:2TsvVKrqykfCypZEqpG1ZphBu8wvmRgQsKGS+JQXhdg=
github.com/ledgerwatch/interfaces v0.0.0-20241024121018-582714adcf8a/go.mod h1:ugQv1QllJzBny3cKZKxUrSnykkjkBgm27eQM6dnGAcc=
github.com/ledgerwatch/log/v3 v3.9.0 h1:iDwrXe0PVwBC68Dd94YSsHbMgQ3ufsgjzXtFNFVZFRk=
github.com/ledgerwatch/log/v3 v3.9.0/go.mod h1:EiAY6upmI/6LkNhOVxb4eVsmsP11HZCnZ3PlJMjYiqE=
github.com/ledgerwatch/secp256k1 v1.0.0 h1:Usvz87YoTG0uePIV8woOof5cQnLXGYa162rFf3YnwaQ=
Expand Down
Loading
Loading