Merge pull request #707 from majestrate/fix-bootstrap

Fix bootstraping
pull/711/head
Jeff 5 years ago committed by GitHub
commit 4724bbf279
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -61,7 +61,8 @@ namespace llarp
auto threads = config->router.workerThreads();
if(threads <= 0)
threads = 1;
worker = std::make_shared< llarp::thread::ThreadPool >(threads, 1024, "llarp-worker");
worker = std::make_shared< llarp::thread::ThreadPool >(threads, 1024,
"llarp-worker");
nodedb_dir = config->netdb.nodedbDir();

@ -645,7 +645,7 @@ namespace llarp
// ourKey should never be in the connected list
// requester is likely in the connected list
// 4 or connection nodes (minus a potential requestor), whatever is less
if(!_nodes->GetManyNearExcluding(t, found, 1,
if(!_nodes->GetManyNearExcluding(t, found, nodeCount >= 4 ? 4 : 1,
std::set< Key_t >{ourKey, requester}))
{
llarp::LogError(
@ -685,12 +685,9 @@ namespace llarp
{
TXOwner asker(whoasked, txid);
TXOwner peer(askpeer, ++ids);
if(target != askpeer)
{
_pendingRouterLookups.NewTX(
peer, asker, target,
new RecursiveRouterLookup(asker, target, this, handler));
}
_pendingRouterLookups.NewTX(
peer, asker, target,
new RecursiveRouterLookup(asker, target, this, handler));
}
llarp_time_t

@ -129,8 +129,13 @@ llarp_nodedb::InsertAsync(llarp::RouterContact rc,
std::shared_ptr< llarp::Logic > logic,
std::function< void(void) > completionHandler)
{
disk->addJob(
std::bind(&handle_async_insert_rc, this, rc, logic, completionHandler));
const auto job = std::bind(&handle_async_insert_rc, this, rc, logic, completionHandler);
size_t tries = 10;
while((!disk->addJob(job)) && tries-- > 0)
std::this_thread::sleep_for(std::chrono::milliseconds(1));
// on fail do synchronous write
if(tries == 0)
Insert(rc);
}
bool

@ -617,7 +617,7 @@ namespace llarp
{
if(!rc.Verify(Now()))
continue;
nodedb()->InsertAsync(rc);
nodedb()->UpdateAsyncIfNewer(rc);
if(ConnectionToRouterAllowed(rc.pubkey)
&& numConnected < minConnectedRouters)
@ -686,6 +686,7 @@ namespace llarp
if(whitelistRouters
&& lokinetRouters.find(result.pubkey) == lokinetRouters.end())
continue;
nodedb()->UpdateAsyncIfNewer(result);
TryConnectAsync(result, 10);
}
}
@ -920,7 +921,6 @@ namespace llarp
std::make_unique< FileLogStream >(diskworker(), logfile, 100, true);
}
netConfig.insert(conf->dns.netConfig.begin(), conf->dns.netConfig.end());
std::vector< std::string > configRouters = conf->connect.routers;
@ -1011,7 +1011,7 @@ namespace llarp
if(result.size() == 1 && !result[0].IsExpired(now))
{
LogInfo("storing rc for ", router);
nodedb()->Insert(result[0]);
nodedb()->UpdateAsyncIfNewer(result[0]);
}
else
{
@ -1074,6 +1074,33 @@ namespace llarp
> 0;
}
bool
Router::ShouldReportStats(llarp_time_t now) const
{
static constexpr llarp_time_t ReportStatsInterval = 30 * 1000;
return now - m_LastStatsReport > ReportStatsInterval;
}
void
Router::ReportStats()
{
const auto now = Now();
LogInfo("---- BEGIN REPORT ----");
LogInfo(nodedb()->num_loaded(), " RCs loaded");
LogInfo(bootstrapRCList.size(), " bootstrap peers");
LogInfo(NumberOfConnectedRouters(), " router connections");
if(IsServiceNode())
{
LogInfo(NumberOfConnectedClients(), " client connections");
LogInfo(_rc.Age(now), " ms since we last updated our RC");
LogInfo(_rc.TimeUntilExpires(now), " ms until our RC expires");
}
LogInfo(now, " system time");
LogInfo(m_LastStatsReport, " last reported stats");
LogInfo("----- END REPORT -----");
m_LastStatsReport = now;
}
void
Router::Tick()
{
@ -1084,7 +1111,49 @@ namespace llarp
routerProfiling().Tick();
if(IsServiceNode())
if(ShouldReportStats(now))
{
ReportStats();
}
const auto insertRouters = [&](const std::vector< RouterContact > &res) {
// store found routers
for(const auto &rc : res)
{
// don't accept expired RCs
if(rc.Verify(Now(), false))
nodedb()->InsertAsync(rc);
}
};
const bool isSvcNode = IsServiceNode();
// try looking up stale routers
nodedb()->VisitInsertedBefore(
[&](const RouterContact &rc) {
if(HasPendingRouterLookup(rc.pubkey))
return;
LookupRouter(rc.pubkey, insertRouters);
},
now - RouterContact::UpdateInterval);
std::set< RouterID > removeStale;
// remove stale routers
const auto timeout = RouterContact::UpdateWindow * RouterContact::UpdateTries;
nodedb()->VisitInsertedBefore(
[&](const RouterContact &rc) {
if(IsBootstrapNode(rc.pubkey))
return;
LogInfo("removing stale router: ", RouterID(rc.pubkey));
removeStale.insert(rc.pubkey);
},
now - timeout);
nodedb()->RemoveIf([removeStale](const RouterContact &rc) -> bool {
return removeStale.count(rc.pubkey) > 0;
});
if(isSvcNode)
{
if(_rc.ExpiresSoon(now, randint() % 10000)
|| (now - _rc.last_updated) > rcRegenInterval)
@ -1103,36 +1172,7 @@ namespace llarp
});
*/
}
else
{
// try looking up stale routers
nodedb()->VisitInsertedBefore(
[&](const RouterContact &rc) {
if(HasPendingRouterLookup(rc.pubkey))
return;
LookupRouter(rc.pubkey,
[&](const std::vector< RouterContact > &result) {
// store found routers
for(const auto &rc : result)
nodedb()->InsertAsync(rc);
});
},
now - RouterContact::UpdateInterval);
std::set< RouterID > removeStale;
// remove stale routers
nodedb()->VisitInsertedBefore(
[&](const RouterContact &rc) {
if(IsBootstrapNode(rc.pubkey))
return;
removeStale.insert(rc.pubkey);
},
now - ((RouterContact::UpdateInterval * 3) / 2));
nodedb()->RemoveIf([removeStale](const RouterContact &rc) -> bool {
return removeStale.count(rc.pubkey) > 0;
});
}
// expire transit paths
// expire paths
paths.ExpirePaths(now);
{
auto itr = pendingEstablishJobs.begin();
@ -1215,7 +1255,7 @@ namespace llarp
ConnectToRandomRouters(dlt);
}
if(!IsServiceNode())
if(!isSvcNode)
{
_hiddenServiceContext.Tick(now);
}
@ -1493,8 +1533,17 @@ namespace llarp
LogInfo("RPC Caller to ", lokidRPCAddr, " started");
}
cryptoworker->start();
disk->start();
if(!cryptoworker->start())
{
LogError("crypto worker failed to start");
return false;
}
if(!disk->start())
{
LogError("disk worker failed to start");
return false;
}
for(const auto &rc : bootstrapRCList)
{

@ -549,6 +549,14 @@ namespace llarp
std::atomic< bool > _stopping;
std::atomic< bool > _running;
llarp_time_t m_LastStatsReport = 0;
bool
ShouldReportStats(llarp_time_t now) const;
void
ReportStats();
bool
UpdateOurRC(bool rotateKeys = false);

@ -36,6 +36,8 @@ namespace llarp
llarp_time_t RouterContact::UpdateInterval = 30 * 60 * 1000;
// 1 minute window for update
llarp_time_t RouterContact::UpdateWindow = 60 * 1000;
// try 5 times to update
int RouterContact::UpdateTries = 5;
NetID::NetID(const byte_t *val) : AlignedBuffer< 8 >()
{
@ -235,16 +237,27 @@ namespace llarp
bool
RouterContact::IsExpired(llarp_time_t now) const
{
return Age(now) >= Lifetime;
}
llarp_time_t
RouterContact::TimeUntilExpires(llarp_time_t now) const
{
const auto expiresAt = last_updated + Lifetime;
return now >= expiresAt;
return now < expiresAt ? expiresAt - now : 0;
}
llarp_time_t
RouterContact::Age(llarp_time_t now) const
{
return now > last_updated ? now - last_updated : 0;
}
bool
RouterContact::ExpiresSoon(llarp_time_t now, llarp_time_t dlt) const
{
const auto expiresAt = last_updated + Lifetime;
return expiresAt <= now || expiresAt - now <= dlt;
return TimeUntilExpires(now) <= dlt;
}
std::string

@ -71,6 +71,7 @@ namespace llarp
static llarp_time_t Lifetime;
static llarp_time_t UpdateInterval;
static llarp_time_t UpdateWindow;
static int UpdateTries;
RouterContact()
{
@ -179,6 +180,14 @@ namespace llarp
bool
IsExpired(llarp_time_t now) const;
/// returns time in ms until we expire or 0 if we have expired
llarp_time_t
TimeUntilExpires(llarp_time_t now) const;
/// get the age of this RC in ms
llarp_time_t
Age(llarp_time_t now) const;
bool
OtherIsNewer(const RouterContact &other) const
{

Loading…
Cancel
Save