-
Notifications
You must be signed in to change notification settings - Fork 96
/
proof_chain.js
154 lines (145 loc) · 5.41 KB
/
proof_chain.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*jslint node: true */
"use strict";
var async = require('async');
var db = require('./db.js');
var graph = require('./graph.js');
// unit's MC index is earlier_mci
function buildProofChain(later_mci, earlier_mci, unit, arrBalls, onDone){
if (earlier_mci === null)
throw Error("earlier_mci=null, unit="+unit);
if (later_mci === earlier_mci)
return buildLastMileOfProofChain(earlier_mci, unit, arrBalls, onDone);
buildProofChainOnMc(later_mci, earlier_mci, arrBalls, function(){
buildLastMileOfProofChain(earlier_mci, unit, arrBalls, onDone);
});
}
// later_mci is already known and not included in the chain
function buildProofChainOnMc(later_mci, earlier_mci, arrBalls, onDone){
function addBall(mci){
if (mci < 0)
throw Error("mci<0, later_mci="+later_mci+", earlier_mci="+earlier_mci);
db.query("SELECT unit, ball, content_hash FROM units JOIN balls USING(unit) WHERE main_chain_index=? AND is_on_main_chain=1", [mci], function(rows){
if (rows.length !== 1)
throw Error("no prev chain element? mci="+mci+", later_mci="+later_mci+", earlier_mci="+earlier_mci);
var objBall = rows[0];
if (objBall.content_hash)
objBall.is_nonserial = true;
delete objBall.content_hash;
db.query(
"SELECT ball FROM parenthoods LEFT JOIN balls ON parent_unit=balls.unit WHERE child_unit=? ORDER BY ball",
[objBall.unit],
function(parent_rows){
if (parent_rows.some(function(parent_row){ return !parent_row.ball; }))
throw Error("some parents have no balls");
if (parent_rows.length > 0)
objBall.parent_balls = parent_rows.map(function(parent_row){ return parent_row.ball; });
db.query(
"SELECT ball, main_chain_index \n\
FROM skiplist_units JOIN units ON skiplist_unit=units.unit LEFT JOIN balls ON units.unit=balls.unit \n\
WHERE skiplist_units.unit=? ORDER BY ball",
[objBall.unit],
function(srows){
if (srows.some(function(srow){ return !srow.ball; }))
throw Error("some skiplist units have no balls");
if (srows.length > 0)
objBall.skiplist_balls = srows.map(function(srow){ return srow.ball; });
arrBalls.push(objBall);
if (mci === earlier_mci)
return onDone();
if (srows.length === 0) // no skiplist
return addBall(mci-1);
var next_mci = mci - 1;
for (var i=0; i<srows.length; i++){
var next_skiplist_mci = srows[i].main_chain_index;
if (next_skiplist_mci < next_mci && next_skiplist_mci >= earlier_mci)
next_mci = next_skiplist_mci;
}
addBall(next_mci);
}
);
}
);
});
}
if (earlier_mci > later_mci)
throw Error("earlier > later");
if (earlier_mci === later_mci)
return onDone();
addBall(later_mci - 1);
}
// unit's MC index is mci, find a path from mci unit to this unit
function buildLastMileOfProofChain(mci, unit, arrBalls, onDone){
function addBall(_unit){
db.query("SELECT unit, ball, content_hash FROM units JOIN balls USING(unit) WHERE unit=?", [_unit], function(rows){
if (rows.length !== 1)
throw Error("no unit?");
var objBall = rows[0];
if (objBall.content_hash)
objBall.is_nonserial = true;
delete objBall.content_hash;
db.query(
"SELECT ball FROM parenthoods LEFT JOIN balls ON parent_unit=balls.unit WHERE child_unit=? ORDER BY ball",
[objBall.unit],
function(parent_rows){
if (parent_rows.some(function(parent_row){ return !parent_row.ball; }))
throw Error("some parents have no balls");
if (parent_rows.length > 0)
objBall.parent_balls = parent_rows.map(function(parent_row){ return parent_row.ball; });
db.query(
"SELECT ball \n\
FROM skiplist_units JOIN units ON skiplist_unit=units.unit LEFT JOIN balls ON units.unit=balls.unit \n\
WHERE skiplist_units.unit=? ORDER BY ball",
[objBall.unit],
function(srows){
if (srows.some(function(srow){ return !srow.ball; }))
throw Error("last mile: some skiplist units have no balls");
if (srows.length > 0)
objBall.skiplist_balls = srows.map(function(srow){ return srow.ball; });
arrBalls.push(objBall);
if (_unit === unit)
return onDone();
findParent(_unit);
}
);
}
);
});
}
function findParent(interim_unit){
db.query(
"SELECT parent_unit FROM parenthoods JOIN units ON parent_unit=unit WHERE child_unit=? AND main_chain_index=?",
[interim_unit, mci],
function(parent_rows){
var arrParents = parent_rows.map(function(parent_row){ return parent_row.parent_unit; });
if (arrParents.indexOf(unit) >= 0)
return addBall(unit);
if (arrParents.length === 1) // only one parent, nothing to choose from
return addBall(arrParents[0]);
async.eachSeries(
arrParents,
function(parent_unit, cb){
graph.determineIfIncluded(db, unit, [parent_unit], function(bIncluded){
bIncluded ? cb(parent_unit) : cb();
});
},
function(parent_unit){
if (!parent_unit)
throw Error("no parent that includes target unit");
addBall(parent_unit);
}
)
}
);
}
// start from MC unit and go back in history
db.query("SELECT unit FROM units WHERE main_chain_index=? AND is_on_main_chain=1", [mci], function(rows){
if (rows.length !== 1)
throw Error("no mc unit?");
var mc_unit = rows[0].unit;
if (mc_unit === unit)
return onDone();
findParent(mc_unit);
});
}
exports.buildProofChain = buildProofChain;
exports.buildProofChainOnMc = buildProofChainOnMc;