Skip to content

Commit

Permalink
Merge pull request #2010 from hirosystems/develop
Browse files Browse the repository at this point in the history
Cut beta release
  • Loading branch information
zone117x authored Jun 18, 2024
2 parents 3badbf0 + ae78773 commit 17e4b07
Show file tree
Hide file tree
Showing 10 changed files with 2,679 additions and 63 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ jobs:
parallel: true

test-subnets:
if: false
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
18 changes: 18 additions & 0 deletions docs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,24 @@ paths:
items:
type: string
enum: [coinbase, token_transfer, smart_contract, contract_call, poison_microblock, tenure_change]
- name: sort_by
in: query
description: Option to sort results by block height, timestamp, or fee
required: false
schema:
type: string
enum: [block_height, burn_block_time, fee]
example: burn_block_time
default: block_height
- name: order
in: query
description: Option to sort results in ascending or descending order
required: false
schema:
type: string
enum: [asc, desc]
example: desc
default: desc
- name: unanchored
in: query
description: Include transaction data from unanchored (i.e. unconfirmed) microblocks
Expand Down
11 changes: 11 additions & 0 deletions migrations/1718632097776_tx-sort-indexes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/** @param { import("node-pg-migrate").MigrationBuilder } pgm */
exports.up = pgm => {
pgm.createIndex('txs', 'burn_block_time');
pgm.createIndex('txs', 'fee_rate');
};

/** @param { import("node-pg-migrate").MigrationBuilder } pgm */
exports.down = pgm => {
pgm.dropIndex('txs', 'burn_block_time');
pgm.dropIndex('txs', 'fee_rate');
};
31 changes: 31 additions & 0 deletions src/api/routes/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,43 @@ export function createTxRouter(db: PgStore): express.Router {
txTypeFilter = [];
}

let order: 'asc' | 'desc' | undefined;
if (req.query.order) {
if (
typeof req.query.order === 'string' &&
(req.query.order === 'asc' || req.query.order === 'desc')
) {
order = req.query.order;
} else {
throw new InvalidRequestError(
`The "order" query parameter must be a 'desc' or 'asc'`,
InvalidRequestErrorType.invalid_param
);
}
}

let sortBy: 'block_height' | 'burn_block_time' | 'fee' | undefined;
if (req.query.sort_by) {
if (
typeof req.query.sort_by === 'string' &&
['block_height', 'burn_block_time', 'fee'].includes(req.query.sort_by)
) {
sortBy = req.query.sort_by as typeof sortBy;
} else {
throw new InvalidRequestError(
`The "sort_by" query parameter must be 'block_height', 'burn_block_time', or 'fee'`,
InvalidRequestErrorType.invalid_param
);
}
}
const includeUnanchored = isUnanchoredRequest(req, res, next);
const { results: txResults, total } = await db.getTxList({
offset,
limit,
txTypeFilter,
includeUnanchored,
order,
sortBy,
});
const results = txResults.map(tx => parseDbTx(tx));
const response: TransactionResults = { limit, offset, total, results };
Expand Down
19 changes: 4 additions & 15 deletions src/datastore/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,29 +273,18 @@ export function prefixedCols(columns: string[], prefix: string): string[] {
return columns.map(c => `${prefix}.${c}`);
}

/**
* Concatenates column names to use on a query. Necessary when one or more of those columns is complex enough
* so that postgres.js can't figure out how to list it (e.g. abi column, aggregates, partitions, etc.).
* @param sql - SQL client
* @param columns - list of columns
* @returns raw SQL column list string
*/
export function unsafeCols(sql: PgSqlClient, columns: string[]): postgres.PendingQuery<any> {
return sql.unsafe(columns.join(', '));
}

/**
* Shorthand function that returns a column query to retrieve the smart contract abi when querying transactions
* that may be of type `contract_call`. Usually used alongside `TX_COLUMNS` or `MEMPOOL_TX_COLUMNS`.
* @param tableName - Name of the table that will determine the transaction type. Defaults to `txs`.
* @returns `string` - abi column select statement portion
*/
export function abiColumn(tableName: string = 'txs'): string {
return `
CASE WHEN ${tableName}.type_id = ${DbTxTypeId.ContractCall} THEN (
export function abiColumn(sql: PgSqlClient, tableName: string = 'txs'): postgres.Fragment {
return sql`
CASE WHEN ${sql(tableName)}.type_id = ${DbTxTypeId.ContractCall} THEN (
SELECT abi
FROM smart_contracts
WHERE smart_contracts.contract_id = ${tableName}.contract_call_contract_id
WHERE smart_contracts.contract_id = ${sql(tableName)}.contract_call_contract_id
ORDER BY abi != 'null' DESC, canonical DESC, microblock_canonical DESC, block_height DESC
LIMIT 1
) END as abi
Expand Down
Loading

0 comments on commit 17e4b07

Please sign in to comment.