Various minor cleanups.
- Updating and adding some copyrights which were missed recently. - Slight improvements to some commenting and naming.
This commit is contained in:
parent
20cb546e74
commit
e05ced391a
@ -207,7 +207,7 @@ func blockConnected() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update vote choices on voting wallets.
|
// Set vote choices on voting wallets.
|
||||||
for agenda, choice := range ticket.VoteChoices {
|
for agenda, choice := range ticket.VoteChoices {
|
||||||
err = walletClient.SetVoteChoice(agenda, choice, ticket.Hash)
|
err = walletClient.SetVoteChoice(agenda, choice, ticket.Hash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@ -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
|
// Use of this source code is governed by an ISC
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
@ -120,7 +120,6 @@ func CreateNew(dbFile, feeXPub string) error {
|
|||||||
|
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
// Create all storage buckets of the VSP if they don't already exist.
|
|
||||||
err = db.Update(func(tx *bolt.Tx) error {
|
err = db.Update(func(tx *bolt.Tx) error {
|
||||||
// Create parent bucket.
|
// Create parent bucket.
|
||||||
vspBkt, err := tx.CreateBucket(vspBktK)
|
vspBkt, err := tx.CreateBucket(vspBktK)
|
||||||
|
|||||||
@ -210,14 +210,12 @@ func getTicketFromBkt(bkt *bolt.Bucket) (Ticket, error) {
|
|||||||
ticket.Confirmed = true
|
ticket.Confirmed = true
|
||||||
}
|
}
|
||||||
|
|
||||||
voteChoices := make(map[string]string)
|
ticket.VoteChoices = make(map[string]string)
|
||||||
err := json.Unmarshal(bkt.Get(voteChoicesK), &voteChoices)
|
err := json.Unmarshal(bkt.Get(voteChoicesK), &ticket.VoteChoices)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ticket, err
|
return ticket, err
|
||||||
}
|
}
|
||||||
|
|
||||||
ticket.VoteChoices = voteChoices
|
|
||||||
|
|
||||||
return ticket, nil
|
return ticket, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|||||||
@ -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
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|||||||
@ -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
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|||||||
@ -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
|
// Use of this source code is governed by an ISC
|
||||||
// license that can be found in the LICENSE file.
|
// 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
|
// Loop through the bucket to count the records, as well as finding the
|
||||||
// most recent and the oldest record.
|
// most recent and the oldest record.
|
||||||
var count int
|
var count int
|
||||||
highest := uint32(0)
|
newest := uint32(0)
|
||||||
lowest := uint32(math.MaxUint32)
|
oldest := uint32(math.MaxUint32)
|
||||||
err = bkt.ForEach(func(k, v []byte) error {
|
err = bkt.ForEach(func(k, v []byte) error {
|
||||||
count++
|
count++
|
||||||
key := bytesToUint32(k)
|
key := bytesToUint32(k)
|
||||||
if key > highest {
|
if key > newest {
|
||||||
highest = key
|
newest = key
|
||||||
}
|
}
|
||||||
if key < lowest {
|
if key < oldest {
|
||||||
lowest = key
|
oldest = key
|
||||||
}
|
}
|
||||||
return nil
|
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
|
// If bucket is at (or over) the limit of max allowed records, remove
|
||||||
// the oldest one.
|
// the oldest one.
|
||||||
if count >= vdb.maxVoteChangeRecords {
|
if count >= vdb.maxVoteChangeRecords {
|
||||||
err = bkt.Delete(uint32ToBytes(lowest))
|
err = bkt.Delete(uint32ToBytes(oldest))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to delete old vote change record: %w", err)
|
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.
|
// otherwise use most recent + 1.
|
||||||
var newKey uint32
|
var newKey uint32
|
||||||
if count > 0 {
|
if count > 0 {
|
||||||
newKey = highest + 1
|
newKey = newest + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert record.
|
// Insert record.
|
||||||
|
|||||||
@ -19,10 +19,10 @@ import (
|
|||||||
// addrMtx protects getNewFeeAddress.
|
// addrMtx protects getNewFeeAddress.
|
||||||
var addrMtx sync.Mutex
|
var addrMtx sync.Mutex
|
||||||
|
|
||||||
// getNewFeeAddress gets a new address from the address generator and stores the
|
// getNewFeeAddress gets a new address from the address generator, and updates
|
||||||
// new address index in the database. In order to maintain consistency between
|
// the last used address index in the database. In order to maintain consistency
|
||||||
// the internal counter of address generator and the database, this function
|
// between the internal counter of address generator and the database, this func
|
||||||
// cannot be run concurrently.
|
// uses a mutex to ensure it is not run concurrently.
|
||||||
func getNewFeeAddress(db *database.VspDatabase, addrGen *addressGenerator) (string, uint32, error) {
|
func getNewFeeAddress(db *database.VspDatabase, addrGen *addressGenerator) (string, uint32, error) {
|
||||||
addrMtx.Lock()
|
addrMtx.Lock()
|
||||||
defer addrMtx.Unlock()
|
defer addrMtx.Unlock()
|
||||||
|
|||||||
@ -26,9 +26,9 @@ func currentVoteVersion(params *chaincfg.Params) uint32 {
|
|||||||
return latestVersion
|
return latestVersion
|
||||||
}
|
}
|
||||||
|
|
||||||
// isValidVoteChoices returns an error if provided vote choices are not valid for
|
// validConsensusVoteChoices returns an error if provided vote choices are not
|
||||||
// the most recent agendas.
|
// valid for the most recent consensus agendas.
|
||||||
func isValidVoteChoices(params *chaincfg.Params, voteVersion uint32, voteChoices map[string]string) error {
|
func validConsensusVoteChoices(params *chaincfg.Params, voteVersion uint32, voteChoices map[string]string) error {
|
||||||
|
|
||||||
agendaLoop:
|
agendaLoop:
|
||||||
for agenda, choice := range voteChoices {
|
for agenda, choice := range voteChoices {
|
||||||
|
|||||||
@ -46,7 +46,7 @@ func TestIsValidVoteChoices(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
err := isValidVoteChoices(params, voteVersion, test.voteChoices)
|
err := validConsensusVoteChoices(params, voteVersion, test.voteChoices)
|
||||||
if (err == nil) != test.valid {
|
if (err == nil) != test.valid {
|
||||||
t.Fatalf("isValidVoteChoices failed for votechoices '%v'.", test.voteChoices)
|
t.Fatalf("isValidVoteChoices failed for votechoices '%v'.", test.voteChoices)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -104,7 +104,7 @@ func payFee(c *gin.Context) {
|
|||||||
// Validate VoteChoices. Just log a warning if vote choices are not valid
|
// Validate VoteChoices. Just log a warning if vote choices are not valid
|
||||||
// for the current vote version - the ticket should still be registered.
|
// for the current vote version - the ticket should still be registered.
|
||||||
validVoteChoices := true
|
validVoteChoices := true
|
||||||
err = isValidVoteChoices(cfg.NetParams, currentVoteVersion(cfg.NetParams), request.VoteChoices)
|
err = validConsensusVoteChoices(cfg.NetParams, currentVoteVersion(cfg.NetParams), request.VoteChoices)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
validVoteChoices = false
|
validVoteChoices = false
|
||||||
log.Warnf("%s: Invalid vote choices (clientIP=%s, ticketHash=%s): %v",
|
log.Warnf("%s: Invalid vote choices (clientIP=%s, ticketHash=%s): %v",
|
||||||
|
|||||||
@ -15,10 +15,6 @@ import (
|
|||||||
"github.com/gin-gonic/gin/binding"
|
"github.com/gin-gonic/gin/binding"
|
||||||
)
|
)
|
||||||
|
|
||||||
type timestampRequest struct {
|
|
||||||
Timestamp int64 `json:"timestamp" binding:"required"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// setVoteChoices is the handler for "POST /api/v3/setvotechoices".
|
// setVoteChoices is the handler for "POST /api/v3/setvotechoices".
|
||||||
func setVoteChoices(c *gin.Context) {
|
func setVoteChoices(c *gin.Context) {
|
||||||
const funcName = "setVoteChoices"
|
const funcName = "setVoteChoices"
|
||||||
@ -76,7 +72,9 @@ func setVoteChoices(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, change := range previousChanges {
|
for _, change := range previousChanges {
|
||||||
var prevReq timestampRequest
|
var prevReq struct {
|
||||||
|
Timestamp int64 `json:"timestamp" binding:"required"`
|
||||||
|
}
|
||||||
err := json.Unmarshal([]byte(change.Request), &prevReq)
|
err := json.Unmarshal([]byte(change.Request), &prevReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("%s: Could not unmarshal vote change record (ticketHash=%s): %v",
|
log.Errorf("%s: Could not unmarshal vote change record (ticketHash=%s): %v",
|
||||||
@ -95,7 +93,7 @@ func setVoteChoices(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
voteChoices := request.VoteChoices
|
voteChoices := request.VoteChoices
|
||||||
err = isValidVoteChoices(cfg.NetParams, currentVoteVersion(cfg.NetParams), voteChoices)
|
err = validConsensusVoteChoices(cfg.NetParams, currentVoteVersion(cfg.NetParams), voteChoices)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnf("%s: Invalid vote choices (clientIP=%s, ticketHash=%s): %v",
|
log.Warnf("%s: Invalid vote choices (clientIP=%s, ticketHash=%s): %v",
|
||||||
funcName, c.ClientIP(), ticket.Hash, err)
|
funcName, c.ClientIP(), ticket.Hash, err)
|
||||||
@ -103,8 +101,8 @@ func setVoteChoices(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update VoteChoices in the database before updating the wallets. DB is
|
// Update VoteChoices in the database before updating the wallets. DB is the
|
||||||
// source of truth and is less likely to error.
|
// source of truth, and also is less likely to error.
|
||||||
ticket.VoteChoices = voteChoices
|
ticket.VoteChoices = voteChoices
|
||||||
err = db.UpdateTicket(ticket)
|
err = db.UpdateTicket(ticket)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -117,8 +115,8 @@ func setVoteChoices(c *gin.Context) {
|
|||||||
// Update vote choices on voting wallets. Tickets are only added to voting
|
// Update vote choices on voting wallets. Tickets are only added to voting
|
||||||
// wallets if their fee is confirmed.
|
// wallets if their fee is confirmed.
|
||||||
if ticket.FeeTxStatus == database.FeeConfirmed {
|
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)
|
err = walletClient.SetVoteChoice(agenda, choice, ticket.Hash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If this fails, we still want to try the other wallets, so
|
// If this fails, we still want to try the other wallets, so
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user