use of org.hyperledger.besu.datatypes.Wei in project besu by hyperledger.
the class BlockchainQueriesTest method getAccountBalanceAtBlockNumber.
@Test
public void getAccountBalanceAtBlockNumber() {
final List<Address> addresses = Arrays.asList(gen.address(), gen.address(), gen.address());
final int blockCount = 3;
final BlockchainWithData data = setupBlockchain(blockCount, addresses);
final BlockchainQueries queries = data.blockchainQueries;
for (int i = 0; i < blockCount; i++) {
final long curBlockNumber = i;
final BlockHeader header = data.blockData.get(i).block.getHeader();
final Hash stateRoot = header.getStateRoot();
final WorldState worldState = data.worldStateArchive.get(stateRoot, header.getHash()).get();
assertThat(addresses).isNotEmpty();
addresses.forEach(address -> {
final Account actualAccount = worldState.get(address);
final Optional<Wei> result = queries.accountBalance(address, curBlockNumber);
assertThat(result).contains(actualAccount.getBalance());
});
}
}
use of org.hyperledger.besu.datatypes.Wei in project besu by hyperledger.
the class BlockchainQueriesTest method getAccountBalanceNonExistentAtBlockNumber.
@Test
public void getAccountBalanceNonExistentAtBlockNumber() {
final List<Address> addresses = Arrays.asList(gen.address(), gen.address(), gen.address());
final BlockchainWithData data = setupBlockchain(3, addresses);
final BlockchainQueries queries = data.blockchainQueries;
assertThat(addresses).isNotEmpty();
// Get random non-existent account
final Wei result = queries.accountBalance(gen.address(), 1L).get();
assertThat(result).isEqualTo(Wei.ZERO);
}
use of org.hyperledger.besu.datatypes.Wei in project besu by hyperledger.
the class AbstractBlockCreator method createPendingBlockHeader.
private ProcessableBlockHeader createPendingBlockHeader(final long timestamp, final Optional<Bytes32> maybePrevRandao) {
final long newBlockNumber = parentHeader.getNumber() + 1;
long gasLimit = protocolSpec.getGasLimitCalculator().nextGasLimit(parentHeader.getGasLimit(), targetGasLimitSupplier.get().orElse(parentHeader.getGasLimit()), newBlockNumber);
final DifficultyCalculator difficultyCalculator = protocolSpec.getDifficultyCalculator();
final BigInteger difficulty = difficultyCalculator.nextDifficulty(timestamp, parentHeader, protocolContext);
final Wei baseFee = Optional.of(protocolSpec.getFeeMarket()).filter(FeeMarket::implementsBaseFee).map(BaseFeeMarket.class::cast).map(feeMarket -> feeMarket.computeBaseFee(newBlockNumber, parentHeader.getBaseFee().orElse(Wei.ZERO), parentHeader.getGasUsed(), feeMarket.targetGasUsed(parentHeader))).orElse(null);
final Bytes32 prevRandao = maybePrevRandao.orElse(null);
return BlockHeaderBuilder.create().parentHash(parentHeader.getHash()).coinbase(coinbase).difficulty(Difficulty.of(difficulty)).number(newBlockNumber).gasLimit(gasLimit).timestamp(timestamp).baseFee(baseFee).prevRandao(prevRandao).buildProcessableBlockHeader();
}
use of org.hyperledger.besu.datatypes.Wei in project besu by hyperledger.
the class AbstractBlockCreator method rewardBeneficiary.
/* Copied from BlockProcessor (with modifications). */
boolean rewardBeneficiary(final MutableWorldState worldState, final ProcessableBlockHeader header, final List<BlockHeader> ommers, final Address miningBeneficiary, final Wei blockReward, final boolean skipZeroBlockRewards) {
// TODO(tmm): Added to make this work, should come from blockProcessor.
final int MAX_GENERATION = 6;
if (skipZeroBlockRewards && blockReward.isZero()) {
return true;
}
final Wei coinbaseReward = protocolSpec.getBlockProcessor().getCoinbaseReward(blockReward, header.getNumber(), ommers.size());
final WorldUpdater updater = worldState.updater();
final EvmAccount beneficiary = updater.getOrCreate(miningBeneficiary);
beneficiary.getMutable().incrementBalance(coinbaseReward);
for (final BlockHeader ommerHeader : ommers) {
if (ommerHeader.getNumber() - header.getNumber() > MAX_GENERATION) {
LOG.trace("Block processing error: ommer block number {} more than {} generations current block number {}", ommerHeader.getNumber(), MAX_GENERATION, header.getNumber());
return false;
}
final EvmAccount ommerCoinbase = updater.getOrCreate(ommerHeader.getCoinbase());
final Wei ommerReward = protocolSpec.getBlockProcessor().getOmmerReward(blockReward, header.getNumber(), ommerHeader.getNumber());
ommerCoinbase.getMutable().incrementBalance(ommerReward);
}
updater.commit();
return true;
}
use of org.hyperledger.besu.datatypes.Wei in project besu by hyperledger.
the class BlockTransactionSelector method evaluateTransaction.
/*
* Passed into the PendingTransactions, and is called on each transaction until sufficient
* transactions are found which fill a block worth of gas.
*
* This function will continue to be called until the block under construction is suitably
* full (in terms of gasLimit) and the provided transaction's gasLimit does not fit within
* the space remaining in the block.
*
*/
private TransactionSelectionResult evaluateTransaction(final Transaction transaction) {
if (isCancelled.get()) {
throw new CancellationException("Cancelled during transaction selection.");
}
if (transactionTooLargeForBlock(transaction)) {
LOG.trace("{} too large to select for block creation", transaction);
if (blockOccupancyAboveThreshold()) {
return TransactionSelectionResult.COMPLETE_OPERATION;
} else {
return TransactionSelectionResult.CONTINUE;
}
}
// If the gas price specified by the transaction is less than this node is willing to accept,
// do not include it in the block.
final Wei actualMinTransactionGasPriceInBlock = feeMarket.getTransactionPriceCalculator().price(transaction, processableBlockHeader.getBaseFee());
if (minTransactionGasPrice.compareTo(actualMinTransactionGasPriceInBlock) > 0) {
LOG.warn("Gas fee of {} lower than configured minimum {}, deleting", transaction, minTransactionGasPrice);
return TransactionSelectionResult.DELETE_TRANSACTION_AND_CONTINUE;
}
final WorldUpdater worldStateUpdater = worldState.updater();
final BlockHashLookup blockHashLookup = new BlockHashLookup(processableBlockHeader, blockchain);
final boolean isGoQuorumPrivateTransaction = transaction.isGoQuorumPrivateTransaction(transactionProcessor.getTransactionValidator().getGoQuorumCompatibilityMode());
TransactionProcessingResult effectiveResult;
if (isGoQuorumPrivateTransaction) {
final ValidationResult<TransactionInvalidReason> validationResult = validateTransaction(processableBlockHeader, transaction, worldStateUpdater);
if (!validationResult.isValid()) {
LOG.warn("Invalid transaction: {}. Block {} Transaction {}", validationResult.getErrorMessage(), processableBlockHeader.getParentHash().toHexString(), transaction.getHash().toHexString());
return transactionSelectionResultForInvalidResult(validationResult);
} else {
// valid GoQuorum private tx, we need to hand craft the receipt and increment the nonce
effectiveResult = publicResultForWhenWeHaveAPrivateTransaction(transaction);
worldStateUpdater.getOrCreate(transaction.getSender()).getMutable().incrementNonce();
}
} else {
effectiveResult = transactionProcessor.processTransaction(blockchain, worldStateUpdater, processableBlockHeader, transaction, miningBeneficiary, blockHashLookup, false, TransactionValidationParams.mining());
}
if (!effectiveResult.isInvalid()) {
worldStateUpdater.commit();
LOG.trace("Selected {} for block creation", transaction);
updateTransactionResultTracking(transaction, effectiveResult);
} else {
return transactionSelectionResultForInvalidResult(effectiveResult.getValidationResult());
}
return TransactionSelectionResult.CONTINUE;
}
Aggregations