Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.

Commit 78be0a7

Browse files
Ruteriavalonche
authored andcommitted
Improvements from greedy improve algo (#41)
* Backport improvements to the builder from incremental improvements * Make linter happy
1 parent e4dfa84 commit 78be0a7

20 files changed

+276
-96
lines changed

builder/builder.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package builder
33
import (
44
"context"
55
"errors"
6-
blockvalidation "github.com/ethereum/go-ethereum/eth/block-validation"
7-
"golang.org/x/time/rate"
86
"math/big"
97
_ "os"
108
"sync"
119
"time"
1210

11+
blockvalidation "github.com/ethereum/go-ethereum/eth/block-validation"
12+
"golang.org/x/time/rate"
13+
1314
"github.com/ethereum/go-ethereum/common/hexutil"
1415
"github.com/ethereum/go-ethereum/core/beacon"
1516
"github.com/ethereum/go-ethereum/core/types"

builder/local_relay_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7-
"golang.org/x/time/rate"
87
"math/big"
98
"net/http"
109
"net/http/httptest"
1110
"testing"
1211
"time"
1312

13+
"golang.org/x/time/rate"
14+
1415
"github.com/ethereum/go-ethereum/common"
1516
"github.com/ethereum/go-ethereum/common/hexutil"
1617
"github.com/ethereum/go-ethereum/core/beacon"

builder/relay.go

-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ func (r *testRelay) GetValidatorForSlot(nextSlot uint64) (ValidatorData, error)
2929
r.requestedSlot = nextSlot
3030
return r.validator, nil
3131
}
32-
func (r *testRelay) handleRegisterValidator(w http.ResponseWriter, req *http.Request) {
33-
}
3432

3533
type RemoteRelay struct {
3634
endpoint string

builder/resubmit_utils.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package builder
22

33
import (
44
"context"
5+
"time"
6+
57
"github.com/ethereum/go-ethereum/log"
68
"golang.org/x/time/rate"
7-
"time"
89
)
910

1011
// runResubmitLoop checks for update signal and calls submit respecting provided rate limiter and context

builder/resubmit_utils_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package builder
22

33
import (
44
"context"
5-
"golang.org/x/time/rate"
65
"math/rand"
76
"sort"
87
"sync"
98
"testing"
109
"time"
10+
11+
"golang.org/x/time/rate"
1112
)
1213

1314
type submission struct {

builder/service.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package builder
33
import (
44
"errors"
55
"fmt"
6-
blockvalidation "github.com/ethereum/go-ethereum/eth/block-validation"
76
"net/http"
87
"os"
98

9+
blockvalidation "github.com/ethereum/go-ethereum/eth/block-validation"
10+
1011
"github.com/ethereum/go-ethereum/common"
1112
"github.com/ethereum/go-ethereum/common/hexutil"
1213
"github.com/ethereum/go-ethereum/core/types"

cmd/utils/flags.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -1740,14 +1740,11 @@ func setMiner(ctx *cli.Context, cfg *miner.Config) {
17401740
cfg.GasPrice = flags.GlobalBig(ctx, MinerGasPriceFlag.Name)
17411741
}
17421742
if ctx.IsSet(MinerAlgoTypeFlag.Name) {
1743-
switch ctx.String(MinerAlgoTypeFlag.Name) {
1744-
case "greedy":
1745-
cfg.AlgoType = miner.ALGO_GREEDY
1746-
case "mev-geth":
1747-
cfg.AlgoType = miner.ALGO_MEV_GETH
1748-
default:
1743+
algoType, err := miner.AlgoTypeFlagToEnum(ctx.String(MinerAlgoTypeFlag.Name))
1744+
if err != nil {
17491745
Fatalf("Invalid algo in --miner.algotype: %s", ctx.String(MinerAlgoTypeFlag.Name))
17501746
}
1747+
cfg.AlgoType = algoType
17511748
}
17521749
if ctx.IsSet(MinerRecommitIntervalFlag.Name) {
17531750
cfg.Recommit = ctx.Duration(MinerRecommitIntervalFlag.Name)

core/types/transaction.go

+13
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,19 @@ func NewTransactionsByPriceAndNonce(signer Signer, txs map[common.Address]Transa
585585
}
586586
}
587587

588+
func (t *TransactionsByPriceAndNonce) DeepCopy() *TransactionsByPriceAndNonce {
589+
newT := &TransactionsByPriceAndNonce{
590+
txs: make(map[common.Address]Transactions),
591+
heads: append(TxByPriceAndTime{}, t.heads...),
592+
signer: t.signer,
593+
baseFee: new(big.Int).Set(t.baseFee),
594+
}
595+
for k, v := range t.txs {
596+
newT.txs[k] = v
597+
}
598+
return newT
599+
}
600+
588601
// Peek returns the next transaction by price.
589602
func (t *TransactionsByPriceAndNonce) Peek() *TxWithMinerFee {
590603
if len(t.heads) == 0 {

eth/block-validation/api_test.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ var (
4141
testValidatorKey, _ = crypto.HexToECDSA("28c3cd61b687fdd03488e167a5d84f50269df2a4c29a2cfb1390903aa775c5d0")
4242
testValidatorAddr = crypto.PubkeyToAddress(testValidatorKey.PublicKey)
4343

44-
testMinerKey, _ = crypto.HexToECDSA("28c3cd61b687fdd03488e167a5d84f50269df2a4c29a2cfb1390903aa775c5d0")
45-
testMinerAddr = crypto.PubkeyToAddress(testValidatorKey.PublicKey)
46-
4744
testBalance = big.NewInt(2e18)
4845
)
4946

@@ -57,7 +54,7 @@ func TestValidateBuilderSubmissionV1(t *testing.T) {
5754
api := NewBlockValidationAPI(ethservice, nil)
5855
parent := preMergeBlocks[len(preMergeBlocks)-1]
5956

60-
api.eth.APIBackend.Miner().SetEtherbase(testMinerAddr)
57+
api.eth.APIBackend.Miner().SetEtherbase(testValidatorAddr)
6158

6259
// This EVM code generates a log when the contract is created.
6360
logCode := common.Hex2Bytes("60606040525b7f24ec1d3ff24c2f6ff210738839dbc339cd45a5294d85c79361016243157aae7b60405180905060405180910390a15b600a8060416000396000f360606040526008565b00")
@@ -142,7 +139,7 @@ func TestValidateBuilderSubmissionV1(t *testing.T) {
142139
invalidPayload.LogsBloom = boostTypes.Bloom{}
143140
copy(invalidPayload.ReceiptsRoot[:], hexutil.MustDecode("0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")[:32])
144141
blockRequest.ExecutionPayload = invalidPayload
145-
copy(blockRequest.Message.BlockHash[:], hexutil.MustDecode("0x272872d14b2a8a0454e747ed472d82d8d5ce342cfafd65fa7b77aa6de1c061d4")[:32])
142+
copy(blockRequest.Message.BlockHash[:], hexutil.MustDecode("0x2ff468dee2e05f1f58744d5496f3ab22fdc23c8141f86f907b4b0f2c8e22afc4")[:32])
146143
require.ErrorContains(t, api.ValidateBuilderSubmissionV1(blockRequest), "could not apply tx 3", "insufficient funds for gas * price + value")
147144
}
148145

eth/tracers/logger/account_touch_tracer.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
package logger
1818

1919
import (
20-
"github.com/ethereum/go-ethereum/common"
21-
"github.com/ethereum/go-ethereum/core/vm"
2220
"math/big"
2321
"time"
22+
23+
"github.com/ethereum/go-ethereum/common"
24+
"github.com/ethereum/go-ethereum/core/vm"
2425
)
2526

2627
type AccountTouchTracer struct {

miner/algo_common.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ var emptyCodeHash = common.HexToHash("c5d2460186f7233c927e7db2dcc703c0e500b653ca
2626

2727
var errInterrupt = errors.New("miner worker interrupted")
2828

29+
type chainData struct {
30+
chainConfig *params.ChainConfig
31+
chain *core.BlockChain
32+
blacklist map[common.Address]struct{}
33+
}
34+
2935
type environmentDiff struct {
3036
baseEnvironment *environment
3137
header *types.Header
@@ -51,7 +57,7 @@ func (e *environmentDiff) copy() *environmentDiff {
5157
gasPool := new(core.GasPool).AddGas(e.gasPool.Gas())
5258

5359
return &environmentDiff{
54-
baseEnvironment: e.baseEnvironment,
60+
baseEnvironment: e.baseEnvironment.copy(),
5561
header: types.CopyHeader(e.header),
5662
gasPool: gasPool,
5763
state: e.state.Copy(),

miner/algo_common_test.go

+34-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import (
44
"crypto/ecdsa"
55
"errors"
66
"fmt"
7-
"github.com/stretchr/testify/require"
87
"math/big"
98
"testing"
109

10+
"github.com/stretchr/testify/require"
11+
1112
mapset "github.com/deckarep/golang-set"
1213
"github.com/ethereum/go-ethereum/common"
1314
"github.com/ethereum/go-ethereum/common/hexutil"
@@ -366,10 +367,8 @@ func TestCommitTxOverGasLimit(t *testing.T) {
366367
t.Fatal("Env diff gas pool is not drained")
367368
}
368369

369-
receipt, i, err = envDiff.commitTx(tx2, chData)
370-
if err == nil {
371-
t.Fatal("committed tx over gas limit")
372-
}
370+
_, _, err = envDiff.commitTx(tx2, chData)
371+
require.Error(t, err, "committed tx over gas limit")
373372
}
374373

375374
func TestErrorBundleCommit(t *testing.T) {
@@ -497,6 +496,36 @@ func TestBlacklist(t *testing.T) {
497496
}
498497
}
499498

499+
func TestGetSealingWorkAlgos(t *testing.T) {
500+
t.Cleanup(func() {
501+
testConfig.AlgoType = ALGO_MEV_GETH
502+
})
503+
504+
for _, algoType := range []AlgoType{ALGO_MEV_GETH, ALGO_GREEDY} {
505+
local := new(params.ChainConfig)
506+
*local = *ethashChainConfig
507+
local.TerminalTotalDifficulty = big.NewInt(0)
508+
testConfig.AlgoType = algoType
509+
testGetSealingWork(t, local, ethash.NewFaker(), true)
510+
}
511+
}
512+
513+
func TestGetSealingWorkAlgosWithProfit(t *testing.T) {
514+
t.Cleanup(func() {
515+
testConfig.AlgoType = ALGO_MEV_GETH
516+
testConfig.BuilderTxSigningKey = nil
517+
})
518+
519+
for _, algoType := range []AlgoType{ALGO_GREEDY} {
520+
var err error
521+
testConfig.BuilderTxSigningKey, err = crypto.GenerateKey()
522+
require.NoError(t, err)
523+
testConfig.AlgoType = algoType
524+
t.Logf("running for %d", algoType)
525+
testBundles(t)
526+
}
527+
}
528+
500529
func TestPayoutTxUtils(t *testing.T) {
501530
availableFunds := big.NewInt(50000000000000000) // 0.05 eth
502531

miner/algo_greedy.go

+11-20
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,6 @@ import (
88
"github.com/ethereum/go-ethereum/params"
99
)
1010

11-
type chainData struct {
12-
chainConfig *params.ChainConfig
13-
chain *core.BlockChain
14-
blacklist map[common.Address]struct{}
15-
}
16-
17-
type IBuilder interface {
18-
buildBlock(simBundles []types.SimulatedBundle, transactions map[common.Address]types.Transactions) (*environment, []types.SimulatedBundle)
19-
}
20-
2111
// / To use it:
2212
// / 1. Copy relevant data from the worker
2313
// / 2. Call buildBlock
@@ -37,14 +27,8 @@ func newGreedyBuilder(chain *core.BlockChain, chainConfig *params.ChainConfig, b
3727
}
3828
}
3929

40-
func (b *greedyBuilder) buildBlock(simBundles []types.SimulatedBundle, transactions map[common.Address]types.Transactions) (*environment, []types.SimulatedBundle) {
41-
42-
env := b.inputEnvironment.copy()
43-
44-
orders := types.NewTransactionsByPriceAndNonce(env.signer, transactions, simBundles, env.header.BaseFee)
45-
envDiff := newEnvironmentDiff(env)
46-
47-
usedBundles := make([]types.SimulatedBundle, 0)
30+
func (b *greedyBuilder) mergeOrdersIntoEnvDiff(envDiff *environmentDiff, orders *types.TransactionsByPriceAndNonce) []types.SimulatedBundle {
31+
usedBundles := []types.SimulatedBundle{}
4832

4933
for {
5034
order := orders.Peek()
@@ -65,7 +49,7 @@ func (b *greedyBuilder) buildBlock(simBundles []types.SimulatedBundle, transacti
6549
log.Trace("could not apply tx", "hash", order.Tx.Hash(), "err", err)
6650
continue
6751
}
68-
effGapPrice, err := order.Tx.EffectiveGasTip(env.header.BaseFee)
52+
effGapPrice, err := order.Tx.EffectiveGasTip(envDiff.baseEnvironment.header.BaseFee)
6953
if err == nil {
7054
log.Trace("Included tx", "EGP", effGapPrice.String(), "gasUsed", receipt.GasUsed)
7155
}
@@ -84,6 +68,13 @@ func (b *greedyBuilder) buildBlock(simBundles []types.SimulatedBundle, transacti
8468
}
8569
}
8670

71+
return usedBundles
72+
}
73+
74+
func (b *greedyBuilder) buildBlock(simBundles []types.SimulatedBundle, transactions map[common.Address]types.Transactions) (*environment, []types.SimulatedBundle) {
75+
orders := types.NewTransactionsByPriceAndNonce(b.inputEnvironment.signer, transactions, simBundles, b.inputEnvironment.header.BaseFee)
76+
envDiff := newEnvironmentDiff(b.inputEnvironment.copy())
77+
usedBundles := b.mergeOrdersIntoEnvDiff(envDiff, orders)
8778
envDiff.applyToBaseEnv()
88-
return env, usedBundles
79+
return envDiff.baseEnvironment, usedBundles
8980
}

miner/algo_greedy_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package miner
22

33
import (
44
"fmt"
5+
"math/big"
6+
"testing"
7+
58
"github.com/ethereum/go-ethereum/common"
69
"github.com/ethereum/go-ethereum/core/types"
710
"github.com/ethereum/go-ethereum/log"
8-
"math/big"
9-
"testing"
1011
)
1112

1213
func TestBuildBlockGasLimit(t *testing.T) {
@@ -65,5 +66,4 @@ func TestTxWithMinerFeeHeap(t *testing.T) {
6566
orders.Pop()
6667
}
6768
}
68-
6969
}

miner/bundle_cache.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package miner
22

33
import (
4+
"sync"
5+
46
"github.com/ethereum/go-ethereum/common"
57
"github.com/ethereum/go-ethereum/core/types"
6-
"sync"
78
)
89

910
const (

miner/bundle_cache_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package miner
22

33
import (
4+
"testing"
5+
46
"github.com/ethereum/go-ethereum/common"
57
"github.com/ethereum/go-ethereum/core/types"
6-
"testing"
78
)
89

910
func TestBundleCacheEntry(t *testing.T) {

miner/miner.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ package miner
1919

2020
import (
2121
"crypto/ecdsa"
22+
"errors"
2223
"fmt"
23-
"github.com/ethereum/go-ethereum/crypto"
2424
"math/big"
2525
"os"
2626
"strings"
@@ -34,6 +34,7 @@ import (
3434
"github.com/ethereum/go-ethereum/core/state"
3535
"github.com/ethereum/go-ethereum/core/txpool"
3636
"github.com/ethereum/go-ethereum/core/types"
37+
"github.com/ethereum/go-ethereum/crypto"
3738
"github.com/ethereum/go-ethereum/eth/downloader"
3839
"github.com/ethereum/go-ethereum/event"
3940
"github.com/ethereum/go-ethereum/log"
@@ -54,6 +55,17 @@ const (
5455
ALGO_GREEDY
5556
)
5657

58+
func AlgoTypeFlagToEnum(algoString string) (AlgoType, error) {
59+
switch algoString {
60+
case "mev-geth":
61+
return ALGO_MEV_GETH, nil
62+
case "greedy":
63+
return ALGO_GREEDY, nil
64+
default:
65+
return ALGO_MEV_GETH, errors.New("algo not recognized")
66+
}
67+
}
68+
5769
// Config is the configuration parameters of mining.
5870
type Config struct {
5971
Etherbase common.Address `toml:",omitempty"` // Public address for block mining rewards

0 commit comments

Comments
 (0)