Skip to content

Commit

Permalink
test: db contenthash
Browse files Browse the repository at this point in the history
  • Loading branch information
pikonha committed Oct 14, 2024
1 parent 0e6f7ad commit 9586966
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 14 deletions.
64 changes: 60 additions & 4 deletions packages/client/test/db.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import {
Hex,
PrivateKeyAccount,
toHex,
stringToHex,
decodeFunctionResult,
} from 'viem'
import { assert, expect } from 'chai'
import { ApolloServer } from '@apollo/server'
Expand All @@ -61,6 +63,7 @@ import {
withSetAddr,
withSetText,
withRegisterDomain,
withSetContentHash,
} from '@blockful/gateway/src/handlers'
import {
DomainData,
Expand All @@ -70,7 +73,12 @@ import {
} from '@blockful/gateway/src/types'
import { domainResolver } from '@blockful/gateway/src/resolvers'
import { PostgresRepository } from '@blockful/gateway/src/repositories'
import { Text, Domain, Address } from '@blockful/gateway/src/entities'
import {
Text,
Domain,
Address,
Contenthash,
} from '@blockful/gateway/src/entities'
import { withSigner } from '@blockful/gateway/src/middlewares'
import {
EthereumClient,
Expand Down Expand Up @@ -190,6 +198,7 @@ function setupGateway(
withGetAddr(repo),
withSetAddr(repo, validator),
withGetContentHash(repo),
withSetContentHash(repo, validator),
)
server.makeApp('/').listen('3000')
}
Expand Down Expand Up @@ -256,7 +265,7 @@ describe('DatabaseResolver', () => {
datasource = new DataSource({
type: 'better-sqlite3',
database: './test.db',
entities: [Text, Domain, Address],
entities: [Text, Domain, Address, Contenthash],
synchronize: true,
})
repo = new PostgresRepository(await datasource.initialize())
Expand All @@ -267,7 +276,7 @@ describe('DatabaseResolver', () => {
})

beforeEach(async () => {
for (const entity of ['Text', 'Address', 'Domain']) {
for (const entity of ['Text', 'Address', 'Domain', 'Contenthash']) {
await datasource.getRepository(entity).clear()
}
})
Expand Down Expand Up @@ -716,6 +725,45 @@ describe('DatabaseResolver', () => {
const name = normalize('l1domain.eth')
const node = namehash(name)

it('should set and read contenthash from database', async () => {
const contentHash =
'ipns://k51qzi5uqu5dgccx524mfjv7znyfsa6g013o6v4yvis9dxnrjbwojc62pt0450'

const response = await offchainWriting({
name,
functionName: 'setContenthash',
abi: abiDBResolver,
args: [node, stringToHex(contentHash)],
universalResolverAddress,
signer: owner,
})

expect(response?.status).equal(200)

const [r] = (await client.readContract({
address: universalResolverAddress as Hex,
functionName: 'resolve',
abi: abiUniversalResolver,
args: [
toHex(packetToBytes(name)),
encodeFunctionData({
abi: abiDBResolver,
functionName: 'contenthash',
args: [node],
}),
],
})) as [Hex]

const actual = decodeFunctionResult({
abi: abiDBResolver,
functionName: 'contenthash',
data: r,
args: [node],
})

expect(actual).equal(stringToHex(contentHash))
})

it('should read the avatar from database', async () => {
const text = new Text()
text.domain = node
Expand Down Expand Up @@ -1151,6 +1199,14 @@ describe('DatabaseResolver', () => {
a.updatedAt = new Date()
await datasource.manager.save(a)

const ch = new Contenthash()
ch.domain = d.node
ch.contenthash =
'ipns://k51qzi5uqu5dgccx524mfjv7znyfsa6g013o6v4yvis9dxnrjbwojc62pt0450'
ch.resolver = '0x1resolver'
ch.resolverVersion = '1'
await datasource.manager.save(ch)

const response = await server.executeOperation({
query: `query Domain($name: String!) {
domain(name: $name) {
Expand Down Expand Up @@ -1218,7 +1274,7 @@ describe('DatabaseResolver', () => {
expect(subdomain.resolver).to.have.property('addr', '0x1')
expect(subdomain.resolver).to.have.property(
'contentHash',
d.contenthash,
ch.contenthash,
)
expect(subdomain.resolver.texts).to.eql([
{ key: t.key, value: t.value },
Expand Down
8 changes: 4 additions & 4 deletions packages/gateway/src/repositories/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ export class PostgresRepository {

if (contenthash)
return {
value: contenthash.contenthash_contenthash as string,
ttl: contenthash.domain_ttl,
value: contenthash?.contenthash_contenthash,
ttl: contenthash?.domain_ttl || 600,
}
}

Expand Down Expand Up @@ -217,7 +217,7 @@ export class PostgresRepository {
.select(['addr.address', 'domain.ttl'])
.getRawOne()

if (addr) return { value: addr.addr_address, ttl: addr.domain_ttl || 300 } // default ttl value
if (addr) return { value: addr.addr_address, ttl: addr.domain_ttl || 600 }
}

async getAddresses({ node }: NodeProps): Promise<Address[]> {
Expand Down Expand Up @@ -268,7 +268,7 @@ export class PostgresRepository {
.select(['text.value', 'domain.ttl'])
.getRawOne()

if (text) return { value: text.text_value, ttl: text.domain_ttl || 300 } // default ttl value
if (text) return { value: text.text_value, ttl: text.domain_ttl || 600 }
}

async setPubkey({ node, x, y, resolver, resolverVersion }: SetPubkeyProps) {
Expand Down
4 changes: 2 additions & 2 deletions packages/gateway/test/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ describe('Gateway API', () => {
server.add(serverAbi, withSetContentHash(repo, validator))
const app = server.makeApp('/')

const args = [domain.node, contenthash]
const args = [domain.node, stringToHex(contenthash)]
const data = encodeFunctionData({
abi,
functionName: 'setContenthash',
Expand Down Expand Up @@ -495,7 +495,7 @@ describe('Gateway API', () => {
functionName: 'contenthash',
data,
}),
).toEqual(expected)
).toEqual(stringToHex(expected))
expect(parseInt(ttl.toString())).toBeCloseTo(
parseInt(formatTTL(domain.ttl)),
)
Expand Down
16 changes: 12 additions & 4 deletions packages/gateway/test/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ import {
assert,
} from 'vitest'
import * as ccip from '@blockful/ccip-server'
import { Hex, encodeFunctionData, pad, parseAbi, toHex } from 'viem'
import {
Hex,
encodeFunctionData,
pad,
parseAbi,
stringToHex,
toHex,
} from 'viem'
import { namehash } from 'viem/ens'
import { generatePrivateKey, privateKeyToAddress } from 'viem/accounts'

Expand Down Expand Up @@ -279,7 +286,8 @@ describe('Gateway Database', () => {
await datasource.manager.save(domain)

const contenthash =
'0x1e583a944ea6750b0904b8f95a72f593f070ecac52e8d5bc959fa38d745a3909' // blockful
'ipns://k51qzi5uqu5dgccx524mfjv7znyfsa6g013o6v4yvis9dxnrjbwojc62pt0450'

const server = new ccip.Server()
server.add(abi, withSetContentHash(repo, validator))
const result = await doCall({
Expand All @@ -288,7 +296,7 @@ describe('Gateway Database', () => {
sender: TEST_ADDRESS,
method: 'setContenthash',
pvtKey,
args: [domain.node, contenthash],
args: [domain.node, stringToHex(contenthash)],
})

expect(result.data.length).toEqual(0)
Expand Down Expand Up @@ -331,7 +339,7 @@ describe('Gateway Database', () => {

expect(result.data.length).toEqual(1)
const [value] = result.data
expect(value).toEqual(expected)
expect(value).toEqual(toHex(expected))
expect(parseInt(result.ttl!)).toBeCloseTo(parseInt(formatTTL(300)))
})

Expand Down

0 comments on commit 9586966

Please sign in to comment.