Skip to content

Commit

Permalink
feat: support droppingTxHashes when sendBundle (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
irrun authored Dec 18, 2024
1 parent 534c633 commit 3092023
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/types/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type SendBundleArgs struct {
MinTimestamp *uint64 `json:"minTimestamp"`
MaxTimestamp *uint64 `json:"maxTimestamp"`
RevertingTxHashes []common.Hash `json:"revertingTxHashes"`
DroppingTxHashes []common.Hash `json:"droppingTxHashes"`
}

type Bundle struct {
Expand All @@ -31,6 +32,7 @@ type Bundle struct {
MinTimestamp uint64
MaxTimestamp uint64
RevertingTxHashes []common.Hash
DroppingTxHashes []common.Hash

Price *big.Int // for bundle compare and prune

Expand Down
6 changes: 6 additions & 0 deletions core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,12 @@ func (s Transactions) EncodeIndex(i int, w *bytes.Buffer) {
}
}

func (s Transactions) Remove(idx int) Transactions {
copy(s[idx:], s[idx+1:])
s[len(s)-1] = nil
return s[:len(s)-1]
}

// TxDifference returns a new set which is the difference between a and b.
func TxDifference(a, b Transactions) Transactions {
keep := make(Transactions, 0, len(a))
Expand Down
1 change: 1 addition & 0 deletions internal/ethapi/api_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func (s *PrivateTxBundleAPI) SendBundle(ctx context.Context, args types.SendBund
MinTimestamp: minTimestamp,
MaxTimestamp: maxTimestamp,
RevertingTxHashes: args.RevertingTxHashes,
DroppingTxHashes: args.DroppingTxHashes,
}

// If the maxBlockNumber and maxTimestamp are not set, set max ddl of bundle as types.MaxBundleAliveBlock
Expand Down
29 changes: 29 additions & 0 deletions miner/worker_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,15 +413,28 @@ func (w *worker) simulateBundle(
ethSentToSystem = new(big.Int)
)

currentState := state.Copy()

for i, tx := range bundle.Txs {
state.SetTxContext(tx.Hash(), i+currentTxCount)
sysBalanceBefore := state.GetBalance(consensus.SystemAddress)

prevState := currentState.Copy()
prevGasPool := new(core.GasPool).AddGas(gasPool.Gas())

receipt, err := core.ApplyTransaction(w.chainConfig, w.chain, &w.coinbase, gasPool, state, env.header, tx,
&tempGasUsed, *w.chain.GetVMConfig())
if err != nil {
log.Warn("fail to simulate bundle", "hash", bundle.Hash().String(), "err", err)

if containsHash(bundle.DroppingTxHashes, tx.Hash()) {
log.Warn("drop tx in bundle", "hash", tx.Hash().String())
state = prevState
gasPool = prevGasPool
bundle.Txs = bundle.Txs.Remove(i)
continue
}

if prune {
if errors.Is(err, core.ErrGasLimitReached) && !pruneGasExceed {
log.Warn("bundle gas limit exceed", "hash", bundle.Hash().String())
Expand All @@ -435,6 +448,15 @@ func (w *worker) simulateBundle(
}

if receipt.Status == types.ReceiptStatusFailed && !containsHash(bundle.RevertingTxHashes, receipt.TxHash) {
// for unRevertible tx but itself can be dropped, we drop it and revert the state and gas pool
if containsHash(bundle.DroppingTxHashes, receipt.TxHash) {
log.Warn("drop tx in bundle", "hash", receipt.TxHash.String())
state = prevState
gasPool = prevGasPool
bundle.Txs = bundle.Txs.Remove(i)
continue
}

err = errNonRevertingTxInBundleFailed
log.Warn("fail to simulate bundle", "hash", bundle.Hash().String(), "err", err)

Expand Down Expand Up @@ -474,6 +496,13 @@ func (w *worker) simulateBundle(
}
}

// prune bundle when all txs are dropped
if len(bundle.Txs) == 0 {
log.Warn("prune bundle", "hash", bundle.Hash().String(), "err", "empty bundle")
w.eth.TxPool().PruneBundle(bundle.Hash())
return nil, errors.New("empty bundle")
}

// if all txs in the bundle are from mempool, we accept the bundle without checking gas price
bundleGasPrice := big.NewInt(0)

Expand Down

0 comments on commit 3092023

Please sign in to comment.