From edb5d7b643d664da415169444184015ee8c8adac Mon Sep 17 00:00:00 2001 From: Huang-Ming Huang Date: Mon, 8 Feb 2021 11:15:51 -0600 Subject: [PATCH 1/2] revert changes to empty string as present for lower_bound, upper_bound, or index_value --- plugins/chain_plugin/chain_plugin.cpp | 14 ++++----- .../eosio/chain_plugin/chain_plugin.hpp | 6 ++-- tests/get_kv_table_addr_tests.cpp | 30 +++++++++---------- tests/get_kv_table_nodeos_tests.cpp | 11 ------- 4 files changed, 24 insertions(+), 37 deletions(-) diff --git a/plugins/chain_plugin/chain_plugin.cpp b/plugins/chain_plugin/chain_plugin.cpp index ab6cad72681..e4e527fff6e 100644 --- a/plugins/chain_plugin/chain_plugin.cpp +++ b/plugins/chain_plugin/chain_plugin.cpp @@ -2275,7 +2275,7 @@ struct kv_table_rows_context { abis.set_abi(abi, yield_function); } - bool point_query() const { return p.index_value.has_value(); } + bool point_query() const { return p.index_value.size(); } void write_prefix(fixed_buf_stream& strm) const { strm.write('\1'); @@ -2283,17 +2283,15 @@ struct kv_table_rows_context { to_key(p.index_name.to_uint64_t(), strm); } - std::vector get_full_key(const std::optional& maybe_key) const { + std::vector get_full_key(string key) const { // the max possible encoded_key_byte_count occurs when the encoded type is string and when all characters // in the string is '\0' - const size_t max_encoded_key_byte_count = - maybe_key.has_value() ? std::max(sizeof(uint64_t), 2 * maybe_key->size() + 1) : 0; + const size_t max_encoded_key_byte_count = std::max(sizeof(uint64_t), 2 * key.size() + 1); std::vector full_key(prefix_size + max_encoded_key_byte_count); fixed_buf_stream strm(full_key.data(), full_key.size()); write_prefix(strm); - if (maybe_key.has_value()) { - key_helper::write_key(index_type, p.encode_type, *maybe_key, strm); - } + if (key.size()) + key_helper::write_key(index_type, p.encode_type, key, strm); full_key.resize(strm.pos - full_key.data()); return full_key; } @@ -2455,7 +2453,7 @@ read_only::get_table_rows_result read_only::get_kv_table_rows(const read_only::g kv_table_rows_context context{db, p, abi_serializer_max_time, shorten_abi_errors}; if (context.point_query()) { - EOS_ASSERT(!p.lower_bound && !p.upper_bound, chain::contract_table_query_exception, + EOS_ASSERT(p.lower_bound.empty() && p.upper_bound.empty(), chain::contract_table_query_exception, "specify both index_value and ranges (i.e. lower_bound/upper_bound) is not allowed"); read_only::get_table_rows_result result; auto full_key = context.get_full_key(p.index_value); diff --git a/plugins/chain_plugin/include/eosio/chain_plugin/chain_plugin.hpp b/plugins/chain_plugin/include/eosio/chain_plugin/chain_plugin.hpp index 473d656b3f4..ad38393a1bd 100644 --- a/plugins/chain_plugin/include/eosio/chain_plugin/chain_plugin.hpp +++ b/plugins/chain_plugin/include/eosio/chain_plugin/chain_plugin.hpp @@ -353,9 +353,9 @@ class read_only { name table; // name of kv table, name index_name; // name of index index string encode_type = "bytes"; // "bytes" : binary values in index_value/lower_bound/upper_bound - std::optional index_value; // index value for point query. If this is set, it is processed as a point query - std::optional lower_bound; // lower bound value of index of index_name. If index_value is not set and lower_bound is not set, return from the beginning of range in the prefix - std::optional upper_bound; // upper bound value of index of index_name, If index_value is not set and upper_bound is not set, It is set to the beginning of the next prefix range. + string index_value; // index value for point query. If this is set, it is processed as a point query + string lower_bound; // lower bound value of index of index_name. If index_value is not set and lower_bound is not set, return from the beginning of range in the prefix + string upper_bound; // upper bound value of index of index_name, If index_value is not set and upper_bound is not set, It is set to the beginning of the next prefix range. uint32_t limit = 10; // max number of rows bool reverse = false; // if true output rows in reverse order bool show_payer = false; diff --git a/tests/get_kv_table_addr_tests.cpp b/tests/get_kv_table_addr_tests.cpp index eacad53cf78..b0f3483b53e 100644 --- a/tests/get_kv_table_addr_tests.cpp +++ b/tests/get_kv_table_addr_tests.cpp @@ -109,8 +109,8 @@ BOOST_FIXTURE_TEST_CASE( get_kv_table_addr_test, TESTER ) try { p.index_name = "accname"_n; p.index_value = "john"; p.encode_type = "name"; - p.lower_bound = {}; - p.upper_bound = {}; + p.lower_bound = ""; + p.upper_bound = ""; p.json = true; p.reverse = false; result = plugin.read_only::get_kv_table_rows(p); @@ -118,10 +118,10 @@ BOOST_FIXTURE_TEST_CASE( get_kv_table_addr_test, TESTER ) try { chk_result(0, 2); p.index_name = "accname"_n; - p.index_value = {}; + p.index_value = ""; p.encode_type = "name"; p.lower_bound = "aaa"; - p.upper_bound = {}; + p.upper_bound = ""; p.reverse = false; result = plugin.read_only::get_kv_table_rows(p); BOOST_REQUIRE_EQUAL(4u, result.rows.size()); @@ -131,10 +131,10 @@ BOOST_FIXTURE_TEST_CASE( get_kv_table_addr_test, TESTER ) try { chk_result(3, 4); p.index_name = "accname"_n; - p.index_value = {}; + p.index_value = ""; p.encode_type = "name"; p.lower_bound = "john"; - p.upper_bound = {}; + p.upper_bound = ""; p.reverse = false; result = plugin.read_only::get_kv_table_rows(p); BOOST_REQUIRE_EQUAL(3u, result.rows.size()); @@ -143,7 +143,7 @@ BOOST_FIXTURE_TEST_CASE( get_kv_table_addr_test, TESTER ) try { chk_result(2, 4); p.index_name = "accname"_n; - p.index_value = {}; + p.index_value = ""; p.encode_type = "name"; p.lower_bound = "john"; p.upper_bound = "lois"; @@ -153,9 +153,9 @@ BOOST_FIXTURE_TEST_CASE( get_kv_table_addr_test, TESTER ) try { chk_result(0, 2); p.index_name = "accname"_n; - p.index_value = {}; + p.index_value = ""; p.encode_type = "name"; - p.lower_bound = {}; + p.lower_bound = ""; p.upper_bound = "steve"; p.reverse = true; result = plugin.read_only::get_kv_table_rows(p); @@ -166,7 +166,7 @@ BOOST_FIXTURE_TEST_CASE( get_kv_table_addr_test, TESTER ) try { chk_result(3, 1); p.index_name = "accname"_n; - p.index_value = {}; + p.index_value = ""; p.encode_type = "name"; p.lower_bound = "john"; p.upper_bound = "steve"; @@ -177,16 +177,16 @@ BOOST_FIXTURE_TEST_CASE( get_kv_table_addr_test, TESTER ) try { chk_result(1, 3); p.index_name = "accname"_n; - p.index_value = {}; + p.index_value = ""; p.encode_type = "name"; - p.lower_bound = {}; + p.lower_bound = ""; p.upper_bound = "aaaa"; p.reverse = true; result = plugin.read_only::get_kv_table_rows(p); BOOST_REQUIRE_EQUAL(0u, result.rows.size()); p.index_name = "accname"_n; - p.index_value = {}; + p.index_value = ""; p.encode_type = "name"; p.lower_bound = "steve"; p.upper_bound = "john"; @@ -195,9 +195,9 @@ BOOST_FIXTURE_TEST_CASE( get_kv_table_addr_test, TESTER ) try { BOOST_REQUIRE_EQUAL(0u, result.rows.size()); p.index_name = "accname"_n; - p.index_value = {}; + p.index_value = ""; p.encode_type = "name"; - p.lower_bound = {}; + p.lower_bound = ""; p.upper_bound = "john"; p.reverse = true; result = plugin.read_only::get_kv_table_rows(p); diff --git a/tests/get_kv_table_nodeos_tests.cpp b/tests/get_kv_table_nodeos_tests.cpp index 01d94ac332e..bcf4a1c23ce 100644 --- a/tests/get_kv_table_nodeos_tests.cpp +++ b/tests/get_kv_table_nodeos_tests.cpp @@ -142,11 +142,6 @@ BOOST_FIXTURE_TEST_CASE( get_kv_table_nodeos_test, TESTER ) try { p.upper_bound = "bobe"; BOOST_CHECK_THROW(plugin.read_only::get_kv_table_rows(p), contract_table_query_exception); - p.index_value = ""; - p.lower_bound = "bobb"; - p.upper_bound = "bobe"; - BOOST_CHECK_THROW(plugin.read_only::get_kv_table_rows(p), contract_table_query_exception); - p.index_value = {}; p.lower_bound = "bobe"; p.upper_bound = "bobb"; @@ -169,12 +164,6 @@ BOOST_FIXTURE_TEST_CASE( get_kv_table_nodeos_test, TESTER ) try { chk_result(8, 9); chk_result(9, 10); - p.show_payer = false; - p.lower_bound = "aaaa"; - p.upper_bound = ""; - result = plugin.read_only::get_kv_table_rows(p); - BOOST_REQUIRE_EQUAL(0u, result.rows.size()); - p.lower_bound = "boba"; p.upper_bound = {}; result = plugin.read_only::get_kv_table_rows(p); From f641b090496d9ee17cf03c92cad9684b71a5f4c9 Mon Sep 17 00:00:00 2001 From: Huang-Ming Huang Date: Mon, 8 Feb 2021 14:38:14 -0600 Subject: [PATCH 2/2] Remove the bytes default for get_kv_table_rows_params.encode_type. --- .../chain_plugin/include/eosio/chain_plugin/chain_plugin.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/chain_plugin/include/eosio/chain_plugin/chain_plugin.hpp b/plugins/chain_plugin/include/eosio/chain_plugin/chain_plugin.hpp index ad38393a1bd..44e35d36694 100644 --- a/plugins/chain_plugin/include/eosio/chain_plugin/chain_plugin.hpp +++ b/plugins/chain_plugin/include/eosio/chain_plugin/chain_plugin.hpp @@ -352,7 +352,7 @@ class read_only { name code; // name of contract name table; // name of kv table, name index_name; // name of index index - string encode_type = "bytes"; // "bytes" : binary values in index_value/lower_bound/upper_bound + string encode_type; // encoded type for values in index_value/lower_bound/upper_bound string index_value; // index value for point query. If this is set, it is processed as a point query string lower_bound; // lower bound value of index of index_name. If index_value is not set and lower_bound is not set, return from the beginning of range in the prefix string upper_bound; // upper bound value of index of index_name, If index_value is not set and upper_bound is not set, It is set to the beginning of the next prefix range.