Skip to content

Commit

Permalink
feat: add microblock_hash to etag
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr committed Jul 6, 2022
1 parent d9cff9c commit b60336c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
9 changes: 7 additions & 2 deletions src/api/controllers/cache-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(':');
}
}
8 changes: 7 additions & 1 deletion src/datastore/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;
getSubdomainResolver(name: { name: string }): Promise<FoundOrNot<string>>;
Expand Down Expand Up @@ -861,7 +867,7 @@ export interface DataStore extends DataStoreEventEmitter {

getRawTx(txId: string): Promise<FoundOrNot<RawTxQueryResult>>;

getTxStatus(txId: string): Promise<FoundOrNot<{ status: DbTxStatus; index_block_hash: Buffer }>>;
getTxStatus(txId: string): Promise<FoundOrNot<DbTxGlobalStatus>>;

/**
* Returns a list of NFTs owned by the given principal filtered by optional `asset_identifiers`,
Expand Down
16 changes: 10 additions & 6 deletions src/datastore/postgres-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ import {
NftHoldingInfoWithTxMetadata,
NftEventWithTxMetadata,
DbAssetEventTypeId,
DbTxGlobalStatus,
} from './common';
import {
AddressTokenOfferingLocked,
Expand Down Expand Up @@ -4055,12 +4056,10 @@ export class PgDataStore
});
}

async getTxStatus(
txId: string
): Promise<FoundOrNot<{ status: DbTxStatus; index_block_hash: Buffer }>> {
async getTxStatus(txId: string): Promise<FoundOrNot<DbTxGlobalStatus>> {
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<DbTxGlobalStatus>(
`SELECT status, index_block_hash, microblock_hash
FROM txs
WHERE tx_id = $1 AND canonical = TRUE AND microblock_canonical = TRUE`,
[hexToBuffer(txId)]
Expand All @@ -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,
},
};
}
Expand All @@ -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;
Expand Down

0 comments on commit b60336c

Please sign in to comment.