Rename TXInput.LockedBy and TXOutput.Unlock methods
This commit is contained in:
parent
78dbfc69b6
commit
326ecb828c
|
@ -83,7 +83,7 @@ func (bc *Blockchain) FindUnspentTransactions(address string) []*Transaction {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if out.Unlock(address) {
|
if out.CanBeUnlockedWith(address) {
|
||||||
unspentTXs = append(unspentTXs, tx)
|
unspentTXs = append(unspentTXs, tx)
|
||||||
continue Outputs
|
continue Outputs
|
||||||
}
|
}
|
||||||
|
@ -91,7 +91,7 @@ func (bc *Blockchain) FindUnspentTransactions(address string) []*Transaction {
|
||||||
|
|
||||||
if tx.IsCoinbase() == false {
|
if tx.IsCoinbase() == false {
|
||||||
for _, in := range tx.Vin {
|
for _, in := range tx.Vin {
|
||||||
if in.LockedBy(address) {
|
if in.CanUnlockOutputWith(address) {
|
||||||
inTxID := hex.EncodeToString(in.Txid)
|
inTxID := hex.EncodeToString(in.Txid)
|
||||||
spentTXOs[inTxID] = append(spentTXOs[inTxID], in.Vout)
|
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)
|
unspentTXs := bc.FindUnspentTransactions(address)
|
||||||
accumulated := 0
|
accumulated := 0
|
||||||
|
|
||||||
// TODO: Fix this
|
|
||||||
if amount == -1 {
|
|
||||||
accumulated = -2
|
|
||||||
}
|
|
||||||
|
|
||||||
Work:
|
Work:
|
||||||
for _, tx := range unspentTXs {
|
for _, tx := range unspentTXs {
|
||||||
txID := hex.EncodeToString(tx.GetHash())
|
txID := hex.EncodeToString(tx.GetHash())
|
||||||
|
|
||||||
for outIdx, out := range tx.Vout {
|
for outIdx, out := range tx.Vout {
|
||||||
if out.Unlock(address) && accumulated < amount {
|
if out.CanBeUnlockedWith(address) && accumulated < amount {
|
||||||
accumulated += out.Value
|
accumulated += out.Value
|
||||||
unspentOutputs[txID] = append(unspentOutputs[txID], outIdx)
|
unspentOutputs[txID] = append(unspentOutputs[txID], outIdx)
|
||||||
|
|
||||||
|
|
2
cli.go
2
cli.go
|
@ -27,7 +27,7 @@ func (cli *CLI) getBalance(address string) {
|
||||||
|
|
||||||
for _, tx := range utxs {
|
for _, tx := range utxs {
|
||||||
for _, out := range tx.Vout {
|
for _, out := range tx.Vout {
|
||||||
if out.Unlock(address) {
|
if out.CanBeUnlockedWith(address) {
|
||||||
balance += out.Value
|
balance += out.Value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,13 +50,13 @@ type TXOutput struct {
|
||||||
ScriptPubKey string
|
ScriptPubKey string
|
||||||
}
|
}
|
||||||
|
|
||||||
// LockedBy checks whether the address initiated the transaction
|
// CanUnlockOutputWith checks whether the address initiated the transaction
|
||||||
func (in *TXInput) LockedBy(address string) bool {
|
func (in *TXInput) CanUnlockOutputWith(unlockingData string) bool {
|
||||||
return in.ScriptSig == address
|
return in.ScriptSig == unlockingData
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unlock checks if the output can be unlocked with the provided data
|
// CanBeUnlockedWith checks if the output can be unlocked with the provided data
|
||||||
func (out *TXOutput) Unlock(unlockingData string) bool {
|
func (out *TXOutput) CanBeUnlockedWith(unlockingData string) bool {
|
||||||
return out.ScriptPubKey == unlockingData
|
return out.ScriptPubKey == unlockingData
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue