UNPKG

stellar-plus

Version:

beta version of stellar-plus, an all-in-one sdk for the Stellar blockchain

97 lines (96 loc) 5.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const account_1 = require("../../../stellar-plus/account"); const asset_1 = require("../../../stellar-plus/asset"); const network_1 = require("../../../stellar-plus/network"); const stellar_test_ledger_1 = require("../../../stellar-plus/test/stellar-test-ledger"); const soroban_transaction_1 = require("../../../stellar-plus/utils/pipeline/plugins/soroban-transaction"); const utils_1 = require("../../../tests/utils"); describe('Profiler Plugin Use Case: ', () => { const logLevel = 'TRACE'; const stellarTestLedger = new stellar_test_ledger_1.StellarTestLedger({ logLevel, network: stellar_test_ledger_1.TestLedgerNetwork.LOCAL, containerImageVersion: stellar_test_ledger_1.SupportedImageVersions.LASTEST, }); let networkConfig; let profilerPlugin; let transactionCount; beforeAll(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () { // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const container = yield stellarTestLedger.start(); expect(container).toBeDefined(); networkConfig = (0, network_1.CustomNet)(yield stellarTestLedger.getNetworkConfiguration()); expect(networkConfig.horizonUrl).toBeDefined(); expect(networkConfig.networkPassphrase).toBeDefined(); expect(networkConfig.rpcUrl).toBeDefined(); expect(networkConfig.friendbotUrl).toBeDefined(); })); afterAll(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () { yield stellarTestLedger.stop(); yield stellarTestLedger.destroy(); })); it('should initialize a profiler plugin', () => { transactionCount = 0; profilerPlugin = new soroban_transaction_1.ProfilerPlugin(); expect(profilerPlugin).toBeDefined(); expect(profilerPlugin.data.getLog()).toBeDefined(); expect(profilerPlugin.data.getLog().length).toBe(transactionCount); }); describe('Given an initialized profiler plugin', () => { let issuer; let asset; beforeAll(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () { issuer = new account_1.DefaultAccountHandler({ networkConfig }); yield issuer.initializeWithFriendbot(); expect(issuer).toBeDefined(); })); it('should be accepted by a soroban pipeline as plugin', () => { asset = new asset_1.SACHandler({ code: 'SPLUS', issuerAccount: issuer, networkConfig, options: { sorobanTransactionPipeline: { plugins: [profilerPlugin], }, }, }); expect(asset).toBeDefined(); expect(asset.sorobanTokenHandler.sorobanTransactionPipeline.plugins.length).toBe(1); expect(asset.sorobanTokenHandler.sorobanTransactionPipeline.plugins).toStrictEqual([profilerPlugin]); }); it('should collect metrics of a soroban transaction', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { transactionCount += 1; yield asset.wrapAndDeploy((0, utils_1.simpleTxInvocation)(issuer)); expect(profilerPlugin.data.getLog()).toBeDefined(); expect(profilerPlugin.data.getLog().length).toBe(transactionCount); })); it('should collect metrics of multiple soroban transaction', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { transactionCount += 3; yield asset.sorobanTokenHandler.transfer(Object.assign({ to: issuer.getPublicKey(), from: issuer.getPublicKey(), amount: BigInt(10) }, (0, utils_1.simpleTxInvocation)(issuer))); yield asset.sorobanTokenHandler.transfer(Object.assign({ to: issuer.getPublicKey(), from: issuer.getPublicKey(), amount: BigInt(5) }, (0, utils_1.simpleTxInvocation)(issuer))); yield asset.sorobanTokenHandler.transfer(Object.assign({ to: issuer.getPublicKey(), from: issuer.getPublicKey(), amount: BigInt(10) }, (0, utils_1.simpleTxInvocation)(issuer))); expect(profilerPlugin.data.getLog()).toBeDefined(); expect(profilerPlugin.data.getLog().length).toBe(transactionCount); })); it('should filter metrics from the collected data', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { expect(profilerPlugin.data.getLog({ filter: { methods: ['createContract'] } }).length).toBe(1); })); it('should aggregate metrics from the collected data', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { expect(profilerPlugin.data.getLog({ aggregate: { feeCharged: { method: 'sum' } } })[0].feeCharged).toBeWithin(800000, 900000); })); it('should filter and aggregate metrics from the collected data', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { expect(profilerPlugin.data.getLog({ filter: { methods: ['createContract'] }, aggregate: { feeCharged: { method: 'sum' } }, })[0].feeCharged).toBeWithin(700000, 800000); })); it('should clear the logs', () => { transactionCount = 0; profilerPlugin.data.clearLog(); expect(profilerPlugin.data.getLog().length).toBe(transactionCount); }); }); });