From 233dcee11f7aed78cd316f16b4dd05469a5ea1b5 Mon Sep 17 00:00:00 2001 From: Ladd Hoffman Date: Thu, 2 May 2024 19:32:52 -0500 Subject: [PATCH] retry contract calls --- backend/src/event-handlers/rollup.js | 9 +++++++-- backend/src/util/add-post-with-retry.js | 13 ++++--------- .../util/call-contract-method-with-retry.js | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 backend/src/util/call-contract-method-with-retry.js diff --git a/backend/src/event-handlers/rollup.js b/backend/src/event-handlers/rollup.js index 0f278d6..2fbfc3e 100644 --- a/backend/src/event-handlers/rollup.js +++ b/backend/src/event-handlers/rollup.js @@ -11,6 +11,7 @@ const { const read = require('../util/forum/read'); const write = require('../util/forum/write'); const addPostWithRetry = require('../util/add-post-with-retry'); +const callWithRetry = require('../util/call-contract-method-with-retry'); const { ROLLUP_BATCH_SIZE, @@ -39,7 +40,11 @@ const resetBatch = async () => { const stakeRollupAvailability = async () => { const currentRep = await dao.balanceOf(await wallet.getAddress()); if (currentRep) { - await dao.stakeAvailability(rollup.target, currentRep, availabilityStakeDuration); + await callWithRetry(() => dao.stakeAvailability( + rollup.target, + currentRep, + availabilityStakeDuration, + )); } }; @@ -95,7 +100,7 @@ const submitRollup = async () => { await stakeRollupAvailability(); // Call Rollup.submitBatch const poolDuration = 60; - await rollup.submitBatch(batchPostId, batchItems.length, poolDuration); + await callWithRetry(() => rollup.submitBatch(batchPostId, batchItems.length, poolDuration)); // Send matrix event await sendMatrixEvent('io.dgov.rollup.submit', { batchPostId, batchItems, authors }); console.log('Submitted batch', { batchPostId, batchItems, authors }); diff --git a/backend/src/util/add-post-with-retry.js b/backend/src/util/add-post-with-retry.js index ffcc60f..5ba00bf 100644 --- a/backend/src/util/add-post-with-retry.js +++ b/backend/src/util/add-post-with-retry.js @@ -1,16 +1,11 @@ -const Promise = require('bluebird'); - +const callWithRetry = require('./call-contract-method-with-retry'); const { dao } = require('./contracts'); -const addPostWithRetry = async (authors, hash, citations, retryDelay = 5000) => { +const addPostWithRetry = async (authors, hash, citations) => { try { - await dao.addPost(authors, hash, citations); + await callWithRetry(() => dao.addPost(authors, hash, citations)); } catch (e) { - if (e.code === 'REPLACEMENT_UNDERPRICED') { - console.log('retry delay (sec):', retryDelay / 1000); - await Promise.delay(retryDelay); - return addPostWithRetry(authors, hash, citations, retryDelay * 2); - } if (e.reason === 'A post with this postId already exists') { + if (e.reason === 'A post with this postId already exists') { return { alreadyAdded: true }; } throw e; diff --git a/backend/src/util/call-contract-method-with-retry.js b/backend/src/util/call-contract-method-with-retry.js new file mode 100644 index 0000000..e7ff337 --- /dev/null +++ b/backend/src/util/call-contract-method-with-retry.js @@ -0,0 +1,18 @@ +const Promise = require('bluebird'); + +const callWithRetry = async (contractCall, retryDelay = 5000) => { + let result; + try { + result = await contractCall(); + } catch (e) { + if (e.code === 'REPLACEMENT_UNDERPRICED') { + console.log('retry delay (sec):', retryDelay / 1000); + await Promise.delay(retryDelay); + return callWithRetry(contractCall, retryDelay * 2); + } + throw e; + } + return result; +}; + +module.exports = callWithRetry;