Skip to content

Commit

Permalink
Add validators (#33)
Browse files Browse the repository at this point in the history
* Add validator list fixer

* Add ETH asset's checsum fixer

* Fix renaming logic
  • Loading branch information
unanoc authored Dec 13, 2021
1 parent 4ccd693 commit 383c0ba
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 47 deletions.
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ require (
github.com/trustwallet/go-primitives v0.0.17
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

require (
github.com/deckarep/golang-set v1.7.1 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
Expand All @@ -22,6 +28,7 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.8.1 // indirect
github.com/stretchr/testify v1.7.0
github.com/subosito/gotenv v1.2.0 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/sys v0.0.0-20210816183151-1e6c022a8912 // indirect
Expand Down
16 changes: 16 additions & 0 deletions pkg/file/cache.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package file

import (
"path/filepath"
"strings"
"sync"
)

Expand Down Expand Up @@ -30,6 +32,20 @@ func (f *Service) GetAssetFile(path string) (*AssetFile, error) {
return f.getFile(path)
}

func (f *Service) UpdateFile(file *AssetFile, newFileBaseName string) {
f.mu.RLock()
defer f.mu.RUnlock()

oldFileBaseName := filepath.Base(file.Info.Path())

for path := range f.cache {
if strings.Contains(path, oldFileBaseName) {
newPath := strings.ReplaceAll(path, oldFileBaseName, newFileBaseName)
f.cache[path] = NewAssetFile(newPath)
}
}
}

func (f *Service) getFile(path string) (*AssetFile, error) {
if file, exists := f.cache[path]; exists {
err := file.Open()
Expand Down
22 changes: 22 additions & 0 deletions pkg/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,25 @@ func ReadJSONFile(path string, result interface{}) error {

return nil
}

func FormatJSONFile(path string) error {
jsonFile, err := os.Open(path)
if err != nil {
return err
}
defer jsonFile.Close()

data, err := ioutil.ReadAll(jsonFile)
if err != nil {
return err
}

var prettyJSON bytes.Buffer

err = json.Indent(&prettyJSON, data, "", " ")
if err != nil {
return err
}

return ioutil.WriteFile(path, prettyJSON.Bytes(), 0600)
}
20 changes: 3 additions & 17 deletions pkg/validation/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,10 @@ import (
var regexTRC10 = regexp.MustCompile(`^\d+$`)

func ValidateAssetAddress(chain coin.Coin, address string) error {
switch chain.ID {
case
coin.ETHEREUM,
coin.CLASSIC,
coin.POA,
coin.TOMOCHAIN,
coin.GOCHAIN,
coin.WANCHAIN,
coin.THUNDERTOKEN,
coin.SMARTCHAIN,
coin.POLYGON,
coin.OPTIMISM,
coin.XDAI,
coin.AVALANCHEC,
coin.ARBITRUM,
coin.FANTOM:
switch {
case coin.IsEVM(chain.ID):
return ValidateETHForkAddress(chain, address)
case coin.TRON:
case chain.ID == coin.TRON:
return ValidateTronAddress(address)
}

Expand Down
49 changes: 33 additions & 16 deletions src/core/fixers.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,49 @@
package core

import (
"bytes"
"encoding/json"
"io/ioutil"
"fmt"
"os"
"path/filepath"

"github.com/trustwallet/assets-go-libs/pkg"
"github.com/trustwallet/assets-go-libs/pkg/file"
"github.com/trustwallet/assets-go-libs/pkg/validation"
"github.com/trustwallet/go-primitives/address"
"github.com/trustwallet/go-primitives/coin"

log "github.com/sirupsen/logrus"
)

func (s *Service) FixInfoJSON(file *file.AssetFile) error {
jsonFile, err := os.Open(file.Info.Path())
if err != nil {
return err
}
defer jsonFile.Close()
func (s *Service) FixJSON(file *file.AssetFile) error {
return pkg.FormatJSONFile(file.Info.Path())
}

data, err := ioutil.ReadAll(jsonFile)
if err != nil {
return err
func (s *Service) FixETHAddressChecksum(file *file.AssetFile) error {
if !coin.IsEVM(file.Info.Chain().ID) {
return nil
}

var prettyJSON bytes.Buffer
assetDir := filepath.Base(file.Info.Path())

err = json.Indent(&prettyJSON, data, "", " ")
err := validation.ValidateETHForkAddress(file.Info.Chain(), assetDir)
if err != nil {
return err
checksum, e := address.EIP55Checksum(assetDir)
if e != nil {
return fmt.Errorf("failed to get checksum: %s", e)
}

newName := fmt.Sprintf("blockchains/%s/assets/%s", file.Info.Chain().Handle, checksum)

if e = os.Rename(file.Info.Path(), newName); e != nil {
return fmt.Errorf("failed to rename dir: %s", e)
}

s.fileService.UpdateFile(file, checksum)

log.WithField("from", assetDir).
WithField("to", checksum).
Debug("Renamed asset")
}

return ioutil.WriteFile(file.Info.Path(), prettyJSON.Bytes(), 0600)
return nil
}
16 changes: 10 additions & 6 deletions src/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ type Service struct {
}

func NewService(fileProvider *file.Service) *Service {
return &Service{
fileService: fileProvider,
}
return &Service{fileService: fileProvider}
}

// nolint:funlen
Expand Down Expand Up @@ -94,10 +92,16 @@ func (s *Service) GetFixer(f *file.AssetFile) *Fixer {
fileType := f.Info.Type()

switch fileType {
case file.TypeChainInfoFile, file.TypeAssetInfoFile:
case file.TypeChainInfoFile, file.TypeAssetInfoFile, file.TypeValidatorsListFile:
return &Fixer{
Name: "Formatting all info.json files",
Run: s.FixJSON,
FileType: fileType,
}
case file.TypeAssetFolder:
return &Fixer{
Name: "Formatting info.json files",
Run: s.FixInfoJSON,
Name: "Checking address checksum for EVM asset's info.json files",
Run: s.FixETHAddressChecksum,
FileType: fileType,
}
}
Expand Down
13 changes: 5 additions & 8 deletions src/processor/service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package processor

import (
"fmt"

log "github.com/sirupsen/logrus"

"github.com/trustwallet/assets-go-libs/pkg/file"
Expand All @@ -24,13 +26,13 @@ func (s *Service) RunJob(paths []string, job func(*file.AssetFile)) error {
for _, path := range paths {
f, err := s.fileService.GetAssetFile(path)
if err != nil {
return err
return fmt.Errorf("failed to get asset file: %s", err)
}

job(f)

if err = f.Close(); err != nil {
return err
return fmt.Errorf("failed to close asset file: %s", err)
}
}

Expand All @@ -41,9 +43,8 @@ func (s *Service) Check(f *file.AssetFile) {
validator := s.coreService.GetValidator(f)

if validator != nil {
log.WithField("name", validator.Name).Debug("Running validator")

if err := validator.Run(f); err != nil {
// TODO: somehow return an error from Check if there are any errors.
HandleError(err, f.Info, validator.Name)
}
}
Expand All @@ -53,8 +54,6 @@ func (s *Service) Fix(f *file.AssetFile) {
fixer := s.coreService.GetFixer(f)

if fixer != nil {
log.WithField("name", fixer.Name).Debug("Running fixer")

if err := fixer.Run(f); err != nil {
HandleError(err, f.Info, fixer.Name)
}
Expand All @@ -65,8 +64,6 @@ func (s *Service) RunUpdateAuto() error {
updaters := s.coreService.GetUpdatersAuto()

for _, updater := range updaters {
log.WithField("name", updater.Name).Debug("Running updater")

err := updater.Run()
if err != nil {
log.WithError(err).Error()
Expand Down

0 comments on commit 383c0ba

Please sign in to comment.