Coverage Summary for Class: BlockAccessor (co.rsk.pcc.blockheader)

Class Class, % Method, % Line, %
BlockAccessor 0% (0/1) 0% (0/2) 0% (0/11)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2019 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.pcc.blockheader; 21  22 import co.rsk.pcc.ExecutionEnvironment; 23 import co.rsk.pcc.exception.NativeContractIllegalArgumentException; 24 import org.ethereum.core.Block; 25  26 import java.util.Optional; 27  28 /** 29  * Helper class to provide Block access to the BlockHeaderContract native methods. 30  * 31  * @author Diego Masini 32  */ 33 public class BlockAccessor { 34  private final short maximumBlockDepth; 35  36  public BlockAccessor(short maximumBlockDepth) { 37  this.maximumBlockDepth = maximumBlockDepth; 38  } 39  40  public Optional<Block> getBlock(short blockDepth, ExecutionEnvironment environment) throws NativeContractIllegalArgumentException { 41  if (blockDepth < 0) { 42  throw new NativeContractIllegalArgumentException(String.format( 43  "Invalid block depth '%d' (should be a non-negative value)", 44  blockDepth 45  )); 46  } 47  48  // If blockDepth is bigger or equal to the max depth, return an empty optional instead of throwing an exception 49  // to allow users of this method to decide if they want to throw an exception or return a default 50  // (probably empty) value (e.g. an empty byte array). 51  if (blockDepth >= maximumBlockDepth) { 52  return Optional.empty(); 53  } 54  55  // If block is null, return an empty optional instead of throwing an exception to allow users of this method to 56  // decide if they want to throw an exception or return a default (probably empty) value (e.g. an empty byte array). 57  Block block = environment.getBlockStore().getBlockAtDepthStartingAt(blockDepth, environment.getBlock().getParentHash().getBytes()); 58  if (block == null) { 59  return Optional.empty(); 60  } 61  62  return Optional.of(block); 63  } 64 }