-
Notifications
You must be signed in to change notification settings - Fork 328
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
[api] return an error when the specified height is not supported #4174
Closed
Closed
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
|
@@ -71,15 +71,16 @@ var ( | |
Help: "web3 api metrics.", | ||
}, []string{"method"}) | ||
|
||
errUnkownType = errors.New("wrong type of params") | ||
errNullPointer = errors.New("null pointer") | ||
errInvalidFormat = errors.New("invalid format of request") | ||
errNotImplemented = errors.New("method not implemented") | ||
errInvalidFilterID = errors.New("filter not found") | ||
errInvalidEvmChainID = errors.New("invalid EVM chain ID") | ||
errInvalidBlock = errors.New("invalid block") | ||
errUnsupportedAction = errors.New("the type of action is not supported") | ||
errMsgBatchTooLarge = errors.New("batch too large") | ||
errUnkownType = errors.New("wrong type of params") | ||
errNullPointer = errors.New("null pointer") | ||
errInvalidFormat = errors.New("invalid format of request") | ||
errNotImplemented = errors.New("method not implemented") | ||
errInvalidFilterID = errors.New("filter not found") | ||
errInvalidEvmChainID = errors.New("invalid EVM chain ID") | ||
errInvalidBlock = errors.New("invalid block") | ||
errUnsupportedAction = errors.New("the type of action is not supported") | ||
errMsgBatchTooLarge = errors.New("batch too large") | ||
errInvalidBlockHeight = errors.New("invalid block height") | ||
|
||
_pendingBlockNumber = "pending" | ||
_latestBlockNumber = "latest" | ||
|
@@ -311,11 +312,36 @@ func (svr *web3Handler) getBlockByNumber(in *gjson.Result) (interface{}, error) | |
return svr.getBlockWithTransactions(blk.Block, blk.Receipts, isDetailed.Bool()) | ||
} | ||
|
||
func (svr *web3Handler) checkInputBlockNumber(str string) error { | ||
switch str { | ||
case "", _earliestBlockNumber, _pendingBlockNumber: | ||
return errInvalidBlockHeight | ||
case _latestBlockNumber: | ||
return nil | ||
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. delete |
||
default: | ||
height, err := hexStringToNumber(str) | ||
if err != nil { | ||
return err | ||
} | ||
if height != svr.coreService.TipHeight() { | ||
return errInvalidBlockHeight | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (svr *web3Handler) getBalance(in *gjson.Result) (interface{}, error) { | ||
addr := in.Get("params.0") | ||
if !addr.Exists() { | ||
return nil, errInvalidFormat | ||
} | ||
height := in.Get("params.1") | ||
if height.Exists() { | ||
if err := svr.checkInputBlockNumber(height.String()); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
ioAddr, err := ethAddrToIoAddr(addr.String()) | ||
if err != nil { | ||
return nil, err | ||
|
@@ -333,6 +359,12 @@ func (svr *web3Handler) getTransactionCount(in *gjson.Result) (interface{}, erro | |
if !addr.Exists() { | ||
return nil, errInvalidFormat | ||
} | ||
height := in.Get("params.1") | ||
if height.Exists() { | ||
if err := svr.checkInputBlockNumber(height.String()); err != nil { | ||
return nil, err | ||
} | ||
} | ||
ioAddr, err := ethAddrToIoAddr(addr.String()) | ||
if err != nil { | ||
return nil, err | ||
|
@@ -351,6 +383,12 @@ func (svr *web3Handler) call(in *gjson.Result) (interface{}, error) { | |
if err != nil { | ||
return nil, err | ||
} | ||
height := in.Get("params.1") | ||
if height.Exists() { | ||
if err := svr.checkInputBlockNumber(height.String()); err != nil { | ||
return nil, err | ||
} | ||
} | ||
if to == _metamaskBalanceContractAddr { | ||
return nil, nil | ||
} | ||
|
@@ -496,6 +534,12 @@ func (svr *web3Handler) getCode(in *gjson.Result) (interface{}, error) { | |
if !addr.Exists() { | ||
return nil, errInvalidFormat | ||
} | ||
height := in.Get("params.1") | ||
if height.Exists() { | ||
if err := svr.checkInputBlockNumber(height.String()); err != nil { | ||
return nil, err | ||
} | ||
} | ||
ioAddr, err := ethAddrToIoAddr(addr.String()) | ||
if err != nil { | ||
return nil, err | ||
|
@@ -750,6 +794,12 @@ func (svr *web3Handler) getStorageAt(in *gjson.Result) (interface{}, error) { | |
if err != nil { | ||
return nil, err | ||
} | ||
height := in.Get("params.2") | ||
if height.Exists() { | ||
if err := svr.checkInputBlockNumber(height.String()); err != nil { | ||
return nil, err | ||
} | ||
} | ||
val, err := svr.coreService.ReadContractStorage(context.Background(), contractAddr, pos) | ||
if err != nil { | ||
return nil, err | ||
|
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.
according to the eth standards, this
str
could be either a block number, a block enum, or a block hash. The implementation here may be incompleteThere 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.
and prefer name it as
checkInputBlock
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.
https://ethereum.org/en/developers/docs/apis/json-rpc/#default-block
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.
That content may lack complete details. Refer to the following for specifics:
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 see this https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_getbalance