Creating Your First Blockchain with Python

C
In this tutorial we'll learn how to create a very basic Blockchain with Python. We will create a Blockchain with just 30 lines of code! The aim is to introduce you to Blockchain programming without getting into inessential details. You should already know fundamentals of Blockchain, if not then you may want to read this article first. You should also know basic Python programming and object oriented concepts. Prerequisites Python 3 Installed on Windows, Linux or Mac. Knowledge of running Python programs on terminal or IDLE. The Fundamentals A Blockchain is merely a series of blocks. These blocks are logically connected to each other as each one stores previous block's hash. We will cre
Subscribe or log in to read the rest of this content.

About the author

Devji Chhanga

I teach computer science at university of Kutch since 2011, Kutch is the western most district of India. At iDevji, I share tech stories that excite me. You will love reading the blog if you too believe in the disruptive power of technology. Some stories are purely technical while others can involve empathetical approach to problem solving using technology.

9 Comments

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • getting following error while changing
    genesisBlock = Block(“I am genesis block!”,0) to genesisBlock = Block(“I am genesis block”,0)

    ….
    File “blockDev.py”, line 14, in getPoWHash
    return self.getPoWHash (data)
    File “blockDev.py”, line 14, in getPoWHash
    return self.getPoWHash (data)
    File “blockDev.py”, line 14, in getPoWHash
    return self.getPoWHash (data)
    File “blockDev.py”, line 7, in getPoWHash
    nonce = str(random.randint(0,300000))
    File “/usr/lib/python3.5/random.py”, line 218, in randint
    return self.randrange(a, b+1)
    File “/usr/lib/python3.5/random.py”, line 194, in randrange
    return istart + self._randbelow(width)
    File “/usr/lib/python3.5/random.py”, line 228, in _randbelow
    if type(random) is BuiltinMethod or type(getrandbits) is Method:
    RecursionError: maximum recursion depth exceeded while calling a Python object

    • Hi Smit! Python has a default recursion depth limit of 1000. Recursion depth is number of times a function can recursively call itself.

      As you may know, in this problem when we generate random nonces not always we get target nonce in 1000 tries, when this happens you get this error.

      You can increase recursion limit using sys.setrecursionlimit(x) where x is the new limit you want to set.

      But this is a naive solution, which is good for our tutorial. For production systems you should rewrite getPoWHash() using iterations rather than recursion.

      I hope this helps. If you found this post helpful please share it to others.

    • Hi Nila,

      We are not adding zeroes as such. We are repeatedly running getPoWHash () function to generate a hash until we get a hash with two leading zeroes.

      Computing power invested in this effort is the COST of generating a hash.

      • Hi Devji,
        I also had the same question as Nila, but I will word it differently. Is there a way to make it so we only get hash’s that start with 5 zero’s instead of starting with 2 zero’s?

      • Hi Devji,
        I had the same question as Nila but will phrase it differently. Is there a way to get a hash that leads with 4 zeroes instead of just two?

        • Billy, when designing a proof of work function, the designer will choose how many zeroes are good for it! Zeroes or ones or Zs don’t matter. We need to make sure that whoever is adding a solution to the blockchain is doing so only after a lot of “work”.

          Work, here, means number of computations a miner does before it comes up with a solution. The more the number of 0s the harder it is to come up with the solution. It means miner will have to use more processing power, electricity and other resources to come up with a solution. If you keep low number of 0s, it becomes easy to compute the solution.

          Bill, please feel free to ask more questions if this doesn’t explain. I am here to help!

Devji Chhanga

I teach computer science at university of Kutch since 2011, Kutch is the western most district of India. At iDevji, I share tech stories that excite me. You will love reading the blog if you too believe in the disruptive power of technology. Some stories are purely technical while others can involve empathetical approach to problem solving using technology.

Get in touch

Quickly communicate covalent niche markets for maintainable sources. Collaboratively harness resource sucking experiences whereas cost effective meta-services.