Skip to content

Commit

Permalink
capture error and attempt to repeat query
Browse files Browse the repository at this point in the history
  • Loading branch information
JukLee0ira committed Oct 8, 2024
1 parent 55847a3 commit ad2625e
Showing 1 changed file with 75 additions and 19 deletions.
94 changes: 75 additions & 19 deletions src/opCodeFinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,36 +65,40 @@ async function validateInputs(args) {

async function findContracts(startBlockNumber, endBlockNumber) {
let contractCount = 0;
let failedTxs = 0;

for (let i = startBlockNumber; i <= endBlockNumber; i++) {
process.stdout.write(
`\rSearching for block ${i} ,total found ${contractCount} contracts, ${matchContractCount} of which meet the criteria`
`\rSearching for block ${i} ,total found ${contractCount} contracts, ${matchContractCount} of which meet the criteria, ${failedTxs} transactions queried failed`
);
let block = await web3.eth.getBlock(i);
if (block != null) {
if (block.transactions != null) {
let blockFinishedCount = block.transactions.length;
//trace all the transactions in this block
for (let txHash of block.transactions) {
let traceObject = await web3.eth.currentProvider.sendAsync({
method: "debug_traceTransaction",
params: [`${txHash}`, { tracer: "callTracer" }],
jsonrpc: "2.0",
id: "1",
});
//check if the type of the call is for creating a contract
if (
traceObject.result.type === "CREATE" ||
traceObject.result.type === "CREATE2"
) {
getContractBytecode(traceObject.result.to);
contractCount++;
try {
//get the trace object of the transaction
console.log("Processing transaction:", txHash);
const traceObject = await processTransactionsWithRetry(txHash);
//check if the transaction created a contract
if (
traceObject.result.type === "CREATE" ||
traceObject.result.type === "CREATE2"
) {
getContractBytecode(traceObject.result.to);
contractCount++;
}
// go through any nested calls within the trace.
if (
traceObject.result.calls &&
traceObject.result.calls.length > 0
) {
contractCount += isContractCreation(traceObject.result.calls);
}
} catch (error) {
failedTxs++;
}
// go through any nested calls within the trace.
if (traceObject.result.calls && traceObject.result.calls.length > 0) {
contractCount += isContractCreation(traceObject.result.calls);
}

blockFinishedCount--;
}
while (blockFinishedCount > 0) {
Expand All @@ -106,6 +110,58 @@ async function findContracts(startBlockNumber, endBlockNumber) {
console.log();
}

async function processTransactionsWithRetry(
txHash,
maxRetries = 3,
initialDelay = 1000
) {
let retries = 0;
while (retries < maxRetries) {
try {
let traceObject = await web3.eth.currentProvider.sendAsync({
method: "debug_traceTransaction",
params: [`${txHash}`, { tracer: "callTracer" }],
jsonrpc: "2.0",
id: "1",
});
//if the trace object is not found or there is an error, throw an error
if (!traceObject || traceObject.error) {
throw new Error(
"Failed to get trace object: " +
(traceObject.error ? traceObject.error.message : "unknown error")
);
}
//if the trace object is found, return it
return traceObject;
} catch (error) {
//if the trace object is not found or there is an error, retry
retries++;
//if the retries reach the maximum, handle failed transactions
if (retries >= maxRetries) {
// console.log("以下交易处理失败,需要进一步处理:", txHash);
// // 可以在这里实现进一步的处理逻辑,比如保存到数据库或者发送通知
// // TODO: 怎样处理?写入日志中,然后显示失败的交易多少个
// save the failed transaction to a file
fs.appendFile(
"failed-transactions.txt",
txHash + "\t" + error.message + "\n",
{ encoding: "utf8" },
(err) => {
if (err) {
console.error("Error occurred while appending content.");
return;
}
}
);
throw error;
}
await new Promise((resolve) =>
setTimeout(resolve, initialDelay * Math.pow(2, retries))
);
}
}
}

//check if the tx created a contract
function isContractCreation(traceRes) {
let contractCount = 0;
Expand Down

0 comments on commit ad2625e

Please sign in to comment.