Skip to content

Commit

Permalink
Add readonly mode and allow_free_node
Browse files Browse the repository at this point in the history
  • Loading branch information
morebtcg committed Mar 29, 2024
1 parent 5627584 commit c47e40f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
28 changes: 26 additions & 2 deletions bcos-gateway/bcos-gateway/GatewayFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,10 @@ std::shared_ptr<Gateway> GatewayFactory::buildGateway(GatewayConfig::Ptr _config
{
gatewayNodeManager =
std::make_shared<GatewayNodeManager>(_config->uuid(), pubHex, keyFactory, service);
amop = buildLocalAMOP(service, pubHex);
if (!_config->readonly())
{
amop = buildLocalAMOP(service, pubHex);
}
}
else
{
Expand All @@ -697,9 +700,26 @@ std::shared_ptr<Gateway> GatewayFactory::buildGateway(GatewayConfig::Ptr _config
gatewayNodeManager = std::make_shared<ProGatewayNodeManager>(
_config->uuid(), pubHex, keyFactory, service);
}
amop = buildAMOP(service, pubHex);

if (!_config->readonly())
{
amop = buildAMOP(service, pubHex);
}
else
{
// register a null amop message handler
service->registerHandlerByMsgType(GatewayMessageType::AMOPMessageType,
[](const bcos::gateway::NetworkException& _e,
const bcos::gateway::P2PSession::Ptr& session,
const std::shared_ptr<bcos::gateway::P2PMessage>& message) {
// 只读模式下, 不处理其它节点的amop消息
// In read-only mode, AMOP messages from other nodes are not processed
return;
});
}
}


std::shared_ptr<ratelimiter::GatewayRateLimiter> gatewayRateLimiter;
if (_config->rateLimiterConfig().enable)
{
Expand All @@ -710,6 +730,10 @@ std::shared_ptr<Gateway> GatewayFactory::buildGateway(GatewayConfig::Ptr _config
// init Gateway
auto gateway = std::make_shared<Gateway>(
_config, service, gatewayNodeManager, amop, gatewayRateLimiter, _gatewayServiceName);
if (_config->readonly())
{
gateway->enableReadOnlyMode();
}
auto gatewayNodeManagerWeakPtr = std::weak_ptr<GatewayNodeManager>(gatewayNodeManager);
// register disconnect handler
service->registerDisconnectHandler(
Expand Down
1 change: 1 addition & 0 deletions bcos-gateway/bcos-gateway/libp2p/Service.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ class Service : public P2PInterface, public std::enable_shared_from_this<Service
m_beforeMessageHandler;

std::function<std::optional<bcos::Error>(SessionFace::Ptr, Message::Ptr)> m_onMessageHandler;
bcos::LogLevel m_connectionLogLevel = bcos::LogLevel::WARNING;
};

} // namespace gateway
Expand Down
3 changes: 2 additions & 1 deletion bcos-sync/bcos-sync/BlockSync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,8 @@ void BlockSync::onPeerStatus(NodeIDPtr _nodeID, BlockSyncMsgInterface::Ptr _sync
// receive peer not exist in the group
// Note: only should reject syncStatus from the node whose blockNumber falling behind of this
// node
if (!m_config->existsInGroup(_nodeID) && _syncMsg->number() <= m_config->blockNumber())
if (!m_allowFreeNode && !m_config->existsInGroup(_nodeID) &&
_syncMsg->number() <= m_config->blockNumber())
{
return;
}
Expand Down
1 change: 1 addition & 0 deletions bcos-tool/bcos-tool/NodeConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@ void NodeConfig::loadSecurityConfig(boost::property_tree::ptree const& _pt)
void NodeConfig::loadSealerConfig(boost::property_tree::ptree const& _pt)
{
m_minSealTime = checkAndGetValue(_pt, "consensus.min_seal_time", "500");
m_allowFreeNode = _pt.get<bool>("sync.allow_free_node", false);
if (m_minSealTime <= 0 || m_minSealTime > DEFAULT_MAX_SEAL_TIME_MS)
{
BOOST_THROW_EXCEPTION(InvalidConfig() << errinfo_comment(
Expand Down
2 changes: 2 additions & 0 deletions bcos-tool/bcos-tool/NodeConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class NodeConfig
std::string const& password() const { return m_password; }

size_t minSealTime() const { return m_minSealTime; }
bool allowFreeNodeSync() const { return m_allowFreeNode; }
size_t checkPointTimeoutInterval() const { return m_checkPointTimeoutInterval; }
size_t pipelineSize() const { return m_pipelineSize; }

Expand Down Expand Up @@ -329,6 +330,7 @@ class NodeConfig

// sealer configuration
size_t m_minSealTime = 0;
bool m_allowFreeNode = false;
size_t m_checkPointTimeoutInterval;
size_t m_pipelineSize = 50;

Expand Down

0 comments on commit c47e40f

Please sign in to comment.