-
Notifications
You must be signed in to change notification settings - Fork 886
feat(evmonly): add eth_getBalance RPC #4140
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
Merged
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package rpc | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
|
|
||
| "github.com/ethereum/go-ethereum/common" | ||
| "github.com/ethereum/go-ethereum/common/hexutil" | ||
| ethrpc "github.com/ethereum/go-ethereum/rpc" | ||
| ) | ||
|
|
||
| var errHistoricalStateUnsupported = errors.New("historical state is not supported by EVM-only RPC") | ||
|
|
||
| type balanceAPI struct { | ||
| backend Backend | ||
| } | ||
|
|
||
| // GetBalance returns the address balance from the current committed EVM state. | ||
| func (api *balanceAPI) GetBalance(_ context.Context, address common.Address, block ethrpc.BlockNumberOrHash) (*hexutil.Big, error) { | ||
| if err := requireCurrentState(block); err != nil { | ||
| return nil, err | ||
| } | ||
| balance := api.backend.EvmBalance(address) | ||
| return (*hexutil.Big)(balance.ToBig()), nil | ||
| } | ||
|
|
||
| func requireCurrentState(block ethrpc.BlockNumberOrHash) error { | ||
| number, ok := block.Number() | ||
| if !ok { | ||
| return errHistoricalStateUnsupported | ||
| } | ||
| switch number { | ||
| case ethrpc.LatestBlockNumber, ethrpc.SafeBlockNumber, ethrpc.FinalizedBlockNumber, ethrpc.PendingBlockNumber: | ||
| return nil | ||
| default: | ||
| return errHistoricalStateUnsupported | ||
| } | ||
| } | ||
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,80 @@ | ||
| package rpc | ||
|
|
||
| import ( | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/ethereum/go-ethereum/common" | ||
| "github.com/ethereum/go-ethereum/common/hexutil" | ||
| ethrpc "github.com/ethereum/go-ethereum/rpc" | ||
| "github.com/holiman/uint256" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/sei-protocol/sei-chain/giga/evmonly" | ||
| ) | ||
|
|
||
| func TestGetBalance(t *testing.T) { | ||
| address := common.HexToAddress("0x1000000000000000000000000000000000000001") | ||
| want := uint256.NewInt(123456789) | ||
| backend := &testBackend{ | ||
| balance: func(got common.Address) uint256.Int { | ||
| require.Equal(t, address, got) | ||
| return *want | ||
| }, | ||
| } | ||
| api := &balanceAPI{backend: backend} | ||
|
|
||
| for _, tag := range []ethrpc.BlockNumber{ | ||
| ethrpc.LatestBlockNumber, | ||
| ethrpc.SafeBlockNumber, | ||
| ethrpc.FinalizedBlockNumber, | ||
| ethrpc.PendingBlockNumber, | ||
| } { | ||
| got, err := api.GetBalance(t.Context(), address, ethrpc.BlockNumberOrHashWithNumber(tag)) | ||
| require.NoError(t, err) | ||
| require.Equal(t, want.ToBig(), got.ToInt()) | ||
| } | ||
| } | ||
|
|
||
| func TestGetBalanceRejectsHistoricalState(t *testing.T) { | ||
| backend := &testBackend{ | ||
| balance: func(common.Address) uint256.Int { | ||
| t.Fatal("historical request read the current balance") | ||
| return uint256.Int{} | ||
| }, | ||
| } | ||
| api := &balanceAPI{backend: backend} | ||
| address := common.Address{1} | ||
|
|
||
| for _, block := range []ethrpc.BlockNumberOrHash{ | ||
| ethrpc.BlockNumberOrHashWithNumber(ethrpc.EarliestBlockNumber), | ||
| ethrpc.BlockNumberOrHashWithNumber(7), | ||
| ethrpc.BlockNumberOrHashWithHash(common.Hash{2}, true), | ||
| {}, | ||
| } { | ||
| got, err := api.GetBalance(t.Context(), address, block) | ||
| require.ErrorIs(t, err, errHistoricalStateUnsupported) | ||
| require.Nil(t, got) | ||
| } | ||
| } | ||
|
|
||
| func TestHandlerServesGetBalance(t *testing.T) { | ||
| address := common.HexToAddress("0x2000000000000000000000000000000000000002") | ||
| backend := &testBackend{ | ||
| balance: func(common.Address) uint256.Int { | ||
| return *uint256.NewInt(42) | ||
| }, | ||
| } | ||
| handler, err := newHandler(backend, evmonly.NewMemoryReceiptStore()) | ||
| require.NoError(t, err) | ||
| t.Cleanup(handler.Stop) | ||
| server := httptest.NewServer(handler) | ||
| t.Cleanup(server.Close) | ||
| client, err := ethrpc.DialHTTP(server.URL) | ||
| require.NoError(t, err) | ||
| t.Cleanup(client.Close) | ||
|
|
||
| var got hexutil.Big | ||
| require.NoError(t, client.CallContext(t.Context(), &got, "eth_getBalance", address, "latest")) | ||
| require.Equal(t, "0x2a", got.String()) | ||
| } |
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 |
|---|---|---|
|
|
@@ -13,8 +13,8 @@ passed to `deploy`, pass the same name to `list`, `forward`, and `teardown`. | |
| Both targets require Go 1.25.6 and `make`. Local deployment also requires a | ||
| running Docker engine with Docker Compose v2. AWS deployment requires the AWS | ||
| CLI, `git`, and `ssh`, plus credentials allowed to manage EC2 instances, | ||
| security groups, and key pairs. The inspection and receipt examples also use | ||
| `jq` and Foundry's `cast`. | ||
| security groups, and key pairs. The inspection, balance, and receipt examples | ||
| also use `jq` and Foundry's `cast`. | ||
|
|
||
| Build the manager once: | ||
|
|
||
|
|
@@ -297,11 +297,27 @@ tail -f build/generated/logs/seid-0.log | |
| The public EVM JSON-RPC surface intentionally contains only: | ||
|
|
||
| - `eth_sendRawTransaction`, used by `sei-load` and `cast publish`; | ||
| - `eth_getTransactionReceipt`, for finalized receipts. | ||
| - `eth_getTransactionReceipt`, for finalized receipts; | ||
| - `eth_getBalance`, for the current committed EVM balance. | ||
|
|
||
| All other `eth_*` methods currently return JSON-RPC method-not-found. A lookup | ||
| for a pending or unknown hash returns `null`. | ||
|
|
||
| ### Fetch balances with `cast` | ||
|
|
||
| `eth_getBalance` accepts `latest`, `safe`, `finalized`, and `pending`; all four | ||
| read the current committed state because Sei has instant finality. Explicit | ||
| block numbers and hashes return an error because historical EVM-only state is | ||
| not wired yet. | ||
|
Contributor
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. Not wired yet? Will we be supporting archive nodes in the future that do support this? |
||
|
|
||
| Every previously unseen address starts with the test-only `2^200` wei balance: | ||
|
|
||
| ```sh | ||
| cast balance \ | ||
| --rpc-url http://127.0.0.1:8545 \ | ||
| 0x000000000000000000000000000000000000dEaD | ||
| ``` | ||
|
|
||
| ### Fetch receipts with `cast` | ||
|
|
||
| `cast receipt` works for a known finalized transaction hash. Contract | ||
|
|
@@ -350,10 +366,10 @@ correct. | |
| The remaining `cast` gaps are RPC gaps, not receipt-decoding gaps. There is no | ||
| `eth_getTransactionByHash` or block API to discover a `sei-load` transfer hash, | ||
| and `sei-load` does not currently print every submitted hash. There are also no | ||
| chain ID, balance, nonce, fee-estimation, gas-estimation, call, log, or | ||
| WebSocket subscription methods. Commands that depend on those queries cannot | ||
| operate normally; raw transactions must provide chain ID, nonce, gas limit, | ||
| and gas price offline as in the example above. | ||
| chain ID, nonce, fee-estimation, gas-estimation, call, log, or WebSocket | ||
| subscription methods. Commands that depend on those queries cannot operate | ||
| normally; raw transactions must provide chain ID, nonce, gas limit, and gas | ||
| price offline as in the example above. | ||
|
|
||
| ## Tear down | ||
|
|
||
|
|
||
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
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.
So we are explicitly blocking the case where they guess the current block number and slip it in the very narrow window it's true?