Skip to content
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
4 changes: 0 additions & 4 deletions packages/constants/source/identifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,6 @@ export const Identifiers = {
},
Transaction: {
Handler: Symbol("Transaction<Handler>"),
Validator: {
Factory: Symbol("Transaction<Validator.Factory>"),
Instance: Symbol("Transaction<Validator.Instance>"),
},
},
TransactionPool: {
API: {
Expand Down
16 changes: 1 addition & 15 deletions packages/contracts/source/contracts/transactions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Transaction, TransactionData } from "./crypto/index.js";
import type { BlockContext, CommitKey, Instance, TransactionReceipt } from "./evm/index.js";
import type { BlockContext, Instance, TransactionReceipt } from "./evm/index.js";
import type { Wallet } from "./state/index.js";

export type TransactionHandlerConstructor = new () => TransactionHandler;
Expand Down Expand Up @@ -36,17 +36,3 @@ export interface TransactionHandlerProvider {

registerHandlers(): void;
}

export interface TransactionValidatorContext {
commitKey: CommitKey;
gasLimit: number;
timestamp: number;
generatorAddress: string;
}

export interface TransactionValidator {
getEvm(): Instance;
validate(context: TransactionValidatorContext, transaction: Transaction): Promise<TransactionReceipt>;
}

export type TransactionValidatorFactory = () => TransactionValidator;
46 changes: 25 additions & 21 deletions packages/forger/source/transaction-forger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export class TransactionForger implements Contracts.Forger.TransactionForger {
@inject(Identifiers.Cryptography.Configuration)
private readonly cryptoConfiguration!: Contracts.Crypto.Configuration;

@inject(Identifiers.Evm.Instance)
@tagged("instance", "validator")
private readonly evm!: Contracts.Evm.Instance;

@inject(Identifiers.State.Store)
protected readonly stateStore!: Contracts.State.Store;

Expand All @@ -35,8 +39,8 @@ export class TransactionForger implements Contracts.Forger.TransactionForger {
@inject(Identifiers.BlockchainUtils.RoundCalculator)
private readonly roundCalculator!: Contracts.BlockchainUtils.RoundCalculator;

@inject(Identifiers.Transaction.Validator.Factory)
private readonly createTransactionValidator!: Contracts.Transactions.TransactionValidatorFactory;
@inject(Identifiers.Transaction.Handler)
private readonly transactionHandler!: Contracts.Transactions.TransactionHandler;

@inject(Identifiers.Services.Log.Service)
private readonly logger!: Contracts.Kernel.Logger;
Expand All @@ -50,8 +54,6 @@ export class TransactionForger implements Contracts.Forger.TransactionForger {
#generatorAddress!: string;
#timestamp!: number;
#commitKey!: Contracts.Evm.CommitKey;
#validator!: Contracts.Transactions.TransactionValidator;
#evm!: Contracts.Evm.Instance;
#milestone!: Contracts.Crypto.Milestone;
#previousBlock!: Contracts.Crypto.Block;
#timeLimit!: number;
Expand All @@ -67,9 +69,6 @@ export class TransactionForger implements Contracts.Forger.TransactionForger {
this.#timestamp = timestamp;
this.#commitKey = commitKey;

this.#validator = this.createTransactionValidator();
this.#evm = this.#validator.getEvm();

this.#milestone = this.cryptoConfiguration.getMilestone();
this.#previousBlock = this.stateStore.getLastBlock();

Expand All @@ -90,8 +89,8 @@ export class TransactionForger implements Contracts.Forger.TransactionForger {
fee: bigint;
}> {
try {
await this.#evm.initializeGenesis(this.genesisInfo);
await this.#evm.prepareNextCommit({ commitKey: this.#commitKey });
await this.evm.initializeGenesis(this.genesisInfo);
await this.evm.prepareNextCommit({ commitKey: this.#commitKey });

const { fee, gasUsed, transactions } = await this.#processTransactions();

Expand All @@ -101,12 +100,12 @@ export class TransactionForger implements Contracts.Forger.TransactionForger {
return {
fee,
gasUsed,
logsBloom: await this.#evm.logsBloom(this.#commitKey),
stateRoot: await this.#evm.stateRoot(this.#commitKey, this.#previousBlock.stateRoot),
logsBloom: await this.evm.logsBloom(this.#commitKey),
stateRoot: await this.evm.stateRoot(this.#commitKey, this.#previousBlock.stateRoot),
transactions,
};
} finally {
await this.#evm.dispose();
await this.evm.dispose();
}
}

Expand Down Expand Up @@ -157,7 +156,7 @@ export class TransactionForger implements Contracts.Forger.TransactionForger {
`attempting optimistic execution of tx ${transaction.hash} (tx.gas=${transaction.gasLimit} gasLeft=${result.gasLeft})`,
);

await this.#evm.snapshot(this.#commitKey);
await this.evm.snapshot(this.#commitKey);
}

const validation = await this.#validateTransaction(transaction);
Expand All @@ -171,7 +170,7 @@ export class TransactionForger implements Contracts.Forger.TransactionForger {
);

if (optimisticExecution) {
await this.#evm.rollback(this.#commitKey);
await this.evm.rollback(this.#commitKey);
return;
} else {
// In practice, this should never happen since the validator should reject transactions that exceed the block gas limit, but we check just in case.
Expand All @@ -190,12 +189,17 @@ export class TransactionForger implements Contracts.Forger.TransactionForger {
}

async #validateTransaction(transaction: Contracts.Crypto.Transaction): Promise<Contracts.Evm.TransactionReceipt> {
return this.#validator.validate(
return await this.transactionHandler.apply(
{
commitKey: this.#commitKey,
gasLimit: this.#milestone.block.maxGasLimit,
generatorAddress: this.#generatorAddress,
timestamp: this.#timestamp,
evm: {
blockContext: {
commitKey: this.#commitKey,
gasLimit: BigInt(this.#milestone.block.maxGasLimit),
timestamp: BigInt(this.#timestamp),
validatorAddress: this.#generatorAddress,
},
instance: this.evm,
},
},
transaction,
);
Expand All @@ -209,7 +213,7 @@ export class TransactionForger implements Contracts.Forger.TransactionForger {
}

async #updateRewardsAndVotes(): Promise<void> {
await this.#evm.updateRewardsAndVotes({
await this.evm.updateRewardsAndVotes({
blockReward: BigInt(this.#milestone.reward),
commitKey: this.#commitKey,
specId: this.#milestone.evmSpec,
Expand All @@ -222,7 +226,7 @@ export class TransactionForger implements Contracts.Forger.TransactionForger {
if (this.roundCalculator.isNewRound(this.#previousBlock.number + 2)) {
const { roundValidators } = this.cryptoConfiguration.getMilestone(this.#previousBlock.number + 2);

await this.#evm.calculateRoundValidators({
await this.evm.calculateRoundValidators({
commitKey: this.#commitKey,
roundValidators: BigInt(roundValidators),
specId: this.#milestone.evmSpec,
Expand Down
28 changes: 14 additions & 14 deletions packages/forger/test/helpers/prepare-sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,22 @@ export const prepareSandbox = async (context: { app?: Application }): Promise<vo
getTransactionBytes: async () => [],
});

const validator = {
getEvm: () => ({
dispose: async () => {},
initializeGenesis: async () => {},
logsBloom: async () => "0".repeat(512),
prepareNextCommit: async () => {},
rollback: async () => {},
snapshot: async () => {},
stateRoot: async () => "0000000000000000000000000000000000000000000000000000000000000000",
updateRewardsAndVotes: async () => {},
}),
validate: async () => true,
const evm = {
dispose: async () => {},
initializeGenesis: async () => {},
logsBloom: async () => "0".repeat(512),
prepareNextCommit: async () => {},
rollback: async () => {},
snapshot: async () => {},
stateRoot: async () => "0000000000000000000000000000000000000000000000000000000000000000",
updateRewardsAndVotes: async () => {},
};
context.app.rebind(Identifiers.Transaction.Validator.Factory).toConstantValue(() => validator);
context.app.bind(Identifiers.Evm.Instance).toConstantValue(evm).whenTagged("instance", "validator");

context.app.rebind(Identifiers.Transaction.Handler).toConstantValue({
apply: async () => ({ gasRefunded: 0n, gasUsed: 0n, logs: [], status: 1 }),
});

context.app.bind(Identifiers.Evm.Instance).toConstantValue(() => {});
context.app.bind(EvmConsensusIdentifiers.Internal.GenesisInfo).toConstantValue({});

context.app.bind(Identifiers.State.Store).toConstantValue({
Expand Down
12 changes: 0 additions & 12 deletions packages/transactions/source/service-provider.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
import type { Contracts } from "@mainsail/contracts";

import { Identifiers } from "@mainsail/constants";
import { injectable } from "@mainsail/container";
import { Providers } from "@mainsail/kernel";

import { TransactionHandler } from "./handlers/index.js";
import { TransactionValidator } from "./transaction-validator.js";

@injectable()
export class ServiceProvider extends Providers.ServiceProvider {
public async register(): Promise<void> {
this.app.bind(Identifiers.Transaction.Validator.Instance).to(TransactionValidator);

this.app.bind(Identifiers.Transaction.Handler).to(TransactionHandler);

this.app
.bind<() => TransactionValidator>(Identifiers.Transaction.Validator.Factory)
.toFactory(
(context: Contracts.Kernel.Container.ResolutionContext) => () =>
context.get(Identifiers.Transaction.Validator.Instance),
);
}

public async required(): Promise<boolean> {
Expand Down
46 changes: 0 additions & 46 deletions packages/transactions/source/transaction-validator.test.ts

This file was deleted.

54 changes: 0 additions & 54 deletions packages/transactions/source/transaction-validator.ts

This file was deleted.

28 changes: 14 additions & 14 deletions packages/validator/test/helpers/prepare-sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,22 @@ export const prepareSandbox = async (context: { app?: Application }): Promise<vo
}),
});

const validator = {
getEvm: () => ({
dispose: async () => {},
initializeGenesis: async () => {},
logsBloom: async () => "0".repeat(512),
prepareNextCommit: async () => {},
rollback: async () => {},
snapshot: async () => {},
stateRoot: async () => "0000000000000000000000000000000000000000000000000000000000000000",
updateRewardsAndVotes: async () => {},
}),
validate: async () => true,
const evm = {
dispose: async () => {},
initializeGenesis: async () => {},
logsBloom: async () => "0".repeat(512),
prepareNextCommit: async () => {},
rollback: async () => {},
snapshot: async () => {},
stateRoot: async () => "0000000000000000000000000000000000000000000000000000000000000000",
updateRewardsAndVotes: async () => {},
};
context.app.rebind(Identifiers.Transaction.Validator.Factory).toConstantValue(() => validator);
context.app.bind(Identifiers.Evm.Instance).toConstantValue(evm).whenTagged("instance", "validator");

context.app.rebind(Identifiers.Transaction.Handler).toConstantValue({
apply: async () => ({ gasRefunded: 0n, gasUsed: 0n, logs: [], status: 1 }),
});

context.app.bind(Identifiers.Evm.Instance).toConstantValue(() => {});
context.app.bind(EvmConsensusIdentifiers.Internal.GenesisInfo).toConstantValue({});

context.app.bind(Identifiers.State.Store).toConstantValue({
Expand Down
Loading
Loading