forked from byteball/ocore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_witnesses.js
83 lines (79 loc) · 3.12 KB
/
my_witnesses.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
/*jslint node: true */
"use strict";
var db = require('./db.js');
var conf = require('./conf.js');
var constants = require('./constants.js');
var storage = require('./storage.js');
var ValidationUtils = require("./validation_utils.js");
function readMyWitnesses(handleWitnesses, actionIfEmpty){
db.query("SELECT address FROM my_witnesses ORDER BY address", function(rows){
var arrWitnesses = rows.map(function(row){ return row.address; });
// reset witness list if old witnesses found
if (constants.alt === '2' && arrWitnesses.indexOf('5K7CSLTRPC5LFLOS3D34GBHG7RFD4TPO') >= 0
|| constants.versionWithoutTimestamp === '1.0' && arrWitnesses.indexOf('2FF7PSL7FYXVU5UIQHCVDTTPUOOG75GX') >= 0
){
console.log('deleting old witnesses');
db.query("DELETE FROM my_witnesses");
arrWitnesses = [];
}
if (arrWitnesses.length === 0){
if (actionIfEmpty === 'ignore')
return handleWitnesses([]);
if (actionIfEmpty === 'wait'){
console.log('no witnesses yet, will retry later');
setTimeout(function(){
readMyWitnesses(handleWitnesses, actionIfEmpty);
}, 1000);
return;
}
}
if (arrWitnesses.length !== constants.COUNT_WITNESSES)
throw Error("wrong number of my witnesses: "+arrWitnesses.length);
handleWitnesses(arrWitnesses);
});
}
// replaces old_witness with new_witness
function replaceWitness(old_witness, new_witness, handleResult){
if (!ValidationUtils.isValidAddress(new_witness))
return handleResult("new witness address is invalid");
readMyWitnesses(function(arrWitnesses){
if (arrWitnesses.indexOf(old_witness) === -1)
return handleResult("old witness not known");
if (arrWitnesses.indexOf(new_witness) >= 0)
return handleResult("new witness already present");
var doReplace = function(){
db.query("UPDATE my_witnesses SET address=? WHERE address=?", [new_witness, old_witness], function(){
handleResult();
});
};
if (conf.bLight) // absent the full database, there is nothing else to check
return doReplace();
db.query(
"SELECT 1 FROM unit_authors CROSS JOIN units USING(unit) WHERE address=? AND sequence='good' AND is_stable=1 LIMIT 1",
[new_witness],
function(rows){
if (rows.length === 0)
return handleResult("no stable messages from the new witness yet");
storage.determineIfWitnessAddressDefinitionsHaveReferences(db, [new_witness], function(bHasReferences){
if (bHasReferences)
return handleResult("address definition of the new witness has or had references");
doReplace();
});
}
);
});
}
function insertWitnesses(arrWitnesses, onDone){
if (arrWitnesses.length !== constants.COUNT_WITNESSES)
throw Error("attempting to insert wrong number of witnesses: "+arrWitnesses.length);
var placeholders = Array.apply(null, Array(arrWitnesses.length)).map(function(){ return '(?)'; }).join(',');
console.log('will insert witnesses', arrWitnesses);
db.query("INSERT INTO my_witnesses (address) VALUES "+placeholders, arrWitnesses, function(){
console.log('inserted witnesses');
if (onDone)
onDone();
});
}
exports.readMyWitnesses = readMyWitnesses;
exports.replaceWitness = replaceWitness;
exports.insertWitnesses = insertWitnesses;