-
Notifications
You must be signed in to change notification settings - Fork 710
Expand file tree
/
Copy pathFullPrunerTests.cs
More file actions
433 lines (372 loc) · 16.4 KB
/
Copy pathFullPrunerTests.cs
File metadata and controls
433 lines (372 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System;
using System.Collections.Generic;
using System.IO.Abstractions;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Nethermind.Blockchain.FullPruning;
using Nethermind.Config;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Extensions;
using Nethermind.Core.Test;
using Nethermind.Core.Test.Builders;
using Nethermind.Db;
using Nethermind.Db.FullPruning;
using Nethermind.Logging;
using Nethermind.State;
using Nethermind.Trie;
using Nethermind.Trie.Pruning;
using NSubstitute;
using NUnit.Framework;
namespace Nethermind.Blockchain.Test.FullPruning;
[TestFixture(0, 1)]
[TestFixture(0, 4)]
[TestFixture(1, 1)]
[TestFixture(1, 4)]
[Parallelizable(ParallelScope.All)]
[FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
public class FullPrunerTests(int fullPrunerMemoryBudgetMb, int degreeOfParallelism)
{
[Test, MaxTime(Timeout.MaxTestTime)]
public async Task can_prune()
{
TestContext test = CreateTest();
await test.RunFullPruning();
test.ShouldCopyAllValues();
}
[MaxTime(Timeout.MaxTestTime * 2)] // this is particular long test
[TestCase(INodeStorage.KeyScheme.Hash, INodeStorage.KeyScheme.Current, INodeStorage.KeyScheme.Hash)]
[TestCase(INodeStorage.KeyScheme.HalfPath, INodeStorage.KeyScheme.Current, INodeStorage.KeyScheme.HalfPath)]
[TestCase(INodeStorage.KeyScheme.Hash, INodeStorage.KeyScheme.HalfPath, INodeStorage.KeyScheme.HalfPath)]
[TestCase(INodeStorage.KeyScheme.HalfPath, INodeStorage.KeyScheme.Hash, INodeStorage.KeyScheme.HalfPath)]
public async Task can_prune_and_switch_key_scheme(INodeStorage.KeyScheme currentKeyScheme, INodeStorage.KeyScheme newKeyScheme, INodeStorage.KeyScheme expectedNewScheme)
{
TestContext test = new(
true,
false,
FullPruningCompletionBehavior.None,
fullPrunerMemoryBudgetMb,
degreeOfParallelism,
currentKeyScheme: currentKeyScheme,
preferredKeyScheme: newKeyScheme);
test.NodeStorage.Scheme.Should().Be(currentKeyScheme);
await test.RunFullPruning();
test.ShouldCopyAllValuesWhenVisitingTrie();
test.NodeStorage.Scheme.Should().Be(expectedNewScheme);
}
[Test, MaxTime(Timeout.MaxTestTime)]
public async Task pruning_deletes_old_db_on_success()
{
TestContext test = CreateTest(clearPrunedDb: true);
await test.RunFullPruning();
test.TrieDb.Count.Should().Be(0);
}
[Test, MaxTime(Timeout.MaxTestTime)]
public async Task pruning_keeps_old_db_on_fail()
{
TestContext test = CreateTest(false);
int count = test.TrieDb.Count;
await test.RunFullPruning();
test.TrieDb.Count.Should().Be(count);
}
[Test, MaxTime(Timeout.MaxTestTime)]
public async Task pruning_deletes_new_db_on_fail()
{
TestContext test = CreateTest(false);
await test.RunFullPruning();
test.CopyDb.Count.Should().Be(0);
}
[Test, MaxTime(Timeout.MaxTestTime)]
public async Task pruning_keeps_new_db_on_success()
{
TestContext test = CreateTest();
int count = test.TrieDb.Count;
await test.RunFullPruning();
test.CopyDb.Count.Should().Be(count);
}
[MaxTime(Timeout.MaxTestTime)]
[TestCase(true, FullPruningCompletionBehavior.None, false)]
[TestCase(true, FullPruningCompletionBehavior.ShutdownOnSuccess, true)]
[TestCase(true, FullPruningCompletionBehavior.AlwaysShutdown, true)]
[TestCase(false, FullPruningCompletionBehavior.None, false)]
[TestCase(false, FullPruningCompletionBehavior.ShutdownOnSuccess, false)]
[TestCase(false, FullPruningCompletionBehavior.AlwaysShutdown, true)]
[Retry(10)]
public async Task pruning_shuts_down_node(bool success, FullPruningCompletionBehavior behavior, bool expectedShutdown)
{
TestContext test = CreateTest(successfulPruning: success, completionBehavior: behavior);
await test.RunFullPruning();
if (expectedShutdown)
{
test.ProcessExitSource.Received(1).Exit(ExitCodes.Ok);
}
else
{
test.ProcessExitSource.DidNotReceiveWithAnyArgs().Exit(ExitCodes.Ok);
}
}
[Test, MaxTime(Timeout.MaxTestTime)]
public async Task can_not_start_pruning_when_other_is_in_progress()
{
TestContext test = CreateTest();
test.FullPruningDb.CanStartPruning.Should().BeTrue();
test.TriggerPruningViaEvent();
TestFullPruningDb.TestPruningContext pruningContext = await test.WaitForPruningStart();
test.FullPruningDb.CanStartPruning.Should().BeFalse();
await test.WaitForPruningEnd(pruningContext);
test.FullPruningDb.CanStartPruning.Should().BeTrue();
}
[Test, MaxTime(Timeout.MaxTestTime)]
public async Task should_not_start_multiple_pruning()
{
TestContext test = CreateTest();
test.TriggerPruningViaEvent();
TestFullPruningDb.TestPruningContext ctx = await test.WaitForPruningStart();
test.TriggerPruningViaEvent();
await test.WaitForPruningEnd(ctx);
test.FullPruningDb.PruningStarted.Should().Be(1);
}
[Test, MaxTime(Timeout.MaxTestTime)]
public async Task should_duplicate_writes_while_pruning()
{
TestContext test = CreateTest();
TestFullPruningDb.TestPruningContext ctx = await test.WaitForPruningStart();
byte[] key = { 1, 2, 3 };
test.FullPruningDb[key] = key;
test.FullPruningDb.Context.WaitForFinish.Set();
await test.WaitForPruningEnd(ctx);
test.FullPruningDb[key].Should().BeEquivalentTo(key);
}
[Test, MaxTime(Timeout.MaxTestTime)]
public async Task should_duplicate_writes_to_batches_while_pruning()
{
TestContext test = CreateTest();
byte[] key = { 0, 1, 2 };
TestFullPruningDb.TestPruningContext context = await test.WaitForPruningStart();
using (IWriteBatch writeBatch = test.FullPruningDb.StartWriteBatch())
{
writeBatch[key] = key;
}
await test.WaitForPruningEnd(context);
test.FullPruningDb[key].Should().BeEquivalentTo(key);
}
private TestContext CreateTest(
bool successfulPruning = true,
bool clearPrunedDb = false,
FullPruningCompletionBehavior completionBehavior = FullPruningCompletionBehavior.None) =>
new(
successfulPruning,
clearPrunedDb,
completionBehavior,
fullPrunerMemoryBudgetMb,
degreeOfParallelism);
private class TestContext
{
private readonly bool _clearPrunedDb;
private readonly Hash256 _stateRoot;
private long _head;
public TestFullPruningDb FullPruningDb { get; }
public IPruningTrigger PruningTrigger { get; } = Substitute.For<IPruningTrigger>();
public IBlockTree BlockTree { get; } = Substitute.For<IBlockTree>();
public IStateReader StateReader { get; }
public FullPruner Pruner { get; }
public MemDb TrieDb { get; }
public INodeStorage NodeStorage { get; }
public TestMemDb CopyDb { get; }
public IDriveInfo DriveInfo { get; set; } = Substitute.For<IDriveInfo>();
public IChainEstimations _chainEstimations = ChainSizes.UnknownChain.Instance;
public IProcessExitSource ProcessExitSource { get; } = Substitute.For<IProcessExitSource>();
public TestContext(
bool successfulPruning,
bool clearPrunedDb = false,
FullPruningCompletionBehavior completionBehavior = FullPruningCompletionBehavior.None,
int fullScanMemoryBudgetMb = 0,
int degreeOfParallelism = 0,
INodeStorage.KeyScheme currentKeyScheme = INodeStorage.KeyScheme.HalfPath,
INodeStorage.KeyScheme preferredKeyScheme = INodeStorage.KeyScheme.Current)
{
BlockTree.OnUpdateMainChain += (_, e) => _head = e.Blocks[^1].Number;
_clearPrunedDb = clearPrunedDb;
TrieDb = new TestMemDb();
CopyDb = new TestMemDb();
IDbFactory dbFactory = Substitute.For<IDbFactory>();
dbFactory.CreateDb(Arg.Any<DbSettings>()).Returns(TrieDb, CopyDb);
NodeStorage storageForWrite = new(TrieDb, currentKeyScheme);
PatriciaTree trie = Build.A.Trie(storageForWrite).WithAccountsByIndex(0, 100).TestObject;
_stateRoot = trie.RootHash;
FullPruningDb = new TestFullPruningDb(new DbSettings("test", "test"), dbFactory, successfulPruning, clearPrunedDb);
NodeStorageFactory nodeStorageFactory = new(preferredKeyScheme, LimboLogs.Instance);
nodeStorageFactory.DetectCurrentKeySchemeFrom(TrieDb);
NodeStorage = nodeStorageFactory.WrapKeyValueStore(FullPruningDb);
TestRawTrieStore trieStore = TestTrieStoreFactory.Build(NodeStorage, LimboLogs.Instance);
StateReader = new StateReader(trieStore, new TestMemDb(), LimboLogs.Instance);
Pruner = new(
FullPruningDb,
nodeStorageFactory,
NodeStorage,
PruningTrigger,
new PruningConfig()
{
FullPruningMaxDegreeOfParallelism = degreeOfParallelism,
FullPruningMemoryBudgetMb = fullScanMemoryBudgetMb,
FullPruningCompletionBehavior = completionBehavior
},
BlockTree,
Substitute.For<IStateBoundary>(),
StateReader,
ProcessExitSource,
_chainEstimations,
DriveInfo,
trieStore,
LimboLogs.Instance);
}
public async Task RunFullPruning()
{
TestFullPruningDb.TestPruningContext ctx = await WaitForPruningStart();
await WaitForPruningEnd(ctx);
}
public void TriggerPruningViaEvent() =>
PruningTrigger.Prune += Raise.Event<EventHandler<PruningTriggerEventArgs>>();
public async Task<bool> WaitForPruningEnd(TestFullPruningDb.TestPruningContext context)
{
while (!await context.WaitForFinish.WaitOneAsync(TimeSpan.FromMilliseconds(1), CancellationToken.None))
{
AddBlocks(1);
}
AddBlocks(1);
return await context.DisposeEvent.WaitOneAsync(TimeSpan.FromMilliseconds(Timeout.MaxWaitTime * 5), CancellationToken.None);
}
public async Task<TestFullPruningDb.TestPruningContext> WaitForPruningStart()
{
TriggerPruningViaEvent();
using CancellationTokenSource cts = new();
Task addBlockTasks = Task.Run(() =>
{
while (!cts.IsCancellationRequested)
{
AddBlocks(1);
}
});
try
{
Assert.That(() => FullPruningDb.Context, Is.Not.Null.After(Timeout.MaxTestTime, 1));
}
finally
{
await cts.CancelAsync();
await addBlockTasks;
}
TestFullPruningDb.TestPruningContext context = FullPruningDb.Context;
return context;
}
public void AddBlocks(long count)
{
for (int i = 0; i < count; i++)
{
long number = _head + 1;
BlockTree.BestPersistedState.Returns(_head);
Block head = Build.A.Block.WithStateRoot(_stateRoot).WithNumber(number).TestObject;
BlockTree.Head.Returns(head);
BlockTree.FindHeader(number).Returns(head.Header);
BlockTree.OnUpdateMainChain += Raise.EventWith(new OnUpdateMainChainArgs(new List<Block>() { head }, true));
Thread.Sleep(1); // Need to add a little sleep as the wait for event in full pruner is async.
}
}
public void ShouldCopyAllValuesWhenVisitingTrie()
{
PatriciaTree trie = new(new RawScopedTrieStore(new NodeStorage(TrieDb)), LimboLogs.Instance);
TrieCopiedNodeVisitor visitor = new(new NodeStorage(CopyDb));
trie.Accept(visitor, BlockTree.Head!.StateRoot!);
}
public void ShouldCopyAllValues()
{
foreach (KeyValuePair<byte[], byte[]?> keyValuePair in TrieDb.GetAll())
{
CopyDb[keyValuePair.Key].Should().BeEquivalentTo(keyValuePair.Value);
CopyDb.KeyWasWrittenWithFlags(keyValuePair.Key, WriteFlags.LowPriority | WriteFlags.DisableWAL);
}
}
}
private class TestFullPruningDb(DbSettings settings, IDbFactory dbFactory, bool successfulPruning, bool clearPrunedDb = false) : FullPruningDb(settings, dbFactory)
{
private readonly bool _successfulPruning = successfulPruning;
private readonly bool _clearPrunedDb = clearPrunedDb;
public TestPruningContext Context { get; set; } = null!;
public new int PruningStarted { get; private set; }
public ManualResetEvent WaitForClearDb { get; } = new(false);
protected override void ClearOldDb(IDb oldDb)
{
if (_clearPrunedDb)
{
base.ClearOldDb(oldDb);
WaitForClearDb.Set();
}
}
public override bool TryStartPruning(bool duplicateReads, out IPruningContext context)
{
if (base.TryStartPruning(duplicateReads, out context))
{
context = Context = new TestPruningContext(context, _successfulPruning);
PruningStarted++;
return true;
}
return false;
}
internal class TestPruningContext(IPruningContext context, bool successfulPruning) : IPruningContext
{
private readonly IPruningContext _context = context;
private readonly bool _successfulPruning = successfulPruning;
public ManualResetEvent DisposeEvent { get; } = new(false);
public ManualResetEvent WaitForFinish { get; } = new(false);
public void Dispose()
{
_context.Dispose();
DisposeEvent.Set();
CancellationTokenSource.Dispose();
}
public byte[]? this[ReadOnlySpan<byte> key]
{
get => _context[key];
set => _context[key] = value;
}
public IWriteBatch StartWriteBatch() => _context.StartWriteBatch();
public void Set(ReadOnlySpan<byte> key, byte[]? value, WriteFlags flags = WriteFlags.None) => _context.Set(key, value, flags);
public byte[]? Get(ReadOnlySpan<byte> key, ReadFlags flags = ReadFlags.None) => _context.Get(key, flags);
public void Commit()
{
WaitForFinish.Set();
if (_successfulPruning)
{
_context.Commit();
}
}
public void MarkStart() => _context.MarkStart();
public CancellationTokenSource CancellationTokenSource { get; } = new();
}
}
class TrieCopiedNodeVisitor(INodeStorage nodeStorage) : ITreeVisitor<TreePathContextWithStorage>
{
private readonly INodeStorage _nodeStorageToCompareTo = nodeStorage;
private void CheckNode(Hash256? storage, in TreePath path, TrieNode node) =>
_nodeStorageToCompareTo.KeyExists(storage, path, node.Keccak).Should().BeTrue();
public bool IsFullDbScan => true;
public bool ShouldVisit(in TreePathContextWithStorage ctx, in ValueHash256 nextNode) => true;
public void VisitTree(in TreePathContextWithStorage ctx, in ValueHash256 rootHash)
{
}
public void VisitMissingNode(in TreePathContextWithStorage ctx, in ValueHash256 nodeHash)
{
}
public void VisitBranch(in TreePathContextWithStorage ctx, TrieNode node) =>
CheckNode(ctx.Storage, ctx.Path, node);
public void VisitExtension(in TreePathContextWithStorage ctx, TrieNode node) =>
CheckNode(ctx.Storage, ctx.Path, node);
public void VisitLeaf(in TreePathContextWithStorage ctx, TrieNode node) => CheckNode(ctx.Storage, ctx.Path, node);
public void VisitAccount(in TreePathContextWithStorage ctx, TrieNode node, in AccountStruct account)
{
}
}
}