Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit ceb0d18

Browse files
committed
Added Block.get_receipts() and formatting
1 parent 633aa1d commit ceb0d18

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

ethereum/blocks.py

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import time
2+
from itertools import count
3+
import sys
24
import rlp
35
from rlp.sedes import big_endian_int, Binary, binary, CountableList
4-
from rlp.utils import decode_hex, encode_hex, str_to_bytes
6+
from rlp.utils import decode_hex, encode_hex
57
from ethereum import trie
68
from ethereum.trie import Trie
79
from ethereum.securetrie import SecureTrie
@@ -10,7 +12,6 @@
1012
from ethereum import processblock
1113
from ethereum.transactions import Transaction
1214
from ethereum import bloom
13-
import sys
1415

1516
if sys.version_info.major == 2:
1617
from repoze.lru import lru_cache
@@ -95,21 +96,26 @@ def calc_difficulty(parent, timestamp):
9596
# Auxiliary value for must_* error messages
9697
aux = [None]
9798

99+
98100
def set_aux(auxval):
99101
aux[0] = auxval
100102

103+
101104
def must(what, f, symb, a, b):
102105
if not f(a, b):
103106
if aux[0]:
104107
sys.stderr.write('%r' % aux[0])
105108
raise VerificationFailed(what, a, symb, b)
106109

110+
107111
def must_equal(what, a, b):
108112
return must(what, lambda x, y: x == y, "==", a, b)
109113

114+
110115
def must_ge(what, a, b):
111116
return must(what, lambda x, y: x >= y, ">=", a, b)
112117

118+
113119
def must_le(what, a, b):
114120
return must(what, lambda x, y: x <= y, "<=", a, b)
115121

@@ -390,6 +396,7 @@ def decorator(cls):
390396
def make_gs_etter(source, attribute):
391397
def getter(self):
392398
return getattr(getattr(self, source), attribute)
399+
393400
def setter(self, value):
394401
setattr(getattr(self, source), attribute, value)
395402
return getter, setter
@@ -403,7 +410,7 @@ def setter(self, value):
403410

404411

405412
@mirror_from('header', set(field for field, _ in BlockHeader.fields) -
406-
set(['state_root', 'receipts_root', 'tx_list_root']),
413+
set(['state_root', 'receipts_root', 'tx_list_root']),
407414
only_getters=False)
408415
class Block(rlp.Serializable):
409416
"""A block.
@@ -430,7 +437,6 @@ class Block(rlp.Serializable):
430437
('uncles', CountableList(BlockHeader))
431438
]
432439

433-
434440
def __init__(self, header, transaction_list=[], uncles=[], db=None,
435441
parent=None, making=False):
436442
if db is None:
@@ -522,21 +528,17 @@ def __init__(self, header, transaction_list=[], uncles=[], db=None,
522528
if parent:
523529
must_equal('prev_hash', self.prevhash, parent.hash)
524530
must_ge('gas_limit', self.gas_limit,
525-
parent.gas_limit * (GASLIMIT_ADJMAX_FACTOR - 1) //
526-
GASLIMIT_ADJMAX_FACTOR)
531+
parent.gas_limit * (GASLIMIT_ADJMAX_FACTOR - 1) // GASLIMIT_ADJMAX_FACTOR)
527532
must_le('gas_limit', self.gas_limit,
528-
parent.gas_limit * (GASLIMIT_ADJMAX_FACTOR + 1) //
529-
GASLIMIT_ADJMAX_FACTOR)
533+
parent.gas_limit * (GASLIMIT_ADJMAX_FACTOR + 1) // GASLIMIT_ADJMAX_FACTOR)
530534
must_equal('gas_used', original_values['gas_used'], self.gas_used)
531535
must_equal('timestamp', self.timestamp, original_values['timestamp'])
532536
must_equal('difficulty', self.difficulty, original_values['difficulty'])
533537
must_equal('uncles_hash', utils.sha3(rlp.encode(uncles)), original_values['uncles_hash'])
534538
assert header.block is None
535539
must_equal('state_root', self.state.root_hash, header.state_root)
536-
must_equal('tx_list_root', self.transactions.root_hash,
537-
header.tx_list_root)
538-
must_equal('receipts_root', self.receipts.root_hash,
539-
header.receipts_root)
540+
must_equal('tx_list_root', self.transactions.root_hash, header.tx_list_root)
541+
must_equal('receipts_root', self.receipts.root_hash, header.receipts_root)
540542
must_equal('bloom', self.bloom, original_values['bloom'])
541543
set_aux(None)
542544

@@ -553,8 +555,7 @@ def __init__(self, header, transaction_list=[], uncles=[], db=None,
553555
if not self.state.root_hash_valid():
554556
raise ValueError("State Merkle root of block %r not found in "
555557
"database" % self)
556-
if (not self.is_genesis() and self.nonce and
557-
not self.header.check_pow()):
558+
if (not self.is_genesis() and self.nonce and not self.header.check_pow()):
558559
raise ValueError("PoW check failed")
559560
self.db.put('validated:'+self.hash, '1')
560561

@@ -826,7 +827,20 @@ def get_receipt(self, num):
826827
:returns: an instance of :class:`Receipt`
827828
"""
828829
index = rlp.encode(num)
829-
return rlp.decode(self.receipts.get(index), Receipt)
830+
receipt = self.receipts.get(index)
831+
if receipt == trie.BLANK_NODE:
832+
raise IndexError('Receipt does not exist')
833+
else:
834+
return rlp.decode(receipt, Receipt)
835+
836+
def get_receipts(self):
837+
"""Build a list of all receipts in this block."""
838+
receipts = []
839+
for i in count():
840+
try:
841+
receipts.append(self.get_receipt(i))
842+
except IndexError:
843+
return receipts
830844

831845
def get_nonce(self, address):
832846
"""Get the nonce of an account.
@@ -1171,8 +1185,7 @@ def to_dict(self, with_state=False, full_transactions=False,
11711185
if with_state:
11721186
state_dump = {}
11731187
for address, v in self.state.to_dict().items():
1174-
state_dump[encode_hex(address)] = \
1175-
self.account_to_dict(address, with_storage_roots)
1188+
state_dump[encode_hex(address)] = self.account_to_dict(address, with_storage_roots)
11761189
b['state'] = state_dump
11771190
if with_uncles:
11781191
b['uncles'] = [self.__class__.deserialize_header(u)
@@ -1236,8 +1249,7 @@ def chain_difficulty(self):
12361249

12371250
def __eq__(self, other):
12381251
"""Two blocks are equal iff they have the same hash."""
1239-
return isinstance(other, (Block, CachedBlock)) and \
1240-
self.hash == other.hash
1252+
return isinstance(other, (Block, CachedBlock)) and self.hash == other.hash
12411253

12421254
def __hash__(self):
12431255
return utils.big_endian_to_int(self.hash)
@@ -1257,10 +1269,12 @@ def __repr__(self):
12571269
def __structlog__(self):
12581270
return encode_hex(self.hash)
12591271

1272+
12601273
@lru_cache(5)
12611274
def get_cache_memoized(seedhash, size):
12621275
return mkcache(size, seedhash)
12631276

1277+
12641278
# Gas limit adjustment algo
12651279
def calc_gaslimit(parent):
12661280
decay = parent.gas_limit // GASLIMIT_EMA_FACTOR
@@ -1370,9 +1384,8 @@ def genesis(db, start_alloc=GENESIS_INITIAL_ALLOC, difficulty=GENESIS_DIFFICULTY
13701384
block.set_nonce(addr, int(data['nonce']))
13711385
if 'storage' in data:
13721386
for k, v in data['storage'].items():
1373-
block.set_storage_data(addr,
1374-
utils.big_endian_to_int(decode_hex(k[2:])),
1375-
utils.big_endian_to_int(decode_hex(v[2:])))
1387+
block.set_storage_data(addr, utils.big_endian_to_int(decode_hex(k[2:])),
1388+
utils.big_endian_to_int(decode_hex(v[2:])))
13761389
block.commit_state()
13771390
block.state.db.commit()
13781391
# genesis block has predefined state root (so no additional finalization

0 commit comments

Comments
 (0)