Skip to content

Commit

Permalink
Замена Guid на wallet address (solana) для для идентификации nodes (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxirmx committed Jun 23, 2024
1 parent 995d25e commit 0784982
Show file tree
Hide file tree
Showing 18 changed files with 117 additions and 190 deletions.
5 changes: 5 additions & 0 deletions dkgCommon/dkgCommon.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
<PackageReference Include="Solana.Wallet" Version="8.0.2" />
</ItemGroup>

</Project>
26 changes: 13 additions & 13 deletions dkgNode/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@

using dkgNode.Models;
using dkgNode.Services;

using Microsoft.Extensions.Logging;
using Solnet.Wallet;
using Solnet.Wallet.Bip39;

var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService(serviceProvider =>
Expand All @@ -45,26 +47,24 @@
}
}

Guid guid = Guid.NewGuid();
string? gd = Environment.GetEnvironmentVariable("DKG_NODE_GUID");
if (gd != null)
string? address = Environment.GetEnvironmentVariable("DKG_NODE_SOLANA_ADDRESS");
if (address is null)
{
try
{
guid = new(gd);
}
catch
{
logger.LogWarning("DKG_NODE_GUID must be a balid GUID, got {gd}", gd);
}
string? mnemonic;
(address, mnemonic) = DkgNodeConfig.GenerateNewAddress();
logger.LogWarning("**** Creating solana wallet, please use it for testing only ****\nSolana Address: {solanaAddress}\nMnemonic: {mnemonic}", address, mnemonic);
}
else
{
logger.LogInformation("Using Solana Address: {solanaAddress}", address);
}

var config = new DkgNodeConfig()
{
NiceName = Environment.GetEnvironmentVariable("DKG_NODE_NAME"),
PollingInterval = pollingInterval,
ServiceNodeUrl = Environment.GetEnvironmentVariable("DKG_SERVICE_NODE_URL") ?? "https://localhost:8081",
Gd = guid
Address = address
};

string? dieOnStep2 = Environment.GetEnvironmentVariable("DKG_NODE_DIE_ON_STEP_TWO");
Expand Down
6 changes: 3 additions & 3 deletions dkgNode/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning",
"Microsoft.Hosting.Lifetime": "Warning",
"Microsoft.EntityFrameworkCore": "Warning"
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information",
"Microsoft.EntityFrameworkCore": "Information"

}
}
Expand Down
4 changes: 3 additions & 1 deletion dkgNode/dkgNode.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.20.1" />
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
<PackageReference Include="Solana.Wallet" Version="8.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
25 changes: 20 additions & 5 deletions dkgNodeLibrary/Models/DkgNodeConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

using Microsoft.Extensions.Logging;
using Solnet.Wallet.Bip39;
using Solnet.Wallet;
using System.Runtime.CompilerServices;
using System.Text.Json.Serialization;
[assembly: InternalsVisibleTo("dkgNodesTests")]
Expand All @@ -41,8 +44,7 @@ public class DkgNodeConfig
[JsonIgnore]
public string ServiceNodeUrl { get; set; }

[JsonPropertyName("GUID")]
public Guid Gd { get; set; }
public string Address { get; set; }

[JsonPropertyName("PublicKey")]
public string? SerializedPublicKey
Expand All @@ -61,24 +63,37 @@ public void EncodePublicKey(byte[] value)
[JsonPropertyName("Name")]
public string Name
{
get { return NiceName ?? Gd.ToString(); }
get { return NiceName ?? Address; }
}
public DkgNodeConfig()
{
NiceName = null;
PublicKey = null;
Gd = Guid.NewGuid();
Address = string.Empty;
PollingInterval = 3000;
ServiceNodeUrl = "https://localhost:8081";
}
public DkgNodeConfig(DkgNodeConfig other)
{
NiceName = other.NiceName;
PublicKey = other.PublicKey;
Gd = other.Gd;
Address = other.Address;
PollingInterval = other.PollingInterval;
ServiceNodeUrl = other.ServiceNodeUrl;
}

public static (string, string) GenerateNewAddress()
{
var mnemonic = new Mnemonic(WordList.English, WordCount.Twelve);
var wallet = new Wallet(mnemonic);
Account account = wallet.Account;

// The public key of the account is the Solana address
string solanaAddress = account.PublicKey.Key;

return (solanaAddress, mnemonic.ToString());
}

}
}

2 changes: 2 additions & 0 deletions dkgNodeLibrary/dkgNodeLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
<PackageReference Include="Solana.Wallet" Version="8.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
30 changes: 10 additions & 20 deletions dkgNodesTests/DkgNodeConfig.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public class DkgNodeConfigTests
[Test]
public void TestNameReturnsNiceNameIfSet()
{
DkgNodeConfig config = new DkgNodeConfig { NiceName = "Test Node" };
DkgNodeConfig config = new() { NiceName = "Test Node" };
Assert.That(config.Name, Is.EqualTo("Test Node"));
}
[Test]
public void TestDefaultConstructorSetsDefaultValues()
{
DkgNodeConfig config = new DkgNodeConfig();
DkgNodeConfig config = new();
Assert.Multiple(() =>
{
Assert.That(config.NiceName, Is.Null);
Expand All @@ -50,16 +50,16 @@ public void TestDefaultConstructorSetsDefaultValues()
}

[Test]
public void TestNameReturnsGuidIfNiceNameNotSet()
public void TestNameReturnsAddressIfNiceNameNotSet()
{
DkgNodeConfig config = new DkgNodeConfig();
Assert.That(config.Name, Is.EqualTo(config.Gd.ToString()));
DkgNodeConfig config = new();
Assert.That(config.Name, Is.EqualTo(config.Address));
}

[Test]
public void TestGetPublicKeyReturnsPublicKey()
{
DkgNodeConfig config = new DkgNodeConfig();
DkgNodeConfig config = new();
byte[] publicKey = Encoding.ASCII.GetBytes("1234567890123456"); // 16 bytes
config.EncodePublicKey(publicKey);
Assert.That(config.GetPublicKey(), Is.EqualTo(Convert.ToBase64String(publicKey)));
Expand All @@ -68,45 +68,35 @@ public void TestGetPublicKeyReturnsPublicKey()
[Test]
public void TestCopyConstructorCopiesValues()
{
DkgNodeConfig original = new DkgNodeConfig
DkgNodeConfig original = new()
{
NiceName = "Test Node",
PublicKey = "publicKey",
ServiceNodeUrl = "https://example.com",
PollingInterval = 5000
};

DkgNodeConfig copy = new DkgNodeConfig(original);
DkgNodeConfig copy = new(original);

Assert.Multiple(() =>
{
Assert.That(copy.NiceName, Is.EqualTo(original.NiceName));
Assert.That(copy.PublicKey, Is.EqualTo(original.PublicKey));
Assert.That(copy.ServiceNodeUrl, Is.EqualTo(original.ServiceNodeUrl));
Assert.That(copy.PollingInterval, Is.EqualTo(original.PollingInterval));
Assert.That(copy.Gd, Is.EqualTo(original.Gd));
Assert.That(copy.Address, Is.EqualTo(original.Address));
});
}

[Test]
public void TestDefaultValues()
{
DkgNodeConfig config = new DkgNodeConfig();
DkgNodeConfig config = new();
Assert.Multiple(() =>
{
Assert.That(config.PollingInterval, Is.EqualTo(3000));
Assert.That(config.ServiceNodeUrl, Is.EqualTo("https://localhost:8081"));
});
}


[Test]
public void TestGuidIsUniqueForDifferentInstances()
{
DkgNodeConfig config1 = new DkgNodeConfig();
DkgNodeConfig config2 = new DkgNodeConfig();
Assert.That(config1.Gd, Is.Not.EqualTo(config2.Gd));
}

}
}
12 changes: 6 additions & 6 deletions dkgNodesTests/Runner.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ public void TestStartRoundAddsRoundToActiveRounds()
{
var round = new Round { Id = 1 };
_runner.StartRound(round);
Assert.That(_runner.ActiveRounds.Count, Is.EqualTo(1));
Assert.That(_runner.ActiveRounds, Has.Count.EqualTo(1));
Assert.That(_runner.ActiveRounds.First().Id, Is.EqualTo(round.Id));
}

[Test]
public void TestRunRoundRunsRound()
{
var round = new Round { Id = 1 };
var nodes = new List<Node> { new Node { PublicKey = "publicKey" } };
var nodes = new List<Node> { new () { PublicKey = "publicKey" } };
_runner.StartRound(round);
_runner.RunRound(round, nodes);
var activeRound = _runner.ActiveRounds.First();
Expand All @@ -80,7 +80,7 @@ public void TestFinishRoundRemovesRoundFromActiveRounds()
var round = new Round { Id = 1 };
_runner.StartRound(round);
_runner.FinishRound(round);
Assert.That(_runner.ActiveRounds.Count, Is.EqualTo(0));
Assert.That(_runner.ActiveRounds, Is.Empty);
}

[Test]
Expand All @@ -89,7 +89,7 @@ public void TestCancelRoundRemovesRoundFromActiveRounds()
var round = new Round { Id = 1 };
_runner.StartRound(round);
_runner.CancelRound(round);
Assert.That(_runner.ActiveRounds.Count, Is.EqualTo(0));
Assert.That(_runner.ActiveRounds, Is.Empty);
}
[Test]
public void TestProcessDealsCallsProcessDealsOnActiveRound()
Expand All @@ -108,7 +108,7 @@ public void TestProcessResponsesCallsProcessResponsesOnActiveRound()
_runner.StartRound(round);
_runner.ProcessResponses(round);
var activeRound = _runner.ActiveRounds.First();
Assert.IsTrue(activeRound.IsStepThreeDataReady());
Assert.That(activeRound.IsStepThreeDataReady(), Is.True);
}

[Test]
Expand All @@ -130,7 +130,7 @@ public void TestSetResultSetsResultOnActiveRound()
{
var round = new Round { Id = 1 };
var node = new Node { PublicKey = "publicKey" };
string[] data = { "AtDGHAvdzEBXkF9nrlWVyupD6AeTF2zHc+5EGExa13TB", "AQAAAGMygfx9vJSf4XEPUYIByz8rRU7cehXHxylasMN/1486" };
string[] data = ["AtDGHAvdzEBXkF9nrlWVyupD6AeTF2zHc+5EGExa13TB", "AQAAAGMygfx9vJSf4XEPUYIByz8rRU7cehXHxylasMN/1486"];
_runner.StartRound(round);

var nodes = new List<Node> { node };
Expand Down
2 changes: 2 additions & 0 deletions dkgNodesTests/dkgNodesTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
<PackageReference Include="Solana.Wallet" Version="8.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
7 changes: 4 additions & 3 deletions dkgServiceNode/Controllers/NodesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ public async Task<ActionResult<Reference>> RegisterNode(Node node)
roundId = round.Id;
}

var xNode = await dkgContext.FindNodeByGuidAsync(node.Gd);
var xNode = await dkgContext.FindNodeByAddressAsync(node.Address);
if (xNode == null)
{
node.RoundId = roundId;
node.CalculateRandom();
if (roundId == null) node.StatusValue = (short)NStatus.NotRegistered;
dkgContext.Nodes.Add(node);
await dkgContext.SaveChangesAsync();
xNode = await dkgContext.FindNodeByGuidAsync(node.Gd);
xNode = await dkgContext.FindNodeByAddressAsync(node.Address);
}
else
{
Expand Down Expand Up @@ -300,7 +300,8 @@ internal async Task<ObjectResult> AcceptFailed(Round? round, Node node, StatusRe
public async Task<ActionResult<Reference>> Status(StatusReport statusReport)
{
// А это мой любимый автомат Мили ... центр любой системы :)
Dictionary<(RStatus?, NStatus), Func<Round?, Node, StatusReport, Task<ObjectResult>>> actionMap = new Dictionary<(RStatus?, NStatus), Func<Round?, Node, StatusReport, Task<ObjectResult>>>()
Dictionary<(RStatus?, NStatus), Func<Round?, Node, StatusReport, Task<ObjectResult>>> actionMap =
new Dictionary<(RStatus?, NStatus), Func<Round?, Node, StatusReport, Task<ObjectResult>>>()
{
{ (null, NStatus.NotRegistered), Accept },
{ (RStatus.NotStarted, NStatus.NotRegistered), WrongStatus },
Expand Down
2 changes: 1 addition & 1 deletion dkgServiceNode/Controllers/RoundsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public async Task<ActionResult<Round>> CancelRound(int id)
return await UpdateRoundState(dkgContext, round);
}

internal List<Node> GetNodesWithRandomForRound(List<Node> nodes, int roundId)
internal static List<Node> GetNodesWithRandomForRound(List<Node> nodes, int roundId)
{
return nodes
.Where(node => node.NodesRoundHistory.Any(nrh => nrh.RoundId == roundId && nrh.NodeRandom != null))
Expand Down
Loading

0 comments on commit 0784982

Please sign in to comment.