-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
122 lines (95 loc) · 3.31 KB
/
app.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
const Block = require('./block')
const Blockchain = require('./blockchain')
const Transaction = require('./transaction')
const BlockchainNodes = require('./blockchainNode')
const fetch = require('node-fetch')
const express = require('express')
const { response } = require('express')
const app = express()
// console.log(process.argv)
const arguments = process.argv
let PORT = 8080
if (arguments.length >2) {
PORT = arguments[2]
}
// body Parser for JSON
app.use(express.json())
let transactions = []
let nodes = []
let allTransactions = []
let genesisBlock = new Block()
let blockchain = new Blockchain(genesisBlock)
app.get('/resolve', (req, res) => {
// Resolve conflicts between nodes
nodes.forEach(node => {
fetch(`${node.url}/blockchain`)
.then(response => response.json())
.then(otherBlockchain => {
if(blockchain.blocks.length < otherBlockchain.blocks.length) {
allTransactions.forEach(transaction => {
fetch(`${node.url}/transactions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(transaction)
}).then(response => response.json())
.then(_ => { //eslint-disable-line
// result ignored
// The unique routes to add blockchain to the node is throw mine
fetch(`${node.url}/mine`)
.then(response => response.json())
.then(_ => { //eslint-disable-line
// ignore the result
// Fetch the updated blockchain by making a request again
// get the update blockchain
fetch(`${node.url}/blockchain`)
.then(response => response.json())
.then(updatedBlockchain => {
console.log(updatedBlockchain)
blockchain = updatedBlockchain
res.json(blockchain)
})
})
})
})
} else {
res.json(blockchain)
}
})
})
})
app.post('/nodes/register', (req, res) => {
const urls = req.body
urls.forEach( url => {
// console.log(url)
const node = new BlockchainNodes(url)
nodes.push(node)
})
res.json(nodes)
})
app.post('/transactions',(req, res) => {
const to = req.body.to
const from = req.body.from
const amount = req.body.amount
let transaction = new Transaction(from, to, amount)
transactions.push(transaction)
res.json(transactions)
})
app.get('/mine', (req, res) => {
let block = blockchain.getNextBlock(transactions)
blockchain.addBlock(block)
// Add transaction to All transaction
transactions.forEach(transaction => {
allTransactions.push(transaction)
})
// Clear transactios out
transactions = []
res.json(block)
})
app.get('/blockchain', (req, res) =>{
res.json(blockchain)
})
app.listen(PORT, () => {
console.log(`Server is running on PORT ${PORT}`)
})