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 txpool blockaddresses #47

Merged
merged 4 commits into from
Aug 19, 2023
Merged
Changes from all 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
47 changes: 46 additions & 1 deletion eth/mxc_api_backend.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package eth

import (
"fmt"
"math/big"
"strings"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -68,6 +70,7 @@
maxBytesPerTxList uint64,
minTxGasLimit uint64,
locals []string,
blockedAddresses []string,
) ([]types.Transactions, error) {
pending := s.eth.TxPool().Pending(false)

Expand All @@ -79,6 +82,7 @@
"maxBytesPerTxList", maxBytesPerTxList,
"minTxGasLimit", minTxGasLimit,
"locals", locals,
"blockedAddresses", blockedAddresses,
)

contentSplitter, err := core.NewPoolContentSplitter(
Expand All @@ -97,7 +101,7 @@
txsCount = 0
txLists []types.Transactions
)
for _, splittedTxs := range contentSplitter.Split(filterTxs(pending, s.eth.blockchain.CurrentHeader().BaseFee)) {
for _, splittedTxs := range contentSplitter.Split(filterTxs(filterBlockedTxs(pending, blockedAddresses), s.eth.blockchain.CurrentHeader().BaseFee)) {
if txsCount+splittedTxs.Len() < int(maxTransactionsPerBlock) {
txLists = append(txLists, splittedTxs)
txsCount += splittedTxs.Len()
Expand All @@ -111,6 +115,47 @@
return txLists, nil
}

func filterBlockedTxs(pendings map[common.Address]types.Transactions, blockedAddresses []string) map[common.Address]types.Transactions {
if len(blockedAddresses) == 0 {
return pendings
}
var ignoreAddresses []common.Address
for _, account := range blockedAddresses {
if trimmed := strings.TrimSpace(account); !common.IsHexAddress(trimmed) {
log.Warn(fmt.Sprintf("Invalid address: %s", trimmed))
} else {
ignoreAddresses = append(ignoreAddresses, common.HexToAddress(account))
}
}
executableTxs := make(map[common.Address]types.Transactions)

for addr, txs := range pendings {
pendingTxs := make(types.Transactions, 0)

blocked := false
for _, blockedAddress := range ignoreAddresses {
if addr == blockedAddress {
log.Debug(fmt.Sprintf("Ignore blocked address: %s", addr.Hex()))
blocked = true
break
}
}
if blocked {
continue
}
for _, tx := range txs {

Check failure on line 146 in eth/mxc_api_backend.go

View workflow job for this annotation

GitHub Actions / test

S1011: should replace loop with `pendingTxs = append(pendingTxs, txs...)` (gosimple)
pendingTxs = append(pendingTxs, tx)
}

if len(pendingTxs) > 0 {
executableTxs[addr] = pendingTxs
}
}

return executableTxs

}

func filterTxs(pendings map[common.Address]types.Transactions, baseFee *big.Int) map[common.Address]types.Transactions {
executableTxs := make(map[common.Address]types.Transactions)
gasPriceLowerLimit := big.NewInt(0).Div(big.NewInt(0).Mul(baseFee, big.NewInt(95)), big.NewInt(100))
Expand Down
Loading