-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathapp.js
64 lines (57 loc) · 1.85 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
// Import all required modeles
const express = require("express");
const Wallet = require("./wallet");
const bodyParser = require("body-parser");
const TransactionPool = require("./transaction-pool");
const P2pserver = require("./p2p-server");
const Validators = require("./validators");
const Blockchain = require("./blockchain");
const BlockPool = require("./block-pool");
const CommitPool = require("./commit-pool");
const PreparePool = require("./prepare-pool");
const MessagePool = require("./message-pool");
const { NUMBER_OF_NODES } = require("./config");
const HTTP_PORT = process.env.HTTP_PORT || 3001;
// Instantiate all objects
const app = express();
app.use(bodyParser.json());
const wallet = new Wallet(process.env.SECRET);
const transactionPool = new TransactionPool();
const validators = new Validators(NUMBER_OF_NODES);
const blockchain = new Blockchain(validators);
const blockPool = new BlockPool();
const preparePool = new PreparePool();
const commitPool = new CommitPool();
const messagePool = new MessagePool();
const p2pserver = new P2pserver(
blockchain,
transactionPool,
wallet,
blockPool,
preparePool,
commitPool,
messagePool,
validators
);
// sends all transactions in the transaction pool to the user
app.get("/transactions", (req, res) => {
res.json(transactionPool.transactions);
});
// sends the entire chain to the user
app.get("/blocks", (req, res) => {
res.json(blockchain.chain);
});
// creates transactions for the sent data
app.post("/transact", (req, res) => {
const { data } = req.body;
const transaction = wallet.createTransaction(data);
transactionPool.addTransaction(transaction);
p2pserver.broadcastTransaction(transaction);
res.redirect("/transactions");
});
// starts the app server
app.listen(HTTP_PORT, () => {
console.log(`Listening on port ${HTTP_PORT}`);
});
// starts the p2p server
p2pserver.listen();