From 143d32cf5bceb76349910dd3c7305d1992c50f8f Mon Sep 17 00:00:00 2001 From: daniel <24758309+unanoc@users.noreply.github.com> Date: Thu, 3 Feb 2022 23:36:45 +0300 Subject: [PATCH] Add tokenlist-extended validation (#17864) --- internal/file/path.go | 6 ++++-- internal/file/types.go | 17 ++++++++-------- internal/processor/service.go | 5 +++++ internal/processor/validators.go | 33 ++++++++++++++++++++++---------- 4 files changed, 41 insertions(+), 20 deletions(-) diff --git a/internal/file/path.go b/internal/file/path.go index 6efcf02d6..c8d5d2883 100644 --- a/internal/file/path.go +++ b/internal/file/path.go @@ -17,7 +17,8 @@ var ( regexChainInfoFile = regexp.MustCompile(`./blockchains/(\w+[\-]\w+|\w+)/info/info.json$`) 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( `./blockchains/(\w+[\-]\w+|\w+)/validators/assets/(\w+[\-]\w+|\w+)/logo.png$`) @@ -50,7 +51,8 @@ var regexes = map[string]*regexp.Regexp{ TypeChainInfoFile: regexChainInfoFile, TypeChainLogoFile: regexChainLogoFile, - TypeTokenListFile: regexTokenListFile, + TypeTokenListFile: regexTokenListFile, + TypeTokenListExtendedFile: regexTokenListExtendedFile, TypeValidatorsListFile: regexValidatorsList, TypeValidatorsLogoFile: regexValidatorsAssetLogo, diff --git a/internal/file/types.go b/internal/file/types.go index e57c3cec7..a606ac2fb 100644 --- a/internal/file/types.go +++ b/internal/file/types.go @@ -12,14 +12,15 @@ const ( TypeValidatorsAssetFolder = "validators_asset_folder" TypeValidatorsAssetsFolder = "validators_assets_folder" - TypeAssetInfoFile = "asset_info_file" - TypeAssetLogoFile = "asset_logo_file" - TypeChainInfoFile = "chain_info_file" - TypeChainLogoFile = "chain_logo_file" - TypeTokenListFile = "chain_tokenlist_file" - TypeDappsLogoFile = "dapps_logo_file" - TypeValidatorsListFile = "validators_list_file" - TypeValidatorsLogoFile = "validators_logo_file" + TypeAssetInfoFile = "asset_info_file" + TypeAssetLogoFile = "asset_logo_file" + TypeChainInfoFile = "chain_info_file" + TypeChainLogoFile = "chain_logo_file" + TypeTokenListFile = "chain_tokenlist_file" + TypeTokenListExtendedFile = "chain_tokenlist_extended_file" + TypeDappsLogoFile = "dapps_logo_file" + TypeValidatorsListFile = "validators_list_file" + TypeValidatorsLogoFile = "validators_logo_file" TypeUnknown = "unknown_file" ) diff --git a/internal/processor/service.go b/internal/processor/service.go index 49468dc0a..ef36ad1f9 100644 --- a/internal/processor/service.go +++ b/internal/processor/service.go @@ -65,6 +65,11 @@ func (s *Service) GetValidator(f *file.AssetFile) []Validator { jsonValidator, {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: return []Validator{ jsonValidator, diff --git a/internal/processor/validators.go b/internal/processor/validators.go index 65eb8e1f5..73db0f8b8 100644 --- a/internal/processor/validators.go +++ b/internal/processor/validators.go @@ -262,34 +262,47 @@ func isStackingChain(c coin.Coin) bool { } func (s *Service) ValidateTokenListFile(f *file.AssetFile) error { - var tokenList tokenlist.Model - err := filelib.ReadJSONFile(f.Path(), &tokenList) + tokenListPath := f.Path() + 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 { return err } - tokenListExtendedPath := path.GetTokenListPath(f.Chain().Handle, path.TokenlistExtended) - if filelib.Exists(tokenListExtendedPath) { - var tokenListExtended tokenlist.Model - err = filelib.ReadJSONFile(tokenListExtendedPath, &tokenListExtended) + if filelib.Exists(path2) { + var tokenList2 tokenlist.Model + err = filelib.ReadJSONFile(path2, &tokenList2) if err != nil { return err } tokensMap := make(map[string]bool) - for _, token := range tokenListExtended.Tokens { + for _, token := range tokenList2.Tokens { tokensMap[token.Asset] = true } - for _, token := range tokenList.Tokens { + for _, token := range tokenList1.Tokens { if _, exists := tokensMap[token.Asset]; exists { 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 { return err }