Skip to content

Commit

Permalink
add bundle selector short circuit for non-bundle tx, fix minor serial…
Browse files Browse the repository at this point in the history
…ization issues

Signed-off-by: garyschulte <garyschulte@gmail.com>
  • Loading branch information
garyschulte committed Jan 31, 2025
1 parent 227b0f1 commit e33c025
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import net.consensys.linea.rpc.services.BundlePoolService.TransactionBundle;
import net.consensys.linea.rpc.services.LineaLimitedBundlePool;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.PendingTransaction;
import org.hyperledger.besu.datatypes.Transaction;
Expand Down Expand Up @@ -72,7 +71,7 @@ public BundleResponse execute(final PluginRpcRequest request) {
final BundleParameter bundleParams = parseRequest(logId, request.getParams());

if (bundleParams.maxTimestamp.isPresent()
&& bundleParams.maxTimestamp.get() < Instant.now().toEpochMilli()) {
&& bundleParams.maxTimestamp.get() < Instant.now().getEpochSecond()) {
throw new Exception("bundle max timestamp is in the past");
}

Expand Down Expand Up @@ -107,7 +106,7 @@ public BundleResponse execute(final PluginRpcRequest request) {
bundleParams.minTimestamp,
bundleParams.maxTimestamp,
bundleParams.revertingTxHashes()));
return new BundleResponse(bundleHash);
return new BundleResponse(bundleHash.toHexString());
}
}
// otherwise boom.
Expand All @@ -132,7 +131,7 @@ private BundleParameter parseRequest(final int logId, final Object[] params) {
}
}

public record BundleResponse(Bytes32 bundleHash) {}
public record BundleResponse(String bundleHash) {}

static class LineaSendBundleError implements RpcMethodError {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

import static java.lang.Boolean.TRUE;

import java.time.Instant;
import java.util.List;
import java.util.Optional;

import net.consensys.linea.rpc.methods.LineaSendBundle;
import net.consensys.linea.rpc.services.BundlePoolService;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.PendingTransaction;
Expand All @@ -29,7 +31,6 @@

public class LineaSendBundleTransactionSelector implements PluginTransactionSelector {

public static final long DEFAULT_BUNDLE_POOL_SIZE = 10000L;
final BundlePoolService bundlePool;

public LineaSendBundleTransactionSelector(BundlePoolService bundlePool) {
Expand All @@ -40,8 +41,10 @@ public LineaSendBundleTransactionSelector(BundlePoolService bundlePool) {
public TransactionSelectionResult evaluateTransactionPreProcessing(
final TransactionEvaluationContext<? extends PendingTransaction> txContext) {

// TODO: @fab-10 ideally here we have a way to short circuit transaction evalution for
// pendingTransactions that are not part of a bundle. Need upstream interface changes
// short circuit if we are not a PendingBundleTx
if (!(txContext.getPendingTransaction() instanceof LineaSendBundle.PendingBundleTx)) {
return TransactionSelectionResult.SELECTED;
}

final var blockHeader = txContext.getPendingBlockHeader();

Expand All @@ -57,13 +60,13 @@ public TransactionSelectionResult evaluateTransactionPreProcessing(
.filter(
b ->
b.minTimestamp()
.map(minTime -> minTime < System.currentTimeMillis())
.map(minTime -> minTime < Instant.now().getEpochSecond())
.orElse(TRUE))
// filter if we have a max timestamp that has not been satisfied
.filter(
b ->
b.maxTimestamp()
.map(maxTime -> maxTime > System.currentTimeMillis())
.map(maxTime -> maxTime > Instant.now().getEpochSecond())
.orElse(TRUE))
.findAny();
if (satisfiesCriteria.isPresent()) {
Expand All @@ -81,8 +84,10 @@ public TransactionSelectionResult evaluateTransactionPostProcessing(
final TransactionEvaluationContext<? extends PendingTransaction> txContext,
final TransactionProcessingResult transactionProcessingResult) {

// TODO: @fab-10 ideally here we have a way to short circuit transaction evalution for
// pendingTransactions that are not part of a bundle. Need upstream interface changes
// short circuit if we are not a PendingBundleTx
if (!(txContext.getPendingTransaction() instanceof LineaSendBundle.PendingBundleTx)) {
return TransactionSelectionResult.SELECTED;
}

if (transactionProcessingResult.isFailed()) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void testExecute_ValidBundle() {

// Validate response
assertNotNull(response);
assertEquals(expectedTxBundleHash, response.bundleHash());
assertEquals(expectedTxBundleHash.toHexString(), response.bundleHash());
}

@Test
Expand Down Expand Up @@ -116,7 +116,7 @@ void testExecute_ValidBundle_withReplacement() {

// Validate response
assertNotNull(response);
assertEquals(expectedUUIDBundleHash, response.bundleHash());
assertEquals(expectedUUIDBundleHash.toHexString(), response.bundleHash());

// Replace bundle:
transactions = List.of(mockTX2.encoded().toHexString(), mockTX1.encoded().toHexString());
Expand All @@ -136,7 +136,7 @@ void testExecute_ValidBundle_withReplacement() {

// Validate response
assertNotNull(response);
assertEquals(expectedUUIDBundleHash, response.bundleHash());
assertEquals(expectedUUIDBundleHash.toHexString(), response.bundleHash());

// assert the new block number:
assertTrue(bundlePool.get(expectedUUIDBundleHash).blockNumber().equals(12345L));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.time.Instant;
import java.util.List;
import java.util.Optional;

import net.consensys.linea.rpc.methods.LineaSendBundle.PendingBundleTx;
import net.consensys.linea.rpc.services.BundlePoolService.TransactionBundle;
import net.consensys.linea.rpc.services.LineaLimitedBundlePool;
import org.hyperledger.besu.datatypes.Hash;
Expand All @@ -49,7 +51,7 @@ void setUp() {
@Test
void testEvaluateTransactionPreProcessing_Selected() {
var blockHeader = mockBlockHeader(1);
var pendingTransaction = mock(PendingTransaction.class);
var pendingTransaction = mock(PendingBundleTx.class);
var txContext = mockTransactionEvaluationContext(blockHeader, pendingTransaction);

TransactionBundle bundle = createBundle(1, null, null);
Expand All @@ -65,10 +67,10 @@ void testEvaluateTransactionPreProcessing_Selected() {
@Test
void testEvaluateTransactionPreProcessing_FailedCriteria() {
var blockHeader = mockBlockHeader(1);
var pendingTransaction = mock(PendingTransaction.class);
var pendingTransaction = mock(PendingBundleTx.class);
var txContext = mockTransactionEvaluationContext(blockHeader, pendingTransaction);

TransactionBundle bundle = createBundle(1, System.currentTimeMillis() + 10000000, null);
TransactionBundle bundle = createBundle(1, Instant.now().getEpochSecond() + 10000000, null);
doAnswer(invocation -> Optional.of(bundle))
.when(mockBundlePool)
.getBundleByPendingTransaction(1, pendingTransaction);
Expand All @@ -81,7 +83,7 @@ void testEvaluateTransactionPreProcessing_FailedCriteria() {
@Test
void testEvaluateTransactionPostProcessing_FailedNonRevertable() {
var blockHeader = mockBlockHeader(1);
var pendingTransaction = mock(PendingTransaction.class);
var pendingTransaction = mock(PendingBundleTx.class);
var txContext = mockTransactionEvaluationContext(blockHeader, pendingTransaction);

var transactionProcessingResult = mock(TransactionProcessingResult.class);
Expand All @@ -101,7 +103,7 @@ void testEvaluateTransactionPostProcessing_FailedNonRevertable() {
@Test
void testEvaluateTransactionPostProcessing_Selected() {
var blockHeader = mockBlockHeader(1);
var pendingTransaction = mock(PendingTransaction.class);
var pendingTransaction = mock(PendingBundleTx.class);
var txContext = mockTransactionEvaluationContext(blockHeader, pendingTransaction);

var transactionProcessingResult = mock(TransactionProcessingResult.class);
Expand All @@ -115,15 +117,15 @@ void testEvaluateTransactionPostProcessing_Selected() {
private TransactionBundle createBundle(long blockNumber, Long minTimestamp, Long maxTimestamp) {
return new TransactionBundle(
Hash.fromHexStringLenient("0x1234"),
List.of(mock(PendingTransaction.class)),
List.of(mock(PendingBundleTx.class)),
blockNumber,
Optional.ofNullable(minTimestamp),
Optional.ofNullable(maxTimestamp),
Optional.empty());
}

private TransactionEvaluationContext<PendingTransaction> mockTransactionEvaluationContext(
BlockHeader blockHeader, PendingTransaction pendingTransaction) {
BlockHeader blockHeader, PendingBundleTx pendingTransaction) {
return new TestTransactionEvaluationContext(blockHeader, pendingTransaction, Wei.ONE, Wei.ONE);
}

Expand Down

0 comments on commit e33c025

Please # to comment.