Skip to content

Commit

Permalink
Immediate last payloads (#57)
Browse files Browse the repository at this point in the history
* first pass

* Feature/immediate last payloads 3 (#56)

* add auctioneer to datastore

tests fail

* fix main

* fix max bid calculation

* fix max bid calculation

* move auctioneer from datastore to relay

* refactored main

* fix max profit calculation

* fix max block calculation

add slot check on block submission

* fix max block calculation

* fix tests

* fix max block calculation

* fix max block calculation

* add log for auctioneer not having block

* add log for auctioneer not having block

* change dabug to trace

* check slot is not older than current slot

* add logs to auctioneer

* refactor acutioneer

* changed debug to trace log level

* fix max profit calculation

* fix max profit calculation

* add block to auctioneer, after writing payload to storage

* put back memory check on GetMaxProfitHeader

* fix tests

* remove log from datastore

* removed log from auctioner

* add tiomer to auctioneer

* dd slot -1 to auctioneer

* revert logs test

Co-authored-by: Aratz M. Lasa <[email protected]>
Co-authored-by: Aratz M. Lasa <[email protected]>
  • Loading branch information
3 people authored Dec 12, 2022
1 parent d888e78 commit 9e63784
Show file tree
Hide file tree
Showing 16 changed files with 454 additions and 227 deletions.
15 changes: 13 additions & 2 deletions cmd/dreamboat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/blocknative/dreamboat/metrics"
pkg "github.com/blocknative/dreamboat/pkg"
"github.com/blocknative/dreamboat/pkg/api"
"github.com/blocknative/dreamboat/pkg/auction"
"github.com/blocknative/dreamboat/pkg/datastore"
relay "github.com/blocknative/dreamboat/pkg/relay"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand Down Expand Up @@ -153,6 +154,12 @@ var flags = []cli.Flag{
Value: time.Minute * 10,
EnvVars: []string{"RELAY_HEADER_MEMORY_PURGE_INTERVAL"},
},
&cli.IntFlag{
Name: "relay-payload-cache-size",
Usage: "number of payloads to cache for fast in-memory reads",
Value: 1_000,
EnvVars: []string{"RELAY_PAYLOAD_CACHE_SIZE"},
},
}

var (
Expand Down Expand Up @@ -271,7 +278,10 @@ func run() cli.ActionFunc {
hc := datastore.NewHeaderController(config.RelayHeaderMemorySlotLag, config.RelayHeaderMemorySlotTimeLag)
hc.AttachMetrics(m)

ds := datastore.NewDatastore(&datastore.TTLDatastoreBatcher{storage}, storage.DB, hc)
ds, err := datastore.NewDatastore(&datastore.TTLDatastoreBatcher{storage}, storage.DB, hc, c.Int("relay-payload-cache-size")) // TODO: make cache size parameter
if err != nil {
return fmt.Errorf("fail to create datastore: %w", err)
}
if err = datastore.InitDatastoreMetrics(m); err != nil {
return err
}
Expand All @@ -288,13 +298,14 @@ func run() cli.ActionFunc {

go regMgr.RunCleanup(uint64(config.TTL), time.Hour)

auctioneer := auction.NewAuctioneer()
r := relay.NewRelay(config.Log, relay.RelayConfig{
BuilderSigningDomain: domainBuilder,
ProposerSigningDomain: domainBeaconProposer,
PubKey: config.PubKey,
SecretKey: config.SecretKey,
TTL: config.TTL,
}, as, ds, regMgr)
}, as, ds, regMgr, auctioneer)
r.AttachMetrics(m)

service := pkg.NewService(config.Log, config, ds, r, as)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ module github.com/blocknative/dreamboat
go 1.18

require (
github.com/dgraph-io/badger/v2 v2.2007.3
github.com/ethereum/go-ethereum v1.10.21
github.com/golang/mock v1.6.0
github.com/google/uuid v1.3.0
github.com/hashicorp/golang-lru/v2 v2.0.1
github.com/ipfs/go-datastore v0.6.0
github.com/ipfs/go-ds-badger2 v0.1.3
github.com/lthibault/log v1.2.2
Expand All @@ -17,7 +19,6 @@ require (
github.com/urfave/cli/v2 v2.10.2
go.uber.org/atomic v1.9.0
golang.org/x/exp v0.0.0-20220907003533-145caa8ea1d0
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f
)

require (
Expand All @@ -32,7 +33,6 @@ require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/dgraph-io/badger/v2 v2.2007.3 // indirect
github.com/dgraph-io/ristretto v0.0.4-0.20210318174700-74754f61e018 // indirect
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru/v2 v2.0.1 h1:5pv5N1lT1fjLg2VQ5KWc7kmucp2x/kvFOnxuVTqZ6x4=
github.com/hashicorp/golang-lru/v2 v2.0.1/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
Expand Down Expand Up @@ -468,8 +470,6 @@ golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f h1:Ax0t5p6N38Ga0dThY21weqDEyz2oklo4IvDkpigvkD8=
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
43 changes: 22 additions & 21 deletions pkg/api/mocks/mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 81 additions & 0 deletions pkg/auction/auctioneer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package auction

import (
"sync"

"github.com/blocknative/dreamboat/pkg/structs"
"github.com/flashbots/go-boost-utils/types"
)

type Auctioneer struct {
auctions [3]*Auction
}

type Auction struct {
mu sync.RWMutex
maxProfit *structs.CompleteBlockstruct
latestBlockByBuilder map[types.PublicKey]*structs.CompleteBlockstruct
}

func NewAuctioneer() *Auctioneer {
return &Auctioneer{
auctions: [3]*Auction{
{latestBlockByBuilder: make(map[types.PublicKey]*structs.CompleteBlockstruct)}, // slot - 1
{latestBlockByBuilder: make(map[types.PublicKey]*structs.CompleteBlockstruct)}, // slot
{latestBlockByBuilder: make(map[types.PublicKey]*structs.CompleteBlockstruct)}, // slot + 1
},
}
}

func (a *Auctioneer) AddBlock(block *structs.CompleteBlockstruct) bool {
auction := a.auctions[block.Header.Trace.Slot%3]

auction.mu.Lock()
defer auction.mu.Unlock()

auction.latestBlockByBuilder[block.Payload.Trace.Message.BuilderPubkey] = block

// always set new value and bigger slot
if auction.maxProfit == nil || auction.maxProfit.Header.Trace.Slot < block.Header.Trace.Slot {
auction.maxProfit = block
return true
}

// always discard submissions lower than latest slot
if auction.maxProfit.Header.Trace.Slot > block.Header.Trace.Slot {
return false
}

// accept bigger bid
if auction.maxProfit.Header.Trace.Value.Cmp(&block.Header.Trace.Value) <= 0 {
auction.maxProfit = block
return true
}

// reassign biggest for resubmission from the same builder with lower bid
if auction.maxProfit.Header.Trace.BuilderPubkey == block.Header.Trace.BuilderPubkey &&
auction.maxProfit.Header.Trace.Value.Cmp(&block.Header.Trace.Value) > 0 {
auction.maxProfit = block
for _, b := range auction.latestBlockByBuilder {
if auction.maxProfit.Header.Trace.Slot == b.Header.Trace.Slot && // Only check the current slot
auction.maxProfit.Header.Trace.Value.Cmp(&b.Header.Trace.Value) <= 0 {
auction.maxProfit = b
}
}
}

return block == auction.maxProfit
}

func (a *Auctioneer) MaxProfitBlock(slot structs.Slot) (*structs.CompleteBlockstruct, bool) {
auction := a.auctions[slot%3]

auction.mu.RLock()
defer auction.mu.RUnlock()

if auction.maxProfit != nil && structs.Slot(auction.maxProfit.Header.Trace.Slot) == slot {
return auction.maxProfit, true
}

return nil, false
}
2 changes: 1 addition & 1 deletion pkg/beacon.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func (b *beaconClient) queryBeacon(u *url.URL, method string, dst any) error {
logger.
WithField("processingTimeMs", time.Since(timeStart).Milliseconds()).
WithField("bytesAmount", len(bodyBytes)).
Debug("beacon queried")
Trace("beacon queried")

return nil
}
Expand Down
Loading

0 comments on commit 9e63784

Please sign in to comment.