Mining Digital Gold one Block at a Time?! * Don't Expect to Get Insanely Rich (Quick) * Tulips (like Blockchains) are Great and Gorgeous (and will Endure)
{ from: "Dutchgrown", to: "Vincent", what: "Tulip Bloemendaal Sunset", qty: 10 },
{ from: "Keukenhof", to: "Anne", what: "Tulip Semper Augustus", qty: 7 } )
b1 = Block.next( b0,
{ from: "Flowers", to: "Ruben", what: "Tulip Admiral van Eijck", qty: 5 },
{ from: "Vicent", to: "Anne", what: "Tulip Bloemendaal Sunset", qty: 3 },
{ from: "Anne", to: "Julia", what: "Tulip Semper Augustus", qty: 1 },
{ from: "Julia", to: "Luuk", what: "Tulip Semper Augustus", qty: 1 } )
...
(Source: blockchain_with_transactions.rb)
Awesome Blockchains
A collection about awesome blockchains - open distributed databases w/ crypto hashes incl. git ;-). Blockchains are the new tulips .
Contents:
- Frequently Asked Questions (F.A.Q.s) & Answers
- Do-It-Yourself (DIY) - Build Your Own Blockchain
- Python * Ruby * JavaScript * Java * Go
- Blockchain Articles
- Blockchain Books
- Blockchain (Lite) Crypto Hash Libraries
- Git, Git, Git - The Stupid Content Tracker with Crypto Hashes
- Classic Tulip Mania
- Awesome Awesomeness
What's News?
For blockchain books, see the new Best of Crypto Books page >>
The Open Blockchains Book of the Year 2020 Award Goes To...
Libra Shrugged: How Facebook Tried to Take Over the Money by David Gerard, November 2020, 182 Pages -- Introduction: Taking over the money ++ A user's guide to Libra ++ The genesis of Libra: Beller's blockchain ++ To launch a Libra: Let's start a crypto ++ Bitcoin: why Libra is like this ++ The Libra White Papers ++ Banking the unbanked ++ The Libra Reserve plan and economic stability ++ Libra, privacy and your digital identity ++ The regulators recoil in horror ++ David Marcus before the US House and Senate ++ July to September 2019: Libra runs the gauntlet ++ October 2019: Libra's bad month ++ Mark Zuckerberg before the US House ++ November 2019: The comedown ++ Central bank digital currencies ++ Epilogue: Libra 2.0: not dead yet ++ Appendix: 2010-2013: The rise and fall of Facebook Credits
For more about Diem (formerly Libra), see the Awesome Diem (formerly Libra) and Move page >>
For crypto quotes, see the new 100+ Best of Crypto Quotes - I HODL, you HODL, we HODL! - BREAKING: BITCOIN JUST BROKE $22 000! page >>
The Best Crypto Quote "Oracle Saying" of the Year 2020 Award Goes To...
SEC Investor Education:
- Don't understand an investment?
- Don't invest in it.
Yes, but what if there's only 21 million of it?
- Trolly McTrollface
Frequently Asked Questions (F.A.Q.s) & Answers
Q: What's a Blockchain?
A: A blockchain is a distributed database with a list (that is, chain) of records (that is, blocks) linked and secured by
digital fingerprints (that is, cryptho hashes).
Example from blockchain.rb:
[# @timestamp = 1637-09-15 20:52:38,
@data = "Genesis",
@previous_hash = "0000000000000000000000000000000000000000000000000000000000000000",
@hash = "edbd4e11e69bc399a9ccd8faaea44fb27410fe8e3023bb9462450a0a9c4caa1b">,
# @timestamp = 1637-09-15 21:02:38,
@data = "Transaction Data...",
@previous_hash = "edbd4e11e69bc399a9ccd8faaea44fb27410fe8e3023bb9462450a0a9c4caa1b",
@hash = "eb8ecbf6d5870763ae246e37539d82e37052cb32f88bb8c59971f9978e437743">,
# @timestamp = 1637-09-15 21:12:38,
@data = "Transaction Data......",
@previous_hash = "eb8ecbf6d5870763ae246e37539d82e37052cb32f88bb8c59971f9978e437743",
@hash = "be50017ee4bbcb33844b3dc2b7c4e476d46569b5df5762d14ceba9355f0a85f4">,
...
Q: What's a Hash? What's a (One-Way) Crypto(graphic) Hash Digest Checksum?
A: A hash e.g. eb8ecbf6d5870763ae246e37539d82e37052cb32f88bb8c59971f9978e437743
is a small digest checksum calculated
with a one-way crypto(graphic) hash digest checksum function
e.g. SHA256 (Secure Hash Algorithm 256 Bits)
from the data. Example from blockchain.rb:
sha = Digest::SHA256.new
sha.update( @timestamp.to_s + @previous_hash + @data )
sha.hexdigest ## returns "eb8ecbf6d5870763ae246e37539d82e37052cb32f88bb8c59971f9978e437743"
end
A blockchain uses
- the block timestamp (e.g.
1637-09-15 20:52:38) and - the hash from the previous block (e.g.
edbd4e11e69bc399a9ccd8faaea44fb27410fe8e3023bb9462450a0a9c4caa1b) and finally - the block data (e.g.
Transaction Data...)
to calculate the new hash digest checksum, that is, the hash
e.g. be50017ee4bbcb33844b3dc2b7c4e476d46569b5df5762d14ceba9355f0a85f4.
Q: What's a Merkle Tree?
A: A Merkle tree is a hash tree named after Ralph Merkle who patented the concept in 1979 (the patent expired in 2002). A hash tree is a generalization of hash lists or hash chains where every leaf node (in the tree) is labelled with a data block and every non-leaf node (in the tree) is labelled with the crypto(graphic) hash of the labels of its child nodes. For more see the Merkle tree Wikipedia Article.
Note: By adding crypto(graphic) hash functions you can "merkelize" any data structure.
Q: What's a Merkelized DAG (Directed Acyclic Graph)?
A: It's a blockchain secured by crypto(graphic) hashes that uses a directed acyclic graph data structure (instead of linear "classic" linked list).
Note: Git uses merkelized dag (directed acyclic graph)s for its blockchains.
Q: Is the Git Repo a Blockchain?
A: Yes, every branch in the git repo is a blockchain. The "classic" Satoshi-blockchain is like a git repo with a single master branch (only).
Do-It-Yourself (DIY) - Build Your Own Blockchain
Python * Ruby * JavaScript * Java * Go
Python
Let's Build the Tiniest Blockchain in Python Series by Gerald Nash
- Let's Build the Tiniest Blockchain In Less Than 50 Lines of Python (Part 1), July 2016
- Let's Make the Tiniest Blockchain Bigger With More Lines of Python (Part 2), July 2016
Build Your Own Blockchain: A Python Tutorial by Eric Munsing, March 2017, (Source)
Learn Blockchains by Building One (in Python) by Daniel van Flymen, September 2017, (Source) -- The fastest way to learn how Blockchains work is to build one
Build Your Own Blockchain (in Python 3) Series by Jack Schultz, (Source)
- Part 1: Creating, Storing, Syncing, Displaying, Mining, and Proving Work, October 2017
- Part 2: Syncing Chains From Different Nodes, October 2017
- Part 3: Writing Nodes that Mine and Talk, November 2017
A Practical Introduction to Blockchain with Python by Adil Moujahid, March 2018, (Source)
Ruby
How Does Bitcoin Force Consensus Among Byzantine Generals? by Fabio Akita, November 2017
blockchain-lite - Build your own blockchains with crypto hashes; revolutionize the world with blockchains, blockchains, blockchains one block at a time! by Gerald Bauer, Ruby Advent Calendar 2017 / Day 1, December 2017
merkletree library - Build Your Own Crypto Hash Trees; Grow Your Own Money on Trees by Gerald Bauer, Ruby Advent Calendar 2017 / Day 19, December 2017
centralbank command line tool (and library) - Print Your Own Money / Cryptocurrency; Run Your Own Federated Central Bank Nodes on the Blockchain Peer-to-Peer over HTTP by Gerald Bauer, Ruby Advent Calendar 2017 / Day 24, December 2017
A guide to building a blockchain & cryptocurrency from scratch (Source) by Antoine Fink, April 2021
Crystal
Write your own blockchain and Proof-of-Work (PoW) algorithm using Crystal by Bradford Lamson-Scribner, May 2018, (Source)
JavaScript
Writing a Tiny Blockchain in JavaScript by Xavier Decuyper, July 2017
Node.js Blockchain Imlementation: BrewChain: Chain+WebSockets+HTTP Server by Darren Beck, November 2017, (Source) -- Protecting the tea making ledger from unscrupulous colleagues
Build your own Blockchain in Javascript/Visualization of Blockchains by Nam Chu Hoai, January 2018
TypeScript
Naivecoin: a tutorial for building a cryptocurrency by Lauri Hartikka, (Source)
Java
Creating Your First Blockchain with Java, Part 1 by Kass, December 2017, (Source)
Kotlin
Let's implement a cryptocurrency in Kotlin by Vasily Fomin, July 2018, (Source)
SQL
Blockchain by Example in SQL Server by Benjamin Campbell, December 2017, (Source)
Rust
Building Blockchain in Rust Series by Jacob Lindahl, GeekLaunch, (Source), (Slide Decks in PDF)
- Part 1: Blocks and Hashing, January 2019
- Part 2: Mining, January 2019
- Part 3: Chains and Checks, January 2019
Go
Building Blockchain in Go Series by Ivan Kuznetsov, (Source)
- Part 1: Basic Prototype, August 2017
- Part 2: Proof-of-Work, August 2017
- Part 3: Persistence and CLI, August 2017
- Part 4: Transactions 1, September 2017
- Part 5: Addresses, September 2017
- Part 6: Transactions 2, September 2017
- Part 7: Network, October 2017
Blockchain Series in Go by by Coral Health (Source)
- Code your own blockchain in less than 200 lines of Go! by Coral Health, January 2018
- Part 2: Networking -- Code your own blockchain in less than 200 lines of Go! by Coral Health, February 2018
- Code your own blockchain mining algorithm in Go! by Coral Health, March 2018
Talk Notes
- Blockchain! Blockchain! Blockchain! - Build Your Own Blockchains in JavaScript from Zero (Scratch) by Gerald Bauer, Vienna.js, September 2017
- Blockchain! Blockchain! Blockchain! - Build Your Own Blockchains in Ruby from Zero (Scratch) by Gerald Bauer, Vienna.rb, Dezember 2017
- Blockchain vs (Hyper) Ledger -- Inside (Hyper) Ledger Lite - Add Transactions One Block at a Time and Balance the Accounts (Books) by Gerald Bauer, Hyperledger Vienna, March 2018
- Blockchain! Blockchain! Blockchain! - Build Your Own Blockchains in Go from Zero (Scratch) by Gerald Bauer, Vienna.go, April 2018
More
See Build your own Blockchain / Cryptocurrency @ Build your own (insert technology here)
Samples
Blockchain from Scratch - Ruby Version
attr_reader :timestamp
attr_reader :data
attr_reader :previous_hash
attr_reader :hash
def initialize(data, previous_hash)
@timestamp = Time.now
@data = data
@previous_hash = previous_hash
@hash = calc_hash
end
def self.first( data="Genesis" ) # create genesis (big bang! first) block
## note: uses all zero for previous_hash ("0")
Block.new( data, "0000000000000000000000000000000000000000000000000000000000000000" )
end
def self.next( previous, data="Transaction Data..." )
Block.new( data, previous.hash )
end
private
def calc_hash
sha = Digest::SHA256.new
sha.update( @timestamp.to_s + @previous_hash + @data )
sha.hexdigest
end
end # class Block
#####
## let's get started
## build a blockchain a block at a time
b0 = Block.first( "Genesis" )
b1 = Block.next( b0, "Transaction Data..." )
b2 = Block.next( b1, "Transaction Data......" )
b3 = Block.next( b2, "More Transaction Data..." )
blockchain = [b0, b1, b2, b3]
pp blockchain
(Source: blockchain.rb)
will pretty print (pp) something like:
[# @timestamp = 1637-09-15 20:52:38,
@data = "Genesis",
@previous_hash = "0000000000000000000000000000000000000000000000000000000000000000",
@hash = "edbd4e11e69bc399a9ccd8faaea44fb27410fe8e3023bb9462450a0a9c4caa1b">,
# @timestamp = 1637-09-15 21:02:38,
@data = "Transaction Data...",
@previous_hash = "edbd4e11e69bc399a9ccd8faaea44fb27410fe8e3023bb9462450a0a9c4caa1b",
@hash = "eb8ecbf6d5870763ae246e37539d82e37052cb32f88bb8c59971f9978e437743">,
# @timestamp = 1637-09-15 21:12:38,
@data = "Transaction Data......",
@previous_hash = "eb8ecbf6d5870763ae246e37539d82e37052cb32f88bb8c59971f9978e437743",
@hash = "be50017ee4bbcb33844b3dc2b7c4e476d46569b5df5762d14ceba9355f0a85f4">,
# @timestamp = 1637-09-15 21:22:38,
@data = "More Transaction Data...",
@previous_hash = "be50017ee4bbcb33844b3dc2b7c4e476d46569b5df5762d14ceba9355f0a85f4",
@hash = "5ee2981606328abfe0c3b1171440f0df746c1e1f8b3b56c351727f7da7ae5d8d">]
Comments from the reddit ruby posting:
Wait, so a blockchain is just a linked list?
No. A linked list is only required to have a reference to the previous element, a block must have an identifier depending on the previous block's identifier, meaning that you cannot replace a block without recomputing every single block that comes after. In this implementation that happens as the previous digest is input in the calc_hash method.
What about Proof of Work / Waste?
Let's add a proof of work / waste to the blockchain. In the classic blockchain you have to compute a block hash that starts with leading zeros (00). The more leading zeros the harder (more difficult) to compute. Let's keep it easy to compute with two leading zeros (00), that is, 16^2 = 256 possibilites (^1,2). Three leading zeros (000) would be 16^3 = 4_096 possibilites and four zeros (0000) would be 16^4 = 65_536 and so on.
(^1): 16 possibilties because it's a hex or hexadecimal or base 16 number, that is, 0 1 2 3 4 6 7 8 9 a (10) b (11) c (12) d (13) e (14) f (15).
(^2): A random secure hash algorithm needs on average 256 tries (might be lets say 305 tries, for example, because it's NOT a perfect statistic distribution of possibilities).
Example:
nonce = 0
loop do
hash = calc_hash_with_nonce( nonce )
if hash.start_with?( difficulty )
return [nonce,hash] ## bingo! proof of work if hash starts with leading zeros (00)
else
nonce += 1 ## keep trying (and trying and trying)
end
end
end
def calc_hash_with_nonce( nonce=0 )
sha = Digest::SHA256.new
sha.update( nonce.to_s + @timestamp.to_s + @previous_hash + @data )
sha.hexdigest
end
(Source: blockchain_with_proof_of_work.rb)
Let's rerun the sample with the proof of work machinery added. Now the sample will pretty print (pp) something like:
[# @timestamp = 1637-09-20 20:13:38,
@data = "Genesis",
@previous_hash = "0000000000000000000000000000000000000000000000000000000000000000",
@nonce = 242,
@hash = "00b8e77e27378f9aa0afbcea3a2882bb62f6663771dee053364beb1887e18bcf">,
# @timestamp = 1637-09-20 20:23:38,
@data = "Transaction Data...",
@previous_hash = "00b8e77e27378f9aa0afbcea3a2882bb62f6663771dee053364beb1887e18bcf",
@nonce = 46,
@hash = "00aae8d2e9387e13c71b33f8cd205d336ac250d2828011f5970062912985a9af">,
# @timestamp = 1637-09-20 20:33:38,
@data = "Transaction Data......",
@previous_hash = "00aae8d2e9387e13c71b33f8cd205d336ac250d2828011f5970062912985a9af",
@nonce = 350,
@hash = "00ea45e0f4683c3bec4364f349ee2b6816be0c9fd95cfd5ffcc6ed572c62f190">,
# @timestamp = 1637-09-20 20:43:38,
@data = "More Transaction Data...",
@previous_hash = "00ea45e0f4683c3bec4364f349ee2b6816be0c9fd95cfd5ffcc6ed572c62f190",
@nonce = 59,
@hash = "00436f0fca677652963e904ce4c624606a255946b921132d5b1f70f7d86c4ab8">]
See the difference? All hashes now start with leading zeros (00) and the nonce is the random "lucky number"
that makes it happen. That's the magic behind the proof of work.
Blockchain from Scratch - JavaScript Version
constructor(data, previousHash) {
this.timestamp = new Date()
this.data = data
this.previousHash = previousHash
this.hash = this.calcHash()
}
calcHash() {
var sha = SHA256.create()
sha.update( this.timestamp.toString() + this.previousHash + this.data )
return sha.hex()
}
static first( data="Genesis" ) { // create genesis (big bang! first) block
// uses all-zero previousHash
return new Block( data, "0000000000000000000000000000000000000000000000000000000000000000" )
}
static next( previous, data="Transaction Data..." ) {
return new Block( data, previous.hash )
}
}
//////
// let's get started
// build a blockchain a block at a time
b0 = Block.first( "Genesis" )
b1 = Block.next( b0, "Transaction Data..." )
b2 = Block.next( b1, "Transaction Data......" )
b3 = Block.next( b2, "More Transaction Data..." )
blockchain = [b0, b1, b2, b3]
console.log( blockchain )
(Source: blockchain.js)
will log something like:
[ Block {
timestamp : 1637-09-18 08:25:54,
data : 'Genesis',
previousHash : '0000000000000000000000000000000000000000000000000000000000000000',
hash : 'c396de4c03ddb5275661982adc75ce5fc5905d2a2457d1266c74436c1f3 c50f1' },
Block {
timestamp : 1637-09-18 08:35:54,
data : 'Transaction Data...',
previousHash : 'c396de4c03ddb5275661982adc75ce5fc5905d2a2457d1266c74436c1f3c50f1',
hash : '493131e09c069645c82795c96e4715cea0f5558be514b5096d853a5b989 9154a' },
Block {
timestamp : 1637-09-18 08:45:54,
data : 'Transaction Data......',
previousHash : '493131e09c069645c82795c96e4715cea0f5558be514b5096d853a5b9899154a',
hash : '97aa3cb5052615d60ff8e6b41bef606562588c4874f011970ac2f218e2f 0f4a8' },
Block {
timestamp : 1637-09-18 08:55:54,
data : 'More Transaction Data...',
previousHash : '97aa3cb5052615d60ff8e6b41bef606562588c4874f011970ac2f218e2f0f4a8',
hash : 'e10e020f832e46c2b60e1c3c0412bd370b2fde5f0f782c16eb87d0313ea 0d3a3' } ]