diff --git a/bcos-executor/src/precompiled/SystemConfigPrecompiled.cpp b/bcos-executor/src/precompiled/SystemConfigPrecompiled.cpp index 57d6a4648e..83f8d1cf33 100644 --- a/bcos-executor/src/precompiled/SystemConfigPrecompiled.cpp +++ b/bcos-executor/src/precompiled/SystemConfigPrecompiled.cpp @@ -37,6 +37,8 @@ using namespace bcos::executor; using namespace bcos::ledger; using namespace bcos::protocol; +using namespace std::string_view_literals; + const char* const SYSCONFIG_METHOD_SET_STR = "setValueByKey(string,string)"; const char* const SYSCONFIG_METHOD_GET_STR = "getValueByKey(string)"; @@ -46,6 +48,7 @@ SystemConfigPrecompiled::SystemConfigPrecompiled() : Precompiled(GlobalHashImpl: getFuncSelector(SYSCONFIG_METHOD_SET_STR, GlobalHashImpl::g_hashImpl); name2Selector[SYSCONFIG_METHOD_GET_STR] = getFuncSelector(SYSCONFIG_METHOD_GET_STR, GlobalHashImpl::g_hashImpl); + auto defaultCmp = [](std::string_view _key, int64_t _value, int64_t _minValue) { if (_value < _minValue) { diff --git a/bcos-executor/src/precompiled/common/Utilities.cpp b/bcos-executor/src/precompiled/common/Utilities.cpp index b9225a05b6..672aedd9e1 100644 --- a/bcos-executor/src/precompiled/common/Utilities.cpp +++ b/bcos-executor/src/precompiled/common/Utilities.cpp @@ -231,9 +231,11 @@ uint32_t bcos::precompiled::getParamFunc(bytesConstRef _param) } uint32_t bcos::precompiled::getFuncSelectorByFunctionName( - std::string const& _functionName, const crypto::Hash::Ptr& _hashImpl) + std::string_view _functionName, const crypto::Hash::Ptr& _hashImpl) { - uint32_t func = *(uint32_t*)(_hashImpl->hash(_functionName).ref().getCroppedData(0, 4).data()); + auto hash = _hashImpl->hash( + bytesConstRef((const unsigned char*)_functionName.data(), _functionName.size())); + uint32_t func = *(uint32_t*)hash.ref().getCroppedData(0, 4).data(); uint32_t selector = ((func & 0x000000FF) << 24) | ((func & 0x0000FF00) << 8) | ((func & 0x00FF0000) >> 8) | ((func & 0xFF000000) >> 24); return selector; diff --git a/bcos-executor/src/precompiled/common/Utilities.h b/bcos-executor/src/precompiled/common/Utilities.h index c3fe21d297..81d4543f80 100644 --- a/bcos-executor/src/precompiled/common/Utilities.h +++ b/bcos-executor/src/precompiled/common/Utilities.h @@ -101,7 +101,7 @@ uint32_t getFuncSelector(std::string const& _functionName, void clearName2SelectCache(); uint32_t getParamFunc(bytesConstRef _param); uint32_t getFuncSelectorByFunctionName( - std::string const& _functionName, const crypto::Hash::Ptr& _hashImpl); + std::string_view _functionName, const crypto::Hash::Ptr& _hashImpl); bcos::precompiled::ContractStatus getContractStatus( std::shared_ptr _executive, diff --git a/bcos-framework/bcos-framework/multigroup/ChainNodeInfo.h b/bcos-framework/bcos-framework/multigroup/ChainNodeInfo.h index d810bb79c8..1672c055de 100644 --- a/bcos-framework/bcos-framework/multigroup/ChainNodeInfo.h +++ b/bcos-framework/bcos-framework/multigroup/ChainNodeInfo.h @@ -23,6 +23,7 @@ #include "bcos-framework/protocol/Protocol.h" #include "bcos-framework/protocol/ProtocolInfo.h" #include "bcos-framework/protocol/ServiceDesc.h" +#include "bcos-utilities/Ranges.h" #include #include namespace bcos @@ -48,7 +49,7 @@ class ChainNodeInfo m_nodeCryptoType = (NodeCryptoType)_type; } - virtual ~ChainNodeInfo() {} + virtual ~ChainNodeInfo() = default; virtual std::string const& nodeName() const { return m_nodeName; } virtual NodeCryptoType const& nodeCryptoType() const { return m_nodeCryptoType; } @@ -94,10 +95,7 @@ class ChainNodeInfo bcos::protocol::NodeType nodeType() const { return m_nodeType; } bcos::protocol::ProtocolInfo::ConstPtr nodeProtocol() const { return m_nodeProtocol; } - void setNodeProtocol(bcos::protocol::ProtocolInfo&& _protocol) - { - *m_nodeProtocol = std::move(_protocol); - } + void setNodeProtocol(bcos::protocol::ProtocolInfo&& _protocol) { *m_nodeProtocol = _protocol; } void setNodeProtocol(bcos::protocol::ProtocolInfo const& _protocol) { @@ -113,7 +111,19 @@ class ChainNodeInfo void setCompatibilityVersion(uint32_t _version) { m_compatibilityVersion = _version; } uint32_t compatibilityVersion() const { return m_compatibilityVersion; } -protected: + auto const& featureKeys() const { return m_featureKeys; } + void setFeatureKeys(RANGES::input_range auto&& featureKeys) + requires std::same_as>, + std::string> + { + m_featureKeys.clear(); + for (auto&& key : featureKeys) + { + m_featureKeys.emplace_back(std::forward(key)); + } + } + +private: bool m_microService = false; // the node name std::string m_nodeName; @@ -132,6 +142,8 @@ class ChainNodeInfo // the node protocol bcos::protocol::ProtocolInfo::Ptr m_nodeProtocol; + std::vector m_featureKeys; + // the system version uint32_t m_compatibilityVersion; diff --git a/bcos-framework/bcos-framework/multigroup/GroupInfo.h b/bcos-framework/bcos-framework/multigroup/GroupInfo.h index ee8dac7f93..69fdc6dddf 100644 --- a/bcos-framework/bcos-framework/multigroup/GroupInfo.h +++ b/bcos-framework/bcos-framework/multigroup/GroupInfo.h @@ -21,19 +21,18 @@ #pragma once #include "ChainNodeInfoFactory.h" #include "GroupTypeDef.h" -namespace bcos -{ -namespace group +#include +namespace bcos::group { class GroupInfo { public: using Ptr = std::shared_ptr; GroupInfo() = default; - GroupInfo(std::string const& _chainID, std::string const& _groupID) - : m_chainID(_chainID), m_groupID(_groupID) + GroupInfo(std::string _chainID, std::string _groupID) + : m_chainID(std::move(_chainID)), m_groupID(std::move(_groupID)) {} - virtual ~GroupInfo() {} + virtual ~GroupInfo() = default; virtual std::string const& genesisConfig() const { return m_genesisConfig; } virtual std::string const& iniConfig() const { return m_iniConfig; } @@ -60,7 +59,7 @@ class GroupInfo { UpgradableGuard l(x_nodeInfos); auto const& nodeName = _nodeInfo->nodeName(); - if (m_nodeInfos.count(nodeName)) + if (m_nodeInfos.contains(nodeName)) { return false; } @@ -73,7 +72,7 @@ class GroupInfo { WriteGuard l(x_nodeInfos); auto const& nodeName = _nodeInfo->nodeName(); - if (m_nodeInfos.count(nodeName)) + if (m_nodeInfos.contains(nodeName)) { *(m_nodeInfos[nodeName]) = *_nodeInfo; return; @@ -84,7 +83,7 @@ class GroupInfo virtual bool removeNodeInfo(std::string const& _nodeName) { UpgradableGuard l(x_nodeInfos); - if (!m_nodeInfos.count(_nodeName)) + if (!m_nodeInfos.contains(_nodeName)) { return false; } @@ -101,8 +100,19 @@ class GroupInfo return m_nodeInfos.size(); } - // return copied nodeInfos to ensure thread-safe - auto nodeInfos() { return m_nodeInfos; } + auto nodeInfos() const + { + ReadGuard l(x_nodeInfos); + return m_nodeInfos; + } + + // Use range with lock + auto nodeInfoList() const + { + return m_nodeInfos | + RANGES::views::transform([lock = std::make_shared(x_nodeInfos)]( + auto const& pair) -> auto& { return pair; }); + } bcos::group::ChainNodeInfoFactory::Ptr chainNodeInfoFactory() const { @@ -111,7 +121,7 @@ class GroupInfo void setChainNodeInfoFactory(bcos::group::ChainNodeInfoFactory::Ptr _chainNodeInfoFactory) { - m_chainNodeInfoFactory = _chainNodeInfoFactory; + m_chainNodeInfoFactory = std::move(_chainNodeInfoFactory); } bool wasm() const { return m_wasm; } @@ -119,7 +129,7 @@ class GroupInfo virtual void setWasm(bool _wasm) { m_wasm = _wasm; } virtual void setSmCryptoType(bool _smCryptoType) { m_smCryptoType = _smCryptoType; } -protected: +private: bool m_wasm{false}; bool m_smCryptoType{false}; @@ -136,7 +146,7 @@ class GroupInfo mutable SharedMutex x_nodeInfos; }; -inline std::string printGroupInfo(GroupInfo::Ptr _groupInfo) +inline std::string printGroupInfo(const GroupInfo::Ptr& _groupInfo) { if (!_groupInfo) { @@ -147,5 +157,4 @@ inline std::string printGroupInfo(GroupInfo::Ptr _groupInfo) << LOG_KV("nodeSize", _groupInfo->nodesNum()); return oss.str(); } -} // namespace group -} // namespace bcos \ No newline at end of file +} // namespace bcos::group \ No newline at end of file diff --git a/bcos-rpc/bcos-rpc/Rpc.cpp b/bcos-rpc/bcos-rpc/Rpc.cpp index cedb59adf2..c18e7bc8c5 100644 --- a/bcos-rpc/bcos-rpc/Rpc.cpp +++ b/bcos-rpc/bcos-rpc/Rpc.cpp @@ -156,7 +156,7 @@ void Rpc::notifyGroupInfo(bcos::group::GroupInfo::Ptr _groupInfo) continue; } Json::Value groupInfoJson; - groupInfoToJson(groupInfoJson, _groupInfo); + groupInfoToJson(groupInfoJson, *_groupInfo); auto response = groupInfoJson.toStyledString(); auto message = m_wsService->messageFactory()->buildMessage(); message->setPacketType(bcos::protocol::MessageType::GROUP_NOTIFY); diff --git a/bcos-rpc/bcos-rpc/groupmgr/GroupManager.h b/bcos-rpc/bcos-rpc/groupmgr/GroupManager.h index e8c8b73632..db9fefd005 100644 --- a/bcos-rpc/bcos-rpc/groupmgr/GroupManager.h +++ b/bcos-rpc/bcos-rpc/groupmgr/GroupManager.h @@ -103,7 +103,7 @@ class GroupManager : public std::enable_shared_from_this bcos::protocol::BlockNumber _blockNumber) { UpgradableGuard l(x_groupBlockInfos); - if (m_groupBlockInfos.count(_groupID)) + if (m_groupBlockInfos.contains(_groupID)) { // expired block if (m_groupBlockInfos[_groupID] > _blockNumber) @@ -112,23 +112,23 @@ class GroupManager : public std::enable_shared_from_this } // has already in the m_nodesWithLatestBlockNumber if (m_groupBlockInfos[_groupID] == _blockNumber && - m_nodesWithLatestBlockNumber.count(_groupID) && - m_nodesWithLatestBlockNumber[_groupID].count(_nodeName)) + m_nodesWithLatestBlockNumber.contains(_groupID) && + m_nodesWithLatestBlockNumber[_groupID].contains(_nodeName)) { return; } } UpgradeGuard ul(l); bcos::protocol::BlockNumber oldBlockNumber = 0; - if (m_groupBlockInfos.count(_groupID)) + if (m_groupBlockInfos.contains(_groupID)) { oldBlockNumber = m_groupBlockInfos[_groupID]; } - if (!m_nodesWithLatestBlockNumber.count(_groupID)) + if (!m_nodesWithLatestBlockNumber.contains(_groupID)) { m_nodesWithLatestBlockNumber[_groupID] = std::set(); } - if (!m_groupBlockInfos.count(_groupID)) + if (!m_groupBlockInfos.contains(_groupID)) { m_groupBlockInfos[_groupID] = _blockNumber; } @@ -156,12 +156,12 @@ class GroupManager : public std::enable_shared_from_this std::function _blockNumberNotifier) { - m_blockNumberNotifier = _blockNumberNotifier; + m_blockNumberNotifier = std::move(_blockNumberNotifier); } virtual bcos::protocol::BlockNumber getBlockNumberByGroup(const std::string& _groupID); protected: - GroupManager(std::string const& _chainID) : m_chainID(_chainID) {} + GroupManager(std::string _chainID) : m_chainID(std::move(_chainID)) {} bool updateGroupServices(bcos::group::GroupInfo::Ptr _groupInfo, bool _enforceUpdate); bool updateNodeService(std::string const& _groupID, bcos::group::ChainNodeInfo::Ptr _nodeInfo, diff --git a/bcos-rpc/bcos-rpc/jsonrpc/Common.h b/bcos-rpc/bcos-rpc/jsonrpc/Common.h index 219e402fbd..99c1c76494 100644 --- a/bcos-rpc/bcos-rpc/jsonrpc/Common.h +++ b/bcos-rpc/bcos-rpc/jsonrpc/Common.h @@ -26,31 +26,13 @@ #define RPC_IMPL_LOG(LEVEL) BCOS_LOG(LEVEL) << "[RPC][JSONRPC]" -namespace bcos +namespace bcos::rpc { -namespace rpc -{ -struct NodeInfo -{ - std::string version; - std::string supportedVersion; - std::string nodeID; - std::string chainID; - std::string groupID; - std::string agency; - std::string buildTime; - std::string gitCommitHash; - bool isWasm; - bool isSM; -}; - class JsonRpcException : public std::exception { public: JsonRpcException(int32_t _code, std::string const& _msg) : m_code(_code), m_msg(_msg) {} const char* what() const noexcept override { return m_msg.c_str(); } - -public: int32_t code() const noexcept { return m_code; } std::string msg() const noexcept { return m_msg; } @@ -132,18 +114,24 @@ inline void nodeInfoToJson(Json::Value& _response, bcos::group::ChainNodeInfo::P protocolResponse["minSupportedVersion"] = protocol->minVersion(); protocolResponse["maxSupportedVersion"] = protocol->maxVersion(); protocolResponse["compatibilityVersion"] = _nodeInfo->compatibilityVersion(); + + auto featureKeys = Json::Value(Json::arrayValue); + for (auto const& key : _nodeInfo->featureKeys()) + { + featureKeys.append(key); + } + _response["featureKeys"] = std::move(featureKeys); _response["protocol"] = protocolResponse; } -inline void groupInfoToJson(Json::Value& _response, bcos::group::GroupInfo::Ptr _groupInfo) +inline void groupInfoToJson(Json::Value& _response, bcos::group::GroupInfo const& _groupInfo) { - _response["chainID"] = _groupInfo->chainID(); - _response["groupID"] = _groupInfo->groupID(); - _response["genesisConfig"] = _groupInfo->genesisConfig(); - _response["iniConfig"] = _groupInfo->iniConfig(); + _response["chainID"] = _groupInfo.chainID(); + _response["groupID"] = _groupInfo.groupID(); + _response["genesisConfig"] = _groupInfo.genesisConfig(); + _response["iniConfig"] = _groupInfo.iniConfig(); _response["nodeList"] = Json::Value(Json::arrayValue); - auto nodeInfos = _groupInfo->nodeInfos(); - for (auto const& it : nodeInfos) + for (auto const& it : _groupInfo.nodeInfoList()) { Json::Value nodeInfoResponse; nodeInfoToJson(nodeInfoResponse, it.second); @@ -157,9 +145,8 @@ inline void groupInfoListToJson( for (const auto& groupInfo : _groupInfoList) { Json::Value item; - groupInfoToJson(item, groupInfo); + groupInfoToJson(item, *groupInfo); _response.append(item); } } -} // namespace rpc -} // namespace bcos \ No newline at end of file +} // namespace bcos::rpc \ No newline at end of file diff --git a/bcos-rpc/bcos-rpc/jsonrpc/JsonRpcImpl_2_0.cpp b/bcos-rpc/bcos-rpc/jsonrpc/JsonRpcImpl_2_0.cpp index e67968e4c2..48613a8038 100644 --- a/bcos-rpc/bcos-rpc/jsonrpc/JsonRpcImpl_2_0.cpp +++ b/bcos-rpc/bcos-rpc/jsonrpc/JsonRpcImpl_2_0.cpp @@ -1204,7 +1204,7 @@ void JsonRpcImpl_2_0::getGroupInfo(std::string_view _groupID, RespFunc _respFunc if (groupInfo) { // can only recover the deleted group - groupInfoToJson(response, groupInfo); + groupInfoToJson(response, *groupInfo); } _respFunc(nullptr, response); } diff --git a/bcos-rpc/bcos-rpc/jsonrpc/JsonRpcImpl_2_0.h b/bcos-rpc/bcos-rpc/jsonrpc/JsonRpcImpl_2_0.h index b959c48fb2..5e196cb068 100644 --- a/bcos-rpc/bcos-rpc/jsonrpc/JsonRpcImpl_2_0.h +++ b/bcos-rpc/bcos-rpc/jsonrpc/JsonRpcImpl_2_0.h @@ -113,8 +113,6 @@ class JsonRpcImpl_2_0 : public JsonRpcInterface, void getGroupBlockNumber(RespFunc _respFunc) override; - void setNodeInfo(const NodeInfo& _nodeInfo) { m_nodeInfo = _nodeInfo; } - NodeInfo nodeInfo() const { return m_nodeInfo; } GroupManager::Ptr groupManager() { return m_groupManager; } int sendTxTimeout() const { return m_sendTxTimeout; } @@ -161,7 +159,6 @@ class JsonRpcImpl_2_0 : public JsonRpcInterface, bcos::gateway::GatewayInterface::Ptr m_gatewayInterface; std::shared_ptr m_wsService; - NodeInfo m_nodeInfo; // Note: here clientID must non-empty for the rpc will set clientID as source for the tx for // tx-notify and the scheduler will not notify the tx-result if the tx source is empty std::string m_clientID = "localRpc"; diff --git a/libinitializer/PBFTInitializer.cpp b/libinitializer/PBFTInitializer.cpp index 0554ddb114..56f8153d69 100644 --- a/libinitializer/PBFTInitializer.cpp +++ b/libinitializer/PBFTInitializer.cpp @@ -19,6 +19,7 @@ * @date 2021-06-10 */ #include "PBFTInitializer.h" +#include "bcos-framework/ledger/Features.h" #include #include #include @@ -169,6 +170,9 @@ void PBFTInitializer::initChainNodeInfo( auto nodeProtocolInfo = g_BCOSConfig.protocolInfo(ProtocolModuleID::NodeService); m_nodeInfo->setNodeProtocol(*nodeProtocolInfo); m_nodeInfo->setCompatibilityVersion(m_pbft->compatibilityVersion()); + m_nodeInfo->setFeatureKeys( + ledger::Features::featureKeys() | + RANGES::views::transform([](std::string_view view) { return std::string(view); })); m_groupInfo->appendNodeInfo(m_nodeInfo); INITIALIZER_LOG(INFO) << LOG_DESC("PBFTInitializer::initChainNodeInfo") << LOG_KV("nodeType", m_nodeInfo->nodeType())