Skip to content

Commit

Permalink
chore: add fetcher interface
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Dec 21, 2023
1 parent 9b06d50 commit 34f45bd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
18 changes: 9 additions & 9 deletions pkg/data/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package data

import (
"fmt"
"main/pkg/fetchers/cosmos"
"main/pkg/fetchers"
"main/pkg/types"
"sync"

Expand All @@ -26,16 +26,16 @@ func (m *Manager) GetTallies() (map[string]types.ChainTallyInfos, error) {
var mutex sync.Mutex

errors := make([]error, 0)
tallies := make(map[string]types.ChainTallyInfos, 0)
tallies := make(map[string]types.ChainTallyInfos)

for _, chain := range m.Chains {
rpc := cosmos.NewRPC(chain, m.Logger)
fetcher := fetchers.GetFetcher(chain, m.Logger)

wg.Add(1)
go func(c *types.Chain, rpc *cosmos.RPC) {
go func(c *types.Chain, fetcher fetchers.Fetcher) {
defer wg.Done()

talliesForChain, err := rpc.GetTallies()
talliesForChain, err := fetcher.GetTallies()

mutex.Lock()

Expand All @@ -46,7 +46,7 @@ func (m *Manager) GetTallies() (map[string]types.ChainTallyInfos, error) {
tallies[c.Name] = talliesForChain
}
mutex.Unlock()
}(chain, rpc)
}(chain, fetcher)
}

wg.Wait()
Expand All @@ -63,7 +63,7 @@ func (m *Manager) GetParams() (map[string]types.ChainWithVotingParams, error) {
var wg sync.WaitGroup
var mutex sync.Mutex

params := make(map[string]types.ChainWithVotingParams, 0)
params := make(map[string]types.ChainWithVotingParams)
errors := make([]error, 0)

for _, chain := range m.Chains {
Expand All @@ -72,9 +72,9 @@ func (m *Manager) GetParams() (map[string]types.ChainWithVotingParams, error) {
go func(chain *types.Chain) {
defer wg.Done()

rpc := cosmos.NewRPC(chain, m.Logger)
fetcher := fetchers.GetFetcher(chain, m.Logger)

chainParams, errs := rpc.GetChainParams()
chainParams, errs := fetcher.GetChainParams()
mutex.Lock()
defer mutex.Unlock()

Expand Down
20 changes: 20 additions & 0 deletions pkg/fetchers/fetcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package fetchers

import (
"main/pkg/fetchers/cosmos"
"main/pkg/types"

"github.com/rs/zerolog"
)

type Fetcher interface {
GetAllProposals() ([]types.Proposal, *types.QueryError)
GetVote(proposal, voter string) (*types.Vote, *types.QueryError)
GetTallies() (types.ChainTallyInfos, error)

GetChainParams() (*types.ChainWithVotingParams, []error)
}

func GetFetcher(chainConfig *types.Chain, logger zerolog.Logger) Fetcher {

Check failure on line 18 in pkg/fetchers/fetcher.go

View workflow job for this annotation

GitHub Actions / lint

GetFetcher returns interface (main/pkg/fetchers.Fetcher) (ireturn)
return cosmos.NewRPC(chainConfig, logger)
}
12 changes: 6 additions & 6 deletions pkg/state/generator.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package state

import (
"main/pkg/fetchers/cosmos"
"main/pkg/fetchers"
"main/pkg/types"
"sync"

Expand Down Expand Up @@ -44,9 +44,9 @@ func (g *Generator) ProcessChain(
state State,
oldState State,
) {
rpc := cosmos.NewRPC(chain, g.Logger)
fetcher := fetchers.GetFetcher(chain, g.Logger)

proposals, err := rpc.GetAllProposals()
proposals, err := fetcher.GetAllProposals()
if err != nil {
g.Logger.Warn().Err(err).Msg("Error processing proposals")
g.Mutex.Lock()
Expand Down Expand Up @@ -82,7 +82,7 @@ func (g *Generator) ProcessChain(
wg.Add(1)

go func(p types.Proposal, w *types.Wallet) {
g.ProcessProposalAndWallet(chain, p, rpc, w, state, oldState)
g.ProcessProposalAndWallet(chain, p, fetcher, w, state, oldState)
wg.Done()
}(proposal, wallet)
}
Expand All @@ -94,13 +94,13 @@ func (g *Generator) ProcessChain(
func (g *Generator) ProcessProposalAndWallet(
chain *types.Chain,
proposal types.Proposal,
rpc *cosmos.RPC,
fetcher fetchers.Fetcher,
wallet *types.Wallet,
state State,
oldState State,
) {
oldVote, _, found := oldState.GetVoteAndProposal(chain.Name, proposal.ID, wallet.Address)
vote, err := rpc.GetVote(proposal.ID, wallet.Address)
vote, err := fetcher.GetVote(proposal.ID, wallet.Address)

if found && oldVote.HasVoted() && vote == nil {
g.Logger.Trace().
Expand Down

0 comments on commit 34f45bd

Please sign in to comment.