Building a Blockchain with Go - Adding Primitive Transactions - Part 4

Words
701
Reading
4 min
8y
Listen to this post
~5 min

golang.jpg

Repository

https://github.com/golang/go

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:

Credits/Sources:

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.

tx.png

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.

tx-inp.png

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.

tx-in.png

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.

first-cb.png

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.

cb-tx.png

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

Proof of Work Done

https://github.com/tensor-programming

Building a Blockchain with Go - Adding Primitive Transactions - Par... | Ecency