Add comparison handling to Block, fix reward type

This commit is contained in:
Michał Sałaban 2020-01-24 12:38:33 +01:00
parent 1b503fd0ab
commit c6256fe4e1
5 changed files with 83 additions and 6 deletions

View File

@ -115,7 +115,7 @@ class JSONRPCDaemon(object):
'nonce': bhdr['nonce'],
'orphan': bhdr['orphan_status'],
'prev_hash': bhdr['prev_hash'],
'reward': bhdr['reward'],
'reward': from_atomic(bhdr['reward']),
'transactions': self.transactions(
[bhdr['miner_tx_hash']] + sub_json['tx_hashes']),
}

View File

@ -25,10 +25,17 @@ class Block(object):
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)
setattr(self, k, kwargs.get(k, getattr(self, k)))
self.orphan = kwargs.get('orphan', self.orphan)
self.transactions = kwargs.get('transactions', self.transactions or [])
self.blob = kwargs.get('blob', self.blob)
def __eq__(self, other):
if isinstance(other, Block):
return self.hash == other.hash
elif isinstance(other, six.string_types):
return six.ensure_text(self.hash) == six.ensure_text(other)
return super(Block, self).__eq__(other)
def __contains__(self, tx):
if isinstance(tx, six.string_types):

68
tests/test_block.py Normal file
View File

@ -0,0 +1,68 @@
from datetime import datetime
import six
import unittest
from monero.block import Block
from monero.numbers import from_atomic
from monero.transaction import Transaction
class BlockTestCase(unittest.TestCase):
def setUp(self):
self.tx1 = Transaction(
hash="7e5fea8470c5771315bab4b3c77493d2ff534f5201c7c6b2bab069cb7d21ce7b")
self.tx2 = Transaction(
hash="3a2f859dea9d2ad5ecec167719302d4e14e21beef9b74f9583183d8e965d9106")
self.tx3 = Transaction(
hash="bde2b5344b63cbe58ce1a724d0a2276aaa4266be5235d5e5fde969446c3e8de1")
self.tx4 = Transaction(
hash="24fb42f9f324082658524b29b4cf946a9f5fcfa82194070e2f17c1875e15d5d0")
self.block1 = Block(
hash="423cd4d170c53729cf25b4243ea576d1e901d86e26c06d6a7f79815f3fcb9a89",
height=451992,
difficulty=3590,
version= (11,12),
nonce=140046906,
orphan=False,
prev_hash="51f6816891b6a7adedd0f1ad57a846eada1baac476421aa9d32d0630ce3dce41",
reward=from_atomic(15331952645334),
timestamp=datetime.fromtimestamp(1573646422),
transactions=[self.tx1, self.tx2, self.tx3, self.tx4])
self.block1_duplicate = Block(
hash="423cd4d170c53729cf25b4243ea576d1e901d86e26c06d6a7f79815f3fcb9a89",
height=451992,
difficulty=3590,
version= (11,12),
nonce=140046906,
orphan=False,
prev_hash="51f6816891b6a7adedd0f1ad57a846eada1baac476421aa9d32d0630ce3dce41",
reward=from_atomic(15331952645334),
timestamp=datetime.fromtimestamp(1573646422),
transactions=[self.tx1, self.tx2, self.tx3, self.tx4])
def test_basic_ops(self):
self.assertIsNot(self.block1, self.block1_duplicate)
self.assertEqual(self.block1, self.block1_duplicate)
self.assertEqual(self.block1, self.block1.hash)
self.assertEqual(self.block1, self.block1.hash)
self.assertNotEqual(self.block1, 1)
def test_tx_membership(self):
self.assertIn(self.tx1, self.block1)
self.assertIn(self.tx2, self.block1)
self.assertIn(self.tx3, self.block1)
self.assertIn(self.tx4, self.block1)
self.assertIn(self.tx1, self.block1_duplicate)
self.assertIn(self.tx2, self.block1_duplicate)
self.assertIn(self.tx3, self.block1_duplicate)
self.assertIn(self.tx4, self.block1_duplicate)
def test_tx_hash_membership(self):
self.assertIn(self.tx1.hash, self.block1)
self.assertIn(self.tx2.hash, self.block1)
self.assertIn(self.tx3.hash, self.block1)
self.assertIn(self.tx4.hash, self.block1)
self.assertIn(self.tx1.hash, self.block1_duplicate)
self.assertIn(self.tx2.hash, self.block1_duplicate)
self.assertIn(self.tx3.hash, self.block1_duplicate)
self.assertIn(self.tx4.hash, self.block1_duplicate)

View File

@ -1,3 +1,4 @@
import decimal
import responses
from monero.const import NET_STAGE
@ -69,6 +70,7 @@ class JSONRPCDaemonTestCase(JSONTestCase):
blk.hash,
"423cd4d170c53729cf25b4243ea576d1e901d86e26c06d6a7f79815f3fcb9a89")
self.assertEqual(blk.height, 451992)
self.assertIsInstance(blk.reward, decimal.Decimal)
self.assertIn("24fb42f9f324082658524b29b4cf946a9f5fcfa82194070e2f17c1875e15d5d0", blk)
for tx in blk.transactions:

View File

@ -6,7 +6,7 @@ import unittest
from monero.address import address
from monero.numbers import PaymentID
from monero.transaction import IncomingPayment, OutgoingPayment, Transaction, _ByHeight
from monero.transaction import IncomingPayment, Transaction, _ByHeight
class FiltersTestCase(unittest.TestCase):
def setUp(self):