Skip to content

Commit

Permalink
test: fix ttl tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pikonha committed Jun 13, 2024
1 parent 342e773 commit f57de25
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 26 deletions.
2 changes: 1 addition & 1 deletion packages/gateway/src/abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export const abi: string[] = [
'function addr(bytes32 node, uint coinType) view returns (bytes)',
'function contenthash(bytes32 node) view returns (bytes memory)',
'function setContenthash(bytes32 node, bytes calldata contenthash)',
'function getStorageSlots(address addr, bytes32[] memory commands, bytes[] memory constants) external view returns(bytes memory witness)',
'function getStorageSlots(address addr, bytes32[] memory commands, bytes[] memory) external view returns(bytes memory witness)',
'function register(bytes32 node, uint32 ttl)',
]
2 changes: 1 addition & 1 deletion packages/gateway/src/handlers/slots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function withGetStorageSlot<T extends ProvableBlock>(
proofService: IProofService<T>,
): ccip.HandlerDescription {
return {
type: 'getStorageSlots(address addr, bytes32[] memory commands, bytes[] memory constants) external view returns(bytes memory witness)',
type: 'getStorageSlots(address addr, bytes32[] memory commands, bytes[] memory) external view returns (bytes memory witness)',
func: async ([addr, commands, constants]) => {
if (addr === zeroAddress) {
return { error: { message: 'Invalid address', status: 400 } }
Expand Down
10 changes: 6 additions & 4 deletions packages/gateway/test/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
import { InMemoryRepository } from '../src/repositories'
import { withSigner, makeMessageHash, withLogger } from '../src/middlewares'
import { Domain } from '../src/entities'
import { OwnershipValidator } from '../src/services'
import { OwnershipValidator, formatTTL } from '../src/services'

const TEST_ADDRESS = '0x1234567890123456789012345678901234567890'
const abi = parseAbi(serverAbi)
Expand Down Expand Up @@ -151,7 +151,9 @@ describe('Gateway API', () => {
data,
}),
).toEqual(domain.contenthash)
expect(ttl).toEqual(BigInt(domain.ttl))
expect(parseInt(ttl.toString())).toBeCloseTo(
parseInt(formatTTL(domain.ttl)),
)
})
})

Expand Down Expand Up @@ -364,7 +366,7 @@ describe('Gateway API', () => {
repo.setAddresses([
{
domain,
coin: 60,
coin: '60',
address: '0x',
},
])
Expand Down Expand Up @@ -412,7 +414,7 @@ describe('Gateway API', () => {
repo.setAddresses([
{
domain,
coin: 60,
coin: '60',
address,
},
])
Expand Down
39 changes: 20 additions & 19 deletions packages/gateway/test/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
} from '../src/handlers'
import { PostgresRepository } from '../src/repositories'
import { Address, Text, Domain } from '../src/entities'
import { OwnershipValidator } from '../src/services'
import { OwnershipValidator, formatTTL } from '../src/services'
import { generatePrivateKey, privateKeyToAddress } from 'viem/accounts'

const TEST_ADDRESS = '0x1234567890123456789012345678901234567890'
Expand Down Expand Up @@ -63,15 +63,15 @@ describe('Gateway Database', () => {
sender: TEST_ADDRESS,
method: 'register',
pvtKey,
args: [node, 2000],
args: [node, 300],
})

const d = await datasource.getRepository(Domain).findOneBy({
node,
owner,
})
expect(d).not.toBeNull()
expect(d?.ttl).toEqual(2000)
expect(d?.ttl).toEqual(300)
})

// Register a domain 'public.eth' with a given TTL, then attempt to register the same domain with a different TTL
Expand All @@ -80,7 +80,7 @@ describe('Gateway Database', () => {
const owner = privateKeyToAddress(pvtKey)
const domain = new Domain()
domain.node = namehash('public.eth')
domain.ttl = 2000
domain.ttl = 300
domain.owner = owner
await datasource.manager.save(domain)

Expand All @@ -92,7 +92,7 @@ describe('Gateway Database', () => {
sender: TEST_ADDRESS,
method: 'register',
pvtKey,
args: [domain.node, 3000],
args: [domain.node, 400],
})

expect(result.data.length).toEqual(0)
Expand All @@ -110,7 +110,7 @@ describe('Gateway Database', () => {
const pvtKey = generatePrivateKey()
const domain = new Domain()
domain.node = namehash('public.eth')
domain.ttl = 2000
domain.ttl = 300
domain.owner = privateKeyToAddress(pvtKey)
await datasource.manager.save(domain)

Expand Down Expand Up @@ -142,7 +142,7 @@ describe('Gateway Database', () => {
it('should query contenthash', async () => {
const domain = new Domain()
domain.node = namehash('public.eth')
domain.ttl = 2000
domain.ttl = 300
domain.owner = privateKeyToAddress(generatePrivateKey())
await datasource.manager.save(domain)
const content =
Expand All @@ -163,7 +163,7 @@ describe('Gateway Database', () => {
expect(result.data.length).toEqual(1)
const [value] = result.data
expect(value).toEqual(content)
expect(result.ttl).toEqual(domain.ttl)
expect(parseInt(result.ttl!)).toBeCloseTo(parseInt(formatTTL(domain.ttl)))
})

// Attempt to set a content hash for an invalid domain
Expand All @@ -190,7 +190,7 @@ describe('Gateway Database', () => {
beforeEach(async () => {
domain = new Domain()
domain.node = namehash('public.eth')
domain.ttl = 2000
domain.ttl = 300
pvtKey = generatePrivateKey()
domain.owner = privateKeyToAddress(pvtKey)
domain = await datasource.manager.save(domain)
Expand Down Expand Up @@ -333,7 +333,10 @@ describe('Gateway Database', () => {
expect(result.data.length).toEqual(1)
const [avatar] = result.data
expect(avatar).toEqual('blockful.png')
expect(result.ttl).toEqual(domain.ttl)
expect(parseInt(result.ttl!)).toBeCloseTo(
parseInt(formatTTL(domain.ttl)),
2,
)
})
})

Expand All @@ -343,7 +346,7 @@ describe('Gateway Database', () => {
beforeEach(async () => {
domain = new Domain()
domain.node = namehash('public.eth')
domain.ttl = 2000
domain.ttl = 300
pvtKey = generatePrivateKey()
domain.owner = privateKeyToAddress(pvtKey)
await datasource.manager.save(domain)
Expand Down Expand Up @@ -376,18 +379,16 @@ describe('Gateway Database', () => {
expect(actual[0]?.address).toEqual(
'0x1234567890123456789012345678901234567890',
)
expect(actual[0]?.coin).toEqual(60)
expect(actual[0]?.coin).toEqual('60')
})

// TODO: test multicoin read/write when issue is solved: https://github.com/smartcontractkit/ccip-read/issues/32

// Register a domain, then set an Ethereum address for it
it('should allow multiple addresses for same owner and different coins', async () => {
const server = new ccip.Server()
server.add(abi, withSetAddr(repo, validator))
const addr = new Address()
addr.address = TEST_ADDRESS
addr.coin = 1
addr.coin = '1'
addr.domain = domain
await datasource.manager.save(addr)

Expand All @@ -411,11 +412,11 @@ describe('Gateway Database', () => {
expect(actual[0]?.address).toEqual(
'0x1234567890123456789012345678901234567890',
)
expect(actual[0]?.coin).toEqual(1)
expect(actual[0]?.coin).toEqual('1')
expect(actual[1]?.address).toEqual(
'0x1234567890123456789012345678901234567999',
)
expect(actual[1]?.coin).toEqual(60)
expect(actual[1]?.coin).toEqual('60')
})

// Attempt to set an Ethereum address using an unauthorized private key
Expand Down Expand Up @@ -447,7 +448,7 @@ describe('Gateway Database', () => {
// Register a domain with an Ethereum address, then query for it
it('should query ethereum address', async () => {
const addr = new Address()
addr.coin = 60
addr.coin = '60'
addr.address = '0x1234567890123456789012345678901234567890'
addr.domain = domain
await datasource.manager.save(addr)
Expand All @@ -465,7 +466,7 @@ describe('Gateway Database', () => {
expect(result.data.length).toEqual(1)
const [value] = result.data
expect(value).toEqual('0x1234567890123456789012345678901234567890')
expect(result.ttl).toEqual(domain.ttl)
expect(parseInt(result.ttl!)).toBeCloseTo(parseInt(formatTTL(domain.ttl)))
})
})
})
2 changes: 1 addition & 1 deletion packages/gateway/test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export async function doCall({
method: string
pvtKey?: `0x${string}`
args: unknown[]
}): Promise<{ data: Array<unknown>; ttl?: bigint; error?: Error }> {
}): Promise<{ data: Array<unknown>; ttl?: string; error?: Error }> {
const iface = parseAbi(abi)
const func = getAbiItem({ abi: iface, name: method }) as AbiFunction
if (!func) {
Expand Down

0 comments on commit f57de25

Please sign in to comment.