Skip to content

Commit

Permalink
Add dataGasUsed and dataGasPrice to receipts for 4844 txs (#5554)
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel-Trintinalia <gabriel.trintinalia@consensys.net>
  • Loading branch information
Gabriel-Trintinalia authored and jflo committed Jun 29, 2023
1 parent e1a9855 commit 41b35a9
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.api.graphql.GraphQLContextType;
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.api.query.TransactionReceiptWithMetadata;
import org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.LogWithMetadata;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput;

import java.util.ArrayList;
Expand All @@ -46,12 +48,15 @@ private Optional<TransactionReceiptWithMetadata> getReceipt(
final DataFetchingEnvironment environment) {
if (transactionReceiptWithMetadata == null) {
final BlockchainQueries query = getBlockchainQueries(environment);
final ProtocolSchedule protocolSchedule =
environment.getGraphQlContext().get(GraphQLContextType.PROTOCOL_SCHEDULE);

final Transaction transaction = transactionWithMetadata.getTransaction();
if (transaction == null) {
transactionReceiptWithMetadata = Optional.empty();
} else {
transactionReceiptWithMetadata =
query.transactionReceiptByTransactionHash(transaction.getHash());
query.transactionReceiptByTransactionHash(transaction.getHash(), protocolSchedule);
}
}
return transactionReceiptWithMetadata;
Expand Down Expand Up @@ -187,6 +192,9 @@ public Optional<AccountAdapter> getCreatedContract(final DataFetchingEnvironment

public List<LogAdapter> getLogs(final DataFetchingEnvironment environment) {
final BlockchainQueries query = getBlockchainQueries(environment);
final ProtocolSchedule protocolSchedule =
environment.getGraphQlContext().get(GraphQLContextType.PROTOCOL_SCHEDULE);

final Hash hash = transactionWithMetadata.getTransaction().getHash();

final Optional<BlockHeader> maybeBlockHeader =
Expand All @@ -201,7 +209,7 @@ public List<LogAdapter> getLogs(final DataFetchingEnvironment environment) {
}

final Optional<TransactionReceiptWithMetadata> maybeTransactionReceiptWithMetadata =
query.transactionReceiptByTransactionHash(hash);
query.transactionReceiptByTransactionHash(hash, protocolSchedule);
final List<LogAdapter> results = new ArrayList<>();
if (maybeTransactionReceiptWithMetadata.isPresent()) {
final List<LogWithMetadata> logs =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public static MinerDataResult createMinerDataResult(
.map(
t ->
blockchainQueries
.transactionReceiptByTransactionHash(t.getTransaction().getHash())
.transactionReceiptByTransactionHash(
t.getTransaction().getHash(), protocolSchedule)
.map(
receipt ->
receipt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,19 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.TransactionReceiptStatusResult;
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.api.query.TransactionReceiptWithMetadata;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.TransactionReceiptType;

public class EthGetTransactionReceipt implements JsonRpcMethod {

private final BlockchainQueries blockchainQueries;

public EthGetTransactionReceipt(final BlockchainQueries blockchainQueries) {
private final ProtocolSchedule protocolSchedule;

public EthGetTransactionReceipt(
final BlockchainQueries blockchainQueries, final ProtocolSchedule protocolSchedule) {
this.blockchainQueries = blockchainQueries;
this.protocolSchedule = protocolSchedule;
}

@Override
Expand All @@ -44,7 +49,7 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
final Hash hash = requestContext.getRequiredParameter(0, Hash.class);
final TransactionReceiptResult result =
blockchainQueries
.transactionReceiptByTransactionHash(hash)
.transactionReceiptByTransactionHash(hash, protocolSchedule)
.map(this::getResult)
.orElse(null);
return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@
"transactionHash",
"transactionIndex",
"revertReason",
"type"
"type",
"dataGasUsed",
"dataGasPrice"
})
public abstract class TransactionReceiptResult {

Expand All @@ -67,6 +69,9 @@ public abstract class TransactionReceiptResult {
protected final TransactionReceipt receipt;
protected final String type;

private final String dataGasUsed;
private final String dataGasPrice;

protected TransactionReceiptResult(final TransactionReceiptWithMetadata receiptWithMetadata) {
final Transaction txn = receiptWithMetadata.getTransaction();
this.receipt = receiptWithMetadata.getReceipt();
Expand All @@ -76,6 +81,8 @@ protected TransactionReceiptResult(final TransactionReceiptWithMetadata receiptW
this.cumulativeGasUsed = Quantity.create(receipt.getCumulativeGasUsed());
this.from = txn.getSender().toString();
this.gasUsed = Quantity.create(receiptWithMetadata.getGasUsed());
this.dataGasUsed = receiptWithMetadata.getDataGasUsed().map(Quantity::create).orElse(null);
this.dataGasPrice = receiptWithMetadata.getDataGasPrice().map(Quantity::create).orElse(null);
this.effectiveGasPrice =
Quantity.create(txn.getEffectiveGasPrice(receiptWithMetadata.getBaseFee()));

Expand Down Expand Up @@ -127,6 +134,18 @@ public String getGasUsed() {
return gasUsed;
}

@JsonGetter(value = "dataGasUsed")
@JsonInclude(JsonInclude.Include.NON_NULL)
public String getDataGasUsed() {
return dataGasUsed;
}

@JsonGetter(value = "dataGasPrice")
@JsonInclude(JsonInclude.Include.NON_NULL)
public String getDataGasPrice() {
return dataGasPrice;
}

@JsonGetter(value = "effectiveGasPrice")
public String getEffectiveGasPrice() {
return effectiveGasPrice;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ protected Map<String, JsonRpcMethod> create() {
new EthGetTransactionByBlockHashAndIndex(blockchainQueries),
new EthGetTransactionByBlockNumberAndIndex(blockchainQueries),
new EthGetTransactionCount(blockchainQueries, transactionPool.getPendingTransactions()),
new EthGetTransactionReceipt(blockchainQueries),
new EthGetTransactionReceipt(blockchainQueries, protocolSchedule),
new EthUninstallFilter(filterManager),
new EthGetFilterChanges(filterManager),
new EthGetFilterLogs(filterManager),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.hyperledger.besu.ethereum.api.query.cache.TransactionLogBloomCacher.BLOCKS_PER_BLOOM_CACHE;

import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.DataGas;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.api.ApiConfiguration;
Expand All @@ -33,6 +34,8 @@
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.core.TransactionReceipt;
import org.hyperledger.besu.ethereum.eth.manager.EthScheduler;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpec;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;
import org.hyperledger.besu.evm.account.Account;
import org.hyperledger.besu.evm.log.LogsBloomFilter;
Expand Down Expand Up @@ -612,7 +615,7 @@ public Optional<TransactionLocation> transactionLocationByHash(final Hash transa
* @return The transaction receipt associated with the referenced transaction.
*/
public Optional<TransactionReceiptWithMetadata> transactionReceiptByTransactionHash(
final Hash transactionHash) {
final Hash transactionHash, final ProtocolSchedule protocolSchedule) {
final Optional<TransactionLocation> maybeLocation =
blockchain.getTransactionLocation(transactionHash);
if (maybeLocation.isEmpty()) {
Expand All @@ -639,6 +642,12 @@ public Optional<TransactionReceiptWithMetadata> transactionReceiptByTransactionH
- transactionReceipts.get(location.getTransactionIndex() - 1).getCumulativeGasUsed();
}

Optional<Long> maybeDataGasUsed =
getDataGasUsed(transaction, protocolSchedule.getByBlockHeader(header));

Optional<Wei> maybeDataGasPrice =
getDataGasPrice(transaction, header, protocolSchedule.getByBlockHeader(header));

return Optional.of(
TransactionReceiptWithMetadata.create(
transactionReceipt,
Expand All @@ -648,7 +657,47 @@ public Optional<TransactionReceiptWithMetadata> transactionReceiptByTransactionH
gasUsed,
header.getBaseFee(),
blockhash,
header.getNumber()));
header.getNumber(),
maybeDataGasUsed,
maybeDataGasPrice));
}

/**
* Calculates the data gas used for data in a transaction.
*
* @param transaction the transaction to calculate the gas for
* @param protocolSpec the protocol specification to use for gas calculation
* @return an Optional containing the data gas used for data if the transaction type supports
* blobs, otherwise returns an empty Optional
*/
private Optional<Long> getDataGasUsed(
final Transaction transaction, final ProtocolSpec protocolSpec) {
return transaction.getType().supportsBlob()
? Optional.of(protocolSpec.getGasCalculator().dataGasCost(transaction.getBlobCount()))
: Optional.empty();
}

/**
* Calculates the data gas price for data in a transaction.
*
* @param transaction the transaction to calculate the gas price for
* @param header the block header of the current block
* @param protocolSpec the protocol specification to use for gas price calculation
* @return an Optional containing the data gas price for data if the transaction type supports
* blobs, otherwise returns an empty Optional
*/
private Optional<Wei> getDataGasPrice(
final Transaction transaction, final BlockHeader header, final ProtocolSpec protocolSpec) {
if (transaction.getType().supportsBlob()) {
return blockchain
.getBlockHeader(header.getParentHash())
.map(
parentHeader ->
protocolSpec
.getFeeMarket()
.dataPrice(parentHeader.getExcessDataGas().orElse(DataGas.ZERO)));
}
return Optional.empty();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class TransactionReceiptWithMetadata {
private final long blockNumber;
private final Hash blockHash;
private final Transaction transaction;
private final Optional<Long> dataGasUsed;
private final Optional<Wei> dataGasPrice;

private TransactionReceiptWithMetadata(
final TransactionReceipt receipt,
Expand All @@ -39,7 +41,9 @@ private TransactionReceiptWithMetadata(
final long gasUsed,
final Optional<Wei> baseFee,
final Hash blockHash,
final long blockNumber) {
final long blockNumber,
final Optional<Long> dataGasUsed,
final Optional<Wei> dataGasPrice) {
this.receipt = receipt;
this.transactionHash = transactionHash;
this.transactionIndex = transactionIndex;
Expand All @@ -48,6 +52,8 @@ private TransactionReceiptWithMetadata(
this.blockHash = blockHash;
this.blockNumber = blockNumber;
this.transaction = transaction;
this.dataGasUsed = dataGasUsed;
this.dataGasPrice = dataGasPrice;
}

public static TransactionReceiptWithMetadata create(
Expand All @@ -58,7 +64,9 @@ public static TransactionReceiptWithMetadata create(
final long gasUsed,
final Optional<Wei> baseFee,
final Hash blockHash,
final long blockNumber) {
final long blockNumber,
final Optional<Long> dataGasUsed,
final Optional<Wei> dataGasPrice) {
return new TransactionReceiptWithMetadata(
receipt,
transaction,
Expand All @@ -67,7 +75,9 @@ public static TransactionReceiptWithMetadata create(
gasUsed,
baseFee,
blockHash,
blockNumber);
blockNumber,
dataGasUsed,
dataGasPrice);
}

public TransactionReceipt getReceipt() {
Expand Down Expand Up @@ -103,4 +113,12 @@ public long getGasUsed() {
public Optional<Wei> getBaseFee() {
return baseFee;
}

public Optional<Long> getDataGasUsed() {
return dataGasUsed;
}

public Optional<Wei> getDataGasPrice() {
return dataGasPrice;
}
}
Loading

0 comments on commit 41b35a9

Please sign in to comment.