Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix log index in transaction receipt #6206

Merged
merged 8 commits into from
Nov 29, 2023

Conversation

Wetitpig
Copy link
Contributor

PR description

Assign log index to transaction receipt according to position in block instead of in transaction.

Fixed Issue(s)

fixes #6204

Copy link

  • I thought about documentation and added the doc-change-required label to this PR if updates are required.
  • I thought about the changelog and included a changelog update if required.
  • If my PR includes database changes (e.g. KeyValueSegmentIdentifier) I have thought about compatibility and performed forwards and backwards compatibility tests

@Wetitpig Wetitpig changed the title [#6204] Fix log index in transaction receipt Fix log index in transaction receipt Nov 23, 2023
final Transaction transaction =
block.getBody().getTransactions().get(location.getTransactionIndex());
final List<Transaction> transactions = block.getBody().getTransactions();
final Transaction transaction = transactions.get(location.getTransactionIndex());
Copy link
Contributor

Choose a reason for hiding this comment

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

As a "fix it now" patch this seems like a reasonable solution. However there will be performance implications with large blocks. I think a longer term solution would be to add the log index into the TransactionLocation and save that when calculating the location. The data can be written so the value may be missing, and when it is we use this logic. Creating a seamless transition. We also could add a DB migration to re-build the transaction locations in the background.

Copy link
Contributor Author

@Wetitpig Wetitpig Nov 24, 2023

Choose a reason for hiding this comment

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

How to initiate re-building transaction locations?
Do I just add a similar section to migrateVariables?

get(VARIABLES_PREFIX, CHAIN_HEAD_HASH.getBytes())
.map(this::bytesToHash)
.ifPresent(
bch ->
variablesStorage
.getChainHead()
.ifPresentOrElse(
vch -> {
if (!vch.equals(bch)) {
logInconsistencyAndFail(CHAIN_HEAD_HASH, bch, vch);
}
},
() -> {
variablesUpdater.setChainHead(bch);
LOG.info("Migrated key {} to variables storage", CHAIN_HEAD_HASH);
}));

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

@Wetitpig migrateVariables was much easier to implement, since it only had to move few keys around, and did it synchronously, while for this re-build an asynchronous tasks is needed, with all the consequences of making it not hurting the overall Besu performance.

My proposal for this is to:

  1. first support saving the location for newly imported blocks
  2. then for already imported blocks, that miss this data, calculate it and save on demand

Have not yet looking into the code, to understand how much it, but would like to hear what you think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Given that most transaction receipts may not be queried for multiple times I am not sure if it is worth to save the logIndexOffset for each transaction.
Probably I am going to try removing parallel() and see how much the performance impact really is.

Copy link
Contributor

Choose a reason for hiding this comment

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

if calculating that data at runtime is good enough for the moment, we can postpone the work to persist that data until more performance is needed.

@Wetitpig Wetitpig marked this pull request as ready for review November 24, 2023 00:03
@Wetitpig Wetitpig force-pushed the logIndex branch 2 times, most recently from 76779eb to 845f134 Compare November 24, 2023 03:03
Copy link
Contributor

@fab-10 fab-10 left a comment

Choose a reason for hiding this comment

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

LGTM, just a check on the use of parallel stream or not for the sum

Comment on lines 643 to 646
IntStream.range(0, transactionIndex)
.parallel()
.map(i -> transactionReceipts.get(i).getLogs().size())
.sum();
Copy link
Contributor

Choose a reason for hiding this comment

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

since usually we only have few hundreds txs in a block, have you any number to confirm that doing the sum in parallel is faster than doing it serially?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not really any numbers there. I am just thinking that it does not hurt to map and sum the numbers in parallel.

Copy link
Contributor

Choose a reason for hiding this comment

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

just thinking that a quick JMH test could be enough to spot the difference in performance, between serial/parallel

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have no idea how to do a JMH benchmark, but I tried the query with time curl and deducting the user and sys time

query {
	blocks (from: 18676591 to: 18676640) {
		transactions { gasUsed }
	}
}

Every time GraphQL transverses into a transaction it would first obtain the receipt for the amount of gas used, thus running the code for obtaining log indices in the receipt.

So with 8235 transactions in these 50 blocks the results are as follows:

.parallel() Time averaged over 10 queries
with 8.5605 s
without 8.6308 s

So it may not be worth the hassle to parallelise

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, indeed for small lists there should not be a big difference.
Just for reference, an example of a JMH test in Besu is RLPBench

.map(BlockWithMetadata::getTransactions)
.map(List::size)
.orElse(-1));
return blockchain.getBlockHashByNumber(blockNumber).map(this::getTransactionCount);
Copy link
Contributor

Choose a reason for hiding this comment

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

looks better now!

@@ -1079,7 +1070,7 @@ private int logIndexOffset(
break;
}

logIndexOffset += receipts.get(i).getLogs().size();
logIndexOffset += receipts.get(i).getLogsList().size();
Copy link
Contributor

Choose a reason for hiding this comment

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

nice opt

@fab-10 fab-10 merged commit 65d7033 into hyperledger:main Nov 29, 2023
8 checks passed
@Wetitpig Wetitpig deleted the logIndex branch November 29, 2023 13:33
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Nov 30, 2023
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Nov 30, 2023
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Dec 1, 2023
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Dec 1, 2023
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Dec 2, 2023
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Dec 4, 2023
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Dec 4, 2023
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
jflo pushed a commit to jflo/besu that referenced this pull request Dec 4, 2023
* [hyperledger#6204] Fix log index in transaction receipt

Signed-off-by: Wetitpig <[email protected]>

* Update CHANGELOG.md

Add to bug fixes.

Signed-off-by: Wetitpig <[email protected]>

* Update tests

Signed-off-by: Wetitpig <[email protected]>

* Directly sum log size with known index

Signed-off-by: Wetitpig <[email protected]>

* Minor change to gasUsed

Signed-off-by: Wetitpig <[email protected]>

* Place hash and index first

Signed-off-by: Wetitpig <[email protected]>

* Test without parallel()

Signed-off-by: Wetitpig <[email protected]>

* Rewrite header hash as map chain

Signed-off-by: Wetitpig <[email protected]>

---------

Signed-off-by: Wetitpig <[email protected]>
Signed-off-by: Justin Florentine <[email protected]>
jflo pushed a commit to jflo/besu that referenced this pull request Dec 4, 2023
* [hyperledger#6204] Fix log index in transaction receipt

Signed-off-by: Wetitpig <[email protected]>

* Update CHANGELOG.md

Add to bug fixes.

Signed-off-by: Wetitpig <[email protected]>

* Update tests

Signed-off-by: Wetitpig <[email protected]>

* Directly sum log size with known index

Signed-off-by: Wetitpig <[email protected]>

* Minor change to gasUsed

Signed-off-by: Wetitpig <[email protected]>

* Place hash and index first

Signed-off-by: Wetitpig <[email protected]>

* Test without parallel()

Signed-off-by: Wetitpig <[email protected]>

* Rewrite header hash as map chain

Signed-off-by: Wetitpig <[email protected]>

---------

Signed-off-by: Wetitpig <[email protected]>
jflo pushed a commit to jflo/besu that referenced this pull request Dec 4, 2023
* [hyperledger#6204] Fix log index in transaction receipt

Signed-off-by: Wetitpig <[email protected]>

* Update CHANGELOG.md

Add to bug fixes.

Signed-off-by: Wetitpig <[email protected]>

* Update tests

Signed-off-by: Wetitpig <[email protected]>

* Directly sum log size with known index

Signed-off-by: Wetitpig <[email protected]>

* Minor change to gasUsed

Signed-off-by: Wetitpig <[email protected]>

* Place hash and index first

Signed-off-by: Wetitpig <[email protected]>

* Test without parallel()

Signed-off-by: Wetitpig <[email protected]>

* Rewrite header hash as map chain

Signed-off-by: Wetitpig <[email protected]>

---------

Signed-off-by: Wetitpig <[email protected]>
Signed-off-by: Justin Florentine <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Dec 5, 2023
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Dec 5, 2023
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Dec 6, 2023
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Dec 6, 2023
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Dec 6, 2023
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Dec 7, 2023
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Dec 7, 2023
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Dec 7, 2023
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Dec 7, 2023
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Dec 8, 2023
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Dec 11, 2023
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
gfukushima pushed a commit to gfukushima/besu that referenced this pull request Dec 15, 2023
* [hyperledger#6204] Fix log index in transaction receipt

Signed-off-by: Wetitpig <[email protected]>

* Update CHANGELOG.md

Add to bug fixes.

Signed-off-by: Wetitpig <[email protected]>

* Update tests

Signed-off-by: Wetitpig <[email protected]>

* Directly sum log size with known index

Signed-off-by: Wetitpig <[email protected]>

* Minor change to gasUsed

Signed-off-by: Wetitpig <[email protected]>

* Place hash and index first

Signed-off-by: Wetitpig <[email protected]>

* Test without parallel()

Signed-off-by: Wetitpig <[email protected]>

* Rewrite header hash as map chain

Signed-off-by: Wetitpig <[email protected]>

---------

Signed-off-by: Wetitpig <[email protected]>
Signed-off-by: Gabriel Fukushima <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Jan 19, 2024
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Jan 20, 2024
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Feb 25, 2024
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Feb 26, 2024
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Feb 27, 2024
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Feb 28, 2024
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Feb 29, 2024
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Feb 29, 2024
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Mar 1, 2024
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Mar 1, 2024
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Mar 3, 2024
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Mar 6, 2024
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Mar 19, 2024
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Mar 19, 2024
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Wetitpig added a commit to Wetitpig-cross-chain/besu that referenced this pull request Dec 9, 2024
Forgot to replace in [hyperledger#6206]

Signed-off-by: Wetitpig <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect logIndex in eth_getReceipt responses
3 participants