1
1
import { BasePgStoreModule , PgSqlClient , has0xPrefix } from '@hirosystems/api-toolkit' ;
2
2
import {
3
3
BlockLimitParamSchema ,
4
- CompiledBurnBlockHashParam ,
5
4
TransactionPaginationQueryParams ,
6
5
TransactionLimitParamSchema ,
7
- BlockParams ,
8
- BurnBlockParams ,
9
6
BlockPaginationQueryParams ,
10
7
SmartContractStatusParams ,
11
8
AddressParams ,
12
- AddressTransactionParams ,
13
9
PoxCyclePaginationQueryParams ,
14
10
PoxCycleLimitParamSchema ,
15
- PoxSignerPaginationQueryParams ,
16
11
PoxSignerLimitParamSchema ,
17
12
BlockIdParam ,
18
13
BlockSignerSignatureLimitParamSchema ,
@@ -51,13 +46,6 @@ import {
51
46
} from './helpers' ;
52
47
import { SyntheticPoxEventName } from '../pox-helpers' ;
53
48
54
- async function assertAddressExists ( sql : PgSqlClient , address : string ) {
55
- const addressCheck =
56
- await sql `SELECT principal FROM principal_stx_txs WHERE principal = ${ address } LIMIT 1` ;
57
- if ( addressCheck . count === 0 )
58
- throw new InvalidRequestError ( `Address not found` , InvalidRequestErrorType . invalid_param ) ;
59
- }
60
-
61
49
async function assertTxIdExists ( sql : PgSqlClient , tx_id : string ) {
62
50
const txCheck = await sql `SELECT tx_id FROM txs WHERE tx_id = ${ tx_id } LIMIT 1` ;
63
51
if ( txCheck . count === 0 )
@@ -69,11 +57,15 @@ export class PgStoreV2 extends BasePgStoreModule {
69
57
limit : number ;
70
58
offset ?: number ;
71
59
cursor ?: string ;
60
+ tenureHeight ?: number ;
72
61
} ) : Promise < DbCursorPaginatedResult < DbBlock > > {
73
62
return await this . sqlTransaction ( async sql => {
74
63
const limit = args . limit ;
75
64
const offset = args . offset ?? 0 ;
76
65
const cursor = args . cursor ?? null ;
66
+ const tenureFilter = args . tenureHeight
67
+ ? sql `AND tenure_height = ${ args . tenureHeight } `
68
+ : sql `` ;
77
69
78
70
const blocksQuery = await sql <
79
71
( BlockQueryResult & { total : number ; next_block_hash : string ; prev_block_hash : string } ) [ ]
@@ -82,7 +74,7 @@ export class PgStoreV2 extends BasePgStoreModule {
82
74
WITH ordered_blocks AS (
83
75
SELECT *, LEAD(block_height, ${ offset } ) OVER (ORDER BY block_height DESC) offset_block_height
84
76
FROM blocks
85
- WHERE canonical = true
77
+ WHERE canonical = true ${ tenureFilter }
86
78
ORDER BY block_height DESC
87
79
)
88
80
SELECT offset_block_height as block_height
@@ -94,20 +86,22 @@ export class PgStoreV2 extends BasePgStoreModule {
94
86
SELECT ${ sql ( BLOCK_COLUMNS ) }
95
87
FROM blocks
96
88
WHERE canonical = true
97
- AND block_height <= (SELECT block_height FROM cursor_block)
89
+ ${ tenureFilter }
90
+ AND block_height <= (SELECT block_height FROM cursor_block)
98
91
ORDER BY block_height DESC
99
92
LIMIT ${ limit }
100
93
),
101
94
prev_page AS (
102
95
SELECT index_block_hash as prev_block_hash
103
96
FROM blocks
104
97
WHERE canonical = true
105
- AND block_height < (
106
- SELECT block_height
107
- FROM selected_blocks
108
- ORDER BY block_height DESC
109
- LIMIT 1
110
- )
98
+ ${ tenureFilter }
99
+ AND block_height < (
100
+ SELECT block_height
101
+ FROM selected_blocks
102
+ ORDER BY block_height DESC
103
+ LIMIT 1
104
+ )
111
105
ORDER BY block_height DESC
112
106
OFFSET ${ limit - 1 }
113
107
LIMIT 1
@@ -116,18 +110,26 @@ export class PgStoreV2 extends BasePgStoreModule {
116
110
SELECT index_block_hash as next_block_hash
117
111
FROM blocks
118
112
WHERE canonical = true
119
- AND block_height > (
120
- SELECT block_height
121
- FROM selected_blocks
122
- ORDER BY block_height DESC
123
- LIMIT 1
124
- )
113
+ ${ tenureFilter }
114
+ AND block_height > (
115
+ SELECT block_height
116
+ FROM selected_blocks
117
+ ORDER BY block_height DESC
118
+ LIMIT 1
119
+ )
125
120
ORDER BY block_height ASC
126
121
OFFSET ${ limit - 1 }
127
122
LIMIT 1
123
+ ),
124
+ block_count AS (
125
+ SELECT ${
126
+ args . tenureHeight
127
+ ? sql `(SELECT COUNT(*) FROM blocks WHERE tenure_height = ${ args . tenureHeight } )::int`
128
+ : sql `(SELECT block_count FROM chain_tip)::int`
129
+ } AS total
128
130
)
129
131
SELECT
130
- (SELECT block_count FROM chain_tip)::int AS total,
132
+ (SELECT total FROM block_count) AS total,
131
133
sb.*,
132
134
nb.next_block_hash,
133
135
pb.prev_block_hash
0 commit comments