Skip to content
This repository was archived by the owner on Jul 1, 2021. It is now read-only.

Implement eth_getTransactionByHash, eth_getTransactionReceipt JSON RPC function #122

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions trinity/chains/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from eth.rlp.blocks import BaseBlock
from eth.rlp.headers import BlockHeader
from eth.rlp.receipts import Receipt
from eth.rlp.transactions import BaseTransaction


# This class is a work in progress; its main purpose is to define the API of an asyncio-compatible
Expand Down Expand Up @@ -48,6 +49,16 @@ async def coro_get_canonical_block_by_number(self,
block_number: BlockNumber) -> BaseBlock:
pass

@abstractmethod
async def coro_get_canonical_transaction(self,
transaction_hash: Hash32) -> BaseTransaction:
pass

@abstractmethod
async def coro_get_transaction_receipt(self,
transaction_hash: Hash32) -> Receipt:
pass


class BaseAsyncChain(BaseAsyncChainAPI, BaseChain):
pass
2 changes: 2 additions & 0 deletions trinity/chains/coro.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class AsyncChainMixin(BaseAsyncChain):
coro_get_block_by_hash = async_method('get_block_by_hash')
coro_get_block_by_header = async_method('get_block_by_header')
coro_get_canonical_block_by_number = async_method('get_canonical_block_by_number')
coro_get_canonical_transaction = async_method('get_canonical_transaction')
coro_get_transaction_receipt = async_method('get_transaction_receipt')
coro_import_block = async_method('import_block')
coro_validate_chain = async_method('validate_chain')
coro_validate_receipt = async_method('validate_receipt')
11 changes: 10 additions & 1 deletion trinity/chains/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ async def coro_get_canonical_block_by_number(self, block_number: BlockNumber) ->
Raises HeaderNotFound if it is not found.
"""
header = self._headerdb.get_canonical_block_header_by_number(block_number)
return await self.get_block_by_header(header)
return await self.coro_get_block_by_header(header)

def get_canonical_block_hash(self, block_number: BlockNumber) -> Hash32:
return self._headerdb.get_canonical_block_hash(block_number)
Expand Down Expand Up @@ -203,6 +203,15 @@ def create_unsigned_transaction(cls,
def get_canonical_transaction(self, transaction_hash: Hash32) -> BaseTransaction:
raise NotImplementedError("Chain classes must implement " + inspect.stack()[0][3])

async def coro_get_canonical_transaction(self, transaction_hash: Hash32) -> BaseTransaction:
raise NotImplementedError("Chain classes must implement " + inspect.stack()[0][3])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this also provide an implementation?


def get_transaction_receipt(self, transaction_hash: Hash32) -> Receipt:
raise NotImplementedError("Chain classes must implement " + inspect.stack()[0][3])

async def coro_get_transaction_receipt(self, transaction_hash: Hash32) -> Receipt:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this provide an actual implementation?

raise NotImplementedError("Chain classes must implement " + inspect.stack()[0][3])

#
# Execution API
#
Expand Down
15 changes: 15 additions & 0 deletions trinity/rpc/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
from eth.rlp.headers import (
BlockHeader
)
from eth.rlp.logs import (
Log
)
from eth.rlp.receipts import (
Receipt
)
from eth.rlp.transactions import (
BaseTransaction
)
Expand All @@ -51,6 +57,15 @@ def transaction_to_dict(transaction: BaseTransaction) -> Dict[str, str]:
)


def receipt_to_dict(receipt: Receipt) -> Dict[str, Union[str, List[Log]]]:
return dict(
state_root=encode_hex(receipt.state_root),
gas_used=hex(receipt.gas_used),
bloom=hex(receipt.bloom),
logs=receipt.logs,
)


hexstr_to_int = functools.partial(int, base=16)


Expand Down
16 changes: 16 additions & 0 deletions trinity/rpc/modules/eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
from eth.rlp.headers import (
BlockHeader
)
from eth.rlp.logs import (
Log
)
from eth.vm.spoof import (
SpoofTransaction,
)
Expand All @@ -52,6 +55,7 @@
header_to_dict,
format_params,
normalize_transaction_dict,
receipt_to_dict,
to_int_if_hex,
transaction_to_dict,
)
Expand Down Expand Up @@ -247,6 +251,18 @@ async def getTransactionCount(self, address: Address, at_block: Union[str, int])
nonce = account_db.get_nonce(address)
return hex(nonce)

@format_params(decode_hex)
async def getTransactionByHash(self, transaction_hash: Hash32) -> Dict[str, str]:
transaction = self.chain.coro_get_canonical_transaction(transaction_hash)
return transaction_to_dict(transaction)

@format_params(decode_hex)
async def getTransactionReceipt(
self,
transaction_hash: Hash32) -> Dict[str, Union[str, List[Log]]]:
receipt = self.chain.coro_get_transaction_receipt(transaction_hash)
return receipt_to_dict(receipt)

@format_params(decode_hex)
async def getUncleCountByBlockHash(self, block_hash: Hash32) -> str:
block = await self.chain.coro_get_block_by_hash(block_hash)
Expand Down