Repository navigation
fix: redirect v3 tx id and block hash params missing the 0x prefix #2712
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,139 @@ | ||
| import { describe, test, beforeEach, afterEach } from 'node:test'; | ||
| import * as assert from 'node:assert/strict'; | ||
| import { STACKS_TESTNET } from '@stacks/network'; | ||
| import { PgWriteStore } from '../../../src/datastore/pg-write-store.ts'; | ||
| import { ApiServer, startApiServer } from '../../../src/api/init.ts'; | ||
| import { migrate } from '../../test-helpers.ts'; | ||
| import { hex } from '../test-helpers.ts'; | ||
| import { TestBlockBuilder } from '../test-builders.ts'; | ||
|
|
||
| /** | ||
| * Every v3 endpoint that takes a tx id or block hash answers a hash supplied without its `0x` | ||
| * prefix with a `302` to the same URL with the prefix added, keeping the rest of the querystring. | ||
| */ | ||
|
|
||
| const PRINCIPAL = 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27'; | ||
| const TX_1 = hex(1); | ||
| const TX_2 = hex(2); | ||
| const BLOCK_HASH = hex(0xb10c); | ||
| const bare = (hash: string) => hash.slice(2); | ||
|
|
||
| describe('v3 unprefixed hash redirects', () => { | ||
| let db: PgWriteStore; | ||
| let api: ApiServer; | ||
|
|
||
| const get = (url: string) => api.fastifyApp.inject({ method: 'GET', url }); | ||
|
|
||
| beforeEach(async () => { | ||
| await migrate('up'); | ||
| db = await PgWriteStore.connect({ | ||
| usageName: 'tests', | ||
| withNotifier: false, | ||
| skipMigrations: true, | ||
| }); | ||
| api = await startApiServer({ datastore: db, chainId: STACKS_TESTNET.chainId }); | ||
| await db.update( | ||
| new TestBlockBuilder({ | ||
| block_height: 1, | ||
| block_hash: BLOCK_HASH, | ||
| index_block_hash: BLOCK_HASH, | ||
| }) | ||
| .addTx({ tx_id: TX_1, sender_address: PRINCIPAL }) | ||
| .build() | ||
| ); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await api.terminate(); | ||
| await db?.close(); | ||
| await migrate('down'); | ||
| }); | ||
|
|
||
| const pathCases: [string, string, string][] = [ | ||
| ['transaction', `/extended/v3/transactions/${bare(TX_1)}`, `/extended/v3/transactions/${TX_1}`], | ||
| [ | ||
| 'transaction with querystring', | ||
| `/extended/v3/transactions/${bare(TX_1)}?include=post_conditions`, | ||
| `/extended/v3/transactions/${TX_1}?include=post_conditions`, | ||
| ], | ||
| [ | ||
| 'transaction events', | ||
| `/extended/v3/transactions/${bare(TX_1)}/events?limit=5`, | ||
| `/extended/v3/transactions/${TX_1}/events?limit=5`, | ||
| ], | ||
| [ | ||
| 'principal transaction balance changes', | ||
| `/extended/v3/principals/${PRINCIPAL}/transactions/${bare(TX_1)}/balance-changes`, | ||
| `/extended/v3/principals/${PRINCIPAL}/transactions/${TX_1}/balance-changes`, | ||
| ], | ||
| [ | ||
| 'block transactions', | ||
| `/extended/v3/blocks/${bare(BLOCK_HASH)}/transactions`, | ||
| `/extended/v3/blocks/${BLOCK_HASH}/transactions`, | ||
| ], | ||
| ]; | ||
|
|
||
| for (const [name, url, location] of pathCases) { | ||
| test(`redirects ${name} path param`, async () => { | ||
| const res = await get(url); | ||
| assert.equal(res.statusCode, 302); | ||
| assert.equal(res.headers.location, location); | ||
| }); | ||
| } | ||
|
|
||
| const queryCases: [string, string, string][] = [ | ||
| [ | ||
| 'batch, repeated', | ||
| `/extended/v3/transactions/batch?tx_id=${bare(TX_1)}&tx_id=${TX_2}`, | ||
| `/extended/v3/transactions/batch?tx_id=${TX_1}&tx_id=${TX_2}`, | ||
| ], | ||
| [ | ||
| 'batch, comma-separated', | ||
| `/extended/v3/transactions/batch?tx_id=${TX_1},${bare(TX_2)}`, | ||
| `/extended/v3/transactions/batch?tx_id=${TX_1},${TX_2}`, | ||
| ], | ||
| [ | ||
| 'batch, encoded comma', | ||
| `/extended/v3/transactions/batch?tx_id=${bare(TX_1)}%2C${bare(TX_2)}`, | ||
| `/extended/v3/transactions/batch?tx_id=${TX_1},${TX_2}`, | ||
| ], | ||
| [ | ||
| 'principal balance changes, other params kept in place', | ||
| `/extended/v3/principals/${PRINCIPAL}/balance-changes?limit=5&tx_id=${bare(TX_1)}`, | ||
| `/extended/v3/principals/${PRINCIPAL}/balance-changes?limit=5&tx_id=${TX_1}`, | ||
| ], | ||
| ]; | ||
|
|
||
| for (const [name, url, location] of queryCases) { | ||
| test(`redirects ${name} query param`, async () => { | ||
| const res = await get(url); | ||
| assert.equal(res.statusCode, 302); | ||
| assert.equal(res.headers.location, location); | ||
| }); | ||
| } | ||
|
|
||
| test('does not redirect prefixed hashes, heights or aliases', async () => { | ||
| for (const url of [ | ||
| `/extended/v3/transactions/${TX_1}`, | ||
| `/extended/v3/transactions/batch?tx_id=${TX_1}`, | ||
| `/extended/v3/blocks/${BLOCK_HASH}/transactions`, | ||
| `/extended/v3/blocks/1/transactions`, | ||
| `/extended/v3/blocks/latest/transactions`, | ||
| ]) { | ||
| const res = await get(url); | ||
| assert.equal(res.statusCode, 200, url); | ||
| } | ||
| }); | ||
|
|
||
| test('leaves malformed ids to schema validation', async () => { | ||
| const res = await get(`/extended/v3/transactions/${bare(TX_1).slice(1)}`); | ||
| assert.equal(res.statusCode, 400); | ||
| }); | ||
|
|
||
| test('redirect target resolves the transaction', async () => { | ||
| const redirect = await get(`/extended/v3/transactions/${bare(TX_1)}`); | ||
| const res = await get(redirect.headers.location as string); | ||
| assert.equal(res.statusCode, 200); | ||
| assert.equal(JSON.parse(res.body).tx_id, TX_1); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.