Skip to content

Commit

Permalink
read usernames from contract on restore
Browse files Browse the repository at this point in the history
  • Loading branch information
oXtxNt9U committed Dec 5, 2024
1 parent cd4a48c commit f1aa9a4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
5 changes: 4 additions & 1 deletion packages/api-sync/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
41 changes: 39 additions & 2 deletions packages/api-sync/source/restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<void> {
const mostRecentCommit = await (this.databaseService.isEmpty()
? this.stateStore.getGenesisCommit()
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -369,7 +380,8 @@ export class Restore {

...(userAttributes
? {
vote: userAttributes.vote,
...(userAttributes.vote ? { vote: userAttributes.vote } : {}),
...(username ? { username } : {}),
}
: {}),
},
Expand Down Expand Up @@ -535,4 +547,29 @@ export class Restore {
async #updateValidatorRanks(context: RestoreContext): Promise<void> {
await context.entityManager.query("SELECT update_validator_ranks();", []);
}

async #readUsername(account: string): Promise<string | null> {
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;
}
}
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f1aa9a4

Please sign in to comment.