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

It's not a bug, just I think the second judgment condition is not necessary. #56

Open
yudidi opened this issue Mar 2, 2019 · 0 comments

Comments

@yudidi
Copy link

yudidi commented Mar 2, 2019

In branch part_5, blockchain.go#L109:

if out.IsLockedWithKey(pubKeyHash) && accumulated < amount {

The second judgment condition accumulated < amount is not necessary. It only works when amount <= 0, In this case, we should check the value of amount at the beginning of the function.

func (bc *Blockchain) FindSpendableOutputs(pubKeyHash []byte, amount int) (int, map[string][]int) {
	unspentOutputs := make(map[string][]int)
	accumulated := 0
        // Maybe we should check “amount” at the beginning of the function.
	if (amount <= 0){ 
		return accumulated,unspentOutputs
	}
	unspentTXs := bc.FindUnspentTransactions(pubKeyHash)
	
Work:
	for _, tx := range unspentTXs {
		txID := hex.EncodeToString(tx.ID)

		for outIdx, out := range tx.Vout {
			if out.IsLockedWithKey(pubKeyHash)  { // Delete “&& accumulated < amount”
				accumulated += out.Value
				unspentOutputs[txID] = append(unspentOutputs[txID], outIdx)

				if accumulated >= amount {
					break Work 
				}
			}
		}
	}
	return accumulated, unspentOutputs
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant