Skip to content

Commit

Permalink
Documentation using javadoc
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio Di Fabio <[email protected]>
  • Loading branch information
fab-10 committed Jun 26, 2024
1 parent 7b634f8 commit c3ee198
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@
import org.hyperledger.besu.plugin.BesuPlugin;
import org.hyperledger.besu.plugin.services.PicoCLIOptions;

/**
* This abstract class is used as superclass for all the plugins that share one or more
* configuration options.
*
* <p>Configuration options that are exclusive of a single plugin, are not required to be added
* here, but they could stay in the class that implement a plugin, but in case that configuration
* becomes to be used by multiple plugins, then to avoid code duplications and possible different
* management of the options, it is better to move the configuration here so all plugins will
* automatically see it.
*/
@Slf4j
public abstract class AbstractLineaSharedOptionsPlugin implements BesuPlugin {
private static final String CLI_OPTIONS_PREFIX = "linea";
Expand All @@ -42,9 +52,9 @@ public abstract class AbstractLineaSharedOptionsPlugin implements BesuPlugin {
private static LineaTransactionPoolValidatorCliOptions transactionPoolValidatorCliOptions;
private static LineaL1L2BridgeCliOptions l1L2BridgeCliOptions;
private static LineaRpcCliOptions rpcCliOptions;
private static LineaTracerCliOptions tracerCliOptions;
private static LineaProfitabilityCliOptions profitabilityCliOptions;
protected static LineaTransactionSelectorConfiguration transactionSelectorConfiguration;
protected static LineaTracerCliOptions tracerCliOptions;
protected static LineaTransactionPoolValidatorConfiguration transactionPoolValidatorConfiguration;
protected static LineaL1L2BridgeConfiguration l1L2BridgeConfiguration;
protected static LineaRpcConfiguration rpcConfiguration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@
import org.hyperledger.besu.datatypes.Wei;
import org.slf4j.spi.LoggingEventBuilder;

/**
* This class implements the profitability formula, and it is used both to check if a tx is
* profitable and to give an estimation the profitable priorityFeePerGas for a given tx. The
* profitability depends on the context, so it could mean that it is priced enough to have a chance:
* to be accepted in the txpool and to be a candidate for new block creation, it is also used to
* give an estimated priorityFeePerGas in response to a linea_estimateGas call. Each context has it
* own minMargin configuration, so that is possible to accept in the txpool txs, that are not yet
* profitable for block inclusion, but could be in future if the gas price decrease and likewise, it
* is possible to return an estimated priorityFeePerGas that has a profitability buffer to address
* small fluctuations in the gas market.
*/
@Slf4j
public class TransactionProfitabilityCalculator {
private final LineaProfitabilityConfiguration profitabilityConf;
Expand All @@ -32,6 +43,18 @@ public TransactionProfitabilityCalculator(
this.profitabilityConf = profitabilityConf;
}

/**
* Calculate the estimation of priorityFeePerGas that is considered profitable for the given tx,
* according to the current pricing config and the minMargin.
*
* @param transaction the tx we want to get the estimated priorityFeePerGas for
* @param minMargin the min margin to use for this calculation
* @param gas the gas to use for this calculation, could be the gasUsed of the tx, if it has been
* processed/simulated, otherwise the gasLimit of the tx
* @param minGasPriceWei the current minGasPrice, only used in place of the variable cost from the
* config, in case the extra data pricing is disabled
* @return the estimation of priorityFeePerGas that is considered profitable for the given tx
*/
public Wei profitablePriorityFeePerGas(
final Transaction transaction,
final double minMargin,
Expand Down Expand Up @@ -65,24 +88,38 @@ public Wei profitablePriorityFeePerGas(
return profitAtWei;
}

/**
* Checks if then given gas price is considered profitable for the given tx, according to the
* current pricing config, the minMargin and gas used, or gasLimit of the tx.
*
* @param context a string to name the context in which it is called, used for logs
* @param transaction the tx we want to check if profitable
* @param minMargin the min margin to use for this check
* @param payingGasPrice the gas price the tx is willing to pay
* @param gas the gas to use for this check, could be the gasUsed of the tx, if it has been
* processed/simulated, otherwise the gasLimit of the tx
* @param minGasPriceWei the current minGasPrice, only used in place of the variable cost from the
* config, in case the extra data pricing is disabled
* @return true if the tx is priced enough to be profitable, false otherwise
*/
public boolean isProfitable(
final String context,
final Transaction transaction,
final double minMargin,
final Wei effectiveGasPrice,
final Wei payingGasPrice,
final long gas,
final Wei minGasPriceWei) {

final Wei profitablePriorityFee =
profitablePriorityFeePerGas(transaction, minMargin, gas, minGasPriceWei);

if (effectiveGasPrice.lessThan(profitablePriorityFee)) {
if (payingGasPrice.lessThan(profitablePriorityFee)) {
log(
log.atDebug(),
context,
transaction,
minMargin,
effectiveGasPrice,
payingGasPrice,
profitablePriorityFee,
gas,
minGasPriceWei);
Expand All @@ -94,13 +131,19 @@ public boolean isProfitable(
context,
transaction,
minMargin,
effectiveGasPrice,
payingGasPrice,
profitablePriorityFee,
gas,
minGasPriceWei);
return true;
}

/**
* This method calculates the compressed size of a tx using the native lib
*
* @param transaction the tx
* @return the compressed size
*/
private int getCompressedTxSize(final Transaction transaction) {
final byte[] bytes = transaction.encoded().toArrayUnsafe();
return LibCompress.CompressedSize(bytes, bytes.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public void doRegister(final BesuContext context) {
"Failed to obtain BlockchainService from the BesuContext."));
}

/**
* Starts this plugin and in case the extra data pricing is enabled, as first thing it tries to
* extract extra data pricing configuration from the chain head, then it starts listening for new
* imported block, in order to update the extra data pricing on every incoming block.
*/
@Override
public void start() {
super.start();
Expand Down

0 comments on commit c3ee198

Please sign in to comment.