Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Add "transaction applications" (unconfirmed/confirmed) to the same promise chain as Chain.prototype.saveBlock - Closes #1181 #1196

Merged
merged 16 commits into from
Jan 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion db/accounts.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
'use strict';

var PQ = require('pg-promise').ParameterizedQuery;
var columnSet;

function AccountsRepo (db, pgp) {
this.db = db;
this.pgp = pgp;

this.dbTable = 'mem_accounts';

this.dbFields = [
'username',
'isDelegate',
'secondSignature',
'address',
'publicKey',
'secondPublicKey',
'balance',
'rate',
'rank'
];

if (!columnSet) {
columnSet = {};
var table = new pgp.helpers.TableName({table: this.dbTable, schema: 'public'});
columnSet.insert = new pgp.helpers.ColumnSet(this.dbFields, table);
}

this.cs = columnSet;
}

var Queries = {
Expand All @@ -14,7 +37,9 @@ var Queries = {

getOrphanedMemAccounts: 'SELECT a."blockId", b."id" FROM mem_accounts a LEFT OUTER JOIN blocks b ON b."id" = a."blockId" WHERE a."blockId" IS NOT NULL AND a."blockId" != \'0\' AND b."id" IS NULL',

getDelegates: 'SELECT ENCODE("publicKey", \'hex\') FROM mem_accounts WHERE "isDelegate" = 1'
getDelegates: 'SELECT ENCODE("publicKey", \'hex\') FROM mem_accounts WHERE "isDelegate" = 1',

upsert: PQ('INSERT INTO mem_accounts $1 VALUES $2 ON CONFLICT($3) DO UPDATE SET $4')
};

AccountsRepo.prototype.countMemAccounts = function (task) {
Expand All @@ -33,4 +58,13 @@ AccountsRepo.prototype.getDelegates = function (task) {
return (task || this.db).query(Queries.getDelegates);
};

AccountsRepo.prototype.upsert = function (data, conflictingFields) {
return this.db.none(
this.pgp.helpers.concat([
this.pgp.helpers.insert(this.cs, data),
'ON CONFLICT ( ' + conflictingFields.join(',') + ') DO',
this.pgp.helpers.update(this.cs, data)
]));
};

module.exports = AccountsRepo;
46 changes: 46 additions & 0 deletions db/blocks.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
'use strict';

var PQ = require('pg-promise').ParameterizedQuery;
var columnSet;

function BlocksRepo (db, pgp) {
this.db = db;
this.pgp = pgp;

this.dbTable = 'blocks';

this.sortFields = [
'id',
'timestamp',
Expand All @@ -17,6 +20,30 @@ function BlocksRepo (db, pgp) {
'numberOfTransactions',
'generatorPublicKey'
];

this.dbFields = [
'id',
'version',
'timestamp',
'height',
'previousBlock',
'numberOfTransactions',
'totalAmount',
'totalFee',
'reward',
'payloadLength',
'payloadHash',
'generatorPublicKey',
'blockSignature'
];

if (!columnSet) {
columnSet = {};
var table = new pgp.helpers.TableName({table: this.dbTable, schema: 'public'});
columnSet.insert = new pgp.helpers.ColumnSet(this.dbFields, table);
}

this.cs = columnSet;
}

var Queries = {
Expand Down Expand Up @@ -167,4 +194,23 @@ BlocksRepo.prototype.getBlocksForTransport = function (ids) {
return this.db.query(Queries.getBlocksForTransport, [ids]);
};

/**
* Create a transaction to create a block.
*
* @param {Object} block - JSON object for block.
* @return {Promise}
*/
BlocksRepo.prototype.save = function (block) {
try {
block.payloadHash = Buffer.from(block.payloadHash, 'hex');
block.generatorPublicKey = Buffer.from(block.generatorPublicKey, 'hex');
block.blockSignature = Buffer.from(block.blockSignature, 'hex');
block.reward = block.reward || 0;
} catch (e) {
throw e;
}

return this.db.none(this.pgp.helpers.insert(block, this.cs.insert));
};

module.exports = BlocksRepo;
53 changes: 53 additions & 0 deletions db/transactions.dapp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

var _ = require('lodash');
var columnSet;

function DappsTransactionsRepo (db, pgp) {
this.db = db;
this.pgp = pgp;

this.dbTable = 'dapps';

this.dbFields = [
'type',
'name',
'description',
'tags',
'link',
'category',
'icon',
'transactionId'
];

if (!columnSet) {
columnSet = {};
var table = new pgp.helpers.TableName({table: this.dbTable, schema: 'public'});
columnSet.insert = new pgp.helpers.ColumnSet(this.dbFields, table);
}

this.cs = columnSet;
}

DappsTransactionsRepo.prototype.save = function (transactions) {
if (!_.isArray(transactions)) {
transactions = [transactions];
}

transactions = transactions.map(function (transaction) {
return {
type: transaction.asset.dapp.type,
name: transaction.asset.dapp.name,
description: transaction.asset.dapp.description || null,
tags: transaction.asset.dapp.tags || null,
link: transaction.asset.dapp.link || null,
icon: transaction.asset.dapp.icon || null,
category: transaction.asset.dapp.category,
transactionId: transaction.id
};
});

return this.db.none(this.pgp.helpers.insert(transactions, this.cs.insert));
};

module.exports = DappsTransactionsRepo;
42 changes: 42 additions & 0 deletions db/transactions.delegate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

var _ = require('lodash');
var transactionTypes = require('../helpers/transactionTypes')
var columnSet;

function DelegateTransactionsRepo (db, pgp) {
this.db = db;
this.pgp = pgp;

this.dbTable = 'delegates';

this.dbFields = [
'transactionId',
'username'
];

if (!columnSet) {
columnSet = {};
var table = new pgp.helpers.TableName({table: this.dbTable, schema: 'public'});
columnSet.insert = new pgp.helpers.ColumnSet(this.dbFields, table);
}

this.cs = columnSet;
}

DelegateTransactionsRepo.prototype.save = function (transactions) {
if (!_.isArray(transactions)) {
transactions = [transactions];
}

transactions = transactions.map(function (transaction) {
return {
transactionId: transaction.id,
username: transaction.asset.delegate.username
};
});

return this.db.none(this.pgp.helpers.insert(transactions, this.cs.insert));
};

module.exports = DelegateTransactionsRepo;
42 changes: 42 additions & 0 deletions db/transactions.inTransfer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

var _ = require('lodash');
var transactionTypes = require('../helpers/transactionTypes')
var columnSet;

function InTransferTransactionsRepo (db, pgp) {
this.db = db;
this.pgp = pgp;

this.dbTable = 'intransfer';

this.dbFields = [
'dappId',
'transactionId'
];

if (!columnSet) {
columnSet = {};
var table = new pgp.helpers.TableName({table: this.dbTable, schema: 'public'});
columnSet.insert = new pgp.helpers.ColumnSet(this.dbFields, table);
}

this.cs = columnSet;
}

InTransferTransactionsRepo.prototype.save = function (transactions) {
if (!_.isArray(transactions)) {
transactions = [transactions];
}

transactions = transactions.map(function (transaction) {
return {
dappId: transaction.asset.inTransfer.dappId,
transactionId: transaction.id
};
});

return this.db.none(this.pgp.helpers.insert(transactions, this.cs.insert));
};

module.exports = InTransferTransactionsRepo;
78 changes: 78 additions & 0 deletions db/transactions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'use strict';

var PQ = require('pg-promise').ParameterizedQuery;
var _ = require('lodash');
var transactionTypes = require('../helpers/transactionTypes');
var columnSet;

function TransactionsRepo (db, pgp) {
this.db = db;
Expand All @@ -19,6 +22,43 @@ function TransactionsRepo (db, pgp) {
'confirmations',
'height'
];

this.dbTable = 'trs';

this.dbFields = [
'id',
'blockId',
'type',
'timestamp',
'senderPublicKey',
'requesterPublicKey',
'senderId',
'recipientId',
'amount',
'fee',
'signature',
'signSignature',
'signatures'
];

if (!columnSet) {
columnSet = {};
var table = new pgp.helpers.TableName({table: this.dbTable, schema: 'public'});
columnSet.insert = new pgp.helpers.ColumnSet(this.dbFields, table);
columnSet.insert = columnSet.insert.merge([{ name: 'recipientId', def: null }]);
}

this.cs = columnSet;

this.transactionsRepoMap = {};
this.transactionsRepoMap[transactionTypes.SEND] = 'transactions.transfer';
this.transactionsRepoMap[transactionTypes.DAPP] = 'transactions.dapp';
this.transactionsRepoMap[transactionTypes.DELEGATE] = 'transactions.delegate';
this.transactionsRepoMap[transactionTypes.IN_TRANSFER] = 'transactions.inTransfer';
this.transactionsRepoMap[transactionTypes.OUT_TRANSFER] = 'transactions.outTransfer';
this.transactionsRepoMap[transactionTypes.MULTI] = 'transactions.multisignature';
this.transactionsRepoMap[transactionTypes.SIGNATURE] = 'transactions.signature';
this.transactionsRepoMap[transactionTypes.VOTE] = 'transactions.vote';
}

var Queries = {
Expand Down Expand Up @@ -120,4 +160,42 @@ TransactionsRepo.prototype.getOutTransferByIds = function (ids) {
return this.db.query(Queries.getOutTransferByIds, [ids]);
};

TransactionsRepo.prototype.save = function (transactions) {
var self = this;

if (!_.isArray(transactions)) {
transactions = [transactions];
}

transactions.forEach(function (transaction) {
try {
transaction.senderPublicKey = Buffer.from(transaction.senderPublicKey, 'hex');
transaction.signature = Buffer.from(transaction.signature, 'hex');
transaction.signSignature = transaction.signSignature ? Buffer.from(transaction.signSignature, 'hex') : null;
transaction.requesterPublicKey = transaction.requesterPublicKey ? Buffer.from(transaction.requesterPublicKey, 'hex') : null;
transaction.signatures = transaction.signatures ? transaction.signatures.join(',') : null;
} catch (e) {
throw e;
}
});

var batch = [];
batch.push(self.db.none(self.pgp.helpers.insert(transactions, self.cs.insert)));

var groupedTransactions = _.groupBy(transactions, 'type');

Object.keys(groupedTransactions).forEach(function (type) {
batch.push(self.db[self.transactionsRepoMap[type]].save(groupedTransactions[type]));
});

// To avoid nested transactions aka transactions checkpoints
if(this.db.batch) {
return this.db.batch(batch);
} else {
return this.db.tx(function (t) {
return t.batch(batch);
});
}
};

module.exports = TransactionsRepo;
Loading