Hacker News new | past | comments | ask | show | jobs | submit login
A Practical Introduction to Blockchain with Python (adilmoujahid.com)
323 points by dimitrov on Mar 14, 2018 | hide | past | web | favorite | 53 comments



The article adopts one of my preferred metaphors for the blockchain:

"A blockchain is a distributed database that allows direct transactions between two parties without the need of a central authority."

Which as a developer, I feel like I have a good handle on - but where I continually come up short is figuring out what I'd use such a data store for when building an application?

The article starts off calling blockchain tech "significant and disruptive technologies that came into existence since the inception of the Internet."

I'd love if someone had some examples of practical applications or uses that are being developed or could point me in the right direction.


It’s not clear that it doesn’t need a central authority. There is a rich history of blockchain consensus failure needing a central authority to tell everyone which chain is the right one. Satoshi did it in 2010, the Bitcoin developers did it in 2013, Vitalik did it with Ethereum in 2016. When the ledgers don’t reconcile, the developers are the central authority. And proof-of-work can’t save it because that too is centralized.

So if it can’t do the first thing it promises it can do, maybe it becomes more like a Rube Goldberg machine than something that is meant to be useful/practical. The fact that people are praising CryptoKitties as a “practical application” seems to support the Rube Goldberg machine/toy theory.


> It’s not clear that it doesn’t need a central authority. There is a rich history of blockchain consensus failure needing a central authority to tell everyone which chain is the right one. Satoshi did it in 2010, the Bitcoin developers did it in 2013, Vitalik did it with Ethereum in 2016. When the ledgers don’t reconcile, the developers are the central authority. And proof-of-work can’t save it because that too is centralized.

There's a big difference between centralized influence and centralized control. The 2010 and 2013 examples were fairly homogeneous, because the changes were fairly obvious and reasonable, the thought leaders were widely trusted, and almost all the users followed them. But a large number of people didn't follow when Ethereum split (hence Ethereum Classic) or when Bitcoin split in 2017 (hence Bitcoin Cash).

Thought leaders like Satoshi and Vitalik can influence most of the community of their respective coins, but they can't force anyone to accept their blocks.

> So if it can’t do the first thing it promises it can do, maybe it becomes more like a Rube Goldberg machine than something that is meant to be useful/practical. The fact that people are praising CryptoKitties as a “practical application” seems to support the Rube Goldberg machine/toy theory.

The problem I think is that decentralized control is really the only thing that blockchains offer over existing data stores. If you don't care about decentralized control, existing distributed logs such as Datomic are superior to blockchain in every way.

I would go so far as to say that the vast majority of blockchain users fall into two categories:

1. People who don't understand decentralization. 2. People who don't care about decentralization but are riding the hype (whether they understand decentralization or not).

This is why so many "blockchain applications" look like Rube Goldberg machines:

1. People who don't understand decentralization use it to implement things that would be much better served by an existing distributed datastore such as Datomic. 2. People who are just riding hype don't need to implement anything serious to fool people. They can implement a Rube Goldberg machine and there will always be suckers who take it seriously as long as it ticks the "blockchain" hype tickbox.

Very few people actually understand decentralization AND care about it, and even fewer of those have the technical chops to implement them. So I think it will take time for more complex blockchain killer apps to emerge.

It's worth noting that the first simple blockchain application, cryptocurrency, has already had pretty immense effects on our society, from darknet markets to crypto exchanges. You can argue whether the uses people use cryptocurrency for are good, but you can't argue it isn't useful--it's just not complex (from a logical standpoint--it's just a ledger--obvously it's technically and socially complex).


Satoshi told everyone his fork was Bitcoin and Vitalik told everyone his fork was Ethereum. The fact that more people disagreed with Vitalik than people that disagreed with Satoshi doesn't change the fact that the same thing happened in both cases: a central authority told everyone which fork was the right one.

At the end of the day, when consensus breaks in unforeseen circumstances-- whether through a bug or an attack (the difference is purely syntactic)--people turn to the developers to tell them which consensus ruleset to follow. If you disagree with the developers, your chain gets called something else, as we've seen with Bitcoin Cash and Ethereum Classic.

With the 2013 Bitcoin fork, there were 2 ledgers and the network could not find consensus until the developers realized this and told everyone what to do 4 hours later. That's the developers controlling the system.

And who controls the developers? Their big bad State.


> Satoshi told everyone his fork was Bitcoin and Vitalik told everyone his fork was Ethereum. The fact that more people disagreed with Vitalik than people that disagreed with Satoshi doesn't change the fact that the same thing happened in both cases: a central authority told everyone which fork was the right one.

And not everyone listened.

> At the end of the day, when consensus breaks in unforeseen circumstances-- whether through a bug or an attack (the difference is purely syntactic)--people turn to the developers to tell them which consensus ruleset to follow.

Not everyone turns to the developers.

In a decentralized system, people have the ability to disagree with the developers and literally anyone else you might name as a central authority, and have that disagreement make a meaningful difference (a fork). That's obviously significant.

> If you disagree with the developers, your chain gets called something else, as we've seen with Bitcoin Cash and Ethereum Classic.

Who cares?

When the Bitcoin fork happened, I supported the Bitcoin Cash blockchain. I don't give a crap about the name. If I get the blockchain implementation I want, you can call it whatever you want.


Your fork is not the same system though, everyone else considers it a different currency. The argument is whether or not 1 given currency is decentralized, not whether or not you as a human have the freedom to use any currency you want...

In other words, someone creating Bitcoin Cash doesn't somehow prove that Bitcoin is decentralized... We still need to analyze each currency individually if we're going to have any meaningful conversation about their merits.


> Your fork is not the same system though, everyone else considers it a different currency. The argument is whether or not 1 given currency is decentralized, not whether or not you as a human have the freedom to use any currency you want...

No, everyone does not consider it a different currency, you consider it a different currency. The name changed, sure, but again, who cares? A rose by another name would smell as sweet.

Calling either BTC or BCH a different currency from the original bitcoin really just shows a lack of understanding of what a fork is. Both BTC and BCH have made changes that weren't in the original BTC.


> everyone does not consider it a different currency, you consider it a different currency.

Um ok we're spreading lies now. Do you have evidence that the vast majority of people consider Bitcoin Cash and Bitcoin to be the same currency and I'm alone in considering these 2 blockchains 2 different currencies?


> Are you saying you have evidence that the vast majority of people consider Bitcoin Cash and Bitcoin to be the same currency and I'm alone in considering these 2 blockchains 2 different currencies?

No, I didn't say that.

I'm saying that neither Bitcoin nor Bitcoin Cash are identical the original Bitcoin. Both have added changes, and this will be true of any blockchain that is being actively developed. In fact, to say that Bitcoin before the fork is the same coin as Bitcoin when Satoshi made it is also a bit off, because there were changes added before the fork, too.

I'm saying that saying that considering either one of these to be different from the original Bitcoin is a moot point, because any conceptualization of being the same coin doesn't really fit. The only way you could say that any variation of Bitcoin is the same coin is by saying that they build on the same blockchain, but both BTC and BCH fit that criteria.

You've focused in on the names people call the coins, which is pretty much the least important way one could say that coins are the same--implementation is far more relevant than name.

In the end, the only thing that really makes sense to say is that modern BTC and BCH are both variants of the original Bitcoin. It doesn't really make sense to say that either is the original Bitcoin, because both have introduced changes. It also doesn't make sense to say that either isn't the original Bitcoin, because they both originate on the same blockchain. The idea you're trying to apply here, that a fork means a new coin was created and the old coin continues on, just doesn't really capture the full truth of situation.

Looking at popular opinion as if that had any relation to reality is not going to help you understand what's going on here. Most people don't understand Bitcoin, so looking at the opinions of most people as if there's any truth there is pointless.


I believe that the name of a currency is important so that merchants know what they're accepting.

How will the merchants in your world know which currency they're accepting?


They only know which currency they're accepting if they verify that the binary they're running is connected to the blockchain which follows the rules they want.

Admittedly, that's beyond the technical ability of many people, but names don't solve this problem in any way. I can pull a copy of the BCH source code, find/replace every displayed text that says "Bitcoin Cash" with "Bitcoin" but that changes nothing about the currency being exchanged.


While there's a lot that could be written about what decentralization even means, or how to measure it, no reasonable definition would require a decentralized system to be entirely outside of the influence of everyone all the time.

For instance, Satoshi told people something that all participants considered reasonable, and so the participants reacted accordingly. Systemic reasonable behavior is not inconsistent with the principle of decentralization, it's a consequence of it.

The origin of systemically-reasonable policy is orthogonal to the idea of decentralization, unless that origin is structurally mandated.


In 2013 literally the system could not find consensus until the central party told people what to do. If you're still calling that "decentralized" then I'm afraid the word has lost any useful meaning...

But I'm with you man, if we as a decentralized group come up with a reasonable hierarchy of trust supported by free speech and democracy, then we can create a systemically-reasonable Money that we can all agree is good.


I'm not trying to nitpick, but I don't think there has ever in the history of man been a system that was fundamentally social and about shared consensus (which is about the most literal definition of money that there is) that would work any other way.

Having some Eminence say "This is what we should do" and then having a wide network of people go and do that is _not_ a problem. What _is_ a problem is if some eminence says "This is what we should do" and having a bunch of other people say "I don't want to do that" or "That doesn't make sense" or "This will be bad for me" and then _having to do it anyway_. That's the issue. Having to get permission for the US Govt before you change how nodes work, that's an issue. Having some dev frown and say "I can't get the majority to adopt my pull request because of stupid Greg Maxwell" is not an issue.

No complex system involving humans will ever get around the importance of human communication and coordination and agreement, and that is not a technological problem to be solved. A system that artificially mandates who those humans should be, or what kinds of solutions they can offer, that _is_ a problem to be solved, and Bitcoin does a nice job of solving it as well as it can reasonably be solved.

Of course, this is an empirical question. I could be wrong, and the data will demonstrate that in time. But so far there is no such demonstration of wrongness, and the chaos and arguing between various factions (e.g., Segwit2x, Bitcoin Cash) are not evidence in favor of that hypothesis.


Sounds like you’re mostly talking about trademark law here


Are you saying trademark law would help resolving forks?


No, he's saying that you don't seem to understand the technology, because you only are talking about the names of the coins, which is of little importance compared to the implementation.


Sorry you've lost me. How do you differentiate between blockchains if not at least by name? How will a merchant know which currencies to accept if they have no names? When I'm trading Bitcoin for Litecoin, am I just trading the same thing for itself?


> How do you differentiate between blockchains if not at least by name?

By validating the blocks against the rules you've chosen and choosing the longest valid blockchain.

> How will a merchant know which currencies to accept if they have no names?

By choosing the blockchain they agree with the rules of.

> When I'm trading Bitcoin for Litecoin, am I just trading the same thing for itself?

No, but the name isn't the important difference. The important difference is that Bitcoin proof of work uses SHA while Litecoin proof of work uses Scrypt, so the blocks of one don't validate under the rules of the other.


I was asked to do a presentation at work about the blockchain and decided to implement one myself (Python).

With the same basic code base, I've built:

A basic asset tracking system. Think shipping containers.

A simple IoT API (logging sensor data).

A crude lap time tracker for racing drones. This would be easily shareable and loaded into different tracks to log your progress. I build racing drones as a hobby.

The really useful thing about the blockchain is that I can define a data structure and then design systems that consume it. Making the data portable.

Feel free to email me with questions. It's a fun tech to work with and simpler than people make it look.


Can I Haz Blockchain?! A great practical application are the CryptoKitties (Yes, little cute cartoon cats) on the blockchain, see -> https://www.cryptokitties.co

PS: For more info on cryptokitties (and copycats collectibles), see the Awesome CryptoKitties page -> https://github.com/openblockchains/awesome-cryptokitties

PPS: A great way to learn about cryptokitties (and copycat collectibles) is to build your own version from scratch :-) see the (in-progress) open source copycats version (in ruby) -> https://github.com/openblockchains/copycats


So I have a decent understanding of the mechanics of cryptokitties as a collectible game (you get a cat, it's unique, you breed it with others and try to get a cat with even better features).

So the benefit of this application vs a traditionally structured app (lets call it RESTKitties) is what?

That the external ledger of kitty creation and transfers is open and public for everyone? So you have more confidence that the central organization isn't manipulating the ecosystem in some nefarious way?


The idea is that the game code is running on the blockchain itself (not the rendering to cute kitties though), so there is no game server that can be shut down by the developers. So even if the game stops being profitable and the devs lose interest, the game could presumably keep going and everybody would still have access to their kitties and trade them.

(except in this case there is a self destruct function that would freeze the game forever)


The benefit is that it's a tradeable asset / collectible - that is, you can buy and sell it. Use it to mortgage a house, get credit, etc. It's like (real) money that's the difference. So far $20 million traded. On the blockchain you have transactions recorded (not in a private database but public and shared who owns what or who own how much etc.). Cheers. Prost.


I want to be able to trade kitties with people without Kitty Bank of America having to manage and facilitate the transaction, for one.


Guaranteed scarcity for one.


> A great practical application are the CryptoKitties

I am not sure if that is supposed to be sarcasm or not.


Blockchain have a few characteristics that make them unique:

- they are database

- they are distributed

- everybody has the entire db

- it's transparent: everyone see the content

- it's temper proof

- it has a notion of temporality

- it has a notion of transactions, and each transaction has a unique id and an history

- transactions have authors, sources and destinations, with unique identifiers that can't be faked.

Hence it's a transaction based system that is a source of truth that can be audited from end to end by anybody.

So the only applications that are interesting for the blockchain are the ones mapping to this EXACT concept. Otherwise, you don't need a blockchain.

But because of the bubble, people are now using the blockchain for everything, since that gives visibility. I have friends myself that have projects making ICO just for that. And it works. Their product clearly didn't need a coin, but they raised several millions of euros to build their stuff.

Ok, so what kind of project DOES match the blockchain paradigm?

So money, obviously is a contender.

But voting systems as well.

Or a mix of both, such as betting (see the wagger project).

Compliance systems are another use case. E.G: make a public database of all plane inspections or food products origin.

Being decentralized doesn't mean blockchain systems don't have central authorities. The decentralized nature mean the data access (and potentially production) is not on a central system, which has great benefits. But as soon has you need human actions, you need something to tell them in which direction to go.

So you need a central authority for the part dealing with the humans.

E.G: for the voting system, something to register the citizen and produce the polls. For the betting system, something to introduce the bet results into the blockchain. Etc.

One alternative to that we are experimenting with currently as a community is the use masternodes (POS machines holding a lot of coins out of the market for a long period of time to show their commitment). It's basically an oligarchy.

The coin Polis is an example of that: they make proposals of things to finance, and each masternode vote yes or no for the proposal.

They are a lot of masternode coins, now, for various uses. Zencash uses them to distribute the messages of its chat system, wagger to introduce the data from bets into the chain, etc.

But most of the masternodes are just used to increase the ponzi effect (we build and host masternode for our clients as a service so we have a large sample of them on our servers).


> Compliance systems are another use case. E.G: make a public database of all plane inspections or food products origin.

I feel like a broken record when I point out yet again that this is a technical non-solution to a human problem. The same bad actors who would falsify inspection records or food origin information will have no issue simply falsifying it into the blockchain instead of whatever current system is used.

The problem with bad actors faking records has everything to do with the bad actors and nothing whatsoever to do with the mechanism by which the records are recorded. The blockchain solves absolutely nothing in these use cases.


Of course. The problem it solves is not preventing bad human behavior, but easier detection by a broader number of actors, and an history of it.

It's basically by-passing a lot of bureaucracy, cut some of the news reporting middle ware and give a central source of truth (or lie) for a given issue. It's not a silver bullet, but it's a damn good improvement.

Technology and social progress goes hand in hand.


Right.

Can you give me an exact, explicit, description of exactly how use of blockchain would have prevented (eg) the oft-cited melamine milk poisoning scandal in China?

> It's basically by-passing a lot of bureaucracy

That bureaucracy caught the problem. Physical testing of the product uncovered the bad actor. How on earth would blockchain have helped?

> cut some of the news reporting middle ware

???

> give a central source of truth (or lie) for a given issue

There already is. And obviously the problem is when it's a lie. What value does blockchain bring? What good does it bring if the lie is more visible? And if there's no bad actors then there was no problem anyway.

> It's not a silver bullet, but it's a damn good improvement.

It's not any kind of bullet. I just don't see any improvement. Exactly what has improved and how? Consumers can reassure themselves that every step in the chain says everything is OK, rather than relying on the manufacturer to do that, which they do. And? If the manufacturer did not believe everything to be OK, the product would not even be on the shelf. The consumer will never see anything other than "everything OK!".

> Technology and social progress goes hand in hand.

Hype and money also go hand in hand. I don't think you've refuted my point successfully. As always, I want to be convinced otherwise, but I'm just not hearing anything compelling.


> Can you give me an exact, explicit, description of exactly how use of blockchain would have prevented (eg) the oft-cited melamine milk poisoning scandal in China?

I can't give you an exact, explicit description on __exactly__ how smiling more could have prevented one specific argument. But I can tell you that globally it will improve your relationships.

The point is that the track records are not only visible, in a standard format and accessible by all, but also with a permanent impossible to alter format.

So the data is more visible, more easily to a vastly greater number of people. And people can't rewrite history afterward.

You don't need to request papers to some kind of administration.

You don't need to scan thousands of papers.

You don't need to fight the PR trying to tell they never said that.

Again, it's not a silver bullet. But if you ever tried to do a citizen investigation, you knew that the process just to get the administrative data is excruciating.

The block chain helps with that, and so help citizen be part of their democratic process.

In turn, globally, this will help diminishing the number of problem than the one you describe. Not a specific instance, but the global number.

> It's basically by-passing a lot of bureaucracy

> That bureaucracy caught the problem.

Not the same bureaucracy. Besides, the chinese bureaucracy probably knew the probleme since day one and just "caught them" because they stopped paying something.

> Physical testing of the product uncovered the bad actor. How on earth would blockchain have helped?

By giving an easier opportunity to the public to have basic data to compare to the one they get if they choose to investigate.

Democracy is about having the people execicing power, not relying on institution to do everything.

It's like saying "how a knife is going to solve my hunger problem?". Well it won't. But it will make it easier to engage in the process of solving it.

> cut some of the news reporting middle ware

Data journalism is a thing.

> give a central source of truth (or lie) for a given issue

> There already is.

You never tried to investigate obviously. It's a maze of offices that don't talk to each others using incompatible format they make very hard to obtain. And that's just the official, potentially faked record.

You can't bust institutions all the time when they make bad move. But you can make it easier. The block chain can help with that. It makes fact checking easier, because half of what you need, the official story, is in one place, one format, accessible by all.

> What good does it bring if the lie is more visible? And if there's no bad actors then there was no problem anyway.

Visibility is everything. Try to ask your company to reveal salaries of each employees, including bosses. What do you think will happen ?

> Hype and money also go hand in hand.

That's not mutually exclusive with usefullness.

> I don't think you've refuted my point successfully. As always, I want to be convinced otherwise, but I'm just not hearing anything compelling.

It's my last attempt. It's ok. Not everybody needs to understand everything.


> It's my last attempt. It's ok. Not everybody needs to understand everything.

I asked you direct questions and you have answered none of them. Yes I am aware of what a blockchain is, to the finest detail. There is no need to reiterate its features.

You haven't answered my questions at all. I directly challenged you on how blockchain would have helped in a famous case of "food products origin" - an example you yourself mentioned. You have utterly failed to answer that, instead holding forth on the value of smiles, PR, silver bullets, irrelevant theories about chinese governance, democracy and its virtuousness, company salaries, how hype is actually good, and my own lacking IQ.

Can you please just answer my damn question, or do the honourable thing and admit you have no fucking idea how blockchain helps foodchain security.


Thanks for a very useful and clear checklist! Would you say decentralized DNS checks all those boxes (Namecoin is a thing, after all)? Why would a DNS require the notion of transaction?

I'm aware of sites that tell you whether you need a blockchain or not for your application, but they don't say what to use if only one or two of the requirements for a blockchain are not needed. What would a decentralized DNS strictly need if the notion of transaction wasn't required (but only the latest value)?


Actually DNS would benefit from transactions, it would allow to detect attack much more efficiently, or to analyze them after the fact.

It could also help analyze tendencies and adjust.

And as usual, you would get more accountability, which never hurts (Steven ! Why is the website down ? - * Steven quickly revert back his silly click * - Errr, nothing. No reason. Everything is alright !)


Just wanted to say thank you for taking the time to respond. This is exactly what I was trying to wrap my head around.


Product tracking and supply chain management is a practical use case. Pairing RFID tags with block chain technology can be used as an anti-counterfeiting measure to guarantee a product's origin.


i guess the most famous practical application at least in the crypto community right now is called CryptoKitties build on top of Ethereum.


Fun maybe, but it's not really practical.


Please comply with the license of the blockchain implementation in Python. It's MIT, so all you have to do is include the copyright notice ("Copyright (c) 2017 Daniel van Flymen").

You should also tell people under what license you're releasing your changes :)


@icebraining Author here. Thanks for the comments. I will add the copyright notice. Changes also will be MIT :)


Thanks!


FYI: At the Awesome Blockchains [1] page I collect articles on building your own blockchains from scratch (incl. Python, Ruby, JavaScript, etc.) The idea is that a great way to learn and understand blockchains is by building your own blockchains from scratch (zero). Happy blockchaining. [1]: https://github.com/openblockchains/awesome-blockchains


I actually wrote up a small POC for blockchains in Python myself a few months ago. I went a little bit further in to the idea of transaction verification, as well as reverse traversing existing transactions as a part of that.

https://github.com/ltavag/blocks

Always love to see the interest here, I'm excited for the technology to become detached from Crypto speculation!


> Any business or organization that relies on a centralized database as a core competitive advantage can potentially be disrupted by blockchain technology.

That's a pretty big "potentially" since many of those organizations use centralization as a defense against bad actors.


Thanks for the write up! BTW, this would make a great Jupyter notebook (with the text, graphics, code).


This implementation is similar to the work that has been shared [by Daniel](https://hackernoon.com/learn-blockchains-by-building-one-117...)


Great introduction, thanks! Are you thinking about expanding this at all? If so, how?


Great explanation!


yes, as someone with minimal knowledge of bitcoin and blockchains, this helped with visualizing the roles of the miner, blockchain, and bitcoin wallet.

As a sidenote, I have been working in C++-land for a while now, and its really cool to see how the examples both contain a simple blockchain implementation along with the views for a Flask web UI in the same file. I wonder if there are web libraries available to do something similar in C++?


next up - a practical introduction to implementing a relational database from scratch. It's great to learn about the cryptography behind the blockchain etc, but the herd mind is horrible - so many better things to spend one's time on than a 100th implementation of blockchain. Why won't the hype die already?


I would love to read a tutorial about implementing a relational database from scratch. Some people like to learn from the building blocks and others prefer to focus on high level applications.


Agreed, I think no matter how good your abstractions are, it never hurts to know how they work under the hood.




Registration is open for Startup School 2019. Classes start July 22nd.

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

Search: