Skip to content

Commit

Permalink
optimize block scan
Browse files Browse the repository at this point in the history
  • Loading branch information
khaneight committed Jul 6, 2020
1 parent fff83e6 commit a0cbbaa
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions examples/blockexplorerapi/readAllBlocksToDatabase.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ function keepWeb3Alive() {
lastConnectTimer = null;
console.log("STARTING WEB3 connection");
provider = new Web3.providers.WebsocketProvider(process.env.CONNECT_STRING, {
timeout: 60000,
timeout: 150000,
clientConfig: {
maxReceivedFrameSize: 100000000,
maxReceivedMessageSize: 100000000
Expand Down Expand Up @@ -625,7 +625,7 @@ async function getTransactionLog(transaction) {

let balancesToGet = {};

async function getBalances( sql, conn, addrs, index, resolve, reject) {
async function getBalances(sql, conn, addrs, index) {
if ( !sql ) {
sql = ""
}
Expand All @@ -640,7 +640,6 @@ async function getBalances( sql, conn, addrs, index, resolve, reject) {
conn.release()
conn = null
}
resolve(true);
return;
} catch (err) {
if ( conn && !conn.__released ) {
Expand All @@ -649,7 +648,6 @@ async function getBalances( sql, conn, addrs, index, resolve, reject) {
conn = null
}
console.log(" getAllBalances error ", err);
reject(err);
return
}
}
Expand All @@ -671,7 +669,7 @@ async function getBalances( sql, conn, addrs, index, resolve, reject) {
if (balancesReturned[address] && balancesReturned[address] > lastBlock) {
// we have this balance already
console.log("ALREADY HAVE BALANCE " + address);
getBalances(sql, conn, addrs, index + 1, resolve, reject);
getBalances(sql, conn, addrs, index + 1);
return;
}
console.log(`[${chalk.green('BALANCE')}] Getting balance for ${address}`);
Expand Down Expand Up @@ -718,15 +716,14 @@ async function getBalances( sql, conn, addrs, index, resolve, reject) {

balancesReturned[address] = glb_highestBlockOnChain;

getBalances(sql,conn, addrs, index + 1, resolve, reject);
getBalances(sql,conn, addrs, index + 1);
} catch (err) {
if ( conn && !conn.__released ) {
conn.__released = true
conn.release()
conn = null
}
console.log(" getAllBalances error ", err);
reject(err);
} finally {
}
}
Expand Down Expand Up @@ -761,7 +758,8 @@ async function logTransaction( conn , block, transactions, index, resolve, rejec
if (transactions.length === index) {
let keys = Object.keys(balancesToGet);
if (keys.length) {
return getBalances( "", conn, keys, 0, resolve, reject);
getBalances( "", conn, keys, 0);
resolve(true);
} else {
if ( conn && !conn.__released ) {
conn.__released = true
Expand Down Expand Up @@ -1162,7 +1160,6 @@ function resumeBlockScan() {
return;
}

updateOnlinePrice();
if (lastBlock === -1) {
lastBlock = 0;
}
Expand Down Expand Up @@ -1252,7 +1249,7 @@ function scheduleNewScan(timeToSet) {
timerSet = setTimeout(() => {
timerSet = null;
resumeBlockScan();
}, timeToSet || 14000);
}, timeToSet || 4500);
}
}

Expand Down Expand Up @@ -1290,6 +1287,7 @@ async function doBlockScan() {
}
await updateLastBlockProcessed();
lastBlock += 1;
updateOnlinePrice();
scheduleNewScan(10);
} catch (err) {
console.log("uncaught error, try again ", err);
Expand Down

0 comments on commit a0cbbaa

Please sign in to comment.