Skip to content

Commit

Permalink
Only submit blocks that improve profit (ethereum#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruteri authored and avalonche committed Feb 6, 2023
1 parent 1e8352e commit 5d394c9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
29 changes: 26 additions & 3 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package builder

import (
"errors"
"math/big"
_ "os"
"sync"
"time"

"github.com/ethereum/go-ethereum/common/hexutil"
Expand Down Expand Up @@ -47,6 +49,10 @@ type Builder struct {
builderSecretKey *bls.SecretKey
builderPublicKey boostTypes.PublicKey
builderSigningDomain boostTypes.Domain

bestMu sync.Mutex
bestAttrs BuilderPayloadAttributes
bestBlockProfit *big.Int
}

func NewBuilder(sk *bls.SecretKey, bc IBeaconClient, relay IRelay, builderSigningDomain boostTypes.Domain, eth IEthereumService) *Builder {
Expand All @@ -63,10 +69,25 @@ func NewBuilder(sk *bls.SecretKey, bc IBeaconClient, relay IRelay, builderSignin
builderPublicKey: pk,

builderSigningDomain: builderSigningDomain,
bestBlockProfit: big.NewInt(0),
}
}

func (b *Builder) onSealedBlock(executableData *beacon.ExecutableDataV1, block *types.Block, proposerPubkey boostTypes.PublicKey, proposerFeeRecipient boostTypes.Address, slot uint64) error {
func (b *Builder) onSealedBlock(executableData *beacon.ExecutableDataV1, block *types.Block, proposerPubkey boostTypes.PublicKey, proposerFeeRecipient boostTypes.Address, attrs *BuilderPayloadAttributes) error {
b.bestMu.Lock()
defer b.bestMu.Unlock()

// Do not submit blocks that don't improve the profit
if b.bestAttrs != *attrs {
b.bestAttrs = *attrs
b.bestBlockProfit.SetInt64(0)
} else {
if block.Profit.Cmp(b.bestBlockProfit) <= 0 {
log.Info("Ignoring block that is not improving the profit")
return nil
}
}

payload, err := executableDataToExecutionPayload(executableData)
if err != nil {
log.Error("could not format execution payload", "err", err)
Expand All @@ -81,7 +102,7 @@ func (b *Builder) onSealedBlock(executableData *beacon.ExecutableDataV1, block *
}

blockBidMsg := boostTypes.BidTrace{
Slot: slot,
Slot: attrs.Slot,
ParentHash: payload.ParentHash,
BlockHash: payload.BlockHash,
BuilderPubkey: b.builderPublicKey,
Expand Down Expand Up @@ -110,6 +131,8 @@ func (b *Builder) onSealedBlock(executableData *beacon.ExecutableDataV1, block *
return err
}

b.bestBlockProfit.Set(block.Profit)

return nil
}

Expand Down Expand Up @@ -150,7 +173,7 @@ func (b *Builder) OnPayloadAttribute(attrs *BuilderPayloadAttributes) error {
return errors.New("did not receive the payload")
}

err := b.onSealedBlock(executableData, block, proposerPubkey, vd.FeeRecipient, attrs.Slot)
err := b.onSealedBlock(executableData, block, proposerPubkey, vd.FeeRecipient, attrs)
if err != nil {
log.Error("could not run block hook", "err", err)
return err
Expand Down
10 changes: 7 additions & 3 deletions builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,13 @@ func TestOnPayloadAttributes(t *testing.T) {

require.Equal(t, uint64(25), testRelay.requestedSlot)

// Clear the submitted message and check that the job will be ran again and a new message will be submitted
// Clear the submitted message and check that the job will be ran again and but a new message will not be submitted since the profit is the same
testRelay.submittedMsg = nil
time.Sleep(2 * time.Second)
time.Sleep(1200 * time.Millisecond)
require.Nil(t, testRelay.submittedMsg)

// Up the profit, expect to get the block
testEthService.testBlock.Profit.SetInt64(11)
time.Sleep(1200 * time.Millisecond)
require.NotNil(t, testRelay.submittedMsg)
require.Equal(t, expectedMessage, *testRelay.submittedMsg.Message)
}

0 comments on commit 5d394c9

Please sign in to comment.