@@ -252,28 +252,20 @@ export class PgStore {
252252 } ) ;
253253 }
254254
255- async getChainTip (
256- sql : PgSqlClient
257- ) : Promise < {
258- blockHeight : number ;
259- blockHash : string ;
260- indexBlockHash : string ;
261- burnBlockHeight : number ;
262- } > {
263- const currentTipBlock = await sql <
264- {
265- block_height : number ;
266- block_hash : string ;
267- index_block_hash : string ;
268- burn_block_height : number ;
269- } [ ]
270- > `SELECT block_height, block_hash, index_block_hash, burn_block_height FROM chain_tip` ;
271- const height = currentTipBlock [ 0 ] ?. block_height ?? 0 ;
255+ async getChainTip ( ) : Promise < DbChainTip > {
256+ const tipResult = await this . sql < DbChainTip [ ] > `SELECT * FROM chain_tip` ;
257+ const tip = tipResult [ 0 ] ;
272258 return {
273- blockHeight : height ,
274- blockHash : currentTipBlock [ 0 ] ?. block_hash ?? '' ,
275- indexBlockHash : currentTipBlock [ 0 ] ?. index_block_hash ?? '' ,
276- burnBlockHeight : currentTipBlock [ 0 ] ?. burn_block_height ?? 0 ,
259+ block_height : tip ?. block_height ?? 0 ,
260+ block_count : tip ?. block_count ?? 0 ,
261+ block_hash : tip ?. block_hash ?? '' ,
262+ index_block_hash : tip ?. index_block_hash ?? '' ,
263+ burn_block_height : tip ?. burn_block_height ?? 0 ,
264+ microblock_hash : tip ?. microblock_hash ?? undefined ,
265+ microblock_sequence : tip ?. microblock_sequence ?? undefined ,
266+ microblock_count : tip ?. microblock_count ?? 0 ,
267+ tx_count : tip ?. tx_count ?? 0 ,
268+ tx_count_unanchored : tip ?. tx_count_unanchored ?? 0 ,
277269 } ;
278270 }
279271
@@ -368,33 +360,6 @@ export class PgStore {
368360 return this . getPoxForcedUnlockHeightsInternal ( this . sql ) ;
369361 }
370362
371- async getUnanchoredChainTip ( ) : Promise < FoundOrNot < DbChainTip > > {
372- const result = await this . sql <
373- {
374- block_height : number ;
375- index_block_hash : string ;
376- block_hash : string ;
377- microblock_hash : string | null ;
378- microblock_sequence : number | null ;
379- burn_block_height : number ;
380- } [ ]
381- > `SELECT block_height, index_block_hash, block_hash, microblock_hash, microblock_sequence, burn_block_height
382- FROM chain_tip` ;
383- if ( result . length === 0 ) {
384- return { found : false } as const ;
385- }
386- const row = result [ 0 ] ;
387- const chainTipResult : DbChainTip = {
388- blockHeight : row . block_height ,
389- indexBlockHash : row . index_block_hash ,
390- blockHash : row . block_hash ,
391- microblockHash : row . microblock_hash === null ? undefined : row . microblock_hash ,
392- microblockSequence : row . microblock_sequence === null ? undefined : row . microblock_sequence ,
393- burnBlockHeight : row . burn_block_height ,
394- } ;
395- return { found : true , result : chainTipResult } ;
396- }
397-
398363 async getBlock ( blockIdentifer : BlockIdentifier ) : Promise < FoundOrNot < DbBlock > > {
399364 return this . getBlockInternal ( this . sql , blockIdentifer ) ;
400365 }
@@ -678,8 +643,8 @@ export class PgStore {
678643
679644 async getUnanchoredTxsInternal ( sql : PgSqlClient ) : Promise < { txs : DbTx [ ] } > {
680645 // Get transactions that have been streamed in microblocks but not yet accepted or rejected in an anchor block.
681- const { blockHeight } = await this . getChainTip ( sql ) ;
682- const unanchoredBlockHeight = blockHeight + 1 ;
646+ const { block_height } = await this . getChainTip ( ) ;
647+ const unanchoredBlockHeight = block_height + 1 ;
683648 const query = await sql < ContractTxQueryResult [ ] > `
684649 SELECT ${ unsafeCols ( sql , [ ...TX_COLUMNS , abiColumn ( ) ] ) }
685650 FROM txs
@@ -1426,11 +1391,11 @@ export class PgStore {
14261391 sql : PgSqlClient ,
14271392 { includeUnanchored } : { includeUnanchored : boolean }
14281393 ) : Promise < number > {
1429- const chainTip = await this . getChainTip ( sql ) ;
1394+ const chainTip = await this . getChainTip ( ) ;
14301395 if ( includeUnanchored ) {
1431- return chainTip . blockHeight + 1 ;
1396+ return chainTip . block_height + 1 ;
14321397 } else {
1433- return chainTip . blockHeight ;
1398+ return chainTip . block_height ;
14341399 }
14351400 }
14361401
@@ -2213,9 +2178,9 @@ export class PgStore {
22132178
22142179 async getStxBalanceAtBlock ( stxAddress : string , blockHeight : number ) : Promise < DbStxBalance > {
22152180 return await this . sqlTransaction ( async sql => {
2216- const chainTip = await this . getChainTip ( sql ) ;
2181+ const chainTip = await this . getChainTip ( ) ;
22172182 const blockHeightToQuery =
2218- blockHeight > chainTip . blockHeight ? chainTip . blockHeight : blockHeight ;
2183+ blockHeight > chainTip . block_height ? chainTip . block_height : blockHeight ;
22192184 const blockQuery = await this . getBlockByHeightInternal ( sql , blockHeightToQuery ) ;
22202185 if ( ! blockQuery . found ) {
22212186 throw new Error ( `Could not find block at height: ${ blockHeight } ` ) ;
0 commit comments