Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(contracts): move events to contracts #635

Merged
merged 7 commits into from
Jun 21, 2024
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
7 changes: 3 additions & 4 deletions packages/api-sync/source/listeners/api-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import {
Models,
} from "@mainsail/api-database";
import { inject, injectable } from "@mainsail/container";
import { Contracts } from "@mainsail/contracts";
import { Enums } from "@mainsail/kernel";
import { Contracts, Events } from "@mainsail/contracts";

import { AbstractListener, ListenerEvent, ListenerEventMapping } from "./abstract-listener.js";

Expand All @@ -16,8 +15,8 @@ export class ApiNodes extends AbstractListener<Contracts.P2P.ApiNode, Models.Api

protected getEventMapping(): ListenerEventMapping {
return {
[Enums.ApiNodeEvent.Added]: ListenerEvent.OnAdded,
[Enums.ApiNodeEvent.Removed]: ListenerEvent.OnRemoved,
[Events.ApiNodeEvent.Added]: ListenerEvent.OnAdded,
[Events.ApiNodeEvent.Removed]: ListenerEvent.OnRemoved,
};
}

Expand Down
12 changes: 6 additions & 6 deletions packages/api-sync/source/listeners/mempool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
Models,
} from "@mainsail/api-database";
import { inject, injectable } from "@mainsail/container";
import { Contracts } from "@mainsail/contracts";
import { Enums, Utils } from "@mainsail/kernel";
import { Contracts, Events } from "@mainsail/contracts";
import { Utils } from "@mainsail/kernel";

import { AbstractListener, ListenerEvent, ListenerEventMapping } from "./abstract-listener.js";

Expand All @@ -16,10 +16,10 @@ export class Mempool extends AbstractListener<Contracts.Crypto.TransactionData,

protected getEventMapping(): ListenerEventMapping {
return {
[Enums.TransactionEvent.AddedToPool]: ListenerEvent.OnAdded,
[Enums.TransactionEvent.Applied]: ListenerEvent.OnRemoved,
[Enums.TransactionEvent.Expired]: ListenerEvent.OnRemoved,
[Enums.TransactionEvent.RemovedFromPool]: ListenerEvent.OnRemoved,
[Events.TransactionEvent.AddedToPool]: ListenerEvent.OnAdded,
[Events.TransactionEvent.Applied]: ListenerEvent.OnRemoved,
[Events.TransactionEvent.Expired]: ListenerEvent.OnRemoved,
[Events.TransactionEvent.RemovedFromPool]: ListenerEvent.OnRemoved,
};
}

Expand Down
8 changes: 4 additions & 4 deletions packages/api-sync/source/listeners/peers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
Models,
} from "@mainsail/api-database";
import { inject, injectable } from "@mainsail/container";
import { Contracts } from "@mainsail/contracts";
import { Enums, Utils } from "@mainsail/kernel";
import { Contracts, Events } from "@mainsail/contracts";
import { Utils } from "@mainsail/kernel";

import { AbstractListener, ListenerEvent, ListenerEventMapping } from "./abstract-listener.js";

Expand All @@ -16,8 +16,8 @@ export class Peers extends AbstractListener<Contracts.P2P.Peer, Models.Peer> {

protected getEventMapping(): ListenerEventMapping {
return {
[Enums.PeerEvent.Added]: ListenerEvent.OnAdded,
[Enums.PeerEvent.Removed]: ListenerEvent.OnRemoved,
[Events.PeerEvent.Added]: ListenerEvent.OnAdded,
[Events.PeerEvent.Removed]: ListenerEvent.OnRemoved,
};
}

Expand Down
6 changes: 3 additions & 3 deletions packages/api-sync/source/listeners/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
Models,
} from "@mainsail/api-database";
import { inject, injectable } from "@mainsail/container";
import { Contracts, Identifiers } from "@mainsail/contracts";
import { Enums, Providers } from "@mainsail/kernel";
import { Contracts, Events, Identifiers } from "@mainsail/contracts";
import { Providers } from "@mainsail/kernel";

import { AbstractListener, ListenerEvent, ListenerEventMapping } from "./abstract-listener.js";

Expand All @@ -21,7 +21,7 @@ export class Plugins extends AbstractListener<Event, Models.Plugin> {

protected getEventMapping(): ListenerEventMapping {
return {
[Enums.KernelEvent.ServiceProviderBooted]: ListenerEvent.OnAdded,
[Events.KernelEvent.ServiceProviderBooted]: ListenerEvent.OnAdded,
};
}

Expand Down
32 changes: 16 additions & 16 deletions packages/consensus/source/consensus.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Contracts, Identifiers } from "@mainsail/contracts";
import { Enums, Utils } from "@mainsail/kernel";
import { Contracts, Events, Identifiers } from "@mainsail/contracts";
import { Utils } from "@mainsail/kernel";

import { describe, Sandbox } from "../../test-framework/source";
import { Consensus } from "./consensus";
Expand Down Expand Up @@ -224,7 +224,7 @@ describe<Context>("Consensus", ({ it, beforeEach, assert, stub, spy, clock, each
spyGetRoundState.calledWith(1, 0);
spyLoggerInfo.calledWith(`>> Starting new round: ${1}/${0} with proposer: ${proposer}`);
spyDispatch.calledOnce();
spyDispatch.calledWith(Enums.ConsensusEvent.RoundStarted, {
spyDispatch.calledWith(Events.ConsensusEvent.RoundStarted, {
height: 1,
lockedRound: undefined,
round: 0,
Expand Down Expand Up @@ -283,7 +283,7 @@ describe<Context>("Consensus", ({ it, beforeEach, assert, stub, spy, clock, each
spyValidatorPropose.calledWith(1, 0, undefined, block);
spyLoggerInfo.calledWith(`>> Starting new round: ${1}/${0} with proposer: ${proposer}`);
spyDispatch.called();
spyDispatch.calledWith(Enums.ConsensusEvent.RoundStarted, {
spyDispatch.calledWith(Events.ConsensusEvent.RoundStarted, {
height: 1,
lockedRound: undefined,
round: 0,
Expand Down Expand Up @@ -357,7 +357,7 @@ describe<Context>("Consensus", ({ it, beforeEach, assert, stub, spy, clock, each
spyLoggerInfo.calledWith(`>> Starting new round: ${1}/${1} with proposer: ${proposer}`);
spyLoggerInfo.calledWith(`Proposing valid block ${1}/${1} from round ${0} with blockId: ${block.data.id}`);
spyDispatch.calledOnce();
spyDispatch.calledWith(Enums.ConsensusEvent.RoundStarted, {
spyDispatch.calledWith(Events.ConsensusEvent.RoundStarted, {
height: 1,
lockedRound: undefined,
round: 1,
Expand Down Expand Up @@ -506,7 +506,7 @@ describe<Context>("Consensus", ({ it, beforeEach, assert, stub, spy, clock, each

spyLoggerInfo.calledWith(`Received proposal ${1}/${0} blockId: ${proposal.getData().block.data.id}`);
spyDispatch.calledOnce();
spyDispatch.calledWith(Enums.ConsensusEvent.ProposalAccepted, {
spyDispatch.calledWith(Events.ConsensusEvent.ProposalAccepted, {
height: 1,
lockedRound: undefined,
round: 0,
Expand Down Expand Up @@ -564,7 +564,7 @@ describe<Context>("Consensus", ({ it, beforeEach, assert, stub, spy, clock, each
spyPrevoteProcess.calledWith(prevote);
spyLoggerInfo.calledWith(`Received proposal ${1}/${0} blockId: ${proposal.getData().block.data.id}`);
spyDispatch.calledOnce();
spyDispatch.calledWith(Enums.ConsensusEvent.ProposalAccepted, {
spyDispatch.calledWith(Events.ConsensusEvent.ProposalAccepted, {
height: 1,
lockedRound: undefined,
round: 0,
Expand Down Expand Up @@ -621,7 +621,7 @@ describe<Context>("Consensus", ({ it, beforeEach, assert, stub, spy, clock, each

spyLoggerInfo.calledWith(`Received proposal ${1}/${0} blockId: ${proposal.getData().block.data.id}`);
spyDispatch.calledOnce();
spyDispatch.calledWith(Enums.ConsensusEvent.ProposalAccepted, {
spyDispatch.calledWith(Events.ConsensusEvent.ProposalAccepted, {
height: 1,
lockedRound: undefined,
round: 0,
Expand Down Expand Up @@ -691,7 +691,7 @@ describe<Context>("Consensus", ({ it, beforeEach, assert, stub, spy, clock, each
spyLoggerInfo.calledWith(
`Received proposal ${1}/${1} with locked blockId: ${proposal.getData().block.data.id}`,
);
spyDispatch.calledWith(Enums.ConsensusEvent.ProposalAccepted, {
spyDispatch.calledWith(Events.ConsensusEvent.ProposalAccepted, {
height: 1,
lockedRound: undefined,
round: 1,
Expand Down Expand Up @@ -756,7 +756,7 @@ describe<Context>("Consensus", ({ it, beforeEach, assert, stub, spy, clock, each
spyPrevoteProcess.calledWith(prevote);

spyDispatch.calledOnce();
spyDispatch.calledWith(Enums.ConsensusEvent.ProposalAccepted, {
spyDispatch.calledWith(Events.ConsensusEvent.ProposalAccepted, {
height: 1,
lockedRound: undefined,
round: 1,
Expand Down Expand Up @@ -902,7 +902,7 @@ describe<Context>("Consensus", ({ it, beforeEach, assert, stub, spy, clock, each
spyPrecommitProcess.calledWith(precommit);
spyLoggerInfo.calledWith(`Received +2/3 prevotes for ${1}/${0} blockId: ${proposal.getData().block.data.id}`);
spyDispatch.calledOnce();
spyDispatch.calledWith(Enums.ConsensusEvent.PrevotedProposal, {
spyDispatch.calledWith(Events.ConsensusEvent.PrevotedProposal, {
height: 1,
lockedRound: 0,
round: 0,
Expand Down Expand Up @@ -935,7 +935,7 @@ describe<Context>("Consensus", ({ it, beforeEach, assert, stub, spy, clock, each
assert.equal(consensus.getStep(), Contracts.Consensus.Step.Precommit);

spyDispatch.calledOnce();
spyDispatch.calledWith(Enums.ConsensusEvent.PrevotedProposal, {
spyDispatch.calledWith(Events.ConsensusEvent.PrevotedProposal, {
height: 1,
lockedRound: undefined,
round: 0,
Expand Down Expand Up @@ -1109,7 +1109,7 @@ describe<Context>("Consensus", ({ it, beforeEach, assert, stub, spy, clock, each
assert.equal(consensus.getStep(), Contracts.Consensus.Step.Prevote);

spyDispatch.calledOnce();
spyDispatch.calledWith(Enums.ConsensusEvent.PrevotedAny, {
spyDispatch.calledWith(Events.ConsensusEvent.PrevotedAny, {
height: 1,
lockedRound: undefined,
round: 0,
Expand Down Expand Up @@ -1234,7 +1234,7 @@ describe<Context>("Consensus", ({ it, beforeEach, assert, stub, spy, clock, each
spyPrecommitProcess.calledWith(precommit);

spyDispatch.calledOnce();
spyDispatch.calledWith(Enums.ConsensusEvent.PrevotedNull, {
spyDispatch.calledWith(Events.ConsensusEvent.PrevotedNull, {
height: 1,
lockedRound: undefined,
round: 0,
Expand Down Expand Up @@ -1285,7 +1285,7 @@ describe<Context>("Consensus", ({ it, beforeEach, assert, stub, spy, clock, each
spyScheduleTimeout.calledWith(1, 0);

spyDispatch.calledOnce();
spyDispatch.calledWith(Enums.ConsensusEvent.PrecommitedAny, {
spyDispatch.calledWith(Events.ConsensusEvent.PrecommitedAny, {
height: 1,
lockedRound: undefined,
round: 0,
Expand Down Expand Up @@ -1381,7 +1381,7 @@ describe<Context>("Consensus", ({ it, beforeEach, assert, stub, spy, clock, each
spyRoundStateRepositoryClear.calledOnce();
spyLoggerInfo.calledWith(`Received +2/3 precommits for ${1}/${0} blockId: ${proposal.getData().block.data.id}`);
spyDispatch.calledOnce();
spyDispatch.calledWith(Enums.ConsensusEvent.PrecommitedProposal, {
spyDispatch.calledWith(Events.ConsensusEvent.PrecommitedProposal, {
height: 1,
lockedRound: undefined,
round: 0,
Expand Down
26 changes: 13 additions & 13 deletions packages/consensus/source/consensus.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { inject, injectable } from "@mainsail/container";
import { Contracts, Identifiers } from "@mainsail/contracts";
import { Enums, Utils } from "@mainsail/kernel";
import { Contracts, Events, Identifiers } from "@mainsail/contracts";
import { Utils } from "@mainsail/kernel";
import dayjs from "dayjs";

@injectable()
Expand Down Expand Up @@ -205,7 +205,7 @@ export class Consensus implements Contracts.Consensus.Service {
const roundState = this.roundStateRepository.getRoundState(this.#height, this.#round);
this.logger.info(`>> Starting new round: ${this.#height}/${this.#round} with proposer: ${roundState.proposer}`);

await this.eventDispatcher.dispatch(Enums.ConsensusEvent.RoundStarted, this.getState());
await this.eventDispatcher.dispatch(Events.ConsensusEvent.RoundStarted, this.getState());

this.scheduler.scheduleTimeoutBlockPrepare(this.scheduler.getNextBlockTimestamp(this.#roundStartTime));

Expand Down Expand Up @@ -239,7 +239,7 @@ export class Consensus implements Contracts.Consensus.Service {

const { block } = proposal.getData();
this.logger.info(`Received proposal ${this.#height}/${this.#round} blockId: ${block.data.id}`);
await this.eventDispatcher.dispatch(Enums.ConsensusEvent.ProposalAccepted, this.getState());
await this.eventDispatcher.dispatch(Events.ConsensusEvent.ProposalAccepted, this.getState());

await this.prevote(roundState.getProcessorResult() ? block.data.id : undefined);
}
Expand All @@ -261,7 +261,7 @@ export class Consensus implements Contracts.Consensus.Service {
this.#step = Contracts.Consensus.Step.Prevote;

this.logger.info(`Received proposal ${this.#height}/${this.#round} with locked blockId: ${block.data.id}`);
await this.eventDispatcher.dispatch(Enums.ConsensusEvent.ProposalAccepted, this.getState());
await this.eventDispatcher.dispatch(Events.ConsensusEvent.ProposalAccepted, this.getState());

const lockedRound = this.getLockedRound();

Expand Down Expand Up @@ -296,12 +296,12 @@ export class Consensus implements Contracts.Consensus.Service {
this.#validValue = roundState;
this.#step = Contracts.Consensus.Step.Precommit;

await this.eventDispatcher.dispatch(Enums.ConsensusEvent.PrevotedProposal, this.getState());
await this.eventDispatcher.dispatch(Events.ConsensusEvent.PrevotedProposal, this.getState());
await this.precommit(block.data.id);
} else {
this.#validValue = roundState;

await this.eventDispatcher.dispatch(Enums.ConsensusEvent.PrevotedProposal, this.getState());
await this.eventDispatcher.dispatch(Events.ConsensusEvent.PrevotedProposal, this.getState());
}
}

Expand All @@ -311,7 +311,7 @@ export class Consensus implements Contracts.Consensus.Service {
}

if (this.scheduler.scheduleTimeoutPrevote(this.#height, this.#round)) {
await this.eventDispatcher.dispatch(Enums.ConsensusEvent.PrevotedAny, this.getState());
await this.eventDispatcher.dispatch(Events.ConsensusEvent.PrevotedAny, this.getState());
}
}

Expand All @@ -324,7 +324,7 @@ export class Consensus implements Contracts.Consensus.Service {

this.#step = Contracts.Consensus.Step.Precommit;

await this.eventDispatcher.dispatch(Enums.ConsensusEvent.PrevotedNull, this.getState());
await this.eventDispatcher.dispatch(Events.ConsensusEvent.PrevotedNull, this.getState());
await this.precommit();
}

Expand All @@ -334,7 +334,7 @@ export class Consensus implements Contracts.Consensus.Service {
}

if (this.scheduler.scheduleTimeoutPrecommit(this.#height, this.#round)) {
await this.eventDispatcher.dispatch(Enums.ConsensusEvent.PrecommitedAny, this.getState());
await this.eventDispatcher.dispatch(Events.ConsensusEvent.PrecommitedAny, this.getState());
}
}

Expand All @@ -355,7 +355,7 @@ export class Consensus implements Contracts.Consensus.Service {
}

this.logger.info(`Received +2/3 precommits for ${this.#height}/${roundState.round} blockId: ${block.data.id}`);
await this.eventDispatcher.dispatch(Enums.ConsensusEvent.PrecommitedProposal, this.getState());
await this.eventDispatcher.dispatch(Events.ConsensusEvent.PrecommitedProposal, this.getState());

await this.commitLock.runExclusive(async () => {
try {
Expand Down Expand Up @@ -481,7 +481,7 @@ export class Consensus implements Contracts.Consensus.Service {
);
this.logger.info(`Proposing new block ${this.#height}/${this.#round} with blockId: ${block.data.id}`);

void this.eventDispatcher.dispatch(Enums.BlockEvent.Forged, block.data);
void this.eventDispatcher.dispatch(Events.BlockEvent.Forged, block.data);

return registeredProposer.propose(
this.validatorSet.getValidatorIndexByWalletPublicKey(roundState.proposer.getWalletPublicKey()),
Expand Down Expand Up @@ -564,7 +564,7 @@ export class Consensus implements Contracts.Consensus.Service {

this.logger.info(`Completed consensus bootstrap for ${this.#height}/${this.#round}/${store.getTotalRound()}`);

await this.eventDispatcher.dispatch(Enums.ConsensusEvent.Bootstrapped, this.getState());
await this.eventDispatcher.dispatch(Events.ConsensusEvent.Bootstrapped, this.getState());
}

async #processProposal(roundState: Contracts.Consensus.RoundState): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,11 @@ export enum QueueEvent {
Finished = "queue.finished",
Failed = "queue.failed",
}

export enum WebhookEvent {
Broadcasted = "webhooks.broadcasted",
Failed = "webhooks.failed",
Created = "webhooks.created",
Updated = "webhooks.updated",
Removed = "webhooks.removed",
}
1 change: 1 addition & 0 deletions packages/contracts/source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import "reflect-metadata";

export * as Constants from "./constants/index.js";
export * as Contracts from "./contracts/index.js";
export * as Events from "./events.js";
export * as Exceptions from "./exceptions/index.js";
export * from "./identifiers.js";
export * as Utils from "./utils.js";
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { inject, injectable, optional } from "@mainsail/container";
import { Contracts, Exceptions, Identifiers } from "@mainsail/contracts";
import { Contracts, Events, Exceptions, Identifiers } from "@mainsail/contracts";
import { TransactionConstructor } from "@mainsail/crypto-transaction";
import { Enums as AppEnums, Utils as AppUtils } from "@mainsail/kernel";
import { Utils as AppUtils } from "@mainsail/kernel";
import { Handlers } from "@mainsail/transactions";
import { BigNumber } from "@mainsail/utils";

Expand Down Expand Up @@ -48,8 +48,7 @@ export class ValidatorRegistrationTransactionHandler extends Handlers.Transactio
}

public emitEvents(transaction: Contracts.Crypto.Transaction, emitter: Contracts.Kernel.EventDispatcher): void {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
emitter.dispatch(AppEnums.ValidatorEvent.Registered, transaction.data);
void emitter.dispatch(Events.ValidatorEvent.Registered, transaction.data);
}

public async throwIfCannotEnterPool(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { inject, injectable, optional } from "@mainsail/container";
import { Contracts, Exceptions, Identifiers } from "@mainsail/contracts";
import { Contracts, Events, Exceptions, Identifiers } from "@mainsail/contracts";
import { TransactionConstructor } from "@mainsail/crypto-transaction";
import { ValidatorRegistrationTransactionHandler } from "@mainsail/crypto-transaction-validator-registration";
import { Enums as AppEnums, Utils as AppUtils } from "@mainsail/kernel";
import { Utils as AppUtils } from "@mainsail/kernel";
import { Handlers } from "@mainsail/transactions";

import { ValidatorResignationTransaction } from "../versions/1.js";
Expand Down Expand Up @@ -51,8 +51,7 @@ export class ValidatorResignationTransactionHandler extends Handlers.Transaction
}

public emitEvents(transaction: Contracts.Crypto.Transaction, emitter: Contracts.Kernel.EventDispatcher): void {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
emitter.dispatch(AppEnums.ValidatorEvent.Resigned, transaction.data);
void emitter.dispatch(Events.ValidatorEvent.Resigned, transaction.data);
}

public async throwIfCannotEnterPool(
Expand Down
Loading
Loading