Add Block class and handling

This commit is contained in:
Michał Sałaban 2020-01-22 11:37:03 +01:00
parent ccf5066739
commit 1b503fd0ab
6 changed files with 250 additions and 0 deletions

View file

@ -9,6 +9,7 @@ import requests
from .. import exceptions
from ..account import Account
from ..address import address, Address, SubAddress
from ..block import Block
from ..const import NET_MAIN, NET_TEST, NET_STAGE
from ..numbers import from_atomic, to_atomic, PaymentID
from ..seed import Seed
@ -94,6 +95,33 @@ class JSONRPCDaemon(object):
return res['headers']
raise exceptions.BackendException(res['status'])
def block(self, bhash=None, height=None):
data = {}
if bhash:
data['hash'] = bhash
if height:
data['height'] = height
res = self.raw_jsonrpc_request('get_block', data)
if res['status'] == 'OK':
bhdr = res['block_header']
sub_json = json.loads(res['json'])
data = {
'blob': res['blob'],
'hash': bhdr['hash'],
'height': bhdr['height'],
'timestamp': datetime.fromtimestamp(bhdr['timestamp']),
'version': (bhdr['major_version'], bhdr['minor_version']),
'difficulty': bhdr['difficulty'],
'nonce': bhdr['nonce'],
'orphan': bhdr['orphan_status'],
'prev_hash': bhdr['prev_hash'],
'reward': bhdr['reward'],
'transactions': self.transactions(
[bhdr['miner_tx_hash']] + sub_json['tx_hashes']),
}
return Block(**data)
raise exceptions.BackendException(res['status'])
def transactions(self, hashes):
res = self.raw_request('/get_transactions', {
'txs_hashes': hashes,