Final fixes
This commit is contained in:
parent
e89846d490
commit
d107d924a8
|
@ -61,8 +61,8 @@ func (bc *Blockchain) MineBlock(transactions []*Transaction) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindUnspentTransactions returns a list of transactions containing unspent outputs
|
// FindUnspentTransactions returns a list of transactions containing unspent outputs
|
||||||
func (bc *Blockchain) FindUnspentTransactions(address string) []*Transaction {
|
func (bc *Blockchain) FindUnspentTransactions(address string) []Transaction {
|
||||||
var unspentTXs []*Transaction
|
var unspentTXs []Transaction
|
||||||
spentTXOs := make(map[string][]int)
|
spentTXOs := make(map[string][]int)
|
||||||
bci := bc.Iterator()
|
bci := bc.Iterator()
|
||||||
|
|
||||||
|
@ -84,7 +84,7 @@ func (bc *Blockchain) FindUnspentTransactions(address string) []*Transaction {
|
||||||
}
|
}
|
||||||
|
|
||||||
if out.CanBeUnlockedWith(address) {
|
if out.CanBeUnlockedWith(address) {
|
||||||
unspentTXs = append(unspentTXs, tx)
|
unspentTXs = append(unspentTXs, *tx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -75,33 +75,33 @@ func NewCoinbaseTX(to, data string) *Transaction {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewUTXOTransaction creates a new transaction
|
// NewUTXOTransaction creates a new transaction
|
||||||
func NewUTXOTransaction(from, to string, value int, bc *Blockchain) *Transaction {
|
func NewUTXOTransaction(from, to string, amount int, bc *Blockchain) *Transaction {
|
||||||
var inputs []TXInput
|
var inputs []TXInput
|
||||||
var outputs []TXOutput
|
var outputs []TXOutput
|
||||||
|
|
||||||
acc, validOutputs := bc.FindSpendableOutputs(from, value)
|
acc, validOutputs := bc.FindSpendableOutputs(from, amount)
|
||||||
|
|
||||||
if acc < value {
|
if acc < amount {
|
||||||
log.Panic("ERROR: Not enough funds")
|
log.Panic("ERROR: Not enough funds")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build a list of inputs
|
// Build a list of inputs
|
||||||
for txid, outs := range validOutputs {
|
for txid, outs := range validOutputs {
|
||||||
for _, out := range outs {
|
|
||||||
txID, err := hex.DecodeString(txid)
|
txID, err := hex.DecodeString(txid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, out := range outs {
|
||||||
input := TXInput{txID, out, from}
|
input := TXInput{txID, out, from}
|
||||||
inputs = append(inputs, input)
|
inputs = append(inputs, input)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build a list of outputs
|
// Build a list of outputs
|
||||||
outputs = append(outputs, TXOutput{value, to})
|
outputs = append(outputs, TXOutput{amount, to})
|
||||||
if acc > value {
|
if acc > amount {
|
||||||
outputs = append(outputs, TXOutput{acc - value, from}) // a change
|
outputs = append(outputs, TXOutput{acc - amount, from}) // a change
|
||||||
}
|
}
|
||||||
|
|
||||||
tx := Transaction{nil, inputs, outputs}
|
tx := Transaction{nil, inputs, outputs}
|
||||||
|
|
Loading…
Reference in New Issue