Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add fetcher interface #61

Merged
merged 2 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ linters:
- musttag
- depguard
- ifshort
- ireturn
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 {
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 @@
state State,
oldState State,
) {
rpc := cosmos.NewRPC(chain, g.Logger)
fetcher := fetchers.GetFetcher(chain, g.Logger)

Check warning on line 47 in pkg/state/generator.go

View check run for this annotation

Codecov / codecov/patch

pkg/state/generator.go#L47

Added line #L47 was not covered by tests

proposals, err := rpc.GetAllProposals()
proposals, err := fetcher.GetAllProposals()

Check warning on line 49 in pkg/state/generator.go

View check run for this annotation

Codecov / codecov/patch

pkg/state/generator.go#L49

Added line #L49 was not covered by tests
if err != nil {
g.Logger.Warn().Err(err).Msg("Error processing proposals")
g.Mutex.Lock()
Expand Down Expand Up @@ -82,7 +82,7 @@
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)

Check warning on line 85 in pkg/state/generator.go

View check run for this annotation

Codecov / codecov/patch

pkg/state/generator.go#L85

Added line #L85 was not covered by tests
wg.Done()
}(proposal, wallet)
}
Expand All @@ -94,13 +94,13 @@
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)

Check warning on line 103 in pkg/state/generator.go

View check run for this annotation

Codecov / codecov/patch

pkg/state/generator.go#L103

Added line #L103 was not covered by tests

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