Skip to content

Commit

Permalink
Merge pull request #2569 from greymistcube/refactor/remove-preload-state
Browse files Browse the repository at this point in the history
Removed preload status tracking
  • Loading branch information
greymistcube authored Sep 6, 2024
2 parents d88595a + f91e615 commit a8afbc2
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 121 deletions.
3 changes: 0 additions & 3 deletions Libplanet.Headless/Hosting/LibplanetNodeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,6 @@ Task BootstrapSwarmAsync(int depth)
dialTimeout: null,
cancellationToken: cancellationToken);

// We assume the first phase of preloading is BlockHashDownloadState...
((IProgress<BlockSyncState>)PreloadProgress)?.Report(new BlockHashDownloadState());

if (peers.Any())
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,97 +126,6 @@ public async Task SubscribeTx()
return (block, transactions);
}

[Fact]
public async Task SubscribePreloadProgress()
{
var cts = new CancellationTokenSource();

var apvPrivateKey = new PrivateKey();
var apv = AppProtocolVersion.Sign(apvPrivateKey, 0);
var genesisBlock = BlockChain.ProposeGenesisBlock(
transactions: new IAction[]
{
new Initialize(
new ValidatorSet(
new[]
{
new Validator(ProposerPrivateKey.PublicKey, BigInteger.One),
new Validator(apvPrivateKey.PublicKey, BigInteger.One)
}
.ToList()),
states: ImmutableDictionary.Create<Address, IValue>())
}.Select((sa, nonce) => Transaction.Create(nonce, new PrivateKey(), null, new[] { sa.PlainValue }))
.ToImmutableList(),
privateKey: new PrivateKey());
var validators = new List<PrivateKey>
{
ProposerPrivateKey, apvPrivateKey
}.OrderBy(x => x.Address).ToList();

// 에러로 인하여 NineChroniclesNodeService 를 사용할 수 없습니다. https://git.io/JfS0M
// 따라서 LibplanetNodeService로 비슷한 환경을 맞춥니다.
// 1. 노드를 생성합니다.
var seedNode = CreateLibplanetNodeService(genesisBlock, apv, apvPrivateKey.PublicKey);
await StartAsync(seedNode.Swarm, cts.Token);

// 2. Progress를 넘겨 preloadProgress subscription 과 연결합니다.
var service = CreateLibplanetNodeService(
genesisBlock,
apv,
apvPrivateKey.PublicKey,
new Progress<BlockSyncState>(state =>
{
StandaloneContextFx.PreloadStateSubject.OnNext(state);
}),
new[] { seedNode.Swarm.AsPeer });

Block block = seedNode.BlockChain.ProposeBlock(ProposerPrivateKey);
seedNode.BlockChain.Append(block, GenerateBlockCommit(block.Index, block.Hash, validators));
var result = await ExecuteSubscriptionQueryAsync("subscription { preloadProgress { currentPhase totalPhase extra { type currentCount totalCount } } }");
Assert.IsType<SubscriptionExecutionResult>(result);

_ = service.StartAsync(cts.Token);

await service.PreloadEnded.WaitAsync(cts.Token);

var subscribeResult = (SubscriptionExecutionResult)result;
var stream = subscribeResult.Streams!.Values.FirstOrDefault();

// BlockHashDownloadState : 2
// BlockDownloadState : 1
// BlockVerificationState : 1
// ActionExecutionState : 1
var preloadProgressRecords =
new List<(long currentPhase, long totalPhase, string type, long currentCount, long totalCount)>();
var expectedPreloadProgress = new[]
{
(1L, 5L, "BlockHashDownloadState", 0L, 0L),
(1L, 5L, "BlockHashDownloadState", 1L, 1L),
(2L, 5L, "BlockDownloadState", 1L, 1L),
(3L, 5L, "BlockVerificationState", 1L, 1L),
(5L, 5L, "ActionExecutionState", 1L, 1L),
}.ToImmutableHashSet();
foreach (var index in Enumerable.Range(1, expectedPreloadProgress.Count()))
{
var rawEvents = await stream.Take(index);
var events = (Dictionary<string, object>)((ExecutionNode)rawEvents.Data!).ToValue()!;
var preloadProgress = (Dictionary<string, object>)events["preloadProgress"];
var preloadProgressExtra = (Dictionary<string, object>)preloadProgress["extra"];

preloadProgressRecords.Add((
(long)preloadProgress["currentPhase"],
(long)preloadProgress["totalPhase"],
(string)preloadProgressExtra["type"],
(long)preloadProgressExtra["currentCount"],
(long)preloadProgressExtra["totalCount"]));
}

Assert.Equal(expectedPreloadProgress, preloadProgressRecords.ToImmutableHashSet());

await seedNode.StopAsync(cts.Token);
await service.StopAsync(cts.Token);
}

[Fact(Timeout = 25000)]
public async Task SubscribeDifferentAppProtocolVersionEncounter()
{
Expand Down
32 changes: 5 additions & 27 deletions NineChronicles.Headless/GraphTypes/StandaloneSubscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,34 +90,11 @@ public PreloadStateExtraType()

public PreloadStateType()
{
Field<NonNullGraphType<LongGraphType>>(name: "currentPhase", resolve: context => context.Source.CurrentPhase);
Field<NonNullGraphType<LongGraphType>>(name: "totalPhase", resolve: context => BlockSyncState.TotalPhase);
// Dummy report.
Field<NonNullGraphType<LongGraphType>>(name: "currentPhase", resolve: context => 0);
Field<NonNullGraphType<LongGraphType>>(name: "totalPhase", resolve: context => 0);
Field<NonNullGraphType<PreloadStateExtraType>>(name: "extra", resolve: context =>
{
var preloadState = context.Source;
return preloadState switch
{
ActionExecutionState actionExecutionState => new PreloadStateExtra(nameof(ActionExecutionState),
actionExecutionState.ExecutedBlockCount,
actionExecutionState.TotalBlockCount),
BlockDownloadState blockDownloadState => new PreloadStateExtra(nameof(BlockDownloadState),
blockDownloadState.ReceivedBlockCount,
blockDownloadState.TotalBlockCount),
BlockHashDownloadState blockHashDownloadState => new PreloadStateExtra(
nameof(BlockHashDownloadState),
blockHashDownloadState.ReceivedBlockHashCount,
blockHashDownloadState.EstimatedTotalBlockHashCount),
BlockVerificationState blockVerificationState => new PreloadStateExtra(
nameof(BlockVerificationState),
blockVerificationState.VerifiedBlockCount,
blockVerificationState.TotalBlockCount),
StateDownloadState stateDownloadState => new PreloadStateExtra(
nameof(StateDownloadState),
stateDownloadState.ReceivedIterationCount,
stateDownloadState.TotalIterationCount),
_ => throw new ExecutionError($"Not supported preload state. {preloadState.GetType()}"),
};
});
new PreloadStateExtra(nameof(BlockSyncState), 0, 0));
}
}

Expand Down Expand Up @@ -166,6 +143,7 @@ public StandaloneSubscription(StandaloneContext standaloneContext, IConfiguratio
AddField(new EventStreamFieldType
{
Name = "preloadProgress",
DeprecationReason = "Since Libplanet 5.3.0 preload progress is no longer reported.",
Type = typeof(PreloadStateType),
Resolver = new FuncFieldResolver<BlockSyncState>(context => (context.Source as BlockSyncState)!),
Subscriber = new EventStreamResolver<BlockSyncState>(context => StandaloneContext.PreloadStateSubject.AsObservable()),
Expand Down

0 comments on commit a8afbc2

Please sign in to comment.