Coverage Summary for Class: TransactionExecutionSummary (org.ethereum.core)

Class Method, % Line, %
TransactionExecutionSummary 31.6% (6/19) 51.5% (17/33)
TransactionExecutionSummary$Builder 90% (9/10) 78.1% (25/32)
Total 51.7% (15/29) 64.6% (42/65)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2017 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 org.ethereum.core; 21  22 import co.rsk.core.Coin; 23 import org.ethereum.vm.DataWord; 24 import org.ethereum.vm.LogInfo; 25 import org.ethereum.vm.program.InternalTransaction; 26  27 import java.math.BigInteger; 28 import java.util.*; 29  30 import static java.util.Collections.*; 31 import static org.ethereum.util.BIUtil.toBI; 32  33 public class TransactionExecutionSummary { 34  35  private Transaction tx; 36  private Coin value = Coin.ZERO; 37  private Coin gasPrice = Coin.ZERO; 38  private BigInteger gasLimit = BigInteger.ZERO; 39  private BigInteger gasUsed = BigInteger.ZERO; 40  private BigInteger gasLeftover = BigInteger.ZERO; 41  private BigInteger gasRefund = BigInteger.ZERO; 42  43  private List<DataWord> deletedAccounts = emptyList(); 44  private List<InternalTransaction> internalTransactions = emptyList(); 45  46  private byte[] result; 47  private List<LogInfo> logs; 48  49  private boolean failed; 50  51  public Transaction getTransaction() { 52  return tx; 53  } 54  55  public byte[] getTransactionHash() { 56  return getTransaction().getHash().getBytes(); 57  } 58  59  private Coin calcCost(BigInteger gas) { 60  return gasPrice.multiply(gas); 61  } 62  63  public Coin getFee() { 64  if (failed) { 65  return calcCost(gasLimit); 66  } 67  68  return calcCost(gasLimit.subtract(gasLeftover.add(gasRefund))); 69  } 70  71  public Coin getRefund() { 72  if (failed) { 73  return Coin.ZERO; 74  } 75  76  return calcCost(gasRefund); 77  } 78  79  public Coin getLeftover() { 80  if (failed) { 81  return Coin.ZERO; 82  } 83  84  return calcCost(gasLeftover); 85  } 86  87  public Coin getGasPrice() { 88  return gasPrice; 89  } 90  91  public BigInteger getGasLimit() { 92  return gasLimit; 93  } 94  95  public BigInteger getGasUsed() { 96  return gasUsed; 97  } 98  99  public BigInteger getGasLeftover() { 100  return gasLeftover; 101  } 102  103  public Coin getValue() { 104  return value; 105  } 106  107  public List<DataWord> getDeletedAccounts() { 108  return deletedAccounts; 109  } 110  111  public List<InternalTransaction> getInternalTransactions() { 112  return internalTransactions; 113  } 114  115  public BigInteger getGasRefund() { 116  return gasRefund; 117  } 118  119  public boolean isFailed() { 120  return failed; 121  } 122  123  public byte[] getResult() { 124  return result; 125  } 126  127  public List<LogInfo> getLogs() { 128  return logs; 129  } 130  131  public static Builder builderFor(Transaction transaction) { 132  return new Builder(transaction); 133  } 134  135  public static class Builder { 136  137  private final TransactionExecutionSummary summary; 138  139  Builder(Transaction transaction) { 140  Objects.requireNonNull(transaction, "Cannot build TransactionExecutionSummary for null transaction."); 141  142  summary = new TransactionExecutionSummary(); 143  summary.tx = transaction; 144  summary.gasLimit = toBI(transaction.getGasLimit()); 145  summary.gasPrice = transaction.getGasPrice(); 146  summary.value = transaction.getValue(); 147  } 148  149  public Builder gasUsed(BigInteger gasUsed) { 150  summary.gasUsed = gasUsed; 151  return this; 152  } 153  154  public Builder gasLeftover(BigInteger gasLeftover) { 155  summary.gasLeftover = gasLeftover; 156  return this; 157  } 158  159  public Builder gasRefund(BigInteger gasRefund) { 160  summary.gasRefund = gasRefund; 161  return this; 162  } 163  164  public Builder internalTransactions(List<InternalTransaction> internalTransactions) { 165  summary.internalTransactions = unmodifiableList(internalTransactions); 166  return this; 167  } 168  169  public Builder deletedAccounts(Set<DataWord> deletedAccounts) { 170  summary.deletedAccounts = new ArrayList<>(); 171  for (DataWord account : deletedAccounts) { 172  summary.deletedAccounts.add(account); 173  } 174  return this; 175  } 176  177  public Builder markAsFailed() { 178  summary.failed = true; 179  return this; 180  } 181  182  public Builder logs(List<LogInfo> logs) { 183  summary.logs = logs; 184  return this; 185  } 186  187  public Builder result(byte[] result) { 188  summary.result = result; 189  return this; 190  } 191  192  public TransactionExecutionSummary build() { 193  if (summary.failed) { 194  for (InternalTransaction transaction : summary.internalTransactions) { 195  transaction.reject(); 196  } 197  } 198  return summary; 199  } 200  } 201 }