mirror of
https://github.com/tornadocash/tornado-cli.git
synced 2024-10-01 07:15:36 -04:00
Fixed event sync
This commit is contained in:
parent
b8bc6de79d
commit
21b667add9
64
cli.js
64
cli.js
@ -829,39 +829,27 @@ function waitForTxReceipt({ txHash, attempts = 60, delay = 1000 }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function initJson(file) {
|
function initJson(file) {
|
||||||
return new Promise((resolve, reject) => {
|
if (fs.existsSync(file)) {
|
||||||
fs.readFile(file, 'utf8', (error, data) => {
|
return JSON.parse(fs.readFileSync(file, { encoding: 'utf8' }));
|
||||||
if (error) {
|
}
|
||||||
resolve([]);
|
return [];
|
||||||
}
|
|
||||||
try {
|
|
||||||
resolve(JSON.parse(data));
|
|
||||||
} catch (error) {
|
|
||||||
resolve([]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function loadCachedEvents({ type, currency, amount }) {
|
function loadCachedEvents({ type, currency, amount }) {
|
||||||
try {
|
const fileName = `./cache/${netName.toLowerCase()}/${type}s_${currency}_${amount}.json`;
|
||||||
const module = require(`./cache/${netName.toLowerCase()}/${type}s_${currency}_${amount}.json`);
|
const events = initJson(fileName);
|
||||||
|
|
||||||
if (module) {
|
if (events.length > 0) {
|
||||||
const events = module;
|
|
||||||
|
|
||||||
return {
|
|
||||||
events,
|
|
||||||
lastBlock: events[events.length - 1].blockNumber
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
console.log("Error fetching cached files, syncing from block", deployedBlockNumber);
|
|
||||||
return {
|
return {
|
||||||
events: [],
|
events,
|
||||||
lastBlock: deployedBlockNumber,
|
lastBlock: events[events.length - 1].blockNumber
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
console.log("Error fetching cached files, syncing from block", deployedBlockNumber);
|
||||||
|
return {
|
||||||
|
events: [],
|
||||||
|
lastBlock: deployedBlockNumber,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchEvents({ type, currency, amount }) {
|
async function fetchEvents({ type, currency, amount }) {
|
||||||
@ -875,16 +863,12 @@ async function fetchEvents({ type, currency, amount }) {
|
|||||||
console.log("Loaded cached",amount,currency.toUpperCase(),type,"events for",startBlock,"block");
|
console.log("Loaded cached",amount,currency.toUpperCase(),type,"events for",startBlock,"block");
|
||||||
console.log("Fetching",amount,currency.toUpperCase(),type,"events for",netName,"network");
|
console.log("Fetching",amount,currency.toUpperCase(),type,"events for",netName,"network");
|
||||||
|
|
||||||
async function syncEvents(syncedBlock) {
|
async function syncEvents() {
|
||||||
try {
|
try {
|
||||||
let targetBlock = await web3.eth.getBlockNumber();
|
let targetBlock = await web3.eth.getBlockNumber();
|
||||||
let chunks = 1000;
|
let chunks = 1000;
|
||||||
console.log("Querying latest events from RPC");
|
console.log("Querying latest events from RPC");
|
||||||
|
|
||||||
if (syncedBlock) {
|
|
||||||
startBlock = syncedBlock + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let i = startBlock; i < targetBlock; i += chunks) {
|
for (let i = startBlock; i < targetBlock; i += chunks) {
|
||||||
let fetchedEvents = [];
|
let fetchedEvents = [];
|
||||||
|
|
||||||
@ -941,12 +925,12 @@ async function fetchEvents({ type, currency, amount }) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function updateCache() {
|
function updateCache() {
|
||||||
try {
|
try {
|
||||||
const fileName = `./cache/${netName.toLowerCase()}/${type}s_${currency}_${amount}.json`;
|
const fileName = `./cache/${netName.toLowerCase()}/${type}s_${currency}_${amount}.json`;
|
||||||
const localEvents = await initJson(fileName);
|
const localEvents = initJson(fileName);
|
||||||
const events = localEvents.concat(fetchedEvents);
|
const events = localEvents.concat(fetchedEvents);
|
||||||
await fs.writeFileSync(fileName, JSON.stringify(events, null, 2), 'utf8');
|
fs.writeFileSync(fileName, JSON.stringify(events, null, 2), 'utf8');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new Error('Writing cache file failed:',error);
|
throw new Error('Writing cache file failed:',error);
|
||||||
}
|
}
|
||||||
@ -955,6 +939,7 @@ async function fetchEvents({ type, currency, amount }) {
|
|||||||
await updateCache();
|
await updateCache();
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
throw new Error("Error while updating cache");
|
throw new Error("Error while updating cache");
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
@ -1075,11 +1060,11 @@ async function fetchEvents({ type, currency, amount }) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function updateCache(fetchedEvents) {
|
function updateCache(fetchedEvents) {
|
||||||
try {
|
try {
|
||||||
let events = [];
|
let events = [];
|
||||||
const fileName = `./cache/${netName.toLowerCase()}/${type}s_${currency}_${amount}.json`;
|
const fileName = `./cache/${netName.toLowerCase()}/${type}s_${currency}_${amount}.json`;
|
||||||
const localEvents = await initJson(fileName);
|
const localEvents = initJson(fileName);
|
||||||
const totalEvents = localEvents.concat(fetchedEvents);
|
const totalEvents = localEvents.concat(fetchedEvents);
|
||||||
if (type === "deposit") {
|
if (type === "deposit") {
|
||||||
const commit = new Set();
|
const commit = new Set();
|
||||||
@ -1096,7 +1081,7 @@ async function fetchEvents({ type, currency, amount }) {
|
|||||||
return !notSameNull;
|
return !notSameNull;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
await fs.writeFileSync(fileName, JSON.stringify(events, null, 2), 'utf8');
|
fs.writeFileSync(fileName, JSON.stringify(events, null, 2), 'utf8');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new Error('Writing cache file failed:',error);
|
throw new Error('Writing cache file failed:',error);
|
||||||
}
|
}
|
||||||
@ -1136,8 +1121,9 @@ async function fetchEvents({ type, currency, amount }) {
|
|||||||
return startBlock - 1;
|
return startBlock - 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const syncedBlock = await fetchGraphEvents();
|
await fetchGraphEvents();
|
||||||
await syncEvents(syncedBlock);
|
startBlock = loadCachedEvents({ type, currency, amount }).lastBlock + 1;
|
||||||
|
await syncEvents();
|
||||||
}
|
}
|
||||||
if (!privateRpc && subgraph && !isTestRPC) {
|
if (!privateRpc && subgraph && !isTestRPC) {
|
||||||
await syncGraphEvents();
|
await syncGraphEvents();
|
||||||
|
Loading…
Reference in New Issue
Block a user