-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsyncer.js
143 lines (123 loc) · 7.51 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
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() {
async function syncNetworkInfo() {
try {
global.networkInfo = await fetch(config.gateways.arweaveGateway + "/info").catch(e => null).then(c => c ? c?.json() : null) || global.networkInfo
} catch (e) {
return
}
consola.info("Block height: " + global.networkInfo.height)
}
await syncNetworkInfo()
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]
})))
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)
}
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)
setInterval(async () => {
for await (let contract of (await executeBundlrQuery([["Contract-Src", config.allowed.contractSourceIds], ["App-Name", ["SmartWeaveContract"]]]))) {
if (!servedContractsIds.has(contract.id)) {
console.log("fetched contract " + contract.id)
}
await databases.contracts.put(contract.id, contract)
servedContractsIds.add(contract.id)
}
}, 10000)
for (let contract of config.allowed.contractIds) {
let contractTx = await findTxById(contract)
await databases.contracts.put(contract, contractTx)
}
consola.info("Serving " + servedContractsIds.size + " contracts");
for (let contractIndex = 0; contractIndex < servedContractsIds.size; contractIndex += 4) {
let contracts = Array.from(servedContractsIds).slice(contractIndex, contractIndex + 4)
for (let contract of contracts) {
if (!databases.interactions[contract]) {
databases.interactions[contract] = lmdb.open("./db/interactions/" + contract)
}
}
let bundledTransactions = (await executeBundlrQuery([["Contract", contracts], ["App-Name", ["SmartWeaveAction"]]]))
let transactions = (await executeTxQuery(0, [["Contract", contracts], ["App-Name", ["SmartWeaveAction"]]], true))
for await (let txForContract of bundledTransactions) {
let belongingContracts = txForContract.tags.filter(t => t.name == "Contract").filter(t => contracts.includes(t.value)).map(t => t.value)
for (let contract of belongingContracts) {
if (!await databases.interactions[contract].doesExist(txForContract.id)) {
await databases.interactions[contract].put(txForContract.id, txForContract)
consola.info("[" + new Date(txForContract.timestamp).toISOString().split("T").join(" ").split(".")[0] + "]", "Loaded bundled interaction " + txForContract.id + " for contract " + contract)
}
}
}
for await (let txForContract of transactions) {
let belongingContracts = txForContract.tags.filter(t => t.name == "Contract").filter(t => contracts.includes(t.value)).map(t => t.value)
for (let contract of belongingContracts) {
if (!await databases.interactions[contract].doesExist(txForContract.id)) {
await databases.interactions[contract].put(txForContract.id, txForContract)
consola.info("[" + new Date(txForContract.timestamp).toISOString().split("T").join(" ").split(".")[0] + "]", "Loaded base interaction " + txForContract.id + " for contract " + contract)
}
}
}
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)
}
let sortedInteractions = [...databases.interactions[contract].getRange().map(({ key, value }) => ({ id: value.id, timestamp: value.timestamp }))].sort((a, b) => a.timestamp - b.timestamp).map(i => i.id)
await databases.indexes.put(contract,sortedInteractions )
consola.info("Sorted interactions for contract " + contract)
}
consola.success("Synced all contracts interactions")
startExecutionSyncLoop()
setInterval(async () => {
for (let contractIndex = 0; contractIndex < Array.from(servedContractsIds).length; contractIndex += 4) {
let contracts = Array.from(servedContractsIds).slice(contractIndex, contractIndex + 4)
if (!contracts.length) {
break;
}
for (let contract of contracts) {
if (!databases.interactions[contract]) {
databases.interactions[contract] = lmdb.open("./db/interactions/" + contract)
}
}
let bundledTransactions = (await executeBundlrQuery([["Contract", contracts], ["App-Name", ["SmartWeaveAction"]]]))
let transactions = (await executeTxQuery(0, [["Contract", contracts], ["App-Name", ["SmartWeaveAction"]]], true))
for await (let txForContract of bundledTransactions) {
let belongingContracts = txForContract.tags.filter(t => t.name == "Contract").filter(t => contracts.includes(t.value)).map(t => t.value)
for (let contract of belongingContracts) {
if (!await databases.interactions[contract].doesExist(txForContract.id)) {
await databases.interactions[contract].put(txForContract.id, txForContract)
consola.info("[" + new Date(txForContract.timestamp).toISOString().split("T").join(" ").split(".")[0] + "]", "Loaded bundled interaction " + txForContract.id + " for contract " + contract)
}
}
}
for await (let txForContract of transactions) {
let belongingContracts = txForContract.tags.filter(t => t.name == "Contract").filter(t => contracts.includes(t.value)).map(t => t.value)
for (let contract of belongingContracts) {
if (!await databases.interactions[contract].doesExist(txForContract.id)) {
await databases.interactions[contract].put(txForContract.id, txForContract)
consola.info("[" + new Date(txForContract.timestamp).toISOString().split("T").join(" ").split(".")[0] + "]", "Loaded base interaction " + txForContract.id + " for contract " + contract)
}
}
}
// consola.success("Checked for interactions on contracts " + contracts)
}
}, Math.max(servedContractsIds.size * 300, 4000))
setInterval(async () => {
}, Math.max(servedContractsIds.size * 300, 4000))
}