From 765312de75d0168b6d1895923aa9b8da05621ca7 Mon Sep 17 00:00:00 2001 From: Major Date: Fri, 23 Jun 2023 13:09:37 +0200 Subject: [PATCH] fix: use local db when this should be --- cmd/main.go | 1 - common/store/db.badger.go | 11 +- common/store/initializer.go | 3 +- common/store/keys.go | 24 ++-- common/store/store.getter.go | 2 +- common/store/store.loader.go | 122 ++++++++-------- common/store/store.setter.go | 185 +++++++++++++------------ internal/events/filterNewVault.go | 12 +- internal/events/filterStrategyAdded.go | 51 ++----- internal/registries/accessor.go | 6 +- internal/strategies/risk.framework.go | 3 +- 11 files changed, 193 insertions(+), 227 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 81c5a3371..0127ed538 100755 --- a/cmd/main.go +++ b/cmd/main.go @@ -37,7 +37,6 @@ func SummonDaemonsw(chainID uint64, parentWg *sync.WaitGroup) { func main() { initFlags() - logs.Info(`Loading initial state in memory`) loadDaemonsForAllChains(trace) summonDaemonsForAllChains(trace) var wg sync.WaitGroup diff --git a/common/store/db.badger.go b/common/store/db.badger.go index 7fadcc067..bb0c3d24d 100755 --- a/common/store/db.badger.go +++ b/common/store/db.badger.go @@ -39,13 +39,11 @@ func _assertMap() { } if badgerDBMutex[chainID] == nil { badgerDBMutex[chainID] = make(map[string]*sync.Mutex) - badgerDBMutex[chainID][TABLES.BLOCK_TIME] = &sync.Mutex{} badgerDBMutex[chainID][TABLES.PRICES] = &sync.Mutex{} - badgerDBMutex[chainID][TABLES.HISTORICAL_PRICES] = &sync.Mutex{} - badgerDBMutex[chainID][TABLES.STRATEGIES] = &sync.Mutex{} - badgerDBMutex[chainID][TABLES.TOKENS] = &sync.Mutex{} - badgerDBMutex[chainID][TABLES.VAULTS] = &sync.Mutex{} badgerDBMutex[chainID][TABLES.VAULTS_LEGACY] = &sync.Mutex{} + badgerDBMutex[chainID][TABLES.VAULTS_FROM_REGISTRY_SYNC] = &sync.Mutex{} + badgerDBMutex[chainID][TABLES.STRATEGIES_FROM_VAULT_SYNC] = &sync.Mutex{} + badgerDBMutex[chainID][TABLES.REGISTRY_SYNC] = &sync.Mutex{} } } isBadgerDBLoaded = true @@ -143,6 +141,7 @@ func ListFromBadgerDB(chainID uint64, dbKey string, dest interface{}) error { k := item.Key() v, err := item.ValueCopy(nil) if err != nil { + logs.Error(err) traces. Capture(`error`, `impossible to get value for key: `+string(k)). SetEntity(`database`). @@ -205,6 +204,8 @@ func ListFromBadgerDB(chainID uint64, dbKey string, dest interface{}) error { ** Assign our reflected value in our dest pointer **************************************************************************************/ reflect.ValueOf(dest).Elem().Set(destMap) + } else { + logs.Warning(`Unsupported type: `, elem.Kind()) } return nil }) diff --git a/common/store/initializer.go b/common/store/initializer.go index e3684cd65..c22781005 100644 --- a/common/store/initializer.go +++ b/common/store/initializer.go @@ -4,7 +4,6 @@ import ( "sync" "github.com/yearn/ydaemon/common/env" - "github.com/yearn/ydaemon/common/logs" ) type TDBType string @@ -17,6 +16,7 @@ const ( var _dbType TDBType var _blockTimeSyncMap = make(map[uint64]*sync.Map) var _historicalPriceSyncMap = make(map[uint64]*sync.Map) + var _newVaultsFromRegistrySyncMap = make(map[uint64]*sync.Map) var _vaultsSyncMap = make(map[uint64]*sync.Map) var _erc20SyncMap = make(map[uint64]*sync.Map) @@ -44,7 +44,6 @@ func init() { } wg := &sync.WaitGroup{} - logs.Info(`Loading DB`) for _, chainID := range env.SUPPORTED_CHAIN_IDS { wg.Add(3) go LoadBlockTime(chainID, nil) diff --git a/common/store/keys.go b/common/store/keys.go index ff35b664d..b6d200e91 100755 --- a/common/store/keys.go +++ b/common/store/keys.go @@ -2,21 +2,17 @@ package store // TTables lists the keys used in the database type TTables = struct { - BLOCK_TIME string - PRICES string - HISTORICAL_PRICES string - STRATEGIES string - TOKENS string - VAULTS string - VAULTS_LEGACY string + PRICES string + VAULTS_LEGACY string + VAULTS_FROM_REGISTRY_SYNC string // For a given registry, sync the last block checked for a given vault + STRATEGIES_FROM_VAULT_SYNC string // For a given vault, current last block checked for the strategies added + REGISTRY_SYNC string } var TABLES = TTables{ - BLOCK_TIME: "blocktime", - PRICES: "prices", - HISTORICAL_PRICES: "historical_prices", - STRATEGIES: "strategies", - TOKENS: "tokens", - VAULTS: "vaults", - VAULTS_LEGACY: "legacy_vaults", + PRICES: `prices`, + VAULTS_LEGACY: `legacy_vaults`, + VAULTS_FROM_REGISTRY_SYNC: `db_new_vaults_from_registries`, + STRATEGIES_FROM_VAULT_SYNC: `db_new_strategies_from_vaults`, + REGISTRY_SYNC: `db_registry_sync`, } diff --git a/common/store/store.getter.go b/common/store/store.getter.go index 2f7445f50..d0f34c73b 100644 --- a/common/store/store.getter.go +++ b/common/store/store.getter.go @@ -190,7 +190,7 @@ func ListAllVaults(chainID uint64) (asMap map[common.Address]models.TVault, asSl ** given chainID. Both a map and a slice are returned. ** Note: It's for the TStrategyAdded structure. **************************************************************************************************/ -func ListAllStrategiess(chainID uint64) ( +func ListAllStrategies(chainID uint64) ( asMap map[common.Address]models.TStrategyAdded, asSlice []models.TStrategyAdded, ) { diff --git a/common/store/store.loader.go b/common/store/store.loader.go index 691aec55f..9a58d296a 100644 --- a/common/store/store.loader.go +++ b/common/store/store.loader.go @@ -26,14 +26,8 @@ func LoadBlockTime(chainID uint64, wg *sync.WaitGroup) { switch _dbType { case DBBadger: - temp := make(map[uint64]uint64) - ListFromBadgerDB(chainID, TABLES.BLOCK_TIME, &temp) - if temp != nil && (len(temp) > 0) { - for k, v := range temp { - syncMap.Store(k, v) - } - } - + // LEGACY: Deprecated + logs.Warning(`BadgerDB is deprecated for LoadBlockTime`) case DBSql: var temp []DBBlockTime DATABASE.Table(`db_block_times`). @@ -59,13 +53,8 @@ func LoadHistoricalPrice(chainID uint64, wg *sync.WaitGroup) { switch _dbType { case DBBadger: - temp := make(map[string]string) - ListFromBadgerDB(chainID, TABLES.HISTORICAL_PRICES, &temp) - if temp != nil && (len(temp) > 0) { - for k, v := range temp { - syncMap.Store(k, bigNumber.NewInt(0).SetString(v)) - } - } + // LEGACY: Deprecated + logs.Warning(`BadgerDB is deprecated for LoadHistoricalPrice`) case DBSql: var temp []DBHistoricalPrice DATABASE.Table(`db_historical_prices`). @@ -82,7 +71,9 @@ func LoadHistoricalPrice(chainID uint64, wg *sync.WaitGroup) { /************************************************************************************************** ** LoadNewVaultsFromRegistry will retrieve the all the vaults added to the registries from the -** configured DB and store them in the _newVaultsFromRegistrySyncMap for fast access during that +** local DB and store them in the _newVaultsFromRegistrySyncMap for fast access during that same +** execution. +** Use local DB to not screw up the DB with the same data over and over again. **************************************************************************************************/ func LoadNewVaultsFromRegistry(chainID uint64, wg *sync.WaitGroup) { if wg != nil { @@ -90,36 +81,28 @@ func LoadNewVaultsFromRegistry(chainID uint64, wg *sync.WaitGroup) { } syncMap := _newVaultsFromRegistrySyncMap[chainID] - switch _dbType { - case DBBadger: - // Not implemented - logs.Warning(`LoadNewVaultsFromRegistry not implemented for DBBadger. Skipping...`) - case DBSql: - var temp []DBNewVaultsFromRegistry + temp := make(map[string]DBNewVaultsFromRegistry) + ListFromBadgerDB(chainID, TABLES.VAULTS_FROM_REGISTRY_SYNC, &temp) - DATABASE.Table(`db_new_vaults_from_registries`). - Where("chain_id = ?", chainID). - FindInBatches(&temp, 10_000, func(tx *gorm.DB, batch int) error { - for _, vaultFromDB := range temp { - key := strconv.FormatUint(vaultFromDB.Block, 10) + "_" + addresses.ToAddress(vaultFromDB.RegistryAddress).Hex() + "_" + addresses.ToAddress(vaultFromDB.VaultsAddress).Hex() + "_" + addresses.ToAddress(vaultFromDB.TokenAddress).Hex() + "_" + vaultFromDB.APIVersion - vaultFromRegistry := models.TVaultsFromRegistry{ - Address: addresses.ToAddress(vaultFromDB.VaultsAddress), - RegistryAddress: addresses.ToAddress(vaultFromDB.RegistryAddress), - TokenAddress: addresses.ToAddress(vaultFromDB.TokenAddress), - BlockHash: common.HexToHash(vaultFromDB.BlockHash), - Type: vaultFromDB.Type, - APIVersion: vaultFromDB.APIVersion, - ChainID: vaultFromDB.ChainID, - BlockNumber: vaultFromDB.Block, - Activation: vaultFromDB.Activation, - ManagementFee: vaultFromDB.ManagementFee, - TxIndex: vaultFromDB.TxIndex, - LogIndex: vaultFromDB.LogIndex, - } - syncMap.Store(key, vaultFromRegistry) - } - return nil - }) + if temp != nil && (len(temp) > 0) { + for _, vaultFromDB := range temp { + key := strconv.FormatUint(vaultFromDB.Block, 10) + "_" + addresses.ToAddress(vaultFromDB.RegistryAddress).Hex() + "_" + addresses.ToAddress(vaultFromDB.VaultsAddress).Hex() + "_" + addresses.ToAddress(vaultFromDB.TokenAddress).Hex() + "_" + vaultFromDB.APIVersion + vaultFromRegistry := models.TVaultsFromRegistry{ + Address: addresses.ToAddress(vaultFromDB.VaultsAddress), + RegistryAddress: addresses.ToAddress(vaultFromDB.RegistryAddress), + TokenAddress: addresses.ToAddress(vaultFromDB.TokenAddress), + BlockHash: common.HexToHash(vaultFromDB.BlockHash), + Type: vaultFromDB.Type, + APIVersion: vaultFromDB.APIVersion, + ChainID: vaultFromDB.ChainID, + BlockNumber: vaultFromDB.Block, + Activation: vaultFromDB.Activation, + ManagementFee: vaultFromDB.ManagementFee, + TxIndex: vaultFromDB.TxIndex, + LogIndex: vaultFromDB.LogIndex, + } + syncMap.Store(key, vaultFromRegistry) + } } } @@ -135,13 +118,8 @@ func LoadERC20(chainID uint64, wg *sync.WaitGroup) { switch _dbType { case DBBadger: - temp := make(map[common.Address]models.TERC20Token) - ListFromBadgerDB(chainID, TABLES.TOKENS, &temp) - if temp != nil && (len(temp) > 0) { - for _, v := range temp { - syncMap.Store(v.Address, v) - } - } + // LEGACY: Deprecated + logs.Warning(`BadgerDB is deprecated for LoadERC20`) case DBSql: var temp []DBERC20 DATABASE.Table(`db_erc20`). @@ -183,13 +161,8 @@ func LoadVaults(chainID uint64, wg *sync.WaitGroup) { switch _dbType { case DBBadger: - temp := make(map[common.Address]models.TVault) - ListFromBadgerDB(chainID, TABLES.VAULTS, &temp) - if temp != nil && (len(temp) > 0) { - for _, v := range temp { - syncMap.Store(v.Address, v) - } - } + // LEGACY: Deprecated + logs.Warning(`BadgerDB is deprecated for LoadVaults`) case DBSql: var temp []DBVault DATABASE.Table(`db_vaults`). @@ -269,7 +242,8 @@ func LoadStrategies(chainID uint64, wg *sync.WaitGroup) { switch _dbType { case DBBadger: - // not implemented + // LEGACY: Deprecated + logs.Warning(`BadgerDB is deprecated for LoadStrategies`) case DBSql: var temp []DBStrategy DATABASE.Table(`db_strategies`). @@ -298,3 +272,31 @@ func LoadStrategies(chainID uint64, wg *sync.WaitGroup) { }) } } + +/************************************************************************************************** +** LoadSyncStrategiesAdded will try to retrieve all the sync for vaults/strategies for a given +** chain on the local DB. +**************************************************************************************************/ +func LoadSyncStrategiesAdded(chainID uint64) []DBStrategyAddedSync { + syncMap := make(map[string]DBStrategyAddedSync) + ListFromBadgerDB(chainID, TABLES.STRATEGIES_FROM_VAULT_SYNC, &syncMap) + arr := []DBStrategyAddedSync{} + for _, v := range syncMap { + arr = append(arr, v) + } + return arr +} + +/************************************************************************************************** +** LoadSyncRegistry will try to retrieve all the sync for registry/vault for a given chain on local +** DB. +**************************************************************************************************/ +func LoadSyncRegistry(chainID uint64) []DBRegistrySync { + syncMap := make(map[string]DBRegistrySync) + ListFromBadgerDB(chainID, TABLES.REGISTRY_SYNC, &syncMap) + arr := []DBRegistrySync{} + for _, v := range syncMap { + arr = append(arr, v) + } + return arr +} diff --git a/common/store/store.setter.go b/common/store/store.setter.go index fa3a676b0..bf3a8e559 100644 --- a/common/store/store.setter.go +++ b/common/store/store.setter.go @@ -14,7 +14,6 @@ import ( "github.com/yearn/ydaemon/common/logs" "github.com/yearn/ydaemon/internal/models" "golang.org/x/time/rate" - "gorm.io/gorm" "gorm.io/gorm/clause" ) @@ -35,13 +34,8 @@ func StoreBlockTime(chainID uint64, blockNumber uint64, blockTime uint64) { logs.Info(`Storing block time for chain ` + strconv.FormatUint(chainID, 10) + ` block ` + strconv.FormatUint(blockNumber, 10) + ` time ` + strconv.FormatUint(blockTime, 10)) switch _dbType { case DBBadger: - go OpenBadgerDB(chainID, TABLES.BLOCK_TIME).Update(func(txn *badger.Txn) error { - dataByte, err := json.Marshal(blockTime) - if err != nil { - return err - } - return txn.Set([]byte(strconv.FormatUint(blockNumber, 10)), dataByte) - }) + // LEGACY: Deprecated + logs.Warning(`BadgerDB is deprecated for StoreBlockTime`) case DBSql: go func() { DBbaseSchema := DBBaseSchema{ @@ -67,13 +61,8 @@ func StoreHistoricalPrice(chainID uint64, blockNumber uint64, tokenAddress commo switch _dbType { case DBBadger: - go OpenBadgerDB(chainID, TABLES.HISTORICAL_PRICES).Update(func(txn *badger.Txn) error { - dataByte, err := json.Marshal(price.String()) - if err != nil { - return err - } - return txn.Set([]byte(key), dataByte) - }) + // LEGACY: Deprecated + logs.Warning(`BadgerDB is deprecated for StoreHistoricalPrice`) case DBSql: go func() { DBbaseSchema := DBBaseSchema{ @@ -99,7 +88,8 @@ func StoreHistoricalPrice(chainID uint64, blockNumber uint64, tokenAddress commo func StoreManyHistoricalPrice(items []DBHistoricalPrice) { switch _dbType { case DBBadger: - // Not implemented + // LEGACY: Deprecated + logs.Warning(`BadgerDB is deprecated for StoreManyHistoricalPrice`) case DBSql: go func() { storeRateLimiter.Wait(context.Background()) @@ -123,40 +113,38 @@ func AppendInHistoricalMap(chainID uint64, blockNumber uint64, tokenAddress comm /************************************************************************************************** ** StoreNewVaultsFromRegistry will store a new vault in the _newVaultsFromRegistrySyncMap for fast -** access during that same execution, and will store it in the configured DB for future executions. +** access during that same execution, and will store it in the local DB for future executions. +** We are using the local DB because we don't want to trust the shared DB for this data. **************************************************************************************************/ func StoreNewVaultsFromRegistry(chainID uint64, vault models.TVaultsFromRegistry) { AppendInNewVaultsFromRegistry(chainID, vault) key := strconv.FormatUint(vault.BlockNumber, 10) + "_" + vault.RegistryAddress.Hex() + "_" + vault.Address.Hex() + "_" + vault.TokenAddress.Hex() + "_" + vault.APIVersion - switch _dbType { - case DBBadger: - // Not implemented - case DBSql: - go func() { - DBbaseSchema := DBBaseSchema{ - UUID: getUUID(key), - Block: vault.BlockNumber, - ChainID: chainID, - } - storeRateLimiter.Wait(context.Background()) - DATABASE. - Table(`db_new_vaults_from_registries`). - FirstOrCreate(&DBNewVaultsFromRegistry{ - DBbaseSchema, - vault.RegistryAddress.Hex(), - vault.Address.Hex(), - vault.TokenAddress.Hex(), - vault.BlockHash.Hex(), - vault.APIVersion, - vault.Activation, - vault.ManagementFee, - vault.TxIndex, - vault.LogIndex, - vault.Type, - }) - }() - } + go OpenBadgerDB(chainID, TABLES.VAULTS_FROM_REGISTRY_SYNC).Update(func(txn *badger.Txn) error { + DBbaseSchema := DBBaseSchema{ + UUID: getUUID(key), + Block: vault.BlockNumber, + ChainID: chainID, + } + data := &DBNewVaultsFromRegistry{ + DBbaseSchema, + vault.RegistryAddress.Hex(), + vault.Address.Hex(), + vault.TokenAddress.Hex(), + vault.BlockHash.Hex(), + vault.APIVersion, + vault.Activation, + vault.ManagementFee, + vault.TxIndex, + vault.LogIndex, + vault.Type, + } + dataByte, err := json.Marshal(data) + if err != nil { + return err + } + return txn.Set([]byte(key), dataByte) + }) } /************************************************************************************************** @@ -178,13 +166,8 @@ func StoreERC20(chainID uint64, token models.TERC20Token) { switch _dbType { case DBBadger: - go OpenBadgerDB(chainID, TABLES.TOKENS).Update(func(txn *badger.Txn) error { - dataByte, err := json.Marshal(token) - if err != nil { - return err - } - return txn.Set([]byte(key), dataByte) - }) + // LEGACY: Deprecated + logs.Warning(`BadgerDB is deprecated for StoreERC20`) case DBSql: go func() { allUnderlyingAsString := []string{} @@ -231,13 +214,8 @@ func StoreVault(chainID uint64, vault models.TVault) { switch _dbType { case DBBadger: - go OpenBadgerDB(chainID, TABLES.VAULTS).Update(func(txn *badger.Txn) error { - dataByte, err := json.Marshal(vault) - if err != nil { - return err - } - return txn.Set([]byte(key), dataByte) - }) + // LEGACY: Deprecated + logs.Warning(`BadgerDB is deprecated for StoreVault`) case DBSql: go func() { newItem := &DBVault{} @@ -291,7 +269,8 @@ func StoreStrategies(chainID uint64, strat models.TStrategyAdded) { switch _dbType { case DBBadger: - // Not implemented + // LEGACY: Deprecated + logs.Warning(`BadgerDB is deprecated for StoreStrategies`) case DBSql: go func() { newItem := &DBStrategy{} @@ -331,39 +310,69 @@ func AppendInStrategyMap(chainID uint64, strat models.TStrategyAdded) { ** for new vaults. **************************************************************************************************/ func StoreSyncRegistry(chainID uint64, registryAddess common.Address, end *uint64) { - switch _dbType { - case DBBadger: - // Not implemented - case DBSql: - go func() { - storeRateLimiter.Wait(context.Background()) - DATABASE.Table("db_registry_syncs"). - Where("chain_id = ? AND registry = ?", chainID, registryAddess.Hex()). - Where("block <= ?", end). - Assign(DBRegistrySync{Block: *end}). - FirstOrCreate(&DBRegistrySync{ - ChainID: chainID, - Registry: registryAddess.Hex(), - UUID: GetUUID(registryAddess.Hex() + strconv.FormatUint(chainID, 10)), - }) - }() - } + OpenBadgerDB(chainID, TABLES.REGISTRY_SYNC).Update(func(txn *badger.Txn) error { + if getter, err := txn.Get([]byte(registryAddess.Hex())); err == nil { + if previousValue, err := getter.ValueCopy(nil); err == nil { + previousSync := &DBRegistrySync{} + if err := json.Unmarshal(previousValue, previousSync); err != nil { + logs.Error(`StoreSyncRegistry: json.Unmarshal(previousValue, previousSync)`, err) + return err + } + if previousSync.Block > *end { + logs.Info(`StoreSyncRegistry: previousSync.Block > *end`, previousSync.Block, *end) + return nil + } + } + } + + data := &DBRegistrySync{ + ChainID: chainID, + Block: *end, + Registry: registryAddess.Hex(), + UUID: GetUUID(registryAddess.Hex() + strconv.FormatUint(chainID, 10)), + } + dataByte, err := json.Marshal(data) + if err != nil { + return err + } + return txn.Set([]byte(data.Registry), dataByte) + }) } /************************************************************************************************** ** StoreSyncStrategiesAdded will store the sync status indicating we went up to the block number ** to check for new strategies added. **************************************************************************************************/ -func StoreSyncStrategiesAdded(itemsToUpsert []DBStrategyAddedSync) { - switch _dbType { - case DBBadger: - // Not implemented - case DBSql: +func StoreSyncStrategiesAdded(chainID uint64, vaultAddress common.Address, end uint64) { + OpenBadgerDB( + chainID, + TABLES.STRATEGIES_FROM_VAULT_SYNC, + ).Update(func(txn *badger.Txn) error { + if getter, err := txn.Get([]byte(vaultAddress.Hex())); err == nil { + if previousValue, err := getter.ValueCopy(nil); err == nil { + previousSync := &DBRegistrySync{} + if err := json.Unmarshal(previousValue, previousSync); err != nil { + logs.Error(`StoreSyncStrategiesAdded: json.Unmarshal(previousValue, previousSync)`, err) + return err + } + if previousSync.Block > end { + logs.Info(`StoreSyncStrategiesAdded: previousSync.Block > end`, previousSync.Block, end) + return nil + } + } + } - DATABASE.Table("db_strategy_added_syncs"). - Clauses(clause.OnConflict{ - Columns: []clause.Column{{Name: "chain_id"}, {Name: "vault"}}, - DoUpdates: clause.Assignments(map[string]interface{}{"block": gorm.Expr("GREATEST(db_strategy_added_syncs.block, EXCLUDED.block)")}), - }).Create(&itemsToUpsert) - } + data := &DBStrategyAddedSync{ + ChainID: chainID, + Block: end, + Vault: vaultAddress.Hex(), + UUID: GetUUID(vaultAddress.Hex() + strconv.FormatUint(chainID, 10)), + } + dataByte, err := json.Marshal(data) + if err != nil { + logs.Error(`StoreSyncStrategiesAdded: json.Marshal(data)`, err) + return err + } + return txn.Set([]byte(data.Vault), dataByte) + }) } diff --git a/internal/events/filterNewVault.go b/internal/events/filterNewVault.go index 4924f3b4b..0c3d9ad5c 100644 --- a/internal/events/filterNewVault.go +++ b/internal/events/filterNewVault.go @@ -89,8 +89,8 @@ func filterNewVaults( client := ethereum.GetRPC(chainID) if registry.Version == 1 || registry.Version == 2 { - currentVault, _ := contracts.NewYregistryv2(registry.Address, client) //V1 and V2 share the same ABI - if log, err := currentVault.FilterNewVault(opts, nil, nil); err == nil { + currentRegistry, _ := contracts.NewYregistryv2(registry.Address, client) //V1 and V2 share the same ABI + if log, err := currentRegistry.FilterNewVault(opts, nil, nil); err == nil { for log.Next() { if log.Error() != nil { continue @@ -116,8 +116,8 @@ func filterNewVaults( logs.Error(`impossible to FilterNewVault for YRegistryV2 ` + registry.Address.Hex() + ` on chain ` + strconv.FormatUint(chainID, 10) + `: ` + err.Error()) } } else if registry.Version == 3 { - currentVault, _ := contracts.NewYRegistryV3(registry.Address, client) //V3 is not the same - if log, err := currentVault.FilterNewVault(opts, nil, nil); err == nil { + currentRegistry, _ := contracts.NewYRegistryV3Filterer(registry.Address, client) //V3 is not the same + if log, err := currentRegistry.FilterNewVault(opts, nil, nil); err == nil { for log.Next() { if log.Error() != nil { continue @@ -199,7 +199,7 @@ func HandleNewVaults( if _start == 0 { lastEvent := registriesLastBlockSync[registry.Address.Hex()] if lastEvent > 0 { - _start = lastEvent + 1 //Adding one to get the next event + _start = lastEvent - 1 } else { _start = registry.Block } @@ -296,7 +296,7 @@ func HandleNewStandardVaults( if start == 0 { lastEvent := registriesLastBlockSync[registry.Address.Hex()] if lastEvent > 0 { - start = lastEvent + 1 //Adding one to get the next event + start = lastEvent - 1 } else { start = registry.Activation } diff --git a/internal/events/filterStrategyAdded.go b/internal/events/filterStrategyAdded.go index 431086c16..c31d7e266 100644 --- a/internal/events/filterStrategyAdded.go +++ b/internal/events/filterStrategyAdded.go @@ -63,7 +63,7 @@ func filterStrategyAdded( if start == 0 { lastEvent := vaultsLastBlockSync[vault.Address] if lastEvent > 0 { - start = lastEvent + 1 //Adding one to get the next event + start = lastEvent - 1 } else { start = vault.Activation } @@ -206,7 +206,7 @@ func filterStrategiesMigrated( if start == 0 { lastEvent := vaultsLastBlockSync[vault.Address] if lastEvent > 0 { - start = lastEvent + 1 //Adding one to get the next event + start = lastEvent - 1 } else { start = vault.Activation } @@ -279,16 +279,12 @@ func HandleStrategyAdded( ** Our first action is to make sure we ignore the strategies we already have in our local ** storage, which we loaded from the database. **********************************************************************************************/ - var strategyAddedSync []store.DBStrategyAddedSync - store.DATABASE.Table("db_strategy_added_syncs"). - Where("chain_id = ?", chainID). - Find(&strategyAddedSync) - + strategyAddedSync := store.LoadSyncStrategiesAdded(chainID) vaultsLastBlockSync := map[common.Address]uint64{} for _, syncEvent := range strategyAddedSync { vaultsLastBlockSync[common.HexToAddress(syncEvent.Vault)] = syncEvent.Block } - allPreviouslyAddedStrategies, _ := store.ListAllStrategiess(chainID) + allPreviouslyAddedStrategies, _ := store.ListAllStrategies(chainID) /********************************************************************************************** ** We will then listen to all events related to the strategies added or migrated to the vaults. @@ -296,67 +292,37 @@ func HandleStrategyAdded( **********************************************************************************************/ asyncStrategiesForVaults := &sync.Map{} asyncStrategiesMigrated := &sync.Map{} - asyncVaultEnd := &sync.Map{} for _, v := range vaultsMap { wg := &sync.WaitGroup{} wg.Add(2) go func(v models.TVaultsFromRegistry) { defer wg.Done() - end := filterStrategyAdded( + filterStrategyAdded( v, vaultsLastBlockSync, start, end, asyncStrategiesForVaults, ) - - endArr, ok := asyncVaultEnd.Load(v.Address) - if !ok { - endArr = []uint64{} - } - endArr = append(endArr.([]uint64), *end) - asyncVaultEnd.Store(v.Address, endArr) }(v) go func(v models.TVaultsFromRegistry) { defer wg.Done() - end := filterStrategiesMigrated( + filterStrategiesMigrated( v, vaultsLastBlockSync, start, end, asyncStrategiesMigrated, ) - endArr, ok := asyncVaultEnd.Load(v.Address) - if !ok { - endArr = []uint64{} - } - endArr = append(endArr.([]uint64), *end) - asyncVaultEnd.Store(v.Address, endArr) }(v) wg.Wait() } - itemsToUpsert := []store.DBStrategyAddedSync{} for _, v := range vaultsMap { - endArr, ok := asyncVaultEnd.Load(v.Address) - if !ok { - endArr = []uint64{} + if end != nil { + store.StoreSyncStrategiesAdded(chainID, v.Address, *end) } - end := uint64(0) - for _, e := range endArr.([]uint64) { - if e > end { - end = e - } - } - itemsToUpsert = append(itemsToUpsert, store.DBStrategyAddedSync{ - ChainID: chainID, - Vault: v.Address.Hex(), - UUID: store.GetUUID(v.Address.Hex() + strconv.FormatUint(chainID, 10)), - Block: end, - }) - - go store.StoreSyncStrategiesAdded(itemsToUpsert) } /********************************************************************************************** @@ -440,6 +406,5 @@ func HandleStrategyAdded( allStrategiesList = append(allStrategiesList, strat) } - logs.Info(`DONE HERE`) return allStrategiesList } diff --git a/internal/registries/accessor.go b/internal/registries/accessor.go index a76e899a8..847771ee6 100644 --- a/internal/registries/accessor.go +++ b/internal/registries/accessor.go @@ -17,11 +17,7 @@ func RegisterAllVaults( ** Our first action is to make sure we ignore the vaults we already have in our local storage, ** which we loaded from the database. **********************************************************************************************/ - var registerSync []store.DBRegistrySync - store.DATABASE.Table("db_registry_syncs"). - Where("db_registry_syncs.chain_id = ?", chainID). - Find(®isterSync) - + registerSync := store.LoadSyncRegistry(chainID) registriesWithLastEvent := map[string]uint64{} for _, registry := range registerSync { registriesWithLastEvent[registry.Registry] = registry.Block diff --git a/internal/strategies/risk.framework.go b/internal/strategies/risk.framework.go index 5e0f7bae9..d33708a75 100755 --- a/internal/strategies/risk.framework.go +++ b/internal/strategies/risk.framework.go @@ -372,8 +372,7 @@ func InitRiskScore(chainID uint64) { Risk: getTVLImpact(bigNumber.NewFloat(0).Mul(amount, price)), TVL: tvl, } - - logs.Pretty(`[InitRiskScore]`, pool.Token.Hex(), stakingData[chainID][pool.Token.Hex()].Risk, bigNumber.NewFloat(0).Mul(amount, price).String(), amount, price) + // logs.Pretty(`[InitRiskScore]`, pool.Token.Hex(), stakingData[chainID][pool.Token.Hex()].Risk, bigNumber.NewFloat(0).Mul(amount, price).String(), amount, price) } } computeRiskGroupAllocation(chainID)