Coverage Summary for Class: NativeMethod (co.rsk.pcc)

Class Method, % Line, %
NativeMethod 0% (0/4) 0% (0/5)
NativeMethod$WithArguments 0% (0/6) 0% (0/8)
Total 0% (0/10) 0% (0/13)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2019 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; 20  21 import co.rsk.pcc.exception.NativeContractIllegalArgumentException; 22 import org.ethereum.core.CallTransaction; 23  24 /** 25  * This class contains common behavior for a single RSK native contract's method. 26  * 27  * @author Ariel Mendelzon 28  */ 29 public abstract class NativeMethod { 30  public final class WithArguments { 31  private final Object[] arguments; 32  private final byte[] originalData; 33  34  public WithArguments(Object[] arguments, byte[] originalData) { 35  this.arguments = arguments; 36  this.originalData = originalData; 37  } 38  39  public NativeMethod getMethod() { 40  return NativeMethod.this; 41  } 42  43  public long getGas() { 44  return NativeMethod.this.getGas(arguments, originalData); 45  } 46  47  public Object[] getArguments() { 48  return arguments; 49  } 50  51  public byte[] getOriginalData() { 52  return originalData; 53  } 54  55  public Object execute() throws NativeContractIllegalArgumentException { 56  return NativeMethod.this.execute(arguments); 57  } 58  } 59  60  private ExecutionEnvironment executionEnvironment; 61  62  public NativeMethod(ExecutionEnvironment executionEnvironment) { 63  this.executionEnvironment = executionEnvironment; 64  } 65  66  public ExecutionEnvironment getExecutionEnvironment() { 67  return executionEnvironment; 68  } 69  70  public abstract CallTransaction.Function getFunction(); 71  72  public abstract Object execute(Object[] arguments) throws NativeContractIllegalArgumentException; 73  74  public long getGas(Object[] parsedArguments, byte[] originalData) { 75  // By default, gas is twice the length of the original data passed in 76  // This can (AND SHOULD) be overriden in implementing methods, although 77  // the default can always be used on top. 78  // (e.g., "return 23000L + super(parsedArguments, originalData);") 79  80  return originalData == null ? 0 : originalData.length * 2; 81  } 82  83  public abstract boolean isEnabled(); 84  85  public abstract boolean onlyAllowsLocalCalls(); 86  87  public String getName() { 88  // Can be overriden to provide a more specific name 89  return getFunction().name; 90  } 91 }