From 5135cc19724967feffd2c879a4681769e3ca76d4 Mon Sep 17 00:00:00 2001 From: XingQiang Bai Date: Mon, 19 Feb 2024 09:27:53 +0800 Subject: [PATCH] fix keypage hash is inconsistent with stateStorage (#4230) --- CMakeLists.txt | 2 +- .../src/executor/TransactionExecutor.cpp | 8 +- .../bcos-framework/ledger/Features.h | 1 + .../bcos-framework/protocol/Protocol.h | 3 +- .../unittests/interfaces/FeaturesTest.cpp | 7 +- bcos-table/src/KeyPageStorage.cpp | 14 +- bcos-table/src/KeyPageStorage.h | 10 +- bcos-table/src/StateStorage.h | 9 +- bcos-table/src/StateStorageInterface.h | 3 +- bcos-table/test/unittests/libtable/Table.cpp | 4 +- .../unittests/libtable/TestKeyPageStorage.cpp | 175 +++++++++++------- .../unittests/libtable/TestStateStorage.cpp | 73 ++++---- tests/perf/benchmark.cpp | 5 +- 13 files changed, 190 insertions(+), 124 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 433a8a6d2d..8146644843 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,7 @@ endif() list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake) -set(VERSION "3.2.6") +set(VERSION "3.2.7") set(VERSION_SUFFIX "") include(Options) configure_project() diff --git a/bcos-executor/src/executor/TransactionExecutor.cpp b/bcos-executor/src/executor/TransactionExecutor.cpp index aa68074135..d63ef4eee0 100644 --- a/bcos-executor/src/executor/TransactionExecutor.cpp +++ b/bcos-executor/src/executor/TransactionExecutor.cpp @@ -1066,11 +1066,11 @@ void TransactionExecutor::getHash(bcos::protocol::BlockNumber number, // remove suicides beforehand m_blockContext->killSuicides(); - - auto hash = last.storage->hash(m_hashImpl, - m_blockContext->features().get(ledger::Features::Flag::bugfix_statestorage_hash)); + auto start = utcTime(); + auto hash = last.storage->hash(m_hashImpl, m_blockContext->features()); + auto end = utcTime(); EXECUTOR_NAME_LOG(INFO) << BLOCK_NUMBER(number) << "GetTableHashes success" - << LOG_KV("hash", hash.hex()); + << LOG_KV("hash", hash.hex()) << LOG_KV("time(ms)", (end - start)); callback(nullptr, hash); } diff --git a/bcos-framework/bcos-framework/ledger/Features.h b/bcos-framework/bcos-framework/ledger/Features.h index b838490288..91d9bbe435 100644 --- a/bcos-framework/bcos-framework/ledger/Features.h +++ b/bcos-framework/bcos-framework/ledger/Features.h @@ -32,6 +32,7 @@ class Features bugfix_event_log_order, bugfix_call_noaddr_return, bugfix_precompiled_codehash, + bugfix_keypage_system_entry_hash, feature_dmc2serial, }; diff --git a/bcos-framework/bcos-framework/protocol/Protocol.h b/bcos-framework/bcos-framework/protocol/Protocol.h index 516aca567e..8b59ccade4 100644 --- a/bcos-framework/bcos-framework/protocol/Protocol.h +++ b/bcos-framework/bcos-framework/protocol/Protocol.h @@ -113,6 +113,7 @@ enum ProtocolVersion : uint32_t enum class BlockVersion : uint32_t { + V3_2_7_VERSION = 0x03020700, V3_2_6_VERSION = 0x03020600, V3_2_5_VERSION = 0x03020500, V3_2_4_VERSION = 0x03020400, @@ -122,7 +123,7 @@ enum class BlockVersion : uint32_t V3_0_VERSION = 0x03000000, RC4_VERSION = 4, MIN_VERSION = RC4_VERSION, - MAX_VERSION = V3_2_6_VERSION, + MAX_VERSION = V3_2_7_VERSION, }; const std::string RC4_VERSION_STR = "3.0.0-rc4"; const std::string V3_0_VERSION_STR = "3.0.0"; diff --git a/bcos-framework/test/unittests/interfaces/FeaturesTest.cpp b/bcos-framework/test/unittests/interfaces/FeaturesTest.cpp index 19af9c792a..e829666dc6 100644 --- a/bcos-framework/test/unittests/interfaces/FeaturesTest.cpp +++ b/bcos-framework/test/unittests/interfaces/FeaturesTest.cpp @@ -64,6 +64,10 @@ BOOST_AUTO_TEST_CASE(feature) BOOST_CHECK_EQUAL(features5.get(Features::Flag::feature_dmc2serial), true); BOOST_CHECK_EQUAL(features5.get("feature_dmc2serial"), true); + features5.set(Features::Flag::bugfix_keypage_system_entry_hash); + BOOST_CHECK_EQUAL(features5.get(Features::Flag::bugfix_keypage_system_entry_hash), true); + BOOST_CHECK_EQUAL(features5.get("bugfix_keypage_system_entry_hash"), true); + auto keys = Features::featureKeys(); BOOST_CHECK_EQUAL(keys.size(), 7); BOOST_CHECK_EQUAL(keys[0], "bugfix_revert"); @@ -72,7 +76,8 @@ BOOST_AUTO_TEST_CASE(feature) BOOST_CHECK_EQUAL(keys[3], "bugfix_event_log_order"); BOOST_CHECK_EQUAL(keys[4], "bugfix_call_noaddr_return"); BOOST_CHECK_EQUAL(keys[5], "bugfix_precompiled_codehash"); - BOOST_CHECK_EQUAL(keys[6], "feature_dmc2serial"); + BOOST_CHECK_EQUAL(keys[6], "bugfix_keypage_system_entry_hash"); + BOOST_CHECK_EQUAL(keys[7], "feature_dmc2serial"); } BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/bcos-table/src/KeyPageStorage.cpp b/bcos-table/src/KeyPageStorage.cpp index 5d063e6a6a..577e8cc0ac 100644 --- a/bcos-table/src/KeyPageStorage.cpp +++ b/bcos-table/src/KeyPageStorage.cpp @@ -361,8 +361,8 @@ void KeyPageStorage::parallelTraverse(bool onlyDirty, KeyPage_LOG(INFO) << LOG_DESC("parallelTraverse") << LOG_KV("size", m_size); } -auto KeyPageStorage::hash(const bcos::crypto::Hash::Ptr& hashImpl, bool /*useHashV310*/) const - -> crypto::HashType +auto KeyPageStorage::hash(const bcos::crypto::Hash::Ptr& hashImpl, + const ledger::Features& features) const -> crypto::HashType { bcos::crypto::HashType pagesHash(0); bcos::crypto::HashType entriesHash(0); @@ -397,9 +397,12 @@ auto KeyPageStorage::hash(const bcos::crypto::Hash::Ptr& hashImpl, bool /*useHas } else { // sys table - auto hash = hashImpl->hash(data->table); - hash ^= hashImpl->hash(data->key); - hash ^= entry.hash(data->table, data->key, hashImpl, m_blockVersion); + auto hash = entry.hash(data->table, data->key, hashImpl, m_blockVersion); + if (!features.get(ledger::Features::Flag::bugfix_keypage_system_entry_hash)) + { // v3.6.1 open this bugfix default + hash ^= hashImpl->hash(data->table); + hash ^= hashImpl->hash(data->key); + } localEntriesHash ^= hash; ++entrycount; } @@ -413,6 +416,7 @@ auto KeyPageStorage::hash(const bcos::crypto::Hash::Ptr& hashImpl, bool /*useHas totalHash ^= pagesHash; totalHash ^= entriesHash; KeyPage_LOG(INFO) << LOG_DESC("hash") << LOG_KV("size", allData.size()) + << LOG_KV("blockVersion", m_blockVersion) << LOG_KV("readLength", m_readLength) << LOG_KV("writeLength", m_writeLength) << LOG_KV("pageCount", pageCount) << LOG_KV("entrycount", entrycount) << LOG_KV("entriesHash", entriesHash.hex()) diff --git a/bcos-table/src/KeyPageStorage.h b/bcos-table/src/KeyPageStorage.h index f17f4762b7..2e13e26e9a 100644 --- a/bcos-table/src/KeyPageStorage.h +++ b/bcos-table/src/KeyPageStorage.h @@ -144,7 +144,7 @@ class KeyPageStorage : public virtual storage::StateStorageInterface callback) const override; crypto::HashType hash( - const bcos::crypto::Hash::Ptr& hashImpl, bool /*useHashV310*/) const override; + const bcos::crypto::Hash::Ptr& hashImpl, const ledger::Features& features) const override; void rollback(const Recoder& recoder) override; @@ -888,10 +888,10 @@ class KeyPageStorage : public virtual storage::StateStorageInterface m_invalidPageKeys.clear(); if (!entries.empty() && pageKey != entries.rbegin()->first) { - KeyPage_LOG(WARNING) << LOG_DESC("import page with invalid pageKey") - << LOG_KV("pageKey", toHex(pageKey)) - << LOG_KV("validPageKey", toHex(entries.rbegin()->first)) - << LOG_KV("count", entries.size()); + KeyPage_LOG(DEBUG) << LOG_DESC("import page with invalid pageKey") + << LOG_KV("pageKey", toHex(pageKey)) + << LOG_KV("validPageKey", toHex(entries.rbegin()->first)) + << LOG_KV("count", entries.size()); m_invalidPageKeys.insert(std::string(pageKey)); } if (entries.empty()) diff --git a/bcos-table/src/StateStorage.h b/bcos-table/src/StateStorage.h index 7e688162d6..2bbedc21cf 100644 --- a/bcos-table/src/StateStorage.h +++ b/bcos-table/src/StateStorage.h @@ -391,11 +391,13 @@ class BaseStorage : public virtual storage::StateStorageInterface, STORAGE_LOG(INFO) << "Successful merged records" << LOG_KV("count", count); } - crypto::HashType hash(const bcos::crypto::Hash::Ptr& hashImpl, bool useHashV310) const override + crypto::HashType hash( + const bcos::crypto::Hash::Ptr& hashImpl, const ledger::Features& features) const override { bcos::crypto::HashType totalHash; - auto blockVersion = useHashV310 ? (uint32_t)bcos::protocol::BlockVersion::V3_1_VERSION : - (uint32_t)bcos::protocol::BlockVersion::V3_0_VERSION; + auto blockVersion = features.get(ledger::Features::Flag::bugfix_statestorage_hash) ? + (uint32_t)bcos::protocol::BlockVersion::V3_1_VERSION : + (uint32_t)bcos::protocol::BlockVersion::V3_0_VERSION; std::vector hashes(m_buckets.size()); tbb::parallel_for(tbb::blocked_range(0U, m_buckets.size()), [&, this]( @@ -434,7 +436,6 @@ class BaseStorage : public virtual storage::StateStorageInterface, totalHash ^= it; } - return totalHash; } diff --git a/bcos-table/src/StateStorageInterface.h b/bcos-table/src/StateStorageInterface.h index 2a0a8b8f5b..a23f16a6e6 100644 --- a/bcos-table/src/StateStorageInterface.h +++ b/bcos-table/src/StateStorageInterface.h @@ -20,6 +20,7 @@ */ #pragma once +#include "bcos-framework/ledger/Features.h" #include "bcos-framework/storage/StorageInterface.h" #include "bcos-framework/storage/Table.h" #include "tbb/enumerable_thread_specific.h" @@ -107,7 +108,7 @@ class StateStorageInterface : public virtual storage::TraverseStorageInterface } virtual crypto::HashType hash( - const bcos::crypto::Hash::Ptr& hashImpl, bool useHashV310) const = 0; + const bcos::crypto::Hash::Ptr& hashImpl, const ledger::Features& features) const = 0; virtual void setPrev(std::shared_ptr prev) { std::unique_lock lock(m_prevMutex); diff --git a/bcos-table/test/unittests/libtable/Table.cpp b/bcos-table/test/unittests/libtable/Table.cpp index ec3b707dad..cc7d6084a9 100644 --- a/bcos-table/test/unittests/libtable/Table.cpp +++ b/bcos-table/test/unittests/libtable/Table.cpp @@ -224,7 +224,7 @@ BOOST_AUTO_TEST_CASE(removeFromCache) deleteEntry->setStatus(Entry::DELETED); BOOST_CHECK_NO_THROW(table->setRow("name", *deleteEntry)); - auto hashs = tableFactory->hash(hashImpl, false); + auto hashs = tableFactory->hash(hashImpl, ledger::Features()); auto tableFactory2 = std::make_shared(nullptr); BOOST_CHECK(tableFactory2->createTable(tableName, valueField)); @@ -234,7 +234,7 @@ BOOST_AUTO_TEST_CASE(removeFromCache) auto deleteEntry2 = std::make_optional(table2->newEntry()); deleteEntry2->setStatus(Entry::DELETED); BOOST_CHECK_NO_THROW(table2->setRow("name", *deleteEntry2)); - auto hashs2 = tableFactory2->hash(hashImpl, false); + auto hashs2 = tableFactory2->hash(hashImpl, ledger::Features()); BOOST_CHECK_EQUAL_COLLECTIONS(hashs.begin(), hashs.end(), hashs2.begin(), hashs2.end()); } diff --git a/bcos-table/test/unittests/libtable/TestKeyPageStorage.cpp b/bcos-table/test/unittests/libtable/TestKeyPageStorage.cpp index b2d0d6afbd..1421193efa 100644 --- a/bcos-table/test/unittests/libtable/TestKeyPageStorage.cpp +++ b/bcos-table/test/unittests/libtable/TestKeyPageStorage.cpp @@ -19,6 +19,7 @@ #include "Hash.h" #include "bcos-crypto/hash/Keccak256.h" +#include "bcos-framework/ledger/Features.h" #include "bcos-framework/storage/StorageInterface.h" #include "bcos-table/src/KeyPageStorage.h" #include "bcos-table/src/StateStorage.h" @@ -114,6 +115,7 @@ struct KeyPageStorageFixture std::string keyField = "key"; std::string valueField = "value"; Condition c; + ledger::Features features; }; BOOST_FIXTURE_TEST_SUITE(KeyPageStorageTest, KeyPageStorageFixture) @@ -163,7 +165,7 @@ BOOST_AUTO_TEST_CASE(rollback) deleteEntry.setStatus(Entry::DELETED); BOOST_REQUIRE_NO_THROW(table->setRow("name", deleteEntry)); - auto hash = tableFactory->hash(hashImpl, false); + auto hash = tableFactory->hash(hashImpl, features); auto countRet = tableFactory->count(testTableName); BOOST_REQUIRE_EQUAL(countRet.first, 0); @@ -182,7 +184,7 @@ BOOST_AUTO_TEST_CASE(rollback) countRet = tableFactory->count(testTableName); BOOST_REQUIRE_EQUAL(countRet.first, 1); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("c18354d205471d61000000000000000000000000000000000000000000000000").hex()); @@ -191,7 +193,7 @@ BOOST_AUTO_TEST_CASE(rollback) BOOST_REQUIRE(entry.has_value()); BOOST_REQUIRE(entry->dirty() == true); BOOST_REQUIRE(entry->getField(0) == "Lili"); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("c18354d205471d61000000000000000000000000000000000000000000000000").hex()); @@ -204,21 +206,21 @@ BOOST_AUTO_TEST_CASE(rollback) table->setRow("id", *entry); countRet = tableFactory->count(testTableName); BOOST_REQUIRE_EQUAL(countRet.first, 2); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("d26dbc9a92ed28b1000000000000000000000000000000000000000000000000").hex()); #endif entry = table->getRow("id"); BOOST_REQUIRE(entry.has_value()); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("d26dbc9a92ed28b1000000000000000000000000000000000000000000000000").hex()); #endif entry = table->getRow("name"); BOOST_REQUIRE(entry.has_value()); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("d26dbc9a92ed28b1000000000000000000000000000000000000000000000000").hex()); @@ -231,21 +233,21 @@ BOOST_AUTO_TEST_CASE(rollback) table->setRow("balance", *entry); countRet = tableFactory->count(testTableName); BOOST_REQUIRE_EQUAL(countRet.first, 3); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("2b7be3797d97dcf7000000000000000000000000000000000000000000000000").hex()); #endif entry = table->getRow("balance"); BOOST_REQUIRE(entry.has_value()); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("2b7be3797d97dcf7000000000000000000000000000000000000000000000000").hex()); #endif entry = table->getRow("name"); BOOST_REQUIRE(entry.has_value()); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("2b7be3797d97dcf7000000000000000000000000000000000000000000000000").hex()); @@ -257,7 +259,7 @@ BOOST_AUTO_TEST_CASE(rollback) table->setRow("name", *deleteEntry2); countRet = tableFactory->count(testTableName); BOOST_REQUIRE_EQUAL(countRet.first, 2); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); // delete entry will cause hash mismatch #if defined(__APPLE__) @@ -266,7 +268,7 @@ BOOST_AUTO_TEST_CASE(rollback) #endif entry = table->getRow("name"); BOOST_REQUIRE(!entry); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("4160d337ddd671e0000000000000000000000000000000000000000000000001").hex()); @@ -275,14 +277,14 @@ BOOST_AUTO_TEST_CASE(rollback) tableFactory->rollback(*savePoint2); countRet = tableFactory->count(testTableName); BOOST_REQUIRE_EQUAL(countRet.first, 3); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("2b7be3797d97dcf7000000000000000000000000000000000000000000000000").hex()); #endif entry = table->getRow("name"); BOOST_REQUIRE_NE(entry->status(), Entry::DELETED); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("2b7be3797d97dcf7000000000000000000000000000000000000000000000000").hex()); @@ -290,21 +292,21 @@ BOOST_AUTO_TEST_CASE(rollback) tableFactory->rollback(*savePoint1); countRet = tableFactory->count(testTableName); BOOST_REQUIRE_EQUAL(countRet.first, 2); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("d26dbc9a92ed28b1000000000000000000000000000000000000000000000000").hex()); #endif entry = table->getRow("name"); BOOST_REQUIRE(entry.has_value()); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("d26dbc9a92ed28b1000000000000000000000000000000000000000000000000").hex()); #endif entry = table->getRow("balance"); BOOST_REQUIRE(!entry); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("d26dbc9a92ed28b1000000000000000000000000000000000000000000000000").hex()); @@ -315,21 +317,21 @@ BOOST_AUTO_TEST_CASE(rollback) BOOST_REQUIRE_EQUAL(countRet.first, 1); entry = table->getRow("name"); BOOST_REQUIRE(entry.has_value()); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("c18354d205471d61000000000000000000000000000000000000000000000000").hex()); #endif entry = table->getRow("balance"); BOOST_REQUIRE(!entry); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("c18354d205471d61000000000000000000000000000000000000000000000000").hex()); #endif entry = table->getRow("id"); BOOST_REQUIRE(!entry); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("c18354d205471d61000000000000000000000000000000000000000000000000").hex()); @@ -341,7 +343,7 @@ BOOST_AUTO_TEST_CASE(rollback) BOOST_REQUIRE_NO_THROW(table->setRow("id", *entry)); countRet = tableFactory->count(testTableName); BOOST_REQUIRE_EQUAL(countRet.first, 2); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("2c14904fc33bbbae000000000000000000000000000000000000000000000000").hex()); @@ -351,7 +353,7 @@ BOOST_AUTO_TEST_CASE(rollback) BOOST_REQUIRE_NO_THROW(table->setRow("id", *entry)); countRet = tableFactory->count(testTableName); BOOST_REQUIRE_EQUAL(countRet.first, 1); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); // delete entry will cause hash mismatch #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), @@ -361,7 +363,7 @@ BOOST_AUTO_TEST_CASE(rollback) BOOST_AUTO_TEST_CASE(rollback2) { - auto hash0 = tableFactory->hash(hashImpl, false); + auto hash0 = tableFactory->hash(hashImpl, features); // auto savePoint0 = tableFactory->savepoint(); auto savePoint0 = std::make_shared(); tableFactory->setRecoder(savePoint0); @@ -372,7 +374,7 @@ BOOST_AUTO_TEST_CASE(rollback2) auto deleteEntry = table->newDeletedEntry(); table->setRow("name", deleteEntry); - auto hash = tableFactory->hash(hashImpl, false); + auto hash = tableFactory->hash(hashImpl, features); // delete not exist entry will cause hash mismatch #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), @@ -382,14 +384,14 @@ BOOST_AUTO_TEST_CASE(rollback2) // entry->setField("key", "name"); entry->setField(0, "Lili"); table->setRow("name", *entry); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("c18354d205471d61000000000000000000000000000000000000000000000000").hex()); #endif entry = table->getRow("name"); BOOST_REQUIRE(entry.has_value()); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("c18354d205471d61000000000000000000000000000000000000000000000000").hex()); @@ -405,21 +407,21 @@ BOOST_AUTO_TEST_CASE(rollback2) // entry->setField("key", "id"); entry->setField(0, "12345"); table->setRow("id", *entry); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("d26dbc9a92ed28b1000000000000000000000000000000000000000000000000").hex()); #endif entry = table->getRow("id"); BOOST_REQUIRE(entry.has_value()); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("d26dbc9a92ed28b1000000000000000000000000000000000000000000000000").hex()); #endif entry = table->getRow("name"); BOOST_REQUIRE(entry.has_value()); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("d26dbc9a92ed28b1000000000000000000000000000000000000000000000000").hex()); @@ -430,21 +432,21 @@ BOOST_AUTO_TEST_CASE(rollback2) entry = table->getRow("name"); BOOST_REQUIRE(entry.has_value()); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("c18354d205471d61000000000000000000000000000000000000000000000000").hex()); #endif entry = table->getRow("balance"); BOOST_REQUIRE(!entry); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("c18354d205471d61000000000000000000000000000000000000000000000000").hex()); #endif entry = table->getRow("id"); BOOST_REQUIRE(!entry); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("c18354d205471d61000000000000000000000000000000000000000000000000").hex()); @@ -452,12 +454,12 @@ BOOST_AUTO_TEST_CASE(rollback2) // BOOST_REQUIRE(table->dirty() == true); tableFactory->rollback(*savePoint0); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); BOOST_REQUIRE(hash.hex() == crypto::HashType("").hex()); entry = table->getRow("name"); BOOST_REQUIRE(!entry); - auto hash00 = tableFactory->hash(hashImpl, false); + auto hash00 = tableFactory->hash(hashImpl, features); BOOST_REQUIRE(hash00 == crypto::HashType(0)); BOOST_REQUIRE_EQUAL_COLLECTIONS(hash0.begin(), hash0.end(), hash00.begin(), hash00.end()); @@ -468,7 +470,7 @@ BOOST_AUTO_TEST_CASE(rollback2) BOOST_AUTO_TEST_CASE(rollback3) { - auto hash0 = tableFactory->hash(hashImpl, false); + auto hash0 = tableFactory->hash(hashImpl, features); // auto savePoint0 = tableFactory->savepoint(); auto savePoint0 = std::make_shared(); tableFactory->setRecoder(savePoint0); @@ -479,7 +481,7 @@ BOOST_AUTO_TEST_CASE(rollback3) auto entry = table->newEntry(); entry.set("value"); table->setRow("name", entry); - tableFactory->hash(hashImpl, false); + tableFactory->hash(hashImpl, features); // first rollback tableFactory->rollback(*savePoint0); @@ -543,7 +545,7 @@ BOOST_AUTO_TEST_CASE(hash) auto entries = table->getRows(keys); BOOST_REQUIRE(entries.size() == 2); - auto dbHash1 = tableFactory->hash(hashImpl, false); + auto dbHash1 = tableFactory->hash(hashImpl, features); auto savePoint = std::make_shared(); tableFactory->setRecoder(savePoint); @@ -561,7 +563,7 @@ BOOST_AUTO_TEST_CASE(hash) BOOST_REQUIRE(!entry); // BOOST_REQUIRE(table->dirty() == true); - auto dbHash2 = tableFactory->hash(hashImpl, false); + auto dbHash2 = tableFactory->hash(hashImpl, features); BOOST_REQUIRE_EQUAL(dbHash1.hex(), dbHash2.hex()); // getPrimaryKeys and getRows @@ -673,8 +675,8 @@ BOOST_AUTO_TEST_CASE(hash_V3_1_0) getRow.get_future().get(); } - auto dbHash1 = tableFactory1->hash(hashImpl, false); - auto dbHash2 = tableFactory2->hash(hashImpl, false); + auto dbHash1 = tableFactory1->hash(hashImpl, features); + auto dbHash2 = tableFactory2->hash(hashImpl, features); BOOST_REQUIRE_NE(dbHash1.hex(), dbHash2.hex()); } @@ -725,8 +727,8 @@ BOOST_AUTO_TEST_CASE(hash_different_table_same_data) }; setData1(tableFactory1); setData2(tableFactory2); - auto dbHash1 = tableFactory1->hash(hashImpl2, false); - auto dbHash2 = tableFactory2->hash(hashImpl2, false); + auto dbHash1 = tableFactory1->hash(hashImpl2, features); + auto dbHash2 = tableFactory2->hash(hashImpl2, features); BOOST_REQUIRE_EQUAL(dbHash1.hex(), dbHash2.hex()); auto tableFactory3 = make_shared( @@ -736,8 +738,8 @@ BOOST_AUTO_TEST_CASE(hash_different_table_same_data) setData1(tableFactory3); setData2(tableFactory4); - auto dbHash3 = tableFactory3->hash(hashImpl2, false); - auto dbHash4 = tableFactory4->hash(hashImpl2, false); + auto dbHash3 = tableFactory3->hash(hashImpl2, features); + auto dbHash4 = tableFactory4->hash(hashImpl2, features); BOOST_REQUIRE_NE(dbHash3.hex(), dbHash4.hex()); } @@ -1509,7 +1511,7 @@ BOOST_AUTO_TEST_CASE(randomRWHash) } } - hashes.push_back(storage->hash(hashImpl, false)); + hashes.push_back(storage->hash(hashImpl, features)); storage->setReadOnly(false); storage->setReadOnly(true); prev = storage; @@ -1714,7 +1716,7 @@ BOOST_AUTO_TEST_CASE(pageMergeRandom) // } } } - auto hash = tableStorage->hash(hashImpl, false); + auto hash = tableStorage->hash(hashImpl, features); // BOOST_TEST( // hash.hex() == // crypto::HashType("4d4a5c95180905cb000000000000000000000000000000000000000000000000").hex()); @@ -1740,7 +1742,7 @@ BOOST_AUTO_TEST_CASE(pageMergeRandom) } } - hash = tableStorage->hash(hashImpl, false); + hash = tableStorage->hash(hashImpl, features); // BOOST_TEST( // hash.hex() == // crypto::HashType("4d4a5c95180905cb000000000000000000000000000000000000000000000000").hex()); @@ -1770,7 +1772,7 @@ BOOST_AUTO_TEST_CASE(pageMergeRandom) } } - hash = tableStorage->hash(hashImpl, false); + hash = tableStorage->hash(hashImpl, features); // BOOST_TEST( // hash.hex() == // crypto::HashType("4d4a5c95180905cb000000000000000000000000000000000000000000000000").hex()); @@ -2295,11 +2297,11 @@ BOOST_AUTO_TEST_CASE(BigTableAdd) entry1->setField(0, value); BOOST_REQUIRE_NO_THROW(table1->setRow(key, *entry1)); } - auto hash0 = tableStorage0->hash(hashImpl, false); + auto hash0 = tableStorage0->hash(hashImpl, features); BCOS_LOG(DEBUG) << LOG_DESC(">>>>>>>>>>>>KeyPageStorage0") << LOG_KV("i", i) << LOG_KV("hash", hash0.hex()); BOOST_REQUIRE(hash0.hex() != crypto::HashType(0).hex()); - auto hash1 = tableStorage1->hash(hashImpl, false); + auto hash1 = tableStorage1->hash(hashImpl, features); BCOS_LOG(DEBUG) << LOG_DESC(">>>>>>>>>>>>KeyPageStorage1") << LOG_KV("i", i) << LOG_KV("hash", hash1.hex()); BOOST_REQUIRE(hash1.hex() != crypto::HashType(0).hex()); @@ -2310,10 +2312,10 @@ BOOST_AUTO_TEST_CASE(BigTableAdd) tableStorage1->setReadOnly(true); stateStorage0->merge(true, *tableStorage0); stateStorage1->merge(true, *tableStorage1); - hash0 = stateStorage0->hash(hashImpl, false); + hash0 = stateStorage0->hash(hashImpl, features); BCOS_LOG(DEBUG) << LOG_DESC(">>>>>>>>>>>>stateStorage0") << LOG_KV("i", i) << LOG_KV("hash", hash0.hex()); - hash1 = stateStorage1->hash(hashImpl, false); + hash1 = stateStorage1->hash(hashImpl, features); BCOS_LOG(DEBUG) << LOG_DESC(">>>>>>>>>>>>stateStorage1") << LOG_KV("i", i) << LOG_KV("hash", hash1.hex()); // BOOST_TEST(hash0.hex() == hash1.hex()); @@ -2369,11 +2371,11 @@ BOOST_AUTO_TEST_CASE(BigTableAddSerialize) entry1->setField(0, value); BOOST_REQUIRE_NO_THROW(table1->setRow(key, *entry1)); } - auto hash0 = tableStorage0->hash(hashImpl, false); + auto hash0 = tableStorage0->hash(hashImpl, features); BCOS_LOG(DEBUG) << LOG_DESC(">>>>>>>>>>>>KeyPageStorage0") << LOG_KV("i", i) << LOG_KV("hash", hash0.hex()); BOOST_REQUIRE(hash0.hex() != crypto::HashType(0).hex()); - auto hash1 = tableStorage1->hash(hashImpl, false); + auto hash1 = tableStorage1->hash(hashImpl, features); BCOS_LOG(DEBUG) << LOG_DESC(">>>>>>>>>>>>KeyPageStorage1") << LOG_KV("i", i) << LOG_KV("hash", hash1.hex()); BOOST_REQUIRE(hash0.hex() != crypto::HashType(0).hex()); @@ -2383,10 +2385,10 @@ BOOST_AUTO_TEST_CASE(BigTableAddSerialize) tableStorage1->setReadOnly(true); stateStorage0->merge(true, *tableStorage0); stateStorage1->merge(true, *tableStorage1); - hash0 = stateStorage0->hash(hashImpl, false); + hash0 = stateStorage0->hash(hashImpl, features); BCOS_LOG(DEBUG) << LOG_DESC(">>>>>>>>>>>>stateStorage0") << LOG_KV("i", i) << LOG_KV("hash", hash0.hex()); - hash1 = stateStorage1->hash(hashImpl, false); + hash1 = stateStorage1->hash(hashImpl, features); BCOS_LOG(DEBUG) << LOG_DESC(">>>>>>>>>>>>stateStorage1") << LOG_KV("i", i) << LOG_KV("hash", hash1.hex()); BOOST_TEST(hash0.hex() == hash1.hex()); @@ -2468,15 +2470,15 @@ BOOST_AUTO_TEST_CASE(mockCommitProcess) BOOST_REQUIRE(entry2); entry2 = table2->getRow(getKey); } - auto hash0 = tableStorage0->hash(hashImpl, false); + auto hash0 = tableStorage0->hash(hashImpl, features); BCOS_LOG(DEBUG) << LOG_DESC(">>>>>>>>>>>>KeyPageStorage0") << LOG_KV("i", i) << LOG_KV("hash", hash0.hex()); BOOST_REQUIRE(hash0.hex() != crypto::HashType(0).hex()); - auto hash1 = tableStorage1->hash(hashImpl, false); + auto hash1 = tableStorage1->hash(hashImpl, features); BCOS_LOG(DEBUG) << LOG_DESC(">>>>>>>>>>>>KeyPageStorage1") << LOG_KV("i", i) << LOG_KV("hash", hash1.hex()); BOOST_REQUIRE(hash1.hex() != crypto::HashType(0).hex()); - auto hash2 = tableStorage2->hash(hashImpl, false); + auto hash2 = tableStorage2->hash(hashImpl, features); BCOS_LOG(DEBUG) << LOG_DESC(">>>>>>>>>>>>KeyPageStorage2") << LOG_KV("i", i) << LOG_KV("hash", hash2.hex()); BOOST_TEST(hash0.hex() == hash1.hex()); @@ -2586,15 +2588,15 @@ BOOST_AUTO_TEST_CASE(mockCommitProcessParallel) BOOST_REQUIRE(entry2); entry2 = table2->getRow(getKey); } - auto hash0 = tableStorage0->hash(hashImpl, false); + auto hash0 = tableStorage0->hash(hashImpl, features); BCOS_LOG(DEBUG) << LOG_DESC(">>>>>>>>>>>>KeyPageStorage0") << LOG_KV("i", i) << LOG_KV("hash", hash0.hex()); BOOST_REQUIRE(hash0.hex() != crypto::HashType(0).hex()); - auto hash1 = tableStorage1->hash(hashImpl, false); + auto hash1 = tableStorage1->hash(hashImpl, features); BCOS_LOG(DEBUG) << LOG_DESC(">>>>>>>>>>>>KeyPageStorage1") << LOG_KV("i", i) << LOG_KV("hash", hash1.hex()); BOOST_REQUIRE(hash1.hex() != crypto::HashType(0).hex()); - auto hash2 = tableStorage2->hash(hashImpl, false); + auto hash2 = tableStorage2->hash(hashImpl, features); BCOS_LOG(DEBUG) << LOG_DESC(">>>>>>>>>>>>KeyPageStorage2") << LOG_KV("i", i) << LOG_KV("hash", hash2.hex()); BOOST_TEST(hash0.hex() == hash1.hex()); @@ -2844,13 +2846,13 @@ BOOST_AUTO_TEST_CASE(insertAndDelete) BOOST_REQUIRE_NO_THROW(table0->setRow(key, *entry0)); } } - auto hash0 = tableStorage0->hash(hashImpl, false); + auto hash0 = tableStorage0->hash(hashImpl, features); BCOS_LOG(DEBUG) << LOG_DESC(">>>>>>>>>>>>KeyPageStorage0") << LOG_KV("i", i) << LOG_KV("hash", hash0.hex()); BOOST_TEST(hash0.hex() != crypto::HashType(0).hex()); tableStorage0->setReadOnly(true); stateStorage0->merge(true, *tableStorage0); - hash0 = stateStorage0->hash(hashImpl, false); + hash0 = stateStorage0->hash(hashImpl, features); BCOS_LOG(DEBUG) << LOG_DESC(">>>>>>>>>>>>stateStorage0") << LOG_KV("i", i) << LOG_KV("hash", hash0.hex()); index += keyCount; @@ -3054,5 +3056,52 @@ BOOST_AUTO_TEST_CASE(TableMeta_read_write_mutex) std::cout << "==================== test end" << std::endl; // boost::log::core::get()->set_logging_enabled(false); } + + +BOOST_AUTO_TEST_CASE(bugfix_keypage_system_entry_hash) +{ + auto valueFields = "value1"; + auto cacheSize = 256 * 1024 * 1024; + auto pageSize = 512; + auto stateStorage0 = make_shared(nullptr); + stateStorage0->setMaxCapacity(cacheSize); + StateStorageInterface::Ptr prev0 = stateStorage0; + + auto tableName = "table_0"; + BOOST_REQUIRE(prev0->createTable(tableName, valueFields)); + + + auto getHashLambda = [&](std::shared_ptr storage, + const ledger::Features& f) { + auto table0 = storage->openTable(tableName); + BOOST_REQUIRE(table0); + auto key = "1"; + auto value = "value"; + auto entry0 = std::make_optional(table0->newEntry()); + entry0->setField(0, value); + BOOST_REQUIRE_NO_THROW(table0->setRow(key, *entry0)); + auto entry = std::make_optional(table0->newEntry()); + entry->setField(0, value); + storage->asyncSetRow(ledger::SYS_TABLES, "1", *entry, [](Error::UniquePtr) {}); + return storage->hash(hashImpl, f); + }; + auto keypage = std::make_shared( + prev0, pageSize, (uint32_t)protocol::BlockVersion::V3_6_1_VERSION); + auto state = std::make_shared(prev0); + auto hash0 = getHashLambda(keypage, features); + auto hash1 = getHashLambda(state, features); + BOOST_TEST(hash0.hex() != hash1.hex()); + + features.set(ledger::Features::Flag::bugfix_statestorage_hash); + hash0 = getHashLambda(keypage, features); + hash1 = getHashLambda(state, features); + BOOST_TEST(hash0.hex() != hash1.hex()); + + features.set(ledger::Features::Flag::bugfix_keypage_system_entry_hash); + hash0 = getHashLambda(keypage, features); + hash1 = getHashLambda(state, features); + BOOST_TEST(hash0.hex() == hash1.hex()); +} + BOOST_AUTO_TEST_SUITE_END() } // namespace bcos::test diff --git a/bcos-table/test/unittests/libtable/TestStateStorage.cpp b/bcos-table/test/unittests/libtable/TestStateStorage.cpp index 5534fd7223..8825d18fae 100644 --- a/bcos-table/test/unittests/libtable/TestStateStorage.cpp +++ b/bcos-table/test/unittests/libtable/TestStateStorage.cpp @@ -104,6 +104,7 @@ struct TableFactoryFixture std::string testTableName = "t_test"; std::string keyField = "key"; std::string valueField = "value"; + ledger::Features features; }; BOOST_FIXTURE_TEST_SUITE(StateStorageTest, TableFactoryFixture) @@ -138,7 +139,7 @@ BOOST_AUTO_TEST_CASE(rollback) deleteEntry.setStatus(Entry::DELETED); BOOST_REQUIRE_NO_THROW(table->setRow("name", deleteEntry)); - auto hash = tableFactory->hash(hashImpl, false); + auto hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), @@ -148,7 +149,7 @@ BOOST_AUTO_TEST_CASE(rollback) BOOST_REQUIRE_NO_THROW(entry->setField(0, "Lili")); BOOST_REQUIRE_NO_THROW(table->setRow("name", *entry)); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("c18354d205471d61000000000000000000000000000000000000000000000000").hex()); @@ -157,7 +158,7 @@ BOOST_AUTO_TEST_CASE(rollback) BOOST_REQUIRE(entry.has_value()); BOOST_REQUIRE(entry->dirty() == true); BOOST_REQUIRE(entry->getField(0) == "Lili"); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("c18354d205471d61000000000000000000000000000000000000000000000000").hex()); @@ -168,21 +169,21 @@ BOOST_AUTO_TEST_CASE(rollback) entry = table->newEntry(); entry->setField(0, "12345"); table->setRow("id", *entry); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("d26dbc9a92ed28b1000000000000000000000000000000000000000000000000").hex()); #endif entry = table->getRow("id"); BOOST_REQUIRE(entry.has_value()); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("d26dbc9a92ed28b1000000000000000000000000000000000000000000000000").hex()); #endif entry = table->getRow("name"); BOOST_REQUIRE(entry.has_value()); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("d26dbc9a92ed28b1000000000000000000000000000000000000000000000000").hex()); @@ -193,21 +194,21 @@ BOOST_AUTO_TEST_CASE(rollback) entry = table->newEntry(); entry->setField(0, "500"); table->setRow("balance", *entry); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("2b7be3797d97dcf7000000000000000000000000000000000000000000000000").hex()); #endif entry = table->getRow("balance"); BOOST_REQUIRE(entry.has_value()); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("2b7be3797d97dcf7000000000000000000000000000000000000000000000000").hex()); #endif entry = table->getRow("name"); BOOST_REQUIRE(entry.has_value()); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("2b7be3797d97dcf7000000000000000000000000000000000000000000000000").hex()); @@ -217,7 +218,7 @@ BOOST_AUTO_TEST_CASE(rollback) auto deleteEntry2 = std::make_optional(table->newDeletedEntry()); table->setRow("name", *deleteEntry2); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); // delete entry will cause hash mismatch #if defined(__APPLE__) @@ -226,41 +227,41 @@ BOOST_AUTO_TEST_CASE(rollback) #endif entry = table->getRow("name"); BOOST_REQUIRE(!entry); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("4160d337ddd671e0000000000000000000000000000000000000000000000001").hex()); #endif std::cout << "Try remove balance" << std::endl; tableFactory->rollback(*savePoint2); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("2b7be3797d97dcf7000000000000000000000000000000000000000000000000").hex()); #endif entry = table->getRow("name"); BOOST_REQUIRE_NE(entry->status(), Entry::DELETED); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("2b7be3797d97dcf7000000000000000000000000000000000000000000000000").hex()); #endif tableFactory->rollback(*savePoint1); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("d26dbc9a92ed28b1000000000000000000000000000000000000000000000000").hex()); #endif entry = table->getRow("name"); BOOST_REQUIRE(entry.has_value()); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("d26dbc9a92ed28b1000000000000000000000000000000000000000000000000").hex()); #endif entry = table->getRow("balance"); BOOST_REQUIRE(!entry); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("d26dbc9a92ed28b1000000000000000000000000000000000000000000000000").hex()); @@ -269,21 +270,21 @@ BOOST_AUTO_TEST_CASE(rollback) tableFactory->rollback(*savePoint); entry = table->getRow("name"); BOOST_REQUIRE(entry.has_value()); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("c18354d205471d61000000000000000000000000000000000000000000000000").hex()); #endif entry = table->getRow("balance"); BOOST_REQUIRE(!entry); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("c18354d205471d61000000000000000000000000000000000000000000000000").hex()); #endif entry = table->getRow("id"); BOOST_REQUIRE(!entry); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("c18354d205471d61000000000000000000000000000000000000000000000000").hex()); @@ -293,7 +294,7 @@ BOOST_AUTO_TEST_CASE(rollback) entry = table->newEntry(); entry->setField(0, "new record"); BOOST_REQUIRE_NO_THROW(table->setRow("id", *entry)); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("2c14904fc33bbbae000000000000000000000000000000000000000000000000").hex()); @@ -301,7 +302,7 @@ BOOST_AUTO_TEST_CASE(rollback) entry = table->newDeletedEntry(); BOOST_REQUIRE_NO_THROW(table->setRow("id", *entry)); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); // delete entry will cause hash mismatch #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), @@ -311,7 +312,7 @@ BOOST_AUTO_TEST_CASE(rollback) BOOST_AUTO_TEST_CASE(rollback2) { - auto hash0 = tableFactory->hash(hashImpl, false); + auto hash0 = tableFactory->hash(hashImpl, features); // auto savePoint0 = tableFactory->savepoint(); auto savePoint0 = std::make_shared(); tableFactory->setRecoder(savePoint0); @@ -322,7 +323,7 @@ BOOST_AUTO_TEST_CASE(rollback2) auto deleteEntry = table->newDeletedEntry(); table->setRow("name", deleteEntry); - auto hash = tableFactory->hash(hashImpl, false); + auto hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("ab98649ca506b076000000000000000000000000000000000000000000000001").hex()); @@ -331,14 +332,14 @@ BOOST_AUTO_TEST_CASE(rollback2) // entry->setField("key", "name"); entry->setField(0, "Lili"); table->setRow("name", *entry); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("c18354d205471d61000000000000000000000000000000000000000000000000").hex()); #endif entry = table->getRow("name"); BOOST_REQUIRE(entry.has_value()); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("c18354d205471d61000000000000000000000000000000000000000000000000").hex()); @@ -354,21 +355,21 @@ BOOST_AUTO_TEST_CASE(rollback2) // entry->setField("key", "id"); entry->setField(0, "12345"); table->setRow("id", *entry); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("d26dbc9a92ed28b1000000000000000000000000000000000000000000000000").hex()); #endif entry = table->getRow("id"); BOOST_REQUIRE(entry.has_value()); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("d26dbc9a92ed28b1000000000000000000000000000000000000000000000000").hex()); #endif entry = table->getRow("name"); BOOST_REQUIRE(entry.has_value()); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("d26dbc9a92ed28b1000000000000000000000000000000000000000000000000").hex()); @@ -379,21 +380,21 @@ BOOST_AUTO_TEST_CASE(rollback2) entry = table->getRow("name"); BOOST_REQUIRE(entry.has_value()); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("c18354d205471d61000000000000000000000000000000000000000000000000").hex()); #endif entry = table->getRow("balance"); BOOST_REQUIRE(!entry); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("c18354d205471d61000000000000000000000000000000000000000000000000").hex()); #endif entry = table->getRow("id"); BOOST_REQUIRE(!entry); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); #if defined(__APPLE__) BOOST_CHECK_EQUAL(hash.hex(), crypto::HashType("c18354d205471d61000000000000000000000000000000000000000000000000").hex()); @@ -401,12 +402,12 @@ BOOST_AUTO_TEST_CASE(rollback2) // BOOST_REQUIRE(table->dirty() == true); tableFactory->rollback(*savePoint0); - hash = tableFactory->hash(hashImpl, false); + hash = tableFactory->hash(hashImpl, features); BOOST_REQUIRE(hash.hex() == crypto::HashType("").hex()); entry = table->getRow("name"); BOOST_REQUIRE(!entry); - auto hash00 = tableFactory->hash(hashImpl, false); + auto hash00 = tableFactory->hash(hashImpl, features); BOOST_REQUIRE(hash00 == crypto::HashType(0)); BOOST_REQUIRE_EQUAL_COLLECTIONS(hash0.begin(), hash0.end(), hash00.begin(), hash00.end()); @@ -450,7 +451,7 @@ BOOST_AUTO_TEST_CASE(hash) auto entries = table->getRows(keys); BOOST_TEST(entries.size() == 2); - auto dbHash1 = tableFactory->hash(hashImpl, false); + auto dbHash1 = tableFactory->hash(hashImpl, features); auto savePoint = std::make_shared(); tableFactory->setRecoder(savePoint); @@ -468,7 +469,7 @@ BOOST_AUTO_TEST_CASE(hash) BOOST_TEST(!entry); // BOOST_TEST(table->dirty() == true); - auto dbHash2 = tableFactory->hash(hashImpl, false); + auto dbHash2 = tableFactory->hash(hashImpl, features); BOOST_CHECK_EQUAL(dbHash1.hex(), dbHash2.hex()); // getPrimaryKeys and getRows @@ -1133,7 +1134,7 @@ BOOST_AUTO_TEST_CASE(randomRWHash) } } - hashes.push_back(storage->hash(hashImpl, false)); + hashes.push_back(storage->hash(hashImpl, features)); storage->setReadOnly(false); prev = storage; } diff --git a/tests/perf/benchmark.cpp b/tests/perf/benchmark.cpp index c13cdfabf0..bc44ebdf1e 100644 --- a/tests/perf/benchmark.cpp +++ b/tests/perf/benchmark.cpp @@ -1,4 +1,5 @@ #include "bcos-crypto/hash/Keccak256.h" +#include "bcos-framework/ledger/Features.h" #include "bcos-framework/storage/StorageInterface.h" #include "bcos-storage/RocksDBStorage.h" #include "bcos-table/src/KeyPageStorage.h" @@ -172,10 +173,12 @@ int main(int argc, const char* argv[]) auto onlyWriteReadEnd = std::chrono::system_clock::now(); // commit and read auto hashImpl = std::make_shared(); + ledger::Features features; + features.set(ledger::Features::Flag::bugfix_keypage_system_entry_hash); for (int i = 0; i < storageChainLength && !onlyWrite; ++i) { auto s = storages[i]; - s->hash(hashImpl, true); + s->hash(hashImpl, features); TraverseStorageInterface::Ptr t = std::dynamic_pointer_cast(s); bcos::protocol::TwoPCParams p;