Coverage Summary for Class: BlockParentGasLimitRule (co.rsk.validators)
Class |
Class, %
|
Method, %
|
Line, %
|
BlockParentGasLimitRule |
0%
(0/1)
|
0%
(0/4)
|
0%
(0/17)
|
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 org.ethereum.core.Block;
23 import org.ethereum.core.BlockHeader;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import java.math.BigInteger;
28
29 /**
30 * Checks if {@link BlockHeader#gasLimit} matches gas limit bounds. <br>
31 *
32 * This check is NOT run in Frontier
33 *
34 * @author Mikhail Kalinin
35 * @since 02.09.2015
36 */
37 public class BlockParentGasLimitRule implements BlockParentDependantValidationRule, BlockHeaderParentDependantValidationRule {
38
39 private static final Logger logger = LoggerFactory.getLogger("blockvalidator");
40
41 private int gasLimitBoundDivisor;
42
43 public BlockParentGasLimitRule(int gasLimitBoundDivisor) {
44 if (gasLimitBoundDivisor < 1) {
45 throw new IllegalArgumentException("The gasLimitBoundDivisor argument must be strictly greater than 0");
46 }
47
48 this.gasLimitBoundDivisor = gasLimitBoundDivisor;
49 }
50
51
52 @Override
53 public boolean isValid(BlockHeader header, Block parent) {
54 if (header == null || parent == null) {
55 logger.warn("BlockParentGasLimitRule - block or parent are null");
56 return false;
57 }
58
59 BlockHeader parentHeader = parent.getHeader();
60 BigInteger headerGasLimit = new BigInteger(1, header.getGasLimit());
61 BigInteger parentGasLimit = new BigInteger(1, parentHeader.getGasLimit());
62
63 if (headerGasLimit.compareTo(parentGasLimit.multiply(BigInteger.valueOf(gasLimitBoundDivisor - 1L)).divide(BigInteger.valueOf(gasLimitBoundDivisor))) < 0 ||
64 headerGasLimit.compareTo(parentGasLimit.multiply(BigInteger.valueOf(gasLimitBoundDivisor + 1L)).divide(BigInteger.valueOf(gasLimitBoundDivisor))) > 0) {
65 logger.warn(String.format("#%d: gas limit exceeds parentBlock.getGasLimit() (+-) GAS_LIMIT_BOUND_DIVISOR", header.getNumber()));
66 return false;
67 }
68 return true;
69 }
70
71 @Override
72 public boolean isValid(Block block, Block parent) {
73 return isValid(block.getHeader(), parent);
74 }
75 }