-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdifficulty.js
56 lines (46 loc) · 1.79 KB
/
difficulty.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
var fs = require('fs');
var config = require('./config.js')
const difficultyHistoryFile = config.difficultyHistoryFile
module.exports = {
check: function (timestamp, hash)
{
// iterate through each stamp in history, seeing where this timestamp fits
var history = JSON.parse(fs.readFileSync(difficultyHistoryFile))
// check if timestamp matches something
for (var i = 0, n = history.length; i < n; i++)
{
if (history[i].startTime < timestamp && history[i].endTime > timestamp)
return hash.substring(0, history[i].difficulty) === Array(history[i].difficulty + 1).join("0")
}
// check if timestamp is in last slot
for (var i = 0, n = history.length; i < n; i++)
{
if (history[i].startTime * history[i].endTime < 0)
return hash.substring(0, history[i].difficulty) === Array(history[i].difficulty + 1).join("0")
}
return false // timestamp cannot be found in intervals
},
// returns the difficulty for a specific timestamp or 0 if timestamp invalid
retrieve: function (timestamp, hash)
{
// iterate through each stamp in history, seeing where this timestamp fits
var history = JSON.parse(fs.readFileSync(difficultyHistoryFile))
// check if timestamp matches something
for (var i = 0, n = history.length; i < n; i++)
{
if (history[i].startTime < timestamp && history[i].endTime > timestamp)
{
if (hash.substring(0, history[i].difficulty) === Array(history[i].difficulty + 1).join("0"))
return history[i].difficulty
}
}
// check if timestamp is in last slot
for (var i = 0, n = history.length; i < n; i++)
{
if (history[i].startTime * history[i].endTime < 0)
if (hash.substring(0, history[i].difficulty) === Array(history[i].difficulty + 1).join("0"))
return history[i].difficulty
}
return 0 // timestamp cannot be found in intervals
}
}