[#936] ir: Do not require number of Alphabet contracts to be set

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-10-20 16:21:40 +03:00 committed by Alex Vanin
parent b492201a84
commit 6cab1635d4
2 changed files with 17 additions and 2 deletions

View file

@ -64,8 +64,6 @@ func defaultConfiguration(cfg *viper.Viper) {
cfg.SetDefault("contracts.proxy", "") cfg.SetDefault("contracts.proxy", "")
cfg.SetDefault("contracts.processing", "") cfg.SetDefault("contracts.processing", "")
cfg.SetDefault("contracts.reputation", "") cfg.SetDefault("contracts.reputation", "")
// alphabet contracts
cfg.SetDefault("contracts.alphabet.amount", 7)
cfg.SetDefault("timers.epoch", "0") cfg.SetDefault("timers.epoch", "0")
cfg.SetDefault("timers.emit", "0") cfg.SetDefault("timers.emit", "0")

View file

@ -1,6 +1,7 @@
package innerring package innerring
import ( import (
"errors"
"fmt" "fmt"
"strings" "strings"
@ -87,18 +88,34 @@ func parseAlphabetContracts(cfg *viper.Viper, morph *client.Client) (alphabetCon
return nil, fmt.Errorf("amount of alphabet contracts overflows glagolitsa %d > %d", num, lastLetterNum) return nil, fmt.Errorf("amount of alphabet contracts overflows glagolitsa %d > %d", num, lastLetterNum)
} }
thresholdIsSet := num != 0
if !thresholdIsSet {
// try to read maximum alphabet contracts
// if threshold has not been set manually
num = lastLetterNum
}
for letter := az; letter < num; letter++ { for letter := az; letter < num; letter++ {
contractHash, err := parseContract(cfg, morph, contractHash, err := parseContract(cfg, morph,
"contracts.alphabet."+letter.String(), "contracts.alphabet."+letter.String(),
client.NNSAlphabetContractName(int(letter)), client.NNSAlphabetContractName(int(letter)),
) )
if err != nil { if err != nil {
if errors.Is(err, client.ErrNNSRecordNotFound) {
break
}
return nil, fmt.Errorf("invalid alphabet %s contract: %w", letter, err) return nil, fmt.Errorf("invalid alphabet %s contract: %w", letter, err)
} }
alpha.set(letter, contractHash) alpha.set(letter, contractHash)
} }
if thresholdIsSet && len(alpha) != int(num) {
return nil, fmt.Errorf("could not read all contracts: required %d, read %d", num, len(alpha))
}
return alpha, nil return alpha, nil
} }