Repository
What Will I Learn?
- You will learn about Blockchain Transactions
- You will learn about the difference between Coinbase transactions and Normal Transactions
- You will learn about PubKeys and Sigs
- You will learn how to create Inputs and Outputs and how they relate
- You will learn about Labels in Go
- You will learn how Transactions and Tokens work in a Blockchain
Requirements
System Requirements:
Operating System:
- FreeBSD 10.3 or later
- Linux 2.6.23 or later with glibc
- macOS 10.10 or later
- Windows 7, Server 2008R2 or later
Required Knowledge
- A little understanding of the Go programming language
- Go installed on your computer
- A text editor or IDE like Gogland (VS Code used)
Resources for Go and this Project:
- Awesome Go Github: https://github.com/avelino/awesome-go
- Golang Installation Page: https://golang.org/dl/
- Golang Home Page: https://golang.org/
- Golang Documentation Page: https://golang.org/doc/
- Documentation about the
Biglibrary: https://golang.org/pkg/math/big/ - Documentation about the
Byteslibrary: https://golang.org/pkg/bytes/
Credits/Sources:
- Go Logo: https://golang.org/
- LevelDB Logo: http://leveldb.org/
Difficulty
- Advanced
Description
Up until now, the blockchain that we've been building has been made up of blocks filled with non-descriptive data. The data fields used a byte format so it was possible to insert almost anything into a block. The problem with this however, is that it completely ignores one of the most important and innovative concepts that was invented with Blockchain technology; Transactions. In this fourth, Golang blockchain tutorial, we finally start to build out a somewhat primitive transaction system.
Describing Everything with Inputs and Outputs
Transactions can seem pretty foreign at first since most everything is described through inputs and outputs. A blockchain is a public and open database and it is insecure to store anything that relates to account owners. This includes account balances, addresses, coins, and senders/receivers. All of these concepts can be described with a set of Transactions however which are made up of inputs and outputs.
An input transaction references an output transaction from a previously made transaction. In the case of a coinbase, the input has no reference. In our implementation of Inputs, we store the referenced transaction's ID and the referenced output's index in two of the output's fields. We then connect the input to the referenced output through a Sig field which contains an arbitrary account string.
Transaction Outputs are where the Tokens actually reside inside of a transaction. These tokens are locked inside of the outputs with an encrypted public key. This PubKey is what identifies who can control the tokens in the Output. In our implementation, the PubKey is like the Sig field in that it is just a arbitrary account string.
Injecting Tokens with a Coinbase
This idea of Inputs and Outputs is a sort of "Chicken/Egg" problem; inputs create outputs and outputs are what make inputs work properly. Also, inputs and outputs form a self contained system; it is impossible to inject or remove data from with only simple transactions. Every blockchain has a starting line in the form of a genesis block which contains a Coinbase Transaction.
The Coinbase is the first transaction of a blockchain. One is also added to a block when a miner starts to mine it. This Coinbase Transaction doesn't need to reference previously existing outputs; instead, it is able to inject tokens into the system by creating outputs. When the miner mines a block with a coinbase, they receive the coinbase's reward. The above photo is the first coinbase transaction on the bitcoin network.
For our implementation of the Coinbase transaction, we have exactly one input and one output. Since the input doesn't reference a previous output, the values can be consistent. Also, the Sig can be an arbitrary set of data. For the output, we add in the reward amount and then assign that reward to the miner's account. In this way, we can increase the amount of tokens in the blockchain's economy and reward the miners for powering blockchain.
The Source Code for this video may be found here: https://github.com/tensor-programming/golang-blockchain/tree/part_4
Video Tutorial
Curriculum
- Building a Blockchain with Go - Go Modules and a Basic Blockchain - Part 1
- Building a Blockchain with Go - Refactor and Proof of Work - Part 2
- Building a Blockchain with Go - Persistence and Command Line - Part 3