From e3756436b92679ebeffe1bf8c0b143d6d7a26be0 Mon Sep 17 00:00:00 2001
From: blavenie <benoit.lavenier@e-is.pro>
Date: Sat, 15 Feb 2020 11:46:33 +0100
Subject: [PATCH] [fix] Remove cache by default, on /blockchain/current - see
 issue #870

---
 www/js/controllers/blockchain-controllers.js | 14 +++++++-------
 www/js/controllers/currency-controllers.js   |  4 ++--
 www/js/services/bma-services.js              | 19 +++++++++++++------
 www/js/services/currency-services.js         |  2 +-
 www/js/services/network-services.js          |  2 +-
 www/js/services/wallet-services.js           |  4 ++--
 www/js/services/wot-services.js              |  4 +++-
 7 files changed, 29 insertions(+), 20 deletions(-)

diff --git a/www/js/controllers/blockchain-controllers.js b/www/js/controllers/blockchain-controllers.js
index efd8e141..65f3073f 100644
--- a/www/js/controllers/blockchain-controllers.js
+++ b/www/js/controllers/blockchain-controllers.js
@@ -227,7 +227,7 @@ function BlockLookupController($scope, $timeout, $focus, $filter, $state, $ancho
 
     // get blocks
     if (from === 0) {
-      promise = $scope.node.blockchain.current()
+      promise = $scope.node.blockchain.current(false)
         .then(function(current) {
           var size = current.number < $scope.defaultSizeLimit ? current.number : $scope.defaultSizeLimit;
           return $scope.node.blockchain.blocksSlice({count: size, from: current.number-size})
@@ -391,14 +391,14 @@ function BlockLookupController($scope, $timeout, $focus, $filter, $state, $ancho
 
   $scope.onBlock = function(block) {
     // Skip if still loading or if filter/sort is not the default (not last blocks)
-    if ($scope.search.loading || $scope.search.type != 'last' ||
-      ($scope.search.sort && $scope.search.sort != 'desc')) return; // skip
+    if ($scope.search.loading || $scope.search.type !== 'last' ||
+      ($scope.search.sort && $scope.search.sort !== 'desc')) return; // skip
 
     // Make sure results is init
     $scope.search.results = $scope.search.results || [];
 
     if (!$scope.search.results.length) {
-      console.debug('[ES] [blockchain] new block #{0} received (by websocket)'.format(block.number));
+      console.debug('[blockchain] new block #{0} received (by websocket)'.format(block.number));
       // add it to result
       $scope.search.total++;
       $scope.search.results.push(block);
@@ -415,8 +415,8 @@ function BlockLookupController($scope, $timeout, $focus, $filter, $state, $ancho
 
       // replace existing block (fork could have replaced previous block)
       if (existingBlock) {
-        if (existingBlock.hash != block.hash) {
-          console.debug('[ES] [blockchain] block #{0} updated (by websocket)'.format(block.number));
+        if (existingBlock.hash !== block.hash) {
+          console.debug('[blockchain] block #{0} updated (by websocket)'.format(block.number));
           // Replace existing content
           angular.copy(block, existingBlock);
           // Prepare the new block, then show it
@@ -427,7 +427,7 @@ function BlockLookupController($scope, $timeout, $focus, $filter, $state, $ancho
         }
       }
       else {
-        console.debug('[ES] [blockchain] new block #{0} received (by websocket)'.format(block.number));
+        console.debug('[blockchain] new block #{0} received (by websocket)'.format(block.number));
         // Insert at index 0
         $scope.search.total++;
         $scope.search.results.splice(0, 0, block);
diff --git a/www/js/controllers/currency-controllers.js b/www/js/controllers/currency-controllers.js
index 985276be..4ed3ee90 100644
--- a/www/js/controllers/currency-controllers.js
+++ b/www/js/controllers/currency-controllers.js
@@ -173,8 +173,8 @@ function CurrencyViewController($scope, $q, $timeout, $ionicPopover, Modals, BMA
           }
         }),
 
-      // Get the current block informations
-      BMA.blockchain.current()
+      // Get the current block information
+      csCurrency.blockchain.current()
         .then(function(block){
           M = block.monetaryMass;
           data.N = block.membersCount;
diff --git a/www/js/services/bma-services.js b/www/js/services/bma-services.js
index 0fd2b392..5ce668f0 100644
--- a/www/js/services/bma-services.js
+++ b/www/js/services/bma-services.js
@@ -277,7 +277,7 @@ angular.module('cesium.bma.services', ['ngApi', 'cesium.http.services', 'cesium.
     function onSettingsChanged(settings) {
 
       var server = csHttp.getUrl(settings.node.host, settings.node.port, ''/*path*/, settings.node.useSsl);
-      var hasChanged = (server != that.url);
+      var hasChanged = (server !== that.url);
       if (hasChanged) {
         init(settings.node.host, settings.node.port, settings.node.useSsl, that.useCache);
         that.restart();
@@ -422,9 +422,9 @@ angular.module('cesium.bma.services', ['ngApi', 'cesium.http.services', 'cesium.
           all: get('/wot/members', csHttp.cache.LONG),
           pending: get('/wot/pending', csHttp.cache.SHORT)
         },
-        requirements: function(params, withCache) {
+        requirements: function(params, cache) {
           // No cache by default
-          if (withCache !== true) return exports.raw.wot.requirements(params);
+          if (cache !== true) return exports.raw.wot.requirements(params);
           return exports.raw.wot.requirementsWithCache(params);
         },
         add: post('/wot/add'),
@@ -435,7 +435,10 @@ angular.module('cesium.bma.services', ['ngApi', 'cesium.http.services', 'cesium.
         parameters: get('/blockchain/parameters', csHttp.cache.VERY_LONG),
         block: get('/blockchain/block/:block', csHttp.cache.SHORT),
         blocksSlice: get('/blockchain/blocks/:count/:from'),
-        current: get('/blockchain/current', csHttp.cache.SHORT),
+        current: function(cache) {
+          // No cache by default
+          return (cache !== true) ? exports.raw.blockchain.current() : exports.raw.blockchain.currentWithCache();
+        },
         membership: post('/blockchain/membership'),
         stats: {
           ud: get('/blockchain/with/ud', csHttp.cache.MEDIUM),
@@ -459,9 +462,9 @@ angular.module('cesium.bma.services', ['ngApi', 'cesium.http.services', 'cesium.
                 return res;
               });
           },
-          times: function(params, withCache) {
+          times: function(params, cache) {
             // No cache by default
-            return ((withCache !== true) ? exports.raw.tx.history.times(params) : exports.raw.tx.history.timesWithCache(params))
+            return ((cache !== true) ? exports.raw.tx.history.times(params) : exports.raw.tx.history.timesWithCache(params))
               .then(function(res) {
                 res.history = res.history || {};
                 // Clean sending and pendings, because already returned by tx/history/:pubkey/pending
@@ -480,6 +483,10 @@ angular.module('cesium.bma.services', ['ngApi', 'cesium.http.services', 'cesium.
       uri: {},
       version: {},
       raw: {
+        blockchain: {
+          currentWithCache: get('/blockchain/current', csHttp.cache.SHORT),
+          current: get('/blockchain/current')
+        },
         wot: {
           requirementsWithCache: get('/wot/requirements/:pubkey', csHttp.cache.LONG),
           requirements: get('/wot/requirements/:pubkey')
diff --git a/www/js/services/currency-services.js b/www/js/services/currency-services.js
index 4caaacf8..828754ca 100644
--- a/www/js/services/currency-services.js
+++ b/www/js/services/currency-services.js
@@ -279,7 +279,7 @@ angular.module('cesium.currency.services', ['ngApi', 'cesium.bma.services'])
             }
           }
 
-          return BMA.blockchain.current()
+          return BMA.blockchain.current(false)
             .catch(function(err){
               // Special case for currency init (root block not exists): use fixed values
               if (err && err.ucode == BMA.errorCodes.NO_CURRENT_BLOCK) {
diff --git a/www/js/services/network-services.js b/www/js/services/network-services.js
index a605a90a..037345cc 100644
--- a/www/js/services/network-services.js
+++ b/www/js/services/network-services.js
@@ -498,7 +498,7 @@ angular.module('cesium.network.services', ['ngApi', 'cesium.currency.services',
         peer.api = peer.api ||  BMA.lightInstance(peer.getHost(), peer.getPort(), peer.isSsl(), data.timeout);
 
         // Get current block
-        return peer.api.blockchain.current()
+        return peer.api.blockchain.current(false/*no cache*/)
           .then(function(block) {
             peer.currentNumber = block.number;
             peer.online = true;
diff --git a/www/js/services/wallet-services.js b/www/js/services/wallet-services.js
index 174ec9f6..bde2b38c 100644
--- a/www/js/services/wallet-services.js
+++ b/www/js/services/wallet-services.js
@@ -1118,7 +1118,7 @@ angular.module('cesium.wallet.services', ['ngApi', 'ngFileSaver', 'cesium.bma.se
       return $q.all([
           getKeypair(),
           csCurrency.get(),
-          block && $q.when(block) || csCurrency.blockchain.current()
+          block && $q.when(block) || csCurrency.blockchain.current(true)
         ])
         .then(function(res) {
           var keypair = res[0];
@@ -1608,7 +1608,7 @@ angular.module('cesium.wallet.services', ['ngApi', 'ngFileSaver', 'cesium.bma.se
       return $q.all([
           getKeypair(),
           csCurrency.get(),
-          csCurrency.blockchain.current()
+          csCurrency.blockchain.current() // TODO: use lastValid() instead ?
         ])
         .then(function(res) {
           var keypair = res[0];
diff --git a/www/js/services/wot-services.js b/www/js/services/wot-services.js
index 1ddfd428..a0409508 100644
--- a/www/js/services/wot-services.js
+++ b/www/js/services/wot-services.js
@@ -703,13 +703,15 @@ angular.module('cesium.wot.services', ['ngApi', 'cesium.bma.services', 'cesium.c
         var medianTime;
 
         return $q.all([
+
             // Get parameters
             csCurrency.parameters()
               .then(function(res) {
                 parameters = res;
               }),
+
             // Get current time
-            csCurrency.blockchain.current()
+            csCurrency.blockchain.current(true)
               .then(function(current) {
                 medianTime = current.medianTime;
               })
-- 
GitLab