Skip to content

Commit fdf4dd2

Browse files
jparyaniRuteri
authored andcommitted
Flashbots change up to v0.3
1 parent 7a0c19f commit fdf4dd2

27 files changed

+4929
-392
lines changed

.github/workflows/go.yml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
name: Go
2+
3+
on:
4+
push:
5+
pull_request:
6+
branches: [ master ]
7+
8+
jobs:
9+
10+
build:
11+
name: Build
12+
runs-on: ubuntu-latest
13+
steps:
14+
15+
- name: Set up Go 1.x
16+
uses: actions/setup-go@v2
17+
with:
18+
go-version: ^1.13
19+
id: go
20+
21+
- name: Check out code into the Go module directory
22+
uses: actions/checkout@v2
23+
24+
- name: Test
25+
run: go test ./core ./miner/... ./internal/ethapi/... ./les/...
26+
27+
- name: Build
28+
run: make geth
29+
30+
e2e:
31+
name: End to End
32+
runs-on: ubuntu-latest
33+
steps:
34+
35+
- name: Set up Go 1.x
36+
uses: actions/setup-go@v2
37+
with:
38+
go-version: ^1.13
39+
id: go
40+
41+
- name: Use Node.js 12.x
42+
uses: actions/setup-node@v1
43+
with:
44+
node-version: 12.x
45+
46+
- name: Check out code into the Go module directory
47+
uses: actions/checkout@v2
48+
49+
- name: Build
50+
run: make geth
51+
52+
- name: Check out the e2e code repo
53+
uses: actions/checkout@v2
54+
with:
55+
repository: flashbots/mev-geth-demo
56+
path: e2e
57+
58+
- run: cd e2e && yarn install
59+
- run: |
60+
cd e2e
61+
GETH=`pwd`/../build/bin/geth ./run.sh &
62+
sleep 15
63+
yarn run demo-simple
64+
yarn run demo-contract

README.md

Lines changed: 17 additions & 351 deletions
Large diffs are not rendered by default.

README.original.md

Lines changed: 359 additions & 0 deletions
Large diffs are not rendered by default.

cmd/geth/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ var (
130130
utils.MinerExtraDataFlag,
131131
utils.MinerRecommitIntervalFlag,
132132
utils.MinerNoVerifyFlag,
133+
utils.MinerMaxMergedBundles,
133134
utils.NATFlag,
134135
utils.NoDiscoverFlag,
135136
utils.DiscoveryV5Flag,

cmd/geth/usage.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ var AppHelpFlagGroups = []flags.FlagGroup{
189189
utils.MinerExtraDataFlag,
190190
utils.MinerRecommitIntervalFlag,
191191
utils.MinerNoVerifyFlag,
192+
utils.MinerMaxMergedBundles,
192193
},
193194
},
194195
{

cmd/utils/flags.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,11 @@ var (
469469
Usage: "Time interval to recreate the block being mined",
470470
Value: ethconfig.Defaults.Miner.Recommit,
471471
}
472+
MinerMaxMergedBundles = cli.IntFlag{
473+
Name: "miner.maxmergedbundles",
474+
Usage: "flashbots - The maximum amount of bundles to merge. The miner will run this many workers in parallel to calculate if the full block is more profitable with these additional bundles.",
475+
Value: 3,
476+
}
472477
MinerNoVerifyFlag = cli.BoolFlag{
473478
Name: "miner.noverify",
474479
Usage: "Disable remote sealing verification",
@@ -1401,6 +1406,8 @@ func setMiner(ctx *cli.Context, cfg *miner.Config) {
14011406
if ctx.GlobalIsSet(LegacyMinerGasTargetFlag.Name) {
14021407
log.Warn("The generic --miner.gastarget flag is deprecated and will be removed in the future!")
14031408
}
1409+
1410+
cfg.MaxMergedBundles = ctx.GlobalInt(MinerMaxMergedBundles.Name)
14041411
}
14051412

14061413
func setWhitelist(ctx *cli.Context, cfg *ethconfig.Config) {

core/tx_pool.go

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,12 @@ type TxPool struct {
251251
locals *accountSet // Set of local transaction to exempt from eviction rules
252252
journal *txJournal // Journal of local transaction to back up to disk
253253

254-
pending map[common.Address]*txList // All currently processable transactions
255-
queue map[common.Address]*txList // Queued but non-processable transactions
256-
beats map[common.Address]time.Time // Last heartbeat from each known account
257-
all *txLookup // All transactions to allow lookups
258-
priced *txPricedList // All transactions sorted by price
254+
pending map[common.Address]*txList // All currently processable transactions
255+
queue map[common.Address]*txList // Queued but non-processable transactions
256+
beats map[common.Address]time.Time // Last heartbeat from each known account
257+
mevBundles []types.MevBundle
258+
all *txLookup // All transactions to allow lookups
259+
priced *txPricedList // All transactions sorted by price
259260

260261
chainHeadCh chan ChainHeadEvent
261262
chainHeadSub event.Subscription
@@ -557,6 +558,59 @@ func (pool *TxPool) Pending(enforceTips bool) map[common.Address]types.Transacti
557558
return pending
558559
}
559560

561+
/// AllMevBundles returns all the MEV Bundles currently in the pool
562+
func (pool *TxPool) AllMevBundles() []types.MevBundle {
563+
return pool.mevBundles
564+
}
565+
566+
// MevBundles returns a list of bundles valid for the given blockNumber/blockTimestamp
567+
// also prunes bundles that are outdated
568+
func (pool *TxPool) MevBundles(blockNumber *big.Int, blockTimestamp uint64) ([]types.MevBundle, error) {
569+
pool.mu.Lock()
570+
defer pool.mu.Unlock()
571+
572+
// returned values
573+
var ret []types.MevBundle
574+
// rolled over values
575+
var bundles []types.MevBundle
576+
577+
for _, bundle := range pool.mevBundles {
578+
// Prune outdated bundles
579+
if (bundle.MaxTimestamp != 0 && blockTimestamp > bundle.MaxTimestamp) || blockNumber.Cmp(bundle.BlockNumber) > 0 {
580+
continue
581+
}
582+
583+
// Roll over future bundles
584+
if (bundle.MinTimestamp != 0 && blockTimestamp < bundle.MinTimestamp) || blockNumber.Cmp(bundle.BlockNumber) < 0 {
585+
bundles = append(bundles, bundle)
586+
continue
587+
}
588+
589+
// return the ones which are in time
590+
ret = append(ret, bundle)
591+
// keep the bundles around internally until they need to be pruned
592+
bundles = append(bundles, bundle)
593+
}
594+
595+
pool.mevBundles = bundles
596+
return ret, nil
597+
}
598+
599+
// AddMevBundle adds a mev bundle to the pool
600+
func (pool *TxPool) AddMevBundle(txs types.Transactions, blockNumber *big.Int, minTimestamp, maxTimestamp uint64, revertingTxHashes []common.Hash) error {
601+
pool.mu.Lock()
602+
defer pool.mu.Unlock()
603+
604+
pool.mevBundles = append(pool.mevBundles, types.MevBundle{
605+
Txs: txs,
606+
BlockNumber: blockNumber,
607+
MinTimestamp: minTimestamp,
608+
MaxTimestamp: maxTimestamp,
609+
RevertingTxHashes: revertingTxHashes,
610+
})
611+
return nil
612+
}
613+
560614
// Locals retrieves the accounts currently considered local by the pool.
561615
func (pool *TxPool) Locals() []common.Address {
562616
pool.mu.Lock()

core/tx_pool_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2561,3 +2561,13 @@ func BenchmarkPoolMultiAccountBatchInsert(b *testing.B) {
25612561
pool.AddRemotesSync([]*types.Transaction{tx})
25622562
}
25632563
}
2564+
2565+
func checkBundles(t *testing.T, pool *TxPool, block int64, timestamp uint64, expectedRes int, expectedRemaining int) {
2566+
res, _ := pool.MevBundles(big.NewInt(block), timestamp)
2567+
if len(res) != expectedRes {
2568+
t.Fatalf("expected returned bundles did not match got %d, expected %d", len(res), expectedRes)
2569+
}
2570+
if len(pool.mevBundles) != expectedRemaining {
2571+
t.Fatalf("expected remaining bundles did not match got %d, expected %d", len(pool.mevBundles), expectedRemaining)
2572+
}
2573+
}

core/types/transaction.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,3 +635,11 @@ func copyAddressPtr(a *common.Address) *common.Address {
635635
cpy := *a
636636
return &cpy
637637
}
638+
639+
type MevBundle struct {
640+
Txs Transactions
641+
BlockNumber *big.Int
642+
MinTimestamp uint64
643+
MaxTimestamp uint64
644+
RevertingTxHashes []common.Hash
645+
}

eth/api_backend.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@ func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction)
239239
return b.eth.txPool.AddLocal(signedTx)
240240
}
241241

242+
func (b *EthAPIBackend) SendBundle(ctx context.Context, txs types.Transactions, blockNumber rpc.BlockNumber, minTimestamp uint64, maxTimestamp uint64, revertingTxHashes []common.Hash) error {
243+
return b.eth.txPool.AddMevBundle(txs, big.NewInt(blockNumber.Int64()), minTimestamp, maxTimestamp, revertingTxHashes)
244+
}
245+
242246
func (b *EthAPIBackend) GetPoolTransactions() (types.Transactions, error) {
243247
pending := b.eth.txPool.Pending(false)
244248
var txs types.Transactions

0 commit comments

Comments
 (0)