Skip to content

Commit

Permalink
Merge pull request #53 from MinaFoundation/dont-crash-when-whitelist-…
Browse files Browse the repository at this point in the history
…unavailable

PM-1779 Don`t crash when whitelist unavailable
  • Loading branch information
piotr-iohk authored Jun 18, 2024
2 parents 9a2f2fd + 2058af2 commit eb73c06
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ If the `CONFIG_FILE` environment variable is not set, the program will fall back
- `CONFIG_GSHEET_ID` - Set this to your Google Sheet ID with the keys to whitelist.
- `DELEGATION_WHITELIST_LIST` - Set this to your delegation whitelist sheet title where the whitelist keys are.
- `DELEGATION_WHITELIST_COLUMN` - Set this to your delegation whitelist sheet column where the whitelist keys are.
- Or disable whitelisting alltogether by setting `DELEGATION_WHITELIST_DISABLED=1`. The previous four env variables are then ignored.
- `DELEGATION_WHITELIST_REFRESH_INTERVAL` - Whitelist refresh interval in minutes. If not set default value `10` is used.
- Or disable whitelisting alltogether by setting `DELEGATION_WHITELIST_DISABLED=1`. The previous env variables are then ignored.

3. **AWS S3 Configuration**:
- `AWS_ACCOUNT_ID` - Your AWS Account ID.
Expand Down
20 changes: 14 additions & 6 deletions src/cmd/delegation_backend/main_bpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,30 @@ func main() {
// Sheets service and whitelist loop
app.WhitelistDisabled = appCfg.DelegationWhitelistDisabled
if app.WhitelistDisabled {
log.Infof("delegation whitelist is disabled")
log.Infof("Delegation whitelist is disabled")
} else {
log.Infof("delegation whitelist is enabled")
sheetsService, err2 := sheets.NewService(ctx, option.WithScopes(sheets.SpreadsheetsReadonlyScope))
if err2 != nil {
log.Fatalf("Error creating Sheets service: %v", err2)
}
initWl := RetrieveWhitelist(sheetsService, log, appCfg)
initWl, err := RetrieveWhitelist(sheetsService, log, appCfg, 1)
if err != nil {
log.Fatalf("Failed to initialize whitelist: %v", err)
}
wlMvar := new(WhitelistMVar)
wlMvar.Replace(&initWl)
app.Whitelist = wlMvar
log.Infof("Delegation whitelist is enabled")
go func() {
for {
wl := RetrieveWhitelist(sheetsService, log, appCfg)
wlMvar.Replace(&wl)
time.Sleep(WHITELIST_REFRESH_INTERVAL)
time.Sleep(SetWhitelistRefreshInterval(log))
wl, err := RetrieveWhitelist(sheetsService, log, appCfg, 10)
if err != nil {
log.Errorf("Failed to refresh delegation whitelist, using previous one, error: %v", err)
} else {
wlMvar.Replace(&wl)
log.Infof("Delegation whitelist refreshed, number of BPs: %v", len(wl))
}
}
}()
}
Expand Down
19 changes: 19 additions & 0 deletions src/delegation_backend/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ func NetworkId(networkName string) uint8 {
return 0
}

func SetWhitelistRefreshInterval(log logging.StandardLogger) time.Duration {
var defaultValue time.Duration = time.Duration(10) * time.Minute
var whitelistRefreshInterval time.Duration

envVarValue, exists := os.LookupEnv("DELEGATION_WHITELIST_REFRESH_INTERVAL")
if exists {
minutes, err := strconv.Atoi(envVarValue)
if err != nil {
log.Warnf("Error parsing DELEGATION_WHITELIST_REFRESH_INTERVAL, falling back to default value: %v, error: %v", defaultValue, err)
whitelistRefreshInterval = defaultValue
}
whitelistRefreshInterval = time.Duration(minutes) * time.Minute
} else {
whitelistRefreshInterval = defaultValue
}
log.Infof("Delegation whitelist refresh interval: %v", whitelistRefreshInterval)
return whitelistRefreshInterval
}

func SetRequestsPerPkHourly(log logging.StandardLogger) int {
var defaultValue = 120
var requestsPerPkHourly int
Expand Down
6 changes: 3 additions & 3 deletions src/delegation_backend/sheets.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func processRows(rows [][](interface{})) Whitelist {
// Retrieve data from delegation program spreadsheet
// and extract public keys out of the column containing
// public keys of program participants.
func RetrieveWhitelist(service *sheets.Service, log *logging.ZapEventLogger, appCfg AppConfig) Whitelist {
func RetrieveWhitelist(service *sheets.Service, log *logging.ZapEventLogger, appCfg AppConfig, retries int) (Whitelist, error) {
var resp *sheets.ValueRange
var err error

Expand All @@ -41,11 +41,11 @@ func RetrieveWhitelist(service *sheets.Service, log *logging.ZapEventLogger, app
}
return nil
}
retries := 10
err = ExponentialBackoff(operation, retries, initialBackoff)
if err != nil {
log.Errorf("Unable to retrieve data from sheet after %v retries: %v", retries, err)
return nil, err
}

return processRows(resp.Values)
return processRows(resp.Values), nil
}

0 comments on commit eb73c06

Please sign in to comment.