|
| 1 | +import { ObjectId } from 'bson'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { Request, Response } from 'express-serve-static-core'; |
| 4 | +import * as sinon from 'sinon'; |
| 5 | +import { PassThrough } from 'stream'; |
| 6 | +import { MongoBound } from '../../../src/models/base'; |
| 7 | +import { IWallet, WalletStorage } from '../../../src/models/wallet'; |
| 8 | +import { WalletAddressStorage } from '../../../src/models/walletAddress'; |
| 9 | +import { EVMTransactionStorage } from '../../../src/providers/chain-state/evm/models/transaction'; |
| 10 | +import { intAfterHelper, intBeforeHelper } from '../../helpers/integration'; |
| 11 | + |
| 12 | +const chain = 'ETH'; |
| 13 | +const network = 'regtest'; |
| 14 | + |
| 15 | +describe('EVM Memory Leak Prevention', function() { |
| 16 | + const suite = this; |
| 17 | + this.timeout(30000); |
| 18 | + let globalSandbox: sinon.SinonSandbox; |
| 19 | + let ETH: any; |
| 20 | + |
| 21 | + before(intBeforeHelper); |
| 22 | + after(async () => intAfterHelper(suite)); |
| 23 | + |
| 24 | + beforeEach(async () => { |
| 25 | + globalSandbox = sinon.createSandbox(); |
| 26 | + // Use the ETH module instance |
| 27 | + const { ETH: ETHModule } = await import('../../../src/modules/ethereum/api/csp'); |
| 28 | + ETH = ETHModule; |
| 29 | + await EVMTransactionStorage.collection.deleteMany({}); |
| 30 | + await WalletStorage.collection.deleteMany({}); |
| 31 | + await WalletAddressStorage.collection.deleteMany({}); |
| 32 | + }); |
| 33 | + |
| 34 | + afterEach(() => { |
| 35 | + globalSandbox.restore(); |
| 36 | + }); |
| 37 | + |
| 38 | + function createMockReqRes() { |
| 39 | + const reqStream = new PassThrough(); |
| 40 | + const req = reqStream as unknown as Request; |
| 41 | + |
| 42 | + const resStream = new PassThrough(); |
| 43 | + const res = resStream as unknown as Response; |
| 44 | + |
| 45 | + (res as any).write = resStream.write.bind(resStream); |
| 46 | + (res as any).end = resStream.end.bind(resStream); |
| 47 | + |
| 48 | + res.type = () => res; |
| 49 | + res.status = () => res; |
| 50 | + res.send = () => res; |
| 51 | + |
| 52 | + // Consume data to keep stream flowing |
| 53 | + resStream.on('data', () => {}); |
| 54 | + |
| 55 | + return { req, res, reqEmitter: reqStream, resEmitter: resStream }; |
| 56 | + } |
| 57 | + |
| 58 | + describe('Cursor Cleanup on Client Disconnect', () => { |
| 59 | + it('should not crash when client disconnects during streamWalletTransactions', async () => { |
| 60 | + const address = '0x7F17aF79AABC4A297A58D389ab5905fEd4Ec9502'; |
| 61 | + const objectId = ObjectId.createFromHexString('60f9abed0e32086bf9903bb5'); |
| 62 | + const wallet = { |
| 63 | + _id: objectId, |
| 64 | + chain, |
| 65 | + network, |
| 66 | + name: 'test-wallet', |
| 67 | + pubKey: '0x029ec2ebdebe6966259cf3c6f35c4f126b82fe072bf9d0e81dad375f1d6d2d9054', |
| 68 | + path: 'm/44\'/60\'/0\'/0/0', |
| 69 | + singleAddress: true |
| 70 | + } as MongoBound<IWallet>; |
| 71 | + |
| 72 | + await WalletStorage.collection.insertOne(wallet as any); |
| 73 | + await WalletAddressStorage.collection.insertOne({ |
| 74 | + chain, |
| 75 | + network, |
| 76 | + wallet: objectId, |
| 77 | + address, |
| 78 | + processed: true |
| 79 | + }); |
| 80 | + |
| 81 | + const txCount = 5; |
| 82 | + const txs = new Array(txCount).fill({}).map((_, i) => ({ |
| 83 | + chain, |
| 84 | + network, |
| 85 | + blockHeight: 1, |
| 86 | + gasPrice: 10 * 1e9, |
| 87 | + data: Buffer.from(''), |
| 88 | + from: address, |
| 89 | + to: '0xRecipient123', |
| 90 | + txid: '0x' + (i + 1).toString(16).padStart(64, '0'), |
| 91 | + wallets: [objectId] |
| 92 | + } as any)); |
| 93 | + |
| 94 | + await EVMTransactionStorage.collection.insertMany(txs); |
| 95 | + |
| 96 | + const { req, res, reqEmitter } = createMockReqRes(); |
| 97 | + |
| 98 | + const streamPromise = ETH.streamWalletTransactions({ |
| 99 | + chain, |
| 100 | + network, |
| 101 | + wallet, |
| 102 | + req, |
| 103 | + res, |
| 104 | + args: {} |
| 105 | + }); |
| 106 | + |
| 107 | + // Wait for stream to start |
| 108 | + await new Promise(resolve => setTimeout(resolve, 100)); |
| 109 | + |
| 110 | + // Simulate client disconnect |
| 111 | + reqEmitter.emit('close'); |
| 112 | + |
| 113 | + // Wait for cleanup |
| 114 | + await new Promise(resolve => setTimeout(resolve, 100)); |
| 115 | + |
| 116 | + // Should not throw - the implementation should handle cleanup gracefully |
| 117 | + await streamPromise.catch(() => {}); |
| 118 | + |
| 119 | + // If we get here without crashing, the test passes |
| 120 | + expect(true).to.be.true; |
| 121 | + }); |
| 122 | + |
| 123 | + it('should handle multiple interrupted requests without accumulating resources', async () => { |
| 124 | + const address = '0x7F17aF79AABC4A297A58D389ab5905fEd4Ec9502'; |
| 125 | + const objectId = ObjectId.createFromHexString('60f9abed0e32086bf9903bb5'); |
| 126 | + const wallet = { |
| 127 | + _id: objectId, |
| 128 | + chain, |
| 129 | + network, |
| 130 | + name: 'test-wallet-multi', |
| 131 | + pubKey: '0x029ec2ebdebe6966259cf3c6f35c4f126b82fe072bf9d0e81dad375f1d6d2d9054', |
| 132 | + path: 'm/44\'/60\'/0\'/0/0', |
| 133 | + singleAddress: true |
| 134 | + } as MongoBound<IWallet>; |
| 135 | + |
| 136 | + await WalletStorage.collection.insertOne(wallet as any); |
| 137 | + await WalletAddressStorage.collection.insertOne({ |
| 138 | + chain, |
| 139 | + network, |
| 140 | + wallet: objectId, |
| 141 | + address, |
| 142 | + processed: true |
| 143 | + }); |
| 144 | + |
| 145 | + const txCount = 5; |
| 146 | + const txs = new Array(txCount).fill({}).map((_, i) => ({ |
| 147 | + chain, |
| 148 | + network, |
| 149 | + blockHeight: 1, |
| 150 | + gasPrice: 10 * 1e9, |
| 151 | + data: Buffer.from(''), |
| 152 | + from: address, |
| 153 | + to: '0xRecipient456', |
| 154 | + txid: '0x' + (i + 100).toString(16).padStart(64, '0'), |
| 155 | + wallets: [objectId] |
| 156 | + } as any)); |
| 157 | + |
| 158 | + await EVMTransactionStorage.collection.insertMany(txs); |
| 159 | + |
| 160 | + // Make 3 requests that all get interrupted |
| 161 | + const numRequests = 3; |
| 162 | + for (let i = 0; i < numRequests; i++) { |
| 163 | + const { req, res, reqEmitter } = createMockReqRes(); |
| 164 | + |
| 165 | + const streamPromise = ETH.streamWalletTransactions({ |
| 166 | + chain, |
| 167 | + network, |
| 168 | + wallet, |
| 169 | + req, |
| 170 | + res, |
| 171 | + args: {} |
| 172 | + }); |
| 173 | + |
| 174 | + await new Promise(resolve => setTimeout(resolve, 50)); |
| 175 | + reqEmitter.emit('close'); |
| 176 | + await new Promise(resolve => setTimeout(resolve, 50)); |
| 177 | + await streamPromise.catch(() => {}); |
| 178 | + } |
| 179 | + |
| 180 | + // If we completed all requests without issues, test passes |
| 181 | + expect(true).to.be.true; |
| 182 | + }); |
| 183 | + }); |
| 184 | + |
| 185 | + describe('Provider Instance Reuse', () => { |
| 186 | + it('should complete streaming without errors', async () => { |
| 187 | + const address = '0x7F17aF79AABC4A297A58D389ab5905fEd4Ec9502'; |
| 188 | + const objectId = ObjectId.createFromHexString('60f9abed0e32086bf9903bb5'); |
| 189 | + const wallet = { |
| 190 | + _id: objectId, |
| 191 | + chain, |
| 192 | + network, |
| 193 | + name: 'test-wallet-provider', |
| 194 | + pubKey: '0x029ec2ebdebe6966259cf3c6f35c4f126b82fe072bf9d0e81dad375f1d6d2d9054', |
| 195 | + path: 'm/44\'/60\'/0\'/0/0', |
| 196 | + singleAddress: true |
| 197 | + } as MongoBound<IWallet>; |
| 198 | + |
| 199 | + await WalletStorage.collection.insertOne(wallet as any); |
| 200 | + await WalletAddressStorage.collection.insertOne({ |
| 201 | + chain, |
| 202 | + network, |
| 203 | + wallet: objectId, |
| 204 | + address, |
| 205 | + processed: true |
| 206 | + }); |
| 207 | + |
| 208 | + const txCount = 5; |
| 209 | + const txs = new Array(txCount).fill({}).map((_, i) => ({ |
| 210 | + chain, |
| 211 | + network, |
| 212 | + blockHeight: 1, |
| 213 | + gasPrice: 10 * 1e9, |
| 214 | + data: Buffer.from(''), |
| 215 | + from: address, |
| 216 | + to: '0xRecipient789', |
| 217 | + txid: '0x' + (i + 200).toString(16).padStart(64, '0'), |
| 218 | + wallets: [objectId] |
| 219 | + } as any)); |
| 220 | + |
| 221 | + await EVMTransactionStorage.collection.insertMany(txs); |
| 222 | + |
| 223 | + const { req, res, resEmitter } = createMockReqRes(); |
| 224 | + |
| 225 | + let receivedTxCount = 0; |
| 226 | + resEmitter.on('data', () => { |
| 227 | + receivedTxCount++; |
| 228 | + }); |
| 229 | + |
| 230 | + await new Promise((resolve, reject) => { |
| 231 | + resEmitter.on('finish', resolve); |
| 232 | + resEmitter.on('error', reject); |
| 233 | + |
| 234 | + ETH.streamWalletTransactions({ |
| 235 | + chain, |
| 236 | + network, |
| 237 | + wallet, |
| 238 | + req, |
| 239 | + res, |
| 240 | + args: {} |
| 241 | + }).catch(reject); |
| 242 | + }); |
| 243 | + |
| 244 | + // Verify that we received some transactions (stream worked) |
| 245 | + expect(receivedTxCount).to.be.greaterThan(0); |
| 246 | + }); |
| 247 | + }); |
| 248 | +}); |
0 commit comments