Skip to content

Commit

Permalink
Use different builders instead of configuration switch since major re…
Browse files Browse the repository at this point in the history
…factor required to handle more dynamic configurations.
  • Loading branch information
Wazzymandias committed Aug 23, 2023
1 parent 1062c82 commit c14c299
Show file tree
Hide file tree
Showing 14 changed files with 503 additions and 190 deletions.
1 change: 0 additions & 1 deletion cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ var (

builderApiFlags = []cli.Flag{
utils.BuilderEnabled,
utils.BuilderEnableMultiTxSnapshot,
utils.BuilderAlgoTypeFlag,
utils.BuilderPriceCutoffPercentFlag,
utils.BuilderEnableValidatorChecks,
Expand Down
9 changes: 0 additions & 9 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -697,14 +697,6 @@ var (
Usage: "Enable the builder",
Category: flags.BuilderCategory,
}
BuilderEnableMultiTxSnapshot = &cli.BoolFlag{
Name: "builder.multi_tx_snapshot",
Usage: "Enable multi-transaction snapshots for block building, " +
"which decrease amount of state copying on bundle reverts (note: experimental)",
EnvVars: []string{"BUILDER_MULTI_TX_SNAPSHOT"},
Value: ethconfig.Defaults.Miner.EnableMultiTransactionSnapshot,
Category: flags.BuilderCategory,
}

// BuilderAlgoTypeFlag replaces MinerAlgoTypeFlag to move away from deprecated miner package
// Note: builder.algotype was previously miner.algotype - this flag is still propagated to the miner configuration,
Expand Down Expand Up @@ -1972,7 +1964,6 @@ func setMiner(ctx *cli.Context, cfg *miner.Config) {
}
}

cfg.EnableMultiTransactionSnapshot = ctx.Bool(BuilderEnableMultiTxSnapshot.Name)
cfg.DiscardRevertibleTxOnErr = ctx.Bool(BuilderDiscardRevertibleTxOnErr.Name)
cfg.PriceCutoffPercent = ctx.Int(BuilderPriceCutoffPercentFlag.Name)
}
Expand Down
54 changes: 0 additions & 54 deletions miner/algo_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ var (
ExpectedProfit: nil,
ProfitThresholdPercent: defaultProfitThresholdPercent,
PriceCutoffPercent: defaultPriceCutoffPercent,
EnableMultiTxSnap: false,
}
)

Expand Down Expand Up @@ -84,9 +83,6 @@ type algorithmConfig struct {
// is 10 (i.e. 10%), then the minimum effective gas price included in the same bucket as the top transaction
// is (1000 * 10%) = 100 wei.
PriceCutoffPercent int
// EnableMultiTxSnap is true if we want to use multi-transaction snapshot for committing transactions,
// which reduce state copies when reverting failed bundles (note: experimental)
EnableMultiTxSnap bool
}

type chainData struct {
Expand Down Expand Up @@ -121,56 +117,6 @@ type (
CommitTxFunc func(*types.Transaction, chainData) (*types.Receipt, int, error)
)

func NewBuildBlockFunc(
inputEnvironment *environment,
builderKey *ecdsa.PrivateKey,
chData chainData,
algoConf algorithmConfig,
greedyBuckets *greedyBucketsBuilder,
greedy *greedyBuilder,
) BuildBlockFunc {
if algoConf.EnableMultiTxSnap {
return func(simBundles []types.SimulatedBundle, simSBundles []*types.SimSBundle, transactions map[common.Address]types.Transactions) (*environment, []types.SimulatedBundle, []types.UsedSBundle) {
orders := types.NewTransactionsByPriceAndNonce(inputEnvironment.signer, transactions,
simBundles, simSBundles, inputEnvironment.header.BaseFee)

usedBundles, usedSbundles, err := BuildMultiTxSnapBlock(
inputEnvironment,
builderKey,
chData,
algoConf,
orders,
)
if err != nil {
log.Trace("Error(s) building multi-tx snapshot block", "err", err)
}
return inputEnvironment, usedBundles, usedSbundles
}
} else if builder := greedyBuckets; builder != nil {
return func(simBundles []types.SimulatedBundle, simSBundles []*types.SimSBundle, transactions map[common.Address]types.Transactions) (*environment, []types.SimulatedBundle, []types.UsedSBundle) {
orders := types.NewTransactionsByPriceAndNonce(inputEnvironment.signer, transactions,
simBundles, simSBundles, inputEnvironment.header.BaseFee)

envDiff := newEnvironmentDiff(inputEnvironment.copy())
usedBundles, usedSbundles := builder.mergeOrdersIntoEnvDiff(envDiff, orders)
envDiff.applyToBaseEnv()
return envDiff.baseEnvironment, usedBundles, usedSbundles
}
} else if builder := greedy; builder != nil {
return func(simBundles []types.SimulatedBundle, simSBundles []*types.SimSBundle, transactions map[common.Address]types.Transactions) (*environment, []types.SimulatedBundle, []types.UsedSBundle) {
orders := types.NewTransactionsByPriceAndNonce(inputEnvironment.signer, transactions,
simBundles, simSBundles, inputEnvironment.header.BaseFee)

envDiff := newEnvironmentDiff(inputEnvironment.copy())
usedBundles, usedSbundles := builder.mergeOrdersIntoEnvDiff(envDiff, orders)
envDiff.applyToBaseEnv()
return envDiff.baseEnvironment, usedBundles, usedSbundles
}
} else {
panic("invalid call to build block function")
}
}

func ValidateGasPriceAndProfit(algoConf algorithmConfig, actualPrice, expectedPrice *big.Int, tolerablePriceDifferencePercent int,
actualProfit, expectedProfit *big.Int) error {
// allow tolerablePriceDifferencePercent % divergence
Expand Down
4 changes: 2 additions & 2 deletions miner/algo_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ func TestGetSealingWorkAlgos(t *testing.T) {
testConfig.AlgoType = ALGO_MEV_GETH
})

for _, algoType := range []AlgoType{ALGO_MEV_GETH, ALGO_GREEDY, ALGO_GREEDY_BUCKETS} {
for _, algoType := range []AlgoType{ALGO_MEV_GETH, ALGO_GREEDY, ALGO_GREEDY_BUCKETS, ALGO_GREEDY_MULTISNAP, ALGO_GREEDY_BUCKETS_MULTISNAP} {
local := new(params.ChainConfig)
*local = *ethashChainConfig
local.TerminalTotalDifficulty = big.NewInt(0)
Expand All @@ -540,7 +540,7 @@ func TestGetSealingWorkAlgosWithProfit(t *testing.T) {
testConfig.BuilderTxSigningKey = nil
})

for _, algoType := range []AlgoType{ALGO_GREEDY, ALGO_GREEDY_BUCKETS} {
for _, algoType := range []AlgoType{ALGO_GREEDY, ALGO_GREEDY_BUCKETS, ALGO_GREEDY_MULTISNAP, ALGO_GREEDY_BUCKETS_MULTISNAP} {
var err error
testConfig.BuilderTxSigningKey, err = crypto.GenerateKey()
require.NoError(t, err)
Expand Down
26 changes: 9 additions & 17 deletions miner/algo_greedy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,24 @@ type greedyBuilder struct {
chainData chainData
builderKey *ecdsa.PrivateKey
interrupt *int32
buildBlockFunc BuildBlockFunc
algoConf algorithmConfig
}

func newGreedyBuilder(
chain *core.BlockChain, chainConfig *params.ChainConfig, algoConf *algorithmConfig,
blacklist map[common.Address]struct{}, env *environment, key *ecdsa.PrivateKey, interrupt *int32,
) (*greedyBuilder, error) {
) *greedyBuilder {
if algoConf == nil {
return nil, errNoAlgorithmConfig
panic("algoConf cannot be nil")
}

builder := &greedyBuilder{
return &greedyBuilder{
inputEnvironment: env,
chainData: chainData{chainConfig: chainConfig, chain: chain, blacklist: blacklist},
chainData: chainData{chainConfig, chain, blacklist},
builderKey: key,
interrupt: interrupt,
algoConf: *algoConf,
}
// Initialize block builder function
builder.buildBlockFunc = NewBuildBlockFunc(
builder.inputEnvironment,
builder.builderKey,
builder.chainData,
builder.algoConf,
nil,
builder,
)

return builder, nil
}

func (b *greedyBuilder) mergeOrdersIntoEnvDiff(
Expand Down Expand Up @@ -115,5 +103,9 @@ func (b *greedyBuilder) mergeOrdersIntoEnvDiff(
}

func (b *greedyBuilder) buildBlock(simBundles []types.SimulatedBundle, simSBundles []*types.SimSBundle, transactions map[common.Address]types.Transactions) (*environment, []types.SimulatedBundle, []types.UsedSBundle) {
return b.buildBlockFunc(simBundles, simSBundles, transactions)
orders := types.NewTransactionsByPriceAndNonce(b.inputEnvironment.signer, transactions, simBundles, simSBundles, b.inputEnvironment.header.BaseFee)
envDiff := newEnvironmentDiff(b.inputEnvironment.copy())
usedBundles, usedSbundles := b.mergeOrdersIntoEnvDiff(envDiff, orders)
envDiff.applyToBaseEnv()
return envDiff.baseEnvironment, usedBundles, usedSbundles
}
23 changes: 9 additions & 14 deletions miner/algo_greedy_buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,24 @@ type greedyBucketsBuilder struct {
interrupt *int32
gasUsedMap map[*types.TxWithMinerFee]uint64
algoConf algorithmConfig
buildBlockFunc BuildBlockFunc
}

func newGreedyBucketsBuilder(
chain *core.BlockChain, chainConfig *params.ChainConfig, algoConf *algorithmConfig,
blacklist map[common.Address]struct{}, env *environment, key *ecdsa.PrivateKey, interrupt *int32,
) (*greedyBucketsBuilder, error) {
) *greedyBucketsBuilder {
if algoConf == nil {
return nil, errNoAlgorithmConfig
panic("algoConf cannot be nil")
}

builder := &greedyBucketsBuilder{
return &greedyBucketsBuilder{
inputEnvironment: env,
chainData: chainData{chainConfig: chainConfig, chain: chain, blacklist: blacklist},
builderKey: key,
interrupt: interrupt,
gasUsedMap: make(map[*types.TxWithMinerFee]uint64),
algoConf: *algoConf,
}

// Initialize block builder function
builder.buildBlockFunc = NewBuildBlockFunc(builder.inputEnvironment, builder.builderKey, builder.chainData, builder.algoConf, builder, nil)
return builder, nil
}

// CutoffPriceFromOrder returns the cutoff price for a given order based on the cutoff percent.
Expand Down Expand Up @@ -145,9 +140,6 @@ func (b *greedyBucketsBuilder) commit(envDiff *environmentDiff,
usedEntry.Success = false
usedSbundles = append(usedSbundles, usedEntry)
}
} else {
usedEntry.Success = false
usedSbundles = append(usedSbundles, usedEntry)
}
continue
}
Expand Down Expand Up @@ -223,7 +215,10 @@ func (b *greedyBucketsBuilder) mergeOrdersIntoEnvDiff(
return usedBundles, usedSbundles
}

func (b *greedyBucketsBuilder) buildBlock(simBundles []types.SimulatedBundle, simSBundles []*types.SimSBundle,
transactions map[common.Address]types.Transactions) (*environment, []types.SimulatedBundle, []types.UsedSBundle) {
return b.buildBlockFunc(simBundles, simSBundles, transactions)
func (b *greedyBucketsBuilder) buildBlock(simBundles []types.SimulatedBundle, simSBundles []*types.SimSBundle, transactions map[common.Address]types.Transactions) (*environment, []types.SimulatedBundle, []types.UsedSBundle) {
orders := types.NewTransactionsByPriceAndNonce(b.inputEnvironment.signer, transactions, simBundles, simSBundles, b.inputEnvironment.header.BaseFee)
envDiff := newEnvironmentDiff(b.inputEnvironment.copy())
usedBundles, usedSbundles := b.mergeOrdersIntoEnvDiff(envDiff, orders)
envDiff.applyToBaseEnv()
return envDiff.baseEnvironment, usedBundles, usedSbundles
}
Loading

0 comments on commit c14c299

Please sign in to comment.