-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## High Level Overview of Change <!-- Please include a summary/list of the changes. If too broad, please consider splitting into multiple PRs. If a relevant Asana task, please link it here. --> Add a table to store amendments information: - Amendment’s hash - Amendment’s name - Enabled rippled version This will also provide more stable source of truth for AmendmentEnabled transaction in Explorer, which hits 2 repos every time it was called. This information will be accessed via `/network/amendments/info` endpoint. ### Type of Change <!-- Please check relevant options, delete irrelevant ones. --> - [ ] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] Refactor (non-breaking change that only restructures code) - [ ] Tests (You added tests for code that already exists, or your new feature included in this PR) - [ ] Documentation Updates - [ ] Release --------- Co-authored-by: Mayukha Vadari <mvadari@ripple.com> Co-authored-by: Caleb Kniffen <ckniffen@ripple.com>
- Loading branch information
1 parent
8c7d5e0
commit cb10166
Showing
10 changed files
with
425 additions
and
0 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,123 @@ | ||
import { Request, Response } from 'express' | ||
|
||
import { query } from '../../../shared/database' | ||
import { AmendmentsInfo } from '../../../shared/types' | ||
import { isEarlierVersion } from '../../../shared/utils' | ||
import logger from '../../../shared/utils/logger' | ||
|
||
interface AmendmentsInfoResponse { | ||
result: 'success' | 'error' | ||
count: number | ||
amendments: AmendmentsInfo[] | ||
} | ||
|
||
interface SingleAmendmentInfoResponse { | ||
result: 'success' | 'error' | ||
amendment: AmendmentsInfo | ||
} | ||
|
||
interface Cache { | ||
amendments: AmendmentsInfo[] | ||
time: number | ||
} | ||
|
||
const log = logger({ name: 'api-amendments' }) | ||
|
||
const cache: Cache = { | ||
amendments: [], | ||
time: Date.now(), | ||
} | ||
|
||
/** | ||
* Updates amendments in cache. | ||
* | ||
* @returns Void. | ||
*/ | ||
async function cacheAmendmentsInfo(): Promise<void> { | ||
try { | ||
cache.amendments = await query('amendments_info').select('*') | ||
cache.amendments.sort((prev: AmendmentsInfo, next: AmendmentsInfo) => { | ||
if (isEarlierVersion(prev.rippled_version, next.rippled_version)) { | ||
return 1 | ||
} | ||
return -1 | ||
}) | ||
cache.time = Date.now() | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO: clean up | ||
} catch (err: any) { | ||
log.error(err.toString()) | ||
} | ||
} | ||
|
||
void cacheAmendmentsInfo() | ||
|
||
/** | ||
* Handles Amendments Info request. | ||
* | ||
* @param _u - Unused express request. | ||
* @param res - Express response. | ||
*/ | ||
export async function handleAmendmentsInfo( | ||
_u: Request, | ||
res: Response, | ||
): Promise<void> { | ||
try { | ||
if (Date.now() - cache.time > 60 * 1000) { | ||
await cacheAmendmentsInfo() | ||
} | ||
const amendments: AmendmentsInfo[] = cache.amendments | ||
const response: AmendmentsInfoResponse = { | ||
result: 'success', | ||
count: amendments.length, | ||
amendments, | ||
} | ||
res.send(response) | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO: clean up | ||
} catch (err: any) { | ||
res.send({ result: 'error', message: 'internal error' }) | ||
log.error(err.toString()) | ||
} | ||
} | ||
|
||
/** | ||
* Handles Amendment Info request. | ||
* | ||
* @param req - Unused express request. | ||
* @param res - Express response. | ||
*/ | ||
export async function handleAmendmentInfo( | ||
req: Request, | ||
res: Response, | ||
): Promise<void> { | ||
try { | ||
const { param } = req.params | ||
if (Date.now() - cache.time > 60 * 1000) { | ||
await cacheAmendmentsInfo() | ||
} | ||
const amendments: AmendmentsInfo[] = cache.amendments.filter( | ||
(amend) => amend.name === param || amend.id === param, | ||
) | ||
if (amendments.length === 0) { | ||
res.send({ result: 'error', message: "incorrect amendment's id/name" }) | ||
return | ||
} | ||
if (amendments.length > 1) { | ||
res.send({ | ||
result: 'error', | ||
message: | ||
"there's a duplicate amendment's id/name on the server, please try again later", | ||
}) | ||
log.error("there's a duplicate amendment's id/name on the server", param) | ||
return | ||
} | ||
const response: SingleAmendmentInfoResponse = { | ||
result: 'success', | ||
amendment: amendments[0], | ||
} | ||
res.send(response) | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO: clean up | ||
} catch (err: any) { | ||
res.send({ result: 'error', message: 'internal error' }) | ||
log.error(err.toString()) | ||
} | ||
} |
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
Oops, something went wrong.