|
|
|
@ -32,9 +32,11 @@ import org.hyperledger.besu.plugin.data.Quantity; |
|
|
|
|
import org.hyperledger.besu.plugin.data.TransactionType; |
|
|
|
|
|
|
|
|
|
import java.math.BigInteger; |
|
|
|
|
import java.util.Arrays; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Objects; |
|
|
|
|
import java.util.Optional; |
|
|
|
|
import java.util.stream.Stream; |
|
|
|
|
|
|
|
|
|
import org.apache.tuweni.bytes.Bytes; |
|
|
|
|
import org.apache.tuweni.bytes.Bytes32; |
|
|
|
@ -61,11 +63,11 @@ public class Transaction implements org.hyperledger.besu.plugin.data.Transaction |
|
|
|
|
|
|
|
|
|
private final long nonce; |
|
|
|
|
|
|
|
|
|
private final Wei gasPrice; |
|
|
|
|
private final Optional<Wei> gasPrice; |
|
|
|
|
|
|
|
|
|
private final Wei maxPriorityFeePerGas; |
|
|
|
|
private final Optional<Wei> maxPriorityFeePerGas; |
|
|
|
|
|
|
|
|
|
private final Wei maxFeePerGas; |
|
|
|
|
private final Optional<Wei> maxFeePerGas; |
|
|
|
|
|
|
|
|
|
private final long gasLimit; |
|
|
|
|
|
|
|
|
@ -134,9 +136,9 @@ public class Transaction implements org.hyperledger.besu.plugin.data.Transaction |
|
|
|
|
public Transaction( |
|
|
|
|
final TransactionType transactionType, |
|
|
|
|
final long nonce, |
|
|
|
|
final Wei gasPrice, |
|
|
|
|
final Wei maxPriorityFeePerGas, |
|
|
|
|
final Wei maxFeePerGas, |
|
|
|
|
final Optional<Wei> gasPrice, |
|
|
|
|
final Optional<Wei> maxPriorityFeePerGas, |
|
|
|
|
final Optional<Wei> maxFeePerGas, |
|
|
|
|
final long gasLimit, |
|
|
|
|
final Optional<Address> to, |
|
|
|
|
final Wei value, |
|
|
|
@ -185,9 +187,9 @@ public class Transaction implements org.hyperledger.besu.plugin.data.Transaction |
|
|
|
|
|
|
|
|
|
public Transaction( |
|
|
|
|
final long nonce, |
|
|
|
|
final Wei gasPrice, |
|
|
|
|
final Wei maxPriorityFeePerGas, |
|
|
|
|
final Wei maxFeePerGas, |
|
|
|
|
final Optional<Wei> gasPrice, |
|
|
|
|
final Optional<Wei> maxPriorityFeePerGas, |
|
|
|
|
final Optional<Wei> maxFeePerGas, |
|
|
|
|
final long gasLimit, |
|
|
|
|
final Optional<Address> to, |
|
|
|
|
final Wei value, |
|
|
|
@ -242,9 +244,9 @@ public class Transaction implements org.hyperledger.besu.plugin.data.Transaction |
|
|
|
|
final Optional<BigInteger> chainId) { |
|
|
|
|
this( |
|
|
|
|
nonce, |
|
|
|
|
gasPrice, |
|
|
|
|
null, |
|
|
|
|
null, |
|
|
|
|
Optional.of(gasPrice), |
|
|
|
|
Optional.empty(), |
|
|
|
|
Optional.empty(), |
|
|
|
|
gasLimit, |
|
|
|
|
to, |
|
|
|
|
value, |
|
|
|
@ -284,7 +286,19 @@ public class Transaction implements org.hyperledger.besu.plugin.data.Transaction |
|
|
|
|
final Address sender, |
|
|
|
|
final Optional<BigInteger> chainId, |
|
|
|
|
final Optional<BigInteger> v) { |
|
|
|
|
this(nonce, gasPrice, null, null, gasLimit, to, value, signature, payload, sender, chainId, v); |
|
|
|
|
this( |
|
|
|
|
nonce, |
|
|
|
|
Optional.of(gasPrice), |
|
|
|
|
Optional.empty(), |
|
|
|
|
Optional.empty(), |
|
|
|
|
gasLimit, |
|
|
|
|
to, |
|
|
|
|
value, |
|
|
|
|
signature, |
|
|
|
|
payload, |
|
|
|
|
sender, |
|
|
|
|
chainId, |
|
|
|
|
v); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -303,7 +317,7 @@ public class Transaction implements org.hyperledger.besu.plugin.data.Transaction |
|
|
|
|
* @return the transaction gas price |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public Wei getGasPrice() { |
|
|
|
|
public Optional<Wei> getGasPrice() { |
|
|
|
|
return gasPrice; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -313,8 +327,8 @@ public class Transaction implements org.hyperledger.besu.plugin.data.Transaction |
|
|
|
|
* @return the transaction max priority per gas |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public Optional<Quantity> getMaxPriorityFeePerGas() { |
|
|
|
|
return Optional.ofNullable(maxPriorityFeePerGas); |
|
|
|
|
public Optional<Wei> getMaxPriorityFeePerGas() { |
|
|
|
|
return maxPriorityFeePerGas; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -323,8 +337,23 @@ public class Transaction implements org.hyperledger.besu.plugin.data.Transaction |
|
|
|
|
* @return the transaction max fee per gas |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public Optional<Quantity> getMaxFeePerGas() { |
|
|
|
|
return Optional.ofNullable(maxFeePerGas); |
|
|
|
|
public Optional<Wei> getMaxFeePerGas() { |
|
|
|
|
return maxFeePerGas; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Boolean which indicates the transaction has associated cost data, whether gas price or 1559 fee |
|
|
|
|
* market parameters. |
|
|
|
|
* |
|
|
|
|
* @return whether cost params are presetn |
|
|
|
|
*/ |
|
|
|
|
public boolean hasCostParams() { |
|
|
|
|
return Arrays.asList(getGasPrice(), getMaxFeePerGas(), getMaxPriorityFeePerGas()).stream() |
|
|
|
|
.flatMap(Optional::stream) |
|
|
|
|
.map(Quantity::getAsBigInteger) |
|
|
|
|
.filter(q -> q.longValue() > 0L) |
|
|
|
|
.findAny() |
|
|
|
|
.isPresent(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -453,9 +482,9 @@ public class Transaction implements org.hyperledger.besu.plugin.data.Transaction |
|
|
|
|
computeSenderRecoveryHash( |
|
|
|
|
transactionType, |
|
|
|
|
nonce, |
|
|
|
|
gasPrice, |
|
|
|
|
maxPriorityFeePerGas, |
|
|
|
|
maxFeePerGas, |
|
|
|
|
gasPrice.orElse(null), |
|
|
|
|
maxPriorityFeePerGas.orElse(null), |
|
|
|
|
maxFeePerGas.orElse(null), |
|
|
|
|
gasLimit, |
|
|
|
|
to, |
|
|
|
|
value, |
|
|
|
@ -533,7 +562,13 @@ public class Transaction implements org.hyperledger.besu.plugin.data.Transaction |
|
|
|
|
* @return the up-front cost for the gas the transaction can use. |
|
|
|
|
*/ |
|
|
|
|
public Wei getUpfrontGasCost() { |
|
|
|
|
return getUpfrontGasCost((Wei.of(getMaxFeePerGas().orElse(getGasPrice()).getAsBigInteger()))); |
|
|
|
|
return getUpfrontGasCost( |
|
|
|
|
Stream.concat(maxFeePerGas.stream(), gasPrice.stream()) |
|
|
|
|
.findFirst() |
|
|
|
|
.orElseThrow( |
|
|
|
|
() -> |
|
|
|
|
new IllegalStateException( |
|
|
|
|
String.format("Transaction requires either gasPrice or maxFeePerGas")))); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -752,10 +787,16 @@ public class Transaction implements org.hyperledger.besu.plugin.data.Transaction |
|
|
|
|
sb.append(isContractCreation() ? "ContractCreation" : "MessageCall").append("{"); |
|
|
|
|
sb.append("type=").append(getType()).append(", "); |
|
|
|
|
sb.append("nonce=").append(getNonce()).append(", "); |
|
|
|
|
sb.append("gasPrice=").append(getGasPrice()).append(", "); |
|
|
|
|
getGasPrice() |
|
|
|
|
.ifPresent( |
|
|
|
|
gasPrice -> sb.append("gasPrice=").append(gasPrice.toShortHexString()).append(", ")); |
|
|
|
|
if (getMaxPriorityFeePerGas().isPresent() && getMaxFeePerGas().isPresent()) { |
|
|
|
|
sb.append("maxPriorityFeePerGas=").append(getMaxPriorityFeePerGas()).append(", "); |
|
|
|
|
sb.append("maxFeePerGas=").append(getMaxFeePerGas()).append(", "); |
|
|
|
|
sb.append("maxPriorityFeePerGas=") |
|
|
|
|
.append(getMaxPriorityFeePerGas().map(Wei::toShortHexString).get()) |
|
|
|
|
.append(", "); |
|
|
|
|
sb.append("maxFeePerGas=") |
|
|
|
|
.append(getMaxFeePerGas().map(Wei::toShortHexString).get()) |
|
|
|
|
.append(", "); |
|
|
|
|
} |
|
|
|
|
sb.append("gasLimit=").append(getGasLimit()).append(", "); |
|
|
|
|
if (getTo().isPresent()) sb.append("to=").append(getTo().get()).append(", "); |
|
|
|
@ -897,9 +938,9 @@ public class Transaction implements org.hyperledger.besu.plugin.data.Transaction |
|
|
|
|
return new Transaction( |
|
|
|
|
transactionType, |
|
|
|
|
nonce, |
|
|
|
|
gasPrice, |
|
|
|
|
maxPriorityFeePerGas, |
|
|
|
|
maxFeePerGas, |
|
|
|
|
Optional.ofNullable(gasPrice), |
|
|
|
|
Optional.ofNullable(maxPriorityFeePerGas), |
|
|
|
|
Optional.ofNullable(maxFeePerGas), |
|
|
|
|
gasLimit, |
|
|
|
|
to, |
|
|
|
|
value, |
|
|
|
@ -950,7 +991,7 @@ public class Transaction implements org.hyperledger.besu.plugin.data.Transaction |
|
|
|
|
* @param baseFeePerGas optional baseFee from the block header, if we are post-london |
|
|
|
|
* @return the effective gas price. |
|
|
|
|
*/ |
|
|
|
|
public final BigInteger calcEffectiveGas(final Optional<Long> baseFeePerGas) { |
|
|
|
|
public final Wei calcEffectiveGas(final Optional<Long> baseFeePerGas) { |
|
|
|
|
return baseFeePerGas |
|
|
|
|
.filter(fee -> getType().supports1559FeeMarket()) |
|
|
|
|
.map(BigInteger::valueOf) |
|
|
|
@ -965,6 +1006,7 @@ public class Transaction implements org.hyperledger.besu.plugin.data.Transaction |
|
|
|
|
.map( |
|
|
|
|
maxPriorityFeePerGas -> |
|
|
|
|
baseFee.add(maxPriorityFeePerGas).min(maxFeePerGas)))) |
|
|
|
|
.orElse(getGasPrice().getAsBigInteger()); |
|
|
|
|
.map(Wei::ofNumber) |
|
|
|
|
.orElse(getGasPrice().get()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|