Building a Blockchain with Go - Adding Primitive Transactions - Part 4
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
Big
library: https://golang.org/pkg/math/big/ - Documentation about the
Bytes
library: 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
I thank you for your contribution. Here are my thoughts. Note that, my thoughts are my personal ideas on your post and they are not directly related to the review and scoring unlike the answers I gave in the questionnaire;
Content
Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.
To view those questions and the relevant answers related to your post, click here.
Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]
Thank you for your review, @yokunjon! Keep up the good work!
I sort of dropped the ball on this particular part of the tutorial and that is why I imagine that you had difficulty understanding the topics (And yes, pictures would have helped for sure). I had a set amount of stuff that I wanted to do for this video and it ended up being too much. I don't like pushing videos further then 20 minutes and this one came out to be 30 minutes long and so I had to cut many of the smaller explanations out of the video to stop it from being longer.
Anyhow, thank you for moderating my contribution. The next one will be much shorter but it will be easier to understand and more detailed. I do appreciate the constructive criticism; if you end up moderating more of my stuff, feel free to leave more.
Cheers,
Tensor
This post has been voted on by the SteemSTEM curation team and voting trail in collaboration with @curie.
If you appreciate the work we are doing then consider voting both projects for witness by selecting stem.witness and curie!
For additional information please join us on the SteemSTEM discord and to get to know the rest of the community!
Hi, @tensor!
You just got a 7.67% upvote from SteemPlus!
To get higher upvotes, earn more SteemPlus Points (SPP). On your Steemit wallet, check your SPP balance and click on "How to earn SPP?" to find out all the ways to earn.
If you're not using SteemPlus yet, please check our last posts in here to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.
Hi @tensor!
Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server
Hey, @tensor!
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!
Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).
Want to chat? Join us on Discord https://discord.gg/h52nFrV.
Vote for Utopian Witness!
Congratulations @tensor! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :
Click here to view your Board of Honor
If you no longer want to receive notifications, reply to this comment with the word
STOP
Do not miss the last post from @steemitboard: