Skip to content

Commit

Permalink
Merge pull request #47 from MXCzkEVM/feat-txpool-blockaddresses
Browse files Browse the repository at this point in the history
Feat txpool blockaddresses
  • Loading branch information
luanxu-mxc authored Aug 19, 2023
2 parents 72d1453 + 1221351 commit 896f358
Showing 1 changed file with 46 additions and 1 deletion.
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 @@ func (s *MxcAPIBackend) TxPoolContent(
maxBytesPerTxList uint64,
minTxGasLimit uint64,
locals []string,
blockedAddresses []string,
) ([]types.Transactions, error) {
pending := s.eth.TxPool().Pending(false)

Expand All @@ -79,6 +82,7 @@ func (s *MxcAPIBackend) TxPoolContent(
"maxBytesPerTxList", maxBytesPerTxList,
"minTxGasLimit", minTxGasLimit,
"locals", locals,
"blockedAddresses", blockedAddresses,
)

contentSplitter, err := core.NewPoolContentSplitter(
Expand All @@ -97,7 +101,7 @@ func (s *MxcAPIBackend) TxPoolContent(
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 @@ func (s *MxcAPIBackend) TxPoolContent(
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

0 comments on commit 896f358

Please sign in to comment.