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

Commit 3d358a4

Browse files
author
Jan Xie
authored
Merge pull request #732 from gsalgado/txqueue-diff
Fix TransactionQueue.diff()
2 parents b53e828 + 721b9f6 commit 3d358a4

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

ethereum/transaction_queue.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,19 @@ def peek(self, num=None):
4040

4141
def diff(self, txs):
4242
remove_hashes = [tx.hash for tx in txs]
43-
keep = [tx for tx in self.txs if tx.hash not in remove_hashes]
43+
keep = [(prio, tx) for (prio, tx) in self.txs if tx.hash not in remove_hashes]
4444
q = TransactionQueue()
45-
for tx in keep:
46-
q.add_transaction(tx)
45+
q.txs = keep
4746
return q
4847

49-
def test():
48+
49+
def make_test_tx(s=100000, g=50, data=''):
5050
from ethereum.transactions import Transaction
51-
def tx(s, g):
52-
return Transaction(nonce=0, startgas=s, gasprice=g,
53-
value=0, data='', to='\x35' * 20)
51+
return Transaction(nonce=0, startgas=s, gasprice=g,
52+
value=0, data=data, to='\x35' * 20)
53+
54+
55+
def test():
5456
q = TransactionQueue()
5557
# (startgas, gasprice) pairs
5658
params = [(100000, 81), (50000, 74), (40000, 65),
@@ -66,7 +68,7 @@ def tx(s, g):
6668
(999999, 50000, 74)]
6769
# Add transactions to queue
6870
for param in params:
69-
q.add_transaction(tx(param[0], param[1]))
71+
q.add_transaction(make_test_tx(s=param[0], g=param[1]))
7072
# Attempt pops from queue
7173
for (maxgas, expected_s, expected_g) in operations:
7274
tx = q.pop_transaction(max_gas=maxgas)
@@ -75,3 +77,23 @@ def tx(s, g):
7577
else:
7678
assert expected_s is expected_g is None
7779
print('Test successful')
80+
81+
82+
def test_diff():
83+
tx1 = make_test_tx(data='foo')
84+
tx2 = make_test_tx(data='bar')
85+
tx3 = make_test_tx(data='baz')
86+
tx4 = make_test_tx(data='foobar')
87+
q1 = TransactionQueue()
88+
for tx in [tx1, tx2, tx3, tx4]:
89+
q1.add_transaction(tx)
90+
q2 = q1.diff([tx2])
91+
assert len(q2) == 3
92+
assert tx1 in [tx for (_, tx) in q2.txs]
93+
assert tx3 in [tx for (_, tx) in q2.txs]
94+
assert tx4 in [tx for (_, tx) in q2.txs]
95+
96+
q3 = q2.diff([tx4])
97+
assert len(q3) == 2
98+
assert tx1 in [tx for (_, tx) in q3.txs]
99+
assert tx3 in [tx for (_, tx) in q3.txs]

0 commit comments

Comments
 (0)