Coverage Summary for Class: BlockValidatorImpl (co.rsk.core.bc)

Class Class, % Method, % Line, %
BlockValidatorImpl 100% (1/1) 100% (3/3) 75% (12/16)


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.BlockParentDependantValidationRule; 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 /** 28  * BlockValidator has methods to validate block content before its execution 29  * 30  * Created by ajlopez on 29/07/2016. 31  */ 32 public class BlockValidatorImpl implements BlockValidator { 33  34  private BlockStore blockStore; 35  36  private BlockParentDependantValidationRule blockParentValidator; 37  38  private BlockValidationRule blockValidator; 39  40  public BlockValidatorImpl(BlockStore blockStore, BlockParentDependantValidationRule blockParentValidator, BlockValidationRule blockValidator) { 41  this.blockStore = blockStore; 42  this.blockParentValidator = blockParentValidator; 43  this.blockValidator = blockValidator; 44  } 45  46  /** 47  * Validate a block. 48  * The validation includes 49  * - Validate the header data relative to parent block 50  * - Validate the transaction root hash to transaction list 51  * - Validate uncles 52  * - Validate transactions 53  * 54  * @param block Block to validate 55  * @return true if the block is valid, false if the block is invalid 56  */ 57  @Override 58  public boolean isValid(Block block) { 59  if (block.isGenesis()) { 60  return false; 61  } 62  63  Block parent = getParent(block); 64  65  if(!this.blockParentValidator.isValid(block, parent)) { 66  return false; 67  } 68  69  if(!this.blockValidator.isValid(block)) { 70  return false; 71  } 72  73  return true; 74  } 75  76  private Block getParent(Block block) { 77  if (this.blockStore == null) { 78  return null; 79  } 80  81  return blockStore.getBlockByHash(block.getParentHash().getBytes()); 82  } 83 } 84