Skip to content

Commit

Permalink
feat: isolate subscriptions aliases (#63)
Browse files Browse the repository at this point in the history
* feat: isolate subscription aliases

* chore: refactor populate validator moniker

* chore: fixed linting

* chore: refactor FS testing

* chore: added AliasManager tests

* chore: add Makefile cmd for test coverage
  • Loading branch information
freak12techno authored Mar 1, 2024
1 parent 141d7d2 commit 76aaa0e
Show file tree
Hide file tree
Showing 50 changed files with 895 additions and 301 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ lint:

test:
go test -coverpkg=./... -coverprofile cover.out -v ./...

coverage:
go tool cover -html=cover.out
6 changes: 4 additions & 2 deletions assets/fs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package assets

import "embed"
import (
"embed"
)

//go:embed *
var AssetsFS embed.FS
var EmbedFS embed.FS
3 changes: 3 additions & 0 deletions assets/valid-aliases.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[subscription]
[subscription.chain]
wallet = "alias"
14 changes: 10 additions & 4 deletions cmd/cosmos-transactions-bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"main/pkg"
configPkg "main/pkg/config"
"main/pkg/fs"
loggerPkg "main/pkg/logger"
"os"

Expand All @@ -13,14 +14,19 @@ var (
version = "unknown"
)

type MainFS struct{}
type OsFS struct {
}

func (fs *MainFS) ReadFile(name string) ([]byte, error) {
func (fs *OsFS) ReadFile(name string) ([]byte, error) {
return os.ReadFile(name)
}

func (fs *OsFS) Create(path string) (fs.File, error) {
return os.Create(path)
}

func Execute(configPath string) {
filesystem := &MainFS{}
filesystem := &OsFS{}
config, err := configPkg.GetConfig(configPath, filesystem)
if err != nil {
loggerPkg.GetDefaultLogger().Fatal().Err(err).Msg("Could not load config")
Expand All @@ -31,7 +37,7 @@ func Execute(configPath string) {
warning.Log(loggerPkg.GetDefaultLogger())
}

app := pkg.NewApp(config, version)
app := pkg.NewApp(config, version, filesystem)
app.Start()
}

Expand Down
130 changes: 24 additions & 106 deletions pkg/alias_manager/alias_manager.go
Original file line number Diff line number Diff line change
@@ -1,76 +1,33 @@
package alias_manager

import (
"os"

"main/pkg/config"
configTypes "main/pkg/config/types"
"main/pkg/fs"

"github.com/BurntSushi/toml"
"github.com/rs/zerolog"
)

type Aliases *map[string]string
type TomlAliases map[string]Aliases

type ChainAliases struct {
Chain *configTypes.Chain
Aliases Aliases
}
type AllChainAliases map[string]*ChainAliases

type AliasManager struct {
Logger zerolog.Logger
Path string
Chains configTypes.Chains
Aliases AllChainAliases
}

func (a AllChainAliases) ToTomlAliases() TomlAliases {
tomlAliases := make(TomlAliases, len(a))
for chainName, chainAliases := range a {
tomlAliases[chainName] = chainAliases.Aliases
}

return tomlAliases
}

type ChainAliasesLinks struct {
Chain *configTypes.Chain
Links map[string]*configTypes.Link
}

func (a AllChainAliases) ToAliasesLinks() []ChainAliasesLinks {
aliasesLinks := make([]ChainAliasesLinks, 0)

for _, chainAliases := range a {
links := make(map[string]*configTypes.Link)

if chainAliases.Aliases == nil {
continue
}

for wallet, alias := range *chainAliases.Aliases {
link := chainAliases.Chain.GetWalletLink(wallet)
link.Title = alias
links[wallet] = link
}

aliasesLinks = append(aliasesLinks, ChainAliasesLinks{
Chain: chainAliases.Chain,
Links: links,
})
}

return aliasesLinks
Aliases AllAliases
FS fs.FS
}

func NewAliasManager(logger *zerolog.Logger, config *config.AppConfig) *AliasManager {
func NewAliasManager(
logger *zerolog.Logger,
config *config.AppConfig,
fs fs.FS,
) *AliasManager {
return &AliasManager{
Logger: logger.With().Str("component", "alias_manager").Logger(),
Path: config.AliasesPath,
Chains: config.Chains,
Aliases: make(map[string]*ChainAliases, 0),
Aliases: AllAliases{},
FS: fs,
}
}

Expand All @@ -84,7 +41,7 @@ func (m *AliasManager) Load() {
return
}

aliasesBytes, err := os.ReadFile(m.Path)
aliasesBytes, err := m.FS.ReadFile(m.Path)
if err != nil {
m.Logger.Error().Err(err).Msg("Could not load aliases")
return
Expand All @@ -98,19 +55,7 @@ func (m *AliasManager) Load() {
return
}

m.Aliases = make(map[string]*ChainAliases, len(aliasesStruct))
for chainName, chainAliases := range aliasesStruct {
chain := m.Chains.FindByName(chainName)
if chain == nil {
m.Logger.Fatal().Str("chain", chainName).Msg("Could not find chain found in alias config!")
}

m.Aliases[chainName] = &ChainAliases{
Chain: chain,
Aliases: chainAliases,
}
}

m.Aliases = aliasesStruct.ToAliases(m.Chains, m.Logger)
m.Logger.Info().Msg("Aliases loaded")
}

Expand All @@ -122,7 +67,7 @@ func (m *AliasManager) Save() error {

tomlAliases := m.Aliases.ToTomlAliases()

f, err := os.Create(m.Path)
f, err := m.FS.Create(m.Path)
if err != nil {
m.Logger.Error().Err(err).Msg("Could not create aliases file")
return err
Expand All @@ -139,54 +84,27 @@ func (m *AliasManager) Save() error {
return nil
}

func (m *AliasManager) Get(chain, address string) string {
if !m.Enabled() {
m.Logger.Warn().Msg("Aliases path not set, cannot get alias")
return ""
}

chainAliases, ok := m.Aliases[chain]
if !ok {
return ""
}

aliases := *chainAliases.Aliases
alias, ok := aliases[address]
if !ok {
return ""
}

return alias
func (m *AliasManager) Get(subscription, chain, address string) string {
return m.Aliases.Get(subscription, chain, address)
}

func (m *AliasManager) Set(chain, address, alias string) error {
func (m *AliasManager) Set(subscription, chainName, address, alias string) error {
if !m.Enabled() {
m.Logger.Warn().Msg("Aliases path not set, cannot set alias")
return nil
}

_, ok := m.Aliases[chain]
if !ok {
chainFound := m.Chains.FindByName(chain)
if chainFound == nil {
m.Logger.Fatal().Str("chain", chain).Msg("Could not find chain when setting an alias!")
}

aliases := make(map[string]string, 1)

m.Aliases[chain] = &ChainAliases{
Chain: chainFound,
Aliases: &aliases,
}
chainFound := m.Chains.FindByName(chainName)
if chainFound == nil {
m.Logger.Panic().
Str("chain", chainName).
Msg("Could not find chain when setting an alias!")
}

chainAliases := m.Aliases[chain]
aliases := *chainAliases.Aliases
aliases[address] = alias

m.Aliases.Set(subscription, chainFound, address, alias)
return m.Save()
}

func (m *AliasManager) GetAliasesLinks() []ChainAliasesLinks {
return m.Aliases.ToAliasesLinks()
func (m *AliasManager) GetAliasesLinks(subscription string) []ChainAliasesLinks {
return m.Aliases.GetAliasesLinks(subscription)
}
Loading

0 comments on commit 76aaa0e

Please sign in to comment.