diff --git a/setup.py b/setup.py index d421945..6fd5001 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ with open("README.md", "r") as fh: setuptools.setup( name='chainwalkers_utils', - version='0.0.6', + version='0.0.7', description='Collection of utilities to be used across chainwalkers repos', long_description=long_description, long_description_content_type="text/markdown", diff --git a/tendermint_utils/__init__.py b/tendermint_utils/__init__.py index 6e9328b..c34719a 100644 --- a/tendermint_utils/__init__.py +++ b/tendermint_utils/__init__.py @@ -1 +1 @@ -from tendermint.rpc import TendermintRPC \ No newline at end of file +from tendermint_utils.rpc import TendermintRPC \ No newline at end of file diff --git a/tendermint_utils/rpc.py b/tendermint_utils/rpc.py index c88636d..73ede02 100644 --- a/tendermint_utils/rpc.py +++ b/tendermint_utils/rpc.py @@ -1,6 +1,5 @@ import json import requests - class TendermintRPC: def __init__(self, node_url): @@ -20,29 +19,7 @@ class TendermintRPC: response = requests.get(self.node_url + '/block?height=' + str(height)) response.raise_for_status() data = response.json() - block = self.init_block(data['result']) - - block_results = self.get_block_results(height) - - # Capture transactions and underlying events - block_transactions = self.get_transactions_by_block(height) - if block_transactions['txs']: - for tx in block_transactions['txs']: - block_tx = self.get_transactions_by_hash(tx['hash']) - block.add_transaction(block_tx) - - # Capture begin block events () - if block_results['results']['begin_block']: - for event in block_results['results']['begin_block']['events']: - block.begin_block.append(event) - - block.end_block = block_results['results']['end_block'] - - # block_validators = self.get_block_validators(height) - # for validator in block_validators['validators']: - # block.validators.append(validator) - - return block + return data['result'] except Exception as err: print(f'An error occured retrieving block: {err}') @@ -55,7 +32,6 @@ class TendermintRPC: except Exception as err: print(f'An error occured retrieving the results of block height: {err}') - # Need ANKR to turn on indexing at node level for this call to work properly def get_transactions_by_block(self, height): try: response = requests.get(self.node_url + '/tx_search?query=\"tx.height=' + str(height) + '\"&prove=true') @@ -65,8 +41,10 @@ class TendermintRPC: except Exception as err: print(f'An error occured retrieving the transactions in block: {err}') - def get_transactions_by_hash(self, tx_hash): + def get_transactions_by_hash(self, tx_hash, hex_prefix=False): try: + if hex_prefix: + tx_hash = '0x' + tx_hash response = requests.get(self.node_url + '/tx?hash=' + tx_hash) response.raise_for_status() data = response.json() @@ -74,7 +52,6 @@ class TendermintRPC: except Exception as err: print(f'An error occured retrieving the transaction by hash: {err}') - # Currently returning the following response "Height must be less than or equal to the current blockchain height" def get_block_validators(self, height): try: response = requests.get(self.node_url + '/validators?height=' + str(height)) @@ -83,6 +60,3 @@ class TendermintRPC: return data['result'] except Exception as err: print(f'An error occured retrieving the validators for block height: {err}') - - def init_block(self, blockDict): - return BlockSchema(blockDict) \ No newline at end of file diff --git a/tendermint_utils/schema.py b/tendermint_utils/schema.py deleted file mode 100644 index e3883be..0000000 --- a/tendermint_utils/schema.py +++ /dev/null @@ -1,40 +0,0 @@ -class BlockSchema(object): - - def __init__(self, data): - self.hash = data['block_meta']['hash'] - self.chain_id = data['block_meta']['header']['chain_id'] - self.height = data['block_meta']['header']['height'] - self.timestamp = data['block_meta']['header']['time'] - self.parts = data['block_meta']['block_id']['parts'] - self.num_txs = data['block_meta']['header']['num_txs'] - self.total_txs_onchain = data['block_meta']['header']['total_txs'] - self.parent_hash = data['block_meta']['header']['last_block_id']['hash'] - self.last_commit_hash = data['block_meta']['header']['last_commit_hash'] - self.data_hash = data['block_meta']['header']['data_hash'] - self.validators_hash = data['block_meta']['header']['validators_hash'] - self.next_validators_hash = data['block_meta']['header']['next_validators_hash'] - self.consensus_hash = data['block_meta']['header']['consensus_hash'] - self.app_hash = data['block_meta']['header']['app_hash'] - self.last_results_hash = data['block_meta']['header']['last_results_hash'] - self.evidence_hash = data['block_meta']['header']['evidence_hash'] - self.proposer_address = data['block_meta']['header']['proposer_address'] - self.encoded_txs = data['block']['data']['txs'] - self.evidence = data['block']['evidence']['evidence'] - self.transactions = [] - self.precommits = data['block']['last_commit']['precommits'] - self.validators =[] - self.begin_block = [] - self.end_block = {} - - def add_transaction(self, transaction): - self.transactions.append(transaction) - - def get_transactions(self): - return self.transactions - - def __repr__(self): - header = "block height: %d, tx count %d" % (int(self.height), len(self.transactions)) - transactions = [] - for tx in self.transactions: - transactions.append(str(tx)) - return "\n".join([header] + transactions)