From 80b1380dd74ed7dd2397c5c83b9239b1d19bf965 Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Sat, 12 Aug 2017 13:23:07 +0200
Subject: [PATCH] [fix] #1073 /tx/history returns wrong JSON

---
 app/lib/constants.ts             |  3 +++
 app/lib/dal/sqliteDAL/MetaDAL.ts | 31 +++++++++++++++++++++++++++++++
 app/lib/dto/TransactionDTO.ts    |  6 +++++-
 test/dal/dal.js                  |  2 +-
 4 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/app/lib/constants.ts b/app/lib/constants.ts
index b9354483b..15a743224 100644
--- a/app/lib/constants.ts
+++ b/app/lib/constants.ts
@@ -94,6 +94,9 @@ module.exports = {
   FIRST_UNIT_BASE: 0,
 
   PEER: CommonConstants.PEER,
+
+  CURRENT_DB_VERSION: 26,
+
   NETWORK: {
     MAX_MEMBERS_TO_FORWARD_TO_FOR_SELF_DOCUMENTS: 10,
     MAX_NON_MEMBERS_TO_FORWARD_TO_FOR_SELF_DOCUMENTS: 6,
diff --git a/app/lib/dal/sqliteDAL/MetaDAL.ts b/app/lib/dal/sqliteDAL/MetaDAL.ts
index eceba0a29..158d39f4d 100644
--- a/app/lib/dal/sqliteDAL/MetaDAL.ts
+++ b/app/lib/dal/sqliteDAL/MetaDAL.ts
@@ -13,6 +13,7 @@ import {DBBlock} from "../../db/DBBlock"
 import {IdentityDTO} from "../../dto/IdentityDTO"
 import {rawer} from "../../common-libs/index"
 import {CommonConstants} from "../../common-libs/constants"
+import {TxsDAL} from "./TxsDAL"
 
 const _ = require('underscore')
 const logger = require('../../logger').NewLogger('metaDAL');
@@ -364,6 +365,36 @@ export class MetaDAL extends AbstractSQLite<DBMeta> {
         await mindexDAL.exec(updateQuery)
       }
     },
+
+    /**
+     * Wrong transaction storage
+     */
+    25: async () => {
+      const txsDAL = new TxsDAL(this.driverCopy)
+      const wrongTXS = await txsDAL.query('SELECT * FROM txs WHERE outputs LIKE ? OR inputs LIKE ?', ['%amount%', '%amount%'])
+      let i = 1
+      for (const tx of wrongTXS) {
+        logger.info('Updating incorrect transaction %s/%s.', i, wrongTXS.length)
+        i++
+        const dto = TransactionDTO.fromJSONObject(tx)
+        dto.outputs = dto.outputs.map(o => {
+          if (typeof o === 'object') {
+            return TransactionDTO.outputObj2Str(o)
+          }
+          return o
+        })
+        dto.inputs = dto.inputs.map(o => {
+          if (typeof o === 'object') {
+            return TransactionDTO.inputObj2Str(o)
+          }
+          return o
+        })
+        await txsDAL.exec('UPDATE txs SET ' +
+          'outputs = \'' + JSON.stringify(dto.outputs) + '\', ' +
+          'inputs = \'' + JSON.stringify(dto.inputs) + '\' ' +
+          'WHERE hash = \'' + tx.hash + '\'')
+      }
+    },
   };
 
   async init() {
diff --git a/app/lib/dto/TransactionDTO.ts b/app/lib/dto/TransactionDTO.ts
index 97c43cde7..15e65a4a2 100644
--- a/app/lib/dto/TransactionDTO.ts
+++ b/app/lib/dto/TransactionDTO.ts
@@ -1,5 +1,5 @@
 import {hashf} from "../common"
-import {Cloneable} from "./Cloneable";
+import {Cloneable} from "./Cloneable"
 
 export interface BaseDTO {
   base: number
@@ -250,6 +250,10 @@ export class TransactionDTO implements Cloneable {
     return [o.amount, o.base, o.conditions].join(':')
   }
 
+  static inputObj2Str(i:InputDTO) {
+    return [i.amount, i.base, i.type, i.identifier, i.pos].join(':')
+  }
+
   static outputStr2Obj(outputStr:string) {
     const sp = outputStr.split(':');
     return {
diff --git a/test/dal/dal.js b/test/dal/dal.js
index 44b1a63d3..a4014a244 100644
--- a/test/dal/dal.js
+++ b/test/dal/dal.js
@@ -103,7 +103,7 @@ describe("DAL", function(){
   it('should have DB version 21', () => co(function *() {
     let version = yield fileDAL.getDBVersion();
     should.exist(version);
-    version.should.equal(25);
+    version.should.equal(constants.CURRENT_DB_VERSION);
   }));
 
   it('should have no peer in a first time', function(){
-- 
GitLab