Coverage Summary for Class: BlockProcessResult (co.rsk.net)

Class Class, % Method, % Line, %
BlockProcessResult 0% (0/1) 0% (0/11) 0% (0/39)


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.net; 20  21 import co.rsk.core.bc.BlockUtils; 22 import co.rsk.crypto.Keccak256; 23 import co.rsk.util.FormatUtils; 24 import com.google.common.annotations.VisibleForTesting; 25 import java.time.Instant; 26 import org.ethereum.core.Block; 27 import org.ethereum.core.ImportResult; 28 import org.slf4j.Logger; 29 import org.slf4j.LoggerFactory; 30  31 import javax.annotation.Nullable; 32 import java.time.Duration; 33 import java.util.Map; 34  35 /** 36  * Created by mario on 07/02/17. 37  */ 38 public class BlockProcessResult { 39  40  private static final Logger logger = LoggerFactory.getLogger("messagehandler"); 41  private static final Duration LOG_TIME_LIMIT = Duration.ofSeconds(1); 42  43  private final boolean additionalValidationsOk; 44  private final Map<Keccak256, ImportResult> result; 45  46  protected BlockProcessResult(boolean additionalValidations, Map<Keccak256, ImportResult> result, String blockHash, Duration processingTime) { 47  this.additionalValidationsOk = additionalValidations; 48  this.result = result; 49  50  if (processingTime.compareTo(LOG_TIME_LIMIT) >= 0) { 51  logResult(blockHash, processingTime); 52  } 53  } 54  55  public static BlockProcessResult ignoreBlockResult(Block block, Instant start) { 56  return new BlockProcessResult(false, null, block.getPrintableHash(), Duration.between(start, Instant.now())); 57  } 58  59  public static BlockProcessResult connectResult(Block block, Instant start, Map<Keccak256, ImportResult> connectResult) { 60  return new BlockProcessResult(true, connectResult, block.getPrintableHash(), Duration.between(start, Instant.now())); 61  } 62  63  public boolean isScheduledForProcessing() { 64  return additionalValidationsOk && result == null; 65  } 66  67  public boolean wasBlockAdded(Block block) { 68  return additionalValidationsOk && result != null && !result.isEmpty() && importOk(result.get(block.getHash())); 69  } 70  71  public static boolean importOk(@Nullable ImportResult blockResult) { 72  return blockResult == ImportResult.IMPORTED_BEST || blockResult == ImportResult.IMPORTED_NOT_BEST; 73  } 74  75  public boolean isBest() { 76  if (result == null) { 77  return false; 78  } 79  return result.containsValue(ImportResult.IMPORTED_BEST); 80  } 81  82  public boolean isInvalidBlock() { 83  return result != null && this.result.containsValue(ImportResult.INVALID_BLOCK); 84  } 85  86  private void logResult(String blockHash, Duration processingTime) { 87  String message = buildLogMessage(blockHash, processingTime, result); 88  89  if (BlockUtils.tooMuchProcessTime(processingTime.getNano())) { 90  logger.warn(message); 91  } 92  else { 93  logger.debug(message); 94  } 95  } 96  97  @VisibleForTesting 98  public static String buildLogMessage(String blockHash, Duration processingTime, Map<Keccak256, ImportResult> result) { 99  StringBuilder sb = new StringBuilder("[MESSAGE PROCESS] Block[") 100  .append(blockHash) 101  .append("] After[") 102  .append(FormatUtils.formatNanosecondsToSeconds(processingTime.toNanos())) 103  .append("] seconds, process result."); 104  105  if (result == null || result.isEmpty()) { 106  sb.append(" No block connections were made"); 107  } else { 108  sb.append(" Connections attempts: ") 109  .append(result.size()) 110  .append(" | "); 111  112  for (Map.Entry<Keccak256, ImportResult> entry : result.entrySet()) { 113  sb.append(entry.getKey().toString()) 114  .append(" - ") 115  .append(entry.getValue()) 116  .append(" | "); 117  } 118  } 119  120  return sb.toString(); 121  } 122 }