UNPKG

@ton.js/core

Version:

TonWeb - JavaScript API for TON blockchain

101 lines 3.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InMemoryBlockStorage = void 0; /** * Simple in-memory implementation of the processed * block number storage. */ class InMemoryBlockStorage { constructor(logFunction) { this.logFunction = logFunction; /** * @todo: should we use `Map` here? * Map of the processed masterchain blocks: * `key` is the block number, while * `value` reflects `isProcessed` state. */ this.masterBlocks = {}; /** * @todo: should we use `Map` here? * Map of the processed shardchain blocks: * The `key` should be constructed this way: * `${workchain}_${shardId}_${shardBlockNumber}` * and the `value` reflects `isProcessed` state. */ this.shardBlocks = {}; } async insertBlocks(mcBlockNumber, shardBlockNumbers) { var _a; (_a = this.logFunction) === null || _a === void 0 ? void 0 : _a.call(this, 'mc processed ' + mcBlockNumber); if (this.masterBlocks[mcBlockNumber] !== undefined) throw new Error('mc already exists ' + mcBlockNumber); this.masterBlocks[mcBlockNumber] = true; await this.insertShardBlocks(shardBlockNumbers); } async getLastMasterchainBlockNumber() { const blockNumbers = Object.keys(this.masterBlocks) .map(x => Number(x)) .sort((a, b) => b - a); return blockNumbers[0]; } async setBlockProcessed(workchain, shardId, shardBlockNumber, prevShardBlocks) { var _a; const shardBlockKey = this.getShardBlockKey({ workchain, shardId, shardBlockNumber, }); (_a = this.logFunction) === null || _a === void 0 ? void 0 : _a.call(this, `processing shard: ${shardBlockKey}`); if (this.shardBlocks[shardBlockKey] === undefined) throw new Error(`Shard doesn't exist: ${shardBlockKey}`); this.shardBlocks[shardBlockKey] = true; await this.insertShardBlocks(prevShardBlocks); } async getUnprocessedShardBlock() { for (const key in this.shardBlocks) { if (this.shardBlocks[key] === false) { return this.parseShardBlockKey(key); } } return undefined; } /** * Inserts new unprocessed shardchain block numbers. * Block number (workchain + shardId + shardBlockNumber) should be IGNORED if it is already in the storage. */ async insertShardBlocks(shardBlockNumbers) { var _a; for (const shardBlock of shardBlockNumbers) { const shardBlockKey = this.getShardBlockKey(shardBlock); if (this.shardBlocks[shardBlockKey] !== undefined) continue; (_a = this.logFunction) === null || _a === void 0 ? void 0 : _a.call(this, `insert shard: ${shardBlockKey}`); this.shardBlocks[shardBlockKey] = false; } } /** * Generates unique key for identifying the specified * shardchain block. */ getShardBlockKey(shardBlock) { return [ shardBlock.workchain, shardBlock.shardId, shardBlock.shardBlockNumber, ].join('_'); } /** * Parses the specified shardchain block key and returns * a shardchain block definition. */ parseShardBlockKey(key) { const parts = key.split('_'); return { workchain: Number(parts[0]), shardId: parts[1], shardBlockNumber: Number(parts[2]), }; } } exports.InMemoryBlockStorage = InMemoryBlockStorage; //# sourceMappingURL=in-memory-block-storage.js.map