UNPKG

turtlecoin-wallet-backend

Version:

[![Build Status](https://travis-ci.org/turtlecoin/turtlecoin-wallet-backend-js.svg?branch=master)](https://travis-ci.org/turtlecoin/turtlecoin-wallet-backend-js)

62 lines (61 loc) 2.34 kB
"use strict"; // Copyright (c) 2018, Zpalmtree // // Please see the included LICENSE file for more information. Object.defineProperty(exports, "__esModule", { value: true }); const Constants_1 = require("./Constants"); class SynchronizationStatus { constructor(startHeight = 0) { this.blockHashCheckpoints = []; this.lastKnownBlockHashes = []; this.lastKnownBlockHeight = 0; this.lastSavedCheckpointAt = 0; if (startHeight <= 0) { startHeight = 0; } this.lastKnownBlockHeight = startHeight; } static fromJSON(json) { const synchronizationStatus = Object.create(SynchronizationStatus.prototype); return Object.assign(synchronizationStatus, { blockHashCheckpoints: json.blockHashCheckpoints, lastKnownBlockHashes: json.lastKnownBlockHashes, lastKnownBlockHeight: json.lastKnownBlockHeight, }); } toJSON() { return { blockHashCheckpoints: this.blockHashCheckpoints, lastKnownBlockHashes: this.lastKnownBlockHashes, lastKnownBlockHeight: this.lastKnownBlockHeight, }; } getHeight() { return this.lastKnownBlockHeight; } storeBlockHash(blockHeight, blockHash) { this.lastKnownBlockHeight = blockHeight; /* Hash already exists */ if (this.lastKnownBlockHashes.length > 0 && this.lastKnownBlockHashes[0] === blockHash) { return; } /* If we're at a checkpoint height, add the hash to the infrequent checkpoints (at the beginning of the queue) */ if (this.lastSavedCheckpointAt + Constants_1.BLOCK_HASH_CHECKPOINTS_INTERVAL < blockHeight) { this.lastSavedCheckpointAt = blockHeight; this.blockHashCheckpoints.unshift(blockHash); } this.lastKnownBlockHashes.unshift(blockHash); /* If we're exceeding capacity, remove the last (oldest) hash */ if (this.lastKnownBlockHashes.length > Constants_1.LAST_KNOWN_BLOCK_HASHES_SIZE) { this.lastKnownBlockHashes.pop(); } } getBlockCheckpoints() { return this.blockHashCheckpoints; } getRecentBlockHashes() { return this.lastKnownBlockHashes; } } exports.SynchronizationStatus = SynchronizationStatus;