-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathBesuEventsImplTest.java
More file actions
614 lines (526 loc) · 24.1 KB
/
BesuEventsImplTest.java
File metadata and controls
614 lines (526 loc) · 24.1 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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/*
* Copyright ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.services;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.hyperledger.besu.crypto.KeyPair;
import org.hyperledger.besu.crypto.SignatureAlgorithm;
import org.hyperledger.besu.crypto.SignatureAlgorithmFactory;
import org.hyperledger.besu.datatypes.Transaction;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.chain.BadBlockCause;
import org.hyperledger.besu.ethereum.chain.BadBlockManager;
import org.hyperledger.besu.ethereum.chain.DefaultBlockchain;
import org.hyperledger.besu.ethereum.chain.MutableBlockchain;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockBody;
import org.hyperledger.besu.ethereum.core.BlockDataGenerator;
import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture;
import org.hyperledger.besu.ethereum.core.Difficulty;
import org.hyperledger.besu.ethereum.core.MiningConfiguration;
import org.hyperledger.besu.ethereum.core.TransactionReceipt;
import org.hyperledger.besu.ethereum.core.TransactionTestFixture;
import org.hyperledger.besu.ethereum.eth.EthProtocolConfiguration;
import org.hyperledger.besu.ethereum.eth.manager.EthContext;
import org.hyperledger.besu.ethereum.eth.manager.EthMessages;
import org.hyperledger.besu.ethereum.eth.manager.EthPeer;
import org.hyperledger.besu.ethereum.eth.manager.EthPeers;
import org.hyperledger.besu.ethereum.eth.sync.BlockBroadcaster;
import org.hyperledger.besu.ethereum.eth.sync.state.SyncState;
import org.hyperledger.besu.ethereum.eth.transactions.BlobCache;
import org.hyperledger.besu.ethereum.eth.transactions.ImmutableTransactionPoolConfiguration;
import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool;
import org.hyperledger.besu.ethereum.eth.transactions.TransactionPoolConfiguration;
import org.hyperledger.besu.ethereum.eth.transactions.TransactionPoolFactory;
import org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpec;
import org.hyperledger.besu.ethereum.mainnet.TransactionValidatorFactory;
import org.hyperledger.besu.ethereum.mainnet.ValidationResult;
import org.hyperledger.besu.ethereum.mainnet.feemarket.FeeMarket;
import org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueStoragePrefixedKeyBlockchainStorage;
import org.hyperledger.besu.ethereum.storage.keyvalue.VariablesKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.pathbased.common.provider.WorldStateQueryParams;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;
import org.hyperledger.besu.plugin.data.AddedBlockContext;
import org.hyperledger.besu.plugin.data.LogWithMetadata;
import org.hyperledger.besu.plugin.data.PropagatedBlockContext;
import org.hyperledger.besu.plugin.data.SyncStatus;
import org.hyperledger.besu.plugin.services.storage.MutableWorldState;
import org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage;
import org.hyperledger.besu.testutil.DeterministicEthScheduler;
import org.hyperledger.besu.testutil.TestClock;
import org.hyperledger.besu.util.number.ByteUnits;
import java.math.BigInteger;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
@SuppressWarnings("unchecked")
@ExtendWith(MockitoExtension.class)
public class BesuEventsImplTest {
private static final SignatureAlgorithm SIGNATURE_ALGORITHM =
SignatureAlgorithmFactory.getInstance();
private static final KeyPair KEY_PAIR1 = SIGNATURE_ALGORITHM.generateKeyPair();
private static final org.hyperledger.besu.ethereum.core.Transaction TX1 = createTransaction(0);
private static final org.hyperledger.besu.ethereum.core.Transaction TX2 = createTransaction(1);
@Mock private ProtocolSchedule mockProtocolSchedule;
@Mock private ProtocolContext mockProtocolContext;
private SyncState syncState;
@Mock private EthPeers mockEthPeers;
@Mock private EthContext mockEthContext;
@Mock private EthMessages mockEthMessages;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private TransactionValidatorFactory mockTransactionValidatorFactory;
@Mock private ProtocolSpec mockProtocolSpec;
@Mock private WorldStateArchive mockWorldStateArchive;
@Mock private MutableWorldState mockWorldState;
private TransactionPool transactionPool;
private BlockBroadcaster blockBroadcaster;
private BesuEventsImpl serviceImpl;
private MutableBlockchain blockchain;
private final BlockDataGenerator gen = new BlockDataGenerator();
private final BadBlockManager badBlockManager = new BadBlockManager();
@BeforeEach
public void setUp() {
blockchain =
DefaultBlockchain.createMutable(
gen.genesisBlock(),
new KeyValueStoragePrefixedKeyBlockchainStorage(
new InMemoryKeyValueStorage(),
new VariablesKeyValueStorage(new InMemoryKeyValueStorage()),
new MainnetBlockHeaderFunctions(),
false),
new NoOpMetricsSystem(),
0);
when(mockEthContext.getEthMessages()).thenReturn(mockEthMessages);
when(mockEthContext.getEthPeers()).thenReturn(mockEthPeers);
when(mockEthContext.getScheduler()).thenReturn(new DeterministicEthScheduler());
lenient().when(mockEthPeers.streamAvailablePeers()).thenAnswer(z -> Stream.empty());
when(mockProtocolContext.getBlockchain()).thenReturn(blockchain);
lenient().when(mockProtocolContext.getWorldStateArchive()).thenReturn(mockWorldStateArchive);
lenient().when(mockProtocolSchedule.getByBlockHeader(any())).thenReturn(mockProtocolSpec);
lenient()
.when(mockProtocolSpec.getTransactionValidatorFactory())
.thenReturn(mockTransactionValidatorFactory);
lenient().when(mockProtocolSpec.getFeeMarket()).thenReturn(FeeMarket.london(0L));
lenient()
.when(
mockTransactionValidatorFactory
.get()
.validate(any(), any(Optional.class), any(Optional.class), any()))
.thenReturn(ValidationResult.valid());
lenient()
.when(mockTransactionValidatorFactory.get().validateForSender(any(), any(), any()))
.thenReturn(ValidationResult.valid());
lenient()
.when(mockWorldStateArchive.getWorldState(any(WorldStateQueryParams.class)))
.thenReturn(Optional.of(mockWorldState));
blockBroadcaster = new BlockBroadcaster(mockEthContext, 10 * ByteUnits.MEGABYTE);
syncState = new SyncState(blockchain, mockEthPeers);
TransactionPoolConfiguration txPoolConfig =
ImmutableTransactionPoolConfiguration.builder()
.txPoolMaxSize(1)
.minGasPrice(Wei.ZERO)
.build();
transactionPool =
TransactionPoolFactory.createTransactionPool(
mockProtocolSchedule,
mockProtocolContext,
mockEthContext,
TestClock.system(ZoneId.systemDefault()),
new NoOpMetricsSystem(),
syncState,
txPoolConfig,
EthProtocolConfiguration.DEFAULT,
new BlobCache(),
MiningConfiguration.newDefault());
serviceImpl =
new BesuEventsImpl(
blockchain, blockBroadcaster, transactionPool, syncState, badBlockManager);
}
@Test
public void syncStatusEventFiresAfterSubscribe() {
final AtomicReference<Optional<SyncStatus>> result = new AtomicReference<>();
serviceImpl.addSyncStatusListener(result::set);
assertThat(result.get()).isNull();
setSyncTarget();
assertThat(result.get()).isNotNull();
// Reset result for next event
result.set(null);
clearSyncTarget();
assertThat(result.get()).isNotNull();
}
@Test
public void syncStatusEventDoesNotFireAfterUnsubscribe() {
final AtomicReference<Optional<SyncStatus>> result = new AtomicReference<>();
final long id = serviceImpl.addSyncStatusListener(result::set);
assertThat(result.get()).isNull();
setSyncTarget();
assertThat(result.get()).isNotNull();
// Reset result for next event
result.set(null);
// And remove listener
serviceImpl.removeSyncStatusListener(id);
clearSyncTarget();
assertThat(result.get()).isNull();
}
private void setSyncTarget() {
syncState.setSyncTarget(
mock(EthPeer.class),
new org.hyperledger.besu.ethereum.core.BlockHeader(
null, null, null, null, null, null, null, null, 1, 1, 1, 1, null, null, null, 1, null,
null, null, null, null, null, null, null));
}
private void clearSyncTarget() {
syncState.clearSyncTarget();
}
@Test
public void newBlockEventFiresAfterSubscribe() {
final AtomicReference<PropagatedBlockContext> result = new AtomicReference<>();
serviceImpl.addBlockPropagatedListener(result::set);
final Block block = generateBlock();
assertThat(result.get()).isNull();
blockBroadcaster.propagate(block, Difficulty.of(1));
assertThat(result.get()).isNotNull();
assertThat(result.get().getBlockHeader()).isEqualTo(block.getHeader());
assertThat(result.get().getTotalDifficulty().toBigInteger()).isEqualTo(BigInteger.ONE);
}
@Test
public void newBlockEventDoesNotFireAfterUnsubscribe() {
final AtomicReference<PropagatedBlockContext> result = new AtomicReference<>();
final long id = serviceImpl.addBlockPropagatedListener(result::set);
assertThat(result.get()).isNull();
final Block block = generateBlock();
blockBroadcaster.propagate(block, Difficulty.of(2));
assertThat(result.get()).isNotNull();
assertThat(result.get().getBlockHeader()).isEqualTo(block.getHeader());
assertThat(result.get().getTotalDifficulty().toBigInteger()).isEqualTo(BigInteger.valueOf(2L));
serviceImpl.removeBlockPropagatedListener(id);
result.set(null);
blockBroadcaster.propagate(generateBlock(), Difficulty.of(1));
assertThat(result.get()).isNull();
}
@Test
public void propagationWithoutSubscriptionsCompletes() {
blockBroadcaster.propagate(generateBlock(), Difficulty.of(1));
}
@Test
public void newBlockEventUselessUnsubscribesCompletes() {
serviceImpl.removeBlockPropagatedListener(5);
serviceImpl.removeBlockPropagatedListener(5L);
}
@Test
public void addedBlockEventFiresAfterSubscribe() {
final AtomicReference<AddedBlockContext> result = new AtomicReference<>();
serviceImpl.addBlockAddedListener(result::set);
assertThat(result.get()).isNull();
final var block =
gen.block(
new BlockDataGenerator.BlockOptions()
.setParentHash(blockchain.getGenesisBlock().getHash()));
List<TransactionReceipt> transactionReceipts = gen.receipts(block);
blockchain.appendBlock(block, transactionReceipts);
assertThat(result.get()).isNotNull();
assertThat(result.get().getBlockHeader()).isEqualTo(block.getHeader());
assertThat(result.get().getTransactionReceipts()).isEqualTo(transactionReceipts);
assertThat(result.get().getEventType()).isEqualTo(AddedBlockContext.EventType.HEAD_ADVANCED);
}
@Test
public void addedBlockEventDoesNotFireAfterUnsubscribe() {
final AtomicReference<AddedBlockContext> result = new AtomicReference<>();
final long id = serviceImpl.addBlockAddedListener(result::set);
assertThat(result.get()).isNull();
serviceImpl.removeBlockAddedListener(id);
result.set(null);
final var block =
gen.block(
new BlockDataGenerator.BlockOptions()
.setParentHash(blockchain.getGenesisBlock().getHash()));
blockchain.appendBlock(block, gen.receipts(block));
assertThat(result.get()).isNull();
}
@Test
public void additionWithoutSubscriptionsCompletes() {
final var block =
gen.block(
new BlockDataGenerator.BlockOptions()
.setParentHash(blockchain.getGenesisBlock().getHash()));
blockchain.appendBlock(block, gen.receipts(block));
}
@Test
public void addedBlockEventUselessUnsubscribesCompletes() {
serviceImpl.removeBlockAddedListener(5);
serviceImpl.removeBlockAddedListener(5L);
}
@Test
public void reorgedBlockEventFiresAfterSubscribe() {
final AtomicReference<AddedBlockContext> result = new AtomicReference<>();
serviceImpl.addBlockReorgListener(result::set);
assertThat(result.get()).isNull();
final var block =
gen.block(
new BlockDataGenerator.BlockOptions()
.setParentHash(blockchain.getGenesisBlock().getHash())
.setBlockNumber(blockchain.getGenesisBlock().getHeader().getNumber() + 1));
blockchain.appendBlock(block, gen.receipts(block));
assertThat(result.get()).isNull();
final var forkBlock =
gen.block(
new BlockDataGenerator.BlockOptions()
.setParentHash(blockchain.getGenesisBlock().getHash())
.setDifficulty(block.getHeader().getDifficulty().subtract(1))
.setBlockNumber(blockchain.getGenesisBlock().getHeader().getNumber() + 1));
blockchain.appendBlock(forkBlock, gen.receipts(forkBlock));
assertThat(result.get()).isNull();
final var reorgBlock =
gen.block(
new BlockDataGenerator.BlockOptions()
.setParentHash(forkBlock.getHash())
.setDifficulty(Difficulty.of(10000000))
.setBlockNumber(forkBlock.getHeader().getNumber() + 1));
List<TransactionReceipt> transactionReceipts = gen.receipts(reorgBlock);
blockchain.appendBlock(reorgBlock, transactionReceipts);
assertThat(result.get()).isNotNull();
assertThat(result.get().getBlockHeader()).isEqualTo(reorgBlock.getHeader());
assertThat(result.get().getTransactionReceipts()).isEqualTo(transactionReceipts);
}
@Test
public void reorgedBlockEventDoesNotFireAfterUnsubscribe() {
final AtomicReference<AddedBlockContext> result = new AtomicReference<>();
final long id = serviceImpl.addBlockReorgListener(result::set);
assertThat(result.get()).isNull();
serviceImpl.removeBlockReorgListener(id);
result.set(null);
final var block =
gen.block(
new BlockDataGenerator.BlockOptions()
.setParentHash(blockchain.getGenesisBlock().getHash())
.setBlockNumber(blockchain.getGenesisBlock().getHeader().getNumber() + 1));
blockchain.appendBlock(block, gen.receipts(block));
assertThat(result.get()).isNull();
final var reorgBlock =
gen.block(
new BlockDataGenerator.BlockOptions()
.setParentHash(blockchain.getGenesisBlock().getHash())
.setBlockNumber(blockchain.getGenesisBlock().getHeader().getNumber() + 1));
blockchain.appendBlock(reorgBlock, gen.receipts(reorgBlock));
assertThat(result.get()).isNull();
}
@Test
public void reorgWithoutSubscriptionsCompletes() {
final var block =
gen.block(
new BlockDataGenerator.BlockOptions()
.setParentHash(blockchain.getGenesisBlock().getHash())
.setBlockNumber(blockchain.getGenesisBlock().getHeader().getNumber() + 1));
blockchain.appendBlock(block, gen.receipts(block));
final var reorgBlock =
gen.block(
new BlockDataGenerator.BlockOptions()
.setParentHash(blockchain.getGenesisBlock().getHash())
.setBlockNumber(blockchain.getGenesisBlock().getHeader().getNumber() + 1));
List<TransactionReceipt> transactionReceipts = gen.receipts(reorgBlock);
blockchain.appendBlock(reorgBlock, transactionReceipts);
}
@Test
public void reorgedBlockEventUselessUnsubscribesCompletes() {
serviceImpl.removeBlockReorgListener(5);
serviceImpl.removeBlockReorgListener(5L);
}
@Test
public void transactionAddedEventFiresAfterSubscribe() {
final AtomicReference<Transaction> result = new AtomicReference<>();
serviceImpl.addTransactionAddedListener(result::set);
assertThat(result.get()).isNull();
transactionPool.addTransactionViaApi(TX1);
assertThat(result.get()).isNotNull();
}
@Test
public void transactionAddedEventDoesNotFireAfterUnsubscribe() {
final AtomicReference<Transaction> result = new AtomicReference<>();
final long id = serviceImpl.addTransactionAddedListener(result::set);
assertThat(result.get()).isNull();
transactionPool.addTransactionViaApi(TX1);
assertThat(result.get()).isNotNull();
serviceImpl.removeTransactionAddedListener(id);
result.set(null);
transactionPool.addTransactionViaApi(TX2);
assertThat(result.get()).isNull();
}
@Test
public void transactionAddedEventUselessUnsubscribesCompletes() {
serviceImpl.removeTransactionAddedListener(5);
serviceImpl.removeTransactionAddedListener(5L);
}
@Test
public void transactionDroppedEventFiresAfterSubscribe() {
final AtomicReference<Transaction> result = new AtomicReference<>();
serviceImpl.addTransactionDroppedListener((tx, reason) -> result.set(tx));
assertThat(result.get()).isNull();
// sending a replacement with higher gas should drop the previous one
transactionPool.addTransactionViaApi(TX1);
transactionPool.addTransactionViaApi(
bumpTransactionGasPrice(TX1, TX1.getGasPrice().get().multiply(2)));
assertThat(result.get()).isNotNull();
}
@Test
public void transactionDroppedEventDoesNotFireAfterUnsubscribe() {
final AtomicReference<Transaction> result = new AtomicReference<>();
final long id = serviceImpl.addTransactionDroppedListener((tx, reason) -> result.set(tx));
assertThat(result.get()).isNull();
transactionPool.addTransactionViaApi(TX1);
// first replacement with higher gas should drop the previous one
transactionPool.addTransactionViaApi(
bumpTransactionGasPrice(TX1, TX1.getGasPrice().get().multiply(2)));
assertThat(result.get()).isNotNull();
serviceImpl.removeTransactionDroppedListener(id);
result.set(null);
// second replacement with higher gas should drop the previous one
transactionPool.addTransactionViaApi(
bumpTransactionGasPrice(TX1, TX1.getGasPrice().get().multiply(4)));
assertThat(result.get()).isNull();
}
@Test
public void logEventFiresAfterSubscribe() {
final List<LogWithMetadata> result = new ArrayList<>();
blockchain.observeLogs(result::add);
assertThat(result).isEmpty();
final var block =
gen.block(
new BlockDataGenerator.BlockOptions()
.setParentHash(blockchain.getGenesisBlock().getHash()));
blockchain.appendBlock(block, gen.receipts(block));
assertThat(result).hasSize(4);
}
@Test
public void logEventDoesNotFireAfterUnsubscribe() {
final List<LogWithMetadata> result = new ArrayList<>();
final long id = blockchain.observeLogs(result::add);
assertThat(result).isEmpty();
final var block =
gen.block(
new BlockDataGenerator.BlockOptions()
.setParentHash(blockchain.getGenesisBlock().getHash()));
blockchain.appendBlock(block, gen.receipts(block));
assertThat(result).hasSize(4);
result.clear();
serviceImpl.removeLogListener(id);
final var block2 =
gen.block(new BlockDataGenerator.BlockOptions().setParentHash(block.getHash()));
blockchain.appendBlock(block2, gen.receipts(block2));
assertThat(result).isEmpty();
}
@Test
public void badBlockEventFiresAfterSubscribe_badBlockAdded() {
// Track bad block events
final AtomicReference<org.hyperledger.besu.plugin.data.BlockHeader> badBlockResult =
new AtomicReference<>();
final AtomicReference<org.hyperledger.besu.plugin.data.BadBlockCause> badBlockCauseResult =
new AtomicReference<>();
serviceImpl.addBadBlockListener(
(badBlock, cause) -> {
badBlockResult.set(badBlock);
badBlockCauseResult.set(cause);
});
// Add bad block
final BadBlockCause blockCause = BadBlockCause.fromValidationFailure("failed");
final Block block = gen.block(new BlockDataGenerator.BlockOptions());
badBlockManager.addBadBlock(block, blockCause);
// Check we caught the bad block
assertThat(badBlockResult.get()).isEqualTo(block.getHeader());
assertThat(badBlockCauseResult.get()).isEqualTo(blockCause);
}
@Test
public void badBlockEventFiresAfterSubscribe_badBlockHeaderAdded() {
// Track bad block events
final AtomicReference<org.hyperledger.besu.plugin.data.BlockHeader> badBlockResult =
new AtomicReference<>();
final AtomicReference<org.hyperledger.besu.plugin.data.BadBlockCause> badBlockCauseResult =
new AtomicReference<>();
serviceImpl.addBadBlockListener(
(badBlock, cause) -> {
badBlockResult.set(badBlock);
badBlockCauseResult.set(cause);
});
// Add bad block header
final BadBlockCause cause = BadBlockCause.fromValidationFailure("oops");
final Block badBlock = gen.block(new BlockDataGenerator.BlockOptions());
badBlockManager.addBadHeader(badBlock.getHeader(), cause);
// Check we caught the bad block
assertThat(badBlockResult.get()).isEqualTo(badBlock.getHeader());
assertThat(badBlockCauseResult.get()).isEqualTo(cause);
}
@Test
public void badBlockEventDoesNotFireAfterUnsubscribe() {
// Track bad block events
final AtomicReference<org.hyperledger.besu.plugin.data.BlockHeader> badBlockResult =
new AtomicReference<>();
final AtomicReference<org.hyperledger.besu.plugin.data.BadBlockCause> badBlockCauseResult =
new AtomicReference<>();
final long listenerId =
serviceImpl.addBadBlockListener(
(badBlock, cause) -> {
badBlockResult.set(badBlock);
badBlockCauseResult.set(cause);
});
// Unsubscribe
serviceImpl.removeBadBlockListener(listenerId);
// Add bad block
final BadBlockCause blockCause = BadBlockCause.fromValidationFailure("failed");
final Block block = gen.block(new BlockDataGenerator.BlockOptions());
badBlockManager.addBadBlock(block, blockCause);
// Add bad block header
final BadBlockCause headerCause = BadBlockCause.fromValidationFailure("oops");
final Block headerBlock = gen.block(new BlockDataGenerator.BlockOptions());
badBlockManager.addBadHeader(headerBlock.getHeader(), headerCause);
// Check we did not process any events
assertThat(badBlockResult.get()).isNull();
assertThat(badBlockCauseResult.get()).isNull();
}
private Block generateBlock() {
final BlockBody body = new BlockBody(Collections.emptyList(), Collections.emptyList());
return new Block(new BlockHeaderTestFixture().buildHeader(), body);
}
private static org.hyperledger.besu.ethereum.core.Transaction createTransaction(
final int transactionNumber) {
return new TransactionTestFixture()
.nonce(transactionNumber)
.gasLimit(0)
.createTransaction(KEY_PAIR1);
}
private static org.hyperledger.besu.ethereum.core.Transaction bumpTransactionGasPrice(
final Transaction originalTx, final Wei gasPriceBump) {
return new TransactionTestFixture()
.nonce(originalTx.getNonce())
.gasPrice(Wei.fromQuantity(originalTx.getGasPrice().get()).add(gasPriceBump))
.gasLimit(originalTx.getGasLimit())
.createTransaction(KEY_PAIR1);
}
}