Skip to content

Commit

Permalink
v0.6.0: Merge pull request #44 from Adamant-im/dev
Browse files Browse the repository at this point in the history
v0.6.0
- Socket connections return `recipientPublicKey`
- Transactions API, including KVS and Chats, return `block_timestamp`
- Fix for migrations
- Endpoint `/states/get/` support for parameters `SenderIds` and `keyIds`
- Endpoint `/states/get/` support for POST requests
- Documentation update
  • Loading branch information
adamant-al authored Feb 6, 2020
2 parents 5700318 + 93b0375 commit fef0c69
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 11 deletions.
1 change: 1 addition & 0 deletions api/http/states.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function StatesHttpApi (statesModule, app) {

router.map(statesModule.internal, {
'get /get': 'getTransactions',
'post /get': 'getTransactions',
'post /normalize': 'normalize',
'post /store': 'store'
});
Expand Down
21 changes: 18 additions & 3 deletions logic/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -897,9 +897,21 @@ Transaction.prototype.applyUnconfirmed = function (trs, sender, requester, cb) {
amount = amount.toNumber();

if (this.scope.clientWs) {
this.scope.clientWs.emit(trs);
var new_trs = Object.assign({}, trs);
new_trs.block_timestamp = null;
if (!new_trs.recipientPublicKey && new_trs.recipientId) {
this.scope.db.query(`SELECT ENCODE ("publicKey", \'hex\') AS "publicKey" from mem_accounts WHERE address='` + new_trs.recipientId + `' limit 1`).then((rows) => {
if (rows[0]) {
new_trs.recipientPublicKey = rows[0]['publicKey'];
}
this.scope.clientWs.emit(new_trs);
}).catch((err) => {
this.scope.logger.error(err.stack);
});
} else {
this.scope.clientWs.emit(new_trs);
}
}

this.scope.account.merge(sender.address, {
u_balance: -amount
}, function (err, sender) {
Expand Down Expand Up @@ -1195,6 +1207,7 @@ Transaction.prototype.dbRead = function (raw) {
height: raw.b_height,
blockId: raw.b_id || raw.t_blockId,
type: parseInt(raw.t_type),
block_timestamp: parseInt(raw.block_timestamp),
timestamp: parseInt(raw.t_timestamp),
senderPublicKey: raw.t_senderPublicKey,
requesterPublicKey: raw.t_requesterPublicKey,
Expand All @@ -1209,7 +1222,9 @@ Transaction.prototype.dbRead = function (raw) {
confirmations: parseInt(raw.confirmations),
asset: {}
};

if (!tx.block_timestamp && raw.b_timestamp) {
tx.block_timestamp = parseInt(raw.b_timestamp);
}
if (!__private.types[tx.type]) {
throw 'Unknown transaction type ' + tx.type;
}
Expand Down
11 changes: 9 additions & 2 deletions modules/states.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,14 @@ __private.list = function (filter, cb) {
params.key = filter.key;
}
where.push('"t_type" = '+ transactionTypes.STATE);

if (filter.senderIds) {
where.push('"t_senderId" IN (${senderIds:csv})');
params.senderIds = filter.senderIds;
}
if (filter.keyIds) {
where.push('"st_stored_key" IN (${keyIds:csv})');
params.keyIds = filter.keyIds;
}
if (filter.senderId) {
where.push('"t_senderId" = ${name}');
params.name = filter.senderId;
Expand Down Expand Up @@ -227,7 +234,7 @@ States.prototype.internal = {
_.each(req.body, function (value, key) {
var param = String(key).replace(pattern, '');
// Dealing with array-like parameters (csv comma separated)
if (_.includes(['senderIds', 'senderPublicKeys'], param)) {
if (_.includes(['senderIds', 'senderPublicKeys', 'keyIds'], param)) {
value = String(value).split(',');
req.body[key] = value;
}
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "adamant",
"version": "0.5.2",
"version": "0.6.0",
"private": true,
"scripts": {
"start": "node app.js",
Expand Down Expand Up @@ -37,9 +37,9 @@
"json-schema": "=0.2.3",
"json-sql": "LiskHQ/json-sql#27e1ed1",
"lisk-sandbox": "LiskHQ/lisk-sandbox#162da38",
"lodash": "=4.17.11",
"lodash": "=4.17.15",
"method-override": "=2.3.10",
"npm": "=2.15.10",
"npm": "=6.13.4",
"pg-monitor": "=0.7.1",
"pg-native": "=1.10.0",
"pg-promise": "=5.5.6",
Expand Down
10 changes: 10 additions & 0 deletions schema/states.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ module.exports = {
format: 'publicKey'
}
},
keysIds: {
type: 'array',
minItems: 1,
'items': {
type: 'string',
format: 'address',
minLength: 1,
maxLength: 22
}
},
senderIds: {
type: 'array',
minItems: 1,
Expand Down
1 change: 1 addition & 0 deletions sql/chats.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ var ChatsSql = {
'first("t_recipientId" ORDER BY b_height DESC, t_timestamp DESC) as "t_recipientId",',
'first("t_timestamp" ORDER BY b_height DESC, t_timestamp DESC) as "t_timestamp",',
'first("t_timestamp" ORDER BY b_height DESC, t_timestamp DESC) as "timestamp",',
'first("b_timestamp" ORDER BY b_height DESC, b_timestamp DESC) as "block_timestamp",',
'first("t_amount" ORDER BY b_height DESC, t_timestamp DESC) as "t_amount",',
'first("t_fee" ORDER BY b_height DESC, t_timestamp DESC) as "t_fee",',
'first("c_message" ORDER BY b_height DESC, t_timestamp DESC) as "c_message",',
Expand Down
50 changes: 50 additions & 0 deletions sql/migrations/20190815120200_recreateTrsListFullView.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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",
b."timestamp" AS "b_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;
36 changes: 36 additions & 0 deletions sql/migrations/20190815120437_recreateTrsListView.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Recreate 'trs_list' view, normalize addresses, add indexes
*/

BEGIN;

DROP VIEW IF EXISTS trs_list;

CREATE VIEW trs_list 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",
b."timestamp" AS "b_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"

FROM trs t

LEFT JOIN blocks b ON t."blockId" = b."id"
LEFT JOIN mem_accounts m ON t."recipientId" = m."address";

CREATE INDEX IF NOT EXISTS "trs_upper_sender_id" ON "trs"(UPPER("senderId"));
CREATE INDEX IF NOT EXISTS "trs_upper_recipient_id" ON "trs"(UPPER("recipientId"));

COMMIT;
2 changes: 1 addition & 1 deletion sql/states.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var StatesSql = {
list: function (params) {
return [

'SELECT *, t_timestamp as timestamp FROM full_blocks_list',
'SELECT *, t_timestamp as timestamp, b_timestamp as block_timestamp FROM full_blocks_list',
(params.where.length ? 'WHERE ' + params.where.join(' AND ') : ''),
(params.sortField ? 'ORDER BY ' + [params.sortField, params.sortMethod].join(' ') : ''),
'LIMIT ${limit} OFFSET ${offset}'
Expand Down
4 changes: 2 additions & 2 deletions sql/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var TransactionsSql = {

list: function (params) {
return [
'SELECT "t_id", "b_height", "t_blockId", "t_type", "t_timestamp", "t_senderId", "t_recipientId",',
'SELECT "t_id", "b_height", "t_blockId", "t_type", "t_timestamp", "b_timestamp" as "block_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"',
'FROM trs_list',
Expand All @@ -45,7 +45,7 @@ var TransactionsSql = {
},
listFull: function (params) {
return [
'SELECT "t_id", "b_height", "t_blockId", "t_type", "t_timestamp", "t_senderId", "t_recipientId",',
'SELECT "t_id", "b_height", "t_blockId", "t_type", "t_timestamp", "b_timestamp" as "block_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" ',
Expand Down

0 comments on commit fef0c69

Please sign in to comment.