Hacker News new | past | comments | ask | show | jobs | submit login
A blockchain in 200 lines of code (medium.com/lhartikk)
326 points by bpierre on March 4, 2017 | hide | past | favorite | 44 comments



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


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.


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


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 :(.


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?


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


Well, a merkle tree provides verification, too. You can only reach the hash given a certain chain of parents. What am I missing?


The post states "The NaiveChain was created for demonstration and learning purposes. Since it does not have a “mining” algorithm (PoS of PoW) it cannot used in a public network."


Well, what makes a blockchain a blockchain is not that it's a list of nodes where each contain the hash of it's previous node. In that case git would be a blockchain. It is the PoW and consensus programs that make it a blockchain.


Is git (at least the SCM of a big project, the kernel, say)... not a block chain? The hash chain is (notwithstanding some recent discoveries) secure, the series is externally archived, "proof of work" is provided by trusted human review.

Obviously no one uses the kernel series to store arbitrary data on behalf of the patch submitters. But you could, without modification to the software or processes.


Commits aren't automated and their contributions aren't commensurate. It's a distributed system, but not one solving the same problem.

We can always torture the concrete into extended metaphor. It just doesn't add any value here.


No, that's what makes a block chain useful. The "block"+"chain" is exactly a connected set of blocks


That's actually not what makes a blockchain uniquely useful. "block+chain" also makes Git and other merkle tree-based datastructures useful, but what makes blockchain useful is unforgeability via chained PoW's.


Do we agree with this: "blockchain: A list of validated blocks, each linking to its predecessor all the way to the genesis block." from Mastering Bitcoin: Unlocking Digital Cryptocurrencies, by Andreas M. Antonopoulos


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:-)


Good video+interactive explanation: https://anders.com/blockchain/


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


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.


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.


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


Yep. Git and blockchains both use Merkle trees to keep a consistent history.

https://news.ycombinator.com/item?id=9436847


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


Which this doesn't have.


There are lots of different applications where one part of a data structure refers to another part by its hash. BitTorrent is one. HTTPS certificates also do some of this I think.


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.


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


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


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


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


> nothing is wrong with var

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


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


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.


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.


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


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


sounds like 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.


> 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


except twitch plays pokemon was meant for entertainment. the purposeless waste of resources on bitcoin is a cult, believed to enlighten the world.


That is what Golem is building (https://golem.network/). Decentralized compute power that is a global P2P network of PCs. The platform will allow software developers to put their software on top as long as its approved (e.g. to make sure it's not malicious).


BOINC pioneered this sort of shared computation to do massively data parallel experiments for astrophysics research in the early 00s. I think they still do.

These days, I think the hardware investment is much lower than the software+infrastructure+social investment of running your experiment on BOINC.


IIRC the focus of these kinds of infrastructures has shifted towards protein folding [1] which could IMO prove more useful than astrophysics.

[1] https://en.wikipedia.org/wiki/Protein_folding


The Golem team (https://golem.network/) got their inspiration from BOINC. But now more people will have incentive to share their compute power because they will be paid.


For anyone reading this later: the above was sarcastic but I left off the /s. I don't think blockchains are that great of a fundamental primitive. Though, independent of application, they were extemely novel.


you don't need a blockchain for computations; only for transferring value from buyer to seller.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: