Coverage Summary for Class: BlockRootValidationRule (co.rsk.validators)
Class |
Class, %
|
Method, %
|
Line, %
|
BlockRootValidationRule |
100%
(1/1)
|
100%
(3/3)
|
66.7%
(10/15)
|
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.validators;
20
21 import co.rsk.core.bc.BlockHashesHelper;
22 import co.rsk.panic.PanicProcessor;
23 import org.ethereum.config.blockchain.upgrades.ActivationConfig;
24 import org.ethereum.config.blockchain.upgrades.ConsensusRule;
25 import org.ethereum.core.Block;
26 import org.ethereum.util.ByteUtil;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import java.util.Arrays;
31
32 /**
33 * Validate the transaction root of a block.
34 * It calculates the transaction root hash given the block transaction list
35 * and compares the result with the transaction root hash in block header
36 *
37 * @return true if the transaction root is valid, false if the transaction root is invalid
38 */
39 public class BlockRootValidationRule implements BlockValidationRule {
40
41 private static final Logger logger = LoggerFactory.getLogger("blockvalidator");
42 private static final PanicProcessor panicProcessor = new PanicProcessor();
43
44 private final ActivationConfig activationConfig;
45
46 public BlockRootValidationRule(ActivationConfig activationConfig) {
47 this.activationConfig = activationConfig;
48 }
49
50 @Override
51 public boolean isValid(Block block) {
52 boolean isRskip126Enabled = activationConfig.isActive(ConsensusRule.RSKIP126, block.getNumber());
53 byte[] blockTxRootHash = block.getTxTrieRoot();
54 byte[] txListRootHash = BlockHashesHelper.getTxTrieRoot(block.getTransactionsList(), isRskip126Enabled);
55
56 if (!Arrays.equals(blockTxRootHash, txListRootHash)) {
57 String message = String.format("Block's given Trie Hash doesn't match: %s != %s",
58 ByteUtil.toHexString(blockTxRootHash), ByteUtil.toHexString(txListRootHash));
59
60 logger.warn(message);
61 panicProcessor.panic("invalidtrie", message);
62 return false;
63 }
64
65 return true;
66 }
67 }