@@ -2,7 +2,9 @@ package builder
22
33import (
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
5258func 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
0 commit comments