This repository has been archived by the owner on Jun 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
StateSync.js
95 lines (82 loc) · 3.69 KB
/
StateSync.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
95
const parallel = require('async/parallel');
const util = require('util')
const _ = require('underscore');
/**
* Queries the Enigma contract and returns the missing states in comparison to the local tips
* @param {EnigmaContractReaderAPI} api
* @param {Array} localTips [{address,key,delta},...}]
* @param {Function} callback (err, results)=>{}
* @return {Array} missing states [{address, deltas : [deltaHash, index]}]
* TODO: as async function returns a Promise, apply both options: return the data using the callback and return the data using the promise
* */
async function getRemoteMissingStates(api, localTips, callback) {
// create a hashmap from the localTipa array
const tipsHashMaps = localTips.reduce((obj, item) => {
obj[item.address] = item.key;
return obj
}, {});
try {
let remoteSecretContractNumber = await api.countSecretContracts();
try {
let remoteSecretContractsAddresses = await api.getSecretContractAddresses(0, remoteSecretContractNumber);
// initiate jobs
let jobs = [];
remoteSecretContractsAddresses.forEach((secretContractAddress)=>{
jobs.push((cb)=>{
api.countStateDeltas(secretContractAddress)
.then((deltasNumber)=>{
let firstMissingIndex = 0;
// get the local tip index, if exists; otherwise 0
if (secretContractAddress in tipsHashMaps) {
firstMissingIndex = tipsHashMaps[secretContractAddress] + 1;
}
// there are no missing deltas for this secret contract address
if (deltasNumber === firstMissingIndex) {
return cb(null);
}
else {// (deltasNumber > firstMissingIndex) {
api.getStateDeltaHashes(secretContractAddress, firstMissingIndex, deltasNumber)
.then((deltasArray)=>{
let parsedDeltasArray = [];
deltasArray.forEach((deltaHash, index, arr)=>{
parsedDeltasArray.push({deltaHash : deltaHash, index : index + firstMissingIndex});
});
return cb(null, {address : secretContractAddress, deltas : parsedDeltasArray});
})
.catch((err)=>{cb(err)});;
}
})
.catch((err)=>{cb(err)});
});
});
parallel(jobs, (err, results)=>{
if (err) {
return callback(err);
}
// Filter out undefined - due to synced addresses
var filtered = _.filter(results, function(x) {
return (x===undefined ? false : true);
});
return callback(null, filtered);
});
} catch (err) {
callback(err);
}
} catch (err) {
callback(err);
}
}
module.exports = {getRemoteMissingStates: getRemoteMissingStates};
// function asyncGetMissingStates(eth,localTips){
// return new Promise((resolve,reject)=>{
// getMissingStates(eth,localTips,(err,result)=>{
// if(err){
// reject(err);
// }else{
// resolve(result);
// }
// }).catch(e=>{
// reject(e);
// });
// });
// }