From b60336c4552c8fa253865891955324e579ae397e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20C=C3=A1rdenas?= Date: Wed, 6 Jul 2022 16:18:46 -0500 Subject: [PATCH] feat: add microblock_hash to etag --- src/api/controllers/cache-controller.ts | 9 +++++++-- src/datastore/common.ts | 8 +++++++- src/datastore/postgres-store.ts | 16 ++++++++++------ 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/api/controllers/cache-controller.ts b/src/api/controllers/cache-controller.ts index 27485a223e..d3ba27b860 100644 --- a/src/api/controllers/cache-controller.ts +++ b/src/api/controllers/cache-controller.ts @@ -278,7 +278,12 @@ async function calculateETag( if (!status.found) { return ETAG_EMPTY; } - const indexBlockHash = bufferToHexPrefixString(status.result.index_block_hash); - return `${normalizedTxId}:${indexBlockHash}:${status.result.status}`; + const elements: string[] = [ + normalizedTxId, + bufferToHexPrefixString(status.result.index_block_hash), + bufferToHexPrefixString(status.result.microblock_hash), + status.result.status.toString(), + ]; + return elements.join(':'); } } diff --git a/src/datastore/common.ts b/src/datastore/common.ts index dcc34499ae..e257b9901c 100644 --- a/src/datastore/common.ts +++ b/src/datastore/common.ts @@ -611,6 +611,12 @@ export interface DbChainTip { microblockSequence?: number; } +export interface DbTxGlobalStatus { + status: DbTxStatus; + index_block_hash: Buffer; + microblock_hash: Buffer; +} + export interface DataStore extends DataStoreEventEmitter { storeRawEventRequest(eventPath: string, payload: string): Promise; getSubdomainResolver(name: { name: string }): Promise>; @@ -861,7 +867,7 @@ export interface DataStore extends DataStoreEventEmitter { getRawTx(txId: string): Promise>; - getTxStatus(txId: string): Promise>; + getTxStatus(txId: string): Promise>; /** * Returns a list of NFTs owned by the given principal filtered by optional `asset_identifiers`, diff --git a/src/datastore/postgres-store.ts b/src/datastore/postgres-store.ts index 03fe8c8f63..b1862214ac 100644 --- a/src/datastore/postgres-store.ts +++ b/src/datastore/postgres-store.ts @@ -97,6 +97,7 @@ import { NftHoldingInfoWithTxMetadata, NftEventWithTxMetadata, DbAssetEventTypeId, + DbTxGlobalStatus, } from './common'; import { AddressTokenOfferingLocked, @@ -4055,12 +4056,10 @@ export class PgDataStore }); } - async getTxStatus( - txId: string - ): Promise> { + async getTxStatus(txId: string): Promise> { return this.queryTx(async client => { - const chainResult = await client.query<{ status: number; index_block_hash: Buffer }>( - `SELECT status, index_block_hash + const chainResult = await client.query( + `SELECT status, index_block_hash, microblock_hash FROM txs WHERE tx_id = $1 AND canonical = TRUE AND microblock_canonical = TRUE`, [hexToBuffer(txId)] @@ -4071,6 +4070,7 @@ export class PgDataStore result: { status: chainResult.rows[0].status, index_block_hash: chainResult.rows[0].index_block_hash, + microblock_hash: chainResult.rows[0].microblock_hash, }, }; } @@ -4083,7 +4083,11 @@ export class PgDataStore if (mempoolResult.rowCount > 0) { return { found: true, - result: { status: mempoolResult.rows[0].status, index_block_hash: Buffer.from([]) }, + result: { + status: mempoolResult.rows[0].status, + index_block_hash: Buffer.from([]), + microblock_hash: Buffer.from([]), + }, }; } return { found: false } as const;