Rename TXInput.LockedBy and TXOutput.Unlock methods

This commit is contained in:
Ivan Kuznetsov 2017-09-04 11:26:30 +07:00
parent 78dbfc69b6
commit 326ecb828c
3 changed files with 9 additions and 14 deletions

View File

@ -83,7 +83,7 @@ func (bc *Blockchain) FindUnspentTransactions(address string) []*Transaction {
}
}
if out.Unlock(address) {
if out.CanBeUnlockedWith(address) {
unspentTXs = append(unspentTXs, tx)
continue Outputs
}
@ -91,7 +91,7 @@ func (bc *Blockchain) FindUnspentTransactions(address string) []*Transaction {
if tx.IsCoinbase() == false {
for _, in := range tx.Vin {
if in.LockedBy(address) {
if in.CanUnlockOutputWith(address) {
inTxID := hex.EncodeToString(in.Txid)
spentTXOs[inTxID] = append(spentTXOs[inTxID], in.Vout)
}
@ -113,17 +113,12 @@ func (bc *Blockchain) FindUTXOs(address string, amount int) (int, map[string][]i
unspentTXs := bc.FindUnspentTransactions(address)
accumulated := 0
// TODO: Fix this
if amount == -1 {
accumulated = -2
}
Work:
for _, tx := range unspentTXs {
txID := hex.EncodeToString(tx.GetHash())
for outIdx, out := range tx.Vout {
if out.Unlock(address) && accumulated < amount {
if out.CanBeUnlockedWith(address) && accumulated < amount {
accumulated += out.Value
unspentOutputs[txID] = append(unspentOutputs[txID], outIdx)

2
cli.go
View File

@ -27,7 +27,7 @@ func (cli *CLI) getBalance(address string) {
for _, tx := range utxs {
for _, out := range tx.Vout {
if out.Unlock(address) {
if out.CanBeUnlockedWith(address) {
balance += out.Value
}
}

View File

@ -50,13 +50,13 @@ type TXOutput struct {
ScriptPubKey string
}
// LockedBy checks whether the address initiated the transaction
func (in *TXInput) LockedBy(address string) bool {
return in.ScriptSig == address
// CanUnlockOutputWith checks whether the address initiated the transaction
func (in *TXInput) CanUnlockOutputWith(unlockingData string) bool {
return in.ScriptSig == unlockingData
}
// Unlock checks if the output can be unlocked with the provided data
func (out *TXOutput) Unlock(unlockingData string) bool {
// CanBeUnlockedWith checks if the output can be unlocked with the provided data
func (out *TXOutput) CanBeUnlockedWith(unlockingData string) bool {
return out.ScriptPubKey == unlockingData
}