You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.8 KiB

  1. // Copyright (c) 2018-2020, Zpalmtree
  2. //
  3. // Please see the included LICENSE file for more information.
  4. import {
  5. BLOCK_HASH_CHECKPOINTS_INTERVAL, LAST_KNOWN_BLOCK_HASHES_SIZE,
  6. } from './Constants';
  7. import { SynchronizationStatusJSON } from './JsonSerialization';
  8. export class SynchronizationStatus {
  9. public static fromJSON(json: SynchronizationStatusJSON): SynchronizationStatus {
  10. const synchronizationStatus = Object.create(SynchronizationStatus.prototype);
  11. return Object.assign(synchronizationStatus, {
  12. blockHashCheckpoints: json.blockHashCheckpoints,
  13. lastKnownBlockHashes: json.lastKnownBlockHashes,
  14. lastKnownBlockHeight: json.lastKnownBlockHeight,
  15. });
  16. }
  17. private readonly blockHashCheckpoints: string[] = [];
  18. private readonly lastKnownBlockHashes: string[] = [];
  19. private lastKnownBlockHeight: number = 0;
  20. private lastSavedCheckpointAt: number = 0;
  21. constructor(
  22. lastKnownBlockHeight: number = 0,
  23. blockHashCheckpoints: string[] = [],
  24. lastKnownBlockHashes: string[] = [],
  25. lastSavedCheckpointAt: number = 0) {
  26. if (lastKnownBlockHeight <= 0) {
  27. lastKnownBlockHeight = 0;
  28. }
  29. this.lastKnownBlockHeight = lastKnownBlockHeight;
  30. this.blockHashCheckpoints = blockHashCheckpoints;
  31. this.lastKnownBlockHashes = lastKnownBlockHashes;
  32. this.lastSavedCheckpointAt = lastSavedCheckpointAt;
  33. }
  34. public toJSON(): SynchronizationStatusJSON {
  35. return {
  36. blockHashCheckpoints: this.blockHashCheckpoints,
  37. lastKnownBlockHashes: this.lastKnownBlockHashes,
  38. lastKnownBlockHeight: this.lastKnownBlockHeight,
  39. };
  40. }
  41. public getHeight(): number {
  42. return this.lastKnownBlockHeight;
  43. }
  44. public storeBlockHash(blockHeight: number, blockHash: string): void {
  45. this.lastKnownBlockHeight = blockHeight;
  46. /* Hash already exists */
  47. if (this.lastKnownBlockHashes.length > 0 && this.lastKnownBlockHashes[0] === blockHash) {
  48. return;
  49. }
  50. /* If we're at a checkpoint height, add the hash to the infrequent
  51. checkpoints (at the beginning of the queue) */
  52. if (this.lastSavedCheckpointAt + BLOCK_HASH_CHECKPOINTS_INTERVAL < blockHeight) {
  53. this.lastSavedCheckpointAt = blockHeight;
  54. this.blockHashCheckpoints.unshift(blockHash);
  55. }
  56. this.lastKnownBlockHashes.unshift(blockHash);
  57. /* If we're exceeding capacity, remove the last (oldest) hash */
  58. if (this.lastKnownBlockHashes.length > LAST_KNOWN_BLOCK_HASHES_SIZE) {
  59. this.lastKnownBlockHashes.pop();
  60. }
  61. }
  62. public getBlockCheckpoints(): string[] {
  63. return this.blockHashCheckpoints;
  64. }
  65. public getRecentBlockHashes(): string[] {
  66. return this.lastKnownBlockHashes;
  67. }
  68. }