-
Notifications
You must be signed in to change notification settings - Fork 457
Fixed fees calculations in getForgedByAccount endpoint #384
Changes from all commits
a58288f
47ecf23
ddffa97
0817002
65e3cdb
539b4ba
0e6064d
9a38a92
c6022f3
24cac3d
0f8cc6f
0dca3d2
858ea7d
f08ee65
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,9 +26,51 @@ var BlocksSql = { | |
|
||
aggregateBlocksReward: function (params) { | ||
return [ | ||
'SELECT SUM("b_totalFee") AS fees, SUM("b_reward") AS rewards, COUNT(1) AS count', | ||
'FROM blocks_list', | ||
(params.where.length ? 'WHERE ' + params.where.join(' AND ') : '') | ||
'WITH', | ||
'delegate AS (SELECT', | ||
'1 FROM mem_accounts m WHERE m."isDelegate" = 1 AND m."publicKey" = DECODE (${generatorPublicKey}, \'hex\') LIMIT 1),', | ||
'borders AS (SELECT', | ||
'(SELECT (CAST(b.height / ${delegates} AS INTEGER) + (CASE WHEN b.height % ${delegates} > 0 THEN 1 ELSE 0 END)) FROM blocks b ORDER BY b.height DESC LIMIT 1) AS current,', | ||
'(SELECT (CAST(b.height / ${delegates} AS INTEGER) + (CASE WHEN b.height % ${delegates} > 0 THEN 1 ELSE 0 END)) FROM blocks b', | ||
(params.start !== undefined ? ' WHERE b.timestamp >= ${start}' : ''), | ||
'ORDER BY b.height ASC LIMIT 1) AS min,', | ||
'(SELECT (CAST(b.height / ${delegates} AS INTEGER) + (CASE WHEN b.height % ${delegates} > 0 THEN 1 ELSE 0 END)) FROM blocks b', | ||
(params.end !== undefined ? ' WHERE b.timestamp <= ${end}' : ''), | ||
'ORDER BY b.height DESC LIMIT 1) AS max', | ||
'),', | ||
'r AS (SELECT DISTINCT ', | ||
'(CAST(b.height / ${delegates} AS INTEGER) + (CASE WHEN b.height % ${delegates} > 0 THEN 1 ELSE 0 END)) AS round', | ||
'FROM blocks b WHERE b."generatorPublicKey" = DECODE (${generatorPublicKey}, \'hex\')),', | ||
're AS (SELECT r.round AS round, ((r.round-1)*${delegates})+1 AS min, r.round*${delegates} AS max', | ||
'FROM r WHERE r.round >= (SELECT min FROM borders) AND round <= (SELECT max FROM borders)),', | ||
'sum_min AS (SELECT', | ||
'SUM(CASE WHEN b."generatorPublicKey" = DECODE (${generatorPublicKey}, \'hex\') THEN b.reward ELSE 0 END) AS rewards,', | ||
'SUM(CASE WHEN b."generatorPublicKey" = DECODE (${generatorPublicKey}, \'hex\') THEN 1 ELSE 0 END) AS blocks', | ||
'FROM blocks b WHERE b.height BETWEEN (SELECT min FROM re ORDER BY round ASC LIMIT 1) AND (SELECT max FROM re ORDER BY round ASC LIMIT 1)', | ||
(params.start !== undefined ? 'AND b.timestamp >= ${start}' : ''), | ||
'),', | ||
'sum_max AS (SELECT', | ||
'SUM(CASE WHEN b."generatorPublicKey" = DECODE (${generatorPublicKey}, \'hex\') THEN b.reward ELSE 0 END) AS rewards,', | ||
'SUM(CASE WHEN b."generatorPublicKey" = DECODE (${generatorPublicKey}, \'hex\') THEN 1 ELSE 0 END) AS blocks', | ||
'FROM blocks b WHERE b.height BETWEEN (SELECT min FROM re ORDER BY round DESC LIMIT 1) AND (SELECT max FROM re ORDER BY round DESC LIMIT 1)', | ||
(params.end !== undefined ? 'AND b.timestamp <= ${end}' : ''), | ||
'),', | ||
'rs AS (SELECT re.*, SUM(b."totalFee") AS fees,', | ||
'SUM(CASE WHEN b."generatorPublicKey" = DECODE (${generatorPublicKey}, \'hex\') THEN b.reward ELSE 0 END) AS rewards,', | ||
'SUM(CASE WHEN b."generatorPublicKey" = DECODE (${generatorPublicKey}, \'hex\') THEN 1 ELSE 0 END) AS blocks', | ||
'FROM re, blocks b WHERE b.height BETWEEN re.min AND re.max GROUP BY re.round, re.min, re.max),', | ||
'rsc AS (SELECT', | ||
'(CASE WHEN round = borders.current THEN 0 ELSE fees END), round,', | ||
'(CASE WHEN round = borders.min THEN (SELECT blocks FROM sum_min) ELSE (CASE WHEN round = borders.max THEN (SELECT blocks FROM sum_max) ELSE blocks END) END) AS blocks,', | ||
'(CASE WHEN round = borders.min THEN (SELECT rewards FROM sum_min) ELSE (CASE WHEN round = borders.max THEN (SELECT rewards FROM sum_max) ELSE rewards END) END) AS rewards,', | ||
'(SELECT 1 FROM blocks b WHERE b.height = rs.max AND b."generatorPublicKey" = DECODE (${generatorPublicKey}, \'hex\') LIMIT 1) AS last', | ||
'FROM rs, borders)', | ||
'SELECT', | ||
'(SELECT * FROM delegate) AS delegate,', | ||
'SUM(rsc.blocks) AS count,', | ||
'SUM(floor(rsc.fees/${delegates})*rsc.blocks + (CASE WHEN rsc.last = 1 THEN (rsc.fees-floor(rsc.fees/${delegates})*${delegates}) ELSE 0 END)) AS fees,', | ||
'SUM(rsc.rewards) AS rewards', | ||
'FROM rsc' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wow There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @4miners: Would it be possible to move this query to a postgres view? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll check that. |
||
].filter(Boolean).join(' '); | ||
}, | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ var RoundsSql = { | |
|
||
updateBlockId: 'UPDATE mem_accounts SET "blockId" = ${newId} WHERE "blockId" = ${oldId};', | ||
|
||
summedRound: 'SELECT SUM(b."totalFee")::bigint AS "fees", ARRAY_AGG(b."reward") AS "rewards", ARRAY_AGG(ENCODE(b."generatorPublicKey", \'hex\')) AS "delegates" FROM blocks b WHERE (SELECT (CAST(b."height" / ${activeDelegates} AS INTEGER) + (CASE WHEN b."height" % ${activeDelegates} > 0 THEN 1 ELSE 0 END))) = ${round}' | ||
summedRound: 'WITH round_blocks as (SELECT ((${round}-1)*${activeDelegates})+1 as min, ${round}*${activeDelegates} as max) SELECT SUM(b."totalFee")::bigint AS "fees", ARRAY_AGG(b."reward") AS "rewards", ARRAY_AGG(ENCODE(b."generatorPublicKey", \'hex\')) AS "delegates" FROM blocks b WHERE b.height BETWEEN (SELECT min FROM round_blocks) AND (SELECT max FROM round_blocks)' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From 1200-3500 msec to 0.2-0.3 msec. Before:
After:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @4miners Nice 👍 |
||
}; | ||
|
||
module.exports = RoundsSql; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why changed condition here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should allow
start/end
to be0
and search by only one border, previous cond not allow that.