-
Notifications
You must be signed in to change notification settings - Fork 667
Expand file tree
/
Copy pathAuRaBlockProducerTests.cs
More file actions
277 lines (247 loc) · 11.2 KB
/
AuRaBlockProducerTests.cs
File metadata and controls
277 lines (247 loc) · 11.2 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
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Nethermind.Blockchain;
using Nethermind.Config;
using Nethermind.Consensus;
using Nethermind.Consensus.AuRa;
using Nethermind.Consensus.AuRa.Config;
using Nethermind.Consensus.AuRa.Validators;
using Nethermind.Consensus.Processing;
using Nethermind.Consensus.Producers;
using Nethermind.Consensus.Transactions;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Extensions;
using Nethermind.Core.Test.Builders;
using Nethermind.Evm.Tracing;
using Nethermind.Logging;
using Nethermind.Specs;
using Nethermind.Evm.State;
using NSubstitute;
using NSubstitute.ReceivedExtensions;
using NUnit.Framework;
namespace Nethermind.AuRa.Test
{
[Parallelizable(ParallelScope.All)]
public class AuRaBlockProducerTests
{
private class Context
{
public ITxSource TransactionSource { get; }
public IBlockchainProcessor BlockchainProcessor { get; }
public ISealer Sealer { get; }
public IBlockTree BlockTree { get; }
public IBlockProcessingQueue BlockProcessingQueue { get; }
public IWorldState StateProvider { get; }
public ITimestamper Timestamper { get; }
public IAuRaStepCalculator AuRaStepCalculator { get; }
public Address NodeAddress { get; }
public AuRaBlockProducer AuRaBlockProducer { get; private set; }
public IBlockProducerRunner BlockProducerRunner { get; set; }
public TimeSpan StepDelay { get; }
public Context()
{
StepDelay = TimeSpan.FromMilliseconds(20);
TransactionSource = Substitute.For<ITxSource>();
BlockchainProcessor = Substitute.For<IBlockchainProcessor>();
Sealer = Substitute.For<ISealer>();
BlockTree = Substitute.For<IBlockTree>();
BlockProcessingQueue = Substitute.For<IBlockProcessingQueue>();
StateProvider = Substitute.For<IWorldState>();
Timestamper = Substitute.For<ITimestamper>();
AuRaStepCalculator = Substitute.For<IAuRaStepCalculator>();
NodeAddress = TestItem.AddressA;
TransactionSource.GetTransactions(Arg.Any<BlockHeader>(), Arg.Any<long>()).Returns(Array.Empty<Transaction>());
Sealer.CanSeal(Arg.Any<long>(), Arg.Any<Hash256>()).Returns(true);
Sealer.SealBlock(Arg.Any<Block>(), Arg.Any<CancellationToken>()).Returns(c => Task.FromResult(c.Arg<Block>()));
Sealer.Address.Returns(TestItem.AddressA);
BlockProcessingQueue.IsEmpty.Returns(true);
AuRaStepCalculator.TimeToNextStep.Returns(StepDelay);
BlockTree.BestKnownNumber.Returns(1);
BlockTree.Head.Returns(Build.A.Block.WithHeader(Build.A.BlockHeader.WithAura(10, []).TestObject).TestObject);
BlockchainProcessor.Process(Arg.Any<Block>(), ProcessingOptions.ProducingBlock, Arg.Any<IBlockTracer>(), Arg.Any<CancellationToken>()).Returns(returnThis: c =>
{
Block block = c.Arg<Block>();
block.TrySetTransactions(TransactionSource.GetTransactions(BlockTree.Head!.Header, block.GasLimit).ToArray());
return (block, null);
});
StateProvider.HasStateForBlock(Arg.Any<BlockHeader>()).Returns(x => true);
InitProducer();
}
private void InitProducer()
{
AuRaConfig auRaConfig = new();
auRaConfig.ForceSealing = true;
InitProducer(auRaConfig);
}
public void InitProducer(IAuraConfig auraConfig)
{
IBlockProductionTrigger onAuRaSteps = new BuildBlocksOnAuRaSteps(AuRaStepCalculator, LimboLogs.Instance);
IBlockProductionTrigger onlyWhenNotProcessing = new BuildBlocksOnlyWhenNotProcessing(
onAuRaSteps,
BlockProcessingQueue,
BlockTree,
LimboLogs.Instance,
!auraConfig.AllowAuRaPrivateChains);
IBlocksConfig blocksConfig = new BlocksConfig();
FollowOtherMiners gasLimitCalculator = new(MainnetSpecProvider.Instance);
AuRaBlockProducer = new AuRaBlockProducer(
TransactionSource,
BlockchainProcessor,
StateProvider,
Sealer,
BlockTree,
Timestamper,
AuRaStepCalculator,
NullReportingValidator.Instance,
auraConfig,
gasLimitCalculator,
MainnetSpecProvider.Instance,
LimboLogs.Instance,
blocksConfig);
BlockProducerRunner = new StandardBlockProducerRunner(
onlyWhenNotProcessing,
BlockTree,
AuRaBlockProducer);
_ = new
ProducedBlockSuggester(BlockTree, BlockProducerRunner);
}
}
[Test]
public async Task Produces_block()
{
(await StartStop(new Context())).ShouldProduceBlocks(Quantity.AtLeastOne());
}
[Test]
public async Task Can_produce_first_block_when_private_chains_allowed()
{
Context context = new();
context.InitProducer(new AuRaConfig { AllowAuRaPrivateChains = true, ForceSealing = true });
(await StartStop(context, false)).ShouldProduceBlocks(Quantity.AtLeastOne());
}
[Test]
public async Task Cannot_produce_first_block_when_private_chains_not_allowed()
{
(await StartStop(new Context(), false)).ShouldProduceBlocks(Quantity.None());
}
[Test]
public async Task Does_not_produce_block_when_ProcessingQueueEmpty_not_raised()
{
(await StartStop(new Context(), false, true)).ShouldProduceBlocks(Quantity.None());
}
[Test]
public async Task Does_not_produce_block_when_QueueNotEmpty()
{
Context context = new();
context.BlockProcessingQueue.IsEmpty.Returns(false);
(await StartStop(context)).ShouldProduceBlocks(Quantity.None());
}
[Test]
public async Task Does_not_produce_block_when_cannot_seal()
{
Context context = new();
context.Sealer.CanSeal(Arg.Any<long>(), Arg.Any<Hash256>()).Returns(false);
(await StartStop(context)).ShouldProduceBlocks(Quantity.None());
}
[Test]
public async Task Does_not_produce_block_when_ForceSealing_is_false_and_no_transactions()
{
Context context = new();
AuRaConfig auRaConfig = new() { ForceSealing = false };
context.InitProducer(auRaConfig);
(await StartStop(context)).ShouldProduceBlocks(Quantity.None());
}
[Test, Retry(9)]
public async Task Produces_block_when_ForceSealing_is_false_and_there_are_transactions()
{
Context context = new();
AuRaConfig auRaConfig = new() { ForceSealing = false };
context.InitProducer(auRaConfig);
context.TransactionSource.GetTransactions(Arg.Any<BlockHeader>(), Arg.Any<long>()).Returns(new[] { Build.A.Transaction.TestObject });
(await StartStop(context)).ShouldProduceBlocks(Quantity.AtLeastOne());
}
[Test]
public async Task Does_not_produce_block_when_sealing_fails()
{
Context context = new();
context.Sealer.SealBlock(Arg.Any<Block>(), Arg.Any<CancellationToken>()).Returns(static c => Task.FromException(new Exception()));
(await StartStop(context)).ShouldProduceBlocks(Quantity.None());
}
[Test]
public async Task Does_not_produce_block_when_sealing_cancels()
{
Context context = new();
context.Sealer.SealBlock(Arg.Any<Block>(), Arg.Any<CancellationToken>()).Returns(static c => Task.FromCanceled(new CancellationToken(true)));
(await StartStop(context)).ShouldProduceBlocks(Quantity.None());
}
[Test]
public async Task Does_not_produce_block_when_head_is_null()
{
Context context = new();
context.BlockTree.Head.Returns((Block)null);
(await StartStop(context)).ShouldProduceBlocks(Quantity.None());
}
[Test]
public async Task Does_not_produce_block_when_processing_fails()
{
Context context = new();
context.BlockchainProcessor.Process(Arg.Any<Block>(), ProcessingOptions.ProducingBlock, Arg.Any<IBlockTracer>(), Arg.Any<CancellationToken>()).Returns((null, null));
(await StartStop(context)).ShouldProduceBlocks(Quantity.None());
}
[Test, Retry(6)]
public async Task Does_not_produce_block_when_there_is_new_best_suggested_block_not_yet_processed()
{
(await StartStop(new Context(), true, true)).ShouldProduceBlocks(Quantity.None());
}
private async Task<TestResult> StartStop(Context context, bool processingQueueEmpty = true, bool newBestSuggestedBlock = false)
{
AutoResetEvent processedEvent = new(false);
context.BlockTree.SuggestBlock(Arg.Any<Block>(), Arg.Any<BlockTreeSuggestOptions>())
.Returns(AddBlockResult.Added)
.AndDoes(c =>
{
processedEvent.Set();
});
context.BlockProducerRunner.Start();
await processedEvent.WaitOneAsync(context.StepDelay * 20, CancellationToken.None);
context.BlockTree.ClearReceivedCalls();
await Task.Delay(context.StepDelay * 2);
processedEvent.Reset();
try
{
if (processingQueueEmpty)
{
context.BlockProcessingQueue.ProcessingQueueEmpty += Raise.Event();
}
if (newBestSuggestedBlock)
{
context.BlockTree.NewBestSuggestedBlock += Raise.EventWith(new BlockEventArgs(Build.A.Block.TestObject));
context.BlockTree.ClearReceivedCalls();
processedEvent.Reset();
}
await processedEvent.WaitOneAsync(context.StepDelay * 20, CancellationToken.None);
}
finally
{
await context.BlockProducerRunner.StopAsync();
}
return new TestResult(q => context.BlockTree.Received(q).SuggestBlock(Arg.Any<Block>(), Arg.Any<BlockTreeSuggestOptions>()));
}
private class TestResult
{
private readonly Action<Quantity> _assert;
public TestResult(Action<Quantity> assert)
{
_assert = assert;
}
public void ShouldProduceBlocks(Quantity quantity)
{
_assert(quantity);
}
}
}
}