Skip to content

Commit

Permalink
restore contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
oXtxNt9U committed Dec 12, 2024
1 parent 25f0278 commit 1bb8507
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 8 deletions.
33 changes: 31 additions & 2 deletions packages/api-sync/source/restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from "@mainsail/api-database";
import { inject, injectable, tagged } from "@mainsail/container";
import { Contracts, Identifiers } from "@mainsail/contracts";
import { Identifiers as EvmConsensusIdentifiers } from "@mainsail/evm-consensus";
import { Identifiers as EvmConsensusIdentifiers, Deployer } from "@mainsail/evm-consensus";
import { UsernamesAbi } from "@mainsail/evm-contracts";
import { Utils } from "@mainsail/kernel";
import { chunk, validatorSetPack } from "@mainsail/utils";
Expand All @@ -16,6 +16,7 @@ interface RestoreContext {
readonly entityManager: ApiDatabaseContracts.RepositoryDataSource;
readonly blockRepository: ApiDatabaseContracts.BlockRepository;
readonly configurationRepository: ApiDatabaseContracts.ConfigurationRepository;
readonly contractRepository: ApiDatabaseContracts.ContractRepository;
readonly stateRepository: ApiDatabaseContracts.StateRepository;
readonly transactionRepository: ApiDatabaseContracts.TransactionRepository;
readonly transactionTypeRepository: ApiDatabaseContracts.TransactionTypeRepository;
Expand Down Expand Up @@ -86,6 +87,9 @@ export class Restore {
@inject(ApiDatabaseIdentifiers.ConfigurationRepositoryFactory)
private readonly configurationRepositoryFactory!: ApiDatabaseContracts.ConfigurationRepositoryFactory;

@inject(ApiDatabaseIdentifiers.ContractRepositoryFactory)
private readonly contractRepositoryFactory!: ApiDatabaseContracts.ContractRepositoryFactory;

@inject(ApiDatabaseIdentifiers.ReceiptRepositoryFactory)
private readonly receiptRepositoryFactory!: ApiDatabaseContracts.ReceiptRepositoryFactory;

Expand Down Expand Up @@ -134,6 +138,7 @@ export class Restore {
addressToPublicKey: {},
blockRepository: this.blockRepositoryFactory(entityManager),
configurationRepository: this.configurationRepositoryFactory(entityManager),
contractRepository: this.contractRepositoryFactory(entityManager),
entityManager,
lastHeight: 0,
mostRecentCommit,
Expand Down Expand Up @@ -184,7 +189,10 @@ export class Restore {
// 8) Write `state` table
await this.#ingestState(context);

// 9) Update validator ranks
// 9) Write `contracts` table
await this.#ingestContracts(context);

// 10) Update validator ranks
await this.#updateValidatorRanks(context);

restoredHeight = context.lastHeight;
Expand Down Expand Up @@ -553,6 +561,27 @@ export class Restore {
.execute();
}

async #ingestContracts(context: RestoreContext): Promise<void> {
const deploymentEvents = this.app
.get<Deployer>(EvmConsensusIdentifiers.Internal.Deployer)
.getDeploymentEvents();

await context.contractRepository
.createQueryBuilder()
.insert()
.orIgnore()
.values(
deploymentEvents.map((event) => ({
activeImplementation: event.activeImplementation ?? event.address,
address: event.address,
implementations: event.implementations,
name: event.name,
proxy: event.proxy,
})),
)
.execute();
}

async #updateValidatorRanks(context: RestoreContext): Promise<void> {
await context.entityManager.query("SELECT update_validator_ranks();", []);
}
Expand Down
5 changes: 5 additions & 0 deletions packages/api-sync/source/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ export class Sync implements Contracts.ApiSync.Service {
@inject(ApiDatabaseIdentifiers.BlockRepositoryFactory)
private readonly blockRepositoryFactory!: ApiDatabaseContracts.BlockRepositoryFactory;

@inject(ApiDatabaseIdentifiers.ContractRepositoryFactory)
private readonly contractRepositoryFactory!: ApiDatabaseContracts.ContractRepositoryFactory;

@inject(ApiDatabaseIdentifiers.ConfigurationRepositoryFactory)
private readonly configurationRepositoryFactory!: ApiDatabaseContracts.ConfigurationRepositoryFactory;

Expand Down Expand Up @@ -494,6 +497,7 @@ export class Sync implements Contracts.ApiSync.Service {

await this.dataSource.transaction("REPEATABLE READ", async (entityManager) => {
const blockRepository = this.blockRepositoryFactory(entityManager);
const contractRepository = this.contractRepositoryFactory(entityManager);
const stateRepository = this.stateRepositoryFactory(entityManager);
const transactionRepository = this.transactionRepositoryFactory(entityManager);
const receiptRepository = this.receiptRepositoryFactory(entityManager);
Expand All @@ -504,6 +508,7 @@ export class Sync implements Contracts.ApiSync.Service {
await Promise.all(
[
blockRepository,
contractRepository,
stateRepository,
transactionRepository,
receiptRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ export const makeApplication = async (configurationPath: string, options: Record

const app = new Application(new Container());
app.bind(Identifiers.Application.Name).toConstantValue(options.name);
app.bind(Identifiers.Services.EventDispatcher.Service).toConstantValue({});
app.bind(Identifiers.Services.EventDispatcher.Service).toConstantValue({
dispatch: () => {},
});
app.bind(Identifiers.Services.Log.Service).toConstantValue({
debug: (message: string) => console.log(message),
info: (message: string) => console.log(message),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ValidatorWallet } from "../state/wallets.js";
export interface DeployerContract {
readonly name: string;
readonly address: string;
readonly proxy: "UUPS" | undefined;
readonly proxy?: "UUPS";
readonly implementations: { address: string; abi: Record<string, any> }[];
readonly activeImplementation?: string;
}
Expand Down
16 changes: 13 additions & 3 deletions packages/evm-consensus/source/deployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export class Deployer {
`Deployed Consensus PROXY contract from ${this.deployerAddress} to ${proxyResult.receipt.deployedContractAddress}`,
);

void this.events.dispatch(Events.DeployerEvent.ContractCreated, {
this.#emitContractDeployed({
activeImplementation: consensusContractAddress,
address: proxyResult.receipt.deployedContractAddress!,
implementations: [{ abi: ConsensusAbi.abi, address: consensusContractAddress }],
Expand Down Expand Up @@ -243,7 +243,7 @@ export class Deployer {
`Deployed Usernames PROXY contract from ${this.deployerAddress} to ${proxyResult.receipt.deployedContractAddress}`,
);

void this.events.dispatch(Events.DeployerEvent.ContractCreated, {
this.#emitContractDeployed({
activeImplementation: usernamesContractAddress,
address: proxyResult.receipt.deployedContractAddress!,
implementations: [{ abi: UsernamesAbi.abi, address: usernamesContractAddress }],
Expand Down Expand Up @@ -282,12 +282,22 @@ export class Deployer {
`Deployed MultiPayments contract from ${this.deployerAddress} to ${result.receipt.deployedContractAddress}`,
);

void this.events.dispatch(Events.DeployerEvent.ContractCreated, {
this.#emitContractDeployed({
address: result.receipt.deployedContractAddress!,
implementations: [{ abi: MultiPaymentAbi.abi, address: result.receipt.deployedContractAddress! }],
name: "multi-payments",
});

return result.receipt.deployedContractAddress!;
}

public getDeploymentEvents(): Contracts.Evm.DeployerContract[] {
return this.#deploymentEvents;
}

#deploymentEvents: Contracts.Evm.DeployerContract[] = [];
#emitContractDeployed(event: Contracts.Evm.DeployerContract): void {
this.#deploymentEvents.push(event);
void this.events.dispatch(Events.DeployerEvent.ContractCreated, event);
}
}
1 change: 1 addition & 0 deletions packages/evm-consensus/source/identifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const Identifiers = {
},
},
Internal: {
Deployer: Symbol.for("Evm.Consensus<Internal.Deployer>"),
Addresses: {
Deployer: Symbol.for("Evm.Consensus<Internal.Addresses.Deployer>"),
},
Expand Down
4 changes: 3 additions & 1 deletion packages/evm-consensus/source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ export class ServiceProvider extends Providers.ServiceProvider {
const genesisBlock = this.app.config<Contracts.Crypto.CommitJson>("crypto.genesisBlock");
Utils.assert.defined(genesisBlock);

await this.app.resolve(Deployer).deploy({
this.app.bind(EvmConsensusIdentifiers.Internal.Deployer).to(Deployer).inSingletonScope();

await this.app.get<Deployer>(EvmConsensusIdentifiers.Internal.Deployer).deploy({
generatorAddress: genesisBlock.block.generatorAddress,
timestamp: genesisBlock.block.timestamp,
totalAmount: genesisBlock.block.totalAmount,
Expand Down

0 comments on commit 1bb8507

Please sign in to comment.