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 all 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
78 changes: 69 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 @@ -322,11 +323,46 @@ func (svr *web3Handler) getBlockByNumber(in *gjson.Result) (interface{}, error)
return svr.getBlockWithTransactions(blk.Block, blk.Receipts, isDetailed.Bool())
}

func (svr *web3Handler) checkInputBlock(str string) error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

    func (svr *web3Handler) checkHeight(input gjson.Result) error {
        if !input.Exists() {
             return nil
        }

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:
//check str is block hash string
Copy link
Collaborator

Choose a reason for hiding this comment

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

tip := svr.coreService.TipHeight()

if len(str) == 66 || len(str) == 64 {
blk, err := svr.coreService.BlockByHash(str)
if err != nil {
return err
}
if blk.Block.Height() != svr.coreService.TipHeight() {
return errInvalidBlockHeight
}
}
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.checkInputBlock(height.String()); err != nil {
return nil, err
}
}

ioAddr, err := ethAddrToIoAddr(addr.String())
if err != nil {
return nil, err
Expand All @@ -344,6 +380,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.checkInputBlock(height.String()); err != nil {
return nil, err
}
}
ioAddr, err := ethAddrToIoAddr(addr.String())
if err != nil {
return nil, err
Expand All @@ -362,6 +404,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.checkInputBlock(height.String()); err != nil {
return nil, err
}
}
if to == _metamaskBalanceContractAddr {
return nil, nil
}
Expand Down Expand Up @@ -507,6 +555,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.checkInputBlock(height.String()); err != nil {
return nil, err
}
}
ioAddr, err := ethAddrToIoAddr(addr.String())
if err != nil {
return nil, err
Expand Down Expand Up @@ -755,6 +809,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.checkInputBlock(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