Various minor cleanups.

- Updating and adding some copyrights which were missed recently.
- Slight improvements to some commenting and naming.
This commit is contained in:
Jamie Holdstock 2021-05-24 18:20:25 +08:00 committed by Jamie Holdstock
parent 20cb546e74
commit e05ced391a
12 changed files with 42 additions and 35 deletions

View File

@ -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 {

View File

@ -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.
@ -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)

View File

@ -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
}

View File

@ -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 (

View File

@ -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 (

View File

@ -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 (

View File

@ -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.
@ -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
})
@ -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)
}
@ -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.

View File

@ -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()

View File

@ -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 {

View File

@ -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)
}

View File

@ -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",

View File

@ -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"
@ -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",
@ -95,7 +93,7 @@ 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)
@ -103,8 +101,8 @@ func setVoteChoices(c *gin.Context) {
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 {
@ -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