forked from layr-team/Layr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.js
172 lines (148 loc) · 6.18 KB
/
start.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
const bunyan = require('bunyan');
const levelup = require('levelup');
const leveldown = require('leveldown');
const encoding = require('encoding-down');
const kad = require('@kadenceproject/kadence');
const BatNode = require('./batnode.js').BatNode;
const kad_bat = require('./kadence_plugin').kad_bat;
const stellar_account = require('./kadence_plugin').stellar_account;
const seed = require('./constants').SEED_NODE;
const cliServer = require('./constants').CLI_SERVER;
const batNodePort = require('./constants').BATNODE_SERVER_PORT
const kadNodePort = require('./constants').KADNODE_PORT
const publicIp = require('public-ip');
const fs = require('fs');
const fileUtils = require('./utils/file').fileSystem;
const JSONStream = require('JSONStream');
const backoff = require('backoff');
const crypto = require('crypto');
const base32 = require('base32');
publicIp.v4().then(ip => {
const kademliaNode = new kad.KademliaNode({
transport: new kad.HTTPTransport(),
storage: levelup(encoding(leveldown('./dbbb'))),
contact: {hostname: ip, port: kadNodePort}
})
kademliaNode.plugin(kad_bat)
kademliaNode.plugin(stellar_account)
kademliaNode.listen(kadNodePort)
const batNode = new BatNode(kademliaNode)
kademliaNode.batNode = batNode
const nodeConnectionCallback = (serverConnection) => {
serverConnection.on('end', () => {
console.log('end')
})
const stream = JSONStream.parse();
serverConnection.pipe(stream);
stream.on('data', (receivedData, error) => {
if (error) { throw error; }
console.log("received data: ", receivedData)
if (receivedData.messageType === "RETRIEVE_FILE") {
const filePath = './hosted/' + receivedData.fileName;
const readable = fs.createReadStream(filePath);
readable.on('data', (chunk) => {
serverConnection.write(chunk);
});
readable.on('end', () => {
console.log(`finish sending ${receivedData.fileName}`)
});
} else if (receivedData.messageType === "STORE_FILE"){
let fileName = receivedData.fileName
let fileContent = Buffer.from(receivedData.fileContent)
let preimage = fileUtils.sha1HashData(fileContent)
let escrowAccountId = receivedData.escrow;
batNode.acceptPayment(preimage, escrowAccountId)
batNode.kadenceNode.iterativeStore(fileName, [batNode.kadenceNode.identity.toString(), batNode.kadenceNode.contact], (err, stored) => {
console.log('nodes who stored this value: ', stored)
let fileContent = new Buffer(receivedData.fileContent);
let storeStream = fs.createWriteStream("./hosted/" + fileName);
storeStream.write(fileContent, function (writeErr) {
if(writeErr){
throw writeErr;
}
serverConnection.write(JSON.stringify({messageType: "SUCCESS"}));
});
})
} else if (receivedData.messageType === "AUDIT_FILE") {
fs.exists(`./hosted/${receivedData.fileName}`, (doesExist) => {
if (doesExist) {
fs.readFile(`./hosted/${receivedData.fileName}`, (err, data) => {
const shardSha1 = fileUtils.sha1HashData(data);
serverConnection.write(shardSha1);
});
} else {
serverConnection.write("Shard not found")
}
})
} else if (receivedData.messageType === "PATCH_FILE") {
const filePath = './hosted/' + receivedData.fileName;
const readable = fs.createReadStream(filePath);
readable.on('data', (chunk) => {
serverConnection.write(chunk);
});
readable.on('end', () => {
// timeout enables to send a separate individual chunk without attaching to the previous chunk so client receive correctly
setTimeout(function() {
serverConnection.write("finish");
}, 500);
console.log(`finish sending ${receivedData.fileName}`)
});
}
})
}
const nodeCLIConnectionCallback = (serverConnection) => {
const sendAuditDataWhenFinished = (exponentialBackoff) => {
exponentialBackoff.failAfter(10);
exponentialBackoff.on('backoff', function(number, delay) {
console.log(number + ' ' + delay + 'ms');
});
exponentialBackoff.on('ready', function() {
if (!batNode.audit.ready) {
exponentialBackoff.backoff();
} else {
serverConnection.write(JSON.stringify(batNode.audit));
return;
}
});
exponentialBackoff.on('fail', function() {
console.log('Timeout: failed to complete audit');
});
exponentialBackoff.backoff();
}
serverConnection.on('data', (data) => {
let receivedData = JSON.parse(data);
if (receivedData.messageType === "CLI_UPLOAD_FILE") {
let filePath = receivedData.filePath;
batNode.uploadFile(filePath)
} else if (receivedData.messageType === "CLI_DOWNLOAD_FILE") {
let filePath = receivedData.filePath;
batNode.retrieveFile(filePath);
} else if (receivedData.messageType === "CLI_AUDIT_FILE") {
let filePath = receivedData.filePath;
let exponentialBackoff = backoff.exponential({
randomisationFactor: 0,
initialDelay: 20,
maxDelay: 10000
});
batNode.auditFile(filePath);
// post audit cleanup
serverConnection.on('close', () => {
batNode.audit.ready = false;
batNode.audit.data = null;
batNode.audit.passed = false;
batNode.audit.failed = [];
});
// Exponential backoff until file audit finishes
sendAuditDataWhenFinished(exponentialBackoff);
} else if (receivedData.messageType === "CLI_PATCH_FILE") {
const { manifestPath, siblingShardId, failedShaId, copiesToRemoveFromManifest } = receivedData;
batNode.patchFile(manifestPath, failedShaId, siblingShardId, copiesToRemoveFromManifest)
}
})
}
batNode.createCLIServer(cliServer.port, cliServer.host, nodeCLIConnectionCallback);
batNode.createServer(batNodePort, ip, nodeConnectionCallback)
kademliaNode.join(seed, () => {
console.log('you have joined the network! Ready to accept commands from the CLI!')
})
})