Skip to content

Commit

Permalink
feat(validator): log loaded validators (#477)
Browse files Browse the repository at this point in the history
* Inject

* Print method

* Print on bootstrap

* Rename method

* Execute after state check

* Improve text

* Log 0 validators

* Determine activa & standby

* Wallet to string
  • Loading branch information
sebastijankuzner authored Mar 8, 2024
1 parent 8186062 commit b4d5ff7
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 3 deletions.
5 changes: 5 additions & 0 deletions packages/bootstrap/source/bootstrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export class Bootstrapper {
@inject(Identifiers.State.Verifier)
private readonly stateVerifier!: Contracts.State.StateVerifier;

@inject(Identifiers.Validator.Repository)
private readonly validatorRepository!: Contracts.Validator.ValidatorRepository;

@inject(Identifiers.TransactionPool.Service)
private readonly transactionPool!: Contracts.TransactionPool.Service;

Expand Down Expand Up @@ -83,6 +86,8 @@ export class Bootstrapper {

this.stateVerifier.verifyWalletsConsistency();

this.validatorRepository.printLoadedValidators();

await this.transactionPool.reAddTransactions();

void this.consensus.run();
Expand Down
1 change: 0 additions & 1 deletion packages/consensus/source/consensus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export class Consensus implements Contracts.Consensus.ConsensusService {
@inject(Identifiers.Consensus.Scheduler)
private readonly scheduler!: Contracts.Consensus.Scheduler;

// TODO: Rename identifier
@inject(Identifiers.Validator.Repository)
private readonly validatorsRepository!: Contracts.Validator.ValidatorRepository;

Expand Down
2 changes: 2 additions & 0 deletions packages/contracts/source/contracts/state/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export interface Wallet extends Omit<Repository, "fromJson" | "commitChanges"> {

fromJson(data: JsonObject): Wallet;
commitChanges(walletRepository: WalletRepository): void;

toString(): string;
}

export interface ValidatorWallet {
Expand Down
1 change: 1 addition & 0 deletions packages/contracts/source/contracts/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export interface Validator {

export interface ValidatorRepository {
getValidator(publicKey: string): Validator | undefined;
printLoadedValidators(): void;
}
8 changes: 8 additions & 0 deletions packages/state/source/wallets/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,12 @@ export class Wallet implements Contracts.State.Wallet {

return this;
}

public toString(): string {
if (this.hasAttribute("username")) {
return this.getAttribute<string>("username");
}

return this.address;
}
}
58 changes: 56 additions & 2 deletions packages/validator/source/validator-repository.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { injectable } from "@mainsail/container";
import { Contracts } from "@mainsail/contracts";
import { inject, injectable } from "@mainsail/container";
import { Contracts, Identifiers } from "@mainsail/contracts";

@injectable()
export class ValidatorRepository implements Contracts.Validator.ValidatorRepository {
@inject(Identifiers.State.Service)
private readonly stateService!: Contracts.State.Service;

@inject(Identifiers.ValidatorSet.Service)
private readonly validatorSetService!: Contracts.ValidatorSet.Service;

@inject(Identifiers.Services.Log.Service)
private readonly logger!: Contracts.Kernel.Logger;

#validators!: Map<string, Contracts.Validator.Validator>;

configure(validators: Contracts.Validator.Validator[]): ValidatorRepository {
Expand All @@ -14,4 +23,49 @@ export class ValidatorRepository implements Contracts.Validator.ValidatorReposit
public getValidator(consensusPublicKey: string): Contracts.Validator.Validator | undefined {
return this.#validators.get(consensusPublicKey);
}

public printLoadedValidators(): void {
if (this.#validators.size === 0) {
this.logger.info("No validators found on this node");
return;
}

this.logger.info(`A total of ${this.#validators.size} validators(s) were found this node:`);

const validators = this.stateService.getStore().walletRepository.allValidators();
const activeValidators = this.validatorSetService.getActiveValidators();

const active: string[] = [];
const standBy: string[] = [];
const resigned: string[] = [];
const notRegistered: string[] = [];

for (const consensusPublicKey of this.#validators.keys()) {
const validator = validators.find(
(validator) => validator.getAttribute("validatorPublicKey") === consensusPublicKey,
);

if (validator) {
if (validator.hasAttribute("validatorResigned")) {
resigned.push(validator.toString());
}
if (
activeValidators.some(
(activeValidator) => activeValidator.getConsensusPublicKey() === consensusPublicKey,
)
) {
active.push(validator.toString());
} else {
standBy.push(validator.toString());
}
} else {
notRegistered.push(consensusPublicKey);
}
}

this.logger.info(`Active validators (${active.length}): [${active}]`);
this.logger.info(`Stand by validators (${standBy.length}): [${standBy}]`);
this.logger.info(`Resigned validators (${resigned.length}): [${resigned}]`);
this.logger.info(`Undefined validators (${notRegistered.length}): [${notRegistered}]`);
}
}

0 comments on commit b4d5ff7

Please sign in to comment.