Skip to content

Commit

Permalink
Add compression (#605)
Browse files Browse the repository at this point in the history
Signed-off-by: [email protected] <[email protected]>

* make use of compression in profitability calculation

Co-authored-by: Fabio Di Fabio <[email protected]>
  • Loading branch information
pinges and fab-10 authored Feb 21, 2024
1 parent d1e13df commit f16aabc
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.math.BigInteger;
import java.util.List;

import org.bouncycastle.crypto.digests.KeccakDigest;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.tests.acceptance.dsl.account.Account;
import org.hyperledger.besu.tests.acceptance.dsl.account.Accounts;
Expand Down Expand Up @@ -55,16 +56,24 @@ public void transactionIsNotMinedWhenUnprofitable() throws Exception {

final Web3j web3j = minerNode.nodeRequests().eth();
final Credentials credentials = Credentials.create(Accounts.GENESIS_ACCOUNT_ONE_PRIVATE_KEY);
TransactionManager txManager = new RawTransactionManager(web3j, credentials, CHAIN_ID);
final TransactionManager txManager = new RawTransactionManager(web3j, credentials, CHAIN_ID);

final String txData = "not profitable transaction".repeat(60);
final KeccakDigest keccakDigest = new KeccakDigest(256);
final StringBuilder txData = new StringBuilder();
txData.append("0x");
for (int i = 0; i < 10; i++) {
keccakDigest.update(new byte[] {(byte) i}, 0, 1);
final byte[] out = new byte[32];
keccakDigest.doFinal(out, 0);
txData.append(new BigInteger(out));
}

final var txUnprofitable =
txManager.sendTransaction(
MIN_GAS_PRICE.getAsBigInteger(),
BigInteger.valueOf(MAX_TX_GAS_LIMIT / 2),
credentials.getAddress(),
txData,
txData.toString(),
BigInteger.ZERO);

final Account sender = accounts.getSecondaryBenefactor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.math.BigDecimal;

import lombok.extern.slf4j.Slf4j;
import net.consensys.linea.compress.LibCompress;
import net.consensys.linea.config.LineaTransactionSelectorConfiguration;
import org.hyperledger.besu.datatypes.Transaction;
import org.hyperledger.besu.datatypes.Wei;
Expand Down Expand Up @@ -50,7 +51,7 @@ public Wei profitablePriorityFeePerGas(
.setMessage(
"Estimated profitable priorityFeePerGas: {}; estimateGasMinMargin={}, verificationCapacity={}, "
+ "verificationGasCost={}, gasPriceRatio={}, gas={}, minGasPrice={}, "
+ "l1GasPrice={}, txSize={}, compressedTxSize={}, adjustTxSize={}")
+ "l1GasPrice={}, txSize={}, compressedTxSize={}")
.addArgument(profitAtWei::toHumanReadableString)
.addArgument(conf.estimateGasMinMargin())
.addArgument(conf.verificationCapacity())
Expand All @@ -61,7 +62,6 @@ public Wei profitablePriorityFeePerGas(
.addArgument(() -> minGasPrice.multiply(conf.gasPriceRatio()).toHumanReadableString())
.addArgument(transaction::getSize)
.addArgument(compressedTxSize)
.addArgument(conf.adjustTxSize())
.log();

return profitAtWei;
Expand Down Expand Up @@ -93,8 +93,7 @@ public boolean isProfitable(
gas,
minGasPrice,
l1GasPrice,
compressedTxSize,
conf.adjustTxSize());
compressedTxSize);
return false;
} else {
log(
Expand All @@ -106,17 +105,14 @@ public boolean isProfitable(
gas,
minGasPrice,
l1GasPrice,
compressedTxSize,
conf.adjustTxSize());
compressedTxSize);
return true;
}
}

private double getCompressedTxSize(final Transaction transaction) {
// this is just a temporary estimation, that will be replaced by gnarkCompression when available
// at that point conf.txCompressionRatio and conf.adjustTxSize options can be removed
final double adjustedTxSize = Math.max(0, transaction.getSize() + conf.adjustTxSize());
return adjustedTxSize / conf.txCompressionRatio();
final byte[] bytes = transaction.encoded().toArrayUnsafe();
return LibCompress.CompressedSize(bytes, bytes.length);
}

private void log(
Expand All @@ -128,12 +124,11 @@ private void log(
final long gasUsed,
final double minGasPrice,
final double l1GasPrice,
final double compressedTxSize,
final int adjustTxSize) {
final double compressedTxSize) {
leb.setMessage(
"Context {}. Transaction {} has a margin of {}, minMargin={}, verificationCapacity={}, "
+ "verificationGasCost={}, gasPriceRatio={}, effectiveGasPrice={}, gasUsed={}, minGasPrice={}, "
+ "l1GasPrice={}, txSize={}, compressedTxSize={}, adjustTxSize={}")
+ "l1GasPrice={}, txSize={}, compressedTxSize={}")
.addArgument(context)
.addArgument(transaction::getHash)
.addArgument(margin)
Expand All @@ -147,7 +142,6 @@ private void log(
.addArgument(l1GasPrice)
.addArgument(transaction::getSize)
.addArgument(compressedTxSize)
.addArgument(adjustTxSize)
.log();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,6 @@ public class LineaTransactionSelectorCliOptions {
"Recommend a specific gas price when using linea_estimateGas (default: ${DEFAULT-VALUE})")
private BigDecimal estimageGasMinMargin = DEFAULT_ESTIMATE_GAS_MIN_MARGIN;

@Positive
@CommandLine.Option(
names = {ADJUST_TX_SIZE},
hidden = true,
paramLabel = "<INTEGER>",
description =
"Adjust transaction size for profitability calculation (default: ${DEFAULT-VALUE})")
private int adjustTxSize = DEFAULT_ADJUST_TX_SIZE;

@Positive
@CommandLine.Option(
names = {TX_COMPRESSION_RATIO},
hidden = true,
paramLabel = "<INTEGER>",
description =
"The ratio between tx serialized size and its compressed size (default: ${DEFAULT-VALUE})")
private int txCompressionRatio = DEFAULT_TX_COMPRESSION_RATIO;

@Positive
@CommandLine.Option(
names = {UNPROFITABLE_CACHE_SIZE},
Expand Down Expand Up @@ -189,8 +171,6 @@ public static LineaTransactionSelectorCliOptions fromConfig(
options.verificationCapacity = config.verificationCapacity();
options.gasPriceRatio = config.gasPriceRatio();
options.minMargin = BigDecimal.valueOf(config.minMargin());
options.adjustTxSize = config.adjustTxSize();
options.txCompressionRatio = config.txCompressionRatio();
options.unprofitableCacheSize = config.unprofitableCacheSize();
options.unprofitableRetryLimit = config.unprofitableRetryLimit();
return options;
Expand All @@ -212,8 +192,6 @@ public LineaTransactionSelectorConfiguration toDomainObject() {
.gasPriceRatio(gasPriceRatio)
.minMargin(minMargin.doubleValue())
.estimateGasMinMargin((estimageGasMinMargin.doubleValue()))
.adjustTxSize(adjustTxSize)
.txCompressionRatio(txCompressionRatio)
.unprofitableCacheSize(unprofitableCacheSize)
.unprofitableRetryLimit(unprofitableRetryLimit)
.build();
Expand All @@ -231,8 +209,6 @@ public String toString() {
.add(GAS_PRICE_RATIO, gasPriceRatio)
.add(MIN_MARGIN, minMargin)
.add(ESTIMATE_GAS_MIN_MARGIN, estimageGasMinMargin)
.add(ADJUST_TX_SIZE, adjustTxSize)
.add(TX_COMPRESSION_RATIO, txCompressionRatio)
.add(UNPROFITABLE_CACHE_SIZE, unprofitableCacheSize)
.add(UNPROFITABLE_RETRY_LIMIT, unprofitableRetryLimit)
.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public record LineaTransactionSelectorConfiguration(
int gasPriceRatio,
double minMargin,
double estimateGasMinMargin,
int adjustTxSize,
int txCompressionRatio,
int unprofitableCacheSize,
int unprofitableRetryLimit) {}
;
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@

import net.consensys.linea.config.LineaTransactionSelectorCliOptions;
import net.consensys.linea.config.LineaTransactionSelectorConfiguration;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.bouncycastle.crypto.digests.KeccakDigest;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.PendingTransaction;
import org.hyperledger.besu.datatypes.Transaction;
Expand All @@ -47,7 +49,6 @@ public class ProfitableTransactionSelectorTest {
private final LineaTransactionSelectorConfiguration conf =
LineaTransactionSelectorCliOptions.create().toDomainObject().toBuilder()
.gasPriceRatio(GAS_PRICE_RATIO)
.adjustTxSize(ADJUST_TX_SIZE)
.minMargin(MIN_MARGIN)
.unprofitableCacheSize(UNPROFITABLE_CACHE_SIZE)
.unprofitableRetryLimit(UNPROFITABLE_RETRY_LIMIT)
Expand Down Expand Up @@ -398,13 +399,32 @@ private TestTransactionEvaluationContext mockEvaluationContext(
PendingTransaction pendingTransaction = mock(PendingTransaction.class);
Transaction transaction = mock(Transaction.class);
when(transaction.getHash()).thenReturn(Hash.wrap(Bytes32.random()));
when(transaction.getSize()).thenReturn(size);
when(transaction.getGasLimit()).thenReturn(gasLimit);
when(transaction.encoded()).thenReturn(Bytes.wrap(pseudoRandomBytes(size)));
when(pendingTransaction.getTransaction()).thenReturn(transaction);
when(pendingTransaction.hasPriority()).thenReturn(hasPriority);
return new TestTransactionEvaluationContext(pendingTransaction, effectiveGasPrice, minGasPrice);
}

private byte[] pseudoRandomBytes(int size) {
final int expectedCompressedSize =
(size - 58) / 5; // This emulates old behaviour of compression ratio and size adjustment
byte[] bytes = new byte[expectedCompressedSize];
final KeccakDigest keccakDigest = new KeccakDigest(256);

final byte[] out = new byte[32];
int offset = 0;
int i = 0;
do {
keccakDigest.update(new byte[] {(byte) i++}, 0, 1);
keccakDigest.doFinal(out, 0);
System.arraycopy(out, 0, bytes, offset, Math.min(expectedCompressedSize - offset, 32));
offset += 32;
} while (offset < expectedCompressedSize);

return bytes;
}

private TransactionProcessingResult mockTransactionProcessingResult(long gasUsedByTransaction) {
TransactionProcessingResult mockTransactionProcessingResult =
mock(TransactionProcessingResult.class);
Expand Down
4 changes: 2 additions & 2 deletions native/compress/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright Hyperledger Besu Contributors
* Copyright Consensys Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -82,7 +82,7 @@ jar {
'Specification-Version': project.version,
'Implementation-Title': archiveBaseName,
'Implementation-Version': project.version,
'Automatic-Module-Name': 'org.hyperledger.besu.nativelib.compress'
'Automatic-Module-Name': 'org.consensys.nativelib.compress'
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright Hyperledger Besu Contributors
* Copyright Consensys Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright Hyperledger Besu Contributors
* Copyright Consensys Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
Expand Down

0 comments on commit f16aabc

Please sign in to comment.