Skip to content

Commit

Permalink
fix: Add transaction drop listener (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
usmansaleem authored Aug 19, 2024
1 parent 30cea0f commit 0c485dd
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ The plugin jar will be available at `build/libs/`.
## Usage

Drop the `drop-transactions-reporter-plugin-<version>.jar` in the `/plugins` folder under Besu installation.
The plugin will expose following additional cli options:
```shell
--plugin-drop-transaction-reporter-endpoint=<URI>
URI to report drop transactions
```

## License
`SPDX-License-Identifier: Apache-2.0`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
// SPDX-License-Identifier: Apache-2.0
package net.consensys.linea;

import static net.consensys.linea.DropTransactionsReporterCliOptions.NAMESPACE;

import com.google.auto.service.AutoService;
import java.util.Optional;
import org.hyperledger.besu.datatypes.Transaction;
import org.hyperledger.besu.plugin.BesuContext;
import org.hyperledger.besu.plugin.BesuPlugin;
import org.hyperledger.besu.plugin.services.BesuEvents;
import org.hyperledger.besu.plugin.services.PicoCLIOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -15,19 +21,51 @@
@AutoService(BesuPlugin.class)
public class DropTransactionReporterPlugin implements BesuPlugin {
private static final Logger LOG = LoggerFactory.getLogger(DropTransactionReporterPlugin.class);
private BesuContext besuContext;
private Optional<Long> subscriptionId = Optional.empty();
private final DropTransactionsReporterCliOptions cliParams =
new DropTransactionsReporterCliOptions();

@Override
public void register(final BesuContext besuContext) {
LOG.trace("Registering plugin ...");
this.besuContext = besuContext;
registerCliOptions(besuContext);
}

@Override
public void start() {
LOG.trace("Starting plugin ...");
subscriptionId =
besuContext
.getService(BesuEvents.class)
.map(events -> events.addTransactionDroppedListener(this::onTransactionDropped));
LOG.trace("Transaction Drop Listener with ID#{} registered.", subscriptionId);
}

private void onTransactionDropped(Transaction transaction) {
LOG.trace(
"Transaction dropped: {}. Reporting to: {}",
transaction.getHash(),
cliParams.getEndpoint());
// TODO: Implement the logic to report the transaction to a configured endpoint
}

@Override
public void stop() {
LOG.trace("Stopping plugin ...");
subscriptionId.ifPresent(
id ->
besuContext
.getService(BesuEvents.class)
.ifPresent(besuEvents -> besuEvents.removeTransactionDroppedListener(id)));
LOG.trace("Transaction dropped listener with ID#{} removed.", subscriptionId);
}

private void registerCliOptions(final BesuContext besuContext) {
besuContext
.getService(PicoCLIOptions.class)
.orElseThrow(() -> new IllegalStateException("Expecting PicoCLIOptions to be present"))
.addPicoCLIOptions(NAMESPACE, cliParams);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2024, Consensys Software Inc.
// SPDX-License-Identifier: Apache-2.0
package net.consensys.linea;

import java.net.URI;
import picocli.CommandLine;

public class DropTransactionsReporterCliOptions {
static final String NAMESPACE = "drop-transaction-reporter";

@CommandLine.Option(
names = "--plugin-" + NAMESPACE + "-endpoint",
description = "URI to report drop transactions",
required = true,
paramLabel = "<URI>")
private URI endpoint = URI.create("http://localhost:8080");

public URI getEndpoint() {
return endpoint;
}

public void setEndpoint(final URI endpoint) {
this.endpoint = endpoint;
}

public DropTransactionsReporterCliOptions() {}
}

0 comments on commit 0c485dd

Please sign in to comment.