-
Notifications
You must be signed in to change notification settings - Fork 3
/
two_phase_mgr.js
64 lines (56 loc) · 2 KB
/
two_phase_mgr.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
const LOGGER = require('loglevel');
//more docs here - https://github.com/pimterry/loglevel#documentation
LOGGER.setDefaultLevel('info');
class TwoPhaseTxnMgr {
constructor(chaincodeID, view2chain) {
this.chaincodeID = chaincodeID;
this.view2chain = view2chain;
}
async InitNetworks() {
var init_promises = [];
for (var view_name in this.view2chain) {
LOGGER.info(`Init the chain for view ${view_name}`);
init_promises.push(this.view2chain[view_name].InitNetwork());
}
return Promise.all(init_promises).then(()=>{
return this;
});
}
async TwoPhaseCommit(view_names, reqID, req_secret) {
var chains = [];
for (var i = 0; i < view_names.length; i+=1) {
let view_name = view_names[i];
let chain = this.view2chain[view_name];
if (chain === undefined) {
LOGGER.error(`Fail to find the chain for view ${view_name}`);
}
chains.push(chain);
}
var prepare_reqs = [];
chains.forEach((chain)=>{
prepare_reqs.push(chain.InvokeTxn(this.chaincodeID, "Prepare", [reqID, req_secret]));
});
await Promise.all(prepare_reqs);
var commit_reqs = [];
chains.forEach((chain)=>{
commit_reqs.push(chain.InvokeTxn(this.chaincodeID, "Commit", [reqID, req_secret]));
});
await Promise.all(commit_reqs);
}
async ScanLedgersForDelayStorage() {
var scan_requests = [];
for (var view_name in this.view2chain) {
scan_requests.push(this.view2chain[view_name].ScanLedgerForDelayStorage());
}
return Promise.all(scan_requests).then((infos)=>{
var result = {};
var i = 0;
for (var view_name in this.view2chain) {
result[view_name] = infos[i];
i+=1;
}
return result;
});
}
};
module.exports.TwoPhaseTxnMgr = TwoPhaseTxnMgr;