Skip to content

Commit

Permalink
Merge pull request #39 from Adamant-im/dev
Browse files Browse the repository at this point in the history
Release 0.5.2
  • Loading branch information
zyuhel authored Jun 10, 2019
2 parents c944ad8 + b07aaf9 commit 368e496
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 6 deletions.
5 changes: 4 additions & 1 deletion logic/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ var self, modules, __private = {};
* - 5: DApp
* - 6: InTransfer
* - 7: OutTransfer
* - 8: ChatMessage
* - 9: KVS
*
*/
__private.types = {};

Expand Down Expand Up @@ -1234,4 +1237,4 @@ Transaction.prototype.bindModules = function (__modules) {
};

// Export
module.exports = Transaction;
module.exports = Transaction;
36 changes: 32 additions & 4 deletions modules/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ __private.list = function (filter, cb) {
limit: null,
offset: null,
orderBy: null,
returnAsset: null,
// FIXME: Backward compatibility, should be removed after transitional period
ownerAddress: null,
ownerPublicKey: null
Expand Down Expand Up @@ -202,8 +203,11 @@ __private.list = function (filter, cb) {
owner: owner
}), params).then(function (rows) {
var count = rows.length ? rows[0].count : 0;

library.db.query(sql.list({
var sql_method='list';
if (filter.returnAsset) {
sql_method = 'listFull';
}
library.db.query(sql[sql_method]({
where: where,
owner: owner,
sortField: orderBy.sortField,
Expand Down Expand Up @@ -253,6 +257,27 @@ __private.getById = function (id, cb) {
});
};

/**
* Gets transaction by id from `trs_list_full` view.
* @private
* @param {string} id
* @param {function} cb - Callback function.
* @returns {setImmediateCallback} error | data: {transaction}
*/
__private.getByIdFullAsset = function (id, cb) {
library.db.query(sql.getByIdFull, {id: id}).then(function (rows) {
if (!rows.length) {
return setImmediate(cb, 'Transaction not found: ' + id);
}

var transacton = library.logic.transaction.dbRead(rows[0]);

return setImmediate(cb, null, transacton);
}).catch(function (err) {
library.logger.error(err.stack);
return setImmediate(cb, 'Transactions#getById error');
});
};
/**
* Gets votes by transaction id from `votes` table.
* @private
Expand Down Expand Up @@ -666,8 +691,11 @@ Transactions.prototype.shared = {
if (err) {
return setImmediate(cb, err[0].message);
}

__private.getById(req.body.id, function (err, transaction) {
var method='getById';
if (req.body.returnAsset) {
method = 'getByIdFullAsset';
}
__private[method](req.body.id, function (err, transaction) {
if (!transaction || err) {
return setImmediate(cb, 'Transaction not found');
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "adamant",
"version": "0.5.1",
"version": "0.5.2",
"private": true,
"scripts": {
"start": "node app.js",
Expand Down
10 changes: 10 additions & 0 deletions schema/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ module.exports = {
offset: {
type: 'integer',
minimum: 0
},
returnAsset: {
type: 'integer',
minimum: 0,
maximum: 1
}
}
},
Expand All @@ -177,6 +182,11 @@ module.exports = {
format: 'id',
minLength: 1,
maxLength: 20
},
returnAsset: {
type: 'integer',
minimum: 0,
maximum: 1
}
},
required: ['id']
Expand Down
49 changes: 49 additions & 0 deletions sql/migrations/20190601142900_createTrsListFullView.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* create 'trs_list_full' view, normalize addresses, add indexes
*/

BEGIN;

DROP VIEW IF EXISTS trs_list_full;

CREATE VIEW trs_list_full AS

SELECT t."id" AS "t_id",
b."height" AS "b_height",
t."blockId" AS "t_blockId",
t."type" AS "t_type",
t."timestamp" AS "t_timestamp",
t."senderPublicKey" AS "t_senderPublicKey",
m."publicKey" AS "m_recipientPublicKey",
UPPER(t."senderId") AS "t_senderId",
UPPER(t."recipientId") AS "t_recipientId",
t."amount" AS "t_amount",
t."fee" AS "t_fee",
ENCODE(t."signature", 'hex') AS "t_signature",
ENCODE(t."signSignature", 'hex') AS "t_SignSignature",
t."signatures" AS "t_signatures",
(SELECT height + 1 FROM blocks ORDER BY height DESC LIMIT 1) - b."height" AS "confirmations",
d."username" AS "d_username",
v."votes" AS "v_votes",
ms."min" AS "m_min",
ms."lifetime" AS "m_lifetime",
ms."keysgroup" AS "m_keysgroup",
c."message" AS "c_message",
c."own_message" AS "c_own_message",
c."type" AS "c_type",
st."type" as "st_type",
st."stored_value" as "st_stored_value",
st."stored_key" as "st_stored_key"
FROM trs t

LEFT JOIN blocks b ON t."blockId" = b."id"
LEFT JOIN mem_accounts m ON t."recipientId" = m."address"
LEFT OUTER JOIN delegates AS d ON d."transactionId" = t."id"
LEFT OUTER JOIN votes AS v ON v."transactionId" = t."id"
LEFT OUTER JOIN signatures AS s ON s."transactionId" = t."id"
LEFT OUTER JOIN multisignatures AS ms ON ms."transactionId" = t."id"
LEFT OUTER JOIN chats AS c ON c."transactionId" = t."id"
LEFT OUTER JOIN states AS st ON st."transactionId" = t."id";


COMMIT;
16 changes: 16 additions & 0 deletions sql/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,24 @@ var TransactionsSql = {
'LIMIT ${limit} OFFSET ${offset}'
].filter(Boolean).join(' ');
},
listFull: function (params) {
return [
'SELECT "t_id", "b_height", "t_blockId", "t_type", "t_timestamp", "t_senderId", "t_recipientId",',
'"t_amount", "t_fee", "t_signature", "t_SignSignature", "t_signatures", "confirmations",',
'ENCODE ("t_senderPublicKey", \'hex\') AS "t_senderPublicKey", ENCODE ("m_recipientPublicKey", \'hex\') AS "m_recipientPublicKey",',
'"d_username", "v_votes", "m_min", "m_lifetime", "m_keysgroup", "c_message", "c_own_message", "c_type", "st_type", "st_stored_value", "st_stored_key" ',
'FROM trs_list_full',
(params.where.length || params.owner ? 'WHERE' : ''),
(params.where.length ? '(' + params.where.join(' ') + ')' : ''),
// FIXME: Backward compatibility, should be removed after transitional period
(params.where.length && params.owner ? ' AND ' + params.owner : params.owner),
(params.sortField ? 'ORDER BY ' + [params.sortField, params.sortMethod].join(' ') : ''),
'LIMIT ${limit} OFFSET ${offset}'
].filter(Boolean).join(' ');
},

getById: 'SELECT *, ENCODE ("t_senderPublicKey", \'hex\') AS "t_senderPublicKey", ENCODE ("m_recipientPublicKey", \'hex\') AS "m_recipientPublicKey" FROM trs_list WHERE "t_id" = ${id}',
getByIdFull: 'SELECT *, ENCODE ("t_senderPublicKey", \'hex\') AS "t_senderPublicKey", ENCODE ("m_recipientPublicKey", \'hex\') AS "m_recipientPublicKey" FROM trs_list_full WHERE "t_id" = ${id}',

getVotesById: 'SELECT * FROM votes WHERE "transactionId" = ${id}'
};
Expand Down

0 comments on commit 368e496

Please sign in to comment.