diff --git a/scanner/providers/gitcoin/gitcoin_provider.go b/scanner/providers/gitcoin/gitcoin_provider.go index 25a2fd9f..381fac8c 100644 --- a/scanner/providers/gitcoin/gitcoin_provider.go +++ b/scanner/providers/gitcoin/gitcoin_provider.go @@ -73,7 +73,7 @@ type GitcoinPassportConf struct { // Init initializes the Gitcoin Passport provider with the given config. If the // config is not of type GitcoinPassportConf, or the API endpoint is missing, it // returns an error. If the cooldown is not set, it defaults to 6 hours. -func (g *GitcoinPassport) Init(globalCtx context.Context, iconf any) error { +func (g *GitcoinPassport) Init(_ context.Context, iconf any) error { conf, ok := iconf.(GitcoinPassportConf) if !ok { return fmt.Errorf("invalid config type") @@ -90,8 +90,10 @@ func (g *GitcoinPassport) Init(globalCtx context.Context, iconf any) error { g.apiEndpoint = conf.APIEndpoint g.cooldown = conf.Cooldown g.db = conf.DB + // create the context and cancel function, skipping the global context + // provided to avoid cancelling the download process + g.ctx, g.cancel = context.WithCancel(context.Background()) // init download variables - g.ctx, g.cancel = context.WithCancel(globalCtx) g.scoresChan = make(chan *GitcoinScore) g.waiter = new(sync.WaitGroup) g.synced = atomic.Bool{} @@ -138,16 +140,18 @@ func (g *GitcoinPassport) SetLastBalances(_ context.Context, _ []byte, return nil } -func (g *GitcoinPassport) HoldersBalances(_ context.Context, stamp []byte, _ uint64) ( +func (g *GitcoinPassport) HoldersBalances(ctx context.Context, stamp []byte, _ uint64) ( map[common.Address]*big.Int, uint64, uint64, bool, *big.Int, error, ) { + internalCtx, cancel := context.WithCancel(ctx) + defer cancel() // get the current scores from the db, handle the case when the stamp is // empty and when it is not to get the scores from the db synced := g.isSynced(true) totalSupply := big.NewInt(0) currentScores := make(map[common.Address]*big.Int) if len(stamp) > 0 { - dbStampScores, err := g.db.QueriesRW.GetStampScores(g.ctx, string(stamp)) + dbStampScores, err := g.db.QueriesRW.GetStampScores(internalCtx, string(stamp)) if err != nil { return nil, 0, 0, false, big.NewInt(0), fmt.Errorf("error getting stamp scores: %w", err) } @@ -161,7 +165,7 @@ func (g *GitcoinPassport) HoldersBalances(_ context.Context, stamp []byte, _ uin totalSupply.Add(totalSupply, score) } } else { - dbScores, err := g.db.QueriesRW.GetScores(g.ctx) + dbScores, err := g.db.QueriesRW.GetScores(internalCtx) if err != nil { return nil, 0, 0, false, big.NewInt(0), fmt.Errorf("error getting scores: %w", err) } diff --git a/scanner/providers/web3/web3_iter.go b/scanner/providers/web3/web3_iter.go index 94d77ac2..d368d7d5 100644 --- a/scanner/providers/web3/web3_iter.go +++ b/scanner/providers/web3/web3_iter.go @@ -52,6 +52,9 @@ func (w3pp *Web3Iterator) Add(endpoint ...*Web3Endpoint) { // available endpoints, it will reset the disabled endpoints and return the // first available endpoint. func (w3pp *Web3Iterator) Next() (*Web3Endpoint, error) { + if w3pp == nil { + return nil, fmt.Errorf("nil Web3Iterator") + } w3pp.mtx.Lock() defer w3pp.mtx.Unlock() l := len(w3pp.available) diff --git a/scanner/providers/web3/web3_pool.go b/scanner/providers/web3/web3_pool.go index afdc060a..94b38d04 100644 --- a/scanner/providers/web3/web3_pool.go +++ b/scanner/providers/web3/web3_pool.go @@ -111,7 +111,10 @@ func (nm *Web3Pool) DelEndoint(uri string) { // provided. It returns the first available endpoint. If no available endpoint // is found, returns an error. func (nm *Web3Pool) Endpoint(chainID uint64) (*Web3Endpoint, error) { - return nm.endpoints[chainID].Next() + if endpoints, ok := nm.endpoints[chainID]; ok { + return endpoints.Next() + } + return nil, fmt.Errorf("no endpoint found for chainID %d", chainID) } // DisableEndpoint method sets the available flag to false for the URI provided