Try to catch arbitrum error

This commit is contained in:
Marco van Dijk 2022-04-30 13:40:36 +02:00
parent 95ad97c870
commit 57f4f76f8b

View File

@ -1093,6 +1093,8 @@ SMART CONTRACT EVENTS - SYNC BLOCKS
*/ */
hasError = false;
// Syncs events database // Syncs events database
const syncEvents = function (toBlock) { const syncEvents = function (toBlock) {
console.log("Starting sync process for Bonding Manager events to block " + toBlock); console.log("Starting sync process for Bonding Manager events to block " + toBlock);
@ -1148,6 +1150,9 @@ const syncEvents = function (toBlock) {
} }
catch (err) { catch (err) {
console.log("FATAL ERROR: ", err); console.log("FATAL ERROR: ", err);
hasError = true;
isEventSyncing = false;
return;
} }
isEventSyncing = false; isEventSyncing = false;
}); });
@ -1196,6 +1201,9 @@ const syncTickets = function (toBlock) {
} }
catch (err) { catch (err) {
console.log("FATAL ERROR: ", err); console.log("FATAL ERROR: ", err);
hasError = true;
isTicketSyncing = false;
return;
} }
isTicketSyncing = false; isTicketSyncing = false;
}); });
@ -1415,77 +1423,94 @@ const initSync = async function () {
let cycle = 0; let cycle = 0;
// Does the actual looping over last parsed block -> latest block in chain // Does the actual looping over last parsed block -> latest block in chain
const handleSync = async function () { const handleSync = async function () {
if (!CONF_DISABLE_DB && !startedInitSync) { try {
console.log("Preloading all the things from the database"); if (!CONF_DISABLE_DB && !startedInitSync) {
await initSync(); console.log("Preloading all the things from the database");
} await initSync();
cycle++; }
console.log('Starting new sync cycle #' + cycle); cycle++;
isSyncing = true; console.log('Starting new sync cycle #' + cycle);
// Get latest block in chain isSyncing = true;
const latestBlock = await web3layer2.eth.getBlockNumber(); // Get latest block in chain
if (latestBlock > latestBlockInChain) { const latestBlock = await web3layer2.eth.getBlockNumber();
latestBlockInChain = latestBlock; if (latestBlock > latestBlockInChain) {
console.log("Latest L2 Eth block changed to " + latestBlockInChain); latestBlockInChain = latestBlock;
} else { console.log("Latest L2 Eth block changed to " + latestBlockInChain);
// If there are no new blocks, wait for 10 seconds before retrying } else {
console.log("No new blocks. Sleeping for 10 seconds..."); // If there are no new blocks, wait for 10 seconds before retrying
console.log("No new blocks. Sleeping for 10 seconds...");
setTimeout(() => {
handleSync();
}, 10000);
return;
}
console.log("Needs to sync " + (latestBlockInChain - lastBlockEvents) + " blocks for Events sync");
console.log("Needs to sync " + (latestBlockInChain - lastBlockTickets) + " blocks for Tickets sync");
// Batch requests when sync is large, mark if we are going to reach latestBlockInChain in this round
let getFinalTickets = false;
let toTickets = 'latest';
if (latestBlock - lastBlockTickets > 1000000) {
toTickets = lastBlockTickets + 1000000;
} else {
getFinalTickets = true;
}
let getFinalEvents = false;
let toEvents = 'latest';
if (latestBlock - lastBlockEvents > 1000000) {
toEvents = lastBlockEvents + 1000000;
} else {
getFinalEvents = true;
}
// Start initial sync for this sync round
syncTickets(toTickets);
syncEvents(toEvents);
// Then loop until we have reached the last known block
while (isEventSyncing || isTicketSyncing || !getFinalTickets || !getFinalEvents) {
await sleep(500);
if (hasError){
throw("Error while syncing");
}
if (isEventSyncing) {
console.log("Parsed " + lastBlockEvents + " out of " + latestBlockInChain + " blocks for Event sync");
} else if (!getFinalEvents) {
// Start next batch for events
toEvents = 'latest';
if (latestBlock - lastBlockEvents > 1000000) {
toEvents = lastBlockEvents + 1000000;
} else {
getFinalEvents = true;
}
syncEvents(toEvents);
}
if (isTicketSyncing) {
console.log("Parsed " + lastBlockTickets + " out of " + latestBlockInChain + " blocks for Ticket sync");
} else if (!getFinalTickets) {
// Start next batch for tickets
toTickets = 'latest';
if (latestBlock - lastBlockTickets > 1000000) {
toTickets = lastBlockTickets + 1000000;
} else {
getFinalTickets = true;
}
syncTickets(toTickets);
}
}
isSyncing = false;
setTimeout(() => { setTimeout(() => {
handleSync(); handleSync();
}, 10000) }, 10000);
return;
} }
console.log("Needs to sync " + (latestBlockInChain - lastBlockEvents) + " blocks for Events sync"); catch (err) {
console.log("Needs to sync " + (latestBlockInChain - lastBlockTickets) + " blocks for Tickets sync"); console.log("Error while syncing. Retrying in 30 seconds");
// Batch requests when sync is large, mark if we are going to reach latestBlockInChain in this round console.log("latestBlockInChain " + latestBlockInChain);
let getFinalTickets = false; console.log("lastBlockEvents " + lastBlockEvents);
let toTickets = 'latest'; console.log("lastBlockTickets " + lastBlockTickets);
if (latestBlock - lastBlockTickets > 1000000) { isSyncing = false;
toTickets = lastBlockTickets + 1000000; setTimeout(() => {
} else { handleSync();
getFinalTickets = true; }, 30000)
} }
let getFinalEvents = false;
let toEvents = 'latest';
if (latestBlock - lastBlockEvents > 1000000) {
toEvents = lastBlockEvents + 1000000;
} else {
getFinalEvents = true;
}
// Start initial sync for this sync round
syncTickets(toTickets);
syncEvents(toEvents);
// Then loop until we have reached the last known block
while (isEventSyncing || isTicketSyncing || !getFinalTickets || !getFinalEvents) {
await sleep(500);
if (isEventSyncing) {
console.log("Parsed " + lastBlockEvents + " out of " + latestBlockInChain + " blocks for Event sync");
} else if (!getFinalEvents) {
// Start next batch for events
toEvents = 'latest';
if (latestBlock - lastBlockEvents > 1000000) {
toEvents = lastBlockEvents + 1000000;
} else {
getFinalEvents = true;
}
syncEvents(toEvents);
}
if (isTicketSyncing) {
console.log("Parsed " + lastBlockTickets + " out of " + latestBlockInChain + " blocks for Ticket sync");
} else if (!getFinalTickets) {
// Start next batch for tickets
toTickets = 'latest';
if (latestBlock - lastBlockTickets > 1000000) {
toTickets = lastBlockTickets + 1000000;
} else {
getFinalTickets = true;
}
syncTickets(toTickets);
}
}
isSyncing = false;
setTimeout(() => {
handleSync();
}, 10000)
}; };
if (!isSyncing && !CONF_SIMPLE_MODE) { if (!isSyncing && !CONF_SIMPLE_MODE) {
console.log("Starting sync process"); console.log("Starting sync process");