-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from s2quake/feature/evidence
Add Evidence module
- Loading branch information
Showing
25 changed files
with
727 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/console/LibplanetConsole.Consoles.Evidence/Commands/EvidenceCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System.ComponentModel; | ||
using System.ComponentModel.Composition; | ||
using JSSoft.Commands; | ||
using LibplanetConsole.Common.Extensions; | ||
|
||
namespace LibplanetConsole.Consoles.Evidence.Commands; | ||
|
||
[Export(typeof(ICommand))] | ||
[CommandSummary("Provides evidence-related commands.")] | ||
[Category("Evidence")] | ||
[method: ImportingConstructor] | ||
internal sealed class EvidenceCommand(INodeCollection nodes) | ||
: CommandMethodBase | ||
{ | ||
[CommandMethod] | ||
public async Task NewAsync( | ||
string nodeAddress = "", CancellationToken cancellationToken = default) | ||
{ | ||
var node = nodes.Current ?? throw new InvalidOperationException("No node is selected."); | ||
var evidenceContent = node.GetService<IEvidenceContent>(); | ||
var evidenceInfo = await evidenceContent.AddEvidenceAsync(cancellationToken); | ||
await Out.WriteLineAsJsonAsync(evidenceInfo); | ||
} | ||
|
||
[CommandMethod] | ||
public async Task RaiseAsync( | ||
CancellationToken cancellationToken = default) | ||
{ | ||
var node = nodes.Current ?? throw new InvalidOperationException("No node is selected."); | ||
var evidenceContent = node.GetService<IEvidenceContent>(); | ||
await evidenceContent.ViolateAsync(cancellationToken); | ||
} | ||
|
||
[CommandMethod] | ||
public async Task ListAsync(long height = -1, CancellationToken cancellationToken = default) | ||
{ | ||
var node = nodes.Current ?? throw new InvalidOperationException("No node is selected."); | ||
var evidenceContent = node.GetService<IEvidenceContent>(); | ||
var evidenceInfos = await evidenceContent.GetEvidenceAsync(height, cancellationToken); | ||
await Out.WriteLineAsJsonAsync(evidenceInfos); | ||
} | ||
|
||
#if LIBPLANET_DPOS | ||
[CommandMethod] | ||
public async Task UnjailAsync( | ||
CancellationToken cancellationToken = default) | ||
{ | ||
var node = nodes.Current ?? throw new InvalidOperationException("No node is selected."); | ||
var evidenceContent = node.GetService<IEvidenceContent>(); | ||
await evidenceContent.UnjailAsync(cancellationToken); | ||
} | ||
#endif // LIBPLANET_DPOS | ||
} |
42 changes: 42 additions & 0 deletions
42
src/console/LibplanetConsole.Consoles.Evidence/EvidenceNodeContent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.ComponentModel.Composition; | ||
using LibplanetConsole.Common.Services; | ||
using LibplanetConsole.Consoles.Services; | ||
using LibplanetConsole.Evidence.Serializations; | ||
using LibplanetConsole.Evidence.Services; | ||
|
||
namespace LibplanetConsole.Consoles.Evidence; | ||
|
||
[Export(typeof(INodeContent))] | ||
[Export(typeof(IEvidenceContent))] | ||
[Export(typeof(INodeContentService))] | ||
[method: ImportingConstructor] | ||
internal sealed class EvidenceNodeContent(INode node) | ||
: INodeContent, IEvidenceContent, INodeContentService | ||
{ | ||
private readonly RemoteService<IEvidenceService> _evidenceService = new(); | ||
|
||
INode INodeContent.Node => node; | ||
|
||
string INodeContent.Name => "evidence"; | ||
|
||
IRemoteService INodeContentService.RemoteService => _evidenceService; | ||
|
||
private IEvidenceService Service => _evidenceService.Service; | ||
|
||
public Task<EvidenceInfo> AddEvidenceAsync(CancellationToken cancellationToken) | ||
=> Service.AddEvidenceAsync(cancellationToken); | ||
|
||
public Task<EvidenceInfo[]> GetEvidenceAsync(long height, CancellationToken cancellationToken) | ||
=> Service.GetEvidenceAsync(height, cancellationToken); | ||
|
||
public Task ViolateAsync(CancellationToken cancellationToken) | ||
=> Service.ViolateAsync(cancellationToken); | ||
|
||
#if LIBPLANET_DPOS | ||
public Task UnjailAsync(CancellationToken cancellationToken) | ||
{ | ||
var signature = node.Sign(true); | ||
return Service.UnjailAsync(signature, cancellationToken); | ||
} | ||
#endif // LIBPLANET_DPOS | ||
} |
16 changes: 16 additions & 0 deletions
16
src/console/LibplanetConsole.Consoles.Evidence/IEvidenceContent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using LibplanetConsole.Evidence.Serializations; | ||
|
||
namespace LibplanetConsole.Consoles.Evidence; | ||
|
||
internal interface IEvidenceContent | ||
{ | ||
Task<EvidenceInfo> AddEvidenceAsync(CancellationToken cancellationToken); | ||
|
||
Task<EvidenceInfo[]> GetEvidenceAsync(long height, CancellationToken cancellationToken); | ||
|
||
Task ViolateAsync(CancellationToken cancellationToken); | ||
|
||
#if LIBPLANET_DPOS | ||
Task UnjailAsync(CancellationToken cancellationToken); | ||
#endif // LIBPLANET_DPOS | ||
} |
11 changes: 11 additions & 0 deletions
11
src/console/LibplanetConsole.Consoles.Evidence/LibplanetConsole.Consoles.Evidence.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\LibplanetConsole.Consoles\LibplanetConsole.Consoles.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="$(MSBuildThisFileDirectory)..\..\shared\LibplanetConsole.Evidence\**\*.cs" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/node/LibplanetConsole.Nodes.Evidence/Commands/EvidenceCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System.ComponentModel; | ||
using System.ComponentModel.Composition; | ||
using JSSoft.Commands; | ||
using LibplanetConsole.Common.Extensions; | ||
|
||
namespace LibplanetConsole.Nodes.Evidence.Commands; | ||
|
||
[Export(typeof(ICommand))] | ||
[CommandSummary("Provides evidence-related commands.")] | ||
[Category("Evidence")] | ||
[method: ImportingConstructor] | ||
internal sealed class EvidenceCommand(INode node, IEvidenceNode evidenceNode) | ||
: CommandMethodBase | ||
{ | ||
public override bool IsEnabled => node.IsRunning is true; | ||
|
||
[CommandMethod] | ||
[CommandSummary("Adds a new evidence.")] | ||
public async Task NewAsync(CancellationToken cancellationToken) | ||
{ | ||
var evidenceInfo = await evidenceNode.AddEvidenceAsync(cancellationToken); | ||
await Out.WriteLineAsJsonAsync(evidenceInfo); | ||
} | ||
|
||
[CommandMethod] | ||
[CommandSummary("Raises a infraction.")] | ||
public async Task RaiseAsync(CancellationToken cancellationToken) | ||
{ | ||
await evidenceNode.ViolateAsync(cancellationToken); | ||
} | ||
|
||
[CommandMethod] | ||
[CommandMethodStaticProperty(typeof(ListProperties))] | ||
[CommandSummary("Gets the evidence list.")] | ||
public async Task ListAsync(CancellationToken cancellationToken = default) | ||
{ | ||
var height = ListProperties.Height; | ||
var isPending = ListProperties.IsPending; | ||
var evidenceInfos = isPending == true ? | ||
await evidenceNode.GetPendingEvidenceAsync(cancellationToken) : | ||
await evidenceNode.GetEvidenceAsync(height, cancellationToken); | ||
await Out.WriteLineAsJsonAsync(evidenceInfos); | ||
} | ||
|
||
[CommandMethod] | ||
[CommandMethodStaticProperty(typeof(GetProperties))] | ||
[CommandSummary("Gets the evidence.")] | ||
public async Task GetAsync(string evidenceId, CancellationToken cancellationToken) | ||
{ | ||
var isPending = GetProperties.IsPending; | ||
var evidenceInfo = isPending == true ? | ||
await evidenceNode.GetPendingEvidenceAsync(evidenceId, cancellationToken) : | ||
await evidenceNode.GetEvidenceAsync(evidenceId, cancellationToken); | ||
await Out.WriteLineAsJsonAsync(evidenceInfo); | ||
} | ||
|
||
#if LIBPLANET_DPOS | ||
[CommandMethod] | ||
public async Task UnjailAsync(CancellationToken cancellationToken) | ||
{ | ||
await evidenceNode.UnjailAsync(cancellationToken); | ||
} | ||
#endif // LIBPLANET_DPOS | ||
|
||
public static class ListProperties | ||
{ | ||
[CommandPropertyRequired(DefaultValue = -1)] | ||
[CommandSummary("The height of the block to get the evidence. default is the tip.")] | ||
public static long Height { get; set; } | ||
|
||
[CommandPropertySwitch("pending", 'p')] | ||
[CommandPropertyCondition(nameof(Height), -1, IsNot = true)] | ||
[CommandSummary("Indicates whether to get pending evidence. " + | ||
"if true, the height is ignored.")] | ||
public static bool IsPending { get; set; } | ||
} | ||
|
||
public static class GetProperties | ||
{ | ||
[CommandPropertySwitch("pending", 'p')] | ||
[CommandSummary("Indicates whether to get pending evidence.")] | ||
public static bool IsPending { get; set; } | ||
} | ||
} |
Oops, something went wrong.