Skip to content

Commit c06b1de

Browse files
committed
Add new APIs to allow efficient retrieval of receipts
1 parent bc476b2 commit c06b1de

File tree

3 files changed

+79
-9
lines changed

3 files changed

+79
-9
lines changed

eth/abc.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ def get_canonical_block_header_by_number(self, block_number: BlockNumber) -> Blo
440440
"""
441441
Return the block header with the given number in the canonical chain.
442442
443-
Raise ``BlockNotFound`` if there's no block header with the given number in the
443+
Raise ``HeaderNotFound`` if there's no block header with the given number in the
444444
canonical chain.
445445
"""
446446
...
@@ -2876,6 +2876,16 @@ def get_block_header_by_hash(self, block_hash: Hash32) -> BlockHeaderAPI:
28762876
"""
28772877
...
28782878

2879+
@abstractmethod
2880+
def get_canonical_block_header_by_number(self, block_number: BlockNumber) -> BlockHeaderAPI:
2881+
"""
2882+
Return the block header with the given number in the canonical chain.
2883+
2884+
Raise ``HeaderNotFound`` if there's no block header with the given number in the
2885+
canonical chain.
2886+
"""
2887+
...
2888+
28792889
@abstractmethod
28802890
def get_canonical_head(self) -> BlockHeaderAPI:
28812891
"""
@@ -2986,6 +2996,17 @@ def create_unsigned_transaction(cls,
29862996
"""
29872997
...
29882998

2999+
@abstractmethod
3000+
def get_canonical_transaction_index(self, transaction_hash: Hash32) -> Tuple[BlockNumber, int]:
3001+
"""
3002+
Return a 2-tuple of (block_number, transaction_index) indicating which
3003+
block the given transaction can be found in and at what index in the
3004+
block transactions.
3005+
3006+
Raise ``TransactionNotFound`` if the transaction does not exist in the canoncial
3007+
chain.
3008+
"""
3009+
29893010
@abstractmethod
29903011
def get_canonical_transaction(self, transaction_hash: Hash32) -> SignedTransactionAPI:
29913012
"""
@@ -2997,6 +3018,19 @@ def get_canonical_transaction(self, transaction_hash: Hash32) -> SignedTransacti
29973018
"""
29983019
...
29993020

3021+
@abstractmethod
3022+
def get_canonical_transaction_by_index(self,
3023+
block_number: BlockNumber,
3024+
index: int) -> SignedTransactionAPI:
3025+
"""
3026+
Return the requested transaction as specified by the ``block_number``
3027+
and ``index`` from the canonical chain.
3028+
3029+
Raise ``TransactionNotFound`` if no transaction exists at ``index`` at ``block_number`` in
3030+
the canonical chain.
3031+
"""
3032+
...
3033+
30003034
@abstractmethod
30013035
def get_transaction_receipt(self, transaction_hash: Hash32) -> ReceiptAPI:
30023036
"""
@@ -3007,6 +3041,17 @@ def get_transaction_receipt(self, transaction_hash: Hash32) -> ReceiptAPI:
30073041
"""
30083042
...
30093043

3044+
@abstractmethod
3045+
def get_transaction_receipt_by_index(self, block_number: BlockNumber, index: int) -> ReceiptAPI:
3046+
"""
3047+
Return the requested receipt for the transaction as specified by the ``block_number``
3048+
and ``index``.
3049+
3050+
Raise ``ReceiptNotFound`` if not receipt for the specified ``block_number`` and ``index`` is
3051+
found in the canonical chain.
3052+
"""
3053+
...
3054+
30103055
#
30113056
# Execution API
30123057
#

eth/chains/base.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ def get_block_header_by_hash(self, block_hash: Hash32) -> BlockHeaderAPI:
263263
validate_word(block_hash, title="Block Hash")
264264
return self.chaindb.get_block_header_by_hash(block_hash)
265265

266+
def get_canonical_block_header_by_number(self, block_number: BlockNumber) -> BlockHeaderAPI:
267+
return self.chaindb.get_canonical_block_header_by_number(block_number)
268+
266269
def get_canonical_head(self) -> BlockHeaderAPI:
267270
return self.chaindb.get_canonical_head()
268271

@@ -337,15 +340,13 @@ def build_block_with_transactions(
337340
#
338341
# Transaction API
339342
#
343+
def get_canonical_transaction_index(self, transaction_hash: Hash32) -> Tuple[BlockNumber, int]:
344+
return self.chaindb.get_transaction_index(transaction_hash)
345+
340346
def get_canonical_transaction(self, transaction_hash: Hash32) -> SignedTransactionAPI:
341347
(block_num, index) = self.chaindb.get_transaction_index(transaction_hash)
342-
VM_class = self.get_vm_class_for_block_number(block_num)
343348

344-
transaction = self.chaindb.get_transaction_by_index(
345-
block_num,
346-
index,
347-
VM_class.get_transaction_class(),
348-
)
349+
transaction = self.get_canonical_transaction_by_index(block_num, index)
349350

350351
if transaction.hash == transaction_hash:
351352
return transaction
@@ -355,6 +356,18 @@ def get_canonical_transaction(self, transaction_hash: Hash32) -> SignedTransacti
355356
f"instead of {encode_hex(transaction_hash)} in block {block_num} at {index}"
356357
)
357358

359+
def get_canonical_transaction_by_index(self,
360+
block_number: BlockNumber,
361+
index: int) -> SignedTransactionAPI:
362+
363+
VM_class = self.get_vm_class_for_block_number(block_number)
364+
365+
return self.chaindb.get_transaction_by_index(
366+
block_number,
367+
index,
368+
VM_class.get_transaction_class(),
369+
)
370+
358371
def create_transaction(self, *args: Any, **kwargs: Any) -> SignedTransactionAPI:
359372
return self.get_vm().create_transaction(*args, **kwargs)
360373

@@ -379,9 +392,15 @@ def get_transaction_receipt(self, transaction_hash: Hash32) -> ReceiptAPI:
379392
transaction_block_number, transaction_index = self.chaindb.get_transaction_index(
380393
transaction_hash,
381394
)
395+
return self.get_transaction_receipt_by_index(transaction_block_number, transaction_index)
396+
397+
def get_transaction_receipt_by_index(self,
398+
block_number: BlockNumber,
399+
index: int) -> ReceiptAPI:
400+
382401
receipt = self.chaindb.get_receipt_by_index(
383-
block_number=transaction_block_number,
384-
receipt_index=transaction_index,
402+
block_number=block_number,
403+
receipt_index=index,
385404
)
386405

387406
return receipt

newsfragments/1887.feature.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Add new Chain APIs:
2+
3+
- `get_canonical_block_header_by_number`
4+
- `get_canonical_transaction_index`
5+
- `get_canonical_transaction_by_index`
6+
- `get_transaction_receipt_by_index`

0 commit comments

Comments
 (0)