Skip to content

Commit 6ecd6cf

Browse files
refactor(consensus): use toLocaleString for height and round in logs (#1078)
* getHeightRoundString * Use getBlockString * Improve restore string
1 parent 231c130 commit 6ecd6cf

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

packages/consensus/source/consensus.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ export class Consensus implements Contracts.Consensus.Service {
206206

207207
const roundState = this.roundStateRepository.getRoundState(this.#blockNumber, this.#round);
208208
this.logger.info(
209-
`>> Starting new round: ${this.#blockNumber}/${this.#round} with proposer: ${roundState.proposer.address}`,
209+
`>> Starting new round: ${this.#getHeightRoundString()} with proposer: ${roundState.proposer.address}`,
210210
);
211211

212212
await this.eventDispatcher.dispatch(Events.ConsensusEvent.RoundStarted, this.getState());
@@ -324,7 +324,7 @@ export class Consensus implements Contracts.Consensus.Service {
324324
return;
325325
}
326326

327-
this.logger.info(`Received +2/3 prevotes for ${this.#blockNumber}/${this.#round}/null`);
327+
this.logger.info(`Received +2/3 prevotes for ${this.#getHeightRoundString()}/null`);
328328

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

@@ -351,10 +351,10 @@ export class Consensus implements Contracts.Consensus.Service {
351351
this.#didMajorityPrecommit = true;
352352
const block = roundState.getBlock();
353353

354-
this.logger.info(`Received +2/3 precommits for ${this.#blockNumber}/${roundState.round}/${block.data.hash}`);
354+
this.logger.info(`Received +2/3 precommits for ${this.#getBlockString(block)}`);
355355

356356
if (!roundState.getProcessorResult().success) {
357-
this.logger.info(`Block ${this.#blockNumber}/${roundState.round}/${block.data.hash} is invalid`);
357+
this.logger.info(`Block ${this.#getBlockString(block)} is invalid`);
358358
return;
359359
}
360360

@@ -395,7 +395,7 @@ export class Consensus implements Contracts.Consensus.Service {
395395
return;
396396
}
397397

398-
this.logger.info(`Timeout to propose ${this.#blockNumber}/${this.#round} expired`);
398+
this.logger.info(`Timeout to propose ${this.#getHeightRoundString()} expired`);
399399

400400
this.#step = Contracts.Consensus.Step.Prevote;
401401
await this.prevote();
@@ -412,7 +412,7 @@ export class Consensus implements Contracts.Consensus.Service {
412412
return;
413413
}
414414

415-
this.logger.info(`Timeout to prevote ${this.#blockNumber}/${this.#round} expired`);
415+
this.logger.info(`Timeout to prevote ${this.#getHeightRoundString()} expired`);
416416
this.roundStateRepository.getRoundState(this.#blockNumber, this.#round).logPrevotes();
417417

418418
this.#step = Contracts.Consensus.Step.Precommit;
@@ -426,7 +426,7 @@ export class Consensus implements Contracts.Consensus.Service {
426426
return;
427427
}
428428

429-
this.logger.info(`Timeout to precommit ${this.#blockNumber}/${this.#round} expired`);
429+
this.logger.info(`Timeout to precommit ${this.#getHeightRoundString()} expired`);
430430
this.roundStateRepository.getRoundState(this.#blockNumber, this.#round).logPrevotes();
431431
this.roundStateRepository.getRoundState(this.#blockNumber, this.#round).logPrecommits();
432432

@@ -548,8 +548,11 @@ export class Consensus implements Contracts.Consensus.Service {
548548
this.#lockedValue = state.lockedValue;
549549
this.#validValue = state.validValue;
550550
} else {
551+
const storedBlockNumber = state.blockNumber.toLocaleString(Constants.Locale);
552+
const currentBlockNumber = this.#blockNumber.toLocaleString(Constants.Locale);
553+
551554
this.logger.warning(
552-
`Skipping state restore, because stored block number is ${state.blockNumber}, but should be ${this.#blockNumber}`,
555+
`Skipping state restore, because stored block number is ${storedBlockNumber}, but should be ${currentBlockNumber}`,
553556
);
554557

555558
this.roundStateRepository.clear();
@@ -565,7 +568,7 @@ export class Consensus implements Contracts.Consensus.Service {
565568
}
566569

567570
this.logger.info(
568-
`Completed consensus bootstrap for ${this.#blockNumber}/${this.#round} with total round ${this.stateStore.getTotalRound()}`,
571+
`Completed consensus bootstrap for ${this.#getHeightRoundString()} with total round ${this.stateStore.getTotalRound()}`,
569572
);
570573

571574
await this.eventDispatcher.dispatch(Events.ConsensusEvent.Bootstrapped, this.getState());
@@ -599,6 +602,13 @@ export class Consensus implements Contracts.Consensus.Service {
599602
}
600603
}
601604

605+
#getHeightRoundString(): string {
606+
const number = this.#blockNumber.toLocaleString(Constants.Locale);
607+
const consensusRound = this.#round.toLocaleString(Constants.Locale);
608+
609+
return `${number}/${consensusRound}`;
610+
}
611+
602612
#getBlockString(block: Contracts.Crypto.Block): string {
603613
const number = this.#blockNumber.toLocaleString(Constants.Locale);
604614
const consensusRound = this.#round.toLocaleString(Constants.Locale);

0 commit comments

Comments
 (0)