
A blockchain in 200 lines of code - bpierre
https://medium.com/@lhartikk/a-blockchain-in-200-lines-of-code-963cc1cc0e54#.ccg982xfi
======
bascule
Though blockchain is an ill-defined term, this seems to be missing the most
important part of one: consensus. Bitcoin achieves this in two ways:

    
    
      - Proof-of-Work, which effectively functions as a leader election algorithm
      - Consensus programs, which mediate any changes to the blockchain.
    

Each Bitcoin transaction has an associated script that determines how it can
be spent. A conforming block satisfies all of the scripts for all of the
transactions being spent.

This particular post illustrates something which I would say is little more
than a Merkle tree[1], except it isn't even that. But perhaps it's
illustrative for some people.

[1]:
[https://en.wikipedia.org/wiki/Merkle_tree](https://en.wikipedia.org/wiki/Merkle_tree)

~~~
kordless
I suppose we could look at consensus as means by which _multiple_ blockchains
are collapsed into a single chain which contains the "truth" for multiple
entities or aggregates. Once a collapse or consensus occurs, the chain,
regardless of who holds it, would contain the truth.

~~~
throwaway91111
Without consensus, what distinguishes a block chain from a Merkle tree?

~~~
saurik
The Merkle tree is only used within a Bitcoin block to store transactions in
an efficient manner; it isn't part of the block chain. A block chain without
consensus isn't even a tree: it is just a singly-linked list with
cryptographic guarantees that given a linked list node, all previous entries
can be "verified".

However, as other people have pointed out, this implementation is so incorrect
that it fails to even do that: it contains only the hash of the previous data,
not the hash of the previous node, so this actually only manages to verify the
previous one entry of the chain :/. This is essentially just a linked list :(.

~~~
andirk
Isn't calculateHash() creating a hash from the whole block, not just the data
part of it? If not, how should that function calculate the hash so that it
satisfies what it needs to to be a real blockchain?

~~~
saurik
Ah! I was being confused by the diagram and another comment :(. It seems
really strange to assume that the previous block's hash field is accurate, but
I guess that is being verified as the chain is built; if you just remove that
field you could calculate that hash and verify it that way. I now agree: it at
least has the ability to verify the list's history. (Sorry for the confusion.)

------
tromp
Real blockchains do have a blockheader.prevHash field to indicate that the
previous block is the one satisfying

    
    
        HASH(prevheader) == blockheader.prevHash
    

This one actually adds a blockheader.hash field and instead compares

    
    
        prevheader.hash == blockheader.prevHash
    

Naive indeed:-)

~~~
runeks
Good video+interactive explanation:
[https://anders.com/blockchain/](https://anders.com/blockchain/)

------
hdhzy
One caveat: Bitcoin doesn't choose longest chain by the number of blocks but
rather the difficulty, see
[https://en.bitcoin.it/wiki/Difficulty](https://en.bitcoin.it/wiki/Difficulty)

~~~
chjj
More specifically, it chooses by cumulative work. Bitcoin needs that behavior
because of retargetting (a lesser height chain may have required more proof of
work to build than a higher height one).

If your custom blockchain has a static target, choosing the best chain by
height is perfectly fine. Though, I'm not sure how useful a blockchain without
retargetting or POW would be.

------
kang
There is not a single use-case that this code can achieve, which you couldn't
before the invention of bitcoin.

Blockchains are just linked lists; without proof-of-work its useless.

------
mamon
Is this just me or is this blockchain algorithm really similar to stringing
together commits in Git? Can Git be considered "blockchain" application?

~~~
throwawayish
The hash tree (Merkle Tree) is only part of it. The proof-of-work and
consensus / voting protocols are the other (main) part.

~~~
OJFord
Which this doesn't have.

------
AKifer
That's exactly what we need, a Hello world of the blockchain technology.
Everybody talks about these data structures while it's very rare to find
concrete example of even a simple implementation. It's not helping to be said
go look at bitcoin source code.

~~~
carlosgj94
This may help you also:
[https://anders.com/blockchain/](https://anders.com/blockchain/). In fact is
quite better, since it adds more info. Also the code of it is on github ;)

------
davidgerard
This is a good example of how in 2017, Merkle trees will be renamed
"blockchain" in marketing parlance.

------
spraak
I don't understand why someone would use an ES6 feature like arrow funcs, but
still use `var`

~~~
inimino
Because nothing is wrong with `var` and arrow functions are more concise?

~~~
TheDong
> nothing is wrong with var

Other than having utterly illogical scoping. Which is the entire reason 'let'
was introduced and should always be preferred.

~~~
inimino
Please. There's nothing illogical about it. It scopes to the function body.
Maybe you meant to write "unfamiliar".

~~~
nv-vn
It's entirely unintuitive, inconsistent with the syntax, and is generally
senseless. "var" lacks an easy to explain meaning because in many cases it
doesn't even introduce a variable.

~~~
inimino
It ensures that a variable is declared in the current scope. That's the easy
to explain meaning.

It's inconsistent with other languages which introduce a new scope whenever
there are curly braces, but there's nothing inconsistent about the syntax.
That's just a matter of taking expectations from other languages and expecting
them to work unchanged in JavaScript.

------
jamiehoward
I'm not familiar with blockchains. Super interesting idea.

------
logicallee
I wonder if anyone thought of making a distributed operating system where 1-2
million people could all run a NINTENDO console or something else extremely
primitive - but in way that they KNOW no one is pulling any shenanigans.

Can you imagine how cool it would be if a million or, say, a hundred million
people joined forces to do what a $60 mobile phone can do?

This is absolutely the future. No doubt about it. No one can ever conceivably
come up with a paradigm as cool as this level of redundancy. And on top of all
of this, it even works. (Not as designed, because 51% of miners may be
controlled by one entity, but in practice it totally works.)

~~~
frik
sounds like Ethereum:
[https://en.wikipedia.org/wiki/Ethereum](https://en.wikipedia.org/wiki/Ethereum)

 _" Ethereum is an open-source, public, blockchain-based distributed computing
platform featuring smart contract (scripting) functionality. It provides a
decentralized virtual machine, the Ethereum Virtual Machine (EVM). This
virtual machine can execute Turing-complete scripts using an international
network of public nodes and a token called ether. Gas, an internal transaction
pricing mechanism, is used to prevent spam on the network and allocate
resources proportionally to the incentive offered by the request"_

Unrelated, I remember an experiment on Twitch, where hundreds of viewers
controlled/played Pokémon Yellow (Gameboy emulator) live on Twitch via chat
commands like "left", "right". It more or less worked out fine, but was rather
slow progress.

~~~
JoshTriplett
> Unrelated, I remember an experiment on Twitch, where hundreds of viewers
> controlled/played Pokémon Yellow (Gameboy emulator) live on Twitch via chat
> commands like "left", "right". It more or less worked out fine, but was
> rather slow progress.

[https://en.wikipedia.org/wiki/Twitch_Plays_Pok%C3%A9mon](https://en.wikipedia.org/wiki/Twitch_Plays_Pok%C3%A9mon)

