Fix difficulty adjustment start-of-epoch edge cases
This commit is contained in:
		
							parent
							
								
									5681ae3f5c
								
							
						
					
					
						commit
						5f60cb821a
					
				@ -29,7 +29,7 @@ export function calcDifficultyAdjustment(
 | 
				
			|||||||
  const BLOCK_SECONDS_TARGET = 600; // Bitcoin mainnet
 | 
					  const BLOCK_SECONDS_TARGET = 600; // Bitcoin mainnet
 | 
				
			||||||
  const TESTNET_MAX_BLOCK_SECONDS = 1200; // Bitcoin testnet
 | 
					  const TESTNET_MAX_BLOCK_SECONDS = 1200; // Bitcoin testnet
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const diffSeconds = nowSeconds - DATime;
 | 
					  const diffSeconds = Math.max(0, nowSeconds - DATime);
 | 
				
			||||||
  const blocksInEpoch = (blockHeight >= 0) ? blockHeight % EPOCH_BLOCK_LENGTH : 0;
 | 
					  const blocksInEpoch = (blockHeight >= 0) ? blockHeight % EPOCH_BLOCK_LENGTH : 0;
 | 
				
			||||||
  const progressPercent = (blockHeight >= 0) ? blocksInEpoch / EPOCH_BLOCK_LENGTH * 100 : 100;
 | 
					  const progressPercent = (blockHeight >= 0) ? blocksInEpoch / EPOCH_BLOCK_LENGTH * 100 : 100;
 | 
				
			||||||
  const remainingBlocks = EPOCH_BLOCK_LENGTH - blocksInEpoch;
 | 
					  const remainingBlocks = EPOCH_BLOCK_LENGTH - blocksInEpoch;
 | 
				
			||||||
@ -37,7 +37,7 @@ export function calcDifficultyAdjustment(
 | 
				
			|||||||
  const expectedBlocks = diffSeconds / BLOCK_SECONDS_TARGET;
 | 
					  const expectedBlocks = diffSeconds / BLOCK_SECONDS_TARGET;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  let difficultyChange = 0;
 | 
					  let difficultyChange = 0;
 | 
				
			||||||
  let timeAvgSecs = diffSeconds / blocksInEpoch;
 | 
					  let timeAvgSecs = blocksInEpoch ? diffSeconds / blocksInEpoch : BLOCK_SECONDS_TARGET;
 | 
				
			||||||
  // Only calculate the estimate once we have 7.2% of blocks in current epoch
 | 
					  // Only calculate the estimate once we have 7.2% of blocks in current epoch
 | 
				
			||||||
  if (blocksInEpoch >= ESTIMATE_LAG_BLOCKS) {
 | 
					  if (blocksInEpoch >= ESTIMATE_LAG_BLOCKS) {
 | 
				
			||||||
    difficultyChange = (BLOCK_SECONDS_TARGET / timeAvgSecs - 1) * 100;
 | 
					    difficultyChange = (BLOCK_SECONDS_TARGET / timeAvgSecs - 1) * 100;
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user