Skip to content

Commit 32fb9fd

Browse files
committed
add tests for handler reannounce local pending transactions
Signed-off-by: Keefe-Liu <bianze.kernel@gmail.com>
1 parent be9f643 commit 32fb9fd

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

eth/handler_eth_test.go

+43
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,49 @@ func testTransactionPropagation(t *testing.T, protocol uint) {
450450
}
451451
}
452452

453+
// Tests that local pending transactions get propagated to peers.
454+
func TestTransactionPendingReannounce(t *testing.T) {
455+
t.Parallel()
456+
457+
// Create a source handler to send transactions from and a number of sinks
458+
// to receive them. We need multiple sinks since a one-to-one peering would
459+
// broadcast all transactions without announcement.
460+
source := newTestHandler()
461+
defer source.close()
462+
463+
sink := newTestHandler()
464+
defer sink.close()
465+
sink.handler.acceptTxs = 1 // mark synced to accept transactions
466+
467+
sourcePipe, sinkPipe := p2p.MsgPipe()
468+
defer sourcePipe.Close()
469+
defer sinkPipe.Close()
470+
471+
sourcePeer := eth.NewPeer(eth.ETH65, p2p.NewPeer(enode.ID{0}, "", nil), sourcePipe, source.txpool)
472+
sinkPeer := eth.NewPeer(eth.ETH65, p2p.NewPeer(enode.ID{0}, "", nil), sinkPipe, sink.txpool)
473+
defer sourcePeer.Close()
474+
defer sinkPeer.Close()
475+
476+
go source.handler.runEthPeer(sourcePeer, func(peer *eth.Peer) error {
477+
return eth.Handle((*ethHandler)(source.handler), peer)
478+
})
479+
go sink.handler.runEthPeer(sinkPeer, func(peer *eth.Peer) error {
480+
return eth.Handle((*ethHandler)(sink.handler), peer)
481+
})
482+
483+
txs := make([]*types.Transaction, 64)
484+
for nonce := range txs {
485+
tx := types.NewTransaction(uint64(nonce), common.Address{}, big.NewInt(0), 100000, big.NewInt(0), nil)
486+
tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey)
487+
488+
txs[nonce] = tx
489+
}
490+
source.txpool.ReannouceTransactions(txs)
491+
492+
// How to ensure sink has received announce transaction?
493+
494+
}
495+
453496
// Tests that post eth protocol handshake, clients perform a mutual checkpoint
454497
// challenge to validate each other's chains. Hash mismatches, or missing ones
455498
// during a fast sync should lead to the peer getting dropped.

eth/handler_test.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ var (
4848
type testTxPool struct {
4949
pool map[common.Hash]*types.Transaction // Hash map of collected transactions
5050

51-
txFeed event.Feed // Notification feed to allow waiting for inclusion
52-
lock sync.RWMutex // Protects the transaction pool
51+
txFeed event.Feed // Notification feed to allow waiting for inclusion
52+
reannoTxFeed event.Feed // Notification feed to trigger reannouce
53+
lock sync.RWMutex // Protects the transaction pool
5354
}
5455

5556
// newTestTxPool creates a mock transaction pool.
@@ -90,6 +91,12 @@ func (p *testTxPool) AddRemotes(txs []*types.Transaction) []error {
9091
return make([]error, len(txs))
9192
}
9293

94+
// ReannouceTransactions announce the transactions to some peers.
95+
func (p *testTxPool) ReannouceTransactions(txs []*types.Transaction) []error {
96+
p.reannoTxFeed.Send(core.ReannoTxsEvent{Txs: txs})
97+
return make([]error, len(txs))
98+
}
99+
93100
// Pending returns all the transactions known to the pool
94101
func (p *testTxPool) Pending() (map[common.Address]types.Transactions, error) {
95102
p.lock.RLock()
@@ -112,6 +119,12 @@ func (p *testTxPool) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subs
112119
return p.txFeed.Subscribe(ch)
113120
}
114121

122+
// SubscribeReannoTxsEvent should return an event subscription of ReannoTxsEvent and
123+
// send events to the given channel.
124+
func (p *testTxPool) SubscribeReannoTxsEvent(ch chan<- core.ReannoTxsEvent) event.Subscription {
125+
return p.reannoTxFeed.Subscribe(ch)
126+
}
127+
115128
// testHandler is a live implementation of the Ethereum protocol handler, just
116129
// preinitialized with some sane testing defaults and the transaction pool mocked
117130
// out.

0 commit comments

Comments
 (0)