Skip to content

Commit

Permalink
Shutter: refactor & increase disconnection log timeout (#7722)
Browse files Browse the repository at this point in the history
Co-authored-by: ak88 <anders@nethermind.io>
  • Loading branch information
Marchhill and ak88 authored Nov 6, 2024
1 parent a39cd9f commit 8de524c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
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);
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

0 comments on commit 8de524c

Please sign in to comment.