use of org.hyperledger.besu.datatypes.Wei in project besu by hyperledger.
the class PrivateTransactionSimulator method getPrivateTransaction.
private PrivateTransaction getPrivateTransaction(final CallParameter callParams, final BlockHeader header, final Bytes privacyGroupId, final MutableWorldState disposablePrivateState) {
final Address senderAddress = callParams.getFrom() != null ? callParams.getFrom() : DEFAULT_FROM;
final Account sender = disposablePrivateState.get(senderAddress);
final long nonce = sender != null ? sender.getNonce() : 0L;
final long gasLimit = callParams.getGasLimit() >= 0 ? callParams.getGasLimit() : header.getGasLimit();
final Wei gasPrice = callParams.getGasPrice() != null ? callParams.getGasPrice() : Wei.ZERO;
final Wei value = callParams.getValue() != null ? callParams.getValue() : Wei.ZERO;
final Bytes payload = callParams.getPayload() != null ? callParams.getPayload() : Bytes.EMPTY;
return PrivateTransaction.builder().privateFrom(Bytes.EMPTY).privacyGroupId(privacyGroupId).restriction(Restriction.RESTRICTED).nonce(nonce).gasPrice(gasPrice).gasLimit(gasLimit).to(callParams.getTo()).sender(senderAddress).value(value).payload(payload).signature(FAKE_SIGNATURE).build();
}
use of org.hyperledger.besu.datatypes.Wei in project besu by hyperledger.
the class TransactionSimulator method buildTransaction.
private Optional<Transaction> buildTransaction(final CallParameter callParams, final TransactionValidationParams transactionValidationParams, final BlockHeader header, final Address senderAddress, final long nonce, final long gasLimit, final Wei value, final Bytes payload) {
final Transaction.Builder transactionBuilder = Transaction.builder().nonce(nonce).gasLimit(gasLimit).to(callParams.getTo()).sender(senderAddress).value(value).payload(payload).signature(FAKE_SIGNATURE);
final Wei gasPrice;
final Wei maxFeePerGas;
final Wei maxPriorityFeePerGas;
if (transactionValidationParams.isAllowExceedingBalance()) {
gasPrice = Wei.ZERO;
maxFeePerGas = Wei.ZERO;
maxPriorityFeePerGas = Wei.ZERO;
} else {
gasPrice = callParams.getGasPrice() != null ? callParams.getGasPrice() : Wei.ZERO;
maxFeePerGas = callParams.getMaxFeePerGas().orElse(gasPrice);
maxPriorityFeePerGas = callParams.getMaxPriorityFeePerGas().orElse(gasPrice);
}
if (header.getBaseFee().isEmpty()) {
transactionBuilder.gasPrice(gasPrice);
} else if (protocolSchedule.getChainId().isPresent()) {
transactionBuilder.maxFeePerGas(maxFeePerGas).maxPriorityFeePerGas(maxPriorityFeePerGas);
} else {
return Optional.empty();
}
transactionBuilder.guessType();
if (transactionBuilder.getTransactionType().requiresChainId()) {
transactionBuilder.chainId(protocolSchedule.getChainId().orElse(// needed to make some transactions valid
BigInteger.ONE));
}
final Transaction transaction = transactionBuilder.build();
return Optional.ofNullable(transaction);
}
use of org.hyperledger.besu.datatypes.Wei in project besu by hyperledger.
the class TransactionSimulator method processWithWorldUpdater.
@Nonnull
public Optional<TransactionSimulatorResult> processWithWorldUpdater(final CallParameter callParams, final TransactionValidationParams transactionValidationParams, final OperationTracer operationTracer, final BlockHeader header, final WorldUpdater updater) {
final ProtocolSpec protocolSpec = protocolSchedule.getByBlockNumber(header.getNumber());
final Address senderAddress = callParams.getFrom() != null ? callParams.getFrom() : DEFAULT_FROM;
BlockHeader blockHeaderToProcess = header;
if (transactionValidationParams.isAllowExceedingBalance() && header.getBaseFee().isPresent()) {
blockHeaderToProcess = BlockHeaderBuilder.fromHeader(header).baseFee(Wei.ZERO).blockHeaderFunctions(protocolSpec.getBlockHeaderFunctions()).buildBlockHeader();
}
final Account sender = updater.get(senderAddress);
final long nonce = sender != null ? sender.getNonce() : 0L;
final long gasLimit = callParams.getGasLimit() >= 0 ? callParams.getGasLimit() : blockHeaderToProcess.getGasLimit();
final Wei value = callParams.getValue() != null ? callParams.getValue() : Wei.ZERO;
final Bytes payload = callParams.getPayload() != null ? callParams.getPayload() : Bytes.EMPTY;
final MainnetTransactionProcessor transactionProcessor = protocolSchedule.getByBlockNumber(blockHeaderToProcess.getNumber()).getTransactionProcessor();
final Optional<Transaction> maybeTransaction = buildTransaction(callParams, transactionValidationParams, header, senderAddress, nonce, gasLimit, value, payload);
if (maybeTransaction.isEmpty()) {
return Optional.empty();
}
final Transaction transaction = maybeTransaction.get();
final TransactionProcessingResult result = transactionProcessor.processTransaction(blockchain, updater, blockHeaderToProcess, transaction, protocolSpec.getMiningBeneficiaryCalculator().calculateBeneficiary(blockHeaderToProcess), new BlockHashLookup(blockHeaderToProcess, blockchain), false, transactionValidationParams, operationTracer);
// If GoQuorum privacy enabled, and value = zero, get max gas possible for a PMT hash.
// It is possible to have a data field that has a lower intrinsic value than the PMT hash.
// This means a potential over-estimate of gas, but the tx, if sent with this gas, will not
// fail.
final boolean goQuorumCompatibilityMode = transactionProcessor.getTransactionValidator().getGoQuorumCompatibilityMode();
if (goQuorumCompatibilityMode && value.isZero()) {
final long privateGasEstimateAndState = protocolSpec.getGasCalculator().getMaximumTransactionCost(64);
if (privateGasEstimateAndState > result.getEstimateGasUsedByTransaction()) {
// modify the result to have the larger estimate
final TransactionProcessingResult resultPmt = TransactionProcessingResult.successful(result.getLogs(), privateGasEstimateAndState, result.getGasRemaining(), result.getOutput(), result.getValidationResult());
return Optional.of(new TransactionSimulatorResult(transaction, resultPmt));
}
}
return Optional.of(new TransactionSimulatorResult(transaction, result));
}
use of org.hyperledger.besu.datatypes.Wei in project besu by hyperledger.
the class PrivateMigrationBlockProcessor method rewardCoinbase.
private boolean rewardCoinbase(final MutableWorldState worldState, final ProcessableBlockHeader header, final List<BlockHeader> ommers, final boolean skipZeroBlockRewards) {
if (skipZeroBlockRewards && blockReward.isZero()) {
return true;
}
final Wei coinbaseReward = blockReward.add(blockReward.multiply(ommers.size()).divide(32));
final WorldUpdater updater = worldState.updater();
final MutableAccount coinbase = updater.getOrCreate(header.getCoinbase()).getMutable();
coinbase.incrementBalance(coinbaseReward);
for (final BlockHeader ommerHeader : ommers) {
if (ommerHeader.getNumber() - header.getNumber() > MAX_GENERATION) {
LOG.warn("Block processing error: ommer block number {} more than {} generations current block" + " number {}", ommerHeader.getNumber(), MAX_GENERATION, header.getNumber());
return false;
}
final MutableAccount ommerCoinbase = updater.getOrCreate(ommerHeader.getCoinbase()).getMutable();
final long distance = header.getNumber() - ommerHeader.getNumber();
final Wei ommerReward = blockReward.subtract(blockReward.multiply(distance).divide(8));
ommerCoinbase.incrementBalance(ommerReward);
}
return true;
}
use of org.hyperledger.besu.datatypes.Wei in project besu by hyperledger.
the class TransactionPool method validateTransaction.
private ValidationResult<TransactionInvalidReason> validateTransaction(final Transaction transaction, final boolean isLocal) {
final BlockHeader chainHeadBlockHeader = getChainHeadBlockHeader();
final FeeMarket feeMarket = protocolSchedule.getByBlockNumber(chainHeadBlockHeader.getNumber()).getFeeMarket();
// Check whether it's a GoQuorum transaction
boolean goQuorumCompatibilityMode = getTransactionValidator().getGoQuorumCompatibilityMode();
if (transaction.isGoQuorumPrivateTransaction(goQuorumCompatibilityMode)) {
final Optional<Wei> weiValue = ofNullable(transaction.getValue());
if (weiValue.isPresent() && !weiValue.get().isZero()) {
return ValidationResult.invalid(TransactionInvalidReason.ETHER_VALUE_NOT_SUPPORTED);
}
}
// executable:
if ((!effectiveGasPriceIsAboveConfiguredMinGasPrice(transaction) && !miningParameters.isMiningEnabled()) || (!feeMarket.satisfiesFloorTxCost(transaction))) {
return ValidationResult.invalid(TransactionInvalidReason.GAS_PRICE_TOO_LOW);
}
final ValidationResult<TransactionInvalidReason> basicValidationResult = getTransactionValidator().validate(transaction, chainHeadBlockHeader.getBaseFee(), TransactionValidationParams.transactionPool());
if (!basicValidationResult.isValid()) {
return basicValidationResult;
}
if (isLocal && strictReplayProtectionShouldBeEnforceLocally(chainHeadBlockHeader) && transaction.getChainId().isEmpty()) {
// Strict replay protection is enabled but the tx is not replay-protected
return ValidationResult.invalid(TransactionInvalidReason.REPLAY_PROTECTED_SIGNATURE_REQUIRED);
}
if (transaction.getGasLimit() > chainHeadBlockHeader.getGasLimit()) {
return ValidationResult.invalid(TransactionInvalidReason.EXCEEDS_BLOCK_GAS_LIMIT, String.format("Transaction gas limit of %s exceeds block gas limit of %s", transaction.getGasLimit(), chainHeadBlockHeader.getGasLimit()));
}
if (transaction.getType().equals(TransactionType.EIP1559) && !feeMarket.implementsBaseFee()) {
return ValidationResult.invalid(TransactionInvalidReason.INVALID_TRANSACTION_FORMAT, "EIP-1559 transaction are not allowed yet");
}
return protocolContext.getWorldStateArchive().getMutable(chainHeadBlockHeader.getStateRoot(), chainHeadBlockHeader.getHash(), false).map(worldState -> {
final Account senderAccount = worldState.get(transaction.getSender());
return getTransactionValidator().validateForSender(transaction, senderAccount, TransactionValidationParams.transactionPool());
}).orElseGet(() -> ValidationResult.invalid(CHAIN_HEAD_WORLD_STATE_NOT_AVAILABLE));
}
Aggregations