Skip to content

Commit f965da2

Browse files
Ruteriavalonche
authored andcommitted
Only submit blocks that improve profit (ethereum#19)
1 parent 418e7bb commit f965da2

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

builder/builder.go

+26-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package builder
22

33
import (
44
"errors"
5+
"math/big"
56
_ "os"
7+
"sync"
68
"time"
79

810
"github.com/ethereum/go-ethereum/common/hexutil"
@@ -47,6 +49,10 @@ type Builder struct {
4749
builderSecretKey *bls.SecretKey
4850
builderPublicKey boostTypes.PublicKey
4951
builderSigningDomain boostTypes.Domain
52+
53+
bestMu sync.Mutex
54+
bestAttrs BuilderPayloadAttributes
55+
bestBlockProfit *big.Int
5056
}
5157

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

6571
builderSigningDomain: builderSigningDomain,
72+
bestBlockProfit: big.NewInt(0),
6673
}
6774
}
6875

69-
func (b *Builder) onSealedBlock(executableData *beacon.ExecutableDataV1, block *types.Block, proposerPubkey boostTypes.PublicKey, proposerFeeRecipient boostTypes.Address, slot uint64) error {
76+
func (b *Builder) onSealedBlock(executableData *beacon.ExecutableDataV1, block *types.Block, proposerPubkey boostTypes.PublicKey, proposerFeeRecipient boostTypes.Address, attrs *BuilderPayloadAttributes) error {
77+
b.bestMu.Lock()
78+
defer b.bestMu.Unlock()
79+
80+
// Do not submit blocks that don't improve the profit
81+
if b.bestAttrs != *attrs {
82+
b.bestAttrs = *attrs
83+
b.bestBlockProfit.SetInt64(0)
84+
} else {
85+
if block.Profit.Cmp(b.bestBlockProfit) <= 0 {
86+
log.Info("Ignoring block that is not improving the profit")
87+
return nil
88+
}
89+
}
90+
7091
payload, err := executableDataToExecutionPayload(executableData)
7192
if err != nil {
7293
log.Error("could not format execution payload", "err", err)
@@ -81,7 +102,7 @@ func (b *Builder) onSealedBlock(executableData *beacon.ExecutableDataV1, block *
81102
}
82103

83104
blockBidMsg := boostTypes.BidTrace{
84-
Slot: slot,
105+
Slot: attrs.Slot,
85106
ParentHash: payload.ParentHash,
86107
BlockHash: payload.BlockHash,
87108
BuilderPubkey: b.builderPublicKey,
@@ -110,6 +131,8 @@ func (b *Builder) onSealedBlock(executableData *beacon.ExecutableDataV1, block *
110131
return err
111132
}
112133

134+
b.bestBlockProfit.Set(block.Profit)
135+
113136
return nil
114137
}
115138

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

153-
err := b.onSealedBlock(executableData, block, proposerPubkey, vd.FeeRecipient, attrs.Slot)
176+
err := b.onSealedBlock(executableData, block, proposerPubkey, vd.FeeRecipient, attrs)
154177
if err != nil {
155178
log.Error("could not run block hook", "err", err)
156179
return err

builder/builder_test.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,13 @@ func TestOnPayloadAttributes(t *testing.T) {
122122

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

125-
// Clear the submitted message and check that the job will be ran again and a new message will be submitted
125+
// 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
126126
testRelay.submittedMsg = nil
127-
time.Sleep(2 * time.Second)
127+
time.Sleep(1200 * time.Millisecond)
128+
require.Nil(t, testRelay.submittedMsg)
129+
130+
// Up the profit, expect to get the block
131+
testEthService.testBlock.Profit.SetInt64(11)
132+
time.Sleep(1200 * time.Millisecond)
128133
require.NotNil(t, testRelay.submittedMsg)
129-
require.Equal(t, expectedMessage, *testRelay.submittedMsg.Message)
130134
}

0 commit comments

Comments
 (0)