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

Eip 7709 implement gas costs #7983

Merged
merged 3 commits into from
Jan 16, 2025

Conversation

lu-pinto
Copy link
Contributor

@lu-pinto lu-pinto commented Dec 4, 2024

PR description

Implements gas costs for BLOCKHASH lookup opcode using the hash history system contract

TODO: Remove #7971 and #7778 commits. Grafting of first commits is required to support these changes.

Fixed Issue(s)

fixes #7811

Thanks for sending a pull request! Have you done the following?

  • Checked out our contribution guidelines?
  • Considered documentation and added the doc-change-required label to this PR if updates are required.
  • Considered the changelog and included an update if required.
  • For database changes (e.g. KeyValueSegmentIdentifier) considered compatibility and performed forwards and backwards compatibility tests

Locally, you can run these tests to catch failures early:

  • unit tests: ./gradlew build
  • acceptance tests: ./gradlew acceptanceTest
  • integration tests: ./gradlew integrationTest
  • reference tests: ./gradlew ethereum:referenceTests:referenceTests

@lu-pinto lu-pinto force-pushed the eip-7709-implement-gas-costs branch 5 times, most recently from f41b0e8 to 077723a Compare December 10, 2024 13:54
@matkt matkt added the verkle label Dec 20, 2024
@lu-pinto lu-pinto force-pushed the eip-7709-implement-gas-costs branch from 8dbd810 to b82953a Compare January 13, 2025 16:29
@lu-pinto lu-pinto marked this pull request as ready for review January 13, 2025 16:30
@lu-pinto lu-pinto requested a review from matkt January 13, 2025 16:30
@@ -81,6 +82,12 @@ public Hash apply(final MessageFrame frame, final Long blockNumber) {
return ZERO;
}

final long cost = cost(frame, blockNumber);
if (frame.getRemainingGas() < cost) {
return Hash.EMPTY;
Copy link
Contributor

Choose a reason for hiding this comment

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

is it in the spec ? I see that in the first failure we return ZERO and in this one EMPTY

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 in the spec but this will not surface to consensus because it will be handled properly in BlockHashOperation. This was the only way I found to stop execution if out of gas. Could have an exception if you prefer.

Copy link
Contributor

Choose a reason for hiding this comment

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

For precompiles execeptional halts (like this one, out of gas) are handled by returning null rather than Hash.EMPTY, which is as valid a response as any other hash.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

throwing an exception now

final BlockHashLookup blockHashLookup = frame.getBlockHashLookup();
final Hash blockHash = blockHashLookup.apply(frame, blockArg.toLong());
final long lookupCost = remainingGas - frame.getRemainingGas();
if (Hash.EMPTY.equals(blockHash)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems quite strange to use Hash.EMPTY as a way to determine if there is not enough gas.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would recommend null, it matches the way halts are detected in precompiles.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm I think I prefer the exception approach, with precompiles we never return the ExceptionalHaltReason back in result. This one in particular, is really related to not enough gas, so if in the future another type of problem is identified that needs to be relayed back in OperationResult.

Copy link
Contributor

Choose a reason for hiding this comment

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

passing and checking for nulls is faster in a tight loop. There's a good amount of overhead in throwing and catching,.

Copy link
Contributor

Choose a reason for hiding this comment

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

I haven't thought about the performance but indeed if the performances are better I agree with Danno

Copy link
Contributor Author

@lu-pinto lu-pinto Jan 16, 2025

Choose a reason for hiding this comment

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

Ok I can use null, the code is going to look a bit stranger because I'm also using the exception to return back the gas cost on which the check failed on - required to report the exact cost back to the tracers - and now I won't have a clean way to pass that info.

Re: performance concerns - this is a halt condition so the loop will be terminated right after the exception. There is the case of parent frame contexts which could still proceed executing but it's not a new pattern I'm introducing. UnderflowException and OverflowException already do the exact same thing.

@lu-pinto lu-pinto self-assigned this Jan 14, 2025
… system contract

 forgot to add code from previous tip

Signed-off-by: Luis Pinto <[email protected]>
@lu-pinto lu-pinto force-pushed the eip-7709-implement-gas-costs branch from 374afe2 to 74aa9fc Compare January 15, 2025 14:17
Copy link
Contributor

@matkt matkt left a comment

Choose a reason for hiding this comment

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

LGTM I don't have a strong opinion between null and exception. But it's clearly better than using Hash.EMPTY.

Will wait for @shemnon before saying we can merge it

@shemnon
Copy link
Contributor

shemnon commented Jan 16, 2025

Nulls are faster than catch/throw.

… system contract

 reimplement insufficient gas cost case with null

Signed-off-by: Luis Pinto <[email protected]>
@lu-pinto lu-pinto merged commit 5f1e6d1 into hyperledger:verkle Jan 16, 2025
31 of 43 checks passed
lu-pinto added a commit to lu-pinto/besu that referenced this pull request Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants