Skip to content

Commit

Permalink
refactor(transactions): inject EventDispatcher (#636)
Browse files Browse the repository at this point in the history
* Remove emitter in parameter

* Fix tests
  • Loading branch information
sebastijankuzner committed Jun 21, 2024
1 parent a383756 commit 12523da
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 29 deletions.
3 changes: 1 addition & 2 deletions packages/contracts/source/contracts/transactions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { MultiSignatureAsset, Transaction, TransactionConstructor, TransactionData } from "./crypto/index.js";
import { EventDispatcher } from "./kernel/events.js";
import { AttributeType, Wallet, WalletRepository } from "./state/index.js";

export type TransactionHandlerConstructor = new () => TransactionHandler;
Expand All @@ -13,7 +12,7 @@ export interface TransactionHandler {

applyToSender(walletRepository: WalletRepository, transaction: Transaction): Promise<void>;

emitEvents(transaction: Transaction, emitter: EventDispatcher): void;
emitEvents(transaction: Transaction): void;

throwIfCannotEnterPool(walletRepository: WalletRepository, transaction: Transaction): Promise<void>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export class ValidatorRegistrationTransactionHandler extends Handlers.Transactio
return super.throwIfCannotBeApplied(walletRepository, transaction, wallet);
}

public emitEvents(transaction: Contracts.Crypto.Transaction, emitter: Contracts.Kernel.EventDispatcher): void {
void emitter.dispatch(Events.ValidatorEvent.Registered, transaction.data);
public emitEvents(transaction: Contracts.Crypto.Transaction): void {
void this.eventDispatcher.dispatch(Events.ValidatorEvent.Registered, transaction.data);
}

public async throwIfCannotEnterPool(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ export class ValidatorResignationTransactionHandler extends Handlers.Transaction
return super.throwIfCannotBeApplied(walletRepository, transaction, wallet);
}

public emitEvents(transaction: Contracts.Crypto.Transaction, emitter: Contracts.Kernel.EventDispatcher): void {
void emitter.dispatch(Events.ValidatorEvent.Resigned, transaction.data);
public emitEvents(transaction: Contracts.Crypto.Transaction): void {
void this.eventDispatcher.dispatch(Events.ValidatorEvent.Resigned, transaction.data);
}

public async throwIfCannotEnterPool(
Expand Down
28 changes: 12 additions & 16 deletions packages/crypto-transaction-vote/source/handlers/vote.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe<{
walletRepository: any;
poolQuery: any;
handler: VoteTransactionHandler;
emitter: any;
}>("VoteHandler", ({ beforeEach, it, assert, stub }) => {
const wallet: Partial<Contracts.State.Wallet> = {
forgetAttribute: () => false,
Expand Down Expand Up @@ -65,12 +66,17 @@ describe<{
whereKind: () => context.poolQuery,
};

context.emitter = {
dispatch: async () => {},
};

context.sandbox = new Sandbox();

context.sandbox.app.bind(Identifiers.State.Service).toConstantValue({});
context.sandbox.app.bind(Identifiers.Services.Log.Service).toConstantValue({});
context.sandbox.app.bind(Identifiers.Cryptography.Configuration).toConstantValue({});
context.sandbox.app.bind(Identifiers.Cryptography.Transaction.Verifier).toConstantValue({});
context.sandbox.app.bind(Identifiers.Services.EventDispatcher.Service).toConstantValue(context.emitter);
context.sandbox.app.bind(Identifiers.TransactionPool.Query).toConstantValue(context.poolQuery);

context.handler = context.sandbox.app.resolve(VoteTransactionHandler);
Expand Down Expand Up @@ -373,18 +379,14 @@ describe<{
spySuper.neverCalled();
});

it("emitEvents - should dispatch", ({ handler }) => {
const emitter: Partial<Contracts.Kernel.EventDispatcher> = {
dispatch: async () => {},
};
it("emitEvents - should dispatch", ({ handler, sandbox, emitter }) => {
const spyDispatch = stub(emitter, "dispatch");

sandbox.app.rebind(Identifiers.Services.EventDispatcher.Service).toConstantValue(emitter);

const voteTransaction = getTransaction(["validatorPublicKey"], []);

handler.emitEvents(
voteTransaction as Contracts.Crypto.Transaction,
emitter as Contracts.Kernel.EventDispatcher,
);
handler.emitEvents(voteTransaction as Contracts.Crypto.Transaction);

spyDispatch.calledOnce();
spyDispatch.calledWith(Events.VoteEvent.Vote, {
Expand All @@ -394,10 +396,7 @@ describe<{

spyDispatch.reset();
const unvoteTransaction = getTransaction([], ["validatorPublicKey"]);
handler.emitEvents(
unvoteTransaction as Contracts.Crypto.Transaction,
emitter as Contracts.Kernel.EventDispatcher,
);
handler.emitEvents(unvoteTransaction as Contracts.Crypto.Transaction);

spyDispatch.calledOnce();
spyDispatch.calledWith(Events.VoteEvent.Unvote, {
Expand All @@ -407,10 +406,7 @@ describe<{

spyDispatch.reset();
const unvoteVoteTransaction = getTransaction(["voteValidatorPublicKey"], ["unvoteValidatorPublicKey"]);
handler.emitEvents(
unvoteVoteTransaction as Contracts.Crypto.Transaction,
emitter as Contracts.Kernel.EventDispatcher,
);
handler.emitEvents(unvoteVoteTransaction as Contracts.Crypto.Transaction);

spyDispatch.calledTimes(2);
spyDispatch.calledWith(Events.VoteEvent.Unvote, {
Expand Down
6 changes: 3 additions & 3 deletions packages/crypto-transaction-vote/source/handlers/vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,19 @@ export class VoteTransactionHandler extends Handlers.TransactionHandler {
return super.throwIfCannotBeApplied(walletRepository, transaction, wallet);
}

public emitEvents(transaction: Contracts.Crypto.Transaction, emitter: Contracts.Kernel.EventDispatcher): void {
public emitEvents(transaction: Contracts.Crypto.Transaction): void {
Utils.assert.defined<string[]>(transaction.data.asset?.votes);
Utils.assert.defined<string[]>(transaction.data.asset?.unvotes);

for (const unvote of transaction.data.asset.unvotes) {
void emitter.dispatch(Events.VoteEvent.Unvote, {
void this.eventDispatcher.dispatch(Events.VoteEvent.Unvote, {
transaction: transaction.data,
validator: unvote,
});
}

for (const vote of transaction.data.asset.votes) {
void emitter.dispatch(Events.VoteEvent.Vote, {
void this.eventDispatcher.dispatch(Events.VoteEvent.Vote, {
transaction: transaction.data,
validator: vote,
});
Expand Down
3 changes: 1 addition & 2 deletions packages/processor/source/block-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ export class BlockProcessor implements Contracts.Processor.BlockProcessor {

void this.#emit(Events.TransactionEvent.Applied, transaction.data);
const handler = await this.handlerRegistry.getActivatedHandlerForData(transaction.data);
// TODO: ! no reason to pass this.emitter
handler.emitEvents(transaction, this.events);
handler.emitEvents(transaction);
}

async #applyBlockToForger(unit: Contracts.Processor.ProcessableUnit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ describe<{
app.bind(Identifiers.Database.Service).toConstantValue({});

app.bind(Identifiers.Cryptography.Configuration).to(Configuration).inSingletonScope();
app.bind(Identifiers.Services.EventDispatcher.Service).toConstantValue({});

context.app = app;
});
Expand Down
6 changes: 4 additions & 2 deletions packages/transactions/source/handlers/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Contracts, Exceptions, Identifiers } from "@mainsail/contracts";
import { Utils as AppUtils } from "@mainsail/kernel";
import { BigNumber } from "@mainsail/utils";

// @TODO revisit the implementation, container usage and arguments after database rework
@injectable()
export abstract class TransactionHandler implements Contracts.Transactions.TransactionHandler {
@inject(Identifiers.Application.Instance)
Expand All @@ -18,6 +17,9 @@ export abstract class TransactionHandler implements Contracts.Transactions.Trans
@inject(Identifiers.Cryptography.Transaction.Verifier)
protected readonly verifier!: Contracts.Crypto.TransactionVerifier;

@inject(Identifiers.Services.EventDispatcher.Service)
protected readonly eventDispatcher!: Contracts.Kernel.EventDispatcher;

public async verify(
walletRepository: Contracts.State.WalletRepository,
transaction: Contracts.Crypto.Transaction,
Expand Down Expand Up @@ -128,7 +130,7 @@ export abstract class TransactionHandler implements Contracts.Transactions.Trans
sender.setBalance(newBalance);
}

public emitEvents(transaction: Contracts.Crypto.Transaction, emitter: Contracts.Kernel.EventDispatcher): void {}
public emitEvents(transaction: Contracts.Crypto.Transaction): void {}

public walletAttributes(): ReadonlyArray<{ name: string; type: Contracts.State.AttributeType }> {
return [];
Expand Down

0 comments on commit 12523da

Please sign in to comment.