create account

Building a Blockchain with Go - Refactor and Proof of Work - Part 2 by tensor

View this thread on steemit.com
· @tensor ·
$56.68
Building a Blockchain with Go - Refactor and Proof of Work - Part 2
![golang.jpg](https://cdn.steemitimages.com/DQmdE7SfppXmLzLBWEn63VP1pdtREsGyxU3mzfvd3ybW5TF/golang.jpg)

#### Repository
https://github.com/golang/go

#### What Will I Learn?

- You will learn how to create multiple modules for one Go Project
- You will learn how implement Proof Of Work
- You will learn what difficulty is in a PoW consensus algorithm
- You will learn about the Nonce
- You will learn about shifting bytes

#### 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/

#### Difficulty


- Intermediate 


#### Description

In this Golang tutorial, we take a look at how to build out a Proof of Work consensus algorithm for the prototype blockchain that was built in part one.  This PoW algorithm mirrors the original Bitcoin Hashcash algorithm. It also implements a validation function which checks to see that the work was properly done to sign a block.  The difficulty in this PoW algorithm is static for now, but the developer can select to make the work harder or easier.  We also discuss shifting bytes in hashes, using big integers to create a "infinite" loop, the nonce and some other topics. 

#### Refactoring a Go Project into multiple modules

In the last tutorial we discussed building independent modules outside of the GOPATH.  Go supports applications that are made up of multiple module namespaces.  This can be helpful for organization as well as execution purposes.  In a windows console, when you go to execute a multi-file Go program, you have to reference each file so that the `go run` command links the files together.  If you create a new directory with a new module name and reference this module from one of your other Go files, you can execute the main package and the library package will automatically be built as a dependency.   In this project, we make use of this convention to create a blockchain module which is separate from the main entry-point module. 


#### Reaching Consensus to Sign Blocks

One of the most important concepts of a blockchain is security.  Because the chain is distributed across multiple machines which could potentially be malicious, a consensus algorithm is needed.  This algorithm is what decides which blocks contain valid data and which blocks do not.  In the cryptocurrency world, there are many different Consensus protocols: Proof of Work, Proof of Stake, Proof of Brain, Delegated Proof of Stake, Proof of Authority etc.  In this tutorial, we build a Proof of Work system to allow the machine to sign the blocks.  

![pow.png](https://cdn.steemitimages.com/DQmfDdGA8ao1o9sriFZyiDP2amuvZ8EoMkzhh5vpykYLikf/pow.png)

The main data structure used in our Proof of Work implementation is this Proof of Work structure which binds the block to a target.  The block contains data and a hash pointing to the last block and the target is a numerical representation of a set of requirements that we want apply to valid hashes.  In this case, if the big integer of the hash is less then the target, then the block is signed with that hash.  Above is this PoW data structure.

#### Working to Build Blocks with Difficulty  

Our PoW implementation also features a static difficulty.  This difficulty is what allows the blockchain to devise the target for the PoW struct.  The higher the difficulty, the more computational power is required to sign/create the block.  In a normal PoW algorithm, this difficulty is not a static number and is increased according to its own algorithm.

In a very general sense, the difficulty is a representation of the number of bits that we want to shift the hashes by.  A SHA-256 hash is 256 bits in length so we subtract this difficulty from 256. The hexadecimal of this target is represented by a bunch of zeroes and a single one based on this difficulty.  

![target-hash.png](https://cdn.steemitimages.com/DQmNjbeWYpYVR4qMhvmwkmbpeTtyJ2DYp3XYmRfjP3X9EEf/target-hash.png)

In the image above, there are three different hashes. The hash in the middle is a representation of the target.  The one on the top is a hash that is not valid to sign a block in our chain.  The bottom hash however, is valid to sign our chain.  This is because the hash above is greater than the middle hash and the hash below is less than the middle hash.  


#### Using the Nonce to Validate the Work

The work itself is fairly expensive and hard.  This is required for security reasons; the longer it takes to compute the hash for a block, the harder it is to tamper with the values inside of the blockchain.  If an individual was to create fake data and put it into a block, they would then have to sign that block and every block after to make that information valid. 

![validate.png](https://cdn.steemitimages.com/DQmVRMDT7oBNg8czZrbVLbvBZp9B4SiPBqssnFZTGjrUij8/validate.png)

On the other hand, the validation of this work is fairly simple. While a miner could try to mine a few million hashes to sign a single block, a validation function only needs the signed hash and the nonce to validate the block.  In our example, the validation function is the same algorithm that is used for the PoW. The nonce is already known for each block on the chain so this validation function only needs to execute the PoW algorithm a single time.    


The Source Code for this video may be found here: https://github.com/tensor-programming/golang-blockchain/tree/part_2
   


#### Video Tutorial
<iframe width="560" height="315" src="https://www.youtube.com/embed/aE4eDTUAE70" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>


### Curriculum 

- [Building a Blockchain with Go - Go Modules and a Basic Blockchain - Part 1](https://steemit.com/@tensor/building-a-blockchain-with-go---go-modules-and-a-basic-blockchain---part-1)



#### Proof of Work Done
https://github.com/tensor-programming


πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 112 others
properties (37)
authortensor
permlinkbuilding-a-blockchain-with-go---refactor-and-proof-of-work---part-2
categoryutopian-io
json_metadata{"tags":["utopian-io","video-tutorials","steemstem","programming","blockchain"],"app":"steem-plus-app"}
last_update2018-10-25 02:14:39
created2018-10-25 02:14:39
active2018-10-29 22:10:15
last_payout2018-11-01 02:14:39
depth0
children8
net_rshares0
vote_rshares0
children_abs_rshares0
cashout_time1969-12-31 23:59:59
max_cashout_time1969-12-31 23:59:59
total_vote_weight0
reward_weight10,000
total_payout_value42.522 SBD
curator_payout_value14.162 SBD
author_rewards54,377
net_votes166
root_authortensor
root_permlinkbuilding-a-blockchain-with-go---refactor-and-proof-of-work---part-2
max_accepted_payout100,000.000 SBD
percent_steem_dollars10,000
allow_repliestrue
allow_votestrue
allow_curation_rewardstrue
beneficiaries
0.
accountsteemplus-pay
weight100
1.
accountutopian.pay
weight500
root_title"Building a Blockchain with Go - Refactor and Proof of Work - Part 2"
pending_payout_value0.000 SBD
total_pending_payout_value0.000 STEEM
author_reputation57,406,415,315,641
promoted0.000 SBD
body_length0
reblogged_by[]
author_curate_reward""
vote details (176)
@steem-plus ·
SteemPlus upvote
Hi, @tensor!

You just got a **6.66%** 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](https://steemit.com/@steem-plus) to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.
properties (36)
authorsteem-plus
permlinkbuilding-a-blockchain-with-go---refactor-and-proof-of-work---part-2---vote-steemplus
categoryutopian-io
json_metadata{}
last_update2018-10-25 12:03:15
created2018-10-25 12:03:15
active2018-10-25 12:03:15
last_payout2018-11-01 12:03:15
depth1
children0
net_rshares0
vote_rshares0
children_abs_rshares0
cashout_time1969-12-31 23:59:59
max_cashout_time1969-12-31 23:59:59
total_vote_weight0
reward_weight10,000
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
author_rewards0
net_votes0
root_authortensor
root_permlinkbuilding-a-blockchain-with-go---refactor-and-proof-of-work---part-2
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
allow_repliestrue
allow_votestrue
allow_curation_rewardstrue
beneficiaries[]
root_title"Building a Blockchain with Go - Refactor and Proof of Work - Part 2"
pending_payout_value0.000 SBD
total_pending_payout_value0.000 STEEM
author_reputation75,630,936,041,993
promoted0.000 SBD
body_length0
reblogged_by[]
@rosatravels ·
$7.99
Hello @Tensor

Again, another great video tutorial of building a block chain of Go Refractor.

Your teaching is well structured and straight to the point such that it is very easy to follow along from one step to another.

I like your easy going style teaching approach, as it makes learning programming less threatening.

I look forward to your next contribution.

Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/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](https://review.utopian.io/result/9/111211113).

---- 
Need help? Write a ticket on https://support.utopian.io/. 
Chat with us on [Discord](https://discord.gg/uTyJkNm). 
[[utopian-moderator]](https://join.utopian.io/)
πŸ‘  , , , , , , , , , ,
properties (37)
authorrosatravels
permlinkre-tensor-building-a-blockchain-with-go---refactor-and-proof-of-work---part-2-20181025t144044289z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["tensor"],"links":["https://join.utopian.io/guidelines","https://review.utopian.io/result/9/111211113","https://support.utopian.io/","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
last_update2018-10-25 14:40:45
created2018-10-25 14:40:45
active2018-10-29 04:10:24
last_payout2018-11-01 14:40:45
depth1
children2
net_rshares0
vote_rshares0
children_abs_rshares0
cashout_time1969-12-31 23:59:59
max_cashout_time1969-12-31 23:59:59
total_vote_weight0
reward_weight10,000
total_payout_value6.045 SBD
curator_payout_value1.946 SBD
author_rewards7,653
net_votes11
root_authortensor
root_permlinkbuilding-a-blockchain-with-go---refactor-and-proof-of-work---part-2
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
allow_repliestrue
allow_votestrue
allow_curation_rewardstrue
beneficiaries[]
root_title"Building a Blockchain with Go - Refactor and Proof of Work - Part 2"
pending_payout_value0.000 SBD
total_pending_payout_value0.000 STEEM
author_reputation119,521,640,653,383
promoted0.000 SBD
body_length0
reblogged_by[]
author_curate_reward""
vote details (11)
@tensor ·
Thanks for moderating my contribution.
properties (36)
authortensor
permlinkre-rosatravels-re-tensor-building-a-blockchain-with-go---refactor-and-proof-of-work---part-2-20181025t202639247z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
last_update2018-10-25 20:26:36
created2018-10-25 20:26:36
active2018-10-25 20:26:36
last_payout2018-11-01 20:26:36
depth2
children0
net_rshares0
vote_rshares0
children_abs_rshares0
cashout_time1969-12-31 23:59:59
max_cashout_time1969-12-31 23:59:59
total_vote_weight0
reward_weight10,000
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
author_rewards0
net_votes0
root_authortensor
root_permlinkbuilding-a-blockchain-with-go---refactor-and-proof-of-work---part-2
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
allow_repliestrue
allow_votestrue
allow_curation_rewardstrue
beneficiaries[]
root_title"Building a Blockchain with Go - Refactor and Proof of Work - Part 2"
pending_payout_value0.000 SBD
total_pending_payout_value0.000 STEEM
author_reputation57,406,415,315,641
promoted0.000 SBD
body_length0
reblogged_by[]
@utopian-io ·
Thank you for your review, @rosatravels!

So far this week you've reviewed 1 contributions. Keep up the good work!
properties (36)
authorutopian-io
permlinkre-re-tensor-building-a-blockchain-with-go---refactor-and-proof-of-work---part-2-20181025t144044289z-20181029t041023z
categoryutopian-io
json_metadata"{"app": "beem/0.20.9"}"
last_update2018-10-29 04:10:24
created2018-10-29 04:10:24
active2018-10-29 04:10:24
last_payout2018-11-05 04:10:24
depth2
children0
net_rshares0
vote_rshares0
children_abs_rshares0
cashout_time1969-12-31 23:59:59
max_cashout_time1969-12-31 23:59:59
total_vote_weight0
reward_weight10,000
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
author_rewards0
net_votes0
root_authortensor
root_permlinkbuilding-a-blockchain-with-go---refactor-and-proof-of-work---part-2
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
allow_repliestrue
allow_votestrue
allow_curation_rewardstrue
beneficiaries[]
root_title"Building a Blockchain with Go - Refactor and Proof of Work - Part 2"
pending_payout_value0.000 SBD
total_pending_payout_value0.000 STEEM
author_reputation113,905,643,290,058
promoted0.000 SBD
body_length0
reblogged_by[]
@steem-ua ·
#### 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](https://discord.gg/KpBNYGz)**
properties (36)
authorsteem-ua
permlinkre-building-a-blockchain-with-go---refactor-and-proof-of-work---part-2-20181025t150721z
categoryutopian-io
json_metadata"{"app": "beem/0.20.7"}"
last_update2018-10-25 15:07:21
created2018-10-25 15:07:21
active2018-10-25 15:07:21
last_payout2018-11-01 15:07:21
depth1
children0
net_rshares0
vote_rshares0
children_abs_rshares0
cashout_time1969-12-31 23:59:59
max_cashout_time1969-12-31 23:59:59
total_vote_weight0
reward_weight10,000
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
author_rewards0
net_votes0
root_authortensor
root_permlinkbuilding-a-blockchain-with-go---refactor-and-proof-of-work---part-2
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
allow_repliestrue
allow_votestrue
allow_curation_rewardstrue
beneficiaries[]
root_title"Building a Blockchain with Go - Refactor and Proof of Work - Part 2"
pending_payout_value0.000 SBD
total_pending_payout_value0.000 STEEM
author_reputation18,835,154,461,009
promoted0.000 SBD
body_length0
reblogged_by[]
@steemstem ·
re-tensor-building-a-blockchain-with-go---refactor-and-proof-of-work---part-2-20181027t152118133z
<div class='text-justify'> <div class='pull-left'> <br /> <center> <img width='125' src='https://i.postimg.cc/9FwhnG3w/steemstem_curie.png'> </center>  <br/> </div> <br /> <br /> 

 This post has been voted on by the **SteemSTEM** curation team and voting trail in collaboration with **@curie**. <br /> 
 If you appreciate the work we are doing then consider [voting](https://www.steemit.com/~witnesses) both projects for witness by selecting [**stem.witness**](https://steemconnect.com/sign/account_witness_vote?approve=1&witness=stem.witness) and [**curie**](https://steemconnect.com/sign/account_witness_vote?approve=1&witness=curie)! <br /> 
For additional information please join us on the [**SteemSTEM discord**]( https://discord.gg/BPARaqn) and to get to know the rest of the community! </div>
properties (36)
authorsteemstem
permlinkre-tensor-building-a-blockchain-with-go---refactor-and-proof-of-work---part-2-20181027t152118133z
categoryutopian-io
json_metadata{"app":"bloguable-bot"}
last_update2018-10-27 15:21:21
created2018-10-27 15:21:21
active2018-10-27 15:21:21
last_payout2018-11-03 15:21:21
depth1
children0
net_rshares0
vote_rshares0
children_abs_rshares0
cashout_time1969-12-31 23:59:59
max_cashout_time1969-12-31 23:59:59
total_vote_weight0
reward_weight10,000
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
author_rewards0
net_votes0
root_authortensor
root_permlinkbuilding-a-blockchain-with-go---refactor-and-proof-of-work---part-2
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
allow_repliestrue
allow_votestrue
allow_curation_rewardstrue
beneficiaries[]
root_title"Building a Blockchain with Go - Refactor and Proof of Work - Part 2"
pending_payout_value0.000 SBD
total_pending_payout_value0.000 STEEM
author_reputation144,863,620,742,780
promoted0.000 SBD
body_length0
reblogged_by[]
@andrescodec ·
I Am wrinting code in Go is amazing :)
properties (36)
authorandrescodec
permlinkre-tensor-building-a-blockchain-with-go---refactor-and-proof-of-work---part-2-20181027t160327875z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
last_update2018-10-27 16:03:33
created2018-10-27 16:03:33
active2018-10-27 16:03:33
last_payout2018-11-03 16:03:36
depth1
children0
net_rshares0
vote_rshares0
children_abs_rshares0
cashout_time1969-12-31 23:59:59
max_cashout_time1969-12-31 23:59:59
total_vote_weight0
reward_weight10,000
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
author_rewards0
net_votes0
root_authortensor
root_permlinkbuilding-a-blockchain-with-go---refactor-and-proof-of-work---part-2
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
allow_repliestrue
allow_votestrue
allow_curation_rewardstrue
beneficiaries[]
root_title"Building a Blockchain with Go - Refactor and Proof of Work - Part 2"
pending_payout_value0.000 SBD
total_pending_payout_value0.000 STEEM
author_reputation1,003,304,128
promoted0.000 SBD
body_length0
reblogged_by[]
@utopian-io ·
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](https://chrome.google.com/webstore/detail/steemplus/mjbkjgcplmaneajhcbegoffkedeankaj?hl=en) or [Steeditor](https://steeditor.app)).

**Want to chat? Join us on Discord https://discord.gg/h52nFrV.**

<a href='https://steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
properties (36)
authorutopian-io
permlinkre-building-a-blockchain-with-go---refactor-and-proof-of-work---part-2-20181029t221013z
categoryutopian-io
json_metadata"{"app": "beem/0.20.9"}"
last_update2018-10-29 22:10:15
created2018-10-29 22:10:15
active2018-10-29 22:10:15
last_payout2018-11-05 22:10:15
depth1
children0
net_rshares0
vote_rshares0
children_abs_rshares0
cashout_time1969-12-31 23:59:59
max_cashout_time1969-12-31 23:59:59
total_vote_weight0
reward_weight10,000
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
author_rewards0
net_votes0
root_authortensor
root_permlinkbuilding-a-blockchain-with-go---refactor-and-proof-of-work---part-2
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
allow_repliestrue
allow_votestrue
allow_curation_rewardstrue
beneficiaries[]
root_title"Building a Blockchain with Go - Refactor and Proof of Work - Part 2"
pending_payout_value0.000 SBD
total_pending_payout_value0.000 STEEM
author_reputation113,905,643,290,058
promoted0.000 SBD
body_length0
reblogged_by[]