Building Blockchain in Go. Part 4: Transactions

in #building7 years ago

func (bc *Blockchain) FindUTXO(address string) []TXOutput {
var UTXOs []TXOutput
unspentTransactions := bc.FindUnspentTransactions(address)

   for _, tx := range unspentTransactions {
           for _, out := range tx.Vout {
                   if out.CanBeUnlockedWith(address) {
                           UTXOs = append(UTXOs, out)
                   }
           }
   }

   return UTXOs

}
That’s it! Now we can implement getbalance command:

func (cli *CLI) getBalance(address string) {
bc := NewBlockchain(address)
defer bc.db.Close()

balance := 0
UTXOs := bc.FindUTXO(address)

for _, out := range UTXOs {
    balance += out.Value
}

fmt.Printf("Balance of '%s': %d\n", address, balance)

}

The account balance is the sum of values of all unspent transaction outputs locked by the account address.

Let’s check our balance after mining the genesis block:

$ blockchain_go getbalance -address Ivan
Balance of 'Ivan': 10
This is our first money!

Sending Coins
Now, we want to send some coins to someone else. For this, we need to create a new transaction, put it in a block, and mine the block. So far, we implemented only the coinbase transaction (which is a special type of transactions), now we need a general transaction:

func NewUTXOTransaction(from, to string, amount int, bc *Blockchain) *Transaction {
var inputs []TXInput
var outputs []TXOutput

acc, validOutputs := bc.FindSpendableOutputs(from, amount)

if acc < amount {
    log.Panic("ERROR: Not enough funds")
}

// Build a list of inputs
for txid, outs := range validOutputs {
    txID, err := hex.DecodeString(txid)

    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

}
Before creating new outputs, we first have to find all unspent outputs and ensure that they store enough value. This is what FindSpendableOutputs method does. After that, for each found output an input referencing it is created. Next, we create two outputs:

One that’s locked with the receiver address. This is the actual transferring of coins to other address.
One that’s locked with the sender address. This is a change. It’s only created when unspent outputs hold more value than required for the new transaction. Remember: outputs are indivisible.
FindSpendableOutputs method is based on the FindUnspentTransactions method we defined earlier:

func (bc *Blockchain) FindSpendableOutputs(address string, amount int) (int, map[string][]int) {
unspentOutputs := make(map[string][]int)
unspentTXs := bc.FindUnspentTransactions(address)
accumulated := 0

Work:
for _, tx := range unspentTXs {
txID := hex.EncodeToString(tx.ID)

    for outIdx, out := range tx.Vout {
        if out.CanBeUnlockedWith(address) && accumulated < amount {
            accumulated += out.Value
            unspentOutputs[txID] = append(unspentOutputs[txID], outIdx)

            if accumulated >= amount {
                break Work
            }
        }
    }
}

return accumulated, unspentOutputs

}

The method iterates over all unspent transactions and accumulates their values. When the accumulated value is more or equals to the amount we want to transfer, it stops and returns the accumulated value and output indices grouped by transaction IDs. We don’t want to take more than we’re going to spend.

Now we can modify the Blockchain.MineBlock method:

func (bc Blockchain) MineBlock(transactions []Transaction) {
...
newBlock := NewBlock(transactions, lastHash)
...
}
Finally, let’s implement send command:

func (cli *CLI) send(from, to string, amount int) {
bc := NewBlockchain(from)
defer bc.db.Close()

tx := NewUTXOTransaction(from, to, amount, bc)
bc.MineBlock([]*Transaction{tx})
fmt.Println("Success!")

}
Sending coins means creating a transaction and adding it to the blockchain via mining a block. But Bitcoin doesn’t do this immediately (as we do). Instead, it puts all new transactions into memory pool (or mempool), and when a miner is ready to mine a block, it takes all transactions from the mempool and creates a candidate block. Transactions become confirmed only when a block containing them is mined and added to the blockchain.

Let’s check that sending coins works:

$ blockchain_go send -from Ivan -to Pedro -amount 6
00000001b56d60f86f72ab2a59fadb197d767b97d4873732be505e0a65cc1e37

Success!

$ blockchain_go getbalance -address Ivan
Balance of 'Ivan': 4

$ blockchain_go getbalance -address Pedro
Balance of 'Pedro': 6
Nice! Now, let’s create more transactions and ensure that sending from multiple outputs works fine:

$ blockchain_go send -from Pedro -to Helen -amount 2
00000099938725eb2c7730844b3cd40209d46bce2c2af9d87c2b7611fe9d5bdf

Success!

$ blockchain_go send -from Ivan -to Helen -amount 2
000000a2edf94334b1d94f98d22d7e4c973261660397dc7340464f7959a7a9aa

Success!
Now, Helen’s coins are locked in two outputs: one from Pedro and one from Ivan. Let’s send them to someone else:

$ blockchain_go send -from Helen -to Rachel -amount 3
000000c58136cffa669e767b8f881d16e2ede3974d71df43058baaf8c069f1a0

Success!

$ blockchain_go getbalance -address Ivan
Balance of 'Ivan': 2

$ blockchain_go getbalance -address Pedro
Balance of 'Pedro': 4

$ blockchain_go getbalance -address Helen
Balance of 'Helen': 1

$ blockchain_go getbalance -address Rachel
Balance of 'Rachel': 3
Looks fine! Now let’s test a failure:

$ blockchain_go send -from Pedro -to Ivan -amount 5
panic: ERROR: Not enough funds

$ blockchain_go getbalance -address Pedro
Balance of 'Pedro': 4

$ blockchain_go getbalance -address Ivan
Balance of 'Ivan': 2
Conclusion
Phew! It wasn’t easy, but we have transactions now! Although, some key features of a Bitcoin-like cryptocurrency are missing:

Addresses. We don’t have real, private key based addresses yet.
Rewards. Mining blocks is absolutely not profitable!
UTXO set. Getting balance requires scanning the whole blockchain, which can take very long time when there are many and many blocks. Also, it can take a lot of time if we want to validate later transactions. UTXO set is intended to solve these problems and make operations with transactions fast.
Mempool. This is where transactions are stored before being packed in a block. In our current implementation, a block contains only one transaction, and this is quite inefficient.

Sort:  

Hi! I am a robot. I just upvoted you! I found similar content that readers might be interested in:
https://jeiwan.cc/posts/building-blockchain-in-go-part-4/

这是什么

区块链原理的讲解。The principle of blockchain.bitcoin,eth,EOS...

这应该是一部分程式吧?
我外行,看不懂!😌

哈哈,关于区块链一定要多学习的.