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 10 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
40 changes: 32 additions & 8 deletions core/txpool/bundlepool/bundlepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,10 @@ func (p *BundlePool) reset(newHead *types.Header) {
p.mu.Lock()
defer p.mu.Unlock()

if len(p.bundles) == 0 {
return
}

// Prune outdated bundles and invalid bundles
block := p.chain.GetBlock(newHead.Hash(), newHead.Number.Uint64())
txSet := mapset.NewSet[common.Hash]()
Expand All @@ -370,16 +374,27 @@ func (p *BundlePool) reset(newHead *types.Header) {
txSet.Add(tx.Hash())
}
}
var wg sync.WaitGroup
for hash, bundle := range p.bundles {
if (bundle.MaxTimestamp != 0 && newHead.Time > bundle.MaxTimestamp) ||
(bundle.MaxBlockNumber != 0 && newHead.Number.Cmp(new(big.Int).SetUint64(bundle.MaxBlockNumber)) > 0) {
p.slots -= numSlots(p.bundles[hash])
delete(p.bundles, hash)
} else if txSet.Contains(bundle.Txs[0].Hash()) {
p.slots -= numSlots(p.bundles[hash])
delete(p.bundles, hash)
}
wg.Add(1)
go func(hash common.Hash, bundle *types.Bundle) {
defer wg.Done()
if (bundle.MaxTimestamp != 0 && newHead.Time > bundle.MaxTimestamp) ||
(bundle.MaxBlockNumber != 0 && newHead.Number.Cmp(new(big.Int).SetUint64(bundle.MaxBlockNumber)) > 0) {
p.slots -= numSlots(p.bundles[hash])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it thread safe to do p.slots -= and delete(p.bundles, hash)?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reset function itself is thread safe since we have

	p.mu.Lock()
	defer p.mu.Unlock()

in the beginning of the function.

However, if you use go func here, it's not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed go func, just use single thread.

delete(p.bundles, hash)
} else {
for _, tx := range bundle.Txs {
if txSet.Contains(tx.Hash()) && !containsHash(bundle.DroppingTxHashes, tx.Hash()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend maintain unDroppableTxHashesSet as a cache, we can use it here and in the check of simulation, avoid iterate the slice again and again.

p.slots -= numSlots(p.bundles[hash])
delete(p.bundles, hash)
break
}
}
}
}(hash, bundle)
}
wg.Wait()
bundleGauge.Update(int64(len(p.bundles)))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need these 2 lines if we early return when len(p.bundles) == 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

slotsGauge.Update(int64(p.slots))
}
Expand Down Expand Up @@ -447,6 +462,15 @@ func numSlots(bundle *types.Bundle) uint64 {
return (bundle.Size() + bundleSlotSize - 1) / bundleSlotSize
}

func containsHash(arr []common.Hash, match common.Hash) bool {
for _, elem := range arr {
if elem == match {
return true
}
}
return false
}

// =====================================================================================================================

type BundleHeap []*types.Bundle
Expand Down
3 changes: 3 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 All @@ -41,6 +43,7 @@ type Bundle struct {

type SimulatedBundle struct {
OriginalBundle *Bundle
SucceedTxs Transactions

BundleGasFees *big.Int
BundleGasPrice *big.Int
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
35 changes: 31 additions & 4 deletions miner/worker_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,9 @@ func (w *worker) mergeBundles(
log.Info("included bundle",
"gasUsed", simulatedBundle.BundleGasUsed,
"gasPrice", simulatedBundle.BundleGasPrice,
"txcount", len(simulatedBundle.OriginalBundle.Txs))
"txcount", len(simulatedBundle.SucceedTxs))

includedTxs = append(includedTxs, bundle.OriginalBundle.Txs...)
includedTxs = append(includedTxs, simulatedBundle.SucceedTxs...)

mergedBundle.BundleGasFees.Add(mergedBundle.BundleGasFees, simulatedBundle.BundleGasFees)
mergedBundle.BundleGasUsed += simulatedBundle.BundleGasUsed
Expand Down Expand Up @@ -366,13 +366,24 @@ func (w *worker) simulateBundle(
bundleGasFees = new(big.Int)
)

for i, tx := range bundle.Txs {
state.SetTxContext(tx.Hash(), i+currentTxCount)
succeedTxs := types.Transactions{}
succeedTxCount := 0
for _, tx := range bundle.Txs {
state.SetTxContext(tx.Hash(), succeedTxCount+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)
continue
}

if prune {
if errors.Is(err, core.ErrGasLimitReached) && !pruneGasExceed {
Expand All @@ -387,6 +398,11 @@ 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
continue
}
err = errNonRevertingTxInBundleFailed
log.Warn("fail to simulate bundle", "hash", bundle.Hash().String(), "err", err)

Expand All @@ -411,7 +427,17 @@ func (w *worker) simulateBundle(
txGasFees := new(big.Int).Mul(txGasUsed, effectiveTip)
bundleGasFees.Add(bundleGasFees, txGasFees)
}
succeedTxs = append(succeedTxs, tx)
succeedTxCount++
}

// prune bundle when all txs are dropped
if len(succeedTxs) == 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 All @@ -432,6 +458,7 @@ func (w *worker) simulateBundle(

return &types.SimulatedBundle{
OriginalBundle: bundle,
SucceedTxs: succeedTxs,
BundleGasFees: bundleGasFees,
BundleGasPrice: bundleGasPrice,
BundleGasUsed: bundleGasUsed,
Expand Down