From 83240f35062dae6b89a957688982583a32092ac6 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Tue, 12 Jun 2018 00:15:41 -0700 Subject: [PATCH] add tests and remove UTXOPool --- UTXOPool.go | 37 --------------------------- blockchain/blockchain.go | 48 +++++++++++++++++++++++++++++++++++ blockchain/blockchain_test.go | 18 +++++++++++++ blockchain/transaction.go | 4 +-- 4 files changed, 68 insertions(+), 39 deletions(-) delete mode 100644 UTXOPool.go diff --git a/UTXOPool.go b/UTXOPool.go deleted file mode 100644 index 79073170a..000000000 --- a/UTXOPool.go +++ /dev/null @@ -1,37 +0,0 @@ -package main - -// UTXOPool is the data structure to store the current balance. -type UTXOPool struct { - utxos map[string]int -} - -// func (utxoPool *UTXOPool) handleTransaction(transaction Transaction, receiver string) { -// if !isValidTransaction(transaction) { -// return -// } -// // utxoPool[] -// } - -// func (utxoPool *UTXOPool) isValidTransaction(transaction Transaction) { -// const { inputPublicKey, amount, fee } = transaction -// const utxo = this.utxos[inputPublicKey] -// return utxo !== undefined && utxo.amount >= (amount + fee) && amount > 0 -// } - -// func (utxoPool *UTXOPool) handleTransaction(transaction, feeReceiver) { -// if (!this.isValidTransaction(transaction)) -// return -// const inputUTXO = this.utxos[transaction.inputPublicKey]; -// inputUTXO.amount -= transaction.amount -// inputUTXO.amount -= transaction.fee -// if (inputUTXO.amount === 0) -// delete this.utxos[transaction.inputPublicKey] -// this.addUTXO(transaction.outputPublicKey, transaction.amount) -// this.addUTXO(feeReceiver, transaction.fee) -// } - -// func (utxoPool *UTXOPool) isValidTransaction(transaction Transaction) { -// const { inputPublicKey, amount, fee } = transaction -// const utxo = utxoPool.utxos[inputPublicKey] -// return utxo !== undefined && utxo.amount >= (amount + fee) && amount > 0 -// } diff --git a/blockchain/blockchain.go b/blockchain/blockchain.go index 1136a40e9..1ef78b77b 100644 --- a/blockchain/blockchain.go +++ b/blockchain/blockchain.go @@ -85,6 +85,54 @@ Work: return accumulated, unspentOutputs } +// NewUTXOTransaction creates a new transaction +func (bc *Blockchain) NewUTXOTransaction(from, to string, amount int) *Transaction { + var inputs []TXInput + var outputs []TXOutput + + acc, validOutputs := bc.FindSpendableOutputs(from, amount) + + if acc < amount { + return nil + } + + // Build a list of inputs + for txid, outs := range validOutputs { + txID, err := hex.DecodeString(txid) + if err != nil { + return nil + } + + for _, out := range outs { + input := TXInput{txID, out, from} + inputs = append(inputs, input) + } + } + + // Build a list of outputs + outputs = append(outputs, TXOutput{amount, to}) + if acc > amount { + outputs = append(outputs, TXOutput{acc - amount, from}) // a change + } + + tx := Transaction{nil, inputs, outputs} + tx.SetID() + + return &tx +} + +// AddNewTransferAmount creates a new transaction and a block of that transaction. +// Mostly used for testing. +func (bc *Blockchain) AddNewTransferAmount(from, to string, amount int) *Blockchain { + tx := bc.NewUTXOTransaction(from, to, amount) + if tx != nil { + newBlock := NewBlock([]*Transaction{tx}, bc.blocks[len(bc.blocks)-1].PrevBlockHash) + bc.blocks = append(bc.blocks, newBlock) + return bc + } + return nil +} + // CreateBlockchain creates a new blockchain DB func CreateBlockchain(address string) *Blockchain { // TODO: We assume we have not created any blockchain before. diff --git a/blockchain/blockchain_test.go b/blockchain/blockchain_test.go index 08e7ef077..5d9c5f51f 100644 --- a/blockchain/blockchain_test.go +++ b/blockchain/blockchain_test.go @@ -37,3 +37,21 @@ func TestFindUTXO(t *testing.T) { t.Error("FindUTXO failed.") } } + +func TestAddNewTransferAmount(t *testing.T) { + bc := CreateBlockchain("minh") + + bc = bc.AddNewTransferAmount("minh", "alok", 3) + + if bc == nil { + t.Error("Failed to add new transfer") + } + + t.Log(len(bc.blocks)) + bc = bc.AddNewTransferAmount("minh", "rj", DefaultCoinbaseValue-2) + + t.Log(len(bc.blocks)) + if bc != nil { + t.Error("minh should not have enough fun to make the transfer") + } +} diff --git a/blockchain/transaction.go b/blockchain/transaction.go index d8bce39e4..cc99b5acc 100644 --- a/blockchain/transaction.go +++ b/blockchain/transaction.go @@ -68,14 +68,14 @@ func NewUTXOTransaction(from, to string, amount int, bc *Blockchain) *Transactio acc, validOutputs := bc.FindSpendableOutputs(from, amount) if acc < amount { - log.Panic("ERROR: Not enough funds") + return nil } // Build a list of inputs for txid, outs := range validOutputs { txID, err := hex.DecodeString(txid) if err != nil { - log.Panic(err) + return nil } for _, out := range outs {