-
Notifications
You must be signed in to change notification settings - Fork 274
Adds EIP-2935 HISTORY_STORAGE contract support #1618
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
Open
moglu2017
wants to merge
5
commits into
eth-equivalence
Choose a base branch
from
moglu/eip2935
base: eth-equivalence
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ee6152a
Adds EIP-2935 HISTORY_STORAGE contract support
moglu2017 7dadeca
Update bin-rumtime code
moglu2017 cf79c02
Update copyright year and clarify license headers
moglu2017 5dde4d0
Update bin-rumtime code
moglu2017 718bd16
1. Optimize test code.
moglu2017 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| [{"stateMutability":"nonpayable","type":"fallback"}] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 608060405234801561001057600080fd5b5060003660606020821461002357600080fd5b6000610031838501856100ce565b9050438110158061004c5750611fff61004a82436100e7565b115b1561005657600080fd5b604051633549f8d160e21b81526004810182905260009068457874656e73696f6e9063d527e34490602401602060405180830381865afa15801561009e573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906100c2919061010e565b90508060005260206000f35b6000602082840312156100e057600080fd5b5035919050565b8181038181111561010857634e487b7160e01b600052601160045260246000fd5b92915050565b60006020828403121561012057600080fd5b505191905056fea264697066735822122020539bc8dc684617e81d61708951ddc86092d7be378bc0632cd188fcd949cf0064736f6c63430008140033 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,42 @@ | ||||||
| //SPDX-License-Identifier: LGPL-3.0 | ||||||
| // Copyright (c) 2026 The VeChainThor developers | ||||||
|
|
||||||
| // Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||||||
| // file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||||||
|
|
||||||
| pragma solidity 0.8.20; | ||||||
|
|
||||||
| /// @title History serves historical block hashes via the EIP-2935 calling convention. | ||||||
| /// | ||||||
| /// Calldata is exactly 32 bytes — a uint256 block number with no function | ||||||
| /// selector. Dispatch therefore goes through the fallback function. All | ||||||
| /// invalid inputs revert with empty returndata (matching the EIP-2935 | ||||||
| /// reference bytecode's `revert(0, 0)`) so dApps written for ETH mainnet | ||||||
| /// observe identical failure semantics on Thor. | ||||||
| contract History { | ||||||
| uint256 constant HISTORY_SERVE_WINDOW = 8191; | ||||||
|
|
||||||
| // builtin.Extension.Address == thor.BytesToAddress([]byte("Extension")) | ||||||
| address constant EXTENSION = 0x0000000000000000000000457874656E73696F6e; | ||||||
|
|
||||||
| fallback(bytes calldata input) external returns (bytes memory) { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be a view ?
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It can't set 'view', the fallback method only could be set 'payable' or 'non-payable', default is 'non-payable'. |
||||||
| if (input.length != 32) revert(); | ||||||
| uint256 num = abi.decode(input, (uint256)); | ||||||
| if (num >= block.number || block.number - num > HISTORY_SERVE_WINDOW) revert(); | ||||||
|
|
||||||
| bytes32 result = _Extension(EXTENSION).blockID(num); | ||||||
|
|
||||||
| assembly { | ||||||
| mstore(0, result) | ||||||
| return(0, 32) | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /// @dev Minimal interface against the 0.4.24-compiled Extension builtin | ||||||
| /// (see builtin/gen/extension.sol). Underscore prefix follows the repo | ||||||
| /// convention (see _Token, _proto_helper) — the `_*` cleanup step in | ||||||
| /// gen.go drops the unused artifact from compiled/. | ||||||
| interface _Extension { | ||||||
| function blockID(uint256 num) external view returns (bytes32); | ||||||
| } | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| // Copyright (c) 2026 The VeChainThor developers | ||
|
|
||
| // Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||
| // file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||
|
|
||
| package builtin_test | ||
|
|
||
| import ( | ||
| "encoding/binary" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/vechain/thor/v2/builtin" | ||
| "github.com/vechain/thor/v2/genesis" | ||
| "github.com/vechain/thor/v2/test/testchain" | ||
| "github.com/vechain/thor/v2/tx" | ||
| ) | ||
|
|
||
| // callHistory invokes the EIP-2935 facade with raw 32-byte calldata | ||
| // (the EIP-2935 convention has no function selector). | ||
| func callHistory(chain *testchain.Chain, num uint32) ([]byte, error) { | ||
| var data [32]byte | ||
| binary.BigEndian.PutUint32(data[28:], num) | ||
|
|
||
| addr := builtin.History.Address | ||
| clause := tx.NewClause(&addr).WithData(data[:]) | ||
| trx := new(tx.Builder). | ||
| ChainTag(chain.Repo().ChainTag()). | ||
| Expiration(50). | ||
| Gas(200000). | ||
| Clause(clause). | ||
| Build() | ||
| out, _, err := chain.ClauseCall(genesis.DevAccounts()[0], trx, 0) | ||
| return out, err | ||
| } | ||
|
|
||
| // callHistoryRaw invokes the History contract with the given raw calldata | ||
| // (no selector mangling). Used for testing invalid-length inputs. | ||
| func callHistoryRaw(chain *testchain.Chain, data []byte) ([]byte, error) { | ||
| out, _, err := callHistoryRawWithGas(chain, data) | ||
| return out, err | ||
| } | ||
|
|
||
| func callHistoryRawWithGas(chain *testchain.Chain, data []byte) ([]byte, uint64, error) { | ||
| addr := builtin.History.Address | ||
| clause := tx.NewClause(&addr).WithData(data) | ||
| trx := new(tx.Builder). | ||
| ChainTag(chain.Repo().ChainTag()). | ||
| Expiration(50). | ||
| Gas(200000). | ||
| Clause(clause). | ||
| Build() | ||
| return chain.ClauseCall(genesis.DevAccounts()[0], trx, 0) | ||
| } | ||
|
|
||
| func TestHistory_ForkActivation(t *testing.T) { | ||
| chain := newChain(t, nil) // SoloFork: INTERSTELLAR = 1 | ||
| require.NoError(t, chain.MintBlock()) | ||
|
|
||
| st := chain.State() | ||
| code, err := st.GetCode(builtin.History.Address) | ||
| require.NoError(t, err) | ||
| require.Equal(t, builtin.History.RuntimeBytecodes(), code) | ||
| } | ||
|
|
||
| func TestHistory_ValidRead(t *testing.T) { | ||
| chain := newChain(t, nil) | ||
| require.NoError(t, chain.MintBlock()) | ||
| require.NoError(t, chain.MintBlock()) | ||
| require.NoError(t, chain.MintBlock()) | ||
|
|
||
| want, err := chain.Repo().NewBestChain().GetBlockID(1) | ||
| require.NoError(t, err) | ||
|
|
||
| got, err := callHistory(chain, 1) | ||
| require.NoError(t, err) | ||
| require.Equal(t, want.Bytes(), got) | ||
| } | ||
|
|
||
| // TestHistory_WindowBoundary exercises the edges of the [best-8191, best-1] | ||
| // valid range. SERVE_WINDOW is hard-coded to 8191 in history.sol, so the | ||
| // chain must be deep enough to expose both sides of the boundary. | ||
| func TestHistory_WindowBoundary(t *testing.T) { | ||
| chain := newChain(t, nil) | ||
| for range 8193 { | ||
| require.NoError(t, chain.MintBlock()) | ||
| } | ||
|
|
||
| best := chain.Repo().BestBlockSummary().Header.Number() | ||
|
|
||
| // Passing num == block.number must revert per EIP-2935 (execution reverted). | ||
| got, err := callHistory(chain, best) | ||
| require.ErrorContains(t, err, "execution reverted") | ||
| require.Empty(t, got, "EIP-2935 revert must carry no return data") | ||
|
|
||
| // distance == 1: the most recent block, the newest in-window value. | ||
| newest := best - 1 | ||
| want, err := chain.Repo().NewBestChain().GetBlockID(newest) | ||
| require.NoError(t, err) | ||
| got, err = callHistory(chain, newest) | ||
| require.NoError(t, err) | ||
| require.Equal(t, want.Bytes(), got) | ||
|
|
||
| // distance == 8191: the oldest in-window value (block.number - num == SERVE_WINDOW). | ||
| oldestIn := best - 8191 | ||
| want, err = chain.Repo().NewBestChain().GetBlockID(oldestIn) | ||
| require.NoError(t, err) | ||
| got, err = callHistory(chain, oldestIn) | ||
| require.NoError(t, err) | ||
| require.Equal(t, want.Bytes(), got) | ||
|
|
||
| // distance == 8192: one step past the window — must revert with empty data. | ||
| out, err := callHistory(chain, oldestIn-1) | ||
| require.ErrorContains(t, err, "execution reverted") | ||
| require.Empty(t, out, "EIP-2935 revert must carry no return data") | ||
| } | ||
|
|
||
| func TestHistory_FutureBlockReverts(t *testing.T) { | ||
| chain := newChain(t, nil) | ||
| require.NoError(t, chain.MintBlock()) | ||
| require.NoError(t, chain.MintBlock()) | ||
|
|
||
| best := chain.Repo().BestBlockSummary().Header.Number() | ||
| out, err := callHistory(chain, best+9999) | ||
| require.ErrorContains(t, err, "execution reverted") | ||
| require.Empty(t, out, "EIP-2935 revert must carry no return data") | ||
| } | ||
|
|
||
| func TestHistory_InvalidCalldataLength(t *testing.T) { | ||
| chain := newChain(t, nil) | ||
| require.NoError(t, chain.MintBlock()) | ||
|
|
||
| out, err := callHistoryRaw(chain, make([]byte, 31)) | ||
| require.ErrorContains(t, err, "execution reverted") | ||
| require.Empty(t, out, "EIP-2935 revert must carry no return data") | ||
|
|
||
| out, err = callHistoryRaw(chain, make([]byte, 33)) | ||
| require.ErrorContains(t, err, "execution reverted") | ||
| require.Empty(t, out, "EIP-2935 revert must carry no return data") | ||
|
|
||
| out, err = callHistoryRaw(chain, nil) | ||
| require.ErrorContains(t, err, "execution reverted") | ||
| require.Empty(t, out, "EIP-2935 revert must carry no return data") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| // Copyright (c) 2026 The VeChainThor developers | ||
| // | ||
| // Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||
| // file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||
|
|
||
| package builtin | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "math/big" | ||
|
|
||
| "github.com/ethereum/go-ethereum/common/hexutil" | ||
| "github.com/ethereum/go-ethereum/common/math" | ||
|
|
||
| "github.com/vechain/thor/v2/api" | ||
| "github.com/vechain/thor/v2/builtin" | ||
| "github.com/vechain/thor/v2/thor" | ||
| "github.com/vechain/thor/v2/thorclient" | ||
| ) | ||
|
|
||
| // History wraps the EIP-2935 historical-block-hash facade. The contract has | ||
| // no function selector — calldata is a single uint256 block number — so it | ||
| // is invoked via raw clauses rather than the ABI-based bind.Contract. | ||
| type History struct { | ||
| client *thorclient.Client | ||
| address thor.Address | ||
| revision string | ||
| } | ||
|
|
||
| // NewHistory creates a client for the History (EIP-2935) builtin contract. | ||
| func NewHistory(client *thorclient.Client) *History { | ||
| return &History{ | ||
| client: client, | ||
| address: builtin.History.Address, | ||
| } | ||
| } | ||
|
|
||
| // Revision creates a new History instance bound to the specified revision. | ||
| func (h *History) Revision(rev string) *History { | ||
| return &History{ | ||
| client: h.client, | ||
| address: h.address, | ||
| revision: rev, | ||
| } | ||
| } | ||
|
|
||
| // Address returns the History contract address. | ||
| func (h *History) Address() thor.Address { | ||
| return h.address | ||
| } | ||
|
|
||
| // BlockID returns the historical block ID for the given block number. | ||
| // | ||
| // Per EIP-2935 the call reverts (with empty return data) when num is in the | ||
| // future or older than SERVE_WINDOW (8191) blocks behind the best block. | ||
| func (h *History) BlockID(blockNumber uint32) (thor.Bytes32, error) { | ||
| var data [32]byte | ||
| new(big.Int).SetUint64(uint64(blockNumber)).FillBytes(data[:]) | ||
| return h.callBytes32(data[:]) | ||
| } | ||
|
|
||
| // CallRaw invokes the History contract with arbitrary raw calldata and | ||
| // returns the raw response bytes. Useful for exercising invalid-length | ||
| // inputs that would not round-trip through the BlockID helper. | ||
| func (h *History) CallRaw(data []byte) ([]byte, error) { | ||
| res, err := h.call(data) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return hexutil.Decode(res.Data) | ||
| } | ||
|
|
||
| func (h *History) callBytes32(data []byte) (thor.Bytes32, error) { | ||
| out, err := h.CallRaw(data) | ||
| if err != nil { | ||
| return thor.Bytes32{}, err | ||
| } | ||
| return thor.BytesToBytes32(out), nil | ||
| } | ||
|
|
||
| func (h *History) call(data []byte) (*api.CallResult, error) { | ||
| body := &api.BatchCallData{ | ||
| Clauses: api.Clauses{ | ||
| { | ||
| To: &h.address, | ||
| Data: hexutil.Encode(data), | ||
| Value: (*math.HexOrDecimal256)(big.NewInt(0)), | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| var opts []thorclient.Option | ||
| if h.revision != "" { | ||
| opts = append(opts, thorclient.Revision(h.revision)) | ||
| } | ||
|
|
||
| results, err := h.client.InspectClauses(body, opts...) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to inspect history clause: %w", err) | ||
| } | ||
| if len(results) != 1 { | ||
| return nil, fmt.Errorf("expected 1 result, got %d", len(results)) | ||
| } | ||
|
|
||
| result := results[0] | ||
| if result.Reverted { | ||
| if result.VMError != "" { | ||
| return result, errors.New(result.VMError) | ||
| } | ||
| return result, errors.New("execution reverted") | ||
| } | ||
| if result.VMError != "" { | ||
| return nil, fmt.Errorf("VM error: %s", result.VMError) | ||
| } | ||
| return result, nil | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this return bytes32 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, at the end of function , it will return bytes32 data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ooc, why do we need the assembly ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's equivalent this:
return abi.encodePacked(_Extension(EXTENSION).blockID(num));
Assembly is used because it is more concise.