Skip to content

Commit a2e43d2

Browse files
rjl493456442karalabe
authored andcommitted
all: collate new transaction events together
1 parent 6286c25 commit a2e43d2

19 files changed

Lines changed: 165 additions & 111 deletions

File tree

‎accounts/abi/bind/backends/simulated.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ func (fb *filterBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*ty
454454
return logs, nil
455455
}
456456

457-
func (fb *filterBackend) SubscribeTxPreEvent(ch chan<- core.TxPreEvent) event.Subscription {
457+
func (fb *filterBackend) SubscribeTxPreEvent(ch chan<- core.TxsPreEvent) event.Subscription {
458458
return event.NewSubscription(func(quit <-chan struct{}) error {
459459
<-quit
460460
return nil

‎core/events.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import (
2121
"github.com/ethereum/go-ethereum/core/types"
2222
)
2323

24-
// TxPreEvent is posted when a transaction enters the transaction pool.
25-
type TxPreEvent struct{ Tx *types.Transaction }
24+
// TxsPreEvent is posted when a batch of transactions enter the transaction pool.
25+
type TxsPreEvent struct{ Txs types.Transactions }
2626

2727
// PendingLogsEvent is posted pre mining and notifies of pending logs.
2828
type PendingLogsEvent struct {

‎core/tx_journal.go‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func newTxJournal(path string) *txJournal {
5656

5757
// load parses a transaction journal dump from disk, loading its contents into
5858
// the specified pool.
59-
func (journal *txJournal) load(add func(*types.Transaction) error) error {
59+
func (journal *txJournal) load(add func([]*types.Transaction) []error) error {
6060
// Skip the parsing if the journal file doens't exist at all
6161
if _, err := os.Stat(journal.path); os.IsNotExist(err) {
6262
return nil
@@ -76,7 +76,22 @@ func (journal *txJournal) load(add func(*types.Transaction) error) error {
7676
stream := rlp.NewStream(input, 0)
7777
total, dropped := 0, 0
7878

79-
var failure error
79+
// flush imports a batch of transactions and bump the appropriate progress counters
80+
flush := func(txs types.Transactions) {
81+
errs := add(txs)
82+
for _, err := range errs {
83+
if err != nil {
84+
log.Debug("Failed to add journaled transaction", "err", err)
85+
dropped++
86+
}
87+
}
88+
}
89+
90+
var (
91+
failure error
92+
txs types.Transactions
93+
)
94+
8095
for {
8196
// Parse the next transaction and terminate on error
8297
tx := new(types.Transaction)
@@ -86,14 +101,17 @@ func (journal *txJournal) load(add func(*types.Transaction) error) error {
86101
}
87102
break
88103
}
89-
// Import the transaction and bump the appropriate progress counters
104+
txs = append(txs, tx)
90105
total++
91-
if err = add(tx); err != nil {
92-
log.Debug("Failed to add journaled transaction", "err", err)
93-
dropped++
94-
continue
106+
if txs.Len() > 1024 {
107+
flush(txs)
108+
txs = types.Transactions{}
95109
}
96110
}
111+
if txs.Len() > 0 {
112+
flush(txs)
113+
txs = types.Transactions{}
114+
}
97115
log.Info("Loaded local transaction journal", "transactions", total, "dropped", dropped)
98116

99117
return failure

‎core/tx_pool.go‎

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ func NewTxPool(config TxPoolConfig, chainconfig *params.ChainConfig, chain block
238238
if !config.NoLocals && config.Journal != "" {
239239
pool.journal = newTxJournal(config.Journal)
240240

241-
if err := pool.journal.load(pool.AddLocal); err != nil {
241+
if err := pool.journal.load(pool.AddLocals); err != nil {
242242
log.Warn("Failed to load transaction journal", "err", err)
243243
}
244244
if err := pool.journal.rotate(pool.local()); err != nil {
@@ -444,9 +444,9 @@ func (pool *TxPool) Stop() {
444444
log.Info("Transaction pool stopped")
445445
}
446446

447-
// SubscribeTxPreEvent registers a subscription of TxPreEvent and
447+
// SubscribeTxPreEvent registers a subscription of TxsPreEvent and
448448
// starts sending event to the given channel.
449-
func (pool *TxPool) SubscribeTxPreEvent(ch chan<- TxPreEvent) event.Subscription {
449+
func (pool *TxPool) SubscribeTxPreEvent(ch chan<- TxsPreEvent) event.Subscription {
450450
return pool.scope.Track(pool.txFeed.Subscribe(ch))
451451
}
452452

@@ -653,7 +653,7 @@ func (pool *TxPool) add(tx *types.Transaction, local bool) (bool, error) {
653653
log.Trace("Pooled new executable transaction", "hash", hash, "from", from, "to", tx.To())
654654

655655
// We've directly injected a replacement transaction, notify subsystems
656-
go pool.txFeed.Send(TxPreEvent{tx})
656+
go pool.txFeed.Send(TxsPreEvent{types.Transactions{tx}})
657657

658658
return old != nil, nil
659659
}
@@ -715,7 +715,7 @@ func (pool *TxPool) journalTx(from common.Address, tx *types.Transaction) {
715715
// promoteTx adds a transaction to the pending (processable) list of transactions.
716716
//
717717
// Note, this method assumes the pool lock is held!
718-
func (pool *TxPool) promoteTx(addr common.Address, hash common.Hash, tx *types.Transaction) {
718+
func (pool *TxPool) promoteTx(addr common.Address, hash common.Hash, tx *types.Transaction) bool {
719719
// Try to insert the transaction into the pending queue
720720
if pool.pending[addr] == nil {
721721
pool.pending[addr] = newTxList(true)
@@ -729,7 +729,7 @@ func (pool *TxPool) promoteTx(addr common.Address, hash common.Hash, tx *types.T
729729
pool.priced.Removed()
730730

731731
pendingDiscardCounter.Inc(1)
732-
return
732+
return false
733733
}
734734
// Otherwise discard any previous transaction and mark this
735735
if old != nil {
@@ -746,8 +746,7 @@ func (pool *TxPool) promoteTx(addr common.Address, hash common.Hash, tx *types.T
746746
// Set the potentially new pending nonce and notify any subsystems of the new tx
747747
pool.beats[addr] = time.Now()
748748
pool.pendingState.SetNonce(addr, tx.Nonce()+1)
749-
750-
go pool.txFeed.Send(TxPreEvent{tx})
749+
return true
751750
}
752751

753752
// AddLocal enqueues a single transaction into the pool if it is valid, marking
@@ -907,6 +906,7 @@ func (pool *TxPool) removeTx(hash common.Hash, outofbound bool) {
907906
// future queue to the set of pending transactions. During this process, all
908907
// invalidated transactions (low nonce, low balance) are deleted.
909908
func (pool *TxPool) promoteExecutables(accounts []common.Address) {
909+
var promotedTxs types.Transactions
910910
// Gather all the accounts potentially needing updates
911911
if accounts == nil {
912912
accounts = make([]common.Address, 0, len(pool.queue))
@@ -937,11 +937,16 @@ func (pool *TxPool) promoteExecutables(accounts []common.Address) {
937937
queuedNofundsCounter.Inc(1)
938938
}
939939
// Gather all executable transactions and promote them
940-
for _, tx := range list.Ready(pool.pendingState.GetNonce(addr)) {
940+
txs := list.Ready(pool.pendingState.GetNonce(addr))
941+
for _, tx := range txs {
941942
hash := tx.Hash()
942-
log.Trace("Promoting queued transaction", "hash", hash)
943-
pool.promoteTx(addr, hash, tx)
943+
inserted := pool.promoteTx(addr, hash, tx)
944+
if inserted {
945+
log.Trace("Promoting queued transaction", "hash", hash)
946+
promotedTxs = append(promotedTxs, tx)
947+
}
944948
}
949+
945950
// Drop all transactions over the allowed limit
946951
if !pool.locals.contains(addr) {
947952
for _, tx := range list.Cap(int(pool.config.AccountQueue)) {
@@ -957,6 +962,11 @@ func (pool *TxPool) promoteExecutables(accounts []common.Address) {
957962
delete(pool.queue, addr)
958963
}
959964
}
965+
// Notify subsystem for new promoted transactions.
966+
if promotedTxs.Len() > 0 {
967+
pool.txFeed.Send(TxsPreEvent{promotedTxs})
968+
}
969+
960970
// If the pending limit is overflown, start equalizing allowances
961971
pending := uint64(0)
962972
for _, list := range pool.pending {

‎core/tx_pool_test.go‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,21 +118,26 @@ func validateTxPoolInternals(pool *TxPool) error {
118118

119119
// validateEvents checks that the correct number of transaction addition events
120120
// were fired on the pool's event feed.
121-
func validateEvents(events chan TxPreEvent, count int) error {
122-
for i := 0; i < count; i++ {
121+
func validateEvents(events chan TxsPreEvent, count int) error {
122+
received := 0
123+
for {
124+
if received == count {
125+
break
126+
}
123127
select {
124-
case <-events:
128+
case ev := <-events:
129+
received += ev.Txs.Len()
125130
case <-time.After(time.Second):
126-
return fmt.Errorf("event #%d not fired", i)
131+
return fmt.Errorf("event #%d not fired", received)
127132
}
128133
}
129134
select {
130-
case tx := <-events:
131-
return fmt.Errorf("more than %d events fired: %v", count, tx.Tx)
135+
case ev := <-events:
136+
return fmt.Errorf("more than %d events fired: %v", count, ev.Txs)
132137

133138
case <-time.After(50 * time.Millisecond):
134139
// This branch should be "default", but it's a data race between goroutines,
135-
// reading the event channel and pushng into it, so better wait a bit ensuring
140+
// reading the event channel and pushing into it, so better wait a bit ensuring
136141
// really nothing gets injected.
137142
}
138143
return nil
@@ -669,7 +674,7 @@ func TestTransactionGapFilling(t *testing.T) {
669674
pool.currentState.AddBalance(account, big.NewInt(1000000))
670675

671676
// Keep track of transaction events to ensure all executables get announced
672-
events := make(chan TxPreEvent, testTxPoolConfig.AccountQueue+5)
677+
events := make(chan TxsPreEvent, testTxPoolConfig.AccountQueue+5)
673678
sub := pool.txFeed.Subscribe(events)
674679
defer sub.Unsubscribe()
675680

@@ -920,7 +925,7 @@ func TestTransactionPendingLimiting(t *testing.T) {
920925
pool.currentState.AddBalance(account, big.NewInt(1000000))
921926

922927
// Keep track of transaction events to ensure all executables get announced
923-
events := make(chan TxPreEvent, testTxPoolConfig.AccountQueue+5)
928+
events := make(chan TxsPreEvent, testTxPoolConfig.AccountQueue+5)
924929
sub := pool.txFeed.Subscribe(events)
925930
defer sub.Unsubscribe()
926931

@@ -1140,7 +1145,7 @@ func TestTransactionPoolRepricing(t *testing.T) {
11401145
defer pool.Stop()
11411146

11421147
// Keep track of transaction events to ensure all executables get announced
1143-
events := make(chan TxPreEvent, 32)
1148+
events := make(chan TxsPreEvent, 32)
11441149
sub := pool.txFeed.Subscribe(events)
11451150
defer sub.Unsubscribe()
11461151

@@ -1327,7 +1332,7 @@ func TestTransactionPoolUnderpricing(t *testing.T) {
13271332
defer pool.Stop()
13281333

13291334
// Keep track of transaction events to ensure all executables get announced
1330-
events := make(chan TxPreEvent, 32)
1335+
events := make(chan TxsPreEvent, 32)
13311336
sub := pool.txFeed.Subscribe(events)
13321337
defer sub.Unsubscribe()
13331338

@@ -1433,7 +1438,7 @@ func TestTransactionPoolStableUnderpricing(t *testing.T) {
14331438
defer pool.Stop()
14341439

14351440
// Keep track of transaction events to ensure all executables get announced
1436-
events := make(chan TxPreEvent, 32)
1441+
events := make(chan TxsPreEvent, 32)
14371442
sub := pool.txFeed.Subscribe(events)
14381443
defer sub.Unsubscribe()
14391444

@@ -1495,7 +1500,7 @@ func TestTransactionReplacement(t *testing.T) {
14951500
defer pool.Stop()
14961501

14971502
// Keep track of transaction events to ensure all executables get announced
1498-
events := make(chan TxPreEvent, 32)
1503+
events := make(chan TxsPreEvent, 32)
14991504
sub := pool.txFeed.Subscribe(events)
15001505
defer sub.Unsubscribe()
15011506

‎eth/api_backend.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func (b *EthAPIBackend) TxPoolContent() (map[common.Address]types.Transactions,
188188
return b.eth.TxPool().Content()
189189
}
190190

191-
func (b *EthAPIBackend) SubscribeTxPreEvent(ch chan<- core.TxPreEvent) event.Subscription {
191+
func (b *EthAPIBackend) SubscribeTxPreEvent(ch chan<- core.TxsPreEvent) event.Subscription {
192192
return b.eth.TxPool().SubscribeTxPreEvent(ch)
193193
}
194194

‎eth/filters/api.go‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (api *PublicFilterAPI) timeoutLoop() {
104104
// https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_newpendingtransactionfilter
105105
func (api *PublicFilterAPI) NewPendingTransactionFilter() rpc.ID {
106106
var (
107-
pendingTxs = make(chan common.Hash)
107+
pendingTxs = make(chan []common.Hash)
108108
pendingTxSub = api.events.SubscribePendingTxEvents(pendingTxs)
109109
)
110110

@@ -118,7 +118,7 @@ func (api *PublicFilterAPI) NewPendingTransactionFilter() rpc.ID {
118118
case ph := <-pendingTxs:
119119
api.filtersMu.Lock()
120120
if f, found := api.filters[pendingTxSub.ID]; found {
121-
f.hashes = append(f.hashes, ph)
121+
f.hashes = append(f.hashes, ph...)
122122
}
123123
api.filtersMu.Unlock()
124124
case <-pendingTxSub.Err():
@@ -144,13 +144,17 @@ func (api *PublicFilterAPI) NewPendingTransactions(ctx context.Context) (*rpc.Su
144144
rpcSub := notifier.CreateSubscription()
145145

146146
go func() {
147-
txHashes := make(chan common.Hash)
147+
txHashes := make(chan []common.Hash, 128)
148148
pendingTxSub := api.events.SubscribePendingTxEvents(txHashes)
149149

150150
for {
151151
select {
152-
case h := <-txHashes:
153-
notifier.Notify(rpcSub.ID, h)
152+
case hashes := <-txHashes:
153+
// To keep the original behaviour, send a single tx hash in one notification.
154+
// TODO(rjl493456442) Send a batch of tx hashes in one notification
155+
for _, h := range hashes {
156+
notifier.Notify(rpcSub.ID, h)
157+
}
154158
case <-rpcSub.Err():
155159
pendingTxSub.Unsubscribe()
156160
return

‎eth/filters/filter.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type Backend interface {
3636
GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error)
3737
GetLogs(ctx context.Context, blockHash common.Hash) ([][]*types.Log, error)
3838

39-
SubscribeTxPreEvent(chan<- core.TxPreEvent) event.Subscription
39+
SubscribeTxPreEvent(chan<- core.TxsPreEvent) event.Subscription
4040
SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription
4141
SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription
4242
SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription

0 commit comments

Comments
 (0)