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

Optimistic relaying v2: header only parsing. #466

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
32 changes: 30 additions & 2 deletions common/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package common

import (
"compress/gzip"
"encoding/hex"
"encoding/json"
"io"
"os"
"testing"

"github.com/attestantio/go-builder-client/api/capella"
"github.com/attestantio/go-eth2-client/spec/bellatrix"
bellatrixspec "github.com/attestantio/go-eth2-client/spec/bellatrix"
consensuscapella "github.com/attestantio/go-eth2-client/spec/capella"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/flashbots/go-boost-utils/bls"
boostTypes "github.com/flashbots/go-boost-utils/types"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -70,7 +72,7 @@ func TestBuilderSubmitBlockRequest(sk *bls.SecretKey, bid *BidTraceV2) BuilderSu
Message: &bid.BidTrace,
Signature: [96]byte(signature),
ExecutionPayload: &consensuscapella.ExecutionPayload{ //nolint:exhaustruct
Transactions: []bellatrix.Transaction{[]byte{0x03}},
Transactions: []bellatrixspec.Transaction{[]byte{0x03}},
Timestamp: bid.Slot * 12, // 12 seconds per slot.
PrevRandao: _HexToHash("01234567890123456789012345678901"),
Withdrawals: []*consensuscapella.Withdrawal{},
Expand Down Expand Up @@ -98,3 +100,29 @@ func LoadGzippedJSON(t *testing.T, filename string, dst any) {
err := json.Unmarshal(b, dst)
require.NoError(t, err)
}

func TestBuilderSubmitBlockRequestV2(sk *bls.SecretKey, bid *BidTraceV2) *SubmitBlockRequest {
signature, err := boostTypes.SignMessage(bid, boostTypes.DomainBuilder, sk)
check(err, " SignMessage: ", bid, sk)

wRoot, err := hex.DecodeString("792930bbd5baac43bcc798ee49aa8185ef76bb3b44ba62b91d86ae569e4bb535")
check(err)
return &SubmitBlockRequest{
Message: &bid.BidTrace,
ExecutionPayloadHeader: &consensuscapella.ExecutionPayloadHeader{ //nolint:exhaustruct
TransactionsRoot: [32]byte{},
Timestamp: bid.Slot * 12, // 12 seconds per slot.
PrevRandao: _HexToHash("01234567890123456789012345678901"),
WithdrawalsRoot: phase0.Root(wRoot),
ExtraData: []byte{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
},
},
Signature: [96]byte(signature),
Transactions: []bellatrixspec.Transaction{[]byte{0x03}},
Withdrawals: []*consensuscapella.Withdrawal{},
}
}
256 changes: 254 additions & 2 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (
"github.com/attestantio/go-builder-client/spec"
apiv1capella "github.com/attestantio/go-eth2-client/api/v1/capella"
consensusspec "github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/bellatrix"
consensusbellatrix "github.com/attestantio/go-eth2-client/spec/bellatrix"
consensuscapella "github.com/attestantio/go-eth2-client/spec/capella"
"github.com/attestantio/go-eth2-client/spec/phase0"
ssz "github.com/ferranbt/fastssz"
boostTypes "github.com/flashbots/go-boost-utils/types"
)

Expand Down Expand Up @@ -671,7 +672,7 @@ func BoostBidToBidTrace(bidTrace *boostTypes.BidTrace) *apiv1.BidTrace {
BuilderPubkey: phase0.BLSPubKey(bidTrace.BuilderPubkey),
Slot: bidTrace.Slot,
ProposerPubkey: phase0.BLSPubKey(bidTrace.ProposerPubkey),
ProposerFeeRecipient: bellatrix.ExecutionAddress(bidTrace.ProposerFeeRecipient),
ProposerFeeRecipient: consensusbellatrix.ExecutionAddress(bidTrace.ProposerFeeRecipient),
BlockHash: phase0.Hash32(bidTrace.BlockHash),
Value: U256StrToUint256(bidTrace.Value),
ParentHash: phase0.Hash32(bidTrace.ParentHash),
Expand Down Expand Up @@ -781,3 +782,254 @@ func (b *BuilderSubmitBlockRequest) Withdrawals() []*consensuscapella.Withdrawal
}
return nil
}

// SubmitBlockRequest is the v2 request from the builder to submit a block.
type SubmitBlockRequest struct {

This comment was marked as outdated.

Message *apiv1.BidTrace
ExecutionPayloadHeader *consensuscapella.ExecutionPayloadHeader
Signature phase0.BLSSignature `ssz-size:"96"`
Transactions []consensusbellatrix.Transaction `ssz-max:"1048576,1073741824" ssz-size:"?,?"`
Withdrawals []*consensuscapella.Withdrawal `ssz-max:"16"`
}

// MarshalSSZ ssz marshals the SubmitBlockRequest object
func (s *SubmitBlockRequest) MarshalSSZ() ([]byte, error) {
return ssz.MarshalSSZ(s)
}

// UnmarshalSSZ ssz unmarshals the SubmitBlockRequest object
func (s *SubmitBlockRequest) UnmarshalSSZ(buf []byte) error {
var err error
size := uint64(len(buf))
if size < 344 {
return ssz.ErrSize
}

tail := buf
var o1, o3, o4 uint64

// Field (0) 'Message'
if s.Message == nil {
s.Message = new(apiv1.BidTrace)
}
if err = s.Message.UnmarshalSSZ(buf[0:236]); err != nil {
return err
}

// Offset (1) 'ExecutionPayloadHeader'
if o1 = ssz.ReadOffset(buf[236:240]); o1 > size {
return ssz.ErrOffset
}

if o1 < 344 {
return ssz.ErrInvalidVariableOffset
}

// Field (2) 'Signature'
copy(s.Signature[:], buf[240:336])

// Offset (3) 'Transactions'
if o3 = ssz.ReadOffset(buf[336:340]); o3 > size || o1 > o3 {
return ssz.ErrOffset
}

// Offset (4) 'Withdrawals'
if o4 = ssz.ReadOffset(buf[340:344]); o4 > size || o3 > o4 {
return ssz.ErrOffset
}

// Field (1) 'ExecutionPayloadHeader'
{
buf = tail[o1:o3]
if s.ExecutionPayloadHeader == nil {
s.ExecutionPayloadHeader = new(consensuscapella.ExecutionPayloadHeader)
}
if err = s.ExecutionPayloadHeader.UnmarshalSSZ(buf); err != nil {
return err
}
}

// Field (3) 'Transactions'
{
buf = tail[o3:o4]
num, err := ssz.DecodeDynamicLength(buf, 1073741824)
if err != nil {
return err
}
s.Transactions = make([]consensusbellatrix.Transaction, num)
err = ssz.UnmarshalDynamic(buf, num, func(indx int, buf []byte) (err error) {
if len(buf) > 1073741824 {
return ssz.ErrBytesLength
}
if cap(s.Transactions[indx]) == 0 {
s.Transactions[indx] = consensusbellatrix.Transaction(make([]byte, 0, len(buf)))
}
s.Transactions[indx] = append(s.Transactions[indx], buf...)
return nil
})
if err != nil {
return err
}
}

// Field (4) 'Withdrawals'
{
buf = tail[o4:]
num, err := ssz.DivideInt2(len(buf), 44, 16)
if err != nil {
return err
}
s.Withdrawals = make([]*consensuscapella.Withdrawal, num)
for ii := 0; ii < num; ii++ {
if s.Withdrawals[ii] == nil {
s.Withdrawals[ii] = new(consensuscapella.Withdrawal)
}
if err = s.Withdrawals[ii].UnmarshalSSZ(buf[ii*44 : (ii+1)*44]); err != nil {
return err
}
}
}
return err
}

// UnmarshalSSZHeaderOnly ssz unmarshals the first 3 fields of the SubmitBlockRequest object
func (s *SubmitBlockRequest) UnmarshalSSZHeaderOnly(buf []byte) error {
var err error
size := uint64(len(buf))
if size < 344 {
return ssz.ErrSize
}

tail := buf
var o1, o3 uint64

// Field (0) 'Message'
if s.Message == nil {
s.Message = new(apiv1.BidTrace)
}
if err = s.Message.UnmarshalSSZ(buf[0:236]); err != nil {
return err
}

// Offset (1) 'ExecutionPayloadHeader'
if o1 = ssz.ReadOffset(buf[236:240]); o1 > size {
return ssz.ErrOffset
}

if o1 < 344 {
return ssz.ErrInvalidVariableOffset
}

// Field (2) 'Signature'
copy(s.Signature[:], buf[240:336])

// Offset (3) 'Transactions'
if o3 = ssz.ReadOffset(buf[336:340]); o3 > size || o1 > o3 {
return ssz.ErrOffset
}

// Field (1) 'ExecutionPayloadHeader'
{
buf = tail[o1:o3]
if s.ExecutionPayloadHeader == nil {
s.ExecutionPayloadHeader = new(consensuscapella.ExecutionPayloadHeader)
}
if err = s.ExecutionPayloadHeader.UnmarshalSSZ(buf); err != nil {
return err
}
}
return err
}

// MarshalSSZTo ssz marshals the SubmitBlockRequest object to a target array
func (s *SubmitBlockRequest) MarshalSSZTo(buf []byte) (dst []byte, err error) {
dst = buf
offset := int(344)

// Field (0) 'Message'
if s.Message == nil {
s.Message = new(apiv1.BidTrace)
}
if dst, err = s.Message.MarshalSSZTo(dst); err != nil {
return
}

// Offset (1) 'ExecutionPayloadHeader'
dst = ssz.WriteOffset(dst, offset)
if s.ExecutionPayloadHeader == nil {
s.ExecutionPayloadHeader = new(consensuscapella.ExecutionPayloadHeader)
}
offset += s.ExecutionPayloadHeader.SizeSSZ()

// Field (2) 'Signature'
dst = append(dst, s.Signature[:]...)

// Offset (3) 'Transactions'
dst = ssz.WriteOffset(dst, offset)
for ii := 0; ii < len(s.Transactions); ii++ {
offset += 4
offset += len(s.Transactions[ii])
}

// Offset (4) 'Withdrawals'
dst = ssz.WriteOffset(dst, offset)

// Field (1) 'ExecutionPayloadHeader'
if dst, err = s.ExecutionPayloadHeader.MarshalSSZTo(dst); err != nil {
return
}

// Field (3) 'Transactions'
if size := len(s.Transactions); size > 1073741824 {
err = ssz.ErrListTooBigFn("SubmitBlockRequest.Transactions", size, 1073741824)
return
}
{
offset = 4 * len(s.Transactions)
for ii := 0; ii < len(s.Transactions); ii++ {
dst = ssz.WriteOffset(dst, offset)
offset += len(s.Transactions[ii])
}
}
for ii := 0; ii < len(s.Transactions); ii++ {
if size := len(s.Transactions[ii]); size > 1073741824 {
err = ssz.ErrBytesLengthFn("SubmitBlockRequest.Transactions[ii]", size, 1073741824)
return
}
dst = append(dst, s.Transactions[ii]...)
}

// Field (4) 'Withdrawals'
if size := len(s.Withdrawals); size > 16 {
err = ssz.ErrListTooBigFn("SubmitBlockRequest.Withdrawals", size, 16)
return
}
for ii := 0; ii < len(s.Withdrawals); ii++ {
if dst, err = s.Withdrawals[ii].MarshalSSZTo(dst); err != nil {
return
}
}
return dst, nil
}

// SizeSSZ returns the ssz encoded size in bytes for the SubmitBlockRequest object
func (s *SubmitBlockRequest) SizeSSZ() (size int) {
size = 344

// Field (1) 'ExecutionPayloadHeader'
if s.ExecutionPayloadHeader == nil {
s.ExecutionPayloadHeader = new(consensuscapella.ExecutionPayloadHeader)
}
size += s.ExecutionPayloadHeader.SizeSSZ()

// Field (3) 'Transactions'
for ii := 0; ii < len(s.Transactions); ii++ {
size += 4
size += len(s.Transactions[ii])
}

// Field (4) 'Withdrawals'
size += len(s.Withdrawals) * 44

return
}
29 changes: 29 additions & 0 deletions common/types_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
utilcapella "github.com/attestantio/go-eth2-client/util/capella"
"github.com/flashbots/go-boost-utils/bls"
boostTypes "github.com/flashbots/go-boost-utils/types"
"github.com/holiman/uint256"
)

var (
Expand Down Expand Up @@ -71,6 +72,34 @@ func BuildGetHeaderResponse(payload *BuilderSubmitBlockRequest, sk *bls.SecretKe
return nil, ErrEmptyPayload
}

func BuildGetHeaderResponseHeaderOnly(value *uint256.Int, header *consensuscapella.ExecutionPayloadHeader, sk *bls.SecretKey, pubkey *boostTypes.PublicKey, domain boostTypes.Domain) (*GetHeaderResponse, error) {
builderBid := capella.BuilderBid{
Value: value,
Header: header,
Pubkey: *(*phase0.BLSPubKey)(pubkey),
}

sig, err := boostTypes.SignMessage(&builderBid, domain, sk)
if err != nil {
return nil, err
}

signedBuilderBid := &capella.SignedBuilderBid{
Message: &builderBid,
Signature: phase0.BLSSignature(sig),
}

return &GetHeaderResponse{
Capella: &spec.VersionedSignedBuilderBid{
Version: consensusspec.DataVersionCapella,
Capella: signedBuilderBid,
Bellatrix: nil,
},
Bellatrix: nil,
}, nil

}

func BuildGetPayloadResponse(payload *BuilderSubmitBlockRequest) (*GetPayloadResponse, error) {
if payload.Bellatrix != nil {
return &GetPayloadResponse{
Expand Down
Loading