Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fix changelog grouping bug & remove changelogSummary on BE #100

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions packages/backend/src/api/controllers/ChangelogController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,12 @@ export class ChangelogController {
(x) => x.blockNumber === blockNumber,
)
assert(fullChanges.length > 0, 'Timestamp and txHash not found')
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const firstChange = fullChanges[0]!
changelog.push({
blockNumber,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
timestamp: fullChanges[0]!.timestamp,
possibleTxHashes: fullChanges
.map((x) => x.txHash)
.filter((x, i, a) => a.indexOf(x) === i),
timestamp: firstChange.timestamp,
possibleTxHashes: firstChange.txHashes,
changes,
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@ import {
Bytes,
bytes32ToAddress,
ChainId,
ChangelogSummary,
DiscoveryApi,
EthereumAddress,
getChainIdFromEndpointId,
RemoteChain,
} from '@lz/libs'

import {
ChangelogRepository,
ChangelogSummaryRecord,
} from '../../../peripherals/database/ChangelogRepository'
import { CurrentDiscoveryRepository } from '../../../peripherals/database/CurrentDiscoveryRepository'
import {
getAddressFromValue,
Expand All @@ -28,19 +23,16 @@ import {
export class DiscoveryController {
constructor(
private readonly currentDiscoveryRepository: CurrentDiscoveryRepository,
private readonly changelogRepository: ChangelogRepository,
) {}

async getDiscovery(chainId: ChainId): Promise<DiscoveryApi | undefined> {
const discovery = await this.currentDiscoveryRepository.find(chainId)
const changelogSummary =
await this.changelogRepository.getChangesSummary(chainId)

if (!discovery) {
return
}

return toDiscoveryApi(discovery.discoveryOutput, changelogSummary)
return toDiscoveryApi(discovery.discoveryOutput)
}

async getRawDiscovery(
Expand All @@ -56,10 +48,7 @@ export class DiscoveryController {
}
}

function toDiscoveryApi(
discoveryOutput: DiscoveryOutput,
changelogSummary: ChangelogSummaryRecord[],
): DiscoveryApi {
function toDiscoveryApi(discoveryOutput: DiscoveryOutput): DiscoveryApi {
const { contracts, blockNumber, eoas } = discoveryOutput

const endpoint = contracts.find((c) => c.name === 'Endpoint')
Expand All @@ -76,17 +65,16 @@ function toDiscoveryApi(
return {
blockNumber,
contracts: {
endpoint: getEndpointContract(discoveryOutput, changelogSummary),
ulnV2: getUlnV2Contract(discoveryOutput, changelogSummary),
lzMultisig: getLzMultisig(discoveryOutput, changelogSummary),
endpoint: getEndpointContract(discoveryOutput),
ulnV2: getUlnV2Contract(discoveryOutput),
lzMultisig: getLzMultisig(discoveryOutput),
},
addressInfo,
}
}

function getEndpointContract(
discoveryOutput: DiscoveryOutput,
changelogSummary: ChangelogSummaryRecord[],
): DiscoveryApi['contracts']['endpoint'] {
const endpoint = getContractByName('Endpoint', discoveryOutput)

Expand All @@ -99,10 +87,6 @@ function getEndpointContract(

return {
name: 'Endpoint',
changelogSummary: getChangelogSummaryApi(
changelogSummary,
endpoint.address,
),
address: endpoint.address,
owner: getAddressFromValue(endpoint, 'owner'),
defaultSendLibrary: getAddressFromValue(endpoint, 'defaultSendLibrary'),
Expand All @@ -116,13 +100,11 @@ function getEndpointContract(

function getUlnV2Contract(
discoveryOutput: DiscoveryOutput,
changelogSummary: ChangelogSummaryRecord[],
): DiscoveryApi['contracts']['ulnV2'] {
const ulnV2 = getContractByName('UltraLightNodeV2', discoveryOutput)

return {
name: 'UltraLightNodeV2',
changelogSummary: getChangelogSummaryApi(changelogSummary, ulnV2.address),
address: ulnV2.address,
owner: getAddressFromValue(ulnV2, 'owner'),
treasuryContract: getAddressFromValue(ulnV2, 'treasuryContract'),
Expand Down Expand Up @@ -245,7 +227,6 @@ function getRemoteChains(ulnV2: ContractParameters): RemoteChain[] {

function getLzMultisig(
discoveryOutput: DiscoveryOutput,
changelogSummary: ChangelogSummaryRecord[],
): DiscoveryApi['contracts']['lzMultisig'] | null {
try {
const lzMultisig = getContractByName('LayerZero Multisig', discoveryOutput)
Expand All @@ -259,10 +240,6 @@ function getLzMultisig(

return {
name: 'LayerZero Multisig',
changelogSummary: getChangelogSummaryApi(
changelogSummary,
lzMultisig.address,
),
address: lzMultisig.address,
owners,
threshold: getContractValue(lzMultisig, 'getThreshold'),
Expand All @@ -271,16 +248,3 @@ function getLzMultisig(
return null
}
}

function getChangelogSummaryApi(
changelogSummaries: ChangelogSummaryRecord[],
address: EthereumAddress,
): ChangelogSummary {
const changelogSummary = changelogSummaries.find((c) => c.address === address)

assert(changelogSummary, 'Changelog summary not found')
return {
count: changelogSummary.count,
lastChangeTimestamp: changelogSummary.lastChangeTimestamp,
}
}
1 change: 0 additions & 1 deletion packages/backend/src/modules/DiscoveryModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ export function createDiscoveryModule({

const discoveryController = new DiscoveryController(
currentDiscoveryRepository,
changelogRepository,
)
const changelogController = new ChangelogController(
changelogRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,71 +59,6 @@ describe(ChangelogRepository.name, () => {
})
})

describe(ChangelogRepository.prototype.getChangesSummary.name, () => {
it('finds changes summary', async () => {
const expectedTimestamp = new UnixTime(1000)
await blockRepo.addMany([
{
blockNumber: record1.blockNumber,
chainId: record1.chainId,
timestamp: expectedTimestamp,
blockHash: Hash256.random(),
},
{
blockNumber: record2.blockNumber,
chainId: record2.chainId,
timestamp: expectedTimestamp,
blockHash: Hash256.random(),
},
])
await repository.addMany([record1, record2])
const result = await repository.getChangesSummary(chainId)

expect(sortArray(result)).toEqual(
sortArray([
{
count: 1,
lastChangeTimestamp: expectedTimestamp,
address: record1.targetAddress,
},
{
count: 1,
lastChangeTimestamp: expectedTimestamp,
address: record2.targetAddress,
},
]),
)
})

it('does not count changes in the same block twice', async () => {
const expectedTimestamp = new UnixTime(1000)
const blockRepo = new BlockNumberRepository(database, Logger.SILENT)
await blockRepo.add({
blockNumber: record2.blockNumber,
chainId: record2.chainId,
timestamp: expectedTimestamp,
blockHash: Hash256.random(),
})
await repository.addMany([record1, record2, record2])
const result = await repository.getChangesSummary(chainId)

expect(sortArray(result)).toEqual(
sortArray([
{
count: 1,
lastChangeTimestamp: expectedTimestamp,
address: record1.targetAddress,
},
{
count: 1,
lastChangeTimestamp: expectedTimestamp,
address: record2.targetAddress,
},
]),
)
})
})

describe(ChangelogRepository.prototype.getFullChangelog.name, () => {
it('finds full changelog', async () => {
const expectedTimestamp = new UnixTime(1000)
Expand All @@ -148,11 +83,11 @@ describe(ChangelogRepository.name, () => {
)

expect(result).toEqual([
{ ...record1, timestamp: expectedTimestamp, txHash },
{ ...record1, timestamp: expectedTimestamp, txHashes: [txHash] },
])
})

it('returns array if multiple events in the same block', async () => {
it('returns multiple tx_hashes if multiple events in the same block', async () => {
const expectedTimestamp = new UnixTime(1000)
await blockRepo.add({
blockNumber: record1.blockNumber,
Expand All @@ -178,8 +113,11 @@ describe(ChangelogRepository.name, () => {
)

expect(result).toEqualUnsorted([
{ ...record1, timestamp: expectedTimestamp, txHash: event1.txHash },
{ ...record1, timestamp: expectedTimestamp, txHash: event2.txHash },
{
...record1,
timestamp: expectedTimestamp,
txHashes: [event1.txHash, event2.txHash],
},
])
})
})
Expand All @@ -206,9 +144,3 @@ describe(ChangelogRepository.name, () => {
})
})
})

function sortArray<T extends { address: EthereumAddress }>(arr: T[]): T[] {
return arr.sort((a, b) =>
a.address.toString().localeCompare(b.address.toString()),
)
}
66 changes: 20 additions & 46 deletions packages/backend/src/peripherals/database/ChangelogRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,13 @@ import { Database } from './shared/Database'

export type ChangelogRecord = ChangelogEntry

interface ChangelogSummaryRow {
target_address: string
unix_timestamp: Date
count: string
}
export interface ChangelogSummaryRecord {
address: EthereumAddress
count: number
lastChangeTimestamp: UnixTime
}
interface FullChangelogRow extends ChangelogRow {
unix_timestamp: Date
tx_hash: string
tx_hashes: string[]
}
export interface FullChangelogRecord extends ChangelogRecord {
timestamp: UnixTime
txHash: Hash256
txHashes: Hash256[]
}

export class ChangelogRepository extends BaseRepository {
Expand All @@ -53,43 +43,12 @@ export class ChangelogRepository extends BaseRepository {
return rows.map(toRecord)
}

async getChangesSummary(chainId: ChainId): Promise<ChangelogSummaryRecord[]> {
const knex = await this.knex()

const summaryRows = await knex
.with('changes', async (qb) => {
await qb
.select(
'target_address',
knex.raw('max(block_number) as max_block'),
// todo: we should count distinct transactions, not blocks
knex.raw('count(distinct block_number) as count'),
)
.from('changelog_entries')
.where('chain_id', chainId)
.groupBy('target_address')
})
.select<ChangelogSummaryRow[]>(
'target_address',
'unix_timestamp',
'count',
)
.from('changes')
.join('block_numbers', 'block_numbers.block_number', 'changes.max_block')

return summaryRows.map((row) => ({
address: EthereumAddress(row.target_address),
count: Number(row.count),
lastChangeTimestamp: UnixTime.fromDate(row.unix_timestamp),
}))
}

async getFullChangelog(
chainId: ChainId,
address: EthereumAddress,
): Promise<FullChangelogRecord[]> {
const knex = await this.knex()
const rows = await knex
const rows: FullChangelogRow[] = await knex
.with('changes', async (qb) => {
await qb
.select('c.*', 'b.unix_timestamp')
Expand All @@ -98,14 +57,29 @@ export class ChangelogRepository extends BaseRepository {
.andWhere('target_address', address.toString())
.join('block_numbers as b', 'b.block_number', 'c.block_number')
})
.select<FullChangelogRow[]>('e.tx_hash', 'changes.*')
.select<FullChangelogRow[]>(
knex.raw('ARRAY_AGG(e.tx_hash) AS tx_hashes'),
'changes.*',
)
.from('changes')
.leftJoin('events as e', 'e.block_number', 'changes.block_number')
.groupBy(
'changes.chain_id',
'changes.block_number',
'changes.target_address',
'changes.target_name',
'changes.parameter_name',
'changes.parameter_path',
'changes.modification_type',
'changes.previous_value',
'changes.current_value',
'changes.unix_timestamp',
)

return rows.map((r) => ({
...toRecord(r),
timestamp: UnixTime.fromDate(r.unix_timestamp),
txHash: Hash256(r.tx_hash),
txHashes: r.tx_hashes.map(Hash256),
}))
}

Expand Down
Loading