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

Ssz encoded request #69

Merged
merged 8 commits into from
May 11, 2023
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
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ test: all
lint: ## Run linters.
$(GORUN) build/ci.go lint

fmt:
gofmt -s -w .
gofumpt -extra -w .
gci write .
go mod tidy

clean:
env GO111MODULE=on go clean -cache
rm -fr build/_workspace/pkg/ $(GOBIN)/*
Expand Down
5 changes: 3 additions & 2 deletions builder/beacon_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (b *testBeaconClient) Stop() {}
func (b *testBeaconClient) isValidator(pubkey PubkeyHex) bool {
return true
}

func (b *testBeaconClient) getProposerForNextSlot(requestedSlot uint64) (PubkeyHex, error) {
return PubkeyHex(hexutil.Encode(b.validator.Pk)), nil
}
Expand Down Expand Up @@ -68,7 +69,7 @@ type MultiBeaconClient struct {
closeCh chan struct{}
}

func NewMultiBeaconClient(endpoints []string, slotsInEpoch uint64, secondsInSlot uint64) *MultiBeaconClient {
func NewMultiBeaconClient(endpoints []string, slotsInEpoch, secondsInSlot uint64) *MultiBeaconClient {
clients := []*BeaconClient{}
for _, endpoint := range endpoints {
client := NewBeaconClient(endpoint, slotsInEpoch, secondsInSlot)
Expand Down Expand Up @@ -141,7 +142,7 @@ type BeaconClient struct {
cancelFn context.CancelFunc
}

func NewBeaconClient(endpoint string, slotsInEpoch uint64, secondsInSlot uint64) *BeaconClient {
func NewBeaconClient(endpoint string, slotsInEpoch, secondsInSlot uint64) *BeaconClient {
ctx, cancelFn := context.WithCancel(context.Background())
return &BeaconClient{
endpoint: endpoint,
Expand Down
59 changes: 35 additions & 24 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,22 @@ import (
"sync"
"time"

capellaapi "github.com/attestantio/go-builder-client/api/capella"
apiv1 "github.com/attestantio/go-builder-client/api/v1"
"github.com/attestantio/go-eth2-client/spec/bellatrix"
"github.com/attestantio/go-eth2-client/spec/capella"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/ethereum/go-ethereum/common"
blockvalidation "github.com/ethereum/go-ethereum/eth/block-validation"
"github.com/holiman/uint256"
"golang.org/x/time/rate"

"github.com/ethereum/go-ethereum/beacon/engine"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
blockvalidation "github.com/ethereum/go-ethereum/eth/block-validation"
"github.com/ethereum/go-ethereum/flashbotsextra"
"github.com/ethereum/go-ethereum/log"

capellaapi "github.com/attestantio/go-builder-client/api/capella"
apiv1 "github.com/attestantio/go-builder-client/api/v1"

"github.com/flashbots/go-boost-utils/bls"
boostTypes "github.com/flashbots/go-boost-utils/types"
"github.com/holiman/uint256"
"golang.org/x/time/rate"
)

type PubkeyHex string
Expand All @@ -41,6 +38,7 @@ type IRelay interface {
SubmitBlock(msg *boostTypes.BuilderSubmitBlockRequest, vd ValidatorData) error
SubmitBlockCapella(msg *capellaapi.SubmitBlockRequest, vd ValidatorData) error
GetValidatorForSlot(nextSlot uint64) (ValidatorData, error)
Config() RelayConfig
Start() error
Stop()
}
Expand Down Expand Up @@ -73,23 +71,36 @@ type Builder struct {
stop chan struct{}
}

func NewBuilder(sk *bls.SecretKey, ds flashbotsextra.IDatabaseService, relay IRelay, builderSigningDomain boostTypes.Domain, eth IEthereumService, dryRun bool, ignoreLatePayloadAttributes bool, validator *blockvalidation.BlockValidationAPI, beaconClient IBeaconClient) *Builder {
pkBytes := bls.PublicKeyFromSecretKey(sk).Compress()
// BuilderArgs is a struct that contains all the arguments needed to create a new Builder
type BuilderArgs struct {
avalonche marked this conversation as resolved.
Show resolved Hide resolved
sk *bls.SecretKey
ds flashbotsextra.IDatabaseService
relay IRelay
builderSigningDomain boostTypes.Domain
eth IEthereumService
dryRun bool
ignoreLatePayloadAttributes bool
validator *blockvalidation.BlockValidationAPI
beaconClient IBeaconClient
}

func NewBuilder(args BuilderArgs) *Builder {
pkBytes := bls.PublicKeyFromSecretKey(args.sk).Compress()
pk := boostTypes.PublicKey{}
pk.FromSlice(pkBytes)

slotCtx, slotCtxCancel := context.WithCancel(context.Background())
return &Builder{
ds: ds,
relay: relay,
eth: eth,
dryRun: dryRun,
ignoreLatePayloadAttributes: ignoreLatePayloadAttributes,
validator: validator,
beaconClient: beaconClient,
builderSecretKey: sk,
ds: args.ds,
relay: args.relay,
eth: args.eth,
dryRun: args.dryRun,
ignoreLatePayloadAttributes: args.ignoreLatePayloadAttributes,
validator: args.validator,
beaconClient: args.beaconClient,
builderSecretKey: args.sk,
builderPublicKey: pk,
builderSigningDomain: builderSigningDomain,
builderSigningDomain: args.builderSigningDomain,

limiter: rate.NewLimiter(rate.Every(time.Millisecond), 510),
slotCtx: slotCtx,
Expand Down Expand Up @@ -136,7 +147,7 @@ func (b *Builder) Stop() error {
return nil
}

func (b *Builder) onSealedBlock(block *types.Block, blockValue *big.Int, ordersClosedAt time.Time, sealedAt time.Time, commitedBundles []types.SimulatedBundle, allBundles []types.SimulatedBundle, proposerPubkey boostTypes.PublicKey, vd ValidatorData, attrs *types.BuilderPayloadAttributes) error {
func (b *Builder) onSealedBlock(block *types.Block, blockValue *big.Int, ordersClosedAt, sealedAt time.Time, commitedBundles, allBundles []types.SimulatedBundle, proposerPubkey boostTypes.PublicKey, vd ValidatorData, attrs *types.BuilderPayloadAttributes) error {
if b.eth.Config().IsShanghai(block.Time()) {
if err := b.submitCapellaBlock(block, blockValue, ordersClosedAt, sealedAt, commitedBundles, allBundles, proposerPubkey, vd, attrs); err != nil {
return err
Expand All @@ -152,7 +163,7 @@ func (b *Builder) onSealedBlock(block *types.Block, blockValue *big.Int, ordersC
return nil
}

func (b *Builder) submitBellatrixBlock(block *types.Block, blockValue *big.Int, ordersClosedAt time.Time, sealedAt time.Time, commitedBundles []types.SimulatedBundle, allBundles []types.SimulatedBundle, proposerPubkey boostTypes.PublicKey, vd ValidatorData, attrs *types.BuilderPayloadAttributes) error {
func (b *Builder) submitBellatrixBlock(block *types.Block, blockValue *big.Int, ordersClosedAt, sealedAt time.Time, commitedBundles, allBundles []types.SimulatedBundle, proposerPubkey boostTypes.PublicKey, vd ValidatorData, attrs *types.BuilderPayloadAttributes) error {
executableData := engine.BlockToExecutableData(block, blockValue)
payload, err := executableDataToExecutionPayload(executableData.ExecutionPayload)
if err != nil {
Expand Down Expand Up @@ -210,7 +221,7 @@ func (b *Builder) submitBellatrixBlock(block *types.Block, blockValue *big.Int,
return nil
}

func (b *Builder) submitCapellaBlock(block *types.Block, blockValue *big.Int, ordersClosedAt time.Time, sealedAt time.Time, commitedBundles []types.SimulatedBundle, allBundles []types.SimulatedBundle, proposerPubkey boostTypes.PublicKey, vd ValidatorData, attrs *types.BuilderPayloadAttributes) error {
func (b *Builder) submitCapellaBlock(block *types.Block, blockValue *big.Int, ordersClosedAt, sealedAt time.Time, commitedBundles, allBundles []types.SimulatedBundle, proposerPubkey boostTypes.PublicKey, vd ValidatorData, attrs *types.BuilderPayloadAttributes) error {
executableData := engine.BlockToExecutableData(block, blockValue)
payload, err := executableDataToCapellaExecutionPayload(executableData.ExecutionPayload)
if err != nil {
Expand Down Expand Up @@ -371,7 +382,7 @@ func (b *Builder) runBuildingJob(slotCtx context.Context, proposerPubkey boostTy
go runResubmitLoop(ctx, b.limiter, queueSignal, submitBestBlock)

// Populates queue with submissions that increase block profit
blockHook := func(block *types.Block, blockValue *big.Int, ordersCloseTime time.Time, commitedBundles []types.SimulatedBundle, allBundles []types.SimulatedBundle) {
blockHook := func(block *types.Block, blockValue *big.Int, ordersCloseTime time.Time, commitedBundles, allBundles []types.SimulatedBundle) {
if ctx.Err() != nil {
return
}
Expand Down
14 changes: 12 additions & 2 deletions builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,18 @@ func TestOnPayloadAttributes(t *testing.T) {
}

testEthService := &testEthereumService{synced: true, testExecutableData: testExecutableData, testBlock: testBlock, testBlockValue: big.NewInt(10)}

builder := NewBuilder(sk, flashbotsextra.NilDbService{}, &testRelay, bDomain, testEthService, false, false, nil, &testBeacon)
builderArgs := BuilderArgs{
sk: sk,
ds: flashbotsextra.NilDbService{},
relay: &testRelay,
builderSigningDomain: bDomain,
eth: testEthService,
dryRun: false,
ignoreLatePayloadAttributes: false,
validator: nil,
beaconClient: &testBeacon,
}
builder := NewBuilder(builderArgs)
builder.Start()
defer builder.Stop()

Expand Down
9 changes: 9 additions & 0 deletions builder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Config struct {
RemoteRelayEndpoint string `toml:",omitempty"`
SecondaryRemoteRelayEndpoints []string `toml:",omitempty"`
ValidationBlocklist string `toml:",omitempty"`
EnableCancellations bool `toml:",omitempty"`
}

// DefaultConfig is the default config for the builder.
Expand All @@ -41,4 +42,12 @@ var DefaultConfig = Config{
RemoteRelayEndpoint: "",
SecondaryRemoteRelayEndpoints: nil,
ValidationBlocklist: "",
EnableCancellations: false,
}

// RelayConfig is the config for a single remote relay.
type RelayConfig struct {
avalonche marked this conversation as resolved.
Show resolved Hide resolved
Endpoint string
SszEnabled bool
GzipEnabled bool
}
5 changes: 3 additions & 2 deletions builder/eth_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func startEthService(t *testing.T, genesis *core.Genesis, blocks []*types.Block)
ListenAddr: "0.0.0.0:0",
NoDiscovery: true,
MaxPeers: 25,
}})
},
})
if err != nil {
t.Fatal("can't create node:", err)
}
Expand Down Expand Up @@ -93,7 +94,7 @@ func TestBuildBlock(t *testing.T) {
service := NewEthereumService(ethservice)
service.eth.APIBackend.Miner().SetEtherbase(common.Address{0x05, 0x11})

err := service.BuildBlock(testPayloadAttributes, func(block *types.Block, blockValue *big.Int, _ time.Time, _ []types.SimulatedBundle, _ []types.SimulatedBundle) {
err := service.BuildBlock(testPayloadAttributes, func(block *types.Block, blockValue *big.Int, _ time.Time, _, _ []types.SimulatedBundle) {
executableData := engine.BlockToExecutableData(block, blockValue)
require.Equal(t, common.Address{0x05, 0x11}, executableData.ExecutionPayload.FeeRecipient)
require.Equal(t, common.Hash{0x05, 0x10}, executableData.ExecutionPayload.Random)
Expand Down
9 changes: 7 additions & 2 deletions builder/local_relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type LocalRelay struct {
fd ForkData
}

func NewLocalRelay(sk *bls.SecretKey, beaconClient IBeaconClient, builderSigningDomain boostTypes.Domain, proposerSigningDomain boostTypes.Domain, fd ForkData, enableBeaconChecks bool) *LocalRelay {
func NewLocalRelay(sk *bls.SecretKey, beaconClient IBeaconClient, builderSigningDomain, proposerSigningDomain boostTypes.Domain, fd ForkData, enableBeaconChecks bool) *LocalRelay {
pkBytes := bls.PublicKeyFromSecretKey(sk).Compress()
pk := boostTypes.PublicKey{}
pk.FromSlice(pkBytes)
Expand Down Expand Up @@ -105,6 +105,11 @@ func (r *LocalRelay) SubmitBlockCapella(msg *capella.SubmitBlockRequest, _ Valid
return r.submitBlockCapella(msg)
}

func (r *LocalRelay) Config() RelayConfig {
avalonche marked this conversation as resolved.
Show resolved Hide resolved
// local relay does not need config as it is submitting to its own internal endpoint
return RelayConfig{}
}

// TODO: local relay support for capella
func (r *LocalRelay) submitBlockCapella(msg *capella.SubmitBlockRequest) error {
return nil
Expand Down Expand Up @@ -406,6 +411,6 @@ func (r *LocalRelay) handleStatus(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
}

func ExecutionPayloadHeaderEqual(l *boostTypes.ExecutionPayloadHeader, r *boostTypes.ExecutionPayloadHeader) bool {
func ExecutionPayloadHeaderEqual(l, r *boostTypes.ExecutionPayloadHeader) bool {
return l.ParentHash == r.ParentHash && l.FeeRecipient == r.FeeRecipient && l.StateRoot == r.StateRoot && l.ReceiptsRoot == r.ReceiptsRoot && l.LogsBloom == r.LogsBloom && l.Random == r.Random && l.BlockNumber == r.BlockNumber && l.GasLimit == r.GasLimit && l.GasUsed == r.GasUsed && l.Timestamp == r.Timestamp && l.BaseFeePerGas == r.BaseFeePerGas && bytes.Equal(l.ExtraData, r.ExtraData) && l.BlockHash == r.BlockHash && l.TransactionsRoot == r.TransactionsRoot
}
18 changes: 14 additions & 4 deletions builder/local_relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"testing"
"time"

"golang.org/x/time/rate"

"github.com/ethereum/go-ethereum/beacon/engine"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand All @@ -21,6 +19,7 @@ import (
"github.com/flashbots/go-boost-utils/bls"
boostTypes "github.com/flashbots/go-boost-utils/types"
"github.com/stretchr/testify/require"
"golang.org/x/time/rate"
)

func newTestBackend(t *testing.T, forkchoiceData *engine.ExecutableData, block *types.Block, blockValue *big.Int) (*Builder, *LocalRelay, *ValidatorPrivateData) {
Expand All @@ -32,15 +31,26 @@ func newTestBackend(t *testing.T, forkchoiceData *engine.ExecutableData, block *
beaconClient := &testBeaconClient{validator: validator}
localRelay := NewLocalRelay(sk, beaconClient, bDomain, cDomain, ForkData{}, true)
ethService := &testEthereumService{synced: true, testExecutableData: forkchoiceData, testBlock: block, testBlockValue: blockValue}
backend := NewBuilder(sk, flashbotsextra.NilDbService{}, localRelay, bDomain, ethService, false, false, nil, beaconClient)
builderArgs := BuilderArgs{
sk: sk,
ds: flashbotsextra.NilDbService{},
relay: localRelay,
builderSigningDomain: bDomain,
eth: ethService,
dryRun: false,
ignoreLatePayloadAttributes: false,
validator: nil,
beaconClient: beaconClient,
}
backend := NewBuilder(builderArgs)
// service := NewService("127.0.0.1:31545", backend)

backend.limiter = rate.NewLimiter(rate.Inf, 0)

return backend, localRelay, validator
}

func testRequest(t *testing.T, localRelay *LocalRelay, method string, path string, payload any) *httptest.ResponseRecorder {
func testRequest(t *testing.T, localRelay *LocalRelay, method, path string, payload any) *httptest.ResponseRecorder {
var req *http.Request
var err error

Expand Down
Loading