-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
214 lines (180 loc) · 5.55 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const express = require("express");
const bodyParser = require("body-parser");
const request = require("request");
const path = require("path");
const PubSub = require("./src/pubsub");
const Blockchain = require("./src/blockchain/blockchain");
const Wallet = require("./src/wallet/wallet");
const TransactionPool = require("./src/wallet/transactionPool");
const Miner = require("./src/miner");
const isDevelopment = process.env.ENV === "development";
const REDIS_URL = isDevelopment
? "redis://127.0.0.1:6379"
: "redis://h:pd725d1c64913dcec1f259b4faf14d1dd3f5b29fc051386d69117274736d8c5ba@ec2-3-92-100-235.compute-1.amazonaws.com:32089";
const DEFAULT_PORT = 3000;
const ROOT_NODE_ADDRESS = isDevelopment?
`http://localhost:${DEFAULT_PORT}`
:`https://radiant-oasis-26933.herokuapp.com`;
const app = express();
const blockchain = new Blockchain();
const wallet = new Wallet();
const transactionPool = new TransactionPool();
const pubsub = new PubSub({ blockchain, transactionPool, redisUrl: REDIS_URL });
const miner = new Miner({ blockchain, transactionPool, wallet, pubsub });
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, "client/build")));
//Showing existing blocks
app.get("/api/blocks", (req, res) => {
res.json(blockchain.chain);
});
app.get("/api/blocks/length", (req, res) => {
res.json(blockchain.chain.length);
});
app.get("/api/blocks/:id", (req, res) => {
const { id } = req.params;
const { length } = blockchain.chain;
const reversedBlocks = blockchain.chain.slice().reverse();
let startIndex = (id - 1) * 5;
let endIndex = id * 5;
startIndex = startIndex < length ? startIndex : length;
endIndex = endIndex < length ? endIndex : length;
res.json(reversedBlocks.slice(startIndex, endIndex));
});
//mining blocks to the blockchain
app.post("/api/mine", (req, res) => {
const { data } = req.body;
blockchain.addBlock({ data });
pubsub.transmitChain();
res.redirect("/api/blocks");
});
//Transacting to another user
app.post("/api/transact", (req, res) => {
const { amount, receiver } = req.body;
let transaction = transactionPool.existingTransaction({
inputAddress: wallet.publicKey
});
try {
if (transaction) {
transaction.update({ senderWallet: wallet, receiver, amount });
} else {
transaction = wallet.createTransaction({
receiver,
amount,
chain: blockchain.chain
});
}
} catch (error) {
return res.status(400).json({ type: "error", message: error.message });
}
transactionPool.setTransaction(transaction);
pubsub.transmitTransaction(transaction);
res.json({ type: "success", transaction });
});
app.get("/api/transaction-pool-map", (req, res) => {
res.json(transactionPool.transactionMap);
});
app.get("/api/mine-transactions", (req, res) => {
miner.mineTransactions();
res.redirect("/api/blocks");
});
app.get("/api/wallet-info", (req, res) => {
const address = wallet.publicKey;
res.json({
address,
balance: Wallet.calculateBalance({
chain: blockchain.chain,
address
})
});
});
app.get("/api/known-addresses", (req, res) => {
const addressMap = {};
for (let block of blockchain.chain) {
for (let transaction of block.data) {
const receiver = Object.keys(transaction.outputMap);
receiver.forEach(receiver => (addressMap[receiver] = receiver));
}
}
res.json(Object.keys(addressMap));
});
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "client/build/index.html"));
});
// Synchronizing peers with main chain
const syncWithNetwork = () => {
request(
{ url: `${ROOT_NODE_ADDRESS}/api/blocks` },
(error, response, body) => {
if (!error && response.statusCode === 200) {
const rootChain = JSON.parse(body);
console.log("Syncing chain with", rootChain);
blockchain.alterChain(rootChain);
}
}
);
request(
{ url: `${ROOT_NODE_ADDRESS}/api/transaction-pool-map` },
(error, response, body) => {
if (!error && response.statusCode === 200) {
const rootTransactionPoolMap = JSON.parse(body);
console.log("Syncing transactions with ", rootTransactionPoolMap);
transactionPool.setMap(rootTransactionPoolMap);
}
}
);
};
if (isDevelopment) {
const walletOne = new Wallet();
const walletTwo = new Wallet();
const generateWalletTransaction = ({ wallet, receiver, amount }) => {
const transaction = wallet.createTransaction({
receiver,
amount,
chain: blockchain.chain
});
transactionPool.setTransaction(transaction);
};
const walletAction = () =>
generateWalletTransaction({
wallet,
receiver: walletOne.publicKey,
amount: 5
});
const walletOneAction = () =>
generateWalletTransaction({
wallet: walletOne,
receiver: walletTwo.publicKey,
amount: 10
});
const walletTwoAction = () =>
generateWalletTransaction({
wallet: walletTwo,
receiver: wallet.publicKey,
amount: 15
});
for (let i = 0; i < 30; i++) {
if (i % 3 === 0) {
walletAction();
walletOneAction();
} else if (i % 3 === 1) {
walletAction();
walletTwoAction();
} else {
walletOneAction();
walletTwoAction();
}
miner.mineTransactions();
}
}
let PEER_PORT;
//Creating new peeers
if (process.env.GENERATE_PEER_PORT === "true") {
PEER_PORT = DEFAULT_PORT + Math.ceil(Math.random() * 1000);
}
const PORT = process.env.PORT || PEER_PORT || DEFAULT_PORT;
app.listen(PORT, () => {
console.log(`App is listening on localhost:${PORT}`);
if (PORT !== DEFAULT_PORT) {
syncWithNetwork();
}
});