Skip to content

Commit

Permalink
fix: inscriptions index etag
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr committed Jul 18, 2023
1 parent 39cf5bd commit 05456ca
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
1 change: 1 addition & 0 deletions migrations/1676395230930_inscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,5 @@ export function up(pgm: MigrationBuilder): void {
pgm.createIndex('inscriptions', ['sat_ordinal']);
pgm.createIndex('inscriptions', ['sat_rarity']);
pgm.createIndex('inscriptions', ['sat_coinbase_height']);
pgm.createIndex('inscriptions', [{ name: 'updated_at', sort: 'DESC' }]);
}
14 changes: 7 additions & 7 deletions src/api/util/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { InscriptionIdParamCType, InscriptionNumberParamCType } from '../schemas
import { logger } from '@hirosystems/api-toolkit';

export enum ETagType {
inscriptionTransfers,
inscriptionsIndex,
inscription,
inscriptionsPerBlock,
}
Expand All @@ -24,7 +24,7 @@ export async function handleInscriptionTransfersCache(
request: FastifyRequest,
reply: FastifyReply
) {
return handleCache(ETagType.inscriptionTransfers, request, reply);
return handleCache(ETagType.inscriptionsIndex, request, reply);
}

export async function handleInscriptionsPerBlockCache(
Expand All @@ -41,8 +41,8 @@ async function handleCache(type: ETagType, request: FastifyRequest, reply: Fasti
case ETagType.inscription:
etag = await getInscriptionLocationEtag(request);
break;
case ETagType.inscriptionTransfers:
etag = await getInscriptionTransfersEtag(request);
case ETagType.inscriptionsIndex:
etag = await getInscriptionsIndexEtag(request);
break;
case ETagType.inscriptionsPerBlock:
etag = await request.server.db.getInscriptionsPerBlockETag();
Expand Down Expand Up @@ -87,13 +87,13 @@ async function getInscriptionLocationEtag(request: FastifyRequest): Promise<stri
}

/**
* Get an ETag based on the last state of inscription transfers.
* Get an ETag based on the last state of all inscriptions.
* @param request - Fastify request
* @returns ETag string
*/
async function getInscriptionTransfersEtag(request: FastifyRequest): Promise<string | undefined> {
async function getInscriptionsIndexEtag(request: FastifyRequest): Promise<string | undefined> {
try {
return await request.server.db.getInscriptionTransfersETag();
return await request.server.db.getInscriptionsIndexETag();
} catch (error) {
return;
}
Expand Down
8 changes: 5 additions & 3 deletions src/pg/pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,11 @@ export class PgStore extends BasePgStore {
}
}

async getInscriptionTransfersETag(): Promise<string> {
const result = await this.sql<{ max: number }[]>`SELECT MAX(id) FROM locations`;
return result[0].max.toString();
async getInscriptionsIndexETag(): Promise<string> {
const result = await this.sql<{ etag: string }[]>`
SELECT date_part('epoch', MAX(updated_at))::text AS etag FROM inscriptions
`;
return result[0].etag;
}

async getInscriptionsPerBlockETag(): Promise<string> {
Expand Down
35 changes: 34 additions & 1 deletion tests/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ describe('ETag cache', () => {
});
expect(cached.statusCode).toBe(304);

// Simulate new location
// New location
const block3 = new TestChainhookPayloadBuilder()
.apply()
.block({ height: 775618 })
Expand All @@ -233,6 +233,39 @@ describe('ETag cache', () => {
headers: { 'if-none-match': etag },
});
expect(cached2.statusCode).toBe(200);
const etag2 = cached2.headers.etag;

// Upsert genesis location
const block4 = new TestChainhookPayloadBuilder()
.apply()
.block({ height: 778575 })
.transaction({ hash: '0x9f4a9b73b0713c5da01c0a47f97c6c001af9028d6bdd9e264dfacbc4e6790201' })
.inscriptionRevealed({
content_bytes: '0x48656C6C6F',
content_type: 'text/plain',
content_length: 5,
inscription_number: 7,
inscription_fee: 705,
inscription_id: '9f4a9b73b0713c5da01c0a47f97c6c001af9028d6bdd9e264dfacbc4e6790201i0',
inscription_output_value: 10000,
inscriber_address: 'bc1pscktlmn99gyzlvymvrezh6vwd0l4kg06tg5rvssw0czg8873gz5sdkteqj',
ordinal_number: 257418248345364,
ordinal_block_height: 650000,
ordinal_offset: 0,
satpoint_post_inscription:
'9f4a9b73b0713c5da01c0a47f97c6c001af9028d6bdd9e264dfacbc4e6790201:0:0',
inscription_input_index: 0,
transfers_pre_inscription: 0,
tx_index: 0,
})
.build();
await db.updateInscriptions(block4);
const cached3 = await fastify.inject({
method: 'GET',
url: '/ordinals/v1/inscriptions',
headers: { 'if-none-match': etag2 },
});
expect(cached3.statusCode).toBe(200);
});

test('inscriptions stats per block cache control', async () => {
Expand Down

0 comments on commit 05456ca

Please sign in to comment.