Skip to content

Commit

Permalink
Merge pull request #79 from Adamant-im/feat/delegates-get-by-address
Browse files Browse the repository at this point in the history
Feat: find a delegate by the given address
  • Loading branch information
bludnic authored Nov 11, 2024
2 parents a37d48a + 1d77cbe commit fb3f0d7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion modules/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ Accounts.prototype.shared = {
}

if (account.delegates) {
modules.delegates.getDelegates(req.body, function (err, res) {
modules.delegates.getDelegates(req.body, {}, function (err, res) {
var delegates = res.delegates.filter(function (delegate) {
return account.delegates.indexOf(delegate.publicKey) !== -1;
});
Expand Down
37 changes: 25 additions & 12 deletions modules/delegates.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,19 +417,22 @@ Delegates.prototype.generateDelegateList = function (height, cb) {
* Gets delegates and for each one calculates rate, rank, approval, productivity.
* Orders delegates as per criteria.
* @param {Object} query
* @param {Object} filter - account specific filters, e.g. `username`, `publicKey` or `address`
* @param {function} cb - Callback function.
* @return {setImmediateCallback} error| object with delegates ordered, offset, count, limit.
* @todo OrderBy does not affects data? What is the impact?.
*/
Delegates.prototype.getDelegates = function (query, cb) {
Delegates.prototype.getDelegates = function (query, filter, cb) {
if (!query) {
throw 'Missing query argument';
}

var sortFilter = { 'vote': -1, 'publicKey': 1 };
if (modules.blocks.lastBlock.get().height > constants.fairSystemActivateBlock) {
sortFilter = { 'votesWeight': -1, 'publicKey': 1 };
}
modules.accounts.getAccounts({
...filter,
isDelegate: 1,
sort: sortFilter
}, ['username', 'address', 'publicKey', 'votesWeight', 'vote', 'missedblocks', 'producedblocks'], function (err, delegates) {
Expand Down Expand Up @@ -759,20 +762,30 @@ Delegates.prototype.shared = {
return setImmediate(cb, err[0].message);
}

modules.delegates.getDelegates(req.body, function (err, data) {
const filter = {}

if (req.body.publicKey) {
filter.address = modules.accounts.generateAddressByPublicKey(
req.body.publicKey,
);
} else if (req.body.username) {
filter.username = req.body.username;
} else if (req.body.address) {
filter.address = req.body.address;
} else {
return setImmediate(cb, 'Delegate not found');
}

if (req.body.address && req.body.publicKey && filter.address !== req.body.address) {
return setImmediate(cb, 'Delegate publicKey does not match address');
}

modules.delegates.getDelegates(req.body, filter, function (err, data) {
if (err) {
return setImmediate(cb, err);
}

var delegate = _.find(data.delegates, function (delegate) {
if (req.body.publicKey) {
return delegate.publicKey === req.body.publicKey;
} else if (req.body.username) {
return delegate.username === req.body.username;
}

return false;
});
var delegate = data.delegates[0];

if (delegate) {
return setImmediate(cb, null, { delegate: delegate });
Expand Down Expand Up @@ -878,7 +891,7 @@ Delegates.prototype.shared = {
return setImmediate(cb, err[0].message);
}

modules.delegates.getDelegates(req.body, function (err, data) {
modules.delegates.getDelegates(req.body, {}, function (err, data) {
if (err) {
return setImmediate(cb, err);
}
Expand Down
7 changes: 6 additions & 1 deletion schema/delegates.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ module.exports = {
type: 'object',
properties: {
publicKey: {
type: 'string'
type: 'string',
format: 'publicKey'
},
address: {
type: 'string',
format: 'address',
},
username: {
type: 'string',
Expand Down

0 comments on commit fb3f0d7

Please sign in to comment.