I read through How is difficulty calculated? and want to understand where the "off-by-one" bug is in calculating difficulty. Here is a Matlab snipet I wrote to calculate difficulty. What are the correct block step intervals that I should use? Note: minv
and maxv
are the block heights (plus 1) of the interval. Thus, time stamps from block heights 0 and 2015 are used in the first difficulty calculation.
% Calculate difficulty
num = floor(length(block_chain)/2016);
minv = 1;
maxv = 2016;
target = 1209600;
difficulty = ones(num,4);
delta = zeros(num,1);
timedif = delta;
for i = 1:num
if i == 1
difficulty(1,3) = 1;
timedif(i) = block_chain(maxv,2) - block_chain(minv,2);
else
timedif(i) = block_chain(maxv,2) - block_chain(minv,2);
delta(i) = max(0.25,min(target/timedif(i),4));
difficulty(i,3) = max(1,difficulty(i-1,3)*delta(i));
end
difficulty(i,:) = [block_chain(minv,1), block_chain(maxv,1),...
difficulty(i,3), timedif(i)];
minv = maxv + 1;
maxv = maxv + 2016;
end