generated from hirosystems/.github
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: fetch pox constants from stacks-core /v2/pox and store in pg #7
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate'; | ||
|
||
export const shorthands: ColumnDefinitions | undefined = undefined; | ||
|
||
export function up(pgm: MigrationBuilder): void { | ||
pgm.addColumns('chain_tip', { | ||
first_burnchain_block_height: { | ||
type: 'integer', | ||
default: null, | ||
}, | ||
reward_cycle_length: { | ||
type: 'integer', | ||
default: null, | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { logger } from '@hirosystems/api-toolkit'; | ||
import { PgStore } from '../pg/pg-store'; | ||
import { sleep } from '../helpers'; | ||
import { ENV } from '../env'; | ||
|
||
// How long to wait between PoX rpc fetches when the database already has PoX info | ||
const POX_INFO_UPDATE_INTERVAL_MS = 30000; | ||
|
||
// How long to wait between retries when fetching PoX info fails and the database is missing PoX info | ||
const POX_INFO_UPDATE_CRITICAL_RETRY_INTERVAL_MS = 3000; | ||
|
||
export function startPoxInfoUpdater(args: { db: PgStore }) { | ||
const abortController = new AbortController(); | ||
void runPoxInfoBackgroundJob(args.db, abortController.signal); | ||
return { | ||
close: () => abortController.abort(), | ||
}; | ||
} | ||
|
||
async function runPoxInfoBackgroundJob(db: PgStore, abortSignal: AbortSignal) { | ||
let isDbMissingPoxInfo: boolean | null = null; | ||
while (!abortSignal.aborted) { | ||
try { | ||
// Check if isDbMissingPoxInfo is null, which means we haven't checked the database yet | ||
if (isDbMissingPoxInfo === null) { | ||
const dbPoxInfo = await db.getPoxInfo(); | ||
isDbMissingPoxInfo = dbPoxInfo.reward_cycle_length === null; | ||
} | ||
|
||
if (isDbMissingPoxInfo) { | ||
logger.info( | ||
`Database is missing PoX info, fetching from stacks-core RPC ${getStacksNodeUrl()}` | ||
); | ||
} | ||
const rpcPoxInfo = await fetchRpcPoxInfo(abortSignal); | ||
if (isDbMissingPoxInfo) { | ||
logger.info( | ||
`Fetched PoX info from stacks-core RPC: first_burnchain_block_height=${rpcPoxInfo.first_burnchain_block_height}, reward_cycle_length=${rpcPoxInfo.reward_cycle_length}, storing in database` | ||
); | ||
} | ||
await db.updatePoxInfo(rpcPoxInfo); | ||
isDbMissingPoxInfo = false; | ||
await sleep(POX_INFO_UPDATE_INTERVAL_MS, abortSignal); | ||
} catch (error) { | ||
if (abortSignal.aborted) { | ||
return; | ||
} | ||
if (isDbMissingPoxInfo) { | ||
logger.error( | ||
error, | ||
`Failed to fetch PoX info from stacks-core RPC, retrying in ${POX_INFO_UPDATE_CRITICAL_RETRY_INTERVAL_MS}ms ...` | ||
); | ||
await sleep(POX_INFO_UPDATE_CRITICAL_RETRY_INTERVAL_MS, abortSignal); | ||
} else { | ||
logger.warn( | ||
error, | ||
`Failed to update PoX info (database already has PoX info, this is not critical)` | ||
); | ||
await sleep(POX_INFO_UPDATE_INTERVAL_MS, abortSignal); | ||
} | ||
} | ||
} | ||
} | ||
|
||
interface PoxInfo { | ||
first_burnchain_block_height: number; | ||
reward_cycle_length: number; | ||
} | ||
|
||
function getStacksNodeUrl(): string { | ||
return `http://${ENV.STACKS_NODE_RPC_HOST}:${ENV.STACKS_NODE_RPC_PORT}`; | ||
} | ||
|
||
async function fetchRpcPoxInfo(abortSignal: AbortSignal) { | ||
const url = `${getStacksNodeUrl()}/v2/pox`; | ||
const res = await fetch(url, { signal: abortSignal }); | ||
const json = await res.json(); | ||
return json as PoxInfo; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could return and end the background
/v2/pox
fetches, but I don't think it hurts and it can provide some possibly useful debugging data (we can see RPC connection error logs if something weird is happening later on).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I doubt a chain can change phase lengths during operation, so probably not needed, but I don't see the problem with keeping it for logs/interest 😉