-
Notifications
You must be signed in to change notification settings - Fork 241
feat: replay mechanism to sync node with execution layer #2771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7f2c085
feat: implement execution layer synchronization and height checking
randygrok 1d9a4d7
fix: improve variable naming and enhance error messaging in syncExecu…
randygrok 16f5a59
refactor: rename HeightChecker to HeightProvider for clarity in execu…
randygrok 787dfb8
feat: implement ExecutionLayerSyncer for synchronizing execution laye…
randygrok 588ebe2
fix: update module replacements to include core path for consistency
randygrok 70640a6
go mod tidy
randygrok 8b6edad
fix: remove unnecessary reference to Mock in NewMockHeightAwareExecutor
randygrok 78ec2cf
fix: update app hash mismatch handling to return error instead of war…
randygrok a1af7ab
feat: implement ExecutionLayerReplayer for synchronizing execution la…
randygrok 3cf9f35
fix: enhance replayBlock documentation with validation assumptions
randygrok 0a19ef6
Merge branch 'main' into feat/sync-node-replay
randygrok 21f9e3b
rename replayer
randygrok efa1bd8
fix: update block-manager.md docs to include a reference to the execu…
randygrok File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| package common | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/hex" | ||
| "fmt" | ||
|
|
||
| "github.com/rs/zerolog" | ||
|
|
||
| coreexecutor "github.com/evstack/ev-node/core/execution" | ||
| "github.com/evstack/ev-node/pkg/genesis" | ||
| "github.com/evstack/ev-node/pkg/store" | ||
| "github.com/evstack/ev-node/types" | ||
| ) | ||
|
|
||
| // Replayer handles synchronization of the execution layer with ev-node's state. | ||
| // It replays blocks from the store to bring the execution layer up to date. | ||
| type Replayer struct { | ||
| store store.Store | ||
| exec coreexecutor.Executor | ||
| genesis genesis.Genesis | ||
| logger zerolog.Logger | ||
| } | ||
|
|
||
| // NewReplayer creates a new execution layer replayer. | ||
| func NewReplayer( | ||
| store store.Store, | ||
| exec coreexecutor.Executor, | ||
| genesis genesis.Genesis, | ||
| logger zerolog.Logger, | ||
| ) *Replayer { | ||
| return &Replayer{ | ||
| store: store, | ||
| exec: exec, | ||
| genesis: genesis, | ||
| logger: logger.With().Str("component", "execution_replayer").Logger(), | ||
| } | ||
| } | ||
|
|
||
| // SyncToHeight checks if the execution layer is behind ev-node and syncs it to the target height. | ||
| // This is useful for crash recovery scenarios where ev-node is ahead of the execution layer. | ||
| // | ||
| // Returns: | ||
| // - error if sync fails or if execution layer is ahead of ev-node (unexpected state) | ||
| func (s *Replayer) SyncToHeight(ctx context.Context, targetHeight uint64) error { | ||
| // Check if the executor implements HeightProvider | ||
| execHeightProvider, ok := s.exec.(coreexecutor.HeightProvider) | ||
| if !ok { | ||
| s.logger.Debug().Msg("executor does not implement HeightProvider, skipping sync") | ||
| return nil | ||
| } | ||
|
|
||
| // Skip sync check if we're at genesis | ||
| if targetHeight < s.genesis.InitialHeight { | ||
| s.logger.Debug().Msg("at genesis height, skipping execution layer sync check") | ||
| return nil | ||
| } | ||
|
|
||
| execHeight, err := execHeightProvider.GetLatestHeight(ctx) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get execution layer height: %w", err) | ||
| } | ||
|
|
||
| s.logger.Info(). | ||
| Uint64("target_height", targetHeight). | ||
| Uint64("exec_layer_height", execHeight). | ||
| Msg("execution layer height check") | ||
|
|
||
| // If execution layer is ahead, this is unexpected, fail hard | ||
| if execHeight > targetHeight { | ||
| s.logger.Error(). | ||
| Uint64("target_height", targetHeight). | ||
| Uint64("exec_layer_height", execHeight). | ||
| Msg("execution layer is ahead of target height - this should not happen") | ||
| return fmt.Errorf("execution layer height (%d) is ahead of target height (%d)", execHeight, targetHeight) | ||
| } | ||
|
|
||
| // If execution layer is behind, sync the missing blocks | ||
| if execHeight < targetHeight { | ||
| s.logger.Info(). | ||
| Uint64("target_height", targetHeight). | ||
| Uint64("exec_layer_height", execHeight). | ||
| Uint64("blocks_to_sync", targetHeight-execHeight). | ||
| Msg("execution layer is behind, syncing blocks") | ||
|
|
||
| // Sync blocks from execHeight+1 to targetHeight | ||
| for height := execHeight + 1; height <= targetHeight; height++ { | ||
| if err := s.replayBlock(ctx, height); err != nil { | ||
| return fmt.Errorf("failed to replay block %d to execution layer: %w", height, err) | ||
| } | ||
| } | ||
|
|
||
| s.logger.Info(). | ||
| Uint64("synced_blocks", targetHeight-execHeight). | ||
| Msg("successfully synced execution layer") | ||
| } else { | ||
| s.logger.Info().Msg("execution layer is in sync") | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // replayBlock replays a specific block from the store to the execution layer. | ||
| // | ||
| // Validation assumptions: | ||
| // - Blocks in the store have already been fully validated (signatures, timestamps, etc.) | ||
| // - We only verify the AppHash matches to detect state divergence | ||
| // - We skip re-validating signatures and consensus rules since this is a replay | ||
| // - This is safe because we're re-executing transactions against a known-good state | ||
| func (s *Replayer) replayBlock(ctx context.Context, height uint64) error { | ||
| s.logger.Info().Uint64("height", height).Msg("replaying block to execution layer") | ||
|
|
||
| // Get the block from store | ||
| header, data, err := s.store.GetBlockData(ctx, height) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get block data from store: %w", err) | ||
| } | ||
|
|
||
| // Get the previous state | ||
| var prevState types.State | ||
| if height == s.genesis.InitialHeight { | ||
| // For the first block, use genesis state | ||
| prevState = types.State{ | ||
| ChainID: s.genesis.ChainID, | ||
| InitialHeight: s.genesis.InitialHeight, | ||
| LastBlockHeight: s.genesis.InitialHeight - 1, | ||
| LastBlockTime: s.genesis.StartTime, | ||
| AppHash: header.AppHash, // This will be updated by InitChain | ||
| } | ||
| } else { | ||
| // Get previous state from store | ||
| prevState, err = s.store.GetState(ctx) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get previous state: %w", err) | ||
| } | ||
| // We need the state at height-1, so load that block's app hash | ||
| prevHeader, _, err := s.store.GetBlockData(ctx, height-1) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get previous block header: %w", err) | ||
| } | ||
| prevState.AppHash = prevHeader.AppHash | ||
| prevState.LastBlockHeight = height - 1 | ||
| } | ||
|
|
||
| // Prepare transactions | ||
| rawTxs := make([][]byte, len(data.Txs)) | ||
| for i, tx := range data.Txs { | ||
| rawTxs[i] = []byte(tx) | ||
| } | ||
|
|
||
| // Execute transactions on the execution layer | ||
| s.logger.Debug(). | ||
| Uint64("height", height). | ||
| Int("tx_count", len(rawTxs)). | ||
| Msg("executing transactions on execution layer") | ||
|
|
||
| newAppHash, _, err := s.exec.ExecuteTxs(ctx, rawTxs, height, header.Time(), prevState.AppHash) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to execute transactions: %w", err) | ||
| } | ||
|
|
||
| // Verify the app hash matches | ||
randygrok marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if !bytes.Equal(newAppHash, header.AppHash) { | ||
| err := fmt.Errorf("app hash mismatch: expected %s got %s", | ||
| hex.EncodeToString(header.AppHash), | ||
| hex.EncodeToString(newAppHash), | ||
| ) | ||
| s.logger.Error(). | ||
| Str("expected", hex.EncodeToString(header.AppHash)). | ||
| Str("got", hex.EncodeToString(newAppHash)). | ||
| Uint64("height", height). | ||
| Err(err). | ||
| Msg("app hash mismatch during replay") | ||
| return err | ||
| } | ||
|
|
||
| s.logger.Info(). | ||
| Uint64("height", height). | ||
| Msg("successfully replayed block to execution layer") | ||
|
|
||
| return nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.