Operating System:
Big library: https://golang.org/pkg/math/big/Bytes library: https://golang.org/pkg/bytes/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.
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.
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