Skip to content

Commit 27f784f

Browse files
authored
Merge pull request #1992 from hirosystems/develop
cut beta release
2 parents 113a7ea + 5ce9b44 commit 27f784f

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed

src/datastore/pg-store-v2.ts

+32-27
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BasePgStoreModule, PgSqlClient } from '@hirosystems/api-toolkit';
1+
import { BasePgStoreModule, PgSqlClient, has0xPrefix } from '@hirosystems/api-toolkit';
22
import {
33
BlockLimitParamSchema,
44
CompiledBurnBlockHashParam,
@@ -624,6 +624,7 @@ export class PgStoreV2 extends BasePgStoreModule {
624624
return this.sqlTransaction(async sql => {
625625
const limit = args.limit ?? PoxSignerLimitParamSchema.default;
626626
const offset = args.offset ?? 0;
627+
const cycleNumber = parseInt(args.cycle_number);
627628
const cycleCheck =
628629
await sql`SELECT cycle_number FROM pox_cycles WHERE cycle_number = ${args.cycle_number} LIMIT 1`;
629630
if (cycleCheck.count === 0)
@@ -634,8 +635,8 @@ export class PgStoreV2 extends BasePgStoreModule {
634635
FROM pox4_events
635636
WHERE canonical = true AND microblock_canonical = true
636637
AND name in ('stack-aggregation-commit-indexed', 'stack-aggregation-commit')
637-
AND start_cycle_id = ${args.cycle_number}
638-
AND end_cycle_id = ${args.cycle_number + 1}
638+
AND start_cycle_id = ${cycleNumber}
639+
AND end_cycle_id = ${cycleNumber + 1}
639640
ORDER BY stacker, block_height DESC, tx_index DESC, event_index DESC
640641
), delegated_stackers AS (
641642
SELECT DISTINCT ON (main.stacker)
@@ -646,8 +647,8 @@ export class PgStoreV2 extends BasePgStoreModule {
646647
WHERE main.canonical = true
647648
AND main.microblock_canonical = true
648649
AND main.name IN ('delegate-stack-stx', 'delegate-stack-increase', 'delegate-stack-extend')
649-
AND main.start_cycle_id <= ${args.cycle_number}
650-
AND main.end_cycle_id > ${args.cycle_number}
650+
AND main.start_cycle_id <= ${cycleNumber}
651+
AND main.end_cycle_id > ${cycleNumber}
651652
ORDER BY main.stacker, main.block_height DESC, main.microblock_sequence DESC, main.tx_index DESC, main.event_index DESC
652653
), solo_stackers AS (
653654
SELECT DISTINCT ON (stacker)
@@ -656,8 +657,8 @@ export class PgStoreV2 extends BasePgStoreModule {
656657
FROM pox4_events
657658
WHERE canonical = true AND microblock_canonical = true
658659
AND name in ('stack-stx', 'stacks-increase', 'stack-extend')
659-
AND start_cycle_id <= ${args.cycle_number}
660-
AND end_cycle_id > ${args.cycle_number}
660+
AND start_cycle_id <= ${cycleNumber}
661+
AND end_cycle_id > ${cycleNumber}
661662
ORDER BY stacker, block_height DESC, microblock_sequence DESC, tx_index DESC, event_index DESC
662663
)
663664
SELECT
@@ -672,7 +673,7 @@ export class PgStoreV2 extends BasePgStoreModule {
672673
FROM pox_sets ps
673674
LEFT JOIN delegated_stackers ds ON ps.signing_key = ds.signer_key
674675
LEFT JOIN solo_stackers ss ON ps.signing_key = ss.signer_key
675-
WHERE ps.canonical = TRUE AND ps.cycle_number = ${args.cycle_number}
676+
WHERE ps.canonical = TRUE AND ps.cycle_number = ${cycleNumber}
676677
GROUP BY ps.signing_key, ps.weight, ps.stacked_amount, ps.weight_percent, ps.stacked_amount_percent
677678
ORDER BY ps.weight DESC, ps.stacked_amount DESC, ps.signing_key
678679
OFFSET ${offset}
@@ -689,8 +690,10 @@ export class PgStoreV2 extends BasePgStoreModule {
689690

690691
async getPoxCycleSigner(args: PoxCycleSignerParams): Promise<DbPoxCycleSigner | undefined> {
691692
return this.sqlTransaction(async sql => {
693+
const signerKey = has0xPrefix(args.signer_key) ? args.signer_key : '0x' + args.signer_key;
694+
const cycleNumber = parseInt(args.cycle_number);
692695
const cycleCheck =
693-
await sql`SELECT cycle_number FROM pox_cycles WHERE cycle_number = ${args.cycle_number} LIMIT 1`;
696+
await sql`SELECT cycle_number FROM pox_cycles WHERE cycle_number = ${cycleNumber} LIMIT 1`;
694697
if (cycleCheck.count === 0)
695698
throw new InvalidRequestError(`PoX cycle not found`, InvalidRequestErrorType.invalid_param);
696699
const results = await sql<DbPoxCycleSigner[]>`
@@ -699,8 +702,8 @@ export class PgStoreV2 extends BasePgStoreModule {
699702
FROM pox4_events
700703
WHERE canonical = true AND microblock_canonical = true
701704
AND name in ('stack-aggregation-commit-indexed', 'stack-aggregation-commit')
702-
AND start_cycle_id = ${args.cycle_number}
703-
AND end_cycle_id = ${args.cycle_number + 1}
705+
AND start_cycle_id = ${cycleNumber}
706+
AND end_cycle_id = ${cycleNumber + 1}
704707
ORDER BY stacker, block_height DESC, tx_index DESC, event_index DESC
705708
), delegated_stackers AS (
706709
SELECT DISTINCT ON (main.stacker)
@@ -711,8 +714,8 @@ export class PgStoreV2 extends BasePgStoreModule {
711714
WHERE main.canonical = true
712715
AND main.microblock_canonical = true
713716
AND main.name IN ('delegate-stack-stx', 'delegate-stack-increase', 'delegate-stack-extend')
714-
AND main.start_cycle_id <= ${args.cycle_number}
715-
AND main.end_cycle_id > ${args.cycle_number}
717+
AND main.start_cycle_id <= ${cycleNumber}
718+
AND main.end_cycle_id > ${cycleNumber}
716719
ORDER BY main.stacker, main.block_height DESC, main.microblock_sequence DESC, main.tx_index DESC, main.event_index DESC
717720
), solo_stackers AS (
718721
SELECT DISTINCT ON (stacker)
@@ -721,8 +724,8 @@ export class PgStoreV2 extends BasePgStoreModule {
721724
FROM pox4_events
722725
WHERE canonical = true AND microblock_canonical = true
723726
AND name in ('stack-stx', 'stacks-increase', 'stack-extend')
724-
AND start_cycle_id <= ${args.cycle_number}
725-
AND end_cycle_id > ${args.cycle_number}
727+
AND start_cycle_id <= ${cycleNumber}
728+
AND end_cycle_id > ${cycleNumber}
726729
ORDER BY stacker, block_height DESC, microblock_sequence DESC, tx_index DESC, event_index DESC
727730
)
728731
SELECT
@@ -737,8 +740,8 @@ export class PgStoreV2 extends BasePgStoreModule {
737740
LEFT JOIN delegated_stackers ds ON ps.signing_key = ds.signer_key
738741
LEFT JOIN solo_stackers ss ON ps.signing_key = ss.signer_key
739742
WHERE ps.canonical = TRUE
740-
AND ps.cycle_number = ${args.cycle_number}
741-
AND ps.signing_key = ${args.signer_key}
743+
AND ps.cycle_number = ${cycleNumber}
744+
AND ps.signing_key = ${signerKey}
742745
GROUP BY ps.signing_key, ps.weight, ps.stacked_amount, ps.weight_percent, ps.stacked_amount_percent
743746
LIMIT 1
744747
`;
@@ -752,15 +755,17 @@ export class PgStoreV2 extends BasePgStoreModule {
752755
return this.sqlTransaction(async sql => {
753756
const limit = args.limit ?? PoxSignerLimitParamSchema.default;
754757
const offset = args.offset ?? 0;
758+
const signerKey = has0xPrefix(args.signer_key) ? args.signer_key : '0x' + args.signer_key;
759+
const cycleNumber = parseInt(args.cycle_number);
755760
const cycleCheck = await sql`
756-
SELECT cycle_number FROM pox_cycles WHERE cycle_number = ${args.cycle_number} LIMIT 1
761+
SELECT cycle_number FROM pox_cycles WHERE cycle_number = ${cycleNumber} LIMIT 1
757762
`;
758763
if (cycleCheck.count === 0)
759764
throw new InvalidRequestError(`PoX cycle not found`, InvalidRequestErrorType.invalid_param);
760765
const signerCheck = await sql`
761766
SELECT signing_key
762767
FROM pox_sets
763-
WHERE cycle_number = ${args.cycle_number} AND signing_key = ${args.signer_key}
768+
WHERE cycle_number = ${cycleNumber} AND signing_key = ${args.signer_key}
764769
LIMIT 1
765770
`;
766771
if (signerCheck.count === 0)
@@ -774,8 +779,8 @@ export class PgStoreV2 extends BasePgStoreModule {
774779
FROM pox4_events
775780
WHERE canonical = true AND microblock_canonical = true
776781
AND name in ('stack-aggregation-commit-indexed', 'stack-aggregation-commit')
777-
AND start_cycle_id = ${args.cycle_number}
778-
AND end_cycle_id = ${args.cycle_number + 1}
782+
AND start_cycle_id = ${cycleNumber}
783+
AND end_cycle_id = ${cycleNumber + 1}
779784
ORDER BY stacker, block_height DESC, tx_index DESC, event_index DESC
780785
), delegated_stackers AS (
781786
SELECT DISTINCT ON (main.stacker)
@@ -791,8 +796,8 @@ export class PgStoreV2 extends BasePgStoreModule {
791796
WHERE main.canonical = true
792797
AND main.microblock_canonical = true
793798
AND main.name IN ('delegate-stack-stx', 'delegate-stack-increase', 'delegate-stack-extend')
794-
AND main.start_cycle_id <= ${args.cycle_number}
795-
AND main.end_cycle_id > ${args.cycle_number}
799+
AND main.start_cycle_id <= ${cycleNumber}
800+
AND main.end_cycle_id > ${cycleNumber}
796801
ORDER BY main.stacker, main.block_height DESC, main.microblock_sequence DESC, main.tx_index DESC, main.event_index DESC
797802
), solo_stackers AS (
798803
SELECT DISTINCT ON (stacker)
@@ -806,8 +811,8 @@ export class PgStoreV2 extends BasePgStoreModule {
806811
FROM pox4_events
807812
WHERE canonical = true AND microblock_canonical = true
808813
AND name in ('stack-stx', 'stacks-increase', 'stack-extend')
809-
AND start_cycle_id <= ${args.cycle_number}
810-
AND end_cycle_id > ${args.cycle_number}
814+
AND start_cycle_id <= ${cycleNumber}
815+
AND end_cycle_id > ${cycleNumber}
811816
ORDER BY stacker, block_height DESC, microblock_sequence DESC, tx_index DESC, event_index DESC
812817
), combined_stackers AS (
813818
SELECT * FROM delegated_stackers
@@ -826,8 +831,8 @@ export class PgStoreV2 extends BasePgStoreModule {
826831
FROM pox_sets ps
827832
LEFT JOIN combined_stackers cs ON ps.signing_key = cs.signer_key
828833
WHERE ps.canonical = TRUE
829-
AND ps.cycle_number = ${args.cycle_number}
830-
AND ps.signing_key = ${args.signer_key}
834+
AND ps.cycle_number = ${cycleNumber}
835+
AND ps.signing_key = ${signerKey}
831836
ORDER BY locked DESC
832837
LIMIT ${limit}
833838
OFFSET ${offset}

0 commit comments

Comments
 (0)