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

Commit

Permalink
Merge pull request #3065 from LiskHQ/2991-refactor_chain_actions_args
Browse files Browse the repository at this point in the history
Refactor chain actions with arguments - Closes #2991
  • Loading branch information
MaciejBaj authored Mar 13, 2019
2 parents cc0d7d9 + ab041b1 commit d2fed4e
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 45 deletions.
42 changes: 25 additions & 17 deletions framework/src/modules/chain/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,34 +188,42 @@ module.exports = class Chain {

get actions() {
return {
calculateSupply: action => this.blockReward.calcSupply(action.params[0]),
calculateSupply: action =>
this.blockReward.calcSupply(action.params.height),
calculateMilestone: action =>
this.blockReward.calcMilestone(action.params[0]),
calculateReward: action => this.blockReward.calcReward(action.params[0]),
generateDelegateList: action =>
this.blockReward.calcMilestone(action.params.height),
calculateReward: action =>
this.blockReward.calcReward(action.params.height),
generateDelegateList: async action =>
promisify(this.scope.modules.delegates.generateDelegateList)(
action.params[0],
action.params[1]
action.params.round,
action.params.source
),
getNetworkHeight: async action =>
promisify(this.scope.modules.peers.networkHeight)(action.params[0]),
promisify(this.scope.modules.peers.networkHeight)(
action.params.options
),
getAllTransactionsCount: async () =>
promisify(
this.scope.modules.transactions.shared.getTransactionsCount
)(),
updateForgingStatus: async action =>
promisify(this.scope.modules.delegates.updateForgingStatus)(
action.params[0],
action.params[1],
action.params[2]
action.params.publicKey,
action.params.password,
action.params.forging
),
getPeers: async action =>
promisify(this.scope.modules.peers.shared.getPeers)(action.params[0]),
promisify(this.scope.modules.peers.shared.getPeers)(
action.params.parameters
),
getPeersCountByFilter: async action =>
this.scope.modules.peers.shared.getPeersCountByFilter(action.params[0]),
this.scope.modules.peers.shared.getPeersCountByFilter(
action.params.parameters
),
postSignature: async action =>
promisify(this.scope.modules.signatures.shared.postSignature)(
action.params[0]
action.params.signature
),
getLastConsensus: async () => this.scope.modules.peers.getLastConsensus(),
loaderLoaded: async () => this.scope.modules.loader.loaded(),
Expand All @@ -225,17 +233,17 @@ module.exports = class Chain {
getTransactionsFromPool: async action =>
promisify(
this.scope.modules.transactions.shared.getTransactionsFromPool
)(action.params[0], action.params[1]),
)(action.params.type, action.params.filters),
getLastCommit: async () => this.scope.lastCommit,
getBuild: async () => this.scope.build,
postTransaction: async action =>
promisify(this.scope.modules.transactions.shared.postTransaction)(
action.params[0]
action.params.transaction
),
getDelegateBlocksRewards: async action =>
this.scope.components.storage.entities.Account.delegateBlocksRewards(
action.params[0],
action.params[1]
action.params.filters,
action.params.tx
),
getSlotsHelper: async () => this.slots,
};
Expand Down
4 changes: 3 additions & 1 deletion framework/src/modules/http_api/controllers/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ AccountsController.getAccounts = async function(context, next) {
accountFormatter.bind(
null,
lastBlock.height
? await channel.invoke('chain:calculateSupply', [lastBlock.height])
? await channel.invoke('chain:calculateSupply', {
height: lastBlock.height,
})
: 0
)
);
Expand Down
10 changes: 6 additions & 4 deletions framework/src/modules/http_api/controllers/delegates.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ async function _getDelegates(filters, options) {
);

const supply = lastBlock.height
? await channel.invoke('chain:calculateSupply', [lastBlock.height])
? await channel.invoke('chain:calculateSupply', {
height: lastBlock.height,
})
: 0;

return delegates.map(delegate => delegateFormatter(supply, delegate));
Expand Down Expand Up @@ -192,9 +194,9 @@ async function _getForgers(filters) {
const forgerKeys = [];
const round = slots.calcRound(lastBlock.height + 1);

const activeDelegates = await channel.invoke('chain:generateDelegateList', [
const activeDelegates = await channel.invoke('chain:generateDelegateList', {
round,
]);
});

for (
let i = filters.offset + 1;
Expand Down Expand Up @@ -328,7 +330,7 @@ async function _aggregateBlocksReward(filter) {
try {
delegateBlocksRewards = await channel.invoke(
'chain:getDelegateBlocksRewards',
[params]
{ filters: params }
);
} catch (err) {
logger.error(err.stack);
Expand Down
30 changes: 15 additions & 15 deletions framework/src/modules/http_api/controllers/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ NodeController.getConstants = async (context, next) => {
);
const { height } = lastBlock;

const milestone = await library.channel.invoke('chain:calculateMilestone', [
const milestone = await library.channel.invoke('chain:calculateMilestone', {
height,
]);
const reward = await library.channel.invoke('chain:calculateReward', [
});
const reward = await library.channel.invoke('chain:calculateReward', {
height,
]);
const supply = await library.channel.invoke('chain:calculateSupply', [
});
const supply = await library.channel.invoke('chain:calculateSupply', {
height,
]);
});

const build = await library.channel.invoke('chain:getBuild');
const commit = await library.channel.invoke('chain:getLastCommit');
Expand Down Expand Up @@ -115,11 +115,11 @@ NodeController.getStatus = async (context, next) => {
try {
const networkHeight = await library.channel.invoke(
'chain:getNetworkHeight',
[
{
{
options: {
normalized: false,
},
]
}
);

const [lastBlock] = await library.components.storage.entities.Block.get(
Expand Down Expand Up @@ -200,11 +200,11 @@ NodeController.updateForgingStatus = async (context, next) => {
const forging = context.request.swagger.params.data.value.forging;

try {
const data = await library.channel.invoke('chain:updateForgingStatus', [
const data = await library.channel.invoke('chain:updateForgingStatus', {
publicKey,
password,
forging,
]);
});
return next(null, [data]);
} catch (err) {
context.statusCode = apiCodes.NOT_FOUND;
Expand Down Expand Up @@ -246,10 +246,10 @@ NodeController.getPooledTransactions = async function(context, next) {
filters = _.pickBy(filters, v => !(v === undefined || v === null));

try {
const data = await library.channel.invoke('chain:getTransactionsFromPool', [
state,
_.clone(filters),
]);
const data = await library.channel.invoke('chain:getTransactionsFromPool', {
type: state,
filters: _.clone(filters),
});

const transactions = _.map(_.cloneDeep(data.transactions), transaction => {
transaction.senderId = transaction.senderId || '';
Expand Down
10 changes: 6 additions & 4 deletions framework/src/modules/http_api/controllers/peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,19 @@ PeersController.getPeers = async function(context, next) {
filters = _.pickBy(filters, v => !(v === undefined || v === null));

try {
const data = await channel.invoke('chain:getPeers', [filters]);
const data = await channel.invoke('chain:getPeers', {
parameters: filters,
});

const clonedData = _.cloneDeep(data);
const filteredData = clonedData.map(peer => {
const { updated, ...filtered } = peer;
return filtered;
});

const peersCount = await channel.invoke('chain:getPeersCountByFilter', [
_.cloneDeep(filters),
]);
const peersCount = await channel.invoke('chain:getPeersCountByFilter', {
parameters: _.cloneDeep(filters),
});

return next(null, {
data: filteredData,
Expand Down
2 changes: 1 addition & 1 deletion framework/src/modules/http_api/controllers/signatures.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ SignaturesController.postSignature = async function(context, next) {
let error;

try {
const data = await channel.invoke('chain:postSignature', [signature]);
const data = await channel.invoke('chain:postSignature', { signature });

if (data.success) {
return next(null, {
Expand Down
2 changes: 1 addition & 1 deletion framework/src/modules/http_api/controllers/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ TransactionsController.postTransaction = async function(context, next) {
let error;

try {
const data = await channel.invoke('chain:postTransaction', [transaction]);
const data = await channel.invoke('chain:postTransaction', { transaction });

if (data.success) {
return next(null, {
Expand Down
2 changes: 1 addition & 1 deletion framework/test/mocha/functional/http/get/delegates.js
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ describe('GET /delegates', () => {
});
});

describe('?', () => {
describe('timestamp filters', () => {
describe('fromTimestamp', () => {
it('using invalid fromTimestamp should fail', async () => {
return forgedEndpoint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ describe('delegates/api', () => {
it('should call channel.invoke with chain:calculateSupply action if lastBlock.height is not 0', async () => {
expect(channelStub.invoke).to.be.calledWithExactly(
'chain:calculateSupply',
[dummyBlock.height]
{ height: dummyBlock.height }
);
});

Expand Down

0 comments on commit d2fed4e

Please sign in to comment.