-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
49 lines (43 loc) · 1.75 KB
/
routes.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
// to get our blockchain we call this
function get_chain(req, res, blockchain) {
var response = {
'chain': blockchain.chain,
'length': blockchain.chain.length
}
return res.send(response);
}
// This function checks full blockchain in a valid situation or not
function is_valid(req, res, blockchain) {
if(blockchain.isChainValid(blockchain.chain))
return res.send({'message': 'All good. The Blockchain is valid.'})
else
return res.send({'message': 'Houston, we have a problem. The Blockchain is not valid.'})
}
// This function add a transaction in his own tansaction pool
function add_transaction(req,res,blockchain){
var body = req.body
// We must have sender,receiver and amount
var transaction_keys = ['sender', 'receiver', 'amount']
for(let key of transaction_keys)
if(body[key] === undefined)
return res.send('Some elements of the transaction are missing')
var response = {'message': `This transaction will be added to next Block`}
return res.send(response)
}
// connect with other nodes with IP address in a local network
function connect_node(req,res,blockchain)
{
var body = req.body
if(body['nodes']=== undefined)
return res.send("No Node")
for(let node of body['nodes'])
// console.log(node)
blockchain.addNode(node)
console.log(blockchain.node_list)
var response = {
'message': 'All the nodes are now connected. The Hadcoin Blockchain now contains the following nodes:',
'total_nodes': blockchain.node_list.size
}
return res.send(response)
}
module.exports = {get_chain,is_valid,add_transaction,connect_node};