Skip to content

Commit 18371d6

Browse files
Fix processing logic in massive-replace-script to handle errors and update contract counter correctly (#2280)
1 parent 23ebb55 commit 18371d6

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

services/database/massive-replace-script/config-replace-creation-information.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ module.exports = {
2727
AND c.runtime_code_hash IS NOT NULL
2828
-- Exclude unsupported chains
2929
AND cd.chain_id NOT IN (3,4,5,28,43,50,69,77,99,300,420,534,570,592,842,1291,1433,1516,2017,2358,7701,9977,10242,10243,11111,12898,13381,16350,17069,26100,28528,32770,33101,33103,48899,57000,71401,78430,78431,78432,80001,84531,103090,167005,167006,202401,420420,420666,421611,421613,1127469,192837465,222000222,333000333,356256156,486217935,4216137055,7078815900)
30+
-- -- Exclude failing RPC chains
31+
-- AND cd.chain_id NOT IN (40,10200)
3032
AND sm.id >= $1
3133
ORDER BY sm.id ASC
3234
LIMIT $2
@@ -38,6 +40,7 @@ module.exports = {
3840
return {
3941
chainId: contract.chain_id.toString(),
4042
address: `0x${contract.address.toString("hex")}`,
43+
transactionHash: `0x${contract.transaction_hash.toString("hex")}`,
4144
forceCompilation: false,
4245
forceRPCRequest: true,
4346
customReplaceMethod: "replace-creation-information",

services/database/massive-replace-script/massive-replace-script.ts

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ if (fs.existsSync(COUNTER_FILE)) {
3434
);
3535
}
3636

37-
const N = 200; // Number of contracts to process at a time
37+
const N = 5; // Number of contracts to process at a time
3838

3939
const POSTGRES_SCHEMA = process.env.POSTGRES_SCHEMA || "public";
4040

@@ -117,7 +117,10 @@ async function processContract(
117117

118118
console.log(`✅ Successfully processed contract ${address}:`, result);
119119
} catch (error) {
120-
console.error(`❌ Failed to process contract ${address}:`, error);
120+
console.error(
121+
`❌ Failed to process contract ${address} at chain ${contract.chain_id}:`,
122+
error,
123+
);
121124
throw error;
122125
}
123126
}
@@ -155,37 +158,40 @@ async function processContract(
155158

156159
verifiedContractCount = rowCount || 0;
157160

161+
let secondToWait = 2;
158162
// Process the batch in parallel
159163
try {
160164
const processingPromises = verifiedContracts.map((contract) =>
161165
processContract(contract, config),
162166
);
163167
await Promise.all(processingPromises);
164-
165-
// Update the counter file only after the batch successfully completes
166-
if (verifiedContracts.length > 0) {
167-
const lastProcessedId =
168-
verifiedContracts[verifiedContracts.length - 1]
169-
.verified_contract_id ||
170-
verifiedContracts[verifiedContracts.length - 1].id;
171-
CURRENT_VERIFIED_CONTRACT = parseInt(lastProcessedId) + 1;
172-
173-
// Use async write to avoid blocking
174-
fs.writeFile(
175-
COUNTER_FILE,
176-
CURRENT_VERIFIED_CONTRACT.toString(),
177-
"utf8",
178-
(err) => {
179-
if (err) {
180-
console.error("Error writing counter file:", err);
181-
}
182-
},
183-
);
184-
}
185168
} catch (batchError) {
169+
secondToWait = 5; // Increase wait time on error
186170
console.error("Error processing batch:", batchError);
187171
}
188172

173+
// Update the counter file only after the batch successfully completes
174+
175+
const lastProcessedId =
176+
verifiedContracts[verifiedContracts.length - 1].verified_contract_id ||
177+
verifiedContracts[verifiedContracts.length - 1].id;
178+
CURRENT_VERIFIED_CONTRACT = parseInt(lastProcessedId) + 1;
179+
180+
// Use async write to avoid blocking
181+
fs.writeFile(
182+
COUNTER_FILE,
183+
CURRENT_VERIFIED_CONTRACT.toString(),
184+
"utf8",
185+
(err) => {
186+
if (err) {
187+
console.error("Error writing counter file:", err);
188+
}
189+
},
190+
);
191+
192+
console.log(`waiting ${secondToWait} seconds`);
193+
await new Promise((resolve) => setTimeout(resolve, secondToWait * 1000));
194+
189195
const endIterationTime = performance.now();
190196
const iterationTimeTaken = endIterationTime - startIterationTime;
191197
console.log(

0 commit comments

Comments
 (0)