Skip to content

Commit

Permalink
creating isolated context for gitcoin passport, checking nil over web…
Browse files Browse the repository at this point in the history
…3iteraror pointer to prevent panics
  • Loading branch information
lucasmenendez committed Apr 17, 2024
1 parent dfb413f commit 03475bd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
14 changes: 9 additions & 5 deletions scanner/providers/gitcoin/gitcoin_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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{}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
3 changes: 3 additions & 0 deletions scanner/providers/web3/web3_iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion scanner/providers/web3/web3_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 03475bd

Please sign in to comment.