Coverage Summary for Class: BlockDifficultyRule (co.rsk.validators)
Class |
Class, %
|
Method, %
|
Line, %
|
BlockDifficultyRule |
0%
(0/1)
|
0%
(0/4)
|
0%
(0/13)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2017 RSK Labs Ltd.
4 * (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>)
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package co.rsk.validators;
21
22 import co.rsk.core.BlockDifficulty;
23 import co.rsk.core.DifficultyCalculator;
24 import org.ethereum.core.Block;
25 import org.ethereum.core.BlockHeader;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30 * Checks block's difficulty against calculated difficulty value
31 *
32 * @author Mikhail Kalinin
33 * @since 02.09.2015
34 */
35 public class BlockDifficultyRule implements BlockParentDependantValidationRule, BlockHeaderParentDependantValidationRule {
36
37 private static final Logger logger = LoggerFactory.getLogger("blockvalidator");
38
39 private final DifficultyCalculator difficultyCalculator;
40
41 public BlockDifficultyRule(DifficultyCalculator difficultyCalculator) {
42 this.difficultyCalculator = difficultyCalculator;
43 }
44
45 @Override
46 public boolean isValid(BlockHeader header, Block parent) {
47 if (header == null || parent == null) {
48 logger.warn("BlockDifficultyRule - block or parent are null");
49 return false;
50 }
51 BlockDifficulty calcDifficulty = difficultyCalculator.calcDifficulty(header, parent.getHeader());
52 BlockDifficulty difficulty = header.getDifficulty();
53
54 if (!difficulty.equals(calcDifficulty)) {
55 logger.warn("#{}: difficulty != calcDifficulty", header.getNumber());
56 return false;
57 }
58 return true;
59 }
60
61 @Override
62 public boolean isValid(Block block, Block parent) {
63 return isValid(block.getHeader(), parent);
64 }
65 }