-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
162 lines (140 loc) · 3.91 KB
/
index.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
const express = require("express");
const Blockchain = require("./blockchain");
const Pubsub = require("./app/pubsub");
const TransactionPool = require("./wallet/transaction-pool");
const Wallet = require("./wallet");
const tcpPortUsed = require("tcp-port-used");
const axios = require("axios");
const TransactionMiner = require("./app/transaction-miner");
const path = require("path");
const cors = require("cors");
const app = express();
const corsOpts = {
origin: "*",
methods: ["GET", "POST"],
allowedHeaders: ["Content-Type"],
};
app.use(cors(corsOpts));
app.use(express.static("./client/src"));
app.use(express.json());
const blockchain = new Blockchain();
const transactionPool = new TransactionPool();
const wallet = new Wallet();
const pubsub = new Pubsub({ blockchain, transactionPool });
const transactionMiner = new TransactionMiner({
blockchain,
transactionPool,
wallet,
pubsub,
});
setTimeout(() => {
pubsub.broadcastChain();
}, 1000);
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "./client/dist/index.html"));
});
app.get("/api/blocks", (req, res) => {
res.json(blockchain.chain);
});
app.get("/api/wallet-info", (req, res) => {
res.json({
address: wallet.publicKey,
balance: Wallet.calculateBalance({
chain: blockchain.chain,
address: wallet.publicKey,
}),
});
});
app.get("/api/mine-transactions", (req, res) => {
transactionMiner.mineTransactions();
res.redirect("/api/blocks");
});
app.get("/api/transaction-pool-map", (req, res) => {
res.json(transactionPool.transactionMap);
});
app.post("/api/transact", (req, res) => {
let { amount, recipient } = req.body;
amount = parseInt(amount);
let transaction = transactionPool.existingTransaction({
inputAddress: wallet.publicKey,
});
try {
if (transaction) {
transaction.update({ senderWallet: wallet, recipient, amount });
} else {
transaction = wallet.createTransaction({ recipient, amount });
}
} catch (error) {
return res.json({ type: "error", message: error.message });
}
transactionPool.setTransaction(transaction);
pubsub.broadcastTransaction(transaction);
// console.log("transactionPool:", transactionPool);
res.json({ transaction });
});
app.post("/api/mine", (req, res) => {
const { data } = req.body;
blockchain.addBlock({ data });
pubsub.broadcastChain();
res.redirect("/api/blocks");
});
const walletFoo = new Wallet();
const walletBar = new Wallet();
const generateWalletTransaction = ({ wallet, recipient, amount }) => {
const transaction = wallet.createTransaction({
chain: blockchain.chain,
recipient,
amount,
});
transactionPool.setTransaction(transaction);
};
const walletAction = () =>
generateWalletTransaction({
wallet,
recipient: walletFoo.publicKey,
amount: 5,
});
const walletFooAction = () =>
generateWalletTransaction({
wallet: walletFoo,
recipient: walletBar.publicKey,
amount: 10,
});
const walletBarAction = () =>
generateWalletTransaction({
wallet: walletBar,
recipient: wallet.publicKey,
amount: 15,
});
for (let i = 0; i < 10; i++) {
if (i % 3 === 0) {
walletAction();
walletFooAction();
} else if (i % 3 === 1) {
walletAction();
walletBarAction();
} else {
walletFooAction();
walletBarAction();
}
transactionMiner.mineTransactions();
}
const rootPort = 3000;
let PORT = 3000;
const syncOnConnect = async () => {
const response = await axios.get(`http://localhost:${rootPort}/api/blocks`);
blockchain.replaceChain(response.data);
const responsePool = await axios.get(
`http://localhost:${rootPort}/api/transaction-pool-map`
);
transactionPool.setMap(responsePool.data);
};
tcpPortUsed.check(3000, "127.0.0.1").then(function (inUse) {
if (inUse) {
PORT += Math.ceil(Math.random() * 1000);
}
app.listen(PORT, () => {
console.log(`listening at localhost:${PORT}`);
if (PORT !== rootPort) syncOnConnect();
});
});