Skip to content

Instantly share code, notes, and snippets.

@riemannulus
Created December 2, 2022 15:47
Show Gist options
  • Save riemannulus/a246864e0f755e82d1d61d80176b2ba2 to your computer and use it in GitHub Desktop.
Save riemannulus/a246864e0f755e82d1d61d80176b2ba2 to your computer and use it in GitHub Desktop.
WaitForUntilLeast23NodeAlive
public async Task WaitForUntilLeast23NodeAlive(CancellationToken ctx)
{
async Task<bool> PongResponse(BoundPeer peer)
{
try
{
var result = await _transport.SendMessageAsync(
peer,
new PingMsg(),
TimeSpan.FromSeconds(1),
ctx);
return result is PongMsg;
}
catch (CommunicationFailException)
{
return false;
}
}
while (true)
{
var tasks = new List<Task<bool>>();
foreach (var boundPeer in _peers)
{
tasks.Add(PongResponse(boundPeer));
}
var result = await Task.WhenAll(tasks);
if (result.Count(x => x) + 1 >= (_peers.Count() + 1) * 2 / 3)
{
break;
}
_logger.Debug(
"Bootstrapped validators are not reached +2/3 yet," +
" current: {BootstrappedValidatorCount}",
result.Count(x => x));
}
_logger.Debug(
"Bootstrapped validators are reached +2/3");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment