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

Add notary service #691

Closed
wants to merge 14 commits into from
Closed
14 changes: 11 additions & 3 deletions neo-modules.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Microsoft Visual Studio Solution File, Format Version 12.00

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28729.10
MinimumVisualStudioVersion = 10.0.40219.1
Expand Down Expand Up @@ -34,9 +35,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.Plugins.RpcServer.Tests
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TokensTracker", "src\TokensTracker\TokensTracker.csproj", "{48CB0583-26CE-48FC-9F53-F7DC6F1727D8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MPTTrie", "src\MPTTrie\MPTTrie.csproj", "{D167FA6B-D2A3-4D8A-A65D-686DD06650F6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MPTTrie", "src\MPTTrie\MPTTrie.csproj", "{D167FA6B-D2A3-4D8A-A65D-686DD06650F6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.Cryptography.MPTTrie.Tests", "tests\Neo.Cryptography.MPTTrie.Tests\Neo.Cryptography.MPTTrie.Tests.csproj", "{8D2EE375-2E2D-45FE-A4E9-0254D12C7554}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.Cryptography.MPTTrie.Tests", "tests\Neo.Cryptography.MPTTrie.Tests\Neo.Cryptography.MPTTrie.Tests.csproj", "{8D2EE375-2E2D-45FE-A4E9-0254D12C7554}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NotaryService", "src\NotaryService\NotaryService.csproj", "{DC32B4DA-E8AC-4B43-8E7D-2DE77CAAAF95}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -108,6 +111,10 @@ Global
{8D2EE375-2E2D-45FE-A4E9-0254D12C7554}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8D2EE375-2E2D-45FE-A4E9-0254D12C7554}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8D2EE375-2E2D-45FE-A4E9-0254D12C7554}.Release|Any CPU.Build.0 = Release|Any CPU
{DC32B4DA-E8AC-4B43-8E7D-2DE77CAAAF95}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DC32B4DA-E8AC-4B43-8E7D-2DE77CAAAF95}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DC32B4DA-E8AC-4B43-8E7D-2DE77CAAAF95}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DC32B4DA-E8AC-4B43-8E7D-2DE77CAAAF95}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -129,6 +136,7 @@ Global
{48CB0583-26CE-48FC-9F53-F7DC6F1727D8} = {97E81C78-1637-481F-9485-DA1225E94C23}
{D167FA6B-D2A3-4D8A-A65D-686DD06650F6} = {97E81C78-1637-481F-9485-DA1225E94C23}
{8D2EE375-2E2D-45FE-A4E9-0254D12C7554} = {59D802AB-C552-422A-B9C3-64D329FBCDCC}
{DC32B4DA-E8AC-4B43-8E7D-2DE77CAAAF95} = {97E81C78-1637-481F-9485-DA1225E94C23}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {61D3ADE6-BBFC-402D-AB42-1C71C9F9EDE3}
Expand Down
105 changes: 105 additions & 0 deletions src/NotaryService/NotaryPlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using Akka.Actor;
using Neo.ConsoleService;
using Neo.Cryptography.ECC;
using Neo.Ledger;
using Neo.Network.P2P;
using Neo.Persistence;
using Neo.Plugins;
using Neo.SmartContract.Native;
using Neo.Wallets;
using System;
using System.Linq;
using Settings = Neo.Plugins.Settings;

namespace Neo.Consensus
{
public class NotaryPlugin : Plugin
{
private NeoSystem System;
private Wallet wallet;
private IWalletProvider walletProvider;
private bool started = false;
private IActorRef notary;

public override string Description => "Notary plugin";

protected override void Configure()
{
Settings.Load(GetConfiguration());
}

protected override void OnSystemLoaded(NeoSystem system)
{
if (system.Settings.Network != Settings.Default.Network) return;
doubiliu marked this conversation as resolved.
Show resolved Hide resolved
System = system;
System.ServiceAdded += NeoSystem_ServiceAdded;
}

private void NeoSystem_ServiceAdded(object sender, object service)
{
if (service is IWalletProvider)
{
walletProvider = service as IWalletProvider;
System.ServiceAdded -= NeoSystem_ServiceAdded;
if (Settings.Default.AutoStart)
{
walletProvider.WalletChanged += WalletProvider_WalletChanged;
}
}
}

private void WalletProvider_WalletChanged(object sender, Wallet wallet)
{
walletProvider.WalletChanged -= WalletProvider_WalletChanged;
Start(wallet);
}

[ConsoleCommand("notary start", Category = "Notary", Description = "Start notary service")]
private void OnStart()
{
Start(walletProvider?.GetWallet());
}

public void Start(Wallet wallet)
{
if (started) return;
if (wallet is null)
{
Console.WriteLine("Please open wallet first!");
return;
}
if (!CheckNotaryAvaiblable(System.StoreView, out ECPoint[] notarynodes))
{
Console.WriteLine("The notary service is unavailable");
return;
}
Console.WriteLine("notarynodes:" + notarynodes[0].ToString());
if (!CheckNotaryAccount(wallet, notarynodes))
{
Console.WriteLine("There is no notary account in wallet");
return;
}
this.wallet = wallet;
notary = System.ActorSystem.ActorOf(NotaryService.Props(System, this.wallet));
System.ActorSystem.EventStream.Subscribe(notary, typeof(Blockchain.PersistCompleted));
System.ActorSystem.EventStream.Subscribe(notary, typeof(Blockchain.RelayResult));
Comment on lines +80 to +81
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move to NotaryService constructor, use

neoSystem.ActorSystem.EventStream.Subscribe(Self, typeof(Blockchain.PersistCompleted));
neoSystem.ActorSystem.EventStream.Subscribe(Self, typeof(Blockchain.RelayResult));

notary.Tell(new NotaryService.Start());
started = true;
Console.WriteLine($"Notary started");
}

private static bool CheckNotaryAvaiblable(DataCache snapshot, out ECPoint[] notarynodes)
doubiliu marked this conversation as resolved.
Show resolved Hide resolved
{
uint height = NativeContract.Ledger.CurrentIndex(snapshot) + 1;
notarynodes = NativeContract.RoleManagement.GetDesignatedByRole(snapshot, Role.Notary, height);
return notarynodes.Length > 0;
}

private static bool CheckNotaryAccount(Wallet wallet, ECPoint[] notarynodes)
{
return notarynodes
.Select(p => wallet.GetAccount(p))
.Any(p => p is not null && p.HasKey && !p.Lock);
}
}
}
Loading