-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[net]refactor: refactor BlockStream (#693)
- Loading branch information
1 parent
2b033a4
commit 433f764
Showing
7 changed files
with
75 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,37 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using EnsureThat; | ||
using Sails.Remoting.Abstractions.Core; | ||
using Substrate.Gear.Api.Generated; | ||
using Substrate.Gear.Api.Generated.Model.gear_core.message.user; | ||
using Substrate.Gear.Api.Generated.Model.gprimitives; | ||
using Substrate.Gear.Client; | ||
using Substrate.Gear.Client.GearApi.Model.gprimitives; | ||
using Substrate.Gear.Client.NetApi.Model.Types.Base; | ||
|
||
namespace Sails.Remoting.Core; | ||
|
||
internal sealed class BlockStreamEventListener : EventListener<(ActorId Source, byte[] Bytes)> | ||
{ | ||
private readonly SubstrateClientExt nodeClient; | ||
private readonly BlocksStream blocksStream; | ||
|
||
internal BlockStreamEventListener(SubstrateClientExt nodeClient, BlocksStream blocksStream) | ||
internal BlockStreamEventListener(BlocksStream blocksStream) | ||
{ | ||
this.nodeClient = nodeClient; | ||
EnsureArg.IsNotNull(blocksStream, nameof(blocksStream)); | ||
|
||
this.blocksStream = blocksStream; | ||
} | ||
|
||
public override IAsyncEnumerable<(ActorId Source, byte[] Bytes)> ReadAllAsync(CancellationToken cancellationToken) | ||
=> this.blocksStream.ReadAllHeadersAsync(cancellationToken) | ||
.SelectGearEvents(this.nodeClient, cancellationToken) | ||
.SelectServiceEvents(); | ||
=> this.blocksStream.ReadAllGearRuntimeEventsAsync(cancellationToken) | ||
.SelectIfMatches( | ||
GearEvent.UserMessageSent, | ||
(UserMessageSentEventData data) => (UserMessage)data.Value[0]) | ||
.Where(userMessage => userMessage.Destination | ||
.IsEqualTo(ActorIdExtensions.Zero)) | ||
.Select(userMessage => (userMessage.Source, userMessage.Payload.Value.Value.Select(@byte => @byte.Value).ToArray())); | ||
|
||
protected override ValueTask DisposeCoreAsync() => this.blocksStream.DisposeAsync(); | ||
protected override ValueTask DisposeCoreAsync() | ||
=> this.blocksStream.DisposeAsync(); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,40 @@ | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using System.Threading; | ||
using Substrate.Gear.Api.Generated; | ||
using Substrate.Gear.Api.Generated.Model.gear_core.message.user; | ||
using Substrate.Gear.Api.Generated.Model.gprimitives; | ||
using EnsureThat; | ||
using Substrate.Gear.Api.Generated.Model.vara_runtime; | ||
using Substrate.Gear.Client.NetApi.Model.Rpc; | ||
using Substrate.Gear.Client.NetApi.Model.Types.Base; | ||
using Substrate.NetApi.Model.Rpc; | ||
using Substrate.NetApi.Model.Types.Base; | ||
|
||
namespace Substrate.Gear.Client; | ||
|
||
public static class BlocksStreamExtensions | ||
{ | ||
[SuppressMessage( | ||
"Style", | ||
"VSTHRD200:Use \"Async\" suffix for async methods", | ||
Justification = "To be consistent with system provided extensions")] | ||
public static IAsyncEnumerable<BaseEnumRust<GearEvent>> SelectGearEvents( | ||
this IAsyncEnumerable<Header> headers, | ||
SubstrateClientExt nodeClient, | ||
CancellationToken cancellationToken = default) | ||
=> headers | ||
.SelectAwait(async blockHeader => await nodeClient | ||
.ListBlockEventsAsync(blockHeader.GetBlockHash(), cancellationToken).ConfigureAwait(false)) | ||
.SelectMany(eventRecords => eventRecords.ToAsyncEnumerable()) | ||
.Select(eventRecord => eventRecord.Event.ToBaseEnumRust()) | ||
.SelectIfMatches( | ||
RuntimeEvent.Gear, | ||
(EnumGearEvent gearEvent) => gearEvent.ToBaseEnumRust() | ||
); | ||
public static IAsyncEnumerable<BaseEnumRust<RuntimeEvent>> ReadAllRuntimeEventsAsync( | ||
this BlocksStream blocksStream, | ||
CancellationToken cancellationToken) | ||
{ | ||
EnsureArg.IsNotNull(blocksStream, nameof(blocksStream)); | ||
|
||
[SuppressMessage( | ||
"Style", | ||
"VSTHRD200:Use \"Async\" suffix for async methods", | ||
Justification = "To be consistent with system provided extensions")] | ||
public static IAsyncEnumerable<(ActorId Source, byte[] Payload)> SelectServiceEvents( | ||
this IAsyncEnumerable<BaseEnumRust<GearEvent>> gearEvents) | ||
=> gearEvents | ||
var eventRecords = blocksStream | ||
.ReadAllAsync( | ||
selectAsync: async (nodeClient, blockHeader) | ||
=> await nodeClient.ListBlockEventsAsync( | ||
blockHeader.GetBlockHash(), | ||
cancellationToken) | ||
.ConfigureAwait(false), | ||
cancellationToken); | ||
return eventRecords.SelectMany(eventRecords => eventRecords.ToAsyncEnumerable()) | ||
.Select(eventRecord => eventRecord.Event.ToBaseEnumRust()); | ||
} | ||
|
||
public static IAsyncEnumerable<BaseEnumRust<GearEvent>> ReadAllGearRuntimeEventsAsync( | ||
this BlocksStream blocksStream, | ||
CancellationToken cancellationToken) | ||
=> blocksStream | ||
.ReadAllRuntimeEventsAsync(cancellationToken) | ||
.SelectIfMatches( | ||
GearEvent.UserMessageSent, | ||
(UserMessageSentEventData data) => (UserMessage)data.Value[0]) | ||
.Where(userMessage => userMessage.Destination | ||
.IsEqualTo(GearApi.Model.gprimitives.ActorIdExtensions.Zero)) | ||
.Select(userMessage => (userMessage.Source, userMessage.Payload.Value.Value.Select(@byte => @byte.Value).ToArray())); | ||
RuntimeEvent.Gear, | ||
(EnumGearEvent gearEvent) => gearEvent.ToBaseEnumRust()); | ||
} |
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