Files

138 lines
4.5 KiB
C++
Raw Permalink Normal View History

2020-04-17 19:33:06 +02:00
/*
* This file is part of the Flowee project
2023-04-17 19:22:05 +02:00
* Copyright (C) 2020-2023 Tom Zander <tom@flowee.org>
2020-04-17 19:33:06 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "FillAddressDBAction.h"
#include "DownloadManager.h"
#include "ConnectionManager.h"
#include "PeerAddressDB.h"
#include "Peer.h"
#include <time.h>
#include <functional>
2020-10-29 21:47:53 +01:00
static int PORTNR = 8333;
static std::vector<std::string> fillSeeders(P2PNet::Chain chain)
2020-04-17 19:33:06 +02:00
{
std::vector<std::string> answer;
2020-10-29 21:47:53 +01:00
switch (chain) {
case P2PNet::MainChain:
answer.push_back("seed.flowee.cash");
2024-01-30 20:49:22 +01:00
answer.push_back("bchseed.c3-soft.com");
answer.push_back("bch.bitjson.com");
2020-10-29 21:47:53 +01:00
break;
case P2PNet::Testnet4Chain:
answer.push_back("testnet4-seed.flowee.cash");
2024-01-30 20:49:22 +01:00
answer.push_back("testnet4.bitjson.com");
2020-10-29 21:47:53 +01:00
PORTNR = 28333;
break;
}
2020-04-17 19:33:06 +02:00
return answer;
}
2020-11-05 17:42:51 +01:00
static int s_minAddressEntries = 2000;
2020-04-17 19:33:06 +02:00
FillAddressDBAction::FillAddressDBAction(DownloadManager *parent)
: Action(parent),
2025-02-08 19:05:26 +01:00
m_resolver(parent->ioContext()),
2020-10-29 21:47:53 +01:00
m_seeders(fillSeeders(parent->chain()))
2020-04-17 19:33:06 +02:00
{
assert(!m_seeders.empty());
2020-11-05 17:42:51 +01:00
if (parent->chain() != P2PNet::MainChain)
s_minAddressEntries = 25;
2020-04-17 19:33:06 +02:00
}
void FillAddressDBAction::execute(const boost::system::error_code &error)
{
if (error)
return;
2024-01-28 21:09:19 +01:00
if ((m_dnsLookupState % 2) != 1) {
2023-11-03 18:28:27 +01:00
// start a new DNS lookup?
2024-01-28 21:09:19 +01:00
const size_t index = m_dnsLookupState / 2;
2023-11-03 18:28:27 +01:00
if (index == m_seeders.size()) {
2020-11-05 16:55:38 +01:00
logInfo() << "Asked all DNS seeds";
}
2023-11-03 18:28:27 +01:00
else if (index < m_seeders.size()) {
2024-01-28 21:09:19 +01:00
m_dnsLookupState++;
2023-04-17 19:22:05 +02:00
logInfo() << "Start to resolve DNS entry" << m_seeders.at(index);
2020-11-05 16:55:38 +01:00
const std::string port = strprintf("%d", PORTNR);
2023-11-03 16:58:38 +01:00
m_resolver.async_resolve(m_seeders.at(index), port, std::bind(&FillAddressDBAction::onAddressResolveComplete,
2020-11-05 16:55:38 +01:00
this, std::placeholders::_1, std::placeholders::_2));
2021-01-13 18:15:33 +01:00
again();
return;
2020-04-17 19:33:06 +02:00
}
}
2024-01-28 21:09:19 +01:00
if ((m_dnsLookupState % 2) == 0) {
// not running a DNS lookup right now.
if (m_dlm->connectionManager().peerAddressDb().peerCount() > s_minAddressEntries) {
logInfo() << "==== FillAddressDb ==== DONE";
m_dlm->done(this);
return;
}
}
2020-04-17 19:33:06 +02:00
for (auto peer : m_dlm->connectionManager().connectedPeers()) {
auto address = peer->peerAddress();
2023-02-25 11:05:13 +01:00
if (address.isValid() && address.lastReceivedGoodHeaders()) {
2020-04-17 19:33:06 +02:00
if (!address.askedAddresses()) {
address.setAskedAddresses(true);
2023-04-17 19:22:05 +02:00
logInfo() << "Sending GetAddr msg to" << peer->connectionId();
2020-04-17 19:33:06 +02:00
peer->sendMessage(Message(Api::LegacyP2P, Api::P2P::GetAddr));
m_lastRequestStarted = time(nullptr);
again();
return;
}
}
}
2023-04-17 19:22:05 +02:00
if (m_lastRequestStarted > 0 && static_cast<int64_t>(time(nullptr)) - m_lastRequestStarted < 60) {
2020-04-17 19:33:06 +02:00
again();
return;
}
// lets connect to some new peers then.
auto address = m_dlm->connectionManager().peerAddressDb().findBest();
if (address.isValid()) {
logInfo() << "AddressDB still needs more data: creating a new connection";
m_dlm->connectionManager().connect(address);
}
again();
}
2023-11-03 16:58:38 +01:00
void FillAddressDBAction::onAddressResolveComplete(const boost::system::error_code &error, boost::asio::ip::tcp::resolver::results_type results)
2020-04-17 19:33:06 +02:00
{
if (error.value() == boost::asio::error::operation_aborted)
// this means the app is shutting down.
return;
2023-11-03 16:58:38 +01:00
int count = 0;
2020-04-17 19:33:06 +02:00
if (!error) {
2023-11-03 16:58:38 +01:00
auto iter = results.cbegin();
while (iter != results.cend()) {
EndPoint ep(iter->endpoint().address(), PORTNR);
++count;
2020-04-17 19:33:06 +02:00
m_dlm->connectionManager().peerAddressDb().addOne(ep);
2023-11-03 16:58:38 +01:00
iter++;
2020-04-17 19:33:06 +02:00
}
}
2023-11-03 16:58:38 +01:00
logInfo() << "DNS resolve came back. Success:" << !((bool) error) << "Result count:" << count;
2020-04-17 19:33:06 +02:00
m_dnsLookupState++;
}