Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

used UTXO_set index directly to set TxInput.Vout was a bug. #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ func (bc *Blockchain) FindUTXO() map[string]TXOutputs {

outs := UTXO[txID]
outs.Outputs = append(outs.Outputs, out)
outs.OutIdxs = append(outs.OutIdxs,outIdx)
UTXO[txID] = outs
}

Expand Down
1 change: 1 addition & 0 deletions transaction_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func NewTXOutput(value int, address string) *TXOutput {
// TXOutputs collects TXOutput
type TXOutputs struct {
Outputs []TXOutput
OutIdxs []int
}

// Serialize serializes TXOutputs
Expand Down
16 changes: 12 additions & 4 deletions utxo_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (u UTXOSet) FindSpendableOutputs(pubkeyHash []byte, amount int) (int, map[s
for outIdx, out := range outs.Outputs {
if out.IsLockedWithKey(pubkeyHash) && accumulated < amount {
accumulated += out.Value
unspentOutputs[txID] = append(unspentOutputs[txID], outIdx)
unspentOutputs[txID] = append(unspentOutputs[txID], outs.OutIdxs[outIdx])
}
}
}
Expand Down Expand Up @@ -153,9 +153,16 @@ func (u UTXOSet) Update(block *Block) {
outsBytes := b.Get(vin.Txid)
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 {
updatedOuts.Outputs = append(updatedOuts.Outputs, out)
updatedOuts.Outputs = append(updatedOuts.Outputs, outs.Outputs[idx])
updatedOuts.OutIdxs = append(updatedOuts.OutIdxs, outIdx)
}
}

Expand All @@ -175,8 +182,9 @@ func (u UTXOSet) Update(block *Block) {
}

newOutputs := TXOutputs{}
for _, out := range tx.Vout {
for idx, out := range tx.Vout {
newOutputs.Outputs = append(newOutputs.Outputs, out)
newOutputs.OutIdxs = append(newOutputs.OutIdxs, idx)
}

err := b.Put(tx.ID, newOutputs.Serialize())
Expand Down