Skip to content

Commit 387aef7

Browse files
committed
feat: sequencer
1 parent 37b1eaf commit 387aef7

File tree

12 files changed

+458
-65
lines changed

12 files changed

+458
-65
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.optimism.common;
2+
3+
/**
4+
* The sequencer critical exception.
5+
* @author thinkAfCod
6+
* @since 0.4.1
7+
*/
8+
public class CriticalException extends SequencerException {
9+
10+
/**
11+
* Instantiates a new sequencer critical exception.
12+
*
13+
* @param message the message
14+
*/
15+
public CriticalException(String message) {
16+
super(message);
17+
}
18+
19+
/**
20+
* Instantiates a new sequencer critical exception.
21+
*
22+
* @param message the message
23+
* @param cause the cause
24+
*/
25+
public CriticalException(String message, Throwable cause) {
26+
super(message, cause);
27+
}
28+
29+
/**
30+
* Instantiates a new sequencer critical exception.
31+
*
32+
* @param cause the cause
33+
*/
34+
public CriticalException(Throwable cause) {
35+
super(cause);
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.optimism.common;
2+
3+
/**
4+
* The sequencer reset exception.
5+
* @author thinkAfCod
6+
* @since 0.4.1
7+
*/
8+
public class ResetException extends SequencerException {
9+
10+
/**
11+
* Instantiates a new sequencer reset exception.
12+
*
13+
* @param message the message
14+
*/
15+
public ResetException(String message) {
16+
super(message);
17+
}
18+
19+
/**
20+
* Instantiates a new sequencer reset exception.
21+
*
22+
* @param message the message
23+
* @param cause the cause
24+
*/
25+
public ResetException(String message, Throwable cause) {
26+
super(message, cause);
27+
}
28+
29+
/**
30+
* Instantiates a new sequencer reset exception.
31+
*
32+
* @param cause the cause
33+
*/
34+
public ResetException(Throwable cause) {
35+
super(cause);
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.optimism.common;
2+
3+
/**
4+
* the sequencer exception.
5+
* @author thinkAfCod
6+
* @since 0.4.1
7+
*/
8+
public class SequencerException extends RuntimeException {
9+
10+
/**
11+
* Instantiates a new sequencer exception.
12+
*
13+
* @param message the message
14+
*/
15+
public SequencerException(String message) {
16+
super(message);
17+
}
18+
19+
/**
20+
* Instantiates a new sequencer exception.
21+
*
22+
* @param message the message
23+
* @param cause the cause
24+
*/
25+
public SequencerException(String message, Throwable cause) {
26+
super(message, cause);
27+
}
28+
29+
/**
30+
* Instantiates a new sequencer exception.
31+
*
32+
* @param cause the cause
33+
*/
34+
public SequencerException(Throwable cause) {
35+
super(cause);
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.optimism.common;
2+
3+
/**
4+
* The sequencer temporary exception.
5+
* @author thinkAfCod
6+
* @since 0.4.1
7+
*/
8+
public class TemporaryException extends SequencerException {
9+
10+
/**
11+
* Instantiates a new sequencer temporary exception.
12+
*
13+
* @param message the message
14+
*/
15+
public TemporaryException(String message) {
16+
super(message);
17+
}
18+
19+
/**
20+
* Instantiates a new sequencer temporary exception.
21+
*
22+
* @param message the message
23+
* @param cause the cause
24+
*/
25+
public TemporaryException(String message, Throwable cause) {
26+
super(message, cause);
27+
}
28+
29+
/**
30+
* Instantiates a new sequencer temporary exception.
31+
*
32+
* @param cause the cause
33+
*/
34+
public TemporaryException(Throwable cause) {
35+
super(cause);
36+
}
37+
}

hildr-node/src/main/java/io/optimism/driver/Driver.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public static Driver<EngineApi> from(Config config, CountDownLatch latch)
247247
OpStackNetwork opStackNetwork = new OpStackNetwork(config.chainConfig(), unsafeBlockQueue);
248248
ISequencer sequencer = null;
249249
if (config.sequencerEnable()) {
250-
sequencer = new Sequencer();
250+
sequencer = new Sequencer(engineDriver, config.chainConfig());
251251
}
252252

253253
l2Provider.shutdown();
@@ -422,7 +422,7 @@ private void awaitEngineReady() throws InterruptedException {
422422
}
423423

424424
private void sequencerAction() throws InterruptedException, ExecutionException {
425-
if (!this.isP2PNetworkStarted.get()) {
425+
if (sequencer == null || !this.isP2PNetworkStarted.get()) {
426426
return;
427427
}
428428
try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {

hildr-node/src/main/java/io/optimism/driver/EngineDriver.java

+76
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
import io.optimism.engine.OpEthExecutionPayload;
1414
import io.optimism.engine.OpEthForkChoiceUpdate;
1515
import io.optimism.engine.OpEthPayloadStatus;
16+
import io.optimism.network.ExecutionPayloadEnvelop;
1617
import io.optimism.type.Epoch;
1718
import io.optimism.type.L2BlockRef;
19+
import io.optimism.type.PayloadInfo;
20+
import io.optimism.type.enums.BlockInsertion;
1821
import io.optimism.type.enums.SyncStatus;
1922
import io.optimism.utilities.telemetry.TracerTaskWrapper;
2023
import java.math.BigInteger;
@@ -31,6 +34,9 @@
3134
import org.web3j.protocol.core.DefaultBlockParameterName;
3235
import org.web3j.protocol.core.methods.response.EthBlock;
3336
import org.web3j.protocol.core.methods.response.EthBlock.TransactionObject;
37+
import org.web3j.tuples.generated.Tuple2;
38+
import org.web3j.tuples.generated.Tuple3;
39+
import org.web3j.utils.Numeric;
3440

3541
/**
3642
* The type EngineDriver.
@@ -63,6 +69,13 @@ public class EngineDriver<E extends Engine> {
6369
private Epoch finalizedEpoch;
6470

6571
private SyncStatus syncStatus;
72+
73+
// building state
74+
private L2BlockRef buildingOnto;
75+
private PayloadInfo buildingInfo;
76+
private boolean buildingSafe;
77+
private PayloadAttributes safeAttrs;
78+
6679
/**
6780
* Instantiates a new Engine driver.
6881
*
@@ -223,6 +236,69 @@ public void handleUnsafePayload(ExecutionPayload payload) throws ExecutionExcept
223236
}
224237
}
225238

239+
/**
240+
* Start building payload block insertion.
241+
*
242+
* @param parent the parent
243+
* @param attributes the attributes
244+
* @return the block insertion
245+
*/
246+
public BlockInsertion startBuildingPayload(L2BlockRef parent, PayloadAttributes attributes) {
247+
if (this.isEngineSyncing()) {
248+
return BlockInsertion.TEMPORARY;
249+
}
250+
return BlockInsertion.SUCCESS;
251+
}
252+
253+
/**
254+
* Confirm building payload.
255+
*
256+
* @return ExecutionPayloadEnvelop and insertion status
257+
*/
258+
public Tuple2<ExecutionPayloadEnvelop, BlockInsertion> confirmBuildingPayload() {
259+
return null;
260+
}
261+
262+
/**
263+
* Cancel building payload.
264+
* @param force if true then reset building state forcefully, otherwise throw exception
265+
*/
266+
public void cancelPayload(boolean force) {
267+
if (this.buildingInfo == null) {
268+
return;
269+
}
270+
// e.log.Error("cancelling old block sealing job", "payload", e.buildingInfo.ID)
271+
LOGGER.error("cancelling old block sealing job: payload = {}", this.buildingInfo.payloadId());
272+
try {
273+
this.engine.getPayload(this.buildingInfo.timestamp(), Numeric.toBigInt(this.buildingInfo.payloadId()));
274+
} catch (Exception e) {
275+
if (!force) {
276+
throw new RuntimeException(e);
277+
}
278+
LOGGER.error("failed to cancel block building job: payload = {}", this.buildingInfo.payloadId(), e);
279+
}
280+
this.resetBuildingState();
281+
}
282+
283+
/**
284+
* Reset building state.
285+
*/
286+
public void resetBuildingState() {
287+
this.buildingInfo = null;
288+
this.buildingOnto = null;
289+
this.buildingSafe = false;
290+
this.safeAttrs = null;
291+
}
292+
293+
/**
294+
* Gets building payload info.
295+
*
296+
* @return the building payload
297+
*/
298+
public Tuple3<L2BlockRef, String, Boolean> buildingPayload() {
299+
return new Tuple3<>(buildingOnto, buildingInfo.payloadId(), buildingSafe);
300+
}
301+
226302
/**
227303
* Update finalized.
228304
*

hildr-node/src/main/java/io/optimism/driver/ISequencer.java

+17-31
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
/*
2-
* Copyright 2023 [email protected]
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with
6-
* the License. You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing,
11-
* software distributed under the License is distributed on
12-
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13-
* either express or implied. See the License for the
14-
* specific language governing permissions and limitations under the License.
15-
*/
16-
171
package io.optimism.driver;
182

193
import io.optimism.network.ExecutionPayloadEnvelop;
204
import io.optimism.type.L2BlockRef;
21-
import java.time.Duration;
225

236
/**
247
* The sequencer Interface.
258
* @author thinkAfCod
269
* @since 0.4.1
2710
*/
2811
public interface ISequencer {
12+
13+
/**
14+
* Starts new block building work, or seals existing work,
15+
* and is best timed by first awaiting the delay returned by PlanNextSequencerAction.
16+
* If a new block is successfully sealed, it will be returned for publishing, null otherwise.
17+
*
18+
* @return the execution payload envelop
19+
*/
20+
ExecutionPayloadEnvelop runNextSequencerAction();
21+
22+
/**
23+
* Returns a desired delay till the RunNextSequencerAction call.
24+
*
25+
* @return the next start sequencer action duration
26+
*/
27+
long planNextSequencerAction();
28+
2929
/**
3030
* Initiates a block building job on top of the given L2 head, safe and finalized blocks, and using the provided l1Origin.
3131
*/
@@ -39,20 +39,6 @@ public interface ISequencer {
3939
*/
4040
ExecutionPayloadEnvelop completeBuildingBlock();
4141

42-
/**
43-
* Returns a desired delay till the RunNextSequencerAction call.
44-
* @return the next start sequencer action duration
45-
*/
46-
Duration planNextSequencerAction();
47-
48-
/**
49-
* Starts new block building work, or seals existing work,
50-
* and is best timed by first awaiting the delay returned by PlanNextSequencerAction.
51-
* If a new block is successfully sealed, it will be returned for publishing, null otherwise.
52-
* @return the execution payload envelop
53-
*/
54-
ExecutionPayloadEnvelop runNextSequencerAction();
55-
5642
/**
5743
* Returns the L2 head reference that the latest block is or was being built on top of.
5844
* @return the L2 head reference

0 commit comments

Comments
 (0)