Skip to content

Commit

Permalink
When we rate-limit seqno requests, delay rather than dropping the mos…
Browse files Browse the repository at this point in the history
…t recent update
  • Loading branch information
swatanabe committed Aug 4, 2023
1 parent 1944989 commit a211c34
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
16 changes: 15 additions & 1 deletion libraries/net/include/psibase/shortest_path_routing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <psibase/net_base.hpp>
#include <psibase/routing_base.hpp>

#include <boost/asio/steady_timer.hpp>
#include <chrono>
#include <cstdint>
#include <map>
Expand Down Expand Up @@ -156,7 +157,7 @@ namespace psibase::net
std::chrono::steady_clock::time_point time;
};

explicit shortest_path_routing(boost::asio::io_context& ctx) : base_type(ctx)
explicit shortest_path_routing(boost::asio::io_context& ctx) : base_type(ctx), seqnoTimer(ctx)
{
logger.add_attribute("Channel", boost::log::attributes::constant(std::string("p2p")));
}
Expand Down Expand Up @@ -480,6 +481,18 @@ namespace psibase::net
multicast(RouteUpdateMessage{consensus().producer_name(), seqno, 0});
lastSeqnoUpdate = now;
}
else
{
seqnoTimer.expires_at(lastSeqnoUpdate + minSeqnoUpdateInterval);
seqnoTimer.async_wait(
[this](const std::error_code& e)
{
if (!e)
{
incrementSeqno();
}
});
}
}
bool cacheSeqnoRequest(const RouteSeqnoRequest& msg)
{
Expand Down Expand Up @@ -537,6 +550,7 @@ namespace psibase::net
std::map<producer_id, peer_id> selectedRoutes;

std::map<producer_id, CachedSeqnoRequest> recentSeqnoRequests;
boost::asio::steady_timer seqnoTimer;

loggers::common_logger logger;
};
Expand Down
12 changes: 6 additions & 6 deletions programs/psinode/tests/test_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,23 @@ def test_change_route(self, cluster):

@testutil.psinode_test
def test_bft(self, cluster):
(a, b, c, d, e, f, g) = cluster.line('a', 'b', 'c', 'd', 'e', 'f', 'g')
# This isn't working. The problem is that the routing algorithm
# doesn't respect the order dependency between blocks and consensus
# messages related to said blocks.
testutil.boot_with_producers([a, b, c, d, e, f, g], 'bft', timeout=15)
(a, b, c, d, e, f, g) = cluster.ring('a', 'b', 'c', 'd', 'e', 'f', 'g')
testutil.boot_with_producers([a, b, c, d, e, f, g], 'bft')

# wait for irreversibility to advance
a.wait(new_block())
a.wait(irreversible(a.get_block_header()['blockNum']))

a.disconnect(g)

# Switch between two different configurations
print('checking blocks')
testutil.sleep(0.0, 0.5)
for i in range(10):
header = a.get_block_header()
print(header)
self.assertEqual(header['commitNum'] + 2, header['blockNum'])
time.sleep(1)
testutil.sleep(0.5, 0.5)
if i % 2 == 0:
a.connect(g)
a.disconnect(b)
Expand Down
12 changes: 12 additions & 0 deletions programs/psinode/tests/testutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import argparse
import unittest
import sys
import time
import math

def psinode_test(f):
def result(self):
Expand Down Expand Up @@ -33,6 +35,16 @@ def boot_with_producers(nodes, algorithm=None, timeout=10):
p.wait(predicates.producers_are(nodes), timeout=timeout)
return p

def sleep(secs, end_at):
'''Like time.sleep, but waits additional time until the fractional seconds are equal to end_at'''
ticks_per_sec = 1000000000
current = (time.time_ns() % ticks_per_sec) / ticks_per_sec
extra = end_at - (current + math.modf(secs)[0])
if extra < 0:
extra += 1

time.sleep(secs + extra)

def main(argv=sys.argv):
parser = argparse.ArgumentParser()
parser.add_argument("--psinode", default="psinode", help="The path to the psinode executable")
Expand Down

0 comments on commit a211c34

Please sign in to comment.