add tests and remove UTXOPool

pull/3/head
Minh Doan 6 years ago
parent b86c836bb5
commit 83240f3506
  1. 37
      UTXOPool.go
  2. 48
      blockchain/blockchain.go
  3. 18
      blockchain/blockchain_test.go
  4. 4
      blockchain/transaction.go

@ -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
// }

@ -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.

@ -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")
}
}

@ -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 {

Loading…
Cancel
Save