Skip to content

Commit

Permalink
Merge EngineGetPayloadWithBlobs into EngineGetPayload (#7637)
Browse files Browse the repository at this point in the history
  • Loading branch information
yperbasis authored Jun 2, 2023
1 parent 3750ee6 commit b2a90e0
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 87 deletions.
32 changes: 16 additions & 16 deletions cmd/rpcdaemon/commands/engine_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ type TransitionConfiguration struct {

// BlobsBundleV1 holds the blobs of an execution payload
type BlobsBundleV1 struct {
KZGs []types.KZGCommitment `json:"kzgs" gencodec:"required"`
Blobs []types.Blob `json:"blobs" gencodec:"required"`
Proofs []types.KZGProof `json:"proofs" gencodec:"required"`
Commitments []types.KZGCommitment `json:"commitments" gencodec:"required"`
Proofs []types.KZGProof `json:"proofs" gencodec:"required"`
Blobs []types.Blob `json:"blobs" gencodec:"required"`
}

type ExecutionPayloadBodyV1 struct {
Expand Down Expand Up @@ -402,24 +402,24 @@ func (e *EngineImpl) GetPayloadV3(ctx context.Context, payloadID hexutility.Byte
epl := convertPayloadFromRpc(response.ExecutionPayload)
blockValue := gointerfaces.ConvertH256ToUint256Int(response.BlockValue).ToBig()

ep, err := e.api.EngineGetBlobsBundleV1(ctx, decodedPayloadId)
if err != nil {
return nil, err
}
kzgs := ep.GetKzgs()
blobs := ep.GetBlobs()
if len(kzgs) != len(blobs) {
return nil, fmt.Errorf("should have same number of kzgs and blobs, got %v vs %v", len(kzgs), len(blobs))
commitments := response.BlobsBundle.GetCommitments()
proofs := response.BlobsBundle.GetProofs()
blobs := response.BlobsBundle.GetBlobs()
if len(commitments) != len(proofs) || len(proofs) != len(blobs) {
return nil, fmt.Errorf("should have same number of commitments/proofs/blobs, got %v vs %v vs %v", len(commitments), len(proofs), len(blobs))
}
replyKzgs := make([]types.KZGCommitment, len(kzgs))
replyCommitments := make([]types.KZGCommitment, len(commitments))
replyProofs := make([]types.KZGProof, len(proofs))
replyBlobs := make([]types.Blob, len(blobs))
for i := range kzgs {
copy(replyKzgs[i][:], kzgs[i])
for i := range commitments {
copy(replyCommitments[i][:], commitments[i])
copy(replyProofs[i][:], proofs[i])
copy(replyBlobs[i][:], blobs[i])
}
bb := &BlobsBundleV1{
KZGs: replyKzgs,
Blobs: replyBlobs,
Commitments: replyCommitments,
Proofs: replyProofs,
Blobs: replyBlobs,
}

return &GetPayloadV3Response{
Expand Down
6 changes: 0 additions & 6 deletions cmd/rpcdaemon/rpcservices/eth_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,6 @@ func (back *RemoteBackend) EngineGetPayloadBodiesByRangeV1(ctx context.Context,
return back.remoteEthBackend.EngineGetPayloadBodiesByRangeV1(ctx, request)
}

func (back *RemoteBackend) EngineGetBlobsBundleV1(ctx context.Context, payloadId uint64) (*types2.BlobsBundleV1, error) {
return back.remoteEthBackend.EngineGetBlobsBundleV1(ctx, &remote.EngineGetBlobsBundleRequest{
PayloadId: payloadId,
})
}

func (back *RemoteBackend) NodeInfo(ctx context.Context, limit uint32) ([]p2p.NodeInfo, error) {
nodes, err := back.remoteEthBackend.NodeInfo(ctx, &remote.NodesInfoRequest{Limit: limit})
if err != nil {
Expand Down
98 changes: 37 additions & 61 deletions ethdb/privateapi/ethbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ import (
// 3.0.0 - adding PoS interfaces
// 3.1.0 - add Subscribe to logs
// 3.2.0 - add EngineGetBlobsBundleV1
var EthBackendAPIVersion = &types2.VersionReply{Major: 3, Minor: 2, Patch: 0}
// 3.3.0 - merge EngineGetBlobsBundleV1 into EngineGetPayload
var EthBackendAPIVersion = &types2.VersionReply{Major: 3, Minor: 3, Patch: 0}

const MaxBuilders = 128

Expand Down Expand Up @@ -298,60 +299,6 @@ func (s *EthBackendServer) checkWithdrawalsPresence(time uint64, withdrawals []*
return nil
}

func (s *EthBackendServer) EngineGetBlobsBundleV1(ctx context.Context, req *remote.EngineGetBlobsBundleRequest) (*types2.BlobsBundleV1, error) {
// TODO: get the latest update on this function (it was replaced)
if !s.proposing {
return nil, fmt.Errorf("execution layer not running as a proposer. enable proposer by taking out the --proposer.disable flag on startup")
}

if s.config.TerminalTotalDifficulty == nil {
return nil, fmt.Errorf("not a proof-of-stake chain")
}

s.logger.Debug("[GetBlobsBundleV1] acquiring lock")
s.lock.Lock()
defer s.lock.Unlock()
s.logger.Debug("[GetBlobsBundleV1] lock acquired")

builder, ok := s.builders[req.PayloadId]
if !ok {
s.logger.Warn("Payload not stored", "payloadId", req.PayloadId)
return nil, &UnknownPayloadErr
}

block, err := builder.Stop()
if err != nil {
s.logger.Error("Failed to build PoS block", "err", err)
return nil, err
}

blobsBundle := &types2.BlobsBundleV1{
BlockHash: gointerfaces.ConvertHashToH256(block.Block.Header().Hash()),
}
for i, tx := range block.Block.Transactions() {
if tx.Type() != types.BlobTxType {
continue
}
blobtx, ok := tx.(*types.BlobTxWrapper)
if !ok {
return nil, fmt.Errorf("expected blob transaction to be type BlobTxWrapper, got: %T", blobtx)
}
versionedHashes, kzgs, blobs, proofs := blobtx.GetDataHashes(), blobtx.Commitments, blobtx.Blobs, blobtx.Proofs
lenCheck := len(versionedHashes)
if lenCheck != len(kzgs) || lenCheck != len(blobs) || lenCheck != len(blobtx.Proofs) {
return nil, fmt.Errorf("tx %d in block %s has inconsistent blobs (%d) / kzgs (%d) / proofs (%d)"+
" / versioned hashes (%d)", i, block.Block.Hash(), len(blobs), len(kzgs), len(proofs), lenCheck)
}
for _, blob := range blobs {
blobsBundle.Blobs = append(blobsBundle.Blobs, blob[:])
}
for _, kzg := range kzgs {
blobsBundle.Kzgs = append(blobsBundle.Kzgs, kzg[:])
}
}
return blobsBundle, nil
}

// EngineNewPayload validates and possibly executes payload
func (s *EthBackendServer) EngineNewPayload(ctx context.Context, req *types2.ExecutionPayload) (*remote.EnginePayloadStatus, error) {
header := types.Header{
Expand Down Expand Up @@ -619,9 +566,10 @@ func (s *EthBackendServer) EngineGetPayload(ctx context.Context, req *remote.Eng
return nil, err
}
block := blockWithReceipts.Block
header := block.Header()

baseFee := new(uint256.Int)
baseFee.SetFromBig(block.Header().BaseFee)
baseFee.SetFromBig(header.BaseFee)

encodedTransactions, err := types.MarshalTransactionsBinary(block.Transactions())
if err != nil {
Expand All @@ -630,10 +578,10 @@ func (s *EthBackendServer) EngineGetPayload(ctx context.Context, req *remote.Eng

payload := &types2.ExecutionPayload{
Version: 1,
ParentHash: gointerfaces.ConvertHashToH256(block.Header().ParentHash),
Coinbase: gointerfaces.ConvertAddressToH160(block.Header().Coinbase),
Timestamp: block.Header().Time,
PrevRandao: gointerfaces.ConvertHashToH256(block.Header().MixDigest),
ParentHash: gointerfaces.ConvertHashToH256(header.ParentHash),
Coinbase: gointerfaces.ConvertAddressToH160(header.Coinbase),
Timestamp: header.Time,
PrevRandao: gointerfaces.ConvertHashToH256(header.MixDigest),
StateRoot: gointerfaces.ConvertHashToH256(block.Root()),
ReceiptRoot: gointerfaces.ConvertHashToH256(block.ReceiptHash()),
LogsBloom: gointerfaces.ConvertBytesToH2048(block.Bloom().Bytes()),
Expand All @@ -642,7 +590,7 @@ func (s *EthBackendServer) EngineGetPayload(ctx context.Context, req *remote.Eng
BlockNumber: block.NumberU64(),
ExtraData: block.Extra(),
BaseFeePerGas: gointerfaces.ConvertUint256IntToH256(baseFee),
BlockHash: gointerfaces.ConvertHashToH256(block.Header().Hash()),
BlockHash: gointerfaces.ConvertHashToH256(block.Hash()),
Transactions: encodedTransactions,
}
if block.Withdrawals() != nil {
Expand All @@ -658,9 +606,37 @@ func (s *EthBackendServer) EngineGetPayload(ctx context.Context, req *remote.Eng
}

blockValue := blockValue(blockWithReceipts, baseFee)

blobsBundle := &types2.BlobsBundleV1{}
for i, tx := range block.Transactions() {
if tx.Type() != types.BlobTxType {
continue
}
blobTx, ok := tx.(*types.BlobTxWrapper)
if !ok {
return nil, fmt.Errorf("expected blob transaction to be type BlobTxWrapper, got: %T", blobTx)
}
versionedHashes, commitments, proofs, blobs := blobTx.GetDataHashes(), blobTx.Commitments, blobTx.Proofs, blobTx.Blobs
lenCheck := len(versionedHashes)
if lenCheck != len(commitments) || lenCheck != len(proofs) || lenCheck != len(blobs) {
return nil, fmt.Errorf("tx %d in block %s has inconsistent commitments (%d) / proofs (%d) / blobs (%d) / "+
"versioned hashes (%d)", i, block.Hash(), len(commitments), len(proofs), len(blobs), lenCheck)
}
for _, commitment := range commitments {
blobsBundle.Commitments = append(blobsBundle.Commitments, commitment[:])
}
for _, proof := range proofs {
blobsBundle.Proofs = append(blobsBundle.Proofs, proof[:])
}
for _, blob := range blobs {
blobsBundle.Blobs = append(blobsBundle.Blobs, blob[:])
}
}

return &remote.EngineGetPayloadResponse{
ExecutionPayload: payload,
BlockValue: gointerfaces.ConvertUint256IntToH256(blockValue),
BlobsBundle: blobsBundle,
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/ledgerwatch/erigon
go 1.19

require (
github.com/ledgerwatch/erigon-lib v0.0.0-20230602035349-195dc0de982f
github.com/ledgerwatch/erigon-lib v0.0.0-20230602102715-f166e7deb76f
github.com/ledgerwatch/erigon-snapshot v1.2.0
github.com/ledgerwatch/log/v3 v3.8.0
github.com/ledgerwatch/secp256k1 v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,8 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v0.0.0-20170224010052-a616ab194758 h1:0D5M2HQSGD3PYPwICLl+/9oulQauOuETfgFvhBDffs0=
github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8=
github.com/ledgerwatch/erigon-lib v0.0.0-20230602035349-195dc0de982f h1:Z8FYcbz1V6Wwww1n/1Qh44cMcKheBPd5iNVr3knaUuM=
github.com/ledgerwatch/erigon-lib v0.0.0-20230602035349-195dc0de982f/go.mod h1:R1Wsn0BxmEUZOIcAeGJmaqiXSdegEQ/+GfdjFruu+jQ=
github.com/ledgerwatch/erigon-lib v0.0.0-20230602102715-f166e7deb76f h1:of04RfFaI7sXYeAWZAIusucij2hNSRC+NTKkgx0iazo=
github.com/ledgerwatch/erigon-lib v0.0.0-20230602102715-f166e7deb76f/go.mod h1:KvN1A62PZypY+KfIzwXVbNOVS9CWx/gzqiRr5dPJxuk=
github.com/ledgerwatch/erigon-snapshot v1.2.0 h1:Pf6eu5XqB29Mlg3oY9zxZ8qenSi2azgcwuNRDvV2rAM=
github.com/ledgerwatch/erigon-snapshot v1.2.0/go.mod h1:3AuPxZc85jkehh/HA9h8gabv5MSi3kb/ddtzBsTVJFo=
github.com/ledgerwatch/log/v3 v3.8.0 h1:gCpp7uGtIerEz1jKVPeDnbIopFPud9ZnCpBLlLBGqPU=
Expand Down
1 change: 0 additions & 1 deletion turbo/rpchelper/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ type ApiBackend interface {
EngineNewPayload(ctx context.Context, payload *types2.ExecutionPayload) (*remote.EnginePayloadStatus, error)
EngineForkchoiceUpdated(ctx context.Context, request *remote.EngineForkChoiceUpdatedRequest) (*remote.EngineForkChoiceUpdatedResponse, error)
EngineGetPayload(ctx context.Context, payloadId uint64) (*remote.EngineGetPayloadResponse, error)
EngineGetBlobsBundleV1(ctx context.Context, payloadId uint64) (*types2.BlobsBundleV1, error)
NodeInfo(ctx context.Context, limit uint32) ([]p2p.NodeInfo, error)
Peers(ctx context.Context) ([]*p2p.PeerInfo, error)
PendingBlock(ctx context.Context) (*types.Block, error)
Expand Down

0 comments on commit b2a90e0

Please sign in to comment.