Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit 0c77c40

Browse files
committed
Filter by pending status when forwarding transactions to new head candidate
1 parent 4a531f9 commit 0c77c40

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

ethereum/chain.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,16 @@ def _update_head(self, block, forward_pending_transactions=True):
191191
processblock.verify(bc, bc.get_parent())
192192
self.blockchain.put('HEAD', block.hash)
193193
assert self.blockchain.get('HEAD') == block.hash
194-
#log.debug('New head: %s %d\n' % (utils.encode_hex(block.hash), block.number))
195194
self.index.update_blocknumbers(self.head)
195+
assert self.head == block
196+
log.debug('set new head', head=self.head)
196197
self._update_head_candidate(forward_pending_transactions)
197198
if self.new_head_cb and not block.is_genesis():
198199
self.new_head_cb(block)
199200

200201
def _update_head_candidate(self, forward_pending_transactions=True):
201202
"after new head is set"
202-
log.debug('updating head candidate')
203+
log.debug('updating head candidate', head=self.head)
203204
# collect uncles
204205
blk = self.head # parent of the block we are collecting uncles for
205206
uncles = set(u.header for u in self.get_brothers(blk))
@@ -225,12 +226,17 @@ def _update_head_candidate(self, forward_pending_transactions=True):
225226
# add transactions from previous head candidate
226227
old_head_candidate = self.head_candidate
227228
self.head_candidate = head_candidate
228-
if old_head_candidate is not None and forward_pending_transactions:
229-
log.debug('forwarding pending transactions')
230-
for tx in old_head_candidate.get_transactions():
231-
self.add_transaction(tx)
232-
else:
233-
log.debug('discarding pending transactions')
229+
if old_head_candidate is not None:
230+
tx_hashes = self.head.get_transaction_hashes()
231+
pending = [tx for tx in old_head_candidate.get_transactions()
232+
if tx.hash not in tx_hashes]
233+
if pending:
234+
if forward_pending_transactions:
235+
log.debug('forwarding pending transactions', num=len(pending))
236+
for tx in pending:
237+
self.add_transaction(tx)
238+
else:
239+
log.debug('discarding pending transactions', num=len(pending))
234240

235241
def get_uncles(self, block):
236242
"""Return the uncles of `block`."""
@@ -306,7 +312,7 @@ def add_block(self, block, forward_pending_transactions=True):
306312

307313
# set to head if this makes the longest chain w/ most work for that number
308314
if block.chain_difficulty() > self.head.chain_difficulty():
309-
_log.debug('new head')
315+
_log.debug('new head', num_tx=block.num_transactions())
310316
self._update_head(block, forward_pending_transactions)
311317
elif block.number > self.head.number:
312318
_log.warn('has higher blk number than head but lower chain_difficulty',

0 commit comments

Comments
 (0)