Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shutter: refactor & increase disconnection log timeout #7722

Merged
merged 6 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Nethermind/Nethermind.Shutter/Config/IShutterConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ public interface IShutterConfig : IConfig
DefaultValue = "true", HiddenFromDocs = true)]
bool Validator { get; set; }

[ConfigItem(Description = "How many milliseconds to wait for transactions before sending a disconnection warning.",
DefaultValue = "1200000", HiddenFromDocs = true)]
uint DisconnectionLogTimeout { get; set; }

[ConfigItem(Description = "How many milliseconds in between each disconnection warning.",
DefaultValue = "60000", HiddenFromDocs = true)]
uint DisconnectionLogInterval { get; set; }

public void Validate(out Multiaddress[] bootnodeP2PAddresses)
{
if (Validator && ValidatorInfoFile is null)
Expand Down Expand Up @@ -107,6 +115,11 @@ public void Validate(out Multiaddress[] bootnodeP2PAddresses)
throw new ArgumentException("Must set Shutter keyper set manager contract address to valid address.");
}

if (DisconnectionLogTimeout < 60000)
{
throw new ArgumentException("Must set Shutter disconnection log timeout to at least a minute.");
}

if (P2PAgentVersion is null)
{
throw new ArgumentNullException(nameof(P2PAgentVersion));
Expand Down
2 changes: 2 additions & 0 deletions src/Nethermind/Nethermind.Shutter/Config/ShutterConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ public class ShutterConfig : IShutterConfig
public ulong InstanceID { get; set; } = 0;
public int EncryptedGasLimit { get; set; } = 10000000;
public ushort MaxKeyDelay { get; set; } = 1666;
public uint DisconnectionLogTimeout { get; set; } = 1200000;
public uint DisconnectionLogInterval { get; set; } = 60000;
}
}
15 changes: 11 additions & 4 deletions src/Nethermind/Nethermind.Shutter/ShutterP2P.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ public class ShutterP2P : IShutterP2P
private readonly PeerStore _peerStore;
private readonly ILocalPeer _peer;
private readonly ServiceProvider _serviceProvider;
private readonly TimeSpan DisconnectionLogTimeout;
private readonly TimeSpan DisconnectionLogInterval;
private CancellationTokenSource? _cts;
private static readonly TimeSpan DisconnectionLogTimeout = TimeSpan.FromMinutes(5);

public class ShutterP2PException(string message, Exception? innerException = null) : Exception(message, innerException);

Expand All @@ -42,12 +43,14 @@ public ShutterP2P(IShutterConfig shutterConfig, ILogManager logManager, IFileSys
{
_logger = logManager.GetClassLogger();
_cfg = shutterConfig;
DisconnectionLogTimeout = TimeSpan.FromMilliseconds(_cfg.DisconnectionLogTimeout);
Marchhill marked this conversation as resolved.
Show resolved Hide resolved
DisconnectionLogInterval = TimeSpan.FromMilliseconds(_cfg.DisconnectionLogInterval);
_serviceProvider = new ServiceCollection()
.AddLibp2p(builder => builder)
.AddSingleton(new IdentifyProtocolSettings
{
ProtocolVersion = shutterConfig.P2PProtocolVersion,
AgentVersion = shutterConfig.P2PAgentVersion
ProtocolVersion = _cfg.P2PProtocolVersion,
AgentVersion = _cfg.P2PAgentVersion
})
// pubsub settings
.AddSingleton(new Settings()
Expand Down Expand Up @@ -97,15 +100,18 @@ public async Task Start(Multiaddress[] bootnodeP2PAddresses, Func<Dto.Decryption
if (_logger.IsInfo) _logger.Info($"Started Shutter P2P: {_peer.Address}");

long lastMessageProcessed = DateTimeOffset.Now.ToUnixTimeSeconds();
bool hasTimedOut = false;

while (true)
{
try
{
using var timeoutSource = new CancellationTokenSource(DisconnectionLogTimeout);
using var timeoutSource = new CancellationTokenSource(hasTimedOut ? DisconnectionLogInterval : DisconnectionLogTimeout);
using var source = CancellationTokenSource.CreateLinkedTokenSource(_cts.Token, timeoutSource.Token);

byte[] msg = await _msgQueue.Reader.ReadAsync(source.Token);
lastMessageProcessed = DateTimeOffset.Now.ToUnixTimeSeconds();
hasTimedOut = false;
ProcessP2PMessage(msg, onKeysReceived);
}
catch (OperationCanceledException)
Expand All @@ -117,6 +123,7 @@ public async Task Start(Multiaddress[] bootnodeP2PAddresses, Func<Dto.Decryption
}
else if (_logger.IsWarn)
{
hasTimedOut = true;
long delta = DateTimeOffset.Now.ToUnixTimeSeconds() - lastMessageProcessed;
_logger.Warn($"Not receiving Shutter messages ({delta / 60}m)...");
}
Expand Down