Live data from Hacker News

Show HN: An educational blockchain implementation in Python

github.com

21–30 of 44 posts

Re: Show HN: An educational blockchain implementation in Python

#21
post #4

> It is NOT secure neither a real blockchain and you should NOT use this for anything else than educational purposes. It would be nice if non-secure parts of implementation or design were clearly marked. What's the point of education article, if bad examples aren't clearly marked as bad? If MD5 usage is the only issue, author could easily replace it with SHA and get rid of the warning at the start. If there are other…

OP here.

erikb is spot on in the sibling comment. This hasn't been expert-reviewed, hasn't been audited so I'm pretty confident there is a bug somewhere that I don't know about.

It's educational in the sense that I tried as best a I could to implement the various algorithmic parts (mining, validating blocks & transactions, etc...).

I originally used MD5 because I thought I would do more exploration regarding difficulty and MD5 is faster to compute than SHA. In the end, I didn't do that exploration, so I could easily replace MD5 with SHA. I'll update the notebook to use SHA, but I'm still not gonna remove the warning :)

I'll also try to point out more explicitly which parts I think are not secure.

Re: Show HN: An educational blockchain implementation in Python

#22
post #14

This is great. Just last weekend I did the same thing, coding a very basic blockchain in Python for educational purposes. You tackled wallets, which I didn't get to yet, so that was really helpful. I'm still a little unsure around exactly how miners and nodes communicate with each other. Especially things like broadcasting transactions and new blocks. Any good resources for that?

Thanks ! I've completely left out communication from this because it wouldn't fit in the notebook and I haven't researched it. Would also appreciate if anybody has good resources on it.

Re: Show HN: An educational blockchain implementation in Python

#23

FYI: Great blockchain (from sratch) starter article. At the Awesome Blockchains page [1] I collect starter blockchains and articles (in Python, Ruby, JavaScript, etc.) the idea is the best way to learn about blockchains is to do-it-yoursef - build your own blockchains from scratch. Great example. Keep it up. Cheers. [1] https://github.com/openblockchains/awesome-blockchains

Thanks ! The awesome-blockchains is a great resource, thanks for sharing.

Re: Show HN: An educational blockchain implementation in Python

#24
post #4

> It is NOT secure neither a real blockchain and you should NOT use this for anything else than educational purposes. It would be nice if non-secure parts of implementation or design were clearly marked. What's the point of education article, if bad examples aren't clearly marked as bad? If MD5 usage is the only issue, author could easily replace it with SHA and get rid of the warning at the start. If there are other…

just gives you a sense of the protocol, which is probably GTK if you’re technically inclined and speculating.

Re: Show HN: An educational blockchain implementation in Python

#25
post #3

Very good writeup that shows all the steps. Note it uses MD5 hash instead of SHA256 so not exactly bitcoin. I wonder how much more work would be to make the code fully implement bitcoin. Will it still be readable? Or Etherium? Would be great value for understanding even if Python would be inefficient to run in prod.

OP here.

Swapping MD5 for SHA256 is very easy. I'll actually do it - see my other answer above for why MD5.

For the other differences to bitcoin and from the top of my head :

- In my implementation, wallet addresses are the public key of the owner. Bitcoin addresses are slightly more complicated [1] and a wallet can (and should) generate a new address for each transaction.

- Bitcoin uses ECDSA instead of RSA

- Bitcoin transactions use a (simpler than ethereum but still) scripting language [2].

- The whole communication part was left out : you need a way to broadcast blocks. I haven't looked into that

- Bitcoin uses a Merkle tree to store transactions (and prune spent ones).

I think the scripting and communication would be the two biggest tasks. But it would also require unit testing and obviously wouldn't fit in a single notebook.

[1] https://en.bitcoin.it/wiki/Technical_background_of_version_1...

[2] https://en.bitcoin.it/wiki/Script

Re: Show HN: An educational blockchain implementation in Python

#26
post #15

nice walkthrough. also, not sure why folks are nitpicking about minor things like security disclaimers, number of sha256 hashes, md5, etc. while ignoring nontrivial gaps (eg no merkle dags, one of the cornerstone concepts).

Thanks.

You're right about Merkle tree. This is a whole section of the bitcoin paper and it's pretty important. But as far as I understand, it's "only" an optimization to save disk space, so it doesn't change the underlying logic.

Re: Show HN: An educational blockchain implementation in Python

#27

Thanks! Simplest explanation I've seen. Here's an nbviewer link (which, like base58, works on/over a phone): https://nbviewer.jupyter.org/github/julienr/ipynb_playground... Note that Bitcoin does two rounds of SHA256 rather than one round of MD5. There's also a "P2P DHT" (peer-to-peer distributed hash table) for storing and retrieving blocks from the blockchain; instead of traditional database multi-master replicatio…

Thanks for the precision regarding the hash.

Regarding the error, they are logged when a verify_block/transaction returns False, just to be a bit more explicit about what failed. In a real implementation, I guess you would throw exceptions instead (or use some Result pattern), but I tried and it cluttered the code quite a bit, so I went back to logging.

Re: Show HN: An educational blockchain implementation in Python

#28
post #4

> It is NOT secure neither a real blockchain and you should NOT use this for anything else than educational purposes. It would be nice if non-secure parts of implementation or design were clearly marked. What's the point of education article, if bad examples aren't clearly marked as bad? If MD5 usage is the only issue, author could easily replace it with SHA and get rid of the warning at the start. If there are other…

>"What's the point of education article, if bad examples aren't clearly marked as bad?"

The doc string for the hash function states:

    An INSECURE hash function that you should not use in the real world.
    Returns an hexadecimal hash
I'm not sure how much clearer you could mark that.

The point in the article seems to be understanding the blockchain protocol and concepts and not "how to write secure crypto."

Re: Show HN: An educational blockchain implementation in Python

#29
post #25
post #3

Very good writeup that shows all the steps. Note it uses MD5 hash instead of SHA256 so not exactly bitcoin. I wonder how much more work would be to make the code fully implement bitcoin. Will it still be readable? Or Etherium? Would be great value for understanding even if Python would be inefficient to run in prod.

OP here. Swapping MD5 for SHA256 is very easy. I'll actually do it - see my other answer above for why MD5. For the other differences to bitcoin and from the top of my head : - In my implementation, wallet addresses are the public key of the owner. Bitcoin addresses are slightly more complicated [1] and a wallet can (and should) generate a new address for each transaction. - Bitcoin uses ECDSA instead of RSA - Bitcoi…

Bitcoin also hashes twice, i.e. it computes sha256(sha256(.)). Supposedly, this is to protect against extension attacks [1].

Was wondering, any specific reason to choose RSA vs ECDSA? Signatures would be smaller.

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

Re: Show HN: An educational blockchain implementation in Python

#30
post #29
post #25

Earlier quoted context omitted.

OP here. Swapping MD5 for SHA256 is very easy. I'll actually do it - see my other answer above for why MD5. For the other differences to bitcoin and from the top of my head : - In my implementation, wallet addresses are the public key of the owner. Bitcoin addresses are slightly more complicated [1] and a wallet can (and should) generate a new address for each transaction. - Bitcoin uses ECDSA instead of RSA - Bitcoi…

Bitcoin also hashes twice, i.e. it computes sha256(sha256(.)). Supposedly, this is to protect against extension attacks [1]. Was wondering, any specific reason to choose RSA vs ECDSA? Signatures would be smaller. [1] https://en.wikipedia.org/wiki/Length_extension_attack

No good reason for RSA vs ECDSA. I was just more familiar with RSA, but apparently pycryptodome supports ECDSA as well, so I guess the change should be minimal.
Post reply on HN