Simulate the effectiveness of various blockchain difficulty algorithms in terms of volatility and accuracy.
In a decentralized proof-of-work blockchain, where no individual controls the timing of block additions, there must be a mechanism set in place in order to regulate the desired block frequency.
A so-called 'difficulty adjustment algorithm' can be used to adjust the difficulty of mining a new block, based on an estimate of the network's total problem solving power. With all things being equal, the block time should approach and maintain its target block time.
The goal of this project is to provide a simulator to report on the effectiveness of various difficulty algorithms by observing the standard deviation, and mean values of all block intervals after adding X blocks.
Install Dependencies:
Build and Run:
git clone git@github.com:mesosoftware/blockchain-difficulty.git
cd blockchain-difficulty
go run cmd/main.go
Configure global settings using environment variables in the project's .env
file. Options/defaults:
TARGET_BLOCK_TIME_SECONDS=600
SIMULATION_DAYS=365
INITIAL_NETWORK_HASH_POWER=1000000
LIMIT_NETWORK_HASH_POWER_PCT_CHANGE=10
Configure network and algorithm parameters in cmd/main.go
like so:
// initial difficulty = 600000000, window = 10, interval = 10
net1 := network.NewNetwork(600000000, algorithms.NewSMA(10, 10))
This project ships with examples of the following algorithms, some of which are parameterized for tuning:
- Bitcoin
- Ethereum (Homestead)
- ASERT
- Digishield
- LWMA
- EMA
- SMA
- Network power variability is expressed in terms of one value for net mining power. In the real world, network power varies due to a) miners going on/offline, and b) miners having differing computational power. The reasons for the change don't matter to us, we only care about simulating net overall change.
- Solve time is perfectly correlated to difficulty. In the real world, even with a constant difficulty and network mining power, blocks are solved in an arbitrary amount of time, due to the probablistic nature of the typical PoW mining process. This doesn't matter to us, because on average, block solve time is highly correlated to difficulty.
- With the default configuration, the difficulty is initialized to generate the perfect block time for the initial network mining power. As the mining power fluctuates throughout the simulation, the difficulty is adjusted per the selected algorithm. The simulator reports on the standard deviation in block generation times intervals over X blocks. If you wish to also factor in the effectiveness of an algorithm when the initial block difficulty is initially inappropriate for the mining power, that is configurable.
- With the default configuration, the network mining power can fluctuate by up to 10% from its initial power, each day. This seems to be normal for the Bitcoin blockchain in 2023. This daily fluctation limit is configurable.
- The simulation will run for a period of X days, instead of X blocks, since block time will vary and we want to predict effectiveness of algorithms when run for a given period of time.
- The highest resolution for block time is Seconds.
- In order to reduce the number of variables that impact the results, some parameters to the simulation should be globally applied, for example, Target Block Time. The intention is to compare the effectiveness of the Difficulty Adjustment Algorithms, not the Blockchain implementations.
- Difficulty Wiki
- Summary of Difficulty Algorithms
- Why the Bitcoin target block time is 10 minutes
- Why the Bitcoin adjustment interval is 2 weeks
- Live BTC statistics
- LWMA algorithm
- ASERT/others
- ETH Homestead
- Digishield
- Bitcoin Proof-of-Work
- ETH DAA Implementation
- Attacks
- Selfish Mining Against Multiple DAA's
- Economic Analysis of Difficulty Adjustment Algorithms