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

Problem: url encoded characters are not properly handled in contract_by_denom #1560

Merged
merged 3 commits into from
Aug 30, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

* [#1520](https://github.com/crypto-org-chain/cronos/pull/1520) Avoid invalid chain id for signer error when rpc call before chain id set in BeginBlock.
* [#1539](https://github.com/crypto-org-chain/cronos/pull/1539) Fix go-block-stm bug that causes app hash mismatch.
* [#1560](https://github.com/crypto-org-chain/cronos/pull/1560) Ensure url encoded characters are properly handled in contract_by_denom api.

*Jun 18, 2024*

Expand Down
4 changes: 4 additions & 0 deletions integration_tests/configs/genesis_token_mapping.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ config {
denom: 'gravity0x0000000000000000000000000000000000000000',
contract: '0x68542BD12B41F5D51D6282Ec7D91D7d0D78E4503',
},
{
denom: 'ibc/6B5A664BF0AF4F71B2F0BAA33141E2F1321242FBD5D19762F541EC971ACB0865',
contract: '0x0000000000000000000000000000000000000000',
},
],
auto_contracts: [
{
Expand Down
16 changes: 16 additions & 0 deletions integration_tests/test_exported_genesis.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import json
import urllib.parse
from pathlib import Path

import pytest
import requests
from pystarport import ports

from .network import setup_custom_cronos
from .utils import ADDRS, CONTRACTS
Expand Down Expand Up @@ -34,3 +37,16 @@ def test_exported_token_mapping(custom_cronos):
)
assert rsp["contract"] == "0x68542BD12B41F5D51D6282Ec7D91D7d0D78E4503"
assert rsp["auto_contract"] == "0x68542BD12B41F5D51D6282Ec7D91D7d0D78E4503"
denom = "ibc/6B5A664BF0AF4F71B2F0BAA33141E2F1321242FBD5D19762F541EC971ACB0865"
rsp = cli.query_contract_by_denom(denom)
expected = {
"contract": "0x0000000000000000000000000000000000000000",
"auto_contract": "",
}
assert rsp == expected
port = ports.api_port(custom_cronos.base_port(0))
param = urllib.parse.quote(denom, safe="")
param = urllib.parse.quote(param, safe="")
url = f"http://127.0.0.1:{port}/cronos/v1/contract_by_denom/{param}"
rsp = requests.get(url).json()
assert rsp == expected
11 changes: 8 additions & 3 deletions x/cronos/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"context"
"fmt"
"math/big"
"net/url"

errorsmod "cosmossdk.io/errors"
"google.golang.org/grpc/codes"
Expand All @@ -21,18 +22,22 @@

// ContractByDenom query contract by denom, returns both external contract and auto deployed contract
func (k Keeper) ContractByDenom(goCtx context.Context, req *types.ContractByDenomRequest) (*types.ContractByDenomResponse, error) {
decoded, err := url.QueryUnescape(req.Denom)
if err != nil {
return nil, fmt.Errorf("fail to decode coin denom %s", req.Denom)

Check warning on line 27 in x/cronos/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/cronos/keeper/grpc_query.go#L25-L27

Added lines #L25 - L27 were not covered by tests
}
ctx := sdk.UnwrapSDKContext(goCtx)
rsp := types.ContractByDenomResponse{}
contract, found := k.getExternalContractByDenom(ctx, req.Denom)
contract, found := k.getExternalContractByDenom(ctx, decoded)

Check warning on line 31 in x/cronos/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/cronos/keeper/grpc_query.go#L31

Added line #L31 was not covered by tests
if found {
rsp.Contract = contract.String()
}
autoContract, found := k.getAutoContractByDenom(ctx, req.Denom)
autoContract, found := k.getAutoContractByDenom(ctx, decoded)

Check warning on line 35 in x/cronos/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/cronos/keeper/grpc_query.go#L35

Added line #L35 was not covered by tests
if found {
rsp.AutoContract = autoContract.String()
}
if len(rsp.Contract) == 0 && len(rsp.AutoContract) == 0 {
return nil, fmt.Errorf("contract for the coin denom %s is not found", req.Denom)
return nil, fmt.Errorf("contract for the coin denom %s is not found", decoded)

Check warning on line 40 in x/cronos/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/cronos/keeper/grpc_query.go#L40

Added line #L40 was not covered by tests
}
return &rsp, nil
}
Expand Down
Loading