Skip to content

Commit 1672e25

Browse files
committed
feat: add FinalizedBlockLag constant and update setFinalWithHeight logic
1 parent 182e768 commit 1672e25

2 files changed

Lines changed: 47 additions & 8 deletions

File tree

execution/evm/execution.go

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ const (
3838
// under normal operation. A value of 2 means when head is at block N,
3939
// safe is at block N-2.
4040
SafeBlockLag = 2
41+
// FinalizedBlockLag is the number of blocks the finalized block lags behind head.
42+
// This is a temporary mock value until proper DA-based finalization is wired up.
43+
// A value of 3 means when head is at block N, finalized is at block N-3.
44+
FinalizedBlockLag = 3
4145
)
4246

4347
var (
@@ -419,12 +423,18 @@ func (c *EngineClient) setFinal(ctx context.Context, blockHash common.Hash, isFi
419423
return c.setFinalWithHeight(ctx, blockHash, 0, isFinal)
420424
}
421425

422-
// setFinalWithHeight updates forkchoice state with optional safe block lagging.
423-
// When isFinal=false and headHeight > SafeBlockLag, the safe block is automatically
424-
// set to headHeight - SafeBlockLag blocks behind head.
426+
// setFinalWithHeight updates forkchoice state with safe and finalized block lagging.
427+
// When isFinal=false:
428+
// - Safe block is set to headHeight - SafeBlockLag (when headHeight > SafeBlockLag)
429+
// - Finalized block is set to headHeight - FinalizedBlockLag (when headHeight > FinalizedBlockLag)
430+
//
431+
// Note: The finalized lag is a temporary mock until proper DA-based finalization is wired up.
425432
func (c *EngineClient) setFinalWithHeight(ctx context.Context, blockHash common.Hash, headHeight uint64, isFinal bool) error {
426-
var safeHash common.Hash
433+
var safeHash, finalizedHash common.Hash
427434
updateSafe := !isFinal && headHeight > SafeBlockLag
435+
updateFinalized := !isFinal && headHeight > FinalizedBlockLag
436+
437+
// Look up safe block hash
428438
if updateSafe {
429439
safeHeight := headHeight - SafeBlockLag
430440

@@ -446,6 +456,28 @@ func (c *EngineClient) setFinalWithHeight(ctx context.Context, blockHash common.
446456
}
447457
}
448458

459+
// Look up finalized block hash
460+
if updateFinalized {
461+
finalizedHeight := headHeight - FinalizedBlockLag
462+
463+
c.mu.Lock()
464+
cachedFinalizedHash, ok := c.blockHashCache[finalizedHeight]
465+
c.mu.Unlock()
466+
if ok {
467+
finalizedHash = cachedFinalizedHash
468+
} else {
469+
var err error
470+
finalizedHash, _, _, _, err = c.getBlockInfo(ctx, finalizedHeight)
471+
if err != nil {
472+
c.logger.Debug().
473+
Uint64("finalizedHeight", finalizedHeight).
474+
Err(err).
475+
Msg("setFinalWithHeight: finalized block not found, skipping finalized update")
476+
updateFinalized = false
477+
}
478+
}
479+
}
480+
449481
c.mu.Lock()
450482
if isFinal {
451483
c.currentFinalizedBlockHash = blockHash
@@ -456,6 +488,9 @@ func (c *EngineClient) setFinalWithHeight(ctx context.Context, blockHash common.
456488
if updateSafe {
457489
c.currentSafeBlockHash = safeHash
458490
}
491+
if updateFinalized {
492+
c.currentFinalizedBlockHash = finalizedHash
493+
}
459494
}
460495
args := engine.ForkchoiceStateV1{
461496
HeadBlockHash: c.currentHeadBlockHash,

execution/evm/test/execution_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"github.com/ethereum/go-ethereum/common"
1111
ethTypes "github.com/ethereum/go-ethereum/core/types"
1212
"github.com/ethereum/go-ethereum/ethclient"
13+
ds "github.com/ipfs/go-datastore"
14+
dssync "github.com/ipfs/go-datastore/sync"
1315
"github.com/stretchr/testify/require"
1416

1517
"github.com/evstack/ev-node/execution/evm"
@@ -64,15 +66,16 @@ func TestEngineExecution(t *testing.T) {
6466
ethURL := "http://127.0.0.1:" + ni.External.Ports.RPC
6567
engineURL := "http://127.0.0.1:" + ni.External.Ports.Engine
6668

69+
store := dssync.MutexWrap(ds.NewMapDatastore())
6770
executionClient, err := evm.NewEngineExecutionClient(
6871
ethURL,
6972
engineURL,
7073
rethNode.JWTSecretHex(),
7174
genesisHash,
7275
common.Address{},
73-
nil, // store - ExecMeta tracking disabled for tests
76+
store,
7477
)
75-
require.NoError(t, err)
78+
require.NoError(tt, err)
7679

7780
ctx, cancel := context.WithTimeout(context.Background(), 300*time.Second)
7881
defer cancel()
@@ -163,15 +166,16 @@ func TestEngineExecution(t *testing.T) {
163166
ethURL := "http://127.0.0.1:" + ni.External.Ports.RPC
164167
engineURL := "http://127.0.0.1:" + ni.External.Ports.Engine
165168

169+
store := dssync.MutexWrap(ds.NewMapDatastore())
166170
executionClient, err := evm.NewEngineExecutionClient(
167171
ethURL,
168172
engineURL,
169173
rethNode.JWTSecretHex(),
170174
genesisHash,
171175
common.Address{},
172-
nil, // store - ExecMeta tracking disabled for tests
176+
store,
173177
)
174-
require.NoError(t, err)
178+
require.NoError(tt, err)
175179

176180
ctx, cancel := context.WithTimeout(context.Background(), 300*time.Second)
177181
defer cancel()

0 commit comments

Comments
 (0)