This repository has been archived by the owner on Jun 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
validate.js
94 lines (66 loc) · 2.65 KB
/
validate.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
var request = require('request');
// Declare API endpoint for the IOTA node
var iotaNode = 'http://localhost:14265';
var snapshotUrl = {PLACE HOLDER FOR SNAPHOT_URL};
var milestoneIndex = {PLACE HOLDER FOR MILESTONE_INDEX};
var snapshot = function() {
var command = {
'command': 'Snapshot.getState',
'milestoneIndex': milestoneIndex
}
var options = {
url: iotaNode,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(JSON.stringify(command)),
'X-IOTA-API-Version': 1
},
json: command
};
request(options, function (error, response, data) {
if (!error && response.statusCode == 200) {
var latestState = data.ixi.state;
// FIRST VALIDATION
// Check if total sum is equal to the supply 2779530283277761
var totalSupply = (Math.pow(3, 33) - 1) / 2
var snapshotBalance = 0;
for (var key in latestState) {
if (latestState.hasOwnProperty(key)) {
snapshotBalance += parseInt(latestState[key]);
}
}
console.log("BALANCE CORRECT: ", snapshotBalance === totalSupply);
validateSnapshot(latestState);
} else {
console.log("COULD NOT PROCESS REQUEST!", error);
}
});
}
var validateSnapshot = function(latestState) {
// Snapshot as posted by Andreas O (Core Developer of IOTA)
Object.keys(latestState).forEach((key) => {
if(latestState[key] == 0) {
delete latestState[key];
}
});
request(snapshotUrl, function (error, response, body) {
var snapshot = body.split("\n").filter(l => l.length > 0).map(l => l.split(";"));
var numEntries = snapshot.length;
console.log("VALIDATING SNAPSHOT ENTRIES: ", numEntries);
// We now compare the snapshot to the latest state
snapshot.forEach(function(entry) {
var address = entry[0];
var balance = parseInt(entry[1]);
var sameBalance = parseInt(latestState[address]) === parseInt(balance);
if (!sameBalance) {
console.log("FATAL ERROR: Balance incorrect for: ", address);
console.log("Balance (proposed snapshot vs. local): ", balance, parseInt(latestState[address]))
}
// now we remove the address from the latestState
delete latestState[address];
})
console.log("LATEST STATE EQUALS SNAPSHOT: ", Object.keys(latestState).length === 0 && latestState.constructor === Object);
})
}
snapshot()