Skip to content
This repository was archived by the owner on Aug 2, 2022. It is now read-only.

Commit d2ab320

Browse files
authored
Merge pull request #979 from EOSIO/dogfooding_kv_tests
add an example for kv test
2 parents 3895a52 + 58e0514 commit d2ab320

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
include(ExternalProject)
2+
# if no cdt root is given use default path
3+
if(EOSIO_CDT_ROOT STREQUAL "" OR NOT EOSIO_CDT_ROOT)
4+
find_package(eosio.cdt)
5+
endif()
6+
7+
ExternalProject_Add(
8+
kvtest_project
9+
SOURCE_DIR ${CMAKE_SOURCE_DIR}/src
10+
BINARY_DIR ${CMAKE_BINARY_DIR}/kvtest
11+
CMAKE_ARGS -DCMAKE_TOOLCHAIN_FILE=${EOSIO_CDT_ROOT}/lib/cmake/eosio.cdt/EosioWasmToolchain.cmake
12+
UPDATE_COMMAND ""
13+
PATCH_COMMAND ""
14+
TEST_COMMAND ""
15+
INSTALL_COMMAND ""
16+
BUILD_ALWAYS 1
17+
)

examples/kv_table_tests/README.txt

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--- kv_tests Project ---
2+
3+
--backing-store chainbase -- How to Build with CMake and Make --
4+
- cd into the 'build' directory
5+
- run the command 'cmake .. -DEOSIO_CONTRACTS_ROOT=/path/to//eosio.contracts'
6+
- run the command 'make'
7+
8+
- After build -
9+
- The built smart contract is under the 'kvtest' directory in the 'build' directory
10+
- You can then do a 'set contract' action with 'cleos' and point to the './build/kvtest' directory
11+
12+
- Additions to CMake should be done to the CMakeLists.txt in the './src' directory and not in the top level CMakeLists.txt
13+
14+
- After build -
15+
- The built smart contract is in the 'build' directory
16+
- You can then do a 'set contract' action with 'cleos' and point to the 'build' directory
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<h1 class="contract"> hi </h1>
2+
3+
Stub for hi action's ricardian contract
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
project(kvtest)
2+
3+
set(EOSIO_WASM_OLD_BEHAVIOR "Off")
4+
find_package(eosio.cdt)
5+
6+
add_contract( kvtest kvtest kvtest.cpp )
7+
target_ricardian_directory( kvtest ${CMAKE_SOURCE_DIR}/../ricardian )
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <eosio/eosio.hpp>
2+
#include <eosio/asset.hpp>
3+
4+
extern "C" __attribute__((eosio_wasm_import)) void set_kv_parameters_packed(const void* params, uint32_t size);
5+
6+
using namespace eosio;
7+
8+
struct kvtest_record {
9+
std::string id;
10+
std::string data;
11+
};
12+
13+
class [[eosio::contract]] kvtest : public contract {
14+
struct [[eosio::table]] kvtest_table : eosio::kv::table<kvtest_record, "eosio"_n> {
15+
KV_NAMED_INDEX("by.id", id);
16+
kvtest_table(name contract) { init(contract, id); }
17+
};
18+
19+
public:
20+
using contract::contract;
21+
22+
kvtest(eosio::name receiver, eosio::name code, eosio::datastream<const char *> ds) :
23+
contract(receiver, code, ds)
24+
{
25+
uint32_t limits[4];
26+
limits[0] = 0;
27+
limits[1] = 1024 * 1024;
28+
limits[2] = 9 * 1024 * 1024;
29+
limits[3] = 10;
30+
set_kv_parameters_packed(limits, sizeof(limits));
31+
}
32+
33+
[[eosio::action]]
34+
void smalltest() {
35+
require_auth(_self);
36+
kvtest_table table{"eosio"_n};
37+
38+
uint64_t id = 1;
39+
std::string data = "test";
40+
41+
for (; id < 5000; ++id) {
42+
table.put({std::to_string(id), data + std::to_string(id)}, get_self());
43+
}
44+
45+
for (id = 1; id < 5000; ++id) {
46+
auto itr = table. id.find(std::to_string(id));
47+
if (itr != table.id.end()) {
48+
std::string val = itr.value().data;
49+
eosio::check(val.compare(data + std::to_string(id)) == 0, "The value for the key " + std::to_string(id) + " is " + val);
50+
}
51+
}
52+
}
53+
54+
[[eosio::action]]
55+
void largetest() {
56+
require_auth(_self);
57+
58+
kvtest_table table{"eosio"_n};
59+
60+
std::string data = std::string(8 * 1024 * 1024, 't');
61+
62+
uint64_t id = 1;
63+
for (; id < 200; ++id)
64+
table.put({std::to_string(id), std::to_string(id) + data}, get_self());
65+
66+
for (id = 1; id < 200; ++id) {
67+
auto itr = table.id.find(std::to_string(id));
68+
if (itr != table.id.end()) {
69+
std::string val = itr.value().data.substr(0, 10);
70+
std::string expected = (std::to_string(id) + data).substr(0, 10);
71+
eosio::check(val.compare(expected) == 0, "The value for the key " + std::to_string(id) + " is " + val + "...");
72+
}
73+
}
74+
}
75+
};

0 commit comments

Comments
 (0)