Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(op-geth): support droppingTxHashes when sendBundle #240

Open
wants to merge 18 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:])
redhdx marked this conversation as resolved.
Show resolved Hide resolved
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
34 changes: 33 additions & 1 deletion miner/worker_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,27 @@ func (w *worker) simulateBundle(
bundleGasFees = new(big.Int)
)

for i, tx := range bundle.Txs {
txsLen := len(bundle.Txs)
for i := 0; i < txsLen; i++ {
tx := bundle.Txs[i]
state.SetTxContext(tx.Hash(), i+currentTxCount)

snap := state.Snapshot()
gp := 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.RevertToSnapshot(snap)
gasPool.SetGas(gp)
bundle.Txs = bundle.Txs.Remove(i)
owen-reorg marked this conversation as resolved.
Show resolved Hide resolved
txsLen = len(bundle.Txs)
i--
continue
}

if prune {
if errors.Is(err, core.ErrGasLimitReached) && !pruneGasExceed {
Expand All @@ -387,6 +401,16 @@ 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())
owen-reorg marked this conversation as resolved.
Show resolved Hide resolved
state.RevertToSnapshot(snap)
gasPool.SetGas(gp)
bundle.Txs = bundle.Txs.Remove(i)
txsLen = len(bundle.Txs)
i--
continue
}
err = errNonRevertingTxInBundleFailed
log.Warn("fail to simulate bundle", "hash", bundle.Hash().String(), "err", err)

Expand All @@ -412,6 +436,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