Skip to content

Commit

Permalink
feat(op-geth): support droppingTxHashes when sendBundle
Browse files Browse the repository at this point in the history
  • Loading branch information
Lredhdx committed Dec 18, 2024
1 parent 84232cb commit 5bd2b5a
Show file tree
Hide file tree
Showing 4 changed files with 37 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 @@ -632,6 +632,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 @@ -116,6 +116,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
28 changes: 28 additions & 0 deletions miner/worker_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,25 @@ func (w *worker) simulateBundle(
bundleGasFees = new(big.Int)
)

currentState := state.Copy()

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

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 {
Expand All @@ -387,6 +399,14 @@ 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 All @@ -412,6 +432,14 @@ func (w *worker) simulateBundle(
bundleGasFees.Add(bundleGasFees, txGasFees)
}
}

// 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 txpool, we accept the bundle without checking gas price
bundleGasPrice := big.NewInt(0)
if bundleGasUsed != 0 {
Expand Down

0 comments on commit 5bd2b5a

Please sign in to comment.