Skip to content

Commit 62d7dac

Browse files
cyyberjleni
authored andcommitted
Transaction Search Fix (#731)
* Transaction Search from Mempool + Block in Buffer * Removing too much logging * Avoid interactive output in tests * Avoid console output in tests
1 parent 6c34514 commit 62d7dac

38 files changed

Lines changed: 95 additions & 65 deletions

.travis.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
- echo "Skip installing requirements.txt when running integration tests"
3333
script:
3434
- "./travis/build.sh"
35-
- sudo pip install -U coverage codacy-coverage
35+
- sudo pip install -U coverage codacy-coverage | cat
3636
- if [[ "$TRAVIS_BRANCH" == "master" ]]; then python-codacy-coverage -r coverage.xml || echo "failed"; fi
3737

3838
- stage: build+test
@@ -71,7 +71,7 @@ jobs:
7171
- python --version
7272
- echo "Skip installing requirements.txt when running integration tests"
7373
script:
74-
- sudo pip install -U docker-compose
74+
- sudo pip install -U docker-compose | cat
7575
- docker-compose --version
7676
- "cd tests_integration; python3 runtest_nodes_synchronize.py"
7777

@@ -85,7 +85,7 @@ jobs:
8585
- python --version
8686
- echo "Skip installing requirements.txt when running integration tests"
8787
script:
88-
- sudo pip install -U docker-compose
88+
- sudo pip install -U docker-compose | cat
8989
- docker-compose --version
9090
- "cd tests_integration; python3 runtest_nodes_1minAfterSync.py"
9191

@@ -100,7 +100,7 @@ jobs:
100100
- echo "Skip installing requirements.txt when using docker builders"
101101
script:
102102
- sudo apt install -y python-dev libssl-dev libffi-dev
103-
- sudo pip install -U pip setuptools twine pyOpenSSL ndg-httpsclient pyasn1
103+
- sudo pip install -U pip setuptools twine pyOpenSSL ndg-httpsclient pyasn1 | cat
104104
- "./travis/build.sh"
105105

106106
deploy:

qrl/core/BlockMetadata.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# coding=utf-8
22
# Distributed under the MIT software license, see the accompanying
33
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
4-
from typing import Dict # noqa
4+
from typing import Dict, Optional # noqa
5+
from pyqrllib.pyqrllib import bin2hstr
56

67
from qrl.core.AddressState import AddressState # noqa
78
from qrl.core.Block import Block
@@ -36,6 +37,9 @@ def __init__(self,
3637
self.hash_chain = hash_chain
3738
self.voted_weight = 0
3839
self.total_stake_amount = 0
40+
self.approved_txns = dict()
41+
for tx in block.transactions:
42+
self.approved_txns[bin2hstr(tx.transaction_hash)] = tx
3943

4044
def update_vote_metadata(self, prev_stake_validators_tracker):
4145
self.total_stake_amount = prev_stake_validators_tracker.get_total_stake_amount()
@@ -73,3 +77,16 @@ def update_stxn_state(self, pstate):
7377
self.address_state_dict[addr].pubhashes == addr_state.pubhashes and \
7478
self.address_state_dict[addr].tokens == addr_state.tokens:
7579
del self.address_state_dict[addr]
80+
81+
def contains_txn(self, transaction_hash: bytes) -> bool:
82+
if bin2hstr(transaction_hash) in self.approved_txns:
83+
return True
84+
85+
return False
86+
87+
def get_txn(self, transaction_hash: bytes) -> Optional[bool]:
88+
txhash = bin2hstr(transaction_hash)
89+
if txhash in self.approved_txns:
90+
return self.approved_txns[txhash]
91+
92+
return None

qrl/core/BufferedChain.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,28 @@ def get_vote_metadata(self, blocknumber: int):
146146
return [self.blocks[blocknumber].voted_weight, self.blocks[blocknumber].total_stake_amount]
147147
return self._chain.pstate.get_vote_metadata(blocknumber)
148148

149-
def get_transaction(self, transaction_hash)->Optional[Transaction]:
149+
def get_blockidx_from_txhash(self, transaction_hash) -> Optional[Transaction]:
150+
answer = self._chain.pstate.get_tx_metadata(transaction_hash)
151+
if answer is not None:
152+
_, block_index = answer
153+
return block_index
154+
155+
for block_idx in self.blocks:
156+
if self.blocks[block_idx].contains_txn(transaction_hash):
157+
return block_idx
158+
159+
return None
160+
161+
def get_transaction(self, transaction_hash) -> Optional[Transaction]:
150162
for tx in self.tx_pool.transaction_pool:
151163
if tx.txhash == transaction_hash:
152164
return tx
165+
166+
for block_idx in self.blocks:
167+
tx = self.blocks[block_idx].get_txn(transaction_hash)
168+
if tx:
169+
return tx
170+
153171
return self._chain.get_transaction(transaction_hash)
154172

155173
def _move_to_mainchain(self) -> bool:

qrl/core/qrlnode.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -353,12 +353,7 @@ def get_block_from_index(self, index: int) -> Block:
353353
return self._buffered_chain.get_block(index)
354354

355355
def get_blockidx_from_txhash(self, transaction_hash):
356-
answer = self.db_state.get_tx_metadata(transaction_hash)
357-
if answer is None:
358-
return None
359-
else:
360-
_, block_index = answer
361-
return block_index
356+
return self._buffered_chain.get_blockidx_from_txhash(transaction_hash)
362357

363358
def get_token_detailed_list(self):
364359
pbdata = self.db_state.get_token_list()

qrl/services/PublicAPIService.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,12 @@ def GetObject(self, request: qrl_pb2.GetObjectReq, context) -> qrl_pb2.GetObject
109109
if transaction is not None:
110110
answer.found = True
111111
block_index = self.qrlnode.get_blockidx_from_txhash(transaction.txhash)
112-
block = self.qrlnode.get_block_from_index(block_index)
113-
txextended = qrl_pb2.TransactionExtended(header=block.blockheader.pbdata,
112+
blockheader = None
113+
if block_index:
114+
block = self.qrlnode.get_block_from_index(block_index)
115+
blockheader = block.blockheader.pbdata
116+
117+
txextended = qrl_pb2.TransactionExtended(header=blockheader,
114118
tx=transaction.pbdata)
115119
answer.transaction.CopyFrom(txextended)
116120
return answer

tests/core/test_BufferedChain.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from qrl.generated import qrl_pb2
2121
from tests.misc.helper import set_wallet_dir, get_alice_xmss, mocked_genesis, get_random_xmss, get_token_transaction, destroy_state
2222

23-
logger.initialize_default(force_console_output=True)
23+
logger.initialize_default()
2424

2525

2626
class TestBufferedChain(TestCase):

tests/core/test_Chain.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from qrl.core.StakeValidatorsTracker import StakeValidatorsTracker
1717
from tests.misc.helper import set_wallet_dir, get_alice_xmss
1818

19-
logger.initialize_default(force_console_output=True)
19+
logger.initialize_default()
2020

2121

2222
class TestChain(TestCase):
@@ -121,5 +121,3 @@ def test_add_many_and_save(self):
121121
self.assertEqual(i, chain.height) # FIXME: wrong name, it is not height but max_index
122122

123123
self.assertTrue(res)
124-
125-
print(qrl.core.config.dev.disk_writes_after_x_blocks)

tests/core/test_DependencyChecker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from qrl.core import logger
1212
from qrl.core.DependencyChecker import DependencyChecker
1313

14-
logger.initialize_default(force_console_output=True)
14+
logger.initialize_default()
1515

1616

1717
class TestDependencyChecker(TestCase):

tests/core/test_GenesisBlock.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from qrl.core import logger
77
from qrl.core.GenesisBlock import GenesisBlock
88

9-
logger.initialize_default(force_console_output=True)
9+
logger.initialize_default()
1010

1111

1212
class TestGenesisBlock(TestCase):

tests/core/test_StakeValidator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from qrl.crypto.xmss import XMSS
1111
from tests.misc.helper import get_alice_xmss
1212

13-
logger.initialize_default(force_console_output=True)
13+
logger.initialize_default()
1414

1515

1616
class TestStakeValidator(TestCase):

0 commit comments

Comments
 (0)