Skip to content

fixed bug in channel creation and added comments #317

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

Merged
merged 1 commit into from
Nov 24, 2022
Merged
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
6 changes: 6 additions & 0 deletions src/Lachain.Networking/NetworkManagerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public void Start()
_hubConnector.Start();
}

// returns existing worker or create a new one if worker does not exists
// not synchronized for the sake of performance
private ClientWorker? GetClientWorker(ECDSAPublicKey publicKey)
{
if (_messageFactory.GetPublicKey().Equals(publicKey)) return null;
Expand All @@ -83,9 +85,13 @@ public void Start()
return CreateMsgChannel(publicKey);
}

// check if worker exists before creating and adding one
// synchronized to avoid exception of adding existing key to dictionary
[MethodImpl(MethodImplOptions.Synchronized)]
private ClientWorker? CreateMsgChannel(ECDSAPublicKey publicKey)
{
if (_clientWorkers.TryGetValue(publicKey, out var existingWorker))
return existingWorker;
Logger.LogTrace($"Connecting to peer {publicKey.ToHex()}");
var worker = new ClientWorker(publicKey, _messageFactory, _hubConnector);
_clientWorkers.Add(publicKey, worker);
Expand Down