From d107d924a8a70c095d6de3527dbc3394579a9c69 Mon Sep 17 00:00:00 2001 From: Ivan Kuznetsov Date: Tue, 5 Sep 2017 21:35:45 +0700 Subject: [PATCH] Final fixes --- blockchain.go | 6 +++--- transaction.go | 22 +++++++++++----------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/blockchain.go b/blockchain.go index d825955..b9cfb42 100644 --- a/blockchain.go +++ b/blockchain.go @@ -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) } } diff --git a/transaction.go b/transaction.go index d67a505..fe82a7b 100644 --- a/transaction.go +++ b/transaction.go @@ -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}