Skip to content

Commit

Permalink
udb: use a credit iterator for detached credits.
Browse files Browse the repository at this point in the history
This uses a credit iterator for converting detached
 and unspent non-coinbase credits to unmined credit.
  • Loading branch information
dnldd committed Jun 10, 2019
1 parent 917ae5c commit ed2e099
Showing 1 changed file with 32 additions and 29 deletions.
61 changes: 32 additions & 29 deletions wallet/udb/txmined.go
Original file line number Diff line number Diff line change
Expand Up @@ -1808,39 +1808,39 @@ func (s *Store) rollback(ns walletdb.ReadWriteBucket, addrmgrNs walletdb.ReadBuc
// credit output to unmined. If the credit is marked
// unspent, it is removed from the utxo set and the
// mined balance is decremented.
//
// TODO: use a credit iterator
for i, output := range rec.MsgTx.TxOut {
k, v := existsCredit(ns, &rec.Hash, uint32(i),
&b.Block)
if v == nil {
continue
k := keyTxRecord(&rec.Hash, &b.Block)
credIter := makeReadCreditIterator(ns, k, DBVersion)
defer credIter.close()
for credIter.next() {
if int(credIter.elem.Index) >= len(rec.MsgTx.TxOut) {
credIter.close()
return errors.E(errors.IO,
"saved credit index exceeds number of outputs")
}
vcopy := make([]byte, len(v))
copy(vcopy, v)
removedCredits[string(k)] = vcopy

amt, change, err := fetchRawCreditAmountChange(v)
// scrType := pkScriptType(output.PkScript)
scrPos := fetchRawCreditScriptOffset(credIter.cv)
scrLen := fetchRawCreditScriptLength(credIter.cv)

pkScript, err := fetchRawTxRecordPkScript(credIter.ck,
credIter.cv, credIter.elem.Index, scrPos, scrLen)
if err != nil {
return err
}
opCode := fetchRawCreditTagOpCode(v)
isCoinbase := fetchRawCreditIsCoinbase(v)
hasExpiry := fetchRawCreditHasExpiry(v, DBVersion)

scrType := pkScriptType(output.PkScript)
scrLoc := rec.MsgTx.PkScriptLocs()[i]
scrLen := len(rec.MsgTx.TxOut[i].PkScript)

acct, err := s.fetchAccountForPkScript(addrmgrNs, v, nil, output.PkScript)
acct, err := s.fetchAccountForPkScript(addrmgrNs, credIter.cv,
nil, pkScript)
if err != nil {
return err
}

outPointKey := canonicalOutPoint(&rec.Hash, uint32(i))
unminedCredVal := valueUnminedCredit(amt, change, opCode,
isCoinbase, hasExpiry, scrType, uint32(scrLoc), uint32(scrLen),
acct, DBVersion)
scrType := pkScriptType(pkScript)
scrLoc := rec.MsgTx.PkScriptLocs()[credIter.elem.Index]
outPointKey := canonicalOutPoint(&rec.Hash, credIter.elem.Index)
unminedCredVal := valueUnminedCredit(credIter.elem.Amount,
credIter.elem.Change, credIter.elem.OpCode,
credIter.elem.IsCoinbase, credIter.elem.HasExpiry, scrType,
uint32(scrLoc), uint32(scrLen), acct, DBVersion)
err = putRawUnminedCredit(ns, outPointKey, unminedCredVal)
if err != nil {
return err
Expand All @@ -1855,20 +1855,20 @@ func (s *Store) rollback(ns walletdb.ReadWriteBucket, addrmgrNs walletdb.ReadBuc
if credKey != nil {
// Ticket amounts were never added, so ignore them when
// correcting the balance.
isTicketOutput := (txType == stake.TxTypeSStx && i == 0)
isTicketOutput := (txType == stake.TxTypeSStx &&
credIter.elem.Index == 0)
if !isTicketOutput {
minedBalance -= dcrutil.Amount(output.Value)
minedBalance -= credIter.elem.Amount
}
err = deleteRawUnspent(ns, outPointKey)
if err != nil {
return err
}
}

// Check if this output is a multisignature
// P2SH output. If it is, access the value
// for the key and mark it unmined.
msKey := keyMultisigOut(*txHash, uint32(i))
// Check if this output is a multisignature P2SH output.
// If it is, access the value for the key and mark it unmined.
msKey := keyMultisigOut(*txHash, uint32(credIter.elem.Index))
msVal := existsMultisigOutCopy(ns, msKey)
if msVal != nil {
setMultisigOutUnmined(msVal)
Expand All @@ -1878,6 +1878,9 @@ func (s *Store) rollback(ns walletdb.ReadWriteBucket, addrmgrNs walletdb.ReadBuc
}
}
}
if credIter.err != nil {
return credIter.err
}
}

// reposition cursor before deleting this k/v pair and advancing to the
Expand Down

0 comments on commit ed2e099

Please sign in to comment.