forked from Zilliqa/Devex-apollo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
153 lines (123 loc) · 4.33 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
import apollo from "apollo-server-express";
import cors from "cors";
import express from "express";
import mongoose from "mongoose";
import { range } from "./util.js";
import GQLSchema from "./gql/schema2.js";
import Api from "./datasource/api.js";
import config from "./config/config.js";
import {
txBlockReducer,
txnReducer,
transitionReducer,
} from "./mongodb/reducer.js";
import { TxBlockModel, TxnModel, TransitionModel } from "./mongodb/model.js";
const { ApolloServer } = apollo;
console.log("NODE_ENV: " + process.env.NODE_ENV);
const BLOCKS_PER_REQUEST = process.env.BLOCKS_PER_REQUEST;
// Set up apollo express server
const app = express();
app.use(cors());
const api = new Api(process.env.NETWORK_URL);
const server = new ApolloServer({
schema: GQLSchema,
context: {
models: {
TxBlockModel,
TxnModel,
},
api: api,
},
});
server.applyMiddleware({ app, path: "/" });
mongoose.connect(config.dbUrl, { ...config.mongooseOpts });
let connection = mongoose.connection;
const loadData = async (start, end) => {
let blocksPerRequest = BLOCKS_PER_REQUEST;
if (start > end) {
if (start - blocksPerRequest < end) {
blocksPerRequest = start - end;
}
console.log(`Trying to sync from ${start} to ${start - blocksPerRequest}...`);
try {
const blocksRange = [...range(start - blocksPerRequest, start)];
const txBlocks = await api.getTxBlocks(blocksRange);
const reducedBlocks = txBlocks.map(block => txBlockReducer(block));
const blocksWithTxs = reducedBlocks.filter(block => block.header.NumTxns !== 0);
const txns = await api.getTxnBodiesByTxBlocks(blocksWithTxs);
const reducedTxns = txns.map(txn => txnReducer(txn));
const contractsChecked = await api.checkIfContracts(reducedTxns);
const finalTxns = contractsChecked.map(txn => {
const blockDetails = reducedBlocks.find(block => {
return parseInt(block.header.BlockNum) === txn.blockId;
});
return {
...txn,
timestamp: parseInt(blockDetails.header.Timestamp),
}
})
const finalBlocksAdded = await new Promise((resolve, reject) =>
TxBlockModel.insertMany(reducedBlocks, { ordered: false }, function (err, result) {
if (err) {
if (err.code === 11000) {
resolve(err.result.result.insertedIds);
} else {
reject(err);
}
}
resolve(result); // Otherwise resolve
})
)
const finalTxsAdded = await new Promise((resolve, reject) =>
TxnModel.insertMany(finalTxns, { ordered: false }, function (err, result) {
if (err) {
if (err.code === 11000) {
resolve(err.result.result.insertedIds);
} else {
reject(err);
}
}
resolve(result); // Otherwise resolve
})
)
console.log(`Added ${finalTxsAdded.length}/${finalTxns.length} txs and ${finalBlocksAdded.length}/${reducedBlocks.length} blocks.`);
} catch (error) {
if (error.code !== 11000) { // 11000 stands for duplicate entry
console.log(error.message);
}
} finally {
console.log(`Synced blocks from ${start} to ${start - blocksPerRequest}.`);
if (start - blocksPerRequest > end) {
setTimeout(() => {
loadData(start - BLOCKS_PER_REQUEST, end);
}, 5000);
}
}
} else {
console.log(`${end} is lower than ${start}`);
}
};
connection.once("open", function () {
console.log("MongoDB database connection established successfully");
api.getLatestTxBlock().then((latestBlock) => {
try {
if (process.env.FAST_SYNC === false) {
loadData(latestBlock - 1, 0);
}
setInterval(async () => {
const latestBlockInNetwork = await api.getLatestTxBlock();
TxBlockModel.findOne({ customId: { $lt: 20000000 } }).sort({ customId: -1 }).limit(1).exec((err, res) => {
const latestBlockInDB = (res == null) ? 0 : res.customId;
console.log(`Blocks need to be synced from ${latestBlockInNetwork} to ${latestBlockInDB}`);
loadData(latestBlockInNetwork, latestBlockInDB);
});
}, 60000);
} catch (error) {
console.error(error);
return;
}
});
});
app.listen(5000, () => {
console.log("🚀 Server ready at port 5000");
});