-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.js
210 lines (172 loc) · 6.97 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
const express = require('express');//This is the main server that is running up the whole system.
const app = express();
const Blockchain = require('./dev/Blockchain');//This is the method written for the blockchain.
const rp = require('request-promise');
const createError = require('http-errors');
const path = require('path');
const bodyParser = require('body-parser');
const log = require('morgan');
const cookie = require('cookie-parser');
//above are the modules for this system in the backend.
//some above modules are optionals.
app.set('view engine', 'ejs');
//This is important.It connects all the ejs webpages.
app.get('/', function(req, res) {
res.render('main/homepage');
});//This is rendering the main webpages.all ejs webpages are showed here.
app.use(express.static(path.join(__dirname, 'resource')));//This is setting up the directory for user script files.
app.use(log('dev'));
app.use(cookie());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
const bc = new Blockchain();//This is assigning the blockchain to a variable.
app.get('/blockchain', function(req, res)
{
res.send(bc);
});//This method is to send the whole blockchain.
app.get('/block', function(req, res){
res.send(bc.chain);
});//This method is to send only blockchain data.
app.post('/newChain', function(req, res){
const newchain = req.body;
bc.chain = [];
bc.chain = newchain;
});
app.post('/createNewBlock/:Name,:Gender,:DateOfBirth,:Ssn,:Charge,:OffenseDate,:caseNo,:DisposedDate', function(req, res){
const name = req.params.Name;
const gender = req.params.Gender;
const date = req.params.DateOfBirth;
const ssn = req.params.Ssn;
const charge = req.params.Charge;
const caseNo = req.params.caseNo;
const offdate = req.params.OffenseDate;
const disDate = req.params.DisposedDate;
const lastBlock = bc.getLastBlock();
const curBlockData = {
Index : lastBlock['Index'] + 1,
Name : name,
Gender : gender,
DateOfBirth : date,
SSN : ssn,
Charge : charge,
CaseNo : caseNo,
OffenseDate : offdate,
DisposedDate : disDate
};
const previousBlockHash = lastBlock['CurrentBlockHash'];
const nonce = bc.proofOfWork(previousBlockHash, curBlockData);
const CurrentBlockhash = bc.hashBlock(previousBlockHash, curBlockData, nonce);
const newBlock = bc.createNewBlock(nonce, name, gender, date, ssn, charge, caseNo, offdate, disDate, previousBlockHash, CurrentBlockhash);
const requestPromises = [];
bc.AllNetworkNodes.forEach(networkNodeUrl => {
const requestoption = {
uri : networkNodeUrl + '/receive-new-block',
method : 'POST',
body : { newBlock: newBlock },
json : true
};
requestPromises.push(rp(requestoption));
});
});//This method is to create the block.
app.post('/receive-new-block', function(req, res){
const newBlock = req.body.newBlock;
const lastBlock= bc.getLastBlock();
const correctHash = lastBlock.CurrentBlockHash === newBlock.PreviousBlockHash;
const correctIndex = lastBlock['Index'] + 1 === newBlock['Index'];
if ( correctHash && correctIndex) {
bc.chain.push(newBlock);
res.json({
Note : "New block accepted",
NewBlock : newBlock
});
} else {
res.json({
Note : "New block rejected.",
NewBlock : newBlock
});
}
});//This method is to broadcast new block to all the networks.
app.post('/broadcast',function(req, res) {
const newNodeUrl = req.body.newNodeUrl;
if(bc.AllNetworkNodes.indexOf(newNodeUrl) == -1) bc.AllNetworkNodes.push(newNodeUrl);
const registerNode = [];
bc.AllNetworkNodes.forEach(networkNodeUrl => {
const requestOption = {
uri : networkNodeUrl + '/register',
method : 'POST',
body : { newNodeUrl : newNodeUrl},
json : true
};
registerNode.push(rp(requestOption));
});
Promise.all(registerNode)
.then(data => {
const bulkRegisterOptions = {
uri: newNodeUrl + '/bulk',
method : 'POST',
body : { allNetworkNodes : [ ...bc.AllNetworkNodes, bc.CurrentNetworkNode]},
json : true
};
return rp(bulkRegisterOptions);
})
.then(data => {
res.json({ note: 'New node registered with network successfully.' });
})
});//This method is to broadcast the network addresses.
app.post('/register', function(req, res){
const newNode = req.body.newNodeUrl;
const nodeNotAlreadyPresent = bc.AllNetworkNodes.indexOf(newNode) == -1;
const notCurrentNode = bc.CurrentNetworkNode !== newNode;
if(nodeNotAlreadyPresent && notCurrentNode) bc.AllNetworkNodes.push(newNode);
res.json({ note: 'New node registered successfully.'});
});//This method is to register network address of new device.
app.post('/bulk', function(req, res){
const allNetworkNodes = req.body.allNetworkNodes;
allNetworkNodes.forEach(networkNodeUrl => {
const nodeNotAlreadyPresent = bc.AllNetworkNodes.indexOf(networkNodeUrl) == -1;
const notCurrentNode = bc.CurrentNetworkNode !== networkNodeUrl;
if (nodeNotAlreadyPresent && notCurrentNode) bc.AllNetworkNodes.push(networkNodeUrl);
});
res.json({ note: 'Bulk registration successful.'});
});//This method is to add multiple network addresses of new devices.
app.get('/consensus', function(req, res) {
const requestPromises = [];
bc.AllNetworkNodes.forEach(networkNodeUrl => {
const requestoption = {
uri : networkNodeUrl + '/blockchain',
method : 'GET',
json : true
};
requestPromises.push(rp(requestoption));
});
Promise.all(requestPromises).then(blockchains => {
const currentChainLength = bc.chain.length;
let maxChainLength = currentChainLength;
let newLongestChain = null;
blockchains.forEach(blockchain => {
if (blockchain.chain.length > maxChainLength){
maxChainLength = blockchain.chain.length;
newLongestChain = blockchain.chain;
}
});
if (!newLongestChain || (newLongestChain && !bc.isChainValid(newLongestChain))) {
res.render('partial/deny');
}
else if (newLongestChain && bc.isChainValid(newLongestChain)){
bc.chain = newLongestChain;
res.render('partial/grant');
}
});
});//This method is to check the validation of the block chain.
app.use(function(req, res, next) {
next(createError(404));
});// catch 404 and forward to error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});// error handler
module.exports = app;