Skip to content

Commit

Permalink
Call miner_setMinGasPrice from extra data parsing
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 May 22, 2024
1 parent e21dae8 commit ea322d3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,21 @@
import net.consensys.linea.config.LineaProfitabilityConfiguration;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt32;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.datatypes.rpc.JsonRpcResponseType;
import org.hyperledger.besu.plugin.data.AddedBlockContext;
import org.hyperledger.besu.plugin.services.BesuEvents;
import org.hyperledger.besu.plugin.services.RpcEndpointService;

@Slf4j
public class LineaExtraDataHandler implements BesuEvents.BlockAddedListener {
private final RpcEndpointService rpcEndpointService;
private final ExtraDataParser[] extraDataParsers;

public LineaExtraDataHandler(final LineaProfitabilityConfiguration profitabilityConf) {
public LineaExtraDataHandler(
final RpcEndpointService rpcEndpointService,
final LineaProfitabilityConfiguration profitabilityConf) {
this.rpcEndpointService = rpcEndpointService;
extraDataParsers = new ExtraDataParser[] {new Version1Parser(profitabilityConf)};
}

Expand Down Expand Up @@ -61,7 +68,7 @@ static Long toLong(final Bytes fieldBytes) {
}

@SuppressWarnings("rawtypes")
private static class Version1Parser implements ExtraDataParser {
private class Version1Parser implements ExtraDataParser {

private final FieldConsumer[] fieldsSequence;

Expand All @@ -73,7 +80,7 @@ public Version1Parser(final LineaProfitabilityConfiguration profitabilityConf) {
new FieldConsumer<>(
"variableGasCost", 4, ExtraDataParser::toLong, profitabilityConf::variableCostKWei);
final FieldConsumer minGasPriceField =
new FieldConsumer<>("minGasPrice", 4, ExtraDataParser::toLong, this::toDo);
new FieldConsumer<>("minGasPrice", 4, ExtraDataParser::toLong, this::updateMinGasPrice);

this.fieldsSequence =
new FieldConsumer[] {fixedGasCostField, variableGasCostField, minGasPriceField};
Expand All @@ -92,8 +99,14 @@ public void parse(final Bytes extraData) {
}
}

void toDo(final Long minGasPriceKWei) {
log.info("ToDo: call setMinGasPrice to {} kwei", minGasPriceKWei);
void updateMinGasPrice(final Long minGasPriceKWei) {
final var minGasPriceWei = Wei.of(minGasPriceKWei).multiply(1000);
final var resp =
rpcEndpointService.call(
"miner_setMinGasPrice", new Object[] {minGasPriceWei.toShortHexString()});
if (!resp.getType().equals(JsonRpcResponseType.SUCCESS)) {
log.error("setMinGasPrice failed: {}", resp);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
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.RpcEndpointService;

/** This plugin registers handlers that are activated when new blocks are imported */
@Slf4j
@AutoService(BesuPlugin.class)
public class LineaExtraDataPlugin extends AbstractLineaRequiredPlugin {
public static final String NAME = "linea";
private BesuEvents besuEventsService;
private RpcEndpointService rpcEndpointService;

@Override
public Optional<String> getName() {
Expand All @@ -43,11 +45,19 @@ public void doRegister(final BesuContext context) {
.getService(BesuEvents.class)
.orElseThrow(
() -> new RuntimeException("Failed to obtain BesuEvents from the BesuContext."));
rpcEndpointService =
context
.getService(RpcEndpointService.class)
.orElseThrow(
() ->
new RuntimeException(
"Failed to obtain RpcEndpointService from the BesuContext."));
}

@Override
public void beforeExternalServices() {
super.beforeExternalServices();
besuEventsService.addBlockAddedListener(new LineaExtraDataHandler(profitabilityConfiguration));
public void start() {
super.start();
besuEventsService.addBlockAddedListener(
new LineaExtraDataHandler(rpcEndpointService, profitabilityConfiguration));
}
}

0 comments on commit ea322d3

Please sign in to comment.