Skip to content

Commit

Permalink
Various minor cleanups.
Browse files Browse the repository at this point in the history
- Updating and adding some copyrights which were missed recently.
- Slight improvements to some commenting and naming.
  • Loading branch information
jholdstock committed May 31, 2021
1 parent 20cb546 commit e05ced3
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 35 deletions.
2 changes: 1 addition & 1 deletion background/background.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func blockConnected() {
continue
}

// Update vote choices on voting wallets.
// Set vote choices on voting wallets.
for agenda, choice := range ticket.VoteChoices {
err = walletClient.SetVoteChoice(agenda, choice, ticket.Hash)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions database/database.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020 The Decred developers
// Copyright (c) 2020-2021 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -120,7 +120,6 @@ func CreateNew(dbFile, feeXPub string) error {

defer db.Close()

// Create all storage buckets of the VSP if they don't already exist.
err = db.Update(func(tx *bolt.Tx) error {
// Create parent bucket.
vspBkt, err := tx.CreateBucket(vspBktK)
Expand Down
6 changes: 2 additions & 4 deletions database/ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,12 @@ func getTicketFromBkt(bkt *bolt.Bucket) (Ticket, error) {
ticket.Confirmed = true
}

voteChoices := make(map[string]string)
err := json.Unmarshal(bkt.Get(voteChoicesK), &voteChoices)
ticket.VoteChoices = make(map[string]string)
err := json.Unmarshal(bkt.Get(voteChoicesK), &ticket.VoteChoices)
if err != nil {
return ticket, err
}

ticket.VoteChoices = voteChoices

return ticket, nil
}

Expand Down
4 changes: 4 additions & 0 deletions database/upgrade_v2.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright (c) 2021 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package database

import (
Expand Down
4 changes: 4 additions & 0 deletions database/upgrade_v3.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright (c) 2021 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package database

import (
Expand Down
4 changes: 4 additions & 0 deletions database/upgrades.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright (c) 2021 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package database

import (
Expand Down
18 changes: 9 additions & 9 deletions database/votechange.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020 The Decred developers
// Copyright (c) 2020-2021 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -39,16 +39,16 @@ func (vdb *VspDatabase) SaveVoteChange(ticketHash string, record VoteChangeRecor
// Loop through the bucket to count the records, as well as finding the
// most recent and the oldest record.
var count int
highest := uint32(0)
lowest := uint32(math.MaxUint32)
newest := uint32(0)
oldest := uint32(math.MaxUint32)
err = bkt.ForEach(func(k, v []byte) error {
count++
key := bytesToUint32(k)
if key > highest {
highest = key
if key > newest {
newest = key
}
if key < lowest {
lowest = key
if key < oldest {
oldest = key
}
return nil
})
Expand All @@ -59,7 +59,7 @@ func (vdb *VspDatabase) SaveVoteChange(ticketHash string, record VoteChangeRecor
// If bucket is at (or over) the limit of max allowed records, remove
// the oldest one.
if count >= vdb.maxVoteChangeRecords {
err = bkt.Delete(uint32ToBytes(lowest))
err = bkt.Delete(uint32ToBytes(oldest))
if err != nil {
return fmt.Errorf("failed to delete old vote change record: %w", err)
}
Expand All @@ -69,7 +69,7 @@ func (vdb *VspDatabase) SaveVoteChange(ticketHash string, record VoteChangeRecor
// otherwise use most recent + 1.
var newKey uint32
if count > 0 {
newKey = highest + 1
newKey = newest + 1
}

// Insert record.
Expand Down
8 changes: 4 additions & 4 deletions webapi/getfeeaddress.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (
// addrMtx protects getNewFeeAddress.
var addrMtx sync.Mutex

// getNewFeeAddress gets a new address from the address generator and stores the
// new address index in the database. In order to maintain consistency between
// the internal counter of address generator and the database, this function
// cannot be run concurrently.
// getNewFeeAddress gets a new address from the address generator, and updates
// the last used address index in the database. In order to maintain consistency
// between the internal counter of address generator and the database, this func
// uses a mutex to ensure it is not run concurrently.
func getNewFeeAddress(db *database.VspDatabase, addrGen *addressGenerator) (string, uint32, error) {
addrMtx.Lock()
defer addrMtx.Unlock()
Expand Down
6 changes: 3 additions & 3 deletions webapi/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ func currentVoteVersion(params *chaincfg.Params) uint32 {
return latestVersion
}

// isValidVoteChoices returns an error if provided vote choices are not valid for
// the most recent agendas.
func isValidVoteChoices(params *chaincfg.Params, voteVersion uint32, voteChoices map[string]string) error {
// validConsensusVoteChoices returns an error if provided vote choices are not
// valid for the most recent consensus agendas.
func validConsensusVoteChoices(params *chaincfg.Params, voteVersion uint32, voteChoices map[string]string) error {

agendaLoop:
for agenda, choice := range voteChoices {
Expand Down
2 changes: 1 addition & 1 deletion webapi/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestIsValidVoteChoices(t *testing.T) {
}

for _, test := range tests {
err := isValidVoteChoices(params, voteVersion, test.voteChoices)
err := validConsensusVoteChoices(params, voteVersion, test.voteChoices)
if (err == nil) != test.valid {
t.Fatalf("isValidVoteChoices failed for votechoices '%v'.", test.voteChoices)
}
Expand Down
2 changes: 1 addition & 1 deletion webapi/payfee.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func payFee(c *gin.Context) {
// Validate VoteChoices. Just log a warning if vote choices are not valid
// for the current vote version - the ticket should still be registered.
validVoteChoices := true
err = isValidVoteChoices(cfg.NetParams, currentVoteVersion(cfg.NetParams), request.VoteChoices)
err = validConsensusVoteChoices(cfg.NetParams, currentVoteVersion(cfg.NetParams), request.VoteChoices)
if err != nil {
validVoteChoices = false
log.Warnf("%s: Invalid vote choices (clientIP=%s, ticketHash=%s): %v",
Expand Down
18 changes: 8 additions & 10 deletions webapi/setvotechoices.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ import (
"github.com/gin-gonic/gin/binding"
)

type timestampRequest struct {
Timestamp int64 `json:"timestamp" binding:"required"`
}

// setVoteChoices is the handler for "POST /api/v3/setvotechoices".
func setVoteChoices(c *gin.Context) {
const funcName = "setVoteChoices"
Expand Down Expand Up @@ -76,7 +72,9 @@ func setVoteChoices(c *gin.Context) {
}

for _, change := range previousChanges {
var prevReq timestampRequest
var prevReq struct {
Timestamp int64 `json:"timestamp" binding:"required"`
}
err := json.Unmarshal([]byte(change.Request), &prevReq)
if err != nil {
log.Errorf("%s: Could not unmarshal vote change record (ticketHash=%s): %v",
Expand All @@ -95,16 +93,16 @@ func setVoteChoices(c *gin.Context) {
}

voteChoices := request.VoteChoices
err = isValidVoteChoices(cfg.NetParams, currentVoteVersion(cfg.NetParams), voteChoices)
err = validConsensusVoteChoices(cfg.NetParams, currentVoteVersion(cfg.NetParams), voteChoices)
if err != nil {
log.Warnf("%s: Invalid vote choices (clientIP=%s, ticketHash=%s): %v",
funcName, c.ClientIP(), ticket.Hash, err)
sendErrorWithMsg(err.Error(), errInvalidVoteChoices, c)
return
}

// Update VoteChoices in the database before updating the wallets. DB is
// source of truth and is less likely to error.
// Update VoteChoices in the database before updating the wallets. DB is the
// source of truth, and also is less likely to error.
ticket.VoteChoices = voteChoices
err = db.UpdateTicket(ticket)
if err != nil {
Expand All @@ -117,8 +115,8 @@ func setVoteChoices(c *gin.Context) {
// Update vote choices on voting wallets. Tickets are only added to voting
// wallets if their fee is confirmed.
if ticket.FeeTxStatus == database.FeeConfirmed {
for agenda, choice := range voteChoices {
for _, walletClient := range walletClients {
for _, walletClient := range walletClients {
for agenda, choice := range voteChoices {
err = walletClient.SetVoteChoice(agenda, choice, ticket.Hash)
if err != nil {
// If this fails, we still want to try the other wallets, so
Expand Down

0 comments on commit e05ced3

Please sign in to comment.