core/tx_pool.go:update Addremotes for non-blocking mode

This commit is contained in:
ywzqwwt 2019-01-17 13:28:23 +08:00
parent 34f11e752f
commit 6ab8d3a159

View file

@ -38,6 +38,7 @@ import (
const ( const (
// chainHeadChanSize is the size of channel listening to ChainHeadEvent. // chainHeadChanSize is the size of channel listening to ChainHeadEvent.
chainHeadChanSize = 10 chainHeadChanSize = 10
txChanSize = 2048
) )
var ( var (
@ -227,6 +228,7 @@ type TxPool struct {
all *txLookup // All transactions to allow lookups all *txLookup // All transactions to allow lookups
priced *txPricedList // All transactions sorted by price priced *txPricedList // All transactions sorted by price
newTxsCh chan []*types.Transaction
wg sync.WaitGroup // for shutdown sync wg sync.WaitGroup // for shutdown sync
homestead bool homestead bool
@ -249,6 +251,7 @@ func NewTxPool(config TxPoolConfig, chainconfig *params.ChainConfig, chain block
beats: make(map[common.Address]time.Time), beats: make(map[common.Address]time.Time),
all: newTxLookup(), all: newTxLookup(),
chainHeadCh: make(chan ChainHeadEvent, chainHeadChanSize), chainHeadCh: make(chan ChainHeadEvent, chainHeadChanSize),
newTxsCh: make(chan []*types.Transaction, txChanSize),
gasPrice: new(big.Int).SetUint64(config.PriceLimit), gasPrice: new(big.Int).SetUint64(config.PriceLimit),
} }
pool.locals = newAccountSet(pool.signer) pool.locals = newAccountSet(pool.signer)
@ -276,10 +279,23 @@ func NewTxPool(config TxPoolConfig, chainconfig *params.ChainConfig, chain block
// Start the event loop and return // Start the event loop and return
pool.wg.Add(1) pool.wg.Add(1)
go pool.loop() go pool.loop()
go pool.addremote()
return pool return pool
} }
func (pool *TxPool) addremote() {
for {
select {
// Handle new remote transactions
case txs := <-pool.newTxsCh:
if txs != nil {
pool.addTxs(txs, false)
}
}
}
}
// loop is the transaction pool's main event loop, waiting for and reacting to // loop is the transaction pool's main event loop, waiting for and reacting to
// outside blockchain events as well as for various reporting and transaction // outside blockchain events as well as for various reporting and transaction
// eviction events. // eviction events.
@ -813,7 +829,15 @@ func (pool *TxPool) AddLocals(txs []*types.Transaction) []error {
// If the senders are not among the locally tracked ones, full pricing constraints // If the senders are not among the locally tracked ones, full pricing constraints
// will apply. // will apply.
func (pool *TxPool) AddRemotes(txs []*types.Transaction) []error { func (pool *TxPool) AddRemotes(txs []*types.Transaction) []error {
return pool.addTxs(txs, false) errs := make([]error, len(txs))
select {
case pool.newTxsCh <- txs:
return nil
default:
log.Info("discard remote txs", "count", len(txs))
errs[0] = errors.New("newTxsCh is full")
}
return errs
} }
// addTx enqueues a single transaction into the pool if it is valid. // addTx enqueues a single transaction into the pool if it is valid.