Coverage Summary for Class: BN128PrecompiledContract (co.rsk.pcc.altBN128)

Class Class, % Method, % Line, %
BN128PrecompiledContract 0% (0/1) 0% (0/4) 0% (0/19)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2021 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.pcc.altBN128; 20  21 import co.rsk.pcc.altBN128.impls.AbstractAltBN128; 22 import org.ethereum.config.blockchain.upgrades.ActivationConfig; 23 import org.ethereum.config.blockchain.upgrades.ConsensusRule; 24 import org.ethereum.vm.PrecompiledContracts; 25 import org.ethereum.vm.exception.VMException; 26  27 import static org.ethereum.util.ByteUtil.EMPTY_BYTE_ARRAY; 28  29 /** 30  * @author Patricio Gallardo 31  * @since 07.01.2021 32  */ 33 public abstract class BN128PrecompiledContract extends PrecompiledContracts.PrecompiledContract { 34  35  private final ActivationConfig.ForBlock activations; 36  protected final AbstractAltBN128 altBN128Lib; 37  38  public BN128PrecompiledContract(ActivationConfig.ForBlock activations, AbstractAltBN128 altBN128) { 39  this.activations = activations; 40  this.altBN128Lib = altBN128; 41  } 42  43  /** 44  * This is a Template Method, subclasses should override 'concreteExecute()' method. 45  */ 46  @Override 47  public byte[] execute(byte[] data) throws VMException { 48  if (activations.isActive(ConsensusRule.RSKIP197)) { 49  return unsafeExecute(data); 50  } else { 51  return safeExecute(data); 52  } 53  } 54  55  /** 56  * After RSKIP197 we can throw exception of {@link VMException} and they will properly handled as an error. 57  */ 58  private byte[] unsafeExecute(byte[] data) throws VMException { 59  if (data == null) { 60  data = EMPTY_BYTE_ARRAY; 61  } 62  int rs = concreteExecute(data); 63  if (rs < 0) { 64  throw new VMException("Invalid result."); 65  } 66  return altBN128Lib.getOutput(); 67  } 68  69  /** 70  * Before RSKIP197 there were no way of throwing an error, so it returned an empty byte array. 71  */ 72  private byte[] safeExecute(byte[] data) { 73  if (data == null) { 74  data = EMPTY_BYTE_ARRAY; 75  } 76  int rs = concreteExecute(data); 77  if (rs < 0) { 78  return EMPTY_BYTE_ARRAY; 79  } 80  return altBN128Lib.getOutput(); 81  } 82  83  protected abstract int concreteExecute(byte[] data); 84 }