-
Notifications
You must be signed in to change notification settings - Fork 872
Add exporter for FlatKV and CompositeSC #3064
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
10 commits
Select commit
Hold shift + click to select a range
a16dd03
Add exporter for FlatKV and CompositeSC
yzang2019 5209b6e
Fix test
yzang2019 014a4f4
Merge branch 'main' into yzang/add-flatkv-export
yzang2019 bbc9d4c
Add error checking
yzang2019 873f066
Add more unit test
yzang2019 e71e06d
Fix SS import
yzang2019 340caa5
Add import unit test
yzang2019 48fbc3b
Address comments
yzang2019 9280c5a
Merge branch 'main' into yzang/add-flatkv-export
yzang2019 0176dfb
Merge branch 'main' into yzang/add-flatkv-export
yzang2019 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| package composite | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| errorutils "github.com/sei-protocol/sei-chain/sei-db/common/errors" | ||
| "github.com/sei-protocol/sei-chain/sei-db/state_db/sc/types" | ||
| ) | ||
|
|
||
| var _ types.Exporter = (*SnapshotExporter)(nil) | ||
|
|
||
| type exportPhase int | ||
|
|
||
| const ( | ||
| phaseCosmos exportPhase = iota | ||
| phaseFlatKV | ||
| phaseDone | ||
| ) | ||
|
|
||
| // SnapshotExporter coordinates export from cosmos (memiavl) and flatKV backends. | ||
| // | ||
| // Next() returns items in stream order. Each item is either: | ||
| // - string: a module name header that starts a new module section | ||
| // - *types.SnapshotNode: a leaf key/value belonging to the current module | ||
| // | ||
| // FlatKV data is exported as a separate "evm_flatkv" module appended after all | ||
| // cosmos modules complete. This keeps the two backends fully independent in the | ||
| // snapshot stream. | ||
| type SnapshotExporter struct { | ||
| cosmosExporter types.Exporter | ||
| evmExporter types.Exporter | ||
| phase exportPhase | ||
| } | ||
|
|
||
| // NewExporter creates a composite exporter. cosmosExporter must not be nil. | ||
| // evmExporter may be nil when FlatKV is not active. | ||
| func NewExporter(cosmosExporter types.Exporter, evmExporter types.Exporter) (*SnapshotExporter, error) { | ||
| if cosmosExporter == nil { | ||
| return nil, fmt.Errorf("cosmosExporter must not be nil") | ||
| } | ||
| return &SnapshotExporter{ | ||
| cosmosExporter: cosmosExporter, | ||
| evmExporter: evmExporter, | ||
| phase: phaseCosmos, | ||
| }, nil | ||
| } | ||
|
|
||
| // Next returns the next item in the composite snapshot stream. | ||
| // | ||
| // The stream is split into two sequential phases: | ||
| // 1. phaseCosmos — drains all items from the cosmos (memiavl) exporter. | ||
| // When the cosmos exporter is exhausted, if a FlatKV exporter is present, | ||
| // the phase transitions to phaseFlatKV and emits the EVMFlatKVStoreName | ||
| // module header as the first item. | ||
| // 2. phaseFlatKV — drains all items from the FlatKV exporter. | ||
| // | ||
| // Returns ErrorExportDone when both phases are complete. | ||
| func (s *SnapshotExporter) Next() (interface{}, error) { | ||
| switch s.phase { | ||
| case phaseCosmos: | ||
| return s.nextFromCosmos() | ||
| case phaseFlatKV: | ||
| return s.nextFromFlatKV() | ||
| default: | ||
| return nil, errorutils.ErrorExportDone | ||
| } | ||
| } | ||
|
|
||
| // nextFromCosmos pulls items from the cosmos exporter. On exhaustion it | ||
| // transitions to phaseFlatKV (emitting the module header) or phaseDone. | ||
| func (s *SnapshotExporter) nextFromCosmos() (interface{}, error) { | ||
| item, err := s.cosmosExporter.Next() | ||
| if err != nil { | ||
| if !errors.Is(err, errorutils.ErrorExportDone) { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Cosmos done. Append flatKV as a separate module. | ||
| if s.evmExporter != nil { | ||
| s.phase = phaseFlatKV | ||
| return EVMFlatKVStoreName, nil | ||
| } | ||
|
|
||
| s.phase = phaseDone | ||
| return nil, errorutils.ErrorExportDone | ||
| } | ||
| return item, nil | ||
| } | ||
|
|
||
| // nextFromFlatKV pulls items from the FlatKV exporter. On exhaustion it | ||
| // transitions to phaseDone. | ||
| func (s *SnapshotExporter) nextFromFlatKV() (interface{}, error) { | ||
| item, err := s.evmExporter.Next() | ||
| if err != nil { | ||
| if !errors.Is(err, errorutils.ErrorExportDone) { | ||
| return nil, err | ||
| } | ||
| s.phase = phaseDone | ||
| return nil, errorutils.ErrorExportDone | ||
| } | ||
| return item, nil | ||
| } | ||
|
|
||
| func (s *SnapshotExporter) Close() error { | ||
| var errCosmos, errEVM error | ||
| if s.cosmosExporter != nil { | ||
| errCosmos = s.cosmosExporter.Close() | ||
| } | ||
| if s.evmExporter != nil { | ||
| errEVM = s.evmExporter.Close() | ||
| } | ||
| return errors.Join(errCosmos, errEVM) | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Short godoc explaining the methods in struct would be helpful. Purpose of these methods wasn't immediately obvious based on function names and context.