fix TxOutputs index bug
This commit is contained in:
parent
fee9bfd3af
commit
7f1f7b6748
|
@ -178,6 +178,7 @@ func (bc *Blockchain) FindUTXO() map[string]TXOutputs {
|
||||||
|
|
||||||
outs := UTXO[txID]
|
outs := UTXO[txID]
|
||||||
outs.Outputs = append(outs.Outputs, out)
|
outs.Outputs = append(outs.Outputs, out)
|
||||||
|
outs.OutIdxs = append(outs.OutIdxs,outIdx)
|
||||||
UTXO[txID] = outs
|
UTXO[txID] = outs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -35,6 +35,7 @@ func NewTXOutput(value int, address string) *TXOutput {
|
||||||
// TXOutputs collects TXOutput
|
// TXOutputs collects TXOutput
|
||||||
type TXOutputs struct {
|
type TXOutputs struct {
|
||||||
Outputs []TXOutput
|
Outputs []TXOutput
|
||||||
|
OutIdxs []int
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serialize serializes TXOutputs
|
// Serialize serializes TXOutputs
|
||||||
|
|
16
utxo_set.go
16
utxo_set.go
|
@ -31,7 +31,7 @@ func (u UTXOSet) FindSpendableOutputs(pubkeyHash []byte, amount int) (int, map[s
|
||||||
for outIdx, out := range outs.Outputs {
|
for outIdx, out := range outs.Outputs {
|
||||||
if out.IsLockedWithKey(pubkeyHash) && accumulated < amount {
|
if out.IsLockedWithKey(pubkeyHash) && accumulated < amount {
|
||||||
accumulated += out.Value
|
accumulated += out.Value
|
||||||
unspentOutputs[txID] = append(unspentOutputs[txID], outIdx)
|
unspentOutputs[txID] = append(unspentOutputs[txID], outs.OutIdxs[outIdx])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -153,9 +153,16 @@ func (u UTXOSet) Update(block *Block) {
|
||||||
outsBytes := b.Get(vin.Txid)
|
outsBytes := b.Get(vin.Txid)
|
||||||
outs := DeserializeOutputs(outsBytes)
|
outs := DeserializeOutputs(outsBytes)
|
||||||
|
|
||||||
for outIdx, out := range outs.Outputs {
|
//for outIdx, out := range outs.Outputs {
|
||||||
|
// if outIdx != vin.Vout {
|
||||||
|
// updatedOuts.Outputs = append(updatedOuts.Outputs, out)
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
|
for idx, outIdx := range outs.OutIdxs {
|
||||||
if outIdx != vin.Vout {
|
if outIdx != vin.Vout {
|
||||||
updatedOuts.Outputs = append(updatedOuts.Outputs, out)
|
updatedOuts.Outputs = append(updatedOuts.Outputs, outs.Outputs[idx])
|
||||||
|
updatedOuts.OutIdxs = append(updatedOuts.OutIdxs, outIdx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -175,8 +182,9 @@ func (u UTXOSet) Update(block *Block) {
|
||||||
}
|
}
|
||||||
|
|
||||||
newOutputs := TXOutputs{}
|
newOutputs := TXOutputs{}
|
||||||
for _, out := range tx.Vout {
|
for idx, out := range tx.Vout {
|
||||||
newOutputs.Outputs = append(newOutputs.Outputs, out)
|
newOutputs.Outputs = append(newOutputs.Outputs, out)
|
||||||
|
newOutputs.OutIdxs = append(newOutputs.OutIdxs, idx)
|
||||||
}
|
}
|
||||||
|
|
||||||
err := b.Put(tx.ID, newOutputs.Serialize())
|
err := b.Put(tx.ID, newOutputs.Serialize())
|
||||||
|
|
Loading…
Reference in New Issue