This repository has been archived by the owner on Mar 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
local-start.js
153 lines (129 loc) · 5.37 KB
/
local-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
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');
const kademliaNode = new kad.KademliaNode({
transport: new kad.HTTPTransport(),
storage: levelup(encoding(leveldown('./dbbb'))),
contact: {hostname: '0.0.0.0', port: 1705}
})
kademliaNode.plugin(kad_bat)
kademliaNode.plugin(stellar_account)
kademliaNode.listen(1705)
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") {
batNode.readFile(`./hosted/${receivedData.fileName}`, (err, data) => {
serverConnection.write(data)
})
} else if (receivedData.messageType === "STORE_FILE"){
let fileName = receivedData.fileName
let nonce = Buffer.from(receivedData.nonce);
let fileContent = Buffer.from(receivedData.fileContent)
let preimage = fileUtils.sha1HashData(fileContent, nonce)
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)
batNode.writeFile(`./hosted/${fileName}`, fileContent, (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")
}
})
}
})
}
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, 0, () => {
serverConnection.write("File has finished uploading")
})
} 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, '0.0.0.0', nodeConnectionCallback)
kademliaNode.join(seed, () => {
console.log('you have joined the network! Ready to accept commands from the CLI!')
})