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

blockchain: implement EIP-2935 #121

Merged
merged 11 commits into from
Nov 7, 2024
Merged

blockchain: implement EIP-2935 #121

merged 11 commits into from
Nov 7, 2024

Conversation

hyunsooda
Copy link
Contributor

@hyunsooda hyunsooda commented Oct 31, 2024

Proposed changes

The main function has been implemented, but a task has been queued to simplify the history store callsite.

  • Draft commit: c1714134 (implement the main function)
  • Final commit: b38c8f2 (simplify the historical store callsite)

Contract Deployment Process

According to EIP-2935 deployment transaction artifact, the code below generate the exact same system history storage address.

const ethers = require('ethers');
const tx = {
  "from": "0xe473f7e92ba2490e9fcbbe8bb9c3be3adbb74efc",
  "type": "0x00",
  "nonce": "0x",
  "to": "0x",
  "gas": "0x03d090",
  "gasPrice": "0xe8d4a51000",
  "maxPriorityFeePerGas": null,
  "maxFeePerGas": null,
  "value": "0x",
  "input": "0x60648060095f395ff33373fffffffffffffffffffffffffffffffffffffffe1460575767ffffffffffffffff5f3511605357600143035f3511604b575f35612000014311604b57611fff5f3516545f5260205ff35b5f5f5260205ff35b5f5ffd5b5f35611fff60014303165500",
  "v": "0x1b",
  "r": "0x0539",
  "s": "0x1b9b6eb1f0",
  "hash": "0x3c769a03d6e2212f1d26ab59ba797dce0900df29ffd23c1dd391fd6b217973ad",
}

const signedEncodedTransaction = ethers.encodeRlp([tx.nonce, tx.gasPrice, tx.gas, tx.to, tx.value, tx.input, tx.v, tx.r, tx.s])
console.log(signedEncodedTransaction)

// generated rlp raw transcation bytes: 0xf8868085e8d4a510008303d0908080b86d60648060095f395ff33373fffffffffffffffffffffffffffffffffffffffe1460575767ffffffffffffffff5f3511605357600143035f3511604b575f35612000014311604b57611fff5f3516545f5260205ff35b5f5f5260205ff35b5f5ffd5b5f35611fff600143031655001b820539851b9b6eb1f0
> eth.sendRawTransaction("0xf8868085e8d4a510008303d0908080b86d60648060095f395ff33373fffffffffffffffffffffffffffffffffffffffe1460575767ffffffffffffffff5f3511605357600143035f3511604b575f35612000
014311604b57611fff5f3516545f5260205ff35b5f5f5260205ff35b5f5ffd5b5f35611fff600143031655001b820539851b9b6eb1f0")
"0x3c769a03d6e2212f1d26ab59ba797dce0900df29ffd23c1dd391fd6b217973ad"
> kaia.getTransactionReceipt("0x3c769a03d6e2212f1d26ab59ba797dce0900df29ffd23c1dd391fd6b217973ad")
{
  blockHash: "0x9af795d26881f60aafce80051434a4f06a8209bbeb5512bc037f2cb0fcfd574e",
  blockNumber: 19560,
  contractAddress: "0x0aae40965e6800cd9b1f4b05ff21581047e3f91e", // history storage address
  ...
  ...
}

Usage

 // SPDX-License-Identifier: LGPL-3.0-only
 pragma solidity 0.8.24;

 contract EIP2935 {
     address constant HISTORY_STORAGE_ADDR = 0x0AAE40965E6800cD9b1f4b05ff21581047E3F91e;

     function getHash(uint256 blockNumber) public returns (uint256) {
         (bool ok, bytes memory output) = HISTORY_STORAGE_ADDR.call(abi.encode(blockNumber));
         require(ok, "historical retreival error");
         return uint256(bytes32(output));
     }
 }

Once deployed, retrieval works like below:

> kaia.call({to:"0x0eF71b533f074383A14696f7EeBD94446b10F679", data:"0x6b2fafa90000000000000000000000000000000000000000000000000000000000004f6d"}) // getHash(20333)
> 0x52105353978f50ba403e770e7a920d70c3ee9a2df991a428fd5fc6aebaacf4f5 // Hash of block 20333

Implementation details

  • Add consensus.Engine.Initialize() that performs pre-block state transitions.
  • TraceChain: traces blocks (start, end]
  for num = start; num < end; num++ {
-   statedb = StateAtBlock(num)
+   statedb = StateAtTransaction(num+1, 0)
    for tx in block(num+1)
      traceTx(tx)
  }
  • TraceBlock: traces block num
- statedb = StateAtBlock(num-1)
+ statedb = StateAtTransaction(num, 0)
  for tx in block(num)
    traceTx(tx)
  • TraceTransaction: traces the tx in block num -- unchanged
  statedb = StateAtTransaction(num, idx)
  traceTx(tx)
  • TraceCall: traces the tx at the end of the block num, not the beginning of num+1 -- unchanged
  statedb = StateAtBlock(num)
  traceTx(tx)

Types of changes

Please put an x in the boxes related to your change.

  • Bugfix
  • New feature or enhancement
  • Others

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

  • I have read the CONTRIBUTING GUIDELINES doc
  • I have read the CLA and signed by comment I have read the CLA Document and I hereby sign the CLA in first time contribute
  • Lint and unit tests pass locally with my changes ($ make test)
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • Any dependent changes have been merged and published in downstream modules

Related issues

  • Please leave the issue numbers or links related to this PR here.

Further comments

If this is a relatively large or complex change, kick off the discussion by explaining why you chose the solution you did and what alternatives you considered, etc...

consensus/consensus.go Outdated Show resolved Hide resolved
consensus/consensus.go Outdated Show resolved Hide resolved
node/cn/tracers/api.go Outdated Show resolved Hide resolved
node/cn/tracers/api.go Outdated Show resolved Hide resolved
index zero and removal of introduced `ChainContext`
node/cn/tracers/api.go Outdated Show resolved Hide resolved
Copy link
Contributor

@hyeonLewis hyeonLewis left a comment

Choose a reason for hiding this comment

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

In PR Usage comments, the output seems not a block hash for 20333. Could you recheck this?

> kaia.call({to:"0x0eF71b533f074383A14696f7EeBD94446b10F679", data:"0x6b2fafa90000000000000000000000000000000000000000000000000000000000004f6d"}) // getHash(20333)
> 0x6b2fafa90000000000000000000000000000000000000000000000000000000000004f6d // <- It seems data for eth_call, not block hash at 20333.

@hyunsooda
Copy link
Contributor Author

@hyeonLewis Thank you. Typo fixed.

blockchain/evm.go Outdated Show resolved Hide resolved
node/cn/tracers/api.go Outdated Show resolved Hide resolved
node/cn/tracers/api.go Outdated Show resolved Hide resolved
@hyunsooda hyunsooda merged commit 933e977 into kaiachain:dev Nov 7, 2024
11 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Nov 7, 2024
@hyunsooda
Copy link
Contributor Author

Currently, onboarding EIP-2935 requires additional modifications at the core level, specifically for:

  • The historical storage contract address

Other changes involve deployment, but the provided artifact appears non-deployable (the signature value seems invalid). Once the artifact stabilizes, I’ll test it again.

cc. @blukat29

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants