From 59d9f91aff885787f42ccb21ed4f832d91d0b5d4 Mon Sep 17 00:00:00 2001 From: Bucky Kittinger Date: Thu, 25 Jan 2018 14:43:13 -0500 Subject: [PATCH] Still on DB --- contracts/test_api_db/test_api_db.cpp | 2 +- contracts/test_api_db/test_db.cpp | 26 ++++++++----------- .../include/eosio/chain/apply_context.hpp | 6 +++-- libraries/chain/wasm_interface.cpp | 3 +-- libraries/fc/include/fc/utility.hpp | 9 +++---- tests/api_tests/api_tests.cpp | 1 + 6 files changed, 22 insertions(+), 25 deletions(-) diff --git a/contracts/test_api_db/test_api_db.cpp b/contracts/test_api_db/test_api_db.cpp index 09edf7d6e..738a3c97b 100644 --- a/contracts/test_api_db/test_api_db.cpp +++ b/contracts/test_api_db/test_api_db.cpp @@ -17,8 +17,8 @@ extern "C" { //test db WASM_TEST_HANDLER(test_db, key_i64_general); -#if 0 WASM_TEST_HANDLER(test_db, key_i64_remove_all); +#if 0 WASM_TEST_HANDLER(test_db, key_i64_small_load); WASM_TEST_HANDLER(test_db, key_i64_small_store); WASM_TEST_HANDLER(test_db, key_i64_store_scope); diff --git a/contracts/test_api_db/test_db.cpp b/contracts/test_api_db/test_db.cpp index 0876e81e6..204fcdb5a 100644 --- a/contracts/test_api_db/test_db.cpp +++ b/contracts/test_api_db/test_db.cpp @@ -372,12 +372,9 @@ void test_db::key_i64_general() { res = load_i64(current_receiver(), current_receiver(), N(test_table), &dave, sizeof(test_model)); assert(res == sizeof(test_model) && dave.age == 46 && dave.phone == 6535354, "dave error 1"); - res = load_i64(current_receiver(), N(other_code), N(test_table), &alice, sizeof(test_model)); - prints("other "); - printi(res); - prints("\n"); - return; - assert(res == 0, "other_code"); + // TODO fix this + //res = load_i64(current_receiver(), N(other_code), N(test_table), &alice, sizeof(test_model)); + // assert(res == 0, "other_code"); res = load_i64(current_receiver(), current_receiver(), N(other_table), &alice, sizeof(test_model)); assert(res == 0, "other_table"); @@ -411,13 +408,14 @@ void test_db::key_i64_general() { my_memset(&tmp, 0, sizeof(test_model)); tmp.name = N(dave); - res = lower_bound_i64( current_receiver(), current_receiver(), N(test_table), &tmp, sizeof(uint64_t) ); - assert(res == sizeof(uint64_t) && tmp.name == N(dave), "lower_bound_i64 dave" ); + // data packet only big enough for name + res = lower_bound_i64( current_receiver(), current_receiver(), N(test_table), &tmp, sizeof(uint64_t) ); + assert(res == sizeof(test_model) && tmp.name == N(dave), "lower_bound_i64 dave" ); my_memset(&tmp, 0, sizeof(test_model)); tmp.name = N(davf); res = lower_bound_i64( current_receiver(), current_receiver(), N(test_table), &tmp, sizeof(uint64_t) ); - assert(res == -1, "lower_bound_i64 fail" ); + assert(res == 0, "lower_bound_i64 fail" ); my_memset(&tmp, 0, sizeof(test_model)); tmp.name = N(alice); @@ -427,8 +425,7 @@ void test_db::key_i64_general() { my_memset(&tmp, 0, sizeof(test_model)); tmp.name = N(dave); res = upper_bound_i64( current_receiver(), current_receiver(), N(test_table), &tmp, sizeof(test_model) ); - assert(res == -1, "upper_bound_i64 dave" ); - + assert(res == 0, "upper_bound_i64 dave" ); test_model_v3 tmp2; tmp2.name = N(alice); @@ -467,11 +464,10 @@ void test_db::key_i64_general() { //Remove dummy records uint64_t tables[] { N(test_tabld), N(test_tablf) }; for(auto& t : tables) { - while( front_i64( current_receiver(), current_receiver(), t, &tmp, sizeof(test_model) ) != -1 ) { + while( front_i64( current_receiver(), current_receiver(), t, &tmp, sizeof(test_model) ) != 0 ) { remove_i64(current_receiver(), t, &tmp); } } - } void test_db::key_i64_remove_all() { @@ -497,10 +493,10 @@ void test_db::key_i64_remove_all() { test_model tmp; res = front_i64( current_receiver(), current_receiver(), N(test_table), &tmp, sizeof(test_model) ); - assert(res == -1, "front_i64 remove"); + assert(res == 0, "front_i64 remove"); res = back_i64( current_receiver(), current_receiver(), N(test_table), &tmp, sizeof(test_model) ); - assert(res == -1, "back_i64_i64 remove"); + assert(res == 0, "back_i64_i64 remove"); key = N(alice); res = remove_i64(current_receiver(), N(test_table), &key); diff --git a/libraries/chain/include/eosio/chain/apply_context.hpp b/libraries/chain/include/eosio/chain/apply_context.hpp index d1b37a975..baff3d98e 100644 --- a/libraries/chain/include/eosio/chain/apply_context.hpp +++ b/libraries/chain/include/eosio/chain/apply_context.hpp @@ -474,7 +474,6 @@ using apply_handler = std::function; !impl::record_scope_compare::compare(*itr, keys)) return 0; impl::key_helper::get(keys, *itr); - std::cout << "WHY!" << "\n"; if (valuelen) { auto copylen = std::min(itr->value.size(), valuelen); if (copylen) { @@ -622,8 +621,8 @@ using apply_handler = std::function; template int32_t apply_context::lower_bound_record( const table_id_object& t_id, typename IndexType::value_type::key_type* keys, char* value, size_t valuelen ) { + std::cout << "made it to lower\n"; require_read_scope( t_id.scope ); - const auto& idx = db.get_index(); auto tuple = impl::lower_bound_tuple::get(t_id, keys); auto itr = idx.lower_bound(tuple); @@ -635,11 +634,14 @@ using apply_handler = std::function; if (valuelen) { auto copylen = std::min(itr->value.size(), valuelen); + std::cout << "copylen " << copylen << "\n"; + std::cout << "valuelen " << valuelen << "\n"; if (copylen) { itr->value.copy(value, copylen); } return copylen; } else { + std::cout << "valuelen2 " << valuelen << "\n"; return itr->value.size(); } } diff --git a/libraries/chain/wasm_interface.cpp b/libraries/chain/wasm_interface.cpp index 0354b8691..afb7f013f 100644 --- a/libraries/chain/wasm_interface.cpp +++ b/libraries/chain/wasm_interface.cpp @@ -843,8 +843,7 @@ class db_index_api : public context_aware_api { int load(const scope_name& scope, const account_name& code, const name& table, array_ptr data, size_t data_len) { auto res = call(&apply_context::load_record, scope, code, table, data, data_len); - std::cout << "res " << res << "\n"; - return (res == 0) ? 0 : res + sizeof(KeyArrayType); + return (res > 0) ? res + sizeof(KeyArrayType) : 0; } int front(const scope_name& scope, const account_name& code, const name& table, array_ptr data, size_t data_len) { diff --git a/libraries/fc/include/fc/utility.hpp b/libraries/fc/include/fc/utility.hpp index 65941b386..954518b32 100644 --- a/libraries/fc/include/fc/utility.hpp +++ b/libraries/fc/include/fc/utility.hpp @@ -53,11 +53,10 @@ namespace fc { const T& min( const T& a, const T& b ) { return a < b ? a: b; } constexpr size_t const_strlen(const char* str) { - if (*str == '\0') { - return 0; - } - - return 1 + const_strlen(str+1); + int i = 0; + while(*(str+i) != '\0') + i++; + return i; } diff --git a/tests/api_tests/api_tests.cpp b/tests/api_tests/api_tests.cpp index de8a6ded9..ab19f4518 100644 --- a/tests/api_tests/api_tests.cpp +++ b/tests/api_tests/api_tests.cpp @@ -522,6 +522,7 @@ BOOST_FIXTURE_TEST_CASE(db_tests, tester) { try { produce_blocks(1); CALL_TEST_FUNCTION( *this, "test_db", "key_i64_general", {}); + CALL_TEST_FUNCTION( *this, "test_db", "key_i64_remove_all", {}); } FC_LOG_AND_RETHROW() } #endif -- GitLab