-
Notifications
You must be signed in to change notification settings - Fork 0
add sqlite adapter #29
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
Open
Marketen
wants to merge
3
commits into
main
Choose a base branch
from
marc/add-duties-db
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,109 @@ | ||||||||||||||||||||||||||||||||||||||
| package sqlite | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||
| "context" | ||||||||||||||||||||||||||||||||||||||
| "database/sql" // basic sql | ||||||||||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| _ "github.com/mattn/go-sqlite3" // additional driver for sqlite | ||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // Implements ports.ValidatorStoragePort | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| type SQLiteStorage struct { | ||||||||||||||||||||||||||||||||||||||
| DB *sql.DB | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| func NewSQLiteStorage(dbPath string) (*SQLiteStorage, error) { | ||||||||||||||||||||||||||||||||||||||
| db, err := sql.Open("sqlite3", dbPath) | ||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||
| return nil, fmt.Errorf("failed to open sqlite db: %w", err) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| if err := migrate(db); err != nil { | ||||||||||||||||||||||||||||||||||||||
| return nil, fmt.Errorf("failed to migrate sqlite db: %w", err) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| return &SQLiteStorage{DB: db}, nil | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| func migrate(db *sql.DB) error { | ||||||||||||||||||||||||||||||||||||||
| queries := []string{ | ||||||||||||||||||||||||||||||||||||||
| `CREATE TABLE IF NOT EXISTS validator_epoch_status ( | ||||||||||||||||||||||||||||||||||||||
| validator_index INTEGER NOT NULL, | ||||||||||||||||||||||||||||||||||||||
| epoch INTEGER NOT NULL, | ||||||||||||||||||||||||||||||||||||||
| liveness BOOLEAN, | ||||||||||||||||||||||||||||||||||||||
| in_sync_committee BOOLEAN, | ||||||||||||||||||||||||||||||||||||||
| sync_committee_reward INTEGER, | ||||||||||||||||||||||||||||||||||||||
| attestation_reward INTEGER, | ||||||||||||||||||||||||||||||||||||||
| slashed BOOLEAN, | ||||||||||||||||||||||||||||||||||||||
| updated_at DATETIME DEFAULT CURRENT_TIMESTAMP, | ||||||||||||||||||||||||||||||||||||||
| PRIMARY KEY (validator_index, epoch) | ||||||||||||||||||||||||||||||||||||||
| );`, | ||||||||||||||||||||||||||||||||||||||
| `CREATE TABLE IF NOT EXISTS validator_block_proposals ( | ||||||||||||||||||||||||||||||||||||||
| validator_index INTEGER NOT NULL, | ||||||||||||||||||||||||||||||||||||||
| slot INTEGER NOT NULL, | ||||||||||||||||||||||||||||||||||||||
| epoch INTEGER NOT NULL, | ||||||||||||||||||||||||||||||||||||||
| block_reward INTEGER, | ||||||||||||||||||||||||||||||||||||||
| PRIMARY KEY (validator_index, slot) | ||||||||||||||||||||||||||||||||||||||
| );`, | ||||||||||||||||||||||||||||||||||||||
| `CREATE TABLE IF NOT EXISTS validators ( | ||||||||||||||||||||||||||||||||||||||
| validator_index INTEGER PRIMARY KEY, | ||||||||||||||||||||||||||||||||||||||
| label TEXT, | ||||||||||||||||||||||||||||||||||||||
| added_at DATETIME DEFAULT CURRENT_TIMESTAMP | ||||||||||||||||||||||||||||||||||||||
| );`, | ||||||||||||||||||||||||||||||||||||||
| `CREATE INDEX IF NOT EXISTS idx_epoch ON validator_epoch_status(epoch);`, | ||||||||||||||||||||||||||||||||||||||
| `CREATE INDEX IF NOT EXISTS idx_validator_epoch ON validator_epoch_status(validator_index, epoch);`, | ||||||||||||||||||||||||||||||||||||||
| `CREATE INDEX IF NOT EXISTS idx_proposals_epoch ON validator_block_proposals(epoch);`, | ||||||||||||||||||||||||||||||||||||||
| `CREATE INDEX IF NOT EXISTS idx_proposals_slot ON validator_block_proposals(slot);`, | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| for _, q := range queries { | ||||||||||||||||||||||||||||||||||||||
| if _, err := db.Exec(q); err != nil { | ||||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+58
to
+62
|
||||||||||||||||||||||||||||||||||||||
| for _, q := range queries { | |
| if _, err := db.Exec(q); err != nil { | |
| return err | |
| } | |
| } | |
| tx, err := db.Begin() | |
| if err != nil { | |
| return err | |
| } | |
| for _, q := range queries { | |
| if _, err := tx.Exec(q); err != nil { | |
| tx.Rollback() | |
| return err | |
| } | |
| } | |
| if err := tx.Commit(); err != nil { | |
| return err | |
| } |
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,18 @@ | ||
| package ports | ||
|
|
||
| import ( | ||
| "context" | ||
| ) | ||
|
|
||
| // ValidatorStoragePort defines methods for persisting validator duty and proposal data | ||
| // in a hexagonal architecture. | ||
| type ValidatorStoragePort interface { | ||
| // UpsertValidatorEpochStatus inserts or updates validator epoch status. | ||
| UpsertValidatorEpochStatus(ctx context.Context, valIndex uint64, epoch uint64, liveness *bool, inSyncCommittee *bool, syncCommitteeReward *uint64, attestationReward *uint64, slashed *bool) error | ||
|
|
||
| // UpsertValidatorBlockProposal inserts or updates a block proposal for a validator. | ||
| UpsertValidatorBlockProposal(ctx context.Context, valIndex uint64, slot uint64, epoch uint64, blockReward *uint64) error | ||
|
|
||
| // UpsertValidatorMetadata inserts or updates validator metadata. | ||
| UpsertValidatorMetadata(ctx context.Context, valIndex uint64, label *string) error | ||
| } |
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.
The function creates a map of all sync committee members but doesn't filter for the requested validator indices. This returns membership status for all validators in the sync committee instead of just the ones being tracked. The function should only return results for validators in the
indicesparameter.