Skip to content

Commit

Permalink
Merge branch 'develop' into remove-signed-blinded-beacon-block-wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
avalonche committed Jul 20, 2023
2 parents d539b25 + d2994fd commit ca220ab
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 104 deletions.
43 changes: 4 additions & 39 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,15 +325,11 @@ func (b *BuilderSubmitBlockRequest) HasExecutionPayload() bool {
return false
}

func (b *BuilderSubmitBlockRequest) ExecutionPayloadResponse() (*GetPayloadResponse, error) {
func (b *BuilderSubmitBlockRequest) ExecutionPayloadResponse() (*api.VersionedExecutionPayload, error) {
if b.Capella != nil {
return &GetPayloadResponse{
Capella: &api.VersionedExecutionPayload{
Version: consensusspec.DataVersionCapella,
Capella: b.Capella.ExecutionPayload,
Bellatrix: nil,
},
Bellatrix: nil,
return &api.VersionedExecutionPayload{
Version: consensusspec.DataVersionCapella,
Capella: b.Capella.ExecutionPayload,
}, nil
}

Expand Down Expand Up @@ -459,37 +455,6 @@ func (b *BuilderSubmitBlockRequest) Message() *apiv1.BidTrace {
return nil
}

type GetPayloadResponse struct {
Bellatrix *boostTypes.GetPayloadResponse
Capella *api.VersionedExecutionPayload
}

func (p *GetPayloadResponse) UnmarshalJSON(data []byte) error {
capella := new(api.VersionedExecutionPayload)
err := json.Unmarshal(data, capella)
if err == nil && capella.Capella != nil {
p.Capella = capella
return nil
}
bellatrix := new(boostTypes.GetPayloadResponse)
err = json.Unmarshal(data, bellatrix)
if err != nil {
return err
}
p.Bellatrix = bellatrix
return nil
}

func (p *GetPayloadResponse) MarshalJSON() ([]byte, error) {
if p.Bellatrix != nil {
return json.Marshal(p.Bellatrix)
}
if p.Capella != nil {
return json.Marshal(p.Capella)
}
return nil, ErrEmptyPayload
}

type GetHeaderResponse struct {
Bellatrix *boostTypes.GetHeaderResponse
Capella *spec.VersionedSignedBuilderBid
Expand Down
23 changes: 8 additions & 15 deletions common/types_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var NilResponse = struct{}{}

var ZeroU256 = boostTypes.IntToU256(0)

func BuildGetHeaderResponse(payload *BuilderSubmitBlockRequest, sk *bls.SecretKey, pubkey *boostTypes.PublicKey, domain boostTypes.Domain) (*GetHeaderResponse, error) {
func BuildGetHeaderResponse(payload *BuilderSubmitBlockRequest, sk *bls.SecretKey, pubkey *boostTypes.PublicKey, domain boostTypes.Domain) (*spec.VersionedSignedBuilderBid, error) {
if payload == nil {
return nil, ErrMissingRequest
}
Expand All @@ -46,27 +46,20 @@ func BuildGetHeaderResponse(payload *BuilderSubmitBlockRequest, sk *bls.SecretKe
if err != nil {
return nil, err
}
return &GetHeaderResponse{
Capella: &spec.VersionedSignedBuilderBid{
Version: consensusspec.DataVersionCapella,
Capella: signedBuilderBid,
Bellatrix: nil,
},
return &spec.VersionedSignedBuilderBid{
Version: consensusspec.DataVersionCapella,
Capella: signedBuilderBid,
Bellatrix: nil,
}, nil
}
return nil, ErrEmptyPayload
}

func BuildGetPayloadResponse(payload *BuilderSubmitBlockRequest) (*GetPayloadResponse, error) {
func BuildGetPayloadResponse(payload *BuilderSubmitBlockRequest) (*api.VersionedExecutionPayload, error) {
if payload.Capella != nil {
return &GetPayloadResponse{
Capella: &api.VersionedExecutionPayload{
Version: consensusspec.DataVersionCapella,
Capella: payload.Capella.ExecutionPayload,
Bellatrix: nil,
},
Bellatrix: nil,
return &api.VersionedExecutionPayload{
Version: consensusspec.DataVersionCapella,
Capella: payload.Capella.ExecutionPayload,
}, nil
}

Expand Down
7 changes: 4 additions & 3 deletions common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ import (
"errors"
"fmt"
"io"
"math/big"
"net/http"
"os"
"strconv"
"strings"
"testing"
"time"

"github.com/attestantio/go-builder-client/api"
"github.com/attestantio/go-builder-client/api/capella"
v1 "github.com/attestantio/go-builder-client/api/v1"
"github.com/attestantio/go-builder-client/spec"
capellaspec "github.com/attestantio/go-eth2-client/spec/capella"
"github.com/attestantio/go-eth2-client/spec/phase0"
ethcommon "github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -182,7 +183,7 @@ type CreateTestBlockSubmissionOpts struct {
ProposerPubkey string
}

func CreateTestBlockSubmission(t *testing.T, builderPubkey string, value *big.Int, opts *CreateTestBlockSubmissionOpts) (payload *BuilderSubmitBlockRequest, getPayloadResponse *GetPayloadResponse, getHeaderResponse *GetHeaderResponse) {
func CreateTestBlockSubmission(t *testing.T, builderPubkey string, value *uint256.Int, opts *CreateTestBlockSubmissionOpts) (payload *BuilderSubmitBlockRequest, getPayloadResponse *api.VersionedExecutionPayload, getHeaderResponse *spec.VersionedSignedBuilderBid) {
t.Helper()
var err error

Expand Down Expand Up @@ -217,7 +218,7 @@ func CreateTestBlockSubmission(t *testing.T, builderPubkey string, value *big.In
Capella: &capella.SubmitBlockRequest{
Message: &v1.BidTrace{ //nolint:exhaustruct
BuilderPubkey: builderPk,
Value: uint256.MustFromBig(value),
Value: value,
Slot: slot,
ParentHash: parentHash,
ProposerPubkey: proposerPk,
Expand Down
5 changes: 2 additions & 3 deletions database/typesconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,8 @@ func ExecutionPayloadEntryToExecutionPayload(executionPayloadEntry *ExecutionPay
return nil, err
}
return &api.VersionedExecutionPayload{
Version: consensusspec.DataVersionCapella,
Capella: executionPayload,
Bellatrix: nil,
Version: consensusspec.DataVersionCapella,
Capella: executionPayload,
}, nil
} else {
return nil, ErrUnsupportedExecutionPayload
Expand Down
3 changes: 1 addition & 2 deletions datastore/execution_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package datastore

import (
"github.com/attestantio/go-builder-client/api"
"github.com/flashbots/mev-boost-relay/common"
)

// ExecutionPayloadRepository defines methods to fetch and store execution engine payloads
type ExecutionPayloadRepository interface {
GetExecutionPayload(slot uint64, proposerPubKey, blockHash string) (*api.VersionedExecutionPayload, error)
SaveExecutionPayload(slot uint64, proposerPubKey, blockHash string, payload *common.GetPayloadResponse) error
SaveExecutionPayload(slot uint64, proposerPubKey, blockHash string, payload *api.VersionedExecutionPayload) error
}
3 changes: 1 addition & 2 deletions datastore/memcached.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/attestantio/go-builder-client/api"
"github.com/bradfitz/gomemcache/memcache"
"github.com/flashbots/go-utils/cli"
"github.com/flashbots/mev-boost-relay/common"
)

var (
Expand All @@ -25,7 +24,7 @@ type Memcached struct {
// SaveExecutionPayload attempts to insert execution engine payload to memcached using composite key of slot,
// proposer public key, block hash, and cache prefix if specified. Note that writes to the same key value
// (i.e. same slot, proposer public key, and block hash) will overwrite the existing entry.
func (m *Memcached) SaveExecutionPayload(slot uint64, proposerPubKey, blockHash string, payload *common.GetPayloadResponse) error {
func (m *Memcached) SaveExecutionPayload(slot uint64, proposerPubKey, blockHash string, payload *api.VersionedExecutionPayload) error {
// TODO: standardize key format with redis cache and re-use the same function(s)
key := fmt.Sprintf("boost-relay/%s:cache-getpayload-response:%d_%s_%s", m.keyPrefix, slot, proposerPubKey, blockHash)

Expand Down
6 changes: 3 additions & 3 deletions datastore/memcached_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,15 @@ func TestMemcached(t *testing.T) {
require.NoError(t, err)
require.Equal(t, len(prev.Capella.Transactions), tc.Input.NumTx())

payload.Bellatrix.Data.GasLimit++
require.NotEqual(t, prev.Bellatrix.GasLimit, payload.Bellatrix.Data.GasLimit)
payload.Bellatrix.GasLimit++
require.NotEqual(t, prev.Bellatrix.GasLimit, payload.Bellatrix.GasLimit)

err = mem.SaveExecutionPayload(tc.Input.Slot(), tc.Input.ProposerPubkey(), tc.Input.BlockHash(), payload)
require.NoError(t, err)

current, err := mem.GetExecutionPayload(tc.Input.Slot(), tc.Input.ProposerPubkey(), tc.Input.BlockHash())
require.NoError(t, err)
require.Equal(t, current.Bellatrix.GasLimit, payload.Bellatrix.Data.GasLimit)
require.Equal(t, current.Bellatrix.GasLimit, payload.Bellatrix.GasLimit)
require.NotEqual(t, current.Bellatrix.GasLimit, prev.Bellatrix.GasLimit)
}
},
Expand Down
35 changes: 23 additions & 12 deletions datastore/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/attestantio/go-builder-client/api"
"github.com/attestantio/go-builder-client/spec"
consensusspec "github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/capella"
boostTypes "github.com/flashbots/go-boost-utils/types"
Expand Down Expand Up @@ -348,9 +349,9 @@ func (r *RedisCache) GetRelayConfig(field string) (string, error) {
return res, err
}

func (r *RedisCache) GetBestBid(slot uint64, parentHash, proposerPubkey string) (*common.GetHeaderResponse, error) {
func (r *RedisCache) GetBestBid(slot uint64, parentHash, proposerPubkey string) (*spec.VersionedSignedBuilderBid, error) {
key := r.keyCacheGetHeaderResponse(slot, parentHash, proposerPubkey)
resp := new(common.GetHeaderResponse)
resp := new(spec.VersionedSignedBuilderBid)
err := r.GetObj(key, resp)
if errors.Is(err, redis.Nil) {
return nil, nil
Expand All @@ -368,7 +369,6 @@ func (r *RedisCache) SaveExecutionPayloadCapella(ctx context.Context, tx redis.P
}

func (r *RedisCache) GetExecutionPayloadCapella(slot uint64, proposerPubkey, blockHash string) (*api.VersionedExecutionPayload, error) {
resp := new(api.VersionedExecutionPayload)
capellaPayload := new(capella.ExecutionPayload)

key := r.keyExecPayloadCapella(slot, proposerPubkey, blockHash)
Expand All @@ -382,9 +382,10 @@ func (r *RedisCache) GetExecutionPayloadCapella(slot uint64, proposerPubkey, blo
return nil, err
}

resp.Capella = capellaPayload
resp.Version = consensusspec.DataVersionCapella
return resp, nil
return &api.VersionedExecutionPayload{
Version: consensusspec.DataVersionCapella,
Capella: capellaPayload,
}, nil
}

func (r *RedisCache) SaveBidTrace(ctx context.Context, tx redis.Pipeliner, trace *common.BidTraceV2) (err error) {
Expand Down Expand Up @@ -413,7 +414,7 @@ func (r *RedisCache) GetBuilderLatestPayloadReceivedAt(ctx context.Context, tx r
}

// SaveBuilderBid saves the latest bid by a specific builder. TODO: use transaction to make these writes atomic
func (r *RedisCache) SaveBuilderBid(ctx context.Context, tx redis.Pipeliner, slot uint64, parentHash, proposerPubkey, builderPubkey string, receivedAt time.Time, headerResp *common.GetHeaderResponse) (err error) {
func (r *RedisCache) SaveBuilderBid(ctx context.Context, tx redis.Pipeliner, slot uint64, parentHash, proposerPubkey, builderPubkey string, receivedAt time.Time, headerResp *spec.VersionedSignedBuilderBid) (err error) {
// save the actual bid
keyLatestBid := r.keyLatestBidByBuilder(slot, parentHash, proposerPubkey, builderPubkey)
err = r.SetObjPipelined(ctx, tx, keyLatestBid, headerResp, expiryBidCache)
Expand All @@ -434,7 +435,11 @@ func (r *RedisCache) SaveBuilderBid(ctx context.Context, tx redis.Pipeliner, slo

// set the value last, because that's iterated over when updating the best bid, and the payload has to be available
keyLatestBidsValue := r.keyBlockBuilderLatestBidsValue(slot, parentHash, proposerPubkey)
err = tx.HSet(ctx, keyLatestBidsValue, builderPubkey, headerResp.Value().String()).Err()
value, err := headerResp.Value()
if err != nil {
return err
}
err = tx.HSet(ctx, keyLatestBidsValue, builderPubkey, value.ToBig().String()).Err()
if err != nil {
return err
}
Expand All @@ -457,7 +462,7 @@ type SaveBidAndUpdateTopBidResponse struct {
TimeUpdateFloor time.Duration
}

func (r *RedisCache) SaveBidAndUpdateTopBid(ctx context.Context, tx redis.Pipeliner, trace *common.BidTraceV2, payload *common.BuilderSubmitBlockRequest, getPayloadResponse *common.GetPayloadResponse, getHeaderResponse *common.GetHeaderResponse, reqReceivedAt time.Time, isCancellationEnabled bool, floorValue *big.Int) (state SaveBidAndUpdateTopBidResponse, err error) {
func (r *RedisCache) SaveBidAndUpdateTopBid(ctx context.Context, tx redis.Pipeliner, trace *common.BidTraceV2, payload *common.BuilderSubmitBlockRequest, getPayloadResponse *api.VersionedExecutionPayload, getHeaderResponse *spec.VersionedSignedBuilderBid, reqReceivedAt time.Time, isCancellationEnabled bool, floorValue *big.Int) (state SaveBidAndUpdateTopBidResponse, err error) {
var prevTime, nextTime time.Time
prevTime = time.Now()

Expand Down Expand Up @@ -497,7 +502,7 @@ func (r *RedisCache) SaveBidAndUpdateTopBid(ctx context.Context, tx redis.Pipeli
// Time to save things in Redis
//
// 1. Save the execution payload
err = r.SaveExecutionPayloadCapella(ctx, tx, payload.Slot(), payload.ProposerPubkey(), payload.BlockHash(), getPayloadResponse.Capella.Capella)
err = r.SaveExecutionPayloadCapella(ctx, tx, payload.Slot(), payload.ProposerPubkey(), payload.BlockHash(), getPayloadResponse.Capella)
if err != nil {
return state, err
}
Expand Down Expand Up @@ -665,7 +670,10 @@ func (r *RedisCache) GetTopBidValue(ctx context.Context, tx redis.Pipeliner, slo
return nil, err
}
topBidValue = new(big.Int)
topBidValue.SetString(topBidValueStr, 10)
topBidValue, ok := topBidValue.SetString(topBidValueStr, 10)
if !ok {
return nil, fmt.Errorf("could not set top bid value from %s", topBidValueStr) //nolint:goerr113
}
return topBidValue, nil
}

Expand All @@ -679,7 +687,10 @@ func (r *RedisCache) GetBuilderLatestValue(slot uint64, parentHash, proposerPubk
return nil, err
}
topBidValue = new(big.Int)
topBidValue.SetString(topBidValueStr, 10)
topBidValue, ok := topBidValue.SetString(topBidValueStr, 10)
if !ok {
return nil, fmt.Errorf("could not set top bid value from %s", topBidValueStr) //nolint:goerr113
}
return topBidValue, nil
}

Expand Down
30 changes: 15 additions & 15 deletions datastore/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ func TestBuilderBids(t *testing.T) {
ensureBestBidValueEquals := func(expectedValue int64, builderPubkey string) {
bestBid, err := cache.GetBestBid(slot, parentHash, proposerPubkey)
require.NoError(t, err)
require.Equal(t, big.NewInt(expectedValue), bestBid.Value())
value, err := bestBid.Value()
require.NoError(t, err)
require.Equal(t, big.NewInt(expectedValue), value.ToBig())

topBidValue, err := cache.GetTopBidValue(context.Background(), cache.client.Pipeline(), slot, parentHash, proposerPubkey)
require.NoError(t, err)
Expand All @@ -242,7 +244,7 @@ func TestBuilderBids(t *testing.T) {
require.NoError(t, err)

// submit ba1=10
payload, getPayloadResp, getHeaderResp := common.CreateTestBlockSubmission(t, bApubkey, big.NewInt(10), &opts)
payload, getPayloadResp, getHeaderResp := common.CreateTestBlockSubmission(t, bApubkey, uint256.NewInt(10), &opts)
resp, err := cache.SaveBidAndUpdateTopBid(context.Background(), cache.NewPipeline(), trace, payload, getPayloadResp, getHeaderResp, time.Now(), false, nil)
require.NoError(t, err)
require.True(t, resp.WasBidSaved, resp)
Expand All @@ -261,7 +263,7 @@ func TestBuilderBids(t *testing.T) {
ensureBidFloor(10)

// submit ba2=5 (should not update, because floor is 10)
payload, getPayloadResp, getHeaderResp = common.CreateTestBlockSubmission(t, bApubkey, big.NewInt(5), &opts)
payload, getPayloadResp, getHeaderResp = common.CreateTestBlockSubmission(t, bApubkey, uint256.NewInt(5), &opts)
resp, err = cache.SaveBidAndUpdateTopBid(context.Background(), cache.NewPipeline(), trace, payload, getPayloadResp, getHeaderResp, time.Now(), false, nil)
require.NoError(t, err)
require.False(t, resp.WasBidSaved, resp)
Expand All @@ -272,7 +274,7 @@ func TestBuilderBids(t *testing.T) {
ensureBidFloor(10)

// submit ba3c=5 (should not update, because floor is 10)
payload, getPayloadResp, getHeaderResp = common.CreateTestBlockSubmission(t, bApubkey, big.NewInt(5), &opts)
payload, getPayloadResp, getHeaderResp = common.CreateTestBlockSubmission(t, bApubkey, uint256.NewInt(5), &opts)
resp, err = cache.SaveBidAndUpdateTopBid(context.Background(), cache.NewPipeline(), trace, payload, getPayloadResp, getHeaderResp, time.Now(), true, nil)
require.NoError(t, err)
require.True(t, resp.WasBidSaved)
Expand All @@ -284,7 +286,7 @@ func TestBuilderBids(t *testing.T) {
ensureBidFloor(10)

// submit bb1=20
payload, getPayloadResp, getHeaderResp = common.CreateTestBlockSubmission(t, bBpubkey, big.NewInt(20), &opts)
payload, getPayloadResp, getHeaderResp = common.CreateTestBlockSubmission(t, bBpubkey, uint256.NewInt(20), &opts)
resp, err = cache.SaveBidAndUpdateTopBid(context.Background(), cache.NewPipeline(), trace, payload, getPayloadResp, getHeaderResp, time.Now(), false, nil)
require.NoError(t, err)
require.True(t, resp.WasBidSaved)
Expand All @@ -295,7 +297,7 @@ func TestBuilderBids(t *testing.T) {
ensureBidFloor(20)

// submit bb2c=22
payload, getPayloadResp, getHeaderResp = common.CreateTestBlockSubmission(t, bBpubkey, big.NewInt(22), &opts)
payload, getPayloadResp, getHeaderResp = common.CreateTestBlockSubmission(t, bBpubkey, uint256.NewInt(22), &opts)
resp, err = cache.SaveBidAndUpdateTopBid(context.Background(), cache.NewPipeline(), trace, payload, getPayloadResp, getHeaderResp, time.Now(), true, nil)
require.NoError(t, err)
require.True(t, resp.WasBidSaved)
Expand All @@ -306,7 +308,7 @@ func TestBuilderBids(t *testing.T) {
ensureBidFloor(20)

// submit bb3c=12 (should update top bid, using floor at 20)
payload, getPayloadResp, getHeaderResp = common.CreateTestBlockSubmission(t, bBpubkey, big.NewInt(12), &opts)
payload, getPayloadResp, getHeaderResp = common.CreateTestBlockSubmission(t, bBpubkey, uint256.NewInt(12), &opts)
resp, err = cache.SaveBidAndUpdateTopBid(context.Background(), cache.NewPipeline(), trace, payload, getPayloadResp, getHeaderResp, time.Now(), true, nil)
require.NoError(t, err)
require.True(t, resp.WasBidSaved)
Expand Down Expand Up @@ -475,18 +477,16 @@ func TestGetBuilderLatestValue(t *testing.T) {
// With no bids, should return "0".
v, err := cache.GetBuilderLatestValue(slot, parentHash, proposerPubkey, builderPubkey)
require.NoError(t, err)
require.Equal(t, v.Text(10), "0")
require.Equal(t, "0", v.String())

// Set a bid of 1 ETH.
newVal, err := uint256.FromDecimal("1000000000000000000")
require.NoError(t, err)
getHeaderResp := &common.GetHeaderResponse{
Capella: &spec.VersionedSignedBuilderBid{
Version: consensusspec.DataVersionCapella,
Capella: &capella.SignedBuilderBid{
Message: &capella.BuilderBid{
Value: newVal,
},
getHeaderResp := &spec.VersionedSignedBuilderBid{
Version: consensusspec.DataVersionCapella,
Capella: &capella.SignedBuilderBid{
Message: &capella.BuilderBid{
Value: newVal,
},
},
}
Expand Down
Loading

0 comments on commit ca220ab

Please sign in to comment.