Skip to content

Commit

Permalink
api: consolidate how we build ElectionSummaries
Browse files Browse the repository at this point in the history
Fixes one of the TODOs I had left behind.
The other TODO was already solved by a previous commit.
  • Loading branch information
mvdan authored and p4u committed Jul 27, 2023
1 parent 0a5f6bb commit f7bffd9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 63 deletions.
11 changes: 8 additions & 3 deletions api/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,14 @@ func (a *API) electionListHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContex
return ErrParamStatusMissing
}

elections, err := a.electionSummaryList(pids...)
if err != nil {
return err
elections := []*ElectionSummary{}
for _, pid := range pids {
procInfo, err := a.indexer.ProcessInfo(pid)
if err != nil {
return ErrCantFetchElection.WithErr(err)
}
summary := a.electionSummary(procInfo)
elections = append(elections, &summary)
}
data, err := json.Marshal(&Organization{
Elections: elections,
Expand Down
45 changes: 9 additions & 36 deletions api/elections.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/hex"
"encoding/json"
"errors" // required for evm encoding
"errors"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -145,16 +145,7 @@ func (a *API) electionFullListHandler(_ *apirest.APIdata, ctx *httprouter.HTTPCo
if err != nil {
return ErrCantFetchElection.Withf("(%x): %v", eid, err)
}
list = append(list, ElectionSummary{
ElectionID: eid,
OrganizationID: e.EntityID,
Status: models.ProcessStatus_name[e.Status],
StartDate: a.vocinfo.HeightTime(int64(e.StartBlock)),
EndDate: a.vocinfo.HeightTime(int64(e.EndBlock)),
FinalResults: e.FinalResults,
VoteCount: e.VoteCount,
ManuallyEnded: e.EndBlock < e.StartBlock+e.BlockCount,
})
list = append(list, a.electionSummary(e))
}
// wrap list in a struct to consistently return list in a object, return empty
// object if the list does not contains any result
Expand Down Expand Up @@ -191,21 +182,12 @@ func (a *API) electionHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext) e
}

election := Election{
ElectionSummary: ElectionSummary{
ElectionID: electionID,
OrganizationID: proc.EntityID,
Status: strings.ToLower(models.ProcessStatus_name[proc.Status]),
StartDate: a.vocinfo.HeightTime(int64(proc.StartBlock)),
EndDate: a.vocinfo.HeightTime(int64(proc.EndBlock)),
FinalResults: proc.FinalResults,
VoteCount: proc.VoteCount,
ManuallyEnded: proc.EndBlock < proc.StartBlock+proc.BlockCount,
},
MetadataURL: proc.Metadata,
CreationTime: proc.CreationTime,
VoteMode: VoteMode{EnvelopeType: proc.Envelope},
ElectionMode: ElectionMode{ProcessMode: proc.Mode},
TallyMode: TallyMode{ProcessVoteOptions: proc.VoteOpts},
ElectionSummary: a.electionSummary(proc),
MetadataURL: proc.Metadata,
CreationTime: proc.CreationTime,
VoteMode: VoteMode{EnvelopeType: proc.Envelope},
ElectionMode: ElectionMode{ProcessMode: proc.Mode},
TallyMode: TallyMode{ProcessVoteOptions: proc.VoteOpts},
Census: &ElectionCensus{
CensusOrigin: models.CensusOrigin_name[proc.CensusOrigin],
CensusRoot: proc.CensusRoot,
Expand Down Expand Up @@ -674,16 +656,7 @@ func (a *API) electionFilterPaginatedHandler(msg *apirest.APIdata, ctx *httprout
if err != nil {
return ErrCantFetchElection.WithErr(err)
}
list = append(list, ElectionSummary{
OrganizationID: e.EntityID,
ElectionID: eid,
Status: models.ProcessStatus_name[e.Status],
StartDate: a.vocinfo.HeightTime(int64(e.StartBlock)),
EndDate: a.vocinfo.HeightTime(int64(e.EndBlock)),
FinalResults: e.FinalResults,
VoteCount: e.VoteCount,
ManuallyEnded: e.EndBlock < e.StartBlock+e.BlockCount,
})
list = append(list, a.electionSummary(e))
}
data, err := json.Marshal(struct {
Elections []ElectionSummary `json:"elections"`
Expand Down
33 changes: 12 additions & 21 deletions api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,33 @@ import (
"encoding/hex"
"encoding/json"
"errors"
"fmt" // required for evm encoding
"fmt"
"math/big"
"reflect"
"strings"

cmtpool "github.com/cometbft/cometbft/mempool"
ctypes "github.com/cometbft/cometbft/rpc/core/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/iancoleman/strcase"
"go.vocdoni.io/dvote/types"
"go.vocdoni.io/dvote/vochain/indexer/indexertypes"
"go.vocdoni.io/proto/build/go/models"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)

func (a *API) electionSummaryList(pids ...[]byte) ([]*ElectionSummary, error) {
processes := []*ElectionSummary{}
for _, pid := range pids {
// TODO(mvdan): We construct ElectionSummaries in many places, deduplicate.
// TODO(mvdan): ProcessInfo could give us the results envelope height as well.
procInfo, err := a.indexer.ProcessInfo(pid)
if err != nil {
return nil, ErrCantFetchElection.WithErr(err)
}
processes = append(processes, &ElectionSummary{
ElectionID: procInfo.ID,
OrganizationID: procInfo.EntityID,
Status: strings.ToLower(models.ProcessStatus_name[procInfo.Status]),
StartDate: procInfo.CreationTime,
EndDate: a.vocinfo.HeightTime(int64(procInfo.EndBlock)),
FinalResults: procInfo.FinalResults,
VoteCount: procInfo.VoteCount,
})
func (a *API) electionSummary(pi *indexertypes.Process) ElectionSummary {
return ElectionSummary{
ElectionID: pi.ID,
OrganizationID: pi.EntityID,
Status: models.ProcessStatus_name[pi.Status],
StartDate: a.vocinfo.HeightTime(int64(pi.StartBlock)),
EndDate: a.vocinfo.HeightTime(int64(pi.EndBlock)),
FinalResults: pi.FinalResults,
VoteCount: pi.VoteCount,
ManuallyEnded: pi.EndBlock < pi.StartBlock+pi.BlockCount,
}
return processes, nil
}

// sendTx wraps a.vocapp.SendTx(). If an error is returned, it's wrapped into an apirest.APIerror
Expand Down
7 changes: 4 additions & 3 deletions test/testcommon/vochain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package testcommon

import (
"encoding/base64"
secp "github.com/cometbft/cometbft/crypto/secp256k1"
"github.com/cometbft/cometbft/privval"
"google.golang.org/protobuf/proto"
"math/rand"
"strconv"
"testing"

secp "github.com/cometbft/cometbft/crypto/secp256k1"
"github.com/cometbft/cometbft/privval"
"google.golang.org/protobuf/proto"

"go.vocdoni.io/dvote/db"
"go.vocdoni.io/dvote/test/testcommon/testutil"
"go.vocdoni.io/dvote/util"
Expand Down

0 comments on commit f7bffd9

Please sign in to comment.