Skip to content
Snippets Groups Projects
Commit 9ceb4763 authored by Benoit Lavenier's avatar Benoit Lavenier
Browse files

[fix] Currency: Add a retry;, when loading last UD and when the block is not found.

parent fe88da95
No related branches found
No related tags found
No related merge requests found
...@@ -98,7 +98,7 @@ angular.module('cesium.currency.services', ['ngApi', 'cesium.bma.services']) ...@@ -98,7 +98,7 @@ angular.module('cesium.currency.services', ['ngApi', 'cesium.bma.services'])
}); });
} }
function loadCurrentUD() { function loadCurrentUD(res, retryCount) {
return BMA.blockchain.stats.ud() return BMA.blockchain.stats.ud()
.then(function(res) { .then(function(res) {
// Special case for currency init // Special case for currency init
...@@ -106,8 +106,29 @@ angular.module('cesium.currency.services', ['ngApi', 'cesium.bma.services']) ...@@ -106,8 +106,29 @@ angular.module('cesium.currency.services', ['ngApi', 'cesium.bma.services'])
data.currentUD = data.parameters ? data.parameters.ud0 : -1; data.currentUD = data.parameters ? data.parameters.ud0 : -1;
return data.currentUD ; return data.currentUD ;
} }
return _safeLoadCurrentUD(res, res.result.blocks.length - 1);
})
.catch(function(err) {
data.currentUD = null;
throw err;
})
}
/**
* Load the last UD, with a workaround if last block with UD is not found in the node
* @param res
* @param blockIndex
* @returns {*}
* @private
*/
function _safeLoadCurrentUD(res, blockIndex) {
// Special case for currency init
if (!res.result.blocks.length || blockIndex < 0) {
data.currentUD = data.parameters ? data.parameters.ud0 : -1;
return data.currentUD ;
}
else { else {
var lastBlockWithUD = res.result.blocks[res.result.blocks.length - 1]; var lastBlockWithUD = res.result.blocks[blockIndex];
return BMA.blockchain.block({ block: lastBlockWithUD }) return BMA.blockchain.block({ block: lastBlockWithUD })
.then(function(block){ .then(function(block){
data.currentUD = powBase(block.dividend, block.unitbase); data.currentUD = powBase(block.dividend, block.unitbase);
...@@ -115,16 +136,15 @@ angular.module('cesium.currency.services', ['ngApi', 'cesium.bma.services']) ...@@ -115,16 +136,15 @@ angular.module('cesium.currency.services', ['ngApi', 'cesium.bma.services'])
}) })
.catch(function(err) { .catch(function(err) {
console.error("[currency] Unable to load last block with UD, with number {0}".format(lastBlockWithUD)); console.error("[currency] Unable to load last block with UD, with number {0}".format(lastBlockWithUD));
data.currentUD = null; if (blockIndex > 0) {
throw err; console.error("[currency] Retrying to load UD from a previous block...");
}); return _safeLoadCurrentUD(res, blockIndex-1);
} }
})
.catch(function(err) {
data.currentUD = null; data.currentUD = null;
throw err; throw err;
}); });
} }
}
function getData() { function getData() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment