go-ethereum icon indicating copy to clipboard operation
go-ethereum copied to clipboard

Speeding up `eth_getTransactionReceipt`

Open fjl opened this issue 2 years ago • 4 comments

Here's an idea for speeding up RPC method eth_getTransactionReceipt, i.e. the operation that loads a single receipt by txhash. Right now, this operation is very slow because it has to:

  1. load inclusion block from the txhash -> block index
  2. load the full block from the database
  3. find the tx in the block
  4. verify tx signature and derive the sender
  5. load/parse all receipts in the block
  6. derive non-consensus fields for all receipts
  7. return receipt n

Some of these steps should be avoidable. I propose that, in the tx inclusion index (txhash -> block) we should also store the tx sender, as well as other information required to derive receipt fields. If this information was available, the steps for resolving a single receipts would be:

  1. load the inclusion block, sender, etc. from the txhash -> block index
  2. load the full block from the database
  3. find the tx in the block
  4. load all receipts in the block
  5. find receipt n
  6. derive fields of receipt n
  7. return it

This doesn't look very different, but there is another advantage: we wouldn't need to actually parse the receipts just to load the nth one. Decoding receipts is slow, and a bit of time can be saved by not doing it. Unrelated derivation of receipt fields is also avoided.

fjl avatar Mar 24 '23 14:03 fjl

More about the additional content of the txhash -> number index. In order to derive the non-persistent fields of a single receipt, we require these new fields in the index:

  • txIndex: the index of the transaction in the block.
  • blockLogCount: the count of logs in the block up to this receipt. This is needed to assign the index of logs corrrectly.
  • gasUsed: the gas used by a tx is not stored in the receipt. Instead, we store the cumulative amount of used block gas at the end of the transaction. In the index, we could simply store gasUsed directly. This might also be helpful for other use cases of the index.
  • txType: the type byte of the transaction

fjl avatar Mar 28 '23 09:03 fjl

@fjl is this still open for collaboration?

ameya-deshmukh avatar Jun 20 '23 22:06 ameya-deshmukh

Yeah sure, nobody has worked on this. I just posted it as an idea.

fjl avatar Jun 22 '23 13:06 fjl

Cool, thanks! Will start working on this

ameya-deshmukh avatar Jun 26 '23 07:06 ameya-deshmukh

https://github.com/ethereum/go-ethereum/pull/32063#issuecomment-2990298842

omerfirmak avatar Jun 20 '25 10:06 omerfirmak