@@ -202,26 +202,20 @@ export class PgStore extends BasePgStore {
202202 } ) ;
203203 }
204204
205- async getChainTip ( sql : PgSqlClient ) : Promise < {
206- blockHeight : number ;
207- blockHash : string ;
208- indexBlockHash : string ;
209- burnBlockHeight : number ;
210- } > {
211- const currentTipBlock = await sql <
212- {
213- block_height : number ;
214- block_hash : string ;
215- index_block_hash : string ;
216- burn_block_height : number ;
217- } [ ]
218- > `SELECT block_height, block_hash, index_block_hash, burn_block_height FROM chain_tip` ;
219- const height = currentTipBlock [ 0 ] ?. block_height ?? 0 ;
205+ async getChainTip ( ) : Promise < DbChainTip > {
206+ const tipResult = await this . sql < DbChainTip [ ] > `SELECT * FROM chain_tip` ;
207+ const tip = tipResult [ 0 ] ;
220208 return {
221- blockHeight : height ,
222- blockHash : currentTipBlock [ 0 ] ?. block_hash ?? '' ,
223- indexBlockHash : currentTipBlock [ 0 ] ?. index_block_hash ?? '' ,
224- burnBlockHeight : currentTipBlock [ 0 ] ?. burn_block_height ?? 0 ,
209+ block_height : tip ?. block_height ?? 0 ,
210+ block_count : tip ?. block_count ?? 0 ,
211+ block_hash : tip ?. block_hash ?? '' ,
212+ index_block_hash : tip ?. index_block_hash ?? '' ,
213+ burn_block_height : tip ?. burn_block_height ?? 0 ,
214+ microblock_hash : tip ?. microblock_hash ?? undefined ,
215+ microblock_sequence : tip ?. microblock_sequence ?? undefined ,
216+ microblock_count : tip ?. microblock_count ?? 0 ,
217+ tx_count : tip ?. tx_count ?? 0 ,
218+ tx_count_unanchored : tip ?. tx_count_unanchored ?? 0 ,
225219 } ;
226220 }
227221
@@ -316,33 +310,6 @@ export class PgStore extends BasePgStore {
316310 return this . getPoxForcedUnlockHeightsInternal ( this . sql ) ;
317311 }
318312
319- async getUnanchoredChainTip ( ) : Promise < FoundOrNot < DbChainTip > > {
320- const result = await this . sql <
321- {
322- block_height : number ;
323- index_block_hash : string ;
324- block_hash : string ;
325- microblock_hash : string | null ;
326- microblock_sequence : number | null ;
327- burn_block_height : number ;
328- } [ ]
329- > `SELECT block_height, index_block_hash, block_hash, microblock_hash, microblock_sequence, burn_block_height
330- FROM chain_tip` ;
331- if ( result . length === 0 ) {
332- return { found : false } as const ;
333- }
334- const row = result [ 0 ] ;
335- const chainTipResult : DbChainTip = {
336- blockHeight : row . block_height ,
337- indexBlockHash : row . index_block_hash ,
338- blockHash : row . block_hash ,
339- microblockHash : row . microblock_hash === null ? undefined : row . microblock_hash ,
340- microblockSequence : row . microblock_sequence === null ? undefined : row . microblock_sequence ,
341- burnBlockHeight : row . burn_block_height ,
342- } ;
343- return { found : true , result : chainTipResult } ;
344- }
345-
346313 async getBlock ( blockIdentifer : BlockIdentifier ) : Promise < FoundOrNot < DbBlock > > {
347314 return this . getBlockInternal ( this . sql , blockIdentifer ) ;
348315 }
@@ -626,8 +593,8 @@ export class PgStore extends BasePgStore {
626593
627594 async getUnanchoredTxsInternal ( sql : PgSqlClient ) : Promise < { txs : DbTx [ ] } > {
628595 // Get transactions that have been streamed in microblocks but not yet accepted or rejected in an anchor block.
629- const { blockHeight } = await this . getChainTip ( sql ) ;
630- const unanchoredBlockHeight = blockHeight + 1 ;
596+ const { block_height } = await this . getChainTip ( ) ;
597+ const unanchoredBlockHeight = block_height + 1 ;
631598 const query = await sql < ContractTxQueryResult [ ] > `
632599 SELECT ${ unsafeCols ( sql , [ ...TX_COLUMNS , abiColumn ( ) ] ) }
633600 FROM txs
@@ -1372,11 +1339,11 @@ export class PgStore extends BasePgStore {
13721339 sql : PgSqlClient ,
13731340 { includeUnanchored } : { includeUnanchored : boolean }
13741341 ) : Promise < number > {
1375- const chainTip = await this . getChainTip ( sql ) ;
1342+ const chainTip = await this . getChainTip ( ) ;
13761343 if ( includeUnanchored ) {
1377- return chainTip . blockHeight + 1 ;
1344+ return chainTip . block_height + 1 ;
13781345 } else {
1379- return chainTip . blockHeight ;
1346+ return chainTip . block_height ;
13801347 }
13811348 }
13821349
@@ -2159,9 +2126,9 @@ export class PgStore extends BasePgStore {
21592126
21602127 async getStxBalanceAtBlock ( stxAddress : string , blockHeight : number ) : Promise < DbStxBalance > {
21612128 return await this . sqlTransaction ( async sql => {
2162- const chainTip = await this . getChainTip ( sql ) ;
2129+ const chainTip = await this . getChainTip ( ) ;
21632130 const blockHeightToQuery =
2164- blockHeight > chainTip . blockHeight ? chainTip . blockHeight : blockHeight ;
2131+ blockHeight > chainTip . block_height ? chainTip . block_height : blockHeight ;
21652132 const blockQuery = await this . getBlockByHeightInternal ( sql , blockHeightToQuery ) ;
21662133 if ( ! blockQuery . found ) {
21672134 throw new Error ( `Could not find block at height: ${ blockHeight } ` ) ;
0 commit comments