Skip to content

Commit

Permalink
Merge pull request bnb-chain#13 from binance-chain/improve_mev
Browse files Browse the repository at this point in the history
[R4R]only allow bundle when it is turn to propose block
  • Loading branch information
unclezoro authored Nov 24, 2021
2 parents e32f197 + 701cf12 commit bf027d2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 19 deletions.
9 changes: 0 additions & 9 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ const (
MaxBundleTimeDelay = 60 * 60 // second
MaxOracleBlocks = 21
DropBlocks = 3
MaxGasUsedRatio = 90
)

// PublicEthereumAPI provides an API to access Ethereum related information.
Expand Down Expand Up @@ -2540,14 +2539,6 @@ func (s *PrivateTxBundleAPI) SendBundle(ctx context.Context, args SendBundleArgs
if len(validGasUsedRatio) == 0 {
return common.Hash{}, errors.New("no enough example ratio")
}
var totalRatio int
for _, ratio := range validGasUsedRatio {
totalRatio += ratio
}
averageRatio := totalRatio / len(validGasUsedRatio)
if averageRatio >= MaxGasUsedRatio {
return common.Hash{}, errors.New("the network is congested, please try later")
}

var txs types.Transactions
if len(args.Txs) == 0 {
Expand Down
4 changes: 3 additions & 1 deletion miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -970,9 +970,11 @@ func (w *worker) commitNewWork(interrupt *int32, noempty bool, timestamp int64)
if err != nil {
log.Error("Failed to fetch pending transactions", "err", err)
}
if w.config.IsFlashbots {
// only commit bundle when the fork chance is small, try our best to avoid
if w.config.IsFlashbots && header.Difficulty.Cmp(big.NewInt(1)) > 0 {
commitBundles := func() {
if w.current == nil {

return
}

Expand Down
45 changes: 36 additions & 9 deletions miner/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package miner

import (
"crypto/ecdsa"
"math/big"
"math/rand"
"sync/atomic"
Expand Down Expand Up @@ -58,6 +59,10 @@ var (
testBankAddress = crypto.PubkeyToAddress(testBankKey.PublicKey)
testBankFunds = big.NewInt(1000000000000000000)

testBankKey2, _ = crypto.GenerateKey()
testBankAddress2 = crypto.PubkeyToAddress(testBankKey2.PublicKey)
testBankFunds2 = big.NewInt(1000000000000000000)

testUserKey, _ = crypto.GenerateKey()
testUserAddress = crypto.PubkeyToAddress(testUserKey.PublicKey)

Expand Down Expand Up @@ -103,6 +108,12 @@ func init() {
rand.Seed(time.Now().UnixNano())
}

type dummySimulator struct{}

func (d *dummySimulator) SimulateBundle(bundle types.MevBundle) (*big.Int, error) {
return big.NewInt(0), nil
}

// testWorkerBackend implements worker.Backend interfaces and wraps all information needed during the testing.
type testWorkerBackend struct {
db ethdb.Database
Expand All @@ -116,7 +127,7 @@ type testWorkerBackend struct {
func newTestWorkerBackend(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine, db ethdb.Database, n int) *testWorkerBackend {
var gspec = core.Genesis{
Config: chainConfig,
Alloc: core.GenesisAlloc{testBankAddress: {Balance: testBankFunds}},
Alloc: core.GenesisAlloc{testBankAddress: {Balance: testBankFunds}, testBankAddress2: {Balance: testBankFunds2}},
}

switch e := engine.(type) {
Expand Down Expand Up @@ -180,12 +191,12 @@ func (b *testWorkerBackend) newRandomUncle() *types.Block {
return blocks[0]
}

func (b *testWorkerBackend) newRandomTx(creation bool) *types.Transaction {
func (b *testWorkerBackend) newRandomTx(creation bool, addr common.Address, key *ecdsa.PrivateKey) *types.Transaction {
var tx *types.Transaction
if creation {
tx, _ = types.SignTx(types.NewContractCreation(b.txPool.Nonce(testBankAddress), big.NewInt(0), testGas, nil, common.FromHex(testCode)), types.HomesteadSigner{}, testBankKey)
tx, _ = types.SignTx(types.NewContractCreation(b.txPool.Nonce(addr), big.NewInt(0), testGas, big.NewInt(1), common.FromHex(testCode)), types.HomesteadSigner{}, key)
} else {
tx, _ = types.SignTx(types.NewTransaction(b.txPool.Nonce(testBankAddress), testUserAddress, big.NewInt(1000), params.TxGas, nil, nil), types.HomesteadSigner{}, testBankKey)
tx, _ = types.SignTx(types.NewTransaction(b.txPool.Nonce(addr), testUserAddress, big.NewInt(1000), params.TxGas, big.NewInt(1), nil), types.HomesteadSigner{}, key)
}
return tx
}
Expand All @@ -199,14 +210,18 @@ func newTestWorker(t *testing.T, chainConfig *params.ChainConfig, engine consens
}

func TestGenerateBlockAndImportEthash(t *testing.T) {
testGenerateBlockAndImport(t, false)
testGenerateBlockAndImport(t, false, false)
}

func TestGenerateBlockAndImportClique(t *testing.T) {
testGenerateBlockAndImport(t, true)
testGenerateBlockAndImport(t, true, false)
}

func testGenerateBlockAndImport(t *testing.T, isClique bool) {
func TestGenerateBlockAndImportCliqueWithMev(t *testing.T) {
testGenerateBlockAndImport(t, true, true)
}

func testGenerateBlockAndImport(t *testing.T, isClique, mev bool) {
var (
engine consensus.Engine
chainConfig *params.ChainConfig
Expand All @@ -222,6 +237,12 @@ func testGenerateBlockAndImport(t *testing.T, isClique bool) {
}

w, b := newTestWorker(t, chainConfig, engine, db, 0)
if mev {
w.config.IsFlashbots = true
w.config.MaxSimulatBundles = 100
w.config.MevGasPriceFloor = 0
b.txPool.SetBundleSimulator(&dummySimulator{})
}
defer w.close()

// This test chain imports the mined blocks.
Expand All @@ -242,9 +263,15 @@ func testGenerateBlockAndImport(t *testing.T, isClique bool) {
// Start mining!
w.start()

if mev {
_, err := b.txPool.AddMevBundle([]*types.Transaction{b.newRandomTx(false, testBankAddress2, testBankKey2)}, nil, 0, 0, nil)
if err != nil {
t.Fatalf("add mev failed %v", err)
}
}
for i := 0; i < 5; i++ {
b.txPool.AddLocal(b.newRandomTx(true))
b.txPool.AddLocal(b.newRandomTx(false))
b.txPool.AddLocal(b.newRandomTx(true, testBankAddress, testBankKey))
b.txPool.AddLocal(b.newRandomTx(false, testBankAddress, testBankKey))
w.postSideBlock(core.ChainSideEvent{Block: b.newRandomUncle()})
w.postSideBlock(core.ChainSideEvent{Block: b.newRandomUncle()})

Expand Down
2 changes: 2 additions & 0 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ var (

TestChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), new(EthashConfig), nil, nil}

AllParliaProtocolChanges = &ChainConfig{big.NewInt(1437), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, &ParliaConfig{Period: 0, Epoch: 30000}}

TestRules = TestChainConfig.Rules(new(big.Int))
)

Expand Down

0 comments on commit bf027d2

Please sign in to comment.