Skip to content

Commit

Permalink
Add update contract addresses (#356)
Browse files Browse the repository at this point in the history
* refactor `getContractsWithRetry()`

* add missing `AbortController` and jitter to existing loop

* add update contract loop

* Update lib/zinnia.js

Co-authored-by: Miroslav Bajtoš <[email protected]>

---------

Co-authored-by: Miroslav Bajtoš <[email protected]>
  • Loading branch information
juliangruber and bajtos authored Feb 20, 2024
1 parent db4b70a commit f219020
Showing 1 changed file with 54 additions and 29 deletions.
83 changes: 54 additions & 29 deletions lib/zinnia.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,28 @@ async function getContractAddresses () {
return revision.value.split('\n').filter(Boolean)
}

async function getContractsWithRetry (provider, abi) {
const contractAddresses = await pRetry(getContractAddresses, {
retries: 10,
onFailedAttempt: err => {
console.error(err)
console.error('Failed to get contract addresses. Retrying...')
if (String(err).includes('You are being rate limited')) {
const delaySeconds = 30 + (Math.random() * 60)
// Don't DDOS the w3name services
console.error(
`Rate limited. Waiting ${delaySeconds} seconds...`
)
return timers.setTimeout(delaySeconds * 1000)
}
}
})
console.error(`Meridian contract addresses: ${contractAddresses.join(', ')}`)
return contractAddresses.map(address => {
return new ethers.Contract(address, abi, provider)
})
}

export async function run ({
FIL_WALLET_ADDRESS,
ethAddress,
Expand All @@ -125,36 +147,14 @@ export async function run ({
null,
{ batchMaxCount: 1 }
)

const contractAddresses = await pRetry(getContractAddresses, {
retries: 10,
onFailedAttempt: err => {
console.error(err)
console.error('Failed to get contract addresses. Retrying...')
if (String(err).includes('You are being rate limited')) {
const delaySeconds = 30 + (Math.random() * 60)
// Don't DDOS the w3name services
console.error(
`Rate limited. Waiting ${delaySeconds} seconds...`
)
return timers.setTimeout(delaySeconds * 1000)
}
}
})
console.error(`Meridian contract addresses: ${contractAddresses.join(', ')}`)
const contracts = await Promise.all(contractAddresses.map(async address => {
return new ethers.Contract(
address,
JSON.parse(
await fs.readFile(
fileURLToPath(new URL('./abi.json', import.meta.url)),
'utf8'
)
),
provider
const abi = JSON.parse(
await fs.readFile(
fileURLToPath(new URL('./abi.json', import.meta.url)),
'utf8'
)
}))
)

const contracts = await getContractsWithRetry(provider, abi)
const zinniadExe = getBinaryModuleExecutable({ module: 'zinnia', executable: 'zinniad' })

if (!isUpdated) {
Expand Down Expand Up @@ -221,11 +221,20 @@ export async function run ({
}

let shouldRestart
const controller = new AbortController()
const { signal } = controller

await Promise.all([
(async () => {
while (true) {
await timers.setTimeout(10 * 60 * 1000) // 10 minutes
const delay = 10 * 60 * 1000 // 10 minutes
const jitter = Math.random() * 20_000 - 10_000 // +- 10 seconds
try {
await timers.setTimeout(delay + jitter, null, { signal })
} catch (err) {
if (err.name === 'AbortError') return
throw err
}
try {
shouldRestart = await updateAllSourceFiles({
moduleVersionsDir,
Expand All @@ -251,6 +260,21 @@ export async function run ({
}
}
})(),
(async () => {
while (true) {
const delay = 10 * 60 * 1000 // 10 minutes
const jitter = Math.random() * 20_000 - 10_000 // +- 10 seconds
try {
await timers.setTimeout(delay + jitter, null, { signal })
} catch (err) {
if (err.name === 'AbortError') return
throw err
}
const newContracts = await getContractsWithRetry(provider, abi)
contracts.splice(0)
contracts.push(...newContracts)
}
})(),
(async () => {
try {
await Promise.race(childProcesses)
Expand All @@ -276,6 +300,7 @@ export async function run ({
childProcess.stderr.removeAllListeners()
childProcess.stdout.removeAllListeners()
}
controller.abort()
})()
])

Expand Down

0 comments on commit f219020

Please sign in to comment.