Coverage Summary for Class: BlockCompositeRule (co.rsk.validators)

Class Class, % Method, % Line, %
BlockCompositeRule 100% (1/1) 100% (3/3) 88.9% (16/18)


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 com.google.common.annotations.VisibleForTesting; 22 import org.ethereum.core.Block; 23 import org.slf4j.Logger; 24 import org.slf4j.LoggerFactory; 25  26 import java.util.ArrayList; 27 import java.util.List; 28  29 /** 30  * A rule which gathers an arbitrary amount of rules for validation of blocks. 31  * This a lower-level validator which is intended to be used in specific cases and in tests, 32  * but usage of higher-level validators like SyncBlockValidationRule or BlockValidationRule 33  * should be preferred. 34  */ 35 @VisibleForTesting 36 public class BlockCompositeRule implements BlockValidationRule { 37  38  private static final Logger logger = LoggerFactory.getLogger("blockvalidator"); 39  40  private List<BlockValidationRule> rules; 41  42  public BlockCompositeRule(BlockValidationRule... rules) { 43  this.rules = new ArrayList<>(); 44  if(rules != null) { 45  for (BlockValidationRule rule : rules) { 46  if(rule != null) { 47  this.rules.add(rule); 48  } 49  } 50  } 51  } 52  @Override 53  public boolean isValid(Block block) { 54  String shortHash = block.getPrintableHash(); 55  long number = block.getNumber(); 56  logger.debug("Validating block {} {}", shortHash, number); 57  for(BlockValidationRule rule : this.rules) { 58  logger.debug("Validation rule {}", rule.getClass().getSimpleName()); 59  60  if(!rule.isValid(block)) { 61  logger.warn("Error Validating block {} {}", shortHash, number); 62  return false; 63  } 64  } 65  return true; 66  } 67 }