forked from rareweave/glome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncer.js
220 lines (192 loc) · 6.25 KB
/
syncer.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
215
216
217
218
219
220
let {
executeTxQuery,
executeBundlrQuery,
wait,
makeBundlrQuery,
findTxById,
} = require("./utils.js");
let consola = require("consola");
let startExecutionSyncLoop = require("./executionSync.js");
let lmdb = require("lmdb");
const { fetch } = require("ofetch");
module.exports = async function startSyncLoop() {
// Fetch Network data
async function syncNetworkInfo() {
let gatewayNetworkInfo = await fetch(
config.gateways.rpcConfig + "/abci_info"
)
.catch((e) => null)
.then((c) => c.json());
global.networkInfo =
gatewayNetworkInfo.result.response || global.networkInfo;
}
await syncNetworkInfo();
// Log network info
consola.info("Chain Name/Data: " + global.networkInfo.data);
consola.info("Block height: " + global.networkInfo.last_block_height);
consola.info("App Version: " + global.networkInfo.version);
consola.info(
"Last Block App hash: " + global.networkInfo.last_block_app_hash
);
// Set the plugins
global.plugins = Object.fromEntries(
await Promise.all(
(config.plugins || []).map(async (pl) => {
let plugin = require(pl);
let pluginApi = await plugin.setup(config);
return [plugin.id, pluginApi];
})
)
);
// // Resync network info
// setInterval(syncNetworkInfo, 25000);
// for await (let contract of await executeTxQuery(
// 0,
// [
// ["Contract-Src", config.allowed.contractSourceIds],
// ["App-Name", ["SmartWeaveContract"]],
// ],
// false,
// null
// )) {
// await databases.contracts.put(contract.id, contract);
// servedContractsIds.add(contract.id);
// }
for (let contract of config.allowed.contractIds) {
let contractTx = await findTxById(contract);
await databases.contracts.put(contract, contractTx);
}
// setInterval(async () => {
// for await (let contract of await executeTxQuery(
// 0,
// [
// ["Contract-Src", config.allowed.contractSourceIds],
// ["App-Name", ["SmartWeaveContract"]],
// ],
// false,
// null
// )) {
// // console.log("fetched contract "+contract.id)
// await databases.contracts.put(contract.id, contract);
// servedContractsIds.add(contract.id);
// }
// }, 10000);
consola.info("Serving " + servedContractsIds.size + " contracts");
for (
let contractIndex = 0;
contractIndex < servedContractsIds.size;
contractIndex += 4
) {
let contracts = Array.from(servedContractsIds).slice(
contractIndex,
contractIndex + 4
);
// If no interacting list for this contract create it
for (let contract of contracts) {
if (!databases.interactions[contract]) {
databases.interactions[contract] = lmdb.open(
"./db/interactions/" + contract
);
}
}
let transactions = await executeTxQuery(
global.config.allowed.excutionWallets
);
for await (let txForContract of transactions) {
if (txForContract == undefined) continue;
// Runs in the case someone tried interacting with a contract that isnt being cached by glome
if (!servedContractsIds.has(txForContract.contractId)) continue;
if (
!(await databases.interactions[txForContract.contractId].doesExist(
txForContract.interactionId
))
) {
await databases.interactions[txForContract.contractId].put(
txForContract.interactionId,
txForContract
);
consola.info(
"[" + txForContract.blockHeight + "]",
"Loaded base interaction " +
txForContract.interactionId +
" for contract " +
txForContract.contractId
);
}
}
consola.success("Loaded contracts " + contracts.join(", "));
}
consola.info("All contracts loaded");
for (let contract of [...servedContractsIds]) {
if (!databases.interactions[contract]) {
databases.interactions[contract] = lmdb.open(
"./db/interactions/" + contract
);
}
console.log(
await databases.indexes.put(
contract,
[
...databases.interactions[contract]
.getRange()
.map(({ key, value }) => ({
id: value.interactionId,
blockHeight: value.blockHeight,
})),
]
.sort((a, b) => a.blockHeight - b.blockHeight)
.map((i) => i.interactionId)
)
);
consola.info("Sorted interactions for contract " + contract);
}
consola.success("Synced all contracts interactions");
startExecutionSyncLoop();
setInterval(async () => {
for (
let contractIndex = 0;
contractIndex < servedContractsIds.size;
contractIndex += 4
) {
let contracts = Array.from(servedContractsIds).slice(
contractIndex,
contractIndex + 4
);
// If no interacting list for this contract create it
for (let contract of contracts) {
if (!databases.interactions[contract]) {
databases.interactions[contract] = lmdb.open(
"./db/interactions/" + contract
);
}
}
let transactions = await executeTxQuery(
global.config.allowed.excutionWallets
);
for await (let txForContract of transactions) {
if (txForContract == undefined) continue;
// Runs in the case someone tried interacting with a contract that isnt being cached by glome
if (!servedContractsIds.has(txForContract.contractId)) continue;
if (
!(await databases.interactions[txForContract.contractId].doesExist(
txForContract.interactionId
))
) {
await databases.interactions[txForContract.contractId].put(
txForContract.interactionId,
txForContract
);
consola.info(
"[" + txForContract.blockHeight + "]",
"Loaded base interaction " +
txForContract.interactionId +
" for contract " +
txForContract.contractId
);
}
}
consola.success("Loaded contracts " + contracts.join(", "));
}
}, Math.max(servedContractsIds.size * 300, 4000));
setInterval(async () => {}, Math.max(servedContractsIds.size * 300, 4000));
};