Use utxo index in API
This commit is contained in:
parent
61e096b3f9
commit
ce0529c130
@ -827,6 +827,7 @@ func (w *Worker) getAddrDescUtxo(addrDesc bchain.AddressDescriptor, ba *db.AddrB
|
|||||||
Txid: bchainTx.Txid,
|
Txid: bchainTx.Txid,
|
||||||
Vout: int32(i),
|
Vout: int32(i),
|
||||||
AmountSat: (*Amount)(&vout.ValueSat),
|
AmountSat: (*Amount)(&vout.ValueSat),
|
||||||
|
Locktime: bchainTx.LockTime,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -844,61 +845,32 @@ func (w *Worker) getAddrDescUtxo(addrDesc bchain.AddressDescriptor, ba *db.AddrB
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// ba can be nil if the address is only in mempool!
|
// ba can be nil if the address is only in mempool!
|
||||||
if ba != nil && !IsZeroBigInt(&ba.BalanceSat) {
|
if ba != nil && len(ba.Utxos) > 0 {
|
||||||
outpoints := make([]bchain.Outpoint, 0, 8)
|
|
||||||
err = w.db.GetAddrDescTransactions(addrDesc, 0, maxUint32, func(txid string, height uint32, indexes []int32) error {
|
|
||||||
for _, index := range indexes {
|
|
||||||
// take only outputs
|
|
||||||
if index >= 0 {
|
|
||||||
outpoints = append(outpoints, bchain.Outpoint{Txid: txid, Vout: index})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var lastTxid string
|
|
||||||
var ta *db.TxAddresses
|
|
||||||
var checksum big.Int
|
|
||||||
checksum.Set(&ba.BalanceSat)
|
|
||||||
b, _, err := w.db.GetBestBlock()
|
b, _, err := w.db.GetBestBlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
bestheight := int(b)
|
bestheight := int(b)
|
||||||
for i := 0; i < len(outpoints) && checksum.Int64() > 0; i++ {
|
var checksum big.Int
|
||||||
o := outpoints[i]
|
checksum.Set(&ba.BalanceSat)
|
||||||
if lastTxid != o.Txid {
|
// go backwards to get the newest first
|
||||||
ta, err = w.db.GetTxAddresses(o.Txid)
|
for i := len(ba.Utxos) - 1; i >= 0; i-- {
|
||||||
if err != nil {
|
utxo := &ba.Utxos[i]
|
||||||
return nil, err
|
txid, err := w.chainParser.UnpackTxid(utxo.BtxID)
|
||||||
}
|
if err != nil {
|
||||||
lastTxid = o.Txid
|
return nil, err
|
||||||
}
|
}
|
||||||
if ta == nil {
|
_, e := spentInMempool[txid+strconv.Itoa(int(utxo.Vout))]
|
||||||
glog.Warning("DB inconsistency: tx ", o.Txid, ": not found in txAddresses")
|
if !e {
|
||||||
} else {
|
r = append(r, Utxo{
|
||||||
if len(ta.Outputs) <= int(o.Vout) {
|
Txid: txid,
|
||||||
glog.Warning("DB inconsistency: txAddresses ", o.Txid, " does not have enough outputs")
|
Vout: utxo.Vout,
|
||||||
} else {
|
AmountSat: (*Amount)(&utxo.ValueSat),
|
||||||
if !ta.Outputs[o.Vout].Spent {
|
Height: int(utxo.Height),
|
||||||
v := ta.Outputs[o.Vout].ValueSat
|
Confirmations: bestheight - int(utxo.Height) + 1,
|
||||||
// report only outpoints that are not spent in mempool
|
})
|
||||||
_, e := spentInMempool[o.Txid+strconv.Itoa(int(o.Vout))]
|
|
||||||
if !e {
|
|
||||||
r = append(r, Utxo{
|
|
||||||
Txid: o.Txid,
|
|
||||||
Vout: o.Vout,
|
|
||||||
AmountSat: (*Amount)(&v),
|
|
||||||
Height: int(ta.Height),
|
|
||||||
Confirmations: bestheight - int(ta.Height) + 1,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
checksum.Sub(&checksum, &v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
checksum.Sub(&checksum, &utxo.ValueSat)
|
||||||
}
|
}
|
||||||
if checksum.Uint64() != 0 {
|
if checksum.Uint64() != 0 {
|
||||||
glog.Warning("DB inconsistency: ", addrDesc, ": checksum is not zero")
|
glog.Warning("DB inconsistency: ", addrDesc, ": checksum is not zero")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user