Skip to content

Commit

Permalink
multi: Always iterate over all VSP tickets.
Browse files Browse the repository at this point in the history
ForUnspentUnexpiredTickets now collects all errors which occur and
returns them at once, rather than returning on first error.
  • Loading branch information
jholdstock authored and jrick committed Jul 25, 2023
1 parent 35c6ac0 commit a38abe2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
21 changes: 8 additions & 13 deletions internal/rpc/jsonrpc/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -4674,34 +4674,29 @@ func (s *Server) updateVSPVoteChoices(ctx context.Context, w *wallet.Wallet, tic
err = vspClient.SetVoteChoice(ctx, ticketHash, choices, tspendPolicy, treasuryPolicy)
return err
}
var firstErr error

err := w.ForUnspentUnexpiredTickets(ctx, func(hash *chainhash.Hash) error {
vspHost, err := w.VSPHostForTicket(ctx, hash)
if err != nil && firstErr == nil {
if err != nil {
if errors.Is(err, errors.NotExist) {
// Ticket is not registered with a VSP, nothing more to do here.
return nil
}
firstErr = err
return nil
return err
}
vspClient, err := loader.LookupVSP(vspHost)
if err != nil && firstErr == nil {
firstErr = err
return nil
if err != nil {
return err
}
// Never return errors here, so all tickets are tried.
// The first error will be returned to the user.
err = vspClient.SetVoteChoice(ctx, hash, choices, tspendPolicy, treasuryPolicy)
if err != nil && firstErr == nil {
firstErr = err
if err != nil {
return err
}
return nil
})
if err != nil {
return err
}
return firstErr
return err
}

// signMessage signs the given message with the private key for the given
Expand Down
11 changes: 7 additions & 4 deletions internal/rpc/rpcserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4311,22 +4311,25 @@ func (s *walletServer) SetVspdVoteChoices(ctx context.Context, req *pb.SetVspdVo
}

err = s.wallet.ForUnspentUnexpiredTickets(ctx, func(hash *chainhash.Hash) error {
// Skip errors here, but should we log at least?
choices, _, err := s.wallet.AgendaChoices(ctx, hash)
if err != nil {
return nil
return err
}
ticketHost, err := s.wallet.VSPHostForTicket(ctx, hash)
if err != nil {
return err
}
if ticketHost == vspHost {
_ = vspClient.SetVoteChoice(ctx, hash, choices, tSpendChoices, treasuryChoices)
err = vspClient.SetVoteChoice(ctx, hash, choices, tSpendChoices, treasuryChoices)
if err != nil {
return err
}
}
return nil
})
if err != nil {
return nil, status.Errorf(codes.Unknown, "ForUnspentUnexpiredTickets failed. Error: %v", err)
return nil, status.Errorf(codes.Unknown, "ForUnspentUnexpiredTickets failed. Error: %v",
err)
}

return &pb.SetVspdVoteChoicesResponse{}, nil
Expand Down
17 changes: 15 additions & 2 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5882,6 +5882,9 @@ func (w *Wallet) ForUnspentUnexpiredTickets(ctx context.Context,

params := w.ChainParams()

// Quietly collect any errors returned by f to ensure f is called on every
// ticket. All errors are returned to the caller at the end.
var errs []error
iter := func(ticketSummaries []*TicketSummary, _ *wire.BlockHeader) (bool, error) {
for _, ticketSummary := range ticketSummaries {
switch ticketSummary.Status {
Expand All @@ -5895,6 +5898,7 @@ func (w *Wallet) ForUnspentUnexpiredTickets(ctx context.Context,
ticketHash := *ticketSummary.Ticket.Hash
err := f(&ticketHash)
if err != nil {
errs = append(errs, err)
continue
}
}
Expand All @@ -5908,7 +5912,13 @@ func (w *Wallet) ForUnspentUnexpiredTickets(ctx context.Context,
int32(params.TicketExpiry+uint32(params.TicketMaturity)-requiredConfs)
startBlock := NewBlockIdentifierFromHeight(startBlockNum)
endBlock := NewBlockIdentifierFromHeight(blockHeight)
return w.GetTickets(ctx, iter, startBlock, endBlock)

err := w.GetTickets(ctx, iter, startBlock, endBlock)
if err != nil {
errs = append(errs, err)
}

return errors.Join(errs...)
}

// UnprocessedTickets returns the hash of every live/immature ticket in the
Expand Down Expand Up @@ -5940,6 +5950,9 @@ func (w *Wallet) UnprocessedTickets(ctx context.Context) ([]*chainhash.Hash, err
unmanagedTickets = append(unmanagedTickets, hash)
return nil
})
if err != nil {
return nil, err
}

return unmanagedTickets, err
return unmanagedTickets, nil
}

0 comments on commit a38abe2

Please sign in to comment.