Final fixes

This commit is contained in:
Ivan Kuznetsov 2017-09-05 21:35:45 +07:00
parent e89846d490
commit d107d924a8
2 changed files with 14 additions and 14 deletions

View File

@ -61,8 +61,8 @@ func (bc *Blockchain) MineBlock(transactions []*Transaction) {
}
// FindUnspentTransactions returns a list of transactions containing unspent outputs
func (bc *Blockchain) FindUnspentTransactions(address string) []*Transaction {
var unspentTXs []*Transaction
func (bc *Blockchain) FindUnspentTransactions(address string) []Transaction {
var unspentTXs []Transaction
spentTXOs := make(map[string][]int)
bci := bc.Iterator()
@ -84,7 +84,7 @@ func (bc *Blockchain) FindUnspentTransactions(address string) []*Transaction {
}
if out.CanBeUnlockedWith(address) {
unspentTXs = append(unspentTXs, tx)
unspentTXs = append(unspentTXs, *tx)
}
}

View File

@ -75,33 +75,33 @@ func NewCoinbaseTX(to, data string) *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 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")
}
// Build a list of inputs
for txid, outs := range validOutputs {
for _, out := range outs {
txID, err := hex.DecodeString(txid)
if err != nil {
log.Panic(err)
}
txID, err := hex.DecodeString(txid)
if err != nil {
log.Panic(err)
}
for _, out := range outs {
input := TXInput{txID, out, from}
inputs = append(inputs, input)
}
}
// Build a list of outputs
outputs = append(outputs, TXOutput{value, to})
if acc > value {
outputs = append(outputs, TXOutput{acc - value, from}) // a change
outputs = append(outputs, TXOutput{amount, to})
if acc > amount {
outputs = append(outputs, TXOutput{acc - amount, from}) // a change
}
tx := Transaction{nil, inputs, outputs}