Skip to content

Commit

Permalink
fix lightnode upgrade reSync bug and add try catch for no node availa…
Browse files Browse the repository at this point in the history
  • Loading branch information
wenlinlee authored Jan 15, 2024
1 parent d7b1193 commit 9913615
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 23 deletions.
17 changes: 17 additions & 0 deletions bcos-ledger/src/libledger/LedgerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,23 @@ class LedgerImpl : public bcos::concepts::ledger::LedgerBase<LedgerImpl<Hasher,
co_await impl_setBlock<concepts::ledger::HEADER>(std::move(block));
}

task::Task<void> impl_checkGenesisBlock(bcos::concepts::block::Block auto block)
{
try
{
decltype(block) currentBlock;

co_await impl_getBlock<concepts::ledger::HEADER>(0, currentBlock);
co_return;
}
catch (NotFoundBlockHeader& e)
{
LEDGER_LOG(INFO) << "Not found genesis block, may be not initialized";
BOOST_THROW_EXCEPTION(
NotFoundBlockHeader{} << bcos::error::ErrorMessage{"Not found genesis block!"});
}
}

auto& storage() { return bcos::concepts::getRef(m_storage); }

Hasher m_hasher;
Expand Down
4 changes: 4 additions & 0 deletions concepts/bcos-concepts/ledger/Ledger.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ class LedgerBase
{
return impl().template impl_setupGenesisBlock(std::move(block));
}
auto checkGenesisBlock(bcos::concepts::block::Block auto block)
{
return impl().template impl_checkGenesisBlock(std::move(block));
}

private:
friend Impl;
Expand Down
11 changes: 9 additions & 2 deletions lightnode/bcos-lightnode/rpc/LightNodeRPC.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,15 @@ class LightNodeRPC : public bcos::rpc::JsonRpcInterface
}
else
{
co_await self->remoteLedger().template getBlock<bcos::concepts::ledger::ALL>(
blockNumber, block);
try
{
co_await self->remoteLedger()
.template getBlock<bcos::concepts::ledger::ALL>(blockNumber, block);
}
catch (const std::exception& e)
{
BOOST_THROW_EXCEPTION(std::runtime_error{e.what()});
}
if (RANGES::empty(block.transactionsMetaData) && blockNumber != 0)
{
LIGHTNODE_LOG(ERROR)
Expand Down
20 changes: 15 additions & 5 deletions lightnode/fisco-bcos-lightnode/client/LedgerClientImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,27 @@ class LedgerClientImpl : public bcos::concepts::ledger::LedgerBase<LedgerClientI
(processGetBlockFlags<Flags>(request.onlyHeader), ...);

bcostars::ResponseBlock response;
auto nodeIDs = co_await p2p().getAllNodeID();
bcos::crypto::NodeIDs nodeIDs;
try
{
nodeIDs = co_await p2p().getAllNodeID();
}
catch (std::exception const& e)
{
LIGHTNODE_LOG(ERROR) << "lightNode getAllNodeID failed, error: " << e.what();
response.block = {};
std::swap(response.block, block);
}
size_t failedNodeCount = 0;
for(auto& nodeID : nodeIDs)
for (auto& nodeID : nodeIDs)
{
co_await p2p().sendMessageByNodeID(
bcos::protocol::LIGHTNODE_GET_BLOCK, nodeID, request, response);
if(response.error.errorCode)
if (response.error.errorCode)
{
LIGHTNODE_LOG(WARNING) << "getBlock failed, request nodeID: " << nodeID->hex()
<< "response errorCode: " << response.error.errorCode
<< " " << response.error.errorMessage;
<< "response errorCode: " << response.error.errorCode << " "
<< response.error.errorMessage;
continue;
}
else
Expand Down
10 changes: 8 additions & 2 deletions lightnode/fisco-bcos-lightnode/client/P2PClientImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,11 @@ class P2PClientImpl

if (nodeID.empty())
{
BOOST_THROW_EXCEPTION(NoNodeAvailable{});
LIGHTNODE_LOG(INFO) << LOG_DESC(
"randomSelectNode failed, nodeID is empty, no node available");
BOOST_THROW_EXCEPTION(
NoNodeAvailable{} << bcos::error::ErrorMessage{
"no node available, please check the node and network status"});
}

bcos::bytes nodeIDBin;
Expand Down Expand Up @@ -282,7 +286,9 @@ class P2PClientImpl
}
if (nodeIDs.empty())
{
BOOST_THROW_EXCEPTION(NoNodeAvailable{});
BOOST_THROW_EXCEPTION(
NoNodeAvailable{} << bcos::error::ErrorMessage{
"no node available, please check the node and network status"});
}
co_return nodeIDs;
}
Expand Down
43 changes: 29 additions & 14 deletions lightnode/fisco-bcos-lightnode/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ static auto startSyncerThread(bcos::concepts::ledger::Ledger auto fromLedger,
}


void starLightnode(bcos::tool::NodeConfig::Ptr nodeConfig, auto ledger, auto front, auto gateway,
auto keyFactory, auto nodeID)
void starLightnode(bcos::tool::NodeConfig::Ptr nodeConfig, auto ledger, auto nodeLedger, auto front,
auto gateway, auto keyFactory, auto nodeID)
{
LIGHTNODE_LOG(INFO) << "Init lightnode p2p client...";
auto p2pClient = std::make_shared<bcos::p2p::P2PClientImpl>(
Expand All @@ -144,6 +144,20 @@ void starLightnode(bcos::tool::NodeConfig::Ptr nodeConfig, auto ledger, auto fro
std::make_shared<bcos::transaction_pool::TransactionPoolClientImpl>(p2pClient);
auto scheduler = std::make_shared<bcos::scheduler::SchedulerClientImpl>(p2pClient);

// check genesisBlock exists
bcostars::Block genesisBlock;
try
{
bcos::task::syncWait(ledger->checkGenesisBlock(std::move(genesisBlock)));
}
catch (const std::exception& e)
{
LIGHTNODE_LOG(INFO) << "get genesis block failed, genesisBlock maybe not exist, prepare "
"buildGenesisBlock, error:"
<< boost::diagnostic_information(e);
nodeLedger->buildGenesisBlock(nodeConfig->genesisConfig(), *nodeConfig->ledgerConfig());
}


LIGHTNODE_LOG(INFO) << "Init lightnode rpc...";
auto wsService = bcos::lightnode::initRPC(
Expand Down Expand Up @@ -231,34 +245,35 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] const char* argv[])
// local ledger
auto storage = newStorage(nodeConfig->storagePath());
bcos::storage::StorageImpl storageWrapper(storage);

std::shared_ptr<bcos::ledger::Ledger> nodeLedger;
if (nodeConfig->smCryptoType())
{
auto localLedger = std::make_shared<bcos::ledger::LedgerImpl<
auto lightNodeLedger = std::make_shared<bcos::ledger::LedgerImpl<
bcos::crypto::hasher::openssl::OpenSSL_SM3_Hasher, decltype(storageWrapper)>>(
bcos::crypto::hasher::openssl::OpenSSL_SM3_Hasher{}, std::move(storageWrapper),
protocolInitializer.blockFactory(), storage);
nodeLedger = std::make_shared<bcos::ledger::LedgerImpl<
bcos::crypto::hasher::openssl::OpenSSL_SM3_Hasher, decltype(storageWrapper)>>(
bcos::crypto::hasher::openssl::OpenSSL_SM3_Hasher{}, std::move(storageWrapper),
protocolInitializer.blockFactory(), storage);

LIGHTNODE_LOG(INFO) << "prepare genesis block...";
bcos::initializer::LedgerInitializer::build(
protocolInitializer.blockFactory(), storage, nodeConfig);

LIGHTNODE_LOG(INFO) << "start sm light node...";
starLightnode(nodeConfig, localLedger, front, gateway, keyFactory, nodeID);
starLightnode(nodeConfig, lightNodeLedger, nodeLedger, front, gateway, keyFactory, nodeID);
}
else
{
auto localLedger = std::make_shared<bcos::ledger::LedgerImpl<
auto lightNodeLedger = std::make_shared<bcos::ledger::LedgerImpl<
bcos::crypto::hasher::openssl::OpenSSL_Keccak256_Hasher, decltype(storageWrapper)>>(
bcos::crypto::hasher::openssl::OpenSSL_Keccak256_Hasher{}, std::move(storageWrapper),
protocolInitializer.blockFactory(), storage);

LIGHTNODE_LOG(INFO) << "prepare genesis block...";
bcos::initializer::LedgerInitializer::build(
protocolInitializer.blockFactory(), storage, nodeConfig);
nodeLedger = std::make_shared<bcos::ledger::LedgerImpl<
bcos::crypto::hasher::openssl::OpenSSL_SM3_Hasher, decltype(storageWrapper)>>(
bcos::crypto::hasher::openssl::OpenSSL_SM3_Hasher{}, std::move(storageWrapper),
protocolInitializer.blockFactory(), storage);

LIGHTNODE_LOG(INFO) << "start light node...";
starLightnode(nodeConfig, localLedger, front, gateway, keyFactory, nodeID);
starLightnode(nodeConfig, lightNodeLedger, nodeLedger, front, gateway, keyFactory, nodeID);
}

return 0;
Expand Down

0 comments on commit 9913615

Please sign in to comment.