vote-validator: Use shutdown context.
Using the shutdown context provided by the internal signal package means vote-validator can exit cleanly if shutdown is requested by the user.
This commit is contained in:
parent
935dcaece2
commit
9660de7d9f
@ -6,6 +6,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@ -46,7 +47,7 @@ type dcrdataClient struct {
|
|||||||
URL string
|
URL string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *dcrdataClient) txns(txnHashes []string, spends bool) ([]tx, error) {
|
func (d *dcrdataClient) txns(ctx context.Context, txnHashes []string, spends bool) ([]tx, error) {
|
||||||
jsonData, err := json.Marshal(txns{
|
jsonData, err := json.Marshal(txns{
|
||||||
Transactions: txnHashes,
|
Transactions: txnHashes,
|
||||||
})
|
})
|
||||||
@ -55,7 +56,7 @@ func (d *dcrdataClient) txns(txnHashes []string, spends bool) ([]tx, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
url := fmt.Sprintf("%s/api/txs?spends=%t", d.URL, spends)
|
url := fmt.Sprintf("%s/api/txs?spends=%t", d.URL, spends)
|
||||||
request, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
|
request, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,6 +5,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
@ -16,6 +17,7 @@ import (
|
|||||||
|
|
||||||
"github.com/decred/vspd/database"
|
"github.com/decred/vspd/database"
|
||||||
"github.com/decred/vspd/internal/config"
|
"github.com/decred/vspd/internal/config"
|
||||||
|
"github.com/decred/vspd/internal/signal"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -79,6 +81,8 @@ func run() int {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctx := signal.ShutdownListener(log)
|
||||||
|
|
||||||
// Get all voted tickets from database.
|
// Get all voted tickets from database.
|
||||||
dbTickets, err := vdb.GetVotedTickets()
|
dbTickets, err := vdb.GetVotedTickets()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -105,14 +109,22 @@ func run() int {
|
|||||||
// Use dcrdata to get spender info for voted tickets (dcrd can't do this).
|
// Use dcrdata to get spender info for voted tickets (dcrd can't do this).
|
||||||
log.Infof("Getting vote info from %s", dcrdata.URL)
|
log.Infof("Getting vote info from %s", dcrdata.URL)
|
||||||
for i := 0; i < numTickets; i += chunkSize {
|
for i := 0; i < numTickets; i += chunkSize {
|
||||||
|
// Stop if shutdown requested.
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
end := i + chunkSize
|
end := i + chunkSize
|
||||||
if end > numTickets {
|
if end > numTickets {
|
||||||
end = numTickets
|
end = numTickets
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the tx info for each ticket.
|
// Get the tx info for each ticket.
|
||||||
ticketTxns, err := dcrdata.txns(ticketHashes[i:end], true)
|
ticketTxns, err := dcrdata.txns(ctx, ticketHashes[i:end], true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if errors.Is(err, context.Canceled) {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
@ -125,8 +137,11 @@ func run() int {
|
|||||||
mapSpenderToTicket[spenderHash] = txn.TxID
|
mapSpenderToTicket[spenderHash] = txn.TxID
|
||||||
}
|
}
|
||||||
|
|
||||||
spenderTxns, err := dcrdata.txns(spenderHashes, false)
|
spenderTxns, err := dcrdata.txns(ctx, spenderHashes, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if errors.Is(err, context.Canceled) {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
@ -193,6 +208,10 @@ func run() int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, t := range sorted[0:cfg.ToCheck] {
|
for _, t := range sorted[0:cfg.ToCheck] {
|
||||||
|
// Stop if shutdown requested.
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
if t.voteVersion != latestVoteVersion {
|
if t.voteVersion != latestVoteVersion {
|
||||||
results.wrongVersion = append(results.wrongVersion, t)
|
results.wrongVersion = append(results.wrongVersion, t)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user