Skip to content

Commit 2ae2708

Browse files
authored
Merge pull request #379 from AntelopeIO/unhide_cust_entry
Update cdt-llvm to take in customized sync call entry function export fix and add tests for it
2 parents c32f4e4 + 36f2da8 commit 2ae2708

5 files changed

Lines changed: 101 additions & 1 deletion

File tree

cdt-llvm

Submodule cdt-llvm updated 1 file

tests/integration/call_tests.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,18 @@ BOOST_AUTO_TEST_CASE(is_sync_call_test) { try {
258258
BOOST_REQUIRE(return_value == false);
259259
} FC_LOG_AND_RETHROW() }
260260

261+
// Verify customized sync call entry function, without sync call tag
262+
BOOST_AUTO_TEST_CASE(customized_call_entry_func_test1) { try {
263+
call_tester t({{"receiver"_n, contracts::cust_entry_wasm(), contracts::cust_entry_abi().data()}});
264+
265+
BOOST_REQUIRE_NO_THROW(t.push_action("receiver"_n, "cusentrytst1"_n, "receiver"_n, {}));
266+
} FC_LOG_AND_RETHROW() }
267+
268+
// Verify customized sync call entry function, with sync call tag
269+
BOOST_AUTO_TEST_CASE(customized_call_entry_func_test2) { try {
270+
call_tester t({{"receiver"_n, contracts::cust_entry_wasm(), contracts::cust_entry_abi().data()}});
271+
272+
BOOST_REQUIRE_NO_THROW(t.push_action("receiver"_n, "cusentrytst2"_n, "receiver"_n, {}));
273+
} FC_LOG_AND_RETHROW() }
274+
261275
BOOST_AUTO_TEST_SUITE_END()

tests/integration/contracts.hpp.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,7 @@ namespace eosio::testing {
5454
static std::vector<char> addr_book_callee_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/sync_call_addr_book_callee.abi"); }
5555
static std::vector<uint8_t> addr_book_caller_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/sync_call_addr_book_caller.wasm"); }
5656
static std::vector<char> addr_book_caller_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/sync_call_addr_book_caller.abi"); }
57+
static std::vector<uint8_t> cust_entry_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/sync_call_cust_entry.wasm"); }
58+
static std::vector<char> cust_entry_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/sync_call_cust_entry.abi"); }
5759
};
5860
} //ns eosio::testing

tests/unit/test_contracts/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ add_contract(sync_call_not_supported sync_call_not_supported sync_call_not_suppo
1919
add_contract(sync_call_single_func sync_call_single_func sync_call_single_func.cpp)
2020
add_contract(sync_call_addr_book_callee sync_call_addr_book_callee sync_call_addr_book_callee.cpp)
2121
add_contract(sync_call_addr_book_caller sync_call_addr_book_caller sync_call_addr_book_caller.cpp)
22+
add_contract(sync_call_cust_entry sync_call_cust_entry sync_call_cust_entry.cpp)
2223
add_contract(capi_tests capi_tests capi/capi.c capi/action.c capi/chain.c capi/crypto.c capi/db.c capi/permission.c
2324
capi/print.c capi/privileged.c capi/system.c capi/transaction.c capi/call.c)
2425

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* This contract is used to verify customized sync_call entry function works.
3+
* It verifies customized sync_call entry is exported, and `call()` host function
4+
* can reach it.
5+
*/
6+
7+
#include <eosio/eosio.hpp>
8+
#include <eosio/call.hpp>
9+
10+
namespace eosio {
11+
12+
class [[eosio::contract]] sync_call_cust_entry : public contract {
13+
public:
14+
using contract::contract;
15+
16+
[[eosio::action]]
17+
void cusentrytst1() {
18+
// Make a sync call to "receiver"_n account, pass function ID in `data`,
19+
// go to `sync_call()` entry function, and dispatch the call to `get_10()`
20+
// based on the function ID.
21+
const std::vector<char> data{ eosio::pack(1) }; // function ID 1
22+
eosio::call("receiver"_n, 0, data.data(), data.size());
23+
24+
// Retrieve return value and unpack it
25+
std::vector<char> return_value(sizeof(uint32_t));
26+
eosio::get_call_return_value(return_value.data(), return_value.size());
27+
28+
// Verify it
29+
eosio::check(eosio::unpack<uint32_t>(return_value) == 10, "return value is not 10");
30+
}
31+
32+
[[eosio::action]]
33+
void cusentrytst2() {
34+
// Make a sync call to "receiver"_n account, pass function ID in `data`,
35+
// go to `sync_call()` entry function, and dispatch the call to `get_20()`
36+
// based on the function ID.
37+
const std::vector<char> data{ eosio::pack(2) }; // function ID 2
38+
eosio::call("receiver"_n, 0, data.data(), data.size());
39+
40+
// Retrieve return value and unpack it
41+
std::vector<char> return_value(sizeof(uint32_t));
42+
eosio::get_call_return_value(return_value.data(), return_value.size());
43+
44+
// Verify it
45+
eosio::check(eosio::unpack<uint32_t>(return_value) == 20, "return value is not 20");
46+
}
47+
48+
// `eosio::call` tag is optional
49+
uint32_t get_10() {
50+
return 10;
51+
}
52+
53+
[[eosio::call]]
54+
uint32_t get_20() {
55+
return 20;
56+
}
57+
};
58+
} /// namespace eosio
59+
60+
extern "C" {
61+
[[eosio::wasm_entry]]
62+
int64_t sync_call(uint64_t sender, uint64_t receiver, uint32_t data_size) {
63+
eosio::datastream<const char*> ds(nullptr, 0); // for testing, not used
64+
eosio::sync_call_cust_entry obj(eosio::name{receiver}, eosio::name{receiver}, ds);
65+
66+
std::vector<char> data(sizeof(uint32_t));
67+
eosio::get_call_data(data.data(), data.size());
68+
auto func_id = eosio::unpack<uint32_t>(data);
69+
70+
// Dispatch sync calls
71+
uint32_t rv = 0;
72+
switch (func_id) {
73+
case 1: rv = obj.get_10(); break;
74+
case 2: rv = obj.get_20(); break;
75+
default: eosio::check(false, "wrong function ID");
76+
}
77+
78+
// set return value
79+
eosio::set_call_return_value(&rv, sizeof(uint32_t));
80+
81+
return 0; // return 0 to indicate success
82+
}
83+
}

0 commit comments

Comments
 (0)