Skip to content

Commit

Permalink
Load extensions into sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmcgary committed Oct 2, 2024
1 parent ef77751 commit 783c42d
Show file tree
Hide file tree
Showing 21 changed files with 223 additions and 84 deletions.
5 changes: 4 additions & 1 deletion cmd/debugger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ func main() {
etherscanClient := etherscan.NewEtherscanClient(cfg, l)
client := ethereum.NewClient(cfg.EthereumRpcConfig.BaseUrl, l)

db := sqlite.NewSqlite(cfg.GetSqlitePath(), l)
db := sqlite.NewSqlite(&sqlite.SqliteConfig{
Path: cfg.GetSqlitePath(),
ExtensionsPath: cfg.SqliteConfig.ExtensionsPath,
}, l)

grm, err := sqlite.NewGormSqliteFromSqlite(db)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func init() {

rootCmd.PersistentFlags().Bool("sqlite.in-memory", false, `"true" or "false"`)
rootCmd.PersistentFlags().String("sqlite.db-file-path", "", `e.g. "/tmp/sidecar.db"`)
rootCmd.PersistentFlags().StringArray("sqlite.extensions-path", []string{}, `e.g. "./sqlite-extensions"`)

rootCmd.PersistentFlags().Int("rpc.grpc-port", 7100, `e.g. 7100`)
rootCmd.PersistentFlags().Int("rpc.http-port", 7101, `e.g. 7101`)
Expand Down
5 changes: 4 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ var runCmd = &cobra.Command{
}
}

db := sqlite.NewSqlite(cfg.GetSqlitePath(), l)
db := sqlite.NewSqlite(&sqlite.SqliteConfig{
Path: cfg.GetSqlitePath(),
ExtensionsPath: cfg.SqliteConfig.ExtensionsPath,
}, l)

grm, err := sqlite.NewGormSqliteFromSqlite(db)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion cmd/sidecar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ func main() {
etherscanClient := etherscan.NewEtherscanClient(cfg, l)
client := ethereum.NewClient(cfg.EthereumRpcConfig.BaseUrl, l)

db := sqlite.NewSqlite(cfg.GetSqlitePath(), l)
db := sqlite.NewSqlite(&sqlite.SqliteConfig{
Path: cfg.GetSqlitePath(),
ExtensionsPath: cfg.SqliteConfig.ExtensionsPath,
}, l)

grm, err := sqlite.NewGormSqliteFromSqlite(db)
if err != nil {
Expand Down
8 changes: 5 additions & 3 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ type EtherscanConfig struct {
}

type SqliteConfig struct {
InMemory bool
DbFilePath string
InMemory bool
DbFilePath string
ExtensionsPath []string
}

type RpcConfig struct {
Expand Down Expand Up @@ -108,7 +109,8 @@ func NewConfig() *Config {
},

SqliteConfig: SqliteConfig{
InMemory: viper.GetBool(normalizeFlagName("sqlite.in_memory")),
InMemory: viper.GetBool(normalizeFlagName("sqlite.in_memory")),
ExtensionsPath: parseListEnvVar(viper.GetString(normalizeFlagName("sqlite.extensions_path"))),
},

RpcConfig: RpcConfig{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/Layr-Labs/go-sidecar/internal/logger"
"github.com/Layr-Labs/go-sidecar/internal/sqlite/migrations"
"github.com/Layr-Labs/go-sidecar/internal/tests"
"github.com/Layr-Labs/go-sidecar/internal/tests/sqlite"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"gorm.io/gorm"
Expand All @@ -22,7 +23,7 @@ func setup() (
cfg := tests.GetConfig()
l, _ := logger.NewLogger(&logger.LoggerConfig{Debug: cfg.Debug})

db, err := tests.GetInMemorySqliteDatabaseConnection(l)
db, err := sqlite.GetInMemorySqliteDatabaseConnection(l)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/eigenState/avsOperators/avsOperators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func setup() (
cfg := tests.GetConfig()
l, _ := logger.NewLogger(&logger.LoggerConfig{Debug: cfg.Debug})

db, err := tests.GetInMemorySqliteDatabaseConnection(l)
db, err := sqlite.GetInMemorySqliteDatabaseConnection(l)

Check failure on line 28 in internal/eigenState/avsOperators/avsOperators_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: sqlite (typecheck)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/eigenState/eigenstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func setup() (
cfg := tests.GetConfig()
l, _ := logger.NewLogger(&logger.LoggerConfig{Debug: cfg.Debug})

db, err := tests.GetInMemorySqliteDatabaseConnection(l)
db, err := sqlite.GetInMemorySqliteDatabaseConnection(l)

Check failure on line 26 in internal/eigenState/eigenstate_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: sqlite (typecheck)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/eigenState/operatorShares/operatorShares_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func setup() (
cfg := tests.GetConfig()
l, _ := logger.NewLogger(&logger.LoggerConfig{Debug: cfg.Debug})

db, err := tests.GetInMemorySqliteDatabaseConnection(l)
db, err := sqlite.GetInMemorySqliteDatabaseConnection(l)

Check failure on line 30 in internal/eigenState/operatorShares/operatorShares_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: sqlite (typecheck)
if err != nil {
panic(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func setup() (
cfg := tests.GetConfig()
l, _ := logger.NewLogger(&logger.LoggerConfig{Debug: cfg.Debug})

db, err := tests.GetInMemorySqliteDatabaseConnection(l)
db, err := sqlite.GetInMemorySqliteDatabaseConnection(l)
if err != nil {
panic(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func setup() (
cfg := tests.GetConfig()
l, _ := logger.NewLogger(&logger.LoggerConfig{Debug: cfg.Debug})

db, err := tests.GetInMemorySqliteDatabaseConnection(l)
db, err := sqlite.GetInMemorySqliteDatabaseConnection(l)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/eigenState/stakerShares/stakerShares_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func setup() (
cfg := tests.GetConfig()
l, _ := logger.NewLogger(&logger.LoggerConfig{Debug: cfg.Debug})

db, err := tests.GetInMemorySqliteDatabaseConnection(l)
db, err := sqlite.GetInMemorySqliteDatabaseConnection(l)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/eigenState/stateManager/stateManager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func setup() (
cfg := tests.GetConfig()
l, _ := logger.NewLogger(&logger.LoggerConfig{Debug: cfg.Debug})

db, err := tests.GetInMemorySqliteDatabaseConnection(l)
db, err := sqlite.GetInMemorySqliteDatabaseConnection(l)
if err != nil {
panic(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func setup() (
cfg := tests.GetConfig()
l, _ := logger.NewLogger(&logger.LoggerConfig{Debug: cfg.Debug})

db, err := tests.GetInMemorySqliteDatabaseConnection(l)
db, err := sqlite.GetInMemorySqliteDatabaseConnection(l)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/indexer/restakedStrategies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func setup() (
cfg := tests.GetConfig()
l, _ := logger.NewLogger(&logger.LoggerConfig{Debug: cfg.Debug})

db, err := tests.GetInMemorySqliteDatabaseConnection(l)
db, err := sqlite.GetInMemorySqliteDatabaseConnection(l)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/pipeline/pipeline_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func setup() (
client := ethereum.NewClient(rpcUrl, l)

// database
grm, err := tests.GetInMemorySqliteDatabaseConnection(l)
grm, err := sqlite.GetInMemorySqliteDatabaseConnection(l)
if err != nil {
panic(err)
}
Expand Down
63 changes: 30 additions & 33 deletions internal/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,15 @@ var hasRegisteredExtensions = false

const SqliteInMemoryPath = "file::memory:?cache=shared"

func NewInMemorySqlite(l *zap.Logger) gorm.Dialector {
return NewSqlite(SqliteInMemoryPath, l)
type SqliteConfig struct {
Path string
ExtensionsPath []string
}

func NewInMemorySqliteWithName(name string, l *zap.Logger) gorm.Dialector {
path := fmt.Sprintf("file:%s?mode=memory&cache=shared", name)
return NewSqlite(path, l)
}

func NewSqlite(path string, l *zap.Logger) gorm.Dialector {
func NewSqlite(cfg *SqliteConfig, l *zap.Logger) gorm.Dialector {
if !hasRegisteredExtensions {
sql.Register("sqlite3_with_extensions", &goSqlite.SQLiteDriver{
Extensions: cfg.ExtensionsPath,
ConnectHook: func(conn *goSqlite.SQLiteConn) error {
// Generic functions
if err := conn.RegisterAggregator("sum_big", NewSumBigNumbers, true); err != nil {
Expand Down Expand Up @@ -115,10 +112,10 @@ func NewSqlite(path string, l *zap.Logger) gorm.Dialector {
l.Sugar().Errorw("Failed to register function PostNileTokensPerDay", "error", err)
return err
}
if err := conn.RegisterFunc("pre_nile_tokens_per_day", numbers.PreNileTokensPerDay, true); err != nil {
l.Sugar().Errorw("Failed to register function PreNileTokensPerDay", "error", err)
return err
}
// if err := conn.RegisterFunc("pre_nile_tokens_per_day", numbers.PreNileTokensPerDay, true); err != nil {
// l.Sugar().Errorw("Failed to register function PreNileTokensPerDay", "error", err)
// return err
// }
// Staker proportion and weight
if err := conn.RegisterFunc("calc_staker_proportion", numbers.CalculateStakerProportion, true); err != nil {
l.Sugar().Errorw("Failed to register function CalculateStakerProportion", "error", err)
Expand All @@ -129,27 +126,27 @@ func NewSqlite(path string, l *zap.Logger) gorm.Dialector {
return err
}
// Forked rewards for stakers
if err := conn.RegisterFunc("amazon_token_rewards", numbers.CalculateAmazonStakerTokenRewards, true); err != nil {
l.Sugar().Errorw("Failed to register function CalculateAmazonStakerTokenRewards", "error", err)
return err
}
if err := conn.RegisterFunc("nile_token_rewards", numbers.CalculateNileStakerTokenRewards, true); err != nil {
l.Sugar().Errorw("Failed to register function CalculateNileStakerTokenRewards", "error", err)
return err
}
if err := conn.RegisterFunc("post_nile_token_rewards", numbers.CalculatePostNileStakerTokenRewards, true); err != nil {
l.Sugar().Errorw("Failed to register function CalculatePostNileStakerTokenRewards", "error", err)
return err
}
//if err := conn.RegisterFunc("amazon_token_rewards", numbers.CalculateAmazonStakerTokenRewards, true); err != nil {
// l.Sugar().Errorw("Failed to register function CalculateAmazonStakerTokenRewards", "error", err)
// return err
//}
//if err := conn.RegisterFunc("nile_token_rewards", numbers.CalculateNileStakerTokenRewards, true); err != nil {
// l.Sugar().Errorw("Failed to register function CalculateNileStakerTokenRewards", "error", err)
// return err
//}
//if err := conn.RegisterFunc("post_nile_token_rewards", numbers.CalculatePostNileStakerTokenRewards, true); err != nil {
// l.Sugar().Errorw("Failed to register function CalculatePostNileStakerTokenRewards", "error", err)
// return err
//}
// Operator tokens
if err := conn.RegisterFunc("amazon_operator_tokens", numbers.CalculateAmazonOperatorTokens, true); err != nil {
l.Sugar().Errorw("Failed to register function CalculateAmazonOperatorTokens", "error", err)
return err
}
if err := conn.RegisterFunc("nile_operator_tokens", numbers.CalculateNileOperatorTokens, true); err != nil {
l.Sugar().Errorw("Failed to register function CalculateNileOperatorTokens", "error", err)
return err
}
//if err := conn.RegisterFunc("amazon_operator_tokens", numbers.CalculateAmazonOperatorTokens, true); err != nil {
// l.Sugar().Errorw("Failed to register function CalculateAmazonOperatorTokens", "error", err)
// return err
//}
//if err := conn.RegisterFunc("nile_operator_tokens", numbers.CalculateNileOperatorTokens, true); err != nil {
// l.Sugar().Errorw("Failed to register function CalculateNileOperatorTokens", "error", err)
// return err
//}
if err := conn.RegisterFunc("post_nile_operator_tokens", numbers.CalculatePostNileOperatorTokens, true); err != nil {
l.Sugar().Errorw("Failed to register function CalculatePostNileOperatorTokens", "error", err)
return err
Expand All @@ -162,7 +159,7 @@ func NewSqlite(path string, l *zap.Logger) gorm.Dialector {

return &sqlite.Dialector{
DriverName: "sqlite3_with_extensions",
DSN: path,
DSN: cfg.Path,
}
}

Expand Down
Loading

0 comments on commit 783c42d

Please sign in to comment.