Skip to content

Commit

Permalink
Simplify EIP-6110 Deposit requests (#12476)
Browse files Browse the repository at this point in the history
Cherry pick #12388 into `release/2.61`

Co-authored-by: Somnath <snb895@outlook.com>
  • Loading branch information
yperbasis and somnathb1 authored Oct 25, 2024
1 parent 6d92c11 commit 3ba3e0a
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 237 deletions.
5 changes: 2 additions & 3 deletions consensus/merge/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,11 @@ func (s *Merge) Finalize(config *chain.Config, header *types.Header, state *stat
for _, rec := range receipts {
allLogs = append(allLogs, rec.Logs...)
}
depositReqs, err := types.ParseDepositLogs(allLogs, config.DepositContract)
depositReqs, err := misc.ParseDepositLogs(allLogs, config.DepositContract)
if err != nil {
return nil, nil, nil, fmt.Errorf("error: could not parse requests logs: %v", err)
}
rs = append(rs, types.FlatRequest{Type: types.DepositRequestType, RequestData: depositReqs.Encode()})
rs = append(rs, *depositReqs)
withdrawalReq := misc.DequeueWithdrawalRequests7002(syscall)
rs = append(rs, *withdrawalReq)
consolidations := misc.DequeueConsolidationRequests7251(syscall)
Expand All @@ -182,7 +182,6 @@ func (s *Merge) Finalize(config *chain.Config, header *types.Header, state *stat
return nil, nil, nil, fmt.Errorf("error: invalid requests root hash in header, expected: %v, got :%v", header.RequestsHash, rh)
}
}

}

return txs, receipts, rs, nil
Expand Down
85 changes: 85 additions & 0 deletions consensus/misc/eip6110.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2024 The Erigon Authors
// This file is part of Erigon.
//
// Erigon is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Erigon is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Erigon. If not, see <http://www.gnu.org/licenses/>.

package misc

import (
"fmt"

libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon/accounts/abi"
"github.com/ledgerwatch/erigon/core/types"
)

const (
BLSPubKeyLen = 48
WithdrawalCredentialsLen = 32 // withdrawalCredentials size
BLSSigLen = 96 // signature size
)

var (
// DepositABI is an ABI instance of beacon chain deposit events.
DepositABI = abi.ABI{Events: map[string]abi.Event{"DepositEvent": depositEvent}}
bytesT, _ = abi.NewType("bytes", "", nil)
depositEvent = abi.NewEvent("DepositEvent", "DepositEvent", false, abi.Arguments{
{Name: "pubkey", Type: bytesT, Indexed: false},
{Name: "withdrawal_credentials", Type: bytesT, Indexed: false},
{Name: "amount", Type: bytesT, Indexed: false},
{Name: "signature", Type: bytesT, Indexed: false},
{Name: "index", Type: bytesT, Indexed: false}},
)
)

// field type overrides for abi upacking
type depositUnpacking struct {
Pubkey []byte
WithdrawalCredentials []byte
Amount []byte
Signature []byte
Index []byte
}

// unpackDepositLog unpacks a serialized DepositEvent.
func unpackDepositLog(data []byte) ([]byte, error) {
var du depositUnpacking
if err := DepositABI.UnpackIntoInterface(&du, "DepositEvent", data); err != nil {
return nil, err
}
reqData := make([]byte, 0, types.DepositRequestDataLen)
reqData = append(reqData, du.Pubkey...)
reqData = append(reqData, du.WithdrawalCredentials...)
reqData = append(reqData, du.Amount...)
reqData = append(reqData, du.Signature...)
reqData = append(reqData, du.Index...)

return reqData, nil
}

// ParseDepositLogs extracts the EIP-6110 deposit values from logs emitted by
// BeaconDepositContract and returns a FlatRequest object ptr
func ParseDepositLogs(logs []*types.Log, depositContractAddress libcommon.Address) (*types.FlatRequest, error) {
reqData := make([]byte, 0, len(logs)*types.DepositRequestDataLen)
for _, log := range logs {
if log.Address == depositContractAddress {
d, err := unpackDepositLog(log.Data)
if err != nil {
return nil, fmt.Errorf("unable to parse deposit data: %v", err)
}
reqData = append(reqData, d...)
}
}
return &types.FlatRequest{Type: types.DepositRequestType, RequestData: reqData}, nil
}
178 changes: 0 additions & 178 deletions core/types/deposit_request.go

This file was deleted.

2 changes: 2 additions & 0 deletions core/types/eip7685_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const ConsolidationRequestDataLen = 116 // addr + sourcePubkey + targetPubkey

var KnownRequestTypes = []byte{DepositRequestType, WithdrawalRequestType, ConsolidationRequestType}

// FlatRequest carries serialized (flat) request data from any known Request type
// The RequestData slice can contain collated data for more than one request of the same type
type FlatRequest struct {
Type byte
RequestData []byte
Expand Down
56 changes: 0 additions & 56 deletions core/types/encdec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,6 @@ func (tr *TRand) RandWithdrawal() *Withdrawal {
}
}

func (tr *TRand) RandDepositRequest() *DepositRequest {
return &DepositRequest{
Pubkey: [48]byte(tr.RandBytes(48)),
WithdrawalCredentials: tr.RandHash(),
Amount: *tr.RandUint64(),
Signature: [96]byte(tr.RandBytes(96)),
Index: *tr.RandUint64(),
}
}

func (tr *TRand) RandRequest() *DepositRequest {
switch tr.rnd.Intn(3) {
case 0:
return tr.RandDepositRequest()
default:
return nil // unreachable code
}
}

func (tr *TRand) RandHeader() *Header {
wHash := tr.RandHash()
pHash := tr.RandHash()
Expand Down Expand Up @@ -256,14 +237,6 @@ func (tr *TRand) RandWithdrawals(size int) []*Withdrawal {
return withdrawals
}

func (tr *TRand) RandRequests(size int) []FlatRequest {
requests := make([]FlatRequest, size)
for i := 0; i < size; i++ {
requests[i] = FlatRequest{RequestData: tr.RandRequest().Encode(), Type: DepositRequestType}
}
return requests
}

func (tr *TRand) RandRawBody() *RawBody {
return &RawBody{
Transactions: tr.RandRawTransactions(tr.RandIntInRange(1, 6)),
Expand Down Expand Up @@ -365,23 +338,6 @@ func compareTransactions(t *testing.T, a, b Transaction) {
check(t, "Tx.S", s1, s2)
}

func compareDeposits(t *testing.T, a, b *DepositRequest) {
check(t, "Deposit.Pubkey", a.Pubkey, b.Pubkey)
check(t, "Deposit.WithdrawalCredentials", a.WithdrawalCredentials, b.WithdrawalCredentials)
check(t, "Deposit.Amount", a.Amount, b.Amount)
check(t, "Deposit.Signature", a.Signature, b.Signature)
check(t, "Deposit.Index", a.Index, b.Index)
}

func checkRequests(t *testing.T, a, b DepositRequest) {
if a.RequestType() != b.RequestType() {
t.Errorf("request type mismatch: request-a: %v, request-b: %v", a.RequestType(), b.RequestType())
}

compareDeposits(t, &a, &b)

}

func compareHeaders(t *testing.T, a, b []*Header) error {
auLen, buLen := len(a), len(b)
if auLen != buLen {
Expand All @@ -406,18 +362,6 @@ func compareWithdrawals(t *testing.T, a, b []*Withdrawal) error {
return nil
}

func compareRequests(t *testing.T, a, b DepositRequests) error {
// arLen, brLen := len(a), len(b)
// if arLen != brLen {
// return fmt.Errorf("requests len mismatch: expected: %v, got: %v", arLen, brLen)
// }

// for i := 0; i < arLen; i++ {
// checkRequests(t, &a[i], &b[i])
// }
return nil
}

func compareRawBodies(t *testing.T, a, b *RawBody) error {

atLen, btLen := len(a.Transactions), len(b.Transactions)
Expand Down

0 comments on commit 3ba3e0a

Please sign in to comment.