-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
558 lines (518 loc) · 19.7 KB
/
server.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//Back to basic, no route mess, everything are here!!!!
//Will later divide this code to support project scale up
//fs will only be used to overwrite mockup database, will replace mockup with actual database in real deployment
const express = require('express'); //Loadup Express
const app = express();
const http = require("http"); //Loadup http for server
const PORT = 1234; //Assign listening port
const jwt = require('jsonwebtoken'); //jsonwebtoken for authentication
const bcrypt = require('bcryptjs'); //bcryptjs for authentication
const crypto = require('crypto');
const fs = require('fs');
const cron = require('node-cron');
const net = require('net');
//Loadup database
//const db = require("../config/db"); // real db not ready yet
const rawData = fs.readFileSync('config/mockdb.json');
//const db = require("./config/mockdb"); //use mockdb just for demonstration
const db = JSON.parse(rawData); //use mockdb just for demonstration
const users = db['users'];
const assets = db['assets'];
const activeGroup = db['activeGroup'];
const { jwtSecret } = require("./config/config");
//Loadup smartcontract and blockchain interface
const Web3 = require('web3');
//Then setup connection to our prefer node
const web3 = new Web3('http://localhost:8545');
// Load middleware
const cors = require('cors');
const bodyParser = require('body-parser');
//include bodyParser to deal with HTTP POST request
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//include cors, allow cross-origin resource sharing
app.use(cors());
//Function section =================================
//authenticateJWT verify if the request is from the correct user
function authenticateJWT(req, res, next) {
const authHeader = req.headers.authorization;
if (authHeader) {
const token = authHeader.split(' ')[1];
try {
const decoded = jwt.verify(token, jwtSecret);
req.user = users.find((user) => user.id === decoded.id);
next();
} catch (error) {
res.status(401).json({ message: 'Invalid token' });
}
} else {
res.status(401).json({ message: 'Missing token' });
}
}
//update change to mockup database
function mockupDBUpdate (updatedDB) {
const jsonContent = JSON.stringify(updatedDB);
fs.writeFileSync('config/mockdb.json', jsonContent);
console.log('mockup database updated...');
// Return a resolved Promise to signal that the function has completed
return Promise.resolve();
}
//check for status change on group in local database
function groupStatusCheck (groupId) {
console.log('Check for ready status of group ' + groupId);
for (var i=0; i<activeGroup.length; i++) {
if (activeGroup[i].id === groupId) {
var readyCount = 0;
const checkingGroup = activeGroup[i];
const maxGroupMember = checkingGroup.groupMembers.length;
for (var j=0; j<maxGroupMember; j++) {
const checkingMember = checkingGroup.groupMembers[j];
if (checkingMember.readyStatus == true) {
readyCount++;
}
}
if (readyCount == maxGroupMember) {
console.log('All members of this group are ready, this group is ready...');
var updatedDB = db;
updatedDB.activeGroup[i].groupReadyStatus = true;
mockupDBUpdate(updatedDB);
}
}
}
// Return a resolved Promise to signal that the function has completed
return Promise.resolve();
}
//Start group activity
function startGroupActivity (groupId) {
for (var i=0; i<activeGroup.length; i++) {
if (activeGroup[i].id === groupId) {
const checkingGroup = activeGroup[i];
const selectedGroupLOT = checkingGroup.groupPolicy.timeLength;
const selectedGroupTime = checkingGroup.groupPolicy.startTime;
if (selectedGroupLOT == "Instant") {
console.log('Instant group for demo... initiated');
//bypass cron, just for project demo only
shareRound(groupId, "Instant");
}
else {
console.log('Group time length is' + selectedGroupLOT + ' ... initiated');
//use cron to trigger event
const cronLOT = '* * *';
if (selectedGroupLOT == 'Daily') {
cronLOT = '* * *';
}
else if (selectedGroupLOT == 'Weekly') {
cronLOT = '* * 1';
}
else if (selectedGroupLOT == 'Monthly') {
cronLOT = '1 * *';
}
else if (selectedGroupLOT == 'Yearly') {
cronLOT = '1 1 *';
}
const cronTime = selectedGroupTime + cronLOT;
cron.schedule(cronTime, () => {
console.log('Start round... for group:' + groupId);
shareRound (groupId, cronLOT);
});
}
}
}
}
//Group sharing round activity
function shareRound (groupId, groupLOT) {
var checkingGroup = null;
for (var i=0; i<activeGroup.length; i++) {
if (activeGroup[i].id === groupId) {
checkingGroup = activeGroup[i];
}
}
const thisGroupPolicy = checkingGroup.groupPolicy;
if (groupLOT == "Instant") {
//Instant mean just for demo presentation
if (thisGroupPolicy.mainType == "Float") {
//wait for bidding
console.log('Wait for biding...');
bidStatusCheck(groupId);
}
}
else {
console.log('Not implemented yet...');
//Not implemented yet
}
}
var lastRoundWinnerBid = 0;
function bidStatusCheck (groupId) {
var checkingGroup = null;
var selectedGroup = null;
for (var i=0; i<activeGroup.length; i++) {
if (activeGroup[i].id === groupId) {
checkingGroup = activeGroup[i];
selectedGroup = i;
}
}
const totalMembers = checkingGroup.groupPolicy.maxMember;
var bidCount = 0;
var alreadyWonBid = [];
for (var i=0; i<checkingGroup.groupHistory.length; i++) {
bidCount++; //Each finished round will reduce bidable member by 1
alreadyWonBid.push(checkingGroup.groupHistory[i].bidWinner);
//Gather user id that already win bid in past round
}
var winnerBid = -1;
var bidWinner = -1;
const groupActivities = checkingGroup.groupActivities;
for (let i=0; i<groupActivities.length; i++) {
const activity = groupActivities[i];
if (!alreadyWonBid.includes(activity.id)) {
bidCount++;
if (activity.proposedBid > winnerBid) {
winnerBid = activity.proposedBid;
bidWinner = activity.id;
}
if (bidCount == totalMembers) {
console.log('Biding winner is user: ' + bidWinner);
transactPool(bidWinner, winnerBid, selectedGroup);
recordHistory(bidWinner, selectedGroup);
}
}
}
}
function transactPool (bidWinner, winnerBid, selectedGroup) {
//Transfer money from staking pool to the biding winner
console.log('Pool cash sent to user ' + bidWinner);
//This section got commented out due to unable to start Quorum node with Wifi at the venue
//const peer = 1;
//const onGroup = activeGroup[selectedGroup];
//const groupPolicy = onGroup.groupPolicy;
//const totalSumTransfer = lastRoundWinnerBid + (groupPolicy.poolSize/groupPolicy.maxMember);
//lastRoundWinnerBid = winnerBid;
//sendToLocalNode(users[peer].walletAddr, users[bidWinner].walletAddr, totalSumTransfer);
groupCompletedCheck(selectedGroup);
}
function sendToLocalNode (A, B, C) {
//Send to local Quorum node to deploy into smartcontract
const sendingPackage = {
A, B, C
};
var client = new net.Socket();
const nodeHOST = '192.168.1.1';
const nodePORT = 4444;
var sMessage = null;
var sChunks = [];
const chunkSize = 1024;
//Send number of chunk to let the receiver know
var prepareSendChunk = function() {
var chunkCount = sChunks.length + 1; //+1 also count the chunkCount value
console.log("***************************");
console.log("Chunk pieces: " + chunkCount);
console.log("***************************");
if (chunkCount) {
console.log("sending chunkCount");
client.write(chunkCount.toString()); //.write send only string
}
console.log('Sent: \n' + sMessage + '\n');
}
// Send packets one by one
var j = 0;
var sendNextChunk = function() {
if (j < sChunks.length) {
client.write(sChunks[j], function() {
j++;
});
}
};
client.connect(nodePORT, nodeHOST, function() {
console.log('CONNECTED TO: ' + nodeHOST + ':' + nodePORT);
sMessage = sendingPackage.toString();
// Split message into smaller packets
sChunks = [];
var chunk = "";
for (var i = 0; i < sMessage.length; i++) {
chunk += sMessage[i];
if (chunk.length === chunkSize || i === sMessage.length - 1) {
sChunks.push(chunk);
chunk = "";
}
}
prepareSendChunk(); //Prepare and enter packets sending loop
});
client.on('data', function(data) {
var dataGot = data.toString();
if (dataGot == "ACK") {
sendNextChunk();
}
else if (dataGot == "FIN") {
client.end();
console.log('==============================');
console.log('Respond received: ' + data);
console.log('==============================');
}
});
client.on('end', function() {
console.log('Connection ended');
});
client.on('close', function() {
console.log('Connection closed');
});
}
function recordHistory (bidWinner, selectedGroup) {
//record round history into group
var updatedDB = db;
updatedDB.activeGroup[selectedGroup].groupActivities = [];
const historyRecord = {
bidWinner
}
updatedDB.activeGroup[selectedGroup].groupHistory.push(historyRecord);
console.log('Group history record...');
mockupDBUpdate(updatedDB);
}
function groupCompletedCheck (selectedGroup) {
var completeCount = 0;
lastRoundWinnerBid = 0;
for (let i=0; i<activeGroup[selectedGroup].groupHistory.length; i++) {
completeCount++;
if (activeGroup[selectedGroup].groupPolicy.maxMember == completeCount) {
console.log('Group completed... closed');
}
}
}
//----maybe have update info. from smartcontract
//==================================================
// Handling request
app.get('/', (req, res) => {
res.json({ message: 'Welcome to the API' });
});
app.get('/greet', (req, res) => {
res.json({ message: 'Hello World!' });
});
// Login handling
app.post('/login', async (req, res) => {
const { email, password } = req.body;
const user = users.find((user) => user.email === email);
if (user) {
const isPasswordMatch = await bcrypt.compare(password, user.passwordHash);
if (isPasswordMatch) {
const token = jwt.sign({ id: user.id }, jwtSecret);
res.json({ token });
} else {
res.status(401).json({ message: 'Invalid password' });
}
} else {
res.status(401).json({ message: 'Invalid email' });
}
});
// Check user authorization
app.get('/protected', authenticateJWT, (req, res) => {
res.json({ message: `Hello, ${req.user.name}! This is a protected resource.` });
});
// Dashboard summary - status, active group list
app.get('/dashboard', authenticateJWT, (req, res) => {
const selectedUser = req.user.id;
var foundGroup = [];
for (const group of activeGroup) {
const hasPerson = group.groupMembers.some(member => member.id === selectedUser);
if (hasPerson) {
foundGroup.push(group.id);
}
}
var dashboardElement = {
assetsBalance: assets[selectedUser],
belongGroup: foundGroup
};
res.json(dashboardElement);
});
// Create group
app.post('/creategroup', authenticateJWT, (req, res) => {
const createGroupPolicy = req.body; //get object from frontend for new group properties
//hash group property to create group id - This probably will cause bug sometimes
const hash = crypto.createHash('sha1').update(JSON.stringify(createGroupPolicy)).digest('hex');
//sort new property and new group id into object
const newGroupName = createGroupPolicy.groupName;
const newGroupPolicy = createGroupPolicy.groupPolicy;
const newGroupMembers = [{ id: req.user.id, name: req.user.name, readyStatus: false, isHost: true}];
const newGroupActivities = [];
const newGroupHistory = [];
const newGroupProperty = {
id: hash,
groupName: newGroupName,
groupPolicy: newGroupPolicy,
groupMembers: newGroupMembers,
groupActivities: newGroupActivities,
groupHistory: newGroupHistory
};
//create new object to store updated version of mockdb.json
var updateMockDB = db;
updateMockDB.activeGroup.push(newGroupProperty) //add the property of new group
const jsonContent = JSON.stringify(updateMockDB);
fs.writeFileSync('config/mockdb.json', jsonContent);
console.log('mockup database updated...');
res.json({
message: `Create group process...`
});
});
// Discover group
app.get('/discover', authenticateJWT, (req, res) => {
const selectedUser = req.user.id;
var showActiveGroup = activeGroup;
for (var i=0; i<activeGroup.length; i++) {
showActiveGroup[i]['memberCount'] = activeGroup[i].groupMembers.length;
}
if (selectedUser) {
res.json(showActiveGroup);
}
});
// Join group
app.post('/joingroup', authenticateJWT, (req, res) => {
const selectedUserID = req.user.id;
const selectedUserName = req.user.name;
const aimToGroup = req.body.id;
const memberToAdd = {
id: selectedUserID,
name: selectedUserName,
readyStatus: false,
isHost: false
}
for (var selectedGroup = 0; selectedGroup<activeGroup.length; selectedGroup++) {
if (activeGroup[selectedGroup].id === aimToGroup) {
activeGroup[selectedGroup].groupMembers.push(memberToAdd);
console.log('Added new member to the selected group...');
}
}
const jsonContent = JSON.stringify(db);
fs.writeFileSync('config/mockdb.json', jsonContent);
console.log('mockup database updated...');
res.json({
message: `Join group process...`
});
});
// Group ongoing cycle =============================
// Group Ready: every members in the group must be readied for the group to start sharing cycle
app.post('/ready', authenticateJWT, (req, res) => {
res.json({
message: `Ready up to group process...`
});
const fromUser = req.user.id;
const toGroup = req.body.groupId;
var updatedDB = db;
for (var i=0; i<activeGroup.length; i++) {
if (activeGroup[i].id === toGroup) {
const checkingGroup = activeGroup[i];
const maxGroupMember = checkingGroup.groupMembers.length;
for (var j=0; j<maxGroupMember; j++) {
const checkingMember = checkingGroup.groupMembers[j];
if (checkingMember.id == fromUser) {
updatedDB.activeGroup[i].groupMembers[j].readyStatus = true;
console.log('Update ready status for user...');
mockupDBUpdate(updatedDB)
.then(() => groupStatusCheck(toGroup))
.then(() => {
console.log('Done!');
})
.catch((error) => {
console.error(error);
});
}
}
}
}
});
// Group Start: group host will able to start sharing cycle when every members ready
// 1.Update Group Started Status
app.post('/start', authenticateJWT, (req, res) => {
const fromUser = req.user.id;
const toGroup = req.body.groupId;
var updatedDB = db;
var responded = false;
for (var i=0; i<activeGroup.length; i++) {
if (activeGroup[i].id === toGroup) {
const checkingGroup = activeGroup[i];
const maxGroupMember = checkingGroup.groupMembers.length;
if (checkingGroup.groupReadyStatus == true) {
for (var j=0; j<maxGroupMember; j++) {
const checkingMember = checkingGroup.groupMembers[j];
if (checkingMember.id == fromUser) {
if (checkingMember.isHost == true) {
updatedDB.activeGroup[i].groupStartedStatus = true;
res.json({
message: `Start group process...`
});
responded = true;
console.log('Start this group process...');
mockupDBUpdate(updatedDB);
// 2.Spawn sharing process
startGroupActivity(toGroup);
// 3.Spawn smartcontract to record ongoing sharing process and control assets circulation
}
else {
res.json({
message: `User is not the group host...`
});
responded = true;
console.log('Unauthorized group start attempt detected...');
}
}
}
}
else {
res.json({
message: `Group not ready yet...`
});
responded = true;
console.log('Group start attempt without ready... denied');
}
}
}
if (!responded) {
res.json({
message: `Not this group...`
});
console.log('Not this group for this user to start:' + selectedUser);
}
});
// Group Activity
// Bidding accept for Float mainType
app.post('/bid', authenticateJWT, (req, res) => {
res.json({
message: `Bidding process...`
});
const fromUser = req.user.id;
const toGroup = req.body.groupId;
const proposedBid = req.body.bidPropose;
const bidProposal = {
id: fromUser,
proposedBid: proposedBid
}
var updatedDB = db;
for (var i=0; i<activeGroup.length; i++) {
if (activeGroup[i].id === toGroup) {
const checkingGroup = activeGroup[i];
const maxGroupMember = checkingGroup.groupMembers.length;
for (var j=0; j<maxGroupMember; j++) {
const checkingMember = checkingGroup.groupMembers[j];
if (checkingMember.id == fromUser) {
updatedDB.activeGroup[i].groupActivities.push(bidProposal);
console.log('Biding proposed from user... accepted');
mockupDBUpdate(updatedDB)
.then(() => bidStatusCheck(toGroup))
.then(() => {
console.log('Done!');
})
.catch((error) => {
console.error(error);
});
}
}
}
}
});
// HTTP request for support Fix mainType will later be implemented
// Group cycle trigger event
// Group completed
//All setup
//Create Server ====================================
const server = http.createServer(app);
server.listen(PORT, () => {
console.log("Server listening on port: " + PORT);
});
console.log("Server started");