diff --git a/cmd/migrate/main.go b/cmd/migrate/main.go deleted file mode 100644 index 88cd13be..00000000 --- a/cmd/migrate/main.go +++ /dev/null @@ -1,39 +0,0 @@ -package main - -import ( - "github.com/Layr-Labs/sidecar/internal/config" - "github.com/Layr-Labs/sidecar/internal/logger" - "github.com/Layr-Labs/sidecar/internal/postgres" - "github.com/Layr-Labs/sidecar/internal/postgres/migrations" - "go.uber.org/zap" - "log" -) - -func main() { - cfg := config.NewConfig() - - l, _ := logger.NewLogger(&logger.LoggerConfig{Debug: cfg.Debug}) - - db, err := postgres.NewPostgres(&postgres.PostgresConfig{ - Host: cfg.PostgresConfig.Host, - Port: cfg.PostgresConfig.Port, - Username: cfg.PostgresConfig.Username, - Password: cfg.PostgresConfig.Password, - DbName: cfg.PostgresConfig.DbName, - }) - if err != nil { - l.Error("Failed to setup postgres connection", zap.Error(err)) - panic(err) - } - - grm, err := postgres.NewGormFromPostgresConnection(db.Db) - if err != nil { - l.Error("Failed to create gorm instance", zap.Error(err)) - panic(err) - } - - migrator := migrations.NewMigrator(db.Db, grm, l) - if err = migrator.MigrateAll(); err != nil { - log.Fatalf("Failed to migrate: %v", err) - } -} diff --git a/cmd/sidecar/main.go b/cmd/sidecar/main.go index 7e3ed695..eb70f35a 100644 --- a/cmd/sidecar/main.go +++ b/cmd/sidecar/main.go @@ -7,15 +7,15 @@ import ( "github.com/Layr-Labs/sidecar/internal/clients/etherscan" "github.com/Layr-Labs/sidecar/internal/config" "github.com/Layr-Labs/sidecar/internal/contractManager" - "github.com/Layr-Labs/sidecar/internal/contractStore/pgContractStore" + "github.com/Layr-Labs/sidecar/internal/contractStore/sqliteContractStore" "github.com/Layr-Labs/sidecar/internal/fetcher" "github.com/Layr-Labs/sidecar/internal/indexer" "github.com/Layr-Labs/sidecar/internal/logger" "github.com/Layr-Labs/sidecar/internal/metrics" "github.com/Layr-Labs/sidecar/internal/pipeline" - "github.com/Layr-Labs/sidecar/internal/postgres" - "github.com/Layr-Labs/sidecar/internal/postgres/migrations" "github.com/Layr-Labs/sidecar/internal/sidecar" + "github.com/Layr-Labs/sidecar/internal/sqlite" + "github.com/Layr-Labs/sidecar/internal/sqlite/migrations" "github.com/Layr-Labs/sidecar/internal/storage/postgresql" "go.uber.org/zap" "log" @@ -37,31 +37,21 @@ func main() { etherscanClient := etherscan.NewEtherscanClient(cfg, l) client := ethereum.NewClient(cfg.EthereumRpcConfig.BaseUrl, l) - db, err := postgres.NewPostgres(&postgres.PostgresConfig{ - Host: cfg.PostgresConfig.Host, - Port: cfg.PostgresConfig.Port, - Username: cfg.PostgresConfig.Username, - Password: cfg.PostgresConfig.Password, - DbName: cfg.PostgresConfig.DbName, - }) - if err != nil { - l.Error("Failed to setup postgres connection", zap.Error(err)) - panic(err) - } + db := sqlite.NewSqlite(cfg.SqliteConfig.GetSqlitePath()) - grm, err := postgres.NewGormFromPostgresConnection(db.Db) + grm, err := sqlite.NewGormSqliteFromSqlite(db) if err != nil { l.Error("Failed to create gorm instance", zap.Error(err)) panic(err) } - migrator := migrations.NewMigrator(db.Db, grm, l) + migrator := migrations.NewSqliteMigrator(grm, l) migrator.MigrateAll() if err = migrator.MigrateAll(); err != nil { log.Fatalf("Failed to migrate: %v", err) } - contractStore, err := pgContractStore.NewPgContractStore(grm, l) + contractStore := sqliteContractStore.NewSqliteContractStore(grm, l) cm := contractManager.NewContractManager(contractStore, etherscanClient, client, sdc, l) diff --git a/internal/config/config.go b/internal/config/config.go index 0df81c45..36f9784d 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -118,6 +118,7 @@ type Config struct { QuickNodeEthereumRpcConfig EthereumRpcConfig PostgresConfig PostgresConfig EtherscanConfig EtherscanConfig + SqliteConfig SqliteConfig } type EthereumRpcConfig struct { @@ -137,6 +138,18 @@ type EtherscanConfig struct { ApiKeys []string } +type SqliteConfig struct { + InMemory bool + DbFilePath string +} + +func (s *SqliteConfig) GetSqlitePath() string { + if s.InMemory { + return "file::memory:?cache=shared" + } + return s.DbFilePath +} + func NewConfig() *Config { return &Config{ Network: ParseNetwork(getPrefixedEnvVar("NETWORK")), @@ -165,6 +178,11 @@ func NewConfig() *Config { EtherscanConfig: EtherscanConfig{ ApiKeys: parseListEnvVar(getPrefixedEnvVar("ETHERSCAN_API_KEYS")), }, + + SqliteConfig: SqliteConfig{ + InMemory: parseBooleanEnvVar(getPrefixedEnvVar("SQLITE_IN_MEMORY")), + DbFilePath: getPrefixedEnvVar("SQLITE_DB_FILE_PATH"), + }, } } diff --git a/internal/contractStore/pgContractStore/pgContractStore.go b/internal/contractStore/pgContractStore/pgContractStore.go deleted file mode 100644 index 7d67127d..00000000 --- a/internal/contractStore/pgContractStore/pgContractStore.go +++ /dev/null @@ -1,241 +0,0 @@ -package pgContractStore - -import ( - "errors" - "fmt" - "github.com/Layr-Labs/sidecar/internal/contractStore" - pg "github.com/Layr-Labs/sidecar/internal/postgres" - "go.uber.org/zap" - "gorm.io/gorm" - "gorm.io/gorm/clause" - "strings" -) - -type PgContractStore struct { - Db *gorm.DB - Logger *zap.Logger -} - -func NewPgContractStore(db *gorm.DB, l *zap.Logger) (*PgContractStore, error) { - cs := &PgContractStore{ - Db: db, - Logger: l, - } - - return cs, nil -} - -func (p *PgContractStore) GetContractForAddress(address string) (*contractStore.Contract, error) { - var contract *contractStore.Contract - - result := p.Db.First(&contract, "contract_address = ?", strings.ToLower(address)) - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - p.Logger.Sugar().Debugf("Contract not found in store '%s'", address) - return nil, nil - } - return nil, result.Error - } - - return contract, nil -} -func (p *PgContractStore) FindOrCreateContract( - address string, - abiJson string, - verified bool, - bytecodeHash string, - matchingContractAddress string, -) (*contractStore.Contract, bool, error) { - found := false - upsertedContract, err := pg.WrapTxAndCommit[*contractStore.Contract](func(tx *gorm.DB) (*contractStore.Contract, error) { - contract := &contractStore.Contract{} - result := p.Db.First(&contract, "contract_address = ?", strings.ToLower(address)) - if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, result.Error - } - - // found contract - if contract.ContractAddress == address { - found = true - return contract, nil - } - contract = &contractStore.Contract{ - ContractAddress: strings.ToLower(address), - ContractAbi: abiJson, - Verified: verified, - BytecodeHash: bytecodeHash, - MatchingContractAddress: matchingContractAddress, - } - - result = p.Db.Create(contract) - if result.Error != nil { - return nil, result.Error - } - - return contract, nil - }, nil, p.Db) - return upsertedContract, found, err -} - -func (p *PgContractStore) FindVerifiedContractWithMatchingBytecodeHash(bytecodeHash string, address string) (*contractStore.Contract, error) { - query := ` - select - * - from contracts - where - bytecode_hash = ? - and verified = true - and matching_contract_address = '' - and contract_address != ? - order by id asc - limit 1` - - var contract *contractStore.Contract - result := p.Db.Raw(query, bytecodeHash, address).Scan(&contract) - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - p.Logger.Sugar().Debugf("Verified contract not found in store '%s'", bytecodeHash) - return nil, nil - } - return nil, result.Error - } - return contract, nil -} - -func (p *PgContractStore) FindOrCreateProxyContract( - blockNumber uint64, - contractAddress string, - proxyContractAddress string, -) (*contractStore.ProxyContract, bool, error) { - found := false - contractAddress = strings.ToLower(contractAddress) - proxyContractAddress = strings.ToLower(proxyContractAddress) - - upsertedContract, err := pg.WrapTxAndCommit[*contractStore.ProxyContract](func(tx *gorm.DB) (*contractStore.ProxyContract, error) { - contract := &contractStore.ProxyContract{} - // Proxy contracts are unique on block_number && contract - result := tx.First(&contract, "contract_address = ? and block_number = ?", contractAddress, blockNumber) - if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, result.Error - } - - // found contract - if contract.ContractAddress == contractAddress { - found = true - return contract, nil - } - proxyContract := &contractStore.ProxyContract{ - BlockNumber: int64(blockNumber), - ContractAddress: contractAddress, - ProxyContractAddress: proxyContractAddress, - } - - result = tx.Model(&contractStore.ProxyContract{}).Clauses(clause.Returning{}).Create(proxyContract) - if result.Error != nil { - return nil, result.Error - } - - return proxyContract, nil - }, nil, p.Db) - return upsertedContract, found, err -} - -func (p *PgContractStore) GetContractWithProxyContract(address string, atBlockNumber uint64) (*contractStore.ContractsTree, error) { - address = strings.ToLower(address) - - query := `select - c.contract_address as base_address, - c.contract_abi as base_abi, - pcc.contract_address as base_proxy_address, - pcc.contract_abi as base_proxy_abi, - pcclike.contract_address as base_proxy_like_address, - pcclike.contract_abi as base_proxy_like_abi, - clike.contract_address as base_like_address, - clike.contract_abi as base_like_abi - from contracts as c - left join ( - select - * - from proxy_contracts - where contract_address = ? and block_number <= ? - order by block_number desc limit 1 - ) as pc on (1=1) - left join contracts as pcc on (pcc.contract_address = pc.proxy_contract_address) - left join contracts as pcclike on (pcc.matching_contract_address = pcclike.contract_address) - left join contracts as clike on (c.matching_contract_address = clike.contract_address) - where c.contract_address = ?` - - contractTree := &contractStore.ContractsTree{} - result := p.Db.Raw(query, - address, - atBlockNumber, - address, - ).Scan(&contractTree) - - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - p.Logger.Sugar().Debug(fmt.Sprintf("Contract not found '%s'", address)) - return nil, nil - } - return nil, result.Error - } - if contractTree.BaseAddress == "" { - p.Logger.Sugar().Debug(fmt.Sprintf("Contract not found in store '%s'", address)) - return nil, nil - } - - return contractTree, nil -} - -func (p *PgContractStore) SetContractCheckedForProxy(address string) (*contractStore.Contract, error) { - contract := &contractStore.Contract{} - - result := p.Db.Model(contract). - Clauses(clause.Returning{}). - Where("contract_address = ?", strings.ToLower(address)). - Updates(&contractStore.Contract{ - CheckedForProxy: true, - }) - - if result.Error != nil { - return nil, result.Error - } - - return contract, nil -} - -func (p *PgContractStore) SetContractAbi(address string, abi string, verified bool) (*contractStore.Contract, error) { - contract := &contractStore.Contract{} - - result := p.Db.Model(contract). - Clauses(clause.Returning{}). - Where("contract_address = ?", strings.ToLower(address)). - Updates(&contractStore.Contract{ - ContractAbi: abi, - Verified: verified, - CheckedForAbi: true, - }) - - if result.Error != nil { - return nil, result.Error - } - - return contract, nil -} - -func (p *PgContractStore) SetContractMatchingContractAddress(address string, matchingContractAddress string) (*contractStore.Contract, error) { - contract := &contractStore.Contract{} - - result := p.Db.Model(&contract). - Clauses(clause.Returning{}). - Where("contract_address = ?", strings.ToLower(address)). - Updates(&contractStore.Contract{ - MatchingContractAddress: matchingContractAddress, - }) - - if result.Error != nil { - return nil, result.Error - } - - return contract, nil -} diff --git a/internal/contractStore/pgContractStore/pgContractStore_test.go b/internal/contractStore/pgContractStore/pgContractStore_test.go deleted file mode 100644 index 4c2607c7..00000000 --- a/internal/contractStore/pgContractStore/pgContractStore_test.go +++ /dev/null @@ -1,65 +0,0 @@ -package pgContractStore - -import ( - "github.com/Layr-Labs/sidecar/internal/config" - "github.com/Layr-Labs/sidecar/internal/contractStore" - "github.com/Layr-Labs/sidecar/internal/logger" - "github.com/Layr-Labs/sidecar/internal/tests" - "github.com/google/go-cmp/cmp" - "github.com/stretchr/testify/assert" - "testing" -) - -func setup() ( - *config.Config, - *PgContractStore, - error, -) { - cfg := tests.GetConfig() - l, _ := logger.NewLogger(&logger.LoggerConfig{Debug: cfg.Debug}) - - _, grm, err := tests.GetDatabaseConnection(cfg) - - pgcs, _ := NewPgContractStore(grm, l) - - return cfg, pgcs, err -} - -func TestFindOrCreateContract(t *testing.T) { - _, pgcs, err := setup() - if err != nil { - t.Fatal(err) - } - - const ( - contractAddress = "0x2c06ec772df3bbd51a0d1ffa35e07335ce93f414" - abi = "{}" - verified = false - bytecodeHash = "0x1234" - matchingContractAddress = "" - ) - - var createdContract *contractStore.Contract - t.Run("Should create a new address", func(t *testing.T) { - contract, found, err := pgcs.FindOrCreateContract(contractAddress, abi, verified, bytecodeHash, matchingContractAddress) - - assert.False(t, found) - assert.Nil(t, err) - assert.Equal(t, contractAddress, contract.ContractAddress) - assert.Equal(t, abi, contract.ContractAbi) - assert.Equal(t, verified, contract.Verified) - assert.Equal(t, bytecodeHash, contract.BytecodeHash) - assert.Equal(t, matchingContractAddress, contract.MatchingContractAddress) - - createdContract = contract - }) - t.Run("Should return existing address", func(t *testing.T) { - contract, found, err := pgcs.FindOrCreateContract(contractAddress, abi, verified, bytecodeHash, matchingContractAddress) - - assert.True(t, found) - assert.Nil(t, err) - assert.Equal(t, contractAddress, contract.ContractAddress) - assert.Equal(t, contract.Id, createdContract.Id) - assert.True(t, cmp.Equal(contract, createdContract)) - }) -} diff --git a/internal/contractStore/sqliteContractStore/sqliteContractStore.go b/internal/contractStore/sqliteContractStore/sqliteContractStore.go index 9d864358..138dbb6d 100644 --- a/internal/contractStore/sqliteContractStore/sqliteContractStore.go +++ b/internal/contractStore/sqliteContractStore/sqliteContractStore.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "github.com/Layr-Labs/sidecar/internal/contractStore" - pg "github.com/Layr-Labs/sidecar/internal/postgres" "github.com/Layr-Labs/sidecar/internal/sqlite" "go.uber.org/zap" "gorm.io/gorm" @@ -113,7 +112,7 @@ func (s *SqliteContractStore) FindOrCreateProxyContract( contractAddress = strings.ToLower(contractAddress) proxyContractAddress = strings.ToLower(proxyContractAddress) - upsertedContract, err := pg.WrapTxAndCommit[*contractStore.ProxyContract](func(tx *gorm.DB) (*contractStore.ProxyContract, error) { + upsertedContract, err := sqlite.WrapTxAndCommit[*contractStore.ProxyContract](func(tx *gorm.DB) (*contractStore.ProxyContract, error) { contract := &contractStore.ProxyContract{} // Proxy contracts are unique on block_number && contract result := tx.First(&contract, "contract_address = ? and block_number = ?", contractAddress, blockNumber) diff --git a/internal/indexer/indexer.go b/internal/indexer/indexer.go index 7cabc568..8d3c10ed 100644 --- a/internal/indexer/indexer.go +++ b/internal/indexer/indexer.go @@ -140,7 +140,7 @@ func (idx *Indexer) ParseInterestingTransactionsAndLogs(ctx context.Context, fet return parsedTransactions, nil } -func (idx *Indexer) ParseAndIndexTransactionLogs(ctx context.Context, fetchedBlock *fetcher.FetchedBlock, blockSequenceId uint64) *IndexError { +func (idx *Indexer) ParseAndIndexTransactionLogs(ctx context.Context, fetchedBlock *fetcher.FetchedBlock) *IndexError { for i, tx := range fetchedBlock.Block.Transactions { txReceipt, ok := fetchedBlock.TxReceipts[tx.Hash.Value()] if !ok { @@ -172,7 +172,7 @@ func (idx *Indexer) ParseAndIndexTransactionLogs(ctx context.Context, fetchedBlo } for _, log := range parsedTransactionLogs.Logs { - _, err := idx.IndexLog(ctx, fetchedBlock.Block.Number.Value(), blockSequenceId, tx.Hash.Value(), tx.Index.Value(), log) + _, err := idx.IndexLog(ctx, fetchedBlock.Block.Number.Value(), tx.Hash.Value(), tx.Index.Value(), log) if err != nil { idx.Logger.Sugar().Errorw("failed to index log", zap.Error(err), @@ -380,7 +380,6 @@ func (idx *Indexer) handleContractCreation( func (idx *Indexer) IndexLog( ctx context.Context, blockNumber uint64, - blockSequenceId uint64, txHash string, txIndex uint64, log *parser.DecodedLog, diff --git a/internal/pipeline/pipeline.go b/internal/pipeline/pipeline.go index 2c1ac9b3..06d16a98 100644 --- a/internal/pipeline/pipeline.go +++ b/internal/pipeline/pipeline.go @@ -82,7 +82,6 @@ func (p *Pipeline) RunForBlock(ctx context.Context, blockNumber uint64) error { _, err := p.Indexer.IndexLog( ctx, indexedBlock.Number, - indexedBlock.Id, indexedTransaction.TransactionHash, indexedTransaction.TransactionIndex, log, diff --git a/internal/postgres/migrations/202405150900_bootstrapDb/up.go b/internal/postgres/migrations/202405150900_bootstrapDb/up.go deleted file mode 100644 index d3f43482..00000000 --- a/internal/postgres/migrations/202405150900_bootstrapDb/up.go +++ /dev/null @@ -1,71 +0,0 @@ -package _202405150900_bootstrapDb - -import ( - "database/sql" - "fmt" - "github.com/Layr-Labs/sidecar/internal/postgres" - "gorm.io/gorm" -) - -type Migration struct { -} - -func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error { - queries := []string{ - `CREATE TABLE IF NOT EXISTS block_sequences ( - id serial PRIMARY KEY, - number bigint NOT NULL, - hash varchar(255) NOT NULL, - blob_path text NOT NULL, - created_at timestamp with time zone DEFAULT current_timestamp, - updated_at timestamp with time zone DEFAULT NULL, - deleted_at timestamp with time zone DEFAULT NULL - )`, - `CREATE TABLE IF NOT EXISTS transactions ( - sequence_id bigint NOT NULL REFERENCES block_sequences(id) ON DELETE CASCADE, - block_number bigint NOT NULL, - transaction_hash varchar(255) NOT NULL, - transaction_index bigint NOT NULL, - created_at timestamp with time zone DEFAULT current_timestamp, - updated_at timestamp with time zone DEFAULT NULL, - deleted_at timestamp with time zone DEFAULT NULL, - UNIQUE(transaction_hash) - )`, - `CREATE UNIQUE INDEX idx_sequence_id_tx_hash_tx_index on transactions(sequence_id, transaction_hash, transaction_index)`, - `CREATE TABLE IF NOT EXISTS transaction_logs ( - transaction_hash varchar(255) NOT NULL REFERENCES transactions(transaction_hash) ON DELETE CASCADE, - address varchar(255) NOT NULL, - arguments JSONB DEFAULT NULL, - event_name varchar(255) NOT NULL, - log_index bigint NOT NULL, - created_at timestamp with time zone DEFAULT current_timestamp, - updated_at timestamp with time zone DEFAULT NULL, - deleted_at timestamp with time zone DEFAULT NULL - )`, - `CREATE UNIQUE INDEX idx_transaction_hash on transaction_logs(transaction_hash, log_index)`, - `CREATE TABLE IF NOT EXISTS contracts ( - contract_address varchar(255) PRIMARY KEY, - contract_abi text NOT NULL, - created_at timestamp with time zone DEFAULT current_timestamp, - updated_at timestamp with time zone DEFAULT NULL, - deleted_at timestamp with time zone DEFAULT NULL, - UNIQUE(contract_address) - )`, - } - - _, err := postgres.WrapTxAndCommit[interface{}](func(tx *gorm.DB) (interface{}, error) { - for _, query := range queries { - err := tx.Exec(query) - if err.Error != nil { - fmt.Printf("Failed to run migration query: %s - %+v\n", query, err.Error) - return 0, err.Error - } - } - return 0, nil - }, grm, nil) - return err -} - -func (m *Migration) GetName() string { - return "202405150900_bootstrapDb" -} diff --git a/internal/postgres/migrations/202405150917_insertContractAbi/up.go b/internal/postgres/migrations/202405150917_insertContractAbi/up.go deleted file mode 100644 index 601adca3..00000000 --- a/internal/postgres/migrations/202405150917_insertContractAbi/up.go +++ /dev/null @@ -1,38 +0,0 @@ -package _202405150917_insertContractAbi - -import ( - "database/sql" - "github.com/Layr-Labs/sidecar/internal/contractStore" - "gorm.io/gorm" - "gorm.io/gorm/clause" - "os" - "strings" -) - -type Migration struct { -} - -func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error { - pcJson, err := os.ReadFile("./abi/paymentCoordinator.abi") - if err != nil { - return err - } - - contractAddress := strings.ToLower("0xd73A2490E286e50efD167a39302a2701BA40231E") - - contract := &contractStore.Contract{ - ContractAddress: contractAddress, - ContractAbi: string(pcJson), - } - - result := grm.Model(&contractStore.Contract{}).Clauses(clause.Returning{}).Create(contract) - - if result.Error != nil { - return result.Error - } - return nil -} - -func (m *Migration) GetName() string { - return "202405150917_insertContractAbi" -} diff --git a/internal/postgres/migrations/202405151523_addTransactionToFrom/up.go b/internal/postgres/migrations/202405151523_addTransactionToFrom/up.go deleted file mode 100644 index 5e3e9c78..00000000 --- a/internal/postgres/migrations/202405151523_addTransactionToFrom/up.go +++ /dev/null @@ -1,23 +0,0 @@ -package _202405151523_addTransactionToFrom - -import ( - "database/sql" - "gorm.io/gorm" -) - -type Migration struct { -} - -func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error { - query := `ALTER TABLE transactions ADD COLUMN from_address varchar(255) NOT NULL, ADD COLUMN to_address varchar(255) DEFAULT NULL` - - result := grm.Exec(query) - if result.Error != nil { - return result.Error - } - return nil -} - -func (m *Migration) GetName() string { - return "202405151523_addTransactionToFrom" -} diff --git a/internal/postgres/migrations/202405170842_addBlockInfoToTransactionLog/up.go b/internal/postgres/migrations/202405170842_addBlockInfoToTransactionLog/up.go deleted file mode 100644 index 6cf6e17e..00000000 --- a/internal/postgres/migrations/202405170842_addBlockInfoToTransactionLog/up.go +++ /dev/null @@ -1,37 +0,0 @@ -package _202405170842_addBlockInfoToTransactionLog - -import ( - "database/sql" - "fmt" - "github.com/Layr-Labs/sidecar/internal/postgres" - "gorm.io/gorm" -) - -type Migration struct { -} - -func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error { - queries := []string{ - `ALTER TABLE transaction_logs ADD COLUMN block_number bigint NOT NULL, ADD COLUMN transaction_index integer NOT NULL`, - `ALTER TABLE transaction_logs ADD COLUMN block_sequence_id bigint NOT NULL references block_sequences(id) on delete cascade`, - `ALTER TABLE transactions RENAME COLUMN sequence_id TO block_sequence_id`, - `ALTER TABLE block_sequences RENAME TO blocks`, - `ALTER TABLE blocks add column block_time timestamp with time zone NOT NULL`, - } - - _, err := postgres.WrapTxAndCommit[interface{}](func(tx *gorm.DB) (interface{}, error) { - for _, query := range queries { - err := tx.Exec(query) - if err.Error != nil { - fmt.Printf("Failed to run migration query: %s - %+v\n", query, err.Error) - return 0, err.Error - } - } - return 0, nil - }, nil, grm) - return err -} - -func (m *Migration) GetName() string { - return "202405170842_addBlockInfoToTransactionLog" -} diff --git a/internal/postgres/migrations/202405171056_unverifiedContracts/up.go b/internal/postgres/migrations/202405171056_unverifiedContracts/up.go deleted file mode 100644 index 9933c2a7..00000000 --- a/internal/postgres/migrations/202405171056_unverifiedContracts/up.go +++ /dev/null @@ -1,28 +0,0 @@ -package _202405171056_unverifiedContracts - -import ( - "database/sql" - "gorm.io/gorm" -) - -type Migration struct { -} - -func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error { - query := `CREATE TABLE IF NOT EXISTS unverified_contracts ( - contract_address varchar(255) PRIMARY KEY, - created_at timestamp with time zone DEFAULT current_timestamp, - updated_at timestamp with time zone DEFAULT NULL, - deleted_at timestamp with time zone DEFAULT NULL, - UNIQUE(contract_address) - )` - res := grm.Exec(query) - if res.Error != nil { - return res.Error - } - return nil -} - -func (m *Migration) GetName() string { - return "202405171056_unverifiedContracts" -} diff --git a/internal/postgres/migrations/202405171345_addUpdatedPaymentCoordinatorAbi/up.go b/internal/postgres/migrations/202405171345_addUpdatedPaymentCoordinatorAbi/up.go deleted file mode 100644 index 32db609f..00000000 --- a/internal/postgres/migrations/202405171345_addUpdatedPaymentCoordinatorAbi/up.go +++ /dev/null @@ -1,35 +0,0 @@ -package _202405171345_addUpdatedPaymentCoordinatorAbi - -import ( - "database/sql" - "github.com/Layr-Labs/sidecar/internal/contractStore" - "gorm.io/gorm" - "gorm.io/gorm/clause" - "strings" -) - -type Migration struct { -} - -func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error { - - contractAddress := strings.ToLower("0xb22Ef643e1E067c994019A4C19e403253C05c2B0") - - contract := &contractStore.Contract{ - ContractAddress: contractAddress, - ContractAbi: string(contract), - } - - result := grm.Model(&contractStore.Contract{}).Clauses(clause.Returning{}).Create(contract) - - if result.Error != nil { - return result.Error - } - return nil -} - -func (m *Migration) GetName() string { - return "202405171345_addUpdatedPaymentCoordinatorAbi" -} - -const contract = `[{"type":"constructor","inputs":[{"name":"_delegationManager","type":"address","internalType":"contract IDelegationManager"},{"name":"_strategyManager","type":"address","internalType":"contract IStrategyManager"},{"name":"_CALCULATION_INTERVAL_SECONDS","type":"uint32","internalType":"uint32"},{"name":"_MAX_PAYMENT_DURATION","type":"uint32","internalType":"uint32"},{"name":"_MAX_RETROACTIVE_LENGTH","type":"uint32","internalType":"uint32"},{"name":"_MAX_FUTURE_LENGTH","type":"uint32","internalType":"uint32"},{"name":"_GENESIS_PAYMENT_TIMESTAMP","type":"uint32","internalType":"uint32"}],"stateMutability":"nonpayable"},{"type":"function","name":"CALCULATION_INTERVAL_SECONDS","inputs":[],"outputs":[{"name":"","type":"uint32","internalType":"uint32"}],"stateMutability":"view"},{"type":"function","name":"GENESIS_PAYMENT_TIMESTAMP","inputs":[],"outputs":[{"name":"","type":"uint32","internalType":"uint32"}],"stateMutability":"view"},{"type":"function","name":"MAX_FUTURE_LENGTH","inputs":[],"outputs":[{"name":"","type":"uint32","internalType":"uint32"}],"stateMutability":"view"},{"type":"function","name":"MAX_PAYMENT_DURATION","inputs":[],"outputs":[{"name":"","type":"uint32","internalType":"uint32"}],"stateMutability":"view"},{"type":"function","name":"MAX_RETROACTIVE_LENGTH","inputs":[],"outputs":[{"name":"","type":"uint32","internalType":"uint32"}],"stateMutability":"view"},{"type":"function","name":"activationDelay","inputs":[],"outputs":[{"name":"","type":"uint32","internalType":"uint32"}],"stateMutability":"view"},{"type":"function","name":"beaconChainETHStrategy","inputs":[],"outputs":[{"name":"","type":"address","internalType":"contract IStrategy"}],"stateMutability":"view"},{"type":"function","name":"calculateEarnerLeafHash","inputs":[{"name":"leaf","type":"tuple","internalType":"struct IPaymentCoordinator.EarnerTreeMerkleLeaf","components":[{"name":"earner","type":"address","internalType":"address"},{"name":"earnerTokenRoot","type":"bytes32","internalType":"bytes32"}]}],"outputs":[{"name":"","type":"bytes32","internalType":"bytes32"}],"stateMutability":"pure"},{"type":"function","name":"calculateTokenLeafHash","inputs":[{"name":"leaf","type":"tuple","internalType":"struct IPaymentCoordinator.TokenTreeMerkleLeaf","components":[{"name":"token","type":"address","internalType":"contract IERC20"},{"name":"cumulativeEarnings","type":"uint256","internalType":"uint256"}]}],"outputs":[{"name":"","type":"bytes32","internalType":"bytes32"}],"stateMutability":"pure"},{"type":"function","name":"checkClaim","inputs":[{"name":"claim","type":"tuple","internalType":"struct IPaymentCoordinator.PaymentMerkleClaim","components":[{"name":"rootIndex","type":"uint32","internalType":"uint32"},{"name":"earnerIndex","type":"uint32","internalType":"uint32"},{"name":"earnerTreeProof","type":"bytes","internalType":"bytes"},{"name":"earnerLeaf","type":"tuple","internalType":"struct IPaymentCoordinator.EarnerTreeMerkleLeaf","components":[{"name":"earner","type":"address","internalType":"address"},{"name":"earnerTokenRoot","type":"bytes32","internalType":"bytes32"}]},{"name":"tokenIndices","type":"uint32[]","internalType":"uint32[]"},{"name":"tokenTreeProofs","type":"bytes[]","internalType":"bytes[]"},{"name":"tokenLeaves","type":"tuple[]","internalType":"struct IPaymentCoordinator.TokenTreeMerkleLeaf[]","components":[{"name":"token","type":"address","internalType":"contract IERC20"},{"name":"cumulativeEarnings","type":"uint256","internalType":"uint256"}]}]}],"outputs":[{"name":"","type":"bool","internalType":"bool"}],"stateMutability":"view"},{"type":"function","name":"claimerFor","inputs":[{"name":"","type":"address","internalType":"address"}],"outputs":[{"name":"","type":"address","internalType":"address"}],"stateMutability":"view"},{"type":"function","name":"cumulativeClaimed","inputs":[{"name":"","type":"address","internalType":"address"},{"name":"","type":"address","internalType":"contract IERC20"}],"outputs":[{"name":"","type":"uint256","internalType":"uint256"}],"stateMutability":"view"},{"type":"function","name":"currPaymentCalculationEndTimestamp","inputs":[],"outputs":[{"name":"","type":"uint32","internalType":"uint32"}],"stateMutability":"view"},{"type":"function","name":"delegationManager","inputs":[],"outputs":[{"name":"","type":"address","internalType":"contract IDelegationManager"}],"stateMutability":"view"},{"type":"function","name":"distributionRoots","inputs":[{"name":"","type":"uint256","internalType":"uint256"}],"outputs":[{"name":"root","type":"bytes32","internalType":"bytes32"},{"name":"paymentCalculationEndTimestamp","type":"uint32","internalType":"uint32"},{"name":"activatedAt","type":"uint32","internalType":"uint32"}],"stateMutability":"view"},{"type":"function","name":"domainSeparator","inputs":[],"outputs":[{"name":"","type":"bytes32","internalType":"bytes32"}],"stateMutability":"view"},{"type":"function","name":"getDistributionRootsLength","inputs":[],"outputs":[{"name":"","type":"uint256","internalType":"uint256"}],"stateMutability":"view"},{"type":"function","name":"getRootIndexFromHash","inputs":[{"name":"rootHash","type":"bytes32","internalType":"bytes32"}],"outputs":[{"name":"","type":"uint32","internalType":"uint32"}],"stateMutability":"view"},{"type":"function","name":"globalOperatorCommissionBips","inputs":[],"outputs":[{"name":"","type":"uint16","internalType":"uint16"}],"stateMutability":"view"},{"type":"function","name":"initialize","inputs":[{"name":"initialOwner","type":"address","internalType":"address"},{"name":"_pauserRegistry","type":"address","internalType":"contract IPauserRegistry"},{"name":"initialPausedStatus","type":"uint256","internalType":"uint256"},{"name":"_paymentUpdater","type":"address","internalType":"address"},{"name":"_activationDelay","type":"uint32","internalType":"uint32"},{"name":"_globalCommissionBips","type":"uint16","internalType":"uint16"}],"outputs":[],"stateMutability":"nonpayable"},{"type":"function","name":"isPayAllForRangeSubmitter","inputs":[{"name":"","type":"address","internalType":"address"}],"outputs":[{"name":"","type":"bool","internalType":"bool"}],"stateMutability":"view"},{"type":"function","name":"isRangePaymentForAllHash","inputs":[{"name":"","type":"address","internalType":"address"},{"name":"","type":"bytes32","internalType":"bytes32"}],"outputs":[{"name":"","type":"bool","internalType":"bool"}],"stateMutability":"view"},{"type":"function","name":"isRangePaymentHash","inputs":[{"name":"","type":"address","internalType":"address"},{"name":"","type":"bytes32","internalType":"bytes32"}],"outputs":[{"name":"","type":"bool","internalType":"bool"}],"stateMutability":"view"},{"type":"function","name":"operatorCommissionBips","inputs":[{"name":"operator","type":"address","internalType":"address"},{"name":"avs","type":"address","internalType":"address"}],"outputs":[{"name":"","type":"uint16","internalType":"uint16"}],"stateMutability":"view"},{"type":"function","name":"owner","inputs":[],"outputs":[{"name":"","type":"address","internalType":"address"}],"stateMutability":"view"},{"type":"function","name":"pause","inputs":[{"name":"newPausedStatus","type":"uint256","internalType":"uint256"}],"outputs":[],"stateMutability":"nonpayable"},{"type":"function","name":"pauseAll","inputs":[],"outputs":[],"stateMutability":"nonpayable"},{"type":"function","name":"paused","inputs":[{"name":"index","type":"uint8","internalType":"uint8"}],"outputs":[{"name":"","type":"bool","internalType":"bool"}],"stateMutability":"view"},{"type":"function","name":"paused","inputs":[],"outputs":[{"name":"","type":"uint256","internalType":"uint256"}],"stateMutability":"view"},{"type":"function","name":"pauserRegistry","inputs":[],"outputs":[{"name":"","type":"address","internalType":"contract IPauserRegistry"}],"stateMutability":"view"},{"type":"function","name":"payAllForRange","inputs":[{"name":"rangePayments","type":"tuple[]","internalType":"struct IPaymentCoordinator.RangePayment[]","components":[{"name":"strategiesAndMultipliers","type":"tuple[]","internalType":"struct IPaymentCoordinator.StrategyAndMultiplier[]","components":[{"name":"strategy","type":"address","internalType":"contract IStrategy"},{"name":"multiplier","type":"uint96","internalType":"uint96"}]},{"name":"token","type":"address","internalType":"contract IERC20"},{"name":"amount","type":"uint256","internalType":"uint256"},{"name":"startTimestamp","type":"uint32","internalType":"uint32"},{"name":"duration","type":"uint32","internalType":"uint32"}]}],"outputs":[],"stateMutability":"nonpayable"},{"type":"function","name":"payForRange","inputs":[{"name":"rangePayments","type":"tuple[]","internalType":"struct IPaymentCoordinator.RangePayment[]","components":[{"name":"strategiesAndMultipliers","type":"tuple[]","internalType":"struct IPaymentCoordinator.StrategyAndMultiplier[]","components":[{"name":"strategy","type":"address","internalType":"contract IStrategy"},{"name":"multiplier","type":"uint96","internalType":"uint96"}]},{"name":"token","type":"address","internalType":"contract IERC20"},{"name":"amount","type":"uint256","internalType":"uint256"},{"name":"startTimestamp","type":"uint32","internalType":"uint32"},{"name":"duration","type":"uint32","internalType":"uint32"}]}],"outputs":[],"stateMutability":"nonpayable"},{"type":"function","name":"paymentNonce","inputs":[{"name":"","type":"address","internalType":"address"}],"outputs":[{"name":"","type":"uint256","internalType":"uint256"}],"stateMutability":"view"},{"type":"function","name":"paymentUpdater","inputs":[],"outputs":[{"name":"","type":"address","internalType":"address"}],"stateMutability":"view"},{"type":"function","name":"processClaim","inputs":[{"name":"claim","type":"tuple","internalType":"struct IPaymentCoordinator.PaymentMerkleClaim","components":[{"name":"rootIndex","type":"uint32","internalType":"uint32"},{"name":"earnerIndex","type":"uint32","internalType":"uint32"},{"name":"earnerTreeProof","type":"bytes","internalType":"bytes"},{"name":"earnerLeaf","type":"tuple","internalType":"struct IPaymentCoordinator.EarnerTreeMerkleLeaf","components":[{"name":"earner","type":"address","internalType":"address"},{"name":"earnerTokenRoot","type":"bytes32","internalType":"bytes32"}]},{"name":"tokenIndices","type":"uint32[]","internalType":"uint32[]"},{"name":"tokenTreeProofs","type":"bytes[]","internalType":"bytes[]"},{"name":"tokenLeaves","type":"tuple[]","internalType":"struct IPaymentCoordinator.TokenTreeMerkleLeaf[]","components":[{"name":"token","type":"address","internalType":"contract IERC20"},{"name":"cumulativeEarnings","type":"uint256","internalType":"uint256"}]}]},{"name":"recipient","type":"address","internalType":"address"}],"outputs":[],"stateMutability":"nonpayable"},{"type":"function","name":"renounceOwnership","inputs":[],"outputs":[],"stateMutability":"nonpayable"},{"type":"function","name":"setActivationDelay","inputs":[{"name":"_activationDelay","type":"uint32","internalType":"uint32"}],"outputs":[],"stateMutability":"nonpayable"},{"type":"function","name":"setClaimerFor","inputs":[{"name":"claimer","type":"address","internalType":"address"}],"outputs":[],"stateMutability":"nonpayable"},{"type":"function","name":"setGlobalOperatorCommission","inputs":[{"name":"_globalCommissionBips","type":"uint16","internalType":"uint16"}],"outputs":[],"stateMutability":"nonpayable"},{"type":"function","name":"setPauserRegistry","inputs":[{"name":"newPauserRegistry","type":"address","internalType":"contract IPauserRegistry"}],"outputs":[],"stateMutability":"nonpayable"},{"type":"function","name":"setPayAllForRangeSubmitter","inputs":[{"name":"_submitter","type":"address","internalType":"address"},{"name":"_newValue","type":"bool","internalType":"bool"}],"outputs":[],"stateMutability":"nonpayable"},{"type":"function","name":"setPaymentUpdater","inputs":[{"name":"_paymentUpdater","type":"address","internalType":"address"}],"outputs":[],"stateMutability":"nonpayable"},{"type":"function","name":"strategyManager","inputs":[],"outputs":[{"name":"","type":"address","internalType":"contract IStrategyManager"}],"stateMutability":"view"},{"type":"function","name":"submitRoot","inputs":[{"name":"root","type":"bytes32","internalType":"bytes32"},{"name":"paymentCalculationEndTimestamp","type":"uint32","internalType":"uint32"}],"outputs":[],"stateMutability":"nonpayable"},{"type":"function","name":"transferOwnership","inputs":[{"name":"newOwner","type":"address","internalType":"address"}],"outputs":[],"stateMutability":"nonpayable"},{"type":"function","name":"unpause","inputs":[{"name":"newPausedStatus","type":"uint256","internalType":"uint256"}],"outputs":[],"stateMutability":"nonpayable"},{"type":"event","name":"ActivationDelaySet","inputs":[{"name":"oldActivationDelay","type":"uint32","indexed":false,"internalType":"uint32"},{"name":"newActivationDelay","type":"uint32","indexed":false,"internalType":"uint32"}],"anonymous":false},{"type":"event","name":"ClaimerForSet","inputs":[{"name":"earner","type":"address","indexed":true,"internalType":"address"},{"name":"oldClaimer","type":"address","indexed":true,"internalType":"address"},{"name":"claimer","type":"address","indexed":true,"internalType":"address"}],"anonymous":false},{"type":"event","name":"DistributionRootSubmitted","inputs":[{"name":"rootIndex","type":"uint32","indexed":true,"internalType":"uint32"},{"name":"root","type":"bytes32","indexed":true,"internalType":"bytes32"},{"name":"paymentCalculationEndTimestamp","type":"uint32","indexed":true,"internalType":"uint32"},{"name":"activatedAt","type":"uint32","indexed":false,"internalType":"uint32"}],"anonymous":false},{"type":"event","name":"GlobalCommissionBipsSet","inputs":[{"name":"oldGlobalCommissionBips","type":"uint16","indexed":false,"internalType":"uint16"},{"name":"newGlobalCommissionBips","type":"uint16","indexed":false,"internalType":"uint16"}],"anonymous":false},{"type":"event","name":"Initialized","inputs":[{"name":"version","type":"uint8","indexed":false,"internalType":"uint8"}],"anonymous":false},{"type":"event","name":"OwnershipTransferred","inputs":[{"name":"previousOwner","type":"address","indexed":true,"internalType":"address"},{"name":"newOwner","type":"address","indexed":true,"internalType":"address"}],"anonymous":false},{"type":"event","name":"Paused","inputs":[{"name":"account","type":"address","indexed":true,"internalType":"address"},{"name":"newPausedStatus","type":"uint256","indexed":false,"internalType":"uint256"}],"anonymous":false},{"type":"event","name":"PauserRegistrySet","inputs":[{"name":"pauserRegistry","type":"address","indexed":false,"internalType":"contract IPauserRegistry"},{"name":"newPauserRegistry","type":"address","indexed":false,"internalType":"contract IPauserRegistry"}],"anonymous":false},{"type":"event","name":"PayAllForRangeSubmitterSet","inputs":[{"name":"payAllForRangeSubmitter","type":"address","indexed":true,"internalType":"address"},{"name":"oldValue","type":"bool","indexed":true,"internalType":"bool"},{"name":"newValue","type":"bool","indexed":true,"internalType":"bool"}],"anonymous":false},{"type":"event","name":"PaymentClaimed","inputs":[{"name":"root","type":"bytes32","indexed":false,"internalType":"bytes32"},{"name":"earner","type":"address","indexed":true,"internalType":"address"},{"name":"claimer","type":"address","indexed":true,"internalType":"address"},{"name":"recipient","type":"address","indexed":true,"internalType":"address"},{"name":"token","type":"address","indexed":false,"internalType":"contract IERC20"},{"name":"claimedAmount","type":"uint256","indexed":false,"internalType":"uint256"}],"anonymous":false},{"type":"event","name":"PaymentUpdaterSet","inputs":[{"name":"oldPaymentUpdater","type":"address","indexed":true,"internalType":"address"},{"name":"newPaymentUpdater","type":"address","indexed":true,"internalType":"address"}],"anonymous":false},{"type":"event","name":"RangePaymentCreated","inputs":[{"name":"avs","type":"address","indexed":true,"internalType":"address"},{"name":"paymentNonce","type":"uint256","indexed":true,"internalType":"uint256"},{"name":"rangePaymentHash","type":"bytes32","indexed":true,"internalType":"bytes32"},{"name":"rangePayment","type":"tuple","indexed":false,"internalType":"struct IPaymentCoordinator.RangePayment","components":[{"name":"strategiesAndMultipliers","type":"tuple[]","internalType":"struct IPaymentCoordinator.StrategyAndMultiplier[]","components":[{"name":"strategy","type":"address","internalType":"contract IStrategy"},{"name":"multiplier","type":"uint96","internalType":"uint96"}]},{"name":"token","type":"address","internalType":"contract IERC20"},{"name":"amount","type":"uint256","internalType":"uint256"},{"name":"startTimestamp","type":"uint32","internalType":"uint32"},{"name":"duration","type":"uint32","internalType":"uint32"}]}],"anonymous":false},{"type":"event","name":"RangePaymentForAllCreated","inputs":[{"name":"submitter","type":"address","indexed":true,"internalType":"address"},{"name":"paymentNonce","type":"uint256","indexed":true,"internalType":"uint256"},{"name":"rangePaymentHash","type":"bytes32","indexed":true,"internalType":"bytes32"},{"name":"rangePayment","type":"tuple","indexed":false,"internalType":"struct IPaymentCoordinator.RangePayment","components":[{"name":"strategiesAndMultipliers","type":"tuple[]","internalType":"struct IPaymentCoordinator.StrategyAndMultiplier[]","components":[{"name":"strategy","type":"address","internalType":"contract IStrategy"},{"name":"multiplier","type":"uint96","internalType":"uint96"}]},{"name":"token","type":"address","internalType":"contract IERC20"},{"name":"amount","type":"uint256","internalType":"uint256"},{"name":"startTimestamp","type":"uint32","internalType":"uint32"},{"name":"duration","type":"uint32","internalType":"uint32"}]}],"anonymous":false},{"type":"event","name":"Unpaused","inputs":[{"name":"account","type":"address","indexed":true,"internalType":"address"},{"name":"newPausedStatus","type":"uint256","indexed":false,"internalType":"uint256"}],"anonymous":false}]` diff --git a/internal/postgres/migrations/202405201503_fixTransactionHashConstraint/up.go b/internal/postgres/migrations/202405201503_fixTransactionHashConstraint/up.go deleted file mode 100644 index 44c6b82f..00000000 --- a/internal/postgres/migrations/202405201503_fixTransactionHashConstraint/up.go +++ /dev/null @@ -1,30 +0,0 @@ -package _202405201503_fixTransactionHashConstraint - -import ( - "database/sql" - "gorm.io/gorm" -) - -type Migration struct { -} - -func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error { - - queries := []string{ - "ALTER TABLE transaction_logs DROP CONSTRAINT transaction_logs_transaction_hash_fkey", - "alter table transactions drop constraint transactions_transaction_hash_key", - "alter table transactions add constraint transactions_transaction_hash_sequence_id_key unique (transaction_hash, block_sequence_id)", - "alter table transaction_logs add constraint fk_transaction_hash_sequence_id_key foreign key (transaction_hash, block_sequence_id) references transactions(transaction_hash, block_sequence_id) on delete cascade", - } - - for _, query := range queries { - if err := grm.Exec(query).Error; err != nil { - return err - } - } - return nil -} - -func (m *Migration) GetName() string { - return "202405201503_fixTransactionHashConstraint" -} diff --git a/internal/postgres/migrations/202405300925_addUniqueBlockConstraint/up.go b/internal/postgres/migrations/202405300925_addUniqueBlockConstraint/up.go deleted file mode 100644 index b04b1df3..00000000 --- a/internal/postgres/migrations/202405300925_addUniqueBlockConstraint/up.go +++ /dev/null @@ -1,23 +0,0 @@ -package _202405300925_addUniqueBlockConstraint - -import ( - "database/sql" - "gorm.io/gorm" -) - -type Migration struct { -} - -func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error { - query := `alter table blocks add constraint blocks_unique_block_number_hash unique (number, hash)` - - res := grm.Exec(query) - if res.Error != nil { - return res.Error - } - return nil -} - -func (m *Migration) GetName() string { - return "202405300925_addUniqueBlockConstraint" -} diff --git a/internal/postgres/migrations/202405312008_indexTransactionContractAddress/up.go b/internal/postgres/migrations/202405312008_indexTransactionContractAddress/up.go deleted file mode 100644 index d9151046..00000000 --- a/internal/postgres/migrations/202405312008_indexTransactionContractAddress/up.go +++ /dev/null @@ -1,35 +0,0 @@ -package _202405312008_indexTransactionContractAddress - -import ( - "database/sql" - "gorm.io/gorm" -) - -type Migration struct { -} - -func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error { - queries := []string{ - `alter table transactions add column contract_address varchar(255) default null`, - `create index transactions_contract_address on transactions(contract_address)`, - `alter table transactions add column bytecode_hash varchar(64) default null`, - `alter table contracts add column bytecode_hash varchar(64) default null`, - `alter table contracts add column verified boolean default false`, - `update contracts set verified = true`, - `alter table contracts add column matching_contract_address varchar(255) default null`, - `alter table contracts alter column contract_abi drop not null`, - `insert into contracts(contract_address, verified) select distinct(contract_address), false from unverified_contracts where contract_address not in (select contract_address from contracts)`, - } - - for _, query := range queries { - _, err := db.Exec(query) - if err != nil { - return err - } - } - return nil -} - -func (m *Migration) GetName() string { - return "202405312008_indexTransactionContractAddress" -} diff --git a/internal/postgres/migrations/202405312134_handleProxyContracts/up.go b/internal/postgres/migrations/202405312134_handleProxyContracts/up.go deleted file mode 100644 index 3a92471f..00000000 --- a/internal/postgres/migrations/202405312134_handleProxyContracts/up.go +++ /dev/null @@ -1,35 +0,0 @@ -package _202405312134_handleProxyContracts - -import ( - "database/sql" - "gorm.io/gorm" -) - -type Migration struct { -} - -func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error { - queries := []string{ - `create table if not exists proxy_contracts ( - block_number bigint not null, - contract_address varchar(255) not null, - proxy_contract_address varchar(255) not null, - created_at timestamp not null default current_timestamp, - updated_at timestamp with time zone DEFAULT NULL, - deleted_at timestamp with time zone DEFAULT NULL - )`, - `create unique index idx_unique_proxy_contract on proxy_contracts(block_number, contract_address, proxy_contract_address)`, - } - - for _, query := range queries { - _, err := db.Exec(query) - if err != nil { - return err - } - } - return nil -} - -func (m *Migration) GetName() string { - return "202405312134_handleProxyContracts" -} diff --git a/internal/postgres/migrations/202406030920_addCheckedForProxyFlag/up.go b/internal/postgres/migrations/202406030920_addCheckedForProxyFlag/up.go deleted file mode 100644 index 30313cd4..00000000 --- a/internal/postgres/migrations/202406030920_addCheckedForProxyFlag/up.go +++ /dev/null @@ -1,23 +0,0 @@ -package _202406030920_addCheckedForProxyFlag - -import ( - "database/sql" - "gorm.io/gorm" -) - -type Migration struct { -} - -func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error { - query := `alter table contracts add column checked_for_proxy boolean not null default false;` - - _, err := db.Exec(query) - if err != nil { - return err - } - return nil -} - -func (m *Migration) GetName() string { - return "202406030920_addCheckedForProxyFlag" -} diff --git a/internal/postgres/migrations/202406031946_addSerialIdToContracts/up.go b/internal/postgres/migrations/202406031946_addSerialIdToContracts/up.go deleted file mode 100644 index c50f4a25..00000000 --- a/internal/postgres/migrations/202406031946_addSerialIdToContracts/up.go +++ /dev/null @@ -1,23 +0,0 @@ -package _202406031946_addSerialIdToContracts - -import ( - "database/sql" - "gorm.io/gorm" -) - -type Migration struct { -} - -func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error { - query := `alter table contracts add column id serial` - - _, err := db.Exec(query) - if err != nil { - return err - } - return nil -} - -func (m *Migration) GetName() string { - return "202406031946_addSerialIdToContracts" -} diff --git a/internal/postgres/migrations/202406051937_addBytecodeIndex/up.go b/internal/postgres/migrations/202406051937_addBytecodeIndex/up.go deleted file mode 100644 index 9684aa6d..00000000 --- a/internal/postgres/migrations/202406051937_addBytecodeIndex/up.go +++ /dev/null @@ -1,29 +0,0 @@ -package _202406051937_addBytecodeIndex - -import ( - "database/sql" - "gorm.io/gorm" -) - -type Migration struct { -} - -func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error { - queries := []string{ - `create index idx_bytecode_hash on contracts (bytecode_hash)`, - `create index idx_proxy_contract_proxy_contract_address on proxy_contracts (proxy_contract_address)`, - `create index idx_proxy_contract_contract_address on proxy_contracts (contract_address)`, - } - - for _, query := range queries { - _, err := db.Exec(query) - if err != nil { - return err - } - } - return nil -} - -func (m *Migration) GetName() string { - return "202406051937_addBytecodeIndex" -} diff --git a/internal/postgres/migrations/202406071318_indexTransactionLogBlockNumber/up.go b/internal/postgres/migrations/202406071318_indexTransactionLogBlockNumber/up.go deleted file mode 100644 index 9ddfae67..00000000 --- a/internal/postgres/migrations/202406071318_indexTransactionLogBlockNumber/up.go +++ /dev/null @@ -1,23 +0,0 @@ -package _202406071318_indexTransactionLogBlockNumber - -import ( - "database/sql" - "gorm.io/gorm" -) - -type Migration struct { -} - -func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error { - query := `create index concurrently if not exists idx_transaciton_logs_block_number on transaction_logs (block_number);` - - _, err := db.Exec(query) - if err != nil { - return err - } - return nil -} - -func (m *Migration) GetName() string { - return "202406071318_indexTransactionLogBlockNumber" -} diff --git a/internal/postgres/migrations/202406110848_transactionLogsContractIndex/up.go b/internal/postgres/migrations/202406110848_transactionLogsContractIndex/up.go deleted file mode 100644 index 9b099b61..00000000 --- a/internal/postgres/migrations/202406110848_transactionLogsContractIndex/up.go +++ /dev/null @@ -1,23 +0,0 @@ -package _202406110848_transactionLogsContractIndex - -import ( - "database/sql" - "gorm.io/gorm" -) - -type Migration struct { -} - -func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error { - query := `create index concurrently idx_transaction_logs_address on transaction_logs(address)` - - _, err := db.Exec(query) - if err != nil { - return err - } - return nil -} - -func (m *Migration) GetName() string { - return "202406110848_transactionLogsContractIndex" -} diff --git a/internal/postgres/migrations/202406141007_addCheckedForAbiFlag/up.go b/internal/postgres/migrations/202406141007_addCheckedForAbiFlag/up.go deleted file mode 100644 index d1e60235..00000000 --- a/internal/postgres/migrations/202406141007_addCheckedForAbiFlag/up.go +++ /dev/null @@ -1,28 +0,0 @@ -package _202406141007_addCheckedForAbiFlag - -import ( - "database/sql" - "gorm.io/gorm" -) - -type Migration struct { -} - -func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error { - queries := []string{ - `alter table contracts add column checked_for_abi boolean`, - `update contracts set checked_for_abi = true where length(contract_abi) > 0 or matching_contract_address != ''`, - } - - for _, query := range queries { - _, err := db.Exec(query) - if err != nil { - return err - } - } - return nil -} - -func (m *Migration) GetName() string { - return "202406141007_addCheckedForAbiFlag" -} diff --git a/internal/postgres/migrations/202406251424_addTransactionLogsOutputDataColumn/up.go b/internal/postgres/migrations/202406251424_addTransactionLogsOutputDataColumn/up.go deleted file mode 100644 index a2d0513a..00000000 --- a/internal/postgres/migrations/202406251424_addTransactionLogsOutputDataColumn/up.go +++ /dev/null @@ -1,23 +0,0 @@ -package _202406251424_addTransactionLogsOutputDataColumn - -import ( - "database/sql" - "gorm.io/gorm" -) - -type Migration struct { -} - -func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error { - query := `alter table transaction_logs add column output_data jsonb;` - - _, err := db.Exec(query) - if err != nil { - return err - } - return nil -} - -func (m *Migration) GetName() string { - return "202406251424_addTransactionLogsOutputDataColumn" -} diff --git a/internal/postgres/migrations/202406251426_addTransactionIndexes/up.go b/internal/postgres/migrations/202406251426_addTransactionIndexes/up.go deleted file mode 100644 index 63c1fd81..00000000 --- a/internal/postgres/migrations/202406251426_addTransactionIndexes/up.go +++ /dev/null @@ -1,29 +0,0 @@ -package _202406251426_addTransactionIndexes - -import ( - "database/sql" - "gorm.io/gorm" -) - -type Migration struct { -} - -func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error { - queries := []string{ - `create index idx_transactions_to_address on transactions(to_address);`, - `create index idx_transactions_from_address on transactions(from_address);`, - `create index idx_transactions_bytecode_hash on transactions(bytecode_hash);`, - `create index idx_transactions_block_number on transactions(block_number);`, - } - for _, query := range queries { - _, err := db.Exec(query) - if err != nil { - return err - } - } - return nil -} - -func (m *Migration) GetName() string { - return "202406251426_addTransactionIndexes" -} diff --git a/internal/postgres/migrations/202407101440_addOperatorRestakedStrategiesTable/up.go b/internal/postgres/migrations/202407101440_addOperatorRestakedStrategiesTable/up.go deleted file mode 100644 index 4d221b00..00000000 --- a/internal/postgres/migrations/202407101440_addOperatorRestakedStrategiesTable/up.go +++ /dev/null @@ -1,37 +0,0 @@ -package _202407101440_addOperatorRestakedStrategiesTable - -import ( - "database/sql" - "gorm.io/gorm" -) - -type Migration struct { -} - -func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error { - queries := []string{ - `CREATE TABLE IF NOT EXISTS operator_restaked_strategies ( - id serial PRIMARY KEY, - block_number bigint NOT NULL, - operator varchar NOT NULL, - avs varchar NOT NULL, - strategy varchar NOT NULL, - created_at timestamp with time zone DEFAULT current_timestamp, - updated_at timestamp with time zone DEFAULT NULL, - deleted_at timestamp with time zone DEFAULT NULL - )`, - `create unique index idx_unique_operator_restaked_strategies on operator_restaked_strategies(block_number, operator, avs, strategy)`, - } - - for _, query := range queries { - _, err := db.Exec(query) - if err != nil { - return err - } - } - return nil -} - -func (m *Migration) GetName() string { - return "202407101440_addOperatorRestakedStrategiesTable" -} diff --git a/internal/postgres/migrations/202407110946_addBlockTimeToRestakedStrategies/up.go b/internal/postgres/migrations/202407110946_addBlockTimeToRestakedStrategies/up.go deleted file mode 100644 index 8ed54be1..00000000 --- a/internal/postgres/migrations/202407110946_addBlockTimeToRestakedStrategies/up.go +++ /dev/null @@ -1,33 +0,0 @@ -package _202407110946_addBlockTimeToRestakedStrategies - -import ( - "database/sql" - "fmt" - "github.com/Layr-Labs/sidecar/internal/postgres" - "gorm.io/gorm" -) - -type Migration struct { -} - -func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error { - queries := []string{ - `ALTER TABLE operator_restaked_strategies add column block_time timestamp with time zone NOT NULL`, - } - - _, err := postgres.WrapTxAndCommit[interface{}](func(tx *gorm.DB) (interface{}, error) { - for _, query := range queries { - err := tx.Exec(query) - if err.Error != nil { - fmt.Printf("Failed to run migration query: %s - %+v\n", query, err.Error) - return 0, err.Error - } - } - return 0, nil - }, nil, grm) - return err -} - -func (m *Migration) GetName() string { - return "202407110946_addBlockTimeToRestakedStrategies" -} diff --git a/internal/postgres/migrations/202407111116_addAvsDirectoryAddress/up.go b/internal/postgres/migrations/202407111116_addAvsDirectoryAddress/up.go deleted file mode 100644 index 88499844..00000000 --- a/internal/postgres/migrations/202407111116_addAvsDirectoryAddress/up.go +++ /dev/null @@ -1,33 +0,0 @@ -package _202407111116_addAvsDirectoryAddress - -import ( - "database/sql" - "fmt" - "github.com/Layr-Labs/sidecar/internal/postgres" - "gorm.io/gorm" -) - -type Migration struct { -} - -func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error { - queries := []string{ - `ALTER TABLE operator_restaked_strategies add column avs_directory_address varchar`, - } - - _, err := postgres.WrapTxAndCommit[interface{}](func(tx *gorm.DB) (interface{}, error) { - for _, query := range queries { - err := tx.Exec(query) - if err.Error != nil { - fmt.Printf("Failed to run migration query: %s - %+v\n", query, err.Error) - return 0, err.Error - } - } - return 0, nil - }, nil, grm) - return err -} - -func (m *Migration) GetName() string { - return "202407111116_addAvsDirectoryAddress" -} diff --git a/internal/postgres/migrations/202407121407_updateProxyContractIndex/up.go b/internal/postgres/migrations/202407121407_updateProxyContractIndex/up.go deleted file mode 100644 index d5ada47d..00000000 --- a/internal/postgres/migrations/202407121407_updateProxyContractIndex/up.go +++ /dev/null @@ -1,27 +0,0 @@ -package _202407121407_updateProxyContractIndex - -import ( - "database/sql" - "gorm.io/gorm" -) - -type Migration struct { -} - -func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error { - queries := []string{ - `create unique index idx_uniq_proxy_contract on proxy_contracts(block_number, contract_address)`, - `drop index idx_unique_proxy_contract`, - } - for _, query := range queries { - _, err := db.Exec(query) - if err != nil { - return err - } - } - return nil -} - -func (m *Migration) GetName() string { - return "202407121407_updateProxyContractIndex" -} diff --git a/internal/postgres/migrations/202408200934_eigenlayerStateTables/up.go b/internal/postgres/migrations/202408200934_eigenlayerStateTables/up.go deleted file mode 100644 index 5fefbe2b..00000000 --- a/internal/postgres/migrations/202408200934_eigenlayerStateTables/up.go +++ /dev/null @@ -1,160 +0,0 @@ -package _202408200934_eigenlayerStateTables - -import ( - "database/sql" - "fmt" - "github.com/Layr-Labs/sidecar/internal/postgres" - "gorm.io/gorm" -) - -type Migration struct { -} - -func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error { - queries := []string{ - `create table if not exists avs_operator_changes ( - id serial primary key, - operator varchar, - avs varchar, - registered boolean, - transaction_hash varchar, - transaction_index bigint, - log_index bigint, - block_number bigint, - created_at timestamp with time zone default current_timestamp - ) - `, - `create index if not exists idx_avs_operator_changes_avs_operator on avs_operator_changes (avs, operator)`, - `create index if not exists idx_avs_operator_changes_block on avs_operator_changes (block_number)`, - `create table if not exists registered_avs_operators ( - operator varchar, - avs varchar, - block_number bigint, - created_at timestamp with time zone default current_timestamp, - unique(operator, avs, block_number) - ); - `, - `create index if not exists idx_registered_avs_operators_avs_operator on registered_avs_operators (avs, operator)`, - `create index if not exists idx_registered_avs_operators_block on registered_avs_operators (block_number)`, - /* - `create table if not exists staker_share_changes ( - id serial primary key, - staker varchar, - strategy varchar, - shares numeric, - transaction_hash varchar, - log_index bigint, - block_number bigint, - created_at timestamp with time zone default current_timestamp - ); - `, - `create index if not exists idx_staker_share_changes_staker_strat on staker_share_changes (staker, strategy)`, - `create index if not exists idx_staker_share_changes_block on staker_share_changes (block_number)`, - `create table if not exists active_reward_submissions ( - id serial primary key, - avs varchar, - reward_hash varchar, - token varchar, - amount numeric, - strategy varchar, - multiplier numeric, - strategy_index bigint, - transaction_hash varchar, - log_index bigint, - block_number bigint, - start_timestamp timestamp, - end_timestamp timestamp, - duration bigint, - created_at timestamp with time zone default current_timestamp - ); - `, - `create index if not exists idx_active_reward_submissions_avs on active_reward_submissions (avs)`, - `create index if not exists idx_active_reward_submissions_block on active_reward_submissions (block_number)`, - `create table if not exists active_reward_for_all_submissions ( - id serial primary key, - avs varchar, - reward_hash varchar, - token varchar, - amount numeric, - strategy varchar, - multiplier numeric, - strategy_index bigint, - transaction_hash varchar, - log_index bigint, - block_number bigint, - start_timestamp timestamp, - end_timestamp timestamp, - duration bigint, - created_at timestamp with time zone default current_timestamp - ); - `, - `create index if not exists idx_active_reward_for_all_submissions_avs on active_reward_for_all_submissions (avs)`, - `create index if not exists idx_active_reward_for_all_submissions_block on active_reward_for_all_submissions (block_number)`, - `create table if not exists staker_shares ( - staker varchar, - strategy varchar, - shares numeric, - block_number bigint, - created_at timestamp with time zone default current_timestamp, - unique(staker, strategy, block_number) - ) - `, - `create index if not exists idx_staker_shares_staker_strategy on staker_shares (staker, strategy)`, - `create index if not exists idx_staker_shares_block on staker_shares (block_number)`, - - `create table if not exists active_rewards ( - avs varchar, - reward_hash varchar, - token varchar, - amount numeric, - strategy varchar, - multiplier numeric, - strategy_index bigint, - block_number bigint, - start_timestamp timestamp, - end_timestamp timestamp, - duration bigint, - created_at timestamp with time zone default current_timestamp - )`, - `create index if not exists idx_active_rewards_avs on active_rewards (avs)`, - `create index if not exists idx_active_rewards_block on active_rewards (block_number)`, - `create table if not exists active_reward_for_all ( - avs varchar, - reward_hash varchar, - token varchar, - amount numeric, - strategy varchar, - multiplier numeric, - strategy_index bigint, - block_number bigint, - start_timestamp timestamp, - end_timestamp timestamp, - duration bigint, - created_at timestamp with time zone default current_timestamp - )`, - `create index if not exists idx_active_reward_for_all_avs on active_reward_for_all (avs)`, - `create index if not exists idx_active_reward_for_all_block on active_reward_for_all (block_number)`, - */ - } - - // Wrap the queries in a transaction so they all create or fail atomically - _, err := postgres.WrapTxAndCommit[interface{}](func(tx *gorm.DB) (interface{}, error) { - p, err := tx.DB() - if err != nil { - return nil, err - } - for _, query := range queries { - _, err := p.Exec(query) - if err != nil { - fmt.Printf("Failed to execute query: %s\n", query) - return nil, err - } - } - return nil, nil - }, grm, nil) - return err -} - -func (m *Migration) GetName() string { - return "202408200934_eigenlayerStateTables" -} diff --git a/internal/postgres/migrations/202409051720_operatorShareChanges/up.go b/internal/postgres/migrations/202409051720_operatorShareChanges/up.go deleted file mode 100644 index b61ee8e0..00000000 --- a/internal/postgres/migrations/202409051720_operatorShareChanges/up.go +++ /dev/null @@ -1,49 +0,0 @@ -package _202409051720_operatorShareChanges - -import ( - "database/sql" - "gorm.io/gorm" -) - -type Migration struct { -} - -func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error { - queries := []string{ - `create table if not exists operator_share_changes ( - id serial primary key, - operator varchar, - strategy varchar, - shares numeric, - transaction_hash varchar, - transaction_index bigint, - log_index bigint, - block_number bigint, - created_at timestamp with time zone default current_timestamp, - unique(operator, strategy, transaction_hash, log_index) - ) - `, - `create index if not exists idx_operator_share_changes_operator_strat on operator_share_changes (operator, strategy)`, - `create index if not exists idx_operator_share_changes_block on operator_share_changes (block_number)`, - `create table if not exists operator_shares ( - operator varchar, - strategy varchar, - shares numeric, - block_number bigint, - created_at timestamp with time zone default current_timestamp, - unique (operator, strategy, block_number) - )`, - `create index if not exists idx_operator_shares_operator_strategy on operator_shares (operator, strategy)`, - `create index if not exists idx_operator_shares_block on operator_shares (block_number)`, - } - for _, query := range queries { - if _, err := db.Exec(query); err != nil { - return err - } - } - return nil -} - -func (m *Migration) GetName() string { - return "202409051720_operatorShareChanges" -} diff --git a/internal/postgres/migrations/202409052151_stakerDelegations/up.go b/internal/postgres/migrations/202409052151_stakerDelegations/up.go deleted file mode 100644 index 180a010f..00000000 --- a/internal/postgres/migrations/202409052151_stakerDelegations/up.go +++ /dev/null @@ -1,46 +0,0 @@ -package _202409052151_stakerDelegations - -import ( - "database/sql" - "gorm.io/gorm" -) - -type Migration struct { -} - -func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error { - queries := []string{ - `create table if not exists staker_delegation_changes ( - staker varchar, - operator varchar, - delegated boolean, - transaction_hash varchar, - log_index bigint, - transaction_index bigint, - block_number bigint, - created_at timestamp with time zone default current_timestamp, - unique(staker, operator, log_index, block_number) - );`, - `create index if not exists idx_staker_delegation_changes_staker_operator on staker_delegation_changes (staker, operator)`, - `create index if not exists idx_staker_delegation_changes_block on staker_delegation_changes (block_number)`, - `create table if not exists delegated_stakers ( - staker varchar, - operator varchar, - block_number bigint, - created_at timestamp with time zone default current_timestamp, - unique(staker, operator, block_number) - )`, - `create index if not exists idx_delegated_stakers_staker_operator on delegated_stakers (staker, operator)`, - `create index if not exists idx_delegated_stakers_block on delegated_stakers (block_number)`, - } - for _, query := range queries { - if _, err := db.Exec(query); err != nil { - return err - } - } - return nil -} - -func (m *Migration) GetName() string { - return "202409052151_stakerDelegations" -} diff --git a/internal/postgres/migrations/202409061121_removeSequenceId/up.go b/internal/postgres/migrations/202409061121_removeSequenceId/up.go deleted file mode 100644 index aa12a4b1..00000000 --- a/internal/postgres/migrations/202409061121_removeSequenceId/up.go +++ /dev/null @@ -1,49 +0,0 @@ -package _202409061121_removeSequenceId - -import ( - "database/sql" - "fmt" - "gorm.io/gorm" -) - -type Migration struct { -} - -func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error { - queries := []string{ - `alter table blocks add constraint unique_blocks unique (number)`, - - `alter table transactions add constraint transactions_transaction_hash_block_number_key unique (transaction_hash, block_number)`, - `alter table transactions ADD CONSTRAINT transactions_block_number_fkey FOREIGN KEY (block_number) REFERENCES blocks("number") ON DELETE CASCADE;`, - `alter table transaction_logs ADD CONSTRAINT fk_transaction_hash_block_number_key FOREIGN KEY (transaction_hash, block_number) REFERENCES public.transactions(transaction_hash, block_number) ON DELETE CASCADE;`, - `alter table transaction_logs ADD CONSTRAINT transaction_logs_block_number_fkey FOREIGN KEY (block_number) REFERENCES public.blocks("number") ON DELETE CASCADE;`, - - // drop constraints - `alter table transaction_logs drop constraint fk_transaction_hash_sequence_id_key`, - `alter table transaction_logs drop constraint transaction_logs_block_sequence_id_fkey`, - - `alter table transactions drop constraint transactions_transaction_hash_sequence_id_key`, - `alter table transactions drop constraint transactions_block_sequence_id_fkey`, - - `alter table transactions drop column block_sequence_id`, - `alter table transaction_logs drop column block_sequence_id`, - // re-add constraints - - `alter table blocks drop constraint block_sequences_pkey`, - `alter table blocks add constraint block_pkey primary key (number)`, - - `alter table blocks drop column id`, - } - - for _, query := range queries { - if _, err := db.Exec(query); err != nil { - fmt.Println("Error executing query: ", query) - return err - } - } - return nil -} - -func (m *Migration) GetName() string { - return "202409061121_removeSequenceId" -} diff --git a/internal/postgres/migrations/migrator.go b/internal/postgres/migrations/migrator.go deleted file mode 100644 index 1592ed3f..00000000 --- a/internal/postgres/migrations/migrator.go +++ /dev/null @@ -1,135 +0,0 @@ -package migrations - -import ( - "database/sql" - "fmt" - _202405150900_bootstrapDb "github.com/Layr-Labs/sidecar/internal/postgres/migrations/202405150900_bootstrapDb" - _202405150917_insertContractAbi "github.com/Layr-Labs/sidecar/internal/postgres/migrations/202405150917_insertContractAbi" - _202405151523_addTransactionToFrom "github.com/Layr-Labs/sidecar/internal/postgres/migrations/202405151523_addTransactionToFrom" - _202405170842_addBlockInfoToTransactionLog "github.com/Layr-Labs/sidecar/internal/postgres/migrations/202405170842_addBlockInfoToTransactionLog" - _202405171056_unverifiedContracts "github.com/Layr-Labs/sidecar/internal/postgres/migrations/202405171056_unverifiedContracts" - _202405171345_addUpdatedPaymentCoordinatorAbi "github.com/Layr-Labs/sidecar/internal/postgres/migrations/202405171345_addUpdatedPaymentCoordinatorAbi" - _202405201503_fixTransactionHashConstraint "github.com/Layr-Labs/sidecar/internal/postgres/migrations/202405201503_fixTransactionHashConstraint" - _202405300925_addUniqueBlockConstraint "github.com/Layr-Labs/sidecar/internal/postgres/migrations/202405300925_addUniqueBlockConstraint" - _202405312008_indexTransactionContractAddress "github.com/Layr-Labs/sidecar/internal/postgres/migrations/202405312008_indexTransactionContractAddress" - _202405312134_handleProxyContracts "github.com/Layr-Labs/sidecar/internal/postgres/migrations/202405312134_handleProxyContracts" - _202406030920_addCheckedForProxyFlag "github.com/Layr-Labs/sidecar/internal/postgres/migrations/202406030920_addCheckedForProxyFlag" - _202406031946_addSerialIdToContracts "github.com/Layr-Labs/sidecar/internal/postgres/migrations/202406031946_addSerialIdToContracts" - _202406051937_addBytecodeIndex "github.com/Layr-Labs/sidecar/internal/postgres/migrations/202406051937_addBytecodeIndex" - _202406071318_indexTransactionLogBlockNumber "github.com/Layr-Labs/sidecar/internal/postgres/migrations/202406071318_indexTransactionLogBlockNumber" - _202406110848_transactionLogsContractIndex "github.com/Layr-Labs/sidecar/internal/postgres/migrations/202406110848_transactionLogsContractIndex" - _202406141007_addCheckedForAbiFlag "github.com/Layr-Labs/sidecar/internal/postgres/migrations/202406141007_addCheckedForAbiFlag" - _202406251424_addTransactionLogsOutputDataColumn "github.com/Layr-Labs/sidecar/internal/postgres/migrations/202406251424_addTransactionLogsOutputDataColumn" - _202406251426_addTransactionIndexes "github.com/Layr-Labs/sidecar/internal/postgres/migrations/202406251426_addTransactionIndexes" - _202407101440_addOperatorRestakedStrategiesTable "github.com/Layr-Labs/sidecar/internal/postgres/migrations/202407101440_addOperatorRestakedStrategiesTable" - _202407110946_addBlockTimeToRestakedStrategies "github.com/Layr-Labs/sidecar/internal/postgres/migrations/202407110946_addBlockTimeToRestakedStrategies" - _202407111116_addAvsDirectoryAddress "github.com/Layr-Labs/sidecar/internal/postgres/migrations/202407111116_addAvsDirectoryAddress" - _202407121407_updateProxyContractIndex "github.com/Layr-Labs/sidecar/internal/postgres/migrations/202407121407_updateProxyContractIndex" - _202408200934_eigenlayerStateTables "github.com/Layr-Labs/sidecar/internal/postgres/migrations/202408200934_eigenlayerStateTables" - _202409051720_operatorShareChanges "github.com/Layr-Labs/sidecar/internal/postgres/migrations/202409051720_operatorShareChanges" - _202409052151_stakerDelegations "github.com/Layr-Labs/sidecar/internal/postgres/migrations/202409052151_stakerDelegations" - _202409061121_removeSequenceId "github.com/Layr-Labs/sidecar/internal/postgres/migrations/202409061121_removeSequenceId" - "go.uber.org/zap" - "gorm.io/gorm" - "time" -) - -type Migration interface { - Up(db *sql.DB, grm *gorm.DB) error - GetName() string -} - -type Migrator struct { - Db *sql.DB - GDb *gorm.DB - Logger *zap.Logger -} - -func NewMigrator(db *sql.DB, gDb *gorm.DB, l *zap.Logger) *Migrator { - gDb.AutoMigrate(&Migrations{}) - return &Migrator{ - Db: db, - GDb: gDb, - Logger: l, - } -} - -func (m *Migrator) MigrateAll() error { - migrations := []Migration{ - &_202405150900_bootstrapDb.Migration{}, - &_202405150917_insertContractAbi.Migration{}, - &_202405151523_addTransactionToFrom.Migration{}, - &_202405170842_addBlockInfoToTransactionLog.Migration{}, - &_202405171056_unverifiedContracts.Migration{}, - &_202405171345_addUpdatedPaymentCoordinatorAbi.Migration{}, - &_202405201503_fixTransactionHashConstraint.Migration{}, - &_202405300925_addUniqueBlockConstraint.Migration{}, - &_202405312008_indexTransactionContractAddress.Migration{}, - &_202405312134_handleProxyContracts.Migration{}, - &_202406030920_addCheckedForProxyFlag.Migration{}, - &_202406031946_addSerialIdToContracts.Migration{}, - &_202406051937_addBytecodeIndex.Migration{}, - &_202406071318_indexTransactionLogBlockNumber.Migration{}, - &_202406110848_transactionLogsContractIndex.Migration{}, - &_202406141007_addCheckedForAbiFlag.Migration{}, - &_202406251424_addTransactionLogsOutputDataColumn.Migration{}, - &_202406251426_addTransactionIndexes.Migration{}, - &_202407101440_addOperatorRestakedStrategiesTable.Migration{}, - &_202407110946_addBlockTimeToRestakedStrategies.Migration{}, - &_202407111116_addAvsDirectoryAddress.Migration{}, - &_202407121407_updateProxyContractIndex.Migration{}, - &_202408200934_eigenlayerStateTables.Migration{}, - &_202409051720_operatorShareChanges.Migration{}, - &_202409052151_stakerDelegations.Migration{}, - &_202409061121_removeSequenceId.Migration{}, - } - - for _, migration := range migrations { - err := m.Migrate(migration) - if err != nil { - panic(err) - } - } - return nil -} - -func (m *Migrator) Migrate(migration Migration) error { - name := migration.GetName() - - // find migration by name - var migrationRecord Migrations - result := m.GDb.Find(&migrationRecord, "name = ?", name).Limit(1) - - if result.Error == nil && result.RowsAffected == 0 { - m.Logger.Sugar().Infof("Running migration '%s'", name) - // run migration - err := migration.Up(m.Db, m.GDb) - if err != nil { - m.Logger.Sugar().Errorw(fmt.Sprintf("Failed to run migration '%s'", name), zap.Error(err)) - return err - } - - // record migration - migrationRecord = Migrations{ - Name: name, - } - result = m.GDb.Create(&migrationRecord) - if result.Error != nil { - m.Logger.Sugar().Errorw(fmt.Sprintf("Failed to record migration '%s'", name), zap.Error(result.Error)) - return result.Error - } - } else if result.Error != nil { - m.Logger.Sugar().Errorw(fmt.Sprintf("Failed to find migration '%s'", name), zap.Error(result.Error)) - return result.Error - } else if result.RowsAffected > 0 { - m.Logger.Sugar().Infof("Migration %s already run", name) - return nil - } - return nil -} - -type Migrations struct { - Name string `gorm:"primaryKey"` - CreatedAt time.Time `gorm:"default:current_timestamp;type:timestamp with time zone"` - UpdatedAt time.Time `gorm:"default:null;type:timestamp with time zone"` -} diff --git a/internal/postgres/postgres.go b/internal/postgres/postgres.go deleted file mode 100644 index 282b08ba..00000000 --- a/internal/postgres/postgres.go +++ /dev/null @@ -1,80 +0,0 @@ -package postgres - -import ( - "database/sql" - "fmt" - _ "github.com/lib/pq" - "golang.org/x/xerrors" - "gorm.io/driver/postgres" - "gorm.io/gorm" - "gorm.io/gorm/logger" -) - -type PostgresConfig struct { - Host string - Port int - Username string - Password string - DbName string -} - -type Postgres struct { - Db *sql.DB -} - -func NewPostgres(cfg *PostgresConfig) (*Postgres, error) { - authString := "" - if cfg.Username != "" { - authString = fmt.Sprintf("%s user=%s", authString, cfg.Username) - } - if cfg.Password != "" { - authString = fmt.Sprintf("%s password=%s", authString, cfg.Password) - } - connectString := fmt.Sprintf("host=%s %s dbname=%s port=%d sslmode=disable TimeZone=UTC", - cfg.Host, - authString, - cfg.DbName, - cfg.Port, - ) - db, err := sql.Open("postgres", connectString) - if err != nil { - return nil, xerrors.Errorf("Failed to setup database", err) - } - - return &Postgres{ - Db: db, - }, nil -} - -func NewGormFromPostgresConnection(pgDb *sql.DB) (*gorm.DB, error) { - db, err := gorm.Open(postgres.New(postgres.Config{ - Conn: pgDb, - }), &gorm.Config{ - Logger: logger.Default.LogMode(logger.Silent), - }) - if err != nil { - return nil, xerrors.Errorf("Failed to setup database", err) - } - - return db, nil -} - -func WrapTxAndCommit[T any](fn func(*gorm.DB) (T, error), db *gorm.DB, tx *gorm.DB) (T, error) { - exists := tx != nil - - if !exists { - tx = db.Begin() - } - - res, err := fn(tx) - - if err != nil && !exists { - fmt.Printf("Rollback transaction\n") - tx.Rollback() - } - if err == nil && !exists { - fmt.Printf("Commit transaction\n") - tx.Commit() - } - return res, err -} diff --git a/internal/postgres/tests/postgres_test.go b/internal/postgres/tests/postgres_test.go deleted file mode 100644 index d3f3f245..00000000 --- a/internal/postgres/tests/postgres_test.go +++ /dev/null @@ -1,151 +0,0 @@ -package tests - -import ( - "fmt" - "github.com/Layr-Labs/sidecar/internal/config" - "github.com/Layr-Labs/sidecar/internal/postgres" - "github.com/Layr-Labs/sidecar/internal/tests" - "github.com/stretchr/testify/assert" - "gorm.io/gorm" - "testing" -) - -func setup() ( - *config.Config, - *postgres.Postgres, - *gorm.DB, - error, -) { - cfg := tests.GetConfig() - - pg, grm, err := tests.GetDatabaseConnection(cfg) - - return cfg, pg, grm, err -} - -func TestDoesWrappedTransactionRollback(t *testing.T) { - _, pg, grm, err := setup() - if err != nil { - t.Fatal(err) - } - defer pg.Db.Close() - - query := `create table test_table (id int);` - _, err = pg.Db.Exec(query) - if err != nil { - t.Fatal(err) - } - - _, err = postgres.WrapTxAndCommit[interface{}](func(tx *gorm.DB) (interface{}, error) { - for i := 0; i < 10; i++ { - query := "INSERT INTO test_table (id) VALUES (?)" - res := tx.Exec(query, i) - if res.Error != nil { - return nil, err - } - } - t.Logf("Inserted 10 rows, simulating a failure") - return nil, fmt.Errorf("simulated failure") - }, grm, nil) - - assert.NotNil(t, err) - - selectQuery := `select count(*) from test_table;` - var count int - err = pg.Db.QueryRow(selectQuery).Scan(&count) - if err != nil { - t.Fatal(err) - } - t.Logf("Found '%d' rows", count) - assert.Equal(t, 0, count) - - dropQuery := `drop table test_table;` - _, err = pg.Db.Exec(dropQuery) - if err != nil { - t.Fatal(err) - } - -} - -func TestDoesWrappedNestedTransactionRollback(t *testing.T) { - _, pg, grm, err := setup() - if err != nil { - t.Fatal(err) - } - defer pg.Db.Close() - - query := `create table test_table (id int);` - _, err = pg.Db.Exec(query) - if err != nil { - t.Fatal(err) - } - - _, err = postgres.WrapTxAndCommit[interface{}](func(tx *gorm.DB) (interface{}, error) { - _, err = postgres.WrapTxAndCommit[interface{}](func(tx *gorm.DB) (interface{}, error) { - for i := 0; i < 10; i++ { - query := "INSERT INTO test_table (id) VALUES (?)" - res := tx.Exec(query, i) - if res.Error != nil { - return nil, err - } - } - t.Logf("Inserted 10 rows") - var count int - tx.Raw(`select count(*) from test_table;`).Scan(&count) - assert.Equal(t, 10, count) - t.Logf("Found '%d' rows", count) - return nil, nil - }, grm, tx) - _, err = postgres.WrapTxAndCommit[interface{}](func(tx *gorm.DB) (interface{}, error) { - for i := 0; i < 10; i++ { - query := "INSERT INTO test_table (id) VALUES (?)" - res := tx.Exec(query, i) - if res.Error != nil { - return nil, err - } - } - t.Logf("Inserted 10 rows") - var count int - tx.Raw(`select count(*) from test_table;`).Scan(&count) - t.Logf("Found '%d' rows", count) - assert.Equal(t, 20, count) - t.Logf("Inserted 10 rows") - return nil, nil - }, grm, tx) - return postgres.WrapTxAndCommit[interface{}](func(tx *gorm.DB) (interface{}, error) { - for i := 0; i < 10; i++ { - query := "INSERT INTO test_table (id) VALUES (?)" - res := tx.Exec(query, i) - if res.Error != nil { - return nil, err - } - } - t.Logf("Inserted 10 rows") - var count int - tx.Raw(`select count(*) from test_table;`).Scan(&count) - t.Logf("Found '%d' rows", count) - assert.Equal(t, 30, count) - t.Logf("Inserted 10 rows, simulating a failure") - return nil, fmt.Errorf("simulated failure") - }, grm, tx) - - }, grm, nil) - - assert.NotNil(t, err) - - selectQuery := `select count(*) from test_table;` - var count int - err = pg.Db.QueryRow(selectQuery).Scan(&count) - if err != nil { - t.Fatal(err) - } - t.Logf("Found '%d' rows", count) - assert.Equal(t, 0, count) - - dropQuery := `drop table test_table;` - _, err = pg.Db.Exec(dropQuery) - if err != nil { - t.Fatal(err) - } - -} diff --git a/internal/sqlite/sqlite.go b/internal/sqlite/sqlite.go index 94a52c41..f6f98f2b 100644 --- a/internal/sqlite/sqlite.go +++ b/internal/sqlite/sqlite.go @@ -4,6 +4,7 @@ import ( "fmt" "gorm.io/driver/sqlite" "gorm.io/gorm" + "gorm.io/gorm/logger" ) func NewSqlite(path string) gorm.Dialector { @@ -13,7 +14,7 @@ func NewSqlite(path string) gorm.Dialector { func NewGormSqliteFromSqlite(sqlite gorm.Dialector) (*gorm.DB, error) { db, err := gorm.Open(sqlite, &gorm.Config{ - //Logger: logger.Default.LogMode(logger.Silent), + Logger: logger.Default.LogMode(logger.Silent), }) if err != nil { return nil, err diff --git a/internal/storage/postgresql/storage.go b/internal/storage/postgresql/storage.go index 8b94cb8f..17ed5e46 100644 --- a/internal/storage/postgresql/storage.go +++ b/internal/storage/postgresql/storage.go @@ -5,7 +5,7 @@ import ( "errors" "github.com/Layr-Labs/sidecar/internal/config" "github.com/Layr-Labs/sidecar/internal/parser" - pg "github.com/Layr-Labs/sidecar/internal/postgres" + "github.com/Layr-Labs/sidecar/internal/sqlite" "github.com/Layr-Labs/sidecar/internal/storage" "go.uber.org/zap" "golang.org/x/xerrors" @@ -62,7 +62,7 @@ func (m *PostgresBlockStore) InsertBlockAtHeight( hash string, blockTime uint64, ) (*storage.Block, error) { - return pg.WrapTxAndCommit[*storage.Block](func(txn *gorm.DB) (*storage.Block, error) { + return sqlite.WrapTxAndCommit[*storage.Block](func(txn *gorm.DB) (*storage.Block, error) { blockSeq := &storage.Block{ Number: blockNumber, @@ -91,7 +91,7 @@ func (m *PostgresBlockStore) InsertBlockTransaction( to = strings.ToLower(to) from = strings.ToLower(from) contractAddress = strings.ToLower(contractAddress) - return pg.WrapTxAndCommit[*storage.Transaction](func(txn *gorm.DB) (*storage.Transaction, error) { + return sqlite.WrapTxAndCommit[*storage.Transaction](func(txn *gorm.DB) (*storage.Transaction, error) { tx := &storage.Transaction{ BlockNumber: blockNumber, TransactionHash: txHash, @@ -118,7 +118,7 @@ func (m *PostgresBlockStore) InsertTransactionLog( log *parser.DecodedLog, outputData map[string]interface{}, ) (*storage.TransactionLog, error) { - return pg.WrapTxAndCommit[*storage.TransactionLog](func(txn *gorm.DB) (*storage.TransactionLog, error) { + return sqlite.WrapTxAndCommit[*storage.TransactionLog](func(txn *gorm.DB) (*storage.TransactionLog, error) { argsJson, err := json.Marshal(log.Arguments) if err != nil { m.Logger.Sugar().Errorw("Failed to marshal arguments", zap.Error(err)) @@ -179,7 +179,7 @@ func (m *PostgresBlockStore) GetBlockByNumber(blockNumber uint64) (*storage.Bloc } func (m *PostgresBlockStore) InsertOperatorRestakedStrategies(avsDirectorAddress string, blockNumber uint64, blockTime time.Time, operator string, avs string, strategy string) (*storage.OperatorRestakedStrategies, error) { - return pg.WrapTxAndCommit[*storage.OperatorRestakedStrategies](func(txn *gorm.DB) (*storage.OperatorRestakedStrategies, error) { + return sqlite.WrapTxAndCommit[*storage.OperatorRestakedStrategies](func(txn *gorm.DB) (*storage.OperatorRestakedStrategies, error) { ors := &storage.OperatorRestakedStrategies{ AvsDirectoryAddress: strings.ToLower(avsDirectorAddress), BlockNumber: blockNumber, diff --git a/internal/tests/utils.go b/internal/tests/utils.go index f0a0d1d5..7b5380ee 100644 --- a/internal/tests/utils.go +++ b/internal/tests/utils.go @@ -2,7 +2,6 @@ package tests import ( "github.com/Layr-Labs/sidecar/internal/config" - "github.com/Layr-Labs/sidecar/internal/postgres" sqlite2 "github.com/Layr-Labs/sidecar/internal/sqlite" "gorm.io/gorm" ) @@ -11,25 +10,6 @@ func GetConfig() *config.Config { return config.NewConfig() } -func GetDatabaseConnection(cfg *config.Config) (*postgres.Postgres, *gorm.DB, error) { - db, err := postgres.NewPostgres(&postgres.PostgresConfig{ - Host: cfg.PostgresConfig.Host, - Port: cfg.PostgresConfig.Port, - Username: cfg.PostgresConfig.Username, - Password: cfg.PostgresConfig.Password, - DbName: cfg.PostgresConfig.DbName, - }) - if err != nil { - panic(err) - } - - grm, err := postgres.NewGormFromPostgresConnection(db.Db) - if err != nil { - panic(err) - } - return db, grm, nil -} - const sqliteInMemoryPath = "file::memory:?cache=shared" func GetSqliteDatabaseConnection() (*gorm.DB, error) {