Insert utxos in fixUtxo in the growing order

utxocheck
Martin Boehm 2020-02-17 12:19:44 +01:00
parent 72e48ce658
commit b6e3f26eb2
1 changed files with 25 additions and 21 deletions

View File

@ -1898,30 +1898,34 @@ func (d *RocksDB) fixUtxo(addrDesc bchain.AddressDescriptor, ba *AddrBalance) (b
err := d.GetAddrDescTransactions(addrDesc, 0, ^uint32(0), func(txid string, height uint32, indexes []int32) error {
var ta *TxAddresses
var err error
// sort the indexes so that the utxos are appended in the reverse order
sort.Slice(indexes, func(i, j int) bool {
return indexes[i] > indexes[j]
})
for _, index := range indexes {
// take only outputs
if index >= 0 {
if ta == nil {
ta, err = d.GetTxAddresses(txid)
if err != nil {
return err
}
if index < 0 {
break
}
if ta == nil {
ta, err = d.GetTxAddresses(txid)
if err != nil {
return err
}
if ta == nil {
return errors.New("DB inconsistency: tx " + txid + ": not found in txAddresses")
} else {
if len(ta.Outputs) <= int(index) {
glog.Warning("DB inconsistency: txAddresses " + txid + " does not have enough outputs")
} else {
tao := &ta.Outputs[index]
if !tao.Spent {
bTxid, _ := d.chainParser.PackTxid(txid)
checksumFromTxs.Add(&checksumFromTxs, &tao.ValueSat)
utxos = append(utxos, Utxo{BtxID: bTxid, Height: height, Vout: index, ValueSat: tao.ValueSat})
if checksumFromTxs.Cmp(&ba.BalanceSat) == 0 {
return &StopIteration{}
}
}
}
if ta == nil {
return errors.New("DB inconsistency: tx " + txid + ": not found in txAddresses")
}
if len(ta.Outputs) <= int(index) {
glog.Warning("DB inconsistency: txAddresses " + txid + " does not have enough outputs")
} else {
tao := &ta.Outputs[index]
if !tao.Spent {
bTxid, _ := d.chainParser.PackTxid(txid)
checksumFromTxs.Add(&checksumFromTxs, &tao.ValueSat)
utxos = append(utxos, Utxo{BtxID: bTxid, Height: height, Vout: index, ValueSat: tao.ValueSat})
if checksumFromTxs.Cmp(&ba.BalanceSat) == 0 {
return &StopIteration{}
}
}
}