Skip to content

Commit

Permalink
vspd: Split update function.
Browse files Browse the repository at this point in the history
The update function performs four distinct steps which can readily be
broken down into four separate functions.
  • Loading branch information
jholdstock committed Sep 19, 2023
1 parent 6dc6614 commit 32967c0
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions internal/vspd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,37 @@ func (v *Vspd) update(ctx context.Context) {

// Step 1/4: Update the database with any tickets which now have 6+
// confirmations.
v.updateUnconfirmed(ctx, dcrdClient)
if ctx.Err() != nil {
return
}

// Step 2/4: Broadcast fee tx for tickets which are confirmed.
v.broadcastFees(ctx, dcrdClient)
if ctx.Err() != nil {
return
}

// Step 3/4: Add tickets with confirmed fees to voting wallets.
v.addToWallets(ctx, dcrdClient)
if ctx.Err() != nil {
return
}

// Step 4/4: Set ticket outcome in database if any tickets are voted/revoked.
v.setOutcomes(ctx)
if ctx.Err() != nil {
return
}
}

func (v *Vspd) updateUnconfirmed(ctx context.Context, dcrdClient *rpc.DcrdRPC) {
const funcName = "updateUnconfirmed"

unconfirmed, err := v.db.GetUnconfirmedTickets()
if err != nil {
v.log.Errorf("%s: db.GetUnconfirmedTickets error: %v", funcName, err)
return
}

for _, ticket := range unconfirmed {
Expand Down Expand Up @@ -84,12 +111,15 @@ func (v *Vspd) update(ctx context.Context) {
v.log.Infof("%s: Ticket confirmed (ticketHash=%s)", funcName, ticket.Hash)
}
}
}

// Step 2/4: Broadcast fee tx for tickets which are confirmed.
func (v *Vspd) broadcastFees(ctx context.Context, dcrdClient *rpc.DcrdRPC) {
const funcName = "broadcastFees"

pending, err := v.db.GetPendingFees()
if err != nil {
v.log.Errorf("%s: db.GetPendingFees error: %v", funcName, err)
return
}

for _, ticket := range pending {
Expand All @@ -115,12 +145,15 @@ func (v *Vspd) update(ctx context.Context) {
funcName, ticket.Hash, err)
}
}
}

// Step 3/4: Add tickets with confirmed fees to voting wallets.
func (v *Vspd) addToWallets(ctx context.Context, dcrdClient *rpc.DcrdRPC) {
const funcName = "addToWallets"

unconfirmedFees, err := v.db.GetUnconfirmedFees()
if err != nil {
v.log.Errorf("%s: db.GetUnconfirmedFees error: %v", funcName, err)
return
}

walletClients, failedConnections := v.wallets.Clients()
Expand Down Expand Up @@ -226,8 +259,10 @@ func (v *Vspd) update(ctx context.Context) {
}
}
}
}

// Step 4/4: Set ticket outcome in database if any tickets are voted/revoked.
func (v *Vspd) setOutcomes(ctx context.Context) {
const funcName = "setOutcomes"

votableTickets, err := v.db.GetVotableTickets()
if err != nil {
Expand Down

0 comments on commit 32967c0

Please sign in to comment.