Skip to content

Commit

Permalink
Introduce LineaExtraDataPlugin
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 9, 2024
1 parent 6e52d3e commit a1d0dba
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

package net.consensys.linea.extradata;

import lombok.extern.slf4j.Slf4j;
import org.apache.tuweni.bytes.Bytes;
import org.hyperledger.besu.plugin.data.AddedBlockContext;
import org.hyperledger.besu.plugin.services.BesuEvents;

@Slf4j
public class LineaExtraDataHandler implements BesuEvents.BlockAddedListener {
@Override
public void onBlockAdded(final AddedBlockContext addedBlockContext) {
final var blockHeader = addedBlockContext.getBlockHeader();
final var extraData = blockHeader.getExtraData();

if (!Bytes.EMPTY.equals(extraData)) {
final byte version = extractVersion(extraData);
switch (version) {
case 1:
parseVersion1(extraData);
break;
default:
log.warn(
"unsupported extra data version: {}, raw extra data field",
version,
extraData.toHexString());
}
}
}

private void parseVersion1(final Bytes extraData) {
log.info("Found extra data version 1: {}", extraData.toHexString());
}

private byte extractVersion(final Bytes extraData) {
return extraData.get(0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

package net.consensys.linea.extradata;

import java.util.Optional;

import com.google.auto.service.AutoService;
import lombok.extern.slf4j.Slf4j;
import net.consensys.linea.AbstractLineaRequiredPlugin;
import org.hyperledger.besu.plugin.BesuContext;
import org.hyperledger.besu.plugin.BesuPlugin;
import org.hyperledger.besu.plugin.services.BesuEvents;

/** 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;

@Override
public Optional<String> getName() {
return Optional.of(NAME);
}

@Override
public void doRegister(final BesuContext context) {
besuEventsService =
context
.getService(BesuEvents.class)
.orElseThrow(
() -> new RuntimeException("Failed to obtain BesuEvents from the BesuContext."));
}

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

0 comments on commit a1d0dba

Please sign in to comment.