Skip to content

Commit

Permalink
Correct TicketsForAddress returning pruned tickets
Browse files Browse the repository at this point in the history
TicketsForAddress used to return tickets that existed in the wstakemgr
database but which were pruned in the wtxmgr database. This update
also checks the ticket hashes against the wtxmgr database in case they
have been pruned and removes pruned tickets from the response.
  • Loading branch information
cjepson committed Mar 2, 2016
1 parent 0b35b98 commit 8ac528e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
20 changes: 19 additions & 1 deletion rpc/legacyrpc/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,8 @@ func GetStakeInfo(icmd interface{}, w *wallet.Wallet, chainClient *chain.RPCClie
for i := range localTickets {
localTicketPtrs[i] = &localTickets[i]
}

// Check the live ticket pool for the presense of tickets.
existsBitSetBStr, err := chainClient.ExistsLiveTickets(localTicketPtrs)
if err != nil {
return nil, fmt.Errorf("Failed to find assess whether tickets "+
Expand Down Expand Up @@ -2335,6 +2337,9 @@ func RedeemMultiSigOuts(icmd interface{}, w *wallet.Wallet, chainClient *chain.R
return dcrjson.RedeemMultiSigOutsResult{rmsoResults}, nil
}

// TicketsForAddress retrieves all ticket hashes that have the passed voting
// address. It will only return tickets that are in the mempool or blockchain,
// and should not return pruned tickets.
func TicketsForAddress(icmd interface{}, w *wallet.Wallet) (interface{}, error) {
cmd := icmd.(*dcrjson.TicketsForAddressCmd)

Expand All @@ -2343,11 +2348,24 @@ func TicketsForAddress(icmd interface{}, w *wallet.Wallet) (interface{}, error)
return nil, err
}

tickets, err := w.StakeMgr.DumpSStxHashesForAddress(addr)
ticketsAll, err := w.StakeMgr.DumpSStxHashesForAddress(addr)
if err != nil {
return nil, err
}

// Check the wallet database.
tickets := make([]chainhash.Hash, 0, len(ticketsAll))
for i := range ticketsAll {
exists, err := w.TxStore.ExistsTx(&ticketsAll[i])
if err != nil {
log.Errorf("Enountered database error while retrieving "+
"tickets for address: %v", err.Error())
}
if exists {
tickets = append(tickets, ticketsAll[i])
}
}

ticketsStr := make([]string, len(tickets), len(tickets))
for i, h := range tickets {
ticketsStr[i] = h.String()
Expand Down
29 changes: 29 additions & 0 deletions wtxmgr/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,35 @@ func (s *Store) Tx(txHash *chainhash.Hash) (*wire.MsgTx, error) {
return msgTx, err
}

// ExistsTx checks to see if a transaction exists in the database.
func (s *Store) ExistsTx(txHash *chainhash.Hash) (bool, error) {
exists := false
err := scopedView(s.namespace, func(ns walletdb.Bucket) error {
// First, check whether there exists an unmined transaction with this
// hash. Use it if found.
v := existsRawUnmined(ns, txHash[:])
if v != nil {
exists = true
return nil
}

// Otherwise, if there exists a mined transaction with this matching
// hash, skip over to the newest and begin fetching the msgTx.
_, v = latestTxRecord(ns, txHash)
if v != nil {
exists = true
return nil
}

return nil
})
if err != nil {
return false, err
}

return exists, nil
}

// UniqueTxDetails looks up all recorded details for a transaction recorded
// mined in some particular block, or an unmined transaction if block is nil.
//
Expand Down

0 comments on commit 8ac528e

Please sign in to comment.