-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
58 lines (46 loc) · 1.48 KB
/
index.js
File metadata and controls
58 lines (46 loc) · 1.48 KB
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
const express = require('express');
const bodyparser = require('body-parser');
const Blockchain = require('./blockchain');
const PubSub = require('./publishsubscribe');
const request = require('request');
const app = express();
const blockchain = new Blockchain();
const pubsub = new PubSub(({blockchain}));
const DEFAULT_PORT = 3000;
setTimeout(() => pubsub.brodcastChain(),1000);
app.use(bodyparser.json());
app.get('/api/blocks',(req,res)=>{
res.json(blockchain.chain);
})
app.post('/api/mine',(req,res) =>{
const {data} = req.body;
blockchain.addBlock({data});
pubsub.brodcastChain();
res.redirect('/api/blocks');
})
const syncChains = () => {
const ROOT_NODE_ADDRESS = `http://localhost:${DEFAULT_PORT}`;
const url = `${ROOT_NODE_ADDRESS}/api/blocks`;
request(
url,
(error, response, body) => {
console.log("Error: ", error);
console.log("Response: ", response);
if(!error && response.statusCode === 200){
console.log("inside if")
const rootChain = JSON.parse(body);
console.log("Replace chain on sync with ", rootChain);
blockchain.replaceChain(rootChain);
}
}
);
};
let PEER_PORT;
if(process.env.GENERATE_PEER_PORT === 'true'){
PEER_PORT = DEFAULT_PORT + Math.ceil(Math.random()*1000);
}
const PORT = PEER_PORT || DEFAULT_PORT;
app.listen(PORT,() =>{
console.log(`Listening on port ${PORT}`);
syncChains();
});