Skip to content

Commit fb297d1

Browse files
authored
feat: get best block height from features API (#541)
* feat: get best block height from features API
1 parent 6f3d6e9 commit fb297d1

File tree

5 files changed

+24
-151
lines changed

5 files changed

+24
-151
lines changed

__tests__/__fixtures__/http-fixtures.js

+5
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,11 @@ export default {
11201120
success: true,
11211121
blocks: 1242,
11221122
},
1123+
'/feature': {
1124+
block_hash: '00000008707722cde59ac9e7f4d44efbd3a5bd5f244223816ee676d328943b1b',
1125+
block_height: 1242,
1126+
features: [],
1127+
},
11231128
'/nano_contract/state': {
11241129
success: true,
11251130
nc_id: '3cb032600bdf7db784800e4ea911b10676fa2f67591f82bb62628c234e771595',

__tests__/test-utils.js

+1
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ class TestUtils {
218218
return [200, httpFixtures['/transaction']];
219219
});
220220
httpMock.onGet('/getmininginfo').reply(200, httpFixtures['/getmininginfo']);
221+
httpMock.onGet('/feature').reply(200, httpFixtures['/feature']);
221222
httpMock.onGet('http://fake.txmining:8084/health').reply(200, httpFixtures['http://fake.txmining:8084/health']);
222223
httpMock.onGet('/nano_contract/state').reply(200, httpFixtures['/nano_contract/state']);
223224
httpMock.onGet('/nano_contract/history').reply(200, httpFixtures['/nano_contract/history']);

package-lock.json

+10-143
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"dependencies": {
1111
"@dinamonetworks/hsm-dinamo": "4.9.1",
1212
"@hathor/healthcheck-lib": "0.1.0",
13-
"@hathor/wallet-lib": "2.1.1",
13+
"@hathor/wallet-lib": "2.3.0",
1414
"axios": "1.7.7",
1515
"express": "4.18.2",
1616
"express-validator": "6.10.0",

src/controllers/wallet/wallet.controller.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
const { txApi, walletApi, WalletType, constants: hathorLibConstants, helpersUtils, errors, tokensUtils, transactionUtils, PartialTx } = require('@hathor/wallet-lib');
8+
const { txApi, featuresApi, WalletType, constants: hathorLibConstants, helpersUtils, errors, tokensUtils, transactionUtils, PartialTx } = require('@hathor/wallet-lib');
99
const { matchedData } = require('express-validator');
1010
// import is used because there is an issue with winston logger when using require ref: #262
1111
const { JSONBigInt } = require('@hathor/wallet-lib/lib/utils/bigint');
@@ -226,7 +226,6 @@ async function getTxConfirmationBlocks(req, res) {
226226
// Disabling this eslint rule because of the way API call is done in the lib
227227
// otherwise the code would need to be more complex
228228
// We should change this when we refactor the way we call APIs in the lib
229-
// (this comment also applies for the getMiningInfo call)
230229
// eslint-disable-next-line no-promise-executor-return
231230
const txDataResponse = await new Promise(resolve => txApi.getTransaction(id, resolve));
232231

@@ -236,10 +235,11 @@ async function getTxConfirmationBlocks(req, res) {
236235
}
237236

238237
// Now we get the current height of the network
239-
// eslint-disable-next-line no-promise-executor-return
240-
const networkHeightResponse = await new Promise(resolve => walletApi.getMiningInfo(resolve));
241-
242-
if (!networkHeightResponse.success) {
238+
let bestChainHeight;
239+
try {
240+
const featuresResponse = await featuresApi.getFeatures();
241+
bestChainHeight = featuresResponse.block_height;
242+
} catch (err) {
243243
res.send({ success: false, error: 'Failed to get network heigth from the full node.' });
244244
return;
245245
}
@@ -248,7 +248,7 @@ async function getTxConfirmationBlocks(req, res) {
248248

249249
// first_block_height will be null until a block confirms this transaction
250250
if (txDataResponse.meta && txDataResponse.meta.first_block_height) {
251-
confirmationNumber = networkHeightResponse.blocks - txDataResponse.meta.first_block_height;
251+
confirmationNumber = bestChainHeight - txDataResponse.meta.first_block_height;
252252
}
253253

254254
res.send({ success: true, confirmationNumber });

0 commit comments

Comments
 (0)