create account

Building a Blockchain with Go - Part 10 - Finishing Up by tensor

View this thread on: hive.blogpeakd.comecency.com
· @tensor ·
$36.27
Building a Blockchain with Go - Part 10 - Finishing Up
![golang.jpg](https://cdn.steemitimages.com/DQmdE7SfppXmLzLBWEn63VP1pdtREsGyxU3mzfvd3ybW5TF/golang.jpg)

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

#### What Will I Learn?

- You will learn how to implement the network library into the blockchain
- You will learn how to properly close BadgerDB should your program crash
- You will learn how to truncate the BadgerDB database and remove a Lock file
- You will learn how to create multiple files and directories for multiple instances of a program
- You will learn how to check the diff of a blockchain against another node
- You will learn about block height and how it works to sync multiple blockchains

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


- Information about Elliptic Curves: http://mathworld.wolfram.com/EllipticCurve.html
- Information about Base58: https://en.bitcoinwiki.org/wiki/Base58
- BadgerDB Documentation: https://godoc.org/github.com/dgraph-io/badger
- MermaidJS: https://mermaidjs.github.io/

#### Credits/Sources: 

- Go Logo:  https://golang.org/

#### Difficulty


- Advanced 


#### Description
In this final tutorial video, we finish up the blockchain and produce a program that is near production level.  By combining the network module with the code for our Command line interface, wallet module and blockchain module, this application now is able to simulate a blockchain network in real time on a single local machine.  This also includes the creation of multiple databases and wallet files for each instance of the application along with the ability to quickly scan a blockchain for changes.  We also look at how we can protect our BadgerDB against corruption should the program terminate prematurely. 


##### Building a Blockchain Network on a Single Machine

In our network module, we've already specified the main way that we want our programs to talk with one another.  The RPC or remote procedure calls are passed around via TCP with each instance of the client being represented by localhost machines listening on different ports.  Using this structure, we can modify the existing Blockchain, Wallet and Command line interface code to account for multiple different local programs each with their own Node ID.  This also means that we are able to run a miner, SPV and a full node all on the same computer. 

![terminals.png](https://cdn.steemitimages.com/DQmVnBxoLn2KoVMuoFV5gqvsZg9dYXL9FmVWp5zbKRfWBRw/terminals.png)

In the image above are three terminals each running an instance of this blockchain.  The top left terminal is connected to the main central node of the application.  This node acts as a full node on the blockchain network; it preforms routing and tries to maintain the most consistent copy of the blockchain.  The terminal on the bottom left is our wallet or SPV node.  This node is responsible for sending transactions to the network.  The terminal on the far right is the miner node of the network.  When enough transactions are passed from the wallet node to the full node and then to the miner node, the miner node verifies the transactions and then runs the proof of work algorithm to create a new block.    This new block is then sent to the main node which then proliferates the changes out to the other two nodes.

##### Network Simplification 

This network system is a simplified version of a production blockchain.  The central node acts somewhat like a server which goes against the idea of a decentralized architecture.  Also, the SPV node and the miner node contain full copies of the blockchain.  This was mainly done because the nodes do not have a message to discover each other on the network.  

![simplified-network.png](https://cdn.steemitimages.com/DQmbykWddsD9LhLWJkYwdGVzrh49mmSaKSpxBhn942Sbmhh/simplified-network.png)

In a real blockchain, there can be multiple instances of the same chain which have different lengths or heights.  In our example, we just take the new blocks and add them to our blockchain unconditionally.  To save computing power, our network does not validate the block before adding it to the chain. The image above highlights the main differences between our version and a production level version.  Each node routes through the Full Node where as in a real example, each node will communicate with one another independently.  

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


#### Video Tutorial

<iframe width="560" height="315" src="https://www.youtube.com/embed/YkMF-eIJV2c" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" 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)
- [Building a Blockchain with Go - Refactor and Proof of Work - Part 2](https://steemit.com/@tensor/building-a-blockchain-with-go---refactor-and-proof-of-work---part-2)
- [Building a Blockchain with Go - Persistence and Command Line - Part 3](https://steemit.com/@tensor/building-a-blockchain-with-go---persistence-and-command-line---part-3)
- [Building a Blockchain with Go - Adding Primitive Transactions - Part 4](https://steemit.com/utopian-io/@tensor/building-a-blockchain-with-go---adding-primitive-transactions---part-4)
- [Building a Blockchain with Go - Part 5 - Building a Basic Wallet Module](https://steemit.com/utopian-io/@tensor/building-a-blockchain-in-golang---part-5---building-a-basic-wallet-module)
- [Building a Blockchain with Go - Part 6 - Adding Digital Signatures](https://steemit.com/utopian-io/@tensor/building-a-blockchain-with-go---part-6---adding-digital-signatures)
- [Building a Blockchain with Go - Part 7 - The UTXO Set and BadgerDB Iterators](https://steemit.com/@tensor/building-a-blockchain-with-go---part-7---the-utxo-set-and-badgerdb-iterators)
- [Building a Blockchain with Go - Part 8 - The Merkle Tree](https://steemit.com/@tensor/building-a-blockchain-with-go---part-8---the-merkle-tree)
- [Building a Blockchain with Go - Part 9 - The Network Module](https://steemit.com/@tensor/building-a-blockchain-with-go---part-9---the-network-module)

#### Proof of Work Done
https://github.com/tensor-programming
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 186 others
properties (23)
authortensor
permlinkbuilding-a-blockchain-with-go---part-10-----finishing-up
categoryutopian-io
json_metadata{"tags":["utopian-io","video-tutorials","steemstem","programming","blockchain"],"app":"steem-plus-app"}
created2018-11-18 02:15:45
last_update2018-11-18 02:15:45
depth0
children7
last_payout2018-11-25 02:15:45
cashout_time1969-12-31 23:59:59
total_payout_value27.231 HBD
curator_payout_value9.039 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6,985
author_reputation87,856,203,149,624
root_title"Building a Blockchain with Go - Part 10 - Finishing Up"
beneficiaries
0.
accountsteemplus-pay
weight100
1.
accountutopian.pay
weight500
max_accepted_payout100,000.000 HBD
percent_hbd10,000
post_id75,472,453
net_rshares60,987,407,406,329
author_curate_reward""
vote details (250)
@rosatravels ·
$6.15
Hi @Tensor

I get to see the final video on this series of how the final programming goes.  Good for you for having the stamina to work through the entire series until the end.  

This series will be good for learning to learn how to build a blockchain with Go and now they have the step by step outline and flowcharts to follow.

Thank you again for taking the time to outline the fundamental and detail steps for learners.

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/121111113).

---- 
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 (23)
authorrosatravels
permlinkre-tensor-building-a-blockchain-with-go---part-10-----finishing-up-20181119t152536663z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["tensor"],"links":["https://join.utopian.io/guidelines","https://review.utopian.io/result/9/121111113","https://support.utopian.io/","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2018-11-19 15:25:36
last_update2018-11-19 15:25:36
depth1
children2
last_payout2018-11-26 15:25:36
cashout_time1969-12-31 23:59:59
total_payout_value4.657 HBD
curator_payout_value1.497 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length917
author_reputation422,827,447,688,168
root_title"Building a Blockchain with Go - Part 10 - Finishing Up"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,561,485
net_rshares9,780,867,175,880
author_curate_reward""
vote details (12)
@tensor ·
Thanks again @rosatravels.  I do enjoy making these videos so its not hard to sit through the making of an entire series like this.
properties (22)
authortensor
permlinkre-rosatravels-re-tensor-building-a-blockchain-with-go---part-10-----finishing-up-20181119t193239779z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["rosatravels"],"app":"steemit/0.1"}
created2018-11-19 19:32:36
last_update2018-11-19 19:32:36
depth2
children0
last_payout2018-11-26 19:32:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length131
author_reputation87,856,203,149,624
root_title"Building a Blockchain with Go - Part 10 - Finishing Up"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,573,015
net_rshares0
@utopian-io ·
Thank you for your review, @rosatravels! Keep up the good work!
properties (22)
authorutopian-io
permlinkre-re-tensor-building-a-blockchain-with-go---part-10-----finishing-up-20181119t152536663z-20181122t040550z
categoryutopian-io
json_metadata"{"app": "beem/0.20.9"}"
created2018-11-22 04:05:51
last_update2018-11-22 04:05:51
depth2
children0
last_payout2018-11-29 04:05:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length63
author_reputation152,955,367,999,756
root_title"Building a Blockchain with Go - Part 10 - Finishing Up"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,710,449
net_rshares0
@steem-plus ·
SteemPlus upvote
Hi, @tensor!

You just got a **6.71%** 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 (22)
authorsteem-plus
permlinkbuilding-a-blockchain-with-go---part-10-----finishing-up---vote-steemplus
categoryutopian-io
json_metadata{}
created2018-11-18 06:24:00
last_update2018-11-18 06:24:00
depth1
children0
last_payout2018-11-25 06:24:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length433
author_reputation247,952,188,232,400
root_title"Building a Blockchain with Go - Part 10 - Finishing Up"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,480,207
net_rshares0
@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 (22)
authorsteem-ua
permlinkre-building-a-blockchain-with-go---part-10-----finishing-up-20181119t153748z
categoryutopian-io
json_metadata"{"app": "beem/0.20.9"}"
created2018-11-19 15:37:51
last_update2018-11-19 15:37:51
depth1
children0
last_payout2018-11-26 15:37:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length285
author_reputation23,214,230,978,060
root_title"Building a Blockchain with Go - Part 10 - Finishing Up"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,562,238
net_rshares0
@steemstem ·
re-tensor-building-a-blockchain-with-go---part-10-----finishing-up-20181121t032909066z
<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 (22)
authorsteemstem
permlinkre-tensor-building-a-blockchain-with-go---part-10-----finishing-up-20181121t032909066z
categoryutopian-io
json_metadata{"app":"bloguable-bot"}
created2018-11-21 03:29:12
last_update2018-11-21 03:29:12
depth1
children0
last_payout2018-11-28 03:29:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length800
author_reputation262,017,435,115,313
root_title"Building a Blockchain with Go - Part 10 - Finishing Up"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,652,723
net_rshares0
@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 (22)
authorutopian-io
permlinkre-building-a-blockchain-with-go---part-10-----finishing-up-20181120t030802z
categoryutopian-io
json_metadata"{"app": "beem/0.20.9"}"
created2018-11-20 03:08:03
last_update2018-11-20 03:08:03
depth1
children0
last_payout2018-11-27 03:08:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length588
author_reputation152,955,367,999,756
root_title"Building a Blockchain with Go - Part 10 - Finishing Up"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,591,287
net_rshares0