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

[api] return an error when the specified height is not supported #4174

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 59 additions & 9 deletions api/web3server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Copy link
Member

@envestcc envestcc Mar 8, 2024

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 incomplete

Copy link
Member

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

Copy link
Contributor Author

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

When requests are made that act on the state of Ethereum, the last default block parameter determines the height of the block.

The following options are possible for the defaultBlock parameter:

HEX String - an integer block number
String "earliest" for the earliest/genesis block
String "latest" - for the latest mined block
String "safe" - for the latest safe head block
String "finalized" - for the latest finalized block
String "pending" - for the pending state/transactions

Copy link
Member

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:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

switch str {
case "", _earliestBlockNumber, _pendingBlockNumber:
return errInvalidBlockHeight
case _latestBlockNumber:
return nil
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Expand All @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading