vspd: Pass dcrd RPC to findSpentTickets as a param

This allows RPC clients to be reused more easily in the case that the
caller of findSpentTickets already has a connected dcrd RPC client.
This commit is contained in:
jholdstock 2023-09-19 13:50:49 +01:00 committed by Jamie Holdstock
parent 61c9c7087f
commit c4de3d5d95
3 changed files with 12 additions and 10 deletions

View File

@ -88,11 +88,16 @@ func (v *Vspd) checkRevoked(ctx context.Context) error {
v.log.Warnf("Updating %s in revoked status, this may take a while...",
pluralize(len(revoked), "ticket"))
dcrdClient, _, err := v.dcrd.Client()
if err != nil {
return err
}
// Search for the transactions which spend these tickets, starting at the
// earliest height one of them matured.
startHeight := revoked.EarliestPurchaseHeight() + int64(v.network.TicketMaturity)
spent, _, err := v.findSpentTickets(ctx, revoked, startHeight)
spent, _, err := v.findSpentTickets(ctx, dcrdClient, revoked, startHeight)
if err != nil {
return fmt.Errorf("findSpentTickets error: %w", err)
}

View File

@ -13,6 +13,7 @@ import (
"github.com/decred/dcrd/txscript/v4/stdaddr"
"github.com/decred/dcrd/wire"
"github.com/decred/vspd/database"
"github.com/decred/vspd/rpc"
)
type spentTicket struct {
@ -64,12 +65,8 @@ func (s *spentTicket) missed() bool {
// against the block filters of the mainchain blocks between the provided start
// block and the current best block. Returns any found spent tickets and the
// height of the most recent scanned block.
func (v *Vspd) findSpentTickets(ctx context.Context, toCheck database.TicketList,
startHeight int64) ([]spentTicket, int64, error) {
dcrdClient, _, err := v.dcrd.Client()
if err != nil {
return nil, 0, err
}
func (v *Vspd) findSpentTickets(ctx context.Context, dcrdClient *rpc.DcrdRPC,
toCheck database.TicketList, startHeight int64) ([]spentTicket, int64, error) {
endHeight, err := dcrdClient.GetBlockCount()
if err != nil {

View File

@ -45,7 +45,7 @@ func (v *Vspd) update(ctx context.Context) {
}
// Step 4/4: Set ticket outcome in database if any tickets are voted/revoked.
v.setOutcomes(ctx)
v.setOutcomes(ctx, dcrdClient)
if ctx.Err() != nil {
return
}
@ -261,7 +261,7 @@ func (v *Vspd) addToWallets(ctx context.Context, dcrdClient *rpc.DcrdRPC) {
}
}
func (v *Vspd) setOutcomes(ctx context.Context) {
func (v *Vspd) setOutcomes(ctx context.Context, dcrdClient *rpc.DcrdRPC) {
const funcName = "setOutcomes"
votableTickets, err := v.db.GetVotableTickets()
@ -285,7 +285,7 @@ func (v *Vspd) setOutcomes(ctx context.Context) {
startHeight = v.lastScannedBlock
}
spent, endHeight, err := v.findSpentTickets(ctx, votableTickets, startHeight)
spent, endHeight, err := v.findSpentTickets(ctx, dcrdClient, votableTickets, startHeight)
if err != nil {
// Don't log error if shutdown was requested, just return.
if errors.Is(err, context.Canceled) {