Skip to content

Commit

Permalink
Merge pull request #45 from MXCzkEVM/feat-txpool-blockaddresses
Browse files Browse the repository at this point in the history
feat: mxc_txpool block addresses
  • Loading branch information
luanxu-mxc committed Aug 19, 2023
2 parents 922d8cf + f28e4fe commit 75f5181
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 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,42 @@ 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 localsAddresses []common.Address
for _, account := range blockedAddresses {
if trimmed := strings.TrimSpace(account); !common.IsHexAddress(trimmed) {
log.Warn(fmt.Sprintf("Invalid address: %s", trimmed))
} else {
localsAddresses = append(localsAddresses, common.HexToAddress(account))
}
}
executableTxs := make(map[common.Address]types.Transactions)

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

for _, blockedAddress := range localsAddresses {
if addr == blockedAddress {
log.Debug(fmt.Sprintf("Ignore blocked address: %s", addr.Hex()))
break
}
}
for _, tx := range txs {
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 75f5181

Please sign in to comment.