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

[evm] enable Random opcode (EIP-4399) at Redsea height #3940

Closed
wants to merge 5 commits into from
Closed

Conversation

dustinxie
Copy link
Member

Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context. List any dependencies that are required for this change.

Fixes #(issue)

Type of change

Please delete options that are not relevant.

  • [] Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • [] Code refactor or improvement
  • [] Breaking change (fix or feature that would cause a new or changed behavior of existing functionality)
  • [] This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • make test
  • [] fullsync
  • [] Other test (please specify)

Test Configuration:

  • Firmware version:
  • Hardware:
  • Toolchain:
  • SDK:

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@codecov
Copy link

codecov bot commented Oct 18, 2023

Codecov Report

Merging #3940 (9b93019) into master (e1f0636) will increase coverage by 0.72%.
Report is 88 commits behind head on master.
The diff coverage is 78.48%.

❗ Current head 9b93019 differs from pull request most recent head 9b8fae8. Consider uploading reports for the commit 9b8fae8 to get more accurate results

@@            Coverage Diff             @@
##           master    #3940      +/-   ##
==========================================
+ Coverage   75.38%   76.10%   +0.72%     
==========================================
  Files         303      328      +25     
  Lines       25923    27991    +2068     
==========================================
+ Hits        19541    21303    +1762     
- Misses       5360     5593     +233     
- Partials     1022     1095      +73     
Files Coverage Δ
action/candidate_register.go 90.00% <100.00%> (ø)
action/candidate_update.go 89.01% <100.00%> (+0.12%) ⬆️
action/claimreward.go 90.62% <100.00%> (-0.42%) ⬇️
action/depositreward.go 87.50% <100.00%> (-0.56%) ⬇️
action/protocol/context.go 67.91% <100.00%> (+0.73%) ⬆️
action/protocol/execution/evm/evmstatedbadapter.go 66.77% <ø> (ø)
action/protocol/execution/protocol.go 42.10% <100.00%> (ø)
action/protocol/rewarding/reward.go 90.07% <100.00%> (+0.60%) ⬆️
...tion/protocol/staking/candidate_buckets_indexer.go 89.31% <100.00%> (-0.47%) ⬇️
...col/staking/ethabi/stake_composite_bucketscount.go 100.00% <100.00%> (ø)
... and 66 more

... and 3 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@envestcc envestcc requested a review from a team as a code owner October 19, 2023 10:50
// Merge enabled at Redsea height
tipHash := protocol.MustGetBlockchainCtx(ctx).Tip.Hash
h := common.BytesToHash(tipHash[:])
evmParams.context.Random = &h
Copy link
Member

Choose a reason for hiding this comment

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

The initialization of the Random field should occur in where evmParams is created.

Copy link
Member Author

Choose a reason for hiding this comment

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

moved to newParam

@@ -432,7 +438,7 @@ func executeInEVM(ctx context.Context, evmParams *Params, stateDB *StateDBAdapte
remainingGas -= intriGas

// Set up the initial access list
rules := chainConfig.Rules(evm.Context.BlockNumber, g.IsRedsea(blockHeight)) // Merge enabled at Redsea height
rules := chainConfig.Rules(evm.Context.BlockNumber, evm.Context.Random != nil)
Copy link
Member

Choose a reason for hiding this comment

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

I still think using g.IsRedsea() seems clearer than evm.Context.Random != nil, although Ethereum does it that way.

Copy link
Member

Choose a reason for hiding this comment

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

agree.

Copy link
Member Author

Choose a reason for hiding this comment

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

recently I feel more and more that align with Ethereum code is beneficial to our codebase maintenance in the long run, that's why I changed to evm.Context.Random != nil

@@ -432,7 +438,7 @@ func executeInEVM(ctx context.Context, evmParams *Params, stateDB *StateDBAdapte
remainingGas -= intriGas

// Set up the initial access list
rules := chainConfig.Rules(evm.Context.BlockNumber, g.IsRedsea(blockHeight)) // Merge enabled at Redsea height
rules := chainConfig.Rules(evm.Context.BlockNumber, evm.Context.Random != nil)
Copy link
Member

Choose a reason for hiding this comment

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

agree.

@@ -418,6 +418,12 @@ func executeInEVM(ctx context.Context, evmParams *Params, stateDB *StateDBAdapte
config = vmCfg
}
chainConfig := getChainConfig(g, blockHeight, evmParams.evmNetworkID)
if g.IsRedsea(blockHeight) {
// Merge enabled at Redsea height
tipHash := protocol.MustGetBlockchainCtx(ctx).Tip.Hash
Copy link
Member

Choose a reason for hiding this comment

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

TIP.Hash is not of randomness. Here is ETH's design for reference: https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.3/specs/phase0/beacon-chain.md#randao

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, in Beacon chain validator's BLS signature is used as the contribution to entropy, and its hash is XORed into the RANDAO during the update. The Beacon chain maintains the states, and the randomness is reflected as MixDigest common.Hash in Ethereum's block header.

Currently we don't have these in place nor the corresponding data field in our block header. We could implement a similar scheme but that would take quite some time.

Copy link
Member

Choose a reason for hiding this comment

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

for evm, prevrandao() instruction always return a same value (i.e. equals to Random field of BlockCtx) during one block, according to implementation of ethereum. So it's not randomness during one block.

@sonarcloud
Copy link

sonarcloud bot commented Oct 20, 2023

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 3 Code Smells

No Coverage information No Coverage information
3.0% 3.0% Duplication

idea Catch issues before they fail your Quality Gate with our IDE extension sonarlint SonarLint

@dustinxie
Copy link
Member Author

EIP-4399 to be enabled in future release

@dustinxie dustinxie closed this Oct 20, 2023
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.

3 participants