Skip to content

Commit

Permalink
Remove ledger config fetcher (FISCO-BCOS#4593)
Browse files Browse the repository at this point in the history
  • Loading branch information
morebtcg authored Aug 26, 2024
1 parent 19f112a commit 97d00aa
Show file tree
Hide file tree
Showing 58 changed files with 187 additions and 779 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ if(FULLNODE)
include(ProjectGroupSig)
include(ProjectPaillier)
include(ProjectBLST)
include(ProjectTOMLPP)

add_subdirectory(bcos-sealer)
add_subdirectory(bcos-security)
Expand Down
2 changes: 2 additions & 0 deletions bcos-crypto/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ find_package(ZLIB REQUIRED)
add_executable(${TEST_BINARY_NAME} ${SOURCES})
target_include_directories(${TEST_BINARY_NAME} PRIVATE ..)
target_link_libraries(${TEST_BINARY_NAME} Boost::unit_test_framework bcos-utilities bcos-crypto ZLIB::ZLIB)
set_source_files_properties("unittests/main.cpp" "unittests/testMerkle.cpp" PROPERTIES SKIP_UNITY_BUILD_INCLUSION ON)
set_target_properties(${TEST_BINARY_NAME} PROPERTIES UNITY_BUILD "ON")
add_test(NAME test-crypto WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} COMMAND ${TEST_BINARY_NAME})
2 changes: 0 additions & 2 deletions bcos-crypto/test/unittests/testMerkle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#include <boost/iostreams/stream.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/throw_exception.hpp>
#include <chrono>
#include <future>
#include <iterator>
#include <ostream>
#include <random>
Expand Down
48 changes: 34 additions & 14 deletions bcos-executor/src/executive/LedgerCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,35 @@

#include "../Common.h"
#include "bcos-framework/ledger/LedgerInterface.h"
#include "bcos-framework/ledger/LedgerMethods.h"
#include <bcos-framework/ledger/Ledger.h>
#include <bcos-task/Wait.h>
#include <bcos-tool/LedgerConfigFetcher.h>
#include <future>
#include <optional>

namespace bcos::executor
{
class LedgerCache : public bcos::tool::LedgerConfigFetcher
class LedgerCache
{
private:
ledger::LedgerInterface::Ptr m_ledger;
ledger::LedgerConfig::Ptr m_ledgerConfig;
std::optional<evmc_uint256be> m_chainID;

std::map<int64_t, h256, std::less<>> m_blockNumber2Hash;
mutable bcos::SharedMutex x_blockNumber2Hash;

public:
using Ptr = std::shared_ptr<LedgerCache>;
LedgerCache(bcos::ledger::LedgerInterface::Ptr ledger) : bcos::tool::LedgerConfigFetcher(ledger)
LedgerCache(bcos::ledger::LedgerInterface::Ptr ledger)
: m_ledger(std::move(ledger)), m_ledgerConfig(std::make_shared<ledger::LedgerConfig>())
{}
virtual ~LedgerCache() = default;

const ledger::LedgerConfig& ledgerConfig() const { return *m_ledgerConfig; }
void updateLedgerConfig()
{
task::syncWait(ledger::getLedgerConfig(*m_ledger, *m_ledgerConfig));
}

void setBlockNumber2Hash(int64_t blockNumber, h256 blockHash)
{
Expand All @@ -52,7 +67,7 @@ class LedgerCache : public bcos::tool::LedgerConfigFetcher
[blockNumber](const auto& item) { return item.first < blockNumber; });
}

bcos::crypto::HashType fetchBlockHash(bcos::protocol::BlockNumber _blockNumber) override
bcos::crypto::HashType fetchBlockHash(bcos::protocol::BlockNumber _blockNumber)
{
EXECUTOR_LOG(TRACE) << LOG_BADGE("LedgerCache") << "fetchBlockHash start"
<< LOG_KV("blockNumber", _blockNumber);
Expand All @@ -68,7 +83,7 @@ class LedgerCache : public bcos::tool::LedgerConfigFetcher
}
}

auto hash = bcos::tool::LedgerConfigFetcher::fetchBlockHash(_blockNumber);
auto hash = task::syncWait(ledger::getBlockHash(*m_ledger, _blockNumber));
EXECUTOR_LOG(TRACE) << LOG_BADGE("LedgerCache")
<< "fetchBlockHash return by fetching from ledger"
<< LOG_KV("blockNumber", _blockNumber)
Expand Down Expand Up @@ -106,16 +121,21 @@ class LedgerCache : public bcos::tool::LedgerConfigFetcher

evmc_uint256be chainId()
{
if (ledgerConfig()->chainId().has_value())
if (m_chainID)
{
return ledgerConfig()->chainId().value();
return *m_chainID;
}
fetchChainId();
return ledgerConfig()->chainId().value();
}

private:
std::map<int64_t, h256, std::less<>> m_blockNumber2Hash;
mutable bcos::SharedMutex x_blockNumber2Hash;
if (auto value = task::syncWait(
ledger::getSystemConfig(*m_ledger, ledger::SYSTEM_KEY_WEB3_CHAIN_ID)))
{
auto numChainID = boost::lexical_cast<u256>(std::get<0>(*value));
m_chainID.emplace(bcos::toEvmC(numChainID));
EXECUTOR_LOG(INFO) << LOG_DESC("fetchChainId success") << LOG_KV("chainId", numChainID);
return *m_chainID;
}

return {};
}
};
} // namespace bcos::executor
26 changes: 12 additions & 14 deletions bcos-executor/src/executor/TransactionExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,23 +150,22 @@ TransactionExecutor::TransactionExecutor(bcos::ledger::LedgerInterface::Ptr ledg
m_vmFactory(std::move(vmFactory))
{
assert(m_backendStorage);
m_ledgerCache->fetchCompatibilityVersion();
m_ledgerCache->fetchBlockNumberAndHash();

m_ledgerCache->updateLedgerConfig();
GlobalHashImpl::g_hashImpl = m_hashImpl;
m_abiCache = make_shared<ClockCache<bcos::bytes, FunctionAbi>>(32);
#ifdef WITH_WASM
m_gasInjector = std::make_shared<wasm::GasInjector>(wasm::GetInstructionTable());
#endif

m_threadPool = std::make_shared<bcos::ThreadPool>(name, std::thread::hardware_concurrency());
setBlockVersion(m_ledgerCache->ledgerConfig()->compatibilityVersion());
if (m_ledgerCache->ledgerConfig()->compatibilityVersion() >= BlockVersion::V3_3_VERSION)
setBlockVersion(m_ledgerCache->ledgerConfig().compatibilityVersion());
if (m_ledgerCache->ledgerConfig().compatibilityVersion() >= BlockVersion::V3_3_VERSION)
{
m_ledgerCache->fetchAuthCheckStatus();
if (m_ledgerCache->ledgerConfig()->authCheckStatus() != UINT32_MAX)
if (m_ledgerCache->ledgerConfig().authCheckStatus() != UINT32_MAX)
{
// cannot get auth check status, use config value
m_isAuthCheck = !m_isWasm && m_ledgerCache->ledgerConfig()->authCheckStatus() != 0;
m_isAuthCheck = !m_isWasm && m_ledgerCache->ledgerConfig().authCheckStatus() != 0;
}
}
if (m_isWasm)
Expand Down Expand Up @@ -311,7 +310,7 @@ void TransactionExecutor::initEvmEnvironment()
set<string> builtIn = {CRYPTO_ADDRESS, GROUP_SIG_ADDRESS, RING_SIG_ADDRESS, CAST_ADDRESS};
m_staticPrecompiled = std::make_shared<set<string>>(builtIn);
if (m_blockVersion <=> BlockVersion::V3_1_VERSION == 0 &&
m_ledgerCache->ledgerConfig()->blockNumber() > 0)
m_ledgerCache->ledgerConfig().blockNumber() > 0)
{
// Only 3.1 goes here, here is a bug, ignore init test precompiled
}
Expand Down Expand Up @@ -374,7 +373,7 @@ void TransactionExecutor::initWasmEnvironment()
m_staticPrecompiled = std::make_shared<set<string>>(builtIn);

if (m_blockVersion <=> BlockVersion::V3_1_VERSION == 0 &&
m_ledgerCache->ledgerConfig()->blockNumber() > 0)
m_ledgerCache->ledgerConfig().blockNumber() > 0)
{
// Only 3.1 goes here, here is a bug, ignore init test precompiled
}
Expand Down Expand Up @@ -1885,16 +1884,15 @@ void TransactionExecutor::commit(
EXECUTOR_NAME_LOG(DEBUG) << BLOCK_NUMBER(blockNumber) << "Commit success";

m_lastCommittedBlockNumber = blockNumber;
m_ledgerCache->fetchCompatibilityVersion();
auto version = m_ledgerCache->ledgerConfig()->compatibilityVersion();
m_ledgerCache->updateLedgerConfig();
auto version = m_ledgerCache->ledgerConfig().compatibilityVersion();
setBlockVersion(version);
if (version >= BlockVersion::V3_3_VERSION)
{
m_ledgerCache->fetchAuthCheckStatus();
if (m_ledgerCache->ledgerConfig()->authCheckStatus() != UINT32_MAX)
if (m_ledgerCache->ledgerConfig().authCheckStatus() != UINT32_MAX)
{
// cannot get auth check status, not update value
m_isAuthCheck = !m_isWasm && m_ledgerCache->ledgerConfig()->authCheckStatus() != 0;
m_isAuthCheck = !m_isWasm && m_ledgerCache->ledgerConfig().authCheckStatus() != 0;
}
}
removeCommittedState();
Expand Down
4 changes: 2 additions & 2 deletions bcos-executor/src/executor/TransactionExecutorFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
#include "ShardingTransactionExecutor.h"
#include "TransactionExecutor.h"
#include "bcos-framework/storage/StorageInterface.h"
#include "bcos-ledger/src/libledger/utilities/Common.h"
#include <bcos-table/src/CacheStorageFactory.h>
#include <bcos-table/src/StateStorageFactory.h>
#include <utility>

namespace bcos::executor
{
Expand Down Expand Up @@ -59,7 +59,7 @@ class TransactionExecutorFactory
m_ledger(std::move(ledger)),
m_txpool(std::move(txpool)),
m_cacheFactory(std::move(cacheFactory)),
m_stateStorageFactory(stateStorageFactory),
m_stateStorageFactory(std::move(stateStorageFactory)),
m_storage(std::move(storage)),
m_executionMessageFactory(std::move(executionMessageFactory)),
m_hashImpl(std::move(hashImpl)),
Expand Down
1 change: 0 additions & 1 deletion bcos-executor/src/precompiled/SystemConfigPrecompiled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include <bcos-framework/ledger/LedgerTypeDef.h>
#include <bcos-framework/protocol/GlobalConfig.h>
#include <bcos-framework/protocol/Protocol.h>
#include <bcos-ledger/src/libledger/utilities/Common.h>
#include <bcos-tool/VersionConverter.h>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
Expand Down
5 changes: 3 additions & 2 deletions bcos-executor/test/unittest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ find_package(Boost REQUIRED unit_test_framework)
find_package(TBB CONFIG REQUIRED)
include(SearchTestCases)

target_link_libraries(${TEST_BINARY_NAME} ${EXECUTOR_TARGET} ${CRYPTO_TARGET} ${TARS_PROTOCOL_TARGET} bcos-framework Boost::serialization Boost::unit_test_framework)
target_link_libraries(${TEST_BINARY_NAME} ${EXECUTOR_TARGET} ${LEDGER_TARGET} ${CRYPTO_TARGET} ${TARS_PROTOCOL_TARGET} bcos-framework Boost::serialization Boost::unit_test_framework)
target_compile_options(${TEST_BINARY_NAME} PRIVATE -fno-var-tracking -Wno-unused-parameter)
set_source_files_properties("main.cpp" PROPERTIES SKIP_UNITY_BUILD_INCLUSION ON)
set_target_properties(${TEST_BINARY_NAME} PROPERTIES UNITY_BUILD "ON")
#add_test(NAME test-executor WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} COMMAND $<TARGET_FILE:${TEST_BINARY_NAME}>)
config_test_cases("" "${SOURCES}" ${TEST_BINARY_NAME} "")


# for code coverage
#if (COVERAGE)
# include(Coverage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,6 @@ using namespace bcos::ledger;
using namespace bcos::crypto;
using namespace bcos::codec;

#ifndef WITH_WASM
namespace bcos::wasm
{
class GasInjector
{
};
} // namespace bcos::wasm
#endif

namespace bcos::test
{
class CryptoPrecompiledFixture : public PrecompiledFixture
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,6 @@ using namespace bcos::precompiled;
using namespace bcos::executor;
using namespace bcos::storage;

#ifndef WITH_WASM
namespace bcos::wasm
{
class GasInjector
{
};
} // namespace bcos::wasm
#endif

namespace bcos::test
{
struct GroupSigPrecompiledFixture
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

#pragma once
#include "bcos-crypto/interfaces/crypto/CommonType.h"
#include "executive/BlockContext.h"
#include "executive/TransactionExecutive.h"
#include "executor/TransactionExecutorFactory.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,6 @@ using namespace bcos::precompiled;
using namespace bcos::executor;
using namespace bcos::storage;

#ifndef WITH_WASM
namespace bcos::wasm
{
class GasInjector
{
};
} // namespace bcos::wasm
#endif

namespace bcos::test
{
struct RingSigPrecompiledFixture
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,6 @@ using namespace bcos::precompiled;
using namespace bcos::executor;
using namespace bcos::storage;

#ifndef WITH_WASM
namespace bcos::wasm
{
class GasInjector
{
};
} // namespace bcos::wasm
#endif

namespace bcos::test
{
class VRFPrecompiledFixture
Expand Down
14 changes: 11 additions & 3 deletions bcos-executor/test/unittest/mock/MockLedger.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
#include <future>
#include <sstream>

#ifndef WITH_WASM
namespace bcos::wasm
{
class GasInjector
{
};
} // namespace bcos::wasm
#endif

namespace bcos::test
{
class MockLedger : public bcos::ledger::LedgerInterface
Expand Down Expand Up @@ -157,15 +166,14 @@ class MockLedger : public bcos::ledger::LedgerInterface
return;
}


BOOST_CHECK(false); // Need implementations
_onGetConfig(nullptr, "0", 0);
};


void asyncGetNodeListByType(std::string_view const& _type,
std::function<void(Error::Ptr, consensus::ConsensusNodeListPtr)> _onGetConfig) override
{
BOOST_CHECK(false); // Need implementations
_onGetConfig(nullptr, std::make_shared<consensus::ConsensusNodeList>());
};

void asyncGetNonceList(protocol::BlockNumber _startNumber, int64_t _offset,
Expand Down
2 changes: 1 addition & 1 deletion bcos-executor/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

add_executable(injector inject_meter.cpp)
target_include_directories(injector PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../src/)
target_link_libraries(injector PUBLIC ${EXECUTOR_TARGET} ${TOOL_TARGET} wabt)
target_link_libraries(injector PUBLIC ${EXECUTOR_TARGET} ${LEDGER_TARGET} ${TOOL_TARGET} wabt)
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class NativeExecutionMessage : public protocol::ExecutionMessage
Type type() const override { return m_type; }
void setType(Type type) override { m_type = type; }

crypto::HashType transactionHash() const override { return m_transactionHash; }
void setTransactionHash(crypto::HashType hash) override { m_transactionHash = hash; }
bcos::crypto::HashType transactionHash() const override { return m_transactionHash; }
void setTransactionHash(bcos::crypto::HashType hash) override { m_transactionHash = hash; }

int64_t contextID() const override { return m_contextID; }
void setContextID(int64_t contextID) override { m_contextID = contextID; }
Expand Down
25 changes: 25 additions & 0 deletions bcos-framework/bcos-framework/ledger/LedgerTypeDef.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "../protocol/ProtocolTypeDef.h"
#include "SystemConfigs.h"
#include <bcos-utilities/Common.h>
#include <oneapi/tbb/concurrent_unordered_map.h>

namespace bcos::ledger
{
Expand Down Expand Up @@ -123,4 +124,28 @@ struct CurrentState
int64_t totalTransactionCount;
int64_t totalFailedTransactionCount;
};

// parent=>children
using Parent2ChildListMap = std::map<std::string, std::vector<std::string>>;
// child=>parent
using Child2ParentMap = tbb::concurrent_unordered_map<std::string, std::string>;

constexpr static const char* const SYS_VALUE = "value";
constexpr static const char* const SYS_CONFIG_ENABLE_BLOCK_NUMBER = "enable_number";
constexpr static const char* const SYS_VALUE_AND_ENABLE_BLOCK_NUMBER = "value,enable_number";

enum LedgerError : int32_t
{
SUCCESS = 0,
OpenTableFailed = 3001,
CallbackError = 3002,
ErrorArgument = 3003,
DecodeError = 3004,
ErrorCommitBlock = 3005,
CollectAsyncCallbackError = 3006,
LedgerLockError = 3007,
GetStorageError = 3008,
EmptyEntry = 3009,
UnknownError = 3010,
};
} // namespace bcos::ledger
2 changes: 2 additions & 0 deletions bcos-framework/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ add_executable(${TEST_BINARY_NAME} ${SOURCES})
find_package(Boost REQUIRED serialization unit_test_framework)

target_link_libraries(${TEST_BINARY_NAME} PRIVATE ${UTILITIES_TARGET} bcos-framework Boost::serialization Boost::unit_test_framework)
set_source_files_properties("unittests/main/main.cpp" PROPERTIES SKIP_UNITY_BUILD_INCLUSION ON)
set_target_properties(${TEST_BINARY_NAME} PROPERTIES UNITY_BUILD "ON")
add_test(NAME test-framework WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} COMMAND ${TEST_BINARY_NAME})
Loading

0 comments on commit 97d00aa

Please sign in to comment.