The source-code for: "Build a Blockchain from Scratch in Go" eBook.
📚 Get the eBook from: https://gumroad.com/l/build-a-blockchain-from-scratch-in-go
- TBB Training Ledger
- Introduction
- Usage
- Start
- Finish
You will build all of this from scratch! Follow the next 4 steps and you will be connected to the TBB testing blockchain network in 30 seconds.
Go ahead. Try it right now! 💘
curl https://node.tbb.web3.coach/balances/list
In case you have the JQ - CLI JSON processor formatter. installed.
curl https://node.tbb.web3.coach/balances/list | jq
{
"block_hash": "000000a9d18730c133869d175a886d576df5675e0e73900bf072c59047b9d734",
"balances": {
"0x09ee50f2f37fcba1845de6fe5c762e83e65e755c": 1000095,
"0x22ba1f80452e6220c7cc6ea2d1e3eeddac5f694a": 5
}
}
Together with all students, we will use this custom build blockchain for educational purposes.
wget "https://github.com/web3coach/the-blockchain-bar/releases/download/1.3.2-alpha/tbb-linux-amd64" -O /usr/local/bin/tbb
chmod a+x /usr/local/bin/tbb
tbb version
> Version: 1.3.2-alpha 564d63 TX Ga
tbb run --datadir=$HOME/.tbb --ip=127.0.0.1 --port=8081 --disable-ssl
Your blockchain database will synchronize with rest of the students.
Launching TBB node and its HTTP API...
Listening on: 127.0.0.1:8081
Blockchain state:
- height: 0
- hash: 0000000000000000000000000000000000000000000000000000000000000000
Searching for new Peers and their Blocks and Peers: 'node.tbb.web3.coach'
Found 1 new blocks from Peer node.tbb.web3.coach
Importing blocks from Peer node.tbb.web3.coach...
Persisting new Block to disk:
{"hash":"000000a9d18730c133869d175a886d576df5675e0e73900bf072c59047b9d734","block":{"header":{"parent":"0000000000000000000000000000000000000000000000000000000000000000","number":0,"nonce":1925346453,"time":1590684713,"miner":"0x09ee50f2f37fcba1845de6fe5c762e83e65e755c"},"payload":[{"from":"0x09ee50f2f37fcba1845de6fe5c762e83e65e755c","to":"0x22ba1f80452e6220c7cc6ea2d1e3eeddac5f694a","value":5,"nonce":1,"data":"","time":1590684702,"signature":"0JE1yEoA3gwIiTj5ayanUZfo5ZnN7kHIRQPOw8/OZIRYWjbvbMA7vWdPgoqxnhFGiTH7FIbjCQJ25fQlvMvmPwA="}]}}
Searching for new Peers, Blocks: 'node.tbb.web3.coach'
Searching for new Peers, Blocks: 'node.tbb.web3.coach'
This time from your OWN, fully synchronized blockchain node running directly on your computer.
curl -X GET http://127.0.0.1:8081/balances/list | jq
{
"block_hash": "000000a9d18730c133869d175a886d576df5675e0e73900bf072c59047b9d734",
"balances": {
"0x09ee50f2f37fcba1845de6fe5c762e83e65e755c": 1000095,
"0x22ba1f80452e6220c7cc6ea2d1e3eeddac5f694a": 5
}
}
⭐ All of this and much more, you will build from scratch!
Hi 👋,
With Web 3.0 and blockchain becoming more mainstream every day, do you know what blockchain is? Do you know its technical advantages and use-cases?
The goal of this tutorial is to introduce blockchain technology from a technical perspective by building one from scratch.
Forget everything you've heard about blockchain from social media. Now, you will build a blockchain system from ground zero to really understand the ins and outs of this peer-to-peer, distributed technology.
Afterwards, make your own mind up about its future, advantages and shortcomings.
Spoiler alert: you will fall in love with programming blockchain software. 😈
You will follow the story of a software developer who is looking to revolutionize his local bar by implementing blockchain technology for its payment system.
Although blockchain has several undeniable use-cases, at the moment, the number one application is payments. This is because banks are still running on an inefficient, 40 year old infrastructure powered by CSV files and FTP.
The story comes with a lot of fun and intriguing facts about the overall blockchain ecosystem and different protocols such as Bitcoin, Ethereum and XRP.
Chapter by chapter, you will build a full peer-to-peer, autonomous training blockchain system in Go and learn all standard blockchain components!
Note: This codebase is only for educational (training, learning) purposes. Do not use this code in production.
You start with 0 lines of code and end-up with 13+ branches with complete executable source-code.
PS: Don't worry if anything on the screen makes sense yet, it will once you go chapter by chapter; release by release.
No boring theory. Only modern practices.
From diagrams of mining algorithms to actual, implemented and working crypto wallets for storing the mined tokens and all other fundamental components that make blockchain special.
You will test your cryptographic functions, a Bitcoin's like Proof of Work mining algorithm and other key components.
Every eBook chapter has a dedicated branch where you can experiment with the code first-hand.
git pull --all
git branch
> c1_genesis_json
> c2_db_changes_txt
> c3_state_blockchain_component
> c4_caesar_transfer
> c5_broken_trust
> c6_immutable_hash
> c7_blockchain_programming_model
> c8_transparent_db
> c9_tango
> c10_peer_sync
> c11_consensus
> c12_crypto
> c13_training_network
> c14_why_transaction_costs_gas
- Download the eBook from: https://gumroad.com/l/build-a-blockchain-from-scratch-in-go
- Open the book at Chapter 1
- Checkout the first chapter's branch
c1_genesis_json
git pull --all
git checkout c1_genesis_json
go install ./cmd/...
tbb help
tbb run --help
Launches the TBB node and its HTTP API.
Usage:
tbb run [flags]
Flags:
--bootstrap-account string default bootstrap Web3Coach's Genesis account with 1M TBB tokens (default "0x09ee50f2f37fcba1845de6fe5c762e83e65e755c")
--bootstrap-ip string default bootstrap Web3Coach's server to interconnect peers (default "node.tbb.web3.coach")
--bootstrap-port uint default bootstrap Web3Coach's server port to interconnect peers (default 443)
--datadir string Absolute path to your node's data dir where the DB will be/is stored
--disable-ssl should the HTTP API SSL certificate be disabled? (default false)
-h, --help help for run
--ip string your node's public IP to communication with other peers (default "127.0.0.1")
--miner string your node's miner account to receive the block rewards (default "0x0000000000000000000000000000000000000000")
--port uint your node's public HTTP port for communication with other peers (configurable if SSL is disabled) (default 443)
If you are running the node on your localhost, disable the SSL with --disable-ssl
flag.
tbb version
> Version: 1.3.2-alpha 564d63 TX Ga
tbb run --datadir=$HOME/.tbb --ip=127.0.0.1 --port=8081 --miner=0x_YOUR_WALLET_ACCOUNT --disable-ssl
tbb run --datadir=$HOME/.tbb_boostrap --ip=127.0.0.1 --port=8080 --bootstrap-ip=127.0.0.1 --bootstrap-port=8080 --disable-ssl
tbb run --datadir=$HOME/.tbb --ip=127.0.0.1 --port=8081 --bootstrap-ip=127.0.0.1 --bootstrap-port=8080 --disable-ssl
tbb wallet new-account --datadir=$HOME/.tbb
The default node's HTTP port is 443. The SSL certificate is generated automatically as long as the DNS A/AAAA records point at your server.
Example how the official TBB bootstrap node is launched. Customize the --datadir
, --miner
, and --ip
values to match your server.
/usr/local/bin/tbb run --datadir=/home/ec2-user/.tbb --miner=0x09ee50f2f37fcba1845de6fe5c762e83e65e755c --ip=node.tbb.web3.coach --port=443 [email protected] --bootstrap-ip=node.tbb.web3.coach --bootstrap-port=443 --bootstrap-account=0x09ee50f2f37fcba1845de6fe5c762e83e65e755c
curl http://localhost:8080/balances/list | jq
curl --location --request POST 'http://localhost:8080/tx/add' \
--header 'Content-Type: application/json' \
--data-raw '{
"from": "0x22ba1f80452e6220c7cc6ea2d1e3eeddac5f694a",
"from_pwd": "security123",
"to": "0x6fdc0d8d15ae6b4ebf45c52fd2aafbcbb19a65c8",
"value": 100
}'
curl http://localhost:8080/node/status | jq
Run all tests with verbosity but one at a time, without timeout, to avoid ports collisions:
go test -v -p=1 -timeout=0 ./...
Run an individual test:
go test -timeout=0 ./node -test.v -test.run ^TestNode_Mining$
Note: Majority are integration tests and take time. Expect the test suite to finish in ~30 mins.
📚 Get the eBook from: https://gumroad.com/l/build-a-blockchain-from-scratch-in-go
Write a tweet and let me know how did you like this book! Tag me in it @Web3Coach and include your account address 0xYOUR_ADDRESS.
See you on Twitter - @Web3Coach.
The Blockchain Bar repository, the tbb
binary and Build a Blockchain from Scratch in Go
eBook is for learning, educational purposes. The codebase is NOT ready for production. The components are purposefully simplified to don't overwhelm new blockchain students, but complex enough to teach you how blockchains work under the hood.
The Blockchain Bar library (i.e. all code outside of the cmd
directory) is licensed under the
GNU Lesser General Public License v3.0,
also included in our repository in the COPYING.LESSER
file.
The Blockchain Bar binaries (i.e. all code inside of the cmd
directory) is licensed under the
GNU General Public License v3.0, also
included in our repository in the COPYING
file.