Skip to content

Commit

Permalink
added default peers
Browse files Browse the repository at this point in the history
  • Loading branch information
anatolse committed Nov 20, 2023
1 parent e0e73b0 commit b52b406
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
6 changes: 1 addition & 5 deletions wallet/client/wallet_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1939,11 +1939,7 @@ namespace beam::wallet
{
if (auto s = m_nodeNetwork.lock())
{
if (!(s->setNodeAddress(addr)))
{
LOG_ERROR() << "Unable to resolve node address: " << addr;
onWalletError(ErrorType::HostResolvedError);
}
s->setNodeAddress(addr);
}
else
{
Expand Down
51 changes: 30 additions & 21 deletions wallet/core/node_network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
// limitations under the License.

#include "node_network.h"
#include "utility/logger.h"
#include "wallet/core/default_peers.h"

namespace beam::wallet
{
void NodeNetwork::tryToConnect()
{
// if user changed address to correct (using of setNodeAddress)
if (m_Cfg.m_vNodes.size() > 0)
return;
m_Cfg.m_vNodes.clear();

if (!m_timer)
{
Expand All @@ -43,21 +43,40 @@ namespace beam::wallet
}

m_timer->start(RECONNECTION_TIMEOUT, false, [this]() {
io::Address nodeAddr;
assert(!m_nodeAddress.empty());
if (!m_nodeAddress.empty() && nodeAddr.resolve(m_nodeAddress.c_str()))
auto nodeAddresses = getDefaultPeers();
nodeAddresses.push_back(m_nodeAddress);
for (const auto& addr : nodeAddresses)
{
m_Cfg.m_vNodes.push_back(nodeAddr);
io::Address nodeAddr;
assert(!addr.empty());
if (!addr.empty() && nodeAddr.resolve(addr.c_str()))
{
m_Cfg.m_vNodes.push_back(nodeAddr);
}
else
{
LOG_WARNING() << "Unable to resolve node address: " << addr;
}
}
if (!m_Cfg.m_vNodes.empty())
{
std::sort(m_Cfg.m_vNodes.begin(), m_Cfg.m_vNodes.end());
m_Cfg.m_vNodes.erase(std::unique(m_Cfg.m_vNodes.begin(), m_Cfg.m_vNodes.end()), m_Cfg.m_vNodes.end());
Connect();
return;
}
else if (!m_fallbackAddresses.empty())

LOG_WARNING() << "User-specified nodes cannot be resolved.";
if (!m_fallbackAddresses.empty())
{
LOG_WARNING() << "Attempting to connect to fallback nodes";
// try to solve DNS problem with known ip addresses
std::copy(m_fallbackAddresses.begin(), m_fallbackAddresses.end(), std::back_inserter(m_Cfg.m_vNodes));
Connect();
}
else
{
LOG_WARNING() << "There are no fallback nodes. Trying to re-connect in " << RECONNECTION_TIMEOUT << " ms";
tryToConnect();
}
});
Expand Down Expand Up @@ -96,21 +115,11 @@ namespace beam::wallet
return m_nodeAddress;
}

bool NodeNetwork::setNodeAddress(const std::string& nodeAddr)
void NodeNetwork::setNodeAddress(const std::string& nodeAddr)
{
Disconnect();
io::Address address;
if (!nodeAddr.empty() && address.resolve(nodeAddr.c_str()))
{
m_Cfg.m_vNodes.clear();
m_Cfg.m_vNodes.push_back(address);

Connect();

m_nodeAddress = nodeAddr;
return true;
}
return false;
m_nodeAddress = nodeAddr;
tryToConnect();
}

const std::string& NodeNetwork::getLastError() const
Expand Down
4 changes: 3 additions & 1 deletion wallet/core/node_network.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#pragma once

#include <string>
#include <vector>
#include "core/fly_client.h"
#include "wallet/core/node_connection_observer.h"

Expand All @@ -38,7 +40,7 @@ namespace beam::wallet
void Unsubscribe(INodeConnectionObserver* observer);

std::string getNodeAddress() const;
bool setNodeAddress(const std::string&);
void setNodeAddress(const std::string&);
const std::string& getLastError() const;
size_t getConnections() const;

Expand Down

0 comments on commit b52b406

Please sign in to comment.