"use strict";
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 });
/* eslint-disable no-console */
const domainobjs_1 = require("@maci-protocol/domainobjs");
const config_1 = require("hardhat/config");
const logger_1 = require("../../ts/logger");
const Deployment_1 = require("../helpers/Deployment");
const types_1 = require("../helpers/types");
(0, config_1.task)("benchmark", "Run benchmarks").setAction(async (_, hre) => {
    const deployment = Deployment_1.Deployment.getInstance({ hre });
    deployment.setHre(hre);
    const deployer = await deployment.getDeployer();
    const isIncremental = hre.network.name !== "hardhat" && hre.network.name !== "localhost";
    // deploy MACI
    const steps = await deployment.start("full", { incremental: isIncremental, verify: false });
    await deployment.runSteps(steps, 0);
    // update poll start and end dates to ensure it's in the future and we have time to send votes
    const timeNow = Math.floor(Date.now() / 1000);
    const pollDuration = 1000000;
    deployment.updateDeployConfig(types_1.EContracts.Poll, "pollStartDate", timeNow - pollDuration);
    deployment.updateDeployConfig(types_1.EContracts.Poll, "pollEndDate", timeNow + pollDuration);
    // deploy a Poll
    const pollSteps = await deployment.start("poll", { incremental: true, verify: false });
    await deployment.runSteps(pollSteps, 0);
    try {
        const startBalance = await deployer.provider.getBalance(deployer);
        const maxBatchSize = process.env.BENCHMARK_MAX_BATCH_SIZE ? Number(process.env.BENCHMARK_MAX_BATCH_SIZE) : 100;
        (0, logger_1.logMagenta)({ text: "======================================================================" });
        (0, logger_1.logMagenta)({ text: `Starting balance: ${Number(startBalance / 10n ** 12n) / 1e6}\n` });
        // generate a message
        const keypair = new domainobjs_1.Keypair();
        const coordinatorKeypair = new domainobjs_1.Keypair();
        const command = new domainobjs_1.VoteCommand(1n, keypair.publicKey, 0n, 9n, 1n, 0n, 0n);
        const signature = command.sign(keypair.privateKey);
        // not recommended to use the same key for the message but this is just for benchmarking
        const sharedKey = domainobjs_1.Keypair.generateEcdhSharedKey(keypair.privateKey, coordinatorKeypair.publicKey);
        const message = command.encrypt(signature, sharedKey);
        const { publishBatch } = await Promise.resolve().then(() => __importStar(require("../helpers/benchmarks")));
        await publishBatch(deployment, message, keypair, maxBatchSize);
        const endBalance = await deployer.provider.getBalance(deployer);
        (0, logger_1.logMagenta)({ text: `Ending balance: ${Number(endBalance / 10n ** 12n) / 1e6}\n` });
        (0, logger_1.logMagenta)({ text: "======================================================================" });
    }
    catch (err) {
        (0, logger_1.logRed)({
            text: `\n=========================================================\nERROR: ${err.message}\n`,
        });
    }
});
//# sourceMappingURL=benchmarks.js.map