From 87636ebfa2c25c937ad42be445e53f36002c4773 Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Tue, 28 Feb 2017 17:19:40 +0100
Subject: [PATCH] =?UTF-8?q?[fix]=C2=A0#848=20Stock=20is=20wrongly=20comput?=
 =?UTF-8?q?ed?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 app/lib/dal/sqliteDAL/index/CIndexDAL.js | 2 ++
 app/lib/dup/indexer.js                   | 2 +-
 doc/Protocol.md                          | 2 +-
 3 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/app/lib/dal/sqliteDAL/index/CIndexDAL.js b/app/lib/dal/sqliteDAL/index/CIndexDAL.js
index 6f54b4edc..cd4e99a37 100644
--- a/app/lib/dal/sqliteDAL/index/CIndexDAL.js
+++ b/app/lib/dal/sqliteDAL/index/CIndexDAL.js
@@ -89,6 +89,7 @@ function CIndexDAL(driver) {
 
   this.getValidLinksTo = (receiver) => that.query('SELECT * FROM ' + that.table + ' c1 ' +
     'WHERE c1.receiver = ? ' +
+    'AND c1.expired_on = 0 ' +
     'AND NOT EXISTS (' +
     ' SELECT * FROM c_index c2' +
     ' WHERE c1.issuer = c2.issuer' +
@@ -99,6 +100,7 @@ function CIndexDAL(driver) {
 
   this.getValidLinksFrom = (issuer) => that.query('SELECT * FROM ' + that.table + ' c1 ' +
     'WHERE c1.issuer = ? ' +
+    'AND c1.expired_on = 0 ' +
     'AND NOT EXISTS (' +
     ' SELECT * FROM c_index c2' +
     ' WHERE c1.issuer = c2.issuer' +
diff --git a/app/lib/dup/indexer.js b/app/lib/dup/indexer.js
index 37b5b429e..d5719d4f1 100644
--- a/app/lib/dup/indexer.js
+++ b/app/lib/dup/indexer.js
@@ -651,7 +651,7 @@ const indexer = module.exports = {
 
     // BR_G39
     yield cindex.map((ENTRY) => co(function*() {
-      ENTRY.stock = count(yield dal.cindexDAL.sqlFind({ issuer: ENTRY.issuer, expired_on: 0 }));
+      ENTRY.stock = count(yield dal.cindexDAL.getValidLinksFrom(ENTRY.issuer));
     }));
 
     // BR_G40
diff --git a/doc/Protocol.md b/doc/Protocol.md
index c4feaf30f..eb3153f1e 100644
--- a/doc/Protocol.md
+++ b/doc/Protocol.md
@@ -1972,7 +1972,7 @@ If `HEAD.number > 0`:
     
 ####### BR_G39 - ENTRY.stock
 
-    ENTRY.stock = COUNT(GLOBAL_CINDEX[issuer=ENTRY.issuer, expired_on=0]))
+    ENTRY.stock = COUNT(REDUCE_BY(GLOBAL_CINDEX[issuer=ENTRY.issuer], 'receiver', 'created_on')[expired_on=0])
     
 ####### BR_G40 - ENTRY.fromMember
 
-- 
GitLab