Skip to content

Commit

Permalink
add missing abi encodingg
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Apr 9, 2024
1 parent d1e6628 commit 82b6134
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 4 deletions.
25 changes: 23 additions & 2 deletions x/evm/contracts/counter/Counter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions x/evm/contracts/counter/Counter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity ^0.8.24;

import "../i_ibc_async_callback/IIBCAsyncCallback.sol";
import "../i_cosmos/ICosmos.sol";

contract Counter is IIBCAsyncCallback {
uint256 public count;
Expand All @@ -25,4 +26,8 @@ contract Counter is IIBCAsyncCallback {
function ibc_timeout(uint64 callback_id) external {
count += callback_id;
}

function query_cosmos(string memory path, string memory req) external returns (string memory result) {
return COSMOS_CONTRACT.query_cosmos(path, req);
}
}
49 changes: 48 additions & 1 deletion x/evm/keeper/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,55 @@ func Test_QueryCosmosMessage(t *testing.T) {
retBz, _, err := input.EVMKeeper.EVMCall(ctx, evmAddr, types.CosmosPrecompileAddress, inputBz)
require.NoError(t, err)

unpackedRet, err := abi.Methods["query_cosmos"].Outputs.Unpack(retBz)
require.NoError(t, err)

var ret banktypes.QueryBalanceResponse
err = input.EncodingConfig.Codec.UnmarshalJSON([]byte(unpackedRet[0].(string)), &ret)
require.NoError(t, err)

require.Equal(t, math.NewInt(200), ret.Balance.Amount)
}

func Test_QueryCosmosFromContract(t *testing.T) {
ctx, input := createDefaultTestInput(t)
_, _, addr := keyPubAddr()

erc20Keeper, err := keeper.NewERC20Keeper(&input.EVMKeeper)
require.NoError(t, err)

// mint native coin
err = erc20Keeper.MintCoins(ctx, addr, sdk.NewCoins(
sdk.NewCoin("bar", math.NewInt(200)),
))
require.NoError(t, err)

counterBz, err := hexutil.Decode(counter.CounterBin)
require.NoError(t, err)

caller := common.BytesToAddress(addr.Bytes())
retBz, contractAddr, err := input.EVMKeeper.EVMCreate(ctx, caller, counterBz)
require.NoError(t, err)
require.NotEmpty(t, retBz)
require.Len(t, contractAddr, 20)

abi, err := counter.CounterMetaData.GetAbi()
require.NoError(t, err)

inputBz, err := abi.Pack("query_cosmos", "/cosmos.bank.v1beta1.Query/Balance", fmt.Sprintf(`{
"address": "%s",
"denom": "bar"
}`, addr))
require.NoError(t, err)

retBz, _, err = input.EVMKeeper.EVMCall(ctx, contractAddr, types.CosmosPrecompileAddress, inputBz)
require.NoError(t, err)

unpackedRet, err := abi.Methods["query_cosmos"].Outputs.Unpack(retBz)
require.NoError(t, err)

var ret banktypes.QueryBalanceResponse
err = input.EncodingConfig.Codec.UnmarshalJSON(retBz, &ret)
err = input.EncodingConfig.Codec.UnmarshalJSON([]byte(unpackedRet[0].(string)), &ret)
require.NoError(t, err)

require.Equal(t, math.NewInt(200), ret.Balance.Amount)
Expand Down
5 changes: 5 additions & 0 deletions x/evm/precompiles/cosmos/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ func (e CosmosPrecompile) ExtendedRun(caller vm.ContractRef, input []byte, suppl
return nil, ctx.GasMeter().GasConsumedToLimit(), err
}

// abi encode the response
resBz, err = method.Outputs.Pack(string(resBz))
if err != nil {
return nil, ctx.GasMeter().GasConsumedToLimit(), types.ErrPrecompileFailed.Wrap(err.Error())
}
default:
return nil, ctx.GasMeter().GasConsumedToLimit(), types.ErrUnknownPrecompileMethod.Wrap(method.Name)
}
Expand Down
6 changes: 5 additions & 1 deletion x/evm/precompiles/cosmos/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,12 @@ func Test_QueryCosmos(t *testing.T) {
retBz, _, err := cosmosPrecompile.ExtendedRun(vm.AccountRef(evmAddr), inputBz, precompiles.QUERY_COSMOS_GAS+uint64(len(inputBz)), true)
require.NoError(t, err)

// unpack response
unpackedRet, err := abi.Methods["query_cosmos"].Outputs.Unpack(retBz)
require.NoError(t, err)

var ret oracletypes.GetPricesResponse
err = cdc.UnmarshalJSON(retBz, &ret)
err = cdc.UnmarshalJSON([]byte(unpackedRet[0].(string)), &ret)
require.NoError(t, err)

require.Equal(t, expectedRet, ret)
Expand Down

0 comments on commit 82b6134

Please sign in to comment.