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

clsdk: action return values #661

Merged
merged 4 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
11 changes: 10 additions & 1 deletion libraries/abieos/include/eosio/abi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ namespace eosio
};
EOSIO_REFLECT(variant_def, name, types);

struct action_result_def
{
eosio::name name{};
std::string result_type{};
};
EOSIO_REFLECT(action_result_def, name, result_type);

struct abi_def
{
std::string version{};
Expand All @@ -152,6 +159,7 @@ namespace eosio
std::vector<error_message> error_messages{};
abi_extensions_type abi_extensions{};
might_not_exist<std::vector<variant_def>> variants{};
might_not_exist<std::vector<action_result_def>> action_results{};
};
EOSIO_REFLECT(abi_def,
version,
Expand All @@ -162,7 +170,8 @@ namespace eosio
ricardian_clauses,
error_messages,
abi_extensions,
variants);
variants,
action_results);

struct abi_type;

Expand Down
7 changes: 6 additions & 1 deletion libraries/eosiolib/contracts/include/eosio/abi_generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace eosio
{
struct abi_generator
{
eosio::abi_def def{"eosio::abi/1.1"};
eosio::abi_def def{"eosio::abi/1.3"};
std::map<std::type_index, std::string> type_to_name;
std::map<std::string, std::type_index> name_to_type;

Expand Down Expand Up @@ -179,6 +179,11 @@ namespace eosio
struct_def d{struct_name};
add_action_args<0>(d, (typename decltype(wrapper)::args*)nullptr, arg_names...);
def.structs.push_back(std::move(d));
if constexpr (!std::is_same_v<void, typename decltype(wrapper)::return_type>)
{
def.action_results.value.push_back(
{name, get_type<typename decltype(wrapper)::return_type>()});
}
}

template <uint32_t i, typename T, typename... Ts, typename N, typename... Ns>
Expand Down
1 change: 1 addition & 0 deletions libraries/eosiolib/contracts/include/eosio/dispatcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ namespace eosio
struct action_type_wrapper
{
using args = detail::deduced<Action>;
using return_type = typename member_fn<decltype(Action)>::return_type;
};

#define EOSIO_EMPTY(...)
Expand Down
8 changes: 7 additions & 1 deletion libraries/eosiolib/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
enable_testing()

add_executable(get-code contracts/get-code.cpp)
target_include_directories(get-code PUBLIC include)
target_link_libraries(get-code eosio-contract-simple-malloc)
set_target_properties(get-code PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${ROOT_BINARY_DIR}/test-contracts)

add_executable(get-code-abigen contracts/get-code.cpp)
target_link_libraries(get-code-abigen eosio-contract-abigen)
add_custom_command(TARGET get-code-abigen POST_BUILD
COMMAND mkdir -p ${ROOT_BINARY_DIR}/test-contracts
COMMAND ${ROOT_BINARY_DIR}/cltester get-code-abigen.wasm >${ROOT_BINARY_DIR}/test-contracts/get-code.abi
)

add_executable(test-sdk test-sdk.cpp)
target_include_directories(test-sdk PUBLIC include ${ROOT_SOURCE_DIR}/contracts/bios/include)
target_link_libraries(test-sdk catch2 cltestlib)
Expand Down
1 change: 1 addition & 0 deletions libraries/eosiolib/tests/contracts/get-code.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "get-code.hpp"

EOSIO_ACTION_DISPATCHER(get_code::actions)
EOSIO_ABIGEN(actions(get_code::actions))
5 changes: 4 additions & 1 deletion libraries/eosiolib/tests/contracts/get-code.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ namespace get_code
{
eosio::print(eosio::format_json(eosio::get_code_hash(account)));
}

auto get(eosio::name account) { return eosio::get_code_hash(account); }
};

EOSIO_ACTIONS(contract,
"getcode"_n,
action(shouldhave, account),
action(shouldnot, account),
action(print, account))
action(print, account),
action(get))
} // namespace get_code
35 changes: 35 additions & 0 deletions libraries/eosiolib/tests/test-sdk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,54 @@

using namespace eosio;

static void run_nodeos(test_chain& chain)
{
chain.finish_block();
chain.finish_block();

eosio::execute("rm -rf example_chain");
eosio::execute("mkdir -p example_chain/blocks");
eosio::execute("cp " + chain.get_path() + "/blocks/blocks.log example_chain/blocks");

eosio::execute(
"./clsdk/bin/nodeos -d example_chain "
"--config-dir example_config "
"--plugin eosio::chain_api_plugin "
"--access-control-allow-origin \"*\" "
"--access-control-allow-header \"*\" "
"--http-validate-host 0 "
"--http-server-address 0.0.0.0:8888 "
"--contracts-console "
"-e -p eosio");
}

TEST_CASE("get_code")
{
test_chain chain;
chain.set_code("eosio"_n, "clsdk/contracts/bios.wasm");
bios::activate(chain, {
eosio::feature::action_return_value,
eosio::feature::get_code_hash,
});
chain.create_code_account("getcode"_n);
chain.set_code("getcode"_n, "test-contracts/get-code.wasm");
chain.set_abi("getcode"_n, "test-contracts/get-code.abi");
chain.as("getcode"_n).act<get_code::actions::print>("alice"_n);
chain.as("getcode"_n).act<get_code::actions::print>("eosio"_n);
chain.as("getcode"_n).act<get_code::actions::print>("getcode"_n);
chain.as("getcode"_n).act<get_code::actions::shouldhave>("eosio"_n);
chain.as("getcode"_n).act<get_code::actions::shouldhave>("getcode"_n);
chain.as("getcode"_n).act<get_code::actions::shouldnot>("alice"_n);

auto result = chain.as("getcode"_n).act<get_code::actions::get>("getcode"_n);
std::cout << "\naction returned: " << eosio::format_json(result) << "\n";
CHECK(result.struct_version.value == 0);
CHECK(result.code_sequence == 1);
CHECK(result.hash != eosio::checksum256{});
CHECK(result.vm_type == 0);
CHECK(result.vm_version == 0);

// run_nodeos(chain);

// clsdk/bin/cleos push action getcode get '["eosio"]' -p getcode -j | jq .processed.action_traces[0].return_value_data
}
5 changes: 3 additions & 2 deletions programs/cltester/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,9 +492,9 @@ chain_types::action convert(const eosio::chain::action& obj)
return result;
}

chain_types::action_trace_v0 convert(const eosio::chain::action_trace& obj)
chain_types::action_trace_v1 convert(const eosio::chain::action_trace& obj)
{
chain_types::action_trace_v0 result;
chain_types::action_trace_v1 result;
result.action_ordinal.value = obj.action_ordinal.value;
result.creator_action_ordinal.value = obj.creator_action_ordinal.value;
if (obj.receipt)
Expand All @@ -510,6 +510,7 @@ chain_types::action_trace_v0 convert(const eosio::chain::action_trace& obj)
result.except = obj.except->to_string();
if (obj.error_code)
result.error_code = *obj.error_code;
result.return_value = obj.return_value;
return result;
}

Expand Down