From f1aa9a409dcff443163df04c06edd265d0990f7b Mon Sep 17 00:00:00 2001 From: oXtxNt9U <120286271+oXtxNt9U@users.noreply.github.com> Date: Thu, 5 Dec 2024 10:37:10 +0900 Subject: [PATCH] read usernames from contract on restore --- packages/api-sync/package.json | 5 +++- packages/api-sync/source/restore.ts | 41 +++++++++++++++++++++++++++-- pnpm-lock.yaml | 9 +++++++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/packages/api-sync/package.json b/packages/api-sync/package.json index 5adbb1509..71963a7c0 100644 --- a/packages/api-sync/package.json +++ b/packages/api-sync/package.json @@ -25,9 +25,12 @@ "@mainsail/api-database": "workspace:*", "@mainsail/container": "workspace:*", "@mainsail/contracts": "workspace:*", + "@mainsail/evm-contracts": "workspace:*", + "@mainsail/evm-consensus": "workspace:*", "@mainsail/kernel": "workspace:*", "@mainsail/utils": "workspace:*", - "joi": "17.12.2" + "joi": "17.12.2", + "ethers": "^6.11.0" }, "devDependencies": { "uvu": "^0.5.6" diff --git a/packages/api-sync/source/restore.ts b/packages/api-sync/source/restore.ts index 737f59232..26d1ffcda 100644 --- a/packages/api-sync/source/restore.ts +++ b/packages/api-sync/source/restore.ts @@ -5,8 +5,11 @@ import { } from "@mainsail/api-database"; import { inject, injectable, tagged } from "@mainsail/container"; import { Contracts, Identifiers } from "@mainsail/contracts"; +import { UsernamesAbi } from "@mainsail/evm-contracts"; +import { Identifiers as EvmConsensusIdentifiers } from "@mainsail/evm-consensus"; import { Utils } from "@mainsail/kernel"; import { chunk, validatorSetPack } from "@mainsail/utils"; +import { ethers } from "ethers"; import { performance } from "perf_hooks"; interface RestoreContext { @@ -107,6 +110,12 @@ export class Restore { @inject(Identifiers.Evm.ContractService.Consensus) private readonly consensusContractService!: Contracts.Evm.ConsensusContractService; + @inject(EvmConsensusIdentifiers.Contracts.Addresses.Usernames) + private readonly usernamesContractAddress!: string; + + @inject(EvmConsensusIdentifiers.Internal.Addresses.Deployer) + private readonly deployerAddress!: string; + public async restore(): Promise { const mostRecentCommit = await (this.databaseService.isEmpty() ? this.stateStore.getGenesisCommit() @@ -155,7 +164,7 @@ export class Restore { await this.#ingestBlocksAndTransactions(context); // 3) All `accounts` are read from the EVM storage and written to: - // - `wallets` table (NOTE: this does not include attributes yet) + // - `wallets` table await this.#ingestWallets(context); // 4) All `receipts` are read from the EVM storage and written to: @@ -338,6 +347,8 @@ export class Restore { const validatorAttributes = context.validatorAttributes[account.address]; const userAttributes = context.userAttributes[account.address]; + const username = await this.#readUsername(account.address); + accounts.push({ address: account.address, attributes: { @@ -369,7 +380,8 @@ export class Restore { ...(userAttributes ? { - vote: userAttributes.vote, + ...(userAttributes.vote ? { vote: userAttributes.vote } : {}), + ...(username ? { username } : {}), } : {}), }, @@ -535,4 +547,29 @@ export class Restore { async #updateValidatorRanks(context: RestoreContext): Promise { await context.entityManager.query("SELECT update_validator_ranks();", []); } + + async #readUsername(account: string): Promise { + const iface = new ethers.Interface(UsernamesAbi.abi); + const data = iface.encodeFunctionData("getUsername", [account]).slice(2); + + const { evmSpec } = this.configuration.getMilestone(0); + + const result = await this.evm.view({ + caller: this.deployerAddress, + data: Buffer.from(data, "hex"), + recipient: this.usernamesContractAddress, + specId: evmSpec, + }); + + if (!result.success) { + await this.app.terminate("getUsername failed"); + } + + const [username] = iface.decodeFunctionResult("getUsername", result.output!); + if (!username) { + return null; + } + + return username; + } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 497209db9..5d0f5ae73 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -401,12 +401,21 @@ importers: '@mainsail/contracts': specifier: workspace:* version: link:../contracts + '@mainsail/evm-consensus': + specifier: workspace:* + version: link:../evm-consensus + '@mainsail/evm-contracts': + specifier: workspace:* + version: link:../evm-contracts '@mainsail/kernel': specifier: workspace:* version: link:../kernel '@mainsail/utils': specifier: workspace:* version: link:../utils + ethers: + specifier: ^6.11.0 + version: 6.13.1 joi: specifier: 17.12.2 version: 17.12.2