"use strict";
/**
 * This module contains the logic for selecting the nodes that will be allowed
 * to join the network.
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.executeNodeSelection = executeNodeSelection;
exports.selectNodes = selectNodes;
exports.notifyNewestJoinedConsensors = notifyNewestJoinedConsensors;
exports.notifyingNewestJoinedConsensors = notifyingNewestJoinedConsensors;
exports.drainSelectedPublicKeys = drainSelectedPublicKeys;
exports.forceSelectSelf = forceSelectSelf;
const Context_1 = require("../../Context");
const Self = __importStar(require("../../Self"));
const CycleChain = __importStar(require("../../CycleChain"));
const http = __importStar(require("../../../http"));
const _1 = require(".");
const ModeSystemFuncs_1 = require("../../ModeSystemFuncs");
const utils_1 = require("../../../utils");
const nestedCounters_1 = require("../../../utils/nestedCounters");
const logger_1 = require("../../../logger");
const selectedPublicKeys = new Set();
/** The number of nodes that will try to contact a single joining node about its selection. */
const NUM_NOTIFYING_NODES = 5;
/**
 * Decides how many nodes to accept into the network, then selects nodes that
 * will be allowed to join. If this node isn't active yet, selection will be
 * skipped.
 */
function executeNodeSelection() {
    // Only if the node is active or if the network is in restart mode
    if (Self.isActive || (!Self.isActive && Self.isRestartNetwork)) {
        const { add } = (0, ModeSystemFuncs_1.calculateToAcceptV2)(CycleChain.newest);
        /* prettier-ignore */ if (logger_1.logFlags.p2pNonFatal && logger_1.logFlags.console)
            console.log(`selecting ${add} nodes to accept`);
        selectNodes(add);
    }
    else {
        /* prettier-ignore */ if (logger_1.logFlags.p2pNonFatal && logger_1.logFlags.console)
            console.warn('not selecting nodes because we are not active yet');
        return;
    }
}
/**
 * Selects the nodes to be allowed to join.
 * Iterates through all standby nodes and pick the best ones by their scores
 * (`selectionNum`)
 *
 * @returns The list of public keys of the nodes that have been selected.
 */
function selectNodes(maxAllowed) {
    const standbyNodesInfo = (0, _1.getStandbyNodesInfoMap)();
    /* prettier-ignore */ if (logger_1.logFlags.p2pNonFatal && logger_1.logFlags.console)
        console.log('selecting from standbyNodesInfo', standbyNodesInfo);
    // construct a list of objects that we'll sort by `selectionNum`. we'll use
    // the public key to get the join request associated with the public key and
    // inform the node later that it has been accepted
    const objs = [];
    for (const [publicKey, info] of standbyNodesInfo) {
        objs.push({
            publicKey,
            selectionNum: info.selectionNum,
            appJoinData: info.appJoinData,
        });
    }
    // sort the objects by their selection numbers
    objs.sort((a, b) => (a.selectionNum < b.selectionNum ? 1 : a.selectionNum > b.selectionNum ? -1 : 0));
    let offset = 0;
    const cycleMarker = CycleChain.getCurrentCycleMarker();
    if (cycleMarker) {
        const first8HexChars = cycleMarker.substring(0, 8);
        offset = parseInt(first8HexChars, 16);
    }
    /* prettier-ignore */ if (logger_1.logFlags.p2pNonFatal && logger_1.logFlags.console)
        console.log('Input parameters to selectIndexesWithOffset - Max allowed:', maxAllowed, 'Offset:', offset, 'Array Size: ', objs.length);
    if (maxAllowed > objs.length) {
        /* prettier-ignore */ nestedCounters_1.nestedCountersInstance.countEvent('joinV2', `selectNodes: capping maxAllowed ${maxAllowed} to ${objs.length}`);
        maxAllowed = objs.length;
    }
    if (offset >= 0 && maxAllowed >= 1 && maxAllowed <= objs.length) {
        const selectedIndexes = (0, utils_1.selectIndexesWithOffeset)(objs.length, maxAllowed, offset);
        /* prettier-ignore */ if (logger_1.logFlags.p2pNonFatal && logger_1.logFlags.console)
            console.log("SelectedIndexes: ", selectedIndexes);
        for (let i = 0; i < selectedIndexes.length; i++) {
            // eslint-disable-next-line security/detect-object-injection
            const selectedIndex = selectedIndexes[i];
            // eslint-disable-next-line security/detect-object-injection
            selectedPublicKeys.add(objs[selectedIndex].publicKey);
        }
    }
    else {
        /* prettier-ignore */ if (logger_1.logFlags.p2pNonFatal && logger_1.logFlags.console)
            console.log(`Invalid input parameters for selection: array length: ${objs.length} maxAllowed:${maxAllowed} offset:${offset}`);
        /* prettier-ignore */ nestedCounters_1.nestedCountersInstance.countEvent('joinV2', 'selectNodes: Invalid input parameters for selection');
    }
    // If golden ticket is enabled, add nodes with adminCert + golden ticket to selectedPublicKeys if they are not already there
    if (Context_1.shardus.config.p2p.goldenTicketEnabled === true) {
        for (const obj of objs) {
            if (obj.appJoinData?.adminCert?.goldenTicket === true && !selectedPublicKeys.has(obj.publicKey)) {
                /* prettier-ignore */ if (logger_1.logFlags.p2pNonFatal && logger_1.logFlags.console)
                    console.log('selecting golden ticket nodes from standbyNodesInfo');
                selectedPublicKeys.add(obj.publicKey);
            }
        }
    }
}
/**
 * Notifies the nodes that have been selected that they have been selected by
 * calling their `accepted` endpoints.`
 */
async function notifyNewestJoinedConsensors() {
    return; //accepted endpoint seem deprecated and always fails!
    //   const counter = CycleChain.getNewest().counter
    //   if (!Self.isActive) {
    //     if (Self.isRestartNetwork && Self.isFirst) {
    //       nestedCountersInstance.countEvent('joinV2', `C${counter}: notifyNewestJoinedConsensors: isRestartNetwork && isFirst`)
    //       notifyingNewestJoinedConsensors()
    //       // // decide if we should be in charge of notifying joining nodes
    //       // const params = {
    //       //   getOurNodeIndex: CycleChain.getNewest().mode === 'restart' ? 0 : getOurNodeIndex(),
    //       //   activeByIdOrderLength:
    //       //     CycleChain.getNewest().mode === 'restart' ? 1 : NodeList.activeByIdOrder.length,
    //       //   NUM_NOTIFYING_NODES,
    //       //   CycleChainNewestCounter: CycleChain.newest.counter,
    //       // }
    //       // console.log(`C${counter} fastIsPicked params: ${JSON.stringify(params)}`)
    //       // const shouldNotify = fastIsPicked(
    //       //   CycleChain.getNewest().mode === 'restart' ? 1 : getOurNodeIndex(),
    //       //   CycleChain.getNewest().mode === 'restart' ? 0 : NodeList.activeByIdOrder.length,
    //       //   NUM_NOTIFYING_NODES,
    //       //   CycleChain.newest.counter
    //       // )
    //       // console.log(`C${counter} shouldNotify: ${shouldNotify}`)
    //     } else console.warn(`C${counter} not notifying nodes because we are not active yet`)
    //     return
    //   }
    //   // decide if we should be in charge of notifying joining nodes
    //   const params = {
    //     getOurNodeIndex: getOurNodeIndex(),
    //     activeByIdOrderLength: NodeList.activeByIdOrder.length,
    //     NUM_NOTIFYING_NODES,
    //     CycleChainNewestCounter: CycleChain.newest.counter
    //   }
    //   console.log(`C${counter} fastIsPicked params: ${JSON.stringify(params)}`)
    //   const shouldNotify = fastIsPicked(
    //     getOurNodeIndex(),
    //     NodeList.activeByIdOrder.length,
    //     NUM_NOTIFYING_NODES,
    //     CycleChain.newest.counter
    //   )
    //   // if so, do so
    //   if (shouldNotify) {
    //     nestedCountersInstance.countEvent('joinV2', `C${counter}: notifyNewestJoinedConsensors: shouldNotify`)
    //     notifyingNewestJoinedConsensors()
    //   }
}
async function notifyingNewestJoinedConsensors() {
    const marker = CycleChain.getCurrentCycleMarker();
    const counter = CycleChain.getNewest().counter;
    for (const joinedConsensor of CycleChain.newest.joinedConsensors) {
        const publicKey = joinedConsensor.publicKey;
        // no need to notify ourselves
        if (publicKey === Context_1.crypto.keypair.publicKey)
            continue;
        console.log(`C${counter} notifying node`, publicKey, 'that it has been selected');
        // sign an acceptance offer
        const offer = Context_1.crypto.sign({
            cycleMarker: marker,
            activeNodePublicKey: Context_1.crypto.keypair.publicKey,
        });
        // make the call, but don't await. it might take a while.
        http.post(`http://${joinedConsensor.externalIp}:${joinedConsensor.externalPort}/accepted`, offer).catch((e) => {
            nestedCounters_1.nestedCountersInstance.countEvent('joinV2', `C${counter}: notifyingNewestJoinedConsensors: http post failed`);
            console.error(`C${counter} failed to notify node ${publicKey} that it has been selected:`, e);
        });
    }
}
/**
 * Returns the list of public keys of the nodes that have been selected and
 * empties the list.
 */
function drainSelectedPublicKeys() {
    const tmp = [...selectedPublicKeys.values()];
    selectedPublicKeys.clear();
    return tmp;
}
function forceSelectSelf() {
    selectedPublicKeys.add(Context_1.crypto.keypair.publicKey);
}
//# sourceMappingURL=select.js.map