Skip to content

Commit

Permalink
fix balance overflow in rpc response
Browse files Browse the repository at this point in the history
Closes #218
  • Loading branch information
yihuang committed Jul 2, 2021
1 parent f7d975a commit 40981c8
Show file tree
Hide file tree
Showing 8 changed files with 408 additions and 187 deletions.
2 changes: 1 addition & 1 deletion client/docs/statik/statik.go

Large diffs are not rendered by default.

347 changes: 263 additions & 84 deletions client/docs/swagger-ui/swagger.yaml

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ QueryAccountResponse is the response type for the Query/Account RPC method.

| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `balance` | [int64](#int64) | | balance is the balance of the EVM denomination. |
| `balance` | [string](#string) | | balance is the balance of the EVM denomination. |
| `code_hash` | [string](#string) | | code hash is the hex-formatted code bytes from the EOA. |
| `nonce` | [uint64](#uint64) | | nonce is the account's sequence number. |

Expand Down Expand Up @@ -422,7 +422,7 @@ QueryBalanceResponse is the response type for the Query/Balance RPC method.

| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `balance` | [int64](#int64) | | balance is the balance of the EVM denomination. |
| `balance` | [string](#string) | | balance is the balance of the EVM denomination. |



Expand Down
14 changes: 12 additions & 2 deletions ethereum/rpc/namespaces/eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,12 @@ func (e *PublicAPI) GetBalance(address common.Address, blockNum rpctypes.BlockNu
return nil, err
}

return (*hexutil.Big)(big.NewInt(res.Balance)), nil
val, ok := sdk.NewIntFromString(res.Balance)
if !ok {
return nil, errors.New("invalid balance")
}

return (*hexutil.Big)(val.BigInt()), nil
}

// GetStorageAt returns the contract storage at the given address, block number, and key.
Expand Down Expand Up @@ -992,10 +997,15 @@ func (e *PublicAPI) GetProof(address common.Address, storageKeys []string, block
accProofStr = proof.String()
}

balance, ok := sdk.NewIntFromString(res.Balance)
if !ok {
return nil, errors.New("invalid balance")
}

return &rpctypes.AccountResult{
Address: address,
AccountProof: []string{accProofStr},
Balance: (*hexutil.Big)(big.NewInt(res.Balance)),
Balance: (*hexutil.Big)(balance.BigInt()),
CodeHash: common.HexToHash(res.CodeHash),
Nonce: hexutil.Uint64(res.Nonce),
StorageHash: common.Hash{}, // NOTE: Ethermint doesn't have a storage hash. TODO: implement?
Expand Down
4 changes: 2 additions & 2 deletions proto/ethermint/evm/v1alpha1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ message QueryAccountRequest {
// QueryAccountResponse is the response type for the Query/Account RPC method.
message QueryAccountResponse {
// balance is the balance of the EVM denomination.
int64 balance = 1;
string balance = 1;
// code hash is the hex-formatted code bytes from the EOA.
string code_hash = 2;
// nonce is the account's sequence number.
Expand Down Expand Up @@ -136,7 +136,7 @@ message QueryBalanceRequest {
// QueryBalanceResponse is the response type for the Query/Balance RPC method.
message QueryBalanceResponse {
// balance is the balance of the EVM denomination.
int64 balance = 1;
string balance = 1;
}

// QueryStorageRequest is the request type for the Query/Storage RPC method.
Expand Down
4 changes: 2 additions & 2 deletions x/evm/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (k Keeper) Account(c context.Context, req *types.QueryAccountRequest) (*typ
k.WithContext(ctx)

return &types.QueryAccountResponse{
Balance: k.GetBalance(addr).Int64(),
Balance: k.GetBalance(addr).String(),
CodeHash: k.GetCodeHash(addr).Hex(),
Nonce: k.GetNonce(addr),
}, nil
Expand Down Expand Up @@ -127,7 +127,7 @@ func (k Keeper) Balance(c context.Context, req *types.QueryBalanceRequest) (*typ
balanceInt := k.GetBalance(ethcmn.HexToAddress(req.Address))

return &types.QueryBalanceResponse{
Balance: balanceInt.Int64(),
Balance: balanceInt.String(),
}, nil
}

Expand Down
10 changes: 5 additions & 5 deletions x/evm/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (suite *KeeperTestSuite) TestQueryAccount() {
"invalid address",
func() {
expAccount = &types.QueryAccountResponse{
Balance: 0,
Balance: "0",
CodeHash: common.BytesToHash(ethcrypto.Keccak256(nil)).Hex(),
Nonce: 0,
}
Expand All @@ -56,7 +56,7 @@ func (suite *KeeperTestSuite) TestQueryAccount() {
suite.Require().NoError(err)

expAccount = &types.QueryAccountResponse{
Balance: 100,
Balance: "100",
CodeHash: common.BytesToHash(ethcrypto.Keccak256(nil)).Hex(),
Nonce: 0,
}
Expand Down Expand Up @@ -168,7 +168,7 @@ func (suite *KeeperTestSuite) TestQueryCosmosAccount() {
func (suite *KeeperTestSuite) TestQueryBalance() {
var (
req *types.QueryBalanceRequest
expBalance int64
expBalance string
)

testCases := []struct {
Expand All @@ -178,7 +178,7 @@ func (suite *KeeperTestSuite) TestQueryBalance() {
}{
{"invalid address",
func() {
expBalance = 0
expBalance = "0"
req = &types.QueryBalanceRequest{
Address: invalidAddress,
}
Expand All @@ -194,7 +194,7 @@ func (suite *KeeperTestSuite) TestQueryBalance() {
err = suite.app.BankKeeper.SendCoinsFromModuleToAccount(suite.ctx, types.ModuleName, suite.address.Bytes(), amt)
suite.Require().NoError(err)

expBalance = 100
expBalance = "100"
req = &types.QueryBalanceRequest{
Address: suite.address.String(),
}
Expand Down
Loading

0 comments on commit 40981c8

Please sign in to comment.