9
9
using System . Threading ;
10
10
using System . Threading . Tasks ;
11
11
using Autofac ;
12
+ using FluentAssertions ;
12
13
using Nethermind . Api ;
13
14
using Nethermind . Blockchain ;
14
15
using Nethermind . Blockchain . Find ;
@@ -103,8 +104,8 @@ protected TestBlockchain()
103
104
protected AutoCancelTokenSource _cts ;
104
105
public CancellationToken CancellationToken => _cts . Token ;
105
106
106
- private TestBlockchainUtil TestUtil => _fromContainer . TestBlockchainUtil . Value ;
107
- private PoWTestBlockchainUtil PoWTestUtil => _fromContainer . PoWTestBlockchainUtil . Value ;
107
+ private TestBlockchainUtil TestUtil => _fromContainer . TestBlockchainUtil ;
108
+ private PoWTestBlockchainUtil PoWTestUtil => _fromContainer . PoWTestBlockchainUtil ;
108
109
private IManualBlockProductionTrigger BlockProductionTrigger => _fromContainer . BlockProductionTrigger ;
109
110
110
111
public ManualTimestamper Timestamper => _fromContainer . ManualTimestamper ;
@@ -126,37 +127,65 @@ protected TestBlockchain()
126
127
127
128
// Resolving all these component at once is faster.
128
129
private FromContainer _fromContainer = null ! ;
129
- private record FromContainer (
130
- IStateReader StateReader ,
131
- IEthereumEcdsa EthereumEcdsa ,
132
- INonceManager NonceManager ,
133
- IReceiptStorage ReceiptStorage ,
134
- ITxPool TxPool ,
135
- IWorldStateManager WorldStateManager ,
136
- IBlockPreprocessorStep BlockPreprocessorStep ,
137
- IBlockTree BlockTree ,
138
- IBlockFinder BlockFinder ,
139
- ILogFinder LogFinder ,
140
- IChainHeadInfoProvider ChainHeadInfoProvider ,
141
- IDbProvider DbProvider ,
142
- ISpecProvider SpecProvider ,
143
- ISealEngine SealEngine ,
144
- ITransactionComparerProvider TransactionComparerProvider ,
145
- IPoSSwitcher PoSSwitcher ,
146
- IChainLevelInfoRepository ChainLevelInfoRepository ,
147
- IMainProcessingContext MainProcessingContext ,
148
- IReadOnlyTxProcessingEnvFactory ReadOnlyTxProcessingEnvFactory ,
149
- IBlockProducerEnvFactory BlockProducerEnvFactory ,
150
- Configuration Configuration ,
151
- Lazy < TestBlockchainUtil > TestBlockchainUtil ,
152
- Lazy < PoWTestBlockchainUtil > PoWTestBlockchainUtil ,
153
- ManualTimestamper ManualTimestamper ,
154
- IManualBlockProductionTrigger BlockProductionTrigger ,
155
- IShareableTxProcessorSource ShareableTxProcessorSource ,
156
- ISealer Sealer ,
157
- IForkInfo ForkInfo
130
+ public class FromContainer (
131
+ Lazy < IStateReader > stateReader ,
132
+ Lazy < IEthereumEcdsa > ethereumEcdsa ,
133
+ Lazy < INonceManager > nonceManager ,
134
+ Lazy < IReceiptStorage > receiptStorage ,
135
+ Lazy < ITxPool > txPool ,
136
+ Lazy < IWorldStateManager > worldStateManager ,
137
+ Lazy < IBlockPreprocessorStep > blockPreprocessorStep ,
138
+ Lazy < IBlockTree > blockTree ,
139
+ Lazy < IBlockFinder > blockFinder ,
140
+ Lazy < ILogFinder > logFinder ,
141
+ Lazy < IChainHeadInfoProvider > chainHeadInfoProvider ,
142
+ Lazy < IDbProvider > dbProvider ,
143
+ Lazy < ISpecProvider > specProvider ,
144
+ Lazy < ISealEngine > sealEngine ,
145
+ Lazy < ITransactionComparerProvider > transactionComparerProvider ,
146
+ Lazy < IPoSSwitcher > poSSwitcher ,
147
+ Lazy < IChainLevelInfoRepository > chainLevelInfoRepository ,
148
+ Lazy < IMainProcessingContext > mainProcessingContext ,
149
+ Lazy < IReadOnlyTxProcessingEnvFactory > readOnlyTxProcessingEnvFactory ,
150
+ Lazy < IBlockProducerEnvFactory > blockProducerEnvFactory ,
151
+ Lazy < Configuration > configuration ,
152
+ Lazy < TestBlockchainUtil > testBlockchainUtil ,
153
+ Lazy < PoWTestBlockchainUtil > poWTestBlockchainUtil ,
154
+ Lazy < ManualTimestamper > manualTimestamper ,
155
+ Lazy < IManualBlockProductionTrigger > blockProductionTrigger ,
156
+ Lazy < IShareableTxProcessorSource > shareableTxProcessorSource ,
157
+ Lazy < ISealer > sealer ,
158
+ Lazy < IForkInfo > forkInfo
158
159
)
159
160
{
161
+ public IStateReader StateReader => stateReader . Value ;
162
+ public IEthereumEcdsa EthereumEcdsa => ethereumEcdsa . Value ;
163
+ public INonceManager NonceManager => nonceManager . Value ;
164
+ public IReceiptStorage ReceiptStorage => receiptStorage . Value ;
165
+ public ITxPool TxPool => txPool . Value ;
166
+ public IWorldStateManager WorldStateManager => worldStateManager . Value ;
167
+ public IBlockPreprocessorStep BlockPreprocessorStep => blockPreprocessorStep . Value ;
168
+ public IBlockTree BlockTree => blockTree . Value ;
169
+ public IBlockFinder BlockFinder => blockFinder . Value ;
170
+ public ILogFinder LogFinder => logFinder . Value ;
171
+ public IChainHeadInfoProvider ChainHeadInfoProvider => chainHeadInfoProvider . Value ;
172
+ public IDbProvider DbProvider => dbProvider . Value ;
173
+ public ISpecProvider SpecProvider => specProvider . Value ;
174
+ public ISealEngine SealEngine => sealEngine . Value ;
175
+ public ITransactionComparerProvider TransactionComparerProvider => transactionComparerProvider . Value ;
176
+ public IPoSSwitcher PoSSwitcher => poSSwitcher . Value ;
177
+ public IChainLevelInfoRepository ChainLevelInfoRepository => chainLevelInfoRepository . Value ;
178
+ public IMainProcessingContext MainProcessingContext => mainProcessingContext . Value ;
179
+ public IReadOnlyTxProcessingEnvFactory ReadOnlyTxProcessingEnvFactory => readOnlyTxProcessingEnvFactory . Value ;
180
+ public IBlockProducerEnvFactory BlockProducerEnvFactory => blockProducerEnvFactory . Value ;
181
+ public Configuration Configuration => configuration . Value ;
182
+ public TestBlockchainUtil TestBlockchainUtil => testBlockchainUtil . Value ;
183
+ public PoWTestBlockchainUtil PoWTestBlockchainUtil => poWTestBlockchainUtil . Value ;
184
+ public ManualTimestamper ManualTimestamper => manualTimestamper . Value ;
185
+ public IManualBlockProductionTrigger BlockProductionTrigger => blockProductionTrigger . Value ;
186
+ public IShareableTxProcessorSource ShareableTxProcessorSource => shareableTxProcessorSource . Value ;
187
+ public ISealer Sealer => sealer . Value ;
188
+ public IForkInfo ForkInfo => forkInfo . Value ;
160
189
}
161
190
162
191
public class Configuration
@@ -194,7 +223,13 @@ protected virtual async Task<TestBlockchain> Build(Action<ContainerBuilder>? con
194
223
195
224
_cts = AutoCancelTokenSource . ThatCancelAfter ( Debugger . IsAttached ? TimeSpan . FromMilliseconds ( - 1 ) : TimeSpan . FromMilliseconds ( TestTimout ) ) ;
196
225
197
- if ( testConfiguration . SuggestGenesisOnStart ) MainProcessingContext . GenesisLoader . Load ( ) ;
226
+ if ( testConfiguration . SuggestGenesisOnStart )
227
+ {
228
+ // The block added event is not waited by genesis, but its needed to wait here so that `AddBlock` wait correctly.
229
+ Task newBlockWaiter = BlockTree . WaitForNewBlock ( this . CancellationToken ) ;
230
+ MainProcessingContext . GenesisLoader . Load ( ) ;
231
+ await newBlockWaiter ;
232
+ }
198
233
199
234
if ( testConfiguration . AddBlockOnStart )
200
235
await AddBlocksOnStart ( ) ;
@@ -361,6 +396,13 @@ protected virtual async Task AddBlocksOnStart()
361
396
await AddBlock ( ) ;
362
397
await AddBlock ( CreateTransactionBuilder ( ) . WithNonce ( 0 ) . TestObject ) ;
363
398
await AddBlock ( CreateTransactionBuilder ( ) . WithNonce ( 1 ) . TestObject , CreateTransactionBuilder ( ) . WithNonce ( 2 ) . TestObject ) ;
399
+
400
+ while ( true )
401
+ {
402
+ CancellationToken . ThrowIfCancellationRequested ( ) ;
403
+ if ( BlockTree . Head ? . Number == 3 ) return ;
404
+ await Task . Delay ( 1 , CancellationToken ) ;
405
+ }
364
406
}
365
407
366
408
private TransactionBuilder < Transaction > CreateTransactionBuilder ( )
@@ -403,6 +445,11 @@ public virtual async Task AddBlock(params Transaction[] transactions)
403
445
await TestUtil . AddBlockAndWaitForHead ( false , _cts . Token , transactions ) ;
404
446
}
405
447
448
+ public async Task AddBlock ( TestBlockchainUtil . AddBlockFlags flags , params Transaction [ ] transactions )
449
+ {
450
+ await TestUtil . AddBlock ( flags , _cts . Token , transactions ) ;
451
+ }
452
+
406
453
public async Task AddBlockMayMissTx ( params Transaction [ ] transactions )
407
454
{
408
455
await TestUtil . AddBlockAndWaitForHead ( true , _cts . Token , transactions ) ;
0 commit comments