Skip to content

Commit

Permalink
vochain: small simplifications from reading the code
Browse files Browse the repository at this point in the history
NewEmptyVotes always gets the same VoteOpts parameters,
so move the logic inside the func body.

compResultsHeight is only used to set HaveResults;
use the boolean which set compResultsHeight to be non-zero directly.
  • Loading branch information
mvdan authored and p4u committed Oct 27, 2023
1 parent aea6db7 commit 2bff887
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 13 deletions.
2 changes: 1 addition & 1 deletion vochain/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func (idx *Indexer) AfterSyncBootstrap(inTest bool) {
indxR := &results.Results{
ProcessID: p,
// MaxValue requires +1 since 0 is also an option
Votes: results.NewEmptyVotes(int(options.MaxCount), int(options.MaxValue)+1),
Votes: results.NewEmptyVotes(options),
Weight: new(types.BigInt).SetUint64(0),
VoteOpts: options,
EnvelopeType: process.Envelope,
Expand Down
9 changes: 2 additions & 7 deletions vochain/indexer/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,14 @@ func (idx *Indexer) newEmptyProcess(pid []byte) error {
p.StartBlock = idx.App.Height()
}

compResultsHeight := uint32(0)
if !p.EnvelopeType.EncryptedVotes { // like isOpenProcess, but on the state type
compResultsHeight = p.BlockCount + p.StartBlock + 1
}

// Create and store process in the indexer database
procParams := indexerdb.CreateProcessParams{
ID: pid,
EntityID: nonNullBytes(eid),
StartBlock: int64(p.StartBlock),
EndBlock: int64(p.BlockCount + p.StartBlock),
BlockCount: int64(p.BlockCount),
HaveResults: compResultsHeight > 0,
HaveResults: !p.EnvelopeType.EncryptedVotes, // like isOpenProcess, but on the state type
CensusRoot: nonNullBytes(p.CensusRoot),
MaxCensusSize: int64(p.GetMaxCensusSize()),
CensusUri: p.GetCensusURI(),
Expand All @@ -185,7 +180,7 @@ func (idx *Indexer) newEmptyProcess(pid []byte) error {
SourceBlockHeight: int64(p.GetSourceBlockHeight()),
SourceNetworkID: int64(p.SourceNetworkId),
Metadata: p.GetMetadata(),
ResultsVotes: indexertypes.EncodeJSON(results.NewEmptyVotes(int(options.MaxCount), int(options.MaxValue)+1)),
ResultsVotes: indexertypes.EncodeJSON(results.NewEmptyVotes(options)),
}

idx.blockMu.Lock()
Expand Down
2 changes: 1 addition & 1 deletion vochain/results/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func ComputeResults(electionID []byte, st *state.State) (*Results, error) {
return nil, fmt.Errorf("maxCount overflow %d", p.VoteOptions.MaxCount)
}
results := &Results{
Votes: NewEmptyVotes(int(p.VoteOptions.MaxCount), int(p.VoteOptions.MaxValue)+1),
Votes: NewEmptyVotes(p.VoteOptions),
ProcessID: electionID,
Weight: new(types.BigInt).SetUint64(0),
VoteOpts: p.VoteOptions,
Expand Down
10 changes: 6 additions & 4 deletions vochain/results/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (r *Results) AddVote(voteValues []int, weight *big.Int, mutex *sync.Mutex)
// Add the Election weight (tells how much voting power have already been processed)
r.Weight.Add(r.Weight, (*types.BigInt)(weight))
if len(r.Votes) == 0 {
r.Votes = NewEmptyVotes(int(r.VoteOpts.MaxCount), int(r.VoteOpts.MaxValue)+1)
r.Votes = NewEmptyVotes(r.VoteOpts)
}

// If MaxValue is zero, consider discrete value couting. So for each questoin, the value
Expand Down Expand Up @@ -223,14 +223,16 @@ func (r *Results) AddVote(voteValues []int, weight *big.Int, mutex *sync.Mutex)
}

// NewEmptyVotes creates a new results struct with the given number of questions and options
func NewEmptyVotes(questions, options int) [][]*types.BigInt {
func NewEmptyVotes(voteOpts *models.ProcessVoteOptions) [][]*types.BigInt {
questions := voteOpts.MaxCount
options := voteOpts.MaxValue + 1
if questions == 0 || options == 0 {
return nil
}
results := [][]*types.BigInt{}
for i := 0; i < questions; i++ {
for i := uint32(0); i < questions; i++ {
question := []*types.BigInt{}
for j := 0; j < options; j++ {
for j := uint32(0); j < options; j++ {
question = append(question, new(types.BigInt).SetUint64(0))
}
results = append(results, question)
Expand Down

0 comments on commit 2bff887

Please sign in to comment.