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

add an example for kv test #979

Merged
merged 12 commits into from
Oct 23, 2020
17 changes: 17 additions & 0 deletions examples/kv_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
include(ExternalProject)
# if no cdt root is given use default path
if(EOSIO_CDT_ROOT STREQUAL "" OR NOT EOSIO_CDT_ROOT)
find_package(eosio.cdt)
endif()

ExternalProject_Add(
kvtest_project
SOURCE_DIR ${CMAKE_SOURCE_DIR}/src
BINARY_DIR ${CMAKE_BINARY_DIR}/kvtest
CMAKE_ARGS -DCMAKE_TOOLCHAIN_FILE=${EOSIO_CDT_ROOT}/lib/cmake/eosio.cdt/EosioWasmToolchain.cmake
UPDATE_COMMAND ""
PATCH_COMMAND ""
TEST_COMMAND ""
INSTALL_COMMAND ""
BUILD_ALWAYS 1
)
16 changes: 16 additions & 0 deletions examples/kv_tests/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--- kv_addr_book Project ---
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong project name?


--backing-store chainbase -- How to Build with CMake and Make --
- cd into the 'build' directory
- run the command 'cmake .. -DEOSIO_CONTRACTS_ROOT=/path/to//eosio.contracts'
- run the command 'make'

- After build -
- The built smart contract is under the 'kvtest' directory in the 'build' directory
- You can then do a 'set contract' action with 'cleos' and point to the './build/kvtest' directory

- Additions to CMake should be done to the CMakeLists.txt in the './src' directory and not in the top level CMakeLists.txt

- After build -
- The built smart contract is in the 'build' directory
- You can then do a 'set contract' action with 'cleos' and point to the 'build' directory
3 changes: 3 additions & 0 deletions examples/kv_tests/ricardian/kv_example.contracts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1 class="contract"> hi </h1>

Stub for hi action's ricardian contract
7 changes: 7 additions & 0 deletions examples/kv_tests/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
project(kvtest)

set(EOSIO_WASM_OLD_BEHAVIOR "Off")
find_package(eosio.cdt)

add_contract( kvtest kvtest kvtest.cpp )
target_ricardian_directory( kvtest ${CMAKE_SOURCE_DIR}/../ricardian )
60 changes: 60 additions & 0 deletions examples/kv_tests/src/kvtest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <eosio/eosio.hpp>
#include <eosio/asset.hpp>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have to include this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I used eosio::check in the code.


using namespace eosio;

struct kvtest_record {
uint64_t id;
std::string data;
};

class [[eosio::contract]] kvtest : public contract {
struct [[eosio::table]] kvtest_table : eosio::kv::table<kvtest_record, "kvtest"_n> {
KV_NAMED_INDEX("by.id", id);
kvtest_table(name contract) { init(contract, id); }
};

public:
using contract::contract;

[[eosio::action]]
void smalltest() {
require_auth(_self);
kvtest_table table{"kvtest"_n};

uint64_t id = 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check tab character here and below.

std::string data = "test";

for (; id < 5000; ++id) {
table.put({id, data + std::to_string(id)}, get_self());
}

for (id = 1; id < 5000; ++id) {
auto itr = table. id.find(id);
if (itr != table.id.end()) {
std::string val = itr.value().data;
eosio::check(val.compare(data + std::to_string(id)), "The value for the key " + std::to_string(id) + " is " + val);
}
}
}

[[eosio::action]]
void largetest() {
require_auth(_self);
kvtest_table table{"kvtest"_n};

std::string data = std::string(8 * 1024 * 1024, 't');

uint64_t id = 1;
for (; id < 200; ++id)
table.put({id, std::to_string(id) + data}, get_self());

for (id = 1; id < 200; ++id) {
auto itr = table.id.find(id);
if (itr != table.id.end()) {
std::string val = itr.value().data;
eosio::check(val.compare(data + std::to_string(id)), "The value for the key " + std::to_string(id) + " is " + val);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

val can be 8MB string too big to print.

}
}
}
};