-
Notifications
You must be signed in to change notification settings - Fork 94
/
archiving.js
174 lines (161 loc) · 7.9 KB
/
archiving.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*jslint node: true */
"use strict";
var db = require('./db.js');
function generateQueriesToArchiveJoint(conn, objJoint, reason, arrQueries, cb){
var func = (reason === 'uncovered') ? generateQueriesToRemoveJoint : generateQueriesToVoidJoint;
func(conn, objJoint.unit.unit, arrQueries, function(){
conn.addQuery(arrQueries, "INSERT "+conn.getIgnore()+" INTO archived_joints (unit, reason, json) VALUES (?,?,?)",
[objJoint.unit.unit, reason, JSON.stringify(objJoint)]);
cb();
});
}
function generateQueriesToRemoveJoint(conn, unit, arrQueries, cb){
generateQueriesToUnspendOutputsSpentInArchivedUnit(conn, unit, arrQueries, function(){
conn.addQuery(arrQueries, "DELETE FROM aa_responses WHERE trigger_unit=? OR response_unit=?", [unit, unit]);
conn.addQuery(arrQueries, "DELETE FROM original_addresses WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM sent_mnemonics WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM witness_list_hashes WHERE witness_list_unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM earned_headers_commission_recipients WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM unit_witnesses WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM unit_authors WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM parenthoods WHERE child_unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM address_definition_changes WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM inputs WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM outputs WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM spend_proofs WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM poll_choices WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM polls WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM votes WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM attested_fields WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM attestations WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM asset_metadata WHERE asset=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM asset_denominations WHERE asset=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM asset_attestors WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM assets WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM messages WHERE unit=?", [unit]);
// conn.addQuery(arrQueries, "DELETE FROM balls WHERE unit=?", [unit]); // if it has a ball, it can't be uncovered
conn.addQuery(arrQueries, "DELETE FROM units WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM joints WHERE unit=?", [unit]);
cb();
});
}
function generateQueriesToVoidJoint(conn, unit, arrQueries, cb){
generateQueriesToUnspendOutputsSpentInArchivedUnit(conn, unit, arrQueries, function(){
// we keep witnesses, author addresses, and the unit itself
conn.addQuery(arrQueries, "DELETE FROM witness_list_hashes WHERE witness_list_unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM earned_headers_commission_recipients WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "UPDATE unit_authors SET definition_chash=NULL WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM address_definition_changes WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM inputs WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM outputs WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM spend_proofs WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM poll_choices WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM polls WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM votes WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM attested_fields WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM attestations WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM asset_metadata WHERE asset=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM asset_denominations WHERE asset=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM asset_attestors WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM assets WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM messages WHERE unit=?", [unit]);
cb();
});
}
function generateQueriesToUnspendOutputsSpentInArchivedUnit(conn, unit, arrQueries, cb){
generateQueriesToUnspendTransferOutputsSpentInArchivedUnit(conn, unit, arrQueries, function(){
generateQueriesToUnspendHeadersCommissionOutputsSpentInArchivedUnit(conn, unit, arrQueries, function(){
generateQueriesToUnspendWitnessingOutputsSpentInArchivedUnit(conn, unit, arrQueries, cb);
});
});
}
function generateQueriesToUnspendTransferOutputsSpentInArchivedUnit(conn, unit, arrQueries, cb){
conn.query(
"SELECT src_unit, src_message_index, src_output_index \n\
FROM inputs \n\
WHERE inputs.unit=? \n\
AND inputs.type='transfer' \n\
AND NOT EXISTS ( \n\
SELECT 1 FROM inputs AS alt_inputs \n\
WHERE inputs.src_unit=alt_inputs.src_unit \n\
AND inputs.src_message_index=alt_inputs.src_message_index \n\
AND inputs.src_output_index=alt_inputs.src_output_index \n\
AND alt_inputs.type='transfer' \n\
AND inputs.unit!=alt_inputs.unit \n\
)",
[unit],
function(rows){
rows.forEach(function(row){
conn.addQuery(
arrQueries,
"UPDATE outputs SET is_spent=0 WHERE unit=? AND message_index=? AND output_index=?",
[row.src_unit, row.src_message_index, row.src_output_index]
);
});
cb();
}
);
}
function generateQueriesToUnspendHeadersCommissionOutputsSpentInArchivedUnit(conn, unit, arrQueries, cb){
conn.query(
"SELECT headers_commission_outputs.address, headers_commission_outputs.main_chain_index \n\
FROM inputs \n\
CROSS JOIN headers_commission_outputs \n\
ON inputs.from_main_chain_index <= +headers_commission_outputs.main_chain_index \n\
AND inputs.to_main_chain_index >= +headers_commission_outputs.main_chain_index \n\
AND inputs.address = headers_commission_outputs.address \n\
WHERE inputs.unit=? \n\
AND inputs.type='headers_commission' \n\
AND NOT EXISTS ( \n\
SELECT 1 FROM inputs AS alt_inputs \n\
WHERE headers_commission_outputs.main_chain_index >= alt_inputs.from_main_chain_index \n\
AND headers_commission_outputs.main_chain_index <= alt_inputs.to_main_chain_index \n\
AND inputs.address=alt_inputs.address \n\
AND alt_inputs.type='headers_commission' \n\
AND inputs.unit!=alt_inputs.unit \n\
)",
[unit],
function(rows){
rows.forEach(function(row){
conn.addQuery(
arrQueries,
"UPDATE headers_commission_outputs SET is_spent=0 WHERE address=? AND main_chain_index=?",
[row.address, row.main_chain_index]
);
});
cb();
}
);
}
function generateQueriesToUnspendWitnessingOutputsSpentInArchivedUnit(conn, unit, arrQueries, cb){
conn.query(
"SELECT witnessing_outputs.address, witnessing_outputs.main_chain_index \n\
FROM inputs \n\
CROSS JOIN witnessing_outputs \n\
ON inputs.from_main_chain_index <= +witnessing_outputs.main_chain_index \n\
AND inputs.to_main_chain_index >= +witnessing_outputs.main_chain_index \n\
AND inputs.address = witnessing_outputs.address \n\
WHERE inputs.unit=? \n\
AND inputs.type='witnessing' \n\
AND NOT EXISTS ( \n\
SELECT 1 FROM inputs AS alt_inputs \n\
WHERE witnessing_outputs.main_chain_index >= alt_inputs.from_main_chain_index \n\
AND witnessing_outputs.main_chain_index <= alt_inputs.to_main_chain_index \n\
AND inputs.address=alt_inputs.address \n\
AND alt_inputs.type='witnessing' \n\
AND inputs.unit!=alt_inputs.unit \n\
)",
[unit],
function(rows){
rows.forEach(function(row){
conn.addQuery(
arrQueries,
"UPDATE witnessing_outputs SET is_spent=0 WHERE address=? AND main_chain_index=?",
[row.address, row.main_chain_index]
);
});
cb();
}
);
}
exports.generateQueriesToArchiveJoint = generateQueriesToArchiveJoint;