diff --git a/acceptance-tests/src/test/java/linea/plugin/acc/test/ProfitableTransactionTest.java b/acceptance-tests/src/test/java/linea/plugin/acc/test/ProfitableTransactionTest.java index 2f640509d5..5ac2a6ccfe 100644 --- a/acceptance-tests/src/test/java/linea/plugin/acc/test/ProfitableTransactionTest.java +++ b/acceptance-tests/src/test/java/linea/plugin/acc/test/ProfitableTransactionTest.java @@ -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; @@ -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(); diff --git a/arithmetization/src/main/java/net/consensys/linea/bl/TransactionProfitabilityCalculator.java b/arithmetization/src/main/java/net/consensys/linea/bl/TransactionProfitabilityCalculator.java index c40be3093b..dd7f50f98e 100644 --- a/arithmetization/src/main/java/net/consensys/linea/bl/TransactionProfitabilityCalculator.java +++ b/arithmetization/src/main/java/net/consensys/linea/bl/TransactionProfitabilityCalculator.java @@ -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; @@ -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()) @@ -61,7 +62,6 @@ public Wei profitablePriorityFeePerGas( .addArgument(() -> minGasPrice.multiply(conf.gasPriceRatio()).toHumanReadableString()) .addArgument(transaction::getSize) .addArgument(compressedTxSize) - .addArgument(conf.adjustTxSize()) .log(); return profitAtWei; @@ -93,8 +93,7 @@ public boolean isProfitable( gas, minGasPrice, l1GasPrice, - compressedTxSize, - conf.adjustTxSize()); + compressedTxSize); return false; } else { log( @@ -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( @@ -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) @@ -147,7 +142,6 @@ private void log( .addArgument(l1GasPrice) .addArgument(transaction::getSize) .addArgument(compressedTxSize) - .addArgument(adjustTxSize) .log(); } } diff --git a/arithmetization/src/main/java/net/consensys/linea/config/LineaTransactionSelectorCliOptions.java b/arithmetization/src/main/java/net/consensys/linea/config/LineaTransactionSelectorCliOptions.java index 8da3a51032..b9aca5c02a 100644 --- a/arithmetization/src/main/java/net/consensys/linea/config/LineaTransactionSelectorCliOptions.java +++ b/arithmetization/src/main/java/net/consensys/linea/config/LineaTransactionSelectorCliOptions.java @@ -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 = "", - 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 = "", - 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}, @@ -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; @@ -212,8 +192,6 @@ public LineaTransactionSelectorConfiguration toDomainObject() { .gasPriceRatio(gasPriceRatio) .minMargin(minMargin.doubleValue()) .estimateGasMinMargin((estimageGasMinMargin.doubleValue())) - .adjustTxSize(adjustTxSize) - .txCompressionRatio(txCompressionRatio) .unprofitableCacheSize(unprofitableCacheSize) .unprofitableRetryLimit(unprofitableRetryLimit) .build(); @@ -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(); diff --git a/arithmetization/src/main/java/net/consensys/linea/config/LineaTransactionSelectorConfiguration.java b/arithmetization/src/main/java/net/consensys/linea/config/LineaTransactionSelectorConfiguration.java index a7fa215dd5..96252c595d 100644 --- a/arithmetization/src/main/java/net/consensys/linea/config/LineaTransactionSelectorConfiguration.java +++ b/arithmetization/src/main/java/net/consensys/linea/config/LineaTransactionSelectorConfiguration.java @@ -29,8 +29,6 @@ public record LineaTransactionSelectorConfiguration( int gasPriceRatio, double minMargin, double estimateGasMinMargin, - int adjustTxSize, - int txCompressionRatio, int unprofitableCacheSize, int unprofitableRetryLimit) {} ; diff --git a/arithmetization/src/test/java/net/consensys/linea/sequencer/txselection/selectors/ProfitableTransactionSelectorTest.java b/arithmetization/src/test/java/net/consensys/linea/sequencer/txselection/selectors/ProfitableTransactionSelectorTest.java index 6b342161f5..dc8447b1dd 100644 --- a/arithmetization/src/test/java/net/consensys/linea/sequencer/txselection/selectors/ProfitableTransactionSelectorTest.java +++ b/arithmetization/src/test/java/net/consensys/linea/sequencer/txselection/selectors/ProfitableTransactionSelectorTest.java @@ -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; @@ -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) @@ -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); diff --git a/native/compress/build.gradle b/native/compress/build.gradle index 23a1d41f12..f64526f2ef 100644 --- a/native/compress/build.gradle +++ b/native/compress/build.gradle @@ -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 @@ -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' ) } } diff --git a/native/compress/src/main/java/net/consensys/linea/compress/LibCompress.java b/native/compress/src/main/java/net/consensys/linea/compress/LibCompress.java index dbb3bac329..80f5425f92 100644 --- a/native/compress/src/main/java/net/consensys/linea/compress/LibCompress.java +++ b/native/compress/src/main/java/net/consensys/linea/compress/LibCompress.java @@ -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 diff --git a/native/compress/src/test/java/net/consensys/linea/compress/LibCompressTest.java b/native/compress/src/test/java/net/consensys/linea/compress/LibCompressTest.java index 8b069d929a..9e9311abf0 100644 --- a/native/compress/src/test/java/net/consensys/linea/compress/LibCompressTest.java +++ b/native/compress/src/test/java/net/consensys/linea/compress/LibCompressTest.java @@ -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