1

I would like to really understand pow mining by implementing a simple one, but still struggle.

assume we have five miners, m1,...,m5. Each miner has hash rate, m1=30%, m=20%,m3=15,m4,25, m5=10%, in total 100%

How to simulate (e.g., in simple python code) minig competition between these miners in such a way each 15 seconds a new block is found by one of them taking their hash rate into consideration. for example it could be possible that two or three miners got a block at the same time. so each block is distributed (let us say time dealy) and got confirmed by others. How to model this? decide number of stale/uncle blocks etc

Could anyone help and advise in this.

1

Edit: I spent a few hours writing some code that simulates block propagation time and uncles. You can find it at my GitHub repo here: https://github.com/lungj/ethtools. The program in question is mine_sim.py. I figured it would be a bit weird to post such a long program into StackExchange.

The rabbit hole can get pretty deep, depending on how deeply you want to model things. In the actual world of mining, miners compute hashes of particular values. When the hashes satisfy some conditions, the data that is hashed can be propagated onto the blockchain. When the blockchain is in equilibrium for difficulty and hash rate and block time is b, for every timestep ts in your simulation, there is a (hashrate / total hashrate) * b / ts) chance that a block is mined by a particular miner.

Here is a simulation that ignores block propagation times. You can, of course, add that. How you do that depends on the properties of the network you want to simulate. You can create a crude uncle calculation by changing TIMESTEP to your block propagation time and, whenever more than one miner finds a block at the same simulation timestep, you can count that as an uncle. If you want a more involved model, for each node, you can store the chain state from the perspective of that node (with the presumption that some nodes are also miners). You can model propagation delays between individual nodes to simulate block propagations more realistically. You would then want to choose a realistic fan-in/fan-out number for each node (or simulate connections being formed and broken) and perhaps model geographic distribution of various nodes. And, of course, you could go much more deeply than that, even.

#!/usr/bin/env python3
import random

class Miner(object):
    def __init__(self, name, hashrate):
        '''Initialize a new miner named name with hashrate measured in hashes per second.'''
        self.name = name
        self.hashrate = hashrate

MINERS = [
    Miner('Alice', 30e6),
    Miner('Bob', 20e6),
]

TIMESTEP = 0.01     # Timestep of simulation
BLOCKTIME = 15      # Average number of seconds per block

TOTAL_HASHPOWER = sum([miner.hashrate for miner in MINERS])
time = 0

while True:
    for miner in MINERS:
        if (random.random() * (TOTAL_HASHPOWER / miner.hashrate)) < (TIMESTEP / BLOCKTIME):
            print("t=%0.3f: %s mined a block" % (time, miner.name))
    time += TIMESTEP
  • Thank you so much for this answer. could you please clarify what you mean by timestamp here and why it is 0.01? Also, i do not understand IF condition in the loop. I tried the code and it works fine. For block propogation time (delay), let assume once a miner found a block there is delay with a random number on average 12 seconds for the block to reah all other miners. How to calculaute uncles then, model their inclusion and resolve forks?. I would apperciate if you could extend this (block propogation time can be random time , and does not depened on locations or other network attributes). – MWH Mar 27 '18 at 12:10
  • A time step is a discrete step of time in a simulation (the smallest quantum). It also refers to the current iteration of a simulation. You can always improve the fidelity of the model until you have a real world experiment, so I’ll leave that to you. And what you choose to model is a strong function of what you want to find out. And it’s not clear what you’re modeling. Statistical models give better output without long simulations (and aren’t subject to fluke runs). For example, ethereum.stackexchange.com/questions/18201/… – lungj Mar 27 '18 at 15:27
  • Thanks. if block propogation time is 12 seconds do i havr to replace TIMESTEP in both if condition and in last staement, where time += TIMESTEP, by changing 0.01 to 12? – MWH Mar 29 '18 at 17:01
  • Block propagation time isn't modelled in this simulation. You can change the block time to 12 seconds by changing only the line BLOCKTIME = 15. – lungj Mar 30 '18 at 16:10
  • @MWH I updated my answer to include a link to a program I wrote to do a more in-depth simulation. – lungj Mar 31 '18 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.