Uh oh!
There was an error while loading. Please reload this page.
- 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
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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
CopilotAI | ||||||||||||||||||||||||||||||||||||||
| for_, q:=rangequeries { | |
| if_, err:=db.Exec(q); err!=nil { | |
| returnerr | |
| } | |
| } | |
| tx, err:=db.Begin() | |
| iferr!=nil { | |
| returnerr | |
| } | |
| for_, q:=rangequeries { | |
| if_, err:=tx.Exec(q); err!=nil { | |
| tx.Rollback() | |
| returnerr | |
| } | |
| } | |
| iferr:=tx.Commit(); err!=nil { | |
| returnerr | |
| } |
| 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 | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
CopilotAISep 19, 2025
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.