Skip to content

Commit 101922b

Browse files
authored
fix: return the latest name by address #714
1 parent 37adcc7 commit 101922b

File tree

3 files changed

+106
-23
lines changed

3 files changed

+106
-23
lines changed

src/api/routes/bns/addresses.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function createBnsAddressesRouter(db: DataStore): RouterWithAsync {
2323
if (namesByAddress.found) {
2424
res.json({ names: namesByAddress.result });
2525
} else {
26-
res.json([]);
26+
res.json({ names: [] });
2727
}
2828
});
2929

src/datastore/postgres-store.ts

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5620,30 +5620,47 @@ export class PgDataStore
56205620
}): Promise<FoundOrNot<string[]>> {
56215621
const queryResult = await this.queryTx(async client => {
56225622
const maxBlockHeight = await this.getMaxBlockHeight(client, { includeUnanchored });
5623-
return await client.query<{ name: string }>(
5623+
const query = await client.query<{ name: string }>(
56245624
`
5625-
SELECT name FROM (
5626-
(
5627-
SELECT DISTINCT ON (name) name, registered_at as block_height, tx_index
5628-
FROM names
5629-
WHERE address = $1
5630-
AND registered_at <= $2
5631-
AND canonical = true AND microblock_canonical = true
5632-
ORDER BY name, registered_at DESC, tx_index DESC
5633-
)
5634-
UNION ALL (
5635-
SELECT DISTINCT ON (fully_qualified_subdomain) fully_qualified_subdomain as name, block_height, tx_index
5636-
FROM subdomains
5637-
WHERE owner = $1
5638-
AND block_height <= $2
5639-
AND canonical = true AND microblock_canonical = true
5640-
ORDER BY fully_qualified_subdomain, block_height DESC, tx_index DESC
5641-
)
5642-
) results
5643-
ORDER BY block_height DESC, tx_index DESC
5625+
WITH address_names AS(
5626+
(
5627+
SELECT name
5628+
FROM names
5629+
WHERE address = $1
5630+
AND registered_at <= $2
5631+
AND canonical = true AND microblock_canonical = true
5632+
)
5633+
UNION ALL (
5634+
SELECT DISTINCT ON (fully_qualified_subdomain) fully_qualified_subdomain as name
5635+
FROM subdomains
5636+
WHERE owner = $1
5637+
AND block_height <= $2
5638+
AND canonical = true AND microblock_canonical = true
5639+
)),
5640+
5641+
latest_names AS(
5642+
(
5643+
SELECT DISTINCT ON (names.name) names.name, address, registered_at as block_height, tx_index
5644+
FROM names, address_names
5645+
WHERE address_names.name = names.name
5646+
AND canonical = true AND microblock_canonical = true
5647+
ORDER BY names.name, registered_at DESC, tx_index DESC
5648+
)
5649+
UNION ALL(
5650+
SELECT DISTINCT ON (fully_qualified_subdomain) fully_qualified_subdomain as name, owner as address, block_height, tx_index
5651+
FROM subdomains, address_names
5652+
WHERE fully_qualified_subdomain = address_names.name
5653+
AND canonical = true AND microblock_canonical = true
5654+
ORDER BY fully_qualified_subdomain, block_height DESC, tx_index DESC
5655+
))
5656+
5657+
SELECT name from latest_names
5658+
WHERE address = $1
5659+
ORDER BY name, block_height DESC, tx_index DESC
56445660
`,
56455661
[address, maxBlockHeight]
56465662
);
5663+
return query;
56475664
});
56485665

56495666
if (queryResult.rowCount > 0) {

src/tests-bns/api.ts

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ describe('BNS API tests', () => {
249249
canonical: true,
250250
tx_id: '',
251251
tx_index: 0,
252+
status: 'name_register'
252253
};
253254
await db.updateNames(client, {
254255
index_block_hash: dbBlock.index_block_hash,
@@ -266,7 +267,7 @@ describe('BNS API tests', () => {
266267
const subdomain: DbBnsSubdomain = {
267268
namespace_id: 'blockstack',
268269
name: 'id.blockstack',
269-
fully_qualified_subdomain: 'address_test.id.blockstack',
270+
fully_qualified_subdomain: 'zone_test.id.blockstack',
270271
resolver: 'https://registrar.blockstack.org',
271272
owner: 'STRYYQQ9M8KAF4NS7WNZQYY59X93XEKR31JP64CP',
272273
zonefile: 'test',
@@ -418,6 +419,71 @@ describe('BNS API tests', () => {
418419
expect(query2.type).toBe('application/json');
419420
});
420421

422+
test('Success names transfer', async () => {
423+
const blockchain = 'stacks';
424+
const address = 'ST1HB1T8WRNBYB0Y3T7WXZS38NKKPTBR3EG9EPJKA';
425+
const name = 'test-name1';
426+
427+
const dbName: DbBnsName = {
428+
name: name,
429+
address: address,
430+
namespace_id: 'test',
431+
expire_block: 10000,
432+
zonefile: 'test-zone-file',
433+
zonefile_hash: 'zonefileHash',
434+
registered_at: 0,
435+
canonical: true,
436+
tx_id: '',
437+
tx_index: 0,
438+
status: 'name-register'
439+
};
440+
await db.updateNames(client, {
441+
index_block_hash: dbBlock.index_block_hash,
442+
parent_index_block_hash: dbBlock.parent_index_block_hash,
443+
microblock_hash: '',
444+
microblock_sequence: I32_MAX,
445+
microblock_canonical: true,
446+
}, dbName);
447+
448+
const query1 = await supertest(api.server).get(`/v1/addresses/${blockchain}/${address}`);
449+
expect(query1.status).toBe(200);
450+
expect(query1.body.names[0]).toBe(name);
451+
expect(query1.type).toBe('application/json');
452+
453+
const address1 = 'ST1HB1T8WRNBYB0Y3T7WXZS38NKKPTBR3EG9EPJKT';
454+
455+
const dbNameTransfer: DbBnsName = {
456+
name: name,
457+
address: address1,
458+
namespace_id: 'test',
459+
expire_block: 10000,
460+
zonefile: 'test-zone-file',
461+
zonefile_hash: 'zonefileHash',
462+
registered_at: 1,
463+
canonical: true,
464+
tx_id: '',
465+
tx_index: 0,
466+
status: 'name-transfer'
467+
};
468+
await db.updateNames(client, {
469+
index_block_hash: dbBlock.index_block_hash,
470+
parent_index_block_hash: dbBlock.parent_index_block_hash,
471+
microblock_hash: '',
472+
microblock_sequence: I32_MAX,
473+
microblock_canonical: true,
474+
}, dbNameTransfer);
475+
476+
const query2 = await supertest(api.server).get(`/v1/addresses/${blockchain}/${address1}`);
477+
expect(query2.status).toBe(200);
478+
expect(query2.type).toBe('application/json');
479+
expect(query2.body.names[0]).toBe(name);
480+
481+
const query3 = await supertest(api.server).get(`/v1/addresses/${blockchain}/${address}`);
482+
expect(query3.status).toBe(200);
483+
expect(query3.type).toBe('application/json');
484+
expect(query3.body.names.length).toBe(0);
485+
});
486+
421487
test('Fail names by address - Blockchain not support', async () => {
422488
const query1 = await supertest(api.server).get(`/v1/addresses/invalid/test`);
423489
expect(query1.status).toBe(404);
@@ -458,7 +524,7 @@ describe('BNS API tests', () => {
458524
const subdomain: DbBnsSubdomain = {
459525
namespace_id: 'blockstack',
460526
name: 'id.blockstack',
461-
fully_qualified_subdomain: 'address_test.id.blockstack',
527+
fully_qualified_subdomain: 'zonefile_test.id.blockstack',
462528
resolver: 'https://registrar.blockstack.org',
463529
owner: address,
464530
zonefile: 'test',

0 commit comments

Comments
 (0)