I have implemented private chain in ethereum but its difficulty level increases by time as a result the mining process slows down. Is it possible to make the difficulty static or disable it in testnet.
Ethereum Mining – How to Make Ethereum Mining Difficulty Static for a Private Chain?
difficultygo-ethereumminingprivate-blockchaintestnets
Related Solutions
No, you won't be able to get past the point at which you changed the difficulty algorithm. You'll have to start from scratch, I'm afraid.
The error you're seeing is coming from the ValidateHeader()
function, which, surprisingly, attempts to validate the values contained in the header of the current block being imported.
In short, the Block Header Validity section of the Yellow Paper states the following:
(51)
Hd = D(H)
Which is a cryptic way of stating that the difficulty in the current block's header must match the calculated expected difficulty, given the difficulty of the block's parent, and the current block number.
expd := CalcDifficulty(config, header.Time.Uint64(), parent.Time.Uint64(), parent.Number, parent.Difficulty)
if expd.Cmp(header.Difficulty) != 0 {
return fmt.Errorf("Difficulty check failed for header (remote: %v local: %v)", header.Difficulty, expd)
}
Your CalcDifficulty()
function - which you've altered - is now returning a value which doesn't match the block header of a block which was created before you made the change.
Edit:
You could of course edit the code again to either remove or temporarily disable this check, but you'd forever have the discrepancy in your chain. It'd be up to you to decide if that matters or not.
- Ensure you use geth that you have built, not geth installed somewhere in system.
- All nodes in your private network must have the same version of geth.
Here is the patch to mine 1 block per 1 second (I'm not sure, but I think it is as fast as possible):
diff --git a/core/block_validator.go b/core/block_validator.go index 65f9753..2b19c64 100644 --- a/core/block_validator.go +++ b/core/block_validator.go @@ -263,11 +263,7 @@ func ValidateHeader(config *params.ChainConfig, pow pow.PoW, header *types.Heade // the difficulty that a new block should have when created at time // given the parent block's time and difficulty. func CalcDifficulty(config *params.ChainConfig, time, parentTime uint64, parentNumber, parentDiff *big.Int) *big.Int { - if config.IsHomestead(new(big.Int).Add(parentNumber, common.Big1)) { - return calcDifficultyHomestead(time, parentTime, parentNumber, parentDiff) - } else { - return calcDifficultyFrontier(time, parentTime, parentNumber, parentDiff) - } + return big.NewInt(1) } func calcDifficultyHomestead(time, parentTime uint64, parentNumber, parentDiff *big.Int) *big.Int { diff --git a/miner/worker.go b/miner/worker.go index 77e4e02..e92ff62 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -422,7 +422,7 @@ func (self *worker) commitNewWork() { tstamp = parent.Time().Int64() + 1 } // this will ensure we're not going off too far in the future - if now := time.Now().Unix(); tstamp > now+4 { + if now := time.Now().Unix(); tstamp > now { wait := time.Duration(tstamp-now) * time.Second glog.V(logger.Info).Infoln("We are too far in the future. Waiting for", wait) time.Sleep(wait)
Mine with only thread
miner.start(1);
UPDATE
Diff for Geth 1.6.1
diff --git a/consensus/ethash/consensus.go b/consensus/ethash/consensus.go
index 2930032..d141678 100644
--- a/consensus/ethash/consensus.go
+++ b/consensus/ethash/consensus.go
@@ -289,10 +289,11 @@ func (ethash *Ethash) verifyHeader(chain consensus.ChainReader, header, parent *
//
// TODO (karalabe): Move the chain maker into this package and make this private!
func CalcDifficulty(config *params.ChainConfig, time, parentTime uint64, parentNumber, parentDiff *big.Int) *big.Int {
- if config.IsHomestead(new(big.Int).Add(parentNumber, common.Big1)) {
- return calcDifficultyHomestead(time, parentTime, parentNumber, parentDiff)
- }
- return calcDifficultyFrontier(time, parentTime, parentNumber, parentDiff)
+ // if config.IsHomestead(new(big.Int).Add(parentNumber, common.Big1)) {
+ // return calcDifficultyHomestead(time, parentTime, parentNumber, parentDiff)
+ // }
+ // return calcDifficultyFrontier(time, parentTime, parentNumber, parentDiff)
+ return big.NewInt(1)
}
// Some weird constants to avoid constant memory allocs for them.
diff --git a/miner/worker.go b/miner/worker.go
index 01241b3..f1b3839 100644
--- a/miner/worker.go
+++ b/miner/worker.go
@@ -407,7 +407,7 @@ func (self *worker) commitNewWork() {
tstamp = parent.Time().Int64() + 1
}
// this will ensure we're not going off too far in the future
- if now := time.Now().Unix(); tstamp > now+1 {
+ if now := time.Now().Unix(); tstamp > now {
wait := time.Duration(tstamp-now) * time.Second
log.Info("Mining too far in the future", "wait", common.PrettyDuration(wait))
time.Sleep(wait)
Best Answer
Modify the
CalcDifficulty
in Geth to return a static number, then rebuild Geth. Example:Source: Answer to Is it possible to change the block target time?