Skip to content
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

Implement storage interface for sqlite #10

Merged
merged 6 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions internal/eigenState/avsOperators/avsOperators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/Layr-Labs/sidecar/internal/config"
"github.com/Layr-Labs/sidecar/internal/eigenState/stateManager"
"github.com/Layr-Labs/sidecar/internal/logger"
"github.com/Layr-Labs/sidecar/internal/sqlite/migrations"
"github.com/Layr-Labs/sidecar/internal/storage"
"github.com/Layr-Labs/sidecar/internal/tests"
"github.com/stretchr/testify/assert"
Expand All @@ -23,9 +24,16 @@ func setup() (
cfg := tests.GetConfig()
l, _ := logger.NewLogger(&logger.LoggerConfig{Debug: cfg.Debug})

_, grm, err := tests.GetDatabaseConnection(cfg)
db, err := tests.GetSqliteDatabaseConnection()
if err != nil {
panic(err)
}
sqliteMigrator := migrations.NewSqliteMigrator(db, l)
if err := sqliteMigrator.MigrateAll(); err != nil {
l.Sugar().Fatalw("Failed to migrate", "error", err)
}

return cfg, grm, l, err
return cfg, db, l, err
}

func teardown(model *AvsOperatorsModel) {
Expand Down
12 changes: 10 additions & 2 deletions internal/eigenState/eigenstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/Layr-Labs/sidecar/internal/eigenState/operatorShares"
"github.com/Layr-Labs/sidecar/internal/eigenState/stateManager"
"github.com/Layr-Labs/sidecar/internal/logger"
"github.com/Layr-Labs/sidecar/internal/sqlite/migrations"
"github.com/Layr-Labs/sidecar/internal/tests"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
Expand All @@ -23,9 +24,16 @@ func setup() (
cfg := tests.GetConfig()
l, _ := logger.NewLogger(&logger.LoggerConfig{Debug: cfg.Debug})

_, grm, err := tests.GetDatabaseConnection(cfg)
db, err := tests.GetSqliteDatabaseConnection()
if err != nil {
panic(err)
}
sqliteMigrator := migrations.NewSqliteMigrator(db, l)
if err := sqliteMigrator.MigrateAll(); err != nil {
l.Sugar().Fatalw("Failed to migrate", "error", err)
}

return cfg, grm, l, err
return cfg, db, l, err
}

func teardown(grm *gorm.DB) {
Expand Down
12 changes: 10 additions & 2 deletions internal/eigenState/operatorShares/operatorShares_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/Layr-Labs/sidecar/internal/config"
"github.com/Layr-Labs/sidecar/internal/eigenState/stateManager"
"github.com/Layr-Labs/sidecar/internal/logger"
"github.com/Layr-Labs/sidecar/internal/sqlite/migrations"
"github.com/Layr-Labs/sidecar/internal/storage"
"github.com/Layr-Labs/sidecar/internal/tests"
"github.com/stretchr/testify/assert"
Expand All @@ -25,9 +26,16 @@ func setup() (
cfg := tests.GetConfig()
l, _ := logger.NewLogger(&logger.LoggerConfig{Debug: cfg.Debug})

_, grm, err := tests.GetDatabaseConnection(cfg)
db, err := tests.GetSqliteDatabaseConnection()
if err != nil {
panic(err)
}
sqliteMigrator := migrations.NewSqliteMigrator(db, l)
if err := sqliteMigrator.MigrateAll(); err != nil {
l.Sugar().Fatalw("Failed to migrate", "error", err)
}

return cfg, grm, l, err
return cfg, db, l, err
}

func teardown(model *OperatorSharesModel) {
Expand Down
12 changes: 10 additions & 2 deletions internal/eigenState/stakerDelegations/stakerDelegations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/Layr-Labs/sidecar/internal/config"
"github.com/Layr-Labs/sidecar/internal/eigenState/stateManager"
"github.com/Layr-Labs/sidecar/internal/logger"
"github.com/Layr-Labs/sidecar/internal/sqlite/migrations"
"github.com/Layr-Labs/sidecar/internal/storage"
"github.com/Layr-Labs/sidecar/internal/tests"
"github.com/stretchr/testify/assert"
Expand All @@ -23,9 +24,16 @@ func setup() (
cfg := tests.GetConfig()
l, _ := logger.NewLogger(&logger.LoggerConfig{Debug: cfg.Debug})

_, grm, err := tests.GetDatabaseConnection(cfg)
db, err := tests.GetSqliteDatabaseConnection()
if err != nil {
panic(err)
}
sqliteMigrator := migrations.NewSqliteMigrator(db, l)
if err := sqliteMigrator.MigrateAll(); err != nil {
l.Sugar().Fatalw("Failed to migrate", "error", err)
}

return cfg, grm, l, err
return cfg, db, l, err
}

func teardown(model *StakerDelegationsModel) {
Expand Down
97 changes: 97 additions & 0 deletions internal/sqlite/migrations/202409061249_bootstrapDb/up.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package _202409061249_bootstrapDb

import (
"fmt"
"gorm.io/gorm"
)

type SqliteMigration struct {
}

func (m *SqliteMigration) Up(grm *gorm.DB) error {
queries := []string{
`CREATE TABLE IF NOT EXISTS blocks (
number INTEGER NOT NULL PRIMARY KEY,
hash text NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
block_time DATETIME NOT NULL,
updated_at DATETIME DEFAULT NULL,
deleted_at DATETIME DEFAULT NULL
)`,
`CREATE TABLE IF NOT EXISTS transactions (
block_number INTEGER NOT NULL REFERENCES blocks(number) ON DELETE CASCADE,
transaction_hash TEXT NOT NULL PRIMARY KEY,
transaction_index INTEGER NOT NULL,
from_address TEXT NOT NULL,
to_address TEXT DEFAULT NULL,
contract_address TEXT DEFAULT NULL,
bytecode_hash TEXT DEFAULT NULL,
gas_used INTEGER DEFAULT NULL,
cumulative_gas_used INTEGER DEFAULT NULL,
effective_gas_price INTEGER DEFAULT NULL,
created_at DATETIME DEFAULT current_timestamp,
updated_at DATETIME DEFAULT NULL,
deleted_at DATETIME DEFAULT NULL,
UNIQUE(block_number, transaction_hash, transaction_index)
)`,
`CREATE TABLE IF NOT EXISTS transaction_logs (
transaction_hash TEXT NOT NULL REFERENCES transactions(transaction_hash) ON DELETE CASCADE,
address TEXT NOT NULL,
arguments TEXT,
event_name TEXT NOT NULL,
log_index INTEGER NOT NULL,
block_number INTEGER NOT NULL REFERENCES blocks(number) ON DELETE CASCADE,
transaction_index INTEGER NOT NULL,
output_data TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME,
deleted_at DATETIME,
UNIQUE(transaction_hash, log_index)
)`,
`CREATE TABLE IF NOT EXISTS contracts (
contract_address TEXT NOT NULL,
contract_abi TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME,
deleted_at DATETIME,
bytecode_hash TEXT DEFAULT NULL,
verified INTEGER DEFAULT false,
matching_contract_address TEXT DEFAULT NULL,
checked_for_proxy INTEGER DEFAULT 0 NOT NULL,
checked_for_abi INTEGER NOT NULL,
UNIQUE(contract_address)
)`,
`CREATE TABLE IF NOT EXISTS proxy_contracts (
block_number INTEGER NOT NULL,
contract_address TEXT NOT NULL PRIMARY KEY REFERENCES contracts(contract_address) ON DELETE CASCADE,
proxy_contract_address TEXT NOT NULL REFERENCES contracts(contract_address) ON DELETE CASCADE,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at DATETIME,
deleted_at DATETIME
)`,
`CREATE TABLE IF NOT EXISTS operator_restaked_strategies (
block_number INTEGER NOT NULL REFERENCES blocks(number) ON DELETE CASCADE,
operator TEXT NOT NULL,
avs TEXT NOT NULL,
strategy TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME,
deleted_at DATETIME,
block_time DATETIME NOT NULL,
avs_directory_address TEXT
);`,
}

for _, query := range queries {
res := grm.Exec(query)
if res.Error != nil {
fmt.Printf("Failed to run migration query: %s - %+v\n", query, res.Error)
return res.Error
}
}
return nil
}

func (m *SqliteMigration) GetName() string {
return "202409061249_bootstrapDb"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package _202409061250_eigenlayerStateTables

import (
"fmt"
"gorm.io/gorm"
)

type SqliteMigration struct {
}

func (m *SqliteMigration) Up(grm *gorm.DB) error {
queries := []string{
`create table if not exists registered_avs_operators (
operator TEXT NOT NULL,
avs TEXT NOT NULL,
block_number INTEGER NOT NULL,
created_at DATETIME default current_timestamp,
unique(operator, avs, block_number)
);
`,
}

for _, query := range queries {
if res := grm.Exec(query); res.Error != nil {
fmt.Printf("Failed to execute query: %s\n", query)
return res.Error
}
}
return nil
}

func (m *SqliteMigration) GetName() string {
return "202409061250_eigenlayerStateTables"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package _202409061720_operatorShareChanges

import (
"gorm.io/gorm"
)

type SqliteMigration struct {
}

func (m *SqliteMigration) Up(grm *gorm.DB) error {
queries := []string{
`create table if not exists operator_shares (
operator TEXT NOT NULL,
strategy TEXT NOT NULL,
shares NUMERIC NOT NULL,
block_number INTEGER NOT NULL,
created_at DATETIME default current_timestamp,
unique (operator, strategy, block_number)
)`,
}
for _, query := range queries {
if res := grm.Exec(query); res.Error != nil {
return res.Error
}
}
return nil
}

func (m *SqliteMigration) GetName() string {
return "202409061720_operatorShareChanges"
}
30 changes: 30 additions & 0 deletions internal/sqlite/migrations/202409062151_stakerDelegations/up.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package _202409062151_stakerDelegations

import (
"gorm.io/gorm"
)

type SqliteMigration struct {
}

func (m *SqliteMigration) Up(grm *gorm.DB) error {
queries := []string{
`create table if not exists delegated_stakers (
staker TEXT NOT NULL,
operator TEXT NOT NULL,
block_number INTEGER NOT NULL,
created_at DATETIME default current_timestamp,
unique(staker, operator, block_number)
)`,
}
for _, query := range queries {
if res := grm.Exec(query); res.Error != nil {
return res.Error
}
}
return nil
}

func (m *SqliteMigration) GetName() string {
return "202409062151_stakerDelegations"
}
Loading
Loading