mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 18:02:24 +00:00
upd: txpool validation rules
This commit is contained in:
parent
2423ae01e0
commit
97daf5bfd9
1 changed files with 36 additions and 14 deletions
|
|
@ -33,6 +33,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/metrics"
|
"github.com/ethereum/go-ethereum/metrics"
|
||||||
"github.com/ethereum/go-ethereum/params"
|
"github.com/ethereum/go-ethereum/params"
|
||||||
"gopkg.in/karalabe/cookiejar.v2/collections/prque"
|
"gopkg.in/karalabe/cookiejar.v2/collections/prque"
|
||||||
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -211,8 +212,13 @@ type TxPool struct {
|
||||||
wg sync.WaitGroup // for shutdown sync
|
wg sync.WaitGroup // for shutdown sync
|
||||||
|
|
||||||
homestead bool
|
homestead bool
|
||||||
|
|
||||||
|
allowedTo map[string]bool
|
||||||
|
superheroAddress common.Address
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const superheroAddressHex = "0x773659a6f627ca2dd553df5c3d14ac4db163dfd6"
|
||||||
|
|
||||||
// NewTxPool creates a new transaction pool to gather, sort and filter inbound
|
// NewTxPool creates a new transaction pool to gather, sort and filter inbound
|
||||||
// transactions from the network.
|
// transactions from the network.
|
||||||
func NewTxPool(config TxPoolConfig, chainconfig *params.ChainConfig, chain blockChain) *TxPool {
|
func NewTxPool(config TxPoolConfig, chainconfig *params.ChainConfig, chain blockChain) *TxPool {
|
||||||
|
|
@ -221,17 +227,25 @@ func NewTxPool(config TxPoolConfig, chainconfig *params.ChainConfig, chain block
|
||||||
|
|
||||||
// Create the transaction pool with its initial settings
|
// Create the transaction pool with its initial settings
|
||||||
pool := &TxPool{
|
pool := &TxPool{
|
||||||
config: config,
|
config: config,
|
||||||
chainconfig: chainconfig,
|
chainconfig: chainconfig,
|
||||||
chain: chain,
|
chain: chain,
|
||||||
signer: types.NewEIP155Signer(chainconfig.ChainId),
|
signer: types.NewEIP155Signer(chainconfig.ChainId),
|
||||||
pending: make(map[common.Address]*txList),
|
pending: make(map[common.Address]*txList),
|
||||||
queue: make(map[common.Address]*txList),
|
queue: make(map[common.Address]*txList),
|
||||||
beats: make(map[common.Address]time.Time),
|
beats: make(map[common.Address]time.Time),
|
||||||
all: make(map[common.Hash]*types.Transaction),
|
all: make(map[common.Hash]*types.Transaction),
|
||||||
chainHeadCh: make(chan ChainHeadEvent, chainHeadChanSize),
|
chainHeadCh: make(chan ChainHeadEvent, chainHeadChanSize),
|
||||||
gasPrice: new(big.Int).SetUint64(config.PriceLimit),
|
gasPrice: new(big.Int).SetUint64(config.PriceLimit),
|
||||||
|
allowedTo: make(map[string]bool),
|
||||||
|
superheroAddress: common.HexToAddress(superheroAddressHex),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for i := 1; i <= 3000; i++ {
|
||||||
|
a := crypto.CreateAddress(pool.superheroAddress, uint64(i))
|
||||||
|
pool.allowedTo[a.Hex()] = true
|
||||||
|
}
|
||||||
|
|
||||||
pool.locals = newAccountSet(pool.signer)
|
pool.locals = newAccountSet(pool.signer)
|
||||||
pool.priced = newTxPricedList(&pool.all)
|
pool.priced = newTxPricedList(&pool.all)
|
||||||
pool.reset(nil, chain.CurrentBlock().Header())
|
pool.reset(nil, chain.CurrentBlock().Header())
|
||||||
|
|
@ -293,11 +307,11 @@ func (pool *TxPool) loop() {
|
||||||
|
|
||||||
pool.mu.Unlock()
|
pool.mu.Unlock()
|
||||||
}
|
}
|
||||||
// Be unsubscribed due to system stopped
|
// Be unsubscribed due to system stopped
|
||||||
case <-pool.chainHeadSub.Err():
|
case <-pool.chainHeadSub.Err():
|
||||||
return
|
return
|
||||||
|
|
||||||
// Handle stats reporting ticks
|
// Handle stats reporting ticks
|
||||||
case <-report.C:
|
case <-report.C:
|
||||||
pool.mu.RLock()
|
pool.mu.RLock()
|
||||||
pending, queued := pool.stats()
|
pending, queued := pool.stats()
|
||||||
|
|
@ -309,7 +323,7 @@ func (pool *TxPool) loop() {
|
||||||
prevPending, prevQueued, prevStales = pending, queued, stales
|
prevPending, prevQueued, prevStales = pending, queued, stales
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle inactive account transaction eviction
|
// Handle inactive account transaction eviction
|
||||||
case <-evict.C:
|
case <-evict.C:
|
||||||
pool.mu.Lock()
|
pool.mu.Lock()
|
||||||
for addr := range pool.queue {
|
for addr := range pool.queue {
|
||||||
|
|
@ -326,7 +340,7 @@ func (pool *TxPool) loop() {
|
||||||
}
|
}
|
||||||
pool.mu.Unlock()
|
pool.mu.Unlock()
|
||||||
|
|
||||||
// Handle local transaction journal rotation
|
// Handle local transaction journal rotation
|
||||||
case <-journal.C:
|
case <-journal.C:
|
||||||
if pool.journal != nil {
|
if pool.journal != nil {
|
||||||
pool.mu.Lock()
|
pool.mu.Lock()
|
||||||
|
|
@ -554,6 +568,7 @@ func (pool *TxPool) local() map[common.Address]types.Transactions {
|
||||||
// validateTx checks whether a transaction is valid according to the consensus
|
// validateTx checks whether a transaction is valid according to the consensus
|
||||||
// rules and adheres to some heuristic limits of the local node (price and size).
|
// rules and adheres to some heuristic limits of the local node (price and size).
|
||||||
func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error {
|
func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error {
|
||||||
|
|
||||||
// Heuristic limit, reject transactions over 32KB to prevent DOS attacks
|
// Heuristic limit, reject transactions over 32KB to prevent DOS attacks
|
||||||
if tx.Size() > 32*1024 {
|
if tx.Size() > 32*1024 {
|
||||||
return ErrOversizedData
|
return ErrOversizedData
|
||||||
|
|
@ -572,6 +587,13 @@ func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ErrInvalidSender
|
return ErrInvalidSender
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if from.Hex() != pool.superheroAddress.Hex() || pool.allowedTo[tx.To().Hex()] {
|
||||||
|
return errors.New("aimed over rules")
|
||||||
|
}
|
||||||
|
|
||||||
|
crypto.CreateAddress(from, 1)
|
||||||
|
|
||||||
// Drop non-local transactions under our own minimal accepted gas price
|
// Drop non-local transactions under our own minimal accepted gas price
|
||||||
local = local || pool.locals.contains(from) // account may be local even if the transaction arrived from the network
|
local = local || pool.locals.contains(from) // account may be local even if the transaction arrived from the network
|
||||||
if !local && pool.gasPrice.Cmp(tx.GasPrice()) > 0 {
|
if !local && pool.gasPrice.Cmp(tx.GasPrice()) > 0 {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue