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,

42
monero/block.py Normal file
View file

@ -0,0 +1,42 @@
import operator
import six
from .transaction import Transaction
class Block(object):
"""
A Monero block. Identified by `hash` (optionaly by `height`).
This class is not intended to be turned into objects by the user,
it is used by backends.
"""
hash = None
height = None
timestamp = None
version = None
difficulty = None
nonce = None
orphan = False
prev_hash = None
reward = None
blob = None
transactions = None
def __init__(self, **kwargs):
for k in ('hash', 'height', 'timestamp', 'version', 'difficulty', 'nonce', 'prev_hash', 'reward'):
setattr(self, k, kwargs[k])
self.orphan = kwargs['orphan']
self.transactions = kwargs['transactions']
self.blob = kwargs.get('blob', None)
def __contains__(self, tx):
if isinstance(tx, six.string_types):
txid = tx
elif isinstance(tx, Transaction):
txid = tx.hash
else:
raise ValueError(
"Only Transaction or hash strings may be used to test existence in Blocks, "
"got '{:s}'".format(tx))
return txid in map(operator.attrgetter('hash'), self.transactions)

View file

@ -58,6 +58,19 @@ class Daemon(object):
"""
return self._backend.headers(start_height, end_height)
def block(self, bhash=None, height=None):
"""
Returns a block of specified height or hash.
:param str bhash: block hash, or
:param int height: block height
:rtype: :class:`Block <monero.block.Block>`
"""
if not height and not bhash:
raise ValueError("Height or hash must be specified")
return self._backend.block(bhash=bhash, height=height)
def transactions(self, hashes):
"""
Returns transactions matching given hashes. Accepts single hash or a sequence.