"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseBlock = void 0;
const base_1 = require("./base");
class BaseBlock extends base_1.BaseModel {
    constructor(storage) {
        super('blocks', storage);
        this.allowedPaging = [
            {
                key: 'height',
                type: 'number'
            }
        ];
    }
    async onConnect() {
        this.collection.createIndex({ hash: 1 }, { background: true });
        this.collection.createIndex({ chain: 1, network: 1, processed: 1, height: -1 }, { background: true });
        this.collection.createIndex({ chain: 1, network: 1, timeNormalized: 1 }, { background: true });
        this.collection.createIndex({ previousBlockHash: 1 }, { background: true });
    }
    getPoolInfo(coinbase) {
        // TODO need to make this actually parse the coinbase input and map to miner strings
        // also should go somewhere else
        return coinbase;
    }
    async getLocalTip({ chain, network }) {
        const tip = await this.collection.findOne({ chain, network, processed: true }, { sort: { height: -1 } });
        return tip;
    }
    async validateLocatorHashes(params) {
        const { chain, network } = params;
        const headers = new Array();
        const locatorBlocks = await this.collection
            .find({
            processed: true,
            chain,
            network
        })
            .sort({ height: -1 })
            .limit(100)
            .project({ hash: 1, previousBlockHash: 1, nextBlockHash: 1 })
            .addCursorFlag('noCursorTimeout', true)
            .toArray();
        for (let i = 0; i < locatorBlocks.length; i++) {
            let prevMatch = true;
            let nextMatch = true;
            if (i != 0) {
                prevMatch = prevMatch && locatorBlocks[i].nextBlockHash === locatorBlocks[i - 1].hash;
                nextMatch = nextMatch && locatorBlocks[i].hash === locatorBlocks[i - 1].previousBlockHash;
            }
            if (i != locatorBlocks.length - 1) {
                prevMatch = prevMatch && locatorBlocks[i].hash === locatorBlocks[i + 1].nextBlockHash;
                nextMatch = nextMatch && locatorBlocks[i].previousBlockHash === locatorBlocks[i + 1].hash;
            }
            if (!prevMatch || !nextMatch) {
                headers.push(locatorBlocks[i]);
            }
        }
        return headers;
    }
}
exports.BaseBlock = BaseBlock;
//# sourceMappingURL=baseBlock.js.map