From 82b61348740b12f7104e551cc3512086329f1c4f Mon Sep 17 00:00:00 2001 From: beer-1 <147697694+beer-1@users.noreply.github.com> Date: Tue, 9 Apr 2024 12:28:24 +0900 Subject: [PATCH] add missing abi encodingg --- x/evm/contracts/counter/Counter.go | 25 +++++++++++- x/evm/contracts/counter/Counter.sol | 5 +++ x/evm/keeper/context_test.go | 49 ++++++++++++++++++++++- x/evm/precompiles/cosmos/contract.go | 5 +++ x/evm/precompiles/cosmos/contract_test.go | 6 ++- 5 files changed, 86 insertions(+), 4 deletions(-) diff --git a/x/evm/contracts/counter/Counter.go b/x/evm/contracts/counter/Counter.go index faef5c82..0519f0bc 100644 --- a/x/evm/contracts/counter/Counter.go +++ b/x/evm/contracts/counter/Counter.go @@ -31,8 +31,8 @@ var ( // CounterMetaData contains all meta data concerning the Counter contract. var CounterMetaData = &bind.MetaData{ - ABI: "[{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"oldCount\",\"type\":\"uint256\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"newCount\",\"type\":\"uint256\"}],\"name\":\"increased\",\"type\":\"event\"},{\"inputs\":[],\"name\":\"count\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint64\",\"name\":\"callback_id\",\"type\":\"uint64\"},{\"internalType\":\"bool\",\"name\":\"success\",\"type\":\"bool\"}],\"name\":\"ibc_ack\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint64\",\"name\":\"callback_id\",\"type\":\"uint64\"}],\"name\":\"ibc_timeout\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"increase\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]", - Bin: "0x608060405234801561000f575f80fd5b506103c48061001d5f395ff3fe608060405234801561000f575f80fd5b506004361061004a575f3560e01c806306661abd1461004e5780630d4f1f9d1461006c57806331a503f014610088578063e8927fbc146100a4575b5f80fd5b6100566100ae565b6040516100639190610195565b60405180910390f35b61008660048036038101906100819190610224565b6100b3565b005b6100a2600480360381019061009d9190610262565b6100fa565b005b6100ac61011e565b005b5f5481565b80156100df578167ffffffffffffffff165f808282546100d391906102ba565b925050819055506100f6565b5f808154809291906100f0906102ed565b91905055505b5050565b8067ffffffffffffffff165f8082825461011491906102ba565b9250508190555050565b5f8081548092919061012f906102ed565b91905055507f61996fe196f72cb598c483e896a1221263a28bb630480aa89495f737d4a8e3df60015f546101639190610334565b5f54604051610173929190610367565b60405180910390a1565b5f819050919050565b61018f8161017d565b82525050565b5f6020820190506101a85f830184610186565b92915050565b5f80fd5b5f67ffffffffffffffff82169050919050565b6101ce816101b2565b81146101d8575f80fd5b50565b5f813590506101e9816101c5565b92915050565b5f8115159050919050565b610203816101ef565b811461020d575f80fd5b50565b5f8135905061021e816101fa565b92915050565b5f806040838503121561023a576102396101ae565b5b5f610247858286016101db565b925050602061025885828601610210565b9150509250929050565b5f60208284031215610277576102766101ae565b5b5f610284848285016101db565b91505092915050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b5f6102c48261017d565b91506102cf8361017d565b92508282019050808211156102e7576102e661028d565b5b92915050565b5f6102f78261017d565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82036103295761032861028d565b5b600182019050919050565b5f61033e8261017d565b91506103498361017d565b92508282039050818111156103615761036061028d565b5b92915050565b5f60408201905061037a5f830185610186565b6103876020830184610186565b939250505056fea264697066735822122080c757f8605d5638e5ad5049035d58cfbca44d9f4c6551641c78952012fb061864736f6c63430008180033", + ABI: "[{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"oldCount\",\"type\":\"uint256\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"newCount\",\"type\":\"uint256\"}],\"name\":\"increased\",\"type\":\"event\"},{\"inputs\":[],\"name\":\"count\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint64\",\"name\":\"callback_id\",\"type\":\"uint64\"},{\"internalType\":\"bool\",\"name\":\"success\",\"type\":\"bool\"}],\"name\":\"ibc_ack\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint64\",\"name\":\"callback_id\",\"type\":\"uint64\"}],\"name\":\"ibc_timeout\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"increase\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"string\",\"name\":\"path\",\"type\":\"string\"},{\"internalType\":\"string\",\"name\":\"req\",\"type\":\"string\"}],\"name\":\"query_cosmos\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"result\",\"type\":\"string\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]", + Bin: "0x608060405234801561000f575f80fd5b506107cb8061001d5f395ff3fe608060405234801561000f575f80fd5b5060043610610055575f3560e01c806306661abd146100595780630d4f1f9d1461007757806331a503f014610093578063cad23554146100af578063e8927fbc146100df575b5f80fd5b6100616100e9565b60405161006e9190610259565b60405180910390f35b610091600480360381019061008c91906102f5565b6100ee565b005b6100ad60048036038101906100a89190610333565b610135565b005b6100c960048036038101906100c4919061049a565b610159565b6040516100d6919061058a565b60405180910390f35b6100e76101e2565b005b5f5481565b801561011a578167ffffffffffffffff165f8082825461010e91906105d7565b92505081905550610131565b5f8081548092919061012b9061060a565b91905055505b5050565b8067ffffffffffffffff165f8082825461014f91906105d7565b9250508190555050565b606060f273ffffffffffffffffffffffffffffffffffffffff1663cad2355484846040518363ffffffff1660e01b8152600401610197929190610651565b5f604051808303815f875af11580156101b2573d5f803e3d5ffd5b505050506040513d5f823e3d601f19601f820116820180604052508101906101da91906106f4565b905092915050565b5f808154809291906101f39061060a565b91905055507f61996fe196f72cb598c483e896a1221263a28bb630480aa89495f737d4a8e3df60015f54610227919061073b565b5f5460405161023792919061076e565b60405180910390a1565b5f819050919050565b61025381610241565b82525050565b5f60208201905061026c5f83018461024a565b92915050565b5f604051905090565b5f80fd5b5f80fd5b5f67ffffffffffffffff82169050919050565b61029f81610283565b81146102a9575f80fd5b50565b5f813590506102ba81610296565b92915050565b5f8115159050919050565b6102d4816102c0565b81146102de575f80fd5b50565b5f813590506102ef816102cb565b92915050565b5f806040838503121561030b5761030a61027b565b5b5f610318858286016102ac565b9250506020610329858286016102e1565b9150509250929050565b5f602082840312156103485761034761027b565b5b5f610355848285016102ac565b91505092915050565b5f80fd5b5f80fd5b5f601f19601f8301169050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b6103ac82610366565b810181811067ffffffffffffffff821117156103cb576103ca610376565b5b80604052505050565b5f6103dd610272565b90506103e982826103a3565b919050565b5f67ffffffffffffffff82111561040857610407610376565b5b61041182610366565b9050602081019050919050565b828183375f83830152505050565b5f61043e610439846103ee565b6103d4565b90508281526020810184848401111561045a57610459610362565b5b61046584828561041e565b509392505050565b5f82601f8301126104815761048061035e565b5b813561049184826020860161042c565b91505092915050565b5f80604083850312156104b0576104af61027b565b5b5f83013567ffffffffffffffff8111156104cd576104cc61027f565b5b6104d98582860161046d565b925050602083013567ffffffffffffffff8111156104fa576104f961027f565b5b6105068582860161046d565b9150509250929050565b5f81519050919050565b5f82825260208201905092915050565b5f5b8381101561054757808201518184015260208101905061052c565b5f8484015250505050565b5f61055c82610510565b610566818561051a565b935061057681856020860161052a565b61057f81610366565b840191505092915050565b5f6020820190508181035f8301526105a28184610552565b905092915050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b5f6105e182610241565b91506105ec83610241565b9250828201905080821115610604576106036105aa565b5b92915050565b5f61061482610241565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8203610646576106456105aa565b5b600182019050919050565b5f6040820190508181035f8301526106698185610552565b9050818103602083015261067d8184610552565b90509392505050565b5f610698610693846103ee565b6103d4565b9050828152602081018484840111156106b4576106b3610362565b5b6106bf84828561052a565b509392505050565b5f82601f8301126106db576106da61035e565b5b81516106eb848260208601610686565b91505092915050565b5f602082840312156107095761070861027b565b5b5f82015167ffffffffffffffff8111156107265761072561027f565b5b610732848285016106c7565b91505092915050565b5f61074582610241565b915061075083610241565b9250828203905081811115610768576107676105aa565b5b92915050565b5f6040820190506107815f83018561024a565b61078e602083018461024a565b939250505056fea2646970667358221220c1a0ac3a5cede2574e206e9475cca3f1acea4d7e6e80aaf0c714ae8bd878739564736f6c63430008180033", } // CounterABI is the input ABI used to generate the binding from. @@ -296,6 +296,27 @@ func (_Counter *CounterTransactorSession) Increase() (*types.Transaction, error) return _Counter.Contract.Increase(&_Counter.TransactOpts) } +// QueryCosmos is a paid mutator transaction binding the contract method 0xcad23554. +// +// Solidity: function query_cosmos(string path, string req) returns(string result) +func (_Counter *CounterTransactor) QueryCosmos(opts *bind.TransactOpts, path string, req string) (*types.Transaction, error) { + return _Counter.contract.Transact(opts, "query_cosmos", path, req) +} + +// QueryCosmos is a paid mutator transaction binding the contract method 0xcad23554. +// +// Solidity: function query_cosmos(string path, string req) returns(string result) +func (_Counter *CounterSession) QueryCosmos(path string, req string) (*types.Transaction, error) { + return _Counter.Contract.QueryCosmos(&_Counter.TransactOpts, path, req) +} + +// QueryCosmos is a paid mutator transaction binding the contract method 0xcad23554. +// +// Solidity: function query_cosmos(string path, string req) returns(string result) +func (_Counter *CounterTransactorSession) QueryCosmos(path string, req string) (*types.Transaction, error) { + return _Counter.Contract.QueryCosmos(&_Counter.TransactOpts, path, req) +} + // CounterIncreasedIterator is returned from FilterIncreased and is used to iterate over the raw logs and unpacked data for Increased events raised by the Counter contract. type CounterIncreasedIterator struct { Event *CounterIncreased // Event containing the contract specifics and raw log diff --git a/x/evm/contracts/counter/Counter.sol b/x/evm/contracts/counter/Counter.sol index 50c24fac..df89cb73 100644 --- a/x/evm/contracts/counter/Counter.sol +++ b/x/evm/contracts/counter/Counter.sol @@ -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; @@ -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); + } } diff --git a/x/evm/keeper/context_test.go b/x/evm/keeper/context_test.go index d755e198..1a1ffbea 100644 --- a/x/evm/keeper/context_test.go +++ b/x/evm/keeper/context_test.go @@ -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) diff --git a/x/evm/precompiles/cosmos/contract.go b/x/evm/precompiles/cosmos/contract.go index 4772e1dd..ddddf516 100644 --- a/x/evm/precompiles/cosmos/contract.go +++ b/x/evm/precompiles/cosmos/contract.go @@ -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) } diff --git a/x/evm/precompiles/cosmos/contract_test.go b/x/evm/precompiles/cosmos/contract_test.go index 3ddde16e..0f20497b 100644 --- a/x/evm/precompiles/cosmos/contract_test.go +++ b/x/evm/precompiles/cosmos/contract_test.go @@ -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)