Skip to content

Commit

Permalink
feat: sequencer
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkAfCod committed May 10, 2024
1 parent 37b1eaf commit 387aef7
Show file tree
Hide file tree
Showing 12 changed files with 458 additions and 65 deletions.
37 changes: 37 additions & 0 deletions hildr-node/src/main/java/io/optimism/common/CriticalException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.optimism.common;

/**
* The sequencer critical exception.
* @author thinkAfCod
* @since 0.4.1
*/
public class CriticalException extends SequencerException {

/**
* Instantiates a new sequencer critical exception.
*
* @param message the message
*/
public CriticalException(String message) {
super(message);
}

/**
* Instantiates a new sequencer critical exception.
*
* @param message the message
* @param cause the cause
*/
public CriticalException(String message, Throwable cause) {
super(message, cause);
}

/**
* Instantiates a new sequencer critical exception.
*
* @param cause the cause
*/
public CriticalException(Throwable cause) {
super(cause);
}
}
37 changes: 37 additions & 0 deletions hildr-node/src/main/java/io/optimism/common/ResetException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.optimism.common;

/**
* The sequencer reset exception.
* @author thinkAfCod
* @since 0.4.1
*/
public class ResetException extends SequencerException {

/**
* Instantiates a new sequencer reset exception.
*
* @param message the message
*/
public ResetException(String message) {
super(message);
}

/**
* Instantiates a new sequencer reset exception.
*
* @param message the message
* @param cause the cause
*/
public ResetException(String message, Throwable cause) {
super(message, cause);
}

/**
* Instantiates a new sequencer reset exception.
*
* @param cause the cause
*/
public ResetException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.optimism.common;

/**
* the sequencer exception.
* @author thinkAfCod
* @since 0.4.1
*/
public class SequencerException extends RuntimeException {

/**
* Instantiates a new sequencer exception.
*
* @param message the message
*/
public SequencerException(String message) {
super(message);
}

/**
* Instantiates a new sequencer exception.
*
* @param message the message
* @param cause the cause
*/
public SequencerException(String message, Throwable cause) {
super(message, cause);
}

/**
* Instantiates a new sequencer exception.
*
* @param cause the cause
*/
public SequencerException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.optimism.common;

/**
* The sequencer temporary exception.
* @author thinkAfCod
* @since 0.4.1
*/
public class TemporaryException extends SequencerException {

/**
* Instantiates a new sequencer temporary exception.
*
* @param message the message
*/
public TemporaryException(String message) {
super(message);
}

/**
* Instantiates a new sequencer temporary exception.
*
* @param message the message
* @param cause the cause
*/
public TemporaryException(String message, Throwable cause) {
super(message, cause);
}

/**
* Instantiates a new sequencer temporary exception.
*
* @param cause the cause
*/
public TemporaryException(Throwable cause) {
super(cause);
}
}
4 changes: 2 additions & 2 deletions hildr-node/src/main/java/io/optimism/driver/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public static Driver<EngineApi> from(Config config, CountDownLatch latch)
OpStackNetwork opStackNetwork = new OpStackNetwork(config.chainConfig(), unsafeBlockQueue);
ISequencer sequencer = null;
if (config.sequencerEnable()) {
sequencer = new Sequencer();
sequencer = new Sequencer(engineDriver, config.chainConfig());
}

l2Provider.shutdown();
Expand Down Expand Up @@ -422,7 +422,7 @@ private void awaitEngineReady() throws InterruptedException {
}

private void sequencerAction() throws InterruptedException, ExecutionException {
if (!this.isP2PNetworkStarted.get()) {
if (sequencer == null || !this.isP2PNetworkStarted.get()) {
return;
}
try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
Expand Down
76 changes: 76 additions & 0 deletions hildr-node/src/main/java/io/optimism/driver/EngineDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
import io.optimism.engine.OpEthExecutionPayload;
import io.optimism.engine.OpEthForkChoiceUpdate;
import io.optimism.engine.OpEthPayloadStatus;
import io.optimism.network.ExecutionPayloadEnvelop;
import io.optimism.type.Epoch;
import io.optimism.type.L2BlockRef;
import io.optimism.type.PayloadInfo;
import io.optimism.type.enums.BlockInsertion;
import io.optimism.type.enums.SyncStatus;
import io.optimism.utilities.telemetry.TracerTaskWrapper;
import java.math.BigInteger;
Expand All @@ -31,6 +34,9 @@
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.EthBlock;
import org.web3j.protocol.core.methods.response.EthBlock.TransactionObject;
import org.web3j.tuples.generated.Tuple2;
import org.web3j.tuples.generated.Tuple3;
import org.web3j.utils.Numeric;

/**
* The type EngineDriver.
Expand Down Expand Up @@ -63,6 +69,13 @@ public class EngineDriver<E extends Engine> {
private Epoch finalizedEpoch;

private SyncStatus syncStatus;

// building state
private L2BlockRef buildingOnto;
private PayloadInfo buildingInfo;
private boolean buildingSafe;
private PayloadAttributes safeAttrs;

/**
* Instantiates a new Engine driver.
*
Expand Down Expand Up @@ -223,6 +236,69 @@ public void handleUnsafePayload(ExecutionPayload payload) throws ExecutionExcept
}
}

/**
* Start building payload block insertion.
*
* @param parent the parent
* @param attributes the attributes
* @return the block insertion
*/
public BlockInsertion startBuildingPayload(L2BlockRef parent, PayloadAttributes attributes) {
if (this.isEngineSyncing()) {
return BlockInsertion.TEMPORARY;
}
return BlockInsertion.SUCCESS;
}

/**
* Confirm building payload.
*
* @return ExecutionPayloadEnvelop and insertion status
*/
public Tuple2<ExecutionPayloadEnvelop, BlockInsertion> confirmBuildingPayload() {
return null;
}

/**
* Cancel building payload.
* @param force if true then reset building state forcefully, otherwise throw exception
*/
public void cancelPayload(boolean force) {
if (this.buildingInfo == null) {
return;
}
// e.log.Error("cancelling old block sealing job", "payload", e.buildingInfo.ID)
LOGGER.error("cancelling old block sealing job: payload = {}", this.buildingInfo.payloadId());
try {
this.engine.getPayload(this.buildingInfo.timestamp(), Numeric.toBigInt(this.buildingInfo.payloadId()));
} catch (Exception e) {
if (!force) {
throw new RuntimeException(e);
}
LOGGER.error("failed to cancel block building job: payload = {}", this.buildingInfo.payloadId(), e);
}
this.resetBuildingState();
}

/**
* Reset building state.
*/
public void resetBuildingState() {
this.buildingInfo = null;
this.buildingOnto = null;
this.buildingSafe = false;
this.safeAttrs = null;
}

/**
* Gets building payload info.
*
* @return the building payload
*/
public Tuple3<L2BlockRef, String, Boolean> buildingPayload() {
return new Tuple3<>(buildingOnto, buildingInfo.payloadId(), buildingSafe);
}

/**
* Update finalized.
*
Expand Down
48 changes: 17 additions & 31 deletions hildr-node/src/main/java/io/optimism/driver/ISequencer.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
/*
* Copyright 2023 q315xia@163.com
*
* 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.
*/

package io.optimism.driver;

import io.optimism.network.ExecutionPayloadEnvelop;
import io.optimism.type.L2BlockRef;
import java.time.Duration;

/**
* The sequencer Interface.
* @author thinkAfCod
* @since 0.4.1
*/
public interface ISequencer {

/**
* Starts new block building work, or seals existing work,
* and is best timed by first awaiting the delay returned by PlanNextSequencerAction.
* If a new block is successfully sealed, it will be returned for publishing, null otherwise.
*
* @return the execution payload envelop
*/
ExecutionPayloadEnvelop runNextSequencerAction();

/**
* Returns a desired delay till the RunNextSequencerAction call.
*
* @return the next start sequencer action duration
*/
long planNextSequencerAction();

/**
* Initiates a block building job on top of the given L2 head, safe and finalized blocks, and using the provided l1Origin.
*/
Expand All @@ -39,20 +39,6 @@ public interface ISequencer {
*/
ExecutionPayloadEnvelop completeBuildingBlock();

/**
* Returns a desired delay till the RunNextSequencerAction call.
* @return the next start sequencer action duration
*/
Duration planNextSequencerAction();

/**
* Starts new block building work, or seals existing work,
* and is best timed by first awaiting the delay returned by PlanNextSequencerAction.
* If a new block is successfully sealed, it will be returned for publishing, null otherwise.
* @return the execution payload envelop
*/
ExecutionPayloadEnvelop runNextSequencerAction();

/**
* Returns the L2 head reference that the latest block is or was being built on top of.
* @return the L2 head reference
Expand Down
Loading

0 comments on commit 387aef7

Please sign in to comment.