Coverage Summary for Class: BlockRelayValidatorImpl (co.rsk.core.bc)
Class |
Class, %
|
Method, %
|
Line, %
|
BlockRelayValidatorImpl |
0%
(0/1)
|
0%
(0/3)
|
0%
(0/11)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2017 RSK Labs Ltd.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package co.rsk.core.bc;
20
21 import co.rsk.validators.BlockHeaderParentDependantValidationRule;
22 import co.rsk.validators.BlockValidationRule;
23 import co.rsk.validators.BlockValidator;
24 import org.ethereum.core.Block;
25 import org.ethereum.db.BlockStore;
26
27 import javax.annotation.Nonnull;
28
29 /**
30 * Validates a block if it is good enough to be propagated.
31 *
32 * The validation includes:
33 * - validation of the block
34 * - validation of the header data of the parent block
35 */
36 public class BlockRelayValidatorImpl implements BlockValidator {
37
38 private final BlockStore blockStore;
39
40 private final BlockHeaderParentDependantValidationRule blockParentValidator;
41
42 private final BlockValidationRule blockValidator;
43
44 public BlockRelayValidatorImpl(@Nonnull BlockStore blockStore,
45 @Nonnull BlockHeaderParentDependantValidationRule blockParentValidator,
46 @Nonnull BlockValidationRule blockValidator) {
47 this.blockStore = blockStore;
48 this.blockParentValidator = blockParentValidator;
49 this.blockValidator = blockValidator;
50 }
51
52 /**
53 * Checks if a block is valid.
54 *
55 * @param block Block to validate
56 * @return true if the block is valid, otherwise - false.
57 */
58 @Override
59 public boolean isValid(@Nonnull Block block) {
60 if (block.isGenesis()) {
61 return false;
62 }
63
64 if (!blockValidator.isValid(block)) {
65 return false;
66 }
67
68 Block parent = getParent(block);
69 return parent != null && blockParentValidator.isValid(block.getHeader(), parent);
70 }
71
72 private Block getParent(Block block) {
73 return blockStore.getBlockByHash(block.getParentHash().getBytes());
74 }
75 }