Skip to content

Commit

Permalink
Fix accounts REST API error with some query params (#8972)
Browse files Browse the repository at this point in the history
- Fix postgres param index

Signed-off-by: Xin Li <[email protected]>
  • Loading branch information
xin-hedera authored Aug 7, 2024
1 parent f72fe73 commit 3c49fc6
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 77 deletions.
176 changes: 106 additions & 70 deletions hedera-mirror-rest/__tests__/specs/accounts/balance-false.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,76 +48,112 @@
"transactions": [],
"cryptotransfers": []
},
"urls": [
"/api/v1/accounts?balance=false",
"/api/v1/accounts?balance=true&balance=false"
],
"responseStatus": 200,
"responseJson": {
"accounts": [
{
"account": "0.0.7",
"alias": null,
"balance": null,
"created_timestamp": null,
"decline_reward": false,
"deleted": false,
"ethereum_nonce": null,
"evm_address": "0x0000000000000000000000000000000000000007",
"expiry_timestamp": null,
"auto_renew_period": null,
"key": null,
"max_automatic_token_associations": 0,
"memo": "entity memo",
"pending_reward": 0,
"receiver_sig_required": false,
"staked_account_id": null,
"staked_node_id": null,
"stake_period_start": null
},
{
"account": "0.0.8",
"alias": null,
"balance": null,
"created_timestamp": null,
"decline_reward": false,
"deleted": false,
"ethereum_nonce": null,
"evm_address": "0x0000000000000000000000000000000000000008",
"expiry_timestamp": null,
"auto_renew_period": null,
"key": null,
"max_automatic_token_associations": 0,
"memo": "entity memo",
"pending_reward": 0,
"receiver_sig_required": false,
"staked_account_id": null,
"staked_node_id": null,
"stake_period_start": null
},
{
"account": "0.0.9",
"alias": null,
"balance": null,
"created_timestamp": null,
"decline_reward": false,
"deleted": false,
"ethereum_nonce": null,
"evm_address": "0x0000000000000000000000000000000000000009",
"expiry_timestamp": null,
"auto_renew_period": null,
"key": null,
"max_automatic_token_associations": 0,
"memo": "entity memo",
"pending_reward": 0,
"receiver_sig_required": false,
"staked_account_id": null,
"staked_node_id": null,
"stake_period_start": null
"tests": [
{
"urls": [
"/api/v1/accounts?balance=false",
"/api/v1/accounts?balance=true&balance=false"
],
"responseStatus": 200,
"responseJson": {
"accounts": [
{
"account": "0.0.7",
"alias": null,
"balance": null,
"created_timestamp": null,
"decline_reward": false,
"deleted": false,
"ethereum_nonce": null,
"evm_address": "0x0000000000000000000000000000000000000007",
"expiry_timestamp": null,
"auto_renew_period": null,
"key": null,
"max_automatic_token_associations": 0,
"memo": "entity memo",
"pending_reward": 0,
"receiver_sig_required": false,
"staked_account_id": null,
"staked_node_id": null,
"stake_period_start": null
},
{
"account": "0.0.8",
"alias": null,
"balance": null,
"created_timestamp": null,
"decline_reward": false,
"deleted": false,
"ethereum_nonce": null,
"evm_address": "0x0000000000000000000000000000000000000008",
"expiry_timestamp": null,
"auto_renew_period": null,
"key": null,
"max_automatic_token_associations": 0,
"memo": "entity memo",
"pending_reward": 0,
"receiver_sig_required": false,
"staked_account_id": null,
"staked_node_id": null,
"stake_period_start": null
},
{
"account": "0.0.9",
"alias": null,
"balance": null,
"created_timestamp": null,
"decline_reward": false,
"deleted": false,
"ethereum_nonce": null,
"evm_address": "0x0000000000000000000000000000000000000009",
"expiry_timestamp": null,
"auto_renew_period": null,
"key": null,
"max_automatic_token_associations": 0,
"memo": "entity memo",
"pending_reward": 0,
"receiver_sig_required": false,
"staked_account_id": null,
"staked_node_id": null,
"stake_period_start": null
}
],
"links": {
"next": null
}
}
},
{
"description": "reproduction of issue https://github.com/hashgraph/hedera-mirror-node/issues/8971",
"url": "/api/v1/accounts?account.id=0.0.7&balance=false",
"responseStatus": 200,
"responseJson": {
"accounts": [
{
"account": "0.0.7",
"alias": null,
"balance": null,
"created_timestamp": null,
"decline_reward": false,
"deleted": false,
"ethereum_nonce": null,
"evm_address": "0x0000000000000000000000000000000000000007",
"expiry_timestamp": null,
"auto_renew_period": null,
"key": null,
"max_automatic_token_associations": 0,
"memo": "entity memo",
"pending_reward": 0,
"receiver_sig_required": false,
"staked_account_id": null,
"staked_node_id": null,
"stake_period_start": null
}
],
"links": {
"next": null
}
}
],
"links": {
"next": null
}
}
]
}
20 changes: 13 additions & 7 deletions hedera-mirror-rest/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,16 +269,16 @@ const getAccountQuery = (
includeBalance = true,
isHistorical = false
) => {
// Convert MySQL style value placeholder ? to PostgreSQL style $1, $2, .... The query fragments are ordered in the
// same order as their params
// Keep track of Postgres positional param index when converting MySQL style value placeholder ? to Postgres style
// $1, $2, .... The query fragments are ordered in the same order as their params
let paramIndex = 1;
[tokenBalanceQuery, entityBalanceQuery, entityAccountQuery, pubKeyQuery, accountBalanceQuery, limitAndOrderQuery]
.filter((query) => !!query.query)
.forEach((query) => {
query.query = query.query.replace(/\?/g, (s) => `$${paramIndex++}`);
});

if (!includeBalance) {
[entityAccountQuery, pubKeyQuery, limitAndOrderQuery]
.filter((query) => !!query.query)
.forEach((query) => {
query.query = query.query.replace(/\?/g, (s) => `$${paramIndex++}`);
});
const entityCondition = [`e.type in ('ACCOUNT', 'CONTRACT')`, entityAccountQuery.query, pubKeyQuery.query]
.filter((x) => !!x)
.join(' and ');
Expand All @@ -294,6 +294,12 @@ const getAccountQuery = (
};
}

[tokenBalanceQuery, entityBalanceQuery, entityAccountQuery, pubKeyQuery, accountBalanceQuery, limitAndOrderQuery]
.filter((query) => !!query.query)
.forEach((query) => {
query.query = query.query.replace(/\?/g, (s) => `$${paramIndex++}`);
});

return getEntityBalanceQuery(
entityBalanceQuery,
entityAccountQuery,
Expand Down

0 comments on commit 3c49fc6

Please sign in to comment.