|
5 | 5 | "time" |
6 | 6 |
|
7 | 7 | "github.com/ethereum-optimism/optimism/op-service/eth" |
| 8 | + "github.com/ethereum/go-ethereum/common" |
8 | 9 | ) |
9 | 10 |
|
10 | 11 | type PayloadSuccessEvent struct { |
@@ -51,5 +52,47 @@ func (e *EngineController) onPayloadSuccess(ctx context.Context, ev PayloadSucce |
51 | 52 | err := e.tryUpdateEngineInternal(ctx) |
52 | 53 | if err != nil { |
53 | 54 | e.log.Error("Failed to update engine", "error", err) |
| 55 | + } else { |
| 56 | + updateEngineFinish := time.Now() |
| 57 | + e.logBlockProcessingMetrics(updateEngineFinish, ev) |
54 | 58 | } |
55 | 59 | } |
| 60 | + |
| 61 | +func (e *EngineController) logBlockProcessingMetrics(updateEngineFinish time.Time, ev PayloadSuccessEvent) { |
| 62 | + // Protect against nil pointer dereferences |
| 63 | + if ev.Envelope == nil || ev.Envelope.ExecutionPayload == nil { |
| 64 | + e.log.Info("Envelope.ExecutionPayload not found, skipping block processing metrics") |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + mgas := float64(ev.Envelope.ExecutionPayload.GasUsed) / 1e6 |
| 69 | + buildTime := time.Duration(0) |
| 70 | + insertTime := updateEngineFinish.Sub(ev.InsertStarted) |
| 71 | + totalTime := insertTime |
| 72 | + |
| 73 | + // BuildStarted may be zero if sequencer already built + gossiped a block, but failed during |
| 74 | + // insertion and needed a retry of the insertion. In that case we use the default values above, |
| 75 | + // otherwise we calculate buildTime and totalTime below |
| 76 | + if !ev.BuildStarted.IsZero() { |
| 77 | + buildTime = ev.InsertStarted.Sub(ev.BuildStarted) |
| 78 | + totalTime = updateEngineFinish.Sub(ev.BuildStarted) |
| 79 | + } |
| 80 | + |
| 81 | + // Protect against divide-by-zero |
| 82 | + var mgasps float64 // Mgas/s |
| 83 | + if totalTime > 0 { |
| 84 | + // Calculate "block-processing" Mgas/s. |
| 85 | + // NOTE: "realtime" mgasps (chain throughput) is a different calculation: (GasUsed / blockPeriod) |
| 86 | + mgasps = mgas / totalTime.Seconds() |
| 87 | + } |
| 88 | + |
| 89 | + e.log.Info("Inserted new L2 unsafe block", |
| 90 | + "hash", ev.Envelope.ExecutionPayload.BlockHash, |
| 91 | + "number", uint64(ev.Envelope.ExecutionPayload.BlockNumber), |
| 92 | + "build_time", common.PrettyDuration(buildTime), |
| 93 | + "insert_time", common.PrettyDuration(insertTime), |
| 94 | + "total_time", common.PrettyDuration(totalTime), |
| 95 | + "mgas", mgas, |
| 96 | + "mgasps", mgasps, |
| 97 | + ) |
| 98 | +} |
0 commit comments