"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 });
const chai_1 = require("chai");
const sinon = __importStar(require("sinon"));
const crypto_wallet_core_1 = require("@bitpay-labs/crypto-wallet-core");
const config_1 = require("../../../src/services/config");
const p2p_1 = require("../../../src/services/p2p");
const p2p_2 = require("../../../src/providers/chain-state/evm/p2p/p2p");
const csp_1 = require("../../../src/providers/chain-state/evm/api/csp");
const unit_1 = require("../../helpers/unit");
const gethMainnet24486902_1 = require("../../data/ETH/gethMainnet24486902");
const rpcs_1 = require("../../../src/providers/chain-state/evm/p2p/rpcs");
describe('P2P Service', function () {
    const sandbox = sinon.createSandbox();
    class MockP2PWorker extends p2p_1.BaseP2PWorker {
        constructor(params) {
            super(params);
            this.started = false;
            this.started = true;
        }
    }
    before(unit_1.unitBeforeHelper);
    after(unit_1.unitAfterHelper);
    afterEach(() => {
        sandbox.restore();
    });
    it('should have a test which runs', function () {
        (0, chai_1.expect)(true).to.equal(true);
    });
    it('should register a class', () => {
        const chain = 'TEST';
        const network = 'test';
        p2p_1.P2P.register(chain, network, MockP2PWorker);
        const registered = p2p_1.P2P.get(chain, network);
        (0, chai_1.expect)(registered).to.deep.eq(MockP2PWorker);
    });
    it('should start the p2p class', async () => {
        const chain = 'TEST';
        const network = 'testnet';
        const fakeConfig = {};
        p2p_1.P2P.register(chain, network, MockP2PWorker);
        sandbox.stub(config_1.Config, 'chainNetworks').returns([{ chain, network }]);
        sandbox
            .stub(config_1.Config, 'chainConfig')
            .withArgs({ chain, network })
            .returns(fakeConfig);
        (0, chai_1.expect)(p2p_1.P2P.workers).to.deep.eq([]);
        await p2p_1.P2P.start();
        (0, chai_1.expect)(`Worker length: ${p2p_1.P2P.workers.length}`).to.eq('Worker length: 1');
        (0, chai_1.expect)(p2p_1.P2P.workers[0]).to.exist;
        const worker = p2p_1.P2P.workers[0];
        (0, chai_1.expect)(worker.started).to.eq(true);
        await p2p_1.P2P.stop();
        (0, chai_1.expect)(p2p_1.P2P.workers).to.deep.eq([]);
    });
    it('should not start if disabled', async () => {
        const chain = 'TEST';
        const network = 'test';
        p2p_1.P2P.register(chain, network, MockP2PWorker);
        sandbox
            .stub(config_1.Config, 'isDisabled')
            .withArgs('p2p')
            .returns(true);
        (0, chai_1.expect)(p2p_1.P2P.workers).to.deep.eq([]);
        await p2p_1.P2P.start();
        (0, chai_1.expect)(p2p_1.P2P.workers.length).to.eq(0);
    });
    it('should not start if config has disabled', async () => {
        const chain = 'TEST';
        const network = 'testnet';
        const fakeConfig = { disabled: true };
        p2p_1.P2P.register(chain, network, MockP2PWorker);
        sandbox.stub(config_1.Config, 'chainNetworks').returns([{ chain, network }]);
        sandbox
            .stub(config_1.Config, 'chainConfig')
            .withArgs({ chain, network })
            .returns(fakeConfig);
        (0, chai_1.expect)(p2p_1.P2P.workers).to.deep.eq([]);
        await p2p_1.P2P.start();
        (0, chai_1.expect)(p2p_1.P2P.workers.length).to.eq(0);
    });
    it('should not start if config has chainSource other than p2p', async () => {
        const chain = 'TEST';
        const network = 'testnet';
        const fakeConfig = { chainSource: 'rpc' };
        p2p_1.P2P.register(chain, network, MockP2PWorker);
        sandbox.stub(config_1.Config, 'chainNetworks').returns([{ chain, network }]);
        sandbox
            .stub(config_1.Config, 'chainConfig')
            .withArgs({ chain, network })
            .returns(fakeConfig);
        (0, chai_1.expect)(p2p_1.P2P.workers).to.deep.eq([]);
        await p2p_1.P2P.start();
        (0, chai_1.expect)(p2p_1.P2P.workers.length).to.eq(0);
    });
    it('should convert an EVM block', async function () {
        sandbox.stub(rpcs_1.Rpcs.geth.prototype, 'getTransactionsFromBlock').resolves([]);
        sandbox.stub(csp_1.BaseEVMStateProvider, 'initializeRpcs');
        sandbox.stub(config_1.Config, 'get').returns({ chains: { ETH: { mainnet: {} } } });
        class MockEVMP2pWorker extends p2p_2.EVMP2pWorker {
            constructor(params) {
                super(params);
                this.rpc = new rpcs_1.Rpcs.geth(sandbox.stub());
            }
        }
        const p2p = new MockEVMP2pWorker({ chain: 'ETH', network: 'mainnet', chainConfig: {} });
        const converted = await p2p.convertBlock(gethMainnet24486902_1.block);
        (0, chai_1.expect)(converted.convertedTxs.every(tx => {
            return (tx.to === '' || tx.to === crypto_wallet_core_1.Web3.utils.toChecksumAddress(tx.to)) &&
                tx.from === crypto_wallet_core_1.Web3.utils.toChecksumAddress(tx.from);
        })).to.equal(true);
    });
});
//# sourceMappingURL=p2p.test.js.map