Add tokenlist-extended validation (#17864)

This commit is contained in:
daniel 2022-02-03 23:36:45 +03:00 committed by GitHub
parent d926569f68
commit 143d32cf5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 20 deletions

View File

@ -17,7 +17,8 @@ var (
regexChainInfoFile = regexp.MustCompile(`./blockchains/(\w+[\-]\w+|\w+)/info/info.json$`) regexChainInfoFile = regexp.MustCompile(`./blockchains/(\w+[\-]\w+|\w+)/info/info.json$`)
regexChainLogoFile = regexp.MustCompile(`./blockchains/(\w+[\-]\w+|\w+)/info/logo.png$`) regexChainLogoFile = regexp.MustCompile(`./blockchains/(\w+[\-]\w+|\w+)/info/logo.png$`)
regexTokenListFile = regexp.MustCompile(`./blockchains/(\w+[\-]\w+|\w+)/tokenlist.json$`) regexTokenListFile = regexp.MustCompile(`./blockchains/(\w+[\-]\w+|\w+)/tokenlist.json$`)
regexTokenListExtendedFile = regexp.MustCompile(`./blockchains/(\w+[\-]\w+|\w+)/tokenlist-extended.json$`)
regexValidatorsAssetLogo = regexp.MustCompile( regexValidatorsAssetLogo = regexp.MustCompile(
`./blockchains/(\w+[\-]\w+|\w+)/validators/assets/(\w+[\-]\w+|\w+)/logo.png$`) `./blockchains/(\w+[\-]\w+|\w+)/validators/assets/(\w+[\-]\w+|\w+)/logo.png$`)
@ -50,7 +51,8 @@ var regexes = map[string]*regexp.Regexp{
TypeChainInfoFile: regexChainInfoFile, TypeChainInfoFile: regexChainInfoFile,
TypeChainLogoFile: regexChainLogoFile, TypeChainLogoFile: regexChainLogoFile,
TypeTokenListFile: regexTokenListFile, TypeTokenListFile: regexTokenListFile,
TypeTokenListExtendedFile: regexTokenListExtendedFile,
TypeValidatorsListFile: regexValidatorsList, TypeValidatorsListFile: regexValidatorsList,
TypeValidatorsLogoFile: regexValidatorsAssetLogo, TypeValidatorsLogoFile: regexValidatorsAssetLogo,

View File

@ -12,14 +12,15 @@ const (
TypeValidatorsAssetFolder = "validators_asset_folder" TypeValidatorsAssetFolder = "validators_asset_folder"
TypeValidatorsAssetsFolder = "validators_assets_folder" TypeValidatorsAssetsFolder = "validators_assets_folder"
TypeAssetInfoFile = "asset_info_file" TypeAssetInfoFile = "asset_info_file"
TypeAssetLogoFile = "asset_logo_file" TypeAssetLogoFile = "asset_logo_file"
TypeChainInfoFile = "chain_info_file" TypeChainInfoFile = "chain_info_file"
TypeChainLogoFile = "chain_logo_file" TypeChainLogoFile = "chain_logo_file"
TypeTokenListFile = "chain_tokenlist_file" TypeTokenListFile = "chain_tokenlist_file"
TypeDappsLogoFile = "dapps_logo_file" TypeTokenListExtendedFile = "chain_tokenlist_extended_file"
TypeValidatorsListFile = "validators_list_file" TypeDappsLogoFile = "dapps_logo_file"
TypeValidatorsLogoFile = "validators_logo_file" TypeValidatorsListFile = "validators_list_file"
TypeValidatorsLogoFile = "validators_logo_file"
TypeUnknown = "unknown_file" TypeUnknown = "unknown_file"
) )

View File

@ -65,6 +65,11 @@ func (s *Service) GetValidator(f *file.AssetFile) []Validator {
jsonValidator, jsonValidator,
{Name: "Tokenlist file is valid", Run: s.ValidateTokenListFile}, {Name: "Tokenlist file is valid", Run: s.ValidateTokenListFile},
} }
case file.TypeTokenListExtendedFile:
return []Validator{
jsonValidator,
{Name: "Tokenlist Extended file is valid", Run: s.ValidateTokenListExtendedFile},
}
case file.TypeValidatorsListFile: case file.TypeValidatorsListFile:
return []Validator{ return []Validator{
jsonValidator, jsonValidator,

View File

@ -262,34 +262,47 @@ func isStackingChain(c coin.Coin) bool {
} }
func (s *Service) ValidateTokenListFile(f *file.AssetFile) error { func (s *Service) ValidateTokenListFile(f *file.AssetFile) error {
var tokenList tokenlist.Model tokenListPath := f.Path()
err := filelib.ReadJSONFile(f.Path(), &tokenList) tokenListExtendedPath := path.GetTokenListPath(f.Chain().Handle, path.TokenlistExtended)
return validateTokenList(tokenListPath, tokenListExtendedPath, f.Chain())
}
func (s *Service) ValidateTokenListExtendedFile(f *file.AssetFile) error {
tokenListPathExtended := f.Path()
tokenListPath := path.GetTokenListPath(f.Chain().Handle, path.TokenlistDefault)
return validateTokenList(tokenListPathExtended, tokenListPath, f.Chain())
}
func validateTokenList(path1, path2 string, chain1 coin.Coin) error {
var tokenList1 tokenlist.Model
err := filelib.ReadJSONFile(path1, &tokenList1)
if err != nil { if err != nil {
return err return err
} }
tokenListExtendedPath := path.GetTokenListPath(f.Chain().Handle, path.TokenlistExtended) if filelib.Exists(path2) {
if filelib.Exists(tokenListExtendedPath) { var tokenList2 tokenlist.Model
var tokenListExtended tokenlist.Model err = filelib.ReadJSONFile(path2, &tokenList2)
err = filelib.ReadJSONFile(tokenListExtendedPath, &tokenListExtended)
if err != nil { if err != nil {
return err return err
} }
tokensMap := make(map[string]bool) tokensMap := make(map[string]bool)
for _, token := range tokenListExtended.Tokens { for _, token := range tokenList2.Tokens {
tokensMap[token.Asset] = true tokensMap[token.Asset] = true
} }
for _, token := range tokenList.Tokens { for _, token := range tokenList1.Tokens {
if _, exists := tokensMap[token.Asset]; exists { if _, exists := tokensMap[token.Asset]; exists {
return fmt.Errorf("duplicate asset: %s from %s, already exist in %s", return fmt.Errorf("duplicate asset: %s from %s, already exist in %s",
token.Asset, f.Path(), tokenListExtendedPath) token.Asset, path1, path2)
} }
} }
} }
err = tokenlist.ValidateTokenList(tokenList, f.Chain(), f.Path()) err = tokenlist.ValidateTokenList(tokenList1, chain1, path1)
if err != nil { if err != nil {
return err return err
} }